Active.php 16 KB

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