query()->insertGetId($data); // 返回结果 return $id; } /** * 添加数据 * */ public function edit($id,$data) { // 更新时间 $data['update_time'] = time(); // 写入数据表 $result = $this->query()->where(['id'=>$id])->update($data); // 返回结果 return $result; } /** * 获取优惠券 * * @param int $id 客户优惠券ID(注意:不是优惠券的id) * @param int $uid 客户ID * */ public function getCouponById($id,$uid) { // 返回结果 if( !$id ) return []; // 查询条件 $map = [['custom_coupon.id','=',$id],['custom_coupon.custom_uid','=',$uid],['custom_coupon.status','=',0]]; // 查询优惠券 $coupon = $this->query()->join('coupon','custom_coupon.coupon_id','=','coupon.id')->where($map)->first(['custom_coupon.id','coupon.id as coupon_id','coupon.name','coupon.type_id','coupon.rebate_type','coupon.std_pay','coupon.rebate','custom_coupon.status','custom_coupon.exp_time']); // 如果不存在优惠券 if( !$coupon ) return []; // 转数组方便操作 $coupon = $coupon->toArray(); // 查询优惠券的商品范围 $coupon['product_scope'] = $coupon['type_id'] == 1 ? (new CouponProduct())->getProducts($coupon['coupon_id']) : []; // 获取赠品信息 $coupon['rebate_scope'] = $coupon['rebate_type'] == 3 ? (new CouponRebate())->getProductByCouponId($coupon['coupon_id']) : []; // 返回结果 return $coupon; } /** * 过期状态设置 * */ public function setStatusByExpire() { // 上锁 if(RedisLock::lock('customcoupon::set::status::by::expire',1,30)){ // 修改 $result = $this->query()->where([['status','=',0],['exp_time','<=',time()]])->update(['status'=>3,'update_time'=>time()]); // 不管成功失败,都解锁 RedisLock::unlock('customcoupon::set::status::by::expire',1); // 返回结果 return $result; } } /** * 过期状态设置 * */ public function setStatusByCouponId($couponId,$status) { // 修改未使用以及暂停的为对应的状态,其他状态的保持不变 return $this->query()->where([['coupon_id','=',$couponId]])->whereIn('status',[0,2])->update(['status'=>$status,'update_time'=>time()]); } /** * 获取优惠券扣减金额 * */ public function getRebatePrice($customCouponId,$uid,$productPrice){ // 查询用户的指定优惠券 $coupon = $this->getCouponById($customCouponId,$uid); // 如果有优惠券 if( !$coupon ) return ['is_used'=>0,'product_price'=>$productPrice,'rebate_product'=>[]]; // 总价格 $totalPrice = 0; // 判断是否指定商品范围 if( $coupon['product_scope'] ) { // 循环商品范围 foreach ( $coupon['product_scope'] as $productId ) { // 如果在商品范围 if( isset($productPrice[$productId]['price_total']) ) $totalPrice = $totalPrice + $productPrice[$productId]['price_total']; } }else{ $totalPrice = array_sum(array_column($productPrice,'price_total')); } // 如果没有达标 if( $coupon['std_pay'] > $totalPrice ) return ['is_used'=>0,'product_price'=>$productPrice,'rebate_product'=>[]]; // 达标结果 $rebatePrice = 0; // 满减 扣减金额 if( $coupon['rebate_type'] == 1 ) $rebatePrice = (float) $coupon['rebate']; // 折扣 扣减金额 if( $coupon['rebate_type'] == 2 ) $rebatePrice = (float) $totalPrice - ( $totalPrice * $coupon['rebate'] * 0.1); // 赠品 赠送产品 if( $coupon['rebate_type'] == 3 ) { // 如果没有赠品范围 if( !$coupon['rebate_scope'] ) return ['is_used'=>1,'product_price'=>$productPrice,'rebate_product'=>[]]; // 返回结果 $productList = (new Product())->getListByIds(array_column($coupon['rebate_scope'],'product_id')); // 返回赠品数据 $rebateProduct = []; // 循环处理 foreach ($coupon['rebate_scope'] as $scope) { foreach ($productList as $product) { // 如果商品ID相等 if( $scope['product_id'] == $product['id'] ){ // 产品ID $product['product_id'] = $product['id']; $product['buy_num'] = $scope['rebate_num']; $product['price_total'] = $scope['rebate_num'] * $product['price']; $product['pay_total'] = 0; $product['coupon_total']= $product['price_total']; // 写入到赠品中 $rebateProduct[$product['business_id']][$product['product_id']] = $product; } } } // 返回扣减结果 return ['is_used'=>$customCouponId,'product_price'=>$productPrice,'rebate_product'=>$rebateProduct]; } // 判断每个产品扣减多少钱 // 循环产品 foreach ($productPrice as $productId => $value) { // 如果有商品范围,且产品id不在商品范围的 if( !empty($coupon['product_scope']) && !in_array($productId,$coupon['product_scope']) ) { // 跳过,不做计算 continue; } // 优惠价格 = 价格占总价的比例 * 优惠的总价 $value['rebate_price'] = number_format( $rebatePrice * ($value['price_total'] / $totalPrice) , 2 , '.' ,''); // 重组 $productPrice[$productId] = $value; } // 返回扣减结果 return ['is_used'=>$customCouponId,'product_price'=>$productPrice,'rebate_product'=>[]]; } }