链街Dcat后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
5.6 KiB

5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Models;
  3. use App\Admin\Common\Rpc;
  4. use Dcat\Admin\Traits\HasDateTimeFormatter;
  5. use App\Models\v3\Market as MarketModel;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Support\Facades\DB;
  8. use App\Models\v3\LanzuEmployees as EmployeesModel;
  9. class ImsCjdcOrderMain extends Model
  10. {
  11. // 线上订单,外卖
  12. const ORDER_TYPE_ONLINE = 1;
  13. // 线下订单,当面付
  14. const ORDER_TYPE_OFFLINE = 4;
  15. // 配送方式:1服务站配送,2达达配送,3用户自提 shipping_type
  16. const ORDER_SHIPPING_TYPE_MARKET = 1;
  17. const ORDER_SHIPPING_TYPE_DADA = 2;
  18. const ORDER_SHIPPING_TYPE_USER_SELF = 3;
  19. // 订单状态
  20. // 待付款
  21. const ORDER_STATE_UNPAY = 1;
  22. // 待接单
  23. const ORDER_STATE_UNTAKE = 2;
  24. // 待送达
  25. const ORDER_STATE_DELIVERY = 3;
  26. // 已完成
  27. const ORDER_STATE_COMPLETE = 4;
  28. // 已评价
  29. const ORDER_STATE_EVALUATED = 5;
  30. // 已取消
  31. const ORDER_STATE_CANCEL = 6;
  32. // 已拒单
  33. const ORDER_STATE_REFUSE = 7;
  34. // 退款中
  35. const ORDER_STATE_REFUNDING = 8;
  36. // 已退款
  37. const ORDER_STATE_REFUNDED = 9;
  38. // 拒绝退款
  39. const ORDER_STATE_UNREFUND = 10;
  40. // 部分退款
  41. const ORDER_STATE_REBATES = 11;
  42. // 完成状态组合
  43. const ORDER_STATE_FINISH = [self::ORDER_STATE_COMPLETE, self::ORDER_STATE_EVALUATED, self::ORDER_STATE_UNREFUND, self::ORDER_STATE_REBATES];
  44. const STATE_LIST = [
  45. self::ORDER_STATE_UNPAY => '待支付',
  46. self::ORDER_STATE_UNTAKE => '待接单',
  47. self::ORDER_STATE_DELIVERY => '待送达',
  48. self::ORDER_STATE_COMPLETE => '已完成',
  49. self::ORDER_STATE_EVALUATED => '已评价',
  50. self::ORDER_STATE_CANCEL => '已取消',
  51. self::ORDER_STATE_REFUSE => '已拒单',
  52. self::ORDER_STATE_REFUNDING => '退款中',
  53. self::ORDER_STATE_REFUNDED => '已退款',
  54. self::ORDER_STATE_UNREFUND => '拒绝退款',
  55. self::ORDER_STATE_REBATES => '部分退款',
  56. ];
  57. const TABLE_NAME = 'lanzu_order_main';
  58. // 订单支付方式
  59. // 微信支付
  60. const ORDER_PAY_WX = 1;
  61. // 余额支付
  62. const ORDER_PAY_BALANCE = 2;
  63. use HasDateTimeFormatter;
  64. protected $table = 'lanzu_order_main';
  65. public $timestamps = false;
  66. public static $tableName = 'lanzu_order_main';
  67. public function imsCjdcUser()
  68. {
  69. return $this->hasOne('\App\Models\ImsCjdcUser', 'id', 'user_id');
  70. }
  71. public function market()
  72. {
  73. return $this->hasOne('\App\Models\ImsCjdcMarket', 'id', 'market_id');
  74. }
  75. /**
  76. * 变更订单状态,
  77. * @param $oid //主订单id
  78. * @param $state //订单状态
  79. */
  80. public function modifyState($oid, $state)
  81. {
  82. $where = [];
  83. $where['state'] = $state;
  84. $where['updated_at'] = time();
  85. if ($state == 3) {
  86. $where['receive_time'] = time();
  87. } elseif ($state == 4) {
  88. $where['complete_time'] = time();
  89. } elseif ($state == 6) {
  90. $where['cancel_time'] = time();
  91. }
  92. return self::where('id', $oid)->update($where);
  93. }
  94. public function updateShippingType($oid, $type)
  95. {
  96. return self::where('id', $oid)->update(['shipping_type' => $type]);
  97. }
  98. public static function getOrderData($oid)
  99. {
  100. $order = ImsCjdcOrderMain::where('id', $oid)->first()->toArray();
  101. $order['created_at'] = date('Y-m-d H:i:s', $order['created_at']);
  102. $order['updated_at'] = $order['updated_at'] ? date('Y-m-d H:i:s', $order['updated_at']) : null;
  103. $order['pay_type'] = config('order.pay_type')[$order['pay_type']];
  104. $order['shipping_type_num'] = $order['shipping_type'];
  105. $order['shipping_type'] = config('order.shipping_type')[$order['shipping_type']];
  106. $order['horseman_name'] = $order['shipping_name'] ?? LanzuServiceHorseman::getName($order['horseman_id']);
  107. // 获取市场
  108. $marketId = $order['market_id']?$order['market_id']:0;
  109. $market = MarketModel::getMarketInfo($marketId,['name','lat','lng']);
  110. $order['market_name'] = $market?$market['name']:'';
  111. $order['market_lat'] = $market?$market['lat']:'';
  112. $order['market_lng'] = $market?$market['lng']:'';
  113. //获取市场经纬度
  114. //$market = LanzuMarket::where('id',$order['market_id'])->first()->toArray();
  115. //$order['distance'] = Rpc::getDistance($market['lng'],$market['lat'],$order['lng'],$order['lat'])['result'];
  116. if ($order['delivery_distance'] > 1000) {
  117. $order['delivery_distance'] = number_format(($order['delivery_distance'] / 1000), 1) . 'km';
  118. } else {
  119. $order['delivery_distance'] .= '米';
  120. }
  121. return $order;
  122. }
  123. /**
  124. * 根据订单状态统计数量
  125. * @param $state
  126. * @param null $marketId
  127. * @return mixed
  128. */
  129. public static function getOrderStateCount($state, $marketId = null)
  130. {
  131. $builder = self::where('type', 1)
  132. ->where('created_at','>=',time()-(7*86400));
  133. if ($marketId) {
  134. $builder = $builder->where('market_id',$marketId);
  135. }
  136. if ($state==3){
  137. $builder = $builder->where('state',$state)->where('shipping_type',1)->where('horseman_id',0);
  138. }elseif ($state==311){
  139. $builder = $builder->where('state',3)->where('shipping_type',1)->where('horseman_id','>',0);
  140. }else{
  141. $builder = $builder->where('state',$state);
  142. }
  143. $count = $builder->count();
  144. return $count;
  145. }
  146. /**
  147. * 关联懒族员工表
  148. */
  149. public function employees()
  150. {
  151. return $this->belongsTo(new EmployeesModel(),'horseman_id','id','left');
  152. }
  153. }