Admin.php 9.9 KB

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