企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
~~~ <?php namespace app\admin\controller; use app\common\controller\AdminBase; use think\Db; use think\Session; class Parts extends AdminBase { protected function _initialize() { parent::_initialize(); $partsClass = model('partsClass')->getLevelList(); $this->assign('partsClass', $partsClass); $this->assign('brand',model('brand')::All()); $factory=model("factory")->select(); $res=model('Parts')::All(); $this->assign('res',$res); $this->assign('factory',$factory); } public function index($pid=0) { $res=model('Parts')->order('id','desc')->select(); $this->assign('res',$res); return $this->fetch(); } public function add() { return $this->fetch(); } public function save($pid=0) { if ($this->request->isPost()) { $data = $this->request->param(); // $data['photo']=json_encode($data['photo']); $data['types'] = json_encode(array($data['name'], $data['value'])); $data['user_id'] = Session::get('admin_id'); if (model('Parts')->allowField(true)->save($data)) { $userId = Db::name('parts')->getLastInsID(); //关联车型 if(empty($data['configure_id'])){ }else{ foreach ($data['configure_id'] as $k => $v) { // $carsID[$k]=intval($v); Db::table('yp_parts_configure') ->data(['parts_id' =>$userId, 'configure_id' => intval($v)]) ->insert(); } // halt($carsID); } //关联工厂 if(empty($data['factory'])){ }else { //halt($data['factory']); foreach ($data['factory'] as $k => $v) { $factory[$k] = ['parts_id'=>$userId,'factory_id'=>intval($v['factory_id']),'onename' => $v['onename'], 'twoname' => $v['twoname'], 'threename' => $v['threename']]; Db::table('yp_parts_factory') ->data($factory[$k]) ->insert(); } } $this->success('添加成功'); } $this->success('添加失败'); } } //老新增备份 /*public function save44($pid=0) { if ($this->request->isPost()) { $data=$this->request->param(); // $data['photo']=json_encode($data['photo']); $data['types']=json_encode(array($data['name'],$data['value'])); $data['user_id']=Session::get('admin_id'); halt($data); if(model('Parts')->allowField(true)->save($data)){ //关联车型 foreach ($data['configure_id'] as $k => $v) { $carsID[$k]=intval($v); } model('Parts')->Configure()->attach($carsID); //关联工厂 foreach ($data['factory'] as $k => $v) { $factory[$k]=[intval($v['factory_id']),['onename'=>$v['onename'],'twoname'=>$v['twoname'],'threename'=>$v['threename']]]; $factoryID[$k]=intval($v['factory_id']); } model('Parts')->Factory()->attach($factoryID); foreach ($factory as $k => $v) { db('partsFactory')->where('parts_id',model('Parts')->id)->where('factory_id',$v[0])->update($v[1]); } $this->success('添加成功'); } $this->success('添加失败'); } }*/ public function view($id) { $this->assign('r',model('Parts')::get($id)); return $this->fetch(); } public function edit($id) { $r=model('Parts')->where('id',$id)->find($id); $rfactory=Db::table('yp_parts_factory')->where('parts_id',$id)->select(); $rconfigure=Db::table('yp_parts_configure')->where('parts_id',$id)->select(); $this->assign('rfactory',$rfactory); $this->assign('rconfigure',$rconfigure); $this->assign('r',$r); return $this->fetch(); } public function update($id) { if ($this->request->isPost()) { $data=$this->request->param(); if(input('?.photo')){ $data['photo']=json_encode($data['photo']); } $data['types']=json_encode(array($data['name'],$data['value'])); if(model('Parts')->allowField(true)->save($data,['id'=>$data['id']])){ //关联车型 if(empty($data['configure_id'])){ Db::table('yp_parts_configure')->where('parts_id','=',$data['id'])->delete(); }else{ $cid=''; foreach ($data['configure_id'] as $k => $v) { Db::table('yp_parts_configure')->where('parts_id','=',$data['id'])->delete(); $cid.=intval($v).','; } foreach (explode(",",substr($cid, 0, -1)) as $key => $value) { $inserts[$key]['configure_id']=$value; $inserts[$key]['parts_id']=$data['id']; } Db::name('parts_configure')->insertAll($inserts); } //关联工厂 if(empty($data['factory'])){ Db::table('yp_parts_factory')->where('parts_id','=',$data['id'])->delete(); }else{ Db::table('yp_parts_factory')->where('parts_id','=',$data['id'])->delete(); foreach ($data['factory'] as $k => $v) { $factory[$k] = ['parts_id'=>$data['id'],'factory_id'=>intval($v['factory_id']),'onename' => $v['onename'], 'twoname' => $v['twoname'], 'threename' => $v['threename']]; Db::table('yp_parts_factory') ->data($factory[$k]) ->insert(); } } $this->success('更新成功'); } $this->success('更新失败'); } } //更新备份 /*public function update1($id) { if ($this->request->isPost()) { $data=$this->request->param(); if(input('?.photo')){ $data['photo']=json_encode($data['photo']); } $data['types']=json_encode(array($data['name'],$data['value'])); if(model('Parts')->allowField(true)->save($data,['id'=>$data['id']])){ //关联车型 foreach ($data['configure_id'] as $k => $v) { $carsID[$k]=intval($v); } model('Parts')->Configure()->detach(); model('Parts')->Configure()->attach($carsID); //关联工厂 foreach ($data['factory'] as $k => $v) { $factory[$k]=[intval($v['factory_id']),['onename'=>$v['onename'],'twoname'=>$v['twoname'],'threename'=>$v['threename']]]; $factoryID[$k]=intval($v['factory_id']); } model('Parts')->Factory()->detach(); model('Parts')->Factory()->attach($factoryID); foreach ($factory as $k => $v) { db('partsFactory')->where('parts_id',model('Parts')->id)->where('factory_id',$v[0])->update($v[1]); } $this->success('更新成功'); } $this->success('更新失败'); } }*/ public function del($id = 0, $ids = []) { $id = $ids ? $ids : $id; if ($id) { $r=model('Parts')->where('id',$id)->find($id); if ($r->delete()) { $this->success('删除成功'); } else { $this->error('删除失败'); } } else { $this->error('请选择需要删除的条目'); } } } ~~~