package com.jwipc.sys.logic.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jwipc.api.constant.RestCode;
import com.jwipc.api.dto.ApiResult;
import com.jwipc.sys.dto.ExchangeRateApiDTO;
import com.jwipc.sys.dto.ExchangeRateItemDTO;
import com.jwipc.sys.entity.ExchangeRate;
import com.jwipc.sys.logic.ExchangeRateApiLogic;
import com.jwipc.sys.service.IExchangeRateService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


/**
 * @author shenyang.ou
 * @date 2024/4/3 9:52
 * @desc
 */
@Service
@RequiredArgsConstructor
public class ExchangeRateApiLogicImpl implements ExchangeRateApiLogic {
    private final IExchangeRateService exchangeRateService;
    @Override
    public ApiResult sync(ExchangeRateItemDTO param) {
        List<ExchangeRateApiDTO> exchangeRateApiDTOS = param.getExchangeRateApiDTOS();
        List<ExchangeRate> exchangeRates = new ArrayList<>();
        try {
            for (ExchangeRateApiDTO exchangeRateApiDTO : exchangeRateApiDTOS){
                if (StringUtils.isEmpty(exchangeRateApiDTO.getExchangeType())){
                    return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("请传入汇率类型").build();
                }
                if (StringUtils.isEmpty(exchangeRateApiDTO.getOriginalCurrency())){
                    return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("请传入从货币").build();
                }
                if (StringUtils.isEmpty(exchangeRateApiDTO.getMonthDate())){
                    return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("请传入汇率有效起始日期").build();
                }
                if (exchangeRateApiDTO.getExchange()==null){
                    return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("请传入汇率").build();
                }
                ExchangeRate exchangeRate = new ExchangeRate();
                exchangeRate.setExchangeType(exchangeRateApiDTO.getExchangeType());
                exchangeRate.setOriginalCurrency(exchangeRateApiDTO.getOriginalCurrency());
                exchangeRate.setMonthDate(exchangeRateApiDTO.getMonthDate());
                exchangeRate.setConvertExchange(exchangeRateApiDTO.getConvertExchange());
                exchangeRate.setExchange(exchangeRateApiDTO.getExchange());
                ExchangeRate oldExchangeRate = exchangeRateService.getOne(Wrappers.lambdaQuery(ExchangeRate.class)
                        .eq(ExchangeRate::getExchangeType,exchangeRateApiDTO.getExchangeType())
                        .eq(ExchangeRate::getOriginalCurrency,exchangeRateApiDTO.getOriginalCurrency())
                        .eq(ExchangeRate::getConvertExchange,exchangeRateApiDTO.getConvertExchange())
                        .eq(ExchangeRate::getMonthDate,exchangeRateApiDTO.getMonthDate()).last(" limit 1")
                );
                if (ObjectUtil.isNotNull(oldExchangeRate)){
                    exchangeRate.setId(oldExchangeRate.getId());
                    exchangeRate.setUpdateTime(LocalDateTime.now());
                }else {
                    exchangeRate.setCreateTime(LocalDateTime.now());
                }
                exchangeRates.add(exchangeRate);
            }
            exchangeRateService.saveOrUpdateBatch(exchangeRates);
        }catch (Exception e){
            e.printStackTrace();
            return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message(e.getMessage()).build();
        }
        return ApiResult.builder().code(RestCode.SAP_SUCCESS.getCode()).message("成功").build();
    }
}
