| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 | <?php/** * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved. * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0). * @author      pillar<ltmn@qq.com> * 优惠券管理 */namespace app\ais\controller\coupon;use app\ais\controller\Common;use app\ais\model\AisConfig;use app\ais\model\AisCoupon;use app\ais\model\AisCouponUser;use app\ais\model\AisCard;use app\common\facade\Inform;use think\facade\Request;use think\helper\Time;use app\ais\model\AisStore;class User extends Common{    public function initialize() {        parent::initialize();        $this->assign('pathMaps', [['name'=>'优惠券管理','url'=>url("coupon/index")]]);    }    /**     * 列表     */    public function index(int $types = 0,int $page = 0){        $condition = [];        $time      = Request::param('time/d',0);        $starttime = Request::param('starttime/s');        $endtime   = Request::param('endtime/s');        if($time){            switch ($time) {                case 2:                    list($start, $end) = Time::yesterday();                    break;                case 30:                    list($start, $end) = Time::month();                    break;                case 60:                    list($start, $end) = Time::lastMonth();                    break;                default:                    list($start, $end) = Time::today();                    break;            }            $condition[] = ['create_time','>=',$start];            $condition[] = ['create_time','<=',$end];        }else{            if($starttime){                $condition[] = ['create_time','>=',strtotime($starttime)];            }            if($endtime){                $condition[] = ['create_time','<=',strtotime($endtime)];            }        }        $store_id   = Request::param('store_id/d');        $store_name = Request::param('store_name');        if($store_id){            $condition[] = ['store_id','=',$store_id];        }        $keyword = Request::param('keyword');        if(!empty($keyword)){            $condition[] = ['name','like','%'.$keyword.'%'];        }        //统计        $view['coupon_count']          = AisCoupon::where($this->mini_program)->where($condition)->where(['is_end' => 0,'is_lock' => 0])->count();        $view['coupon_end_count']      = AisCoupon::where($this->mini_program)->where($condition)->where(['is_end' => 1])->count();        $view['coupon_user_count']     = AisCouponUser::where($this->mini_program)->where($condition)->where(['is_end' => 0])->count();        $view['coupon_user_end_count'] = AisCouponUser::where($this->mini_program)->where($condition)->where(['is_end' => 1])->count();        switch ($types) {            case 1:                $condition[] = ['is_lock','=',1];                $condition[] = ['is_end','=',0];                break;            case 2:                $condition[] = ['is_lock','=',0];                $condition[] = ['is_end','=',0];                break;            case 4:                $condition[] = ['is_end','=',1];                break;            default;                $condition[] = ['is_end','=',0];                break;        }        $view['lists']      = AisCoupon::where($this->mini_program)->where($condition)->order('sort desc,is_lock desc,update_time desc')->paginate(10,false,['query' => ['types' => $types,'store_id' => $store_id,'store_name' => $store_name]]);        $view['types']      = $types;        $view['page']       = $page;        $view['keyword']    = $keyword;        $view['store_id']   = $store_id;        $view['store_name'] = $store_name;        $view['time']       = $time;        $view['starttime']  = $starttime;        $view['endtime']    = $endtime;        return view()->assign($view);    }    //优惠券预览和统计    public function review(int $id,int $types = 0){        $info = AisCoupon::where($this->mini_program)->where(['id' => $id])->find();        if(empty($info)){            $this->error('内容不存在');        }        $condition = [];        $condition['is_end']      = $types ?? 0;        $condition['coupon_id']   = $id;        $view['lists']            = AisCouponUser::where($this->mini_program)->where($condition)->order('id asc')->paginate(10,false,['query'=>['id' => $id,'types' => $types]]);        $view['types']            = $types;        $view['coupon_count']     = AisCouponUser::where($this->mini_program)->where(['coupon_id' => $id])->count();        $view['coupon_end_count'] = AisCouponUser::where($this->mini_program)->where(['coupon_id' => $id,'is_end' => 1])->count();        $view['info']             = $info;        $config = AisConfig::getConfig($this->member_miniapp_id);        $view['end_time']         = $config->coupon_longtime * 60 * 60;        return view()->assign($view);    }    //编辑    public function edit(){        $id   = Request::param('id/d');        if(request()->isAjax()){            $data = [                'id'         => Request::param('id/d'),                'name'       => Request::param('name/s'),                'price'      => Request::param('price/f'),                'discount'   => Request::param('discount/d',0),                'howmuch'    => Request::param('howmuch/d',0),                'tips'       => Request::param('tips/s'),                'types'      => Request::param('types/d',0),            ];            $validate = $this->validate($data,'Coupon.edit');            if(true !== $validate){                return json(['code'=>0,'msg'=>$validate]);            }            if($data['types'] == 0){                $data['discount']  = 0;            }else{                $data['price'] = 0;            }            if($id){                $data['id'] = $id;                $result = AisCoupon::where($this->mini_program)->where(['id' => $data['id']])->update($data);            }else{                $store_id = Request::param('store_id/d',0);                $store = AisStore::where(['id' => $store_id])->find();                if(empty($store)){                    return json(['code'=>0,'msg'=>'商家必须选择']);                }                $data['member_miniapp_id'] = $this->member_miniapp_id;                $data['create_time']       = time();                $data['update_time']       = time();                $data['store_id']          = $store_id;                $data['cate_id']           = $store->cate_id;                $data['cate_sid']          = $store->cate_sid;                $data['is_top']            = 0;                $result                    = AisCoupon::create($data);            }            if($result){                return json(['code'=>200,'url'=>url('coupon/index',['page' => input('get.page/d')]),'msg'=>'操作成功']);            }else{                return json(['code'=>0,'msg'=>'操作失败']);            }        }else{            $info = AisCoupon::where($this->mini_program)->where(['id' => $id])->find();            $view['info'] = $info;            $view['page'] = input('get.page/d');            return view()->assign($view);        }    }    //删除    public function delete(int $id){        $result = AisCoupon::where($this->mini_program)->where(['id' => $id])->delete();        if($result){            return json(['code'=>200,'msg'=>'操作成功']);        }else{            return json(['code'=>403,'msg'=>'删除失败']);        }     }    /**     * 排序     */    public function sort(){        if(request()->isAjax()){            $data = [                'sort' => $this->request->param('sort/d'),                'id'   => $this->request->param('id/d'),            ];            $validate = $this->validate($data,'Category.sort');            if(true !== $validate){                return json(['code'=>0,'msg'=>$validate]);            }            $result = AisCoupon::where($this->mini_program)->where(['id' => $data['id']])->update(['sort'=>$data['sort']]);            if($result){                return json(['code'=>200,'msg'=>'操作成功']);            }else{                return json(['code'=>0,'msg'=>'操作失败']);            }        }    }       /**     * 置顶/取消     * @param integer $id 用户ID     */    public function isTop(int $id){        $rel = AisCoupon::where(['id' => $id])->field('is_top')->find();        $rel->is_top = $rel->is_top ? 0 : 1;        $rel->save();        return json(['code'=>200,'msg'=>'操作成功']);    }      /**     * 审核和取消     * @param integer $id 用户ID     */    public function isLock(int $id){        $result = AisCoupon::where(['id' => $id])->field('is_lock,store_id')->find();        $result->is_lock = $result->is_lock ? 0 : 1;        $rel = $result->save();        if($rel){            if($result->is_lock == 0){                $store = AisStore::where('id','=',$result->store_id)->find();                if($store){                    Inform::sms($store->manage_uid,$this->member_miniapp_id,['title' =>'业务进展通知','type' => '优惠券申请','content' =>'您的优惠券申请已经通过审核','state' => '成功']);                }            }            return json(['code'=>200,'msg'=>'操作成功']);        }else{            return json(['code'=>0,'msg'=>'操作失败']);        }    }        /**     * 用户已领优惠券管理     *     * @param integer $id  优惠券ID     * @param integer $uid 用户ID     * @return void     */    public function user(int $types = 0){        $condition = [];        $uid  = Request::param('uid/d');        if($uid){            $condition[] = ['uid','=',$uid];        }        $store_id   = Request::param('store_id/d');        $store_name = Request::param('store_name/s');        if($store_id){            $condition[] = ['store_id','=',$store_id];        }        $time = Request::param('time/d',0);        if($time){            switch ($time) {                case 2:                    list($start, $end) = Time::yesterday();                    break;                case 30:                    list($start, $end) = Time::month();                    break;                case 60:                    list($start, $end) = Time::lastMonth();                    break;                default:                    list($start, $end) = Time::today();                    break;            }            $condition[] = ['create_time','>=',$start];            $condition[] = ['create_time','<=',$end];        }        $starttime = Request::param('starttime/s');        $endtime = Request::param('endtime/s');        if($starttime){            $condition[] = ['create_time','>=',strtotime($starttime)];        }        if($endtime){            $condition[] = ['create_time','<=',strtotime($endtime)];        }        $view['coupon_count']     = AisCouponUser::where($this->mini_program)->where($condition)->where(['is_end' => 0])->count();        $view['coupon_end_count'] = AisCouponUser::where($this->mini_program)->where($condition)->where(['is_end' => 1])->count();        $view['lists']      = AisCouponUser::where($this->mini_program)->where($condition)->where(['is_end'=>$types ? 1 : 0])->order('id desc')->paginate(20,false,['query'=>['uid' => $uid,'types' => $types,'starttime' => $starttime,'endtime' => $endtime,'time'=>$time]]);        $view['types']      = $types;        $view['time']       = $time;        $view['starttime']  = $starttime;        $view['endtime']    = $endtime;        $view['uid']        = $uid;        $view['store_id']   = $store_id;        $view['store_name'] = $store_name;        $view['pathMaps']   = [['name'=>'已领优惠券','url'=>url("coupon/user")]];        $config = AisConfig::getConfig($this->member_miniapp_id);        $view['end_time']   = $config->coupon_longtime * 60 * 60;        return view()->assign($view);    }    /**     * 查看用户优惠券信息     * @param integer $id     * @return void     */    public function userCoupon(int $id = 0){        $view['info'] = AisCouponUser::where($this->mini_program)->where(['id' => $id])->find();        return view()->assign($view);    }    /**     * 删除用户的优惠券     * @return void     */    public function deleteUser(int $id){        $result = AisCouponUser::where($this->mini_program)->where(['id' => $id])->delete();        if($result){            return json(['code'=>200,'msg'=>'操作成功']);        }else{            return json(['code'=>403,'msg'=>'删除失败']);        }    }}
 |