package com.czg.service.market.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.czg.market.dto.MkCouponGiftDTO;
import com.czg.market.entity.MkCouponGift;
import com.czg.market.entity.ShopCoupon;
import com.czg.market.service.MkCouponGiftService;
import com.czg.service.market.mapper.MkCouponGiftMapper;
import com.czg.service.market.mapper.ShopCouponMapper;
import com.czg.utils.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 券赠送关联表 服务层实现。
 *
 * @author ww
 * @since 2025-09-12
 */
@Service
public class MkCouponGiftServiceImpl extends ServiceImpl<MkCouponGiftMapper, MkCouponGift> implements MkCouponGiftService {

    @Resource
    private ShopCouponMapper couponMapper;

    @Override
    public Page<MkCouponGiftDTO> getCouponGiftPage(Long couponId, Integer type) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(MkCouponGift::getCouponId, couponId)
                .eq(MkCouponGift::getType, type)
                .orderBy(MkCouponGift::getId).desc();
        return pageAs(PageUtil.buildPage(), queryWrapper, MkCouponGiftDTO.class);
    }

    @Override
    public List<MkCouponGiftDTO> getCouponGiftBySourceId(Long sourceId, Integer type) {
        return listAs(new QueryWrapper().eq(MkCouponGift::getSourceId, sourceId).eq(MkCouponGift::getType, type), MkCouponGiftDTO.class);
    }

    @Transactional
    @Override
    public void addCouponGift(Long sourceId, String sourceName, Integer type, List<MkCouponGiftDTO> couponGiftList) {
        remove(new QueryWrapper().eq(MkCouponGift::getSourceId, sourceId).eq(MkCouponGift::getType, type));
        if (CollUtil.isNotEmpty(couponGiftList)) {
            List<MkCouponGift> newCoupons = new ArrayList<>();
            for (MkCouponGiftDTO giftDTO : couponGiftList) {
                ShopCoupon coupon = couponMapper.selectOneByQuery(new QueryWrapper().eq(ShopCoupon::getId, giftDTO.getCouponId()));
                if (coupon == null) {
                    continue;
                }
                MkCouponGift couponGift = new MkCouponGift();
                couponGift.setSourceId(sourceId);
                couponGift.setSourceName(sourceName);
                couponGift.setType(type);
                couponGift.setCouponName(coupon.getTitle());
                couponGift.setCouponId(giftDTO.getCouponId());
                couponGift.setNum(giftDTO.getNum());
                newCoupons.add(couponGift);
            }
            mapper.insertBatchSelective(newCoupons, 50);

        }
    }

    @Override
    public void upCouponGift(Long sourceId, String sourceName, Integer type, List<MkCouponGiftDTO> couponGiftList) {
        deleteJoinCouponGift(sourceId, type);
        addCouponGift(sourceId, sourceName, type, couponGiftList);
    }

    @Override
    public void cancelGift(Long id) {
        removeById(id);
    }

    @Override
    public void deleteJoinCouponGift(Long sourceId, Integer type) {
        remove(new QueryWrapper().eq(MkCouponGift::getSourceId, sourceId).eq(MkCouponGift::getType, type));
    }

    @Override
    public void deleteCoupon(Long couponId) {
        remove(new QueryWrapper().eq(MkCouponGift::getCouponId, couponId));
    }

    @Override
    public void upCouponName(Long couponId, String couponName) {
        update(new MkCouponGift().setCouponName(couponName), new QueryWrapper().eq(MkCouponGift::getCouponId, couponId));
    }
}
