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

import lombok.AllArgsConstructor;
import net.logstash.logback.encoder.org.apache.commons.lang3.StringUtils;
import org.springblade.community.agency.entity.Agency;
import org.springblade.community.agency.service.IAgencyService;
import org.springblade.community.building.entity.BatchAddInfo;
import org.springblade.community.building.entity.Building;
import org.springblade.community.building.vo.BuildingVO;
import org.springblade.community.building.mapper.BuildingMapper;
import org.springblade.community.building.service.IBuildingService;
import org.springblade.community.floor.entity.Floor;
import org.springblade.community.floor.service.IFloorService;
import org.springblade.community.residential.entity.Residential;
import org.springblade.community.residential.service.IResidentialService;
import org.springblade.community.room.entity.Room;
import org.springblade.community.room.service.IRoomService;
import org.springblade.community.unit.entity.Unit;
import org.springblade.community.unit.service.IUnitService;
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 2020-10-31
 */
@Service
@AllArgsConstructor
public class BuildingServiceImpl extends BaseServiceImpl<BuildingMapper, Building> implements IBuildingService {

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

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

	@Override
	@Transactional
	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.getAgencyName());
			// 楼栋命名
			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.getAgencyName());
					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.getAgencyName());
							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.getAgencyName());
									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.getRoomNumber();
							if ("null".equals(roomNumber + "")){
								roomNumber = 0;
							}
							floor.setRoomNumber(roomNumber + roomCount);
							floorService.updateById(floor);
						}
						//更新单元的楼层数、房间数
						Integer floorNumber = unit.getFloorNumber();
						if ("null".equals(floorNumber + "")){
							floorNumber = 0;
						}
						unit.setFloorNumber(floorNumber + floorCount);
						Integer roomNumber = unit.getRoomNumber();
						if ("null".equals(roomNumber + "")){
							roomNumber = 0;
						}
						unit.setRoomNumber(roomNumber + roomCount * floorCount);
						unitService.updateById(unit);
					}
				}
				// 更新楼栋的单元数、楼层数、房间数
				Integer unitNumber = building.getUnitNumber();
				if ("null".equals(unitNumber + "")){
					unitNumber = 0;
				}
				building.setUnitNumber(unitNumber + unitCount);
				Integer floorNumber = building.getFloorNumber();
				if ("null".equals(floorNumber + "")){
					floorNumber = 0 ;
				}
				building.setFloorNumber(floorNumber + floorCount * unitCount);
				Integer roomNumber = building.getRoomNumber();
				if ("null".equals(roomNumber + "")){
					roomNumber = 0 ;
				}
				building.setRoomNumber(roomNumber + roomCount * floorCount * unitCount);

				this.updateById(building);
			}
		}
		// 更新小区的楼栋数、单元数、楼层数、房间数
		Integer buildingNumber = residential.getBuildingNumber();
		if ("null".equals(buildingNumber + "")){
			buildingNumber = 0 ;
		}
		residential.setBuildingNumber(buildingNumber + buildingCount);
		Integer unitNumber = residential.getUnitNumber();
		if ("null".equals(unitNumber + "")){
			unitNumber = 0;
		}
		residential.setUnitNumber(unitNumber + unitCount * buildingCount);
		Integer floorNumber = residential.getFloorNumber();
		if ("null".equals(floorNumber + "")){
			floorNumber = 0 ;
		}
		residential.setFloorNumber(floorNumber + floorCount * unitCount * buildingCount);
		Integer roomNumber = residential.getRoomNumber();
		if ("null".equals(roomNumber + "")){
			roomNumber = 0 ;
		}
		residential.setRoomNumber(roomNumber + roomCount * floorCount * unitCount * buildingCount);
		residentialService.updateById(residential);

		return true;
	}

}
