Order.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\common\model;
  10. use think\Hook;
  11. use app\common\model\store\shop\Order as ShopOrder;
  12. use app\common\service\Order as OrderService;
  13. use app\common\service\order\Complete as OrderCompleteService;
  14. use app\common\enum\OrderType as OrderTypeEnum;
  15. use app\common\enum\DeliveryType as DeliveryTypeEnum;
  16. use app\common\enum\order\PayType as PayTypeEnum;
  17. use app\common\enum\order\PayStatus as PayStatusEnum;
  18. use app\common\library\helper;
  19. /**
  20. * 订单模型
  21. * Class Order
  22. * @package app\common\model
  23. */
  24. class Order extends BaseModel
  25. {
  26. protected $name = 'order';
  27. protected $alias = 'order';
  28. /**
  29. * 追加字段
  30. * @var array
  31. */
  32. protected $append = [
  33. 'state_text', // 售后单状态文字描述
  34. ];
  35. /**
  36. * 订单模型初始化
  37. */
  38. public static function init()
  39. {
  40. parent::init();
  41. // 监听订单处理事件
  42. $static = new static;
  43. Hook::listen('order', $static);
  44. }
  45. /**
  46. * 订单商品列表
  47. * @return \think\model\relation\HasMany
  48. */
  49. public function goods()
  50. {
  51. $module = self::getCalledModule() ?: 'common';
  52. return $this->hasMany("app\\{$module}\\model\\OrderGoods");
  53. }
  54. /**
  55. * 关联订单收货地址表
  56. * @return \think\model\relation\HasOne
  57. */
  58. public function address()
  59. {
  60. $module = self::getCalledModule() ?: 'common';
  61. return $this->hasOne("app\\{$module}\\model\\OrderAddress");
  62. }
  63. /**
  64. * 关联订单收货地址表
  65. * @return \think\model\relation\HasOne
  66. */
  67. public function extract()
  68. {
  69. $module = self::getCalledModule() ?: 'common';
  70. return $this->hasOne("app\\{$module}\\model\\OrderExtract");
  71. }
  72. /**
  73. * 关联自提门店表
  74. * @return \think\model\relation\BelongsTo
  75. */
  76. public function extractShop()
  77. {
  78. $module = self::getCalledModule() ?: 'common';
  79. return $this->belongsTo("app\\{$module}\\model\\store\\Shop", 'extract_shop_id');
  80. }
  81. /**
  82. * 关联门店店员表
  83. * @return \think\model\relation\BelongsTo
  84. */
  85. public function extractClerk()
  86. {
  87. $module = self::getCalledModule() ?: 'common';
  88. return $this->belongsTo("app\\{$module}\\model\\store\\shop\\Clerk", 'extract_clerk_id');
  89. }
  90. /**
  91. * 关联用户表
  92. * @return \think\model\relation\BelongsTo
  93. */
  94. public function user()
  95. {
  96. $module = self::getCalledModule() ?: 'common';
  97. return $this->belongsTo("app\\{$module}\\model\\User");
  98. }
  99. /**
  100. * 关联物流公司表
  101. * @return \think\model\relation\BelongsTo
  102. */
  103. public function express()
  104. {
  105. $module = self::getCalledModule() ?: 'common';
  106. return $this->belongsTo("app\\{$module}\\model\\Express");
  107. }
  108. /**
  109. * 订单状态文字描述
  110. * @param $value
  111. * @param $data
  112. * @return string
  113. */
  114. public function getStateTextAttr($value, $data)
  115. {
  116. // 订单状态
  117. if (in_array($data['order_status'], [20, 30])) {
  118. $orderStatus = [20 => '已取消', 30 => '已完成'];
  119. return $orderStatus[$data['order_status']];
  120. }
  121. // 付款状态
  122. if ($data['pay_status'] == 10) {
  123. return '待付款';
  124. }
  125. // 订单类型:单独购买
  126. if ($data['delivery_status'] == 10) {
  127. return '已付款,待发货';
  128. }
  129. if ($data['receipt_status'] == 10) {
  130. return '已发货,待收货';
  131. }
  132. return $value;
  133. }
  134. /**
  135. * 获取器:订单金额(含优惠折扣)
  136. * @param $value
  137. * @param $data
  138. * @return string
  139. */
  140. public function getOrderPriceAttr($value, $data)
  141. {
  142. // 兼容旧数据:订单金额
  143. if ($value == 0) {
  144. return helper::bcadd(helper::bcsub($data['total_price'], $data['coupon_money']), $data['update_price']);
  145. }
  146. return $value;
  147. }
  148. /**
  149. * 改价金额(差价)
  150. * @param $value
  151. * @return array
  152. */
  153. public function getUpdatePriceAttr($value)
  154. {
  155. return [
  156. 'symbol' => $value < 0 ? '-' : '+',
  157. 'value' => sprintf('%.2f', abs($value))
  158. ];
  159. }
  160. /**
  161. * 付款状态
  162. * @param $value
  163. * @return array
  164. */
  165. public function getPayTypeAttr($value)
  166. {
  167. return ['text' => PayTypeEnum::data()[$value]['name'], 'value' => $value];
  168. }
  169. /**
  170. * 付款状态
  171. * @param $value
  172. * @return array
  173. */
  174. public function getPayStatusAttr($value)
  175. {
  176. return ['text' => PayStatusEnum::data()[$value]['name'], 'value' => $value];
  177. }
  178. /**
  179. * 发货状态
  180. * @param $value
  181. * @return array
  182. */
  183. public function getDeliveryStatusAttr($value)
  184. {
  185. $status = [10 => '待发货', 20 => '已发货'];
  186. return ['text' => $status[$value], 'value' => $value];
  187. }
  188. /**
  189. * 收货状态
  190. * @param $value
  191. * @return array
  192. */
  193. public function getReceiptStatusAttr($value)
  194. {
  195. $status = [10 => '待收货', 20 => '已收货'];
  196. return ['text' => $status[$value], 'value' => $value];
  197. }
  198. /**
  199. * 收货状态
  200. * @param $value
  201. * @return array
  202. */
  203. public function getOrderStatusAttr($value)
  204. {
  205. $status = [10 => '进行中', 20 => '已取消', 21 => '待取消', 30 => '已完成'];
  206. return ['text' => $status[$value], 'value' => $value];
  207. }
  208. /**
  209. * 配送方式
  210. * @param $value
  211. * @return array
  212. */
  213. public function getDeliveryTypeAttr($value)
  214. {
  215. return ['text' => DeliveryTypeEnum::data()[$value]['name'], 'value' => $value];
  216. }
  217. /**
  218. * 生成订单号
  219. * @return string
  220. */
  221. public function orderNo()
  222. {
  223. return OrderService::createOrderNo();
  224. }
  225. /**
  226. * 订单详情
  227. * @param array|int $where
  228. * @param array $with
  229. * @return null|static
  230. * @throws \think\exception\DbException
  231. */
  232. public static function detail($where, $with = [
  233. 'user',
  234. 'address',
  235. 'goods' => ['image'],
  236. 'extract',
  237. 'express',
  238. 'extract_shop.logo',
  239. 'extract_clerk'
  240. ])
  241. {
  242. is_array($where) ? $filter = $where : $filter['order_id'] = (int)$where;
  243. return self::get($filter, $with);
  244. }
  245. /**
  246. * 批量获取订单列表
  247. * @param $orderIds
  248. * @param array $with 关联查询
  249. * @return array
  250. * @throws \think\db\exception\DataNotFoundException
  251. * @throws \think\db\exception\ModelNotFoundException
  252. * @throws \think\exception\DbException
  253. */
  254. public function getListByIds($orderIds, $with = [])
  255. {
  256. $data = $this->getListByInArray('order_id', $orderIds, $with);
  257. return helper::arrayColumn2Key($data, 'order_id');
  258. }
  259. /**
  260. * 批量获取订单列表
  261. * @param string $field
  262. * @param array $data
  263. * @param array $with
  264. * @return false|\PDOStatement|string|\think\Collection
  265. * @throws \think\db\exception\DataNotFoundException
  266. * @throws \think\db\exception\ModelNotFoundException
  267. * @throws \think\exception\DbException
  268. */
  269. private function getListByInArray($field, $data, $with = [])
  270. {
  271. return $this->with($with)
  272. ->where($field, 'in', $data)
  273. ->where('is_delete', '=', 0)
  274. ->select();
  275. }
  276. /**
  277. * 根据订单号批量查询
  278. * @param $orderNos
  279. * @param array $with
  280. * @return false|\PDOStatement|string|\think\Collection
  281. * @throws \think\db\exception\DataNotFoundException
  282. * @throws \think\db\exception\ModelNotFoundException
  283. * @throws \think\exception\DbException
  284. */
  285. public function getListByOrderNos($orderNos, $with = [])
  286. {
  287. return $this->getListByInArray('order_no', $orderNos, $with);
  288. }
  289. /**
  290. * 批量更新订单
  291. * @param $orderIds
  292. * @param $data
  293. * @return false|int
  294. */
  295. public function onBatchUpdate($orderIds, $data)
  296. {
  297. return $this->isUpdate(true)->save($data, ['order_id' => ['in', $orderIds]]);
  298. }
  299. /**
  300. * 确认核销(自提订单)
  301. * @param int $extractClerkId 核销员id
  302. * @return bool|false|int
  303. */
  304. public function verificationOrder($extractClerkId)
  305. {
  306. if (
  307. $this['pay_status']['value'] != 20
  308. || $this['delivery_type']['value'] != DeliveryTypeEnum::EXTRACT
  309. || $this['delivery_status']['value'] == 20
  310. || in_array($this['order_status']['value'], [20, 21])
  311. ) {
  312. $this->error = '该订单不满足核销条件';
  313. return false;
  314. }
  315. return $this->transaction(function () use ($extractClerkId) {
  316. // 更新订单状态:已发货、已收货
  317. $status = $this->save([
  318. 'extract_clerk_id' => $extractClerkId, // 核销员
  319. 'delivery_status' => 20,
  320. 'delivery_time' => time(),
  321. 'receipt_status' => 20,
  322. 'receipt_time' => time(),
  323. 'order_status' => 30
  324. ]);
  325. // 新增订单核销记录
  326. ShopOrder::add(
  327. $this['order_id'],
  328. $this['extract_shop_id'],
  329. $this['extract_clerk_id'],
  330. OrderTypeEnum::MASTER
  331. );
  332. // 执行订单完成后的操作
  333. $OrderCompleteService = new OrderCompleteService(OrderTypeEnum::MASTER);
  334. $OrderCompleteService->complete([$this], static::$wxapp_id);
  335. return $status;
  336. });
  337. }
  338. }