package com.sckw.order.serivce;

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.order.dao.KwoTradeOrderGoodsMapper;
import com.sckw.order.model.KwoTradeOrderGoods;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @desc: 交易订单商品信息service
 * @author: yzc
 * @date: 2023-06-25 14:28
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class KwoTradeOrderGoodsService {

    private final KwoTradeOrderGoodsMapper kwoTradeOrderGoodsMapper;

    /**
     * @desc: 插入
     * @author: yzc
     * @date: 2023-07-13 9:51
     * @Param goods:
     * @return: void
     */
    public void insert(KwoTradeOrderGoods goods) {
        kwoTradeOrderGoodsMapper.insert(goods);
    }

    /**
     * @desc: 根据订单id获取
     * @author: yzc
     * @date: 2023-07-13 17:43
     * @Param orderId:
     * @return: com.sckw.order.model.KwoTradeOrderGoods
     */
    public KwoTradeOrderGoods getByOrderId(Long orderId) {
        LambdaQueryWrapper<KwoTradeOrderGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwoTradeOrderGoods::getTOrderId, orderId).eq(KwoTradeOrderGoods::getDelFlag, Global.NO).last("LIMIT 1");
        return kwoTradeOrderGoodsMapper.selectOne(wrapper);
    }

    /**
     * @desc: 根据orderId删除
     * @author: yzc
     * @date: 2023-07-14 14:49
     * @Param orderId:
     * @return: void
     */
    public void delByOrderId(Long orderId) {
        LambdaUpdateWrapper<KwoTradeOrderGoods> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(KwoTradeOrderGoods::getTOrderId, orderId).eq(KwoTradeOrderGoods::getDelFlag, Global.NO)
                .set(KwoTradeOrderGoods::getDelFlag, Global.YES);
        kwoTradeOrderGoodsMapper.update(null, wrapper);
    }

    /**
     * @desc: 根据orderIds删除
     * @author: yzc
     * @date: 2023-07-18 16:48
     * @Param orderIds:
     * @return: void
     */
    public void delByOrderIds(List<Long> orderIds) {
        LambdaUpdateWrapper<KwoTradeOrderGoods> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(KwoTradeOrderGoods::getTOrderId, orderIds).eq(KwoTradeOrderGoods::getDelFlag, Global.NO)
                .set(KwoTradeOrderGoods::getDelFlag, Global.YES);
        kwoTradeOrderGoodsMapper.update(null, wrapper);
    }

    /**
     * @desc: 更新商品价格
     * @author: yzc
     * @date: 2023-09-12 15:52
     * @Param orderId:
     * @Param unitPrice:
     * @return: void
     */
    public void updateUnitPrice(Long orderId, BigDecimal unitPrice) {
        LambdaUpdateWrapper<KwoTradeOrderGoods> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(KwoTradeOrderGoods::getTOrderId, orderId).eq(KwoTradeOrderGoods::getDelFlag, Global.NO)
                .set(KwoTradeOrderGoods::getUnitPrice, unitPrice);
        kwoTradeOrderGoodsMapper.update(null, wrapper);
    }
}
