admin_id; $driver_id = $this->driver_id; $agentFront = Admin::find()->where(['id' => $admin_id])->select('id, name, mobile, lat, lng, address, province_id, city_id, district_id') ->asArray()->one(); if ($agentFront) { $province = District::findOne($agentFront['province_id'])->name; $city = District::findOne($agentFront['city_id'])->name; $district = District::findOne($agentFront['district_id'])->name; $agentFront['address'] = $province . $city. $district . $agentFront['address']; } $driver = Driver::findOne($driver_id); $driverLine = DriverLine::findOne(['driver_id' => $driver_id, 'status' => DriverLine::STATUS_NOT_CAR_LOADING]); $driverLineOrder = DriverLineOrder::find()->alias('d')->where(['d.line_id' => $driverLine->id]) ->leftJoin(['md' => Md::tableName()], 'd.md_id = md.id')->select('md.id, d.sortkey, md.name, GROUP_CONCAT(d.centralize_goods_ext_id) centralize_goods_ext_id, SUM(d.goods_num) as goods_num') ->groupBy('d.md_id')->orderBy('d.sortkey ASC')->asArray()->all(); $centralize_goods_ext_id_arr = []; foreach ($driverLineOrder as $driverLineOrderItem) { $centralize_goods_ext_id = explode(',', $driverLineOrderItem['centralize_goods_ext_id']); $centralize_goods_ext_id_arr = array_merge($centralize_goods_ext_id_arr, $centralize_goods_ext_id); } $md_arr = DriverMdBind::find()->where(['driver_id' => $driver_id, 'is_delete' => 0])->select('md_id')->column(); //订货数 $goods_count = AgentFrontCentralizeGoodsExt::find()->where(['md_id' => $md_arr, 'is_delete' => 0, 'id' => $centralize_goods_ext_id_arr]) ->select('goods_num')->sum('goods_num') ?? 0; //分拣商品数量 $sorting_goods = DriverLineOrder::find()->where(['line_id' => $driverLine->id])->select('goods_num') ->sum('goods_num') ?? 0; return [ 'code' => 0, 'msg' => '', 'data' => $driverLine ? [ 'id' => $driverLine->id, 'agent_front' => $agentFront, 'line_name' => $driver->line_name, 'driverLineOrder' => $driverLineOrder, 'md_count' => count($driverLineOrder), 'goods_count' => $goods_count, 'sorting_goods' => $sorting_goods, 'bind_md_count' => count($md_arr), 'status' => intval($driverLine->status) ] : null ]; } //装车信息确认 public function driverLineConfirmInfo() { $driver_id = $this->driver_id; $driver_line = DriverLine::findOne(['id' => $this->id, 'driver_id' => $this->driver_id]); $md_arr = DriverMdBind::find()->where(['driver_id' => $driver_id, 'is_delete' => 0])->select('md_id')->column(); // // // $md_arr_count = DriverLineOrder::find()->where(['md_id' => $md_arr, 'line_id' => $driver_line->id]) // ->groupBy('md_id')->select('md_id')->count(); //线货商品数量 / 团货商品数量 $goods_num_arr = DriverLineOrder::find()->alias('dlo')->leftJoin(['a' => AgentFrontCentralizeGoods::tableName()], 'a.id = dlo.centralize_goods_id') ->where(['line_id' => $driver_line->id])->groupBy('a.centralize_goods_type') ->select('SUM(dlo.goods_num) as goods_num, a.centralize_goods_type') ->asArray()->all(); $line_goods_num = 0; $group_goods_num = 0; foreach ($goods_num_arr as $item) { if (!intval($item['centralize_goods_type'])) { $line_goods_num = $item['goods_num']; } else { $group_goods_num = $item['goods_num']; } } $md_info = DriverLineOrder::find()->alias('d')->where(['d.line_id' => $this->id]) ->leftJoin(['md' => Md::tableName()], 'd.md_id = md.id')->select('md.id, d.sortkey, md.name') ->groupBy('d.md_id')->orderBy('d.sortkey ASC')->asArray()->one(); return [ 'code' => 0, 'msg' => '', 'data' => [ 'bind_md_count' => $group_goods_num,//团商品数量 'sorting_goods' => $line_goods_num,//线商品数量 'md_info' => $md_info ] ]; } //点击装车 public function driverLineStart() { try { $driver_line = DriverLine::findOne(['id' => $this->id, 'driver_id' => $this->driver_id]); if (!$driver_line) { throw new \Exception('订单未找到'); } if (intval($driver_line->status) !== DriverLine::STATUS_NOT_CAR_LOADING) { throw new \Exception('已经装车 请勿重复操作'); } if (empty(trim($this->car_loading_pic)) || empty(trim($this->car_state_pic)) || empty(trim($this->car_front_pic))) { throw new \Exception('缺失装车图片'); } $car_state_pic = explode(',', $this->car_state_pic); if (count($car_state_pic) < 3) { throw new \Exception('装车状况图片缺失'); } $driver_line->status = DriverLine::STATUS_WAIT; $driver_line->car_loading_time = time(); $driver_line->car_loading_pic = $this->car_loading_pic; $driver_line->car_state_pic = $this->car_state_pic; $driver_line->car_front_pic = $this->car_front_pic; if (!$driver_line->save()) { throw new \Exception(implode(';', array_values($driver_line->firstErrors))); } return [ 'code' => 0, 'msg' => '操作成功', ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage(), ]; } } //获取待送达列表 public function getDriverLineOrderList() { $driver_id = $this->driver_id; $driverLine = DriverLine::find()->where([ 'driver_id' => $driver_id, 'status' => [DriverLine::STATUS_WAIT, DriverLine::STATUS_DOING] ])->orderBy('id')->asArray()->one(); //获取需要配送的门店位置以及经纬度数量序号等信息 $query = DriverLineOrder::find()->alias('d') ->leftJoin(['dl' => DriverLine::tableName()], 'dl.id = d.line_id') ->leftJoin(['md' => Md::tableName()], 'd.md_id = md.id') ->where(['dl.id' => $driverLine['id']]) ->select('d.id, md.id md_id, md.cover_url, d.sortkey, md.name, md.mobile, md.latitude, md.longitude, md.address, md.province, md.city, md.district, SUM(d.goods_num) as goods_num, d.line_id, d.status, dl.status as dl_status') ->groupBy('d.md_id')->orderBy('d.status ASC, d.sortkey ASC'); $list = pagination_make($query); foreach ($list['list'] as &$item) { $item['province'] = District::findOne($item['province'])->name ?? ''; $item['city'] = District::findOne($item['city'])->name ?? ''; $item['district'] = District::findOne($item['district'])->name ?? ''; $item['status'] = intval($item['status']); } return [ 'code' => 0, 'msg' => '', 'data' => $list ]; } //获取团点坐标以及线路 public function getDriverToMd() { $id = $this->id; if (!$id) { return [ 'code' => 1, 'msg' => '参数错误', ]; } $driver_id = $this->driver_id; $admin_id = $this->admin_id; $latitude = $this->latitude; $longitude = $this->longitude; // $driver = Driver::findOne($driver_id); $driverLine = DriverLine::findOne(['id' => $id, 'driver_id' => $driver_id]); //获取固定的自定义排序 $customSort = DriverLineOrderCustomSort::find()->where(['driver_id' => $driver_id, 'is_delete' => 0]) ->orderBy('sort ASC, id ASC') ->asArray()->all(); //获取当前所有的配送门店ID $driverLineOrderMdId = DriverLineOrder::find()->alias('d')->where(['d.line_id' => $driverLine->id]) ->leftJoin(['md' => Md::tableName()], 'd.md_id = md.id')->select('md.id') ->groupBy('d.md_id')->orderBy('d.sortkey')->column(); if ($customSort) { //找到当前固定排序中之前没有添加过的门店 $customSortMdId = array_column($customSort, 'md_id'); $md_diff = array_diff($driverLineOrderMdId, $customSortMdId); $md_diff = array_values($md_diff); if ($md_diff) { //将新增的几条门店数据前置一下 DriverLineOrderCustomSort::updateAllCounters(['sort' => count($md_diff)], ['driver_id' => $driver_id, 'is_delete' => 0]); foreach ($md_diff as $md_diff_index => $md_diff_item) { $customSortItem = new DriverLineOrderCustomSort(); $customSortItem->driver_id = $driver_id; $customSortItem->md_id = $md_diff_item; $customSortItem->sort = ($md_diff_index + 1); $customSortItem->save(); } $customSortMdId = array_merge($customSortMdId, $md_diff); } } else { foreach ($driverLineOrderMdId as $md_id_index => $md_id_item) { $customSortItem = new DriverLineOrderCustomSort(); $customSortItem->driver_id = $driver_id; $customSortItem->md_id = $md_id_item; $customSortItem->sort = ($md_id_index + 1); $customSortItem->save(); } $customSortMdId = $driverLineOrderMdId; } $customSortMdId = implode(',', $customSortMdId); //获取需要配送的门店位置以及经纬度数量序号等信息 $driverLineOrder = DriverLineOrder::find()->alias('d')->where(['d.line_id' => $driverLine->id]) ->leftJoin(['md' => Md::tableName()], 'd.md_id = md.id')->select('d.id, md.id md_id, md.cover_url, d.sortkey, md.name, md.mobile, md.latitude, md.longitude, md.address, md.province, md.city, md.district, SUM(d.goods_num) as goods_num') ->groupBy('d.md_id')->orderBy(new Expression('FIELD(md.id,'.$customSortMdId.')'))->asArray()->all();; $md_address = []; //腾讯地图限制 途径点只能每次添加30个 $count_index = 0; foreach ($driverLineOrder as $index => &$item) { $md_address[$count_index] = $md_address[$count_index] ?? []; array_push($md_address[$count_index], ['latitude' => $item['latitude'], 'longitude' => $item['longitude'], 'sortkey' => $item['sortkey']]); // $waypoints .= $item['latitude'] . ',' . $item['longitude'] . ';'; if (($index + 1) % 30 == 0) {//30 60 90 ...//腾讯地图限制 途径点只能每次添加30个 ++$count_index; } $item['province'] = District::findOne($item['province'])->name ?? ''; $item['city'] = District::findOne($item['city'])->name ?? ''; $item['district'] = District::findOne($item['district'])->name ?? ''; } //获取当前位置到每个门店(途径点)的路线规划坐标 $address_line = []; if ($latitude && $longitude) { foreach ($md_address as $address_index => $address_item) { $waypoints = ''; foreach ($address_item as $address_child_item) { $waypoints .= $address_child_item['latitude'] . ',' . $address_child_item['longitude'] . ';'; } $form = !$address_index ? ($latitude . ',' . $longitude) : ($address_item[0]['latitude'] . ',' . $address_item[0]['longitude']); $to = $address_item[count($address_item) - 1]['latitude'] . ',' . $address_item[count($address_item) - 1]['longitude']; $waypoints = substr($waypoints, 0, strlen($waypoints) - 1); //请求腾讯地图接口 获取路线规划点的经纬度 $address_line_ = $this->getAddress($form, $to, $waypoints); if (!$address_line_) { return [ 'code' => 1, 'msg' => '接口错误 请求失败', ]; } $address_line = array_merge($address_line, $address_line_); } } //获取前端所需要的地图上显示的坐标点 $md_address_data = []; foreach ($md_address as $address_item) { $md_address_data = array_merge($md_address_data, $address_item); } //获取仓库坐标点 $admin = Admin::findOne($admin_id); $agent_front_address = [ 'latitude' => $admin->lat, 'longitude' => $admin->lng, ]; $md_sortkey = array_column($driverLineOrder, 'sortkey'); return [ 'code' => 0, 'msg' => '', 'data' => [ 'id' => $id, 'md_list' => $driverLineOrder, 'address_line' => $address_line, 'agent_front_address' => $agent_front_address, 'md_address' => $md_address_data, 'status' => intval($driverLine->status), 'md_sortkey' => $md_sortkey ] ]; } //获取点单团配送的信息 public function getLineOrderInfo() { $id = $this->id; $md_id = $this->md_id; if (!$id || !$md_id) { return [ 'code' => 1, 'msg' => '参数错误', ]; } $driverLineOrder = DriverLineOrder::find()->alias('d')->where(['d.line_id' => $id, 'd.md_id' => $md_id]) ->leftJoin(['md' => Md::tableName()], 'd.md_id = md.id') ->select("md.id md_id, md.name, SUM(d.goods_num) as goods_num, GROUP_CONCAT(d.centralize_goods_ext_id) AS centralize_goods_ext_id, d.sortkey, d.status, d.order_finish_pic, d.md_head_pic, SUM(d.cancel_goods_num) as cancel_goods_num, d.real_num") ->groupBy('d.md_id')->asArray()->one(); if (!$driverLineOrder) { return [ 'code' => 1, 'msg' => '数据错误' ]; } $driverLineOrder['centralize_goods_ext_id'] = explode(',', $driverLineOrder['centralize_goods_ext_id']); $driverLineOrder['centralize_goods_num'] = AgentFrontCentralizeGoodsExt::find()->where([ 'id' => $driverLineOrder['centralize_goods_ext_id'], 'is_delete' => 0 ])->select('goods_num')->sum('goods_num') ?: 0; // $group_sort_key = AgentFrontCentralizeGoodsExt::find()->where(['id' => $driverLineOrder['centralize_goods_ext_id'], 'is_delete' => 0]) // ->select('sort_key')->scalar() ?: 0; // if ($group_sort_key > 0) { // $driverLineOrder['name'] = $driverLineOrder['name']; // } $driverLineOrder['status'] = intval($driverLineOrder['status']); if (!$driverLineOrder['status']) { $driverLineOrder['goods_num'] = 0; } $driverLineOrder['order_num'] = $driverLineOrder['centralize_goods_num'];//页面上有当前订货和应交 感觉应该为同一个参数 unset($driverLineOrder['centralize_goods_ext_id']); return [ 'code' => 0, 'msg' => '', 'data' => $driverLineOrder ]; } //线路配送详情明细 public function getLineOrderDetail() { try { $id = $this->id; $md_id = $this->md_id; $sortkey = $this->sortkey; $type = $this->type; $centralize_goods_type = $this->centralize_goods_type; $keyword = $this->keyword; if (!$id || (!$md_id && !isset($sortkey))) { throw new \Exception('参数错误'); } //详情头部团信息 $where = ['d.line_id' => $id]; if ($md_id) { $where['d.md_id'] = $md_id; } $query = DriverLineOrder::find()->alias('d')->where($where) ->leftJoin(['ge' => AgentFrontCentralizeGoodsExt::tableName()], 'ge.id = d.centralize_goods_ext_id') ->leftJoin(['md' => Md::tableName()], 'd.md_id = md.id'); $orderBy = 'ASC';//下一个团 if ($type) {//上一个团 $orderBy = 'DESC'; if (isset($sortkey) && is_numeric($sortkey)) { $query->andWhere(['<', 'd.sortkey', $sortkey]); } } else { if (isset($sortkey) && is_numeric($sortkey)) { $query->andWhere(['>', 'd.sortkey', $sortkey]); } } $md_info = $query->select("md.id md_id, md.name, d.sortkey, GROUP_CONCAT(d.centralize_goods_ext_id) AS centralize_goods_ext_id") ->orderBy('d.sortkey ' . $orderBy)->groupBy('md_id') ->asArray()->one(); $list['goods_list'] = [ [ 'centralize_goods_type' => 0, 'goods_race' => 0, 'goods_num' => 0, 'list' => [] ], [ 'centralize_goods_type' => 1, 'goods_race' => 0, 'goods_num' => 0, 'list' => [] ] ]; //商品信息 if ($md_info) { if ($md_info['centralize_goods_ext_id']) { // $centralize_goods_ext_id = explode(',', $md_info['centralize_goods_ext_id']); // $agentFrontCentralizeGoodsExt = AgentFrontCentralizeGoodsExt::findOne($centralize_goods_ext_id[0]); // if ($agentFrontCentralizeGoodsExt->sort_key > 0) { // $md_info['name'] = $md_info['name'] . '[团货' . $agentFrontCentralizeGoodsExt->sort_key . ']'; // } // $query = AgentFrontCentralizeGoodsExt::find()->alias('ge') // ->leftJoin(['g' => AgentFrontCentralizeGoods::tableName()], 'g.id = ge.centralize_goods_id') // ->leftJoin(['d' => DriverLineOrder::tableName()], 'ge.id = d.centralize_goods_ext_id AND g.id = d.centralize_goods_id') // ->where(['ge.id' => $centralize_goods_ext_id, 'ge.is_delete' => 0, 'ge.md_id' => $md_info['md_id'], 'd.line_id' => $id]); $query = DriverLineOrder::find()->alias('d') ->leftJoin(['ge' => AgentFrontCentralizeGoodsExt::tableName()], 'd.centralize_goods_ext_id = ge.id') ->leftJoin(['g' => AgentFrontCentralizeGoods::tableName()], 'd.centralize_goods_id = g.id') ->where(['ge.is_delete' => 0, 'd.md_id' => $md_info['md_id'], 'd.line_id' => $id])->groupBy('d.centralize_goods_id'); if (!empty(trim($keyword))) { $query->andWhere(['OR', ['LIKE', 'g.goods_name', $keyword], ['LIKE', 'g.goods_no', $keyword]]); } //线商品数量以及是否缺少商品 $line_goods_query = (clone $query)->andWhere(['g.centralize_goods_type' => 0]) ->select('SUM(d.goods_num) as sorting_num, ge.goods_num, g.centralize_goods_type,ge.id,ge.centralize_goods_id, g.goods_name,g.pic_url,g.goods_no,g.attr_info, SUM(d.cancel_goods_num) as cancel_goods_num '); //团商品数量以及是否缺少商品 $group_goods_query = (clone $query)->andWhere(['g.centralize_goods_type' => 1]) ->select('SUM(d.goods_num) as sorting_num,ge.goods_num, g.centralize_goods_type,ge.id,ge.centralize_goods_id, g.goods_name,g.pic_url,g.goods_no,g.attr_info, SUM(d.cancel_goods_num) as cancel_goods_num'); // if (is_numeric($centralize_goods_type) && in_array($centralize_goods_type, [0, 1])) { // $query->andWhere(['g.centralize_goods_type' => $centralize_goods_type]); // } //线商品类型列表分拣商品种类以及数量 $line_total_data = (clone $line_goods_query) ->asArray()->all(); //团商品类型列表分拣商品种类以及数量 $group_total_data = (clone $group_goods_query) ->asArray()->all(); $list['goods_list'] = [ [ 'centralize_goods_type' => 0, 'goods_race' => count($line_total_data), 'goods_num' => array_sum(array_column($line_total_data, 'sorting_num')) ?: 0, 'list' => $line_goods_query->addSelect('ge.id as centralize_goods_ext_id')->asArray()->all() ], [ 'centralize_goods_type' => 1, 'goods_race' => count($group_total_data), 'goods_num' => array_sum(array_column($group_total_data, 'sorting_num')) ?: 0, 'list' => $group_goods_query->addSelect('ge.id as centralize_goods_ext_id')->asArray()->all() ] ]; foreach ($list['goods_list'] as &$tab_item) { foreach ($tab_item['list'] as &$item) { $md_goods_num = DriverLineOrder::find() ->where(['md_id' => $this->md_id, 'centralize_goods_ext_id' => $item['centralize_goods_ext_id']]) ->sum('goods_num') ?: 0; $item['is_lack'] = 0; if ($md_goods_num < $item['goods_num']) { $item['is_lack'] = 1; } $item['attr_info'] = json_decode($item['attr_info'], true); $item['centralize_goods_type'] = intval($item['centralize_goods_type']); $driverLineOrder = DriverLineOrder::find()->where([ 'centralize_goods_ext_id' => $item['centralize_goods_ext_id'], 'md_id' => $md_info['md_id'] ]) ->select('created_at')->orderBy('id DESC')->one(); // $AgentFrontCentralizeGoodsExtLast = AgentFrontCentralizeGoodsExt::find()->where(['centralize_goods_id' => $item['centralize_goods_id']]) // ->orderBy('id desc')->select('created_at')->asArray()->one(); $item['created_at'] = date('Y-m-d H:i:s', $driverLineOrder['created_at']); $item['real_num'] = ($item['sorting_num'] - $item['cancel_goods_num']); } } // $list = pagination_make($query); // $list['goods_race'] = count($total_data); // $list['goods_num'] = array_sum(array_column($total_data, 'sorting_num')) ?: 0; } } $list['tab_list'] = [ [ 'is_lack' => (array_sum(array_column($line_goods_list ?? [], 'goods_num')) ?: 0) - (array_sum(array_column($line_goods_list ?? [], 'sorting_num')) ?: 0) > 0, 'centralize_goods_type_text' => '线商品', 'centralize_goods_type' => 0 ], [ 'goods_num' => array_sum(array_column($group_goods_list ?? [], 'goods_num')), 'sorting_num' => array_sum(array_column($group_goods_list ?? [], 'sorting_num')), 'is_lack' => (array_sum(array_column($group_goods_list ?? [], 'goods_num')) ?: 0) - (array_sum(array_column($group_goods_list ?? [], 'sorting_num')) ?: 0) > 0, 'centralize_goods_type_text' => '团商品', 'centralize_goods_type' => 1 ], ]; $list['md_info'] = $md_info; $list['next_md_info'] = (bool)DriverLineOrder::find()->where(['line_id' => $id])->andWhere(['>', 'sortkey', $md_info['sortkey']]) ->orderBy('sortkey ASC')->one(); $list['prev_md_info'] = (bool)DriverLineOrder::find()->where(['line_id' => $id])->andWhere(['<', 'sortkey', $md_info['sortkey']]) ->orderBy('sortkey DESC')->one(); return [ 'code' => 0, 'msg' => '', 'data' => $list ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //线路团点配送详情明细(按照团线商品区分 不做团线tab) public function getLineMdDetail() { try { $id = $this->id; $md_id = $this->md_id; $sortkey = $this->sortkey; $centralize_goods_type = $this->centralize_goods_type; if (!$id || (!$md_id && !isset($sortkey))) { throw new \Exception('参数错误'); } //详情头部团信息 $where = ['d.line_id' => $id]; $query = DriverLineOrder::find()->alias('d')->where($where) ->leftJoin(['ge' => AgentFrontCentralizeGoodsExt::tableName()], 'ge.id = d.centralize_goods_ext_id') ->leftJoin(['md' => Md::tableName()], 'd.md_id = md.id'); if (is_numeric($sortkey)) {//下一个团 $query->andWhere(['>', 'd.sortkey', $sortkey]); } else { //指定团 if ($md_id) { $query->andWhere(['d.md_id' => $md_id]); } } $md_info = $query->select("md.id md_id, md.name, d.sortkey, GROUP_CONCAT(d.centralize_goods_ext_id) AS centralize_goods_ext_id") ->orderBy('d.sortkey ASC')->groupBy('md_id') ->asArray()->one(); $list = []; //商品信息 if ($md_info) { if ($md_info['centralize_goods_ext_id']) { $centralize_goods_ext_id = explode(',', $md_info['centralize_goods_ext_id']); $agentFrontCentralizeGoodsExt = AgentFrontCentralizeGoodsExt::findOne($centralize_goods_ext_id[0]); if ($agentFrontCentralizeGoodsExt->sort_key > 0) { $md_info['name'] = $md_info['name'] . '[团货' . $agentFrontCentralizeGoodsExt->sort_key . ']'; } $query = AgentFrontCentralizeGoodsExt::find()->alias('ge') ->leftJoin(['g' => AgentFrontCentralizeGoods::tableName()], 'g.id = ge.centralize_goods_id') ->where(['ge.id' => $centralize_goods_ext_id, 'ge.is_delete' => 0]); if (is_numeric($centralize_goods_type) && in_array($centralize_goods_type, [0, 1])) { $query->andWhere(['g.centralize_goods_type' => $centralize_goods_type]); } $query->select('ge.id, ge.centralize_goods_id, g.goods_name, SUM(ge.sorting_num) AS sorting_num, SUM(ge.goods_num) AS goods_num, g.pic_url, g.goods_no, g.attr_info, g.centralize_goods_type'); $query->groupBy('g.id'); $list = pagination_make($query); foreach ($list['list'] as &$item) { $AgentFrontCentralizeGoodsExtLast = AgentFrontCentralizeGoodsExt::find()->where(['centralize_goods_id' => $item['centralize_goods_id']]) ->orderBy('id desc')->select('created_at')->asArray()->one(); $item['created_at'] = date('Y-m-d H:i:s', $AgentFrontCentralizeGoodsExtLast['created_at']); $item['attr_info'] = json_decode($item['attr_info'], true); $item['centralize_goods_type'] = intval($item['centralize_goods_type']); } } } $list['md_info'] = $md_info; $list['next_md_info'] = (bool)DriverLineOrder::find()->where(['line_id' => $id])->andWhere(['>', 'sortkey', $md_info['sortkey']]) ->orderBy('sortkey ASC')->one(); return [ 'code' => 0, 'msg' => '', 'data' => $list ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //去配送 public function goDeliveryOrder() { try { $id = $this->id; $md_id = $this->md_id; $order_finish_pic = $this->order_finish_pic; $md_head_pic = $this->md_head_pic; $real_num = $this->real_num; if (empty($id) || empty($md_id)) { throw new \Exception('参数错误'); } if (empty($order_finish_pic)) { throw new \Exception('缺失送达照片'); } if (empty($md_head_pic)) { throw new \Exception('缺失门头照片'); } $driverLine = DriverLine::findOne($id); if (!$driverLine) { throw new \Exception('数据错误 当前线路订单不存在'); } if (!in_array($driverLine->status, [DriverLine::STATUS_WAIT, DriverLine::STATUS_DOING])) { throw new \Exception('数据错误 当前线路订单状态错误' . $driverLine->status); } $driverLineOrder = DriverLineOrder::findOne(['line_id' => $id, 'md_id' => $md_id]); if (!$driverLineOrder) { throw new \Exception('数据错误'); } DriverLineOrder::updateAll([ 'md_head_pic' => $md_head_pic, 'order_finish_pic' => $order_finish_pic, 'status' => DriverLine::STATUS_FINISH, 'real_num' => $real_num, 'finish_time' => time() ], [ 'line_id' => $id, 'md_id' => $md_id ]); $driverLineOrder = DriverLineOrder::findOne(['line_id' => $id, 'status' => DriverLine::STATUS_WAIT]); if ($driverLineOrder) { $driverLine->status = DriverLine::STATUS_DOING; } else { $driverLine->status = DriverLine::STATUS_FINISH; $driverLine->finish_time = time(); } if ($driverLine->start_time <= 0) { $driverLine->start_time = time(); } if (!$driverLine->save()) { throw new \Exception(implode(';', array_values($driverLine->firstErrors))); } return [ 'code' => 0, 'msg' => '操作成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //已完成运单 public function finishOrderList() { try { $driver_id = $this->driver_id; $query = DriverLine::find()->where(['driver_id' => $driver_id, 'is_delete' => 0, 'status' => DriverLine::STATUS_FINISH]); $query->select('id, finish_time, line_no')->orderBy('id DESC'); $list = pagination_make($query); foreach ($list['list'] as &$item) { $driverLineOrder = DriverLineOrder::find()->where(['line_id' => $item['id'], 'is_delete' => 0])->select('md_id, SUM(goods_num) as goods_num') ->groupBy('md_id')->asArray()->all(); $item['md_count'] = count($driverLineOrder);//成功的团数量 $item['goods_num'] = array_sum(array_column($driverLineOrder ?? [], 'goods_num'));//成功的商品数量 $item['finish_time'] = $item['finish_time'] > 0 ? date('Y-m-d H:i:s', $item['finish_time']) : ''; $item['fail_md_count'] = 0;//失败团数量 $item['fail_goods_num'] = 0;//失败商品数量 } return [ 'code' => 0, 'msg' => '', 'data' => $list ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //已完成运单详情 public function finishOrderDetail() { try { $driver_id = $this->driver_id; $id = $this->id; $centralize_goods_type = $this->centralize_goods_type; $driverLine = DriverLine::findOne(['id' => $id, 'driver_id' => $driver_id]); if (!$driverLine) { throw new \Exception('数据错误'); } $query = DriverLineOrder::find()->alias('d')->where(['d.line_id' => $id]) ->leftJoin(['ge' => AgentFrontCentralizeGoodsExt::tableName()], 'ge.id = d.centralize_goods_ext_id') ->leftJoin(['g' => AgentFrontCentralizeGoods::tableName()], 'g.id = ge.centralize_goods_id') ->leftJoin(['md' => Md::tableName()], 'd.md_id = md.id'); if (is_numeric($centralize_goods_type) && in_array($centralize_goods_type, [0, 1])) { $query->andWhere([ 'g.centralize_goods_type' => $centralize_goods_type ]); } $query->select('d.id, md.id md_id, md.cover_url, d.sortkey, md.name, md.mobile, md.latitude, md.longitude, md.address, md.province, md.city, md.district, SUM(d.goods_num) as goods_num, SUM(ge.goods_num) as order_num, d.finish_time, d.status, ge.id centralize_goods_ext_id') ->groupBy('d.md_id')->orderBy('d.sortkey'); $list = pagination_make($query); foreach ($list['list'] as &$item) { $group_sort_key = AgentFrontCentralizeGoodsExt::find()->where(['id' => $item['centralize_goods_ext_id'], 'is_delete' => 0]) ->select('sort_key')->scalar() ?: 0; if ($group_sort_key > 0) { $item['name'] = $item['name'] . '[团货' . $group_sort_key . ']'; } $item['province'] = District::findOne($item['province'])->name ?? ''; $item['city'] = District::findOne($item['city'])->name ?? ''; $item['district'] = District::findOne($item['district'])->name ?? ''; $item['finish_time'] = $item['finish_time'] > 0 ? date('Y-m-d H:i:s', $item['finish_time']) : ''; $item['status'] = intval($item['status']); } return [ 'code' => 0, 'msg' => '', 'data' => $list ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //查询商品 public function searchGoods() { $driver_id = $this->driver_id; $centralize_goods_type = $this->centralize_goods_type; $id = $this->id; $md_arr = DriverMdBind::find()->where(['driver_id' => $driver_id, 'is_delete' => 0]) ->select('md_id')->column(); $driverLine = DriverLine::findOne($id); if (!$driverLine) { return [ 'code' => 1, 'msg' => '数据错误' ]; } $query = DriverLineOrder::find()->alias('d')->where(['d.line_id' => $id, 'd.md_id' => $md_arr]) ->leftJoin(['ge' => AgentFrontCentralizeGoodsExt::tableName()], 'ge.id = d.centralize_goods_ext_id') ->leftJoin(['g' => AgentFrontCentralizeGoods::tableName()], 'g.id = ge.centralize_goods_id'); if (is_numeric($centralize_goods_type) && in_array($centralize_goods_type, [0, 1])) { $query->andWhere([ 'g.centralize_goods_type' => $centralize_goods_type ]); } $query ->groupBy('ge.centralize_goods_id') ->select('ge.id, ge.centralize_goods_id, SUM(ge.goods_num) as goods_num, g.goods_name, g.attr_info, g.goods_no, SUM(ge.sorting_num) as sorting_num, g.pic_url, d.created_at'); $list = pagination_make($query); foreach ($list['list'] as &$item) { $item['attr_info'] = json_decode($item['attr_info'], true); $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']); } return [ 'code' => 0, 'msg' => '', 'data' => $list ]; } //请求腾讯地图接口 (驾车路线规划 预计要换成货车路线规划 因为有地区限行) public function getAddress($from, $to, $waypoints) { $url = 'https://apis.map.qq.com/ws/direction/v1/driving?'; $tencent_map_key = Option::get('tencent_map_key', 0, 'saas', '')['value']; $params = [ 'key' => $tencent_map_key, 'from' => $from, 'to' => $to, //'size' => '4', //'trailer_type' => 2, 'waypoints' => $waypoints, 'with_dest' => 0 ]; $url .= http_build_query($params); // var_dump($url); $json = file_get_contents($url); // var_dump($json); // $json = '{"status":0,"message":"query ok","request_id":"3940191711926976785","result":{"rows":[{"elements":[{"distance":7040,"duration":1146},{"distance":12416,"duration":1501}]},{"elements":[{"distance":6132,"duration":847},{"distance":9476,"duration":1049}]}]}}'; $res = json_decode($json, true); //腾讯提供的换算接口 if ($res['status'] == 0 && isset($res['result']['routes'][0]['polyline'])) { $polyline = $res['result']['routes'][0]['polyline']; for ($i = 2; $i < count($polyline); $i++) { $polyline[$i] = $polyline[$i - 2] + $polyline[$i] / 1000000; } $data = []; $index_ = 0; foreach ($polyline as $index => $item) { if ($index % 2 == 0) { $data[$index_] = ['latitude' => $item, 'longitude' => $polyline[$index + 1]]; } else { ++$index_; } } return $data; } return null; } /** * 修改配送顺序 */ public function setLineOrderSort() { $t = \Yii::$app->db->beginTransaction(); try { $id = $this->id; $front_agent_admin_id = $this->admin_id; $driver_id = $this->driver_id; $sortKey = $this->sortkey; if ($sortKey === null) { throw new \Exception('缺少配送序号'); } $line_order = DriverLineOrder::findOne($id); if (!$line_order) { throw new \Exception('订单不存在'); } if ((string)$line_order->sortkey === (string)$sortKey) { return [ 'code' => 0, 'msg' => '修改成功' ]; } $driverLine = DriverLine::findOne($line_order->line_id); if ($driverLine) { if (!in_array($driverLine->status, [DriverLine::STATUS_WAIT, DriverLine::STATUS_NOT_CAR_LOADING]) ) { throw new \Exception('当前状态不可修改配送序号'); } } //增加操作日志(获取配置) $oldDriverLineOrderCustomSort = DriverLineOrderCustomSort::findOne([ 'driver_id' => $driver_id, 'is_delete' => 0, 'md_id' => $line_order->md_id ]); $line_order_key = DriverLineOrder::findOne(['line_id' => $line_order->line_id, 'sortkey' => $sortKey]); if ($line_order_key) { DriverLineOrder::updateAll(['sortkey' => $line_order->sortkey], [ 'line_id' => $line_order_key->line_id, 'md_id' => $line_order_key->md_id ]); $md = Md::findOne($line_order_key->md_id); if ($md && $oldDriverLineOrderCustomSort->sort) { $driverLineOrderCustomSort = DriverLineOrderCustomSort::findOne([ 'driver_id' => $driver_id, 'is_delete' => 0, 'md_id' => $md->id ]); if ($driverLineOrderCustomSort) { $old_sort = $oldDriverLineOrderCustomSort->sort; $oldDriverLineOrderCustomSort->sort = $driverLineOrderCustomSort->sort; $oldDriverLineOrderCustomSort->save(); $driverLineOrderCustomSort->sort = $old_sort; $driverLineOrderCustomSort->save(); } } //增加操作日志(增加记录) $result = AgentFrontStaffOperateLog::addOperateLog( AgentFrontStaffOperateLog::OPERATE_TYPE_SET_ROUTE, $driver_id, $front_agent_admin_id, $line_order->line_id, "修改配送顺序:投篮单号:" . $line_order_key->line_no . ';配送门店:' . $md->name . '(' . $line_order_key->md_id . ')' . ';顺序:' . $line_order->sortkey, AgentFrontStaffOperateLog::IS_DRIVER_YES ); } DriverLineOrder::updateAll(['sortkey' => $sortKey], ['line_id' => $line_order->line_id, 'md_id' => $line_order->md_id]); $md = Md::findOne($line_order->md_id); //增加操作日志(增加记录) AgentFrontStaffOperateLog::addOperateLog( AgentFrontStaffOperateLog::OPERATE_TYPE_SET_ROUTE, $driver_id, $front_agent_admin_id, $line_order->line_id, "修改配送顺序:投篮单号:" . $line_order_key->line_no . ';配送门店:' . $md->name . '(' . $line_order->md_id . ')' . ';顺序:' . $sortKey, AgentFrontStaffOperateLog::IS_DRIVER_YES ); $t->commit(); return [ 'code' => 0, 'msg' => '操作成功' ]; } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //获取退货列表 public function goodsRefundList() { try { $driver_id = $this->driver_id; $status = intval($this->status); $refund_type = intval($this->refund_type); $start_time = $this->start_time; $md_id_list = DriverMdBind::find()->where(['driver_id' => $driver_id, 'is_delete' => 0]) ->select('md_id')->column(); $query = AgentFrontCentralizeGoodsRefundLog::find() ->where(['md_id' => $md_id_list ?: [0]]); if (in_array($refund_type, [AgentFrontCentralizeGoodsRefundLog::REFUND_TYPE_VISIT, AgentFrontCentralizeGoodsRefundLog::REFUND_TYPE_BACK_MD])) { $query->andWhere(['refund_type' => $refund_type]); } if (!empty(trim($start_time))) { $start_time = strtotime($start_time); $query->andWhere(['>=', 'created_at' , $start_time]); } $wait_count = (clone $query)->andWhere(['status' => 0])->count(); $success_count = (clone $query)->andWhere(['status' => 1])->count(); if (in_array($status, [0, 1])) { $query->andWhere(['status' => $status]); } $query->select('id, md_id, status, refund_type, longitude, latitude, name, mobile, province_id, city_id, district_id, address, created_at'); $list = pagination_make($query); foreach ($list['list'] as &$item) { $item['start_time'] = ''; $item['end_time'] = ''; $item['open_status'] = 0; if (intval($item['refund_type']) === AgentFrontCentralizeGoodsRefundLog::REFUND_TYPE_BACK_MD) { $md = Md::findOne($item['md_id']); if ($md) { $item['name'] = $md->name; $item['mobile'] = $md->mobile; $item['address'] = $md->address; $item['longitude'] = $md->longitude; $item['latitude'] = $md->latitude; $item['province_id'] = $md->province; $item['city_id'] = $md->city; $item['district_id'] = $md->district; $item['start_time'] = $md->start_time; $item['end_time'] = $md->end_time; $item['open_status'] = intval($md->open_status); } } $item['province'] = District::findOne($item['province_id'])->name ?? ''; $item['city'] = District::findOne($item['city_id'])->name ?? ''; $item['district'] = District::findOne($item['district_id'])->name ?? ''; $item['refund_type'] = intval($item['refund_type']); $item['open_status'] = intval($item['open_status']); $item['status'] = intval($item['status']); $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']); } $list['wait_count'] = $wait_count; $list['success_count'] = $success_count; return [ 'code' => 0, 'msg' => '', 'data' => $list ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //退货取回详情 public function refundDetail() { try { $id = $this->id; $driver_id = $this->driver_id; $refund_log = AgentFrontCentralizeGoodsRefundLog::findOne($id); if (!$refund_log) { return [ 'code' => 1, 'msg' => '数据不存在' ]; } $md_id_list = DriverMdBind::find()->where(['driver_id' => $driver_id, 'is_delete' => 0]) ->select('md_id')->column(); if (!in_array($refund_log->md_id, $md_id_list)) { return [ 'code' => 1, 'msg' => '无取回权限' ]; } // if (intval($refund_log->status)) { // return [ // 'code' => 1, // 'msg' => '当前已完成取货 请勿重复操作' // ]; // } $address = $refund_log->address; $mobile = $refund_log->mobile; if (intval($refund_log->refund_type) === 2) { $md = Md::findOne($refund_log->md_id); if (!$md) { return [ 'code' => 1, 'msg' => '门店不存在' ]; } $address = $md->name; $mobile = $md->mobile; } $centralize_goods_id = explode(',', $refund_log->centralize_goods_id); $goods = []; foreach ($centralize_goods_id as $item) { $agentFrontCentralizeGoods = AgentFrontCentralizeGoods::findOne($item); $goods[] = [ 'pic_url' => $agentFrontCentralizeGoods->pic_url, 'goods_name' => $agentFrontCentralizeGoods->goods_name, 'attr_info' => json_decode($agentFrontCentralizeGoods->attr_info, true), 'goods_no' => $agentFrontCentralizeGoods->goods_no, ]; } $goods_num_info = json_decode($refund_log->goods_num_info, true) ?? []; $goods_num = array_sum(array_column($goods_num_info, 'num')); return [ 'code' => 0, 'msg' => '', 'data' => [ 'goods_list' => $goods, 'goods_num' => $goods_num, 'goods_race' => count($goods_num_info), 'address' => $address, 'mobile' => $mobile, 'is_confirm_goods' => $refund_log->is_confirm_goods, 'main_goods_pic' => $refund_log->main_goods_pic, 'other_goods_pic' => $refund_log->other_goods_pic, 'real_goods_num' => $refund_log->goods_num, 'status' => intval($refund_log->status) ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //商品退回取货 public function refundConfirm() { $t = \Yii::$app->db->beginTransaction(); try { $id = $this->id; $driver_id = $this->driver_id; $is_confirm_goods = $this->is_confirm_goods; $status = $this->status; $goods_num = $this->goods_num; $main_goods_pic = $this->main_goods_pic; $other_goods_pic = $this->other_goods_pic; $refund_log = AgentFrontCentralizeGoodsRefundLog::findOne($id); if (!$refund_log) { return [ 'code' => 1, 'msg' => '数据不存在' ]; } $md_id_list = DriverMdBind::find()->where(['driver_id' => $driver_id, 'is_delete' => 0]) ->select('md_id')->column(); if (!in_array($refund_log->md_id, $md_id_list)) { return [ 'code' => 1, 'msg' => '无取回权限' ]; } if (intval($refund_log->status)) { return [ 'code' => 1, 'msg' => '当前已完成取货 请勿重复操作' ]; } if (intval($status)) { $refund_log->status = AgentFrontCentralizeGoodsRefundLog::STATUS_TAKE_ERROR; } else { if (empty($main_goods_pic)) { throw new \Exception('缺少高价值产品图片'); } if ($goods_num <= 0) { throw new \Exception('实收数量必须大于0'); } $refund_log->is_confirm_goods = $is_confirm_goods; $refund_log->driver_id = $driver_id; $refund_log->goods_num = $goods_num; $refund_log->main_goods_pic = $main_goods_pic; $refund_log->other_goods_pic = $other_goods_pic; $refund_log->confirm_time = time(); $refund_log->status = AgentFrontCentralizeGoodsRefundLog::STATUS_TAKE_SUCCESS; } if (!$refund_log->save()) { throw new \Exception('操作失败'); }; $orderRefund = OrderRefund::findOne($refund_log->refund_id); $orderTransit = OrderTransit::findOne(['order_id' => $orderRefund->order_id, 'is_delete' => 0]); $order_data = [ 'order_id' => $orderTransit->cloud_order_id, 'status' => 0 ]; $order_url = "/user/getRefundOrder"; $domain = (new OptionSetting)->getCloudDomainName(); $orderInfo = cloud_post($domain . $order_url, $order_data); $orderInfo = json_decode($orderInfo,true); if ($orderInfo['code']) { throw new \Exception($orderInfo['msg']); } $orderInfo = $orderInfo['data'] ?: []; if (!$orderInfo['id']) { throw new \Exception('订单不存在'); } $form = new SupplierForm(); $form->status = 1; $form->is_agree = 1; $form->order_id = $orderInfo['id']; $res = $form->supplierApplyHandle($orderTransit->cloud_supplier_id); if ($res['code']) { throw new \Exception($res['msg']); } $t->commit(); return [ 'code' => 0, 'msg' => '操作成功' ]; } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //获取装车记录 public function carLoadingLog() { try { $driver_id = $this->driver_id; $query = DriverLine::find()->where(['driver_id' => $driver_id])->select('id, status')->orderBy('id DESC'); $list = pagination_make($query); foreach ($list['list'] as &$item) { $order_query = DriverLineOrder::find()->where(['line_id' => $item['id']]); //线商品数量 //团商品数量 //线货商品数量 / 团货商品数量 $goods_num_arr = DriverLineOrder::find()->alias('dlo')->leftJoin(['a' => AgentFrontCentralizeGoods::tableName()], 'a.id = dlo.centralize_goods_id') ->where(['line_id' => $item['id']])->groupBy('a.centralize_goods_type') ->select('SUM(dlo.goods_num) as goods_num, a.centralize_goods_type'); $item['goods_num_arr_sql'] = $goods_num_arr->createCommand()->getRawSql(); $goods_num_arr = $goods_num_arr ->asArray()->all(); $item['line_goods_num'] = 0; $item['group_goods_num'] = 0; foreach ($goods_num_arr as $goods_num_item) { if (intval($goods_num_item['centralize_goods_type'])) { $item['group_goods_num'] = $goods_num_item['goods_num']; } else { $item['line_goods_num'] = $goods_num_item['goods_num']; } } //团点数 $item['md_num'] = (clone $order_query)->groupBy('md_id')->select('md_id')->count() ?? 0; //订货数 $item['goods_num'] = 0; $centralize_goods_ext_arr = $order_query->select('centralize_goods_ext_id') ->column(); if ($centralize_goods_ext_arr) { $goods_num_arr = AgentFrontCentralizeGoodsExt::find()->where(['id' => $centralize_goods_ext_arr]) ->select('goods_num')->column(); $item['goods_num'] = array_sum($goods_num_arr); } $item['status'] = intval($item['status']); $driverLineOrder = DriverLineOrder::find()->where(['line_id' => $item['id']]) ->orderBy('sortkey desc')->asArray()->one(); $item['driver_order_md_id'] = $driverLineOrder['md_id'] ?? 0; } $driver = Driver::findOne($driver_id); $list['line_name'] = $driver->line_name; return [ 'code' => 0, 'msg' => '', 'data' => $list ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }