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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yifu.cloud.plus.v1.csp.entity.EmployeeRegistrationLeave;
import com.yifu.cloud.plus.v1.csp.service.EmployeeRegistrationLeaveService;
import com.yifu.cloud.plus.v1.csp.vo.BatchSureLeaveVo;
import com.yifu.cloud.plus.v1.csp.vo.EmployeeRegistrationLeaveSearchVo;
import com.yifu.cloud.plus.v1.csp.vo.EmployeeRegistrationLeaveTableVo;
import com.yifu.cloud.plus.v1.csp.vo.ErrorInfoVo;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.BaseServiceParamListVO;
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 io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

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


/**
 * 自动化离职待办
 *
 * @author hgw
 * @date 2025-11-19 19:23:15
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/employeeregistrationleave")
@Tag(name = "自动化离职待办管理")
public class EmployeeRegistrationLeaveController {

	private final EmployeeRegistrationLeaveService employeeRegistrationLeaveService;

	private final MenuUtil menuUtil;

	/**
	 * 简单分页查询
	 *
	 * @param page                      分页对象
	 * @return
	 */
	@Operation(description = "离职待办分页查询")
	@GetMapping("/getEmployeeRegistrationLeaveTablePage")
	public R<IPage<EmployeeRegistrationLeaveTableVo>> getEmployeeRegistrationLeaveTablePage(Page<EmployeeRegistrationLeaveTableVo> page
			, EmployeeRegistrationLeaveSearchVo searchVo) {
		this.setLeaveBaseAuth(searchVo);
		return new R<>(employeeRegistrationLeaveService.getEmployeeRegistrationLeaveTablePage(page, searchVo));
	}

	@Operation(description = "离职待办数量查询")
	@GetMapping("/getRegistrationLeaveCount")
	public R<Long> getRegistrationLeaveCount(EmployeeRegistrationLeaveSearchVo searchVo) {
		this.setLeaveBaseAuth(searchVo);
		return R.ok(employeeRegistrationLeaveService.getRegistrationLeaveCount(searchVo));
	}

	/**
	 * 简单分页查询
	 *
	 * @param searchVo 自动化离职待办
	 * @return
	 */
	@Operation(description = "离职待办分页查询2000条")
	@GetMapping("/getEmployeeRegistrationLeaveTablePage2000")
	public R<List<EmployeeRegistrationLeaveTableVo>> getEmployeeRegistrationLeaveTablePage2000(EmployeeRegistrationLeaveSearchVo searchVo) {
		this.setLeaveBaseAuth(searchVo);
		return new R<>(employeeRegistrationLeaveService.getEmployeeRegistrationLeaveTablePage2000(searchVo));
	}

	/**
	 * 简单分页查询
	 *
	 * @param page                      分页对象
	 * @param searchVo 自动化离职待办
	 * @return
	 */
	@Operation(description = "离职确认分页查询")
	@GetMapping("/getEmployeeRegistrationLeaveConfirmTablePage")
	public R<IPage<EmployeeRegistrationLeaveTableVo>> getEmployeeRegistrationLeaveConfirmTablePage(Page<EmployeeRegistrationLeaveTableVo> page
			, EmployeeRegistrationLeaveSearchVo searchVo) {
		this.setLeaveBaseAuth(searchVo);
		return new R<>(employeeRegistrationLeaveService.getEmployeeRegistrationLeaveConfirmTablePage(page, searchVo));
	}

	/**
	 * 通过id查询自动化离职待办
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	public R<EmployeeRegistrationLeave> getById(@PathVariable("id") String id) {
		return R.ok(employeeRegistrationLeaveService.initLeaveInfo(id));
	}

	/**
	 * 批量确认初始化
	 */
	@Operation(summary = "批量确认初始化", description = "批量确认初始化")
	@GetMapping("/initBatchSureInfo")
	public R<BatchSureLeaveVo> initBatchSureInfo() {
		return R.ok(employeeRegistrationLeaveService.initBatchSureInfo());
	}

	/**
	 * 自动化离职待办 批量导出
	 *
	 * @author hgw
	 * @date 2025-11-19 19:23:15
	 **/
	@SysLog(value = "导出自动化离职待办")
	@Operation(description = "导出自动化离职待办")
	@PostMapping("/exportTable")
	public void exportTable(HttpServletResponse response, @RequestBody EmployeeRegistrationLeaveSearchVo searchVo) {
		this.setLeaveBaseAuth(searchVo);
		employeeRegistrationLeaveService.exportTable(response, searchVo);
	}

	/**
	 * 自动化离职确认 批量导出
	 *
	 * @author hgw
	 * @date 2025-11-19 19:23:15
	 **/
	@SysLog(value = "导出自动化离职确认")
	@Operation(description = "导出自动化离职确认")
	@PostMapping("/exportConfirmTable")
	public void exportConfirmTable(HttpServletResponse response, @RequestBody EmployeeRegistrationLeaveSearchVo searchVo) {
		this.setLeaveBaseAuth(searchVo);
		employeeRegistrationLeaveService.exportConfirmTable(response, searchVo);
	}

	// 设置权限
	private void setLeaveBaseAuth(EmployeeRegistrationLeaveSearchVo searchVo) {
		YifuUser user = SecurityUtils.getUser();
		menuUtil.setAuthSql(user, searchVo);
	}

	/**
	 * 单个手动关闭
	 *
	 * 	String doType 需处理项：1社保2公积金3商险4合同5项目档案6人员档案")
	 */
	@Operation(summary = "单个手动关闭-离职待办", description = "单个手动关闭-离职待办")
	@SysLog("单个手动关闭-离职待办")
	@GetMapping("/doClose")
	public R<String> doClose(@RequestParam String id, @RequestParam String remark
			, @RequestParam(required = false) String doTypeWu, @RequestParam(required = false) String doTypeYi) {
		return employeeRegistrationLeaveService.doClose(id, remark, doTypeWu, doTypeYi);
	}

	/**
	 * @param leave 离职待办
	 */
	@Operation(summary = "单个确认-离职待办", description = "单个确认-离职待办")
	@SysLog("单个确认-离职待办")
	@PostMapping("/doSure")
	public R<String> doSure(@RequestBody EmployeeRegistrationLeave leave) {
		return employeeRegistrationLeaveService.doSure(leave);
	}

	/**
	 * @Description: 批量确认
	 * @Author: hgw
	 * @Date: 2025-11-20 20:12:53
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R<java.lang.Boolean>
	 **/
	@Operation(summary = "批量确认-离职待办", description = "批量确认-离职待办")
	@SysLog("批量确认-离职待办（与单个确认分开，是因为单个确认还有编辑功能，批量仅改状态")
	@PostMapping("/doSureBatch")
	public R<String> doSureBatch(@RequestBody BatchSureLeaveVo vo) {
		if (null == vo || vo.getIds() == null || vo.getIds().isEmpty()){
			return R.failed(CommonConstants.PARAM_IS_NOT_ERROR);
		}
		return employeeRegistrationLeaveService.doSureBatch(vo);
	}

	/**
	 * @Description: 定时任务批量派单
	 * @Author: hgw
	 * @Date: 2025-11-21 16:20:03
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R
	 **/
	@Operation(description = "定时任务批量派单")
	@PostMapping("/inner/autoDoLeave")
	@Inner
	public String autoDoLeave() {
		employeeRegistrationLeaveService.autoDoLeave();
		return CommonConstants.RESULT_DATA_SUCESS;
	}

	/**
	 * @param isExit: 1仅发起日期为今日及之前的事项 0立即发起（全量）
	 * @param doType: 需处理项：1社保2公积金3商险4合同5项目档案6人员档案
	 * @Description: 离职待办单个/批量派单
	 * @Author: hgw
	 * @Date: 2025/11/17 14:17
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R
	 **/
	@Operation(description = "离职待办单个/批量派单")
	@PostMapping("/batchDoLeave")
	public R<List<EmployeeRegistrationLeave>> batchDoLeave(@RequestBody List<String> idList, @RequestParam String isExit
			, @RequestParam(required = false) String doType) {
		return employeeRegistrationLeaveService.batchDoLeave(idList, isExit, doType);
	}

	@Operation(description = "减少办理同步离职待办")
	@PostMapping("/inner/reduceHandleToLeave")
	@Inner
	public String reduceHandleToLeave(@RequestBody BaseServiceParamListVO vo) {
		employeeRegistrationLeaveService.reduceHandleToLeave(vo);
		return CommonConstants.RESULT_DATA_SUCESS;
	}



}
