/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the yifu4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.yifu.cloud.plus.v1.yifu.archives.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasc.open.api.exception.ApiException;
import com.fasc.open.api.v5_1.res.template.SignTemplateListInfo;
import com.yifu.cloud.plus.v1.yifu.archives.entity.*;
import com.yifu.cloud.plus.v1.yifu.archives.mapper.*;
import com.yifu.cloud.plus.v1.yifu.archives.service.*;
import com.yifu.cloud.plus.v1.yifu.archives.utils.FascUtil;
import com.yifu.cloud.plus.v1.yifu.archives.vo.TEmployeeContractPrePushFascVo;
import com.yifu.cloud.plus.v1.yifu.archives.vo.TFascTemplateSearchVo;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.OSSUtil;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.YifuUser;
import com.yifu.cloud.plus.v1.yifu.common.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

/**
 * 法大大专业版
 *
 * @author hgw
 * @date 2025-9-29 14:37:03
 */
@Log4j2
@Service
@RequiredArgsConstructor
public class FascServiceImpl extends ServiceImpl<FddContractInfoMapper, FddContractInfo> implements FascService {

	private final FascUtil fascUtil;

	private final TFascEditLogService tFascEditLogService;
	private final TFascPushLogService tFascPushLogService;
	private final TFascTemplateService tFascTemplateService;
	private final TFascTemplateDetailService tFascTemplateDetailService;
	private final TEmployeeContractPreMapper tEmployeeContractPreMapper;
	private final TEmployeeContractPreNewMapper tEmployeeContractPreNewMapper;
	private final TEmployeeContractInfoMapper tEmployeeContractInfoMapper;
	private final TEmployeeInfoMapper tEmployeeInfoMapper;
	private final TEmpContactInfoMapper tEmpContactInfoMapper;
	private final TAttaInfoMapper tAttaInfoMapper;
	private final TFascCompanyService tFascCompanyService;

	private final OSSUtil ossUtil;
	private static final String FASC_NO_CONTRACT = "未找到合同自动化";
	private static final String FASC_NO_EMP_CONTRACT = "未找到自动生成的员工合同";
	private static final String FASC_NO_TEMPLATE = "未找到新法大大模板";

	private static final Logger logger = LoggerFactory.getLogger(FascService.class);

	// 获取新建模板的url
	@Override
	public R<String> getCreateNewTemplate(String companyId) {
		if (Common.isEmpty(companyId)) {
			return R.failed("请传参公司ID！");
		}
		TFascCompany company = tFascCompanyService.getById(companyId);
		if (company == null || Common.isEmpty(company.getId())) {
			return R.failed("未找到公司信息，请刷新后重试！");
		}
		return fascUtil.getCreateNewTemplate(tFascPushLogService, company);
	}

	// 获取新建模板的url
	@Override
	public R<String> getEditNewTemplate(String id) {
		if (Common.isEmpty(id)) {
			return R.failed("请传参列表ID！");
		}
		TFascTemplate tFascTemplate = tFascTemplateService.getById(id);
		if (tFascTemplate == null || Common.isEmpty(tFascTemplate.getId())) {
			return R.failed("未找到模板信息，请刷新后重试！");
		}
		if (Common.isEmpty(tFascTemplate.getSignTemplateId()) || Common.isEmpty(tFascTemplate.getCompanyId())) {
			return R.failed("模板信息缺失，请刷新后重试！");
		}
		TFascCompany company = tFascCompanyService.getById(tFascTemplate.getCompanyId());
		if (company == null || Common.isEmpty(company.getId())) {
			return R.failed("未找到公司信息，请刷新后重试！");
		}
		return fascUtil.getEditNewTemplate(tFascPushLogService, tFascTemplate.getSignTemplateId(), company);
	}

	@Override
	public R<String> getTemplate(String templateName) throws ApiException {

		String userId = "-1";
		String userName = "系统";
		YifuUser user = SecurityUtils.getUser();
		if (user != null && Common.isNotNull(user.getId())) {
			userId = user.getId();
			userName = user.getNickname();
		}
		List<TFascCompany> companyList = tFascCompanyService.getTFascCompanyAllList(null);
		if (companyList != null && !companyList.isEmpty()) {
			// templateName 签署任务模板名称
			List<TFascTemplate> tList ;
			List<SignTemplateListInfo> listSubList;
			List<TFascTemplateDetail> detailList ;
			// 重试次数
			int maxRetries = 3;
			// 是否全部成功，若为false，则说明有失败的，不删除未找到的。
			boolean success;
			Exception lastException;
			// 20秒后重试、40秒后重试、60秒后重试
			long millis = 20000;
			for (TFascCompany company : companyList) {
				listSubList = null;
				try {
					listSubList = fascUtil.getTemplateList(templateName, tFascPushLogService, company);
				} catch (Exception e) {
					logger.error("法大大获取模板信息失败", e);
				}
				if (listSubList != null && !listSubList.isEmpty()) {
					detailList = new ArrayList<>();
					tList = new ArrayList<>();
					success = false;
					lastException = null;
					for (int attempt = 0; attempt < maxRetries && !success; attempt++) {
						// 重试，要重置
						detailList = new ArrayList<>();
						tList = new ArrayList<>();
						try {
							fascUtil.getTemplateDetailList(listSubList, tList, detailList, tFascPushLogService, company);
							success = true;
						} catch (Exception e) {
							lastException = e;
							if (attempt < maxRetries - 1) { // Don't wait after the last attempt
								try {
									Thread.sleep(millis * (attempt + 1)); // Wait 10 seconds
								} catch (InterruptedException ie) {
									Thread.currentThread().interrupt();
								}
							}
						}
					}

					if (!success) {
						logger.error("法大大获取模板详情，重试 " + maxRetries + " 次依然失败", lastException);
					}

					// 拉取到数据再更新：
					if (!tList.isEmpty() && !detailList.isEmpty()) {
						String editContent1 = "定时任务同步，拉取到数据";
						String editContent2 = "定时任务同步，法大大无该数据";
						if (!"-1".equals(userId)) {
							editContent1 = "手动拉取任务同步，拉取到数据";
							editContent2 = "手动拉取任务同步，法大大无该数据";
						}
						List<TFascTemplate> updateMain = new ArrayList<>();
						List<TFascTemplateDetail> updateDetail = new ArrayList<>();

						// 分每个公司去拉取
						TFascTemplateSearchVo searchMain = new TFascTemplateSearchVo();
						searchMain.setCompanyId(company.getId());
						List<TFascTemplate> allMain = tFascTemplateService.getTFascTemplateListAll(searchMain);
						// 分每个公司去拉取
						TFascTemplateDetail searchDetail = new TFascTemplateDetail();
						searchDetail.setCompanyId(company.getId());
						List<TFascTemplateDetail> allDetail = tFascTemplateDetailService.getTFascTemplateDetailList(searchDetail);
						List<TFascEditLog> logList = new ArrayList<>();
						if (allMain == null || allMain.isEmpty()) {
							// 都是新的：
							tFascTemplateService.saveBatch(tList);
							tFascTemplateDetailService.saveOrUpdateBatch(detailList);
							for (TFascTemplate t : tList) {
								this.addFascLogToList(t.getId(), CommonConstants.ONE_STRING, editContent1, logList, userId, userName);
							}
							for (TFascTemplateDetail t : detailList) {
								this.addFascLogToList(t.getId(), CommonConstants.TWO_STRING, editContent1, logList, userId, userName);
							}
						} else {
							// 混合老新数据
							Map<String, TFascTemplate> oldMainMap = new HashMap<>();
							Map<String, TFascTemplateDetail> oldDetailMap = new HashMap<>();
							for (TFascTemplate t : allMain) {
								oldMainMap.put(t.getSignTemplateId(), t);
							}
							for (TFascTemplateDetail t : allDetail) {
								oldDetailMap.put(t.getSignTemplateId() + CommonConstants.DOWN_LINE_STRING + t.getFascFieldId(), t);
							}
							// 主表：
							TFascTemplate oldMain;
							String logId;
							for (TFascTemplate t : tList) {
								oldMain = oldMainMap.get(t.getSignTemplateId());
								if (oldMain != null) {
									t.setId(oldMain.getId());
								} else {
									logId = String.valueOf(UUID.randomUUID()).replaceAll("-", "");
									t.setId(logId);
								}
								this.addFascLogToList(t.getId(), CommonConstants.ONE_STRING, editContent1, logList, userId, userName);
								updateMain.add(t);
								oldMainMap.remove(t.getSignTemplateId());
							}
							if (success) {
								for (TFascTemplate t : oldMainMap.values()) {
									this.addFascLogToList(t.getId(), CommonConstants.ONE_STRING, editContent2, logList, userId, userName);
									t.setDeleteFlag(CommonConstants.DELETE_FLAG);
									updateMain.add(t);
								}
							}
							// 映射表：
							TFascTemplateDetail oldDetail;
							for (TFascTemplateDetail t : detailList) {
								oldDetail = oldDetailMap.get(t.getSignTemplateId() + CommonConstants.DOWN_LINE_STRING + t.getFascFieldId());
								if (oldDetail != null) {
									t.setId(oldDetail.getId());
									t.setHrField(oldDetail.getHrField());
									t.setHrFieldId(oldDetail.getHrFieldId());
								} else {
									logId = String.valueOf(UUID.randomUUID()).replaceAll("-", "");
									t.setId(logId);
								}
								this.addFascLogToList(t.getId(), CommonConstants.TWO_STRING, editContent1, logList, userId, userName);
								updateDetail.add(t);
								oldDetailMap.remove(t.getSignTemplateId() + CommonConstants.DOWN_LINE_STRING + t.getFascFieldId());
							}
							if (success) {
								for (TFascTemplateDetail t : oldDetailMap.values()) {
									this.addFascLogToList(t.getId(), CommonConstants.TWO_STRING, editContent2, logList, userId, userName);
									t.setDeleteFlag(CommonConstants.DELETE_FLAG);
									updateDetail.add(t);
								}
							}
						}
						// 更新日志
						if (!logList.isEmpty()) {
							tFascEditLogService.saveBatch(logList);
						}
						// 更新模板
						if (!updateMain.isEmpty()) {
							tFascTemplateService.saveOrUpdateBatch(updateMain);
						}
						// 更新模板映射
						if (!updateDetail.isEmpty()) {
							tFascTemplateDetailService.saveOrUpdateBatch(updateDetail);
						}
					}
				}
			}

			// 重新刷新主模板表的必填是否已填
			tFascTemplateDetailService.initFascTemplateIsMust(null);
		}
		return R.ok();
	}

	// 获取模板详情预览链接
	@Override
	public R<String> getTemplateDetailUrl(String signTemplateId) throws ApiException {
		// templateName 签署任务模板名称
		TFascCompany company = null;
		TFascTemplate tFascTemplate = tFascTemplateService.getOne(Wrappers.<TFascTemplate>query()
				.lambda().eq(TFascTemplate::getSignTemplateId, signTemplateId)
				.last(CommonConstants.LAST_ONE_SQL));
		if (tFascTemplate != null && Common.isNotNull(tFascTemplate.getCompanyId())) {
			company = tFascCompanyService.getById(tFascTemplate.getCompanyId());
		}
		if (company == null) {
			return R.failed("无公司信息，请检查数据!");
		}
		return fascUtil.getTemplateDetailUrl(signTemplateId, tFascPushLogService, company);
	}

	/**
	 * 向list里添加新日志
	 */
	private void addFascLogToList(String mainId, String mainType, String editContent, List<TFascEditLog> logList, String userId, String userName) {
		TFascEditLog log = new TFascEditLog();
		log.setMainId(mainId);
		log.setMainType(mainType);
		log.setEditContent(editContent);
		log.setCreateBy(userId);
		log.setCreateName(userName);
		log.setCreateTime(LocalDateTime.now());
		logList.add(log);
	}


	/**
	 * @param id TEmployeeContractPre主键
	 * @Description: 推送法大大任务
	 * @Author: hgw
	 * @Date: 2025/10/15 16:40
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R<java.lang.String>
	 **/
	@Override
	public R<String> submitContract(String id) throws ApiException {
		// templateName 签署任务模板名称
		TEmployeeContractPre contract = tEmployeeContractPreMapper.selectById(id);
		if (contract == null) {
			return R.failed(FASC_NO_CONTRACT);
		}
		if (Common.isEmpty(contract.getContractId())) {
			return R.failed("未生成合同，请联系管理员");
		}
		String empIdCard = contract.getEmpIdcard();
		if (Common.isEmpty(empIdCard)) {
			return R.failed("未找到身份证，请联系管理员");
		}

		TEmployeeContractInfo empContract = tEmployeeContractInfoMapper.selectById(contract.getContractId());
		if (empContract == null) {
			return R.failed(FASC_NO_EMP_CONTRACT);
		}
		TEmployeeContractPrePushFascVo empContactInfo = tEmpContactInfoMapper.getTEmpContactInfoOneByCard(empIdCard);
		TEmployeeContractPrePushFascVo empInfo = tEmpContactInfoMapper.getTEmpInfoOneByCard(empIdCard);

		if (empContactInfo != null) {
			contract.setContactEmpName(empContactInfo.getContactEmpName());
			contract.setContactTel(empContactInfo.getContactTel());
			contract.setContactAddress(empContactInfo.getContactAddress());
		}
		if (empInfo != null) {
			contract.setHuArea(empInfo.getHuArea());
			contract.setContactArea(empInfo.getContactArea());
			contract.setEmpEmail(empInfo.getEmpEmail());
			contract.setEmpPhone(empInfo.getEmpPhone());
			contract.setWeChatId(empInfo.getWeChatId());
		}

		if (Common.isEmpty(contract.getFadadaTemplateId())) {
			return R.failed("未找到合同模板ID，请联系管理员");
		}
		if (Common.isEmpty(contract.getSignType())) {
			return R.failed("签署方式为空，请联系管理员");
		} else if (!CommonConstants.ZERO_STRING.equals(contract.getSignType())) {
			return R.failed("签署方式不是电子签，不可发起电子签署");
		}
		// 如果状态过多，可以改为这个：
		// ArrayUtils.contains(forbidStatusArr, tGzOfferInfo.getOfferStatus())

		if (Common.isNotNull(contract.getRequestId())) {
			return R.failed("合同已发起，请勿重复发起！");
		}
		TFascTemplate tFascTemplate = tFascTemplateService.getOne(Wrappers.<TFascTemplate>query()
				.lambda().eq(TFascTemplate::getSignTemplateId, contract.getFadadaTemplateId())
				.last(CommonConstants.LAST_ONE_SQL));
		if (tFascTemplate == null) {
			return R.failed(FASC_NO_TEMPLATE);
		} else if (!"valid".equals(tFascTemplate.getSignTemplateStatus())) {
			return R.failed("法大大模版状态不是“启用”，不可发起电子签署");
		}
		List<TFascTemplateDetail> detailList = tFascTemplateDetailService.getTDetailListByFieldType(contract.getFadadaTemplateId());
		// - 判断是否入职超过1个月——接口入口做了

		TFascCompany company = tFascCompanyService.getById(tFascTemplate.getCompanyId());

		R<String> requestInfo = fascUtil.submitContract(contract, tFascPushLogService, tEmployeeContractPreMapper, tFascTemplate, detailList, company);
		return requestInfo;

	}
	@Override
	public R<String> submitContractRenew(String id) throws ApiException {
		// templateName 签署任务模板名称
		TEmployeeContractPreNew contract = tEmployeeContractPreNewMapper.selectById(id);
		if (contract == null) {
			return R.failed(FASC_NO_CONTRACT);
		}
		TEmployeeContractInfo empContract = tEmployeeContractInfoMapper.selectOne(Wrappers.<TEmployeeContractInfo>query()
				.lambda().eq(TEmployeeContractInfo::getPreNewId,id)
				.eq(TEmployeeContractInfo::getDeleteFlag,CommonConstants.ZERO_STRING)
				.last(CommonConstants.LAST_ONE_SQL));
		String empIdCard = contract.getEmpIdcard();
		if (Common.isEmpty(empIdCard)) {
			return R.failed("未找到身份证，请联系管理员");
		}
		if (empContract == null) {
			return R.failed(FASC_NO_EMP_CONTRACT);
		}
		TEmployeeContractPrePushFascVo empContactInfo = tEmpContactInfoMapper.getTEmpContactInfoOneByCard(empIdCard);
		TEmployeeContractPrePushFascVo empInfo = tEmpContactInfoMapper.getTEmpInfoOneByCard(empIdCard);
		if (empContactInfo != null) {
			contract.setContactEmpName(empContactInfo.getContactEmpName());
			contract.setContactTel(empContactInfo.getContactTel());
			contract.setContactAddress(empContactInfo.getContactAddress());
		}
		// 如果员工档案信息有,默认用档案的手机号
		if (empInfo != null) {
			contract.setHuArea(empInfo.getHuArea());
			contract.setContactArea(empInfo.getContactArea());
			contract.setEmpEmail(empInfo.getEmpEmail());
			contract.setEmpPhone(empInfo.getEmpPhone());
			contract.setWeChatId(empInfo.getWeChatId());
		}

		if (Common.isEmpty(contract.getFadadaTemplateId())) {
			return R.failed("未找到合同模板ID，请联系管理员");
		}
		if (Common.isEmpty(contract.getSignType())) {
			return R.failed("签署方式为空，请联系管理员");
		} else if (!CommonConstants.ZERO_STRING.equals(contract.getSignType())) {
			return R.failed("签署方式不是电子签，不可发起电子签署");
		}
		// 如果状态过多，可以改为这个：
		// ArrayUtils.contains(forbidStatusArr, tGzOfferInfo.getOfferStatus())
		//系统自动撤销及客服撤销都放行
		if (Common.isNotNull(contract.getRequestId())
				&& !CommonConstants.ELEVEN_STRING.equals(contract.getProcessStatus())
				&& !CommonConstants.TEN_STRING.equals(contract.getProcessStatus())
				&& !CommonConstants.FIVE_STRING.equals(contract.getProcessStatus())) {
			return R.failed("合同已发起，请勿重复发起！");
		}
		TFascTemplate tFascTemplate = tFascTemplateService.getOne(Wrappers.<TFascTemplate>query()
				.lambda().eq(TFascTemplate::getSignTemplateId, contract.getFadadaTemplateId())
				.last(CommonConstants.LAST_ONE_SQL));
		if (tFascTemplate == null) {
			return R.failed(FASC_NO_TEMPLATE);
		} else if (!"valid".equals(tFascTemplate.getSignTemplateStatus())) {
			return R.failed("法大大模版状态不是“启用”，不可发起电子签署");
		}
		List<TFascTemplateDetail> detailList = tFascTemplateDetailService.getTDetailListByFieldType(contract.getFadadaTemplateId());
		// - 判断是否入职超过1个月——接口入口做了

		TFascCompany company = tFascCompanyService.getById(tFascTemplate.getCompanyId());
		R<String> requestInfo = fascUtil.submitContract(contract, tFascPushLogService, tEmployeeContractPreNewMapper, tFascTemplate, detailList, company);
		return requestInfo;

	}
	/**
	 * @param id TEmployeeContractPre主键
	 * @Description: 撤销签署任务
	 * @Author: hgw
	 * @Date: 2025/10/15 16:40
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R<java.lang.String>
	 **/
	@Override
	public R<String> cancelTask(String id) throws ApiException {
		// templateName 签署任务模板名称
		TEmployeeContractPre contract = tEmployeeContractPreMapper.selectById(id);
		if (contract == null) {
			return R.failed(FASC_NO_CONTRACT);
		}
		if (Common.isEmpty(contract.getRequestId())) {
			return R.failed("无requestId，请检查数据");
		}
		TFascCompany company = getTFascCompany(contract);
		if (company == null) {
			return R.failed("无公司信息，请检查数据!");
		}
		return fascUtil.cancelTask(contract, tFascPushLogService, tEmployeeContractPreMapper, company);

	}
	@Override
	public R<String> cancelTaskRenew(String id) throws ApiException {
		// templateName 签署任务模板名称
		TEmployeeContractPreNew contract = tEmployeeContractPreNewMapper.selectById(id);
		if (contract == null) {
			return R.failed(FASC_NO_CONTRACT);
		}
		if (Common.isEmpty(contract.getRequestId())) {
			return R.failed("无requestId，请检查数据");
		}
		TFascCompany company = getTFascCompany(contract);
		if (company == null) {
			return R.failed("无公司信息，请检查数据!");
		}
		return fascUtil.cancelTask(contract, tFascPushLogService, tEmployeeContractPreNewMapper, company);

	}
	private TFascCompany getTFascCompany(TEmployeeContractPre contract) {
		TFascCompany company = null;
		if (Common.isNotNull(contract.getFadadaTemplateId())) {
			TFascTemplate tFascTemplate = tFascTemplateService.getOne(Wrappers.<TFascTemplate>query()
					.lambda().eq(TFascTemplate::getSignTemplateId, contract.getFadadaTemplateId())
					.last(CommonConstants.LAST_ONE_SQL));
			if (tFascTemplate != null && Common.isNotNull(tFascTemplate.getCompanyId())) {
				company = tFascCompanyService.getById(tFascTemplate.getCompanyId());
			}
		}
		return company;
	}

	private TFascCompany getTFascCompany(TEmployeeContractPreNew contract) {
		TFascCompany company = null;
		if (Common.isNotNull(contract.getFadadaTemplateId())) {
			TFascTemplate tFascTemplate = tFascTemplateService.getOne(Wrappers.<TFascTemplate>query()
					.lambda().eq(TFascTemplate::getSignTemplateId, contract.getFadadaTemplateId())
					.last(CommonConstants.LAST_ONE_SQL));
			if (tFascTemplate != null && Common.isNotNull(tFascTemplate.getCompanyId())) {
				company = tFascCompanyService.getById(tFascTemplate.getCompanyId());
			}
		}
		return company;
	}

	/**
	 * @param id TEmployeeContractPre主键
	 * @Description: 催办签署任务
	 * @Author: hgw
	 * @Date: 2025/10/15 17:56
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R<java.lang.String>
	 **/
	@Override
	public R<String> urgeTask(String id) throws ApiException {
		// templateName 签署任务模板名称
		TEmployeeContractPre contract = tEmployeeContractPreMapper.selectById(id);
		if (contract == null) {
			return R.failed(FASC_NO_CONTRACT);
		}
		if (Common.isEmpty(contract.getRequestId())) {
			return R.failed("无requestId，请检查数据");
		}

		TFascCompany company = getTFascCompany(contract);
		if (company == null) {
			return R.failed("无公司信息，请检查数据!");
		}
		return fascUtil.urgeTask(contract, tFascPushLogService, tEmployeeContractPreMapper, company);

	}

	/**
	 * @Author fxj
	 * @Description 续签待办自动化：催办签署任务
	 * @Date 19:25 2026/2/10
	**/
	@Override
	public R<String> urgeTaskRenew(String id) throws ApiException {
		// templateName 签署任务模板名称
		TEmployeeContractPreNew contract = tEmployeeContractPreNewMapper.selectById(id);
		if (contract == null) {
			return R.failed(FASC_NO_CONTRACT);
		}
		if (Common.isEmpty(contract.getRequestId())) {
			return R.failed("无requestId，请检查数据");
		}

		TFascCompany company = getTFascCompany(contract);
		if (company == null) {
			return R.failed("无公司信息，请检查数据!");
		}
		return fascUtil.urgeTask(contract, tFascPushLogService, tEmployeeContractPreNewMapper, company);

	}

	/**
	 * @param id TEmployeeContractPre主键
	 * @Description: 获取附件
	 * @Author: hgw
	 * @Date: 2025/10/15 17:56
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R<java.lang.String>
	 **/
	@Override
	public boolean getFileByRequestId(String id) throws ApiException {
		// templateName 签署任务模板名称
		TEmployeeContractPre contract = tEmployeeContractPreMapper.selectById(id);
		if (contract == null) {
			return false;
		}
		if (Common.isEmpty(contract.getRequestId())) {
			// return R.failed("无requestId，请检查数据")
			return false;
		}
		TFascCompany company = getTFascCompany(contract);
		if (company == null) {
			return false;
		}
		return fascUtil.getFileByRequestId(contract, tFascPushLogService, tAttaInfoMapper, ossUtil, company);
	}
	@Override
	public boolean getFileByRequestIdRenew(String id,String contractId) throws ApiException {
		// templateName 签署任务模板名称
		TEmployeeContractPreNew contract = tEmployeeContractPreNewMapper.selectById(id);
		if (contract == null) {
			return false;
		}
		if (Common.isEmpty(contract.getRequestId())) {
			// return R.failed("无requestId，请检查数据")
			return false;
		}
		TFascCompany company = getTFascCompany(contract);
		if (company == null) {
			return false;
		}
		return fascUtil.getFileByRequestId(contract, tFascPushLogService, tAttaInfoMapper, ossUtil, company, contractId);
	}

}
