model = new \app\admin\model\Order; $this->logmodel = new \app\admin\model\OrderLog; $this->view->assign("statusList", $this->model->getStatusList()); } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 查看 * * @return string|Json * @throws \think\Exception * @throws DbException */ public function index(){ //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); if (false === $this->request->isAjax()) { return $this->view->fetch(); } //如果发送的来源是 Selectpage,则转发到 Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } [$where, $sort, $order, $offset, $limit] = $this->buildparams(); $user_info = Session::get('admin'); $map = []; if ($user_info['id'] !== 1){ $map['company_id'] = $user_info['company_id']; } $list = $this->model ->where($where) ->where($map) ->order($sort, $order) ->paginate($limit); $result = ['total' => $list->total(), 'rows' => $list->items()]; return json($result); } /** * 添加 * * @return string * @throws \think\Exception */ public function add(){ if (false === $this->request->isPost()) { return $this->view->fetch(); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $params = $this->preExcludeFields($params); $user_info = Session::get('admin'); $params['user_id'] = $user_info['id']; $params['company_id'] = $user_info['company_id']; if ($this->dataLimit && $this->dataLimitFieldAutoFill) { $params[$this->dataLimitField] = $this->auth->id; } $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)->save($params); $arr['oid'] = $this->model->getLastInsID(); $arr['userid'] = $user_info['id']; $arr['username'] = $user_info['nickname']; $arr['type'] = 1; $arr['orderstu'] = 1; $this->model->save($arr);//添加到订单管理 $this->logmodel->save($arr);//在添加订单管理日志 Db::commit(); } catch (ValidateException|PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result === false) { $this->error(__('No rows were inserted')); } $this->success(); } /** * 根据订单表 stock.js检测是否有新订单 有返回值为1 代表有新增订单数据 * @return string * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function stockad(){ $company_id = $_SESSION['think']['admin']['company_id'];//查询对应公司id $order = Db::name('order')->where('company_id',$company_id)->where('orderstu',1)->find(); if(!empty($order)){ return $order['orderstu']; } } public function edit($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__('You have no permission')); } if (false === $this->request->isPost()) { $this->view->assign('row', $row); return $this->view->fetch(); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $params = $this->preExcludeFields($params); $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 . '.edit' : $name) : $this->modelValidate; $row->validateFailException()->validate($validate); } $arr = []; foreach ($params as $k=>$v){ if($v!=$row[$k]) { switch ($k){ case 'no': $arr['field'] = '编号'; $arr['before'] = $row['no']; //写入订单管理日志表 查看修改前的字段值 $arr['after'] = $params['no']; //写入订单管理日志表 查看修改后的字段值 // continue; break; case 'customer': $arr['field'] = '订货单位'; $arr['before'] = $row['customer']; $arr['after'] = $params['customer']; break; case 'product': $arr['field'] = '品名'; $arr['before'] = $row['product']; $arr['after'] = $params['product']; break; case 'specs': $arr['field'] = '包装规格'; $arr['before'] = $row['specs']; $arr['after'] = $params['specs']; break; case 'unit': $arr['field'] = '单位'; $arr['before'] = $row['unit']; $arr['after'] = $params['unit']; break; case 'number': $arr['field'] = '数量'; $arr['before'] = $row['number']; $arr['after'] = $params['number']; break; case 'price': $arr['field'] = '单价'; $arr['before'] = $row['price']; $arr['after'] = $params['price']; break; case 'delivery_date': $arr['field'] = '交货期'; $arr['before'] = $row['delivery_date']; $arr['after'] = $params['delivery_date']; break; case 'remark': $arr['field'] = '备注'; $arr['before'] = $row['remark']; $arr['after'] = $params['remark']; break; case 'date': $arr['field'] = '日期'; $arr['before'] = $row['date']; $arr['after'] = $params['date']; break; case 'user_name': $arr['field'] = '经办'; $arr['before'] = $row['user_name']; $arr['after'] = $params['user_name']; break; case 'examine': $arr['field'] = '审核'; $arr['before'] = $row['examine']; $arr['after'] = $params['examine']; break; case 'status': $arr['field'] = '状态'; if ($row['status'] == 1) { $arr['before'] = '计划中'; if($params['status'] == 2){ $arr['after'] = '生产中'; }else if($params['status'] == 3){ $arr['after'] = '已完成'; } } if ($row['status'] == 2) { $arr['before'] = '生产中'; if($params['status'] == 1){ $arr['after'] = '计划中'; }else if($params['status'] == 3){ $arr['after'] = '已完成'; } } if ($row['status'] == 3) { $arr['before'] = '已完成'; if($params['status'] == 2){ $arr['after'] = '生产中'; }else if($params['status'] == 1){ $arr['after'] = '计划中'; } } break; } $user_info = Session::get('admin'); $arr['userid'] = $user_info['id'];//用户id $arr['username'] = $user_info['nickname'];//用户名 $arr['oid'] = $row['id'];//订单表id $arr['type'] = 2;//订单日志类型 2 修改后 $arr['orderstu'] = 2;//订单状态 $res[]=$arr; } } $this->logmodel->saveAll($res);//添加订单日志表,查哪个用户在什么时间修改哪个字段值 $result = $row->allowField(true)->save($params); Db::commit(); } catch (ValidateException|PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if (false === $result) { $this->error(__('No rows were updated')); } $this->success(); } //生产作业票 public function task(){ $ids = input('ids'); if (!$ids) { $this->error(__('No Results were found')); } if (false === $this->request->isPost()) { $order = Db::name('order')->where('id',$ids)->find(); $map = []; $map['name'] = array('like','%'.$order['product'].'%'); $map['examine_status'] = 2; $list = Db::name('formula')->where($map)->field('id,name,version')->order('id desc')->select(); $res = []; if (!empty($list)){ $res = $this->get_repeat_data($list); } // halt($res);die; $this->view->assign('ids',$ids); $this->view->assign('row', $res); return $this->view->fetch(); } } //获取二维数组的重复数据 function get_repeat_data($array){ //计算出数组的总数量 $count = count($array); $num = [];//算出那个值是不要的 去掉就行 for($i=0;$i<$count;$i++){ $i_data = $array[$i]['name']; for($j=$i+1;$j<$count;$j++){ $j_data = $array[$j]['name']; if($i_data == $j_data){ $i_version = substr($array[$i]['version'],1); $j_version = substr($array[$j]['version'],1); if ($j_version > $i_version){ $num[$i] = $i; }else{ $num[$i] = $j; } } } } foreach ($num as $key=>$value){ unset($array[$value]); } $result = array_values($array); return $result; } }