/*
 *      Copyright (c) 2018-2028, Chill Zhuang 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 dreamlu.net 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: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springblade.common.cache.DictCache;
import org.springblade.common.cache.UserCache;
import org.springblade.common.enums.DictEnum;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.cache.utils.CacheUtil;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.launch.constant.AppConstant;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.secure.constant.AuthConstant;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.csInfo.service.ICsInfoService;
import org.springblade.modules.system.entity.Bzdz;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.entity.UserDept;
import org.springblade.modules.system.excel.DeptExcel;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.system.service.IDictBizService;
import org.springblade.modules.system.service.IUserDeptService;
import org.springblade.modules.system.service.IUserService;
import org.springblade.modules.system.vo.*;
import org.springblade.modules.system.wrapper.DeptWrapper;
import org.springblade.modules.zdb.entity.ZdbEntity;
import org.springblade.modules.zdb.service.IZdbService;
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.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.springblade.core.cache.constant.CacheConstant.SYS_CACHE;

/**
 * 控制器
 *
 * @author Chill
 */
@NonDS
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SYSTEM_NAME + "/dept")
@Api(value = "部门", tags = "部门")
//@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
public class DeptController extends BladeController {

	private final IDeptService deptService;

	private final IUserService userService;

	private final IUserDeptService userDeptService;

	private final IDictBizService dictBizService;

	private final IZdbService zdbService;

	private final ICsInfoService csInfoService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入dept")
	public R<Dept> detail(Dept dept) {
		Dept detail = deptService.getOne(Condition.getQueryWrapper(dept));
		return R.data(detail);
	}

	/**
	 * 列表
	 */
	@GetMapping("/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "deptName", value = "部门名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "fullName", value = "部门全称", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "列表", notes = "传入dept")
	public R<List<DeptVO>> list(@ApiIgnore @RequestParam Map<String, Object> dept, BladeUser bladeUser) {
		QueryWrapper<Dept> queryWrapper = Condition.getQueryWrapper(dept, Dept.class);
		List<Dept> list = deptService.list((!bladeUser.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID)) ? queryWrapper.lambda().eq(Dept::getTenantId, bladeUser.getTenantId()) : queryWrapper);
		return R.data(DeptWrapper.build().listNodeVO(list));
	}

	/**
	 * 懒加载列表
	 */
	@GetMapping("/lazy-list")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "懒加载列表", notes = "传入dept")
	public R<List<DeptVO>> lazyList(@ApiIgnore @RequestParam Map<String, Object> dept, Long parentId, BladeUser bladeUser) {
		dept.put("deptId", AuthUtil.getDeptId());
		List<DeptVO> list = deptService.lazyList(bladeUser.getTenantId(), parentId, dept);
		return R.data(DeptWrapper.build().listNodeLazyVO(list));
	}

	/**
	 * 获取部门树形结构
	 */
	@GetMapping("/tree")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "树形结构", notes = "树形结构")
	public R<List<DeptVO>> tree(String tenantId, String deptId, BladeUser bladeUser) {
		List<DeptVO> tree = deptService.tree(Func.toStrWithEmpty(tenantId, bladeUser.getTenantId()), deptId);
		return R.data(tree);
	}

	/**
	 * 懒加载获取部门树形结构
	 */
	@GetMapping("/lazy-tree")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "懒加载树形结构", notes = "树形结构")
	public R<List<DeptVO>> lazyTree(String tenantId, Long parentId, BladeUser bladeUser) {
		List<DeptVO> tree = deptService.lazyTree(Func.toStrWithEmpty(tenantId, bladeUser.getTenantId()), parentId, AuthUtil.getDeptId());
		return R.data(tree);
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入dept")
	public R submit(@Valid @RequestBody Dept dept) {
		if (deptService.submit(dept)) {
			CacheUtil.clear(SYS_CACHE);
			CacheUtil.clear(SYS_CACHE, Boolean.FALSE);
			// 返回懒加载树更新节点所需字段
			Kv kv = Kv.create().set("id", String.valueOf(dept.getId())).set("tenantId", dept.getTenantId())
				.set("deptCategoryName", DictCache.getValue(DictEnum.ORG_CATEGORY, dept.getDeptCategory()));
			return R.data(kv);
		}
		return R.fail("操作失败");
	}
	/**
	 * 新增或修改
	 */
	@PostMapping("/submitPc")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入dept")
	public R submitPc(@Valid @RequestBody Dept dept) {
		//判断是否为新增社区
		if(dept.getDeptCategory() == 7){
			Long parentId = dept.getParentId();
			//新的区划 前7位
			String xzqh = parentId.toString().substring(0, 6)+"9";
			//后5位
			//根据区划的前7位及部门类型查询部门是否存在，倒序。获取部门id，作为新增部门的id参考
			Long xzqhid = deptService.getDeptByQhAndCategory(xzqh);
			if(xzqhid != null && xzqhid.toString().length() == 12){
				long l = xzqhid % 100000L;
				String format = String.format("%05d", l + 1);
				//新的行政区划（全）  7+5
				xzqh = xzqh +format;
			}else{
				xzqh = xzqh +"00001";
			}
			dept.setId(Long.parseLong(xzqh));
		}
		if (deptService.submit(dept)) {
			CacheUtil.clear(SYS_CACHE);
			CacheUtil.clear(SYS_CACHE, Boolean.FALSE);
			// 返回懒加载树更新节点所需字段
			Kv kv = Kv.create().set("id", String.valueOf(dept.getId())).set("tenantId", dept.getTenantId())
				.set("deptCategoryName", DictCache.getValue(DictEnum.ORG_CATEGORY, dept.getDeptCategory()));
			return R.data(kv);
		}
		return R.fail("操作失败");
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		String[] split = ids.split(",");
		for (int i = 0; i < split.length; i++) {
			Dept dept = deptService.getById(Long.parseLong(split[i]));
			if(dept.getDeptCategory() == 16){
				deptService.updateSSSqDmIsNUll(String.valueOf(dept.getId()));
				deptService.deleteByLdId(split[i]);
				return R.status(true);
			}else {
				CacheUtil.clear(SYS_CACHE);
				CacheUtil.clear(SYS_CACHE, Boolean.FALSE);
				return R.status(deptService.removeDept(split[i]));
			}
		}
		return R.status(false);
	}
	/**
	 * 删除
	 */
	@PostMapping("/updateSSSqDmIsNUll")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R updateSSSqDmIsNUll(@RequestParam String dzbm,String sssqBm) {
		return R.status(deptService.updateSSSqDmIsNUllnew(dzbm,sssqBm));
	}

	/**
	 * 下拉数据源
	 */
	@PreAuth(AuthConstant.PERMIT_ALL)
	@GetMapping("/select")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "下拉数据源", notes = "传入id集合")
	public R<List<Dept>> select(Long userId, String deptId) {
		if (Func.isNotEmpty(userId)) {
			User user = UserCache.getUser(userId);
			deptId = user.getDeptId();
		}
		List<Dept> list = deptService.list(Wrappers.<Dept>lambdaQuery().in(Dept::getId, Func.toLongList(deptId)));
		return R.data(list);
	}

	/**
	 * 获取派出所
	 */
	@PreAuth(AuthConstant.PERMIT_ALL)
	@GetMapping("/getPcs")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "下拉数据源", notes = "传入id集合")
	public R<List<Dept>> getPcs() {
		QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("dept_category",3);
		List<Dept> list = deptService.list(queryWrapper);
		return R.data(list);
	}

	/**
	 * 获取警务室
	 */
	@GetMapping("/getJws")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "下拉数据源", notes = "传入id集合")
	public R<List<Dept>> getJws(@RequestParam String deptId) {
		QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("parent_id",Long.parseLong(deptId));
		queryWrapper.eq("dept_category",4);
		List<Dept> list = deptService.list(queryWrapper);
		return R.data(list);
	}

	/**
	 * 获取警务室
	 */
	@PreAuth(AuthConstant.PERMIT_ALL)
	@GetMapping("/getJws1")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "下拉数据源", notes = "传入id集合")
	public R<List<Dept>> getJws1() {
		QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
//		queryWrapper.eq("parent_id",Long.parseLong(deptId));
		queryWrapper.eq("dept_category",4);
		List<Dept> list = deptService.list(queryWrapper);
		return R.data(list);
	}

	/**
	 * 导出模板
	 */
	@PostMapping("export-template")
	@ApiOperationSupport(order = 14)
	@ApiOperation(value = "导出模板")
	public void exportUser(HttpServletResponse response) {
		List<DeptExcel> list = new ArrayList<>();
		ExcelUtil.export(response, "用户数据模板", "用户数据表", list, DeptExcel.class);
	}

	/**
	 * 导入机构
	 */
	@PostMapping("/import-dept")
	public R importDept(MultipartFile file) {
		List<DeptExcel> deptExcelList = ExcelUtil.read(file, DeptExcel.class);
		Dept dept = new Dept();
		dept.setId(Long.valueOf("1"));
		dept.setParentId(Long.valueOf("1"));
		int sort = 1;
		for (DeptExcel deptExcel:deptExcelList) {
			Dept depts = BeanUtil.copy(deptExcel, Dept.class);
			if (dept.getId().equals(depts.getId())){
				continue;
			}
			if (!dept.getParentId().equals(depts.getParentId())){
				sort = 1;
			}
			dept = depts;
			depts.setDeptCategory(6);
			depts.setAncestors("0" + "," + "220000000000" + "," + depts.getParentIds() + "," + depts.getParentId());
			dept.setSort(sort);
			sort++;
			submit(depts);
		}
		/*deptExcelList.forEach(deptExcel -> {
			Dept dept = BeanUtil.copy(deptExcel, Dept.class);
			dept.setParentId(deptService.getParentId(deptExcel));
			if (dept.getParentId() != null) {
				submit(dept);
			}
		});*/
		return R.success("操作成功");
	}

	/**
	 * 获取当前派出所的社区列表
	 */
	@GetMapping("/getSqList")
	public R getSqList() {
		MjDeptVO mjDeptVO = new MjDeptVO();
		String deptId = AuthUtil.getDeptId();
		if(StringUtils.isBlank(deptId)){
			deptId = "0";
		}
		mjDeptVO = deptService.getDeptDetail(deptId.split(",")[0]);
		if (mjDeptVO.getDeptCategory() == 6) {
			// 派出所
			List<Dept> sqList = deptService.getSqByPcs(mjDeptVO);
			mjDeptVO.setSqList(sqList);
			return R.data(mjDeptVO);
		} else {
			// 用户不在派出所下
			return R.data(mjDeptVO);
		}
	}

	/**
	 * 民警app更改部门
	 */
	@Transactional
	@PostMapping("/updateUserDept")
	public R updateUserDept(@RequestBody MjDeptVO params) {
		AuthUtil.getUserId();
		// 删除blade_user_dept
		userDeptService.deleteByUserId(AuthUtil.getUserId());
		// 插入blade_user_dept
		List<UserDept> userDeptList = new ArrayList<>();
		String[] sqIdList = params.getSqIds().split(",");
		String[] sqNameList = params.getSqNames().split(",");
		for (int i = 0; i < sqIdList.length; i++) {
			UserDept userDept = new UserDept();
			userDept.setUserId(AuthUtil.getUserId());
			userDept.setDeptId(Long.valueOf(sqIdList[i]));
			userDeptList.add(userDept);
			// 查询社区是否存在，不存在则新增社区
			int sqNum = deptService.countDeptById(Long.valueOf(sqIdList[i]));
			if (sqNum == 0) {
				Dept sq = new Dept();
				sq.setId(Long.valueOf(sqIdList[i]));
				sq.setDeptCategory(7);
				sq.setDeptName(sqNameList[i]);
				sq.setFullName(sqNameList[i]);
				sq.setParentId(params.getId());
				sq.setSort(1);
				submit(sq);
			} else {
				// 所选社区的归属派出所
				Dept dept = deptService.getParentDept(sqIdList[i]);
				if (null != dept && null != dept.getId() && !dept.getId().equals(params.getId())) {
					return R.fail(sqNameList[i] + " 已被 " + dept.getDeptName() + " 认领，请联系管理员");
				}
			}
		}
		userDeptService.saveBatch(userDeptList);
		// 更新blade_user表的deptId
		return R.status(userService.updateDeptId(AuthUtil.getUserId(), params.getSqIds()));
	}

	/**
	 * 全省九小场所统计
	 */
	@GetMapping("getQsjxcs")
	public R getQsjxcs(Integer current, Integer size){
		//Dept jxcspc = deptService.getJxcspc(AuthUtil.getDeptId());
		List<Dept> dept = deptService.getQsga(AuthUtil.getDeptId());
		IPage<Dept> page = new Page<>();
		List<Dept> jxcs = new ArrayList<>();
		for (Dept qsga:dept){
			Dept jxcspc = deptService.getJxcspc(qsga.getId());
			jxcspc.setDeptName(qsga.getDeptName());
			jxcs.add(jxcspc);
		}
		List<Dept> subList = jxcs.stream().skip((current - 1) * size).limit(size).collect(Collectors.toList());
		page.setRecords(subList).setTotal(jxcs.size()).setSize(size).setCurrent(current);
		return R.data(page);
	}

	/**
	 * 全省九小场所检查统计
	 */
	@GetMapping("getQsjxcsjc")
	public R getQsjxcsjc(Integer current, Integer size){
		List<Dept> dept = deptService.getQsga(AuthUtil.getDeptId());
		IPage<Dept> page = new Page<>();
		List<Dept> jxcs = new ArrayList<>();
		for (Dept qsga:dept){
			Dept jxcspc = deptService.getJxcsjc(qsga.getId());
			jxcspc.setDeptName(qsga.getDeptName());
			jxcs.add(jxcspc);
		}
		List<Dept> subList = jxcs.stream().skip((current - 1) * size).limit(size).collect(Collectors.toList());
		page.setRecords(subList).setTotal(jxcs.size()).setSize(size).setCurrent(current);
		return R.data(page);
	}

	/**
	 * 全省用户场所检查
	 */
	@GetMapping("getQsyhcsjc")
	public R getQsyhcsjc(DeptVOS deptVOS, Query query){
		IPage<DeptVOS> page = deptService.getQsyhcsjc(Condition.getPage(query),deptVOS);
		for (DeptVOS deptVO:page.getRecords()){
			List<DeptVOS> list = deptService.getQsyhcsjcfj(deptVO.getId(),deptVO.getDatetime());
			deptVO.setChildren(list);
		}
		return R.data(page);
	}

	/**
	 * 根据登录用户的派出所id查询派出所下的楼栋
	 */
	@GetMapping("getLdByPcsbm")
	public R<List<Bzdz>> getLdByPcsbm(String wzxxdz){
		List<Bzdz> ldByPcsbm = deptService.getLdByPcsbm(AuthUtil.getDeptId(),wzxxdz);
		return R.data(ldByPcsbm);
	}

	/**
	 * 根据楼栋的id，和社区id，修改标准地址重新关联社区
	 */
	@PostMapping("updateBzdzGlsq")
	public R updateBzdzGlsq(String ldIds,String sqId){
		return R.data(deptService.updateBzdzGlsq(ldIds,sqId));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入info")
	public R update(@Valid @RequestBody Dept dept) {
		return R.status(deptService.updateId(dept));
	}


	/**
	 * 修改
	 */
	@GetMapping("/getZdLdBySssqbm")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入info")
	public R<List<ZdldVo>> getZdLdBySssqbm(@RequestParam String sssqBm) {
		return R.data(deptService.getZdLdBySssqbm(sssqBm));
	}

	/**
	 * 楼栋批量删除
	 */
	@PostMapping("/updatePlsc")
	public R updatePlsc(@RequestBody DzSqVO dzSqVO) {
		String[] dzbms = dzSqVO.getDzbms().split(",");
		return R.data(deptService.updatePlsc(dzbms,dzSqVO.getSssqBm()));
	}

	/**
	 * 部门修改
	 */
	@PostMapping("getBmxg")
	public R getBmxg(@RequestBody Dept dept){
		Dept pcs = deptService.getById(dept.getPcsid());
		dept.setAncestors(pcs.getAncestors() + "," + dept.getPcsid().toString());
		return R.data(deptService.getBmxg(dept));
	}

	/**
	 * 获取分局派出所部门信息
	 */
	@GetMapping("getFjPcs")
	public R getFjPcs(){
		List<FjVOS> fj = deptService.getfJLists(AuthUtil.getDeptId());
		for (FjVOS fjVO:fj){
			List<PcsVOS> pcs = deptService.getPcsLists(AuthUtil.getDeptId(), fjVO.getValue());
			fjVO.setChildren(pcs);
		}
		return R.data(fj);
	}

	/**
	 * 根据市分局的部门id来获取街道
	 */
	@GetMapping("getSfjIdByJd")
	public R getSfjIdByJd(){
		String deptId = AuthUtil.getDeptId();
		if (deptId.equals("220581010000")){
			deptId = "220581000000";
		}
		Dept dept = deptService.getById(Long.parseLong(deptId));
		Dept fjs = deptService.getById(dept.getParentId());
		if (dept.getDeptCategory() == 6){
			deptId = fjs.getParentId().toString();
		}
		if (dept.getDeptCategory() == 5){
			deptId = fjs.getId().toString();
		}
		if (dept.getAncestors().contains("229600000000")){
			deptId = "2200000000007";
		}
		List<FjVOS> fj = deptService.getSfjZdb(deptId);
		for (FjVOS fjVO:fj){
			List<PcsVOS> pcs = deptService.getPcsZdb(fjVO.getValue());
			fjVO.setChildren(pcs);
		}
		return R.data(fj);
	}

	/**
	 * 新增社区
	 */
	@PostMapping("getSq")
	public R getSq(@RequestBody ZdbEntity zdbEntity){
		ZdbEntity zdb = new ZdbEntity();
		ZdbEntity parent = zdbService.selectParent(zdbEntity.getJdid());
		ZdbEntity zddm = zdbService.selectZddm(zdbEntity.getJdid());
		Long dm = Long.valueOf(zddm.getZddm()) + 1;
		zdb.setZddm(dm.toString());
		zdb.setZdz(zdbEntity.getZdz());
		zdb.setShi(parent.getShi());
		zdb.setQv(zdbEntity.getFjid());
		zdb.setJiedao(zdbEntity.getJdid());
		zdb.setParentId(zdbEntity.getJdid());
		zdb.setParents(parent.getParents() + "," + zdbEntity.getJdid());
		zdb.setLb("5");
		zdbService.saveZdb(zdb);
		Dept dept = new Dept();
		Dept pcs = deptService.getById(Long.parseLong(zdbEntity.getPcsid()));
		dept.setId(dm);
		dept.setTenantId("000000");
		dept.setParentId(Long.parseLong(zdbEntity.getPcsid()));
		dept.setAncestors(pcs.getAncestors() + "," + zdbEntity.getPcsid());
		dept.setDeptCategory(7);
		dept.setDeptName(zdbEntity.getZdz());
		dept.setFullName(zdbEntity.getZdz());
		dept.setSort(1);
		dept.setIsDeleted(0);
		deptService.save(dept);
		return R.success("添加成功");
	}

	@GetMapping("getPcsByFjId")
	public R getPcsByFjId(){
		return R.data(deptService.getPcsByFjId(AuthUtil.getDeptId()));
	}

	/**
	 * 获取当前登陆人的派出所id
	 */
	@GetMapping("/getPcsId")
	public R getPcsId(){
		// 获取dept_category 14=省厅 4=市局 5=分局 6=派出所 7=社区
		Dept dept = deptService.getDeptCategory(AuthUtil.getDeptId().split(",")[0]);
		if (dept.getDeptCategory() == 7) {
			return R.data(deptService.getParentDept(AuthUtil.getDeptId().split(",")[0]));
		} else {
			return R.data(deptService.getById(Long.valueOf(AuthUtil.getDeptId())));
		}
	}

	/**
	 * 合并派出所
	 */
	@PostMapping("getHbpcs")
	public R getHbpcs(@RequestBody HbpcsVO hbpcsVO){
		Dept byId = deptService.getById(hbpcsVO.getId());
		if (byId != null){
			return R.fail("填写的机构代码已存在，请重新填写");
		}
		Dept dept = new Dept();
		Dept fj = deptService.getById(Long.valueOf(AuthUtil.getDeptId()));
		dept.setId(hbpcsVO.getId());
		dept.setTenantId("000000");
		dept.setParentId(Long.valueOf(AuthUtil.getDeptId()));
		dept.setAncestors(fj.getAncestors() + "," + fj.getId());
		dept.setDeptCategory(6);
		dept.setDeptName(hbpcsVO.getDeptName());
		dept.setFullName(hbpcsVO.getDeptName());
		dept.setSort(1);
		dept.setIsDeleted(0);
		deptService.save(dept);
		String ancestors = fj.getAncestors() + "," + fj.getId() + "," + hbpcsVO.getId();
		for (String str:hbpcsVO.getList()){
			deptService.updateAncestors(str, hbpcsVO.getId(), ancestors);
			deptService.deleteByPcsId(str);
			csInfoService.updateSspcs(str, hbpcsVO.getId(), hbpcsVO.getDeptName());
		}
		return R.success("修改成功");
	}

	/**
	 * 派出所账号获取自己的社区
	 */
	@GetMapping("getJsByPcsDownSq")
	public R getJsByPcsDownSq(){
		String value = AuthUtil.getDeptId();
		return R.data(deptService.getJsByPcsDownSq(value));
	}

	/**
	 * 根据登陆账号获取账号的市局
	 */
	@GetMapping("getSjByAccount")
	public R getSjByAccount() {
		String deptId = AuthUtil.getDeptId().split(",")[0];
		return R.data(deptService.getSjByAccount(deptId));
	}
}
