DocumentCate.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Model;
  6. class DocumentCate extends Model
  7. {
  8. public function add(){
  9. $data = [
  10. 'id' => input('id/d',0),
  11. 'name' => input('name','','trim'),
  12. 'org_id' => input('orgId/d',0),
  13. 'parent_id' => input('parentId/d',0),
  14. ];
  15. $result = validate('DocumentCate')->check($data,[],'');
  16. if(true !== $result){
  17. HelpHander::error(validate('DocumentCate')->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($orgId){
  39. $lists = $this->where('org_id',$orgId)->select();
  40. $lists = $lists?$lists->toArray():[];
  41. $ids = [];
  42. foreach ($lists as $v){
  43. $ids[] = $v['id'];
  44. }
  45. $lists = list_to_tree($lists, 'parent_id', 'childList');
  46. return ['ids' => $ids, 'tree' => $lists];
  47. }
  48. public function all($orgId){
  49. $lists = $this->select();
  50. $lists = $lists?$lists->toArray():[];
  51. return $lists;
  52. }
  53. public function del($id){
  54. // 检查是否已被使用
  55. $info = Db::name('document')
  56. ->where('cate_id',$id)
  57. ->where('del',0)
  58. ->find();
  59. if($info){
  60. HelpHander::error('已被使用,无法删除');
  61. }
  62. $cate = Db::name('document_cate')->where('parent_id',$id)->find();
  63. if($cate){
  64. HelpHander::error('已被使用,无法删除');
  65. }
  66. $ret = $this->where('id',$id)->delete();
  67. if(!$ret){
  68. HelpHander::error('删除失败');
  69. }
  70. return true;
  71. }
  72. public function queryDocumentList($title,$userId,$orgId){
  73. if(!$title){ // 为空取一级分类
  74. $lists = Db::name('document_cate')
  75. ->where('org_id',$orgId)
  76. ->where('parent_id',0)
  77. ->order('id asc')
  78. ->select();
  79. } else {
  80. $lists = model('Document')->getAppList($title,$userId,$orgId);
  81. }
  82. return $lists?$lists:[];
  83. }
  84. public function getSubList($cateId,$orgId){
  85. $lists = Db::name('document_cate')
  86. ->where('org_id',$orgId)
  87. ->where('parent_id',$cateId)
  88. ->order('id asc')
  89. ->select();
  90. $lists = $lists?$lists:[];
  91. foreach ($lists as $k=>$v){
  92. $lists[$k]['type'] = 0;
  93. $lists[$k]['file'] = '';
  94. $lists[$k]['source_file'] = '';
  95. }
  96. return $lists;
  97. }
  98. }