0
0

AssetItems.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Model;
  7. class AssetItems extends Model
  8. {
  9. public $statusTxt = ['禁用','在库','使用','借用','维修','处置','待处置'];
  10. public function updates(){
  11. $data = request()->post();
  12. $data['org_id'] = cur_org_id();
  13. $result = validate('asset_items')->check($data,[],'');
  14. if(true !== $result){
  15. $this->error = validate('asset_items')->getError();
  16. return false;
  17. }
  18. if($data['cate_id']){
  19. $arr = $data['cate_id']?explode(',',$data['cate_id']):[];
  20. $data['cate_id'] = end($arr);
  21. }
  22. $id = $data['id'];
  23. unset($data['id']);
  24. if($id > 0){
  25. $data['update_time'] = date('Y-m-d H:i:s');
  26. unset($data['nums']);
  27. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  28. if(!$ret){
  29. $this->error = '操作失败';
  30. return false;
  31. }
  32. }else{
  33. $data['create_time'] = date('Y-m-d H:i:s');
  34. Db::startTrans();
  35. try{
  36. $max = Db::name('asset_items')
  37. ->where('org_id',$data['org_id'])
  38. ->where('create_time','>=',date('Y-m-d').' 00:00:00')
  39. ->order('id desc')
  40. ->value('sn');
  41. $maxinit = $max?$max:date('Ymd').'0000';
  42. $nums = $data['nums'];
  43. $data['nums'] = 1;
  44. for ($i=1;$i<=$nums;$i++){
  45. $data['sn'] = $maxinit + $i;
  46. $ret = Db::name('asset_items')->insert($data);
  47. if(!$ret){
  48. \exception('操作失败');
  49. }
  50. }
  51. Db::commit();
  52. }catch (Exception $e){
  53. Db::rollback();
  54. $this->error = $e->getMessage();
  55. return false;
  56. }
  57. }
  58. return true;
  59. }
  60. public function info($id){
  61. $info = $this->where('id',$id)->find();
  62. if(!$info){
  63. HelpHander::error('数据不存在');
  64. }
  65. return $info->toArray();
  66. }
  67. // type 0=全部在库和使用 1=在库 2=使用
  68. // 去除掉正在审批中的物品
  69. public function selectlists($page,$size,$title,$cateId,$companyId,$addId,$classId,$userId,$orgId,$type=0){
  70. $map[] = ['org_id','=',$orgId];
  71. $map[] = ['del','=',0];
  72. // $map[] = ['is_check','=',0]; // 不取盘库中的物品
  73. // $ids = $this->itemsuse($orgId);
  74. // if($ids){
  75. // $map[] = ['id','not in',$ids];
  76. // }
  77. if($cateId > 0){
  78. $map[] = ['cate_id','=',$cateId];
  79. }
  80. if($title){
  81. $map[] = ['sn|title','like','%'.$title.'%'];
  82. }
  83. if($companyId > 0){
  84. $map[] = ['company_id','=',$companyId];
  85. }
  86. if($addId > 0){
  87. $map[] = ['add_id','=',$addId];
  88. }
  89. if($classId > 0){
  90. $map[] = ['class_id','=',$classId];
  91. }
  92. if($type == 1){
  93. $map[] = ['enable','=',1];
  94. } else if($type == 2){
  95. $map[] = ['enable','=',2];
  96. // $map[] = ['user_id','=',$userId];
  97. }else{
  98. $map[] = ['enable','in','1,2'];
  99. }
  100. $lists = $this
  101. ->where($map)
  102. ->page($page,$size)
  103. ->order('id desc')
  104. ->select();
  105. $lists = $lists?$lists->toArray():[];
  106. foreach ($lists as $k=>$v){
  107. $lists[$k]['cate_name'] = Db::name('asset_cate')->where('id',$v['cate_id'])->value('title');
  108. $lists[$k]['unit_name'] = Db::name('asset_unit')->where('id',$v['unit_id'])->value('title');
  109. $lists[$k]['class_name'] = Db::name('asset_class')->where('id',$v['class_id'])->value('title');
  110. $lists[$k]['add_name'] = Db::name('asset_add')->where('id',$v['add_id'])->value('title');
  111. $lists[$k]['user_name'] = '';
  112. $lists[$k]['dep_name'] = '';
  113. if($v['user_id'] > 0){
  114. $lists[$k]['user_name'] = Db::name('user')->where('id',$v['user_id'])->value('real_name');
  115. }
  116. if($v['dep_id'] > 0){
  117. $lists[$k]['dep_name'] = Db::name('dep')->where('id',$v['dep_id'])->value('title');
  118. }
  119. }
  120. $total = $this->where($map)->count();
  121. $data = [
  122. 'total' => $total,
  123. 'list' => $lists
  124. ];
  125. return $data;
  126. }
  127. }