| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- <?php
- namespace app\api\logic;
- use app\common\logic\BaseLogic;
- use app\common\model\coupon\CouponCategory;
- use app\common\model\coupon\CouponGoods;
- use app\common\model\coupon\CouponRules;
- use app\common\model\coupon\UserCoupon;
- use app\common\model\goods\Goods;
- use think\db\Query;
- use think\db\Where;
- use think\Exception;
- use think\facade\Db;
- use think\facade\Log;
- /**
- * @author 林海涛
- * @date 2024/7/18 下午4:49
- */
- class UserCouponLogic extends BaseLogic
- {
- public static function add($params)
- {
- Db::startTrans();
- try{
- $errMsgArr = [];
- $rules = CouponRules::whereIn('code',$params['codes'])->where('voucher_status',1)->lock(true)->select();
- $userCoupon = UserCoupon::where('user_id',$params['user_id'])
- ->whereIn('code',$params['codes'])
- ->where('voucher_count','>',0)
- ->select();
- if($rules->isEmpty()){
- $errMsgArr[] = '优惠券不存在';
- }
- $updateData = [];
- $createData = [];
- foreach($rules as $v) {
- if($v->voucher_status == 2){
- $errMsgArr[] = $v->event_name .'已设置为过期';
- continue;
- }
- if($v->remaining_count <= 0 ){
- $errMsgArr[] = $v->event_name .'数量不足';
- continue;
- }
- if(isset($params['property_activity_id']) && !empty($params['property_activity_id'])){
- if($userCoupon->where('coupon_id',$v->id)->where('property_activity_id',$params['property_activity_id'])->count()){
- $errMsgArr[] = $v->event_name . '活动券已经领取过了,请勿重复领取';
- continue;
- }
- }else{
- if($userCoupon->where('coupon_id',$v->id)->where('property_activity_id',0)->count()){
- $errMsgArr[] = $v->event_name . '已经领取过了,请勿重复领取';
- continue;
- }
- }
- $updateData[$v->id] = ['remaining_count' => --$v->remaining_count];
- $createData[] = [
- 'user_id'=>$params['user_id'],
- 'coupon_target'=>$v->coupon_target,
- 'coupon_id' => $v->id,
- 'code' => $v->code,
- 'voucher_status' => 0,
- 'voucher_count' =>1,
- 'amount' => $v->mold_type != 1 ? $v->amount:null,
- 'amount_require' => $v->amount_require,
- 'begin_use' => time(),
- 'discount_ratio' => $v->mold_type == 1 ? $v->discount_ratio:null,
- 'event_name' => $v->event_name,
- 'expire_time' => time()+$v->expire_time,
- 'max_deductible_price' => $v->mold_type == 1 ? $v->max_deductible_price:null,
- 'mold_type' => $v->mold_type,
- 'server_category_name' => $v->server_category_name,
- 'property_activity_id' => $params['property_activity_id']??0,
- ];
- }
- if(!empty($updateData)){
- CouponRules::updateWhenCase($updateData);
- }
- if(!empty($createData)){
- (new UserCoupon())->saveAll($createData);
- }
- Db::commit();
- $errMsgArr && self::setError(implode(',',$errMsgArr));
- return $errMsgArr;
- }catch(\Exception $e){
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 我的优惠卷列表
- * @param $params
- * @return array|false
- */
- public static function userCouponList($params)
- {
- try{
- $where = [];
- $where[] = ['voucher_status','=',0];
- if(isset($params['voucher_status']) && is_array($params['voucher_status'])){
- $where[] = ['voucher_status','IN',$params['voucher_status']];
- }
- $where[] = ['user_id','=',$params['user_id']];
- $data = UserCoupon::where($where)
- ->where(function(Query $query){
- $query->where(' expire_time >'. time());
- })
- ->field(['id','code','amount','amount_require','begin_use','voucher_status','discount_ratio','event_name','expire_time','max_deductible_price','server_category_name','mold_type'])
- ->append(['voucher_status_text','discount_ratio_text'])
- ->order(['id' => 'desc'])
- ->select()
- ->toArray();
- $couponCodes = [];
- foreach($data as $k => $v){
- if($v['expire_time'] < time()){
- $couponCodes[] = $v['code'];
- $data[$k]['voucher_status_text'] = '已过期';
- }
- $data[$k]['begin_use'] = date("Y-m-d H:i:s",$v['begin_use'] );
- $data[$k]['expire_time'] = date("Y-m-d H:i:s",$v['expire_time'] );
- $data[$k]['amount_require'] = '满'.$v['amount_require'].'可用';
- }
- if(!empty($couponCodes)){
- UserCoupon::whereIn('code',$couponCodes)->update(['voucher_status'=>2]);
- }
- return $data;
- } catch (\Exception $e){
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 商品分类优惠卷领取
- * @param $params
- * @return array|false
- */
- public static function categoryCouponLists($params)
- {
- try{
- $category_coupon_ids = CouponCategory::where('goods_category_id',$params['goods_category_id'])->column('coupon_id');
- $goods_coupon_ids = !empty($params['goods_id'])?CouponGoods::where('goods_id',$params['goods_id'])->column('coupon_id'):'';
- $data = CouponRules::where('remaining_count','>',0)
- ->where('coupon_type',1)
- ->where('voucher_status',1)
- ->where(function($query) use ($category_coupon_ids, $goods_coupon_ids) {
- $query->whereOr(function($query1) use ($goods_coupon_ids) {
- $query1->whereIn('id', $goods_coupon_ids)
- ->where('coupon_target', 'goods');
- });
- $query->whereOr(function($query2) use ($category_coupon_ids) {
- $query2->whereIn('id', $category_coupon_ids)
- ->where('coupon_target', 'category');
- });
- })
- ->append(['discount_ratio_text'])
- ->order(['id' => 'desc'])
- ->field(['code','amount','amount_require','discount_ratio','server_category_name',
- 'event_name','expire_time','max_deductible_price','mold_type',])
- ->select();
- foreach($data as $k => $v){
- $data[$k]['amount_require'] = '满'.$v['amount_require'].'可用';
- }
- $codes = $data->column('code');
- $userCouponCodes = [];
- if(!empty($code)){
- $userCouponCodes = UserCoupon::where('code',$codes)
- ->where('user_id',$params['user_id'])->column('code');
- }
- $data = $data->whereNotIn('code',$userCouponCodes)->toArray();
- return $data;
- }catch(\Exception $e){
- self::setError($e->getMessage());
- return false;
- }
- }
- public static function categoryWithAmountLists($params)
- {
- Log::info('coupon-'.json_encode($params));
- try{
- // 构建查询条件
- $query = UserCoupon::where('user_id', $params['user_id'])
- ->where('voucher_count', '>', 0)
- ->where('voucher_status', 0)
- ->where('expire_time', '>', time())
- ->where('amount_require', '<=', $params['amount']);
- // 执行查询并获取结果集
- $coupons = $query->select()->toArray(); // 使用 toArray() 获取数组形式的结果
- $filteredCoupons = array_filter($coupons, function ($item) use ($params) {
- if ($item['coupon_target'] == 'category') {
- $coupon_id = CouponCategory::where('coupon_id', $item['coupon_id'])
- ->where('goods_category_id', $params['goods_category_id'])
- ->value('coupon_id'); // 获取单个值
- } elseif ($item['coupon_target'] == 'goods') {
- $coupon_id = CouponGoods::where('coupon_id', $item['coupon_id'])
- ->where('goods_id', $params['goods_id'])
- ->value('coupon_id'); // 获取单个值
- }
- // 如果 $coupon_id 不为空,则保留该项
- return !empty($coupon_id);
- });
- // 提取所有 coupon_id 到数组
- $coupon_ids = array_column($filteredCoupons, 'coupon_id');
- $data = UserCoupon::where('user_id',$params['user_id'])
- ->where('voucher_count','>',0)
- ->where('voucher_status',0)
- ->whereIn('coupon_id',$coupon_ids);
- // 去除代理券
- if(isset($params['property_activity_id']) && !empty($params['property_activity_id'])){
- $remove_user_coupon_ids = UserCoupon::where('property_activity_id',$params['property_activity_id'])->column('id')?:[];
- !empty($remove_user_coupon_ids) && $data = $data->whereNotIn('id',$remove_user_coupon_ids);
- Log::info('remove_coupon_ids-1001:'.json_encode([$params['property_activity_id'],$remove_user_coupon_ids]));
- }elseif (isset($params['goods_id']) && !empty($params['goods_id'])){
- $remove_coupon_ids = [];
- $property_activity_id = Goods::where('id',$params['goods_id'])->value('property_activity_id');
- $property_activity_id && $remove_coupon_ids = CouponRules::where('property_activity_id',$property_activity_id)->column('id')?:[];
- !empty($remove_coupon_ids) && $data = $data->whereNotIn('coupon_id',$remove_coupon_ids);
- Log::info('remove_coupon_ids-1001:'.json_encode([$params['goods_id'],$property_activity_id,$remove_coupon_ids]));
- }
- $data = $data->append(['discount_ratio_text'])
- ->where('expire_time','>',time())
- ->visible(['id','coupon_id','amount','amount_require','begin_use','discount_ratio','event_name','expire_time','max_deductible_price','server_category_name','mold_type'])
- ->order('amount desc')
- ->select()
- ->each(function (&$item){
- $item['begin_use'] = date("Y-m-d H:i:s",$item['begin_use'] );
- $item['expire_time'] = date("Y-m-d H:i:s",$item['expire_time'] );
- $item['amount_require'] = '满'.$item['amount_require'].'可用';
- })
- ->toArray();
- return $data;
- } catch(\Exception $e){
- self::setError($e->getMessage());
- return false;
- }
- }
- public static function grant($codes = [],$user_id = 0,$order_id = 0)
- {
- try{
- if($codes){
- $userCouponLogic = UserCouponLogic::add(['codes'=>$codes,'user_id'=>$user_id]);
- if($userCouponLogic === false) throw new \Exception(UserCouponLogic::getError());
- if(empty(UserCouponLogic::getError())){
- // 通知用户公众号 跳转优惠券
- event('Notice', [
- 'scene_id' => 129,
- 'params' => [
- 'user_id' => $user_id??0,
- 'order_id' => $order_id??0,
- ]
- ]);
- }
- }
- return true;
- }catch(\Exception $e){
- throw new \Exception($e->getMessage());
- }
- }
- }
|