123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- <?php
- namespace app\admin\controller\lottery;
- use app\common\controller\Backend;
- use app\common\Enum\LotteryEnum;
- use think\Db;
- use think\exception\ValidateException;
- use think\exception\PDOException;
- use Exception;
- /**
- * 抽奖活动管理
- *
- * @icon fa fa-gift
- */
- class Activity extends Backend
- {
- /**
- * Activity模型对象
- * @var \app\admin\model\lottery\Activity
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\lottery\Activity;
-
- // 获取枚举值列表
- $this->view->assign("typeList", LotteryEnum::getActivityTypeMap());
- $this->assignconfig("typeList", json_encode(LotteryEnum::getActivityTypeMap()));
- $this->view->assign("statusList", LotteryEnum::getActivityStatusMap());
- $this->assignconfig("statusList", json_encode(LotteryEnum::getActivityStatusMap()));
- $this->view->assign("lotteryTypeList", LotteryEnum::getLotteryTypeMap());
- $this->assignconfig("lotteryTypeList", json_encode(LotteryEnum::getLotteryTypeMap()));
- $this->view->assign("userLimitTypeList", LotteryEnum::getUserLimitTypeMap());
- $this->assignconfig("userLimitTypeList", json_encode(LotteryEnum::getUserLimitTypeMap()));
- $this->view->assign("guideStyleList", LotteryEnum::getGuideStyleMap());
- $this->assignconfig("guideStyleList", json_encode(LotteryEnum::getGuideStyleMap()));
- $this->view->assign("conditionTypeList", LotteryEnum::getConditionTypeMap());
- $this->assignconfig("conditionTypeList", json_encode(LotteryEnum::getConditionTypeMap()));
-
- // 奖品类型配置
- $this->view->assign("prizeTypeList", LotteryEnum::getPrizeTypeMap());
- $this->assignconfig("prizeTypeList", json_encode(LotteryEnum::getPrizeTypeMap()));
-
- // 奖品默认图片配置
- $this->assignconfig("prizeDefaultImages", json_encode(LotteryEnum::getPrizeDefaultImageMap()));
- $config = config('site');
- $lottery_guide = $config['lottery_guide'] ?? '';
- $prize_losing_lottery = $config['prize_losing_lottery'] ?? '';
- $this->view->assign("prize_losing_lottery", $prize_losing_lottery);
- $this->view->assign("lottery_guide", $lottery_guide);
-
- }
- /**
- * 查看
- */
- public function index()
- {
- //当前是否为关联查询
- $this->relationSearch = true;
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
-
- $list = $this->model
- ->with(['prize'])
- ->where($where)
- ->order($sort, $order)
- ->paginate($limit);
- foreach ($list as $row) {
- $row->visible(['id','name','description','cover_image','type','status','start_time','end_time','lottery_type','total_draw_count','total_people_count','total_win_count']);
- $row->visible(['prize']);
- $row->getRelation('prize')->visible(['name','type','total_stock','remain_stock']);
- }
-
- $result = array("total" => $list->total(), "rows" => $list->items());
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 添加
- */
- public function add()
- {
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- if ($params) {
- $params = $this->preExcludeFields($params);
-
- // 处理时间字段
- if (isset($params['start_time']) && $params['start_time']) {
- $params['start_time'] = strtotime($params['start_time']);
- }
- if (isset($params['end_time']) && $params['end_time']) {
- $params['end_time'] = strtotime($params['end_time']);
- }
- if (isset($params['lottery_time']) && $params['lottery_time']) {
- $params['lottery_time'] = strtotime($params['lottery_time']);
- }
-
- // 处理JSON字段
- if (isset($params['user_limit_value']) && $params['user_limit_value']) {
- $params['user_limit_value'] = json_encode($params['user_limit_value']);
- }
- $result = false;
- Db::startTrans();
- try {
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
- $this->model->validateFailException(true)->validate($validate);
- }
- $result = $this->model->allowField(true)->save($params);
- Db::commit();
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success();
- } else {
- $this->error(__('No rows were inserted'));
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- 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'));
- }
- }
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- if ($params) {
- $params = $this->preExcludeFields($params);
-
- // 处理时间字段
- if (isset($params['start_time']) && $params['start_time']) {
- $params['start_time'] = strtotime($params['start_time']);
- }
- if (isset($params['end_time']) && $params['end_time']) {
- $params['end_time'] = strtotime($params['end_time']);
- }
- if (isset($params['lottery_time']) && $params['lottery_time']) {
- $params['lottery_time'] = strtotime($params['lottery_time']);
- }
-
- // 处理JSON字段
- if (isset($params['user_limit_value']) && $params['user_limit_value']) {
- $params['user_limit_value'] = json_encode($params['user_limit_value']);
- }
- $result = false;
- Db::startTrans();
- try {
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
- $row->validateFailException(true)->validate($validate);
- }
- $result = $row->allowField(true)->save($params);
- Db::commit();
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success();
- } else {
- $this->error(__('No rows were updated'));
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
-
- // 处理时间显示
- if ($row->start_time) {
- $row->start_time = date('Y-m-d H:i:s', $row->start_time);
- }
- if ($row->end_time) {
- $row->end_time = date('Y-m-d H:i:s', $row->end_time);
- }
- if ($row->lottery_time) {
- $row->lottery_time = date('Y-m-d H:i:s', $row->lottery_time);
- }
-
- // 处理JSON字段
- if ($row->user_limit_value) {
- $row->user_limit_value = json_decode($row->user_limit_value, true);
- }
-
- $this->view->assign("row", $row);
- return $this->view->fetch();
- }
- /**
- * 奖品管理
- */
- public function prize($ids = null)
- {
- $activity = $this->model->get($ids);
- if (!$activity) {
- $this->error(__('No Results were found'));
- }
-
- $this->view->assign("activity", $activity);
- return $this->view->fetch();
- }
- /**
- * 参与条件管理
- */
- public function condition($ids = null)
- {
- $activity = $this->model->get($ids);
- if (!$activity) {
- $this->error(__('No Results were found'));
- }
-
- $this->view->assign("activity", $activity);
- return $this->view->fetch();
- }
- /**
- * 抽奖记录
- */
- public function records($ids = null)
- {
- $activity = $this->model->get($ids);
- if (!$activity) {
- $this->error(__('No Results were found'));
- }
-
- $this->view->assign("activity", $activity);
- return $this->view->fetch();
- }
- /**
- * 统计数据
- */
- public function statistics($ids = null)
- {
- $activity = $this->model->get($ids);
- if (!$activity) {
- $this->error(__('No Results were found'));
- }
-
- // 获取统计数据
- $statisticsModel = new \app\admin\model\lottery\Statistics;
- $statistics = $statisticsModel->where('activity_id', $ids)->order('stat_date', 'desc')->paginate(15);
-
- $this->view->assign("activity", $activity);
- $this->view->assign("statistics", $statistics);
- return $this->view->fetch();
- }
- }
|