ShopHotel.php 9.2 KB

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