package org.springblade.modules.csjcInfo.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springblade.common.utils.HttpUtil;
import org.springblade.common.utils.JsonDataSecurity;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.csInfo.entity.CsInfoEntity;
import org.springblade.modules.csInfo.service.ICsInfoService;
import org.springblade.modules.csjcInfo.entity.CsjcInfoEntity;
import org.springblade.modules.csjcInfo.excel.CsjcInfoExcel;
import org.springblade.modules.csjcInfo.service.ICsjcInfoService;
import org.springblade.modules.csjcInfo.vo.CsjcInfoVO;
import org.springblade.modules.csjcInfo.wrapper.CsjcInfoWrapper;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.system.service.IUserService;
import org.springblade.modules.system.vo.DeptVO;
import org.springblade.modules.yhyj.entity.YhyjEntity;
import org.springblade.modules.yhyj.service.IYhyjService;
import org.springblade.modules.yhyj.vo.InspectionVO;
import org.springblade.modules.yhyj.vo.YhyjParamsVO;
import org.springblade.modules.yhyj.vo.YhyjResultVO;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.*;

/**
 * 场所检查 控制器
 *
 * @author BladeX
 * @since 2023-07-21
 */
@RestController
@AllArgsConstructor
@RequestMapping("csjcInfo/info")
@Api(value = "场所检查", tags = "场所检查接口")
public class CsjcInfoController extends BladeController {

	private final ICsjcInfoService infoService;

	private final ICsInfoService csInfoService;

	private final IDeptService deptService;

	private final IUserService userService;

	private final IYhyjService yhyjService;

	/**
	 * 场所检查 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入info")
	public R detail(Long id) {
		CsjcInfoVO detail = infoService.getDetail(id);
		return R.data(detail);
	}
	/**
	 * 场所检查 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入info")
	public R<IPage<CsjcInfoVO>> list(@ApiIgnore @RequestParam Map<String, Object> info, Query query) {
		IPage<CsjcInfoEntity> pages = infoService.page(Condition.getPage(query), Condition.getQueryWrapper(info, CsjcInfoEntity.class));
		return R.data(CsjcInfoWrapper.build().pageVO(pages));
	}

	/**
	 * 场所检查 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入info")
	public R page(@RequestParam Map<String, Object> params, Query query) {
		params.put("deptId", AuthUtil.getDeptId());
		IPage<CsjcInfoVO> pages = infoService.selectInfoPage(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * 场所检查 自定义分页
	 */
	@GetMapping("/getPageByCsId")
	public R getPageByCsId(@RequestParam Map<String, Object> params, Query query) {
		IPage<CsjcInfoVO> pages = infoService.getPageByCsId(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * 移交记录-派出所
	 */
	@GetMapping("/getQsfkList")
	public R getQsfkList(@RequestParam Map<String, Object> params, Query query) {
		params.put("deptId", AuthUtil.getDeptId());
		IPage<CsjcInfoVO> pages = infoService.getQsfkList(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * 消防app-整改列表
	 */
	@GetMapping("/getZgList")
	public R getZgList(@RequestParam Map<String, Object> params, Query query) {
		params.put("deptId", AuthUtil.getDeptId());
		IPage<CsjcInfoVO> pages = infoService.getZgList(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * 场所检查 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入info")
	public R save(@Valid @RequestBody CsjcInfoEntity info) {
		return R.status(infoService.save(info));
	}

	/**
	 * 场所检查 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入info")
	public R update(@Valid @RequestBody CsjcInfoEntity info) {
		return R.status(infoService.updateById(info));
	}

	/**
	 * 场所检查 新增或修改
	 */
	@DSTransactional
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入info")
	public R submit(@Valid @RequestBody CsjcInfoEntity info) {
		User user = userService.getById(AuthUtil.getUserId());
		if(StringUtils.isNotBlank(info.getPj()) && info.getPjsj() == null){
			info.setPjsj(new Date());
			info.setPjr(user.getName());
		}
		infoService.saveOrUpdate(info);
		// 向吉林数据库插入
		if (deptService.getById(Long.valueOf(AuthUtil.getDeptId().split(",")[0])).getAncestors().contains("220200000000")) {
			infoService.saveToJl(info);
		}
		return R.status(csInfoService.updateLastJcId(info.getCsId(), info.getId()));
	}

	/**
	 * 场所检查 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(infoService.deleteLogic(Func.toLongList(ids)));
	}


	/**
	 * 导出数据
	 */
	@GetMapping("/export-info")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出数据", notes = "传入info")
	public void exportInfo(@RequestParam Map<String, Object> params, HttpServletResponse response) {
		params.put("deptId", AuthUtil.getDeptId());
		List<CsjcInfoExcel> list = infoService.exportInfo(params);
		ExcelUtil.export(response, "消防检查管理" + DateUtil.time(), "消防检查管理", list, CsjcInfoExcel.class);
	}

	/**
	 * app获取消防部门下拉框
	 */
	@GetMapping("/getXfbm")
	public R getXfbm(@RequestParam Map<String, Object> params) {
		List<DeptVO> data = deptService.getXfbm();
		return R.data(data);
	}

	/**
	 * 场所检查 修改
	 */
	@PostMapping("/updates")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入info")
	public R updates(@Valid @RequestBody CsjcInfoEntity info) {
		return R.status(infoService.updateById(info));
	}


	@RequestMapping("/generaWord")
	public void genera(HttpServletResponse response,String id) {

//		CsjcInfoEntity info = new CsjcInfoEntity();
//		info.setId(Long.parseLong(id));
//		CsjcInfoEntity csjcInfoEntity = infoService.getOne(Condition.getQueryWrapper(info));
//
//		CsInfoEntity csInfo = new CsInfoEntity();
//		csInfo.setId(csjcInfoEntity.getCsId());
//		CsInfoEntity csInfoEntity = csInfoService.getOne(Condition.getQueryWrapper(csInfo));

		CsjcInfoEntity csjcInfoEntity = infoService.getPublicCsjcInfo(Long.parseLong(id));
		CsInfoEntity csInfoEntity = csInfoService.getPublicCsInfo(csjcInfoEntity.getCsId());
		//1.组装数据
		Map<String, Object> params = new HashMap<>();
		params.put("dwcsmc", csInfoEntity.getCsmc());
		params.put("zyfzr", csInfoEntity.getFzr());
		params.put("address", csInfoEntity.getDz());
		params.put("dwlb", csInfoEntity.getDwlb());
		params.put("jzmj", csInfoEntity.getJzmj());
		params.put("jtcs", csInfoEntity.getJzcs());
		params.put("jzgd", csInfoEntity.getGd());
		params.put("jdjcr", csjcInfoEntity.getStry());
		params.put("stjcr", csjcInfoEntity.getBjcdwstry());//记录表
		params.put("jcrq", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(csjcInfoEntity.getCreateTime()));//记录表
		params.put("xfaqzd", "有".equals(csjcInfoEntity.getAqzd()) ? "☑有 □无" : "□有 ☑无");//记录表
		params.put("aqjypx", "组织开展".equals(csjcInfoEntity.getAqjypx()) ? "☑组织开展 □未组织开展" : "□组织开展 ☑未组织开展");//记录表
		params.put("fhjc", "组织开展".equals(csjcInfoEntity.getFhjc()) ? "☑组织开展 □未组织开展" : "□组织开展 ☑未组织开展");//记录表
		params.put("ssya", "有".equals(csjcInfoEntity.getSsya()) ? "☑有 □无" : "□有 ☑无");//记录表
		params.put("xfyl", "组织".equals(csjcInfoEntity.getXfyl()) ? "☑组织  □未组织" : "□组织  ☑未组织");//记录表
		params.put("xfaqglQtqk", csjcInfoEntity.getXfaqglQtqk());//记录表
		params.put("xfctd", "畅通".equals(csjcInfoEntity.getXfctd()) ? "☑畅通   □被堵塞、占用   □无" : ("无".equals(csjcInfoEntity.getXfctd()) ? "□畅通   □被堵塞、占用   ☑无":"□畅通   ☑被堵塞、占用   □无"));//记录表
		params.put("sstd", "畅通".equals(csjcInfoEntity.getSstd()) ? "☑畅通   □堵塞   □锁闭" : ("堵塞".equals(csjcInfoEntity.getSstd()) ? "□畅通   ☑堵塞   □锁闭":"□畅通   □堵塞   ☑锁闭"));//记录表
		params.put("aqck", "畅通".equals(csjcInfoEntity.getAqck()) ? "☑畅通   □堵塞   □锁闭  □缺少" : ("堵塞".equals(csjcInfoEntity.getAqck()) ? "□畅通   ☑堵塞   □锁闭  □缺少":("锁闭".equals(csjcInfoEntity.getAqck()) ? "□畅通   □堵塞   ☑锁闭  □缺少":"□畅通   □堵塞   □锁闭  ☑缺少")));//记录表
		params.put("fhm", "完好有效".equals(csjcInfoEntity.getFhm()) ? "☑完好有效  □常闭式防火门常开                   \n" +
			"                □损坏      □不涉及" : ("常闭式防火门常开".equals(csjcInfoEntity.getFhm()) ? "□完好有效  ☑常闭式防火门常开                   \n" +
			"                □损坏      □不涉及":("损坏".equals(csjcInfoEntity.getFhm()) ? "□完好有效  □常闭式防火门常开                   \n" +
			"                ☑损坏      □不涉及":"□完好有效  □常闭式防火门常开                   \n" +
			"                □损坏      ☑不涉及")));//记录表
		params.put("sszsbz", "完好有效".equals(csjcInfoEntity.getSszsbz()) ? "☑完好有效  □损坏  □缺少  □无" : ("损坏".equals(csjcInfoEntity.getSszsbz()) ? "□完好有效  ☑损坏  □缺少  □无":("缺少".equals(csjcInfoEntity.getSszsbz()) ? "□完好有效  □损坏  ☑缺少  □无":"□完好有效  □损坏  □缺少  ☑无")));//记录表
		params.put("zaw", "否".equals(csjcInfoEntity.getZaw()) ? "☑否  □是  □不涉及" : ("是".equals(csjcInfoEntity.getZaw()) ? "□否  ☑是  □不涉及":"□否  □是  ☑不涉及"));
		params.put("yjzm", "完好有效".equals(csjcInfoEntity.getYjzm()) ? "☑完好有效  □损坏  □缺少  □无" : ("损坏".equals(csjcInfoEntity.getYjzm()) ? "□完好有效  ☑损坏  □缺少  □无":("缺少".equals(csjcInfoEntity.getYjzm()) ? "□完好有效  □损坏  ☑缺少  □无":"□完好有效  □损坏  □缺少  ☑无")));//记录表
		params.put("jzfhQtqk", csjcInfoEntity.getJzfhQtqk());//记录表

		//1. □未设置  □完好有效 □损坏  □无水
		//                □配件不齐  □被遮挡、圈占   □不涉及
		if("未设置".equals(csjcInfoEntity.getSnxhs())){
			params.put("snxhs","1. ☑未设置  □完好有效 □损坏  □无水 \n" +
				"                □配件不齐  □被遮挡、圈占   □不涉及");
		}
		if("完好有效".equals(csjcInfoEntity.getSnxhs())){
			params.put("snxhs","1. □未设置  ☑完好有效 □损坏  □无水 \n" +
				"                □配件不齐  □被遮挡、圈占   □不涉及");
		}
		if("损坏".equals(csjcInfoEntity.getSnxhs())){
			params.put("snxhs","1. □未设置  □完好有效 ☑损坏  □无水 \n" +
				"                □配件不齐  □被遮挡、圈占   □不涉及");
		}
		if("无水".equals(csjcInfoEntity.getSnxhs())){
			params.put("snxhs","1. □未设置  □完好有效 □损坏  ☑无水 \n" +
				"                □配件不齐  □被遮挡、圈占   □不涉及");
		}
		if("配件不齐".equals(csjcInfoEntity.getSnxhs())){
			params.put("snxhs","1. □未设置  □完好有效 □损坏  □无水 \n" +
				"                ☑配件不齐  □被遮挡、圈占   □不涉及");
		}
		if("被遮挡、圈占".equals(csjcInfoEntity.getSnxhs())){
			params.put("snxhs","1. □未设置  □完好有效 □损坏  □无水 \n" +
				"                □配件不齐  ☑被遮挡、圈占   □不涉及");
		}
		if("不涉及".equals(csjcInfoEntity.getSnxhs())){
			params.put("snxhs","1. □未设置  □完好有效 □损坏  □无水 \n" +
				"                □配件不齐  □被遮挡、圈占   ☑不涉及");
		}
		//2.□未配置 □完好有效   □失效  □缺少
		//                □配置类型错误        □设置地点不当
		if("未配置".equals(csjcInfoEntity.getMhq())){
			params.put("mhq","☑未配置 □完好有效   □失效  □缺少\n" +
				"                □配置类型错误        □设置地点不当");
		}
		if("完好有效".equals(csjcInfoEntity.getMhq())){
			params.put("mhq","□未配置 ☑完好有效   □失效  □缺少\n" +
				"                □配置类型错误        □设置地点不当");
		}
		if("失效".equals(csjcInfoEntity.getMhq())){
			params.put("mhq","□未配置 □完好有效   ☑失效  □缺少\n" +
				"                □配置类型错误        □设置地点不当");
		}
		if("缺少".equals(csjcInfoEntity.getMhq())){
			params.put("mhq","□未配置 □完好有效   □失效  ☑缺少\n" +
				"                □配置类型错误        □设置地点不当");
		}

		if("配置类型错误".equals(csjcInfoEntity.getMhq())){
			params.put("mhq","□未配置 □完好有效   □失效  □缺少\n" +
				"                ☑配置类型错误        □设置地点不当");
		}
		if("设置地点不当".equals(csjcInfoEntity.getMhq())){
			params.put("mhq","□未配置 □完好有效   □失效  □缺少\n" +
				"                □配置类型错误        ☑设置地点不当");
		}
		//3.□定期维修保养并记录  □无记录
		//  □未定期维修保养      □不涉及

		if("定期维修保养并记录".equals(csjcInfoEntity.getJzxfss())){
			params.put("jzxfss","☑定期维修保养并记录  □无记录\n" +
				"  □未定期维修保养      □不涉及");
		}
		if("无记录".equals(csjcInfoEntity.getJzxfss())){
			params.put("jzxfss","□定期维修保养并记录  ☑无记录\n" +
				"  □未定期维修保养      □不涉及");
		}
		if("未定期维修保养".equals(csjcInfoEntity.getJzxfss())){
			params.put("jzxfss","□定期维修保养并记录  □无记录\n" +
				"  ☑未定期维修保养      □不涉及");
		}
		if("不涉及".equals(csjcInfoEntity.getJzxfss())){
			params.put("jzxfss","□定期维修保养并记录  □无记录\n" +
				"  □未定期维修保养      ☑不涉及");
		}
		params.put("mh", "否".equals(csjcInfoEntity.getMh()) ? "☑否  □是  □不涉及" : ("是".equals(csjcInfoEntity.getMh()) ? "□否  ☑是  □不涉及":"□否  □是  ☑不涉及"));
		params.put("wxpcs", "否".equals(csjcInfoEntity.getWxpcs()) ? "☑否  □是  □不涉及" : ("是".equals(csjcInfoEntity.getWxpcs()) ? "□否  ☑是  □不涉及":"□否  □是  ☑不涉及"));
		params.put("jzcs", "否".equals(csjcInfoEntity.getJzcs()) ? "☑否  □是  □不涉及" : ("是".equals(csjcInfoEntity.getJzcs()) ? "□否  ☑是  □不涉及":"□否  □是  ☑不涉及"));
		params.put("bz", csjcInfoEntity.getBz());//记录表

		LoopRowTableRenderPolicy hackLoopTableRenderPolicy = new LoopRowTableRenderPolicy();
		Configure config =
			Configure.builder()
				.bind("setKjjxList", hackLoopTableRenderPolicy)
				.bind("setLgsjList", hackLoopTableRenderPolicy)
				.bind("setLwzzList", hackLoopTableRenderPolicy)
				.bind("setRzqkList", hackLoopTableRenderPolicy)
				.bind("setZlqkList", hackLoopTableRenderPolicy).build();

		//2.获取根目录，创建模板文件
		String path = copyTempFile("word/fujiantwo.docx");
//		String fileName = System.currentTimeMillis() + ".docx";
		String fileName = "场所检查-" + csInfoEntity.getCsmc() + ".docx";
		String tmpPath = path + fileName;
		try {
			//3.将模板文件写入到根目录
			//4.编译模板，渲染数据
			XWPFTemplate template = XWPFTemplate.compile(path, config).render(params);
			//5.写入到指定目录位置
			FileOutputStream fos = new FileOutputStream(tmpPath);
			template.write(fos);
			fos.flush();
			fos.close();
			template.close();
//			//6.提供前端下载
			infoService.down(response, tmpPath, fileName);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			//7.删除临时文件
			File file = new File(tmpPath);
			file.delete();
			File copyFile = new File(path);
			copyFile.delete();
		}
	}
	/**
	 * 将项目中的模板文件拷贝到根目录下
	 *
	 * @return
	 */
	private String copyTempFile(String templeFilePath) {
		InputStream inputStream = getClass().getClassLoader().getResourceAsStream(templeFilePath);
		String tempFileName = System.getProperty("user.home") + "/" + "fujiantwo.docx";
//		String tempFileName = "D:\\fujian\\fujiantwo.docx";
		File tempFile = new File(tempFileName);
		try {
			FileUtils.copyInputStreamToFile(inputStream, tempFile);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		return tempFile.getPath();
	}

	private String getUrl() throws SocketException {
		// 本地测试地址
		String url = "https://30m3717k91.goho.co";
		Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
		for (NetworkInterface networkInterface : Collections.list(networkInterfaces)) {
			Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
			for (InetAddress inetAddress : Collections.list(inetAddresses)) {
				if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()) {
					if (inetAddress.getHostAddress().contains("192.168.50")) {
						// 互联网线上地址
						url = "https://aqjdjczhgl.ajj.changchun.gov.cn/gsdss-api/si";
					}
				}
			}
		}
		return url;
	}

	private InspectionVO getSign(InspectionVO inspection) throws Exception {
		Instant instant = Instant.now();
		long timestamp = instant.getEpochSecond();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("creditCode", inspection.getCreditCode());
		jsonObject.put("timestamp", timestamp);
		String sign = JsonDataSecurity.encryptJsonData(jsonObject, "bN24thtUgKaxzkkl");
		inspection.setSign(sign);
		inspection.setTimestamp(timestamp);
		return inspection;
	}

	/**
	 * 隐患录入
	 * 根据“社会统一信用代码”，确定企业录入隐患
	 */
	@Transactional
	@PostMapping("/yhlr")
	public R yhlr(@RequestBody YhyjParamsVO yhyjParamsVO) {
		String result = null;
		try {
			// 获取三方接口的鉴权信息
			InspectionVO inspectionVO = getSign(yhyjParamsVO.getInspection());
			yhyjParamsVO.setInspection(inspectionVO);
			// 调用三方接口，拿到接口的返回结果
			result = HttpUtil.doHttpsPost(getUrl() + "/api/inspection/p/v1/add", JSONObject.toJSONString(yhyjParamsVO.getInspection()));
			YhyjResultVO yhyjResultVO = JSONObject.parseObject(result, YhyjResultVO.class);
			if (yhyjResultVO == null) {
				return R.fail("隐患上报失败");
			} else if (yhyjResultVO.getStatus() == 1300001) {
				return R.fail("参数校验异常，请联系管理员");
			} else if (yhyjResultVO.getStatus() == 1300002) {
				QueryWrapper<YhyjEntity> queryWrapper = new QueryWrapper<>();
				queryWrapper.eq("csjc_id", Long.valueOf(yhyjParamsVO.getCsjcId()));
				YhyjEntity yhyjEntity = yhyjService.getOne(queryWrapper);
				if (null == yhyjEntity) {
					yhyjEntity = new YhyjEntity();
					yhyjEntity.setCsId(Long.valueOf(yhyjParamsVO.getCsId()));
					yhyjEntity.setTyxydm(yhyjParamsVO.getTyxydm());
					yhyjEntity.setCsjcId(Long.valueOf(yhyjParamsVO.getCsjcId()));
					yhyjEntity.setJcdId("社会统一信用代码不正确");
					yhyjService.save(yhyjEntity);
				}
//				CsInfoEntity csInfoEntity = csInfoService.getById(Long.valueOf(yhyjParamsVO.getCsId()));
//				List<CsInfoEntity> jxcsList = yhyjService.getJxcsList(csInfoEntity);
//				return R.data(201, jxcsList, "操作成功");
				return R.data(202, new ArrayList<CsInfoEntity>(), "操作成功");
//				return R.fail("社会统一信用代码不正确，应急局无法处理，该企业不属于试点街道(宽城区柳影街道)");
			} else if (yhyjResultVO.getStatus() == 1300003) {
				return R.fail("服务异常，请稍后再试");
			} else if (yhyjResultVO.getStatus() == 1300004) {
				return R.fail("业务逻辑异常，请联系管理员");
			} else if (yhyjResultVO.getStatus() == 1300005) {
				return R.fail("HTTP请求异常，请联系管理员");
			} else if (yhyjResultVO.getStatus() == 1300006) {
				return R.fail("服务请求异常，请联系管理员");
			} else if (yhyjResultVO.getStatus() == 1300007) {
				return R.fail("客户端请求异常，请联系管理员");
			} else if (yhyjResultVO.getStatus() == 1300008) {
				return R.fail("登录失败，请联系管理员");
			} else {
				// 更改检查记录的移交状态
				CsjcInfoEntity csjcInfoEntity = new CsjcInfoEntity();
				csjcInfoEntity.setId(Long.valueOf(yhyjParamsVO.getCsjcId()));
				csjcInfoEntity.setYjzt(2);
				csjcInfoEntity.setYjrq(new Date());
				infoService.updateById(csjcInfoEntity);
				QueryWrapper<YhyjEntity> queryWrapper = new QueryWrapper<>();
				queryWrapper.eq("csjc_id", Long.valueOf(yhyjParamsVO.getCsjcId()));
				YhyjEntity yhyjEntity = yhyjService.getOne(queryWrapper);
				// 保存移交记录
				if (null == yhyjEntity) {
					yhyjEntity = new YhyjEntity();
					yhyjEntity.setCsId(Long.valueOf(yhyjParamsVO.getCsId()));
					yhyjEntity.setTyxydm(yhyjParamsVO.getTyxydm());
					yhyjEntity.setCsjcId(Long.valueOf(yhyjParamsVO.getCsjcId()));
					yhyjEntity.setJcdId(yhyjResultVO.getData());
					yhyjService.save(yhyjEntity);
				} else {
					yhyjEntity.setJcdId(yhyjResultVO.getData());
					yhyjService.updateById(yhyjEntity);
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return R.success("操作成功");
	}

	/**
	 * 隐患处理查询
	 * 根据录入时返回的检查id作为查询条件查询隐患处理结果
	 */
	@GetMapping("/getYhcl")
	public Object getYhcl(String id) {
		String result = null;
		try {
//			result = "{\"status\":200,\"data\":{\"id\":\"ff8080818d737d23018d738182510002\",\"corpId\":\"724217\",\"corpName\":\"吉林省明博慧酒店管理有限公司\",\"gridId\":\"G0104090716\",\"districtCounty\":\"220105000000\",\"districtStreet\":\"220105010000\",\"districtVillage\":\"220105010004\",\"inspectTime\":\"2024-02-0417:44:43\",\"hdFormList\":[{\"id\":\"f48570818d732843018d651282510002\",\"inspectId\":\"ff8080818d737d23018d738182510002\",\"inspectionContent\":\"检查内容xxxxxxxx\",\"rectificationStatus\":\"3\",\"rectificationCycle\":\"1\",\"rectificationDeadline\":\"2024-02-0717:46:15\",\"rectificationTime\":\"2024-02-0510:23:15\",\"updateTime\":\"2024-02-0510:23:15\",\"delayed\":\"0\",\"description\":\"问题描述xxxxxxxx\",\"HdFormFileList\":[{\"hdId\":\"f48570818d732843018d651282510002\",\"type\":\"5\",\"fileId\":\"f4857086251748918d73281282847064\",\"fileUrl\":\"xxxx\"}],\"hdApproveList\":[{\"id\":\"xxxxxxxx\",\"hdId\":\"f48570818d732843018d651282510002\",\"currentStatus\":10,\"handleUser\":\"jsjsjshanx\",\"handleUserName\":\"张三\",\"handleInfo\":\"处理信息如：二道区商务局-汪xx移交二道区安全生产委员会办公室-郑xx\",\"handleTime\":\"2024-02-0506:12:18\"}]}]}}";
			result = HttpUtil.doGet(getUrl() + "/api/inspection/p/v1/info/" + id);
			if (StringUtils.isBlank(result)) {
				return "隐患查询失败";
			}
			// 更新
			YhyjEntity yhyjEntity = new YhyjEntity();
			yhyjEntity.setJcdId(id);
			yhyjEntity.setResult(result);
			yhyjService.updateByJcdId(yhyjEntity);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return JSONObject.parse(result);
	}
}
