Game.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 排位赛
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Game extends Backend
  11. {
  12. /**
  13. * Game模型对象
  14. * @var \app\admin\model\Game
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\Game;
  21. $this->view->assign("statusList", $this->model->getStatusList());
  22. $this->view->assign("msgstatusList", $this->model->getMsgstatusList());
  23. $this->view->assign("showstatusList", $this->model->getShowstatusList());
  24. $this->view->assign("isAutoshowList", $this->model->getIsAutoshowList());
  25. $this->view->assign("isBannerList", $this->model->getIsBannerList());
  26. }
  27. /**
  28. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  29. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  30. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  31. */
  32. /**
  33. * 添加
  34. */
  35. public function add() {
  36. if ($this->request->isPost()) {
  37. $params = $this->request->post("row/a");
  38. if ($params) {
  39. $params = $this->preExcludeFields($params);
  40. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  41. $params[$this->dataLimitField] = $this->auth->id;
  42. }
  43. if (!preg_match('/^[0-9]+(.[0-9]{1,2})?$/', $params['price']) || $params['price'] <= 0) {
  44. $this->error('请输入正确价格');
  45. }
  46. if (strtotime($params['starttime']) < time()) {
  47. $this->error('活动开始时间不能小于当前时间');
  48. }
  49. if (strtotime($params['starttime']) >= strtotime($params['endtime'])) {
  50. $this->error('活动结束时间必须大于活动开始时间');
  51. }
  52. if ($params['num'] < 1) {
  53. $this->error('请输入正确每队最大人数');
  54. }
  55. if (strtotime($params['signupendtime']) < time()) {
  56. $this->error('报名截止时间不能小于当前时间');
  57. }
  58. if (strtotime($params['signupendtime']) >= strtotime($params['starttime'])) {
  59. $this->error('报名截止时间必须小于活动开始时间');
  60. }
  61. // if (strtotime($params['refundendtime']) < time()) {
  62. // $this->error('退款截止时间不能小于当前时间');
  63. // }
  64. // if (strtotime($params['refundendtime']) >= strtotime($params['signupendtime'])) {
  65. // $this->error('退款截止时间必须小于报名截止时间');
  66. // }
  67. $result = false;
  68. Db::startTrans();
  69. try {
  70. //是否采用模型验证
  71. if ($this->modelValidate) {
  72. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  73. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  74. $this->model->validateFailException(true)->validate($validate);
  75. }
  76. $result = $this->model->allowField(true)->save($params);
  77. $id = $this->model->id;
  78. Db::commit();
  79. } catch (ValidateException $e) {
  80. Db::rollback();
  81. $this->error($e->getMessage());
  82. } catch (PDOException $e) {
  83. Db::rollback();
  84. $this->error($e->getMessage());
  85. } catch (Exception $e) {
  86. Db::rollback();
  87. $this->error($e->getMessage());
  88. }
  89. if ($result !== false) {
  90. /*if ($params['is_banner'] == 1) {
  91. //在轮播图显示
  92. $banner_data['title'] = $params['title'];
  93. $banner_data['image'] = $params['image'];
  94. $banner_data['url'] = '/pages/home/detail?id='.$id;
  95. $banner_data['createtime'] = time();
  96. Db::name('banner')->insertGetId($banner_data);
  97. }*/
  98. $this->success();
  99. } else {
  100. $this->error(__('No rows were inserted'));
  101. }
  102. }
  103. $this->error(__('Parameter %s can not be empty', ''));
  104. }
  105. return $this->view->fetch();
  106. }
  107. /**
  108. * 编辑
  109. */
  110. public function edit($ids = null) {
  111. $row = $this->model->get($ids);
  112. if (!$row) {
  113. $this->error(__('No Results were found'));
  114. }
  115. $adminIds = $this->getDataLimitAdminIds();
  116. if (is_array($adminIds)) {
  117. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  118. $this->error(__('You have no permission'));
  119. }
  120. }
  121. if ($this->request->isPost()) {
  122. $params = $this->request->post("row/a");
  123. if ($params) {
  124. $params = $this->preExcludeFields($params);
  125. // p($row['status']);p($params);die;
  126. if ($row['status'] == 2 || $row['status'] == 3) {
  127. $this->error('活动已经结束或取消');
  128. }
  129. if ($params['showstatus'] == 0) { //下架
  130. $count = Db::name('game_people')->where(['game_id' => $ids, 'status' => 1])->count('id');
  131. if ($count) {
  132. $this->error('已经有人报名,不能下架');
  133. }
  134. }
  135. $result = false;
  136. Db::startTrans();
  137. /*//是否在轮播图显示
  138. if ($params['is_banner'] == 0) {
  139. //不显示
  140. Db::name('banner')->where(['title' => $row['title']])->delete();
  141. } elseif ($params['is_banner'] == 1) {
  142. //显示
  143. $banner_count = Db::name('banner')->where(['title' => $row['title']])->count('id');
  144. if (!$banner_count) {
  145. $banner_data['title'] = $row['title'];
  146. $banner_data['image'] = $row['image'];
  147. $banner_data['url'] = '/pages/home/detail?id=' . $ids;
  148. $banner_data['createtime'] = time();
  149. Db::name('banner')->insertGetId($banner_data);
  150. }
  151. }*/
  152. try {
  153. if (isset($params['status'])) {
  154. if ($params['status'] == 2) {
  155. //活动结束
  156. //修改活动人员状态
  157. $people_rs = Db::name('game_people')->where(['game_id' => $ids, 'status' => 1])->setField(['status' => 2, 'updatetime' => time()]);
  158. if ($people_rs === false) {
  159. Db::rollback();
  160. return ['code' => 0, 'msg' => '操作失败'];
  161. }
  162. } elseif ($params['status'] == 3) {
  163. //活动取消
  164. /*//查询报名订单
  165. $sys_msg = Db::name('sys_msg'); //消息通知
  166. $hu_game_people = Db::name('game_people'); //报名人员表
  167. $active_order_list = $hu_game_people->where(['game_id' => $ids, 'status' => 1])->select();
  168. if ($active_order_list) {
  169. //活动订单修改信息
  170. $order_data['status'] = 3; //状态:0=待付款,1=待出行,2=已完成,3=已取消
  171. $order_data['updatetime'] = time();
  172. foreach ($active_order_list as &$v) {
  173. //退还支付金额
  174. if ($v['price'] > 0) {
  175. $order_data['refundstatus'] = 1; //退款状态:0=无需退款,1=退款成功,2=退款失败
  176. $order_data['refundprice'] = $v['price'];//退款金额
  177. $order_data['refundtime'] = time();
  178. if ($v['paytype'] == 0) {
  179. //余额支付, 退回余额
  180. $rs = create_log($v['price'], '活动取消返还', $v['user_id'], 3, $v['id']);
  181. if ($rs != 1) {
  182. $order_data['refundstatus'] = 2;
  183. }
  184. } elseif ($v['paytype'] == 1) {
  185. //微信支付, 退回微信
  186. //扣除用户成长值
  187. $paygrowth = (int)config('site.paygrowth'); //每消费1元赠送成长值数量, 1元=?成长值
  188. $balance = floor($paygrowth * $v['price']);
  189. if ($balance > 0) {
  190. $paygrowth_rs = create_growth_log(-$balance, '活动取消扣除', $v['user_id'], 4, $v['id']);
  191. }
  192. //退款单号
  193. $order_data['refund_no'] = $v['order_sn'];
  194. //调用微信退款
  195. $wxData['transaction_id'] = $v['transaction_id'];
  196. $wxData['out_refund_no'] = $order_data['refund_no'];
  197. $wxData['total_fee'] = (int)($v['price'] * 100);//1 微信支付 单位为分
  198. $wxData['refund_fee'] = (int)($v['price'] * 100);//1 微信支付 单位为分
  199. $wxData['refund_desc'] = '活动取消返还';
  200. // require_once("Plugins/WxPay/WxPay.php");
  201. $wxPay = new wxpay\WxPay(config('wxchatpay'));
  202. $back = $wxPay->WxPayRefund($wxData);
  203. if($back['return_code'] != 'SUCCESS' || $back['result_code'] != 'SUCCESS') {
  204. $order_data['refundstatus'] = 2;
  205. }
  206. }
  207. }
  208. //修改活动订单状态
  209. $order_rs = $hu_game_people->where(['id' => $v['id'], 'status' => 1])->setField($order_data);
  210. if ($order_rs === false) {
  211. Db::rollback();
  212. return ['code' => 0, 'msg' => '操作失败'];
  213. }
  214. //发送消息
  215. $data = [
  216. 'user_id' => $v['user_id'],
  217. 'type' => 1,
  218. 'title' => '活动通知',
  219. 'content' => '您报名的' . $row['title'] . '活动已取消',
  220. 'createtime' => time()
  221. ];
  222. $sys_msg->insertGetId($data);
  223. }
  224. }*/
  225. //修改活动人员状态
  226. $people_rs = Db::name('game_people')->where(['game_id' => $ids, 'status' => 1])->setField(['status' => 3, 'updatetime' => time()]);
  227. if ($people_rs === false) {
  228. Db::rollback();
  229. return ['code' => 0, 'msg' => '操作失败'];
  230. }
  231. }
  232. }
  233. //是否采用模型验证
  234. if ($this->modelValidate) {
  235. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  236. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  237. $row->validateFailException(true)->validate($validate);
  238. }
  239. $result = $row->allowField(true)->save($params);
  240. Db::commit();
  241. } catch (ValidateException $e) {
  242. Db::rollback();
  243. $this->error($e->getMessage());
  244. } catch (PDOException $e) {
  245. Db::rollback();
  246. $this->error($e->getMessage());
  247. } catch (Exception $e) {
  248. Db::rollback();
  249. $this->error($e->getMessage());
  250. }
  251. if ($result !== false) {
  252. $this->success();
  253. } else {
  254. $this->error(__('No rows were updated'));
  255. }
  256. }
  257. $this->error(__('Parameter %s can not be empty', ''));
  258. }
  259. $this->view->assign("row", $row);
  260. return $this->view->fetch();
  261. }
  262. /**
  263. * 删除
  264. */
  265. public function del($ids = "") {
  266. if (!$this->request->isPost()) {
  267. $this->error(__("Invalid parameters"));
  268. }
  269. $ids = $ids ? $ids : $this->request->post("ids");
  270. $count = Db::name('game_people')->where(['game_id' => $ids, 'status' => ['in', [1, 2]]])->count('id');
  271. if ($count) {
  272. $this->error('已经有人报名或未退款,不能删除');
  273. }
  274. Db::name('game')->delete($ids);
  275. Db::name('game_people')->where(['game_id' => $ids])->delete();
  276. $this->success();
  277. }
  278. }