Activity.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. namespace app\admin\controller\lottery;
  3. use app\common\controller\Backend;
  4. use app\common\Enum\LotteryEnum;
  5. use think\Db;
  6. use think\exception\ValidateException;
  7. use think\exception\PDOException;
  8. use Exception;
  9. /**
  10. * 抽奖活动管理
  11. *
  12. * @icon fa fa-gift
  13. */
  14. class Activity extends Backend
  15. {
  16. /**
  17. * Activity模型对象
  18. * @var \app\admin\model\lottery\Activity
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\lottery\Activity;
  25. // 获取枚举值列表
  26. $this->view->assign("typeList", LotteryEnum::getActivityTypeMap());
  27. $this->assignconfig("typeList", json_encode(LotteryEnum::getActivityTypeMap()));
  28. $this->view->assign("statusList", LotteryEnum::getActivityStatusMap());
  29. $this->assignconfig("statusList", json_encode(LotteryEnum::getActivityStatusMap()));
  30. $this->view->assign("lotteryTypeList", LotteryEnum::getLotteryTypeMap());
  31. $this->assignconfig("lotteryTypeList", json_encode(LotteryEnum::getLotteryTypeMap()));
  32. $this->view->assign("userLimitTypeList", LotteryEnum::getUserLimitTypeMap());
  33. $this->assignconfig("userLimitTypeList", json_encode(LotteryEnum::getUserLimitTypeMap()));
  34. $this->view->assign("guideStyleList", LotteryEnum::getGuideStyleMap());
  35. $this->assignconfig("guideStyleList", json_encode(LotteryEnum::getGuideStyleMap()));
  36. $this->view->assign("conditionTypeList", LotteryEnum::getConditionTypeMap());
  37. $this->assignconfig("conditionTypeList", json_encode(LotteryEnum::getConditionTypeMap()));
  38. // 奖品类型配置
  39. $this->view->assign("prizeTypeList", LotteryEnum::getPrizeTypeMap());
  40. $this->assignconfig("prizeTypeList", json_encode(LotteryEnum::getPrizeTypeMap()));
  41. // 奖品默认图片配置
  42. $this->assignconfig("prizeDefaultImages", json_encode(LotteryEnum::getPrizeDefaultImageMap()));
  43. $config = config('site');
  44. $lottery_guide = $config['lottery_guide'] ?? '';
  45. $prize_losing_lottery = $config['prize_losing_lottery'] ?? '';
  46. $this->view->assign("prize_losing_lottery", $prize_losing_lottery);
  47. $this->view->assign("lottery_guide", $lottery_guide);
  48. }
  49. /**
  50. * 查看
  51. */
  52. public function index()
  53. {
  54. //当前是否为关联查询
  55. $this->relationSearch = true;
  56. //设置过滤方法
  57. $this->request->filter(['strip_tags', 'trim']);
  58. if ($this->request->isAjax()) {
  59. //如果发送的来源是Selectpage,则转发到Selectpage
  60. if ($this->request->request('keyField')) {
  61. return $this->selectpage();
  62. }
  63. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  64. $list = $this->model
  65. ->with(['prize'])
  66. ->where($where)
  67. ->order($sort, $order)
  68. ->paginate($limit);
  69. foreach ($list as $row) {
  70. $row->visible(['id','name','description','cover_image','type','status','start_time','end_time','lottery_type','total_draw_count','total_people_count','total_win_count']);
  71. $row->visible(['prize']);
  72. $row->getRelation('prize')->visible(['name','type','total_stock','remain_stock']);
  73. }
  74. $result = array("total" => $list->total(), "rows" => $list->items());
  75. return json($result);
  76. }
  77. return $this->view->fetch();
  78. }
  79. /**
  80. * 添加
  81. */
  82. public function add()
  83. {
  84. if ($this->request->isPost()) {
  85. $params = $this->request->post("row/a");
  86. if ($params) {
  87. $params = $this->preExcludeFields($params);
  88. // 处理时间字段
  89. if (isset($params['start_time']) && $params['start_time']) {
  90. $params['start_time'] = strtotime($params['start_time']);
  91. }
  92. if (isset($params['end_time']) && $params['end_time']) {
  93. $params['end_time'] = strtotime($params['end_time']);
  94. }
  95. if (isset($params['lottery_time']) && $params['lottery_time']) {
  96. $params['lottery_time'] = strtotime($params['lottery_time']);
  97. }
  98. // 处理JSON字段
  99. if (isset($params['user_limit_value']) && $params['user_limit_value']) {
  100. $params['user_limit_value'] = json_encode($params['user_limit_value']);
  101. }
  102. $result = false;
  103. Db::startTrans();
  104. try {
  105. //是否采用模型验证
  106. if ($this->modelValidate) {
  107. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  108. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  109. $this->model->validateFailException(true)->validate($validate);
  110. }
  111. $result = $this->model->allowField(true)->save($params);
  112. Db::commit();
  113. } catch (ValidateException $e) {
  114. Db::rollback();
  115. $this->error($e->getMessage());
  116. } catch (PDOException $e) {
  117. Db::rollback();
  118. $this->error($e->getMessage());
  119. } catch (Exception $e) {
  120. Db::rollback();
  121. $this->error($e->getMessage());
  122. }
  123. if ($result !== false) {
  124. $this->success();
  125. } else {
  126. $this->error(__('No rows were inserted'));
  127. }
  128. }
  129. $this->error(__('Parameter %s can not be empty', ''));
  130. }
  131. return $this->view->fetch();
  132. }
  133. /**
  134. * 编辑
  135. */
  136. public function edit($ids = null)
  137. {
  138. $row = $this->model->get($ids);
  139. if (!$row) {
  140. $this->error(__('No Results were found'));
  141. }
  142. $adminIds = $this->getDataLimitAdminIds();
  143. if (is_array($adminIds)) {
  144. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  145. $this->error(__('You have no permission'));
  146. }
  147. }
  148. if ($this->request->isPost()) {
  149. $params = $this->request->post("row/a");
  150. if ($params) {
  151. $params = $this->preExcludeFields($params);
  152. // 处理时间字段
  153. if (isset($params['start_time']) && $params['start_time']) {
  154. $params['start_time'] = strtotime($params['start_time']);
  155. }
  156. if (isset($params['end_time']) && $params['end_time']) {
  157. $params['end_time'] = strtotime($params['end_time']);
  158. }
  159. if (isset($params['lottery_time']) && $params['lottery_time']) {
  160. $params['lottery_time'] = strtotime($params['lottery_time']);
  161. }
  162. // 处理JSON字段
  163. if (isset($params['user_limit_value']) && $params['user_limit_value']) {
  164. $params['user_limit_value'] = json_encode($params['user_limit_value']);
  165. }
  166. $result = false;
  167. Db::startTrans();
  168. try {
  169. //是否采用模型验证
  170. if ($this->modelValidate) {
  171. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  172. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  173. $row->validateFailException(true)->validate($validate);
  174. }
  175. $result = $row->allowField(true)->save($params);
  176. Db::commit();
  177. } catch (ValidateException $e) {
  178. Db::rollback();
  179. $this->error($e->getMessage());
  180. } catch (PDOException $e) {
  181. Db::rollback();
  182. $this->error($e->getMessage());
  183. } catch (Exception $e) {
  184. Db::rollback();
  185. $this->error($e->getMessage());
  186. }
  187. if ($result !== false) {
  188. $this->success();
  189. } else {
  190. $this->error(__('No rows were updated'));
  191. }
  192. }
  193. $this->error(__('Parameter %s can not be empty', ''));
  194. }
  195. // 处理时间显示
  196. if ($row->start_time) {
  197. $row->start_time = date('Y-m-d H:i:s', $row->start_time);
  198. }
  199. if ($row->end_time) {
  200. $row->end_time = date('Y-m-d H:i:s', $row->end_time);
  201. }
  202. if ($row->lottery_time) {
  203. $row->lottery_time = date('Y-m-d H:i:s', $row->lottery_time);
  204. }
  205. // 处理JSON字段
  206. if ($row->user_limit_value) {
  207. $row->user_limit_value = json_decode($row->user_limit_value, true);
  208. }
  209. $this->view->assign("row", $row);
  210. return $this->view->fetch();
  211. }
  212. /**
  213. * 奖品管理
  214. */
  215. public function prize($ids = null)
  216. {
  217. $activity = $this->model->get($ids);
  218. if (!$activity) {
  219. $this->error(__('No Results were found'));
  220. }
  221. $this->view->assign("activity", $activity);
  222. return $this->view->fetch();
  223. }
  224. /**
  225. * 参与条件管理
  226. */
  227. public function condition($ids = null)
  228. {
  229. $activity = $this->model->get($ids);
  230. if (!$activity) {
  231. $this->error(__('No Results were found'));
  232. }
  233. $this->view->assign("activity", $activity);
  234. return $this->view->fetch();
  235. }
  236. /**
  237. * 抽奖记录
  238. */
  239. public function records($ids = null)
  240. {
  241. $activity = $this->model->get($ids);
  242. if (!$activity) {
  243. $this->error(__('No Results were found'));
  244. }
  245. $this->view->assign("activity", $activity);
  246. return $this->view->fetch();
  247. }
  248. /**
  249. * 统计数据
  250. */
  251. public function statistics($ids = null)
  252. {
  253. $activity = $this->model->get($ids);
  254. if (!$activity) {
  255. $this->error(__('No Results were found'));
  256. }
  257. // 获取统计数据
  258. $statisticsModel = new \app\admin\model\lottery\Statistics;
  259. $statistics = $statisticsModel->where('activity_id', $ids)->order('stat_date', 'desc')->paginate(15);
  260. $this->view->assign("activity", $activity);
  261. $this->view->assign("statistics", $statistics);
  262. return $this->view->fetch();
  263. }
  264. }