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

import cn.hutool.core.util.CharsetUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
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.log.annotation.SysLog;
import com.yifu.cloud.plus.v1.yifu.insurances.constants.InsurancesConstants;
import com.yifu.cloud.plus.v1.yifu.insurances.entity.TInsuranceDetail;
import com.yifu.cloud.plus.v1.yifu.insurances.service.insurance.TInsuranceDetailService;
import com.yifu.cloud.plus.v1.yifu.insurances.vo.TBusinessInsuranceVo;
import com.yifu.cloud.plus.v1.yifu.insurances.vo.WorkBanchWarnVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.ArrayList;
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;
	/**
	 * @Author fxj
	 * @Description 商险到期提醒分页查询
	 * @Date 16:51 2023/2/16
	**/
	@Operation(description = "商险到期提醒分页查询")
	@GetMapping("/getExpireWarn/Vopage")
	public R<IPage<TBusinessInsuranceVo>> getTBusinessInsuranceExpireWarnVoPage(Page<TBusinessInsuranceVo> page, TBusinessInsuranceVo tBusinessInsurance) {
		return R.ok(insuranceDetailService.getInsuranceExpireWarnVoPage(page, tBusinessInsurance));
	}

	/**
	 * @Author fxj
	 * @Description 备注批量导入
	 * @Date 16:57 2023/2/16
	**/
	@Operation(description = "备注批量导入(wxhr:insurance_remark_add)")
	@SysLog("备注批量导入")
	@PostMapping("/remark/batchAdd")
	public R<Object> remarkBatchAdd(@RequestBody String jsonString) {
		/*ExcelUtil<TBusinessInsuranceVo> util1 = new ExcelUtil<>(TBusinessInsuranceVo.class);
		util1.parseJsonStringToList(jsonString, BasicServiceUtil.initMapForExport(remoteSysDictService, preRemoteBasicArchivesService));
		List<TBusinessInsuranceVo> businessInsuranceVoList = util1.getEntityList();
		if (businessInsuranceVoList.isEmpty()) {
			return Result.fail("无数据");
		}
		if (null != util1.getErrorInfo() && !util1.getErrorInfo().isEmpty()) {
			return Result.fail(InsuranceConstant.DATA_CHECK_FAIL, util1.getErrorInfo());
		}*/
		return null;//insuranceDetailService.remarkBatchAdd(businessInsuranceVoList);
	}


	/**
	 * @Author fxj
	 * @Description 商险到期提醒分页查询导出
	 * @Date 16:57 2023/2/16
	**/
	@GetMapping("/getExpireWarn/export")
	@Operation(description = "商险到期提醒分页查询导出 hasPermission('wxhr:insurance_expirewarnexport')")
	@SysLog("商险到期提醒分页查询导出")
	@PreAuthorize("@pms.hasPermission('wxhr:insurance_expirewarnexport')")
	public void getExpireWarnExport(HttpServletResponse response, HttpServletRequest request,
									@RequestParam(name = "idStr", required = false) String[] idstr,
									@RequestParam(name = "exportFields", required = true) String[] exportFields, TBusinessInsuranceVo vo) {
		List<TBusinessInsuranceVo> list = null;
	}

	/**
	 * @Author fxj
	 * @Description 到期提醒备注
	 * @Date 17:01 2023/2/16
	**/
	@Operation(description = "到期提醒备注")
	@SysLog("到期提醒备注")
	@PutMapping("/expireRemark")
	public R<Object> expireRemark(@RequestBody TBusinessInsuranceVo vo) {
		if (Common.isEmpty(vo.getId()) || Common.isEmpty(vo.getExpireRemark())) {
			return R.failed(InsurancesConstants.UPLOAD_PARAM_CHECK_FAIL);
		}
		TInsuranceDetail entity = insuranceDetailService.getById(vo.getId());
		if (entity == null) {
			return R.failed("未查询到相关商险订单");
		}
	/*	entity.setExpireRemark(vo.getExpireRemark());
		entity.setLastUpdateUser(SecurityUtils.getUser().getNickName());
		entity.setLastUpdateUserId(SecurityUtils.getUser().getId());
		entity.setLastUpdateTime(LocalDateTime.now());*/
		insuranceDetailService.updateById(entity);
		return R.ok("操作成功");
	}

	/**
	 * @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) {
		if (Common.isEmpty(vo.getId())) {
			return R.failed(InsurancesConstants.UPLOAD_PARAM_CHECK_FAIL);
		}
		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(@RequestParam("ids") String ids) {
		return insuranceDetailService.batchexpireIgnore(ids);
	}


	/**
	 * @Author: wangan
	 * @Date: 2020/5/18
	 * @Description: 到期提醒忽略
	 * @return: com.yifu.cloud.v1.common.core.util.R
	 **/
	/*@Operation(description = "到期提醒忽略(wxhr:insurance_expireignore)")
	@SysLog("到期提醒忽略")
	@PutMapping("/batchExpireIgnore")
	@PreAuthorize("@pms.hasPermission('wxhr:insurance_expireignore')")
	public Result<Object> batchExpireIgnoreList(@RequestBody List<TBusinessInsurance> voList) {
		if (voList.isEmpty()) {
			return Result.fail("无数据");
		}
		ArrayList<ErrorMessage> errorMessages = Lists.newArrayList();
		for (int i = 0; i < voList.size(); i++) {
			TBusinessInsurance vo = voList.get(i);
			if (Common.isEmpty(vo.getId())) {
				errorMessages.add(new ErrorMessage(i + 1, "提醒商险ID不能为空"));
				continue;
			}
			TBusinessInsurance entity = tBusinessInsuranceService.getById(vo.getId());
			if (entity == null) {
				errorMessages.add(new ErrorMessage(i + 1, "未查询到相关商险订单"));
				continue;
			}
			try {
				entity.setExpireIgnoreFlag(CommonConstants.dingleDigitStrArray[0]);
				entity.setLastUpdateUser(SecurityUtils.getUser().getNickName());
				entity.setLastUpdateUserId(SecurityUtils.getUser().getId());
				entity.setLastUpdateTime(LocalDateTime.now());
				tBusinessInsuranceService.updateById(entity);
			} catch (Exception e) {
				log.error("执行异常", e);
				errorMessages.add(new ErrorMessage(i + 1, "执行忽略失败"));
				continue;
			}
		}
		if (!errorMessages.isEmpty()) {
			return Result.fail("数据条数" + voList.size(), voList, errorMessages);
		}
		return Result.success("数据条数" + voList.size(), voList, errorMessages);
	}*/

	/**
	 * @Author fxj
	 * @Description  工作台消息提醒：商险到期总数通知提醒/商险未购买总数通知提醒
	 * @Date 17:03 2023/2/16
	**/
    @Operation(description = "商险到期总数通知提醒/商险未购买总数通知提醒")
	@PostMapping("/inner/workBranch/getWorkBanchInsuranceMessageWarn")
	public R<List<WorkBanchWarnVo>> getExpireInsuranceWarn(@RequestBody
														   Integer expireInsuranceWarnNameUrlFlag,
														   Integer noBuyInsuranceWarnNameUrlFlag,
														   Integer noSettleInsuranceWarnNameUrlFlag, Boolean haveAllOrg, Integer userId) {
		return insuranceDetailService.getExpireInsuranceWarn(expireInsuranceWarnNameUrlFlag, noBuyInsuranceWarnNameUrlFlag, noSettleInsuranceWarnNameUrlFlag, haveAllOrg, userId);
	}
}
