pid) { $this->result('您正使用本系统内部接口,禁止非法链接使用!'); } return parent::beforeAction($action); } //出车单 public function actionIndex() { $apis = [ 'list' => '板车装载记录清单', 'detail' => '装载记录详情', 'get-status'=>'获取板车装载状态字典', 'add'=>'添加', 'edit'=>'编辑', 'delete'=>'删除', 'show'=>'显示', 'hide'=>'隐藏', 'ajax'=>'ajax操作' ]; $this->result('您正使用CMTS-GM系统板车装载管理接口!', $apis, 200); } public function actionGetStatus(){ $status = Yii::$app->params['TruckOnlineStatus']; $this->result('板车在途状态字典', $status, 200); } public function actionList() { $s = $this->search(); $res = $s['res']; if (!$res) { $this->result('没有查询到相应的数据!', [], 0); } $data= [ 'total'=>$s['query']->count(), 'truckols' => $res, 'stores' => $s['stores'], 'page'=>$this->page ]; $this->result('板车装载记录查询成功!', $data, 200); } public function actionDetail() { $pid = $this->pid; $post = $this->postdata; $id = isset($post['id']) ? $post['id'] : 0; if ($id <= 0) { $this->result('查询参数错误!'); } $model = new AcTruckOnline(); $detail = $model->findOne($id)->toArray(); if(!$detail) $this->result('未查询到相应数据'); $this->result('查询成功!', $detail, 200); } public function actionAdd() { //数据预检查、编排 $data = $this->preSave('add'); if(!$data) $this->result('数据预检查未通过,保存失败', $data, 100); //保存资料 $model = new AcTruckOnline(); foreach ($data as $key=>$val) { $model->$key = $val; } $res = $model->save(); $msg = '数据保存失败!'; if(!$res) $this->result($msg, [], 100); $msg = '数据保存成功!'; $return = []; $return['id']= $model->attributes['id']; //获取插入后id; $this->result($msg,$return, 200); } //编辑板车装车计划资料 public function actionEdit() { $detail = $this->preUpdate(); //数据预检查、编排 $data = $this->preSave('edit'); if(!$data) $this->result('数据预检查未通过,保存失败', $data, 100); //保存资料 foreach ($data as $key=>$val) { $detail->$key = $val; } $res = $detail->save(); $msg = '数据编辑失败!'; if(!$res) $this->result($msg, [], 100); $msg = '数据编辑成功!'; $return = []; $return['data']= $detail; $this->result($msg,$return, 200); } public function actionAjax() { $detail = $this->preUpdate(); $get = Yii::$app->request->get(); $msg = ''; $errorCode = 0; $_time = time(); $detail->update_at = $_time; switch ($get['do']) { case 'chufa': //装发任务 出发 if ($detail->status_code != 0) { $this->result('关联出车任务非“待出发”状态,操作失败', [], 400); } $res = $this->chufa($detail); if(!$res) $this->result('关联板车与运单状态操作失败,请重试或检查', [], 100); $detail->status_code = 1; $res = $detail->save(); if ($res) { $msg = '板车装载任务板标记出发成功'; $errorCode = 200; } else { $msg = '板车装载任务板标记出发失败'; $errorCode = 100; } break; case 'arrive': //装发任务 出发 if ($detail->status_code != 1) { $this->result('关联出车任务非“进行中”(在途)状态,操作失败', [], 400); } $res = $this->chufa($detail); if(!$res) $this->result('关联板车与运单状态操作失败,请重试或检查', [], 100); $detail->status_code = 99; $res = $detail->save(); if ($res) { $msg = '板车装载任务板标记到达成功'; $errorCode = 200; } else { $msg = '板车装载任务板标记到达失败'; $errorCode = 100; } break; default: break; } $this->result($msg, [], $errorCode); } //装载车辆明细 public function actionCars() { $detail = $this->preUpdate(); $truckol_id = $detail->id; $AcTruckCars = new AcTruckCars(); $ids = $AcTruckCars->find()->where(['online_id' => $truckol_id,'deleted'=>0])->select(['order_id','car_id'])->all(); $order_ids = $car_ids = []; foreach ($ids as $id) { $order_ids[] = $id->order_id; $car_ids[] = $id->car_id; } $AcOrder = new AcOrder(); $orders = $AcOrder->find()->where(['IN','id',$order_ids],['=','status_code',1])->all(); $AcCar = new AcCar(); $cars = $AcCar->find()->where(['in','id',$car_ids])->all(); $data = ['orders'=>$orders,'cars'=>$cars]; $this->result('装车单明细数据查询完成', $data, 200); } //批量装车 public function actionBatchCars() { $detail = $this->preUpdate(); $res = $this->zhuangche($detail); if($res) $this->result('操作完成,共新关联'.$res.'个运单',[],200); } //删除板车装车任务 public function actionDelete() { $detail = $this->preUpdate(); if ($detail->deleted != 0) { $this->result('数据已被删除过,操作无效', [], 400); } if ($detail->status_code > 0) { $this->result('装载任务已经启动,操作终止', [], 403); } if ($detail->cars > 0) { $this->result('已有装载车辆,操作终止', [], 403); } $AcTruckCars = new AcTruckCars(); $truckCars = $AcTruckCars->find()->where([['=','online_id',$detail->id],['=','deleted',0],['=','pid',$this->pid]])->one(); if($truckCars) $this->result('已有装载车辆,操作终止', [], 403); $data = []; $data['update_at'] = time(); $data['deleted'] = $detail->deleted + 1; //保存资料 foreach ($data as $key=>$val) { $detail->$key = $val; } $res = $detail->save(); $msg = '板车装载任务删除失败!'; if(!$res) $this->result($msg, [], 100); $msg = '板车装载任务删除成功!'; $this->result($msg,[], 200); } private function search(){ $pid = $this->pid; $return = []; $model = new AcTruckOnline(); $where = []; $where[] = ['=', 'pid', $pid]; $where[] = ['>', 'status_code', 0]; $post = $this->postdata; $search = $post['search'] ?? []; //板车车牌号 $search['truck'] = isset($post['truck']) ? trim(htmlspecialchars_decode($post['truck'])) : ''; if (!empty($search['truck'])) { $where[] = ['LIKE', 'truck_num', $search['title']]; } $search['start_city'] = isset($search['start_city']) ? trim(htmlspecialchars_decode($search['start_city'])) : ''; if (!empty($search['start_city'])) { $where[] = ['LIKE', 'from_city', $search['start_city']]; } $search['aim_city'] = isset($search['aim_city']) ? trim(htmlspecialchars_decode($search['aim_city'])) : ''; if (!empty($search['aim_city'])) { $where[] = ['LIKE', 'to_city', $search['aim_city']]; } $search['truck_id'] = isset($search['truck_id']) ? (int)$search['turck_id'] : 0; if (!$search['truck_id']) { $where[] = ['=', 'truck_id', $search['truck_id']]; } $where[] = ['=', 'deleted', 0]; $data = $model->find()->where($where); $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]); $res = $data->offset($pages->offset)->limit($pages->limit)->orderby('id DESC')->all(); $stores = []; foreach ($res as $r) { $AcStore = new AcStore(); $store_ids = []; foreach ($res as $r) { $store_ids[$r->from_store] = $r->from_store; $store_ids[$r->end_store] = $r->end_store; } $_ids = array_unique($store_ids); $_stores = $AcStore->find()->where(['IN', 'id', $_ids])->all(); if ($_stores) { foreach ($_stores as $r) { $stores[$r->id] = $r->toArray(); } } } return ['res'=>$res,'query'=>$data,'stores'=>$stores]; } /* * 数据更新前的预检查,返回对应关联数据 * 必须确保get与post数据中均包含需更新的数据id且一致 * */ private function preUpdate() { $id = (int)Yii::$app->request->get('id'); if(!$id) $this->result('请求错误,未携带ID参数'); $post = $this->postdata; if($post['id'] != $id) $this->result('传参id与请求数据不匹配',[],403); $model = new AcTruckCars(); $res = $model->findOne($id); if(!$res) $this->result('未查询到相应数据',[],404); if($res->pid != $this->pid) $this->result('非本平台数据,不允许操作',[],401); return $res; } /* * 数据保存前的预检查(查重、参数校验等) * 要保存的数据[],直接从post中取出 * @op,操作类型(add,edit……) * 校验机制: * 编辑数据时,必须用get方式传入参数id,并与post进来的数据id进行比对,只有一致时才能继续; * 查重:禁止录入重复数据 * 返回:校验重组后的数据 * */ private function preSave($op) { $post = $this->postdata; $data = []; $_time = time(); //格式化数据 //目标键=》POST键 switch ($op) { case 'add': /*板车关联判断*/ $truck_id = $post['truck_id'] ?? 0; if (!(int)$truck_id) { $this->result('未关联板车,保存失败',[],400); } $AcTruck = new AcTruck(); $truck = $AcTruck->findOne($truck_id); if (!$truck) { $this->result('未找到指定板车,保存失败',[],404); } if ($truck->status_code!=4) { $this->result('板车不是装车状态,关联失败',[],401); } $AcTruckOnline = new AcTruckOnline(); $where = []; $where[] = 'and'; $where[] = ['=', 'pid', $this->pid]; $where[] = ['=', 'truck_id', $truck_id]; $where[] = ['<', 'status_code', 99]; $where[] = ['=', 'deleted', 0]; $hasOne = $AcTruckOnline->find()->where($where)->one(); if($hasOne) $this->result('该板车已有未完成的托运任务,不允许重复指派',[],403); $data['truck_id'] = $truck_id; $data['truck_num'] = $truck->num_p.$truck->num_area.$truck->num_left; $data['create_at'] = time(); $data['update_at'] = $data['create_at']; $key = 'from_date'; $data[$key] = isset($post[$key]) ? strtotime($post[$key]) : $_time; break; case 'edit': $id = (int)Yii::$app->request->get('id'); $_id = (int)$post['id']; if(!$id != $_id) $this->result('id参数不匹配,请检查'); $data['update_at'] = $_time; $key = 'from_date'; if (isset($post[$key])) { $data[$key] = strtotime($post[$key]); } break; } $data['pid'] = $this->pid; $key = 'remark'; $data[$key] = isset($post[$key]) ? htmlspecialchars(trim($post[$key])) : ''; $cols = ['from_city' => 'from_city', 'end_city' => 'end_city','driver_name'=>'driver_name','driver_mobiles'=>'driver_mobiles','driver_idcard'=>'driver_idcard']; foreach ($cols as $col => $key) { $data[$col] = isset($post[$key]) ? trim($post[$key]) : ''; } $key = 'end_date'; if (isset($post[$key])) { $data[$key] = strtotime($post[$key]); } $cols = ['from_store' => 'from_store', 'end_store' => 'end_store', 'status_code'=>'status_code']; foreach ($cols as $col => $key) { $data[$col] = isset($post[$key]) ? (int)$post[$key] : 0; } return $data; } /* * 装载任务标记状态-出发,同时修改: * 承运板车,标记为运输中状态-代码:5 * 全部关联装车清单,标记为运输中状态-代码:1 * 装车单内所有待启运运单的状态为运输中-代码:2 * */ private function chufa($truckol){ $_time = time(); $truck_id = $truckol->truck_id; $AcTruck = new AcTruck(); $truck = $AcTruck->findOne($truck_id); $truck->update_at = $_time; $truck->status_code = 5; $res1 = $truck->save(); if(!$res1) return false; $AcTruckCars = new AcTruckCars(); $truckol_id = $truckol->id; $data = []; $data['update_at'] = $_time; $data['status_code'] = 1; $res2 = $AcTruckCars->updateAll($data, ['online_id' => $truckol_id]); if(!$res2) return false; $order_ids = $AcTruckCars->find()->where(['online_id' => $truckol_id,'deleted'=>0])->select('order_id')->column(); $AcOrder = new AcOrder(); $orders = $AcOrder->find()->where([['IN','id',$order_ids],['=','status_code',5]])->all(); foreach ($orders as $order) { $order->update_at = $_time; $order->status_code = 2; $res = $order->save(); if(!$res) return false; } return true; } /* * 装载任务标记状态-到达,同时修改: * 承运板车,标记为到达后的状态(待入库)-代码:0 * 全部关联装车清单,标记为已运达状态-代码:99 * */ private function arrive($truckol){ $_time = time(); $truck_id = $truckol->truck_id; $AcTruck = new AcTruck(); $truck = $AcTruck->findOne($truck_id); $truck->update_at = $_time; $truck->status_code = 0; $res1 = $truck->save(); if(!$res1) return false; $AcTruckCars = new AcTruckCars(); $truckol_id = $truckol->id; $data = []; $data['update_at'] = $_time; $data['status_code'] = 99; $res2 = $AcTruckCars->updateAll($data, ['online_id' => $truckol_id]); if(!$res2) return false; return true; } //批量装车(批量传入拟装车运单-》筛选出待承运运单、建承运清单=》) private function zhuangche($truckol){ $post = $this->postdata; $order_ids = isset($post['order_id']) ? $post['order_id'] : []; if (!$order_ids) { $this->result('未传入运单信息,保存失败',[],400); } if (!is_array($order_ids)) { $this->result('传入的运单数据不正确,保存失败',[],400); } $order_ids = array_unique($order_ids); if (!$order_ids) { $this->result('传入的运单数据无效,保存失败',[],400); } /*筛除已标记删除的运单--原则上来讲,其他状态的运单皆应可创建装车单,因为存在后补或者预制承运单的情况,但是在确认上板出发时需要有严格的流程及状态校验 */ /*同步创建对应承运清单*/ $truckol_id = $truckol->id; $_time = time(); $AcOrder = new AcOrder(); $_orders = $AcOrder->find()->where([['IN', 'id', $order_ids],['=','deleted',0]])->indexBy('id')->all(); $_orders_ids = []; $_cars_ids = []; $AcTruckCars = new AcTruckCars(); $truckCars = $AcTruckCars->find()->select('order_id')->where([['=','online_id',$truckol_id],['=','deleted',0],['=','pid',$this->pid]])->indexBy('order_id')->all(); if($_orders){ foreach ($_orders as $order) { //相应运单还没有关联承运清单的,创建一个,并更新运单状态:待承运的改为运输中 if(!$truckCars[$order->id]){ $_orders_ids[] = $order->id; $_cars_ids[] = $order->car_id; $truckCarsId = $this->orderToTruck($order,$truckol); //创建承运清单 if($order->status_code == 5){ $order->update_at = $_time; $order->status_code = 2; $res = $order->save(); } } } } if(!$_orders_ids) $this->result('传入的运单均无须再关联该板',[],400); $cars = count($_cars_ids); $truckol->update_at = $_time; $truckol->cars += $cars; $res = $truckol->save(); if($res) return $cars; } private function orderToTruck($order,$truckol){ $_time = time(); $AcTruckCars = new AcTruckCars(); $data = []; $data['online_id'] = $truckol->id; $data['create_at'] = $_time; $data['update_at'] = $_time; $data['pid'] = $this->pid; $data['from_city'] = $order->start_city; $data['to_city'] = $order->end_city; $data['from_store'] = $truckol->from_store; $data['to_store'] = $truckol->end_store; $data['order_id'] = $order->id; $data['car_id'] = $order->car_id; $data['status_code'] = 0;//待出发 $AcTruckCars->save($data); return $AcTruckCars->attributes['id']; //获取插入后id; } }