ShopOffline.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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\OfflineShopModel;
  7. use think\Db;
  8. /**
  9. * 示例接口
  10. */
  11. class ShopOffline extends Api
  12. {
  13. protected $noNeedLogin = [''];
  14. protected $noNeedRight = ['*'];
  15. public function home()
  16. {
  17. $user_id = $this->auth->id;
  18. $model = new OfflineShopModel();
  19. $info = $model->getDetail(
  20. params: ['user_id' => $user_id],
  21. select: ['id','invite_id','user_id','name','image','images','back_rate','address']
  22. );
  23. if (!$info){
  24. return $this->error('未开通门店');
  25. }
  26. // 待核销金额
  27. $wait_use = Db::name('bill')->where(['shop_id'=>$info['id'],'table_name'=>'offline_shop_order','status'=>2])->field('sum(total_amount - back_amount) as money')->find();
  28. // 已核销
  29. $used = Db::name('bill')->where(['shop_id'=>$info['id'],'table_name'=>'offline_shop_order','status'=>3])->field('sum(total_amount - back_amount) as money')->find();
  30. // 已退款
  31. $refund = Db::name('bill')->where(['shop_id'=>$info['id'],'table_name'=>'offline_shop_order','status'=>4])->field('sum(total_amount - back_amount) as money')->find();
  32. $statistics = [
  33. 'wait_use' => $wait_use['money'] ?? 0,
  34. 'used' => $used['money'] ?? 0,
  35. 'refund' => $refund['money'] ?? 0,
  36. ];
  37. return $this->success('success',[
  38. 'info' => $info,
  39. 'statistics' => $statistics
  40. ]);
  41. }
  42. // 设置让利比例
  43. public function set_back_rate()
  44. {
  45. $params = $this->request->param();
  46. $user_id = $this->auth->id;
  47. $model = new OfflineShopModel();
  48. $info = $model->getDetail(
  49. params: ['user_id' => $user_id],
  50. select: ['id','invite_id','user_id','name','image','images','back_rate','address']
  51. );
  52. if (!$info){
  53. return $this->error('未开通门店');
  54. }
  55. if (!OfflineShopModel::where('id',$info['id'])->update(['back_rate'=>$params['rate']??20])){
  56. return $this->error('操作失败');
  57. }
  58. return $this->success('设置成功');
  59. }
  60. // 订单列表
  61. public function bill()
  62. {
  63. $params = $this->request->param();
  64. $user_id = $this->auth->id;
  65. $model = new OfflineShopModel();
  66. $info = $model->getDetail(
  67. params: ['user_id' => $user_id],
  68. select: ['id','invite_id','user_id','name','image','images','back_rate','address']
  69. );
  70. if (!$info){
  71. return $this->error('未开通门店');
  72. }
  73. // 全部
  74. if (empty($params['status'])){
  75. $params['status_in'] = [1,2,3];
  76. unset($params['status']);
  77. }
  78. $model = new BillModel();
  79. $list = $model->getList(
  80. params: array_merge(['shop_id'=>$info['id'],'table_name'=>'offline_shop_order'],$params)
  81. );
  82. return $this->success('success',$list);
  83. }
  84. // 订单详情
  85. public function bill_detail()
  86. {
  87. $params = $this->request->param();
  88. $user_id = $this->auth->id;
  89. $model = new OfflineShopModel();
  90. $info = $model->getDetail(
  91. params: ['user_id' => $user_id],
  92. select: ['id','invite_id','user_id','name','image','images','back_rate','address']
  93. );
  94. if (!$info){
  95. return $this->error('未开通门店');
  96. }
  97. // 全部
  98. if (empty($params['status'])){
  99. $params['status_in'] = [1,2,3];
  100. unset($params['status']);
  101. }
  102. $model = new BillModel();
  103. $list = $model->getDetail(
  104. params: array_merge(['shop_id'=>$info['id'],'table_name'=>'offline_shop_order'],$params)
  105. );
  106. return $this->success('success',$list);
  107. }
  108. // 交易统计
  109. public function statistics()
  110. {
  111. $params = $this->request->param();
  112. $user_id = $this->auth->id;
  113. if (empty($params['type'])){
  114. $params['type'] = 1;
  115. }
  116. $model = new OfflineShopModel();
  117. $info = $model->getDetail(
  118. params: ['user_id' => $user_id],
  119. select: ['id','invite_id','user_id','name','image','images','back_rate','address']
  120. );
  121. if (!$info){
  122. return $this->error('未开通门店');
  123. }
  124. switch ($params['type']){
  125. case 1:
  126. // 今天
  127. $createtimeBetween = [
  128. strtotime(date('Y-m-d 00:00:00')),
  129. strtotime(date('Y-m-d 23:59:59')),
  130. ];
  131. break;
  132. case 2:
  133. // 昨天
  134. $createtimeBetween = [
  135. strtotime(date('Y-m-d 00:00:00', strtotime('-1 day'))),
  136. strtotime(date('Y-m-d 23:59:59', strtotime('-1 day'))),
  137. ];
  138. break;
  139. case 3:
  140. // 本月
  141. $createtimeBetween = [
  142. strtotime(date('Y-m-01 00:00:00')),
  143. strtotime(date('Y-m-d 23:59:59')),
  144. ];
  145. break;
  146. default:
  147. $createtimeBetween = [
  148. strtotime(date('Y-m-d 00:00:00'),strtotime($params['date'])),
  149. strtotime(date('Y-m-d 23:59:59'),strtotime($params['date'])),
  150. ];
  151. break;
  152. }
  153. $wait_use = Db::name('bill')
  154. ->where(['shop_id'=>$info['id'],'table_name'=>'offline_shop_order'])
  155. ->whereIn('status',[1,2,3])
  156. ->whereBetween('createtime',$createtimeBetween)
  157. ->field('sum(total_amount - back_amount) as money,count(id) as num')
  158. ->find();
  159. return $this->success('success',[
  160. 'money' => $wait_use['money'] ?? 0,
  161. 'num' => $used['num'] ?? 0,
  162. ]);
  163. }
  164. // 趋势图
  165. public function trend()
  166. {
  167. $user_id = $this->auth->id;
  168. $model = new OfflineShopModel();
  169. $info = $model->getDetail(
  170. params: ['user_id' => $user_id],
  171. select: ['id','invite_id','user_id','name','image','images','back_rate','address']
  172. );
  173. if (!$info){
  174. return $this->error('未开通门店');
  175. }
  176. // 七日数据走势统计
  177. $createtimeBetween = [
  178. strtotime(date('Y-m-d 00:00:00', strtotime('-6 day'))),
  179. strtotime(date('Y-m-d 23:59:59')),
  180. ];
  181. $dates = [];
  182. for ($i = 6; $i >= 0; $i--){
  183. $dates[] = date('Y-m-d', strtotime('-'.$i.' day'));
  184. }
  185. $wait_use = Db::name('bill')
  186. ->field('DATE(FROM_UNIXTIME(createtime)) as order_date,sum(total_amount - back_amount) as money,count(id) as num')
  187. ->where(['shop_id'=>$info['id'],'table_name'=>'offline_shop_order'])
  188. ->whereIn('status',[1,2,3])
  189. ->whereBetween('createtime',$createtimeBetween)
  190. ->group('order_date')
  191. ->order('order_date','asc')
  192. ->select();
  193. $num = [];
  194. $money = [];
  195. foreach ($dates as $key=>$date){
  196. $num[$key] = 0;
  197. $money[$key] = '0';
  198. foreach ($wait_use as $k=>$v){
  199. if ($date == $v['order_date']){
  200. $num[$key] = $v['num'];
  201. $money[$key] = $v['money'];
  202. }
  203. }
  204. $dates[$key] = date('m/d',strtotime($date));
  205. }
  206. return $this->success('success',[
  207. 'money' => $money,
  208. 'num' => $num,
  209. 'dates' => $dates
  210. ]);
  211. }
  212. }