DeliveryInfo.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use app\utils\Tools;
  9. use Yii;
  10. /**
  11. * This is the model class for table "{{%delivery_info}}".
  12. *
  13. * @property integer $id
  14. * @property integer $store_id
  15. * @property string $order_no
  16. * @property string $waybill_id
  17. * @property integer $status
  18. * @property integer $reason_id
  19. * @property integer $fee
  20. * @property integer $deduct_fee
  21. * @property integer $delivery_type
  22. * @property string $delivery_token
  23. * @property integer $is_delete
  24. * @property string $rider_name
  25. * @property string $rider_mobile
  26. * @property integer $created_at
  27. * @property integer $updated_at
  28. * @property integer $mini_id
  29. * @property integer $address_id
  30. * @property integer $is_local
  31. * @property integer $rider_id
  32. * @property integer $local_status
  33. * @property integer $arrive_time
  34. * @property integer $confirm_time
  35. * @property integer $md_id
  36. * @property integer $wechat_delivery_type
  37. * @property integer $wx_order_status
  38. * @property string $distance
  39. * @property float $last_latitude
  40. * @property float $last_longitude
  41. * @property integer $is_store_delivery_type
  42. * @property integer $is_front_delivery
  43. */
  44. class DeliveryInfo extends \yii\db\ActiveRecord
  45. {
  46. /**
  47. * 未分配骑手
  48. */
  49. const LOCAL_STATUS_NO_SEND = 0;
  50. /**
  51. * 等待骑手取货
  52. */
  53. const LOCAL_STATUS_WAITING = 1;
  54. /**
  55. * 骑手配送中
  56. */
  57. const LOCAL_STATUS_SENDING = 2;
  58. /**
  59. * 配送已完成
  60. */
  61. const LOCAL_STATUS_CONFIRM = 3;
  62. const LOCAL_STATUS_ARR = [
  63. self::LOCAL_STATUS_NO_SEND => '未分配骑手',
  64. self::LOCAL_STATUS_WAITING => '等待骑手取货',
  65. self::LOCAL_STATUS_SENDING => '骑手配送中',
  66. self::LOCAL_STATUS_CONFIRM => '配送已完成',
  67. ];
  68. /**
  69. * 是否是平台自配订单:是
  70. */
  71. const IS_LOCAL_YSE = 1;
  72. /**
  73. * 是否是平台自配订单:否
  74. */
  75. const IS_LOCAL_NO = 0;
  76. /**
  77. * 是否是仓库配送订单:是
  78. */
  79. const IS_FRONT_DELIVERY_YSE = 1;
  80. /**
  81. * 是否是仓库配送订单:否
  82. */
  83. const IS_FRONT_DELIVERY_NO = 0;
  84. /**
  85. * @inheritdoc
  86. */
  87. public static function tableName()
  88. {
  89. return '{{%delivery_info}}';
  90. }
  91. /**
  92. * @inheritdoc
  93. */
  94. public function rules()
  95. {
  96. return [
  97. [['store_id', 'status', 'reason_id', 'created_at', 'updated_at', 'delivery_type', 'is_delete', 'is_local', 'rider_id', 'local_status','arrive_time', 'confirm_time','rush_time', 'serial_num', 'md_id', 'wechat_delivery_type', 'is_store_delivery_type', 'is_front_delivery'], 'integer'],
  98. [['order_no', 'waybill_id', 'rider_name', 'rider_mobile', 'delivery_token'], 'string'],
  99. [['fee', 'deduct_fee', 'wx_order_status', 'distance', 'last_latitude', 'last_longitude'], 'number'],
  100. [['mini_id', 'address_id'], 'safe'],
  101. ];
  102. }
  103. /**
  104. * @inheritdoc
  105. */
  106. public function attributeLabels()
  107. {
  108. return [
  109. 'id' => 'ID',
  110. 'store_id' => 'Store ID',
  111. 'mini_id' => 'mini_id',
  112. 'address_id' => 'address_id',
  113. 'order_no' => '订单号',
  114. 'waybill_id' => '配送单id',
  115. 'status' => '配送单状态',
  116. 'reason_id' => '取消原因id',
  117. 'fee' => '费用',
  118. 'deduct_fee' => '取消费用',
  119. 'delivery_type' => '配送类型',
  120. 'created_at' => '创建时间',
  121. 'updated_at' => '状态更新时间',
  122. 'is_delete' => 'is_delete',
  123. 'is_local' => '是否是平台自配:0=否,1=是;',
  124. 'rider_id' => '骑手ID',
  125. 'local_status' => '同城配送订单状态:0=未分配骑手,1=待取货,2=配送中,3=配送完成',
  126. 'arrive_time' => '到店时间',
  127. 'confirm_time' => '送到时间',
  128. 'serial_num' => '流水号',
  129. 'wechat_delivery_type' => '微信同城配送类型0即时 1同城',
  130. 'wx_order_status' => '微信订单状态',
  131. 'distance' => '配送距离',
  132. 'last_latitude' => '获取骑手最后一次位置纬度',
  133. 'last_longitude' => '获取骑手最后一次位置经度',
  134. 'is_store_delivery_type' => '是否是商城自配',
  135. 'is_front_delivery' => '是否是仓库配送订单'
  136. ];
  137. }
  138. public function beforeSave($insert)
  139. {
  140. if (parent::beforeSave($insert)) {
  141. //如果是仓库配送订单 未转单成功到仓库时禁止修改状态
  142. if (!$insert && intval($this->is_front_delivery) === self::IS_FRONT_DELIVERY_YSE) {
  143. $frontDeliveryOrder = FrontDeliveryOrder::findOne(['delivery_info_id' => $this->id]);
  144. if (!$frontDeliveryOrder) {
  145. if (isset($this->dirtyAttributes['local_status']) && $this->dirtyAttributes['local_status'] > 0) {
  146. $this->addError('local_status', ' 仓库正在抓紧配货,请稍后重试..');
  147. return false;
  148. }
  149. }
  150. }
  151. if ($this->confirm_time > 0) {
  152. $order = Order::findOne(['order_no' => $this->order_no, 'is_delete' => 0]);
  153. if (empty($order)) {
  154. $this->addError('order', '订单查询失败');
  155. return false;
  156. }
  157. //订单里程
  158. $store = Store::findOne($order->store_id);
  159. if (empty($store)) {
  160. $this->addError('store', '商城信息查询失败');
  161. return false;
  162. }
  163. $address_data = json_decode($order->address_data, true);
  164. if (empty($address_data)) {
  165. $this->addError('address_data', '地址信息查询失败');
  166. return false;
  167. }
  168. $distance = Tools::getDistance($store->latitude, $store->longitude, $address_data['latitude'], $address_data['longitude']);
  169. $this->distance = $distance;
  170. $sql = "SELECT `di`.`id`
  171. FROM
  172. `cyy_delivery_info` `di`
  173. LEFT JOIN `cyy_order` `o` ON di.order_no = o.order_no
  174. AND o.is_delete = 0
  175. WHERE
  176. `di`.`rider_id` = " . $this->rider_id ?: 0 . " AND `di`.`local_status` = " . self::LOCAL_STATUS_CONFIRM;
  177. $passRateQuery = $sql . " AND `di`.`confirm_time` <= `o`.`delivery_time`";
  178. $passRateQuery .= " GROUP BY `di`.`id`";
  179. $sql .= " GROUP BY `di`.`id`";
  180. $passRateCount = \Yii::$app->db->createCommand($passRateQuery)->query()->count();
  181. $totalCount = \Yii::$app->db->createCommand($sql)->query()->count();
  182. $courier = LocalDeliveryCourier::findOne($this->rider_id);
  183. if (!empty($courier)) {
  184. $courier->pass_rate = round($passRateCount / $totalCount, 2);
  185. if (!$courier->save()) {
  186. $this->addError('courier', '准时率修改失败');
  187. }
  188. }
  189. }
  190. return true;
  191. }
  192. }
  193. public function afterSave($insert, $changedAttributes)
  194. {
  195. parent::afterSave($insert, $changedAttributes);
  196. if($insert){
  197. (new \app\utils\OrderUtil())->LocalDeliveryOrder($this);
  198. }
  199. if(!$insert && $changedAttributes['local_status']){
  200. $order = Order::findOne(['order_no' => $this->order_no]);
  201. \app\modules\admin\models\WastoreForm::afterDeliveryChange($this->store_id, $order);
  202. }
  203. }
  204. }