| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\models;
- use app\utils\Tools;
- use Yii;
- /**
- * This is the model class for table "{{%delivery_info}}".
- *
- * @property integer $id
- * @property integer $store_id
- * @property string $order_no
- * @property string $waybill_id
- * @property integer $status
- * @property integer $reason_id
- * @property integer $fee
- * @property integer $deduct_fee
- * @property integer $delivery_type
- * @property string $delivery_token
- * @property integer $is_delete
- * @property string $rider_name
- * @property string $rider_mobile
- * @property integer $created_at
- * @property integer $updated_at
- * @property integer $mini_id
- * @property integer $address_id
- * @property integer $is_local
- * @property integer $rider_id
- * @property integer $local_status
- * @property integer $arrive_time
- * @property integer $confirm_time
- * @property integer $md_id
- * @property integer $wechat_delivery_type
- * @property integer $wx_order_status
- * @property string $distance
- * @property float $last_latitude
- * @property float $last_longitude
- * @property integer $is_store_delivery_type
- * @property integer $is_front_delivery
- */
- class DeliveryInfo extends \yii\db\ActiveRecord
- {
- /**
- * 未分配骑手
- */
- const LOCAL_STATUS_NO_SEND = 0;
- /**
- * 等待骑手取货
- */
- const LOCAL_STATUS_WAITING = 1;
- /**
- * 骑手配送中
- */
- const LOCAL_STATUS_SENDING = 2;
- /**
- * 配送已完成
- */
- const LOCAL_STATUS_CONFIRM = 3;
- const LOCAL_STATUS_ARR = [
- self::LOCAL_STATUS_NO_SEND => '未分配骑手',
- self::LOCAL_STATUS_WAITING => '等待骑手取货',
- self::LOCAL_STATUS_SENDING => '骑手配送中',
- self::LOCAL_STATUS_CONFIRM => '配送已完成',
- ];
- /**
- * 是否是平台自配订单:是
- */
- const IS_LOCAL_YSE = 1;
- /**
- * 是否是平台自配订单:否
- */
- const IS_LOCAL_NO = 0;
- /**
- * 是否是仓库配送订单:是
- */
- const IS_FRONT_DELIVERY_YSE = 1;
- /**
- * 是否是仓库配送订单:否
- */
- const IS_FRONT_DELIVERY_NO = 0;
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%delivery_info}}';
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['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'],
- [['order_no', 'waybill_id', 'rider_name', 'rider_mobile', 'delivery_token'], 'string'],
- [['fee', 'deduct_fee', 'wx_order_status', 'distance', 'last_latitude', 'last_longitude'], 'number'],
- [['mini_id', 'address_id'], 'safe'],
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'store_id' => 'Store ID',
- 'mini_id' => 'mini_id',
- 'address_id' => 'address_id',
- 'order_no' => '订单号',
- 'waybill_id' => '配送单id',
- 'status' => '配送单状态',
- 'reason_id' => '取消原因id',
- 'fee' => '费用',
- 'deduct_fee' => '取消费用',
- 'delivery_type' => '配送类型',
- 'created_at' => '创建时间',
- 'updated_at' => '状态更新时间',
- 'is_delete' => 'is_delete',
- 'is_local' => '是否是平台自配:0=否,1=是;',
- 'rider_id' => '骑手ID',
- 'local_status' => '同城配送订单状态:0=未分配骑手,1=待取货,2=配送中,3=配送完成',
- 'arrive_time' => '到店时间',
- 'confirm_time' => '送到时间',
- 'serial_num' => '流水号',
- 'wechat_delivery_type' => '微信同城配送类型0即时 1同城',
- 'wx_order_status' => '微信订单状态',
- 'distance' => '配送距离',
- 'last_latitude' => '获取骑手最后一次位置纬度',
- 'last_longitude' => '获取骑手最后一次位置经度',
- 'is_store_delivery_type' => '是否是商城自配',
- 'is_front_delivery' => '是否是仓库配送订单'
- ];
- }
- public function beforeSave($insert)
- {
- if (parent::beforeSave($insert)) {
- //如果是仓库配送订单 未转单成功到仓库时禁止修改状态
- if (!$insert && intval($this->is_front_delivery) === self::IS_FRONT_DELIVERY_YSE) {
- $frontDeliveryOrder = FrontDeliveryOrder::findOne(['delivery_info_id' => $this->id]);
- if (!$frontDeliveryOrder) {
- if (isset($this->dirtyAttributes['local_status']) && $this->dirtyAttributes['local_status'] > 0) {
- $this->addError('local_status', ' 仓库正在抓紧配货,请稍后重试..');
- return false;
- }
- }
- }
- if ($this->confirm_time > 0) {
- $order = Order::findOne(['order_no' => $this->order_no, 'is_delete' => 0]);
- if (empty($order)) {
- $this->addError('order', '订单查询失败');
- return false;
- }
- //订单里程
- $store = Store::findOne($order->store_id);
- if (empty($store)) {
- $this->addError('store', '商城信息查询失败');
- return false;
- }
- $address_data = json_decode($order->address_data, true);
- if (empty($address_data)) {
- $this->addError('address_data', '地址信息查询失败');
- return false;
- }
- $distance = Tools::getDistance($store->latitude, $store->longitude, $address_data['latitude'], $address_data['longitude']);
- $this->distance = $distance;
- $sql = "SELECT `di`.`id`
- FROM
- `cyy_delivery_info` `di`
- LEFT JOIN `cyy_order` `o` ON di.order_no = o.order_no
- AND o.is_delete = 0
- WHERE
- `di`.`rider_id` = " . $this->rider_id ?: 0 . " AND `di`.`local_status` = " . self::LOCAL_STATUS_CONFIRM;
- $passRateQuery = $sql . " AND `di`.`confirm_time` <= `o`.`delivery_time`";
- $passRateQuery .= " GROUP BY `di`.`id`";
- $sql .= " GROUP BY `di`.`id`";
- $passRateCount = \Yii::$app->db->createCommand($passRateQuery)->query()->count();
- $totalCount = \Yii::$app->db->createCommand($sql)->query()->count();
- $courier = LocalDeliveryCourier::findOne($this->rider_id);
- if (!empty($courier)) {
- $courier->pass_rate = round($passRateCount / $totalCount, 2);
- if (!$courier->save()) {
- $this->addError('courier', '准时率修改失败');
- }
- }
- }
- return true;
- }
- }
- public function afterSave($insert, $changedAttributes)
- {
- parent::afterSave($insert, $changedAttributes);
- if($insert){
- (new \app\utils\OrderUtil())->LocalDeliveryOrder($this);
- }
- if(!$insert && $changedAttributes['local_status']){
- $order = Order::findOne(['order_no' => $this->order_no]);
- \app\modules\admin\models\WastoreForm::afterDeliveryChange($this->store_id, $order);
- }
- }
- }
|