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

import com.baomidou.mybatisplus.core.metadata.IPage;
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.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.TInsuranceAlert;
import com.yifu.cloud.plus.v1.yifu.insurances.service.insurance.TInsuranceDetailService;
import com.yifu.cloud.plus.v1.yifu.insurances.service.insurance.TInsuranceWarnService;
import com.yifu.cloud.plus.v1.yifu.insurances.vo.BatchIgnoreVo;
import com.yifu.cloud.plus.v1.yifu.insurances.vo.InsuranceSearchVo;
import com.yifu.cloud.plus.v1.yifu.insurances.vo.TBusinessInsuranceVo;
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.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author fxj
 * @Date 2023/2/16
 * @Description 商险提醒相关
 * @Version 1.0
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/insuranceWarn")
@Tag(name = "商险提醒相关")
public class TInsurancesWarnController {
	@Resource
	private TInsuranceDetailService insuranceDetailService;

	@Resource
	private TInsuranceWarnService insuranceWarnService;

	private final MenuUtil menuUtil;

	@Operation(description = "商险待续保列表查询")
	@GetMapping("/getTInsuranceAlertPage")
	public R<IPage<TInsuranceAlert>> getTInsuranceAlertPage(Page<TInsuranceAlert> page, TInsuranceAlertSearchVo tInsuranceAlert) {
		YifuUser user = SecurityUtils.getUser();
		menuUtil.setAuthSql(user, tInsuranceAlert);
		return R.ok(insuranceWarnService.getTInsuranceAlertPage(page, tInsuranceAlert));
	}

	// 用来批量确认，批量派单等
	@Operation(description = "商险待续保列表查询2000条")
	@PostMapping("/getTInsuranceAlertList2000")
	public R<List<TInsuranceAlert>> getTInsuranceAlertList2000(@RequestBody TInsuranceAlertSearchVo tInsuranceAlert) {
		YifuUser user = SecurityUtils.getUser();
		menuUtil.setAuthSql(user, tInsuranceAlert);
		return R.ok(insuranceWarnService.getTInsuranceAlertList2000(tInsuranceAlert));
	}

	/**
	 * @Description: 商险待续签保数量查询
	 * @Author: hgw
	 * @Date: 2025/11/17 9:48
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R
	 **/
	@Operation(description = "商险待续签保数量查询")
	@GetMapping("/getInsuranceAlertCount")
	public R<Long> getInsuranceAlertCount(TInsuranceAlertSearchVo tInsuranceAlert) {
		YifuUser user = SecurityUtils.getUser();
		menuUtil.setAuthSql(user, tInsuranceAlert);
		return R.ok(insuranceWarnService.getInsuranceAlertCount(tInsuranceAlert));
	}

	@Operation(summary = "商险待续保导出", description = "商险待续保导出")
	@SysLog("商险待续保导出")
	@PostMapping("/exportTInsuranceAlert")
	public void exportTInsuranceAlert(@RequestBody(required = false) TInsuranceAlertSearchVo searchVo,
										HttpServletResponse response) {
		if (Common.isEmpty(searchVo)) {
			searchVo = new TInsuranceAlertSearchVo();
		}
		YifuUser user = SecurityUtils.getUser();
		menuUtil.setAuthSql(user, searchVo);
		insuranceWarnService.exportTInsuranceAlert(searchVo, response);
	}

	/**
	 * @Author fxj
	 * @Description 商险到期提醒分页查询
	 * @Date 16:51 2023/2/16
	**/
	@Operation(description = "商险到期提醒分页查询")
	@GetMapping("/getExpireWarn/Vopage")
	public R<IPage<TBusinessInsuranceVo>> getTBusinessInsuranceExpireWarnVoPage(Page<TBusinessInsuranceVo> page, InsuranceSearchVo tBusinessInsurance) {
		return R.ok(insuranceDetailService.getInsuranceExpireWarnVoPage(page, tBusinessInsurance));
	}

	/**
	 * @Author fxj
	 * @Description 备注批量导入
	 * @Date 16:57 2023/2/16
	**/
	@SneakyThrows
	@Operation(description = "备注批量导入(wxhr:insurance_Warn_batch_update)")
	@SysLog("备注批量导入")
	@PostMapping("/remark/batchAdd")
	//@PreAuthorize("@pms.hasPermission('insurance_Warn_batch_update')")
	public R<List<ErrorMessage>> remarkBatchAdd(@RequestBody MultipartFile file) {
		return insuranceDetailService.remarkBatchAdd(file.getInputStream());
	}

	/**
	 * @Author fxj
	 * @Description 商险到期提醒分页查询导出
	 * @Date 16:57 2023/2/16
	**/
	@PostMapping("/getExpireWarn/export")
	@Operation(description = "商险到期提醒分页查询导出 hasPermission('wxhr:insurance_expirewarnexport')")
	@SysLog("商险到期提醒分页查询导出")
	//@PreAuthorize("@pms.hasPermission('wxhr:insurance_expirewarnexport')")
	public void getExpireWarnExport(HttpServletResponse response, HttpServletRequest request, @RequestBody InsuranceSearchVo vo) {
		insuranceDetailService.exportInsurancesWarn(vo, response,vo.getExportFields(),vo.getIdStr());
	}

	/**
	 * @Author fxj
	 * @Description 到期提醒备注
	 * @Date 17:01 2023/2/16
	**/
	@Operation(description = "到期提醒备注")
	@SysLog("到期提醒备注")
	@PutMapping("/expireRemark")
	public R<Object> expireRemark(@RequestBody TBusinessInsuranceVo vo) {
		return insuranceDetailService.expireRemark(vo);
	}

	/**
	 * @Author fxj
	 * @Description 到期提醒忽略
	 * @Date 17:01 2023/2/16
	**/
	@Operation(description = "到期提醒忽略(wxhr:insurance_expireignore)")
	@SysLog("到期提醒忽略")
	@PutMapping("/expireIgnore")
	@PreAuthorize("@pms.hasPermission('wxhr:insurance_expireignore')")
	public R<Object> expireIgnore(@RequestBody TBusinessInsuranceVo vo) {
		return insuranceDetailService.expireIgnore(vo);
	}

	/**
	 * @Author fxj
	 * @Description 到期提醒批量忽略
	 * @Date 17:06 2023/2/16
	**/
	@Operation(description = "到期提醒批量忽略(wxhr:insurance_expireignore)")
	@SysLog("到期提醒批量忽略")
	@PutMapping("/batchexpireIgnore")
	@PreAuthorize("@pms.hasPermission('wxhr:insurance_expireignore')")
	public R<Object> batchexpireIgnore(@RequestBody BatchIgnoreVo vo) {
		if (null == vo || vo.getIds() == null || vo.getIds().size() == 0){
			return R.failed(CommonConstants.PARAM_IS_NOT_ERROR);
		}
		return insuranceDetailService.batchexpireIgnore(vo.getIds(),vo.getRemark());
	}

	/**
	 * @Description: 商险待续保的批量忽略
	 * @Author: hgw
	 * @Date: 2025/11/17 10:05
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R<java.lang.Object>
	 **/
	@Operation(description = "商险待续保的批量忽略")
	@SysLog("商险待续保的批量忽略")
	@PostMapping("/batchIgnoreInsuranceAlert")
	public R<Object> batchIgnoreInsuranceAlert(@RequestBody BatchIgnoreVo vo) {
		if (null == vo || vo.getIds() == null || vo.getIds().isEmpty()){
			return R.failed(CommonConstants.PARAM_IS_NOT_ERROR);
		}
		return insuranceDetailService.batchIgnoreInsuranceAlert(vo.getIds(),vo.getRemark());
	}

	/**
	 * @Author fxj
	 * @Description  工作台消息提醒：商险到期总数通知提醒
	 * @Date 17:03 2023/2/16
	**/
    @Operation(description = "商险到期总数通知提醒")
	@PostMapping("/workBranch/getWorkBanchInsuranceMessageWarn")
	public R<Integer> getExpireInsuranceWarn() {
		return insuranceDetailService.getExpireInsuranceWarn();
	}

	@Operation(description = "商险到期个人总数提醒")
	@PostMapping("/getUserInsuranceMessageWarnCount")
	public R<Integer> getUserInsuranceMessageWarnCount(@RequestParam String userName) {
		return insuranceDetailService.getUserInsuranceMessageWarnCount(userName);
	}

	@Operation(description = "每天刷新商险到期提醒信息")
	@Inner
	@PostMapping("/inner/createInsuranceAlert")
	public void createInsuranceAlert() {
		insuranceWarnService.createInsuranceAlert();
	}

	/**
	 * @Author fxj
	 * @Description 每日09:00推送一条整合的待办数据——只推送前端客服，详情去HRO处理——消息待定：项目名称-项目编码、商险临期人数：**；商险过期人数：**；
	 * ——项目数：***、商险临期人数：***、商险过期人数：***，请及时至HRO系统进行处理，以免人员脱保！；
	 * @Date 18:55 2025/4/23
	**/
	@Operation(description = "每天9：00推送整合后的待办商险数据给对应项目的前端客服信息")
	@Inner
	@PostMapping("/inner/pushInsuranceAlertToWx")
	public void pushInsuranceAlertToWx() {
		insuranceWarnService.pushInsuranceAlertToWx();
	}
}
