ArticleCateModel.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. use think\Db;
  5. class ArticleCateModel extends Model
  6. {
  7. protected $name = 'article_cate';
  8. // 开启自动写入时间戳
  9. protected $autoWriteTimestamp = true;
  10. /**
  11. * [getAllCate 获取全部分类]
  12. * @author
  13. */
  14. public function getAllCate($map, $Nowpage, $limits,$od)
  15. {
  16. return $this->where($map)->page($Nowpage, $limits)->order($od)->select();
  17. }
  18. /**
  19. * [getCate 获取全部分类]
  20. * @author
  21. */
  22. public function getCate()
  23. {
  24. return $this->order('create_time desc')->select();
  25. }
  26. /**
  27. * 根据条件获取所有配置信息数量
  28. * @param $map
  29. */
  30. public function getAllCount($map)
  31. {
  32. return $this->where($map)->count();
  33. }
  34. /**
  35. * [insertCate 添加分类]
  36. * @author
  37. */
  38. public function insertCate($param)
  39. {
  40. Db::startTrans();// 启动事务
  41. try{
  42. $this->save($param);
  43. Db::commit();// 提交事务
  44. writelog(session('uid'),session('username'),'文章分类【'.$param['name'].'】添加成功',1);
  45. return ['code' => 200, 'data' => '', 'msg' => '分类添加成功'];
  46. }catch( \Exception $e){
  47. Db::rollback();// 回滚事务
  48. writelog(session('uid'),session('username'),'文章分类【'.$param['name'].'】添加失败',2);
  49. return ['code' => 100, 'data' => '', 'msg' => '分类添加失败'];
  50. }
  51. }
  52. /**
  53. * [editMenu 编辑分类]
  54. * @author
  55. */
  56. public function editCate($param)
  57. {
  58. Db::startTrans();// 启动事务
  59. try{
  60. $this->save($param, ['id' => $param['id']]);
  61. Db::commit();// 提交事务
  62. writelog(session('uid'),session('username'),'文章分类【'.$param['name'].'】编辑成功',1);
  63. return ['code' => 200, 'data' => '', 'msg' => '分类编辑成功'];
  64. }catch( \Exception $e){
  65. Db::rollback();// 回滚事务
  66. writelog(session('uid'),session('username'),'文章分类【'.$param['name'].'】编辑失败',2);
  67. return ['code' => 100, 'data' => '', 'msg' => '分类编辑失败'];
  68. }
  69. }
  70. /**
  71. * [getOneMenu 根据分类id获取一条信息]
  72. * @return [type] [description]
  73. * @author
  74. */
  75. public function getOneCate($id)
  76. {
  77. return $this->where('id', $id)->find();
  78. }
  79. /**
  80. * [delMenu 删除分类]
  81. * @return [type] [description]
  82. * @author
  83. */
  84. public function delCate($id)
  85. {
  86. $name = $this->where('id',$id)->value('name');
  87. Db::startTrans();// 启动事务
  88. try{
  89. $this->where('id', $id)->delete();
  90. Db::commit();// 提交事务
  91. writelog(session('uid'),session('username'),'文章分类【'.$name.'】删除成功',1);
  92. return ['code' => 200, 'data' => '', 'msg' => '分类删除成功'];
  93. }catch( \Exception $e){
  94. Db::rollback();// 回滚事务
  95. writelog(session('uid'),session('username'),'文章分类【'.$name.'】删除失败',2);
  96. return ['code' => 100, 'data' => '', 'msg' => '分类删除失败'];
  97. }
  98. }
  99. /**
  100. * batchDelCate 批量删除文章分类
  101. * @param $param
  102. * @return array
  103. */
  104. public function batchDelCate($param){
  105. Db::startTrans();// 启动事务
  106. try{
  107. ArticleCateModel::destroy($param);
  108. Db::commit();// 提交事务
  109. writelog(session('uid'),session('username'),'文章分类批量删除成功',1);
  110. return ['code' => 200, 'data' => '', 'msg' => '批量删除成功'];
  111. }catch( \Exception $e){
  112. Db::rollback();// 回滚事务
  113. writelog(session('uid'),session('username'),'文章分类批量删除失败',2);
  114. return ['code' => 100, 'data' => '', 'msg' => '批量删除失败'];
  115. }
  116. }
  117. /**
  118. * [cateState 文章分类状态]
  119. * @param $param
  120. * @return array
  121. */
  122. public function cateState($id,$num)
  123. {
  124. $name = Db::name('article_cate')->where(['id'=>$id])->value('name');//判断当前状态情况
  125. if($num == 2){
  126. $msg = '禁用';
  127. }else{
  128. $msg = '启用';
  129. }
  130. Db::startTrans();// 启动事务
  131. try {
  132. $this->where ('id' , $id)->setField (['status' => $num]);
  133. Db::commit();// 提交事务
  134. writelog (session ('uid') , session ('username') , '文章分类【' . $name . '】'.$msg.'成功' , 1);
  135. // return ['code' => 200 , 'data' => '' , 'msg' => '已'.$msg];
  136. } catch (\Exception $e) {
  137. Db::rollback();// 回滚事务
  138. writelog (session ('uid') , session ('username') , '文章分类【' . $name . '】'.$msg.'失败' , 2);
  139. return ['code' => 100 , 'data' => '' , 'msg' => $msg.'失败'];
  140. }
  141. }
  142. /**
  143. * 批量禁用分类
  144. * @param $param
  145. * @return array
  146. */
  147. public function forbiddenCate($param){
  148. Db::startTrans();// 启动事务
  149. try{
  150. if($param){
  151. $this->saveAll($param);
  152. }else{
  153. $this->where('1=1')->update(['status'=>2]);
  154. }
  155. Db::commit();// 提交事务
  156. writelog(session('uid'),session('username'),'批量禁用文章分类成功',1);
  157. return ['code' => 200, 'data' => '', 'msg' => '批量禁用成功'];
  158. }catch( \Exception $e){
  159. Db::rollback ();//回滚事务
  160. writelog(session('uid'),session('username'),'批量禁用文章分类失败',2);
  161. return ['code' => 100, 'data' => '', 'msg' => '批量禁用失败'];
  162. }
  163. }
  164. /**
  165. * 批量启用分类
  166. * @param $param
  167. * @return array
  168. */
  169. public function usingCate($param){
  170. Db::startTrans();// 启动事务
  171. try{
  172. if($param){
  173. $this->saveAll($param);
  174. }else{
  175. $this->where('1=1')->update(['status'=>1]);
  176. }
  177. Db::commit();// 提交事务
  178. writelog(session('uid'),session('username'),'批量启用文章分类成功',1);
  179. return ['code' => 200, 'data' => '', 'msg' => '批量启用成功'];
  180. }catch( \Exception $e){
  181. Db::rollback ();//回滚事务
  182. writelog(session('uid'),session('username'),'批量启用文章分类失败',2);
  183. return ['code' => 100, 'data' => '', 'msg' => '批量启用失败'];
  184. }
  185. }
  186. }