Replyrecommand.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\library\Redis;
  5. use app\common\service\BookRelationService;
  6. use think\Controller;
  7. use think\Request;
  8. /**
  9. * 关键字回复书库
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Replyrecommand extends Backend
  14. {
  15. /**
  16. * SignRecommand模型对象
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = model('GzhreplyRecommand');
  23. $this->view->assign("sexList", $this->model->getSexList());
  24. $this->view->assign("statusList", $this->model->getStatusList());
  25. }
  26. /**
  27. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  28. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  29. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  30. */
  31. /**
  32. * 查看
  33. */
  34. public function index()
  35. {
  36. //设置过滤方法
  37. $this->request->filter(['strip_tags']);
  38. if ($this->request->isAjax())
  39. {
  40. //如果发送的来源是Selectpage,则转发到Selectpage
  41. if ($this->request->request('pkey_name'))
  42. {
  43. return $this->selectpage();
  44. }
  45. list($where, $sort, $order, $offset, $limit) = $this->buildparams('', true);
  46. $total = $this->model
  47. ->join('book', 'book.id=gzhreply_recommand.book_id')
  48. ->where($where)
  49. ->order($sort, $order)
  50. ->count();
  51. $list = $this->model
  52. ->join('book', 'book.id=gzhreply_recommand.book_id')
  53. ->field(['gzhreply_recommand.*','book.cansee','book.state'])
  54. ->where($where)
  55. ->order($sort, $order)
  56. ->limit($offset, $limit)
  57. ->select();
  58. $result = array("total" => $total, "rows" => $list);
  59. return json($result);
  60. }
  61. return $this->view->fetch();
  62. }
  63. /**
  64. * 添加
  65. */
  66. public function add()
  67. {
  68. if ($this->request->isPost())
  69. {
  70. $params = $this->request->post("row/a");
  71. if ($params)
  72. {
  73. if ($this->dataLimit)
  74. {
  75. $params[$this->dataLimitField] = $this->auth->id;
  76. }
  77. try
  78. {
  79. $res = $this->model->all(['sex'=>$params['sex']]);
  80. if ( !empty($res) ){
  81. $bookIds = array_column($res, 'book_id');
  82. if( in_array( $params['book_id'], $bookIds ) ){
  83. $this->error('该书籍已经存在,不可重复添加');
  84. }
  85. //获取推荐库里的互斥书
  86. $bookIds[] = $params['book_id'];
  87. $bookRelations = BookRelationService::instance()->getBookRelationById($params['book_id']);
  88. if ( array_intersect($bookIds,$bookRelations) ){
  89. $this->error('该书籍与已添加的书籍 存在互斥关系,不可添加');
  90. }
  91. unset( $bookRelations );
  92. }
  93. //是否采用模型验证
  94. if ($this->modelValidate)
  95. {
  96. $name = basename(str_replace('\\', '/', get_class($this->model)));
  97. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  98. $this->model->validate($validate);
  99. }
  100. $result = $this->model->allowField(true)->save($params);
  101. if ($result !== false)
  102. {
  103. $redis = Redis::instance();
  104. $key = 'SRB:' . $params['sex'];
  105. $redis->del($key);
  106. $key = 'SRB:W:' . $params['sex'];
  107. $redis->del($key);
  108. $this->success();
  109. }
  110. else
  111. {
  112. $this->error($this->model->getError());
  113. }
  114. }
  115. catch (\think\exception\PDOException $e)
  116. {
  117. $this->error($e->getMessage());
  118. }
  119. }
  120. $this->error(__('Parameter %s can not be empty', ''));
  121. }
  122. return $this->view->fetch();
  123. }
  124. /**
  125. * 编辑
  126. */
  127. public function edit($ids = NULL)
  128. {
  129. $row = $this->model->get($ids);
  130. if (!$row)
  131. $this->error(__('No Results were found'));
  132. $adminIds = $this->getDataLimitAdminIds();
  133. if (is_array($adminIds))
  134. {
  135. if (!in_array($row[$this->dataLimitField], $adminIds))
  136. {
  137. $this->error(__('You have no permission'));
  138. }
  139. }
  140. if ($this->request->isPost())
  141. {
  142. $params = $this->request->post("row/a");
  143. if ($params)
  144. {
  145. /*
  146. * 已经弃用,如果为了兼容老版可取消注释
  147. foreach ($params as $k => &$v)
  148. {
  149. $v = is_array($v) ? implode(',', $v) : $v;
  150. }
  151. */
  152. try
  153. {
  154. //是否采用模型验证
  155. if ($this->modelValidate)
  156. {
  157. $name = basename(str_replace('\\', '/', get_class($this->model)));
  158. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  159. $row->validate($validate);
  160. }
  161. $result = $row->allowField(true)->save($params);
  162. if ($result !== false)
  163. {
  164. $redis = Redis::instance();
  165. $key = 'SRB:' . $params['sex'];
  166. $redis->del($key);
  167. $key = 'SRB:W:' . $params['sex'];
  168. $redis->del($key);
  169. $this->success();
  170. }
  171. else
  172. {
  173. $this->error($row->getError());
  174. }
  175. }
  176. catch (\think\exception\PDOException $e)
  177. {
  178. $this->error($e->getMessage());
  179. }
  180. }
  181. $this->error(__('Parameter %s can not be empty', ''));
  182. }
  183. $this->view->assign("row", $row);
  184. return $this->view->fetch();
  185. }
  186. /**
  187. * 删除
  188. */
  189. public function del($ids = "")
  190. {
  191. if ($ids)
  192. {
  193. $pk = $this->model->getPk();
  194. $adminIds = $this->getDataLimitAdminIds();
  195. if (is_array($adminIds))
  196. {
  197. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  198. }
  199. $list = $this->model->where($pk, 'in', $ids)->select();
  200. $count = 0;
  201. foreach ($list as $k => $v)
  202. {
  203. $count += $v->delete();
  204. }
  205. if ($count)
  206. {
  207. $redis = Redis::instance();
  208. $redis->del('SRB:1');
  209. $redis->del('SRB:2');
  210. $redis->del('SRB:W:1');
  211. $redis->del('SRB:W:2');
  212. $this->success();
  213. }
  214. else
  215. {
  216. $this->error(__('No rows were deleted'));
  217. }
  218. }
  219. $this->error(__('Parameter %s can not be empty', 'ids'));
  220. }
  221. }