Signrecommand.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 Signrecommand extends Backend
  14. {
  15. /**
  16. * SignRecommand模型对象
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = model('SignRecommand');
  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=sign_recommand.book_id')
  48. ->where($where)
  49. ->order($sort, $order)
  50. ->count();
  51. $list = $this->model
  52. ->join('book', 'book.id=sign_recommand.book_id')
  53. ->field(['sign_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. $bookRelations = BookRelationService::instance()->getBookRelationById($params['book_id']);
  87. if ( array_intersect($bookIds,$bookRelations) ){
  88. $this->error('该书籍与已添加的书籍 存在互斥关系,不可添加');
  89. }
  90. unset( $bookRelations );
  91. }
  92. //是否采用模型验证
  93. if ($this->modelValidate)
  94. {
  95. $name = basename(str_replace('\\', '/', get_class($this->model)));
  96. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  97. $this->model->validate($validate);
  98. }
  99. $result = $this->model->allowField(true)->save($params);
  100. if ($result !== false)
  101. {
  102. $redis = Redis::instance();
  103. $key = 'SRB:' . $params['sex'];
  104. $redis->del($key);
  105. $this->success();
  106. }
  107. else
  108. {
  109. $this->error($this->model->getError());
  110. }
  111. }
  112. catch (\think\exception\PDOException $e)
  113. {
  114. $this->error($e->getMessage());
  115. }
  116. }
  117. $this->error(__('Parameter %s can not be empty', ''));
  118. }
  119. return $this->view->fetch();
  120. }
  121. /**
  122. * 编辑
  123. */
  124. public function edit($ids = NULL)
  125. {
  126. $row = $this->model->get($ids);
  127. if (!$row)
  128. $this->error(__('No Results were found'));
  129. $adminIds = $this->getDataLimitAdminIds();
  130. if (is_array($adminIds))
  131. {
  132. if (!in_array($row[$this->dataLimitField], $adminIds))
  133. {
  134. $this->error(__('You have no permission'));
  135. }
  136. }
  137. if ($this->request->isPost())
  138. {
  139. $params = $this->request->post("row/a");
  140. if ($params)
  141. {
  142. /*
  143. * 已经弃用,如果为了兼容老版可取消注释
  144. foreach ($params as $k => &$v)
  145. {
  146. $v = is_array($v) ? implode(',', $v) : $v;
  147. }
  148. */
  149. try
  150. {
  151. //是否采用模型验证
  152. if ($this->modelValidate)
  153. {
  154. $name = basename(str_replace('\\', '/', get_class($this->model)));
  155. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  156. $row->validate($validate);
  157. }
  158. $result = $row->allowField(true)->save($params);
  159. if ($result !== false)
  160. {
  161. $redis = Redis::instance();
  162. $key = 'SRB:' . $params['sex'];
  163. $redis->del($key);
  164. $this->success();
  165. }
  166. else
  167. {
  168. $this->error($row->getError());
  169. }
  170. }
  171. catch (\think\exception\PDOException $e)
  172. {
  173. $this->error($e->getMessage());
  174. }
  175. }
  176. $this->error(__('Parameter %s can not be empty', ''));
  177. }
  178. $this->view->assign("row", $row);
  179. return $this->view->fetch();
  180. }
  181. /**
  182. * 删除
  183. */
  184. public function del($ids = "")
  185. {
  186. if ($ids)
  187. {
  188. $pk = $this->model->getPk();
  189. $adminIds = $this->getDataLimitAdminIds();
  190. if (is_array($adminIds))
  191. {
  192. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  193. }
  194. $list = $this->model->where($pk, 'in', $ids)->select();
  195. $count = 0;
  196. foreach ($list as $k => $v)
  197. {
  198. $count += $v->delete();
  199. }
  200. if ($count)
  201. {
  202. $redis = Redis::instance();
  203. $redis->del('SRB:1');
  204. $redis->del('SRB:2');
  205. $redis->del('SRB:W:2');
  206. $redis->del('SRB:W:1');
  207. $this->success();
  208. }
  209. else
  210. {
  211. $this->error(__('No rows were deleted'));
  212. }
  213. }
  214. $this->error(__('Parameter %s can not be empty', 'ids'));
  215. }
  216. }