ShopHotel.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 bill_detail()
  75. {
  76. $params = $this->request->param();
  77. $user_id = $this->auth->id;
  78. $model = new HotelModel();
  79. $info = $model->getDetail(
  80. params: ['user_id' => $user_id],
  81. select: ['id','invite_id','user_id','name','image','images','back_rate','address']
  82. );
  83. if (!$info){
  84. return $this->error('未开通门店');
  85. }
  86. // 全部
  87. if (empty($params['status'])){
  88. $params['status_in'] = [1,2,3];
  89. unset($params['status']);
  90. }
  91. $model = new BillModel();
  92. $list = $model->getDetail(
  93. params: array_merge(['shop_id'=>$info['id'],'table_name'=>'hotel_order'],$params),
  94. with: [
  95. 'hotelorder' => function ($query) {
  96. $query->field('id,hotel_id,room_id,name,phone');
  97. }
  98. ]
  99. );
  100. return $this->success('success',$list);
  101. }
  102. // 交易统计
  103. public function statistics()
  104. {
  105. $params = $this->request->param();
  106. $user_id = $this->auth->id;
  107. if (empty($params['type'])){
  108. $params['type'] = 1;
  109. }
  110. $model = new HotelModel();
  111. $info = $model->getDetail(
  112. params: ['user_id' => $user_id],
  113. select: ['id','invite_id','user_id','name','image','images','back_rate','address']
  114. );
  115. if (!$info){
  116. return $this->error('未开通门店');
  117. }
  118. switch ($params['type']){
  119. case 1:
  120. // 今天
  121. $createtimeBetween = [
  122. strtotime(date('Y-m-d 00:00:00')),
  123. strtotime(date('Y-m-d 23:59:59')),
  124. ];
  125. break;
  126. case 2:
  127. // 昨天
  128. $createtimeBetween = [
  129. strtotime(date('Y-m-d 00:00:00', strtotime('-1 day'))),
  130. strtotime(date('Y-m-d 23:59:59', strtotime('-1 day'))),
  131. ];
  132. break;
  133. case 3:
  134. // 本月
  135. $createtimeBetween = [
  136. strtotime(date('Y-m-01 00:00:00')),
  137. strtotime(date('Y-m-d 23:59:59')),
  138. ];
  139. break;
  140. default:
  141. $createtimeBetween = [
  142. strtotime(date('Y-m-d 00:00:00'),strtotime($params['date'])),
  143. strtotime(date('Y-m-d 23:59:59'),strtotime($params['date'])),
  144. ];
  145. break;
  146. }
  147. $wait_use = Db::name('bill')
  148. ->where(['shop_id'=>$info['id'],'table_name'=>'hotel_order'])
  149. ->whereIn('status',[1,2,3])
  150. ->whereBetween('createtime',$createtimeBetween)
  151. ->field('sum(total_amount - back_amount) as money,count(id) as num')
  152. ->find();
  153. return $this->success('success',[
  154. 'money' => $wait_use['money'] ?? 0,
  155. 'num' => $used['num'] ?? 0,
  156. ]);
  157. }
  158. // 趋势图
  159. public function trend()
  160. {
  161. $user_id = $this->auth->id;
  162. $model = new HotelModel();
  163. $info = $model->getDetail(
  164. params: ['user_id' => $user_id],
  165. select: ['id','invite_id','user_id','name','image','images','back_rate','address']
  166. );
  167. if (!$info){
  168. return $this->error('未开通门店');
  169. }
  170. // 七日数据走势统计
  171. $createtimeBetween = [
  172. strtotime(date('Y-m-d 00:00:00', strtotime('-6 day'))),
  173. strtotime(date('Y-m-d 23:59:59')),
  174. ];
  175. $dates = [];
  176. for ($i = 6; $i >= 0; $i--){
  177. $dates[] = date('Y-m-d', strtotime('-'.$i.' day'));
  178. }
  179. $wait_use = Db::name('bill')
  180. ->field('DATE(FROM_UNIXTIME(createtime)) as order_date,sum(total_amount - back_amount) as money,count(id) as num')
  181. ->where(['shop_id'=>$info['id'],'table_name'=>'hotel_order'])
  182. ->whereIn('status',[1,2,3])
  183. ->whereBetween('createtime',$createtimeBetween)
  184. ->group('order_date')
  185. ->order('order_date','asc')
  186. ->select();
  187. $num = [];
  188. $money = [];
  189. foreach ($dates as $key=>$date){
  190. $num[$key] = 0;
  191. $money[$key] = '0';
  192. foreach ($wait_use as $k=>$v){
  193. if ($date == $v['order_date']){
  194. $num[$key] = $v['num'];
  195. $money[$key] = $v['money'];
  196. }
  197. }
  198. $dates[$key] = date('m/d',strtotime($date));
  199. }
  200. return $this->success('success',[
  201. 'money' => $money,
  202. 'num' => $num,
  203. 'dates' => $dates
  204. ]);
  205. }
  206. }