企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
其中的save2为入库核心逻辑,save3为出库核心逻辑 ~~~ <?php namespace app\admin\controller; use app\common\model\Stock as StockModel; use app\common\controller\AdminBase; use think\Db; use think\Session; use think\Request; class Stock extends AdminBase { protected $stock_model; protected function _initialize() { parent::_initialize(); $this->stock_model = new StockModel(); $stock_list = $this->stock_model->order(['in_time' => 'DESC', 'id' => 'desc'])->paginate(9,false,['query' => request()->param()]); $this->assign('stock_list', $stock_list); } //明细列表 public function index($is_out= 2) { if($is_out==0||$is_out==1){ $stock_list = $this->stock_model->where('is_out',$is_out)->order(['in_time' => 'DESC', 'id' => 'desc'])->paginate(9,false,['query' => request()->param()]); $this->assign('stock_list', $stock_list); } return $this->fetch(); } //出入库1-1 public function instock() { /* $alist= db('parts')->where('number',$number)->select(); $this->assign('alist',$alist);*/ return $this->fetch(); } //出入库1-2 public function outstock() { /* $alist= db('parts')->where('number',$number)->select(); $this->assign('alist',$alist);*/ return $this->fetch(); } //出入库更新1-1-入库 public function save2() { $status=0; $message="操作失败"; $data = $this->request->param(); $arr=array($data['parts_id'],$data['number'],$data['price'],$data['remarks']);//二维数组 $num=count($arr[0]);//插入数量 for($i=0;$i<$num;){//二维数组长度 $arr2 = array_column($arr,$i);//获取二维数组一列并组成新的一维数组 $map=array();//新建初始一维数组 foreach ($arr2 as $k =>$v){//改变索引数组改为键值数组 switch($k){ case 0: $k='parts_id'; break; case 1: $k='number'; break; case 2: $k='price'; break; case 3: $k='remarks'; break; } $map[$k]=$v; } $map['in_time']=time();//map为新组合的一维数组 $map['is_out']=0; $arr3[$i]=$map;//新组合的二维数组 $i=$i+1; } $this->stock_model->allowField(true)->saveAll($arr3);//批量插入 $status = 1; $message = "入库成功"; return ['status'=>$status, 'message'=>$message]; } //出入库更新1-2-出库 public function save3() { $status=0; $message="操作失败"; $data = $this->request->param(); $arr=array($data['parts_id'],$data['number'],$data['price'],$data['remarks']);//二维数组 $num=count($arr[0]);//插入数量 for($i=0;$i<$num;){// $arr2 = array_column($arr,$i);//获取二维数组一列并组成新的一维数组 $map=array();//新建初始一维数组 foreach ($arr2 as $k =>$v){//改变索引数组改为键值数组 switch($k){ case 0: $k='parts_id'; break; case 1: $k='number'; break; case 2: $k='price'; break; case 3: $k='remarks'; break; } $map[$k]=$v; } $map['in_time']=time();//map为新组合的一维数组 $map['is_out']=1; $parts_id=$map['parts_id']; $number=$map['number']; //1.判断是否有库存,2.判断库存数量是否大于出库数量 $res = getStockNum($parts_id); if($res > 0){ if($res>=$number){ $iserror=0; }else{ $iserror=1; } }else{ $iserror=1; } //放在判断库存的结果 $arr4[$i]=$iserror; $arr3[$i]=$map;//新组合的二维数组 $i=$i+1; } if(in_array(1,$arr4)){//检查数组中某个值是否存在 $status = 0; $message = "出库失败,选中配件没有库存或者库存量不足,请先入库"; }else{ $this->stock_model->allowField(true)->saveAll($arr3);//批量插入 $status = 1; $message = "出库成功"; } return ['status'=>$status, 'message'=>$message]; } //更新1-1 public function edit($id) { $data=db('stock')->find($id); $this->assign('data',$data); return $this->fetch(); } //更新1-2 public function update() { $data = $this->request->param(); if($this->stock_model->allowField(true)->save($data,['id'=>$data['id']])){ $this->success('更新成功'); }else{ $this->success('更新失败'); } } //删除 public function del($id = 0, $ids = []) { $id = $ids ? $ids : $id; if ($id) { if ($this->stock_model->destroy($id)) { $this->success('删除成功'); } else { $this->error('删除失败'); } } else { $this->error('请选择需要删除的信息'); } } } ~~~