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.EmployeePreSocialListVo;
import com.yifu.cloud.plus.v1.yifu.archives.vo.EmployeeRegistrationPreVo;
import com.yifu.cloud.plus.v1.yifu.archives.vo.TEmployeeAutoRegistCheckVo;
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.DaprSocialProperties;
import com.yifu.cloud.plus.v1.yifu.insurances.vo.EkpInsuranceViewVo;
import com.yifu.cloud.plus.v1.yifu.insurances.vo.EkpSocialViewVo;
import com.yifu.cloud.plus.v1.yifu.insurances.vo.HolidayListVO;
import com.yifu.cloud.plus.v1.yifu.salary.vo.*;
import com.yifu.cloud.plus.v1.yifu.social.entity.TIncomeDetail;
import com.yifu.cloud.plus.v1.yifu.social.entity.TPaymentInfo;
import com.yifu.cloud.plus.v1.yifu.social.vo.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

import java.util.List;

/**
 * @Author fxj
 * @Date 2022-07-18
 * @Description
 * @Version 1.0
 */
@Log4j2
@EnableConfigurationProperties(DaprSocialProperties.class)
public class SocialDaprUtils {

	@Autowired
	private DaprSocialProperties daprProperties;

	/**
	 * @Author fxj
	 * @Description 去缴费库查询查询这些人社保是否存在
	 * @Date 17:38 2022/8/16
	 * @Param
	 * @return
	**/
	public R<HaveSalaryNoSocialVo> getPaymentinfoListByEmpdIdCard(List<String> idCards, String settleMonth) {
		HaveSalaryNoSocialSearchVo searchVo = new HaveSalaryNoSocialSearchVo();
		searchVo.setIdCards(idCards);
		searchVo.setSettleMonth(settleMonth);
		R<HaveSalaryNoSocialVo> noSocialVoR = HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId(),"/tpaymentinfo/inner/listByEmpdIdCard", JSON.toJSONString(searchVo), HaveSalaryNoSocialVo.class, SecurityConstants.FROM_IN);
		if (Common.isEmpty(noSocialVoR)){
			return R.failed("查询人员社保是否存在失败！");
		}
		return noSocialVoR;
	}

	/**
	 * @Author huyc
	 * @Description 去缴费库查询查询这些人社保是否存在
	 * @Date 17:38 2022/8/15
	 * @Param
	 * @return
	 **/
	public void updatePaymentSocialAndFound(UpdateSocialFoundVo infoVo) {
		HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId(),"/tpaymentinfo/inner/updatePaymentSocialAndFound", JSON.toJSONString(infoVo), Object.class, SecurityConstants.FROM_IN);
	}

	/**
	 * @Author huyc
	 * @Description 更新预估库
	 * @Date 2022/8/15
	 * @Param
	 * @return
	 **/
	public void updateForSocialAndFound(UpdateSocialFoundVo infoVo) {
		HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId(),"/tforecastlibrary/inner/updateForSocialAndFound", JSON.toJSONString(infoVo), Object.class, SecurityConstants.FROM_IN);
	}

	/**
	 * @Author huyc
	 * @Description 更新社保公积金明细
	 * @Date 2022/8/15
	 * @Param
	 * @return
	 **/
	public void updateSocialAndFoundInfo(UpdateSocialFoundVo infoVo) {
		HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId(),"/tsocialfundinfo/inner/updateSocialAndFoundInfo", JSON.toJSONString(infoVo), Object.class, SecurityConstants.FROM_IN);
	}

	/**
	 * @Author huyc
	 * @Description 查询人员收入数据
	 * @Date 2022/9/2
	 * @Param
	 * @return
	 **/
	public R<TIncomeDetailReturnVo> getTIncomeDetailList(TIncomeDetail infoVo) {
		R<TIncomeDetailReturnVo> listVo = HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId(),"/tincomedetail/inner/getTIncomeDetailList", JSON.toJSONString(infoVo), TIncomeDetailReturnVo.class, SecurityConstants.FROM_IN);
		if (Common.isEmpty(listVo)){
			return R.failed("查询人员收入数据失败！");
		}
		return listVo;
	}

	/**
	 * @Author huyc
	 * @Description 生成收入数据
	 * @Date 2022/9/2
	 * @Param
	 * @return
	 **/
	public R<Boolean> createTIncomeDetail(TIncomeDetail infoVo) {
		return HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId(),"/tincome/inner/saveDetail", JSON.toJSONString(infoVo), Boolean.class, SecurityConstants.FROM_IN);
	}

	/**
	 * @Author huyc
	 * @Description 查询人员缴费库数据
	 * @Date 2022/9/2
	 * @Param
	 * @return
	 **/
	public R<TPaymentVo> getPaymentSocialAndFound(TPaymentInfo infoVo) {
		R<TPaymentVo> listVo = HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId(),"/tpaymentinfo/inner/getPaymentSocialAndFound", JSON.toJSONString(infoVo), TPaymentVo.class, SecurityConstants.FROM_IN);
		if (Common.isEmpty(listVo)){
			return R.failed("查询人员缴费库数据失败！");
		}
		return listVo;
	}

	/**
	 * @Author huyc
	 * @Description 通过收入id删除收入数据
	 * @Date 2022/9/2
	 * @Param
	 * @return
	 **/
	public void deleteById(TIncomeDetail infoVo) {
		HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId(),"/tincomedetail/inner/deleteById", JSON.toJSONString(infoVo), Object.class, SecurityConstants.FROM_IN);
	}

	/**
	 * @Author fxj
	 * @Description 通过收入id收入数据
	 * @Date 16:56 2026/1/27
	 * @Param
	 * @return
	**/
	public void deleteIncomeById(TIncomeDetail infoVo) {
		HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId(),"/tincome/inner/deleteById", JSON.toJSONString(infoVo), Object.class, SecurityConstants.FROM_IN);
	}
	/**
	 * @Author huyc
	 * @Description 更新结算信息
	 * @Date 17:38 2022/12/26
	 * @Param
	 * @return
	 **/
	public R<Boolean> updateSocialSettleStatus(List<EkpSocialViewVo> infoVo) {
		return HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId(),"/tpaymentinfo/inner/updateSocialSettleStatus", JSON.toJSONString(infoVo), Boolean.class, SecurityConstants.FROM_IN);
	}

	/**
	 * @Author huyc
	 * @Description 更新结算信息
	 * @Date 17:38 2022/12/26
	 * @Param
	 * @return
	 **/
	public R<Boolean> updateIncomeSettleStatus(List<EkpInsuranceViewVo> infoVo) {
		return HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId(),"/tincome/inner/updateIncomeSettleStatus", JSON.toJSONString(infoVo), Boolean.class, SecurityConstants.FROM_IN);
	}

	/**
	 * @Author huyc
	 * @Description 判断是否存在正常的工伤缴费
	 * @Date 16:38 2025/1/15
	 * @Param
	 * @return
	 **/
	public R<Boolean> selectExitSocialInjury(TInsuranceUnpurchasePersonCardVo cardVo) {
		return HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId(),"/tsocialfundinfo/inner/selectExitSocialInjury", JSON.toJSONString(cardVo), Boolean.class, SecurityConstants.FROM_IN);
	}

	/**
	 * @Description: 查询社保已购买的身份证
	 * @Author: hgw
	 * @Date: 2025/1/15 16:11
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R<java.lang.Boolean>
	 **/
	public R<ListStringVo> selectWorkInjuryHandleAll() {
		return HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId()
				,"/tsocialfundinfo/inner/selectWorkInjuryHandleAll", null, ListStringVo.class, SecurityConstants.FROM_IN);
	}

	/**
	 * @Author huyc
	 * @Description 查询最近的工作日期
	 * @Date 16:39 2025/4/7
	 * @Param
	 * @return
	 **/
	public R<TEmployeeInsuranceWorkDayVo> getBeforeOrAfterWorkDay(TEmployeeInsuranceWorkDayVo vo) {
		return HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId(),"/tholidayinfo/inner/getInnerInitBeforeOrAfterWorkDay", JSON.toJSONString(vo), TEmployeeInsuranceWorkDayVo.class, SecurityConstants.FROM_IN);
	}

	/**
	 * @Author huyc
	 * @Description 查询最近的工作日期
	 * @Date 17:56 2025/4/7
	 * @Param
	 * @return
	 **/
	public R<Boolean> checkIsWorkDay(TEmployeeInsuranceWorkDayVo vo) {
		return HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId(),"/tholidayinfo/inner/checkIsWorkDay", JSON.toJSONString(vo), Boolean.class, SecurityConstants.FROM_IN);
	}

	/**
	 * @Author huyc
	 * @Description 合同：查询最近的工作日期
	 * @Date 16:39 2025/6/16
	 * @Param
	 * @return
	 **/
	public R<TEmployeeInsuranceWorkDayVo> getContractAfterWorkDay(TEmployeeInsuranceWorkDayVo vo) {
		return HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId(),"/tholidayinfo/inner/selectBeforeOrAfterWorkDayCommon", JSON.toJSONString(vo), TEmployeeInsuranceWorkDayVo.class, SecurityConstants.FROM_IN);
	}

	/**
	 * @Author huyc
	 * @Description 判断是否存在正常或者在途的合同
	 * @Date 10:26 2025/6/12
	 * @Param
	 * @return
	 **/
	public R<Boolean> selectExitEmpSocial(TEmployeeAutoRegistCheckVo cardVo) {
		return HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId(),"/tsocialfundinfo/selectExitEmpSocial", JSON.toJSONString(cardVo), Boolean.class, SecurityConstants.FROM_IN);
	}

	/**
	 * @Author huyc
	 * @Description 新增社保待购买信息
	 * @Date 9:22 2025/10/10
	 * @Param
	 * @return
	 **/
	public R<Boolean> saveSocialPreInfo(EmployeeRegistrationPreVo preVo) {
		return HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId(),
				"/tdispatchinfopre/saveSocialPreInfo", JSON.toJSONString(preVo), Boolean.class, SecurityConstants.FROM_IN);
	}
	/**
	 * @Author fxj
	 * @Description 判断是否存在正常或者在途的公积金
	 * @Date 10:08 2025/11/7
	 * @Param
	 * @return
	**/
	public R<Boolean> selectExitEmpFund(TEmployeeAutoRegistCheckVo cardVo) {
		return HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId(),"/tsocialfundinfo/selectExitEmpFund", JSON.toJSONString(cardVo), Boolean.class, SecurityConstants.FROM_IN);
	}
	/**
	 * @Author fxj
	 * @Description 新增公积金待购买信息
	 * @Date 10:07 2025/11/7
	 * @Param
	 * @return
	**/
	public R<Boolean> saveFundPreInfo(EmployeeRegistrationPreVo preVo) {
		return HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId(),
				"/tdispatchinfopre/saveFundPreInfo", JSON.toJSONString(preVo), Boolean.class, SecurityConstants.FROM_IN);
	}
	/**
	 * @Author fxj
	 * @Description 获取社保配置信息
	 * @Date 12:11 2025/10/11
	 * @Param
	 * @return
	**/
	public R<SysBaseSetInfoVo> getSocialHouseBaseSet(String houseId) {
		R<SysBaseSetInfoVo>   res = HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId(),"/sysbasesetinfo/inner/getSocialHouseBaseSet", houseId, SysBaseSetInfoVo.class, SecurityConstants.FROM_IN);
		if (Common.isEmpty(res)){
			return R.failed("获取社保配置信息失败！");
		}
		return res;
	}

	/**
	 * @Author huych
	 * @Description 更新社保待购买的前端客服姓名
	 * @Date 2025-10-10 11:01:12
	 * @Param
	 * @return
	 **/
	public void updateSocialPreCustomerName(EmployeeRegistrationCustomerUserUpdateVo updateVo){
		HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl()
				, daprProperties.getAppId(),"/tdispatchinfopre/updateSocialPreCustomerName" , updateVo
				, Object.class, SecurityConstants.FROM_IN);
	}

	/**
	 * @Author huych
	 * @Description 获取社保待购买和已购买的信息
	 * @Date 2025-10-10 15:48:08
	 * @Param
	 * @return
	 **/
	public R<EmployeePreSocialListVo> getSocialPreInfoList(String id){
		return HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl()
				, daprProperties.getAppId(),"/tdispatchinfopre/getSocialPreInfoList" , id
				, EmployeePreSocialListVo.class, SecurityConstants.FROM_IN);
	}

	/**
	 * @Author huych
	 * @Description 获取社保已购买的信息
	 * @Date 2025-10-10 15:48:08
	 * @Param
	 * @return
	 **/
	public R<EmployeePreSocialListVo> getSocialPreInfoStatus(String id){
		return HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl()
				, daprProperties.getAppId(),"/tdispatchinfopre/getSocialPreInfoStatus" , id
				, EmployeePreSocialListVo.class, SecurityConstants.FROM_IN);
	}

	/**
	 * @Author fxj
	 * @Description 获取公积金已购买信息
	 * @Date 2025-10-10 15:48:08
	 * @Param
	 * @return
	 **/
	public R<EmployeePreSocialListVo> getFundPreInfoStatus(String id){
		return HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl()
				, daprProperties.getAppId(),"/tdispatchinfopre/getFundPreInfoStatus" , id
				, EmployeePreSocialListVo.class, SecurityConstants.FROM_IN);
	}

	/**
	 * @Author fxj
	 * @Description 获取公积金已购买信息 是否在途或在保
	 * @Date 2025-10-10 15:48:08
	 * @Param  true 在途或在保  false 不在途不在保
	 * @return
	 **/
	public R<Boolean> getFundPreStatus(String id){
		return HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl()
				, daprProperties.getAppId(),"/tdispatchinfopre/getFundPreStatus" , id
				, Boolean.class, SecurityConstants.FROM_IN);
	}


	/**
	 * @Author huych
	 * @Description 删除社保已购买非流程中的数据
	 * @Date 2025-10-10 15:51:36
	 * @Param
	 * @return
	 **/
	public R<Boolean> deleteUnProcessSocialPreInfo(String id){
		return HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl()
				, daprProperties.getAppId(),"/tdispatchinfopre/deleteUnProcessSocialPreInfo" , id
				, Object.class, SecurityConstants.FROM_IN);
	}
	/**
	 * @Author huych
	 * @Description 拒绝入职更新社保已购买非流程中的数据
	 * @Date 2025-10-10 15:51:36
	 * @Param
	 * @return
	 **/
	public R<Boolean> updateRefuseSocialPreInfo(String id){
		return HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl()
				, daprProperties.getAppId(),"/tdispatchinfopre/updateRefuseSocialPreInfo" , id
				, Object.class, SecurityConstants.FROM_IN);
	}

	// 离职确认联动清理待办
	public R<Boolean> updateLeaveSocialPreInfo(String id){
		return HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl()
				, daprProperties.getAppId(),"/tdispatchinfopre/updateLeaveSocialPreInfo" , id
				, Object.class, SecurityConstants.FROM_IN);
	}

	/**
	 * @Author fxj
	 * @Description 删除未处理的公积金待购买信息
	 * @Date 9:23 2025/10/11
	 * @Param
	 * @return
	 **/
	public R<Boolean> deleteUnProcessFundPreInfo(String id) {
		return HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId(),
				"/tdispatchinfopre/deleteUnProcessFundPreInfo", id, Boolean.class, SecurityConstants.FROM_IN);
	}

	public R<Boolean> updateRefuseFundPreInfo(String id) {
		return HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId(),
				"/tdispatchinfopre/updateRefuseFundPreInfo", id, Boolean.class, SecurityConstants.FROM_IN);
	}
	public R<Boolean> updateLeaveFundPreInfo(String id) {
		return HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId(),
				"/tdispatchinfopre/updateLeaveFundPreInfo", id, Boolean.class, SecurityConstants.FROM_IN);
	}

	/**
	 * @Author fxj
	 * @Description 获取公积金待购买信息
	 * @Date 10:07 2025/11/7
	 * @Param
	 * @return
	**/
	public R<EmployeePreSocialListVo> getFundPreInfoList(String id) {
		return HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId(),
				"/tdispatchinfopre/getFundPreInfoList", id, EmployeePreSocialListVo.class, SecurityConstants.FROM_IN);
	}

	public R<HolidayListVO> getOneMonthDay(){
		return HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl()
				, daprProperties.getAppId(),"/tholidayinfo/inner/getOneMonthDay" , null
				, HolidayListVO.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> getSocialAndFundStatus(BaseSearchVO paramVo) {
		R<BaseSearchVO> res = HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId()
				,"/tsocialfundinfo/inner/getSocialAndFundStatus", paramVo, BaseSearchVO.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> getSocialAndFundStatusByClose(BaseSearchVO paramVo) {
		R<BaseSearchVO> res = HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId()
				,"/tsocialfundinfo/inner/getSocialAndFundStatusByClose", 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: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> leaveToReduceSocialAndFund(TDispatchReduceListVo paramVo) {
		R<BaseSearchListVO> res = HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId()
				,"/tdispatchinfo/inner/leaveToReduceSocialAndFund", paramVo, BaseSearchListVO.class, SecurityConstants.FROM_IN);
		if (Common.isEmpty(res)){
			return R.failed("离职待办调用的批量派减社保、公积金失败！");
		}
		return res;
	}

	/**
	 * @Author fxj
	 * @Description 通过户名称及类型 获取到截止日，然后看是否为工作日，如果不是工作日则往前推，知道找到工作日，然后返回工作日
	 * @Date 14:22 2025/12/12
	 * @Param
	 * @return
	**/
	public R<THouseHoldLimitInnerVo> getWorkDayByDeadLineDay(BaseSearchVO paramVo) {
		R<THouseHoldLimitInnerVo> res = HttpDaprUtil.invokeMethodPost(daprProperties.getAppUrl(),daprProperties.getAppId()
				,"/thouseholdlimit/inner/getWorkDayByDeadLineDay", paramVo, THouseHoldLimitInnerVo.class, SecurityConstants.FROM_IN);
		if (Common.isEmpty(res)){
			return R.failed("查询截止日对应的工作日信息失败！");
		}
		return res;
	}

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