AssetUnit.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Model;
  6. class AssetUnit extends Model
  7. {
  8. public function add(){
  9. $data = [
  10. 'id' => input('id/d',0),
  11. 'title' => input('title','','trim'),
  12. 'org_id' => input('orgId/d',0),
  13. ];
  14. $result = validate('AssetUnit')->check($data,[],'');
  15. if(true !== $result){
  16. HelpHander::error(validate('AssetUnit')->getError());
  17. }
  18. $id = $data['id'];
  19. unset($data['id']);
  20. if($id > 0){
  21. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  22. }else{
  23. $ret = $this->allowField(true)->save($data);
  24. }
  25. if(!$ret){
  26. HelpHander::error('操作失败');
  27. }
  28. return true;
  29. }
  30. public function info($id){
  31. $info = $this->where('id',$id)->find();
  32. if(!$info){
  33. HelpHander::error('数据不存在');
  34. }
  35. return $info->toArray();
  36. }
  37. public function lists($page,$size,$title,$orgId){
  38. $map[] = ['org_id','=',$orgId];
  39. if($title){
  40. $map[] = ['title','like','%'.$title.'%'];
  41. }
  42. $lists = $this
  43. ->where($map)
  44. ->page($page,$size)
  45. ->order('id desc')
  46. ->select();
  47. $total = $this->where($map)->count();
  48. $data = [
  49. 'total' => $total,
  50. 'list' => $lists?$lists->toArray():[]
  51. ];
  52. return $data;
  53. }
  54. public function all($orgId){
  55. $map[] = ['org_id','=',$orgId];
  56. $lists = $this
  57. ->where($map)
  58. ->field('id,title')
  59. ->order('sort asc,id asc')
  60. ->select();
  61. return $lists?$lists->toArray():[];
  62. }
  63. public function del($id){
  64. // 检查是否已被使用
  65. $info = Db::name('asset_items')
  66. ->where('unit_id',$id)
  67. ->find();
  68. if($info){
  69. HelpHander::error('已被使用,无法删除');
  70. }
  71. $ret = $this->where('id',$id)->delete();
  72. if(!$ret){
  73. HelpHander::error('删除失败');
  74. }
  75. return true;
  76. }
  77. }