123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- <?php
- namespace app\admin\controller\campaign;
- use app\common\controller\Backend;
- use app\common\library\Redis;
- use app\common\service\CampaignService;
- use app\main\constants\CampaignConstants;
- use app\main\service\UserService;
- use think\Controller;
- use think\Request;
- /**
- * 消费活动-读书挑战赛
- *
- * @icon fa fa-circle-o
- */
- class Read extends Backend
- {
- /**
- * CampaignRead模型对象
- */
- protected $model = null;
- protected $noNeedRight = ['detail', 'ajaxlapse', 'ajaxgetgoods'];
- public function _initialize()
- {
- parent::_initialize();
- $this->model = model('CampaignRead');
- $this->view->assign("statusList", $this->model->getStatusList());
- $this->view->assign('levelList', $this->model->getDefaultLevel());
- $this->view->assign('popupPosition', $this->model->getPopupPosition());
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
- * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- /**
- * 查看
- */
- public function index()
- {
- //设置过滤方法
- $this->request->filter(['strip_tags']);
- if ($this->request->isAjax())
- {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('pkey_name'))
- {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $total = $this->model
- ->where($where)
- ->order($sort, $order)
- ->count();
- $list = $this->model
- ->where($where)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 添加
- */
- public function add()
- {
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- if ($params) {
- /*
- * 已经弃用,如果为了兼容老版可取消注释
- foreach ($params as $k => &$v)
- {
- $v = is_array($v) ? implode(',', $v) : $v;
- }
- */
- if ($this->dataLimit) {
- $params[$this->dataLimitField] = $this->auth->id;
- }
- try {
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = basename(str_replace('\\', '/', get_class($this->model)));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
- $this->model->validate($validate);
- }
- // 校验场次金额不能相同
- if($params['elementary_need'] == $params['intermediate_need'] || $params['elementary_need'] == $params['advanced_need'] || $params['intermediate_need'] == $params['advanced_need']){
- $this->error('初级场次、中级场次、高级场次金额不能相同');
- }
- if(strtotime($params['end_time']) <= strtotime($params['start_time'])){
- $this->error('活动结束时间应该大于活动开始时间');
- }
- $tmp_start_time = strtotime(date('Y-m-d 00:00:00', strtotime($params['start_time'])));
- $tmp_end_time = strtotime(date('Y-m-d 23:59:59', strtotime($params['end_time'])));
- // 校验活动时间是否有交叉
- $g_obj = $this->model->where(function ($query) use ($params, $tmp_start_time) {
- $query->where('start_time', '>=', $tmp_start_time)
- ->where('end_time', '>=', $tmp_start_time);
- })->find();
- if ($g_obj) {
- $this->error('活动开始时间跟历史活动时间有交叉');
- }
- // 校验活动时间是否有交叉
- $f_obj = $this->model->where(function ($query) use ($params, $tmp_start_time) {
- $query->where('start_time', '<=', $tmp_start_time)
- ->where('end_time', '>=', $tmp_start_time);
- })->find();
- if ($f_obj) {
- $this->error('活动开始时间跟历史活动时间有交叉');
- }
- // 校验活动时间是否有交叉
- $e_obj = $this->model->where(function ($query) use ($params, $tmp_end_time) {
- $query->where('start_time', '<=', $tmp_end_time)
- ->where('end_time', '>=', $tmp_end_time);
- })->find();
- if ($e_obj) {
- $this->error('活动结束时间跟历史活动时间有交叉');
- }
- if($params['popup_position']){
- $params['popup_position'] = implode(',', $params['popup_position']);
- }
- $result = $this->model->allowField(true)->save($params);
- //维护Redis
- CampaignService::instance()->delLatestCampaignRedis();
- // 维护对应的比赛场次数据
- $campaign_id = $this->model->id;
- CampaignService::instance()->maintainMacth($campaign_id, $params);
- //开启活动
- Redis::instance()->set(CampaignConstants::CAMPAIGN_IS_OPEN,1,3600*24);
- if ($result !== false) {
- $this->success();
- } else {
- $this->error($this->model->getError());
- }
- } catch (\think\exception\PDOException $e) {
- $this->error($e->getMessage());
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $goods_list = CampaignService::instance()->getCampaignGoods();
- $this->assign('goods_list', $goods_list);
- $this->assign('position_one', CampaignConstants::CAMPAIGN_POPUP_POSITION_ONE);
- $this->assign('position_two', CampaignConstants::CAMPAIGN_POPUP_POSITION_TWO);
- $this->assign('position_thr', CampaignConstants::CAMPAIGN_POPUP_POSITION_THR);
- return $this->view->fetch();
- }
- /**
- * 编辑
- */
- public function edit($ids = NULL)
- {
- $row = $this->model->get($ids);
- if (!$row)
- $this->error(__('No Results were found'));
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds)) {
- if (!in_array($row[$this->dataLimitField], $adminIds)) {
- $this->error(__('You have no permission'));
- }
- }
- $edit_disabled = (time() - $row->start_time) > 0 ? 1 : 0;
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- if ($params) {
- /*
- * 已经弃用,如果为了兼容老版可取消注释
- foreach ($params as $k => &$v)
- {
- $v = is_array($v) ? implode(',', $v) : $v;
- }
- */
- try {
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = basename(str_replace('\\', '/', get_class($this->model)));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
- $row->validate($validate);
- }
- if (!$edit_disabled) {
- // 校验场次金额不能相同
- if($params['elementary_need'] == $params['intermediate_need'] || $params['elementary_need'] == $params['advanced_need'] || $params['intermediate_need'] == $params['advanced_need']){
- $this->error('初级场次、中级场次、高级场次金额不能相同');
- }
- if(strtotime($params['end_time']) <= strtotime($params['start_time'])){
- $this->error('活动结束时间应该大于活动开始时间');
- }
- // 校验活动时间是否有交叉
- $pre_obj = $this->model -> where('id', 'lt', $ids) -> order('id', 'desc') ->find();
- if (!is_null($pre_obj)) {
- if(strtotime($params['start_time']) <= $pre_obj->end_time){
- $this->error('活动开始时间跟上一个活动时间有交叉');
- }
- }
- $next_obj = $this->model -> where('id', 'gt', $ids) -> order('id', 'desc') ->find();
- if (!is_null($next_obj)) {
- if(strtotime($params['end_time']) <= $next_obj->start_time){
- $this->error('活动结束时间跟下一个活动时间有交叉');
- }
- }
- if($params['popup_position']){
- $params['popup_position'] = implode(',', $params['popup_position']);
- }
- }
- $result = $row->allowField(true)->save($params);
- //维护Redis
- CampaignService::instance()->delLatestCampaignRedis();
- CampaignService::instance()->delReadRedisById($ids);
- if (!$edit_disabled) {
- //删除Match表中老数据,同时插入新数据
- $campaign_id = $row->id;
- CampaignService::instance()->delMatch($campaign_id);
- CampaignService::instance()->maintainMacth($campaign_id, $params);
- }
- if ($result !== false) {
- $this->success();
- } else {
- $this->error($row->getError());
- }
- } catch (\think\exception\PDOException $e) {
- $this->error($e->getMessage());
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $goods_list = CampaignService::instance()->getCampaignGoods();
- $this->assign('edit_disabled', $edit_disabled);
- $this->assign('goods_list', $goods_list);
- $this->assign('position_one', CampaignConstants::CAMPAIGN_POPUP_POSITION_ONE);
- $this->assign('position_two', CampaignConstants::CAMPAIGN_POPUP_POSITION_TWO);
- $this->assign('position_thr', CampaignConstants::CAMPAIGN_POPUP_POSITION_THR);
- $this->view->assign("row", $row);
- return $this->view->fetch();
- }
- /**
- * 删除
- */
- public function del($ids = "")
- {
- if ($ids) {
- $pk = $this->model->getPk();
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds)) {
- $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
- }
- $list = $this->model->where($pk, 'in', $ids)->select();
- $count = 0;
- foreach ($list as $k => $v) {
- $campaign_id = $v->id;
- $count += $v->delete();
- //维护Redis
- CampaignService::instance()->delLatestCampaignRedis();
- CampaignService::instance()->delReadRedisById($campaign_id);
- // 删除Match表中对应的数据
- CampaignService::instance()->delMatch($campaign_id);
- }
- if ($count) {
- $this->success();
- } else {
- $this->error(__('No rows were deleted'));
- }
- }
- $this->error(__('Parameter %s can not be empty', 'ids'));
- }
- /**
- * 详情页面
- * @param $ids
- * @return mixed
- */
- public function detail($ids)
- {
- $data = $this->model->get(['id', $ids]);
- $this->assign('row', $data);
- return $this->fetch();
- }
- /**
- * 将活动设置为失效|有效
- */
- public function ajaxLapse()
- {
- $campaign_id = $this->request->param('campaign_id');
- $campaign_status = $this->request->param('campaign_status');
- if ($campaign_status == 'hidden') {
- $status = 'normal';
- } else {
- $status = 'hidden';
- }
- $rst = $this->model->save(['status' => $status], ['id' => $campaign_id]);
- //维护Redis
- CampaignService::instance()->delLatestCampaignRedis();
- if ($rst) {
- $data = ['code' => 0, 'msg' => 'success'];
- } else {
- $data = ['code' => 1, 'msg' => 'fail'];
- }
- echo json_encode($data);
- }
- }
|