enterpriseId = $enterpriseId; $this->userCenterId = $userCenterId; $this->objDDeliveryRoute = new DDeliveryRoute('default'); $this->objDDeliveryRouteCustomer = new DDeliveryRouteCustomer('default'); $this->objMDriver = new MDriver($this->enterpriseId, $this->userCenterId); $this->objDDeliveryRoute->setTable('qianniao_delivery_route_'.$enterpriseId); $this->objDDeliveryRouteCustomer->setTable('qianniao_delivery_route_customer_'.$enterpriseId); } /** * 配送线路添加 * @param $params * @return ResultWrapper * @throws Exception */ public function addDeliveryRoute($params) { $params['createTime'] = time(); $params['updateTime'] = time(); $customerData = isset($params['customerId']) ? $params['customerId'] : []; $driverIds = isset($params['driverId']) ? trim($params['driverId'], ',') : ''; $driverIds = strpos($driverIds, ',') ? explode(',', $driverIds) : $driverIds; unset($params['customerId'], $params['driverId']); $beginStatus = $this->objDDeliveryRoute->beginTransaction(); $dbResult = $this->objDDeliveryRoute->insert($params); if($dbResult === false){ $this->objDDeliveryRoute->rollBack(); return ResultWrapper::fail($this->objDDeliveryRoute->error(), ErrorCode::$dberror); } $routeId = $dbResult; unset($dbResult); //增加客户绑定 if(!empty($customerData)){ $dbResult = $this->objDDeliveryRouteCustomer->update(['deleteStatus' => StatusCode::$delete, 'updateTime' => time()], ['customerId' => $customerData]); if($dbResult === false){ $this->objDDeliveryRoute->rollBack(); return ResultWrapper::fail($this->objDDeliveryRouteCustomer->error(), ErrorCode::$dberror); } $insert = []; foreach($customerData as $value){ $insert[] = [ 'routeId' => $routeId, 'customerId' => $value, 'createTime' => $params['createTime'], 'updateTime' => $params['updateTime'], ]; } $dbResult = $this->objDDeliveryRouteCustomer->insert($insert, true); if($dbResult === false){ $this->objDDeliveryRoute->rollBack(); return ResultWrapper::fail($this->objDDeliveryRouteCustomer->error(), ErrorCode::$dberror); } } //增加司机绑定 if(!empty($driverIds)){ $update = [ 'routeId' => $routeId, 'updateTime' => time() ]; $modelResult = $this->objMDriver->updateDriverData($update, ['id' => $routeId]); if(!$modelResult->isSuccess()){ $this->objDDeliveryRoute->rollBack(); return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } } $beginStatus && $this->objDDeliveryRoute->commit(); return ResultWrapper::success($routeId); } /** * 配送线路修改 * @param $update * @param $where * @return ResultWrapper */ public function updateDeliveryRoute($update, $where) { $update['updateTime'] = time(); $customerData = isset($update['customerId']) ? array_unique($update['customerId']) : []; $driverIds = isset($update['driverId']) ? $update['driverId'] : ''; $driverIds = strpos($driverIds, ',') ? explode(',', $driverIds) : $driverIds; unset($update['customerId'], $update['driverId']); $beginStatus = $this->objDDeliveryRoute->beginTransaction(); //处理客户数据 if(!empty($customerData)){ $dbResult = $this->objDDeliveryRouteCustomer->update(['deleteStatus' => StatusCode::$delete, 'updateTime' => time()],['customerId' => $customerData]); if($dbResult === false){ return ResultWrapper::fail($this->objDDeliveryRouteCustomer->error(), ErrorCode::$dberror); } foreach($customerData as $value){ $insert[] = [ 'routeId' => $where['id'], 'customerId' => $value, 'createTime' => $update['updateTime'], 'updateTime' => $update['updateTime'], ]; } if(!empty($insert)){ $dbResult = $this->objDDeliveryRouteCustomer->insert($insert, true); if($dbResult === false){ $this->objDDeliveryRoute->rollBack(); return ResultWrapper::fail($this->objDDeliveryRouteCustomer->error(), ErrorCode::$dberror); } } }else{ $dbResult = $this->objDDeliveryRouteCustomer->update(['deleteStatus' => StatusCode::$delete, 'updateTime' => time()], ['routeId' => $where['id']]); if($dbResult === false){ $this->objDDeliveryRoute->rollBack(); return ResultWrapper::fail($this->objDDeliveryRouteCustomer->error(), ErrorCode::$dberror); } } //处理司机数据 if(!empty($driverIds)){ $modelResult = $this->objMDriver->updateDriverData(['routeId' => 0, 'updateTime' => time()], ['routeId' => $where['id']]); if(!$modelResult->isSuccess()){ $this->objDDeliveryRoute->rollBack(); return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $modelResult = $this->objMDriver->updateDriverData(['routeId' => $where['id'], 'updateTime' => time()], ['id' => $driverIds]); if(!$modelResult->isSuccess()){ $this->objDDeliveryRoute->rollBack(); return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } }else{ $modelResult = $this->objMDriver->updateDriverData(['routeId' => 0, 'updateTime' => time()], ['routeId' => $where['id']]); if(!$modelResult->isSuccess()){ $this->objDDeliveryRoute->rollBack(); return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } } $dbResult = $this->objDDeliveryRoute->update($update,$where); if($dbResult === false){ $this->objDDeliveryRoute->rollBack(); return ResultWrapper::fail($this->objDDeliveryRoute->error(), ErrorCode::$dberror); } $beginStatus && $this->objDDeliveryRoute->commit(); return ResultWrapper::success($dbResult); } /** * 配送线路列表 * @param $selectParams * @return ResultWrapper */ public function getAllDeliveryRoute($selectParams) { $limit = $selectParams['limit']; unset($selectParams['limit']); $offset = $selectParams['offset']; unset($selectParams['offset']); $selectParams['deleteStatus'] = StatusCode::$standard; $whereSql = self::formatSqlWhere($selectParams); $dbResult = $this->objDDeliveryRoute->select($whereSql, '*', 'createTime desc', $limit, $offset); if ($dbResult === false) { return ResultWrapper::fail($this->objDDeliveryRoute->error(), ErrorCode::$dberror); } $total = $this->objDDeliveryRoute->count($whereSql); $return = [ 'data' => self::formatDeliveryRoute($dbResult), 'total' => $total ? intval($total) : 0, ]; return ResultWrapper::success($return); } /** * 配送线路详情 * @param $where * @return ResultWrapper */ public function getDeliveryRouteInfo($where) { $dbResult = $this->objDDeliveryRoute->get($where); if($dbResult === false){ return ResultWrapper::fail($this->objDDeliveryRoute->error(), ErrorCode::$dberror); } return ResultWrapper::success(self::formatDeliveryRoute($dbResult)); } /** * 配送线路客户 * @param $selectParams * @return ResultWrapper */ public function getDeliveryRouteCustomer($selectParams) { $limit = $selectParams['limit']; unset($selectParams['limit']); $offset = $selectParams['offset']; unset($selectParams['offset']); $selectParams['deleteStatus'] = StatusCode::$standard; $dbResult = $this->objDDeliveryRouteCustomer->select($selectParams, '*', 'sort desc', $limit, $offset); if($dbResult === false){ return ResultWrapper::fail($this->objDDeliveryRouteCustomer->error(), ErrorCode::$dberror); } $total = $this->objDDeliveryRouteCustomer->count($selectParams); $return = [ 'data' => self::formatDeliveryRouteCustomer($dbResult), 'total' => $total ? $total : 0 ]; return ResultWrapper::success($return); } /** * 配送线路客户排序 * @param $update * @param $where * @return ResultWrapper */ public function updateDeliveryRouteCustomer($update, $where) { $update['updateTime'] = time(); $dbResult = $this->objDDeliveryRouteCustomer->update($update, $where); if($dbResult === false){ return ResultWrapper::fail($this->objDDeliveryRouteCustomer->error(), ErrorCode::$dberror); } return ResultWrapper::success($dbResult); } /** * 格式化线路客户 * @param $params * @return mixed */ public function formatDeliveryRouteCustomer($params) { $customerIds = array_column($params, 'customerId'); //客户数据 $customerData = []; if(!empty($customerIds)){ $objMCustomer = new MCustomer($this->enterpriseId, $this->userCenterId); $modelResult = $objMCustomer->getCustomerData(['id' => $customerIds], '*', false, true); if($modelResult->isSuccess()){ $customerResult = $modelResult->getData(); foreach($customerResult as $customer){ $customerData[$customer['id']] = $customer; } } } foreach($params as &$value){ $value['customerName'] = ''; $value['customerType'] = ''; $value['customerMobile'] = ''; $value['customerArea'] = []; if(isset($customerData[$value['customerId']])){ $value['customerName'] = $customerData[$value['customerId']]['name']; $value['customerType'] = $customerData[$value['customerId']]['customerType']; $value['customerMobile'] = $customerData[$value['customerId']]['mobile']; $value['customerArea'] = $customerData[$value['customerId']]['area']; } } return $params; } /** * 格式化线路客户订单数据 * @param $customerData * @return mixed */ public function formatDeliveryRouteCustomerOrder($customerData) { $customerIds = array_column($customerData, 'customerId'); //客户数据 $customerOrderData = []; $params['orderCustomerNum'] = 0; $params['orderTotal'] = 0; $params['orderAmount'] = 0; $params['goodsTotal'] = 0; $params['otherTotal'] = 0; $params['outOrderTotal'] = 0; $params['outOrderAmount'] = 0; if(!empty($customerIds)){ //查询没有删除 并且订单状态是3代发货 4待收货 5已完成 $whereSql = ' where customerId in('.implode(',',array_unique($customerIds)).') and deleteStatus = '.StatusCode::$standard.' and orderStatus in(3,4) and deliveryType = '.StatusCode::$deliveryType['logistics'].' and auditStatus = '.StatusCode::$auditStatus['auditPass']; $whereSqlAs = ' where o.customerId in('.implode(',',array_unique($customerIds)).') and o.deleteStatus = '.StatusCode::$standard.' and o.orderStatus in(3,4) and o.deliveryType = '.StatusCode::$deliveryType['logistics'].' and o.auditStatus = '.StatusCode::$auditStatus['auditPass'].' and g.deleteStatus = '.StatusCode::$standard; //下单客户数 $sql = 'select count(a.customerId) as total from (select customerId from qianniao_order_'.$this->enterpriseId.'_1 '.$whereSql.' group by customerId) as a'; $dbResult = $this->objDDeliveryRoute->query($sql); $customerOrderData['orderCustomerNum'] = isset($dbResult[0]['total']) ? $dbResult[0]['total'] : 0; //总单数 总金额 $sql = 'select count(*) as total, sum(totalMoney) as amount from qianniao_order_'.$this->enterpriseId.'_1 '.$whereSql; $dbResult = $this->objDDeliveryRoute->query($sql); $customerOrderData['orderTotal'] = isset($dbResult[0]['total']) ? $dbResult[0]['total'] : 0; $customerOrderData['orderAmount'] = isset($dbResult[0]['amount']) ? $dbResult[0]['amount'] : 0; //商品数量 $sql = 'select count(a.skuId) as total from (select count(g.skuId) as skuId from qianniao_order_goods_'.$this->enterpriseId.'_1 g left join qianniao_order_'.$this->enterpriseId.'_1 o on o.id = g.orderId '.$whereSqlAs.' group by g.skuId) as a'; $dbResult = $this->objDDeliveryRoute->query($sql); $customerOrderData['goodsTotal'] = isset($dbResult[0]['total']) ? $dbResult[0]['total'] : 0; //商品吨数 (只统计抄码商品) $sql = 'select sum(g.buyNum) as total from qianniao_order_goods_'.$this->enterpriseId.'_1 g left join qianniao_order_'.$this->enterpriseId.'_1 o on o.id = g.orderId '.$whereSqlAs; $dbResult = $this->objDDeliveryRoute->query($sql); $customerOrderData['otherTotal'] = isset($dbResult[0]['total']) ? $dbResult[0]['total'] : 0; $whereSql .= ' and outStatus <> '.StatusCode::$delete; //已出库商品吨数 (只统计抄码商品) $sql = 'select sum(g.outNum) as total from qianniao_order_goods_'.$this->enterpriseId.'_1 g left join qianniao_order_'.$this->enterpriseId.'_1 o on o.id = g.orderId '.$whereSqlAs; $dbResult = $this->objDDeliveryRoute->query($sql); $customerOrderData['outOtherTotal'] = isset($dbResult[0]['total']) ? $dbResult[0]['total'] : 0; //已出库 订单金额 订单数 $sql = 'select count(*) as total, sum(totalMoney) as amount from qianniao_order_'.$this->enterpriseId.'_1 '.$whereSql; $dbResult = $this->objDDeliveryRoute->query($sql); $customerOrderData['outOrderTotal'] = isset($dbResult[0]['total']) ? $dbResult[0]['total'] : 0; $customerOrderData['outOrderAmount'] = isset($dbResult[0]['amount']) ? $dbResult[0]['amount'] : 0; } $params['orderCustomerNum'] = $customerOrderData['orderCustomerNum']; $params['orderTotal'] = $customerOrderData['orderTotal']; $params['orderAmount'] = $customerOrderData['orderAmount']; $params['goodsTotal'] = $customerOrderData['goodsTotal']; $params['otherTotal'] = $customerOrderData['otherTotal']; $params['outOtherTotal'] = $customerOrderData['outOtherTotal']; $params['outOrderTotal'] = $customerOrderData['outOrderTotal']; $params['outOrderAmount'] = $customerOrderData['outOrderAmount']; return $params; } /** * 格式化线路 * @param $params * @return array|mixed */ public function formatDeliveryRoute($params) { if(isset($params['id'])){ $data = [$params]; }else{ $data = $params; } $routeIds = []; foreach($data as $value){ $routeIds[] = $value['id']; } $routeCustomerData = []; $routeCustomerOrderData = []; $driverData = []; if(!empty($routeIds)){ //查询线路绑定客户 $dbResult = $this->objDDeliveryRouteCustomer->select(['routeId' => $routeIds, 'deleteStatus' => StatusCode::$standard], '*', 'sort desc'); if($dbResult){ foreach($dbResult as $value){ $routeCustomerData[$value['routeId']][] = $value; } foreach($routeCustomerData as $key => &$value){ $value = self::formatDeliveryRouteCustomer($value);//根据线路绑定的客户查找客户数据 $routeCustomerOrderData[$key] = self::formatDeliveryRouteCustomerOrder($value);//根据客户查找订单数据 } unset($value); } //查询线路绑定司机 $modelResult = $this->objMDriver->getDriverData(['routeId' => $routeIds, 'state' => StatusCode::$standard]); if($modelResult->isSuccess()){ $driverResult = $modelResult->getData(); foreach($driverResult as $value){ $driverData[$value['routeId']][] = $value; } } } foreach($data as &$value){ $value['driverData'] = isset($driverData[$value['id']]) ? $driverData[$value['id']] : []; $value['customerData'] = []; if(isset($routeCustomerData[$value['id']])){ $value['customerData'] = $routeCustomerData[$value['id']]; } $value['orderCustomerNum'] = 0; $value['orderTotal'] = 0; $value['orderAmount'] = 0; $value['goodsNum'] = 0; $value['otherNum'] = 0; $value['outOtherTotal'] = 0; $value['outGoodsTotal'] = 0; $value['outAmount'] = 0; if(isset($routeCustomerOrderData[$value['id']])){ $value['orderCustomerNum'] = $routeCustomerOrderData[$value['id']]['orderCustomerNum']; $value['orderTotal'] = $routeCustomerOrderData[$value['id']]['orderTotal']; $value['orderAmount'] = $routeCustomerOrderData[$value['id']]['orderAmount']; $value['goodsNum'] = $routeCustomerOrderData[$value['id']]['goodsTotal']; $value['outOtherTotal'] = $routeCustomerOrderData[$value['id']]['outOtherTotal'] > 0 ? bcdiv($routeCustomerOrderData[$value['id']]['outOtherTotal'], 1000) : $routeCustomerOrderData[$value['id']]['outOtherTotal']; $value['otherNum'] = $routeCustomerOrderData[$value['id']]['otherTotal'] > 0 ? bcdiv($routeCustomerOrderData[$value['id']]['otherTotal'], 1000) : $routeCustomerOrderData[$value['id']]['otherTotal']; $value['outGoodsTotal'] = $routeCustomerOrderData[$value['id']]['outOrderTotal']; $value['outAmount'] = $routeCustomerOrderData[$value['id']]['outOrderAmount']; } } if(isset($params['id'])){ $return = array_shift($data); }else{ $return = $data; } return $return; } }