Active.php 18 KB

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