123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types = 1);
- namespace app\store\controller;
- // use app\common\model\coupon\Usercoupon as UserCouponModel;
- // use app\common\model\coupon\Usercoupondetail as UsercoupondetailModel;
- // use app\common\model\coupon\Couponcard as CouponcardModel;
- use app\store\model\Coupon as CouponModel;
- use app\common\service\Export as ExportService;
- use app\store\model\coupon\CouponGenRedeemCodes;
- use app\store\model\coupon\CouponRedeemCodes;
- use app\store\model\UserCoupon;
- use think\facade\Validate;
- /**
- * 优惠券管理
- * Class Files
- * @package app\store\controller
- */
- class Coupon extends Controller
- {
- /**
- * 添加卡
- * @return array|string
- */
- public function add()
- {
- // 新增记录
- $model = new CouponModel;
- $post = $this->postForm();
- // $post['coupon_type'] = CouponModel::ManJian;
- $post['create_user'] =$this->store['user']['real_name'];
- $post['audit_admin_id'] = $this->store['user']['store_user_id']?:'';
- if(isset($post['limit_total_type']) && $post['limit_total_type']==10){
- if(empty($post['total_num'])){
- return $this->renderError('限制数量total_num参数值要大于0');
- }
- }
- if(isset($post['limit_total_type']) && $post['limit_total_type']==20){
- $post['total_num'] = $post['total_num']??0;
- if($post['total_num']>0){
- return $this->renderError('不限制数量total_num应该为0');
- }
- }
- if ($model->addOne($post)) {
- return $this->renderSuccess('添加成功');
- }
- return $this->renderError($model->getError() ?: '添加失败');
- }
- /**
- * 添加卡
- * @return array|string
- */
- public function update()
- {
- $post = $this->postForm();
- $model = CouponModel::detail((int)$post['coupon_id']);
- $post['audit_user'] =$this->store['user']['real_name'];
- $post['audit_admin_id'] = $this->store['user']['store_user_id']?:'';
- if(isset($post['limit_total_type']) && $post['limit_total_type']==10){
- if(empty($post['total_num'])){
- return $this->renderError('限制数量total_num参数值要大于0');
- }
- }
- if(isset($post['limit_total_type']) && $post['limit_total_type']==20){
- $post['total_num'] = $post['total_num']??0;
- if($post['total_num']>0){
- return $this->renderError('不限制数量total_num应该为0');
- }
- }
- if ($model->updateOne($post)) {
- return $this->renderSuccess('修改成功');
- }
- return $this->renderError($model->getError() ?: '修改失败');
- }
- //详情
- public function detail(int $coupon_id){
- $detail = CouponModel::detail($coupon_id,['goodsExcept']);
- return $this->renderSuccess(compact('detail'));
- }
- // /**
- // * 启用 禁用
- // * @param int $coupon_id
- // * @return array|bool
- // */
- public function status(int $coupon_id){
- $model = CouponModel::detail($coupon_id);
- $postData = $this->postForm();
- unset($postData['coupon_id']);
- $res ='操作成功';
- $status =$postData['status'];
- if($status==1){
- $res = '已启用';
- }
- if($status==0){
- $res = '已禁用';
- }
- if ($model->status($postData)) {
- return $this->renderSuccess($postData,$res);
- }
- return $this->renderError($model->getError() ?: '操作失败');
- }
- // /**
- // * 审核
- // * @param int $coupon_id
- // * @return array|bool
- // */
- public function audit(int $coupon_id)
- {
- $model = CouponModel::detail($coupon_id);
- if(empty($model)){
- return $this->renderError('找不到优惠券');
- }
- // 确认审核
- $postData = $this->postForm();
- unset($postData['coupon_id']);
- $res ='操作成功';
- $audit_status =$postData['audit_status'];
- if($audit_status==10){
- $res = '已审核通过';
- $postData['status'] =1;//启用
- }
- if($audit_status==20){
- $res = '已审核不通过';
- $postData['status'] = 0;//禁用
- }
- $postData['audit_time'] = Date("Y-m-d H:i:s",time());
- $postData['audit_user'] = $this->store['user']['real_name'];
- //$postData['create_user'] = $this->store['user']['real_name'];s
- $postData['audit_admin_id'] = $this->store['user']['store_user_id']?:'';
- if ($model->audit($postData)) {
- return $this->renderSuccess($postData,$res);
- }
- return $this->renderError($model->getError() ?: '操作失败');
- }
- //裂变活动优惠券
- public function couponGroupBuyLblist(){
- $model = new CouponModel;
- $list = $model->getCouponGroupBuyLbPagelist($this->request->param());
- return $this->renderSuccess(compact('list'));
- }
- //后台优惠券列表
- public function couponlist(){
- $model = new CouponModel;
- $list = $model->getList($this->request->param());
- return $this->renderSuccess(compact('list'));
- }
- /**
- * 米卡发放列表
- * @return array
- * @throws \think\db\exception\DbException
- */
- public function list()
- {
- $model = new UserCouponModel;
- $list = $model->getAllList($this->request->param());
- return $this->renderSuccess(compact('list'));
- }
- //冻结
- public function frozen($id,$frozen_state=1){
- $model = new UserCouponModel;
- $model->where('id',$id)->update(['frozen_state'=>$frozen_state]);
- return $this->renderSuccess('更新成功');
- }
- //兑换明细
- public function detaillist($user_coupon_id=0){
- $model = new UsercoupondetailModel;
- $param['user_coupon_id'] = $user_coupon_id;
- $list = $model->getAllList($param);
- return $this->renderSuccess(compact('list'));
- }
- //米卡配置列表
- public function cardlist(){
- $model = new CouponcardModel;
- $list = $model->getAllList([]);
- return $this->renderSuccess(compact('list'));
- }
- //添加米卡券
- public function createcoupon($id,$num=1){
- $model = new CouponcardModel;
- $list = $model->addCard($id,$num);
- return $this->renderSuccess('添加成功');
- }
- //添加数据
- public function addcard(){
- $param = $this->request->param();
- $model = new CouponcardModel;
- $param['created_at'] =Date("Y-m-d H:i:s",time());
- $param['good_sku_id'] = empty($param['good_sku_id'])?0:$param['good_sku_id'];
- $model->save($param);
- return $this->renderSuccess('添加成功');
- }
- // /**
- // * 编辑文件
- // * @param int $fileId
- // * @return array
- // */
- // public function edit(int $fileId)
- // {
- // // 文件详情
- // $model = UploadFileModel::detail($fileId);
- // // 更新记录
- // if ($model->edit($this->postForm())) {
- // return $this->renderSuccess('更新成功');
- // }
- // return $this->renderError($model->getError() ?: '更新失败');
- // }
- // /**
- // * 删除文件(批量)
- // * @param array $fileIds 文件id集
- // * @return array
- // * @throws \think\Exception
- // * @throws \think\db\exception\DataNotFoundException
- // * @throws \think\db\exception\DbException
- // * @throws \think\db\exception\ModelNotFoundException
- // */
- // public function delete(array $fileIds)
- // {
- // $model = new UploadFileModel;
- // if (!$model->setDelete($fileIds)) {
- // return $this->renderError($model->getError() ?: '操作失败');
- // }
- // return $this->renderSuccess('操作成功');
- // }
- // /**
- // * 移动文件到指定分组(批量)
- // * @param int $groupId
- // * @param array $fileIds
- // * @return array
- // */
- // public function moveGroup(int $groupId, array $fileIds)
- // {
- // $model = new UploadFileModel;
- // if (!$model->moveGroup($groupId, $fileIds)) {
- // return $this->renderError($model->getError() ?: '操作失败');
- // }
- // return $this->renderSuccess('操作成功');
- // }
- /**
- * zq---------------------------------------------------------------------------------------------------------------
- * @Time: 2021/12/29 13:43
- */
- /**
- * 生成优惠券的兑换码
- * @return array
- */
- public function genCouponCodes(){
- $post = $this->postForm();
- $validate = Validate::rule('couponId', 'require|number|gt:0')
- ->rule([
- 'amount' => 'require|number|gt:0',
- 'giveType' => 'require|number|in:1,2'
- ]);
- if (!$validate->check($post)) {
- return $this->renderError($validate->getError());
- }
- //检验优惠券数量是否超发
- $m = new CouponGenRedeemCodes();
- /* $flag = $m->sunAmount($post['couponId'],$post['amount']);
- if($flag){
- return $this->renderError('优惠券数量已达上限');
- }*/
- if($post['giveType'] == 1){
- if (!isset($post['code']) || !$post['code']){
- return $this->renderError('兑换码不能为空');
- }
- //兑换码匹配汉字数字字母
- $pattern = "/^[a-zA-Z0-9\x{4e00}-\x{9fa5}]+$/u";
- if(!preg_match($pattern,$post['code'])){
- return $this->renderError('兑换码格式不正确');
- }
- $codeM = new CouponRedeemCodes();
- if ($codeM->checkExistedCode($post['code'])){
- return $this->renderError('兑换码已被使用');
- }
- }
- if( $m->addCodes($post)){
- return $this->renderSuccess([],'成功');
- }
- return $this->renderError('系统繁忙');
- }
- /**
- *优惠券兑换码列表
- * @return array
- */
- public function couponCodesList(){
- $param = $this->request->param();
- $m = new CouponGenRedeemCodes();
- $list = $m->getAllList($param);
- return $this->renderSuccess(compact('list'),'成功');
- }
- /**
- *优惠券兑换码明细表
- * @return array
- */
- public function codesDetail(){
- $m = new CouponRedeemCodes();
- $list = $m->getAllList($this->request->param('id'));
- return $this->renderSuccess(compact('list'),'成功');
- }
- /**
- * 兑换码导出功能
- * @return array
- * @Time: 2021/10/15 13:43
- */
- public function redeemCodesExport()
- {
- $param = $this->request->param();
- /* if(!isset($param['ids']) || empty($param['ids'])){
- return $this->renderError('请勾选兑换码后再导出');
- }*/
- $model = new CouponRedeemCodes;
- if(isset($param['id']) && $param['id'] > 0){
- $data = $model->redeemCodesExport($param,true);
- //return $this->renderError('请勾选兑换码后再导出');
- }else{
- $data = $model->redeemCodesExport($param);
- }
- //$data = $model->redeemCodesExport($param);
- if (!$data['data'] || !count($data['data'])){
- return $this->renderError('没有数据哟');
- }
- $res = ExportService::export($data['data'],$data['header'],$data['filename'],'兑换码列表');
- return $this->renderSuccess($res,'导出成功');
- }
- /**
- * 优惠券统计表
- * @return array
- */
- public function couponStatistics()
- {
- $params = $this->request->param();
- $m = new CouponModel();
- $list = $m->getStatisticList($params);
- return $this->renderSuccess(compact('list'),'成功');
- }
- /**
- * 优惠券统计表-统计明细
- * @return array
- */
- public function couponStatisticsDetail()
- {
- $params = $this->request->param();
- if (empty($params['couponId'])){
- return $this->renderError('要选择优惠券哟');
- }
- $m = new UserCoupon();
- $list = $m->getStatisticDetail($params);
- return $this->renderSuccess(compact('list'),'成功');
- }
- /**
- * 会员优惠券统计表
- * @return array
- */
- public function memberCouponStatistics()
- {
- $params = $this->request->param();
- $m = new CouponModel();
- $list = $m->getMemberStatisticList($params);
- return $this->renderSuccess(compact('list'),'成功');
- }
- }
|