package com.sckw.product.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sckw.core.model.constant.Global;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.product.dao.KwpGoodsPriceRangeMapper;
import com.sckw.product.model.KwpGoodsPriceRange;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @desc: 商品-价格段service
 * @author: yzc
 * @date: 2023-06-25 14:47
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class KwpGoodsPriceRangeService {

    private final KwpGoodsPriceRangeMapper kwpGoodsPriceRangeMapper;

    /**
     * @desc: 批量插入
     * @author: yzc
     * @date: 2023-07-05 11:24
     * @Param list:
     * @return: void
     */
    public void insertBatch(List<KwpGoodsPriceRange> list) {
        kwpGoodsPriceRangeMapper.insertBatch(list);
    }

    /**
     * @desc: 通过商品id获取
     * @author: yzc
     * @date: 2023-07-05 14:42
     * @Param goodsId:
     * @return: java.util.List<com.sckw.product.model.KwpGoodsPriceRange>
     */
    public List<KwpGoodsPriceRange> getByGoodsId(Long goodsId) {
        LambdaQueryWrapper<KwpGoodsPriceRange> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwpGoodsPriceRange::getGoodsId, goodsId).eq(KwpGoodsPriceRange::getDelFlag, Global.NO)
                .orderByAsc(KwpGoodsPriceRange::getSort);
        return kwpGoodsPriceRangeMapper.selectList(wrapper);
    }

    /**
     * @desc: 根据商品id删除
     * @author: yzc
     * @date: 2023-07-06 15:04
     * @Param goodsId:
     * @return: void
     */
    public void delByGoodsIds(List<Long> goodsIds) {
        LambdaUpdateWrapper<KwpGoodsPriceRange> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(KwpGoodsPriceRange::getGoodsId, goodsIds).eq(KwpGoodsPriceRange::getDelFlag, Global.NO)
                .set(KwpGoodsPriceRange::getDelFlag, Global.YES);
        kwpGoodsPriceRangeMapper.update(null, wrapper);
    }

    /**
     * @desc: 通过商品ids获取
     * @author: yzc
     * @date: 2023-07-06 15:59
     * @Param goodsIds:
     * @return: java.util.List<com.sckw.product.model.KwpGoodsPriceRange>
     */
    public List<KwpGoodsPriceRange> getByGoodsIds(List<Long> goodsIds) {
        LambdaQueryWrapper<KwpGoodsPriceRange> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(KwpGoodsPriceRange::getGoodsId, goodsIds).eq(KwpGoodsPriceRange::getDelFlag, Global.NO)
                .orderByAsc(KwpGoodsPriceRange::getPrice);
        List<KwpGoodsPriceRange> list = kwpGoodsPriceRangeMapper.selectList(wrapper);
        return CollectionUtils.emptyIfNull(list);
    }
}
