model = new \app\admin\model\Feeding; } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 添加 * * @return string * @throws \think\Exception */ public function add() { if (false === $this->request->isPost()) { //车间=>操作人员 $cjcz = Db::name('personnel')->where('bid',"=",5)->where('position','=',"cjcz")->order('name desc')->select(); //车间=>检验人员 $cjjy = Db::name('personnel')->where('bid',"=",5)->where('position','=',"cjjy")->order('name desc')->select(); $this->assign('cjcz',$cjcz); $this->assign('cjjy',$cjjy); return $this->view->fetch(); } $params = $this->request->post('row/a'); $params = $this->preExcludeFields($params); $tid = Db::name('task')->where('bach',$params['bach'])->order('id desc')->find(); $arr = []; if(empty($params['weight'])){ $this->error('投料数据不能为空'); } foreach($params['weight'] as $k=>$v){ if($v){ $param['bach'] = $params['bach']; $param['date'] = $params['date']; $param['operator'] = $params['operator']; $param['inspector'] = $params['inspector']; $param['weight'] = $params['weight'][$k]; $param['nweight'] = $params['nweight'][$k]; $param['material'] = $params['material'][$k]; $param['gy_num'] = $params['gy_num'][$k]; $param['tid'] = $tid['id']; $param['create'] = date('Y-m-d H:i:s'); $arr[] = $param; } } //去掉已经存入数据库的工艺数据 $list = array(); foreach ($arr as $key=>$value){ $map=[]; $map['bach'] = $value['bach']; $map['material'] = $value['material']; $map['gy_num'] = $value['gy_num']; $is_feeding_gy = Db::name('feeding')->where($map)->order('id desc')->find(); if (!$is_feeding_gy){ $list[$key] = $value; } } $result = false; Db::startTrans(); try { //是否采用模型验证 if ($this->modelValidate) { $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate; $this->model->validateFailException()->validate($validate); } $result = $this->model->allowField(true)->saveAll($list); Db::commit(); } catch (ValidateException|PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result === false) { $this->error(__('No rows were inserted')); } $this->success(); } //获取作业票信息 public function get_task(){ $bach = $this->request->post('bach'); $row = Db::name('feeding')->where('bach',$bach)->select(); $res = Db::name('task')->where('bach',$bach)->order('create','desc')->select(); //已有过工序 if($row){ // Session::set('process',serialize($row)); $result['inspector'] = $row[0]['inspector']; $result['operator'] = $row[0]['operator']; }else{ // Session::delete('process'); $result['inspector'] = ''; $result['operator'] = ''; } $result['data'] = $res; return json($result); } //获取配方信息 public function get_formula(){ $bach = $this->request->post('bach');//批次号 $num = $this->request->post('num');//生产量 $process = Db::name('feeding')->where('bach',$bach)->select(); if($num){//如果有,批次号重复,需精确查找 $res = Db::name('task')->alias('t') ->join('formula_detail f','f.pid=t.fid','left') ->field('f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id') ->where('t.bach',$bach)->where('t.number',$num)->select(); }else{//如果没有,,批次号未重复,直接差出数据 $res = Db::name('task')->alias('t') ->join('formula_detail f','t.fid = f.pid','left') ->field('f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id') ->where('t.bach',$bach)->select(); } // print_r($res);die; $pro = 0; //按照百分比计算出应投重量 foreach($res as &$v){ $v['weight']=0; if($process){ //已有工序,接上一次工序 foreach ($process as $val){ if(($val['material']==$v['material'] || in_array($val['material'],explode('/',$v['material']))) && $val['tid']==$v['id'] && $val['gy_num'] == $v['gy_num']){ $v['weight']=$val['weight']; $pro = $val['gy_num']; } } } if($v['gy_name'] == null){ $v['gy_name'] = ''; } if($v['gy_num'] == null){ $v['gy_num'] = ''; } if($v['material'] == null){ $v['material'] = ''; } $total = array_column($res,'percentage'); foreach ($total as $key=>$value){ $total[$key] = decode($value); } $num = array_sum($total); if($v['percentage']){ // $v['nweight'] = number_format(decode($v['percentage']) / $num * $v['number'],3); $number = ceil(decode($v['percentage']) / $num * $v['number'] *1000); $v['nweight'] = number_format($number/1000,3); }else{ $v['nweight']=''; } } $row['total']=$num; $row['data'] = $res; $row['process'] = $pro+1; return json($row); } //查找替代料 /*public function replace(){ $bach = $this->request->post('bach'); $wuliao = $this->request->post('wuliao'); $res = Db::name('task')->alias('t') ->join('formula f','f.id = t.fid','left') ->join('formula_detail fd','fd.pid = f.id','left') ->join('formula_replace fr','fr.fid = fd.id','left') ->where('t.bach',$bach)->where('is_replace=1')->where('fr.material',$wuliao) ->field('fd.material,fd.id')->find(); $res['yuan'] = explode('/',$res['material']); $key = array_search($wuliao, $res['yuan']); if ($key !== false) array_splice($res['yuan'], $key, 1); return json($res); }*/ }