| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- <?php
- namespace app\admin\controller;
- use app\common\model\ComplexMembers as MembersModel;
- use app\common\model\ComplexChannelModel;
- use app\common\model\ComplexPay;
- use GuzzleHttp\Exception\RequestException;
- use think\Db;
- use app\common\library\MakeReport;
- use GuzzleHttp\Client;
- class Coupon extends Admin
- {
- public function _initialize()
- {
- parent::_initialize(); // TODO: Change the autogenerated stub
- }
- public function index()
- {
- $where = [];
- //查询参数
- $ganemid = $this->request->get('game_id', '', 'trim');
- $name = $this->request->get('name', '', 'trim');
- if (isset($name) && !empty($name)) {
- $where['name'] = ['like', "%$name%"];
- }
- $order = 'id desc';
- $tmpGameList = model('Common/Game')->getAllByCondition('id,name');
- $gameList = array();
- foreach ($tmpGameList as $game) {
- $gameList[$game['id']] = $game;
- }
- $couponModel = model('Coupon');
- $list = $couponModel
- ->where($where)
- ->where(function ($query) use ($ganemid) {
- if (isset($ganemid) && !empty($ganemid)) {
- $query->whereRaw(sprintf('FIND_IN_SET(%s,game_id)', $ganemid));
- }
- })
- ->order($order)
- ->paginate(10, false, ['query' => input('get.')])->each(function ($item, $key) use ($gameList) {
- if ($item['game_id']) {
- $game_ids = explode(',', $item['game_id']);
- $tmp = [];
- foreach ($game_ids as $k => $v) {
- if (isset($gameList[$v])) {
- $tmp[] = $gameList[$v]['name'];
- }
- }
- $item['game_name'] = implode(', ', $tmp);
- } else {
- $item['game_name'] = '全部';
- }
- return $item;
- });
- $this->assign('game_list', $tmpGameList);
- $this->assign('list', $list);
- $this->assign('page', $list->render());
- return $this->fetch();
- }
- /**
- *增
- */
- public function add()
- {
- if ($this->request->isPost()) {
- $data = $this->request->post();
- $checkResult = $this->validate($data, 'Coupon.add');
- if (true !== $checkResult) {
- $this->error($checkResult);
- }
- if ($data['select']) {
- $data['game_id'] = $data['select'];
- }
- unset($data['select']);
- $data['update_time'] = $data['create_time'] = time();
- if ($data['type_id'] == 1) {
- $data['start_time'] = strtotime($data['start_time']);
- $data['end_time'] = strtotime($data['end_time']);
- $data['day'] = 0;
- } else {
- $data['start_time'] = 0;
- $data['end_time'] = 0;
- }
- if (model('Coupon')->insertGetId($data)) {
- $this->success('添加成功', url('index'));
- }
- $this->error('添加失败');
- }
- $games = model('Common/Game')->getAllByCondition('id,name', ['cooperation_status' => ['neq', 3]], 'id desc', 'self');
- $this->assign('games', $games);
- return $this->fetch();
- }
- public function edit()
- {
- $id = $this->request->param('id', '', 'intval');
- $appModel = model('Coupon')->where(['id' => $id]);
- if (!$data = $appModel->find($id)) {
- $this->error('参数错误,不存在该数据');
- }
- if (empty($id)) {
- $this->error('参数错误!');
- }
- if ($this->request->isPost()) {
- $data = $this->request->post();
- $checkResult = $this->validate($data, 'Coupon.edit');
- if (true !== $checkResult) {
- $this->error($checkResult);
- }
- $data['update_time'] = $data['create_time'] = time();
- if ($data['type_id'] == 1) {
- $data['start_time'] = strtotime($data['start_time']);
- $data['end_time'] = strtotime($data['end_time']);
- $data['day'] = 0;
- } else {
- $data['start_time'] = 0;
- $data['end_time'] = 0;
- }
- $data['update_time'] = time();
- if (model('Coupon')->where(['id' => $id])->update($data)) {
- $this->success('修改成功', url('index'));
- }
- $this->error($appModel->getError() ?: '修改失败');
- }
- if ($data['type_id'] == 1) {
- $data['start_time'] = strtotime($data['start_time']);
- $data['end_time'] = strtotime($data['end_time']);
- $data['day'] = '';
- } else {
- $data['start_time'] = '';
- $data['end_time'] = '';
- }
- if ($data['game_id']) {
- $game = explode(',', $data['game_id']);
- $tmpGameList = model('Common/Game')->getAllByCondition('name', ['id' => ['in', $game]]);
- $this->assign('game_name', implode(',', array_column($tmpGameList, 'name')));
- } else {
- $this->assign('game_name', '全部');
- }
- $this->assign('data', $data);
- return $this->fetch();
- }
- /**
- *删
- */
- public function delete()
- {
- $id = $this->request->param('id', '', 'intval');
- if (empty($id)) {
- $this->error('参数错误!');
- }
- $appModel = model('Common/Coupon');
- if (!$info = $appModel->find($id)) {
- $this->error('参数错误,不存在该数据');
- }
- if ($info['grant_num'] > 0) {
- $this->error('已存在发放记录 不能删除');
- }
- if ($appModel->where('id', '=', $id)->delete()) {
- $this->success('删除成功', url('index'));
- }
- $error = $appModel->getError();
- $this->error($error ?: '删除失败');
- }
- public function addCoupon()
- {
- if (!$id = input('id')) {
- $this->error('记录不存在');
- }
- $coupon = model('Common/Coupon')->where('id', $id)->whereRaw(sprintf('(type_id=1 and end_time>=%s) or (type_id=2)',Db::quote(date('Y-m-d'))))->find();
- if (!$coupon) {
- $this->error('记录不存在');
- }
- $this->assign('id', $id);
- return $this->fetch();
- }
- public function addCouponSave()
- {
- if (!$id = input('id')) {
- $this->error('记录不存在');
- }
- $coupon = model('Common/Coupon')->where('id', $id)->whereRaw(sprintf('(type_id=1 and end_time>=%s) or (type_id=2)',Db::quote(date('Y-m-d'))))->find();
- if (!$coupon) {
- $this->error('记录不存在');
- }
- if (!$number = (int)input('number')) {
- $this->error('数量不存在');
- }
- if ($number <= 0) {
- $this->error('数量错误');
- }
- for ($i = 0; $i < $number; $i++) {
- $data[] = [
- 'coupon_id' => $coupon['id'],
- 'examine' => 2,
- 'code' => makeUniqueid('Q'),
- ];
- }
- model('Common/Coupon')->startTrans();
- if(!model('Common/Coupon')->where('id', $id)->update(['grant_num' => $coupon['grant_num']+$number])){
- model('Common/Coupon')->rollback();
- $this->jsonResult('', 0, '生成失败');
- }
- if (model('couponMember')->insertAll($data)) {
- model('Common/Coupon')->commit();
- $this->jsonResult('', 1, '生成成功');
- } else {
- model('Common/Coupon')->rollback();
- $this->jsonResult('', 0, '生成失败');
- }
- }
- public function coupon(){
- $where = [];
- //查询参数
- $username = input('username');
- $name = input('name');
- $id = input('id');
- $is_use = input('is_use');
- $state = input('state');
- if (isset($id) && !empty($id)) {
- $where['a.id'] = $id;
- }
- if (isset($name) && !empty($name)) {
- $where['a.name'] = ['like', "$name%"];
- }
- if (isset($username) && !empty($username)) {
- $where['b.username'] = $username;
- }
- if (isset($is_use) && !empty($is_use)) {
- $where['c.is_use'] = $is_use;
- }
- if (isset($state) && !empty($state)) {
- $where['c.state'] = $state;
- }
- $couponModel = model('Coupon');
- $list = $couponModel
- ->alias('a')
- ->join('cy_coupon_member c', 'a.id=c.coupon_id','left')
- ->join('cy_members b', 'c.member_id=b.id','left')
- ->where($where)
- ->field('c.*,b.username,a.name,a.money,a.min_money,a.start_time as coupon_start_time,a.end_time as coupon_end_time,a.type_id')
- ->order('id desc')
- ->paginate(10, false, ['query' => input()])->each(function ($item, $key) {
- if($item['use_time']) {
- $item['use_time'] = date('Y-m-d H:i:s',$item['use_time']);
- }
- if($item['use_time'] == 0) {
- $item['use_time'] = "";
- }
- $dayTime = strtotime(date('Y-m-d'));
- // 类型:1有效时间, 2领取后多少天内有效
- if ($item['type_id'] == 2) {
- if ($item['member_id'] > 0 && $item['end_time'] < $dayTime) {
- $item['state_str'] = '<span style="color: red">(已过期)</span>';
- } else {
- $item['state_str'] = '';
- }
- } else {
- if ($item['coupon_end_time'] < $dayTime) {
- $item['state_str'] = '<span style="color: red">(已过期)</span>';
- } else {
- $item['state_str'] = '';
- }
- }
- return $item;
- });
- $this->assign('list', $list);
- $this->assign('page', $list->render());
- return $this->fetch();
- }
- public function frozen()
- {
- $id = input('id');
- $coupon_id = input('coupon_id');
- $coupon = model('Common/CouponMember')->where('id', $id)->find();
- if (!$coupon) {
- $this->error('记录不存在');
- }
- if ($coupon['state'] == 2) {
- $this->success('冻结成功', url('index'));
- }
- if (model('common/CouponMember')->where('id', $id)->update(['state' => 2])) {
- if($coupon_id){
- $this->success('冻结成功', url('Coupon/coupon', ['id'=> $coupon_id]));
- }else{
- $this->success('冻结成功', url('Coupon/index'));
- }
- } else {
- $this->error('冻结失败');
- }
- }
- public function thaw()
- {
- $id = input('id');
- $coupon_id = input('coupon_id');
- $coupon = model('common/CouponMember')->where('id', $id)->find();
- if (!$coupon) {
- $this->error('记录不存在');
- }
- if ($coupon['state'] == 1) {
- $this->success('解冻成功', url('index'));
- }
- if (model('Common/CouponMember')->where('id', $id)->update(['state' => 1])) {
- if($coupon_id){
- $this->success('解冻成功', url('Coupon/coupon', ['id'=> $coupon_id]));
- }else{
- $this->success('解冻成功', url('Coupon/index'));
- }
- } else {
- $this->error('解冻失败');
- }
- }
- public function approved()
- {
- $data = input();
- $state = $data['state'];
- $ids = explode(',', $data['ids']);
- unset($data['ids']);
- if (empty($state)) {
- $this->error("系统错误");
- }
- if (!in_array($state, [1, 2])) {
- $this->error('状态错误');
- }
- if (empty($ids)) {
- $this->error("请选择记录");
- }
- $data = model('common/CouponMember')->whereIn('id', $ids)->column('id');
- if ($data) {
- if (model('common/CouponMember')->whereIn('id', $data)->update(['state' => $state])) {
- $this->success("审核成功");
- } else {
- $this->error('审核失败');
- }
- }
- }
- }
|