package com.ruoyi.loanpricing.service;

import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.loanpricing.domain.dto.ModelInvokeDTO;
import com.ruoyi.loanpricing.domain.entity.LoanPricingWorkflow;
import com.ruoyi.loanpricing.domain.entity.ModelCorpOutputFields;
import com.ruoyi.loanpricing.domain.entity.ModelRetailOutputFields;
import com.ruoyi.loanpricing.mapper.LoanPricingWorkflowMapper;
import com.ruoyi.loanpricing.mapper.ModelCorpOutputFieldsMapper;
import com.ruoyi.loanpricing.mapper.ModelRetailOutputFieldsMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @Author: wkc
 * @CreateTime: 2026-01-21
 */
@Service
@Slf4j
@EnableAsync
public class LoanPricingModelService {

    @Resource
    private ModelService modelService;

    @Resource
    private LoanPricingWorkflowMapper loanPricingWorkflowMapper;

    @Resource
    private ModelRetailOutputFieldsMapper modelRetailOutputFieldsMapper;

    @Resource
    private ModelCorpOutputFieldsMapper modelCorpOutputFieldsMapper;

    public void invokeModelAsync(Long workflowId) {
        invokeModelAndSave(workflowId, false);
    }

    public void reinvokeModelAndOverwrite(Long workflowId) {
        invokeModelAndSave(workflowId, true);
    }

    private void invokeModelAndSave(Long workflowId, boolean overwriteModelOutput) {
        LoanPricingWorkflow loanPricingWorkflow = loanPricingWorkflowMapper.selectById(workflowId);
        if (Objects.isNull(loanPricingWorkflow)){
            log.error("未找到对应的流程信息，未调用模型服务");
            return;
        }
        ModelInvokeDTO modelInvokeDTO = new ModelInvokeDTO();
        BeanUtils.copyProperties(loanPricingWorkflow, modelInvokeDTO);
        if ("个人".equals(loanPricingWorkflow.getCustType()))
        {
            normalizePersonalModelInvokeDTO(modelInvokeDTO);
        }
        if ("企业".equals(loanPricingWorkflow.getCustType()))
        {
            normalizeCorporateModelInvokeDTO(modelInvokeDTO);
        }
        if (loanPricingWorkflow.getCustType().equals("个人")){
            // 个人模型
            ModelRetailOutputFields modelRetailOutputFields = modelService.invokePersonalModel(modelInvokeDTO);
            if (overwriteModelOutput && Objects.nonNull(loanPricingWorkflow.getModelOutputId()))
            {
                modelRetailOutputFields.setId(loanPricingWorkflow.getModelOutputId());
                modelRetailOutputFieldsMapper.updateById(modelRetailOutputFields);
            }
            else
            {
                modelRetailOutputFieldsMapper.insert(modelRetailOutputFields);
            }
            log.info("个人模型调用成功");
            updateWorkflowModelOutputId(loanPricingWorkflow, modelRetailOutputFields.getId());
        }else if (loanPricingWorkflow.getCustType().equals("企业")){
            // 企业模型
            ModelCorpOutputFields modelCorpOutputFields = modelService.invokeCorporateModel(modelInvokeDTO);
            if (overwriteModelOutput && Objects.nonNull(loanPricingWorkflow.getModelOutputId()))
            {
                modelCorpOutputFields.setId(loanPricingWorkflow.getModelOutputId());
                modelCorpOutputFieldsMapper.updateById(modelCorpOutputFields);
            }
            else
            {
                modelCorpOutputFieldsMapper.insert(modelCorpOutputFields);
            }
            log.info("企业模型调用成功");
            updateWorkflowModelOutputId(loanPricingWorkflow, modelCorpOutputFields.getId());
        }
    }

    private void updateWorkflowModelOutputId(LoanPricingWorkflow loanPricingWorkflow, Long modelOutputId)
    {
        LoanPricingWorkflow workflowToUpdate = new LoanPricingWorkflow();
        workflowToUpdate.setId(loanPricingWorkflow.getId());
        workflowToUpdate.setModelOutputId(modelOutputId);
        loanPricingWorkflowMapper.updateById(workflowToUpdate);
        log.info("更新流程信息成功");
    }

    private void normalizePersonalModelInvokeDTO(ModelInvokeDTO modelInvokeDTO)
    {
        modelInvokeDTO.setBizProof(toZeroOne(modelInvokeDTO.getBizProof()));
        modelInvokeDTO.setLoanLoop(toZeroOne(modelInvokeDTO.getLoanLoop()));
        modelInvokeDTO.setCollThirdParty(toZeroOne(modelInvokeDTO.getCollThirdParty()));
    }

    private void normalizeCorporateModelInvokeDTO(ModelInvokeDTO modelInvokeDTO)
    {
        modelInvokeDTO.setCollThirdParty(toZeroOne(modelInvokeDTO.getCollThirdParty()));
        modelInvokeDTO.setResCover(toZeroOne(modelInvokeDTO.getResCover()));
        modelInvokeDTO.setIsGreenLoan(toZeroOne(modelInvokeDTO.getIsGreenLoan()));
        modelInvokeDTO.setIsTradeBuildEnt(toZeroOne(modelInvokeDTO.getIsTradeBuildEnt()));
    }

    private String toZeroOne(String value)
    {
        if ("true".equals(value) || "1".equals(value))
        {
            return "1";
        }
        if ("false".equals(value) || "0".equals(value))
        {
            return "0";
        }
        return value;
    }
}
