Order.php 16 KB

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