ShopHotel.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace app\api\controller\shop;
  3. use app\common\controller\Api;
  4. use app\common\model\BillModel;
  5. use app\common\model\HotelModel;
  6. use app\common\model\OfflineShopModel;
  7. use app\common\model\OfflineTypeModel;
  8. use app\utils\DataUtil;
  9. use think\Db;
  10. /**
  11. * 示例接口
  12. */
  13. class ShopHotel extends Api
  14. {
  15. protected $noNeedLogin = [''];
  16. protected $noNeedRight = ['*'];
  17. public function home()
  18. {
  19. $user_id = $this->auth->id;
  20. $model = new HotelModel();
  21. $info = $model->getDetail(
  22. params: ['user_id' => $user_id],
  23. select: ['id','invite_id','user_id','name','image','images','back_rate','address']
  24. );
  25. if (!$info){
  26. return $this->error('未开通门店');
  27. }
  28. // 待核销金额
  29. $wait_use = Db::name('bill')->where(['shop_id'=>$info['id'],'table_name'=>'hotel_order','status'=>2])->field('sum(total_amount - back_amount) as money')->find();
  30. // 已核销
  31. $used = Db::name('bill')->where(['shop_id'=>$info['id'],'table_name'=>'hotel_order','status'=>3])->field('sum(total_amount - back_amount) as money')->find();
  32. // 已退款
  33. $refund = Db::name('bill')->where(['shop_id'=>$info['id'],'table_name'=>'hotel_order','status'=>4])->field('sum(total_amount - back_amount) as money')->find();
  34. $statistics = [
  35. 'wait_use' => $wait_use['money'] ?? 0,
  36. 'used' => $used['money'] ?? 0,
  37. 'refund' => $refund['money'] ?? 0,
  38. ];
  39. return $this->success('success',[
  40. 'info' => $info,
  41. 'statistics' => $statistics
  42. ]);
  43. }
  44. // 订单列表
  45. public function bill()
  46. {
  47. $params = $this->request->param();
  48. $user_id = $this->auth->id;
  49. $model = new HotelModel();
  50. $info = $model->getDetail(
  51. params: ['user_id' => $user_id],
  52. select: ['id','invite_id','user_id','name','image','images','back_rate','address']
  53. );
  54. if (!$info){
  55. return $this->error('未开通门店');
  56. }
  57. // 全部
  58. if (empty($params['status'])){
  59. $params['status_in'] = [1,2,3];
  60. unset($params['status']);
  61. }
  62. $model = new BillModel();
  63. $list = $model->getList(
  64. params: array_merge(['shop_id'=>$info['id'],'table_name'=>'hotel_order'],$params),
  65. with: [
  66. 'hotelorder' => function ($query) {
  67. $query->field('id,hotel_id,room_id,name,phone');
  68. }
  69. ]
  70. );
  71. return $this->success('success',$list);
  72. }
  73. // 交易统计
  74. public function statistics()
  75. {
  76. $params = $this->request->param();
  77. $user_id = $this->auth->id;
  78. if (empty($params['type'])){
  79. $params['type'] = 1;
  80. }
  81. $model = new HotelModel();
  82. $info = $model->getDetail(
  83. params: ['user_id' => $user_id],
  84. select: ['id','invite_id','user_id','name','image','images','back_rate','address']
  85. );
  86. if (!$info){
  87. return $this->error('未开通门店');
  88. }
  89. switch ($params['type']){
  90. case 1:
  91. // 今天
  92. $createtimeBetween = [
  93. strtotime(date('Y-m-d 00:00:00')),
  94. strtotime(date('Y-m-d 23:59:59')),
  95. ];
  96. break;
  97. case 2:
  98. // 昨天
  99. $createtimeBetween = [
  100. strtotime(date('Y-m-d 00:00:00', strtotime('-1 day'))),
  101. strtotime(date('Y-m-d 23:59:59', strtotime('-1 day'))),
  102. ];
  103. break;
  104. case 3:
  105. // 本月
  106. $createtimeBetween = [
  107. strtotime(date('Y-m-01 00:00:00')),
  108. strtotime(date('Y-m-d 23:59:59')),
  109. ];
  110. break;
  111. default:
  112. $createtimeBetween = [
  113. strtotime(date('Y-m-d 00:00:00'),strtotime($params['date'])),
  114. strtotime(date('Y-m-d 23:59:59'),strtotime($params['date'])),
  115. ];
  116. break;
  117. }
  118. $wait_use = Db::name('bill')
  119. ->where(['shop_id'=>$info['id'],'table_name'=>'hotel_order'])
  120. ->whereIn('status',[1,2,3])
  121. ->whereBetween('createtime',$createtimeBetween)
  122. ->field('sum(total_amount - back_amount) as money,count(id) as num')
  123. ->find();
  124. return $this->success('success',[
  125. 'money' => $wait_use['money'] ?? 0,
  126. 'num' => $used['num'] ?? 0,
  127. ]);
  128. }
  129. // 趋势图
  130. public function trend()
  131. {
  132. $user_id = $this->auth->id;
  133. $model = new HotelModel();
  134. $info = $model->getDetail(
  135. params: ['user_id' => $user_id],
  136. select: ['id','invite_id','user_id','name','image','images','back_rate','address']
  137. );
  138. if (!$info){
  139. return $this->error('未开通门店');
  140. }
  141. // 七日数据走势统计
  142. $createtimeBetween = [
  143. strtotime(date('Y-m-d 00:00:00', strtotime('-6 day'))),
  144. strtotime(date('Y-m-d 23:59:59')),
  145. ];
  146. $dates = [];
  147. for ($i = 6; $i >= 0; $i--){
  148. $dates[] = date('Y-m-d', strtotime('-'.$i.' day'));
  149. }
  150. $wait_use = Db::name('bill')
  151. ->field('DATE(FROM_UNIXTIME(createtime)) as order_date,sum(total_amount - back_amount) as money,count(id) as num')
  152. ->where(['shop_id'=>$info['id'],'table_name'=>'hotel_order'])
  153. ->whereIn('status',[1,2,3])
  154. ->whereBetween('createtime',$createtimeBetween)
  155. ->group('order_date')
  156. ->order('order_date','asc')
  157. ->select();
  158. $num = [];
  159. $money = [];
  160. foreach ($dates as $key=>$date){
  161. $num[$key] = 0;
  162. $money[$key] = '0';
  163. foreach ($wait_use as $k=>$v){
  164. if ($date == $v['order_date']){
  165. $num[$key] = $v['num'];
  166. $money[$key] = $v['money'];
  167. }
  168. }
  169. $dates[$key] = date('m/d',strtotime($date));
  170. }
  171. return $this->success('success',[
  172. 'money' => $money,
  173. 'num' => $num,
  174. 'dates' => $dates
  175. ]);
  176. }
  177. }