Order.php 15 KB

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