Active.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use Think\Db;
  5. use wxpay;
  6. /**
  7. * 活动管理
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Active extends Backend
  12. {
  13. /**
  14. * Active模型对象
  15. * @var \app\admin\model\Active
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\Active;
  22. $this->view->assign("typeList", $this->model->getTypeList());
  23. $this->view->assign("isFreeList", $this->model->getIsFreeList());
  24. $this->view->assign("isOverlyingList", $this->model->getIsOverlyingList());
  25. $this->view->assign("statusList", $this->model->getStatusList());
  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 (strtotime($params['collectiontime']) < time()) {
  53. $this->error('集合时间不能小于当前时间');
  54. }
  55. if (strtotime($params['collectiontime']) >= strtotime($params['starttime'])) {
  56. $this->error('集合时间必须小于活动开始时间');
  57. }
  58. if ($params['maxperson'] < 1) {
  59. $this->error('请输入正确限报人数');
  60. }
  61. if (strtotime($params['signupendtime']) < time()) {
  62. $this->error('报名截止时间不能小于当前时间');
  63. }
  64. if (strtotime($params['signupendtime']) >= strtotime($params['starttime'])) {
  65. $this->error('报名截止时间必须小于活动开始时间');
  66. }
  67. if (strtotime($params['refundendtime']) < time()) {
  68. $this->error('退款截止时间不能小于当前时间');
  69. }
  70. if (strtotime($params['refundendtime']) >= strtotime($params['starttime'])) {
  71. $this->error('退款截止时间必须小于活动开始时间');
  72. }
  73. if ($params['girldiscount'] < 1 || $params['girldiscount'] > 100) {
  74. $this->error('女生活动折扣取值范围1-100');
  75. }
  76. if ($params['minage'] < 0) {
  77. $this->error('请输入正确最小年龄');
  78. }
  79. if ($params['minage'] >= $params['maxage']) {
  80. $this->error('最大年龄必须大于最小年龄');
  81. }
  82. $result = false;
  83. Db::startTrans();
  84. try {
  85. //是否采用模型验证
  86. if ($this->modelValidate) {
  87. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  88. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  89. $this->model->validateFailException(true)->validate($validate);
  90. }
  91. $result = $this->model->allowField(true)->save($params);
  92. Db::commit();
  93. } catch (ValidateException $e) {
  94. Db::rollback();
  95. $this->error($e->getMessage());
  96. } catch (PDOException $e) {
  97. Db::rollback();
  98. $this->error($e->getMessage());
  99. } catch (Exception $e) {
  100. Db::rollback();
  101. $this->error($e->getMessage());
  102. }
  103. if ($result !== false) {
  104. $this->success();
  105. } else {
  106. $this->error(__('No rows were inserted'));
  107. }
  108. }
  109. $this->error(__('Parameter %s can not be empty', ''));
  110. }
  111. return $this->view->fetch();
  112. }
  113. /**
  114. * 编辑
  115. */
  116. public function edit($ids = null) {
  117. $row = $this->model->get($ids);
  118. if (!$row) {
  119. $this->error(__('No Results were found'));
  120. }
  121. $adminIds = $this->getDataLimitAdminIds();
  122. if (is_array($adminIds)) {
  123. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  124. $this->error(__('You have no permission'));
  125. }
  126. }
  127. if ($this->request->isPost()) {
  128. $params = $this->request->post("row/a");
  129. if ($params) {
  130. $params = $this->preExcludeFields($params);
  131. // p($row['status']);p($params);die;
  132. if ($row['status'] == 2 || $row['status'] == 3) {
  133. $this->error('活动已经结束或取消');
  134. }
  135. $result = false;
  136. Db::startTrans();
  137. try {
  138. if (isset($params['status'])) {
  139. if ($params['status'] == 2) {
  140. //活动结束
  141. //修改活动订单状态
  142. $order_rs = Db::name('active_order')->where(['active_id' => $ids, 'status' => 1])->setField(['status' => 2, 'updatetime' => time()]);
  143. if ($order_rs === false) {
  144. Db::rollback();
  145. return ['code' => 0, 'msg' => '操作失败'];
  146. }
  147. //修改活动人员状态
  148. $people_rs = Db::name('active_people')->where(['active_id' => $ids, 'status' => 1])->setField(['status' => 2, 'modifystatus' => 0, 'updatetime' => time()]);
  149. if ($people_rs === false) {
  150. Db::rollback();
  151. return ['code' => 0, 'msg' => '操作失败'];
  152. }
  153. //修改活动人员信息修改表
  154. $people_modify_rs = Db::name('active_people_modify')->where(['active_id' => $ids, 'status' => 0])->setField(['status' => 2, 'updatetime' => time()]);
  155. if ($people_modify_rs === false) {
  156. Db::rollback();
  157. return ['code' => 0, 'msg' => '操作失败'];
  158. }
  159. //修改活动申请取消表
  160. $refund_rs = Db::name('active_refund')->where(['active_id' => $ids, 'status' => 0])->setField(['status' => 2, 'updatetime' => time()]);
  161. if ($refund_rs === false) {
  162. Db::rollback();
  163. return ['code' => 0, 'msg' => '操作失败'];
  164. }
  165. } elseif ($params['status'] == 3) {
  166. //活动取消
  167. //修改活动人员状态
  168. $people_rs = Db::name('active_people')->where(['active_id' => $ids, 'status' => 1])->setField(['status' => 3, 'modifystatus' => 0, 'updatetime' => time()]);
  169. if ($people_rs === false) {
  170. Db::rollback();
  171. return ['code' => 0, 'msg' => '操作失败'];
  172. }
  173. //修改活动人员信息修改表
  174. $people_modify_rs = Db::name('active_people_modify')->where(['active_id' => $ids, 'status' => 0])->setField(['status' => 2, 'updatetime' => time()]);
  175. if ($people_modify_rs === false) {
  176. Db::rollback();
  177. return ['code' => 0, 'msg' => '操作失败'];
  178. }
  179. //修改活动申请取消表
  180. $refund_rs = Db::name('active_refund')->where(['active_id' => $ids, 'status' => 0])->setField(['status' => 2, 'updatetime' => time()]);
  181. if ($refund_rs === false) {
  182. Db::rollback();
  183. return ['code' => 0, 'msg' => '操作失败'];
  184. }
  185. //查询活动订单
  186. $active_order = Db::name('active_order'); //活动订单表
  187. $user_coupon = Db::name('user_coupon'); //用户优惠券表
  188. $sys_msg = Db::name('sys_msg'); //消息通知
  189. $active_order_list = $active_order->where(['active_id' => $ids, 'status' => 1])->select();
  190. if ($active_order_list) {
  191. //活动订单修改信息
  192. $order_data['status'] = 3; //状态:0=待付款,1=待出行,2=已完成,3=已取消
  193. $order_data['updatetime'] = time();
  194. foreach ($active_order_list as &$v) {
  195. //退还支付金额
  196. if ($v['price'] > 0) {
  197. $order_data['refundstatus'] = 1; //退款状态:0=无需退款,1=退款成功,2=退款失败
  198. $order_data['refundprice'] = $v['price'];//退款金额
  199. $order_data['refundtime'] = time();
  200. if ($v['paytype'] == 0) {
  201. //余额支付, 退回余额
  202. $rs = create_log($v['price'], '活动取消返还', $v['user_id'], 3, $v['id']);
  203. if ($rs != 1) {
  204. $order_data['refundstatus'] = 2;
  205. }
  206. } elseif ($v['paytype'] == 1) {
  207. //微信支付, 退回微信
  208. //扣除用户成长值
  209. $paygrowth = (int)config('site.paygrowth'); //每消费1元赠送成长值数量, 1元=?成长值
  210. $balance = floor($paygrowth * $v['price']);
  211. if ($balance > 0) {
  212. $paygrowth_rs = create_growth_log(-$balance, '活动取消扣除', $v['user_id'], 4);
  213. }
  214. //退款单号
  215. $order_data['refund_no'] = $v['order_sn'];
  216. //调用微信退款
  217. $wxData['transaction_id'] = $v['transaction_id'];
  218. $wxData['out_refund_no'] = $order_data['refund_no'];
  219. $wxData['total_fee'] = (int)($v['price'] * 100);//1 微信支付 单位为分
  220. $wxData['refund_fee'] = (int)($v['price'] * 100);//1 微信支付 单位为分
  221. $wxData['refund_desc'] = '活动取消返还';
  222. // require_once("Plugins/WxPay/WxPay.php");
  223. $wxPay = new wxpay\WxPay(config('wxchatpay'));
  224. $back = $wxPay->WxPayRefund($wxData);
  225. if($back['return_code'] != 'SUCCESS' || $back['result_code'] != 'SUCCESS') {
  226. $order_data['refundstatus'] = 2;
  227. }
  228. }
  229. }
  230. //修改活动订单状态
  231. $order_rs = $active_order->where(['id' => $v['id'], 'status' => 1])->setField($order_data);
  232. if ($order_rs === false) {
  233. Db::rollback();
  234. return ['code' => 0, 'msg' => '操作失败'];
  235. }
  236. //上级优惠券设为过期
  237. $user_coupon->where(['active_id' => $ids, 'status' => 0])->setField('endtime', time() - 1);
  238. //发送消息
  239. $data = [
  240. 'user_id' => $v['user_id'],
  241. 'type' => 1,
  242. 'title' => '活动通知',
  243. 'content' => '您报名的' . $row['title'] . '活动已取消',
  244. 'createtime' => time()
  245. ];
  246. $sys_msg->insertGetId($data);
  247. }
  248. }
  249. }
  250. }
  251. //是否采用模型验证
  252. if ($this->modelValidate) {
  253. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  254. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  255. $row->validateFailException(true)->validate($validate);
  256. }
  257. $result = $row->allowField(true)->save($params);
  258. Db::commit();
  259. } catch (ValidateException $e) {
  260. Db::rollback();
  261. $this->error($e->getMessage());
  262. } catch (PDOException $e) {
  263. Db::rollback();
  264. $this->error($e->getMessage());
  265. } catch (Exception $e) {
  266. Db::rollback();
  267. $this->error($e->getMessage());
  268. }
  269. if ($result !== false) {
  270. $this->success();
  271. } else {
  272. $this->error(__('No rows were updated'));
  273. }
  274. }
  275. $this->error(__('Parameter %s can not be empty', ''));
  276. }
  277. $this->view->assign("row", $row);
  278. return $this->view->fetch();
  279. }
  280. /**
  281. * 删除
  282. */
  283. public function del($ids = "") {
  284. if (!$this->request->isPost()) {
  285. $this->error(__("Invalid parameters"));
  286. }
  287. $ids = $ids ? $ids : $this->request->post("ids");
  288. $count = Db::name('active_order')->where(['active_id' => $ids, 'status' => ['neq', 3]])->count('id');
  289. if ($count) {
  290. $this->error('已经有人报名,不能删除');
  291. }
  292. Db::name('active')->delete($ids);
  293. $this->success();
  294. }
  295. }