Order.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. namespace app\common\model\order;
  3. use app\common\enum\order\OrderSourceEnum;
  4. use app\common\model\BaseModel;
  5. use app\common\enum\settings\DeliveryTypeEnum;
  6. use app\common\enum\order\OrderPayStatusEnum;
  7. use app\common\enum\order\OrderTypeEnum;
  8. use app\common\enum\order\OrderPayTypeEnum;
  9. use app\common\library\helper;
  10. use app\common\service\order\OrderService;
  11. use app\common\service\order\OrderCompleteService;
  12. use app\common\model\store\Order as StoreOrderModel;
  13. /**
  14. * 订单模型模型
  15. */
  16. class Order extends BaseModel
  17. {
  18. protected $pk = 'order_id';
  19. protected $name = 'order';
  20. /**
  21. * 追加字段
  22. * @var string[]
  23. */
  24. protected $append = [
  25. 'state_text',
  26. 'order_source_text',
  27. ];
  28. /**
  29. * 订单商品列表
  30. * @return \think\model\relation\HasMany
  31. */
  32. public function product()
  33. {
  34. return $this->hasMany('app\\common\\model\\order\\OrderProduct', 'order_id', 'order_id')->hidden(['content']);
  35. }
  36. /**
  37. * 关联订单收货地址表
  38. * @return \think\model\relation\HasOne
  39. */
  40. public function address()
  41. {
  42. return $this->hasOne('app\\common\\model\\order\\OrderAddress');
  43. }
  44. /**
  45. * 关联自提订单联系方式
  46. * @return \think\model\relation\HasOne
  47. */
  48. public function extract()
  49. {
  50. return $this->hasOne('app\\common\\model\\order\\OrderExtract');
  51. }
  52. /**
  53. * 关联物流公司表
  54. * @return \think\model\relation\BelongsTo
  55. */
  56. public function express()
  57. {
  58. return $this->belongsTo('app\\common\\model\\settings\\Express', 'express_id', 'express_id');
  59. }
  60. /**
  61. * 关联自提门店表
  62. * @return \think\model\relation\BelongsTo
  63. */
  64. public function extractStore()
  65. {
  66. return $this->belongsTo('app\\common\\model\\store\\Store', 'extract_store_id', 'store_id');
  67. }
  68. /**
  69. * 关联门店店员表
  70. * @return \think\model\relation\BelongsTo
  71. */
  72. public function extractClerk()
  73. {
  74. return $this->belongsTo('app\\common\\model\\store\\Clerk', 'extract_clerk_id');
  75. }
  76. /**
  77. * 关联用户表
  78. * @return \think\model\relation\BelongsTo
  79. */
  80. public function user()
  81. {
  82. return $this->belongsTo('app\\common\\model\\user\\User', 'user_id', 'user_id');
  83. }
  84. /**
  85. * 订单状态文字描述
  86. * @param $value
  87. * @param $data
  88. * @return string
  89. */
  90. public function getStateTextAttr($value, $data)
  91. {
  92. // 订单状态
  93. if (in_array($data['order_status'], [20, 30])) {
  94. $orderStatus = [20 => '已取消', 30 => '已完成'];
  95. return $orderStatus[$data['order_status']];
  96. }
  97. // 付款状态
  98. if ($data['pay_status'] == 10) {
  99. return '待付款';
  100. }
  101. // 拼团状态
  102. if($data['order_source'] == OrderSourceEnum::ASSEMBLE){
  103. $assemble_status = $this->getAssembleStatus($data);
  104. if($assemble_status != ''){
  105. return $assemble_status;
  106. }
  107. }
  108. // 发货状态
  109. if ($data['delivery_status'] == 10) {
  110. return '已付款,待发货';
  111. }
  112. if ($data['receipt_status'] == 10) {
  113. return '已发货,待收货';
  114. }
  115. return $value;
  116. }
  117. /**
  118. * 拼团订单状态
  119. */
  120. private function getAssembleStatus($data){
  121. // 发货状态
  122. if ($data['assemble_status'] == 10) {
  123. return '已付款,未成团';
  124. }
  125. if ($data['assemble_status'] == 20 && $data['delivery_status'] == 10) {
  126. return '拼团成功,待发货';
  127. }
  128. if ($data['assemble_status'] == 30) {
  129. return '拼团失败';
  130. }
  131. return '';
  132. }
  133. /**
  134. * 付款状态
  135. * @param $value
  136. * @return array
  137. */
  138. public function getPayTypeAttr($value)
  139. {
  140. return ['text' => OrderPayTypeEnum::data()[$value]['name'], 'value' => $value];
  141. }
  142. /**
  143. * 订单来源
  144. * @param $value
  145. * @return array
  146. */
  147. public function getOrderSourceTextAttr($value, $data)
  148. {
  149. return OrderSourceEnum::data()[$data['order_source']]['name'];
  150. }
  151. /**
  152. * 付款状态
  153. * @param $value
  154. * @return array
  155. */
  156. public function getPayStatusAttr($value)
  157. {
  158. return ['text' => OrderPayStatusEnum::data()[$value]['name'], 'value' => $value];
  159. }
  160. /**
  161. * 改价金额(差价)
  162. * @param $value
  163. * @return array
  164. */
  165. public function getUpdatePriceAttr($value)
  166. {
  167. return [
  168. 'symbol' => $value < 0 ? '-' : '+',
  169. 'value' => sprintf('%.2f', abs($value))
  170. ];
  171. }
  172. /**
  173. * 发货状态
  174. * @param $value
  175. * @return array
  176. */
  177. public function getDeliveryStatusAttr($value)
  178. {
  179. $status = [10 => '待发货', 20 => '已发货'];
  180. return ['text' => $status[$value], 'value' => $value];
  181. }
  182. /**
  183. * 收货状态
  184. * @param $value
  185. * @return array
  186. */
  187. public function getReceiptStatusAttr($value)
  188. {
  189. $status = [10 => '待收货', 20 => '已收货'];
  190. return ['text' => $status[$value], 'value' => $value];
  191. }
  192. /**
  193. * 收货状态
  194. * @param $value
  195. * @return array
  196. */
  197. public function getOrderStatusAttr($value)
  198. {
  199. $status = [10 => '进行中', 20 => '已取消', 21 => '待取消', 30 => '已完成'];
  200. return ['text' => $status[$value], 'value' => $value];
  201. }
  202. /**
  203. * 配送方式
  204. * @param $value
  205. * @return array
  206. */
  207. public function getDeliveryTypeAttr($value)
  208. {
  209. return ['text' => DeliveryTypeEnum::data()[$value]['name'], 'value' => $value];
  210. }
  211. /**
  212. * 订单详情
  213. */
  214. public static function detail($where, $with = ['user', 'address', 'product' => ['image', 'refund'], 'extract', 'express', 'extractStore.logo', 'extractClerk'])
  215. {
  216. is_array($where) ? $filter = $where : $filter['order_id'] = (int)$where;
  217. return (new static())->with($with)->where($filter)->find();
  218. }
  219. /**
  220. * 订单详情
  221. */
  222. public static function detailByNo($order_no, $with = ['user', 'address', 'product' => ['image', 'refund'], 'extract', 'express', 'extractStore.logo', 'extractClerk'])
  223. {
  224. return (new static())->with($with)->where('order_no', '=', $order_no)->find();
  225. }
  226. /**
  227. * 批量获取订单列表
  228. * @param $orderIds
  229. * @param array $with
  230. * @return array
  231. */
  232. public function getListByIds($orderIds, $with = [])
  233. {
  234. $data = $this->getListByInArray('order_id', $orderIds, $with);
  235. return helper::arrayColumn2Key($data, 'order_id');
  236. }
  237. /**
  238. * 批量更新订单
  239. * @param $orderIds
  240. * @param $data
  241. * @return bool
  242. */
  243. public function onBatchUpdate($orderIds, $data)
  244. {
  245. return $this->where('order_id', 'in', $orderIds)->save($data);
  246. }
  247. /**
  248. * 批量获取订单列表
  249. * @param $field
  250. * @param $data
  251. * @param array $with
  252. * @return \think\Collection
  253. * @throws \think\db\exception\DataNotFoundException
  254. * @throws \think\db\exception\DbException
  255. * @throws \think\db\exception\ModelNotFoundException
  256. */
  257. private function getListByInArray($field, $data, $with = [])
  258. {
  259. return $this->with($with)
  260. ->where($field, 'in', $data)
  261. ->where('is_delete', '=', 0)
  262. ->select();
  263. }
  264. /**
  265. * 生成订单号
  266. * @return string
  267. */
  268. public function orderNo()
  269. {
  270. return OrderService::createOrderNo();
  271. }
  272. /**
  273. * 确认核销(自提订单)
  274. * @param $extractClerkId
  275. * @return bool|mixed
  276. */
  277. public function verificationOrder($extractClerkId)
  278. {
  279. if (
  280. $this['pay_status']['value'] != 20
  281. || $this['delivery_type']['value'] != DeliveryTypeEnum::EXTRACT
  282. || $this['delivery_status']['value'] == 20
  283. || in_array($this['order_status']['value'], [20, 21])
  284. ) {
  285. $this->error = '该订单不满足核销条件';
  286. return false;
  287. }
  288. return $this->transaction(function () use ($extractClerkId) {
  289. // 更新订单状态:已发货、已收货
  290. $status = $this->save([
  291. 'extract_clerk_id' => $extractClerkId, // 核销员
  292. 'delivery_status' => 20,
  293. 'delivery_time' => time(),
  294. 'receipt_status' => 20,
  295. 'receipt_time' => time(),
  296. 'order_status' => 30
  297. ]);
  298. // 新增订单核销记录
  299. StoreOrderModel::add(
  300. $this['order_id'],
  301. $this['extract_store_id'],
  302. $this['extract_clerk_id'],
  303. OrderTypeEnum::MASTER
  304. );
  305. // 执行订单完成后的操作
  306. $OrderCompleteService = new OrderCompleteService(OrderTypeEnum::MASTER);
  307. $OrderCompleteService->complete([$this], static::$app_id);
  308. return $status;
  309. });
  310. }
  311. }