package xiaoqu.home.open.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import xiaoqu.home.open.controller.input.DistrictIdAndNameInput;
import xiaoqu.home.open.controller.input.DistrictIdInput;
import xiaoqu.home.open.controller.input.ProperUserDistrictPageInput;
import xiaoqu.home.open.controller.input.grid.GridSelectInput;
import xiaoqu.home.open.controller.input.object.ObjectByScenesInput;
import xiaoqu.home.open.controller.input.sceneObject.Scene;
import xiaoqu.home.open.controller.input.scenes.OneScenesSelectInput;
import xiaoqu.home.open.controller.output.ScenesKeyEntity;
import xiaoqu.home.open.controller.output.TreeNodeEntity;
import xiaoqu.home.open.controller.output.grid.AppointGridTreeOutput;
import xiaoqu.home.open.controller.output.grid.GridTreeOutput;
import xiaoqu.home.open.controller.output.scenes.SceneTreeOutput;
import xiaoqu.home.open.model.db1.Agent;
import xiaoqu.home.open.model.db1.Building;
import xiaoqu.home.open.model.db1.District;
import xiaoqu.home.open.model.db1.Floor;
import xiaoqu.home.open.model.db1.MultistageGrid;
import xiaoqu.home.open.model.db1.Positions;
import xiaoqu.home.open.model.db1.ProperUserDistrict;
import xiaoqu.home.open.model.db1.Properyt;
import xiaoqu.home.open.model.db1.Toilet;
import xiaoqu.home.open.model.db1.User;
import xiaoqu.home.open.model.db1.UserProper;
import xiaoqu.home.open.model.db2.InspectScenes;
import xiaoqu.home.open.model.db2.LabelManage;
import xiaoqu.home.open.model.db2.Matter;
import xiaoqu.home.open.model.db2.Object;
import xiaoqu.home.open.model.db2.Plan;
import xiaoqu.home.open.model.db2.SceneObject;
import xiaoqu.home.open.model.db2.TaskType;
import xiaoqu.home.open.model.db2.Tools;
import xiaoqu.home.open.model.db2.TypeSource;
import xiaoqu.home.open.model.db2.Wage;
import xiaoqu.home.open.model.db2.WorkGroup;
import xiaoqu.home.open.publicBean.BaseOutput;
import xiaoqu.home.open.publicBean.DropDownEntity;
import xiaoqu.home.open.publicBean.DropDownLongKeyEntity;
import xiaoqu.home.open.service.RedisService5;
import xiaoqu.home.open.service.db1.AgentService;
import xiaoqu.home.open.service.db1.BuildingService;
import xiaoqu.home.open.service.db1.DistrictService;
import xiaoqu.home.open.service.db1.FloorService;
import xiaoqu.home.open.service.db1.MultistageGridService;
import xiaoqu.home.open.service.db1.PositionsService;
import xiaoqu.home.open.service.db1.ProperUserDistrictService;
import xiaoqu.home.open.service.db1.ProperytService;
import xiaoqu.home.open.service.db1.ToiletService;
import xiaoqu.home.open.service.db1.UserProperService;
import xiaoqu.home.open.service.db1.UserService;
import xiaoqu.home.open.service.db2.InspectScenesService;
import xiaoqu.home.open.service.db2.LabelManageService;
import xiaoqu.home.open.service.db2.MatterService;
import xiaoqu.home.open.service.db2.ObjectService;
import xiaoqu.home.open.service.db2.PlanService;
import xiaoqu.home.open.service.db2.SceneObjectService;
import xiaoqu.home.open.service.db2.TaskTypeService;
import xiaoqu.home.open.service.db2.ToolsService;
import xiaoqu.home.open.service.db2.TypeSourceService;
import xiaoqu.home.open.service.db2.WageService;
import xiaoqu.home.open.service.db2.WorkGroupService;
import xiaoqu.home.open.utils.ResultResponseBeanUtil;
import xiaoqu.home.open.utils.StringUtil;
import xiaoqu.home.open.utils.UserUtils;
import xiaoqu.home.open.utils.WrapperUtils;

@Api(value = "下拉菜单")
@Controller
@RequestMapping("/dropDown")
public class DropDownController {

	@Autowired
	private TypeSourceService typeSourceService;
	@Autowired
	private UserService userService;
	@Autowired
	private UserProperService userProperService;
	@Autowired
	private DistrictService districtService;
	@Autowired
	private BuildingService buildingService;
	@Autowired
	private FloorService floorService;
	@Autowired
	private ToiletService toiletService;
	@Autowired
	private PositionsService positionsService;
	@Autowired
	private MultistageGridService multistageGridService;
	@Autowired
	private ObjectService objectService;
	@Autowired
	private MatterService matterService;
	@Autowired
	private WorkGroupService workGroupService;
	@Autowired
	private AgentService agentService;
	@Autowired
	private ProperytService properytService;
	@Autowired
	private ToolsService toolsService;
	@Autowired
	private SceneObjectService sceneObjectService;
	@Autowired
	private LabelManageService labelManageService;

	@Autowired
	private ProperUserDistrictService properUserDistrictService;
	@Autowired
	private RedisService5 redisService5;

	@Autowired
	private TaskTypeService taskTypeService;

	@Autowired
	private PlanService planService;
	@Autowired
	private WageService wageService;

	@Autowired
	private InspectScenesService inspectScenesService;

	@ApiOperation(value = "巡检点网格场景树结构")
	@ResponseBody
	@RequestMapping(value = "/inspectScenesTree", method = RequestMethod.GET)
	public BaseOutput<SceneTreeOutput> inspectScenesTree(HttpServletRequest request, @RequestParam(required = true, name = "districtId") Long districtId) {
		if (StringUtil.isNull(districtId)) {
			return ResultResponseBeanUtil.fail("请传递站点ID");
		}
		SceneTreeOutput cridTreeOutput = new SceneTreeOutput();
		QueryWrapper<InspectScenes> queryWrapper = WrapperUtils.getWrapper(false);
		queryWrapper.eq("district_id", districtId);
		List<InspectScenes> inspectSceness = inspectScenesService.list(queryWrapper);
		Set<String> idsSet = new HashSet<String>();
		if (inspectSceness.size() > 0) {
			for (InspectScenes inspectScenes : inspectSceness) {
				String key = inspectScenes.getScenesId() + "_s" + inspectScenes.getScenesLevel();
				idsSet.add(key);
			}
		}

		List<SceneTreeOutput> output = new ArrayList<SceneTreeOutput>();
//		楼座-》楼层-》多级网格
		QueryWrapper<Building> wrapperBuilding = WrapperUtils.getWrapper(false);
		wrapperBuilding.eq("district_id", districtId);
		wrapperBuilding.orderByAsc("orderby");
		List<Building> listBuilding = buildingService.list(wrapperBuilding);
		// 1级
		for (Building building : listBuilding) {
			SceneTreeOutput gridTreeOutputB = new SceneTreeOutput();
			gridTreeOutputB.setId(building.getId() + "_1");
			gridTreeOutputB.setGridId(building.getId());
			gridTreeOutputB.setName(building.getName());
			gridTreeOutputB.setLevel(1);
			gridTreeOutputB.setIsShow(false);
			// 查楼层 2级
			QueryWrapper<Floor> wrapperFloor = WrapperUtils.getWrapper(false);
			wrapperFloor.eq("building_id", building.getId());
			wrapperFloor.orderByAsc("orderby");
			List<Floor> listFloor = floorService.list(wrapperFloor);
			List<SceneTreeOutput> outputFloor = new ArrayList<SceneTreeOutput>();
			for (Floor floor : listFloor) {
				SceneTreeOutput gridTreeOutputF = new SceneTreeOutput();
				gridTreeOutputF.setId(floor.getId() + "_2");
				gridTreeOutputF.setGridId(floor.getId());
				gridTreeOutputF.setName(floor.getName());
				gridTreeOutputF.setLevel(2);
				gridTreeOutputF.setIsShow(false);
				// 查多级网格 3级
				QueryWrapper<MultistageGrid> wrapperMultistageGrid = WrapperUtils.getWrapper(false);
				wrapperMultistageGrid.eq("superior_id", floor.getId());
				wrapperMultistageGrid.eq("grid_level", 3);// 直接查三级
				wrapperMultistageGrid.orderByAsc("orderby");
				List<MultistageGrid> listMultistageGrid = multistageGridService.list(wrapperMultistageGrid);
				List<SceneTreeOutput> outputMultistageGrid = new ArrayList<SceneTreeOutput>();
				for (MultistageGrid multistageGrid : listMultistageGrid) {
					SceneTreeOutput gridTreeOutputM = new SceneTreeOutput();
					gridTreeOutputM.setId(multistageGrid.getId() + "_3");
					gridTreeOutputM.setGridId(multistageGrid.getId());
					gridTreeOutputM.setName(multistageGrid.getName());
					gridTreeOutputM.setLevel(3);
					gridTreeOutputM.setIsShow(false);
					// 查多级网格 4级
					QueryWrapper<MultistageGrid> wrapperMultistageGrid2 = WrapperUtils.getWrapper(false);
					wrapperMultistageGrid2.eq("superior_id", floor.getId());
					wrapperMultistageGrid2.eq("grid_level", 4);// 直接查四级
					wrapperMultistageGrid2.orderByAsc("orderby");
					List<MultistageGrid> listMultistageGrid1 = multistageGridService.list(wrapperMultistageGrid2);
					List<SceneTreeOutput> outputMultistageGrid1 = new ArrayList<SceneTreeOutput>();
					for (MultistageGrid multistageGrid1 : listMultistageGrid1) {
						SceneTreeOutput gridTreeOutputM1 = new SceneTreeOutput();
						gridTreeOutputM1.setId(multistageGrid1.getId() + "_4");
						gridTreeOutputM1.setGridId(multistageGrid1.getId());
						gridTreeOutputM1.setName(multistageGrid1.getName());
						gridTreeOutputM1.setLevel(4);
						gridTreeOutputM1.setIsShow(false);
						List<SceneTreeOutput> outputMultistageGridToiletList = new ArrayList<SceneTreeOutput>();
						boolean flag = setSceneNode(multistageGrid1.getId(), 4, outputMultistageGridToiletList, idsSet);
						if (flag) {
							gridTreeOutputM1.setIsShow(true);
						}
						gridTreeOutputM1.setChildNode(outputMultistageGridToiletList);
						outputMultistageGrid1.add(gridTreeOutputM1);
					}
					boolean flag = setSceneNode(multistageGrid.getId(), 3, outputMultistageGrid1, idsSet);
					if (flag) {
						gridTreeOutputM.setIsShow(true);
					}
					gridTreeOutputM.setChildNode(outputMultistageGrid1);
					outputMultistageGrid.add(gridTreeOutputM);
				}
				boolean flag = setSceneNode(floor.getId(), 2, outputMultistageGrid, idsSet);
				if (flag) {
					gridTreeOutputF.setIsShow(true);
				}
				gridTreeOutputF.setChildNode(outputMultistageGrid);
				outputFloor.add(gridTreeOutputF);
			}
			// 楼座下面的肯定不会建立场景
			if (outputFloor.size() > 0) {
				for (SceneTreeOutput sceneTreeOutput : outputFloor) {
					if (sceneTreeOutput.getIsShow()) {
						gridTreeOutputB.setIsShow(true);
						break;
					}
				}
			}
			gridTreeOutputB.setChildNode(outputFloor);
			output.add(gridTreeOutputB);
		}
		cridTreeOutput.setId("0_0");
		cridTreeOutput.setName("全部");
		cridTreeOutput.setIsShow(true);// 顶级节点肯定是必须显示的
		cridTreeOutput.setChildNode(output);
		// 循环一遍去掉false的
		cleanFalseTree(cridTreeOutput.getChildNode());
		return ResultResponseBeanUtil.success(cridTreeOutput);
	}

	public void cleanFalseTree(List<SceneTreeOutput> childNode) {
		for (SceneTreeOutput sceneTreeOutput : new ArrayList<>(childNode)) {
			if (!sceneTreeOutput.getIsShow()) {
				childNode.remove(sceneTreeOutput);
				continue;
			}
			if (sceneTreeOutput.getChildNode() != null && sceneTreeOutput.getChildNode().size() > 0) {
				cleanFalseTree(sceneTreeOutput.getChildNode());
			} else {
				return;
			}
		}
	}

	@ApiOperation(value = "巡检路线模糊查询选择下拉")
	@ResponseBody
	@RequestMapping(value = "/inspectWay", method = RequestMethod.POST)
	public BaseOutput<List<DropDownLongKeyEntity>> inspectWay(HttpServletRequest request, @RequestBody DistrictIdAndNameInput input) {
		Long districtId = input.getDistrictId();
		if (StringUtil.isNull(districtId)) {
			return ResultResponseBeanUtil.fail("请选择站点");
		}
		QueryWrapper<Plan> wrapper = WrapperUtils.getWrapper(false);
		wrapper = WrapperUtils.beanToWrapper(wrapper, input);
		wrapper.eq("type", 5);// 巡检计划类型
		wrapper.orderByDesc("id");
		if (StringUtil.isNotEmpty(input.getName())) {
			wrapper.last("limit 10");// 默认查10条
		}
		List<Plan> planList = planService.list(wrapper);
		List<DropDownLongKeyEntity> output = new ArrayList<DropDownLongKeyEntity>();
		for (Plan plan : planList) {
			DropDownLongKeyEntity dropDownLongKeyEntity = new DropDownLongKeyEntity();
			dropDownLongKeyEntity.setId(plan.getId());
			dropDownLongKeyEntity.setName(plan.getTaskName());
			output.add(dropDownLongKeyEntity);
		}
		return ResultResponseBeanUtil.success(output);
	}

	@ApiOperation(value = "服务事项下拉")
	@ResponseBody
	@RequestMapping(value = "/selectMatter", method = RequestMethod.POST)
	public BaseOutput<List<DropDownLongKeyEntity>> selectMatter(HttpServletRequest request, @RequestBody DistrictIdInput input) {
		Long districtId = input.getDistrictId();
		if (districtId == null || districtId == 0) {
			return ResultResponseBeanUtil.fail("请选择站点");
		}
		District district = districtService.getById(districtId);
		if (district == null) {
			return ResultResponseBeanUtil.fail("选择的站点不存在");
		}
		QueryWrapper<Matter> wrapper = WrapperUtils.getWrapper(false);
		wrapper = WrapperUtils.beanToWrapper(wrapper, input);
		wrapper.groupBy("object_id");
		wrapper.eq("parent_id", 0);
		List<Matter> matterList = matterService.list(wrapper);
		
		List<DropDownLongKeyEntity> output = new ArrayList<DropDownLongKeyEntity>();
		for (Matter matter : matterList) {
			DropDownLongKeyEntity dropDownLongKeyEntity = new DropDownLongKeyEntity();
			dropDownLongKeyEntity.setId(matter.getObjectId());
			dropDownLongKeyEntity.setName(matter.getObjectName());
			output.add(dropDownLongKeyEntity);
		}
		QueryWrapper<Matter> wrapper1 = WrapperUtils.getWrapper(false);
		wrapper1 = WrapperUtils.beanToWrapper(wrapper1, input);
		wrapper1.groupBy("parent_id");
		wrapper1.gt("parent_id", 0);
		List<Matter> matterList1 = matterService.list(wrapper1);
		for (Matter matter : matterList1) {
			DropDownLongKeyEntity dropDownLongKeyEntity = new DropDownLongKeyEntity();
			dropDownLongKeyEntity.setId(matter.getParentId());
			dropDownLongKeyEntity.setName(matter.getParentName());
			output.add(dropDownLongKeyEntity);
		}
		return ResultResponseBeanUtil.success(output);
	}

	@ApiOperation(value = "工作组选择下拉")
	@ResponseBody
	@RequestMapping(value = "/selectWorkGroup", method = RequestMethod.POST)
	public BaseOutput<List<DropDownLongKeyEntity>> selectWorkGroup(HttpServletRequest request, @RequestBody DistrictIdInput input) {
		Long districtId = input.getDistrictId();
		if (StringUtil.isNull(districtId)) {
			return ResultResponseBeanUtil.fail("请选择站点");
		}
		District district = districtService.getById(districtId);
		if (StringUtil.isNull(district)) {
			return ResultResponseBeanUtil.fail("选择的站点不存在");
		}
		QueryWrapper<WorkGroup> wrapper = WrapperUtils.getWrapper(false);
		wrapper = WrapperUtils.beanToWrapper(wrapper, input);
		List<WorkGroup> WorkGroupList = workGroupService.list(wrapper);
		List<DropDownLongKeyEntity> output = new ArrayList<DropDownLongKeyEntity>();
		for (WorkGroup workGroup : WorkGroupList) {
			DropDownLongKeyEntity dropDownLongKeyEntity = new DropDownLongKeyEntity();
			dropDownLongKeyEntity.setId(workGroup.getId());
			dropDownLongKeyEntity.setName(workGroup.getWorkGroupName());
			output.add(dropDownLongKeyEntity);
		}
		return ResultResponseBeanUtil.success(output);
	}

	@ApiOperation(value = "负责人下拉  传递站点ID就行")
	@ResponseBody
	@RequestMapping(value = "/principalList", method = RequestMethod.POST)
	public BaseOutput<List<DropDownLongKeyEntity>> principalList(HttpServletRequest request, @RequestBody DistrictIdInput input) {
		Long districtId = input.getDistrictId();
		if (StringUtil.isNull(districtId)) {
			return ResultResponseBeanUtil.fail("请选择站点");
		}
		District district = districtService.getById(districtId);
		if (StringUtil.isNull(district)) {
			return ResultResponseBeanUtil.fail("选择的站点不存在");
		}
		QueryWrapper<UserProper> wrapperUP = WrapperUtils.getWrapper(false);
		wrapperUP.eq("district_id", districtId);
		wrapperUP.in("type", 0, 1);// 保洁和物业
		List<UserProper> userPropers = userProperService.list(wrapperUP);
		List<DropDownLongKeyEntity> output = new ArrayList<DropDownLongKeyEntity>();
		if (StringUtil.isEmpty(userPropers)) {
			return ResultResponseBeanUtil.success(output);
		}
		Set<Long> userIds = userPropers.stream().map(userProper -> userProper.getUserId()).collect(Collectors.toSet());
		input.setDistrictId(null);
		QueryWrapper<User> wrapper = WrapperUtils.getWrapper(false);
		wrapper.in("id", userIds);
		List<User> userList = userService.list(wrapper);
		for (User user2 : userList) {
			DropDownLongKeyEntity dropDownLongKeyEntity = new DropDownLongKeyEntity();
			dropDownLongKeyEntity.setId(user2.getId());
			dropDownLongKeyEntity.setName(user2.getName());
			output.add(dropDownLongKeyEntity);
		}
		return ResultResponseBeanUtil.success(output);
	}

	@ApiOperation(value = "一级场景下拉")
	@ResponseBody
	@RequestMapping(value = "/oneScenesList", method = RequestMethod.POST)
	public BaseOutput<List<ScenesKeyEntity>> oneScenesList(HttpServletRequest request, @RequestBody OneScenesSelectInput input) {
		List<ScenesKeyEntity> output = new ArrayList<ScenesKeyEntity>();
		Long districtId = input.getDistrictId();
		if (StringUtil.isNull(districtId)) {
			return ResultResponseBeanUtil.fail("请选择站点");
		}
		District district = districtService.getById(districtId);
		if (StringUtil.isNull(district)) {
			return ResultResponseBeanUtil.fail("选择的站点不存在");
		}
		List<GridSelectInput> gridSelect = input.getGridSelect();
		for (GridSelectInput gridSelectInput : gridSelect) {
			QueryWrapper<Toilet> wrapperUP = WrapperUtils.getWrapper(false);
			wrapperUP.eq("district_id", districtId);
			Integer gridLevel = gridSelectInput.getGridLevel();
			switch (gridLevel) {
			case 1:
				wrapperUP.eq("building_id", gridSelectInput.getGridId());
				break;
			case 2:
				wrapperUP.eq("floor_id", gridSelectInput.getGridId());
				break;

			default:
				wrapperUP.eq("multistage_grid_id", gridSelectInput.getGridId());
				break;
			}
			wrapperUP.orderByAsc("id");
			List<Toilet> toilets = toiletService.list(wrapperUP);
			for (Toilet toilet : toilets) {
				ScenesKeyEntity dropDownLongKeyEntity = new ScenesKeyEntity();
				dropDownLongKeyEntity.setId(toilet.getId());
				dropDownLongKeyEntity.setName(toilet.getName());
				dropDownLongKeyEntity.setScenesType(toilet.getScenesType());
				output.add(dropDownLongKeyEntity);
			}
		}
		return ResultResponseBeanUtil.success(output);
	}

	@ApiOperation(value = "网格树结构")
	@ResponseBody
	@RequestMapping(value = "/gridTree", method = RequestMethod.GET)
	public BaseOutput<GridTreeOutput> gridTree(HttpServletRequest request, @RequestParam(required = true, name = "districtId") Long districtId) {
		GridTreeOutput cridTreeOutput = new GridTreeOutput();
		List<GridTreeOutput> output = new ArrayList<GridTreeOutput>();
//		楼座-》楼层-》多级网格
		QueryWrapper<Building> wrapperBuilding = WrapperUtils.getWrapper(false);
		wrapperBuilding.eq("district_id", districtId);
		wrapperBuilding.orderByAsc("orderby");
		List<Building> listBuilding = buildingService.list(wrapperBuilding);
		// 1级
		for (Building building : listBuilding) {
			GridTreeOutput gridTreeOutputB = new GridTreeOutput();
			gridTreeOutputB.setId(building.getId() + "_1");
			gridTreeOutputB.setGridId(building.getId());
			gridTreeOutputB.setName(building.getName());
			gridTreeOutputB.setGridLevel(1);
			// 查楼层 2级
			QueryWrapper<Floor> wrapperFloor = WrapperUtils.getWrapper(false);
			wrapperFloor.eq("building_id", building.getId());
			wrapperFloor.orderByAsc("orderby");
			List<Floor> listFloor = floorService.list(wrapperFloor);
			List<GridTreeOutput> outputFloor = new ArrayList<GridTreeOutput>();
			for (Floor floor : listFloor) {
				GridTreeOutput gridTreeOutputF = new GridTreeOutput();
				gridTreeOutputF.setId(floor.getId() + "_2");
				gridTreeOutputF.setGridId(floor.getId());
				gridTreeOutputF.setName(floor.getName());
				gridTreeOutputF.setGridLevel(2);
				// 查多级网格 3级
				QueryWrapper<MultistageGrid> wrapperMultistageGrid = WrapperUtils.getWrapper(false);
				wrapperMultistageGrid.eq("superior_id", floor.getId());
				wrapperMultistageGrid.eq("grid_level", 3);// 直接查三级
				wrapperMultistageGrid.orderByAsc("orderby");
				List<MultistageGrid> listMultistageGrid = multistageGridService.list(wrapperMultistageGrid);
				List<GridTreeOutput> outputMultistageGrid = new ArrayList<GridTreeOutput>();
				for (MultistageGrid multistageGrid : listMultistageGrid) {
					GridTreeOutput gridTreeOutputM = new GridTreeOutput();
					gridTreeOutputM.setId(multistageGrid.getId() + "_3");
					gridTreeOutputM.setGridId(multistageGrid.getId());
					gridTreeOutputM.setName(multistageGrid.getName());
					gridTreeOutputM.setGridLevel(3);
					// 查多级网格 4级
					QueryWrapper<MultistageGrid> wrapperMultistageGrid2 = WrapperUtils.getWrapper(false);
					wrapperMultistageGrid2.eq("superior_id", floor.getId());
					wrapperMultistageGrid2.eq("grid_level", 4);// 直接查四级
					wrapperMultistageGrid2.orderByAsc("orderby");
					List<MultistageGrid> listMultistageGrid1 = multistageGridService.list(wrapperMultistageGrid2);
					List<GridTreeOutput> outputMultistageGrid1 = new ArrayList<GridTreeOutput>();
					for (MultistageGrid multistageGrid1 : listMultistageGrid1) {
						GridTreeOutput gridTreeOutputM1 = new GridTreeOutput();
						gridTreeOutputM1.setId(multistageGrid1.getId() + "_4");
						gridTreeOutputM1.setGridId(multistageGrid1.getId());
						gridTreeOutputM1.setName(multistageGrid1.getName());
						gridTreeOutputM1.setGridLevel(4);
						outputMultistageGrid1.add(gridTreeOutputM1);
					}
					gridTreeOutputM.setChildNode(outputMultistageGrid1);
					outputMultistageGrid.add(gridTreeOutputM);
				}
				gridTreeOutputF.setChildNode(outputMultistageGrid);
				outputFloor.add(gridTreeOutputF);
			}
			gridTreeOutputB.setChildNode(outputFloor);
			output.add(gridTreeOutputB);
		}
		cridTreeOutput.setId("0_0");
		cridTreeOutput.setName("全部");
		cridTreeOutput.setChildNode(output);
		return ResultResponseBeanUtil.success(cridTreeOutput);
	}

	@ApiOperation(value = "网格树结构添加网格时特殊处理")
	@ResponseBody
	@RequestMapping(value = "/specialGridTreeByAdd", method = RequestMethod.GET)
	public BaseOutput<GridTreeOutput> specialGridTreeByAdd(HttpServletRequest request, @RequestParam(required = true, name = "districtId") Long districtId) {
		GridTreeOutput cridTreeOutput = new GridTreeOutput();
		List<GridTreeOutput> output = new ArrayList<GridTreeOutput>();
//		楼座-》楼层-》多级网格
		QueryWrapper<Building> wrapperBuilding = WrapperUtils.getWrapper(false);
		wrapperBuilding.eq("district_id", districtId);
		wrapperBuilding.orderByAsc("orderby");
		List<Building> listBuilding = buildingService.list(wrapperBuilding);
		// 1级
		for (Building building : listBuilding) {
			GridTreeOutput gridTreeOutputB = new GridTreeOutput();
			gridTreeOutputB.setId(building.getId() + "_1");
			gridTreeOutputB.setGridId(building.getId());
			gridTreeOutputB.setName(building.getName());
			gridTreeOutputB.setGridLevel(1);
			// 查楼层 2级
			QueryWrapper<Floor> wrapperFloor = WrapperUtils.getWrapper(false);
			wrapperFloor.eq("building_id", building.getId());
			wrapperFloor.orderByAsc("orderby");
			List<Floor> listFloor = floorService.list(wrapperFloor);
			List<GridTreeOutput> outputFloor = new ArrayList<GridTreeOutput>();
			for (Floor floor : listFloor) {
				GridTreeOutput gridTreeOutputF = new GridTreeOutput();
				gridTreeOutputF.setId(floor.getId() + "_2");
				gridTreeOutputF.setGridId(floor.getId());
				gridTreeOutputF.setName(floor.getName());
				gridTreeOutputF.setGridLevel(2);
				outputFloor.add(gridTreeOutputF);
			}
			gridTreeOutputB.setChildNode(outputFloor);
			output.add(gridTreeOutputB);
		}
		cridTreeOutput.setId("0_0");
		cridTreeOutput.setName("全部");
		cridTreeOutput.setChildNode(output);
		return ResultResponseBeanUtil.success(cridTreeOutput);
	}

	@ApiOperation(value = "获取作业类型")
	@ResponseBody
	@RequestMapping(value = "/getOperationType", method = RequestMethod.GET)
	public BaseOutput<List<DropDownLongKeyEntity>> getOperationType(HttpServletRequest request) {
		List<DropDownLongKeyEntity> dropDownList = new ArrayList<DropDownLongKeyEntity>();
		QueryWrapper<TypeSource> queryWrapper = WrapperUtils.getWrapper(false);
		queryWrapper.eq("type_name", "operationType");
		queryWrapper.orderByAsc("order_by");
		List<TypeSource> list = typeSourceService.list(queryWrapper);
		for (TypeSource typeSource : list) {
			DropDownLongKeyEntity dropDownEntity = new DropDownLongKeyEntity();
			dropDownEntity.setId(Long.parseLong(typeSource.getName()));
			dropDownEntity.setName(typeSource.getContent());
			dropDownList.add(dropDownEntity);
		}
		return ResultResponseBeanUtil.success(dropDownList);

	}

	@ApiOperation(value = "获取树形对象")
	@ResponseBody
	@RequestMapping(value = "/getObjectTree", method = RequestMethod.GET)
	public BaseOutput<TreeNodeEntity> getObjectTree(HttpServletRequest request, @RequestParam Long districtId) {

		TreeNodeEntity treeNodeEntity = new TreeNodeEntity();
		treeNodeEntity.setName("全部");
		treeNodeEntity.setId(0L);
		treeNodeEntity.setChildrenNode(new ArrayList<>());
		QueryWrapper<Object> queryWrapper = WrapperUtils.getWrapper(false);
		queryWrapper.eq("district_id", districtId);
		queryWrapper.eq("status", 1);
		queryWrapper.orderByAsc("parent_id", "sort");
		List<Object> objectList = objectService.list(queryWrapper);
		Map<Long, Integer> idMap = new HashMap<Long, Integer>();
		for (Object object : objectList) {
			if (object.getParentId() == 0) {
				List<TreeNodeEntity> childrenNode = treeNodeEntity.getChildrenNode();
				TreeNodeEntity sontreeNodeEntity = new TreeNodeEntity();
				sontreeNodeEntity.setChildrenNode(new ArrayList<>());
				sontreeNodeEntity.setId(object.getId());
				sontreeNodeEntity.setLevel(object.getLevel());
				sontreeNodeEntity.setName(object.getName());
				childrenNode.add(sontreeNodeEntity);
				idMap.put(object.getId(), childrenNode.size() - 1);
			} else {
				if (idMap.get(object.getParentId()) == null) {
					continue;
				}
				List<TreeNodeEntity> sonchildrenNode = treeNodeEntity.getChildrenNode().get(idMap.get(object.getParentId())).getChildrenNode();
				TreeNodeEntity subtreeNodeEntity = new TreeNodeEntity();
				subtreeNodeEntity.setChildrenNode(new ArrayList<>());
				subtreeNodeEntity.setId(object.getId());
				subtreeNodeEntity.setLevel(object.getLevel());
				subtreeNodeEntity.setName(object.getName());
				sonchildrenNode.add(subtreeNodeEntity);

			}
		}
		return ResultResponseBeanUtil.success(treeNodeEntity);
	}

	@ApiOperation(value = "获取树形工具")
	@ResponseBody
	@RequestMapping(value = "/getToolTree", method = RequestMethod.GET)
	public BaseOutput<TreeNodeEntity> getToolTree(HttpServletRequest request, @RequestParam Long districtId) {

		TreeNodeEntity treeNodeEntity = new TreeNodeEntity();
		treeNodeEntity.setName("全部");
		treeNodeEntity.setId(0L);
		treeNodeEntity.setChildrenNode(new ArrayList<>());
		QueryWrapper<Tools> queryWrapper = WrapperUtils.getWrapper(false);
		queryWrapper.eq("district_id", districtId);
		queryWrapper.orderByAsc("parent_id");
		queryWrapper.eq("status", 1);

		List<Tools> objectList = toolsService.list(queryWrapper);
		Map<Long, Integer> idMap = new HashMap<Long, Integer>();
		for (Tools object : objectList) {
			if (object.getParentId() == 0) {
				List<TreeNodeEntity> childrenNode = treeNodeEntity.getChildrenNode();
				TreeNodeEntity sontreeNodeEntity = new TreeNodeEntity();
				sontreeNodeEntity.setChildrenNode(new ArrayList<>());
				sontreeNodeEntity.setId(object.getId());
				sontreeNodeEntity.setLevel(object.getLevel());
				sontreeNodeEntity.setName(object.getName());
				childrenNode.add(sontreeNodeEntity);
				idMap.put(object.getId(), childrenNode.size() - 1);
			} else {
				Integer curid = idMap.get(object.getParentId());
				if (curid == null) {
					continue;
				}
				List<TreeNodeEntity> sonchildrenNode = treeNodeEntity.getChildrenNode().get(idMap.get(object.getParentId())).getChildrenNode();
				TreeNodeEntity subtreeNodeEntity = new TreeNodeEntity();
				subtreeNodeEntity.setChildrenNode(new ArrayList<>());
				subtreeNodeEntity.setId(object.getId());
				subtreeNodeEntity.setLevel(object.getLevel());
				subtreeNodeEntity.setName(object.getName());
				sonchildrenNode.add(subtreeNodeEntity);

			}
		}
		return ResultResponseBeanUtil.success(treeNodeEntity);
	}

	@ApiOperation(value = "获取站点")
	@ResponseBody
	@RequestMapping(value = "/getDistrict", method = RequestMethod.POST)
	public BaseOutput<List<DropDownLongKeyEntity>> getDistrict(HttpServletRequest request, @RequestBody ProperUserDistrictPageInput input) {

		User user = UserUtils.getUser(request);
		List<DropDownLongKeyEntity> dropDownList = new ArrayList<DropDownLongKeyEntity>();
//        PageOutput<DropDownLongKeyEntity> output = new PageOutput<>();
		QueryWrapper<ProperUserDistrict> queryWrapper = WrapperUtils.getWrapper(false);
		queryWrapper.eq("user_id", user.getId());
		queryWrapper.orderByAsc("id");
		WrapperUtils.beanToWrapper(queryWrapper, input);
//        Page<ProperUserDistrict> pageList = new Page<>(input.getPage(), input.getRows());
		List<ProperUserDistrict> list = properUserDistrictService.list(queryWrapper);
//        List<ProperUserDistrict> list = properUserDistrictService.page(pageList, queryWrapper).getRecords();
		for (ProperUserDistrict properUserDistrict : list) {
			DropDownLongKeyEntity dropDownEntity = new DropDownLongKeyEntity();
			dropDownEntity.setId(properUserDistrict.getDistrictId());
			dropDownEntity.setName(properUserDistrict.getDistrictName());
			dropDownList.add(dropDownEntity);
		}

//        output.setPage(pageList.getCurrent());
//        output.setRecords(pageList.getTotal());
//        output.setTotal(pageList.getPages());
//        output.setRows(dropDownList);
		return ResultResponseBeanUtil.success(dropDownList);

	}

	@ApiOperation(value = "获取所有的代理")
	@ResponseBody
	@RequestMapping(value = "/selectAllAgent", method = RequestMethod.GET)
	public BaseOutput<List<DropDownLongKeyEntity>> selectAllAgent(HttpServletRequest request) {
		User user = UserUtils.getUser(request);
		List<DropDownLongKeyEntity> dropDownList = new ArrayList<DropDownLongKeyEntity>();
		QueryWrapper<Agent> queryWrapper = WrapperUtils.getWrapper(false);
		Agent temPagent = agentService.getById(user.getAgentId());

		// 总部
		if (user.getAgentId() == 17 || user.getAgentId() == 1) {//
			List<Agent> agentsList = agentService.list(queryWrapper);
			if (agentsList.size() > 0) {
				for (Agent agent : agentsList) {
					DropDownLongKeyEntity dropDownEntity = new DropDownLongKeyEntity();
					dropDownEntity.setId(agent.getId());
					dropDownEntity.setName(agent.getName());
					dropDownList.add(dropDownEntity);
				}
			}
		} else {
			DropDownLongKeyEntity dropDownEntity = new DropDownLongKeyEntity();
			dropDownEntity.setId(temPagent.getId());
			dropDownEntity.setName(temPagent.getName());
			dropDownList.add(dropDownEntity);
			// 20220215 代理下拉权限区分分公司
			// 20220221 取消分公司限制，所有代理都查询子代理
			queryWrapper.eq("supervisor_id", temPagent.getId());
			List<Agent> agentsList = agentService.list(queryWrapper);
			if (agentsList != null && agentsList.size() > 0) {
				for (Agent agent : agentsList) {
					DropDownLongKeyEntity subEntity = new DropDownLongKeyEntity();
					subEntity.setId(agent.getId());
					subEntity.setName(agent.getName());
					dropDownList.add(subEntity);
				}

			}
		}

		return ResultResponseBeanUtil.success(dropDownList);
	}

	@ApiOperation(value = "根据代理ID查询物业列表")
	@ResponseBody
	@RequestMapping(value = "/selectPropertyByAgentId", method = RequestMethod.GET)
	public BaseOutput<List<DropDownEntity>> selectPropertyListByAgentId(HttpServletRequest request, @RequestParam(name = "agentId") Long agentId) {
		List<DropDownEntity> dropDownList = new ArrayList<DropDownEntity>();
		QueryWrapper<Properyt> queryWrapper = WrapperUtils.getWrapper(false);
		queryWrapper.eq("agent_id", agentId);
		List<Properyt> properytList = properytService.list(queryWrapper);
		if (properytList.size() > 0) {
			for (Properyt properyt : properytList) {
				DropDownEntity dropDownEntity = new DropDownEntity();
				dropDownEntity.setId(properyt.getId());
				dropDownEntity.setName(properyt.getName());
				dropDownList.add(dropDownEntity);
			}
		}
		return ResultResponseBeanUtil.success(dropDownList);
	}

	@ApiOperation(value = "获取上级对象")
	@ResponseBody
	@RequestMapping(value = "/getParentObject", method = RequestMethod.GET)
	public BaseOutput<List<DropDownLongKeyEntity>> getParentObject(HttpServletRequest request, @RequestParam Long districtId) {

		QueryWrapper<Object> queryWrapper = WrapperUtils.getWrapper(false);
		queryWrapper.eq("district_id", districtId);
		queryWrapper.eq("parent_id", 0);
		queryWrapper.eq("status", 1);

		queryWrapper.orderByAsc("sort");
		List<Object> objectList = objectService.list(queryWrapper);
		List<DropDownLongKeyEntity> dropDownList = new ArrayList<DropDownLongKeyEntity>();

		for (Object object : objectList) {
			DropDownLongKeyEntity dropDownEntity = new DropDownLongKeyEntity();
			dropDownEntity.setId(object.getId());
			dropDownEntity.setName(object.getName());
			dropDownList.add(dropDownEntity);
		}
		return ResultResponseBeanUtil.success(dropDownList);
	}

	@ApiOperation(value = "获取上级工具")
	@ResponseBody
	@RequestMapping(value = "/getParentTools", method = RequestMethod.GET)
	public BaseOutput<List<DropDownLongKeyEntity>> getParentTools(HttpServletRequest request, @RequestParam Long districtId) {

		QueryWrapper<Tools> queryWrapper = WrapperUtils.getWrapper(false);
		queryWrapper.eq("district_id", districtId);
		queryWrapper.eq("parent_id", 0);
		queryWrapper.eq("status", 1);

//		queryWrapper.orderByAsc("sort");
		List<Tools> objectList = toolsService.list(queryWrapper);
		List<DropDownLongKeyEntity> dropDownList = new ArrayList<DropDownLongKeyEntity>();

		for (Tools object : objectList) {
			DropDownLongKeyEntity dropDownEntity = new DropDownLongKeyEntity();
			dropDownEntity.setId(object.getId());
			dropDownEntity.setName(object.getName());
			dropDownList.add(dropDownEntity);
		}
		return ResultResponseBeanUtil.success(dropDownList);
	}

	@ApiOperation(value = "网格场景树结构")
	@ResponseBody
	@RequestMapping(value = "/gridSceneTree", method = RequestMethod.GET)
	public BaseOutput<SceneTreeOutput> gridSceneTree(HttpServletRequest request, @RequestParam(required = true, name = "districtId") Long districtId) {
		if (StringUtil.isNull(districtId)) {
			return ResultResponseBeanUtil.fail("请传递站点ID");
		}
		SceneTreeOutput cridTreeOutput = new SceneTreeOutput();
		List<SceneTreeOutput> output = new ArrayList<SceneTreeOutput>();
//		楼座-》楼层-》多级网格
		QueryWrapper<Building> wrapperBuilding = WrapperUtils.getWrapper(false);
		wrapperBuilding.eq("district_id", districtId);
		wrapperBuilding.orderByAsc("orderby");
		List<Building> listBuilding = buildingService.list(wrapperBuilding);

		// 1级
		for (Building building : listBuilding) {
			SceneTreeOutput gridTreeOutputB = new SceneTreeOutput();
			gridTreeOutputB.setId(building.getId() + "_1");
			gridTreeOutputB.setGridId(building.getId());
			gridTreeOutputB.setName(building.getName());
			gridTreeOutputB.setLevel(1);
			// 查楼层 2级
			QueryWrapper<Floor> wrapperFloor = WrapperUtils.getWrapper(false);
			wrapperFloor.eq("building_id", building.getId());
			wrapperFloor.orderByAsc("orderby");
			List<Floor> listFloor = floorService.list(wrapperFloor);
			List<SceneTreeOutput> outputFloor = new ArrayList<SceneTreeOutput>();
			for (Floor floor : listFloor) {
				SceneTreeOutput gridTreeOutputF = new SceneTreeOutput();
				gridTreeOutputF.setId(floor.getId() + "_2");
				gridTreeOutputF.setGridId(floor.getId());
				gridTreeOutputF.setName(floor.getName());
				gridTreeOutputF.setLevel(2);
				// 查多级网格 3级
				QueryWrapper<MultistageGrid> wrapperMultistageGrid = WrapperUtils.getWrapper(false);
				wrapperMultistageGrid.eq("superior_id", floor.getId());
				wrapperMultistageGrid.eq("grid_level", 3);// 直接查三级
				wrapperMultistageGrid.orderByAsc("orderby");
				List<MultistageGrid> listMultistageGrid = multistageGridService.list(wrapperMultistageGrid);
				List<SceneTreeOutput> outputMultistageGrid = new ArrayList<SceneTreeOutput>();
				for (MultistageGrid multistageGrid : listMultistageGrid) {
					SceneTreeOutput gridTreeOutputM = new SceneTreeOutput();
					gridTreeOutputM.setId(multistageGrid.getId() + "_3");
					gridTreeOutputM.setGridId(multistageGrid.getId());
					gridTreeOutputM.setName(multistageGrid.getName());
					gridTreeOutputM.setLevel(3);
					// 查多级网格 4级
					QueryWrapper<MultistageGrid> wrapperMultistageGrid2 = WrapperUtils.getWrapper(false);
					wrapperMultistageGrid2.eq("superior_id", floor.getId());
					wrapperMultistageGrid2.eq("grid_level", 4);// 直接查四级
					wrapperMultistageGrid2.orderByAsc("orderby");
					List<MultistageGrid> listMultistageGrid1 = multistageGridService.list(wrapperMultistageGrid2);
					List<SceneTreeOutput> outputMultistageGrid1 = new ArrayList<SceneTreeOutput>();
					for (MultistageGrid multistageGrid1 : listMultistageGrid1) {
						SceneTreeOutput gridTreeOutputM1 = new SceneTreeOutput();
						gridTreeOutputM1.setId(multistageGrid1.getId() + "_4");
						gridTreeOutputM1.setGridId(multistageGrid1.getId());
						gridTreeOutputM1.setName(multistageGrid1.getName());
						gridTreeOutputM1.setLevel(4);
						List<SceneTreeOutput> outputMultistageGridToiletList = new ArrayList<SceneTreeOutput>();

						setSceneNode(multistageGrid1.getId(), 4, outputMultistageGridToiletList);

						gridTreeOutputM1.setChildNode(outputMultistageGridToiletList);
						outputMultistageGrid1.add(gridTreeOutputM1);

					}

					setSceneNode(multistageGrid.getId(), 3, outputMultistageGrid1);
					gridTreeOutputM.setChildNode(outputMultistageGrid1);
					outputMultistageGrid.add(gridTreeOutputM);
				}
				setSceneNode(floor.getId(), 2, outputMultistageGrid);
				gridTreeOutputF.setChildNode(outputMultistageGrid);
				outputFloor.add(gridTreeOutputF);
			}
			setSceneNode(building.getId(), 1, outputFloor);
			gridTreeOutputB.setChildNode(outputFloor);
			output.add(gridTreeOutputB);
		}
		cridTreeOutput.setId("0_0");
		cridTreeOutput.setName("全部");
		cridTreeOutput.setChildNode(output);
		return ResultResponseBeanUtil.success(cridTreeOutput);
	}

	private boolean setSceneNode(Long gridId, Integer gridLevel, List<SceneTreeOutput> gridChildren, Set<String> idSet) {
		boolean flag = false;
		QueryWrapper<Toilet> toiletQueryWrapper = WrapperUtils.getWrapper(false);
		toiletQueryWrapper.eq("grid_level", gridLevel);
		toiletQueryWrapper.eq("enable_status", 0);
		switch (gridLevel) {
		case 1:
			toiletQueryWrapper.eq("building_id", gridId);
			break;
		case 2:
			toiletQueryWrapper.eq("floor_id", gridId);
			break;
		default:
			toiletQueryWrapper.eq("multistage_grid_id", gridId);
			break;

		}
		List<Toilet> toiletlist = toiletService.list(toiletQueryWrapper);
		for (Toilet toilet : toiletlist) {
			SceneTreeOutput sceneTreeOutputToilet = new SceneTreeOutput();
			sceneTreeOutputToilet.setId(toilet.getId() + "_s1");
			sceneTreeOutputToilet.setGridId(gridId);
			sceneTreeOutputToilet.setName(toilet.getName());
			sceneTreeOutputToilet.setLevel(1);
			sceneTreeOutputToilet.setSceneId(toilet.getId());
			sceneTreeOutputToilet.setIsscene(1);
			sceneTreeOutputToilet.setDisable(false);
			if (!idSet.contains(sceneTreeOutputToilet.getId())) {
				sceneTreeOutputToilet.setIsShow(false);
			} else {
				if (!flag) {
					flag = true;
				}
			}
			QueryWrapper<Positions> positionsQueryWrapper = WrapperUtils.getWrapper(false);
			positionsQueryWrapper.eq("toilet_id", toilet.getId());
			positionsQueryWrapper.eq("enable_status", 0);

			List<Positions> positionsList = positionsService.list(positionsQueryWrapper);
			if (positionsList.isEmpty()) {
				sceneTreeOutputToilet.setIsLast(1);
			}
			List<SceneTreeOutput> outputMultistageGridPositionsList = new ArrayList<SceneTreeOutput>();
			for (Positions positions : positionsList) {
				SceneTreeOutput sceneTreeOutputPositions = new SceneTreeOutput();
				sceneTreeOutputPositions.setId(positions.getId() + "_s2");
				sceneTreeOutputPositions.setGridId(gridId);
				sceneTreeOutputPositions.setSceneId(positions.getId());
				sceneTreeOutputPositions.setName(positions.getName());
				sceneTreeOutputPositions.setLevel(2);
				sceneTreeOutputPositions.setIsscene(1);
				sceneTreeOutputPositions.setDisable(false);
				if (!idSet.contains(sceneTreeOutputToilet.getId())) {
					sceneTreeOutputPositions.setIsShow(false);
				} else {
					if (!flag) {
						flag = true;
					}
				}
				sceneTreeOutputPositions.setIsLast(1);
				outputMultistageGridPositionsList.add(sceneTreeOutputPositions);
			}
			sceneTreeOutputToilet.setChildNode(outputMultistageGridPositionsList);
			gridChildren.add(sceneTreeOutputToilet);
		}
		return flag;
	}

	private void setSceneNode(Long gridId, Integer gridLevel, List<SceneTreeOutput> gridChildren) {
		QueryWrapper<Toilet> toiletQueryWrapper = WrapperUtils.getWrapper(false);
		toiletQueryWrapper.eq("grid_level", gridLevel);
		toiletQueryWrapper.eq("enable_status", 0);
		switch (gridLevel) {
		case 1:
			toiletQueryWrapper.eq("building_id", gridId);
			break;
		case 2:
			toiletQueryWrapper.eq("floor_id", gridId);
			break;
		default:
			toiletQueryWrapper.eq("multistage_grid_id", gridId);
			break;

		}
		List<Toilet> toiletlist = toiletService.list(toiletQueryWrapper);

		for (Toilet toilet : toiletlist) {
			SceneTreeOutput sceneTreeOutputToilet = new SceneTreeOutput();
			sceneTreeOutputToilet.setId(toilet.getId() + "_s1");
			sceneTreeOutputToilet.setGridId(gridId);
			sceneTreeOutputToilet.setName(toilet.getName());
			sceneTreeOutputToilet.setLevel(1);
			sceneTreeOutputToilet.setSceneId(toilet.getId());
			sceneTreeOutputToilet.setIsscene(1);
			sceneTreeOutputToilet.setDisable(false);
			//
			QueryWrapper<Positions> positionsQueryWrapper = WrapperUtils.getWrapper(false);
			positionsQueryWrapper.eq("toilet_id", toilet.getId());
			positionsQueryWrapper.eq("enable_status", 0);

			List<Positions> positionsList = positionsService.list(positionsQueryWrapper);
			if (positionsList.isEmpty()) {
				sceneTreeOutputToilet.setIsLast(1);
			}
			List<SceneTreeOutput> outputMultistageGridPositionsList = new ArrayList<SceneTreeOutput>();
			for (Positions positions : positionsList) {
				SceneTreeOutput sceneTreeOutputPositions = new SceneTreeOutput();
				sceneTreeOutputPositions.setId(positions.getId() + "_s2");
				sceneTreeOutputPositions.setGridId(gridId);
				sceneTreeOutputPositions.setSceneId(positions.getId());
				sceneTreeOutputPositions.setName(positions.getName());
				sceneTreeOutputPositions.setLevel(2);
				sceneTreeOutputPositions.setIsscene(1);
				sceneTreeOutputPositions.setDisable(false);
				sceneTreeOutputPositions.setIsLast(1);
				outputMultistageGridPositionsList.add(sceneTreeOutputPositions);
			}
			sceneTreeOutputToilet.setChildNode(outputMultistageGridPositionsList);
			gridChildren.add(sceneTreeOutputToilet);
		}
	}

	@ApiOperation(value = "获取取消原因")
	@ResponseBody
	@RequestMapping(value = "/getTaskCancelReason", method = RequestMethod.GET)
	public BaseOutput<List<DropDownLongKeyEntity>> getTaskCancelReason(HttpServletRequest request) {
		List<DropDownLongKeyEntity> dropDownList = new ArrayList<DropDownLongKeyEntity>();
		QueryWrapper<TypeSource> queryWrapper = WrapperUtils.getWrapper(false);
		queryWrapper.eq("type_name", "taskCancelReason");
		queryWrapper.orderByAsc("order_by");
		List<TypeSource> list = typeSourceService.list(queryWrapper);
		for (TypeSource typeSource : list) {
			DropDownLongKeyEntity dropDownEntity = new DropDownLongKeyEntity();
			dropDownEntity.setId(Long.parseLong(typeSource.getName()));
			dropDownEntity.setName(typeSource.getContent());
			dropDownList.add(dropDownEntity);
		}
		return ResultResponseBeanUtil.success(dropDownList);

	}

	@ApiOperation(value = "查询设备维护任务类型")
	@ResponseBody
	@RequestMapping(value = "/selectDevOpsType", method = RequestMethod.GET)
	public BaseOutput<List<DropDownLongKeyEntity>> selectDevOpsType(HttpServletRequest request) {
		List<DropDownLongKeyEntity> dropDownList = new ArrayList<DropDownLongKeyEntity>();
		QueryWrapper<TaskType> queryWrapper = WrapperUtils.getWrapper(false);
		String assignmentIds = redisService5.hmGet("Dictionary", "OperateAssignmentTypes").toString();
		String[] split = assignmentIds.split(",");
		List<String> arrayList = new ArrayList<String>(Arrays.asList(split));

		if (assignmentIds != null && assignmentIds.length() > 0) {
			queryWrapper.in("id", arrayList);
		}
		queryWrapper.orderByAsc("id");
		List<TaskType> list = taskTypeService.list(queryWrapper);
		if (list.size() > 0) {
			for (TaskType bean : list) {
				DropDownLongKeyEntity dropDownEntity = new DropDownLongKeyEntity();
				dropDownEntity.setId(bean.getId());
				dropDownEntity.setName(bean.getName());
				dropDownList.add(dropDownEntity);
			}
		}
		return ResultResponseBeanUtil.success(dropDownList);
	}

	@ApiOperation(value = "根据场景获取树形对象(计划用)")
	@ResponseBody
	@RequestMapping(value = "/getSceneObject", method = RequestMethod.POST)
	public BaseOutput<TreeNodeEntity> getSceneObject(HttpServletRequest request, @RequestBody ObjectByScenesInput input) {
		if (input.getDistrictId() == null || input.getDistrictId() == 0) {
			return ResultResponseBeanUtil.fail("请选择所属站点");
		}
		if (input.getScenes() == null || input.getScenes().isEmpty()) {
			return ResultResponseBeanUtil.fail("请选择场景");
		}
		QueryWrapper<SceneObject> queryWrapper = WrapperUtils.getWrapper(false);
		queryWrapper.eq("district_id", input.getDistrictId());
		queryWrapper.eq("status", 1);
		queryWrapper.orderByAsc("object_parent_id");
		List<SceneObject> solist = sceneObjectService.list(queryWrapper);
		Set<String> sceneHashSet = new HashSet<>();
		for (Scene scene : input.getScenes()) {
			sceneHashSet.add(scene.getLevel() + "_" + scene.getId());
		}

		Set<Long> objectIdSet = new HashSet<>();

		TreeNodeEntity treeNodeEntity = new TreeNodeEntity();
		treeNodeEntity.setName("全部");
		treeNodeEntity.setId(0L);
		treeNodeEntity.setChildrenNode(new ArrayList<>());

		Map<Long, Integer> idMap = new HashMap<Long, Integer>();
		for (SceneObject sceneObject : solist) {
			if (objectIdSet.contains(sceneObject.getObjectId())) {
				continue;
			}
			if (sceneHashSet.contains(sceneObject.getScenesLevel() + "_" + sceneObject.getScenesId())) {
				if (sceneObject.getObjectParentId() == 0) {
					List<TreeNodeEntity> childrenNode = treeNodeEntity.getChildrenNode();
					TreeNodeEntity sontreeNodeEntity = new TreeNodeEntity();
					sontreeNodeEntity.setChildrenNode(new ArrayList<>());
					sontreeNodeEntity.setId(sceneObject.getObjectId());
					sontreeNodeEntity.setLevel(sceneObject.getLevel());
					sontreeNodeEntity.setName(sceneObject.getObjectName());
					childrenNode.add(sontreeNodeEntity);
					objectIdSet.add(sceneObject.getObjectId());
					idMap.put(sceneObject.getObjectId(), childrenNode.size() - 1);

				} else {
					if (idMap.get(sceneObject.getObjectParentId()) == null) {
						Object byId = objectService.getById(sceneObject.getObjectParentId());
						List<TreeNodeEntity> childrenNode = treeNodeEntity.getChildrenNode();
						TreeNodeEntity sontreeNodeEntity = new TreeNodeEntity();
						sontreeNodeEntity.setChildrenNode(new ArrayList<>());
						sontreeNodeEntity.setId(byId.getId());
						sontreeNodeEntity.setLevel(byId.getLevel());
						sontreeNodeEntity.setName(byId.getName());
						childrenNode.add(sontreeNodeEntity);
						objectIdSet.add(byId.getId());
						idMap.put(byId.getId(), childrenNode.size() - 1);
					}
					Integer index = idMap.get(sceneObject.getObjectParentId());
					TreeNodeEntity parentNode = treeNodeEntity.getChildrenNode().get(index);

					List<TreeNodeEntity> sonchildrenNode = parentNode.getChildrenNode();
					TreeNodeEntity subtreeNodeEntity = new TreeNodeEntity();
					subtreeNodeEntity.setChildrenNode(new ArrayList<>());
					subtreeNodeEntity.setId(sceneObject.getObjectId());
					subtreeNodeEntity.setLevel(sceneObject.getLevel());
					subtreeNodeEntity.setName(sceneObject.getObjectName());
					sonchildrenNode.add(subtreeNodeEntity);
					objectIdSet.add(sceneObject.getObjectId());
				}
			}
		}
		return ResultResponseBeanUtil.success(treeNodeEntity);
	}

	@ApiOperation(value = "根据站点查询标签")
	@ResponseBody
	@RequestMapping(value = "/selectLabel", method = RequestMethod.GET)
	public BaseOutput<List<DropDownLongKeyEntity>> selectLabel(HttpServletRequest request, @RequestParam Long districtId) {
		List<DropDownLongKeyEntity> dropDownList = new ArrayList<DropDownLongKeyEntity>();
		QueryWrapper<LabelManage> queryWrapper = WrapperUtils.getWrapper(false);

		queryWrapper.in("district_id", districtId);

		queryWrapper.orderByAsc("id");
		List<LabelManage> list = labelManageService.list(queryWrapper);
		if (list.size() > 0) {
			for (LabelManage bean : list) {
				DropDownLongKeyEntity dropDownEntity = new DropDownLongKeyEntity();
				dropDownEntity.setId(bean.getId());
				dropDownEntity.setName(bean.getLabelName());
				dropDownList.add(dropDownEntity);
			}
		}
		return ResultResponseBeanUtil.success(dropDownList);
	}

	@ApiOperation(value = "获取时薪设置类型 时薪设置页面使用")
	@ResponseBody
	@RequestMapping(value = "/getWageType", method = RequestMethod.GET)
	public BaseOutput<List<DropDownLongKeyEntity>> getWageType(HttpServletRequest request) {
		List<DropDownLongKeyEntity> dropDownList = new ArrayList<DropDownLongKeyEntity>();
		QueryWrapper<TypeSource> queryWrapper = WrapperUtils.getWrapper(false);
		queryWrapper.eq("type_name", "wageType");
		queryWrapper.orderByAsc("order_by");
		List<TypeSource> list = typeSourceService.list(queryWrapper);
		for (TypeSource typeSource : list) {
			DropDownLongKeyEntity dropDownEntity = new DropDownLongKeyEntity();
			dropDownEntity.setId(Long.parseLong(typeSource.getName()));
			dropDownEntity.setName(typeSource.getContent());
			dropDownList.add(dropDownEntity);
		}
		return ResultResponseBeanUtil.success(dropDownList);
	}

	@ApiOperation(value = "获取站点时薪类型")
	@ResponseBody
	@RequestMapping(value = "/getDistrictWage", method = RequestMethod.GET)
	public BaseOutput<List<DropDownLongKeyEntity>> getDistrictWage(HttpServletRequest request, @RequestParam Long districtId) {
		if (districtId == null) {
			return ResultResponseBeanUtil.fail("请选择站点");
		}
		List<DropDownLongKeyEntity> dropDownList = new ArrayList<DropDownLongKeyEntity>();
		QueryWrapper<Wage> queryWrapper = WrapperUtils.getWrapper(false);
		queryWrapper.eq("district_id", districtId);
		queryWrapper.orderByAsc("type");
		List<Wage> list = wageService.list(queryWrapper);
		for (Wage typeSource : list) {
			DropDownLongKeyEntity dropDownEntity = new DropDownLongKeyEntity();
			dropDownEntity.setId(typeSource.getId().longValue());
			dropDownEntity.setName(typeSource.getName());
			dropDownList.add(dropDownEntity);
		}
		return ResultResponseBeanUtil.success(dropDownList);
	}

	@ApiOperation(value = "指定网格树结构")
	@ResponseBody
	@RequestMapping(value = "/gridSubTree", method = RequestMethod.POST)
	public BaseOutput<GridTreeOutput> gridSubTree(HttpServletRequest request, @RequestBody AppointGridTreeOutput input) {
		GridTreeOutput cridTreeOutput = new GridTreeOutput();
		List<GridTreeOutput> output = new ArrayList<GridTreeOutput>();

//		楼座-》楼层-》多级网格
		QueryWrapper<Building> wrapperBuilding = WrapperUtils.getWrapper(false);
		wrapperBuilding.eq("district_id", input.getDistrictId());

		wrapperBuilding.orderByAsc("orderby");
		List<Building> listBuilding = buildingService.list(wrapperBuilding);
		// 1级
		for (Building building : listBuilding) {
			GridTreeOutput gridTreeOutputB = new GridTreeOutput();
			gridTreeOutputB.setId(building.getId() + "_1");
			gridTreeOutputB.setGridId(building.getId());
			gridTreeOutputB.setName(building.getName());
			gridTreeOutputB.setGridLevel(1);
			// 查楼层 2级
			QueryWrapper<Floor> wrapperFloor = WrapperUtils.getWrapper(false);
			wrapperFloor.eq("building_id", building.getId());
			wrapperFloor.orderByAsc("orderby");
			List<Floor> listFloor = floorService.list(wrapperFloor);
			List<GridTreeOutput> outputFloor = new ArrayList<GridTreeOutput>();
			for (Floor floor : listFloor) {
				GridTreeOutput gridTreeOutputF = new GridTreeOutput();
				gridTreeOutputF.setId(floor.getId() + "_2");
				gridTreeOutputF.setGridId(floor.getId());
				gridTreeOutputF.setName(floor.getName());
				gridTreeOutputF.setGridLevel(2);
				// 查多级网格 3级
				QueryWrapper<MultistageGrid> wrapperMultistageGrid = WrapperUtils.getWrapper(false);
				wrapperMultistageGrid.eq("superior_id", floor.getId());
				wrapperMultistageGrid.eq("grid_level", 3);// 直接查三级
				wrapperMultistageGrid.orderByAsc("orderby");
				List<MultistageGrid> listMultistageGrid = multistageGridService.list(wrapperMultistageGrid);
				List<GridTreeOutput> outputMultistageGrid = new ArrayList<GridTreeOutput>();
				for (MultistageGrid multistageGrid : listMultistageGrid) {
					GridTreeOutput gridTreeOutputM = new GridTreeOutput();
					gridTreeOutputM.setId(multistageGrid.getId() + "_3");
					gridTreeOutputM.setGridId(multistageGrid.getId());
					gridTreeOutputM.setName(multistageGrid.getName());
					gridTreeOutputM.setGridLevel(3);
					// 查多级网格 4级
					QueryWrapper<MultistageGrid> wrapperMultistageGrid2 = WrapperUtils.getWrapper(false);
					wrapperMultistageGrid2.eq("superior_id", floor.getId());
					wrapperMultistageGrid2.eq("grid_level", 4);// 直接查四级
					wrapperMultistageGrid2.orderByAsc("orderby");
					List<MultistageGrid> listMultistageGrid1 = multistageGridService.list(wrapperMultistageGrid2);
					List<GridTreeOutput> outputMultistageGrid1 = new ArrayList<GridTreeOutput>();
					for (MultistageGrid multistageGrid1 : listMultistageGrid1) {
						GridTreeOutput gridTreeOutputM1 = new GridTreeOutput();
						gridTreeOutputM1.setId(multistageGrid1.getId() + "_4");
						gridTreeOutputM1.setGridId(multistageGrid1.getId());
						gridTreeOutputM1.setName(multistageGrid1.getName());
						gridTreeOutputM1.setGridLevel(4);
						outputMultistageGrid1.add(gridTreeOutputM1);
					}
					gridTreeOutputM.setChildNode(outputMultistageGrid1);
					outputMultistageGrid.add(gridTreeOutputM);
				}
				gridTreeOutputF.setChildNode(outputMultistageGrid);
				outputFloor.add(gridTreeOutputF);
			}
			gridTreeOutputB.setChildNode(outputFloor);
			output.add(gridTreeOutputB);
		}
		cridTreeOutput.setId("0_0");
		cridTreeOutput.setName("全部");
		cridTreeOutput.setChildNode(output);
		return ResultResponseBeanUtil.success(cridTreeOutput);
	}

	@ApiOperation(value = "指定网格场景树结构")
	@ResponseBody
	@RequestMapping(value = "/gridSceneAppointTree", method = RequestMethod.POST)
	public BaseOutput<SceneTreeOutput> gridSceneAppointTree(HttpServletRequest request, @RequestBody AppointGridTreeOutput input) {
		if (StringUtil.isNull(input.getDistrictId())) {
			return ResultResponseBeanUtil.fail("请传递站点ID");
		}
		Building curBuilding = null;
		Floor curFloor = null;
		MultistageGrid curMultistageGrid = null;
		Integer gridLevel = input.getGridLevel();
		switch (gridLevel) {
		case 1:
			curBuilding = buildingService.getById(input.getGridId());
			break;
		case 2:
			curFloor = floorService.getById(input.getGridId());

			curBuilding = buildingService.getById(curFloor.getBuildingId());

			break;
		case 3:
			curMultistageGrid = multistageGridService.getById(input.getGridId());
			curFloor = floorService.getById(curMultistageGrid.getFloorId());
			curBuilding = buildingService.getById(curMultistageGrid.getBuildingId());
			break;
		}

		SceneTreeOutput cridTreeOutput = new SceneTreeOutput();
		List<SceneTreeOutput> output = new ArrayList<SceneTreeOutput>();
//		楼座-》楼层-》多级网格
		QueryWrapper<Building> wrapperBuilding = WrapperUtils.getWrapper(false);
		wrapperBuilding.eq("district_id", input.getDistrictId());
		if (curBuilding != null) {
			wrapperBuilding.eq("id", curBuilding.getId());
		}
		wrapperBuilding.orderByAsc("orderby");
		List<Building> listBuilding = buildingService.list(wrapperBuilding);

		// 1级
		for (Building building : listBuilding) {
			SceneTreeOutput gridTreeOutputB = new SceneTreeOutput();
			gridTreeOutputB.setId(building.getId() + "_1");
			gridTreeOutputB.setGridId(building.getId());
			gridTreeOutputB.setName(building.getName());
			gridTreeOutputB.setLevel(1);
			// 查楼层 2级
			QueryWrapper<Floor> wrapperFloor = WrapperUtils.getWrapper(false);
			wrapperFloor.eq("building_id", building.getId());
			if (curFloor != null) {
				wrapperFloor.eq("id", curFloor.getId());
			}
			wrapperFloor.orderByAsc("orderby");
			List<Floor> listFloor = floorService.list(wrapperFloor);
			List<SceneTreeOutput> outputFloor = new ArrayList<SceneTreeOutput>();
			for (Floor floor : listFloor) {
				SceneTreeOutput gridTreeOutputF = new SceneTreeOutput();
				gridTreeOutputF.setId(floor.getId() + "_2");
				gridTreeOutputF.setGridId(floor.getId());
				gridTreeOutputF.setName(floor.getName());
				gridTreeOutputF.setLevel(2);
				// 查多级网格 3级
				QueryWrapper<MultistageGrid> wrapperMultistageGrid = WrapperUtils.getWrapper(false);
				wrapperMultistageGrid.eq("superior_id", floor.getId());
				wrapperMultistageGrid.eq("grid_level", 3);// 直接查三级
				if (curMultistageGrid != null) {
					wrapperFloor.eq("id", curMultistageGrid.getId());
				}
				wrapperMultistageGrid.orderByAsc("orderby");
				List<MultistageGrid> listMultistageGrid = multistageGridService.list(wrapperMultistageGrid);
				List<SceneTreeOutput> outputMultistageGrid = new ArrayList<SceneTreeOutput>();
				for (MultistageGrid multistageGrid : listMultistageGrid) {
					SceneTreeOutput gridTreeOutputM = new SceneTreeOutput();
					gridTreeOutputM.setId(multistageGrid.getId() + "_3");
					gridTreeOutputM.setGridId(multistageGrid.getId());
					gridTreeOutputM.setName(multistageGrid.getName());
					gridTreeOutputM.setLevel(3);
					// 查多级网格 4级
					QueryWrapper<MultistageGrid> wrapperMultistageGrid2 = WrapperUtils.getWrapper(false);
					wrapperMultistageGrid2.eq("superior_id", floor.getId());
					wrapperMultistageGrid2.eq("grid_level", 4);// 直接查四级
					wrapperMultistageGrid2.orderByAsc("orderby");
					List<MultistageGrid> listMultistageGrid1 = multistageGridService.list(wrapperMultistageGrid2);
					List<SceneTreeOutput> outputMultistageGrid1 = new ArrayList<SceneTreeOutput>();
					for (MultistageGrid multistageGrid1 : listMultistageGrid1) {
						SceneTreeOutput gridTreeOutputM1 = new SceneTreeOutput();
						gridTreeOutputM1.setId(multistageGrid1.getId() + "_4");
						gridTreeOutputM1.setGridId(multistageGrid1.getId());
						gridTreeOutputM1.setName(multistageGrid1.getName());
						gridTreeOutputM1.setLevel(4);
						List<SceneTreeOutput> outputMultistageGridToiletList = new ArrayList<SceneTreeOutput>();

						setSceneNode(multistageGrid1.getId(), 4, outputMultistageGridToiletList);

						gridTreeOutputM1.setChildNode(outputMultistageGridToiletList);
						outputMultistageGrid1.add(gridTreeOutputM1);

					}

					setSceneNode(multistageGrid.getId(), 3, outputMultistageGrid1);
					gridTreeOutputM.setChildNode(outputMultistageGrid1);
					outputMultistageGrid.add(gridTreeOutputM);
				}
				setSceneNode(floor.getId(), 2, outputMultistageGrid);
				gridTreeOutputF.setChildNode(outputMultistageGrid);
				outputFloor.add(gridTreeOutputF);
			}
			setSceneNode(building.getId(), 1, outputFloor);
			gridTreeOutputB.setChildNode(outputFloor);
			output.add(gridTreeOutputB);
		}
		cridTreeOutput.setId("0_0");
		cridTreeOutput.setName("全部");
		cridTreeOutput.setChildNode(output);
		return ResultResponseBeanUtil.success(cridTreeOutput);
	}

}
