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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.BaseSearchVO;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.YifuUser;
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.social.entity.THouseHoldLimit;
import com.yifu.cloud.plus.v1.yifu.social.service.THouseHoldLimitService;
import com.yifu.cloud.plus.v1.yifu.social.vo.THouseHoldLimitInnerVo;
import com.yifu.cloud.plus.v1.yifu.social.vo.THouseHoldLimitSearchVo;
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.List;


/**
 * 户截止日配置表
 *
 * @author fxj
 * @date 2025-12-11 14:19:54
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/thouseholdlimit")
@Tag(name = "户截止日配置表管理")
public class THouseHoldLimitController {

	private final THouseHoldLimitService tHouseHoldLimitService;

	private final static String ADD_UPDATE_REPEAT_ERROR = "对应户名及户类型的配置已存在！";


	/**
	 * 简单分页查询
	 *
	 * @param page            分页对象
	 * @param tHouseHoldLimit 户截止日配置表
	 * @return
	 */
	@Operation(description = "简单分页查询")
	@GetMapping("/page")
	public R<IPage<THouseHoldLimit>> getTHouseHoldLimitPage(Page<THouseHoldLimit> page, THouseHoldLimitSearchVo tHouseHoldLimit) {
		return new R<>(tHouseHoldLimitService.getTHouseHoldLimitPage(page, tHouseHoldLimit));
	}


	/**
	 * 通过id查询户截止日配置表
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	public R<THouseHoldLimit> getById(@PathVariable("id") String id) {
		return R.ok(tHouseHoldLimitService.getById(id));
	}
	/**
	 * 通过户名称及类型查询到具体截止日期后去看是否为工作日，如果不是工作日则往前推，直到找到工作日，然后返回工作日
	 * 如果找不到对应截止日期，则返回null
	 * @param paramVo:户名称 type: 0 社保、1 公积金
	 * @return R
	 */
	@Operation(summary = "户名称及类型查询可用的截止日期", description = "通过户名称及类型查询到具体截止日期后去查询是否为工作日，如果不是工作日则往前推，直到找到工作日，然后返回工作日")
	@PostMapping("/getWorkDayByDeadLineDay")
	public R<THouseHoldLimitInnerVo> getWorkDayByDeadLineDay(@RequestBody BaseSearchVO paramVo) {
		return R.ok(tHouseHoldLimitService.getWorkDayByDeadLineDay(paramVo));
	}

	/**
	 * 获取派单发起时间
	 * @param paramVo:户名称 type: 0 社保、1 公积金  startDate 起缴日期
	 * @return R
	 */
	@Operation(summary = "获取派单发起时间", description = "获取派单发起时间")
	@PostMapping("/getConfirmDate")
	public R<String> getConfirmDate(@RequestBody BaseSearchVO paramVo) {
		return tHouseHoldLimitService.getConfirmDate(paramVo);
	}


	/**
	 * 通过户名称及类型查询到具体截止日期后判断是否为工作日，如果不是工作往前推，直到找到工作日，
	 * 然后和当前日期进行比较，如果当前日期大于截止日期则返回false，否则返回true
	 * @param paramVo:name 户名称 type: 0 社保、1 公积金
	 * @return R
	 */
	@Inner
	@Operation(summary = "户名称及类型查询可用的截止日期", description = "通过户名称及类型查询到具体截止日期后去查询是否为工作日，如果不是工作日则往前推，直到找到工作日，然后返回工作日")
	@PostMapping("/inner/getWorkDayByDeadLineDay")
	public THouseHoldLimitInnerVo getWorkDayByDeadLineDayInner(@RequestBody BaseSearchVO paramVo) {
		return tHouseHoldLimitService.getWorkDayByDeadLineDay(paramVo);
	}

	/**
	 * 新增户截止日配置表
	 *
	 * @param tHouseHoldLimit 户截止日配置表
	 * @return R
	 */
	@Operation(summary = "新增户截止日配置表", description = "新增户截止日配置表")
	@SysLog("新增户截止日配置表")
	@PostMapping
	public R<Boolean> save(@RequestBody THouseHoldLimit tHouseHoldLimit) {
		//先查验是否已存在对应户名称和户类型的数据
		THouseHoldLimit houseHoldLimit = tHouseHoldLimitService
				.getOne(Wrappers.<THouseHoldLimit>query().lambda()
				.eq(THouseHoldLimit::getName, tHouseHoldLimit.getName())
				.eq(THouseHoldLimit::getType, tHouseHoldLimit.getType())
				.last(CommonConstants.LAST_ONE_SQL));
		if (null != houseHoldLimit){
			return R.failed(ADD_UPDATE_REPEAT_ERROR);
		}
		return R.ok(tHouseHoldLimitService.save(tHouseHoldLimit));
	}

	/**
	 * 修改户截止日配置表
	 *
	 * @param tHouseHoldLimit 户截止日配置表
	 * @return R
	 */
	@Operation(summary = "修改户截止日配置表", description = "修改户截止日配置表")
	@SysLog("修改户截止日配置表")
	@PutMapping
	public R<Boolean> updateById(@RequestBody THouseHoldLimit tHouseHoldLimit) {
		if (tHouseHoldLimit.getId() == null){
			return R.failed(CommonConstants.PARAM_INFO_ERROR);
		}
		//先查验是否已存在对应户名称和户类型的数据
		THouseHoldLimit houseHoldLimit = tHouseHoldLimitService
				.getOne(Wrappers.<THouseHoldLimit>query().lambda()
						.eq(THouseHoldLimit::getName, tHouseHoldLimit.getName())
						.eq(THouseHoldLimit::getType, tHouseHoldLimit.getType())
						.ne(THouseHoldLimit::getId, tHouseHoldLimit.getId())
						.last(CommonConstants.LAST_ONE_SQL));
		if (null != houseHoldLimit){
			return R.failed(ADD_UPDATE_REPEAT_ERROR);
		}
		return R.ok(tHouseHoldLimitService.updateById(tHouseHoldLimit));
	}

	/**
	 * 通过id删除户截止日配置表
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id删除户截止日配置表", description = "通过id删除户截止日配置表")
	@SysLog("通过id删除户截止日配置表")
	@DeleteMapping("/{id}")
	public R<Boolean> removeById(@PathVariable String id) {
		return R.ok(tHouseHoldLimitService.removeById(id));
	}

	/**
	 * 户截止日配置表 批量导入
	 *
	 * @author fxj
	 * @date 2025-12-11 14:19:54
	 **/
	@SneakyThrows
	@Operation(description = "批量新增户截止日配置表")
	@SysLog("批量新增户截止日配置表")
	@PostMapping("/importListAdd")
	public R<List<ErrorMessage>> importListAdd(@RequestBody MultipartFile file) {
		YifuUser user = SecurityUtils.getUser();
		if (Common.isEmpty(user)){
			return R.failed(CommonConstants.USER_FAIL);
		}
		return tHouseHoldLimitService.importDiy(file.getInputStream(),user);
	}

	/**
	 * 户截止日配置表 批量导出
	 *
	 * @author fxj
	 * @date 2025-12-11 14:19:54
	 **/
	@Operation(description = "导出户截止日配置表")
	@PostMapping("/export")
	public void export(HttpServletResponse response, @RequestBody THouseHoldLimitSearchVo searchVo) {
		tHouseHoldLimitService.listExport(response, searchVo);
	}
}
