package com.gxwebsoft.shop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.shop.mapper.UserCouponMapper;
import com.gxwebsoft.shop.service.CouponService;
import com.gxwebsoft.shop.service.UserCouponService;
import com.gxwebsoft.shop.entity.UserCoupon;
import com.gxwebsoft.shop.param.UserCouponParam;
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-12-03 13:47:42
 */
@Service
public class UserCouponServiceImpl extends ServiceImpl<UserCouponMapper, UserCoupon> implements UserCouponService {
    @Resource
    private CouponService couponService;

    @Override
    public PageResult<UserCoupon> pageRel(UserCouponParam param) {
        PageParam<UserCoupon, UserCouponParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        List<UserCoupon> list = baseMapper.selectPageRel(page, param);
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<UserCoupon> listRel(UserCouponParam param) {
        List<UserCoupon> list = baseMapper.selectListRel(param);
        // 排序
        PageParam<UserCoupon, UserCouponParam> page = new PageParam<>();
        page.setDefaultOrder("create_time desc");
        for (UserCoupon userCoupon : list) {
            userCoupon.setCoupon(couponService.getByIdRel(userCoupon.getCouponId()));
        }
        return page.sortRecords(list);
    }

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

}
