/*
 *    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.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yifu.cloud.plus.v1.yifu.archives.entity.TEmployeeContractInfo;
import com.yifu.cloud.plus.v1.yifu.archives.entity.TEmployeeContractPreNew;
import com.yifu.cloud.plus.v1.yifu.archives.service.TEmployeeContractPreNewService;
import com.yifu.cloud.plus.v1.yifu.archives.vo.*;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.DateUtil;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.common.log.annotation.SysLog;
import com.yifu.cloud.plus.v1.yifu.common.security.annotation.Inner;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.Date;
import java.util.List;


/**
 * 合同续签预派单信息
 *
 * @author fxj
 * @date 2026-02-05 15:45:40
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/temployeecontractprenew")
@Tag(name = "合同续签预派单信息管理")
public class TEmployeeContractPreNewController {

	private final TEmployeeContractPreNewService tEmployeeContractPreNewService;


	/**
	 * 简单分页查询
	 *
	 * @param page                    分页对象
	 * @param tEmployeeContractPreNew 合同续签预派单信息
	 * @return
	 */
	@Operation(description = "简单分页查询")
	@GetMapping("/page")
	public R<IPage<TEmployeeContractPreNew>> getTEmployeeContractPreNewPage(Page<TEmployeeContractPreNew> page, TEmployeeContractPreNewSearchVo tEmployeeContractPreNew) {
		return new R<>(tEmployeeContractPreNewService.getTEmployeeContractPreNewPage(page, tEmployeeContractPreNew));
	}

	/**
	 * 简单分页查询--批量发起选择数据 最大2000条
	 * 系统会自动拉取当前客服且“预计合同发起时间”为空的或为今日及今日之前的，续签状态为“待发起、发起失败、审核不通过、签署失败”的合同信息；
	 *
	 * @param tEmployeeContractPreNew 合同续签预派单信息
	 * @return
	 */
	@Operation(description = "简单分页查询")
	@PostMapping("/getDetailList")
	public R<List<TEmployeeContractPreNewDetailVo>> getDetailList(@RequestBody ContractAlertSearchVo tEmployeeContractPreNew) {
		return tEmployeeContractPreNewService.getDetailList(tEmployeeContractPreNew);
	}
	/**
	 * 简单分页查询--批量发起总数（权限内）
	 * 系统会自动拉取当前客服且“预计合同发起时间”为空的或为今日及今日之前的，续签状态为“待发起、发起失败、审核不通过、签署失败”的合同信息；
	 *
	 * @param tEmployeeContractPreNew 合同续签预派单信息
	 * @return
	 */
	@Operation(description = "简单分页查询")
	@PostMapping("/getDetailListCount")
	public R<Integer> getDetailListCount(@RequestBody  ContractAlertSearchVo tEmployeeContractPreNew) {
		return tEmployeeContractPreNewService.getDetailListCount(tEmployeeContractPreNew);
	}

	/**
	 * 不分页查询
	 *
	 * @param tEmployeeContractPreNew 合同续签预派单信息
	 * @return
	 */
	@Operation(summary = "不分页查询", description = "不分页查询")
	@PostMapping("/noPage")
	//@PreAuthorize("@pms.hasPermission('archives_temployeecontractprenew_get')" )
	public R<List<TEmployeeContractPreNew>> getTEmployeeContractPreNewNoPage(@RequestBody TEmployeeContractPreNewSearchVo tEmployeeContractPreNew) {
		return R.ok(tEmployeeContractPreNewService.noPageDiy(tEmployeeContractPreNew));
	}

	/**
	 * 通过id查询合同续签预派单信息
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	public R<TEmployeeContractPreNew> getById(@PathVariable("id") String id) {
		TEmployeeContractPreNew preNew = tEmployeeContractPreNewService.getById(id);
		//特殊处理兼容处理 合同详情
		if (null != preNew){
			preNew.setConfirmEmpSelect(preNew.getContractContent());
		}
		return R.ok(preNew);
	}

	/**
	 * 新增合同续签预派单信息
	 *
	 * @param tEmployeeContractPreNew 合同续签预派单信息
	 * @return R
	 */
	@Operation(summary = "新增合同续签预派单信息", description = "新增合同续签预派单信息：hasPermission('archives_temployeecontractprenew_add')")
	@SysLog("新增合同续签预派单信息")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('archives_temployeecontractprenew_add')")
	public R<Boolean> save(@RequestBody TEmployeeContractPreNew tEmployeeContractPreNew) {
		return R.ok(tEmployeeContractPreNewService.save(tEmployeeContractPreNew));
	}

	/**
	 * 修改合同续签预派单信息
	 *
	 * @param tEmployeeContractPreNew 合同续签预派单信息
	 * @return R
	 */
	@Operation(summary = "修改合同续签预派单信息", description = "修改合同续签预派单信息：hasPermission('archives_temployeecontractprenew_edit')")
	@SysLog("修改合同续签预派单信息")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('archives_temployeecontractprenew_edit')")
	public R<Boolean> updateById(@RequestBody TEmployeeContractPreNew tEmployeeContractPreNew) {
		return R.ok(tEmployeeContractPreNewService.updateById(tEmployeeContractPreNew));
	}

	/**
	 * 通过id删除合同续签预派单信息
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id删除合同续签预派单信息", description = "通过id删除合同续签预派单信息：hasPermission('archives_temployeecontractprenew_del')")
	@SysLog("通过id删除合同续签预派单信息")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('archives_temployeecontractprenew_del')")
	public R<Boolean> removeById(@PathVariable String id) {
		return R.ok(tEmployeeContractPreNewService.removeById(id));
	}

	/**
	 * 合同续签预派单信息 批量导出
	 *
	 * @author fxj
	 * @date 2026-02-05 15:45:40
	 **/
	@Operation(description = "导出合同续签预派单信息 hasPermission('archives_temployeecontractprenew-export')")
	@PostMapping("/export")
	@PreAuthorize("@pms.hasPermission('archives_temployeecontractprenew-export')")
	public void export(HttpServletResponse response, @RequestBody TEmployeeContractPreNewSearchVo searchVo) {
		tEmployeeContractPreNewService.listExport(response, searchVo);
	}

	/**
	 * @Author fxj
	 * @Description 合同待续签单个确认
	 * @Date 10:30 2026/2/9
	 * @Param
	 * @return
	**/
	@Operation(description = "合同待续签单个确认")
	@PostMapping("/confirmRenewalContract")
	public R confirmReceive(@RequestBody TEmployeeContractPreNewVo preVo) {
		return tEmployeeContractPreNewService.confirmRenewalContract(preVo);
	}

	/**
	 * 入职待确认表 批量确认接收
	 *
	 * @author huych
	 * @date 2025-03-13 16:55:32
	 **/
	@Operation(description = "合同待续签批量确认")
	@PostMapping("/batchCconfirmRenewalContract")
	public R batchConfirmReceive(@RequestBody TEmployeeContractPreNewVo preVo) {
		return tEmployeeContractPreNewService.batchCconfirmRenewalContract(preVo);
	}

	/**
	 * 合同续签待签订信息单个/批量发起签署任务
	 * @param idList id数组
	 **/
	@Operation(description = "合同续签待签订信息单个/批量发起签署任务")
	@PostMapping("/dispatcherContract")
	public R dispatcherContract(@RequestBody List<String> idList) {
		return tEmployeeContractPreNewService.dispatcherContract(idList,"1");
	}

	/**
	 * 合同待签订撤销签署
	 * @param registRevokeVoList 撤销合同数组
	 **/
	@Operation(description = "合同待签订撤销签署")
	@PostMapping("/revokeSign")
	public R revokeSign(@RequestBody List<TEmployeeAutoRegistRevokeVo> registRevokeVoList) {
		return tEmployeeContractPreNewService.revokeSign(registRevokeVoList);
	}
	/**
	 * 每天11点15点/15点20推送待派单数据 --- 工作日
	 * 会将发起失败、签署失败、审核不通过的“预计合同发起时间”为当日及之前日期的（空白的不在）合同续签任务自动尝试发起
	 * @author fxj
	 * @date 2025-02-24
	 * @return void
	 */
	@SysLog("每天11、15、15：20 发起电子签自动发起任务")
	@Inner
	@PostMapping("/inner/pushDisConfrimContracts")
	public void pushDisConfrimContracts() {
		tEmployeeContractPreNewService.pushDisConfrimContracts();
	}
	/**
	 * @Author fxj
	 * @Description 每日晚23:00自动撤销超时电子签署任务(当前时间超出当前合同开始日期一个月的数据)
	 * @Date 18:06 2026/2/10
	**/
	@Inner
	@PostMapping("/inner/autoRevokeElectronicSign")
	public R autoRevokeElectronicSign() {
		return tEmployeeContractPreNewService.autoRevokeElectronicSign();
	}

	/**
	 * @Author fxj
	 * @Description 每日早9点通知前端客服前一日撤销电子签署情况
	 * @Date 18:42 2026/2/10
	**/
	@Inner
	@PostMapping("/inner/notifyRevokedElectronicSign")
	public R notifyRevokedElectronicSign() {
		return tEmployeeContractPreNewService.notifyRevokedElectronicSign();
	}

	/**
	 * @Author fxj
	 * @Description 每日早9点电子签合同即将超期提醒
	 * @Date 19:08 2026/2/10
	**/
	@Inner
	@PostMapping("/inner/electronicSignExpiringReminder")
	public R electronicSignExpiringReminder() {
		return tEmployeeContractPreNewService.electronicSignExpiringReminder();
	}

	/**
	 * @Author fxj
	 * @Description 一键催办
	 * @Param id  loginName
	 * @Date 19:19 2026/2/10
	**/
	@Operation(description = "一键催办")
	@PostMapping("/contractUrg")
	public R contractUrg(@RequestParam String id) {
		return tEmployeeContractPreNewService.contractUrg(id);
	}

	/**
	 * 通过preId查询合同续签已确认信息
	 * @param preId
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过preId查询合同续签已确认信息")
	@GetMapping("/getInfoByPreId" )
	public R<TEmployeeContractPreNew> getInfoByPreId(@RequestParam("preId" ) String preId) {
		return tEmployeeContractPreNewService.getInfoByPreId(preId);

	}
	/**
	 * 通过preId查询已续签合同信息
	 * @param preId
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过preId查询已续签合同信息")
	@GetMapping("/getRenewContractByPreId" )
	public R<TEmployeeContractInfo> getRenewContractByPreId(@RequestParam("preId" ) String preId) {
		return tEmployeeContractPreNewService.getRenewContractByPreId(preId);

	}

	/**
	 * 续签确认提醒 9:00
	 * 续签确认提醒节点：
	 * 第一个时间：上一份合同截止日期前一个月往前找最近的工作日（若前一个月当天是工作日，那就取当日）；
	 * 第二个时间：合同截止日期（若为节假日，往前找最近的一个工作日）；
	 * 第三个时间：合同截止日期后一个月往前找第五个工作日（找不到就找第四、第三、第二、第一），都找不到，就不提醒
	 * 提醒至前端客服，点击提醒，提示：请至HRO系统及时进行合同确认，以免续签不及时！
	 * @author fxj
	 */
	@SysLog("每日9点统一推送续签确认信息")
	@Inner
	@PostMapping("/inner/pushWxConfrimMessage")
	public R pushWxConfrimMessage() {
		return tEmployeeContractPreNewService.pushWxConfrimMessage();
	}

	/**
	 * @Author fxj
	 * @Description 反推年限信息
	 * @Date 19:19 2026/2/10
	 **/
	@Operation(description = "反推年限信息")
	@GetMapping("/getContractTerm")
	public R getContractTerm(@RequestParam String startDate, @RequestParam String endDate) {
		return tEmployeeContractPreNewService.getContractTerm(startDate,endDate);
	}

	/**
	 * @Author fxj
	 * @Description 通过配置找到合同预计发起时间
	 * @Date 19:19 2026/2/10
	 **/
	@Operation(description = "通过配置找到合同预计发起时间")
	@GetMapping("/getPreDateByConfigId")
	public R getPreDateByConfigId(@RequestParam String configId, @RequestParam String endDate) {
		if (!Common.isNotNull(configId) || !Common.isNotNull(endDate)){
			return R.failed();
		}
		Date temp = null;
		try {
			temp = DateUtil.parseDate(endDate, DateUtil.ISO_EXPANDED_DATE_FORMAT);
		} catch (ParseException e) {
			return R.failed();
		}
		return R.ok(tEmployeeContractPreNewService.getPreDateByConfigId(configId,temp));
	}
}
