OfficeCate.php 2.2 KB

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