package com.sckw.order.serivce;

import cn.hutool.core.collection.CollUtil;
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.KwoTradeOrderAmountMapper;
import com.sckw.order.model.KwoTradeOrderAmount;
import com.sckw.order.model.vo.req.UpdateAddressAmountParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author: yzc
 * @date: 2024-03-04  16:57
 * @desc: 交易订单装卸货地址委派及交付量信息service
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class KwoTradeOrderAmountService {

    private final KwoTradeOrderAmountMapper tradeOrderAmountMapper;

    /**
     * @param orderAmount
     * @return void
     * @desc 添加
     * @author yzc
     * @date 2024/3/5 15:04
     */
    public void add(KwoTradeOrderAmount orderAmount) {
        tradeOrderAmountMapper.insert(orderAmount);
    }

    /**
     * @param orderId
     * @return void
     * @desc 根据贸易订单id删除
     * @author yzc
     * @date 2024/3/5 15:04
     */
    public void delByOrderId(Long orderId) {
        LambdaUpdateWrapper<KwoTradeOrderAmount> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(KwoTradeOrderAmount::getTOrderId, orderId).eq(KwoTradeOrderAmount::getDelFlag, Global.NO)
                .set(KwoTradeOrderAmount::getDelFlag, Global.YES);
        tradeOrderAmountMapper.update(null, wrapper);
    }

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

    /**
     * @param orderId
     * @param addressIds
     * @return void
     * @desc 根据订单id及地址ids删除
     * @author yzc
     * @date 2024/3/12 13:45
     */
    public void delByOrderIdAndAddressIds(Long orderId, List<Long> addressIds) {
        LambdaUpdateWrapper<KwoTradeOrderAmount> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(KwoTradeOrderAmount::getTOrderId, orderId)
                .in(KwoTradeOrderAmount::getTAddressId, addressIds)
                .eq(KwoTradeOrderAmount::getDelFlag, Global.NO)
                .set(KwoTradeOrderAmount::getDelFlag, Global.YES);
        tradeOrderAmountMapper.update(null, wrapper);
    }

    /**
     * @desc: 根据orderId获取
     * @author: yzc
     * @date: 2023-07-18 11:24
     * @Param torderId:
     * @return: java.util.List<com.sckw.order.model.KwoTradeOrderAmount>
     */
    public List<KwoTradeOrderAmount> getByOrderId(Long tOrderId) {
        LambdaQueryWrapper<KwoTradeOrderAmount> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwoTradeOrderAmount::getTOrderId, tOrderId).eq(KwoTradeOrderAmount::getDelFlag, Global.NO);
        List<KwoTradeOrderAmount> list = tradeOrderAmountMapper.selectList(wrapper);
        return CollUtil.emptyIfNull(list);
    }

    /**
     * @param param
     * @return void
     * @desc 更新
     * @author yzc
     * @date 2024/3/12 11:45
     */
    public void update(UpdateAddressAmountParam param) {
        LambdaUpdateWrapper<KwoTradeOrderAmount> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(KwoTradeOrderAmount::getAmount, param.getAmount())
                .eq(KwoTradeOrderAmount::getId, param.getId());
        tradeOrderAmountMapper.update(null, wrapper);
    }

    public KwoTradeOrderAmount getByOrderIdAndTaddressId(Long tOrderId, Long tAddressId) {
        LambdaQueryWrapper<KwoTradeOrderAmount> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwoTradeOrderAmount::getTOrderId, tOrderId)
                .eq(KwoTradeOrderAmount::getTAddressId,tAddressId)
                .eq(KwoTradeOrderAmount::getDelFlag, Global.NO);
        return tradeOrderAmountMapper.selectOne(wrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateById(KwoTradeOrderAmount orderAmount) {
        tradeOrderAmountMapper.updateById(orderAmount);
    }
}