Admin.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020/7/20
  7. *
  8. *
  9. */
  10. namespace app\controller\api\user;
  11. use app\common\repositories\store\order\StoreOrderRepository;
  12. use app\controller\merchant\Common;
  13. use crmeb\basic\BaseController;
  14. use think\App;
  15. use think\exception\HttpResponseException;
  16. use think\exception\ValidateException;
  17. use think\facade\Db;
  18. class Admin extends BaseController
  19. {
  20. protected $merId;
  21. public function __construct(App $app)
  22. {
  23. parent::__construct($app);
  24. $user = $this->request->userInfo();
  25. if (isset($user->service->customer) && $user->service->customer == 1) {
  26. $this->merId = $user->service->mer_id;
  27. } else {
  28. throw new HttpResponseException(app('json')->fail('没有权限'));
  29. }
  30. }
  31. public function orderStatistics(StoreOrderRepository $repository)
  32. {
  33. $order = $repository->merchantOrderNumber($this->merId);
  34. $common = app()->make(Common::class);
  35. $data = [];
  36. $data['today'] = $common->mainGroup('today', $this->merId);
  37. $data['yesterday'] = $common->mainGroup('yesterday', $this->merId);
  38. $data['month'] = $common->mainGroup('month', $this->merId);
  39. return app('json')->success(compact('order', 'data'));
  40. }
  41. public function orderDetail(StoreOrderRepository $repository)
  42. {
  43. [$page, $limit] = $this->getPage();
  44. list($start, $stop) = $this->request->params([
  45. ['start', strtotime(date('Y-m'))],
  46. ['stop', time()],
  47. ], true);
  48. $where = $this->request->has('start') ? ['dateRange' => compact('start', 'stop')] : [];
  49. $list = $repository->orderGroupNumPage($where, $page, $limit, $this->merId);
  50. return app('json')->success($list);
  51. }
  52. public function orderList(StoreOrderRepository $repository)
  53. {
  54. [$page, $limit] = $this->getPage();
  55. $where = $this->request->params(['status']);
  56. $where['mer_id'] = $this->merId;
  57. $where['is_del'] = 0;
  58. return app('json')->success($repository->merchantGetList($where, $page, $limit));
  59. }
  60. public function order($id, StoreOrderRepository $repository)
  61. {
  62. $detail = $repository->getDetail($id);
  63. if (!$detail)
  64. return app('json')->fail('订单不存在');
  65. if ($detail['mer_id'] != $this->merId)
  66. return app('json')->fail('没有权限');
  67. return app('json')->success($detail->toArray());
  68. }
  69. protected function checkOrderAuth($id)
  70. {
  71. if (!app()->make(StoreOrderRepository::class)->existsWhere(['mer_id' => $this->merId, 'order_id' => $id]))
  72. throw new ValidateException('没有权限');
  73. }
  74. public function mark($id, StoreOrderRepository $repository)
  75. {
  76. $this->checkOrderAuth($id);
  77. $data = $this->request->params(['remark']);
  78. $repository->update($id, $data);
  79. return app('json')->success('备注成功');
  80. }
  81. public function price($id, StoreOrderRepository $repository)
  82. {
  83. $this->checkOrderAuth($id);
  84. $data = $this->request->params(['pay_price']);
  85. if ($data['pay_price'] < 0)
  86. app('json')->fail('支付金额不能小于0');
  87. if (!$repository->merStatusExists((int)$id, $this->merId))
  88. return app('json')->fail('订单信息或状态错误');
  89. $repository->eidt($id, $data);
  90. return app('json')->success('修改成功');
  91. }
  92. public function delivery($id, StoreOrderRepository $repository)
  93. {
  94. $this->checkOrderAuth($id);
  95. if (!$repository->merDeliveryExists((int)$id, $this->merId))
  96. return app('json')->fail('订单信息或状态错误');
  97. $data = $this->request->params(['delivery_type', 'delivery_name', 'delivery_id']);
  98. $repository->delivery($id, $data);
  99. return app('json')->success('发货成功');
  100. }
  101. public function payPrice(StoreOrderRepository $repository)
  102. {
  103. list($start, $stop) = $this->request->params([
  104. ['start', strtotime(date('Y-m'))],
  105. ['stop', time()]
  106. ], true);
  107. if ($start == $stop) return app('json')->fail('参数有误');
  108. if ($start > $stop) {
  109. $middle = $stop;
  110. $stop = $start;
  111. $start = $middle;
  112. }
  113. $space = bcsub($stop, $start, 0);//间隔时间段
  114. $front = bcsub($start, $space, 0);//第一个时间段
  115. $front = date('Y/m/d H:i:s', $front);
  116. $start = date('Y/m/d H:i:s', $start);
  117. $stop = date('Y/m/d H:i:s', $stop);
  118. $frontPrice = $repository->dateOrderPrice($front . '-' . $start, $this->merId);
  119. $afterPrice = $repository->dateOrderPrice($start . '-' . $stop, $this->merId);
  120. $chartInfo = $repository->chartTimePrice($start, $stop, $this->merId);
  121. $data['chart'] = $chartInfo;//营业额图表数据
  122. $data['time'] = $afterPrice;//时间区间营业额
  123. $increase = (float)bcsub((string)$afterPrice, (string)$frontPrice, 2); //同比上个时间区间增长营业额
  124. $growthRate = abs($increase);
  125. if ($growthRate == 0) $data['growth_rate'] = 0;
  126. else if ($frontPrice == 0) $data['growth_rate'] = $growthRate;
  127. else $data['growth_rate'] = (int)bcmul((string)bcdiv((string)$growthRate, (string)$frontPrice, 2), '100', 0);//时间区间增长率
  128. $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
  129. $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
  130. return app('json')->success($data);
  131. }
  132. /**
  133. * @param StoreOrderRepository $repository
  134. * @return \think\response\Json
  135. * @author xaboy
  136. * @day 2020/8/27
  137. */
  138. public function payNumber(StoreOrderRepository $repository)
  139. {
  140. list($start, $stop) = $this->request->params([
  141. ['start', strtotime(date('Y-m'))],
  142. ['stop', time()]
  143. ], true);
  144. if ($start == $stop) return app('json')->fail('参数有误');
  145. if ($start > $stop) {
  146. $middle = $stop;
  147. $stop = $start;
  148. $start = $middle;
  149. }
  150. $space = bcsub($stop, $start, 0);//间隔时间段
  151. $front = bcsub($start, $space, 0);//第一个时间段
  152. $front = date('Y/m/d H:i:s', $front);
  153. $start = date('Y/m/d H:i:s', $start);
  154. $stop = date('Y/m/d H:i:s', $stop);
  155. $frontNumber = $repository->dateOrderNum($front . '-' . $start, $this->merId);
  156. $afterNumber = $repository->dateOrderNum($start . '-' . $stop, $this->merId);
  157. $chartInfo = $repository->chartTimeNum($start, $stop, $this->merId);
  158. $data['chart'] = $chartInfo;//订单数图表数据
  159. $data['time'] = $afterNumber;//时间区间订单数
  160. $increase = $afterNumber - $frontNumber; //同比上个时间区间增长订单数
  161. $growthRate = abs($increase);
  162. if ($growthRate == 0) $data['growth_rate'] = 0;
  163. else if ($frontNumber == 0) $data['growth_rate'] = $growthRate;
  164. else $data['growth_rate'] = (int)bcmul((string)bcdiv((string)$growthRate, (string)$frontNumber, 2), '100', 0);//时间区间增长率
  165. $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
  166. $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
  167. return app('json')->success($data);
  168. }
  169. }