Order.php 13 KB

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