package com.yd.csf.api.service.impl;

import com.yd.common.exception.BusinessException;
import com.yd.common.result.Result;
import com.yd.csf.api.service.ApiCommissionConditionService;
import com.yd.csf.service.model.PolicyFollow;
import com.yd.csf.service.model.PolicyPolicyholder;
import com.yd.csf.service.service.PolicyFollowService;
import com.yd.csf.service.service.PolicyPolicyholderService;
import com.yd.product.feign.client.announcementcommissionratio.ApiAnnouncementCommissionRatioFeignClient;
import com.yd.product.feign.request.announcementcommissionratio.ApiAnnouncementCommissionRatioListRequest;
import com.yd.product.feign.response.announcementcommissionratio.ApiAnnouncementCommissionRatioListResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 佣金条件服务类
 */
@Slf4j
@Service
public class ApiCommissionConditionServiceImpl implements ApiCommissionConditionService {

    @Autowired
    private ApiAnnouncementCommissionRatioFeignClient ratioFeignClient;

    @Autowired
    private PolicyFollowService policyFollowService;

    @Autowired
    private PolicyPolicyholderService policyPolicyholderService;

    /**
     * 根据保单号查询保单所参与过滤产品发佣率数据筛选的字段列表数据
     * @param policyNo
     * @return
     */
    @Override
    public Result<List<ApiAnnouncementCommissionRatioListResponse>> getCommissionRatioList(String policyNo) {
        //根据保单号查询保单跟进表数据
        PolicyFollow policyFollow = policyFollowService.queryOneByPolicyNo(policyNo);
        if (policyFollow == null) {
            throw new BusinessException("新单跟进表信息不存在");
        }
        //列出固定几个需要参与发佣率筛选数据的保单字段
        //供款年期（数字）
        String paymentTerm = policyFollow.getIssueNumber().toString();
        //投保年龄（区间值）查询保单投保人表数据  0-64  64-
        PolicyPolicyholder policyPolicyholder = policyPolicyholderService.queryOne(policyFollow.getPolicyBizId());
        String age = "";
        if (policyPolicyholder != null) {
            age = policyPolicyholder.getAge();
        }
        //期缴保费（区间值）例：0-10000  10000-
        BigDecimal eachIssuePremium = policyFollow.getEachIssuePremium();
        //专业投资者（Yes/No）（选项）
        String professionalInvestor = policyFollow.getProfessionalInvestor();
        //保单币种（选项）
        String policyCurrency = policyFollow.getPolicyCurrency();
        //保障年期（数字）
        String guaranteePeriod = policyFollow.getGuaranteePeriod();
        ApiAnnouncementCommissionRatioListRequest ratioListRequest = new ApiAnnouncementCommissionRatioListRequest();
        ratioListRequest.setProductLaunchBizId(policyFollow.getProductLaunchBizId());
        ratioListRequest.setEffectiveDate(policyFollow.getEffectiveDate());
        ratioListRequest.setPaymentTerm(paymentTerm);
        ratioListRequest.setAge(age);
        ratioListRequest.setEachIssuePremium(eachIssuePremium);
        ratioListRequest.setGuaranteePeriod(guaranteePeriod);
        ratioListRequest.setPolicyCurrency(policyCurrency);
        ratioListRequest.setProfessionalInvestor(professionalInvestor);
        Result<List<ApiAnnouncementCommissionRatioListResponse>> result = ratioFeignClient.list(ratioListRequest);
        return result;
    }


}
