package com.gxwebsoft.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.shop.entity.ShopCouponApplyCate;
import com.gxwebsoft.shop.entity.ShopCouponApplyItem;
import com.gxwebsoft.shop.mapper.ShopCouponMapper;
import com.gxwebsoft.shop.service.ShopCouponApplyCateService;
import com.gxwebsoft.shop.service.ShopCouponApplyItemService;
import com.gxwebsoft.shop.service.ShopCouponService;
import com.gxwebsoft.shop.entity.ShopCoupon;
import com.gxwebsoft.shop.param.ShopCouponParam;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 优惠券Service实现
 *
 * @author 科技小王子
 * @since 2025-08-11 23:51:23
 */
@Service
public class ShopCouponServiceImpl extends ServiceImpl<ShopCouponMapper, ShopCoupon> implements ShopCouponService {
    @Resource
    private ShopCouponApplyItemService couponApplyItemService;
    @Resource
    private ShopCouponApplyCateService couponApplyCateService;

    @Override
    public PageResult<ShopCoupon> pageRel(ShopCouponParam param) {
        PageParam<ShopCoupon, ShopCouponParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number asc, create_time desc");
        List<ShopCoupon> list = baseMapper.selectPageRel(page, param);
        for (ShopCoupon coupon : list) {
            coupon.setCouponApplyCateList(
                    couponApplyCateService.list(
                            new LambdaQueryWrapper<ShopCouponApplyCate>()
                                    .eq(ShopCouponApplyCate::getCouponId, coupon.getId())
                    )
            );
            coupon.setCouponApplyItemList(
                    couponApplyItemService.list(
                            new LambdaQueryWrapper<ShopCouponApplyItem>()
                                    .eq(ShopCouponApplyItem::getCouponId, coupon.getId())
                    )
            );
        }
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<ShopCoupon> listRel(ShopCouponParam param) {
        List<ShopCoupon> list = baseMapper.selectListRel(param);
        // 排序
        PageParam<ShopCoupon, ShopCouponParam> page = new PageParam<>();
        page.setDefaultOrder("sort_number asc, create_time desc");
        return page.sortRecords(list);
    }

    @Override
    public ShopCoupon getByIdRel(Integer id) {
        ShopCouponParam param = new ShopCouponParam();
        param.setId(id);
        return param.getOne(baseMapper.selectListRel(param));
    }

}
