Order.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. <?php
  2. namespace app\store\model\sharing;
  3. use app\common\model\sharing\Order as OrderModel;
  4. use app\store\model\User as UserModel;
  5. use app\store\model\UserCoupon as UserCouponModel;
  6. use app\store\service\order\Export as Exportservice;
  7. use app\common\library\helper;
  8. use app\common\enum\OrderType as OrderTypeEnum;
  9. use app\common\enum\DeliveryType as DeliveryTypeEnum;
  10. use app\common\service\Message as MessageService;
  11. use app\common\service\order\Refund as RefundService;
  12. /**
  13. * 拼团订单模型
  14. * Class Order
  15. * @package app\store\model\sharing
  16. */
  17. class Order extends OrderModel
  18. {
  19. /**
  20. * 订单列表
  21. * @param string $dataType
  22. * @param array $query
  23. * @return \think\Paginator
  24. * @throws \think\exception\DbException
  25. */
  26. public function getList($dataType, $query = [])
  27. {
  28. // 检索查询条件
  29. !empty($query) && $this->setWhere($query);
  30. // 获取数据列表
  31. return $this->with(['active', 'goods.image', 'address', 'user'])
  32. ->alias('order')
  33. ->field('order.*, active.status as active_status')
  34. ->join('user', 'user.user_id = order.user_id', 'LEFT')
  35. ->join('sharing_active active', 'order.active_id = active.active_id', 'LEFT')
  36. ->where($this->transferDataType($dataType))
  37. ->where('order.is_delete', '=', 0)
  38. ->order(['order.create_time' => 'desc'])
  39. ->paginate(10, false, [
  40. 'query' => \request()->request()
  41. ]);
  42. }
  43. /**
  44. * 订单列表(全部)
  45. * @param $dataType
  46. * @param array $query
  47. * @return false|\PDOStatement|string|\think\Collection
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. * @throws \think\exception\DbException
  51. */
  52. public function getListAll($dataType, $query = [])
  53. {
  54. // 检索查询条件
  55. !empty($query) && $this->setWhere($query);
  56. // 获取数据列表
  57. return $this->with(['goods.image', 'address', 'user', 'extract', 'extract_shop'])
  58. ->alias('order')
  59. ->field('order.*, active.status as active_status')
  60. ->join('sharing_active active', 'order.active_id = active.active_id', 'LEFT')
  61. ->where($this->transferDataType($dataType))
  62. ->where('order.is_delete', '=', 0)
  63. ->order(['create_time' => 'desc'])
  64. ->select();
  65. }
  66. /**
  67. * 订单导出
  68. * @param $dataType
  69. * @param $query
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. * @throws \think\exception\DbException
  73. */
  74. public function exportList($dataType, $query)
  75. {
  76. // 获取订单列表
  77. $list = $this->getListAll($dataType, $query);
  78. // 导出csv文件
  79. return (new Exportservice)->orderList($list);
  80. }
  81. /**
  82. * 批量发货模板
  83. */
  84. public function deliveryTpl()
  85. {
  86. return (new Exportservice)->deliveryTpl();
  87. }
  88. /**
  89. * 设置检索查询条件
  90. * @param $query
  91. */
  92. private function setWhere($query)
  93. {
  94. if (isset($query['search']) && !empty($query['search'])) {
  95. $this->where('order_no|user.nickName', 'like', '%' . trim($query['search']) . '%');
  96. }
  97. if (isset($query['start_time']) && !empty($query['start_time'])) {
  98. $this->where('order.create_time', '>=', strtotime($query['start_time']));
  99. }
  100. if (isset($query['end_time']) && !empty($query['end_time'])) {
  101. $this->where('order.create_time', '<', strtotime($query['end_time']) + 86400);
  102. }
  103. if (isset($query['active_id']) && $query['active_id'] > 0) {
  104. $this->where('order.active_id', '=', (int)$query['active_id']);
  105. }
  106. if (isset($query['delivery_type']) && !empty($query['delivery_type'])) {
  107. $query['delivery_type'] > -1 && $this->where('delivery_type', '=', $query['delivery_type']);
  108. }
  109. if (isset($query['extract_shop_id']) && !empty($query['extract_shop_id'])) {
  110. $query['extract_shop_id'] > -1 && $this->where('extract_shop_id', '=', $query['extract_shop_id']);
  111. }
  112. }
  113. /**
  114. * 转义数据类型条件
  115. * @param $dataType
  116. * @return array
  117. */
  118. private function transferDataType($dataType)
  119. {
  120. // 数据类型
  121. $filter = [];
  122. switch ($dataType) {
  123. case 'all':
  124. // 全部
  125. $filter = [];
  126. break;
  127. case 'pay':
  128. // 待支付
  129. $filter = ['pay_status' => 10, 'order_status' => 10];
  130. break;
  131. case 'sharing';
  132. // 拼团中
  133. $filter['active.status'] = 10;
  134. break;
  135. case 'sharing_succeed';
  136. // 拼团成功
  137. $filter['active.status'] = 20;
  138. break;
  139. case 'sharing_fail';
  140. // 拼团失败
  141. $filter['active.status'] = 30;
  142. break;
  143. case 'delivery':
  144. // 待发货
  145. $this->where('IF ( (`order`.`order_type` = 20), (`active`.`status` = 20), TRUE)');
  146. $filter = [
  147. 'pay_status' => 20,
  148. 'delivery_status' => 10,
  149. 'order_status' => ['in', [10, 21]]
  150. ];
  151. break;
  152. case 'receipt':
  153. // 待收货
  154. $filter = [
  155. 'pay_status' => 20,
  156. 'delivery_status' => 20,
  157. 'receipt_status' => 10
  158. ];
  159. break;
  160. case 'complete':
  161. // 已完成
  162. $filter = ['order_status' => 30];
  163. break;
  164. case 'cancel':
  165. // 已取消
  166. $filter = ['order_status' => 20];
  167. break;
  168. }
  169. return $filter;
  170. }
  171. /**
  172. * 确认发货(单独订单)
  173. * @param $data
  174. * @return array|bool|false
  175. * @throws \think\Exception
  176. * @throws \think\exception\DbException
  177. * @throws \Exception
  178. */
  179. public function delivery($data)
  180. {
  181. // 转义为订单列表
  182. $orderList = [$this];
  183. // 验证订单是否满足发货条件
  184. if (!$this->verifyDelivery($orderList)) {
  185. return false;
  186. }
  187. // 整理更新的数据
  188. $updateList = [[
  189. 'order_id' => $this['order_id'],
  190. 'express_id' => $data['express_id'],
  191. 'express_no' => $data['express_no']
  192. ]];
  193. // 更新订单发货状态
  194. if ($status = $this->updateToDelivery($updateList)) {
  195. // 获取已发货的订单
  196. $completed = self::detail($this['order_id'], ['user', 'address', 'goods', 'express']);
  197. // 发送消息通知
  198. $this->sendDeliveryMessage([$completed]);
  199. }
  200. return $status;
  201. }
  202. /**
  203. * 批量发货
  204. * @param $data
  205. * @return bool
  206. * @throws \think\db\exception\DataNotFoundException
  207. * @throws \think\db\exception\ModelNotFoundException
  208. * @throws \think\exception\DbException
  209. * @throws \Exception
  210. */
  211. public function batchDelivery($data)
  212. {
  213. // 获取csv文件中的数据
  214. if (!$csvData = $this->getCsvData()) {
  215. return false;
  216. }
  217. // 整理订单id集
  218. $orderNos = helper::getArrayColumn($csvData, 0);
  219. // 获取订单列表数据
  220. $orderList = helper::arrayColumn2Key($this->getListByOrderNos($orderNos), 'order_no');
  221. // 验证订单是否存在
  222. $tempArr = array_values(array_diff($orderNos, array_keys($orderList)));
  223. if (!empty($tempArr)) {
  224. $this->error = "订单号[{$tempArr[0]}] 不存在!";
  225. return false;
  226. }
  227. // 整理物流单号
  228. $updateList = [];
  229. foreach ($csvData as $item) {
  230. $updateList[] = [
  231. 'order_id' => $orderList[$item[0]]['order_id'],
  232. 'express_id' => $data['express_id'],
  233. 'express_no' => $item[1],
  234. ];
  235. }
  236. // 验证订单是否满足发货条件
  237. if (!$this->verifyDelivery($orderList)) {
  238. return false;
  239. }
  240. // 更新订单发货状态(批量)
  241. if ($status = $this->updateToDelivery($updateList)) {
  242. // 获取已发货的订单
  243. $completed = $this->getListByOrderNos($orderNos, ['user', 'address', 'goods', 'express']);
  244. // 发送消息通知
  245. $this->sendDeliveryMessage($completed);
  246. }
  247. return $status;
  248. }
  249. /**
  250. * 确认发货后发送消息通知
  251. * @param $orderList
  252. * @return bool
  253. */
  254. private function sendDeliveryMessage($orderList)
  255. {
  256. // 发送消息通知
  257. foreach ($orderList as $item) {
  258. MessageService::send('order.delivery', [
  259. 'order' => $item,
  260. 'order_type' => OrderTypeEnum::SHARING,
  261. ]);
  262. }
  263. return true;
  264. }
  265. /**
  266. * 更新订单发货状态(批量)
  267. * @param $orderList
  268. * @return array|false
  269. * @throws \Exception
  270. */
  271. private function updateToDelivery($orderList)
  272. {
  273. $data = [];
  274. foreach ($orderList as $item) {
  275. $data[] = [
  276. 'order_id' => $item['order_id'],
  277. 'express_no' => $item['express_no'],
  278. 'express_id' => $item['express_id'],
  279. 'delivery_status' => 20,
  280. 'delivery_time' => time(),
  281. ];
  282. }
  283. return $this->isUpdate()->saveAll($data);
  284. }
  285. /**
  286. * 验证订单是否满足发货条件
  287. * @param $orderList
  288. * @return bool
  289. */
  290. private function verifyDelivery($orderList)
  291. {
  292. foreach ($orderList as $order) {
  293. if (
  294. $order['pay_status']['value'] != 20
  295. || $order['delivery_type']['value'] != DeliveryTypeEnum::EXPRESS
  296. || $order['delivery_status']['value'] != 10
  297. || (
  298. // 拼团订单验证拼单状态
  299. $order['order_type']['value'] == 20
  300. && $order['active']['status']['value'] != 20
  301. )
  302. ) {
  303. $this->error = "订单号[{$order['order_no']}] 不满足发货条件!";
  304. return false;
  305. }
  306. }
  307. return true;
  308. }
  309. /**
  310. * 获取csv文件中的数据
  311. * @return array|bool
  312. */
  313. private function getCsvData()
  314. {
  315. // 获取表单上传文件 例如上传了001.jpg
  316. $file = \request()->file('iFile');
  317. if (empty($file)) {
  318. $this->error = '请上传发货模板';
  319. return false;
  320. }
  321. // 设置区域信息
  322. setlocale(LC_ALL, 'zh_CN');
  323. // 打开上传的文件
  324. $csvFile = fopen($file->getInfo()['tmp_name'], 'r');
  325. // 忽略第一行(csv标题)
  326. fgetcsv($csvFile);
  327. // 遍历并记录订单信息
  328. $orderList = [];
  329. while ($item = fgetcsv($csvFile)) {
  330. if (!isset($item[0]) || empty($item[0]) || !isset($item[1]) || empty($item[1])) {
  331. $this->error = '模板文件数据不合法';
  332. return false;
  333. }
  334. $orderList[] = $item;
  335. }
  336. if (empty($orderList)) {
  337. $this->error = '模板文件中没有订单数据';
  338. return false;
  339. }
  340. return $orderList;
  341. }
  342. /**
  343. * 修改订单价格
  344. * @param $data
  345. * @return bool
  346. */
  347. public function updatePrice($data)
  348. {
  349. if ($this['pay_status']['value'] != 10) {
  350. $this->error = '该订单不合法';
  351. return false;
  352. }
  353. // 实际付款金额
  354. $payPrice = bcadd($data['update_price'], $data['update_express_price'], 2);
  355. if ($payPrice <= 0) {
  356. $this->error = '订单实付款价格不能为0.00元';
  357. return false;
  358. }
  359. return $this->save([
  360. 'order_no' => $this->orderNo(), // 修改订单号, 否则微信支付提示重复
  361. 'order_price' => $data['update_price'],
  362. 'pay_price' => $payPrice,
  363. 'update_price' => helper::bcsub($data['update_price'], helper::bcsub($this['total_price'], $this['coupon_money'])),
  364. 'express_price' => $data['update_express_price']
  365. ]) !== false;
  366. }
  367. /**
  368. * 审核:用户取消订单
  369. * @param $data
  370. * @return bool
  371. */
  372. public function confirmCancel($data)
  373. {
  374. // 判断订单是否有效
  375. if ($this['pay_status']['value'] != 20) {
  376. $this->error = '该订单不合法';
  377. return false;
  378. }
  379. // 订单取消事件
  380. return $this->transaction(function () use ($data) {
  381. if ($data['is_cancel'] == true) {
  382. // 执行退款操作
  383. (new RefundService)->execute($this);
  384. // 回退商品库存
  385. (new OrderGoods)->backGoodsStock($this['goods'], true);
  386. // 回退用户优惠券
  387. $this['coupon_id'] > 0 && UserCouponModel::setIsUse($this['coupon_id'], false);
  388. // 回退用户积分
  389. $User = UserModel::detail($this['user_id']);
  390. $describe = "订单取消:{$this['order_no']}";
  391. $this['points_num'] > 0 && $User->setIncPoints($this['points_num'], $describe);
  392. }
  393. // 更新订单状态
  394. return $this->save(['order_status' => $data['is_cancel'] ? 20 : 10]);
  395. });
  396. }
  397. /**
  398. * 拼团失败手动退款
  399. * @return bool|false|int
  400. * @throws \app\common\exception\BaseException
  401. * @throws \think\Exception
  402. * @throws \think\exception\DbException
  403. */
  404. public function refund()
  405. {
  406. if (
  407. $this['order_type']['value'] != 20
  408. || $this['pay_status']['value'] != 20
  409. || $this['active']['status']['value'] != 30
  410. || $this['is_refund'] == 1
  411. ) {
  412. $this->error = '该订单不合法';
  413. return false;
  414. }
  415. // 执行退款操作
  416. (new RefundService)->execute($this);
  417. // 更新订单状态
  418. return $this->save(['order_status' => 20, 'is_refund' => 1]);
  419. }
  420. /**
  421. * 获取已付款订单总数 (可指定某天)
  422. * @param null $day
  423. * @return int|string
  424. * @throws \think\Exception
  425. */
  426. public function getPayOrderTotal($day = null)
  427. {
  428. $filter = ['pay_status' => 20];
  429. if (!is_null($day)) {
  430. $startTime = strtotime($day);
  431. $filter['pay_time'] = [
  432. ['>=', $startTime],
  433. ['<', $startTime + 86400],
  434. ];
  435. }
  436. return $this->getOrderTotal($filter);
  437. }
  438. /**
  439. * 获取订单总数量
  440. * @param array $filter
  441. * @return int|string
  442. * @throws \think\Exception
  443. */
  444. public function getOrderTotal($filter = [])
  445. {
  446. return $this->where($filter)
  447. ->where('is_delete', '=', 0)
  448. ->count();
  449. }
  450. /**
  451. * 获取某天的总销售额
  452. * @param $day
  453. * @return float|int
  454. */
  455. public function getOrderTotalPrice($day)
  456. {
  457. $startTime = strtotime($day);
  458. return $this->where('pay_time', '>=', $startTime)
  459. ->where('pay_time', '<', $startTime + 86400)
  460. ->where('pay_status', '=', 20)
  461. ->where('is_delete', '=', 0)
  462. ->sum('pay_price');
  463. }
  464. /**
  465. * 获取某天的下单用户数
  466. * @param $day
  467. * @return float|int
  468. */
  469. public function getPayOrderUserTotal($day)
  470. {
  471. $startTime = strtotime($day);
  472. $userIds = $this->distinct(true)
  473. ->where('pay_time', '>=', $startTime)
  474. ->where('pay_time', '<', $startTime + 86400)
  475. ->where('pay_status', '=', 20)
  476. ->where('is_delete', '=', 0)
  477. ->column('user_id');
  478. return count($userIds);
  479. }
  480. }