123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Model;
- class DocumentCate extends Model
- {
- public function add(){
- $data = [
- 'id' => input('id/d',0),
- 'name' => input('name','','trim'),
- 'org_id' => input('orgId/d',0),
- 'parent_id' => input('parentId/d',0),
- ];
- $result = validate('DocumentCate')->check($data,[],'');
- if(true !== $result){
- HelpHander::error(validate('DocumentCate')->getError());
- }
- $id = $data['id'];
- unset($data['id']);
- if($id > 0){
- $ret = $this->allowField(true)->save($data,['id'=>$id]);
- }else{
- $ret = $this->allowField(true)->save($data);
- }
- if(!$ret){
- HelpHander::error('操作失败');
- }
- return true;
- }
- public function info($id){
- $info = $this->where('id',$id)->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- return $info->toArray();
- }
- public function lists($orgId){
- $lists = $this->where('org_id',$orgId)->select();
- $lists = $lists?$lists->toArray():[];
- $ids = [];
- foreach ($lists as $v){
- $ids[] = $v['id'];
- }
- $lists = list_to_tree($lists, 'parent_id', 'childList');
- return ['ids' => $ids, 'tree' => $lists];
- }
- public function all($orgId){
- $lists = $this->select();
- $lists = $lists?$lists->toArray():[];
- return $lists;
- }
- public function del($id){
- // 检查是否已被使用
- $info = Db::name('document')
- ->where('cate_id',$id)
- ->where('del',0)
- ->find();
- if($info){
- HelpHander::error('已被使用,无法删除');
- }
- $cate = Db::name('document_cate')->where('parent_id',$id)->find();
- if($cate){
- HelpHander::error('已被使用,无法删除');
- }
- $ret = $this->where('id',$id)->delete();
- if(!$ret){
- HelpHander::error('删除失败');
- }
- return true;
- }
- public function queryDocumentList($title,$userId,$orgId){
- if(!$title){ // 为空取一级分类
- $lists = Db::name('document_cate')
- ->where('org_id',$orgId)
- ->where('parent_id',0)
- ->order('id asc')
- ->select();
- } else {
- $lists = model('Document')->getAppList($title,$userId,$orgId);
- }
- return $lists?$lists:[];
- }
- public function getSubList($cateId,$orgId){
- $lists = Db::name('document_cate')
- ->where('org_id',$orgId)
- ->where('parent_id',$cateId)
- ->order('id asc')
- ->select();
- $lists = $lists?$lists:[];
- foreach ($lists as $k=>$v){
- $lists[$k]['type'] = 0;
- $lists[$k]['file'] = '';
- $lists[$k]['source_file'] = '';
- }
- return $lists;
- }
- }
|