ShopHotelCanteen.php 9.9 KB

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