Order.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2025 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\store\model;
  13. use app\common\model\Order as OrderModel;
  14. use app\common\service\Order as OrderService;
  15. use app\common\service\order\Refund as RefundService;
  16. use app\common\service\order\Printer as PrinterService;
  17. use app\common\service\order\PaySuccess as OrderPaySuccesService;
  18. use app\common\enum\order\{
  19. DataType as DataTypeEnum,
  20. PayStatus as PayStatusEnum,
  21. OrderStatus as OrderStatusEnum,
  22. ReceiptStatus as ReceiptStatusEnum,
  23. DeliveryStatus as DeliveryStatusEnum
  24. };
  25. use app\common\enum\payment\Method as PaymentMethod;
  26. use app\common\library\helper;
  27. use cores\exception\BaseException;
  28. /**
  29. * 订单管理
  30. * Class Order
  31. * @package app\store\model
  32. */
  33. class Order extends OrderModel
  34. {
  35. /**
  36. * 订单详情页数据
  37. * @param int $orderId
  38. * @return Order|array|null
  39. */
  40. public function getDetail(int $orderId)
  41. {
  42. return static::detail($orderId, [
  43. 'user', 'address', 'express',
  44. 'goods.image',
  45. 'delivery' => ['goods', 'express'],
  46. 'trade',
  47. ]);
  48. }
  49. /**
  50. * 订单列表
  51. * @param array $param
  52. * @return mixed
  53. */
  54. public function getList(array $param = [])
  55. {
  56. // 检索查询条件
  57. $filter = $this->getQueryFilter($param);
  58. // 设置订单类型条件
  59. $dataTypeFilter = $this->getFilterDataType($param['dataType']);
  60. // 获取数据列表
  61. return $this->with(['goods.image', 'user.avatar', 'address'])
  62. ->alias('order')
  63. ->field('order.*')
  64. ->leftJoin('user', 'user.user_id = order.user_id')
  65. ->leftJoin('order_address address', 'address.order_id = order.order_id')
  66. ->where($dataTypeFilter)
  67. ->where($filter)
  68. ->where('order.is_delete', '=', 0)
  69. ->order(['order.create_time' => 'desc'])
  70. ->paginate(10);
  71. }
  72. /**
  73. * 订单列表(全部)
  74. * @param array $param
  75. * @return iterable|\think\model\Collection|\think\Paginator
  76. */
  77. public function getListAll(array $param = [])
  78. {
  79. // 检索查询条件
  80. $queryFilter = $this->getQueryFilter($param);
  81. // 设置订单类型条件
  82. $dataTypeFilter = $this->getFilterDataType($param['dataType']);
  83. // 获取数据列表
  84. return $this->with(['goods.image', 'address', 'user.avatar', 'express', 'trade'])
  85. ->alias('order')
  86. ->field('order.*')
  87. ->join('user', 'user.user_id = order.user_id')
  88. ->where($dataTypeFilter)
  89. ->where($queryFilter)
  90. ->where('order.is_delete', '=', 0)
  91. ->order(['order.create_time' => 'desc'])
  92. ->select();
  93. }
  94. /**
  95. * 设置检索查询条件
  96. * @param array $param
  97. * @return array
  98. */
  99. private function getQueryFilter(array $param): array
  100. {
  101. // 默认参数
  102. $params = $this->setQueryDefaultValue($param, [
  103. 'searchType' => '', // 关键词类型 (10订单号 20会员昵称 30会员ID 40收货人姓名 50收货人电话)
  104. 'searchValue' => '', // 关键词内容
  105. 'orderSource' => -1, // 订单来源
  106. 'payMethod' => '', // 支付方式
  107. 'deliveryType' => -1, // 配送方式
  108. 'betweenTime' => [], // 起止时间
  109. 'userId' => 0, // 会员ID
  110. ]);
  111. // 检索查询条件
  112. $filter = [];
  113. // 关键词
  114. if (!empty($params['searchValue'])) {
  115. $searchWhere = [
  116. 10 => ['order.order_no', 'like', "%{$params['searchValue']}%"],
  117. 20 => ['user.nick_name', 'like', "%{$params['searchValue']}%"],
  118. 30 => ['order.user_id', '=', (int)$params['searchValue']],
  119. 40 => ['address.name', 'like', "%{$params['searchValue']}%"],
  120. 50 => ['address.phone', 'like', "%{$params['searchValue']}%"],
  121. ];
  122. array_key_exists($params['searchType'], $searchWhere) && $filter[] = $searchWhere[$params['searchType']];
  123. }
  124. // 起止时间
  125. if (!empty($params['betweenTime'])) {
  126. $times = between_time($params['betweenTime']);
  127. $filter[] = ['order.create_time', '>=', $times['start_time']];
  128. $filter[] = ['order.create_time', '<', $times['end_time'] + 86400];
  129. }
  130. // 订单来源
  131. $params['orderSource'] > -1 && $filter[] = ['order_source', '=', (int)$params['orderSource']];
  132. // 支付方式
  133. !empty($params['payMethod']) && $filter[] = ['pay_method', '=', $params['payMethod']];
  134. // 配送方式
  135. $params['deliveryType'] > -1 && $filter[] = ['delivery_type', '=', (int)$params['deliveryType']];
  136. // 会员ID
  137. $params['userId'] > 0 && $filter[] = ['order.user_id', '=', (int)$params['userId']];
  138. return $filter;
  139. }
  140. /**
  141. * 设置订单类型条件
  142. * @param string $dataType
  143. * @return array
  144. */
  145. private function getFilterDataType(string $dataType): array
  146. {
  147. // 数据类型
  148. $filter = [];
  149. switch ($dataType) {
  150. case DataTypeEnum::ALL:
  151. break;
  152. case DataTypeEnum::PAY:
  153. $filter[] = ['pay_status', '=', PayStatusEnum::PENDING];
  154. $filter[] = ['order_status', '=', OrderStatusEnum::NORMAL];
  155. break;
  156. case DataTypeEnum::DELIVERY:
  157. $filter = [
  158. ['pay_status', '=', PayStatusEnum::SUCCESS],
  159. ['delivery_status', '<>', DeliveryStatusEnum::DELIVERED],
  160. ['order_status', 'in', [OrderStatusEnum::NORMAL, OrderStatusEnum::APPLY_CANCEL]]
  161. ];
  162. break;
  163. case DataTypeEnum::RECEIPT:
  164. $filter = [
  165. ['pay_status', '=', PayStatusEnum::SUCCESS],
  166. ['delivery_status', '=', DeliveryStatusEnum::DELIVERED],
  167. ['receipt_status', '=', ReceiptStatusEnum::NOT_RECEIVED]
  168. ];
  169. break;
  170. case DataTypeEnum::COMPLETE:
  171. $filter[] = ['order_status', '=', OrderStatusEnum::COMPLETED];
  172. break;
  173. case DataTypeEnum::APPLY_CANCEL:
  174. $filter[] = ['order_status', '=', OrderStatusEnum::APPLY_CANCEL];
  175. break;
  176. case DataTypeEnum::CANCEL:
  177. $filter[] = ['order_status', '=', OrderStatusEnum::CANCELLED];
  178. break;
  179. }
  180. return $filter;
  181. }
  182. /**
  183. * 修改订单价格
  184. * @param array $data
  185. * @return bool
  186. */
  187. public function updatePrice(array $data): bool
  188. {
  189. if ($this['pay_status'] != PayStatusEnum::PENDING) {
  190. $this->error = '该订单不合法';
  191. return false;
  192. }
  193. // 实际付款金额
  194. $payPrice = helper::bcadd($data['order_price'], $data['express_price']);
  195. if ($payPrice <= 0) {
  196. $this->error = '订单实付款价格不能为0.00元';
  197. return false;
  198. }
  199. // 改价的金额差价
  200. $updatePrice = helper::bcsub($data['order_price'], $this['order_price']);
  201. // 更新订单记录
  202. return $this->save([
  203. 'order_price' => $data['order_price'],
  204. 'pay_price' => $payPrice,
  205. 'update_price' => $updatePrice,
  206. 'express_price' => $data['express_price']
  207. ]) !== false;
  208. }
  209. /**
  210. * 修改商家备注
  211. * @param array $data
  212. * @return bool
  213. */
  214. public function updateRemark(array $data): bool
  215. {
  216. return $this->save(['merchant_remark' => $data['content'] ?? '']);
  217. }
  218. /**
  219. * 小票打印
  220. * @param array $data
  221. * @return bool
  222. * @throws BaseException
  223. * @throws \think\db\exception\DataNotFoundException
  224. * @throws \think\db\exception\DbException
  225. * @throws \think\db\exception\ModelNotFoundException
  226. */
  227. public function printer(array $data): bool
  228. {
  229. // 实例化打印机驱动
  230. $Printer = new PrinterService;
  231. // 手动打印小票
  232. $status = $Printer->printEvent($this, $data['printerId']);
  233. if ($status === false) {
  234. $this->error = $Printer->getError();
  235. }
  236. return $status;
  237. }
  238. /**
  239. * 审核:用户取消订单
  240. * @param array $data
  241. * @return bool|mixed
  242. */
  243. public function confirmCancel(array $data)
  244. {
  245. // 判断订单是否有效
  246. if (
  247. $this['pay_status'] != PayStatusEnum::SUCCESS
  248. || $this['order_status'] != OrderStatusEnum::APPLY_CANCEL
  249. ) {
  250. $this->error = '该订单不合法';
  251. return false;
  252. }
  253. // 订单取消事件
  254. return $this->transaction(function () use ($data) {
  255. if ($data['status']) {
  256. // 执行退款操作
  257. if (!(new RefundService)->handle($this)) {
  258. throwError('执行订单退款失败');
  259. }
  260. // 订单取消事件
  261. OrderService::cancelEvent($this);
  262. }
  263. // 更新订单状态
  264. return $this->save([
  265. 'order_status' => $data['status'] ? OrderStatusEnum::CANCELLED : OrderStatusEnum::NORMAL
  266. ]);
  267. });
  268. }
  269. /**
  270. * 将订单记录设置为已删除
  271. * @return bool
  272. */
  273. public function setDelete(): bool
  274. {
  275. return $this->save(['is_delete' => 1]);
  276. }
  277. /**
  278. * 获取已付款订单总数 (可指定某天)
  279. * @param null $startDate
  280. * @param null $endDate
  281. * @return int
  282. */
  283. public function getPayOrderTotal($startDate = null, $endDate = null): int
  284. {
  285. $filter = [
  286. ['pay_status', '=', PayStatusEnum::SUCCESS],
  287. ['order_status', '<>', OrderStatusEnum::CANCELLED]
  288. ];
  289. if (!is_null($startDate) && !is_null($endDate)) {
  290. $filter[] = ['pay_time', '>=', strtotime($startDate)];
  291. $filter[] = ['pay_time', '<', strtotime($endDate) + 86400];
  292. }
  293. return $this->getOrderTotal($filter);
  294. }
  295. /**
  296. * 获取未发货订单数量
  297. * @return int
  298. */
  299. public function getNotDeliveredOrderTotal(): int
  300. {
  301. $filter = [
  302. ['pay_status', '=', PayStatusEnum::SUCCESS],
  303. ['delivery_status', '<>', DeliveryStatusEnum::DELIVERED],
  304. ['order_status', 'in', [OrderStatusEnum::NORMAL, OrderStatusEnum::APPLY_CANCEL]]
  305. ];
  306. return $this->getOrderTotal($filter);
  307. }
  308. /**
  309. * 获取未付款订单数量
  310. * @return int
  311. */
  312. public function getNotPayOrderTotal(): int
  313. {
  314. $filter = [
  315. ['pay_status', '=', PayStatusEnum::PENDING],
  316. ['order_status', '=', OrderStatusEnum::NORMAL]
  317. ];
  318. return $this->getOrderTotal($filter);
  319. }
  320. /**
  321. * 获取订单总数
  322. * @param array $filter
  323. * @return int
  324. */
  325. private function getOrderTotal(array $filter = []): int
  326. {
  327. // 获取订单总数量
  328. return $this->where($filter)
  329. ->where('is_delete', '=', 0)
  330. ->count();
  331. }
  332. /**
  333. * 获取某天的总销售额
  334. * @param null $startDate
  335. * @param null $endDate
  336. * @return float
  337. */
  338. public function getOrderTotalPrice($startDate = null, $endDate = null): float
  339. {
  340. // 查询对象
  341. $query = $this->getNewQuery();
  342. // 设置查询条件
  343. if (!is_null($startDate) && !is_null($endDate)) {
  344. $query->where('pay_time', '>=', strtotime($startDate))
  345. ->where('pay_time', '<', strtotime($endDate) + 86400);
  346. }
  347. // 总销售额
  348. return $query->where('pay_status', '=', PayStatusEnum::SUCCESS)
  349. ->where('order_status', '<>', OrderStatusEnum::CANCELLED)
  350. ->where('is_delete', '=', 0)
  351. ->sum('pay_price');
  352. }
  353. /**
  354. * 获取某天的下单用户数
  355. * @param string $day
  356. * @return float|int
  357. */
  358. public function getPayOrderUserTotal(string $day)
  359. {
  360. $startTime = strtotime($day);
  361. return $this->field('user_id')
  362. ->where('pay_time', '>=', $startTime)
  363. ->where('pay_time', '<', $startTime + 86400)
  364. ->where('pay_status', '=', PayStatusEnum::SUCCESS)
  365. ->where('is_delete', '=', '0')
  366. ->group('user_id')
  367. ->count();
  368. }
  369. /**
  370. * 根据订单号获取ID集
  371. * @param array $orderNoArr
  372. * @return array
  373. * @throws \think\db\exception\DataNotFoundException
  374. * @throws \think\db\exception\DbException
  375. * @throws \think\db\exception\ModelNotFoundException
  376. */
  377. public static function getOrderIds(array $orderNoArr): array
  378. {
  379. $list = (new static)->where('order_no', 'in', $orderNoArr)->select();
  380. $data = [];
  381. foreach ($list as $item) {
  382. $data[$item['order_no']] = $item['order_id'];
  383. }
  384. return $data;
  385. }
  386. }