Read.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. namespace app\admin\controller\campaign;
  3. use app\common\controller\Backend;
  4. use app\common\library\Redis;
  5. use app\common\service\CampaignService;
  6. use app\main\constants\CampaignConstants;
  7. use app\main\service\UserService;
  8. use think\Controller;
  9. use think\Request;
  10. /**
  11. * 消费活动-读书挑战赛
  12. *
  13. * @icon fa fa-circle-o
  14. */
  15. class Read extends Backend
  16. {
  17. /**
  18. * CampaignRead模型对象
  19. */
  20. protected $model = null;
  21. protected $noNeedRight = ['detail', 'ajaxlapse', 'ajaxgetgoods'];
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = model('CampaignRead');
  26. $this->view->assign("statusList", $this->model->getStatusList());
  27. $this->view->assign('levelList', $this->model->getDefaultLevel());
  28. $this->view->assign('popupPosition', $this->model->getPopupPosition());
  29. }
  30. /**
  31. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  32. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  33. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  34. */
  35. /**
  36. * 查看
  37. */
  38. public function index()
  39. {
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags']);
  42. if ($this->request->isAjax())
  43. {
  44. //如果发送的来源是Selectpage,则转发到Selectpage
  45. if ($this->request->request('pkey_name'))
  46. {
  47. return $this->selectpage();
  48. }
  49. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  50. $total = $this->model
  51. ->where($where)
  52. ->order($sort, $order)
  53. ->count();
  54. $list = $this->model
  55. ->where($where)
  56. ->order($sort, $order)
  57. ->limit($offset, $limit)
  58. ->select();
  59. $result = array("total" => $total, "rows" => $list);
  60. return json($result);
  61. }
  62. return $this->view->fetch();
  63. }
  64. /**
  65. * 添加
  66. */
  67. public function add()
  68. {
  69. if ($this->request->isPost()) {
  70. $params = $this->request->post("row/a");
  71. if ($params) {
  72. /*
  73. * 已经弃用,如果为了兼容老版可取消注释
  74. foreach ($params as $k => &$v)
  75. {
  76. $v = is_array($v) ? implode(',', $v) : $v;
  77. }
  78. */
  79. if ($this->dataLimit) {
  80. $params[$this->dataLimitField] = $this->auth->id;
  81. }
  82. try {
  83. //是否采用模型验证
  84. if ($this->modelValidate) {
  85. $name = basename(str_replace('\\', '/', get_class($this->model)));
  86. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  87. $this->model->validate($validate);
  88. }
  89. // 校验场次金额不能相同
  90. if($params['elementary_need'] == $params['intermediate_need'] || $params['elementary_need'] == $params['advanced_need'] || $params['intermediate_need'] == $params['advanced_need']){
  91. $this->error('初级场次、中级场次、高级场次金额不能相同');
  92. }
  93. if(strtotime($params['end_time']) <= strtotime($params['start_time'])){
  94. $this->error('活动结束时间应该大于活动开始时间');
  95. }
  96. $tmp_start_time = strtotime(date('Y-m-d 00:00:00', strtotime($params['start_time'])));
  97. $tmp_end_time = strtotime(date('Y-m-d 23:59:59', strtotime($params['end_time'])));
  98. // 校验活动时间是否有交叉
  99. $g_obj = $this->model->where(function ($query) use ($params, $tmp_start_time) {
  100. $query->where('start_time', '>=', $tmp_start_time)
  101. ->where('end_time', '>=', $tmp_start_time);
  102. })->find();
  103. if ($g_obj) {
  104. $this->error('活动开始时间跟历史活动时间有交叉');
  105. }
  106. // 校验活动时间是否有交叉
  107. $f_obj = $this->model->where(function ($query) use ($params, $tmp_start_time) {
  108. $query->where('start_time', '<=', $tmp_start_time)
  109. ->where('end_time', '>=', $tmp_start_time);
  110. })->find();
  111. if ($f_obj) {
  112. $this->error('活动开始时间跟历史活动时间有交叉');
  113. }
  114. // 校验活动时间是否有交叉
  115. $e_obj = $this->model->where(function ($query) use ($params, $tmp_end_time) {
  116. $query->where('start_time', '<=', $tmp_end_time)
  117. ->where('end_time', '>=', $tmp_end_time);
  118. })->find();
  119. if ($e_obj) {
  120. $this->error('活动结束时间跟历史活动时间有交叉');
  121. }
  122. if($params['popup_position']){
  123. $params['popup_position'] = implode(',', $params['popup_position']);
  124. }
  125. $result = $this->model->allowField(true)->save($params);
  126. //维护Redis
  127. CampaignService::instance()->delLatestCampaignRedis();
  128. // 维护对应的比赛场次数据
  129. $campaign_id = $this->model->id;
  130. CampaignService::instance()->maintainMacth($campaign_id, $params);
  131. //开启活动
  132. Redis::instance()->set(CampaignConstants::CAMPAIGN_IS_OPEN,1,3600*24);
  133. if ($result !== false) {
  134. $this->success();
  135. } else {
  136. $this->error($this->model->getError());
  137. }
  138. } catch (\think\exception\PDOException $e) {
  139. $this->error($e->getMessage());
  140. }
  141. }
  142. $this->error(__('Parameter %s can not be empty', ''));
  143. }
  144. $goods_list = CampaignService::instance()->getCampaignGoods();
  145. $this->assign('goods_list', $goods_list);
  146. $this->assign('position_one', CampaignConstants::CAMPAIGN_POPUP_POSITION_ONE);
  147. $this->assign('position_two', CampaignConstants::CAMPAIGN_POPUP_POSITION_TWO);
  148. $this->assign('position_thr', CampaignConstants::CAMPAIGN_POPUP_POSITION_THR);
  149. return $this->view->fetch();
  150. }
  151. /**
  152. * 编辑
  153. */
  154. public function edit($ids = NULL)
  155. {
  156. $row = $this->model->get($ids);
  157. if (!$row)
  158. $this->error(__('No Results were found'));
  159. $adminIds = $this->getDataLimitAdminIds();
  160. if (is_array($adminIds)) {
  161. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  162. $this->error(__('You have no permission'));
  163. }
  164. }
  165. $edit_disabled = (time() - $row->start_time) > 0 ? 1 : 0;
  166. if ($this->request->isPost()) {
  167. $params = $this->request->post("row/a");
  168. if ($params) {
  169. /*
  170. * 已经弃用,如果为了兼容老版可取消注释
  171. foreach ($params as $k => &$v)
  172. {
  173. $v = is_array($v) ? implode(',', $v) : $v;
  174. }
  175. */
  176. try {
  177. //是否采用模型验证
  178. if ($this->modelValidate) {
  179. $name = basename(str_replace('\\', '/', get_class($this->model)));
  180. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  181. $row->validate($validate);
  182. }
  183. if (!$edit_disabled) {
  184. // 校验场次金额不能相同
  185. if($params['elementary_need'] == $params['intermediate_need'] || $params['elementary_need'] == $params['advanced_need'] || $params['intermediate_need'] == $params['advanced_need']){
  186. $this->error('初级场次、中级场次、高级场次金额不能相同');
  187. }
  188. if(strtotime($params['end_time']) <= strtotime($params['start_time'])){
  189. $this->error('活动结束时间应该大于活动开始时间');
  190. }
  191. // 校验活动时间是否有交叉
  192. $pre_obj = $this->model -> where('id', 'lt', $ids) -> order('id', 'desc') ->find();
  193. if (!is_null($pre_obj)) {
  194. if(strtotime($params['start_time']) <= $pre_obj->end_time){
  195. $this->error('活动开始时间跟上一个活动时间有交叉');
  196. }
  197. }
  198. $next_obj = $this->model -> where('id', 'gt', $ids) -> order('id', 'desc') ->find();
  199. if (!is_null($next_obj)) {
  200. if(strtotime($params['end_time']) <= $next_obj->start_time){
  201. $this->error('活动结束时间跟下一个活动时间有交叉');
  202. }
  203. }
  204. if($params['popup_position']){
  205. $params['popup_position'] = implode(',', $params['popup_position']);
  206. }
  207. }
  208. $result = $row->allowField(true)->save($params);
  209. //维护Redis
  210. CampaignService::instance()->delLatestCampaignRedis();
  211. CampaignService::instance()->delReadRedisById($ids);
  212. if (!$edit_disabled) {
  213. //删除Match表中老数据,同时插入新数据
  214. $campaign_id = $row->id;
  215. CampaignService::instance()->delMatch($campaign_id);
  216. CampaignService::instance()->maintainMacth($campaign_id, $params);
  217. }
  218. if ($result !== false) {
  219. $this->success();
  220. } else {
  221. $this->error($row->getError());
  222. }
  223. } catch (\think\exception\PDOException $e) {
  224. $this->error($e->getMessage());
  225. }
  226. }
  227. $this->error(__('Parameter %s can not be empty', ''));
  228. }
  229. $goods_list = CampaignService::instance()->getCampaignGoods();
  230. $this->assign('edit_disabled', $edit_disabled);
  231. $this->assign('goods_list', $goods_list);
  232. $this->assign('position_one', CampaignConstants::CAMPAIGN_POPUP_POSITION_ONE);
  233. $this->assign('position_two', CampaignConstants::CAMPAIGN_POPUP_POSITION_TWO);
  234. $this->assign('position_thr', CampaignConstants::CAMPAIGN_POPUP_POSITION_THR);
  235. $this->view->assign("row", $row);
  236. return $this->view->fetch();
  237. }
  238. /**
  239. * 删除
  240. */
  241. public function del($ids = "")
  242. {
  243. if ($ids) {
  244. $pk = $this->model->getPk();
  245. $adminIds = $this->getDataLimitAdminIds();
  246. if (is_array($adminIds)) {
  247. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  248. }
  249. $list = $this->model->where($pk, 'in', $ids)->select();
  250. $count = 0;
  251. foreach ($list as $k => $v) {
  252. $campaign_id = $v->id;
  253. $count += $v->delete();
  254. //维护Redis
  255. CampaignService::instance()->delLatestCampaignRedis();
  256. CampaignService::instance()->delReadRedisById($campaign_id);
  257. // 删除Match表中对应的数据
  258. CampaignService::instance()->delMatch($campaign_id);
  259. }
  260. if ($count) {
  261. $this->success();
  262. } else {
  263. $this->error(__('No rows were deleted'));
  264. }
  265. }
  266. $this->error(__('Parameter %s can not be empty', 'ids'));
  267. }
  268. /**
  269. * 详情页面
  270. * @param $ids
  271. * @return mixed
  272. */
  273. public function detail($ids)
  274. {
  275. $data = $this->model->get(['id', $ids]);
  276. $this->assign('row', $data);
  277. return $this->fetch();
  278. }
  279. /**
  280. * 将活动设置为失效|有效
  281. */
  282. public function ajaxLapse()
  283. {
  284. $campaign_id = $this->request->param('campaign_id');
  285. $campaign_status = $this->request->param('campaign_status');
  286. if ($campaign_status == 'hidden') {
  287. $status = 'normal';
  288. } else {
  289. $status = 'hidden';
  290. }
  291. $rst = $this->model->save(['status' => $status], ['id' => $campaign_id]);
  292. //维护Redis
  293. CampaignService::instance()->delLatestCampaignRedis();
  294. if ($rst) {
  295. $data = ['code' => 0, 'msg' => 'success'];
  296. } else {
  297. $data = ['code' => 1, 'msg' => 'fail'];
  298. }
  299. echo json_encode($data);
  300. }
  301. }