package com.yifu.cloud.plus.v1.yifu.social.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.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.R;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.BaseSearchVO;
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.entity.TEmployeeInsurancePre;
import com.yifu.cloud.plus.v1.yifu.social.entity.TDispatchInfoPre;
import com.yifu.cloud.plus.v1.yifu.social.service.TDispatchInfoPreService;
import com.yifu.cloud.plus.v1.yifu.social.vo.TDispatchInfoPreSearchVo;
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.util.*;

/**
 * 社保待购买表
 *
 * @author huych
 * @date 2025-09-30 16:40:07
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/tdispatchinfopre")
@Tag(name = "社保待购买表管理")
public class TDispatchInfoPreController {

	private final TDispatchInfoPreService tDispatchInfoPreService;

	private final MenuUtil menuUtil;
	// 提取公共方法用于构建状态列表
	private static final Map<String, List<String>> STATUS_MAP = new HashMap<>();

	static {
		// 社保购买状态列表:社保购买状态，0待确认，1待派单，2派单失败，3待审核，4审核不通过，5待办理，6办理中，7办理成功，8办理失败，9部分办理失败
		STATUS_MAP.put(CommonConstants.ZERO_STRING, Arrays.asList(
				CommonConstants.ZERO_STRING,
				CommonConstants.ONE_STRING,
				CommonConstants.TWO_STRING,
				CommonConstants.THREE_STRING,
				CommonConstants.FOUR_STRING,
				CommonConstants.FIVE_STRING,
				CommonConstants.SIX_STRING,
				CommonConstants.EIGHT_STRING,
				CommonConstants.NINE_STRING
		));

		// 公积金购买状态列表:公积金购买状态，0待确认，1待派单，2派单失败，3待审核，4审核不通过，5待办理，7办理成功，8办理失败
		STATUS_MAP.put(CommonConstants.ONE_STRING, Arrays.asList(
				CommonConstants.ZERO_STRING,
				CommonConstants.ONE_STRING,
				CommonConstants.TWO_STRING,
				CommonConstants.THREE_STRING,
				CommonConstants.FOUR_STRING,
				CommonConstants.FIVE_STRING,
				CommonConstants.EIGHT_STRING
		));
	}



	/**
	 * 简单分页查询
	 *
	 * @param page             分页对象
	 * @param tDispatchInfoPre 社保或公积金待购买表
	 * @return
	 */
	@Operation(description = "简单分页查询")
	@GetMapping("/page")
	public R<IPage<TDispatchInfoPre>> getTDispatchInfoPrePage(Page<TDispatchInfoPre> page, TDispatchInfoPreSearchVo tDispatchInfoPre) {
		YifuUser user = SecurityUtils.getUser();
		menuUtil.setAuthSql(user, tDispatchInfoPre);
		//typeSuB 0 社保 1 公积金
		if (Common.isEmpty(tDispatchInfoPre.getTypeSub())){
			tDispatchInfoPre.setTypeSub(CommonConstants.ZERO_STRING);
		}else if (!Common.isEmpty(tDispatchInfoPre.getTypeSub())){
			tDispatchInfoPre.setTypeSub(tDispatchInfoPre.getTypeSub());
		}
		// 主逻辑优化
		if (Common.isEmpty(tDispatchInfoPre.getProcessStatus())) {
			String typeSub = null==tDispatchInfoPre.getTypeSub()?CommonConstants.EMPTY_STRING:tDispatchInfoPre.getTypeSub();
			List<String> statusList = STATUS_MAP.get(typeSub);
			if (statusList != null && Common.isEmpty(tDispatchInfoPre.getStatusList())) {
				tDispatchInfoPre.setStatusList(new ArrayList<>(statusList)); // 拷贝以防止外部修改
			}
		}

		return new R<>(tDispatchInfoPreService.getTDispatchInfoPrePage(page, tDispatchInfoPre));
	}

	/**
	 * 不分页查询
	 *
	 * @param tDispatchInfoPre 社保待购买表
	 * @return
	 */
	@Operation(summary = "不分页查询", description = "不分页查询")
	@PostMapping("/getNoPageList")
	public R<List<TDispatchInfoPre>> getTDispatchInfoPreNoPage(@RequestBody TDispatchInfoPreSearchVo tDispatchInfoPre) {
		YifuUser user = SecurityUtils.getUser();
		menuUtil.setAuthSql(user, tDispatchInfoPre);
		//typeSun 0 社保 1 公积金
		if (Common.isEmpty(tDispatchInfoPre.getTypeSub())){
			tDispatchInfoPre.setTypeSub(CommonConstants.ZERO_STRING);
		}else if (Common.isEmpty(tDispatchInfoPre.getTypeSub())){
			tDispatchInfoPre.setTypeSub(tDispatchInfoPre.getTypeSub());
		}
		return R.ok(tDispatchInfoPreService.getNoPageList(tDispatchInfoPre));
	}

	/**
	 * 不分页查询
	 *
	 * @param tDispatchInfoPre 社保待购买表
	 * @return
	 */
	@Operation(summary = "不分页查询（新）", description = "不分页查询（新）")
	@PostMapping("/getNoPageListNew")
	public R<List<TDispatchInfoPre>> getTDispatchInfoPreNoPageNew(@RequestBody TDispatchInfoPreSearchVo tDispatchInfoPre) {
		YifuUser user = SecurityUtils.getUser();
		menuUtil.setAuthSql(user, tDispatchInfoPre);
		//typeSun 0 社保 1 公积金
		if (Common.isEmpty(tDispatchInfoPre.getTypeSub())){
			tDispatchInfoPre.setTypeSub(CommonConstants.ZERO_STRING);
		}else if (Common.isEmpty(tDispatchInfoPre.getTypeSub())){
			tDispatchInfoPre.setTypeSub(tDispatchInfoPre.getTypeSub());
		}
		return R.ok(tDispatchInfoPreService.getNoPageListNew(tDispatchInfoPre));
	}

	/**
	 * 通过id查询社保待购买表
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	public R<TDispatchInfoPre> getById(@PathVariable("id") String id) {
		return R.ok(tDispatchInfoPreService.getById(id));
	}

	/**
	 * 修改社保待购买表
	 *
	 * @param tDispatchInfoPre 社保待购买表
	 * @return R
	 */
	@Operation(summary = "修改社保待购买表", description = "修改社保待购买表")
	@SysLog("修改社保待购买表")
	@PutMapping
	public R<Boolean> updateById(@RequestBody TDispatchInfoPre tDispatchInfoPre) {
		return R.ok(tDispatchInfoPreService.updateById(tDispatchInfoPre));
	}

	/**
	 * 通过id删除社保待购买表
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id删除社保待购买表", description = "通过id删除社保待购买表：hasPermission('social_tdispatchinfopre_del')")
	@SysLog("通过id删除社保待购买表")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('social_tdispatchinfopre_del')")
	public R<Boolean> removeById(@PathVariable String id) {
		return R.ok(tDispatchInfoPreService.removeById(id));
	}

	/**
	 * 社保待购买表 批量导出
	 *
	 * @author huych
	 * @date 2025-09-30 16:40:07
	 **/
	@Operation(description = "导出社保待购买表")
	@PostMapping("/export")
	public void export(HttpServletResponse response, @RequestBody TDispatchInfoPreSearchVo searchVo) {
		YifuUser user = SecurityUtils.getUser();
		menuUtil.setAuthSql(user, searchVo);
		//typeSuB 0 社保 1 公积金
		searchVo.setTypeSub(CommonConstants.ZERO_STRING);
		// 主逻辑优化
		if (Common.isEmpty(searchVo.getProcessStatus())) {
			String typeSub = null==searchVo.getTypeSub()?CommonConstants.EMPTY_STRING:searchVo.getTypeSub();
			List<String> statusList = STATUS_MAP.get(typeSub);
			if (statusList != null && Common.isEmpty(searchVo.getStatusList())) {
				searchVo.setStatusList(new ArrayList<>(statusList)); // 拷贝以防止外部修改
			}
		}
		tDispatchInfoPreService.listExport(response, searchVo);
	}

	/**
	 * @Author fxj
	 * @Description 公积金待购买表 批量导出
	 * @Date 16:00 2025/11/7
	 * @Param
	 * @return
	**/
	@Operation(description = "导出公积金待购买表")
	@PostMapping("/exportFund")
	public void exportFund(HttpServletResponse response, @RequestBody TDispatchInfoPreSearchVo searchVo) {
		YifuUser user = SecurityUtils.getUser();
		menuUtil.setAuthSql(user, searchVo);
		//typeSuB 0 社保 1 公积金
		searchVo.setTypeSub(CommonConstants.ONE_STRING);
		// 主逻辑优化
		if (Common.isEmpty(searchVo.getProcessStatus())) {
			String typeSub = null==searchVo.getTypeSub()?CommonConstants.EMPTY_STRING:searchVo.getTypeSub();
			List<String> statusList = STATUS_MAP.get(typeSub);
			if (statusList != null && Common.isEmpty(searchVo.getStatusList())) {
				searchVo.setStatusList(new ArrayList<>(statusList)); // 拷贝以防止外部修改
			}
		}
		tDispatchInfoPreService.listExportFund(response, searchVo);
	}

	/**
	 * 社保待购买信息单个/批量确认
	 * @author huych
	 * @date 2025-10-09 10:25:13
	 **/
	@Operation(description = "社保待购买信息单个/批量确认")
	@PostMapping("/confirm")
	public R confirm(@RequestBody List<String> idList) {
		return tDispatchInfoPreService.confirm(idList);
	}

	/**
	 * @Author fxj
	 * @Description 公积金待购买信息单个/批量确认
	 * @Date 15:20 2025/11/10
	 * @Param
	 * @return
	**/
	@Operation(description = "公积金待购买信息单个/批量确认")
	@PostMapping("/confirmFund")
	public R confirmFund(@RequestBody List<String> idList) {
		return tDispatchInfoPreService.confirmFund(idList);
	}

	/**
	 * 社保待购买信息单个/批量发起购买任务
	 * @param idList id数组
	 **/
	@Operation(description = "社保待购买信息单个/批量发起购买任务")
	@PostMapping("/dispatcherContract")
	public R dispatcherContract(@RequestBody List<String> idList) {
		return tDispatchInfoPreService.dispatcherSocial(idList,"1");
	}

	/**
	 * @Author fxj
	 * @Description 手动派单-公积金待购买信息单个/批量发起购买任务
	 * @Date 14:08 2025/11/7
	 * @Param
	 * @return
	**/
	@Operation(description = "公积金待购买信息单个/批量发起购买任务")
	@PostMapping("/dispatcherFund")
	public R dispatcherFund(@RequestBody List<String> idList) {
		return tDispatchInfoPreService.dispatcherFund(idList,"1");
	}

	/**
	 * 社保待购买数量查询   公积金可用这个接口
	 * @param searchVo 社保待购买任务表
	 * @return
	 */
	@Operation(description = "社保待购买数量查询")
	@PostMapping("/getListCount")
	public R getListCount(@RequestBody TDispatchInfoPreSearchVo searchVo) {
		YifuUser user = SecurityUtils.getUser();
		menuUtil.setAuthSql(user, searchVo);
		//社保购买状态，0待确认，1待派单，2派单失败，3待审核，4审核不通过，5待办理，6办理中，7办理成功，8办理失败，9部分办理失败
		List<String> statusList = STATUS_MAP.get(CommonConstants.ZERO_STRING);
		if (statusList != null) {
			searchVo.setStatusList(new ArrayList<>(statusList)); // 拷贝以防止外部修改
		}
		return R.ok(tDispatchInfoPreService.getDispatchInfoCount(searchVo));
	}

	/**
	 * 公积金待购买数量查询
	 * @param searchVo 公积金待购买任务表
	 * @return
	 */
	@Operation(description = "公积金待购买数量查询")
	@PostMapping("/getListFundCount")
	public R getListCountFund(@RequestBody TDispatchInfoPreSearchVo searchVo) {
		YifuUser user = SecurityUtils.getUser();
		menuUtil.setAuthSql(user, searchVo);
		// 公积金购买状态，0待确认，1待派单，2派单失败，3待审核，4审核不通过，5待办理，7办理成功，8办理失败
		List<String> statusList = STATUS_MAP.get(CommonConstants.ONE_STRING);
		if (statusList != null) {
			searchVo.setStatusList(new ArrayList<>(statusList)); // 拷贝以防止外部修改
		}
		return R.ok(tDispatchInfoPreService.getDispatchInfoFundCount(searchVo));
	}



	/**
	 * 新增社保待购买
	 * @author huych
	 * @date 2025-10-10 9:26:35
	 **/
	@Operation(description = "新增社保待购买")
	@Inner
	@PostMapping("/saveSocialPreInfo")
	public Boolean saveSocialPreInfo(@RequestBody EmployeeRegistrationPreVo preVo) {
		return tDispatchInfoPreService.saveSocialPreInfo(preVo);
	}

	/**
	 * @Author fxj
	 * @Description 新增公积金待购买
	 * @Date 14:08 2025/11/7
	 * @Param
	 * @return \
	**/
	@Operation(description = "新增公积金待购买")
	@Inner
	@PostMapping("/saveFundPreInfo")
	public Boolean saveFundPreInfo(@RequestBody EmployeeRegistrationPreVo preVo) {
		return tDispatchInfoPreService.saveFundPreInfo(preVo);
	}

	/**
	 * 更新社保待购买的前端客服姓名
	 * @author huych
	 * @date 2025-10-10 11:02:35
	 **/
	@Operation(description = "更新社保待购买的前端客服姓名")
	@Inner
	@PostMapping("/updateSocialPreCustomerName")
	public void updateSocialPreCustomerName(@RequestBody EmployeeRegistrationCustomerUserUpdateVo updateVo) {
		tDispatchInfoPreService.updateSocialPreCustomerName(updateVo);
	}

	/**
	 * 入职确认信息详情中的社保明细信息
	 * @author huych
	 * @date 2025-10-10 15:51:18
	 **/
	@Operation(description = "入职确认信息详情中的社保明细信息")
	@Inner
	@PostMapping("/getSocialPreInfoList")
	public EmployeePreSocialListVo getSocialPreInfoList(@RequestBody String id) {
		return tDispatchInfoPreService.getSocialPreInfoList(id);
	}

	/**
	 * @Author fxj
	 * @Description 入职确认信息详情中的公积金明细信息
	 * @Date 14:08 2025/11/7
	 * @Param
	 * @return
	**/
	@Operation(description = "入职确认信息详情中的公积金明细信息")
	@Inner
	@PostMapping("/getFundPreInfoList")
	public EmployeePreSocialListVo getFundPreInfoList(@RequestBody String id) {
		return tDispatchInfoPreService.getFundPreInfoList(id);
	}

	/**
	 * 查询社保待购买的状态
	 * @author huych
	 * @date 2025-10-11 14:45:26
	 **/
	@Operation(description = "查询社保待购买的状态")
	@Inner
	@PostMapping("/getSocialPreInfoStatus")
	public EmployeePreSocialListVo getSocialPreInfoStatus(@RequestBody String id) {
		return tDispatchInfoPreService.getSocialPreInfoStatus(id);
	}

	/**
	 * @Author fxj
	 * @Description 查询公积金待购买的状态
	 * @Date 14:08 2025/11/7
	 * @Param
	 * @return
	**/
	@Operation(description = "查询公积金待购买的状态")
	@Inner
	@PostMapping("/getFundPreInfoStatus")
	public EmployeePreSocialListVo getFundPreInfoStatus(@RequestBody String id) {
		return tDispatchInfoPreService.getFundPreInfoStatus(id);
	}

	/**
	 * @Author fxj
	 * @Description 查询公积金待购买的状态：待审核、待办理、办理成功——为在途或在保 待确认   待派单   派单失败   审核不通过    办理失败    ——为不在途不在保
	 * @Date 9:47 2025/11/17
	 * @Param
	 * @return true  在途或在保   false 不在途不在保
	**/
	@Operation(description = "查询公积金待办的状态")
	@Inner
	@PostMapping("/getFundPreStatus")
	public Boolean getFundPreStatus(@RequestBody String id) {
		return tDispatchInfoPreService.getFundPreStatus(id);
	}


	/**
	 * 删除非流程中的社保待购买数据   公积金可以通用
	 * @author huych
	 * @date 2025-10-11 15:30:02
	 **/
	@Operation(description = "删除非流程中的社保待购买数据")
	@Inner
	@PostMapping("/deleteUnProcessSocialPreInfo")
	public Boolean deleteUnProcessSocialPreInfo(@RequestBody String id) {
		return tDispatchInfoPreService.deleteUnProcessSocialPreInfo(id);
	}

	/**
	 * @Author fxj
	 * @Description 删除非流程中的公积金待购买数据
	 * @Date 15:06 2025/11/10
	 * @Param
	 * @return
	**/
	@Operation(description = "删除非流程中的公积金待购买数据")
	@Inner
	@PostMapping("/deleteUnProcessFundPreInfo")
	public Boolean deleteUnProcessFundPreInfo(@RequestBody String id) {
		return tDispatchInfoPreService.deleteUnProcessFundPreInfo(id);
	}
	/**
	 * @Author fxj
	 * @Description 拒绝入职更新非流程中的公积金待购买数据
	 * @Date 11:09 2026/1/27
	 * @Param
	 * @return
	**/
	@Operation(description = "拒绝入职更新非流程中的公积金待购买数据")
	@Inner
	@PostMapping("/updateRefuseFundPreInfo")
	public Boolean updateRefuseFundPreInfo(@RequestBody String id) {
		return tDispatchInfoPreService.updateRefuseFundPreInfo(id);
	}
	/**
	 * @Author hgw
	 * @Description 离职确认更新非流程中的公积金待购买数据
	 * @Param
	 * @return
	**/
	@Operation(description = "离职确认更新非流程中的公积金待购买数据")
	@Inner
	@PostMapping("/updateLeaveFundPreInfo")
	public Boolean updateLeaveFundPreInfo(@RequestBody String id) {
		return tDispatchInfoPreService.updateLeaveFundPreInfo(id);
	}

	/**
	 * 派单当日9点统一推送确认信息 公积金可以通用
	 * @author huych
	 * @date 2025-10-11 18:50:16
	 * @return void
	 */
	@SysLog("派单当日9点统一推送确认信息")
	@Inner
	@PostMapping("/inner/pushWxConfrimMessage")
	public void pushWxConfrimMessage() {
		tDispatchInfoPreService.pushWxConfrimMessage();
	}

	/**
	 * 每天下午三点二十或三点或上午11点推送待购买数据 公积金可以通用
	 * @author huych
	 * @date 2025-10-11 18:51:58
	 * @return void
	 */
	@SysLog("每天下午三点二十或三点或上午11点推送待购买数据")
	@Inner
	@PostMapping("/inner/pushDisConfrimSocial")
	public void pushDisConfrimSocial() {
		tDispatchInfoPreService.pushDisConfrimSocial();
	}

	/**
	 * 通过id查询社保或公积金待签订任务记录表
	 * @param registId
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/getInfoByRegistId" )
	public R<TDispatchInfoPre> getInfoByRegistId(@RequestParam("registId" ) String registId, @RequestParam("type" ) String type) {
		return tDispatchInfoPreService.getInfoByRegistId(registId,type);
	}

	/**
	 * @Author fxj
	 * @Description 拒绝入职更新非流程中的社保待购买数据
	 * @Date 11:04 2026/1/27
	 * @Param
	 * @return
	**/
	@Operation(description = "拒绝入职更新非流程中的社保待购买数据")
	@Inner
	@PostMapping("/updateRefuseSocialPreInfo")
	public Boolean updateRefuseSocialPreInfo(@RequestBody String id) {
		return tDispatchInfoPreService.updateRefuseSocialPreInfo(id);
	}

	@Operation(description = "拒绝入职更新非流程中的社保待购买数据")
	@Inner
	@PostMapping("/updateLeaveSocialPreInfo")
	public Boolean updateLeaveSocialPreInfo(@RequestBody String id) {
		return tDispatchInfoPreService.updateLeaveSocialPreInfo(id);
	}

	/**
	 * 查询是否有处理中的自动化的合同（通过身份证与项目编码）
	 */
	@Operation(summary = "查询是否有处理中的自动化的合同（通过身份证与项目编码）", description = "查询是否有处理中的自动化的合同（通过身份证与项目编码）")
	@Inner
	@PostMapping("/inner/judgeHaveProcessingSocialOrFund" )
	public Boolean judgeHaveProcessingSocialOrFund(@RequestBody BaseSearchVO paramVo) {
		return tDispatchInfoPreService.judgeHaveProcessingSocialOrFund(paramVo);
	}

}
