ShopTravel.php 9.8 KB

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