package com.yifu.cloud.plus.v1.yifu.common.dapr.util;

import com.alibaba.fastjson.JSON;
import com.yifu.cloud.plus.v1.csp.vo.EmployeeRegistrationCustomerUserUpdateVo;
import com.yifu.cloud.plus.v1.yifu.archives.vo.EmployeeRegistrationPreVo;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.SecurityConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.BaseSearchListVO;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.BaseSearchVO;
import com.yifu.cloud.plus.v1.yifu.common.dapr.config.DaprInsurancesProperties;
import com.yifu.cloud.plus.v1.yifu.insurances.vo.TEmployeeInsurancePreVo;
import com.yifu.cloud.plus.v1.yifu.insurances.vo.TEmployeeInsuranceSelectVo;
import com.yifu.cloud.plus.v1.yifu.social.vo.ChangeDeptVo;
import com.yifu.cloud.plus.v1.yifu.social.vo.TDispatchReduceVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author hgw
 * @Date 2022-11-17 10:16:59
 * @Description
 * @Version 1.0
 */
@Log4j2
@EnableConfigurationProperties(DaprInsurancesProperties.class)
public class InsuranceDaprUtil {
	@Autowired
	private DaprInsurancesProperties daprInsurancesProperties;
	/**
	 * @Author hgw
	 * @Description ekp费用划转，商险划转项目
	 * @Date 2022-11-17 11:03:08
	 * @Param
	 * @return
	**/
	public void ekpUpdateInsuranceDept(ChangeDeptVo changeDeptVo){
		HttpDaprUtil.invokeMethodPost(daprInsurancesProperties.getAppUrl()
				, daprInsurancesProperties.getAppId(),"/insuranceDetail/inner/ekpUpdateInsuranceDept" , changeDeptVo
				, Object.class, SecurityConstants.FROM_IN);
	}

	/**
	 * @Author huyc
	 * @Description 新增商险待购买信息
	 * @Date 14:34 2025/4/3
	 * @Param
	 * @return
	 **/
	public R<Boolean> saveInsurancePreInfo(EmployeeRegistrationPreVo preVo) {
		return HttpDaprUtil.invokeMethodPost(daprInsurancesProperties.getAppUrl(),daprInsurancesProperties.getAppId(),
				"/temployeeinsurancepre/inner/saveInsurancePreInfo", JSON.toJSONString(preVo), Boolean.class, SecurityConstants.FROM_IN);
	}

	/**
	 * @Author huyc
	 * @Description 查询商险确认信息
	 * @Date 14:34 2025/4/8
	 * @Param
	 * @return
	 **/
	public R<TEmployeeInsuranceSelectVo> selectInsurancePreInfoList(TEmployeeInsurancePreVo preVo) {
		return HttpDaprUtil.invokeMethodPost(daprInsurancesProperties.getAppUrl(),daprInsurancesProperties.getAppId(),
				"/temployeeinsurancepre/inner/selectInsurancePreInfoList", JSON.toJSONString(preVo), TEmployeeInsuranceSelectVo.class, SecurityConstants.FROM_IN);
	}

	/**
	 * @Author huyc
	 * @Description 查询商险信息
	 * @Date 14:34 2025/4/8
	 * @Param
	 * @return
	 **/
	public R<TEmployeeInsuranceSelectVo> selectInsuranceInfo(TEmployeeInsurancePreVo preVo) {
		return HttpDaprUtil.invokeMethodPost(daprInsurancesProperties.getAppUrl(),daprInsurancesProperties.getAppId(),
				"/insuranceDetail/inner/selectInsuranceInfo", JSON.toJSONString(preVo), TEmployeeInsuranceSelectVo.class, SecurityConstants.FROM_IN);
	}

	/**
	 * @Author huych
	 * @Description 更新商险待购买的前端客服姓名
	 * @Date 2025-4-9 9:25:12
	 * @Param
	 * @return
	 **/
	public void updateInsurancePreCustomerName(EmployeeRegistrationCustomerUserUpdateVo updateVo){
		HttpDaprUtil.invokeMethodPost(daprInsurancesProperties.getAppUrl()
				, daprInsurancesProperties.getAppId(),"/temployeeinsurancepre/inner/updateInsurancePreCustomerName" , updateVo
				, Object.class, SecurityConstants.FROM_IN);
	}

	public R<Boolean> checkExitInsuanceIsProcess(EmployeeRegistrationPreVo preVo) {
		return HttpDaprUtil.invokeMethodPost(daprInsurancesProperties.getAppUrl(),daprInsurancesProperties.getAppId(),
				"/temployeeinsurancepre/inner/checkExitInsuanceIsProcess", JSON.toJSONString(preVo), Boolean.class, SecurityConstants.FROM_IN);
	}

	/**
	 * @param paramVo 查询参数，主要是身份证与项目编码
	 * @Description: 查询人员的商险状态
	 * @Author: hgw
	 * @Date: 2025/11/20 15:50
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R<com.yifu.cloud.plus.v1.yifu.common.core.vo.BaseSearchVO>
	 **/
	public R<BaseSearchVO> getInsurancesStatus(BaseSearchVO paramVo) {
		R<BaseSearchVO> res = HttpDaprUtil.invokeMethodPost(daprInsurancesProperties.getAppUrl(),daprInsurancesProperties.getAppId()
				,"/insuranceDetail/inner/getInsurancesStatus", paramVo, BaseSearchVO.class, SecurityConstants.FROM_IN);
		if (Common.isEmpty(res)){
			return R.failed("查询人员的商险状态失败！");
		}
		return res;
	}
	/**
	 * @Author fxj
	 * @Description 通过如入职ID删除商险待办数据
	 * @Date 20:28 2026/1/22
	 * @Param
	 * @return
	**/
	public R<Boolean> delInsurancePreByRegisterId(BaseSearchVO paramVo) {
		R<Boolean> res = HttpDaprUtil.invokeMethodPost(daprInsurancesProperties.getAppUrl(),daprInsurancesProperties.getAppId()
				,"/temployeeinsurancepre/inner/delInsurancePreByRegisterId", paramVo, Boolean.class, SecurityConstants.FROM_IN);
		if (Common.isEmpty(res)){
			return R.failed("通过如入职ID删除商险待办数据！");
		}
		return res;
	}

	/**
	 * @Author fxj
	 * @Description 拒绝入职状态更新
	 * @Date 20:28 2026/1/22
	 * @Param
	 * @return
	 **/
	public R<Boolean> refuseInsurancePreByRegisterId(BaseSearchVO paramVo) {
		R<Boolean> res = HttpDaprUtil.invokeMethodPost(daprInsurancesProperties.getAppUrl(),daprInsurancesProperties.getAppId()
				,"/temployeeinsurancepre/inner/refuseInsurancePreByRegisterId", paramVo, Boolean.class, SecurityConstants.FROM_IN);
		if (Common.isEmpty(res)){
			return R.failed("拒绝入职更新商险待办数据！");
		}
		return res;
	}
	public R<Boolean> leaveInsurancePreByRegisterId(BaseSearchVO paramVo) {
		R<Boolean> res = HttpDaprUtil.invokeMethodPost(daprInsurancesProperties.getAppUrl(),daprInsurancesProperties.getAppId()
				,"/temployeeinsurancepre/inner/leaveInsurancePreByRegisterId", paramVo, Boolean.class, SecurityConstants.FROM_IN);
		if (Common.isEmpty(res)){
			return R.failed("离职确认更新商险待办数据！");
		}
		return res;
	}
	/**
	 * @param paramVo 查询参数，主要是身份证与项目编码
	 * @Description: 查询人员的已减完成的商险（返回值，则表示有未减完成的商险
	 * @Author: hgw
	 * @Date: 2025/11/20 15:50
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R<com.yifu.cloud.plus.v1.yifu.common.core.vo.BaseSearchVO>
	 **/
	public R<BaseSearchVO> getInsurancesStatusByClose(BaseSearchVO paramVo) {
		R<BaseSearchVO> res = HttpDaprUtil.invokeMethodPost(daprInsurancesProperties.getAppUrl(),daprInsurancesProperties.getAppId()
				,"/insuranceDetail/inner/getInsurancesStatusByClose", paramVo, BaseSearchVO.class, SecurityConstants.FROM_IN);
		if (Common.isEmpty(res)){
			return R.failed("查询人员的已减完成的商险！");
		}
		return res;
	}

	/**
	 * @param paramVo 查询参数，主要是身份证与项目编码
	 * @Description: 离职待办确认后，更新为商险续签待办为已离职
	 * @Author: hgw
	 * @Date: 2025-11-21 14:28:20
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R<com.yifu.cloud.plus.v1.yifu.common.core.vo.BaseSearchVO>
	 **/
	public void updateInsurancePreRenewToIsLeave(BaseSearchVO paramVo) {
		HttpDaprUtil.invokeMethodPost(daprInsurancesProperties.getAppUrl(),daprInsurancesProperties.getAppId()
				,"/tinsuranceprerenewdetail/inner/updateInsurancePreRenewToIsLeave", paramVo, String.class, SecurityConstants.FROM_IN);
	}

	/**
	 * @param paramVo 查询参数，主要是身份证与项目编码
	 * @Description: 离职待办确认后，更新为商险续签待办为已离职
	 * @Author: hgw
	 * @Date: 2025-11-21 14:28:20
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R<com.yifu.cloud.plus.v1.yifu.common.core.vo.BaseSearchVO>
	 **/
	public void updateInsurancePreRenewToIsLeaveList(BaseSearchListVO paramVo) {
		HttpDaprUtil.invokeMethodPost(daprInsurancesProperties.getAppUrl(),daprInsurancesProperties.getAppId()
				,"/tinsuranceprerenewdetail/inner/updateInsurancePreRenewToIsLeaveList", paramVo, String.class, SecurityConstants.FROM_IN);
	}

	/**
	 * @param paramVo 查询参数，主要是身份证与项目编码
	 * @Description: 离职待办调用的批量派减商险
	 * @Author: hgw
	 * @Date: 2025-11-21 14:54:37
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R<com.yifu.cloud.plus.v1.yifu.common.core.vo.BaseSearchVO>
	 **/
	public R<BaseSearchListVO> leaveToReduceInsurance(BaseSearchListVO paramVo) {
		R<BaseSearchListVO> res = HttpDaprUtil.invokeMethodPost(daprInsurancesProperties.getAppUrl(),daprInsurancesProperties.getAppId()
				,"/insuranceDetail/inner/leaveToReduceInsurance", paramVo, BaseSearchListVO.class, SecurityConstants.FROM_IN);
		if (Common.isEmpty(res)){
			return R.failed("离职待办调用的批量派减商险失败！");
		}
		return res;
	}

	/**
	 * @param paramVoList 查询参数列表（身份证 + 项目编码）
	 * @param operator 操作人员
	 * @param operateType 操作人ID
	 * @param operateType 操作类型
	 * @Description: 批量更新商险替换标签和说明（商险服务侧实现完整逻辑）
	 * @Author: fxj
	 * @Date: 2026-03-19
	 * @return: void
	 **/
	public void batchUpdateInsuranceReplaceTagByParamList(List<BaseSearchVO> paramVoList, String operator,String operatorId, String operateType) {
		// 构建请求参数
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("paramVoList", paramVoList);
		paramMap.put("operator", operator);
		paramMap.put("operatorId", operatorId);
		paramMap.put("operateType", operateType);
		
		HttpDaprUtil.invokeMethodPost(daprInsurancesProperties.getAppUrl(),daprInsurancesProperties.getAppId()
				,"/insuranceDetail/inner/batchUpdateInsuranceReplaceTagByParamList", paramMap, Object.class, SecurityConstants.FROM_IN);
	}

	/**
	 * @Description: 查询是否有处理中的自动化的商险
	 * @Author: hgw
	 * @Date: 2026-03-30
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R<java.lang.Boolean>
	 **/
	public R<Boolean> judgeHaveProcessingInsurance(BaseSearchVO paramVo){
		R<Boolean> res = HttpDaprUtil.invokeMethodPost(
				daprInsurancesProperties.getAppUrl(),daprInsurancesProperties.getAppId()
				,"/temployeeinsurancepre/inner/judgeHaveProcessingInsurance"
				, paramVo, Boolean.class, SecurityConstants.FROM_IN);
		if (Common.isEmpty(res)){
			return R.failed("查询是否有处理中的自动化的商险失败！");
		}
		return res;
	}

}
