123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use Think\Db;
- use wxpay;
- class Active extends Backend
- {
-
- 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());
- }
-
-
- 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 (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['starttime'])) {
- $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);
- 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 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);
- if ($row['status'] == 2 || $row['status'] == 3) {
- $this->error('活动已经结束或取消');
- }
- $result = false;
- Db::startTrans();
- 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;
- $order_data['updatetime'] = time();
- foreach ($active_order_list as &$v) {
-
- if ($v['price'] > 0) {
- $order_data['refundstatus'] = 1;
- $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');
- $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);
- $wxData['refund_fee'] = (int)($v['price'] * 100);
- $wxData['refund_desc'] = '活动取消返还';
- $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);
- $this->success();
- }
- }
|