Admin.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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\common\repositories\store\service\StoreServiceRepository;
  14. use app\controller\merchant\Common;
  15. use crmeb\basic\BaseController;
  16. use think\App;
  17. use think\exception\HttpResponseException;
  18. use think\exception\ValidateException;
  19. use think\facade\Db;
  20. use think\response\Json;
  21. class Admin extends BaseController
  22. {
  23. public function __construct(App $app)
  24. {
  25. parent::__construct($app);
  26. }
  27. /**
  28. * 订单统计
  29. * @param $merId
  30. * @param StoreOrderRepository $repository
  31. * @return Json
  32. * @author wuhaotian
  33. * @email 442384644@qq.com
  34. * @date 2024/7/8
  35. */
  36. public function orderStatistics($merId, StoreOrderRepository $repository)
  37. {
  38. $order = $repository->OrderTitleNumber($merId, null);
  39. $common = app()->make(Common::class);
  40. $data = [];
  41. $data['today'] = $common->mainGroup('today', $merId);
  42. $data['yesterday'] = $common->mainGroup('yesterday', $merId);
  43. $data['month'] = $common->mainGroup('month', $merId);
  44. return app('json')->success(compact('order', 'data'));
  45. }
  46. /**
  47. * 订单详情
  48. * @param $merId
  49. * @param StoreOrderRepository $repository
  50. * @return Json
  51. * @author wuhaotian
  52. * @email 442384644@qq.com
  53. * @date 2024/7/8
  54. */
  55. public function orderDetail($merId, StoreOrderRepository $repository)
  56. {
  57. [$page, $limit] = $this->getPage();
  58. list($start, $stop) = $this->request->params([
  59. ['start', strtotime(date('Y-m'))],
  60. ['stop', time()],
  61. ], true);
  62. if ($start == $stop) return app('json')->fail('参数有误');
  63. if ($start > $stop) {
  64. $middle = $stop;
  65. $stop = $start;
  66. $start = $middle;
  67. }
  68. $where = $this->request->has('start') ? ['dateRange' => compact('start', 'stop')] : [];
  69. $list = $repository->orderGroupNumPage($where, $page, $limit, $merId);
  70. return app('json')->success($list);
  71. }
  72. /**
  73. * 订单列表
  74. * @param $merId
  75. * @param StoreOrderRepository $repository
  76. * @return Json
  77. * @author wuhaotian
  78. * @email 442384644@qq.com
  79. * @date 2024/7/8
  80. */
  81. public function orderList($merId, StoreOrderRepository $repository)
  82. {
  83. [$page, $limit] = $this->getPage();
  84. $where = $this->request->params(['status']);
  85. $where['mer_id'] = $merId;
  86. $where['is_del'] = 0;
  87. return app('json')->success($repository->merchantGetList($where, $page, $limit));
  88. }
  89. /**
  90. * 订单详情
  91. * @param $merId
  92. * @param $id
  93. * @param StoreOrderRepository $repository
  94. * @return Json
  95. * @throws \think\db\exception\DataNotFoundException
  96. * @throws \think\db\exception\DbException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. * @author wuhaotian
  99. * @email 442384644@qq.com
  100. * @date 2024/7/8
  101. */
  102. public function order($merId, $id, StoreOrderRepository $repository)
  103. {
  104. $detail = $repository->getDetail($id);
  105. if (!$detail)
  106. return app('json')->fail('订单不存在');
  107. if ($detail['mer_id'] != $merId)
  108. return app('json')->fail('没有权限');
  109. return app('json')->success($detail->toArray());
  110. }
  111. /**
  112. * 检查订单权限
  113. * @param $merId
  114. * @param $id
  115. * @author wuhaotian
  116. * @email 442384644@qq.com
  117. * @date 2024/7/8
  118. */
  119. protected function checkOrderAuth($merId, $id)
  120. {
  121. if (!app()->make(StoreOrderRepository::class)->existsWhere(['mer_id' => $merId, 'order_id' => $id]))
  122. throw new ValidateException('没有权限');
  123. }
  124. /**
  125. * 订单备注
  126. * @param $merId
  127. * @param $id
  128. * @param StoreOrderRepository $repository
  129. * @return Json
  130. * @author wuhaotian
  131. * @email 442384644@qq.com
  132. * @date 2024/7/8
  133. */
  134. public function mark($merId, $id, StoreOrderRepository $repository)
  135. {
  136. $this->checkOrderAuth($merId, $id);
  137. $data = $this->request->params(['remark']);
  138. $repository->update($id, $data);
  139. return app('json')->success('备注成功');
  140. }
  141. /**
  142. * 订单改价
  143. * @param $merId
  144. * @param $id
  145. * @param StoreOrderRepository $repository
  146. * @return Json
  147. * @author wuhaotian
  148. * @email 442384644@qq.com
  149. * @date 2024/7/8
  150. */
  151. public function price($merId, $id, StoreOrderRepository $repository)
  152. {
  153. $this->checkOrderAuth($merId, $id);
  154. $data = $this->request->params(['total_price', 'pay_postage']);
  155. if ($data['total_price'] < 0 || $data['pay_postage'] < 0)
  156. return app('json')->fail('金额不可未负数');
  157. if (!$repository->merStatusExists((int)$id, $merId))
  158. return app('json')->fail('订单信息或状态错误');
  159. $repository->eidt($id, $data, $this->request->serviceInfo()->service_id);
  160. return app('json')->success('修改成功');
  161. }
  162. /**
  163. * 订单发货
  164. * @param $merId
  165. * @param $id
  166. * @param StoreOrderRepository $repository
  167. * @return Json
  168. * @author wuhaotian
  169. * @email 442384644@qq.com
  170. * @date 2024/7/8
  171. */
  172. public function delivery($merId, $id, StoreOrderRepository $repository)
  173. {
  174. $this->checkOrderAuth($merId, $id);
  175. if (!$repository->merDeliveryExists((int)$id, $merId,1))
  176. return app('json')->fail('订单信息或状态错误');
  177. $type = $this->request->param('delivery_type');
  178. if($type == 4){
  179. if(!systemConfig('crmeb_serve_dump')) return app('json')->fail('电子面单功能未开启');
  180. $params = $this->request->params([
  181. 'delivery_name',
  182. 'from_name',
  183. 'from_tel',
  184. 'from_addr',
  185. 'temp_id',
  186. ]);
  187. $repository->dump($id,$merId,$params);
  188. } else {
  189. $data = $this->request->params([
  190. 'delivery_type',
  191. 'delivery_name',
  192. 'delivery_id',
  193. ]);
  194. if(preg_match('/([\x81-\xfe][\x40-\xfe])/',$data['delivery_id']))
  195. return app('json')->fail('请输入正确的单号/电话');
  196. $repository->delivery($id, $merId, $data);
  197. }
  198. return app('json')->success('发货成功');
  199. }
  200. /**
  201. * 订单金额统计
  202. * @param $merId
  203. * @param StoreOrderRepository $repository
  204. * @return Json
  205. * @author wuhaotian
  206. * @email 442384644@qq.com
  207. * @date 2024/7/8
  208. */
  209. public function payPrice($merId, StoreOrderRepository $repository)
  210. {
  211. list($start, $stop, $month) = $this->request->params([
  212. ['start', strtotime(date('Y-m'))],
  213. ['stop', time()],
  214. 'month'
  215. ], true);
  216. if ($month) {
  217. $start = date('Y/m/d', strtotime(getStartModelTime('month')));
  218. $stop = date('Y/m/d H:i:s', strtotime('+ 1day'));
  219. $front = date('Y/m/d', strtotime('first Day of this month', strtotime('-1 day', strtotime('first Day of this month'))));
  220. $end = date('Y/m/d H:i:s', strtotime($start . ' -1 second'));
  221. } else {
  222. if ($start == $stop) return app('json')->fail('参数有误');
  223. if ($start > $stop) {
  224. $middle = $stop;
  225. $stop = $start;
  226. $start = $middle;
  227. }
  228. $space = bcsub($stop, $start, 0);//间隔时间段
  229. $front = bcsub($start, $space, 0);//第一个时间段
  230. $front = date('Y/m/d H:i:s', $front);
  231. $start = date('Y/m/d H:i:s', $start);
  232. $stop = date('Y/m/d H:i:s', $stop);
  233. $end = date('Y/m/d H:i:s', strtotime($start . ' -1 second'));
  234. }
  235. $frontPrice = $repository->dateOrderPrice($front . '-' . $end, $merId);
  236. $afterPrice = $repository->dateOrderPrice($start . '-' . date('Y/m/d H:i:s', strtotime($stop . '-1 second')), $merId);
  237. $chartInfo = $repository->chartTimePrice($start, date('Y/m/d H:i:s', strtotime($stop . '-1 second')), $merId);
  238. $data['chart'] = $chartInfo;//营业额图表数据
  239. $data['time'] = $afterPrice;//时间区间营业额
  240. $increase = (float)bcsub((string)$afterPrice, (string)$frontPrice, 2); //同比上个时间区间增长营业额
  241. $growthRate = abs($increase);
  242. if ($growthRate == 0) $data['growth_rate'] = 0;
  243. else if ($frontPrice == 0) $data['growth_rate'] = bcmul($growthRate, 100, 0);
  244. else $data['growth_rate'] = (int)bcmul((string)bcdiv((string)$growthRate, (string)$frontPrice, 2), '100', 0);//时间区间增长率
  245. $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
  246. $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
  247. return app('json')->success($data);
  248. }
  249. /**
  250. * 订单数量统计
  251. * @param StoreOrderRepository $repository
  252. * @return Json
  253. * @author xaboy
  254. * @day 2020/8/27
  255. */
  256. public function payNumber($merId, StoreOrderRepository $repository)
  257. {
  258. list($start, $stop, $month) = $this->request->params([
  259. ['start', strtotime(date('Y-m'))],
  260. ['stop', time()],
  261. 'month'
  262. ], true);
  263. if ($month) {
  264. $start = date('Y/m/d', strtotime(getStartModelTime('month')));
  265. $stop = date('Y/m/d H:i:s', strtotime('+ 1day'));
  266. $front = date('Y/m/d', strtotime('first Day of this month', strtotime('-1 day', strtotime('first Day of this month'))));
  267. $end = date('Y/m/d H:i:s', strtotime($start . ' -1 second'));
  268. } else {
  269. if ($start == $stop) return app('json')->fail('参数有误');
  270. if ($start > $stop) {
  271. $middle = $stop;
  272. $stop = $start;
  273. $start = $middle;
  274. }
  275. $space = bcsub($stop, $start, 0);//间隔时间段
  276. $front = bcsub($start, $space, 0);//第一个时间段
  277. $front = date('Y/m/d H:i:s', $front);
  278. $start = date('Y/m/d H:i:s', $start);
  279. $stop = date('Y/m/d H:i:s', $stop);
  280. $end = date('Y/m/d H:i:s', strtotime($start . ' -1 second'));
  281. }
  282. $frontNumber = $repository->dateOrderNum($front . '-' . $end, $merId);
  283. $afterNumber = $repository->dateOrderNum($start . '-' . date('Y/m/d H:i:s', strtotime($stop . '-1 second')), $merId);
  284. $chartInfo = $repository->chartTimeNum($start . '-' . date('Y/m/d H:i:s', strtotime($stop . '-1 second')), $merId);
  285. $data['chart'] = $chartInfo;//订单数图表数据
  286. $data['time'] = $afterNumber;//时间区间订单数
  287. $increase = $afterNumber - $frontNumber; //同比上个时间区间增长订单数
  288. $growthRate = abs($increase);
  289. if ($growthRate == 0) $data['growth_rate'] = 0;
  290. else if ($frontNumber == 0) $data['growth_rate'] = bcmul($growthRate, 100, 0);
  291. else $data['growth_rate'] = (int)bcmul((string)bcdiv((string)$growthRate, (string)$frontNumber, 2), '100', 0);//时间区间增长率
  292. $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
  293. $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
  294. return app('json')->success($data);
  295. }
  296. /**
  297. * 获取信息
  298. * @param $merId
  299. * @return Json
  300. * @author wuhaotian
  301. * @email 442384644@qq.com
  302. * @date 2024/7/8
  303. */
  304. public function getFormData($merId)
  305. {
  306. $config = [
  307. 'mer_from_com',
  308. 'mer_from_name',
  309. 'mer_from_tel',
  310. 'mer_from_addr',
  311. 'mer_config_siid',
  312. 'mer_config_temp_id'
  313. ];
  314. $data = merchantConfig($merId,$config);
  315. return app('json')->success($data);
  316. }
  317. }