// +---------------------------------------------------------------------- declare (strict_types = 1); namespace app\api\controller; // use app\api\model\Coupon as CouponModel; use app\common\model\coupon\Usercoupon as UsercouponModel; use app\api\service\User as UserService; use app\common\model\coupon\Usercoupondetail as UsercoupondetailModel; use app\common\model\coupon\Couponcard as CouponcardModel; use app\api\model\Order as OrderModel; use app\common\model\OrderAddress as OrderAddressModel; use app\common\model\User as UserModel; use app\common\model\OrderGoods as OrderGoodsModel; use app\common\model\GoodsSku as GoodsSkuModel; use app\common\model\Goods as GoodsModel; use app\api\service\Cart as CartService; use app\api\service\order\Checkout as CheckoutService; use app\api\validate\order\Checkout as CheckoutValidate; use app\common\exception\BaseException; use app\common\enum\order\PayType as OrderPayTypeEnum; use app\common\enum\order\PayStatus as PayStatusEnum; use think\cache\driver\Redis; use think\Facade\Cache; use app\api\model\Coupon as CouponModel; /** * 优惠券中心 米卡 已废弃 * Class Coupon * @package app\api\controller */ class Coupon extends Controller { /* @var CheckoutValidate $validate */ private $validate; /** * 构造方法 * @throws BaseException */ public function initialize() { parent::initialize(); // 用户信息 // 验证类 $this->validate = new CheckoutValidate; } //领券中心 v1.3.2 public function couponlist(){ $model = new CouponModel; $type = $this->request->get('cptype',null); $userinfo = UserService::getCurrentLoginUser(false); $list = $model->getList(1000, true,0,$type,1); return $this->renderSuccess(compact('list')); } /** * 优惠券落地页 * @param $couponId * @return array|\think\response\Json */ public function couponDetail($couponId) { $detail = CouponModel::getDetail($couponId); if (empty($detail)) { return $this->renderError('优惠券不存在'); } return $this->renderSuccess(compact('detail')); } //领券中心 限时抢购 public function limitcouponlist(){ $model = new CouponModel; $userinfo = UserService::getCurrentLoginUser(false); $is_limit_hour = 1; $oldlist = $model->getLimitList(1000, true, $is_limit_hour); $list = []; $h = str_pad(Date("H",time()),2,"0",STR_PAD_LEFT); foreach($oldlist as $row){ $today_end = Date("Y-m-d",time())." ".$row['end_hour']."00:00"; if(!empty($row['start_hour'])&&!empty($row['start_hour'])){ $now = Date("Y-m-d H:i:s",time()); if($now> $today_end){ continue; } } $list[] = $row; } return $this->renderSuccess(compact('list')); } //我的优惠券 public function list(int $type =0){ $model = new UsercouponModel; $userinfo = UserService::getCurrentLoginUser(true); $list = $model->getList($userinfo->user_id,$type); return $this->renderSuccess(compact('list')); } //激活 public function activation(){ $userinfo = UserService::getCurrentLoginUser(true); $param = $this->request->param(); $coupon_code = $param['coupon_code']??''; $key = 'activation:user_id:'.$userinfo->user_id; $rds = new Redis(config('cache.stores.redis')); $num = $rds->get($key); if($num==10){ return $this->renderError('你验证的次数太多了,请稍后再试'); } if($num==null){ $num = 1; }else{ $num++; } $rds->set($key,$num,600); $coupon = UsercouponModel::where('coupon_code',$coupon_code)->find(); if(empty($coupon)){ return $this->renderError('激活码有误'); } if($coupon->activation_state==1||$coupon->user_id>0){ return $this->renderError('激活码已被使用'); } $now = Date("Y-m-d H:i:s",time()); $coupon_card_id = $coupon->coupon_card_id; $card = CouponcardModel::where("id",$coupon_card_id)->find(); $day = $card->expire_day??1; $deadline_at = Date("Y-m-d H:i:s",time()+86400*$day);//先写死一个月 $coupon->user_id = $userinfo->user_id; $coupon->get_at = $now; $coupon->activation_state = 1; $coupon->activation_at = $now; $coupon->deadline_at = $deadline_at; $coupon->save(); return $this->renderSuccess('米卡已激活'); } //兑换明细 public function details($user_coupon_id =0){ $userinfo = UserService::getCurrentLoginUser(true); $couponArr = UsercouponModel::field('id')->where('id',$user_coupon_id)->select(); $card_name = ''; $user_coupon_id_arr = []; foreach($couponArr as $row){ $user_coupon_id_arr[] = $row->id; } $model = new UsercoupondetailModel; $list = $model->getList($user_coupon_id_arr); foreach($list as $row){ $order = OrderGoodsModel::where("order_id",$row->order_id)->find(); $row->express_no = $order->express_no; $order_address = OrderAddressModel::where("order_id",$row->order_id)->find(); $full_address = ''; $address_name = $order_address->name??''; $address_phone = $order_address->phone??''; $row->address_name = $address_name; $row->address_phone = $address_phone; //$row->address = $order_address; if($order_address){ $full_address = $order_address['region']['province'] . $order_address['region']['city'] . $order_address['region']['region'] . $order_address['detail']; } $row->full_address = $full_address; $row->coupon_card_name = $coupon->coupon_card_name??''; if($row->user_id==$userinfo->user_id){ $row->coupon_is_self = true;//如果是当前登录用户用的,就不显示赠送好友 }else{ $row->coupon_is_self = $row->parent_id==0?true:false; } } return $this->renderSuccess(compact('list')); } //领取转赠卡的信息 public function receiveinfo($sign){ $userinfo = UserService::getCurrentLoginUser(true); $coupon = UsercouponModel::where('sign',$sign)->find(); if(!$coupon){ return $this->renderError('找不到信息'); } if($coupon->effect_state==2){ return $this->renderError('该卡已失效'); } if($coupon->frozen_state==1){ return $this->renderError("该卡已冻结"); } $parent_id = $coupon->parent_id; $parent = UsercouponModel::where('id',$parent_id)->find(); $user = UserModel::detail($parent->user_id); $nick_name = $user->nick_name??''; $info['id'] = $coupon->id; $info['coupon_user_id'] = $parent->user_id; $info['good_id'] = $coupon->good_id; $info['nick_name'] = $nick_name; $info['coupon_card_name'] = $coupon->coupon_card_name; $info['coupon_category'] = $coupon->coupon_category; $info['coupon_category_text'] = $coupon->coupon_category_text; $info['isall_trans'] = $coupon->isall_trans; $info['coupon_no'] = $coupon->coupon_no; if($coupon->good_sku_id>0){ $goodsku = GoodsSkuModel::where('goods_sku_id',$coupon->good_sku_id)->where('goods_id',$coupon->good_id)->find(); } $goods = GoodsModel::field('goods_name')->where("goods_id",$coupon->good_id)->find(); $info['sku_name'] = $goodsku->goods_props[0]['value']['name']??''; $info['good_name'] = $goods->goods_name??''; $info['good_num'] = $coupon->good_num; return $this->renderSuccess(compact('info')); } //领取卡 public function receive($sign){ $param = $this->request->param(); $sign = $param['sign']??''; if(empty($sign)){ return $this->renderError('参数有误'); } $userinfo = UserService::getCurrentLoginUser(true); $coupon = UsercouponModel::where('sign',$sign)->find(); $now = Date("Y-m-d H:i:s",time()); if(!$coupon){ return $this->renderError('找不到信息'); } if($coupon->effect_state==2){ return $this->renderError('该卡已失效'); } if($coupon->frozen_state==1){ return $this->renderError("该卡已冻结"); } if($coupon->user_id>0){ return $this->renderError("该卡已被领取"); } if($coupon->avaiable_good_num==0){ return $this->renderError("该卡余额已用完"); } if(time()>strtotime($coupon->deadline_at)){ return $this->renderError("该卡已过期"); } $isall_trans = $coupon->isall_trans; $parent_coupon = UsercouponModel::where('id',$coupon->parent_id)->find(); if($isall_trans==1){ $coupon->user_id = $userinfo->user_id; $coupon->get_at = $now; $coupon->activation_at = $now; $coupon->save(); //原米卡改为失效 UsercouponModel::where('id',$coupon->parent_id)->update(['effect_state'=>2]); return $this->renderSuccess('领取成功'); }else{ $Checkout = new CheckoutService; // 订单结算api参数 $params['goodsId'] = $coupon->good_id; $params['goodsSkuId'] = $coupon->good_sku_id; $params['goodsNum'] = $coupon->good_num; $params['staffUserId'] =0; $params = array_merge($this->request->param(),$params); $Checkout->setParam($params); // 表单验证 if (!$this->validate->scene('buyNow')->check($params)) { return $this->renderError($this->validate->getError(), ['is_created' => false]); } //立即购买:获取订单商品列表 $model = new OrderModel; $goodsList = $model->getOrderGoodsListByNow( (int)$params['goodsId'], (string)$params['goodsSkuId'], (int)$params['goodsNum'], (int)$params['staffUserId'] ); // // 获取订单确认信息 $orderInfo = $Checkout->onCheckout($goodsList); if ($Checkout->hasError()) { $status = $Checkout->getErrorStatus(); if($status==423){ return $this->renderError('很抱歉,您的收货地址不在商家的配送范围内', ['is_created' => false]); }else{ // return $this->renderError($Checkout->getError(), ['is_created' => false]); return $this->renderError('商品暂无法兑换,请到“我的”页面联系客服', ['is_created' => false]); } } $orderInfo['couponId'] = 0; $orderInfo['couponMoney'] = $orderInfo['orderPrice']; $orderInfo['orderPayPrice'] = 0; $orderInfo['payType'] = OrderPayTypeEnum::MICAH_EXCHANGE; $orderInfo['pay_status'] = PayStatusEnum::SUCCESS; // 创建订单 if(!$Checkout->createOrder($orderInfo)) { return $this->renderError($Checkout->getError() ?: '订单创建失败', ['is_created' => false]); } $order_id = $Checkout->model['order_id']; $coupon->user_id = $userinfo->user_id; $coupon->get_at = $now; $coupon->activation_at = $now; $coupon->avaiable_good_num = 0; $coupon->save(); $detail['user_coupon_id'] = $coupon->parent_id; $detail['good_num'] = $coupon->good_num; $detail['good_id'] = $coupon->good_id; $detail['good_sku_id'] = $coupon->good_sku_id; $detail['created_at'] = $now; $detail['coupon_user_id'] = $parent_coupon->user_id??0; $detail['user_id'] = $userinfo->user_id; $detail['order_id'] = $order_id; $detail['good_name'] = $goodsList[0]['goods_name']??''; $detail['tmp_id'] = 1000000+intval($order_id); $detail['parent_id'] = $coupon->parent_id; $detailModel = new UsercoupondetailModel; $detailModel->save($detail); $good_num = UsercoupondetailModel::where("user_coupon_id",$coupon->parent_id)->sum('good_num'); if($parent_coupon->good_num==$good_num){ //兑换完成了,状态改为已失效 $parent_coupon->effect_state = 2; $parent_coupon->save(); } return $this->renderSuccess([ 'orderId' => $order_id, // 订单id ]); } } //商品详情页 自己兑换 public function exchange(){ $userinfo = UserService::getCurrentLoginUser(true); $p = $this->request->param(); $user_coupon_id = $p['user_coupon_id']??0; $good_num = $p['good_num']??1; $coupon = UsercouponModel::where('id',$user_coupon_id)->where('user_id',$userinfo->user_id)->find(); $now = Date("Y-m-d H:i:s",time()); if(!$coupon){ return $this->renderError('找不到信息'); } if($coupon->effect_state!=1){ return $this->renderError('该卡已失效'); } if($coupon->frozen_state==1){ return $this->renderError("该卡已冻结"); } if($coupon->avaiable_good_num==0){ return $this->renderError("该卡余额已用完"); } if($coupon->avaiable_good_num<$good_num){ return $this->renderError("该卡余额不够本次兑换"); } if(time()>strtotime($coupon->deadline_at)){ return $this->renderError("该卡已过期"); } $isall_trans = $coupon->isall_trans; $Checkout = new CheckoutService; // 订单结算api参数 $params['goodsId'] = $coupon->good_id; $params['goodsSkuId'] = $coupon->good_sku_id; $params['goodsNum'] = $good_num; $params['staffUserId'] =0; $params = array_merge($this->request->param(),$params); $Checkout->setParam($params); // 表单验证 if (!$this->validate->scene('buyNow')->check($params)) { return $this->renderError($this->validate->getError(), ['is_created' => false]); } //立即购买:获取订单商品列表 $model = new OrderModel; $goodsList = $model->getOrderGoodsListByNow( (int)$params['goodsId'], (string)$params['goodsSkuId'], (int)$params['goodsNum'], (int)$params['staffUserId'] ); // 获取订单确认信息 $orderInfo = $Checkout->onCheckout($goodsList); if ($Checkout->hasError()) { $status = $Checkout->getErrorStatus(); if($status==423){ return $this->renderError('很抱歉,您的收货地址不在商家的配送范围内', ['is_created' => false]); }else{ // return $this->renderError($Checkout->getError(), ['is_created' => false]); return $this->renderError('商品暂无法兑换,请到“我的”页面联系客服', ['is_created' => false]); } } $orderInfo['couponId'] = 0; $orderInfo['couponMoney'] = $orderInfo['orderPrice']; $orderInfo['orderPayPrice'] = 0; $orderInfo['payType'] = OrderPayTypeEnum::MICAH_EXCHANGE; $orderInfo['pay_status'] = PayStatusEnum::SUCCESS; // 创建订单 if(!$Checkout->createOrder($orderInfo)) { return $this->renderError($Checkout->getError() ?: '订单创建失败', ['is_created' => false]); } $order_id = $Checkout->model['order_id']; $coupon->user_id = $userinfo->user_id; $coupon->get_at = $now; $coupon->activation_at = $now; $coupon->avaiable_good_num = $coupon->avaiable_good_num - $good_num; // if($coupon->avaiable_good_num==0){ // $coupon->effect_state = 2; // } $coupon->save(); $coupon_user_id = 0; if($coupon->parent_id>0){ $parent_coupon = UsercouponModel::where("id",$coupon->parent_id)->find(); $coupon_user_id = $parent_coupon->user_id??0; }else{ $coupon_user_id = $coupon->user_id; } $detail['user_coupon_id'] = $coupon->id; $detail['good_num'] = $good_num; $detail['good_id'] = $coupon->good_id; $detail['good_sku_id'] = $coupon->good_sku_id; $detail['created_at'] = $now; $detail['coupon_user_id'] = $coupon_user_id; $detail['user_id'] = $userinfo->user_id; $detail['order_id'] = $order_id; $detail['good_name'] = $goodsList[0]['goods_name']??''; $detail['tmp_id'] = 1000000+intval($order_id); $detail['parent_id'] = $coupon->parent_id; $detailModel = new UsercoupondetailModel; $detailModel->save($detail); $good_num = UsercoupondetailModel::where("user_coupon_id",$coupon->id)->sum('good_num'); if($coupon->good_num==$good_num){ //兑换完成了,状态改为已失效 UsercouponModel::where("id",$coupon->id)->update(['effect_state'=>2]); } return $this->renderSuccess([ 'orderId' => $order_id, //订单id ]); } //一条米卡信息 public function one($user_coupon_id=0){ $userinfo = UserService::getCurrentLoginUser(true); $info = UsercouponModel::where('id',$user_coupon_id)->where("user_id",$userinfo->user_id)->find(); $now = Date("Y-m-d H:i:s",time()); if(!$info){ return $this->renderError('找不到信息'); } if($info->effect_state==2){ return $this->renderError('该卡已失效'); } if($info->frozen_state==1){ return $this->renderError("该卡已冻结"); } if($info->good_sku_id>0){ $goodsku = GoodsSkuModel::where('goods_sku_id',$info->good_sku_id)->where('goods_id',$info->good_id)->find(); } $goods = GoodsModel::field('goods_name')->where("goods_id",$info->good_id)->find(); $info->good_name = $goods->goods_name??''; $info->sku_name = $goodsku->goods_props[0]['value']['name']??''; return $this->renderSuccess(compact("info")); } //卡转赠, 整卡转赠 public function shareto($user_coupon_id,$isall=1,$trans_good_num=1){ $userinfo = UserService::getCurrentLoginUser(true); $coupon = UsercouponModel::where('id',$user_coupon_id)->where("user_id",$userinfo->user_id)->find(); if($coupon->effect_state==2){ return $this->renderError('该卡已失效'); } if($coupon->frozen_state==1){ return $this->renderError("该卡已冻结"); } if($coupon->activation_state==0){ return $this->renderError("该卡未激活"); } if($isall==1){ if($coupon->good_num!=$coupon->avaiable_good_num){ return $this->renderError("该卡不能整体转赠"); } }else{ if($coupon->avaiable_good_num<$trans_good_num){ return $this->renderError("该卡转赠数量不能超过剩余可用数量"); } } if($coupon->avaiable_good_num<=0){ return $this->renderError("该卡没有可用余额"); } $now = Date("Y-m-d H:i:s",time()); $value = $coupon->id."|".$userinfo->user_id.'|'.time(); $sign = md5($value); if($isall==1){ $data['good_num'] = $coupon->good_num; $data['avaiable_good_num'] = $coupon->avaiable_good_num; }else{ $data['good_num'] = $trans_good_num; $data['avaiable_good_num'] = $trans_good_num; $data['is_delete'] = 1; } $data['isall_trans'] = $isall; $data['coupon_card_id'] = $coupon->coupon_card_id; $data['coupon_card_name'] = $coupon->coupon_card_name; $data['coupon_no'] = $coupon->coupon_no; $data['coupon_code'] = $coupon->coupon_code; $data['coupon_type'] = $coupon->coupon_type; $data['coupon_category'] = $coupon->coupon_category; $data['good_id'] = $coupon->good_id; $data['good_sku_id'] = $coupon->good_sku_id; $data['created_at'] = $now; $data['effect_state'] = 1; $data['sign'] = $sign; $data['parent_id'] = $coupon->id; $data['deadline_at'] = $coupon->deadline_at; $data['activation_state']=1; $model = new UsercouponModel; $model->save($data);// 写一张新的米卡记录,未领取 if($isall==1){ $coupon->effect_state = 0;//原卡变更为已失效 $coupon->avaiable_good_num =0; }else{ $coupon->avaiable_good_num = $coupon->avaiable_good_num - $trans_good_num; } $coupon->save(); return $this->renderSuccess(compact('sign')); } // public function index(){ $t = 86400; // $t = 120; //整卡转赠 $list = UsercouponModel::field("id")->where('effect_state',0)->select(); // return $list; $idarr = []; foreach($list as $row){ $idarr[] = $row['id']; } foreach($idarr as $id){ $coupon = UsercouponModel::where('parent_id',$id)->find(); if($coupon){ $add_time = strtotime($coupon['created_at']) + $t;//加一天的时 $now = time(); //过期了没领 if($now>$add_time){ //整卡赠送 if($coupon->isall_trans==1){ $coupon->effect_state = 2;//已失效 $coupon->save();//该卡改为已失效 $pcouponModel = UsercouponModel::where('id',$coupon->parent_id)->where('effect_state',0)->find(); $pcouponModel->effect_state = 1;//原卡改为生效中, $pcouponModel->avaiable_good_num = $pcouponModel->good_num;//可用数量改为原来的值 $pcouponModel->save(); } } } } //部分转赠 未被领取 $list = UsercouponModel::where('user_id',0)->where('isall_trans',0)->where('effect_state',1)->select(); foreach($list as $row){ $add_time = strtotime($row['created_at']) + $t;//加一天的时间 $now = time(); //过期了没领 if($now>$add_time){ $id = $row['id']; $coupon = UsercouponModel::where("id",$id)->find(); $coupon->effect_state = 2; $coupon->save(); $parent_coupon = UsercouponModel::where('id',$coupon->parent_id)->find(); if($parent_coupon->effect_state==1){ $parent_coupon->avaiable_good_num = $parent_coupon->avaiable_good_num+$coupon->avaiable_good_num; if($parent_coupon->avaiable_good_num<=$parent_coupon->good_num){ $parent_coupon->save(); } } } } echo 'success'; echo 't:'.$t; } }