123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use Think\Db;
- use wxpay;
- /**
- * 活动管理
- *
- * @icon fa fa-circle-o
- */
- class Active extends Backend
- {
- /**
- * Active模型对象
- * @var \app\admin\model\Active
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\Active;
- $this->view->assign("typeList", $this->model->getTypeList());
- $this->view->assign("isFreeList", $this->model->getIsFreeList());
- $this->view->assign("isOverlyingList", $this->model->getIsOverlyingList());
- $this->view->assign("statusList", $this->model->getStatusList());
- $this->view->assign("showstatusList", $this->model->getShowstatusList());
- $this->view->assign("isAutoshowList", $this->model->getIsAutoshowList());
- $this->view->assign("isBannerList", $this->model->getIsBannerList());
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- /**
- * 添加
- */
- public function add() {
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- if ($params) {
- $params = $this->preExcludeFields($params);
- if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
- $params[$this->dataLimitField] = $this->auth->id;
- }
- if (!preg_match('/^[0-9]+(.[0-9]{1,2})?$/', $params['price']) || $params['price'] <= 0) {
- $this->error('请输入正确价格');
- }
- if (strtotime($params['starttime']) < time()) {
- $this->error('活动开始时间不能小于当前时间');
- }
- if (strtotime($params['starttime']) >= strtotime($params['endtime'])) {
- $this->error('活动结束时间必须大于活动开始时间');
- }
- if (strtotime($params['collectiontime']) < time()) {
- $this->error('集合时间不能小于当前时间');
- }
- if (strtotime($params['collectiontime']) >= strtotime($params['starttime'])) {
- $this->error('集合时间必须小于活动开始时间');
- }
- if ($params['maxperson'] < 1) {
- $this->error('请输入正确限报人数');
- }
- if ($params['minperson'] < 1) {
- $this->error('请输入正确最低成行人数');
- }
- if ($params['minperson'] > $params['maxperson']) {
- $this->error('最低成行人数不能大于限报人数');
- }
- if (strtotime($params['signupendtime']) < time()) {
- $this->error('报名截止时间不能小于当前时间');
- }
- if (strtotime($params['signupendtime']) >= strtotime($params['starttime'])) {
- $this->error('报名截止时间必须小于活动开始时间');
- }
- if (strtotime($params['refundendtime']) < time()) {
- $this->error('退款截止时间不能小于当前时间');
- }
- if (strtotime($params['refundendtime']) >= strtotime($params['signupendtime'])) {
- $this->error('退款截止时间必须小于报名截止时间');
- }
- if ($params['girldiscount'] < 1 || $params['girldiscount'] > 100) {
- $this->error('女生活动折扣取值范围1-100');
- }
- if ($params['minage'] < 0) {
- $this->error('请输入正确最小年龄');
- }
- if ($params['minage'] >= $params['maxage']) {
- $this->error('最大年龄必须大于最小年龄');
- }
- $result = false;
- Db::startTrans();
- try {
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
- $this->model->validateFailException(true)->validate($validate);
- }
- $result = $this->model->allowField(true)->save($params);
- $id = $this->model->id;
- Db::commit();
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- if ($params['is_banner'] == 1) {
- //在轮播图显示
- $banner_data['title'] = $params['title'];
- $banner_data['image'] = $params['image'];
- $banner_data['url'] = '/pages/home/detail?id='.$id;
- $banner_data['createtime'] = time();
- Db::name('banner')->insertGetId($banner_data);
- }
- $this->success();
- } else {
- $this->error(__('No rows were inserted'));
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- return $this->view->fetch();
- }
- /**
- * 编辑
- */
- public function edit($ids = null) {
- $row = $this->model->get($ids);
- if (!$row) {
- $this->error(__('No Results were found'));
- }
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds)) {
- if (!in_array($row[$this->dataLimitField], $adminIds)) {
- $this->error(__('You have no permission'));
- }
- }
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- if ($params) {
- $params = $this->preExcludeFields($params);
- // p($row['status']);p($params);die;
- if ($row['status'] == 2 || $row['status'] == 3) {
- $this->error('活动已经结束或取消');
- }
- if ($params['showstatus'] == 0) { //下架
- $count = Db::name('active_order')->where(['active_id' => $ids, 'status' => ['neq', 3]])->count('id');
- if ($count) {
- $this->error('已经有人报名,不能下架');
- }
- }
- $result = false;
- Db::startTrans();
- //是否在轮播图显示
- if ($params['is_banner'] == 0) {
- //不显示
- Db::name('banner')->where(['title' => $row['title']])->delete();
- } elseif ($params['is_banner'] == 1) {
- //显示
- $banner_count = Db::name('banner')->where(['title' => $row['title']])->count('id');
- if (!$banner_count) {
- $banner_data['title'] = $row['title'];
- $banner_data['image'] = $row['image'];
- $banner_data['url'] = '/pages/home/detail?id=' . $ids;
- $banner_data['createtime'] = time();
- Db::name('banner')->insertGetId($banner_data);
- }
- }
- try {
- if (isset($params['status'])) {
- if ($params['status'] == 2) {
- //活动结束
- //修改活动订单状态
- $order_rs = Db::name('active_order')->where(['active_id' => $ids, 'status' => 1])->setField(['status' => 2, 'updatetime' => time()]);
- if ($order_rs === false) {
- Db::rollback();
- return ['code' => 0, 'msg' => '操作失败'];
- }
- //修改活动人员状态
- $people_rs = Db::name('active_people')->where(['active_id' => $ids, 'status' => 1])->setField(['status' => 2, 'modifystatus' => 0, 'updatetime' => time()]);
- if ($people_rs === false) {
- Db::rollback();
- return ['code' => 0, 'msg' => '操作失败'];
- }
- //修改活动人员信息修改表
- $people_modify_rs = Db::name('active_people_modify')->where(['active_id' => $ids, 'status' => 0])->setField(['status' => 2, 'updatetime' => time()]);
- if ($people_modify_rs === false) {
- Db::rollback();
- return ['code' => 0, 'msg' => '操作失败'];
- }
- //修改活动申请取消表
- $refund_rs = Db::name('active_refund')->where(['active_id' => $ids, 'status' => 0])->setField(['status' => 2, 'updatetime' => time()]);
- if ($refund_rs === false) {
- Db::rollback();
- return ['code' => 0, 'msg' => '操作失败'];
- }
- } elseif ($params['status'] == 3) {
- //活动取消
- //修改活动人员状态
- $people_rs = Db::name('active_people')->where(['active_id' => $ids, 'status' => 1])->setField(['status' => 3, 'modifystatus' => 0, 'updatetime' => time()]);
- if ($people_rs === false) {
- Db::rollback();
- return ['code' => 0, 'msg' => '操作失败'];
- }
- //修改活动人员信息修改表
- $people_modify_rs = Db::name('active_people_modify')->where(['active_id' => $ids, 'status' => 0])->setField(['status' => 2, 'updatetime' => time()]);
- if ($people_modify_rs === false) {
- Db::rollback();
- return ['code' => 0, 'msg' => '操作失败'];
- }
- //修改活动申请取消表
- $refund_rs = Db::name('active_refund')->where(['active_id' => $ids, 'status' => 0])->setField(['status' => 2, 'updatetime' => time()]);
- if ($refund_rs === false) {
- Db::rollback();
- return ['code' => 0, 'msg' => '操作失败'];
- }
- //查询活动订单
- $active_order = Db::name('active_order'); //活动订单表
- $user_coupon = Db::name('user_coupon'); //用户优惠券表
- $sys_msg = Db::name('sys_msg'); //消息通知
- $active_order_list = $active_order->where(['active_id' => $ids, 'status' => 1])->select();
- if ($active_order_list) {
- //活动订单修改信息
- $order_data['status'] = 3; //状态:0=待付款,1=待出行,2=已完成,3=已取消
- $order_data['updatetime'] = time();
- foreach ($active_order_list as &$v) {
- //退还支付金额
- if ($v['price'] > 0) {
- $order_data['refundstatus'] = 1; //退款状态:0=无需退款,1=退款成功,2=退款失败
- $order_data['refundprice'] = $v['price'];//退款金额
- $order_data['refundtime'] = time();
- if ($v['paytype'] == 0) {
- //余额支付, 退回余额
- $rs = create_log($v['price'], '活动取消返还', $v['user_id'], 3, $v['id']);
- if ($rs != 1) {
- $order_data['refundstatus'] = 2;
- }
- } elseif ($v['paytype'] == 1) {
- //微信支付, 退回微信
- //扣除用户成长值
- $paygrowth = (int)config('site.paygrowth'); //每消费1元赠送成长值数量, 1元=?成长值
- $balance = floor($paygrowth * $v['price']);
- if ($balance > 0) {
- $paygrowth_rs = create_growth_log(-$balance, '活动取消扣除', $v['user_id'], 4, $v['id']);
- }
- //退款单号
- $order_data['refund_no'] = $v['order_sn'];
- //调用微信退款
- $wxData['transaction_id'] = $v['transaction_id'];
- $wxData['out_refund_no'] = $order_data['refund_no'];
- $wxData['total_fee'] = (int)($v['price'] * 100);//1 微信支付 单位为分
- $wxData['refund_fee'] = (int)($v['price'] * 100);//1 微信支付 单位为分
- $wxData['refund_desc'] = '活动取消返还';
- // require_once("Plugins/WxPay/WxPay.php");
- $wxPay = new wxpay\WxPay(config('wxchatpay'));
- $back = $wxPay->WxPayRefund($wxData);
- if($back['return_code'] != 'SUCCESS' || $back['result_code'] != 'SUCCESS') {
- $order_data['refundstatus'] = 2;
- }
- }
- }
- //修改活动订单状态
- $order_rs = $active_order->where(['id' => $v['id'], 'status' => 1])->setField($order_data);
- if ($order_rs === false) {
- Db::rollback();
- return ['code' => 0, 'msg' => '操作失败'];
- }
- //上级优惠券设为过期
- $user_coupon->where(['active_id' => $ids, 'status' => 0])->setField('endtime', time() - 1);
- //发送消息
- $data = [
- 'user_id' => $v['user_id'],
- 'type' => 1,
- 'title' => '活动通知',
- 'content' => '您报名的' . $row['title'] . '活动已取消',
- 'createtime' => time()
- ];
- $sys_msg->insertGetId($data);
- }
- }
- }
- }
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
- $row->validateFailException(true)->validate($validate);
- }
- $result = $row->allowField(true)->save($params);
- Db::commit();
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success();
- } else {
- $this->error(__('No rows were updated'));
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $this->view->assign("row", $row);
- return $this->view->fetch();
- }
- /**
- * 删除
- */
- public function del($ids = "") {
- if (!$this->request->isPost()) {
- $this->error(__("Invalid parameters"));
- }
- $ids = $ids ? $ids : $this->request->post("ids");
- $count = Db::name('active_order')->where(['active_id' => $ids, 'status' => ['neq', 3]])->count('id');
- if ($count) {
- $this->error('已经有人报名,不能删除');
- }
- Db::name('active')->delete($ids);
- Db::name('active_order')->where(['active_id' => $ids])->delete();
- Db::name('active_people')->where(['active_id' => $ids])->delete();
- Db::name('active_people_modify')->where(['active_id' => $ids])->delete();
- Db::name('active_refund')->where(['active_id' => $ids])->delete();
- $this->success();
- }
- }
|