|
|
@@ -2,6 +2,7 @@
|
|
|
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;
|
|
|
@@ -172,14 +173,30 @@ class UserCouponLogic extends BaseLogic
|
|
|
public static function categoryWithAmountLists($params)
|
|
|
{
|
|
|
try{
|
|
|
- $coupon_all_ids =UserCoupon::where('user_id',$params['user_id'])
|
|
|
- ->where('voucher_count','>',0)
|
|
|
- ->where('voucher_status',0)
|
|
|
- ->where('expire_time','>',time())
|
|
|
- ->where('amount_require','<=',$params['amount'])
|
|
|
- ->column('coupon_id');
|
|
|
+ // 构建查询条件
|
|
|
+ $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_ids = CouponCategory::where('goods_category_id',$params['goods_category_id'])->whereIn('coupon_id',$coupon_all_ids)->column('coupon_id');
|
|
|
+ // 提取所有 coupon_id 到数组
|
|
|
+ $coupon_ids = array_column($filteredCoupons, 'coupon_id');
|
|
|
|
|
|
$data = UserCoupon::where('user_id',$params['user_id'])
|
|
|
->where('voucher_count','>',0)
|
|
|
@@ -200,12 +217,14 @@ class UserCouponLogic extends BaseLogic
|
|
|
$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'])
|
|
|
- ->select()->toArray();
|
|
|
- foreach($data as $k => $v){
|
|
|
- $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'].'可用';
|
|
|
- }
|
|
|
+ ->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());
|