package org.springblade.modules.csInfo.controller;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.lang3.StringUtils;
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.BeanUtil;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.modules.csInfo.dto.TimeDTO;
import org.springblade.modules.csInfo.entity.CsInfoEntity;
import org.springblade.modules.csInfo.entity.CsRlb;
import org.springblade.modules.csInfo.excel.*;
import org.springblade.modules.csInfo.service.ICsInfoService;
import org.springblade.modules.csInfo.utils.ExcelUtils;
import org.springblade.modules.csInfo.vo.CsInfoVO;
import org.springblade.modules.csInfo.vo.CsInfoVOS;
import org.springblade.modules.csInfo.vo.InfoVO;
import org.springblade.modules.csInfo.vo.TjfxVO;
import org.springblade.modules.csInfo.wrapper.CsInfoWrapper;
import org.springblade.modules.csjcInfo.entity.CsjcInfoEntity;
import org.springblade.modules.csjcInfo.service.ICsjcInfoService;
import org.springblade.modules.csjcInfo.vo.TxVO;
import org.springblade.modules.infoSb.entity.InfoSbEntity;
import org.springblade.modules.infoSb.service.IInfoSbService;
import org.springblade.modules.log.logCsInfo.entity.LogCsInfoEntity;
import org.springblade.modules.log.logCsInfo.service.ILogCsInfoService;
import org.springblade.modules.log.sjSp.entity.SjSpEntity;
import org.springblade.modules.log.sjSp.service.ISjSpService;
import org.springblade.modules.mdjfdj.service.IMdjfDjService;
import org.springblade.modules.rhzf.fwInfo.service.IFwInfoService;
import org.springblade.modules.rhzf.fwInfo.util.GsonUtil;
import org.springblade.modules.rhzf.fwInfo.vo.LdBzdzVO;
import org.springblade.modules.rwxffkb.service.IRwxffkbService;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.system.service.IDictBizService;
import org.springblade.modules.system.service.IUserService;
import org.springblade.modules.system.vo.DeptAllVO;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

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

	private final ICsInfoService infoService;

	private final ICsjcInfoService csjcInfoService;

	private final IUserService userService;

	private final IDeptService deptService;

	private final IRwxffkbService rwxffkbService;

	private final IMdjfDjService mdjfDjService;

	private final IDictBizService dictBizService;

	private final IInfoSbService infoSbService;

	private final IFwInfoService fwInfoService;

	private final ISjSpService sjSpService;

	private final ILogCsInfoService logCsInfoService;

	private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/**
	 * 场所 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入info")
	public R<CsInfoEntity> detail(CsInfoEntity info) {
		CsInfoEntity detail = infoService.selectCsById(info);
		//CsInfoEntity detail = infoService.getOne(Condition.getQueryWrapper(info));
		/*if (StringUtils.isNotBlank(detail.getCjr())) {
			User user = userService.getById(Long.parseLong(detail.getCjr()));
			detail.setCjr(user.getName());
		}
		if (StringUtils.isNotBlank(detail.getCjdw())) {
			Dept dept = deptService.getById(Long.parseLong(detail.getCjdw()));
			detail.setCjdw(dept.getDeptName());
		}*/
		return R.data(detail);
	}

	/**
	 * 场所 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入info")
	public R<IPage<CsInfoVO>> list(@ApiIgnore @RequestParam Map<String, Object> info, Query query) {
		IPage<CsInfoEntity> pages = infoService.page(Condition.getPage(query), Condition.getQueryWrapper(info, CsInfoEntity.class));
		return R.data(CsInfoWrapper.build().pageVO(pages));
	}

	/**
	 * 场所 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入info")
	public R page(@RequestParam Map<String, Object> params, Query query) {
		// 获取dept_category 14=省厅 4=市局 5=分局 6=派出所 7=社区
		Dept dept = deptService.getDeptCategory(AuthUtil.getDeptId().split(",")[0]);
		String gajgdm = "";
		if (dept.getDeptCategory() == 7) {
			gajgdm = String.valueOf(dept.getId());
			params.put("gajgdm", gajgdm);
		}

		params.put("deptId", AuthUtil.getDeptId());
		if (params.get("cjr") != null) {
			User cjr = userService.getByName(params.get("cjr").toString());
			if (cjr != null){
				params.put("cjr", cjr.getId());
			}else {
				params.put("cjr", "");
			}
		}
		IPage<CsInfoVO> pages = infoService.selectInfoPage(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * 消防检查-提醒
	 */
	@GetMapping("/getOverDue")
	public R getOverDue(@RequestParam Map<String, Object> params) {
		params.put("deptId", AuthUtil.getDeptId());
		params.put("userId", AuthUtil.getUserId());
		// 获取3个月没检查的场所数量
		int noCheck = infoService.getOverDue(params);
		// 消防检查-限期整改超时提醒
		int xqzg = infoService.getXqzgTx(params);
		// 任务下发反馈-待签收提醒
		int rw = rwxffkbService.getRwNum(params);
		// 30天矛盾纠纷未回访提醒
		int jfhf = mdjfDjService.getJfhf(params);
		TxVO tx = new TxVO(noCheck, xqzg, rw, jfhf);
		return R.data(tx);
	}

	/**
	 * 场所 新增
	 */
	@DSTransactional
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入info")
	public R save(@Valid @RequestBody CsInfoEntity info) {
		CsInfoEntity xydm = infoService.selectShtyxydm(info.getTyxydm());
		if (xydm != null) {
			return R.fail("社会信用统一信用代码已存在，请重新填写");
		}
		Long sssqBm = info.getSssqBm();
		if(info.getSssqBm() == null){
			sssqBm = Long.parseLong(info.getSssqName());
		}
		//省厅版本 九小场所id
		String xzqh = sssqBm.toString().substring(0, 6);
		String csId = generateId(xzqh);
		info.setId(Long.parseLong(csId));
		info.setCreateUser(AuthUtil.getUserId());
		info.setCreateDept(sssqBm);
		info.setSssqBm(sssqBm);

		info.setCjr(AuthUtil.getUserId().toString());
		DeptAllVO deptAll = infoService.getParentInfo(sssqBm);
		info.setCjdw(sssqBm.toString());
		info.setSssqMc(deptAll.getSqMc());
		info.setSspcsBm(deptAll.getPcsId());
		info.setSspcsMc(deptAll.getPcsMc());
		info.setSsfjBm(deptAll.getFjId());
		info.setSsfjMc(deptAll.getFjMc());
		infoService.save(info);
		// 向吉林数据库插入
		if (deptService.getById(Long.valueOf(AuthUtil.getDeptId().split(",")[0])).getAncestors().contains("220200000000")) {
			infoService.saveToJl(info);
		}
		// 保存日志
		LogCsInfoEntity csInfoLog = new LogCsInfoEntity();
		BeanUtil.copy(info, csInfoLog);
		logCsInfoService.save(csInfoLog);
		return R.status(true);
	}

	/**
	 * 6位行政区划+13位时间戳+4位随机数  id
	 * @return
	 */
	public static String generateId(String xzqh) {
		StringBuilder id = new StringBuilder();
		id.append(xzqh);
		id.append(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()).substring(8,17));
		id.append(String.format("%03d", new Random().nextInt(1000)));
		return id.toString();
	}

	/**
	 * 查询民警所在派出所的专项行动社区的九小场所集  查询public标下的
	 */
	@GetMapping("/getCsListByPcsBm")
	@ApiOperationSupport(order = 31)
	@ApiOperation(value = "分页", notes = "传入info")
	public R getCsListByPcsBm(@RequestParam Map<String, Object> params, Query query) {
		try {
			String deptId = AuthUtil.getDeptId();
			String[] split = deptId.split(",");
			Dept pcsDept = deptService.getById(Long.parseLong(split[0]));
			params.put("sspcsBm", pcsDept.getParentId().toString());
			IPage<CsInfoVO> pages = infoService.selectInfoPagePublic(Condition.getPage(query), params);
			return R.data(pages);
		} catch (Exception e) {
			return R.fail(602,"调用getCsListByPcsBm接口失败："+e.getMessage());
		}
	}

	/**
	 * 将选中的九小场所ids及认领的社区  存入到jdrh中的cs_info 形成多条数据
	 */
	@PostMapping("addJdrhCsInfo")
	public R addJdrhCsInfo(@RequestParam Map<String, Object> params) throws Exception {
		if (params.get("sssqBm").toString().equals("1")) {
			throw new Exception();
		}
		if(params.containsKey("ids")){
			try {
				String deptId = AuthUtil.getDeptId();
				String[] split = deptId.split(",");
				Dept pcsDept = deptService.getById(Long.parseLong(split[0]));
				String sspcsBm = pcsDept.getParentId().toString();
				String sssqBm = params.get("sssqBm").toString();
				String[] ids = params.get("ids").toString().split(",");
				if(ids.length>0){
					try {
						for (int i = 0; i < ids.length; i++) {
							//保存到cs_info
							Map<String, Object> param = new HashMap<>();
							param.put("id",ids[i]);
							param.put("sspcsBm",sspcsBm);
							List<CsRlb> rlByCszjAndPcsbm = infoService.getRlByCszjAndPcsbm(param);
							if(rlByCszjAndPcsbm.size() == 0){
								CsInfoEntity csInfoEntity = infoService.selectCsInfoByIdAndPcsbm(param);
								if(csInfoEntity != null){
									long l = Long.parseLong(generateId(sspcsBm.substring(0, 6)));
									csInfoEntity.setId(l);
									csInfoEntity.setCreateUser(AuthUtil.getUserId());
									csInfoEntity.setCreateDept(Long.parseLong(sssqBm));
									csInfoEntity.setSssqBm(Long.parseLong(sssqBm));
									csInfoEntity.setSssqMc(pcsDept.getDeptName());
									infoService.save(csInfoEntity);
									//查询检查记录  添加到csjc_info中
									List<CsjcInfoEntity> publicCsjcList = csjcInfoService.getPublicCsjcList(Long.parseLong(ids[i]));
									if(publicCsjcList.size()>0){
										List<CsjcInfoEntity> csjcInfoEntities = new ArrayList<>();
										for (int j = 0; j < publicCsjcList.size(); j++) {
											CsjcInfoEntity csjcInfoEntity = publicCsjcList.get(j);
											csjcInfoEntity.setId(Long.parseLong(generateId(sspcsBm.substring(0,6))));
											csjcInfoEntity.setCreateDept(Long.parseLong(sssqBm));
											csjcInfoEntity.setCreateUser(AuthUtil.getUserId());
											csjcInfoEntity.setCsId(csInfoEntity.getId());
											csjcInfoEntities.add(csjcInfoEntity);
										}
										csjcInfoService.saveBatch(csjcInfoEntities);
									}
									//更新关联表  public中csId
									CsRlb csRlb = new CsRlb();
									csRlb.setId(Long.parseLong(generateId(sssqBm.substring(0,6))));
									csRlb.setRlsq(Long.parseLong(sspcsBm));
									csRlb.setCszj(Long.parseLong(ids[i]));
									csRlb.setCsids(l);
									infoService.addCsRl(csRlb);
								}
							}
						}
					} catch (Exception e) {
						e.printStackTrace();
						return R.fail(602,"调用addJdrhCsInfo循环内部出错："+e.getMessage());
					}
				}
				return R.status(true);
			} catch (Exception e) {
				e.printStackTrace();
				return R.fail(602,"调用addJdrhCsInfo出错："+e.getMessage());
			}
		}else{
			return R.fail(601,"请选择场所数据");
		}
	}

	/**
	 * 九小场所的编辑删除权限
	 */
	@GetMapping("/getPermission")
	public R getPermission() {
		String role = AuthUtil.getUserRole();
		if (role.contains("民警") || role.contains("派出所管理员")) {
			// 有编辑、删除权限
			return R.data("0");
		} else {
			return R.data("0");
		}
	}

	/**
	 * 场所 修改
	 */
	@DSTransactional
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入info")
	public R update(@Valid @RequestBody CsInfoEntity csInfoEntity) throws ParseException {
		CsInfoEntity oldInfo = infoService.getById(csInfoEntity.getId());
		CsInfoEntity csByTyxydm = infoService.getTyxydm(csInfoEntity.getTyxydm());
		if (null != csByTyxydm && !csByTyxydm.getId().equals(csInfoEntity.getId())) {
			return R.fail("当前社会统一信用代码已被 " + csByTyxydm.getCsmc() + " 占用");
		}
		if (StringUtils.isBlank(oldInfo.getTyxydm()) || oldInfo.getTyxydm().contains("Lin") || csInfoEntity.getTyxydm().equals(oldInfo.getTyxydm())) {
			infoService.updateById(csInfoEntity);
			if (deptService.getById(Long.valueOf(AuthUtil.getDeptId().split(",")[0])).getAncestors().contains("220200000000")) {
				// 向吉林数据库修改
				infoService.updateByIdToJl(csInfoEntity);
			}
			// 保存日志
			LogCsInfoEntity csInfoLog = new LogCsInfoEntity();
			BeanUtil.copy(csInfoEntity, csInfoLog);
			logCsInfoService.save(csInfoLog);
		} else {
			save(csInfoEntity);
		}
		return R.status(true);
	}

	/**
	 * 场所 停业歇业 状态修改
	 */
	@Transactional
	@PostMapping("/tyxy")
	public R tyxy(@RequestBody CsInfoEntity csInfoEntity) {
		if (sjSpService.getIsExist(2, String.valueOf(csInfoEntity.getId())) > 0) {
			return R.fail("等待分局审批中，请不要重复提交");
		}
		SjSpEntity sjSpEntity = new SjSpEntity();
		sjSpEntity.setYwlb(2);
		sjSpEntity.setYwid(Long.valueOf(csInfoEntity.getId()));
		sjSpEntity.setParams(csInfoEntity.getYyzt().toString());
		sjSpEntity.setQqyy(csInfoEntity.getQqyy());
		sjSpService.save(sjSpEntity);
		return R.success("营业状态变更，已向分局提交审批");
	}

	/**
	 * 场所 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入info")
	public R submit(@Valid @RequestBody CsInfoEntity info) {
		DeptAllVO deptAll = infoService.getParentInfo(info.getSssqBm());
		info.setSssqMc(deptAll.getSqMc());
		info.setSspcsBm(deptAll.getPcsId());
		info.setSspcsMc(deptAll.getPcsMc());
		info.setSsfjBm(deptAll.getFjId());
		info.setSsfjMc(deptAll.getFjMc());
		infoService.saveOrUpdate(info);
		// 保存日志
		LogCsInfoEntity csInfoLog = new LogCsInfoEntity();
		BeanUtil.copy(info, csInfoLog);
		logCsInfoService.save(csInfoLog);
		return R.status(true);
	}

	/**
	 * 场所 删除
	 * ids qqyy
	 */
	@Transactional
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam Map<String, String> params) {
		String ids = params.get("ids");
//		infoService.deleteLogic(Func.toLongList(ids));
		String idList[] = ids.split(",");
		for (String id : idList) {
			// 查看当前数据是否已经等待审批
			if (sjSpService.getIsExist(1, id) > 0) {
				return R.fail("等待分局审批中，请不要重复提交");
			}
			SjSpEntity sjSpEntity = new SjSpEntity();
			sjSpEntity.setYwlb(1);
			sjSpEntity.setYwid(Long.valueOf(id));
			sjSpEntity.setQqyy(params.get("qqyy"));
			sjSpService.save(sjSpEntity);
		}
		return R.status(true);
	}
	/**
	 * 场所 删除
	 */
	@PostMapping("/deleteYrl")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R deleteYrl(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(infoService.deleteByIds(ids));
	}

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

	/**
	 * 场所下载模板
	 */
	@PostMapping("csxzmb")
	public void csxzmb(HttpServletResponse response) {
		List<Object> head = Arrays.asList("单位/场所名称", "单位类别", "地址", "法人", "法人电话", "消防负责人", "消防负责人身份证号", "消防负责人电话", "房屋类型", "建筑面积", "具体层数", "建筑高度", "注册资本", "成立日期", "经营期限", "经营范围", "工商登记机关", "工商登记日期", "所属派出所", "所属社区", "所属分局");
		List<List<Object>> list = new ArrayList<>();
		list.add(head);
		Map<Integer, List<String>> listMap = new HashMap<>();
		String dwlb = dictBizService.selectDwlb();
		String sq = deptService.selectSq(AuthUtil.getDeptId());
		String pcs = deptService.selectPcs(AuthUtil.getDeptId());
		listMap.put(1, Arrays.asList(dwlb));
		listMap.put(18, Arrays.asList(pcs));
		listMap.put(19, Arrays.asList(sq));
		ExcelUtils.export(response, "场所模板", list, listMap);
	}

	/**
	 * 场所导入
	 */
	@PostMapping("bytdr")
	public R<Boolean> bytdr(MultipartFile file) {
		List<CsInfoEntity> cslist = new ArrayList<>();
		List<CsInfoExcel> list = ExcelUtil.read(file, CsInfoExcel.class);
		for (CsInfoExcel csInfoExcel : list) {
			if (StringUtils.isBlank(csInfoExcel.getCsmc())) {
				return R.fail("单位/场所名称为空,请重新填写");
			}
			Dept ssfjBm = deptService.selectSsfjBm(csInfoExcel.getSsfjName());
			if (null == ssfjBm) {
				return R.fail(csInfoExcel.getCsmc() + " 的所属分局不正确");
			}
			Dept sspcsBm = deptService.selectSspcsBm(csInfoExcel.getSspcsName(), csInfoExcel.getSsfjName());
			if (null == sspcsBm) {
				return R.fail(csInfoExcel.getCsmc() + " 的所属派出所不正确");
			}
			Dept sssqBm = deptService.selectSssqBm(csInfoExcel.getSssqName(), sspcsBm.getId());
			if (null == sssqBm) {
				return R.fail(csInfoExcel.getCsmc() + " 的所属社区不正确");
			}
			csInfoExcel.setSsfjBm(ssfjBm.getId());
			csInfoExcel.setSspcsBm(sspcsBm.getId());
			csInfoExcel.setSssqBm(sssqBm.getId());
			CsInfoEntity cs = BeanUtil.copy(csInfoExcel, CsInfoEntity.class);
			cslist.add(cs);
		}
		return R.data(infoService.saveBatch(cslist));
	}

	/**
	 * 九小场所查询统计
	 */
	@GetMapping("selectCsPcs")
	public R selectCsPcs(CsInfoEntity csInfo, Query query) {
		IPage<CsInfoEntity> page = infoService.selectCsPcs(Condition.getPage(query), csInfo, AuthUtil.getDeptId());
		if (page.getRecords().size() == 0) {
			IPage<CsInfoEntity> pages = infoService.selectSqByDeptname(Condition.getPage(query), csInfo, AuthUtil.getDeptId());
			return R.data(pages);
		}
		for (CsInfoEntity csInfoEntity : page.getRecords()) {
			List<CsInfoVOS> list = infoService.selectCsSq(csInfoEntity.getId(), csInfo.getDatetime());
			csInfoEntity.setChildren(list);
		}
		return R.data(page);
	}

	/**
	 * 根据登陆账号角色判断搜索条件————社区
	 */
	@GetMapping("getJsBySqs")
	public R getJsBySqs(@RequestParam Map<String, Object> params) {
		params.put("deptId", AuthUtil.getDeptId());
		String roleId = AuthUtil.getUser().getRoleId();
		//经侦管理员
		if("1780755525022023699".equals(roleId)){
			if (null == params.get("value")) {
				String s = "220000000000";
				params.put("value",s);
			}
			if (null == params.get("deptCategory")) {
				params.put("deptCategory", "4");
			}
		}
		//户政管理员
		if("1685514703810543617".equals(roleId)){
			if (null == params.get("value")) {
				String s = String.valueOf(deptService.getById(Long.parseLong(AuthUtil.getDeptId())).getParentId());
				params.put("value",s);
			}
			if (null == params.get("deptCategory")) {
				params.put("deptCategory", "5");
			}
		}
		//省厅管理员
		if (AuthUtil.getDeptId().equals("220000000000")){
			if (null == params.get("value")) {
				//先查询分局分局
				String s = "220000000000";
				params.put("value",s);
			}
			if (null == params.get("deptCategory")) {
				params.put("deptCategory", "4");
			}
		}
		//市局管理员
		if (roleId.equals("1684515448861990913")){
			if (null == params.get("value")) {
				Dept byId = deptService.getById(Long.parseLong(AuthUtil.getDeptId()));
				String s = byId.getParentId().toString();
				params.put("value",s);
			}
			if (null == params.get("deptCategory")) {
				params.put("deptCategory", "4");
			}
		}
		//分局管理员
		if("1677215952925745154".equals(roleId)){
			if (null == params.get("value")) {
				Dept dept = deptService.selectSjByFj(AuthUtil.getDeptId());
				String s = dept.getParentId().toString();
				params.put("value",s);
			}
			if (null == params.get("deptCategory")) {
				params.put("deptCategory", "4");
			}
		}
		//派出所管理员
		if("1677215117370056705".equals(roleId)){
			if (null == params.get("value")) {
				Long parentId = deptService.selectSjByPcs(AuthUtil.getDeptId()).getParentId();
				params.put("value",parentId.toString());
			}
			if (null == params.get("deptCategory")) {
				params.put("deptCategory", "4");
			}
		}
		//社区民警
		if("1677215233615192065".equals(roleId)){
			if (null == params.get("value")) {
				String deptId = AuthUtil.getDeptId();
				if (deptId.indexOf(",") >= 0){
					int index = deptId.indexOf(",");
					deptId = deptId.substring(0, index);
				}
				Long parentId = deptService.selectSjBySqmj(deptId).getParentId();
				params.put("value",parentId.toString());
			}
			if (null == params.get("deptCategory")) {
				params.put("deptCategory", "4");
			}
		}
		List<Dept> list = infoService.getDeptList(params);
		return R.data(list);
	}

	/**
	 * 全市九小场所明细-列表
	 */
	@GetMapping("/getQsJxcs")
	public R getQsJxcs(@RequestParam Map<String, Object> params, Query query) {
		params.put("deptId", AuthUtil.getDeptId());
		IPage<QsJxcsExcel> pages = infoService.getQsJxcs(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * 全市九小场所明细-导出
	 */
	@GetMapping("/exportQsJxcs")
	public void exportQsJxcs(@RequestParam Map<String, Object> params, HttpServletResponse response) {
		params.put("deptId", AuthUtil.getDeptId());
		List<QsJxcsExcel> data = infoService.exportQsJxcs(params);
		ExcelUtil.export(response, "场所数据" + DateUtil.time(), "场所数据表", data, QsJxcsExcel.class);
	}

	/**
	 * 消防检查数据统计
	 */
	@GetMapping("/getMjsjtj")
	public R getMjsjtj(@RequestParam Map<String, Object> params, Query query) {
		params.put("deptId", AuthUtil.getDeptId());
		if (params.get("type").toString().equals("sq")) {
			IPage<MjsjtjExcel> pages = infoService.getSqTj(Condition.getPage(query), params);
			return R.data(pages);
		} else if (params.get("type").toString().equals("pcs")) {
			IPage<MjsjtjExcel> pages = infoService.getPcsTj(Condition.getPage(query), params);
			return R.data(pages);
		} else if (params.get("type").toString().equals("fj")) {
			IPage<MjsjtjExcel> pages = infoService.getFjTj(Condition.getPage(query), params);
			return R.data(pages);
		} else {
			IPage<MjsjtjExcel> pages = infoService.getQsTj(Condition.getPage(query), params);
			return R.data(pages);
		}
	}

	/**
	 * 消防检查数据统计-导出
	 */
	@GetMapping("/exportMjsjtj")
	public void exportMjsjtj(@RequestParam Map<String, Object> params, HttpServletResponse response) {
		params.put("deptId", AuthUtil.getDeptId());
		if (params.get("type").toString().equals("sq")) {
			List<MjsjtjExcel> data = infoService.exportSqTj(params);
			ExcelUtil.export(response, "社区消防检查数据" + DateUtil.time(), "社区消防检查数据", data, MjsjtjExcel.class);
		} else if (params.get("type").toString().equals("pcs")) {
			List<MjsjtjExcel> data = infoService.exportPcsTj(params);
			ExcelUtil.export(response, "派出所消防检查数据" + DateUtil.time(), "派出所消防检查数据", data, MjsjtjExcel.class);
		} else if (params.get("type").toString().equals("fj")) {
			List<MjsjtjExcel> data = infoService.exportFjTj(params);
			ExcelUtil.export(response, "分局消防检查数据" + DateUtil.time(), "分局消防检查数据", data, MjsjtjExcel.class);
		} else {
			List<MjsjtjExcel> data = infoService.exportQsTj(params);
			ExcelUtil.export(response, "全市消防检查数据" + DateUtil.time(), "全市消防检查数据", data, MjsjtjExcel.class);
		}
	}

	/**
	 * 查询blade_test 时间下拉框
	 */
	@GetMapping("/getXfjcTime")
	public R getXfjcTime(@RequestParam Map<String, Object> params) {
		List<TimeDTO> data = infoService.getXfjcTime(params);
		return R.data(data);
	}

	/**
	 * 统计分析-市局统计
	 */
	@GetMapping("/getTjfxSjtj")
	public R getTjfxSjtj(Query query, TjfxVO tjfxVO) {
		IPage<TjfxVO> pages = infoService.getTjfxSjtj(Condition.getPage(query), tjfxVO);
		return R.data(pages);
	}
	/**
	 * 统计分析-市局统计导出
	 */
	@GetMapping("/exportTjfxSjtj")
	public void exportTjfxSjtj(HttpServletResponse response) {
		List<TjfxSjExcel> list = infoService.exportTjfxSjtj();
		TjfxSjExcel tjfxSjExcel = new TjfxSjExcel();
		tjfxSjExcel.setSj("合计");
		int yzcyh = 0;
		int ydlyh = 0;
		int jrdlyh = 0;
		int jxcssl = 0;
		int xfjcsl = 0;
		int zgsl = 0;
		int mddjsl = 0;
		int ytjsl = 0;
		int yhfsl = 0;
		int yflsl = 0;
		for (TjfxSjExcel tjfxSjExcel1:list){
			yzcyh += tjfxSjExcel1.getYzcyh();
			ydlyh += tjfxSjExcel1.getYdlyh();
			jrdlyh += tjfxSjExcel1.getJrdlyh();
			jxcssl += tjfxSjExcel1.getJxcssl();
			xfjcsl += tjfxSjExcel1.getXfjcsl();
			zgsl += tjfxSjExcel1.getZgsl();
			mddjsl += tjfxSjExcel1.getMddjsl();
			ytjsl += tjfxSjExcel1.getYtjsl();
			yhfsl += tjfxSjExcel1.getYhfsl();
			yflsl += tjfxSjExcel1.getYflsl();
		}
		tjfxSjExcel.setYzcyh(yzcyh);
		tjfxSjExcel.setYdlyh(ydlyh);
		tjfxSjExcel.setJrdlyh(jrdlyh);
		tjfxSjExcel.setJxcssl(jxcssl);
		tjfxSjExcel.setXfjcsl(xfjcsl);
		tjfxSjExcel.setZgsl(zgsl);
		tjfxSjExcel.setMddjsl(mddjsl);
		tjfxSjExcel.setYtjsl(ytjsl);
		tjfxSjExcel.setYhfsl(yhfsl);
		tjfxSjExcel.setYflsl(yflsl);
		list.add(tjfxSjExcel);
		ExcelUtil.export(response, "统计分析-市局统计" + DateUtil.time(), "统计分析-市局统计", list, TjfxSjExcel.class);
	}

	/**
	 * 统计分析-分局统计
	 */
	@GetMapping("/getTjfxFjtj")
	public R getTjfxFjtj(TjfxVO tjfxVO,Query query) {
		IPage<TjfxVO> pages = infoService.getTjfxFjtj(Condition.getPage(query),tjfxVO);
		return R.data(pages);
	}
	/**
	 * 统计分析-分局统计导出
	 */
	@GetMapping("/exportTjfxFjtj")
	public void exportTjfxFjtj(HttpServletResponse response) {
		List<TjfxFjExcel> list = infoService.exportTjfxFjtj();
		TjfxFjExcel tjfxSjExcel = new TjfxFjExcel();
		tjfxSjExcel.setSj("合计");
		int yzcyh = 0;
		int ydlyh = 0;
		int jrdlyh = 0;
		int jxcssl = 0;
		int xfjcsl = 0;
		int zgsl = 0;
		int mddjsl = 0;
		int ytjsl = 0;
		int yhfsl = 0;
		int yflsl = 0;
		for (TjfxFjExcel tjfxSjExcel1:list){
			yzcyh += tjfxSjExcel1.getYzcyh();
			ydlyh += tjfxSjExcel1.getYdlyh();
			jrdlyh += tjfxSjExcel1.getJrdlyh();
			jxcssl += tjfxSjExcel1.getJxcssl();
			xfjcsl += tjfxSjExcel1.getXfjcsl();
			zgsl += tjfxSjExcel1.getZgsl();
			mddjsl += tjfxSjExcel1.getMddjsl();
			ytjsl += tjfxSjExcel1.getYtjsl();
			yhfsl += tjfxSjExcel1.getYhfsl();
			yflsl += tjfxSjExcel1.getYflsl();
		}
		tjfxSjExcel.setYzcyh(yzcyh);
		tjfxSjExcel.setYdlyh(ydlyh);
		tjfxSjExcel.setJrdlyh(jrdlyh);
		tjfxSjExcel.setJxcssl(jxcssl);
		tjfxSjExcel.setXfjcsl(xfjcsl);
		tjfxSjExcel.setZgsl(zgsl);
		tjfxSjExcel.setMddjsl(mddjsl);
		tjfxSjExcel.setYtjsl(ytjsl);
		tjfxSjExcel.setYhfsl(yhfsl);
		tjfxSjExcel.setYflsl(yflsl);
		list.add(tjfxSjExcel);
		ExcelUtil.export(response, "统计分析-分局统计" + DateUtil.time(), "统计分析-分局统计", list, TjfxFjExcel.class);
	}
	/**
	 * 统计分析-派出所统计
	 */
	@GetMapping("/getTjfxPcstj")
	public R getTjfxPcstj(TjfxVO tjfxVO, Query query) {
		IPage<TjfxVO> pages = infoService.getTjfxPcstj(Condition.getPage(query), tjfxVO);
		return R.data(pages);
	}
	/**
	 * 统计分析-派出所统计导出
	 */
	@GetMapping("/exportTjfxPcstj")
	public void exportTjfxPcstj(HttpServletResponse response) {
		List<TjfxPcsExcel> list = infoService.exportTjfxPcstj();
		TjfxPcsExcel tjfxSjExcel = new TjfxPcsExcel();
		tjfxSjExcel.setSj("合计");
		int yzcyh = 0;
		int ydlyh = 0;
		int jrdlyh = 0;
		int jxcssl = 0;
		int xfjcsl = 0;
		int zgsl = 0;
		int mddjsl = 0;
		int ytjsl = 0;
		int yhfsl = 0;
		int yflsl = 0;
		for (TjfxPcsExcel tjfxSjExcel1:list){
			yzcyh += tjfxSjExcel1.getYzcyh();
			ydlyh += tjfxSjExcel1.getYdlyh();
			jrdlyh += tjfxSjExcel1.getJrdlyh();
			jxcssl += tjfxSjExcel1.getJxcssl();
			xfjcsl += tjfxSjExcel1.getXfjcsl();
			zgsl += tjfxSjExcel1.getZgsl();
			mddjsl += tjfxSjExcel1.getMddjsl();
			ytjsl += tjfxSjExcel1.getYtjsl();
			yhfsl += tjfxSjExcel1.getYhfsl();
			yflsl += tjfxSjExcel1.getYflsl();
		}
		tjfxSjExcel.setYzcyh(yzcyh);
		tjfxSjExcel.setYdlyh(ydlyh);
		tjfxSjExcel.setJrdlyh(jrdlyh);
		tjfxSjExcel.setJxcssl(jxcssl);
		tjfxSjExcel.setXfjcsl(xfjcsl);
		tjfxSjExcel.setZgsl(zgsl);
		tjfxSjExcel.setMddjsl(mddjsl);
		tjfxSjExcel.setYtjsl(ytjsl);
		tjfxSjExcel.setYhfsl(yhfsl);
		tjfxSjExcel.setYflsl(yflsl);
		list.add(tjfxSjExcel);
		ExcelUtil.export(response, "统计分析-派出所统计" + DateUtil.time(), "统计分析-派出所统计", list, TjfxPcsExcel.class);
	}
	/**
	 * 统计分析-民警统计
	 */
	@GetMapping("/getTjfxMjtj")
	public R getTjfxMjtj(TjfxVO tjfxVO, Query query) {
		IPage<TjfxVO> pages = infoService.getTjfxMjtj(Condition.getPage(query),tjfxVO);
		return R.data(pages);
	}
	/**
	 * 统计分析-民警统计导出
	 */
	@GetMapping("/exportTjfxMjtj")
	public void exportTjfxMjtj(HttpServletResponse response) {
		List<TjfxMjExcel> list = infoService.exportTjfxMjtj();
		TjfxMjExcel tjfxSjExcel = new TjfxMjExcel();
		tjfxSjExcel.setSj("合计");
		int jxcssl = 0;
		int xfjcsl = 0;
		int zgsl = 0;
		int mddjsl = 0;
		int ytjsl = 0;
		int yhfsl = 0;
		int yflsl = 0;
		for (TjfxMjExcel tjfxSjExcel1:list){
			jxcssl += tjfxSjExcel1.getJxcssl();
			xfjcsl += tjfxSjExcel1.getXfjcsl();
			zgsl += tjfxSjExcel1.getZgsl();
			mddjsl += tjfxSjExcel1.getMddjsl();
			ytjsl += tjfxSjExcel1.getYtjsl();
			yhfsl += tjfxSjExcel1.getYhfsl();
			yflsl += tjfxSjExcel1.getYflsl();
		}
		tjfxSjExcel.setJxcssl(jxcssl);
		tjfxSjExcel.setXfjcsl(xfjcsl);
		tjfxSjExcel.setZgsl(zgsl);
		tjfxSjExcel.setMddjsl(mddjsl);
		tjfxSjExcel.setYtjsl(ytjsl);
		tjfxSjExcel.setYhfsl(yhfsl);
		tjfxSjExcel.setYflsl(yflsl);
		list.add(tjfxSjExcel);
		ExcelUtil.export(response, "统计分析-民警统计" + DateUtil.time(), "统计分析-民警统计", list, TjfxMjExcel.class);
	}

	/**
	 * 九小场所上报
	 */
	@PostMapping("getJxcsSb")
	public R getJxcsSb(@RequestBody CsInfoEntity csInfoEntity){
		String[] csIds = csInfoEntity.getCsIds();
		List<CsInfoEntity> csinfo = infoService.selectSb(csIds);
		if (csinfo != null) {
			return R.fail("选中的场所拥有已经上报过的场所");
		}
		for (int i = 0; i < csIds.length; i++) {
			InfoSbEntity info = new InfoSbEntity();
			String s = csIds[i];
			info.setCsId(Long.parseLong(s));
			info.setSbdw(csInfoEntity.getSbdw());
			info.setSbpcs(Long.parseLong(AuthUtil.getDeptId()));
			infoSbService.save(info);
		}
		return R.success("上报成功");
	}

	/**
	 * 上报单位
	 */
	@GetMapping("getSbdw")
	public R getSbdw(){
		Dept dept = deptService.getById(Long.parseLong(AuthUtil.getDeptId()));
		Dept byId = deptService.getById(dept.getParentId());
		Dept dw = new Dept();
		dw.setDeptName(byId.getDeptName());
		dw.setId(byId.getId());
		return R.data(dw);
	}

	/**
	 * 九小场所下发至社区
	 */
	@PostMapping("getJxcsXf")
	public R getJxcsXf(@RequestBody CsInfoEntity csInfoEntity){
		InfoSbEntity info = new InfoSbEntity();
		info.setSbzt("1");
		String[] csIds = csInfoEntity.getCsIds();
		Dept dept = deptService.getFjPcsBySq(csInfoEntity.getSssqBm());
		csInfoEntity.setSsfjBm(dept.getFjid());
		csInfoEntity.setSsfjMc(dept.getFjmc());
		csInfoEntity.setSspcsBm(dept.getPcsid());
		csInfoEntity.setSspcsMc(dept.getPcsmc());
		for (int i = 0; i < csIds.length; i++) {
			String s = csIds[i];
			info.setCsId(Long.parseLong(s));
			csInfoEntity.setCsId(s);
			infoSbService.updateByCsId(info);
			infoService.getJxcsXf(csInfoEntity);
		}
		return R.success("下发成功");
	}

	/**
	 * 场所上报列表
	 */
	@GetMapping("getCsLb")
	public R getCsLb(Query query, CsInfoEntity csInfoEntity){
		Dept dept = deptService.getById(Long.parseLong(AuthUtil.getDeptId()));
		IPage<CsInfoEntity> page = infoService.getCsLb(Condition.getPage(query), dept.getId(), csInfoEntity);
		return R.data(page);
	}

	/**
	 * 九小场所删除列表
	 */
	@GetMapping("getJxcsSc")
	public R getJxcsSc(Map<String, Object> params, Query query) {
		params.put("deptId", AuthUtil.getDeptId());
		if (params.get("cjr") != null) {
			User cjr = userService.getByName(params.get("cjr").toString());
			if (cjr != null){
				params.put("cjr", cjr.getId());
			}else {
				params.put("cjr", "");
			}
		}
		IPage<CsInfoVO> pages = infoService.getJxcsSc(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * 九小场所恢复
	 */
	@PostMapping("getJxcsHf")
	public R getJxcsHf(@RequestBody CsInfoEntity cs){
		String[] ids = new String[]{};
		if (cs.getId() != null) {
			ids = cs.getId().toString().split(",");
		}
		return R.data(infoService.getJxcsHf(ids));
	}

	/**
	 * 获取地址列表
	 */
	@GetMapping("/getDzList-web")
	public R getDzListWeb(@RequestParam Map<String, Object> params) {
		// 获取dept_category 14=省厅 4=市局 5=分局 6=派出所 7=社区
		Dept dept = deptService.getDeptCategory(AuthUtil.getDeptId().split(",")[0]);
		String gajgdm = "";
		if (dept.getDeptCategory() == 7) {
			gajgdm = String.valueOf(dept.getId());
		} else if (dept.getDeptCategory() == 6) {
			gajgdm = AuthUtil.getDeptId();
		}
		if (gajgdm.equals("2296911130000")) {
			gajgdm = "22969A130000"; // 天桥岭森林公安局青松派出所
		} else if (gajgdm.equals("2296911140000")) {
			gajgdm = "22969A140000"; // 天桥岭森林公安局内河派出所
		} else if (gajgdm.equals("2296911150000")) {
			gajgdm = "22969A150000"; // 天桥岭森林公安分局响水派出所
		} else if (gajgdm.equals("2296911160000")) {
			gajgdm = "22969A160000"; // 天桥岭森林公安分局张家店派出所
		} else if (gajgdm.equals("2296911170000")) {
			gajgdm = "22969A170000"; // 天桥岭森林公安局上河派出所
		} else if (gajgdm.equals("2296911180000")) {
			gajgdm = "22969A180000"; // 天桥岭森林公安局响水派出所
		}
		params.put("gajgdm", gajgdm);
		List<LdBzdzVO> ldList = fwInfoService.getDzList(params);
		return R.data(GsonUtil.toGsonStr(ldList));
	}

	/**
	 * 获取地址列表
	 */
	@GetMapping("/getDzList-app")
	public R getDzListApp(@RequestParam Map<String, Object> params) {
		// 获取dept_category 14=省厅 4=市局 5=分局 6=派出所 7=社区
		Dept dept = deptService.getDeptCategory(AuthUtil.getDeptId().split(",")[0]);
		String gajgdm = "";
		if (dept.getDeptCategory() == 7) {
			gajgdm = String.valueOf(dept.getId());
			params.put("gajgdm", gajgdm);
		}
		// 获取九小场所对应的ldphxlh
		params.put("deptId", AuthUtil.getDeptId());
		List<Long> ldphxlhList = infoService.getLdList(params);
		List<LdBzdzVO> csLdList = new ArrayList<>();
		if (null != ldphxlhList && ldphxlhList.size() > 0) {
			// 获取九小场所对应的楼栋信息
			csLdList = fwInfoService.getCsLdList(ldphxlhList);
		}
		return R.data(GsonUtil.toGsonStr(csLdList));
	}

	/**
	 * 场所-楼栋关联
	 * ids ldphxlh
	 */
	@PostMapping("/csLdGl")
	public R csLdGl(@RequestParam Map<String, String> params) {
		String[] ids = params.get("ids").split(",");
		String ldphxlh = params.get("ldphxlh");
		return R.status(infoService.csLdGl(ids, ldphxlh));
	}

	/**
	 * 取消关联
	 */
	@PostMapping("getQxgl")
	public R getQxgl(@RequestBody CsInfoEntity csInfoEntity){
		return R.data(infoService.getQxgl(csInfoEntity));
	}

	/**
	 * 地址统计
	 */
	@GetMapping("getDztj")
	public R getDztj(Query query){
		IPage<InfoVO> page = infoService.getDztj(Condition.getPage(query), AuthUtil.getDeptId());
		return R.data(page);
	}

	/**
	 * 地址详情
	 */
	@GetMapping("/getDzxq")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入info")
	public R getDzxq(@RequestParam Map<String, Object> params, Query query) {
		IPage<CsInfoVO> page = infoService.getDzxq(Condition.getPage(query), params);
		return R.data(page);
	}

	/**
	 * 地址统计导出数据
	 */
	@GetMapping("/export-dztj")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出数据", notes = "传入yjjl")
	public void exportDztj(@ApiIgnore @RequestParam Map<String, Object> dztj, HttpServletResponse response) {
		dztj.put("deptId", AuthUtil.getDeptId());
		List<DztjExcel> list = infoService.exportDztj(dztj);
		ExcelUtil.export(response, "地址统计" + DateUtil.time(), "地址统计", list, DztjExcel.class);
	}
}
