123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- use think\Model;
- class AssetItems extends Model
- {
- public $statusTxt = ['禁用','在库','使用','借用','维修','处置','待处置'];
- public function updates(){
- $data = request()->post();
- $data['org_id'] = cur_org_id();
- $result = validate('asset_items')->check($data,[],'');
- if(true !== $result){
- $this->error = validate('asset_items')->getError();
- return false;
- }
- if($data['cate_id']){
- $arr = $data['cate_id']?explode(',',$data['cate_id']):[];
- $data['cate_id'] = end($arr);
- }
- $id = $data['id'];
- unset($data['id']);
- if($id > 0){
- $data['update_time'] = date('Y-m-d H:i:s');
- unset($data['nums']);
- $ret = $this->allowField(true)->save($data,['id'=>$id]);
- if(!$ret){
- $this->error = '操作失败';
- return false;
- }
- }else{
- $data['create_time'] = date('Y-m-d H:i:s');
- Db::startTrans();
- try{
- $max = Db::name('asset_items')
- ->where('org_id',$data['org_id'])
- ->where('create_time','>=',date('Y-m-d').' 00:00:00')
- ->order('id desc')
- ->value('sn');
- $maxinit = $max?$max:date('Ymd').'0000';
- $nums = $data['nums'];
- $data['nums'] = 1;
- for ($i=1;$i<=$nums;$i++){
- $data['sn'] = $maxinit + $i;
- $ret = Db::name('asset_items')->insert($data);
- if(!$ret){
- \exception('操作失败');
- }
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- $this->error = $e->getMessage();
- return false;
- }
- }
- return true;
- }
- public function info($id){
- $info = $this->where('id',$id)->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- return $info->toArray();
- }
- // type 0=全部在库和使用 1=在库 2=使用
- // 去除掉正在审批中的物品
- public function selectlists($page,$size,$title,$cateId,$companyId,$addId,$classId,$userId,$orgId,$type=0){
- $map[] = ['org_id','=',$orgId];
- $map[] = ['del','=',0];
- // $map[] = ['is_check','=',0]; // 不取盘库中的物品
- // $ids = $this->itemsuse($orgId);
- // if($ids){
- // $map[] = ['id','not in',$ids];
- // }
- if($cateId > 0){
- $map[] = ['cate_id','=',$cateId];
- }
- if($title){
- $map[] = ['sn|title','like','%'.$title.'%'];
- }
- if($companyId > 0){
- $map[] = ['company_id','=',$companyId];
- }
- if($addId > 0){
- $map[] = ['add_id','=',$addId];
- }
- if($classId > 0){
- $map[] = ['class_id','=',$classId];
- }
- if($type == 1){
- $map[] = ['enable','=',1];
- } else if($type == 2){
- $map[] = ['enable','=',2];
- // $map[] = ['user_id','=',$userId];
- }else{
- $map[] = ['enable','in','1,2'];
- }
- $lists = $this
- ->where($map)
- ->page($page,$size)
- ->order('id desc')
- ->select();
- $lists = $lists?$lists->toArray():[];
- foreach ($lists as $k=>$v){
- $lists[$k]['cate_name'] = Db::name('asset_cate')->where('id',$v['cate_id'])->value('title');
- $lists[$k]['unit_name'] = Db::name('asset_unit')->where('id',$v['unit_id'])->value('title');
- $lists[$k]['class_name'] = Db::name('asset_class')->where('id',$v['class_id'])->value('title');
- $lists[$k]['add_name'] = Db::name('asset_add')->where('id',$v['add_id'])->value('title');
- $lists[$k]['user_name'] = '';
- $lists[$k]['dep_name'] = '';
- if($v['user_id'] > 0){
- $lists[$k]['user_name'] = Db::name('user')->where('id',$v['user_id'])->value('real_name');
- }
- if($v['dep_id'] > 0){
- $lists[$k]['dep_name'] = Db::name('dep')->where('id',$v['dep_id'])->value('title');
- }
- }
- $total = $this->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists
- ];
- return $data;
- }
- }
|