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.core.utils.FileUtils;
import com.sckw.product.dao.KwpGoodsImageMapper;
import com.sckw.product.model.KwpGoodsImage;
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 KwpGoodsImageService {

    private final KwpGoodsImageMapper kwpGoodsImageMapper;

    /**
     * @desc: 批量插入
     * @author: yzc
     * @date: 2023-07-05 14:31
     * @Param list:
     * @return: void
     */
    public void insertBatch(List<KwpGoodsImage> list) {
        kwpGoodsImageMapper.insertBatch(list);
    }

    /**
     * @desc: 通过商品id获取
     * @author: yzc
     * @date: 2023-07-05 14:34
     * @Param goodsId:
     * @return: java.util.List<com.sckw.product.model.KwpGoodsImage>
     */
    public List<KwpGoodsImage> getByGoodsId(Long goodsId) {
        LambdaQueryWrapper<KwpGoodsImage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwpGoodsImage::getGoodsId, goodsId).eq(KwpGoodsImage::getDelFlag, Global.NO)
                .orderByAsc(KwpGoodsImage::getSort);
        List<KwpGoodsImage> list = kwpGoodsImageMapper.selectList(wrapper);
        //为每张图片地址加上域名前缀
        list.forEach(e -> e.setImage(FileUtils.splice(e.getImage())));
        return CollectionUtils.emptyIfNull(list);
    }

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