package xiaoqu.home.open.service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

import xiaoqu.home.open.controller.input.workgroup.WorkGroupToPrincipalInput;
import xiaoqu.home.open.controller.output.grid.GridOutput;
import xiaoqu.home.open.model.db1.Building;
import xiaoqu.home.open.model.db1.Floor;
import xiaoqu.home.open.model.db1.GridToPrincipal;
import xiaoqu.home.open.model.db1.MultistageGrid;
import xiaoqu.home.open.model.db1.User;
import xiaoqu.home.open.service.db1.BuildingService;
import xiaoqu.home.open.service.db1.FloorService;
import xiaoqu.home.open.service.db1.GridToPrincipalService;
import xiaoqu.home.open.service.db1.MultistageGridService;
import xiaoqu.home.open.service.db1.UserService;
import xiaoqu.home.open.utils.StringUtil;
import xiaoqu.home.open.utils.WrapperUtils;

@Service
public class PublicService {
	@Autowired
	private BuildingService buildingService;
	@Autowired
	private FloorService floorService;
	@Autowired
	private UserService userService;
	@Autowired
	private MultistageGridService multistageGridService;
	@Autowired
	private GridToPrincipalService gridToPrincipalService;

	/***************************
	 * 根据网格级别和网格ID查网格对象
	 * 
	 * @param gridLevel
	 * @param id
	 * @return GridOutput 网格输出
	 *******************************/
	public GridOutput getGridByLevelAndId(int gridLevel, Long id) {
		GridOutput gridOutput = new GridOutput();
		Long grid_id = 0L;
		switch (gridLevel) {
		case 1:
			Building building = buildingService.getById(id);
			if (building == null) {
				return null;
			}
			BeanUtils.copyProperties(building, gridOutput);
			grid_id = building.getId();
			break;
		case 2:
			Floor floor = floorService.getById(id);
			if (floor == null) {
				return null;
			}
			BeanUtils.copyProperties(floor, gridOutput);
			grid_id = floor.getId();
			break;
		default:
			MultistageGrid multistageGriddefault = multistageGridService.getById(id);
			if (multistageGriddefault == null) {
				return null;
			}
			BeanUtils.copyProperties(multistageGriddefault, gridOutput);
			grid_id = multistageGriddefault.getId();
			break;
		}
		// 查下负责人
		QueryWrapper<GridToPrincipal> wrapper = WrapperUtils.getWrapper(false);
		wrapper.eq("grid_id", grid_id);
		wrapper.eq("grid_level", gridLevel);
		List<GridToPrincipal> list = new ArrayList<GridToPrincipal>();
		list = gridToPrincipalService.list(wrapper);
		gridOutput.setPrincipal(list.stream().map(gridToPrincipal -> gridToPrincipal.getUserName()).collect(Collectors.joining(",")));
		if (StringUtil.isNotEmpty(list)) {
			List<WorkGroupToPrincipalInput> userIds = new ArrayList<WorkGroupToPrincipalInput>();
			for (GridToPrincipal gridToPrincipal : list) {
				WorkGroupToPrincipalInput workGroupToPrincipalInput = new WorkGroupToPrincipalInput();
				User userTemp = userService.getById(gridToPrincipal.getUserId()); 
				workGroupToPrincipalInput.setId(gridToPrincipal.getUserId());
				workGroupToPrincipalInput.setMobile(userTemp.getMobile());
				workGroupToPrincipalInput.setName(userTemp.getName());
				userIds.add(workGroupToPrincipalInput);
			}
			gridOutput.setUserIds(userIds);
		}
		return gridOutput;
	}

	/****************************
	 * 根据级别和名称判断网格名称是否存在
	 * 
	 * @param gridLevel
	 * @param gridName
	 * @param districtId
	 * @param buildingId
	 * @param floorId
	 * @return boolean
	 **************************/
	public boolean isExistGridByLevelAndName(int gridLevel, Long districtId, Long buildingId, Long floorId, String gridName) {
		boolean flag = false;
		switch (gridLevel) {
		case 1:
			QueryWrapper<Building> wrapperB = WrapperUtils.getWrapper(false);
			wrapperB.eq("name", gridName);
			wrapperB.eq("district_id", districtId);
			int countB = buildingService.count(wrapperB);
			if (countB > 0) {
				flag = true;
			}
			break;
		case 2:
			QueryWrapper<Floor> wrapperF = WrapperUtils.getWrapper(false);
			wrapperF.eq("name", gridName);
			wrapperF.eq("building_id", buildingId);
			wrapperF.eq("district_id", districtId);
			int countF = floorService.count(wrapperF);
			if (countF > 0) {
				flag = true;
			}
			break;
		default:
			QueryWrapper<MultistageGrid> wrapperM = WrapperUtils.getWrapper(false);
			wrapperM.eq("name", gridName);
			wrapperM.eq("grid_level", gridLevel);
			wrapperM.eq("district_id", districtId);
			wrapperM.eq("building_id", buildingId);
			wrapperM.eq("floor_id", floorId);
			wrapperM.eq("grid_level", gridLevel);
			int countM = multistageGridService.count(wrapperM);
			if (countM > 0) {
				flag = true;
			}
			break;
		}
		return flag;
	}
}
