ShopHotelCanteen.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. namespace app\api\controller\shop;
  3. use app\common\business\ShopWalletBusiness;
  4. use app\common\controller\Api;
  5. use app\common\model\BillModel;
  6. use app\common\model\HotelCanteenModel;
  7. use app\common\model\HotelCanteenOrderModel;
  8. use app\common\model\HotelModel;
  9. use app\common\model\HotelOrderModel;
  10. use app\common\model\OfflineShopModel;
  11. use app\common\model\OfflineTypeModel;
  12. use app\utils\DataUtil;
  13. use think\Db;
  14. /**
  15. * 示例接口
  16. */
  17. class ShopHotelCanteen extends Api
  18. {
  19. protected $noNeedLogin = [''];
  20. protected $noNeedRight = ['*'];
  21. public function home()
  22. {
  23. $user_id = $this->auth->id;
  24. $model = new HotelCanteenModel();
  25. $info = $model->getDetail(
  26. params: ['user_id' => $user_id],
  27. select: ['id','invite_id','user_id','name','image','images','back_rate','address']
  28. );
  29. if (!$info){
  30. return $this->error('未开通门店');
  31. }
  32. // 待核销金额
  33. $wait_use = Db::name('bill')->where(['shop_id'=>$info['id'],'table_name'=>'hotel_canteen_order','status'=>2])->field('sum(total_amount - back_amount) as money')->find();
  34. // 已核销
  35. $used = Db::name('bill')->where(['shop_id'=>$info['id'],'table_name'=>'hotel_canteen_order','status'=>3])->field('sum(total_amount - back_amount) as money')->find();
  36. // 已退款
  37. $refund = Db::name('bill')->where(['shop_id'=>$info['id'],'table_name'=>'hotel_canteen_order','status'=>4])->field('sum(total_amount - back_amount) as money')->find();
  38. $statistics = [
  39. 'wait_use' => $wait_use['money'] ?? 0,
  40. 'used' => $used['money'] ?? 0,
  41. 'refund' => $refund['money'] ?? 0,
  42. ];
  43. return $this->success('success',[
  44. 'info' => $info,
  45. 'statistics' => $statistics
  46. ]);
  47. }
  48. // 订单列表
  49. public function bill()
  50. {
  51. $params = $this->request->param();
  52. $user_id = $this->auth->id;
  53. $model = new HotelCanteenModel();
  54. $info = $model->getDetail(
  55. params: ['user_id' => $user_id],
  56. select: ['id','invite_id','user_id','name','image','images','back_rate','address']
  57. );
  58. if (!$info){
  59. return $this->error('未开通门店');
  60. }
  61. // 全部
  62. if (empty($params['status'])){
  63. $params['status_in'] = [1,2,3];
  64. unset($params['status']);
  65. }
  66. $model = new BillModel();
  67. $list = $model->getList(
  68. params: array_merge(['shop_id'=>$info['id'],'table_name'=>'hotel_canteen_order'],$params),
  69. with: [
  70. 'hotelcanteenorder' => function ($query) {
  71. $query->field('id,hotel_canteen_id,room_id,name,phone');
  72. }
  73. ]
  74. );
  75. return $this->success('success',$list);
  76. }
  77. // 订单详情
  78. public function bill_detail()
  79. {
  80. $params = $this->request->param();
  81. $user_id = $this->auth->id;
  82. $model = new HotelCanteenModel();
  83. $info = $model->getDetail(
  84. params: ['user_id' => $user_id],
  85. select: ['id','invite_id','user_id','name','image','images','back_rate','address']
  86. );
  87. if (!$info){
  88. return $this->error('未开通门店');
  89. }
  90. // 全部
  91. if (empty($params['status'])){
  92. $params['status_in'] = [1,2,3];
  93. unset($params['status']);
  94. }
  95. $model = new BillModel();
  96. $list = $model->getDetail(
  97. params: array_merge(['shop_id'=>$info['id'],'table_name'=>'hotel_canteen_order'],$params),
  98. with: [
  99. 'hotelcanteenorder' => function ($query) {
  100. $query->field('id,hotel_canteen_id,room_id,name,phone');
  101. }
  102. ]
  103. );
  104. return $this->success('success',$list);
  105. }
  106. // 酒店订单核销
  107. public function deal()
  108. {
  109. $params = $this->request->param();
  110. $user_id = $this->auth->id;
  111. $model = new HotelCanteenModel();
  112. $info = $model->getDetail(
  113. params: ['user_id' => $user_id],
  114. select: ['id','invite_id','user_id','name','image','images','back_rate','address']
  115. );
  116. if (!$info){
  117. return $this->error('未开通门店');
  118. }
  119. $params['status_in'] = [1,2,3];
  120. $model = new BillModel();
  121. $order = $model->getDetail(
  122. params: array_merge(['shop_id'=>$info['id'],'table_name'=>'hotel_canteen_order'],$params),
  123. with: [
  124. 'hotelcanteenorder' => function ($query) {
  125. $query->field('id,hotel_canteen_id,room_id,name,phone');
  126. }
  127. ]
  128. );
  129. if (empty($order)){
  130. return $this->error('未找到该订单');
  131. }
  132. if ($params['status'] == 3){
  133. return $this->error('订单已核销');
  134. }
  135. if ($params['status'] != 2){
  136. return $this->error('订单状态错误');
  137. }
  138. Db::startTrans();
  139. // 更改订单状态
  140. BillModel::where('id',$order['id'])->update(['status'=>3,'back_status' => 1,'back_time' => time()]);
  141. HotelCanteenOrderModel::where('id',$order['table_id'])->update(['is_use'=>1,'update_time' => time()]);
  142. // 发放商家收益
  143. $money = bcsub($order['total_amount'],$order['back_amount'],2);// 除去让利后的金额
  144. $wallet = new ShopWalletBusiness('hotel_canteen_order');
  145. if (!$wallet->change($info['id'],$money,'money',ShopWalletBusiness::log_type[40],'订单核销','bill',$order['id'])){
  146. Db::rollback();
  147. return $this->error($wallet->getMessage(),$wallet->getData());
  148. }
  149. Db::commit();
  150. return $this->success('操作成功');
  151. }
  152. // 交易统计
  153. public function statistics()
  154. {
  155. $params = $this->request->param();
  156. $user_id = $this->auth->id;
  157. if (empty($params['type'])){
  158. $params['type'] = 1;
  159. }
  160. $model = new HotelCanteenModel();
  161. $info = $model->getDetail(
  162. params: ['user_id' => $user_id],
  163. select: ['id','invite_id','user_id','name','image','images','back_rate','address']
  164. );
  165. if (!$info){
  166. return $this->error('未开通门店');
  167. }
  168. switch ($params['type']){
  169. case 1:
  170. // 今天
  171. $createtimeBetween = [
  172. strtotime(date('Y-m-d 00:00:00')),
  173. strtotime(date('Y-m-d 23:59:59')),
  174. ];
  175. break;
  176. case 2:
  177. // 昨天
  178. $createtimeBetween = [
  179. strtotime(date('Y-m-d 00:00:00', strtotime('-1 day'))),
  180. strtotime(date('Y-m-d 23:59:59', strtotime('-1 day'))),
  181. ];
  182. break;
  183. case 3:
  184. // 本月
  185. $createtimeBetween = [
  186. strtotime(date('Y-m-01 00:00:00')),
  187. strtotime(date('Y-m-d 23:59:59')),
  188. ];
  189. break;
  190. default:
  191. $createtimeBetween = [
  192. strtotime(date('Y-m-d 00:00:00'),strtotime($params['date'])),
  193. strtotime(date('Y-m-d 23:59:59'),strtotime($params['date'])),
  194. ];
  195. break;
  196. }
  197. $wait_use = Db::name('bill')
  198. ->where(['shop_id'=>$info['id'],'table_name'=>'hotel_canteen_order'])
  199. ->whereIn('status',[1,2,3])
  200. ->whereBetween('createtime',$createtimeBetween)
  201. ->field('sum(total_amount - back_amount) as money,count(id) as num')
  202. ->find();
  203. return $this->success('success',[
  204. 'money' => $wait_use['money'] ?? 0,
  205. 'num' => $used['num'] ?? 0,
  206. ]);
  207. }
  208. // 趋势图
  209. public function trend()
  210. {
  211. $user_id = $this->auth->id;
  212. $model = new HotelCanteenModel();
  213. $info = $model->getDetail(
  214. params: ['user_id' => $user_id],
  215. select: ['id','invite_id','user_id','name','image','images','back_rate','address']
  216. );
  217. if (!$info){
  218. return $this->error('未开通门店');
  219. }
  220. // 七日数据走势统计
  221. $createtimeBetween = [
  222. strtotime(date('Y-m-d 00:00:00', strtotime('-6 day'))),
  223. strtotime(date('Y-m-d 23:59:59')),
  224. ];
  225. $dates = [];
  226. for ($i = 6; $i >= 0; $i--){
  227. $dates[] = date('Y-m-d', strtotime('-'.$i.' day'));
  228. }
  229. $wait_use = Db::name('bill')
  230. ->field('DATE(FROM_UNIXTIME(createtime)) as order_date,sum(total_amount - back_amount) as money,count(id) as num')
  231. ->where(['shop_id'=>$info['id'],'table_name'=>'hotel_canteen_order'])
  232. ->whereIn('status',[1,2,3])
  233. ->whereBetween('createtime',$createtimeBetween)
  234. ->group('order_date')
  235. ->order('order_date','asc')
  236. ->select();
  237. $num = [];
  238. $money = [];
  239. foreach ($dates as $key=>$date){
  240. $num[$key] = 0;
  241. $money[$key] = '0';
  242. foreach ($wait_use as $k=>$v){
  243. if ($date == $v['order_date']){
  244. $num[$key] = $v['num'];
  245. $money[$key] = $v['money'];
  246. }
  247. }
  248. $dates[$key] = date('m/d',strtotime($date));
  249. }
  250. return $this->success('success',[
  251. 'money' => $money,
  252. 'num' => $num,
  253. 'dates' => $dates
  254. ]);
  255. }
  256. }