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.KwpGoodsAddressMapper;
import com.sckw.product.model.KwpGoodsAddress;
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 KwpGoodsAddressService {

    private final KwpGoodsAddressMapper kwpGoodsAddressMapper;

    /**
     * @desc: 添加商品地址信息
     * @author: yzc
     * @date: 2023-07-05 11:16
     * @Param address:
     * @return: void
     */
    public void insert(KwpGoodsAddress address) {
        kwpGoodsAddressMapper.insert(address);
    }


    /**
     * @desc: 通过商品id获取
     * @author: yzc
     * @date: 2023-07-06 15:53
     * @Param goodsId:
     * @return: com.sckw.product.model.KwpGoodsAddress
     */
    public KwpGoodsAddress getByGoodsId(Long goodsId) {
        LambdaQueryWrapper<KwpGoodsAddress> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwpGoodsAddress::getGoodsId, goodsId).eq(KwpGoodsAddress::getDelFlag, Global.NO).last("LIMIT 1");
        return kwpGoodsAddressMapper.selectOne(wrapper);
    }

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

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