/*
 *    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.TEmpContractAlert;
import com.yifu.cloud.plus.v1.yifu.archives.service.TEmpContractAlertService;
import com.yifu.cloud.plus.v1.yifu.archives.vo.*;
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.ErrorMessage;
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.dapr.util.MenuUtil;
import com.yifu.cloud.plus.v1.yifu.common.log.annotation.SysLog;
import com.yifu.cloud.plus.v1.yifu.common.security.annotation.Inner;
import com.yifu.cloud.plus.v1.yifu.common.security.util.SecurityUtils;
import com.yifu.cloud.plus.v1.yifu.insurances.vo.TInsuranceAlertSearchVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


/**
 * 员工合同续签待办
 *
 * @author fxj
 * @date 2022-07-01 14:15:05
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/tempcontractalert" )
@Tag(name = "员工合同续签待办管理")
public class TEmpContractAlertController {

    private final TEmpContractAlertService tEmpContractAlertService;
	private final MenuUtil menuUtil;
	/**
	 * 自动化合同续签待办查询
	 * @param page 分页对象
	 * @param tEmpContractAlert 自动化合同续签待办查询
	 * @return
	 */
	@Operation(description = "自动化合同续签待办查询")
	@GetMapping("/autoPage")
	public R<IPage<ContractAlertAutoVo>> getTEmpContractAlertAutoPage(Page<TEmpContractAlert> page, ContractAlertSearchVo tEmpContractAlert) {
		YifuUser user = SecurityUtils.getUser();
		menuUtil.setAuthSql(user, tEmpContractAlert);
		//默认开启自动化的
		tEmpContractAlert.setAutoFlag(CommonConstants.ZERO_STRING);
		return  R.ok(tEmpContractAlertService.getTEmpContractAlertAutoPage(page,tEmpContractAlert));
	}

	/**
	 * 自动化合同续签待办查询
	 * @param tEmpContractAlert 自动化合同续签待办查询
	 * @return
	 */
	@Operation(description = "自动化合同续签待办查询")
	@GetMapping("/autoRenewForConfirm")
	public R<List<ContractAlertConfirmVo>> getAutoRenewForConfirm(ContractAlertSearchVo tEmpContractAlert) {
		//默认开启自动化的
		tEmpContractAlert.setAutoFlag(CommonConstants.ZERO_STRING);
		//默认查询待确认的信息
		List<String> processStatusList = new ArrayList<>();
		processStatusList.add(CommonConstants.ZERO_STRING);
		tEmpContractAlert.setProcessStatusList(processStatusList);
		return  tEmpContractAlertService.getAutoRenewForConfirm(tEmpContractAlert);
	}

	/**
	 * @Author fxj
	 * @Description 自动化合同续签待办数量
	 * @Date 16:49 2026/2/5
	 * @Param
	 * @return
	**/
	@Operation(description = "自动化合同续签待办数量")
	@GetMapping("/autoCount")
	public R<Integer> getTEmpContractAlertAutoCount(@RequestParam(required = false) String type, @RequestParam(required = false) String menuInfo) {
		ContractAlertSearchVo tEmpContractAlert = new ContractAlertSearchVo();
		//默认开启自动化的
		tEmpContractAlert.setAutoFlag(CommonConstants.ZERO_STRING);
		if (Common.isNotNull(type)) {
			tEmpContractAlert.setType(type);
		}
		if (Common.isNotNull(menuInfo)) {
			tEmpContractAlert.setMenuInfo(menuInfo);
		}
		YifuUser user = SecurityUtils.getUser();
		menuUtil.setAuthSql(user, tEmpContractAlert);
		return  R.ok(tEmpContractAlertService.getTEmpContractAlertAutoCount(tEmpContractAlert));
	}

    /**
     * 分页查询
     * @param page 分页对象
     * @param searchVo 员工合同续签待办
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page" )
    //@PreAuthorize("@pms.hasPermission('demo_tempcontractalert_get')" )
    public R<IPage<TEmpContractAlert>> getTEmpContractAlertPage(Page page, ContractAlertSearchVo searchVo) {
		setAuth(searchVo);
        return R.ok(tEmpContractAlertService.pageDiy(page, searchVo));
    }

	/**
     * 合同待续签-分页查询
     * @param page 分页对象
     * @param searchVo 员工合同续签待办
     * @return
     */
    @Operation(summary = "给自动化用的-合同待续签-分页查询", description = "合同待续签-分页查询")
    @GetMapping("/getAutoPage" )
    public R<IPage<TEmpContractAlert>> getAutoPage(Page page, ContractAlertSearchVo searchVo) {
		setAuth(searchVo);
        return R.ok(tEmpContractAlertService.getAutoPage(page, searchVo));
    }
	@Operation(description = "合同待续签数量查询")
	@GetMapping("/getAutoContractCount")
	public R<Long> getAutoContractCount(ContractAlertSearchVo searchVo) {
		setAuth(searchVo);
		return R.ok(tEmpContractAlertService.getAutoContractCount(searchVo));
	}

	/**
	 * 不分页查询
	 * @param searchVo 员工合同续签待办
	 * @return
	 */
	@Operation(summary = "给自动化用的-导出-不分页查询", description = "不分页查询")
	@PostMapping("/noPageToAuto" )
	public R<List<TEmpContractAlert>> noPageToAuto(@RequestBody ContractAlertSearchVo searchVo) {
		setAuth(searchVo);
		return R.ok(tEmpContractAlertService.noPageToAuto(searchVo));
	}
    /**
     * 不分页查询
     * @param searchVo 员工合同续签待办
     * @return
     */
    @Operation(summary = "不分页查询", description = "不分页查询")
    @PostMapping("/noPage" )
    //@PreAuthorize("@pms.hasPermission('demo_tempcontractalert_get')" )
    public R<List<TEmpContractAlert>> getTEmpContractAlertNoPage(@RequestBody ContractAlertSearchVo searchVo) {
		setAuth(searchVo);
        return R.ok(tEmpContractAlertService.listDiy(searchVo));
    }

    /**
     * 通过id查询员工合同续签待办
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询：hasPermission('demo_tempcontractalert_get')")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('demo_tempcontractalert_get')" )
    public R<TEmpContractAlert> getById(@PathVariable("id" ) String id) {
        return R.ok(tEmpContractAlertService.getById(id));
    }

    /**
     * 新增员工合同续签待办
     * @param tEmpContractAlert 员工合同续签待办
     * @return R
     */
    @Operation(summary = "新增员工合同续签待办", description = "新增员工合同续签待办：hasPermission('demo_tempcontractalert_add')")
    @SysLog("新增员工合同续签待办" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('demo_tempcontractalert_add')" )
    public R<Boolean> save(@RequestBody TEmpContractAlert tEmpContractAlert) {
        return R.ok(tEmpContractAlertService.save(tEmpContractAlert));
    }

    /**
     * 修改员工合同续签待办
     * @param tEmpContractAlert 员工合同续签待办
     * @return R
     */
    @Operation(summary = "修改员工合同续签待办", description = "修改员工合同续签待办：hasPermission('demo_tempcontractalert_edit')")
    @SysLog("修改员工合同续签待办" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('demo_tempcontractalert_edit')" )
    public R<Boolean> updateById(@RequestBody TEmpContractAlert tEmpContractAlert) {
        return R.ok(tEmpContractAlertService.updateById(tEmpContractAlert));
    }

    /**
     * 通过id删除员工合同续签待办
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id删除员工合同续签待办", description = "通过id删除员工合同续签待办：hasPermission('demo_tempcontractalert_del')")
    @SysLog("通过id删除员工合同续签待办" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('demo_tempcontractalert_del')" )
    public R<Boolean> removeById(@PathVariable String id) {
        return R.ok(tEmpContractAlertService.removeById(id));
    }

	/**
	 * 批量导出合同信息
	 * @param searchVo
	 * @return R<List>
	 * @Author FXJ
	 * @Date 2022-07-1
	 **/
	@Operation(description = "导出")
	@PostMapping("/export")
	public void export(HttpServletResponse response, @RequestBody ContractAlertSearchVo searchVo) {
		setAuth(searchVo);
		tEmpContractAlertService.listExport(response,searchVo);
	}

	private void setAuth(ContractAlertSearchVo searchVo) {
		YifuUser user = SecurityUtils.getUser();
		menuUtil.setAuthSql(user, searchVo);
		if (Common.isNotNull(searchVo.getAuthSql())) {
			if (searchVo.getAuthSql().contains(CommonConstants.A_DEPT_ID)) {
				searchVo.setAuthSql(searchVo.getAuthSql().replace(CommonConstants.A_DEPT_ID, "dept_id"));
			}
			if (searchVo.getAuthSql().contains(CommonConstants.A_CREATE_BY)) {
				searchVo.setAuthSql(searchVo.getAuthSql().replace(CommonConstants.A_CREATE_BY, "CREATE_BY"));
			}
		}
	}

	/**
	 * 定时任务生成合同续签代码信息
	 * @return R<List>
	 * @Author FXJ
	 * @Date 2022-07-4
	 **/
	@SysLog("定时任务生成合同续签代码信息")
	@PostMapping("/taskCreateContractAlert")
	@Inner
	public R taskCreateContractAlert() {
		return tEmpContractAlertService.taskCreateContractAlert();
	}

	/**
	 * 批量反馈
	 *
	 * @author wzb
	 * @param changeFeedBackAllVo
	 * @return {@link R}
	 */
	@SysLog("员工合同续签待办-批量反馈")
	@Operation(summary = "批量反馈", description = "批量反馈")
	@PostMapping("/changeFeedbackAll")
	public R<Boolean> changeFeedBackAll(@RequestBody ChangeFeedBackAllVo changeFeedBackAllVo) {
		return  R.ok(tEmpContractAlertService.changeFeedBackAll(changeFeedBackAllVo));
	}


	/**
	 * 获取反馈日志详情
	 *
	 * @author wzb
	 * @param applyNo
	 * @return {@link R}
	 */
	@Operation(summary = "获取反馈日志详情", description = "获取反馈日志详情")
	@GetMapping("/getFeedbackDetail/{applyNo}" )
	public R getFeedbackDetail(@PathVariable("applyNo" ) String applyNo) {
		return R.ok(tEmpContractAlertService.getFeedbackDetail(applyNo));
	}

	/**
	 * 批量更新-合同续签待办
	 * @param file
	 * @return
	 * @Author hgw
	 * @Date 2023-7-17 09:45:07
	 **/
	@SneakyThrows
	@Operation(description = "批量更新-合同续签待办 hasPermission('tempcontractalert_batch_update')")
	@SysLog("批量更新-合同续签待办")
	@PostMapping("/batchUpdate")
	@PreAuthorize("@pms.hasPermission('tempcontractalert_batch_update')")
	public R<List<EmpContractAlertUpdateVO>> batchUpdate(@RequestBody MultipartFile file) {
		return tEmpContractAlertService.batchUpdate(file.getInputStream());
	}


	/**
	 * 员工合同续签原因排查
	 *
	 * @author fxj
	 * @date 2023-09-01 17:31:05
	 **/
	@SneakyThrows
	@Operation(description = "员工合同续签原因排查")
	@SysLog("员工合同续签原因排查")
	@PostMapping("/importListAdd")
	public R<List<ErrorMessage>> importListAdd(@RequestBody MultipartFile file) {
		return tEmpContractAlertService.importDiy(file.getInputStream());
	}

	/**
	 * @Author fxj
	 * @Description 每日09:00推送一条整合的续签代码信息给配置的人员
	 * @Date 16:03 2025/8/15
	**/
	@Operation(description = "每天10：00推送整合后的待办商险数据给对应项目的前端客服信息")
	@Inner
	@PostMapping("/inner/pushContractAlertToWx")
	public void pushInsuranceAlertToWx() {
		tEmpContractAlertService.pushContractAlertToWx();
	}


	/**
	 * @Author fxj
	 * @Description 批量导出自动化合同续签待办
	 * @Date 17:00 2026/2/5
	 * @Param
	 * @return
	**/
	@Operation(description = "导出自动化续签待办")
	@PostMapping("/autoExport")
	public void autoExport(HttpServletResponse response, @RequestBody ContractAlertSearchVo searchVo) {
		searchVo.setAutoFlag(CommonConstants.ZERO_STRING);
		tEmpContractAlertService.listAutoExport(response,searchVo);
	}

	/**
	 * 通过合同ID查询合同对应的项目档案信息及人员手机号码
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过合同ID查询合同对应的项目档案信息及人员手机号码", description = "通过合同ID查询合同对应的项目档案信息及人员手机号码")
	@GetMapping("/getProjectInfoByContractId" )
	public R<EmpProjectVO> getProjectInfoByContractId(@RequestParam("id" ) String id) {
		return R.ok(tEmpContractAlertService.getProjectInfoByContractId(id));
	}

	/**
	 * @Author fxj
	 * @Description 通过合同提醒ID查询合同提醒信息、确认接收信息、新增续签合同信息
	 * @Date 16:53 2026/2/11
	**/
	@Operation(summary = "通过合同提醒ID查询合同提醒信息、确认接收信息、新增续签合同信息", description = "通过合同提醒ID查询合同提醒信息、确认接收信息、新增续签合同信息")
	@GetMapping("/getAllRenewInfoById" )
	public R<EmpContractRenewVO> getAllRenewInfoById(@RequestParam("id" ) String id) {
		return tEmpContractAlertService.getAllRenewInfoById(id);
	}
}
