0
0

QualityCate.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace app\common\model;
  3. use app\common\util\AppAuth;
  4. use app\hander\HelpHander;
  5. use think\Db;
  6. class QualityCate extends Base {
  7. protected $createTime = 'create_time';
  8. protected $updateTime = 'update_time';
  9. public $table = 'quality_cate';
  10. protected $validateName = 'QualityCate';
  11. public function updates(){
  12. $data = request()->post();
  13. $data['org_id'] =cur_org_id();
  14. $result = validate($this->validateName)->check($data,[],'');
  15. if(true !== $result){
  16. $this->error = validate($this->validateName)->getError();
  17. return false;
  18. }
  19. $id = $data['id'];
  20. $this->startTrans();
  21. try{
  22. if($id > 0){
  23. $data['update_time'] = date('Y-m-d H:i:s');
  24. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  25. if(!$ret){
  26. exception('保存失败');
  27. }
  28. }else{
  29. if(!empty($data['pid'])){
  30. $pInfo = Db::name('quality_cate')
  31. ->where('id',$data['pid'])
  32. ->find();
  33. if($pInfo['pid']==0){
  34. $data['level']==2;
  35. }else{
  36. $data['level'] = 3;
  37. }
  38. }else{
  39. $data['level']==1;
  40. }
  41. $data['create_time'] = date('Y-m-d H:i:s');
  42. $ret = $this->insertGetId($data);
  43. $id = $ret;
  44. if(!$id){
  45. exception('保存失败');
  46. }
  47. }
  48. $this->commit();
  49. return true;
  50. }catch (\Exception $e){
  51. // 回滚事务
  52. $this->error = $e->getMessage();
  53. $this->rollback();
  54. return false;
  55. }
  56. }
  57. public function list($orgId){
  58. $map[] = ['org_id','=',$orgId];
  59. $map[] = ['enable','=',1];
  60. $map[] = ['del','=',0];
  61. $list = Db::name('quality_cate')
  62. ->where($map)
  63. ->where('pid','=',0)
  64. ->field('id,title')
  65. ->select();
  66. foreach ($list as $k=>$v){
  67. $pList = Db::name('quality_cate')
  68. ->where('pid',$v['id'])
  69. ->where($map)
  70. ->field('id,title')
  71. ->select();
  72. $list[$k]['child'] = $pList;
  73. foreach ($pList as $k1=>$v1){
  74. $cList = Db::name('quality_cate')
  75. ->where('pid',$v1['id'])
  76. ->where($map)
  77. ->field('id,title')
  78. ->select();
  79. $list[$k]['child'][$k1]['child'] = $cList;
  80. }
  81. }
  82. return $list;
  83. }
  84. public function getPTitle($id){
  85. $title = '';
  86. $info = Db::name('quality_cate')
  87. ->where('id',$id)
  88. ->find();
  89. if($info){
  90. $pInfo = Db::name('quality_cate')
  91. ->where('id',$info['pid'])
  92. ->find();
  93. $PPInfo = Db::name('quality_cate')
  94. ->where('id',$pInfo['pid'])
  95. ->find();
  96. $title = $PPInfo['title'].'/'.$pInfo['title'].'/'.$info['title'];
  97. }
  98. return $title;
  99. }
  100. }