/*
 *      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.community.service.impl;

import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import lombok.AllArgsConstructor;
import net.logstash.logback.encoder.org.apache.commons.lang3.StringUtils;
import org.springblade.community.dto.AgencyMngDelDTO;
import org.springblade.community.entity.*;
import org.springblade.community.service.*;
import org.springblade.community.vo.BuildingVO;
import org.springblade.community.mapper.BuildingMapper;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;

/**
 * 服务实现类
 *
 * @author BladeX
 * @since 2021-03-29
 */
@AllArgsConstructor
@Service
public class BuildingServiceImpl extends BaseServiceImpl<BuildingMapper, Building> implements IBuildingService {

	private final IAgencyService agencyService;
	private final IResidentialService residentialService;
	private final IUnitService unitService;
	private final IFloorService floorService;
	private final IRoomService roomService;

	@Override
	public IPage<BuildingVO> selectBuildingPage(IPage<BuildingVO> page, BuildingVO building) {
		return page.setRecords(baseMapper.selectBuildingPage(page, building));
	}

	@Override
	@Transactional
	public boolean saveOrUpdateBuilding(Building building) {

		if (building.getId() == null && !this.save(building)) {
			return false;
		}

		building.setOrgPosition(building.getOrgPosition() + "/" + building.getId());
		return this.updateById(building);

	}

	@Override
	public boolean batchAddBuilding(BatchAddInfo batchAddInfo) {
		Residential residential = residentialService.getById(batchAddInfo.getResidentialId());
		Agency agency = agencyService.getById(residential.getAgencyId());

		// 计算将要新建的楼栋数、单元数、楼层数、房间数
		Integer buildingCount = batchAddInfo.getBuildingEnd() - batchAddInfo.getBuildingBegin() + 1;
		Integer unitCount = batchAddInfo.getUnitEnd() - batchAddInfo.getUnitBegin() + 1;
		Integer floorCount = batchAddInfo.getFloorEnd() - batchAddInfo.getFloorBegin() + 1;
		Integer roomCount = batchAddInfo.getRoomEnd() - batchAddInfo.getRoomBegin() + 1;

		// 遍历新建楼栋
		for (int i = batchAddInfo.getBuildingBegin(); i <= batchAddInfo.getBuildingEnd(); i++) {
			Building building = new Building();
			building.setResidentialId(batchAddInfo.getResidentialId());
			building.setResidentialName(residential.getName());
			building.setAgencyId(agency.getId());
			building.setAgencyName(agency.getName());
			// 楼栋命名
			String buildingName = "";
			if (StringUtils.isNotBlank(batchAddInfo.getBuildingPrefix())) {
				buildingName += batchAddInfo.getBuildingPrefix();
			}
			buildingName += (i + "");
			if (StringUtils.isNotBlank(batchAddInfo.getBuildingSuffix())) {
				buildingName += batchAddInfo.getBuildingSuffix();
			} else {
				buildingName += "栋";
			}
			building.setName(buildingName);
			if (this.save(building)) {
				// 更新楼栋的orgPosition
				building.setOrgPosition(residential.getOrgPosition() + "/" + building.getId());
				this.updateById(building);

				// 遍历新建单元
				for (int j = batchAddInfo.getUnitBegin(); j <= batchAddInfo.getUnitEnd(); j++) {
					Unit unit = new Unit();
					unit.setBuildingId(building.getId());
					unit.setBuildingName(building.getName());
					unit.setAgencyId(agency.getId());
					unit.setAgencyName(agency.getName());
					unit.setResidentialId(residential.getId());
					unit.setResidentialName(residential.getName());

					// 单元命名
					String unitName = "";
					if (StringUtils.isNotBlank(batchAddInfo.getUnitPrefix())) {
						unitName += batchAddInfo.getUnitPrefix();
					}
					unitName += (j + "");
					if (StringUtils.isNotBlank(batchAddInfo.getUnitSuffix())) {
						unitName += batchAddInfo.getUnitSuffix();
					} else {
						unitName += "单元";
					}
					unit.setName(unitName);
					if (unitService.save(unit)) {
						// 更新单元orgPosition
						unit.setOrgPosition(building.getOrgPosition() + "/" + unit.getId());
						unitService.updateById(unit);

						// 遍历新建楼层
						for (int k = batchAddInfo.getFloorBegin(); k <= batchAddInfo.getFloorEnd(); k++) {
							//跳过排除楼层
							if (StringUtils.isNotBlank(batchAddInfo.getFloorExclude()) && (batchAddInfo.getFloorExclude()).contains((k + ""))) {
								continue;
							}
							Floor floor = new Floor();
							floor.setAgencyId(agency.getId());
							floor.setAgencyName(agency.getName());
							floor.setResidentialId(residential.getId());
							floor.setResidentialName(residential.getName());
							floor.setBuildingId(building.getId());
							floor.setBuildingName(building.getName());
							floor.setUnitId(unit.getId());
							floor.setUnitName(unitName);

							// 楼层命名
							String floorName = "";
							if (StringUtils.isNotBlank(batchAddInfo.getFloorPrefix())) {
								floorName += batchAddInfo.getFloorPrefix();
							}
							floorName += (k + "");
							if (StringUtils.isNotBlank(batchAddInfo.getFloorSuffix())) {
								floorName += batchAddInfo.getFloorSuffix();
							} else {
								floorName += "层";
							}
							floor.setName(floorName);

							if (floorService.save(floor)) {
								//更新楼层orgPosition
								floor.setOrgPosition(unit.getOrgPosition() + "/" + floor.getId());
								floorService.updateById(floor);

								// 遍历新建房间/房屋
								for (int m = batchAddInfo.getRoomBegin(); m <= batchAddInfo.getRoomEnd(); m++) {
									// 跳过排除楼层
									if (StringUtils.isNotBlank(batchAddInfo.getRoomExclude()) && (batchAddInfo.getRoomExclude()).contains((m + ""))) {
										continue;
									}
									Room room = new Room();
									room.setAgencyId(agency.getId());
									room.setAgencyName(agency.getName());
									room.setResidentialId(residential.getId());
									room.setResidentialName(residential.getName());
									room.setBuildingId(building.getId());
									room.setBuildingName(building.getName());
									room.setUnitId(unit.getId());
									room.setUnitName(unit.getName());
									room.setFloorId(floor.getId());
									room.setFloorName(floorName);

									// 房屋命名
									String roomName = "";
									if (StringUtils.isNotBlank(batchAddInfo.getRoomPrefix())) {
										roomName += batchAddInfo.getRoomPrefix();
									}

									roomName += (k + (m < 10 ? "0" : "") + m);
									if (StringUtils.isNotBlank(batchAddInfo.getRoomSuffix())) {
										roomName += batchAddInfo.getRoomSuffix();
									}
									room.setName(roomName);
									if (roomService.save(room)) {
										// 更新房屋orgPosition
										room.setOrgPosition(floor.getOrgPosition() + "/" + room.getId());
										roomService.updateById(room);
									}
								}
							}
							//更新楼层的房间数
							Integer roomNumber = floor.getRoomCount();
							if ("null".equals(roomNumber + "")) {
								roomNumber = 0;
							}
							floor.setRoomCount(roomNumber + roomCount);
							floorService.updateById(floor);
						}
						//更新单元的楼层数、房间数
						Integer floorNumber = unit.getFloorCount();
						if ("null".equals(floorNumber + "")) {
							floorNumber = 0;
						}
						unit.setFloorCount(floorNumber + floorCount);
						Integer roomNumber = unit.getRoomCount();
						if ("null".equals(roomNumber + "")) {
							roomNumber = 0;
						}
						unit.setRoomCount(roomNumber + roomCount * floorCount);
						unitService.updateById(unit);
					}
				}
				// 更新楼栋的单元数、楼层数、房间数
				Integer unitNumber = building.getUnitCount();
				if ("null".equals(unitNumber + "")) {
					unitNumber = 0;
				}
				building.setUnitCount(unitNumber + unitCount);
				Integer floorNumber = building.getFloorCount();
				if ("null".equals(floorNumber + "")) {
					floorNumber = 0;
				}
				building.setFloorCount(floorNumber + floorCount * unitCount);
				Integer roomNumber = building.getRoomCount();
				if ("null".equals(roomNumber + "")) {
					roomNumber = 0;
				}
				building.setRoomCount(roomNumber + roomCount * floorCount * unitCount);

				this.updateById(building);
			}
		}
		// 更新小区的楼栋数、单元数、楼层数、房间数
		Integer buildingNumber = residential.getBuildingCount();
		if ("null".equals(buildingNumber + "")) {
			buildingNumber = 0;
		}
		residential.setBuildingCount(buildingNumber + buildingCount);
		Integer unitNumber = residential.getUnitCount();
		if ("null".equals(unitNumber + "")) {
			unitNumber = 0;
		}
		residential.setUnitCount(unitNumber + unitCount * buildingCount);
		Integer floorNumber = residential.getFloorCount();
		if ("null".equals(floorNumber + "")) {
			floorNumber = 0;
		}
		residential.setFloorCount(floorNumber + floorCount * unitCount * buildingCount);
		Integer roomNumber = residential.getRoomCount();
		if ("null".equals(roomNumber + "")) {
			roomNumber = 0;
		}
		residential.setRoomCount(roomNumber + roomCount * floorCount * unitCount * buildingCount);
		residentialService.updateById(residential);

		return true;
	}

	@Override
	public Boolean delete(AgencyMngDelDTO agencyMngDelDTO) {
		return baseMapper.delete(agencyMngDelDTO);
	}

}
