Order.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. namespace app\api\controller\inspection;
  3. use app\api\controller\inspection\Base;
  4. use app\common\Service\OrderService;
  5. use app\common\Enum\OrderEnum;
  6. use app\common\Service\OrderActionService;
  7. use app\common\Enum\OrderActionEnum;
  8. /**
  9. * 验货端订单接口
  10. *
  11. * 包含功能:
  12. * - 订单详情查询
  13. * - 订单列表查询
  14. * - 订单发货确认
  15. * - 订单统计功能(按日/月/年统计)
  16. * - 订单趋势数据
  17. *
  18. * 统计功能说明:
  19. * - 验收订单:已验货通过的订单
  20. * - 退回订单:验货不通过的订单
  21. * - 售后订单:退款相关的订单
  22. */
  23. class Order extends Base
  24. {
  25. protected $noNeedLogin = [];
  26. //订单详情
  27. public function detail()
  28. {
  29. // 验证请求参数
  30. $validate = new \app\api\validate\Order();
  31. $orderId = $this->request->get('orderId');
  32. $params = ['orderId' => $orderId];
  33. if (!$validate->scene('detail')->check($params)) {
  34. $this->error($validate->getError());
  35. }
  36. $order = OrderService::getDetail($orderId, $this->auth->id);
  37. if (empty($order)) {
  38. $this->error('未找到订单');
  39. }
  40. //$order->append(['order_status_text']);
  41. $address = OrderService::getAddressInfo($orderId);
  42. $order->address = $address;
  43. // $order->append(['status_text']);
  44. // $order->hidden(explode(',', 'method,transactionid,updatetime,deletetime'));
  45. // $order->expiretime = $order->expiretime - time();
  46. $order->order_status_text = OrderEnum::STATUS_TEXT_MAP[$order->order_status];
  47. $this->success('', $order);
  48. }
  49. //订单列表
  50. public function index()
  51. {
  52. // 验证请求参数
  53. $validate = new \app\api\validate\inspection\Order();
  54. $param = $this->request->param();
  55. $param['time_range'] = '1'; // 添加触发时间范围验证的字段
  56. if (!$validate->scene('lists')->check($param)) {
  57. $this->error($validate->getError());
  58. }
  59. // 设置默认值
  60. $userId = $this->auth->id;
  61. $param['page'] = $param['page'] ?? 1;
  62. $param['page_size'] = $param['page_size'] ?? 10;
  63. $status = $param['status'] ?? 0; // 默认为0(全部订单)
  64. $param['keywords'] = $param['keywords'] ?? '';
  65. $status = OrderEnum::SHOW_TYPE_STATUS_MAP[$status];
  66. // 查询对应的工厂 信息
  67. $supplierId = $this->application->supplier_id;
  68. $list = OrderService::getOrderList($userId ,$param, $status,$supplierId);
  69. foreach ($list as $item) {
  70. // $item->append(['order_status_text']);
  71. $field = 'id,order_sn,amount,goods_price,order_amount,express_name,express_no,order_goods,order_status_text,order_status';
  72. $item->visible(explode(',', $field));
  73. $item->order_status_text = OrderEnum::STATUS_TEXT_MAP[$item->order_status];
  74. }
  75. $this->success('获取成功', $list);
  76. }
  77. //确认发货
  78. public function send()
  79. {
  80. // 验证请求参数
  81. $validate = new \app\api\validate\Order();
  82. $orderId = $this->request->post('orderId');
  83. $params = ['orderId' => $orderId];
  84. if (!$validate->scene('detail')->check($params)) {
  85. $this->error($validate->getError());
  86. }
  87. $order = OrderService::getByOrderId($orderId);
  88. if (empty($order)) {
  89. $this->error('订单不存在');
  90. }
  91. if ($order->user_id != $this->auth->id) {
  92. $this->error('该订单不属于当前用户');
  93. }
  94. $expressname = $this->request->post('expressname');
  95. $expressno = $this->request->post('expressno');
  96. $order = OrderService::getDetail($orderId, $this->auth->id);
  97. if (empty($order)) {
  98. $this->error('未找到订单');
  99. }
  100. //发货 / 修改快递信息
  101. if ($order->order_status == OrderEnum::STATUS_INSPECTION_PASS) {
  102. $oldStatus = $order->order_status;
  103. $order->express_name = $expressname;
  104. $order->express_no = $expressno;
  105. $order->order_status = OrderEnum::STATUS_SHIP;
  106. $order->shipping_time = time();
  107. $order->save();
  108. // 记录管理员操作
  109. OrderActionService::recordAdminAction(
  110. $order->order_sn,
  111. OrderActionEnum::ACTION_SHIP,
  112. '验货端',
  113. '验货通过,发货,快递公司:' . $expressname . ',快递单号:' . $expressno,
  114. $this->auth->id ?? 0,
  115. [
  116. 'express_name' => $expressname,
  117. 'express_no' => $expressno,
  118. ]
  119. );
  120. $this->success('发货成功');
  121. }else{
  122. $this->error('订单不允许确认收货');
  123. }
  124. }
  125. // 统计订单
  126. public function statistics()
  127. {
  128. // 验证请求参数
  129. $validate = new \app\api\validate\Order();
  130. $params = [
  131. 'type' => $this->request->param('type', 'day'),
  132. 'date' => $this->request->param('date', date('Y-m-d'))
  133. ];
  134. if (!$validate->scene('statistics')->check($params)) {
  135. $this->error($validate->getError());
  136. }
  137. // 获取验证后的参数
  138. $type = $params['type'];
  139. $date = $params['date'];
  140. // 根据类型处理日期和时间范围
  141. switch ($type) {
  142. case 'day':
  143. $startTime = strtotime($date . ' 00:00:00');
  144. $endTime = strtotime($date . ' 23:59:59');
  145. $displayDate = date('m-d', $startTime);
  146. // 前一天
  147. $prevStartTime = strtotime($date . ' 00:00:00') - 86400;
  148. $prevEndTime = strtotime($date . ' 23:59:59') - 86400;
  149. break;
  150. case 'month':
  151. $startTime = strtotime(date('Y-m-01 00:00:00', strtotime($date)));
  152. $endTime = strtotime(date('Y-m-t 23:59:59', strtotime($date)));
  153. $displayDate = date('Y-m', $startTime);
  154. // 前一个月
  155. $prevStartTime = strtotime(date('Y-m-01 00:00:00', strtotime($date . ' -1 month')));
  156. $prevEndTime = strtotime(date('Y-m-t 23:59:59', strtotime($date . ' -1 month')));
  157. break;
  158. case 'year':
  159. $startTime = strtotime(date('Y-01-01 00:00:00', strtotime($date)));
  160. $endTime = strtotime(date('Y-12-31 23:59:59', strtotime($date)));
  161. $displayDate = date('Y', $startTime) . '年';
  162. // 前一年
  163. $prevStartTime = strtotime(date('Y-01-01 00:00:00', strtotime($date . ' -1 year')));
  164. $prevEndTime = strtotime(date('Y-12-31 23:59:59', strtotime($date . ' -1 year')));
  165. break;
  166. }
  167. // 获取当前用户ID
  168. $userId = $this->auth->id;
  169. // 如果需要根据工厂统计,可以在这里添加工厂相关逻辑
  170. // 目前先按用户统计,后续可以根据实际的工厂关联字段进行调整
  171. // 当前期间统计
  172. $currentStats = $this->getOrderStatistics($userId, $startTime, $endTime);
  173. // 上期统计(用于对比)
  174. $prevStats = $this->getOrderStatistics($userId, $prevStartTime, $prevEndTime);
  175. // 计算增长率
  176. $growthRate = [
  177. 'inspection_rate' => $this->calculateGrowthRate($prevStats['inspection_count'], $currentStats['inspection_count']),
  178. 'return_rate' => $this->calculateGrowthRate($prevStats['return_count'], $currentStats['return_count']),
  179. 'aftersale_rate' => $this->calculateGrowthRate($prevStats['aftersale_count'], $currentStats['aftersale_count'])
  180. ];
  181. // 返回统计数据
  182. $data = [
  183. 'date' => $displayDate,
  184. 'type' => $type,
  185. 'current_period' => $currentStats,
  186. 'previous_period' => $prevStats,
  187. 'growth_rate' => $growthRate,
  188. 'statistics' => [
  189. 'inspection_count' => $currentStats['inspection_count'],
  190. 'return_count' => $currentStats['return_count'],
  191. 'aftersale_count' => $currentStats['aftersale_count']
  192. ]
  193. ];
  194. $this->success('统计数据获取成功', $data);
  195. }
  196. /**
  197. * 获取订单统计数据
  198. */
  199. private function getOrderStatistics($userId, $startTime, $endTime)
  200. {
  201. // 统计验收订单(验货通过)
  202. $inspectionCount = \app\common\model\Order::where('user_id', $userId)
  203. ->where('order_status', OrderEnum::STATUS_INSPECTION_PASS)
  204. ->where('createtime', '>=', $startTime)
  205. ->where('createtime', '<=', $endTime)
  206. ->count();
  207. // 统计退回订单(验货不通过)
  208. $returnCount = \app\common\model\Order::where('user_id', $userId)
  209. ->where('order_status', OrderEnum::STATUS_INSPECTION_FAIL)
  210. ->where('createtime', '>=', $startTime)
  211. ->where('createtime', '<=', $endTime)
  212. ->count();
  213. // 统计售后订单(退款相关)
  214. $aftersaleCount = \app\common\model\Order::where('user_id', $userId)
  215. ->whereIn('order_status', [OrderEnum::STATUS_REFUND, OrderEnum::STATUS_REFUND_CONFIRM])
  216. ->where('createtime', '>=', $startTime)
  217. ->where('createtime', '<=', $endTime)
  218. ->count();
  219. return [
  220. 'inspection_count' => $inspectionCount,
  221. 'return_count' => $returnCount,
  222. 'aftersale_count' => $aftersaleCount
  223. ];
  224. }
  225. /**
  226. * 计算增长率
  227. */
  228. private function calculateGrowthRate($prevValue, $currentValue)
  229. {
  230. if ($prevValue == 0) {
  231. return $currentValue > 0 ? 100 : 0;
  232. }
  233. return round((($currentValue - $prevValue) / $prevValue) * 100, 2);
  234. }
  235. /**
  236. * 获取统计数据的时间范围选择
  237. */
  238. public function getStatisticsDateRange()
  239. {
  240. // 获取最早和最晚的订单时间
  241. $userId = $this->auth->id;
  242. $earliest = \app\common\model\Order::where('user_id', $userId)
  243. ->order('createtime', 'asc')
  244. ->value('createtime');
  245. $latest = \app\common\model\Order::where('user_id', $userId)
  246. ->order('createtime', 'desc')
  247. ->value('createtime');
  248. $data = [
  249. 'earliest_date' => $earliest ? date('Y-m-d', $earliest) : date('Y-m-d'),
  250. 'latest_date' => $latest ? date('Y-m-d', $latest) : date('Y-m-d'),
  251. 'current_date' => date('Y-m-d'),
  252. 'current_month' => date('Y-m'),
  253. 'current_year' => date('Y')
  254. ];
  255. $this->success('日期范围获取成功', $data);
  256. }
  257. /**
  258. * 获取历史趋势数据
  259. */
  260. public function getTrendData()
  261. {
  262. // 验证请求参数
  263. $validate = new \app\api\validate\Order();
  264. $params = [
  265. 'type' => $this->request->param('type', 'day'),
  266. 'days' => $this->request->param('days', 7)
  267. ];
  268. if (!$validate->scene('trend')->check($params)) {
  269. $this->error($validate->getError());
  270. }
  271. // 获取验证后的参数
  272. $type = $params['type'];
  273. $days = $params['days'];
  274. $userId = $this->auth->id;
  275. $trendData = [];
  276. for ($i = $days - 1; $i >= 0; $i--) {
  277. $date = date('Y-m-d', strtotime("-{$i} days"));
  278. $startTime = strtotime($date . ' 00:00:00');
  279. $endTime = strtotime($date . ' 23:59:59');
  280. $stats = $this->getOrderStatistics($userId, $startTime, $endTime);
  281. $trendData[] = [
  282. 'date' => $date,
  283. 'display_date' => date('m-d', $startTime),
  284. 'inspection_count' => $stats['inspection_count'],
  285. 'return_count' => $stats['return_count'],
  286. 'aftersale_count' => $stats['aftersale_count']
  287. ];
  288. }
  289. $this->success('趋势数据获取成功', $trendData);
  290. }
  291. }