Admin.php 9.5 KB

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