MallOrder.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace addons\StarChain\common\models;
  3. use addons\StarChain\common\enums\StarChainEnum;
  4. use addons\StarChain\common\models\Order as ModelsOrder;
  5. use common\models\order\Order;
  6. /**
  7. * 主商城订单模型
  8. * @package addons\StarChain\common\models
  9. */
  10. class MallOrder extends Order
  11. {
  12. /**
  13. * 获取商城订单
  14. *
  15. * @param integer $order_id
  16. * @return Order
  17. */
  18. public static function getOrder($order_id)
  19. {
  20. $model = self::find();
  21. // $model->select('id, user_id, mall_id, order_no, mobile, region_name, address, pay_status, receiver_name, order_source');
  22. return $model->where(['id' => $order_id])
  23. ->with(['detail' => function($query) {
  24. $query->select('id, order_id, num, goods_attr_id, goods_id')->with([
  25. 'goods' => function($query) {
  26. $query->select('id, goods_source');
  27. }
  28. ]);
  29. }])
  30. ->one();
  31. }
  32. /**
  33. * 获取地址
  34. *
  35. * @return array
  36. */
  37. public function getRegion()
  38. {
  39. $region = $this->region_name;
  40. list($province, $city, $area, $town) = explode(' ', $region);
  41. $town = $town ?? $area;
  42. $address = $this->address;
  43. return compact('province', 'city', 'area', 'town', 'address');
  44. }
  45. /**
  46. * 是否已经创建供应链订单
  47. *
  48. * @return boolean
  49. */
  50. public function hasChainOrder()
  51. {
  52. return ModelsOrder::find()->select('id')->where([
  53. 'mall_id' => $this->mall_id,
  54. 'order_no' => $this->order_no
  55. ])->one() ? true : false;
  56. }
  57. /**
  58. * 判断当前订单是否是供应链订单
  59. *
  60. * @return boolean
  61. */
  62. public function isChainOrder()
  63. {
  64. if (empty($this->detail)) return false;
  65. $goods_sources = array_map(function($goods) {
  66. return $goods->goods_source;
  67. }, array_column($this->detail, 'goods'));
  68. return array_unique($goods_sources) == [StarChainEnum::PLUGIN_SIGN];
  69. }
  70. /**
  71. * @return boolean
  72. */
  73. public function changeOrderSource()
  74. {
  75. $this->order_source = StarChainEnum::PLUGIN_SIGN;
  76. return $this->save();
  77. }
  78. }