Order.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. <?php
  2. namespace addons\unishop\model;
  3. use addons\unishop\extend\Hashids;
  4. use addons\unishop\extend\Snowflake;
  5. use think\Hook;
  6. use think\Model;
  7. use traits\model\SoftDelete;
  8. /**
  9. * 收货地址模型
  10. * Class Favorite
  11. * @package addons\unishop\model
  12. */
  13. class Order extends Model
  14. {
  15. use SoftDelete;
  16. protected $deleteTime = 'deletetime';
  17. // 表名
  18. protected $name = 'unishop_order';
  19. // 开启自动写入时间戳字段
  20. protected $autoWriteTimestamp = 'int';
  21. // 定义时间戳字段名
  22. protected $createTime = 'createtime';
  23. protected $updateTime = 'updatetime';
  24. // 隐藏属性
  25. protected $hidden = [
  26. 'id',
  27. 'user_id'
  28. ];
  29. // 支付类型
  30. const PAY_ONLINE = 1; // 在线支付
  31. const PAY_OFFLINE = 2; // 线下支付 或 货到付款
  32. const PAY_WXPAY = 3; // 微信支付
  33. const PAY_ALIPAY = 4; // 支付宝支付
  34. // 订单状态
  35. const STATUS_NORMAL = 1; // 正常
  36. const STATUS_CANCEL = 0; // 用户取消订单
  37. const STATUS_REFUND = -1; // 申请售后
  38. // 申请售后状态 0=无,1=申请中,2=通过(让用户发货)或无需退货,3=通过,4=拒绝
  39. const REFUND_STATUS_NONE = 0;
  40. const REFUND_STATUS_APPLY = 1;
  41. const REFUND_STATUS_DELIVERY = 2;
  42. const REFUND_STATUS_AGREE = 3;
  43. const REFUND_STATUS_REFUSE = 4;
  44. // 是否支付
  45. const PAID_NO = 0; // 否
  46. // 是否发货 delivered
  47. const DELIVERED_NO = 0; // 否
  48. // 是否评论
  49. const COMMENTED_NO = 0; // 否
  50. // 是否收货
  51. const RECEIVED_NO = 0; // 否
  52. // 订单类型
  53. const TYPE_ALL = 0; // 全部
  54. const TYPE_PAY = 1; // 待付款
  55. // const TYPE_DELIVES = 2; // 待发货
  56. const TYPE_RECEIVE = 3; // 待核销
  57. const TYPE_FINISH = 4; // 已完成
  58. const TYPE_NOCOMMENT = 40; // 待评价
  59. const TYPE_COMMENTED = 41; // 已评价
  60. // const TYPE_REFUND = 5; // 售后
  61. // const TYPE_REFUSE = 6; // 拒绝退款
  62. // const TYPE_OFF = 9; // 订单关闭
  63. const TYPE_CANCEL = 20; //订单取消(已支付+未支付)
  64. /**
  65. * 格式化时间
  66. * @param $value
  67. * @return false|string
  68. */
  69. public function getCreatetimeAttr($value)
  70. {
  71. return date('Y-m-d H:i:s', $value);
  72. }
  73. /**
  74. * 格式化时间 paidtime
  75. * @return false|int|string
  76. */
  77. public function getPaidtimeAttr($value, $data)
  78. {
  79. return $data['have_paid'] > 0 ? date('Y-m-d H:i:s', $data['have_paid']) : '';
  80. }
  81. /**
  82. * 格式化时间 deliveredtime
  83. * @return false|int|string
  84. */
  85. public function getDeliveredtimeAttr($value, $data)
  86. {
  87. return $data['have_delivered'] > 0 ? date('Y-m-d H:i:s', $data['have_delivered']) : '';
  88. }
  89. /**
  90. * 格式化时间 receivedtime
  91. * @return false|int|string
  92. */
  93. public function getReceivedtimeAttr($value, $data)
  94. {
  95. return $data['have_received'] > 0 ? date('Y-m-d H:i:s', $data['have_received']) : '';
  96. }
  97. /**
  98. * 格式化时间 commentedtime
  99. * @return false|int|string
  100. */
  101. public function getCommentedtimeAttr($value, $data)
  102. {
  103. return $data['have_commented'] > 0 ? date('Y-m-d H:i:s', $data['have_commented']) : '';
  104. }
  105. /**
  106. * 支付类型
  107. */
  108. public function getPayTypeTextAttr($value, $data)
  109. {
  110. switch ($data['pay_type']) {
  111. case self::PAY_ONLINE:
  112. return __('Online');
  113. break;
  114. case self::PAY_OFFLINE:
  115. return __('Offline');
  116. break;
  117. case self::PAY_WXPAY:
  118. return __('wxPay');
  119. break;
  120. case self::PAY_ALIPAY:
  121. return __('aliPay');
  122. break;
  123. }
  124. }
  125. /**
  126. * 加密订单id
  127. * @param $value
  128. * @param $data
  129. * @return string
  130. */
  131. public function getOrderIdAttr($value, $data)
  132. {
  133. return Hashids::encodeHex($data['id']);
  134. }
  135. /**
  136. * * 0=全部,1=待付款,3=待核销,41=已评价,40=未评价,20=已取消
  137. * 获取当前的订单状态
  138. */
  139. public function getStateAttr($value, $data)
  140. {
  141. switch (true) {
  142. case $data['have_paid'] == self::PAID_NO && $data['status'] == self::STATUS_NORMAL:
  143. $state = self::TYPE_PAY;
  144. break;
  145. /*case $data['have_delivered'] == self::DELIVERED_NO && $data['status'] == self::STATUS_NORMAL:
  146. $state = self::TYPE_DELIVES;
  147. break;*/
  148. case $data['have_paid'] != self::PAID_NO && $data['have_received'] == self::RECEIVED_NO && $data['status'] == self::STATUS_NORMAL:
  149. $state = self::TYPE_RECEIVE;
  150. break;
  151. case $data['have_paid'] != self::PAID_NO && $data['have_received'] != self::RECEIVED_NO && $data['have_commented'] == self::COMMENTED_NO && $data['status'] == self::STATUS_NORMAL:
  152. $state = self::TYPE_NOCOMMENT;
  153. break;
  154. case $data['have_paid'] != self::PAID_NO && $data['have_received'] != self::RECEIVED_NO && $data['have_commented'] != self::COMMENTED_NO && $data['status'] == self::STATUS_NORMAL:
  155. $state = self::TYPE_COMMENTED;
  156. break;
  157. case $data['status'] == self::STATUS_CANCEL:
  158. $state = self::TYPE_CANCEL;
  159. break;
  160. //case $data['status'] == self::STATUS_REFUND && $data['refund_status'] == self::REFUND_STATUS_AGREE: // TODO 申请退款并且已通过同意,则订单为关闭状态
  161. /* case $data['status'] == self::STATUS_REFUND && $data['refund_status'] == self::REFUND_STATUS_REFUSE:
  162. $state = self::TYPE_REFUSE;
  163. break;
  164. case $data['status'] == self::STATUS_REFUND:
  165. $state = self::TYPE_REFUND;
  166. break;*/
  167. default:
  168. $state = self::TYPE_ALL;
  169. break;
  170. }
  171. return $state;
  172. }
  173. //获取当前的订单状态,中文
  174. public function getStatetextAttr($state,$refund_status){
  175. $data = [
  176. 1 => '待付款',
  177. 3 => '待核销',
  178. 41 => '已评价',
  179. 40 => '待评价',
  180. 20 => '已取消',
  181. ];
  182. if($state == 20 && $refund_status == 3){
  183. return '已退款';
  184. }
  185. return isset($data[$state]) ? $data[$state] : $state;
  186. }
  187. /**
  188. * 退款状态
  189. */
  190. public function getRefundStatusTextAttr($value, $data)
  191. {
  192. $name = '';
  193. if ($data['status'] == self::STATUS_REFUND) {
  194. switch ($data['refund_status']) {
  195. case self::REFUND_STATUS_APPLY:
  196. $name = '申请中';
  197. break;
  198. case self::REFUND_STATUS_DELIVERY:
  199. $name = '通过申请/请发货';
  200. break;
  201. case self::REFUND_STATUS_AGREE:
  202. $name = '退款成功';
  203. break;
  204. case self::REFUND_STATUS_REFUSE:
  205. $name = '退款失败';
  206. break;
  207. }
  208. }
  209. return $name;
  210. }
  211. /**
  212. * 创建订单
  213. * @param $userId
  214. * @param $data
  215. * @return int
  216. * @throws \Exception
  217. */
  218. public function createOrder($userId, $data)
  219. {
  220. $data['userId'] = $userId;
  221. Hook::listen('create_order_before', $params, $data);
  222. list($products, $delivery, $coupon, $baseProductInfos, $address, $orderPrice, $specs, $numbers) = $params;
  223. // 获取雪花算法分布式id,方便以后扩展
  224. $snowflake = new Snowflake();
  225. $id = $snowflake->id();
  226. // 优惠费用
  227. // $discountPrice = isset($coupon['value']) ? $coupon['value'] : 0;
  228. $discountPrice = 0;
  229. // 订单费用
  230. //$orderPrice;
  231. // 运费
  232. // $deliveryPrice = Delivery::algorithm($delivery, array_sum($numbers));
  233. $deliveryPrice = '0';
  234. // 总费用
  235. $totalPrice = bcadd(bcsub($orderPrice, $discountPrice, 2), $deliveryPrice, 2);
  236. $out_trade_no = date('Ymd',time()).uniqid().$userId;
  237. $db_pay_type = [
  238. 'wallet' => 2,
  239. 'wechat' => 3,
  240. 'alipay' => 4,
  241. ];
  242. (new self)->save([
  243. 'id' => $id,
  244. 'user_id' => $userId,
  245. 'out_trade_no' => $out_trade_no,
  246. 'order_price' => $orderPrice,
  247. 'discount_price' => $discountPrice,
  248. 'delivery_price' => $deliveryPrice,
  249. 'total_price' => $totalPrice,
  250. 'pay_type' => $db_pay_type[$data['pay_type']],
  251. 'ip' => $_SERVER['REMOTE_ADDR'] ?? '',
  252. 'remark' => $data['remark'] ?? '',
  253. 'status' => self::STATUS_NORMAL,
  254. ]);
  255. (new OrderExtend)->save([
  256. 'user_id' => $userId,
  257. 'order_id' => $id,
  258. 'coupon_id' => $coupon ? $coupon['id'] : 0,
  259. 'coupon_json' => json_encode($coupon),
  260. 'delivery_id' => $delivery ? $delivery['id'] : 0,
  261. 'delivery_json' => json_encode($delivery),
  262. // 'address_id' => $address['id'],
  263. // 'address_json' => json_encode($address),
  264. ]);
  265. $orderProduct = $specNumber = [];
  266. foreach($products as $key => $product) {
  267. $orderProduct[] = [
  268. 'user_id' => $userId,
  269. 'order_id' => $id,
  270. 'product_id' => $product['id'],
  271. 'title' => $product['title'],
  272. 'image' => $product['image'],
  273. 'number' => $numbers[$key],
  274. 'spec' => $specs[$key] ?? '',
  275. 'price' => $baseProductInfos[$key]['sales_price'],
  276. //'product_json' => json_encode($product), // Todo 耗内存,损速度 (考虑去掉)
  277. 'createtime' => time(),
  278. 'updatetime' => time(),
  279. 'flash_id' => $data['flash_id'] ?? 0, // 秒杀id
  280. ];
  281. if (!empty($specs[$key])) {
  282. $specNumber[$specs[$key]] = $numbers[$key];
  283. } else {
  284. $specNumber[$key] = $numbers[$key];
  285. }
  286. }
  287. (new OrderProduct)->insertAll($orderProduct);
  288. $data['specNumber'] = $specNumber;
  289. Hook::listen('create_order_after', $products, $data);
  290. return [
  291. 'order_id' => Hashids::encodeHex($id),
  292. 'out_trade_no' => $out_trade_no
  293. ];
  294. }
  295. /**
  296. * 获取我的订单
  297. * @param int $userId 用户id
  298. * @param int $state 0=全部,1=待付款,3=待核销,4=已完成,40=待评价
  299. */
  300. public function getOrdersByType($userId, $state = 0, $page = 1, $pageSize = 10)
  301. {
  302. $condition['user_id'] = ['=', $userId];
  303. switch ($state) {
  304. case self::TYPE_PAY: //1=待付款
  305. $condition['have_paid'] = ['=', self::PAID_NO];
  306. $condition['status'] = ['=', self::STATUS_NORMAL];
  307. $orderBy = 'createtime';
  308. break;
  309. /*case self::TYPE_DELIVES:
  310. $condition['have_paid'] = ['>', self::PAID_NO];
  311. $condition['have_delivered'] = ['=', self::DELIVERED_NO];
  312. $condition['status'] = ['=', self::STATUS_NORMAL];
  313. $orderBy = 'have_paid';
  314. break;*/
  315. case self::TYPE_RECEIVE: //3=待核销
  316. $condition['have_paid'] = ['>', self::PAID_NO];
  317. $condition['have_delivered'] = ['>', self::DELIVERED_NO];
  318. $condition['have_received'] = ['=', self::RECEIVED_NO];
  319. $condition['status'] = ['=', self::STATUS_NORMAL];
  320. $orderBy = 'have_delivered';
  321. break;
  322. case self::TYPE_FINISH: //4=已完成
  323. $condition['have_paid'] = ['>', self::PAID_NO];
  324. $condition['have_delivered'] = ['>', self::DELIVERED_NO];
  325. $condition['have_received'] = ['>', self::RECEIVED_NO];
  326. // $condition['have_commented'] = ['=', self::COMMENTED_NO];
  327. $condition['status'] = ['=', self::STATUS_NORMAL];
  328. $orderBy = 'have_received';
  329. break;
  330. case self::TYPE_NOCOMMENT: //40=待评价
  331. $condition['have_paid'] = ['>', self::PAID_NO];
  332. $condition['have_delivered'] = ['>', self::DELIVERED_NO];
  333. $condition['have_received'] = ['>', self::RECEIVED_NO];
  334. $condition['have_commented'] = ['=', self::COMMENTED_NO];
  335. $condition['status'] = ['=', self::STATUS_NORMAL];
  336. $orderBy = 'have_received';
  337. break;
  338. /*case self::TYPE_REFUND:
  339. $condition['have_paid'] = ['>', self::PAID_NO];
  340. $condition['status'] = ['=', self::STATUS_REFUND];
  341. $condition['refund_status'] = ['<>', self::REFUND_STATUS_AGREE];
  342. $orderBy = 'createtime';
  343. break;*/
  344. default: //全部
  345. $orderBy = 'createtime';
  346. break;
  347. }
  348. $result = $this
  349. ->with([
  350. 'products' => function($query) {
  351. $query->field('id,id as order_product_id,title,image,number,price,spec,order_id,product_id');
  352. },
  353. 'evaluate' => function($query) {
  354. $query->field('id,order_id,order_product_id,product_id');
  355. },
  356. ])
  357. ->where($condition)
  358. ->order([$orderBy => 'desc'])
  359. ->limit(($page - 1) * $pageSize, $pageSize)
  360. ->select();
  361. foreach ($result as &$item) {
  362. $item->append(['order_id','state']);
  363. $item['state_text'] = $this->getStatetextAttr($item['state'],$item['refund_status']);//状态中文
  364. $item = $item->toArray();
  365. // 是否已评论
  366. $evaluate = array_column($item['evaluate'], 'order_product_id');
  367. unset($item['evaluate']);
  368. foreach ($item['products'] as &$product) {
  369. $product['image'] = Config::getImagesFullUrl($product['image']);
  370. // 是否已评论
  371. if (in_array($product['order_product_id'], $evaluate)) {
  372. $product['evaluate'] = true;
  373. } else {
  374. $product['evaluate'] = false;
  375. }
  376. }
  377. }
  378. return $result;
  379. }
  380. //订单各个状态的数量
  381. public function getOrdersByType_num($userId, $state = 0)
  382. {
  383. $condition['user_id'] = ['=', $userId];
  384. switch ($state) {
  385. case self::TYPE_PAY: //1=待付款
  386. $condition['have_paid'] = ['=', self::PAID_NO];
  387. $condition['status'] = ['=', self::STATUS_NORMAL];
  388. $orderBy = 'createtime';
  389. break;
  390. /*case self::TYPE_DELIVES:
  391. $condition['have_paid'] = ['>', self::PAID_NO];
  392. $condition['have_delivered'] = ['=', self::DELIVERED_NO];
  393. $condition['status'] = ['=', self::STATUS_NORMAL];
  394. $orderBy = 'have_paid';
  395. break;*/
  396. case self::TYPE_RECEIVE: //3=待核销
  397. $condition['have_paid'] = ['>', self::PAID_NO];
  398. $condition['have_delivered'] = ['>', self::DELIVERED_NO];
  399. $condition['have_received'] = ['=', self::RECEIVED_NO];
  400. $condition['status'] = ['=', self::STATUS_NORMAL];
  401. $orderBy = 'have_delivered';
  402. break;
  403. case self::TYPE_FINISH: //4=已完成
  404. $condition['have_paid'] = ['>', self::PAID_NO];
  405. $condition['have_delivered'] = ['>', self::DELIVERED_NO];
  406. $condition['have_received'] = ['>', self::RECEIVED_NO];
  407. // $condition['have_commented'] = ['=', self::COMMENTED_NO];
  408. $condition['status'] = ['=', self::STATUS_NORMAL];
  409. $orderBy = 'have_received';
  410. break;
  411. case self::TYPE_NOCOMMENT: //40=待评价
  412. $condition['have_paid'] = ['>', self::PAID_NO];
  413. $condition['have_delivered'] = ['>', self::DELIVERED_NO];
  414. $condition['have_received'] = ['>', self::RECEIVED_NO];
  415. $condition['have_commented'] = ['=', self::COMMENTED_NO];
  416. $condition['status'] = ['=', self::STATUS_NORMAL];
  417. $orderBy = 'have_received';
  418. break;
  419. /*case self::TYPE_REFUND:
  420. $condition['have_paid'] = ['>', self::PAID_NO];
  421. $condition['status'] = ['=', self::STATUS_REFUND];
  422. $condition['refund_status'] = ['<>', self::REFUND_STATUS_AGREE];
  423. $orderBy = 'createtime';
  424. break;*/
  425. default: //全部
  426. $orderBy = 'createtime';
  427. break;
  428. }
  429. $result = $this->where($condition)->count();
  430. return $result;
  431. }
  432. /**
  433. * 关联订单的商品
  434. */
  435. public function products()
  436. {
  437. return $this->hasMany('orderProduct', 'order_id', 'id');
  438. }
  439. /**
  440. * 关联扩展订单信息
  441. * @return \think\model\relation\HasOne
  442. */
  443. public function extend()
  444. {
  445. return $this->hasOne('orderExtend', 'order_id', 'id');
  446. }
  447. /**
  448. * 关联评价
  449. * @return \think\model\relation\HasOne
  450. */
  451. public function evaluate()
  452. {
  453. return $this->hasMany('evaluate', 'order_id', 'id');
  454. }
  455. /**
  456. * 关联退货信息
  457. * @return \think\model\relation\HasMany
  458. */
  459. public function refund()
  460. {
  461. return $this->hasOne('orderRefund', 'order_id', 'id');
  462. }
  463. /**
  464. * 关联退货的商品
  465. * @return \think\model\relation\HasMany
  466. */
  467. public function refundProducts()
  468. {
  469. return $this->hasMany('orderRefundProduct', 'order_id', 'id');
  470. }
  471. }