/*
 *      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.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.community.dto.AgencyMngDelDTO;
import org.springblade.community.entity.*;
import org.springblade.community.service.*;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.system.expand.feign.IOrgPositionClient;
import org.springblade.system.expand.util.QueryWrapperUtil;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.community.vo.RoomVO;
import org.springblade.community.wrapper.RoomWrapper;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.Map;

/**
 * 房间表 控制器
 *
 * @author BladeX
 * @since 2021-03-29
 */
@RestController
@AllArgsConstructor
@RequestMapping("/room")
@Api(value = "房间表", tags = "房间表接口")
public class RoomController extends BladeController {

	private final IRoomService roomService;
	private final IFloorService floorService;
	private final IUnitService unitService;
	private final IResidentialService residentialService;
	private final IBuildingService buildingService;
	private final IOrgPositionClient orgPositionClient;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入room")
	public R<RoomVO> detail(Room room) {
		Room detail = roomService.getOne(Condition.getQueryWrapper(room));
		return R.data(RoomWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 房间表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入room")
	public R<IPage<RoomVO>> list(@RequestParam Map<String,Object> room, Query query, BladeUser bladeUser) {
		Long floorId = null;
		Long residentialId = null;
		Long buildingId = null;
		if(room.get("floorId") != null){
			floorId = Long.parseLong(String.valueOf(room.get("floorId")));
			room.remove("floorId");
		}
		if(room.get("residentialId") != null){
			residentialId = Long.parseLong(String.valueOf(room.get("residentialId")));
			room.remove("residentialId");
		}
		if(room.get("buildingId") != null){
			buildingId = Long.parseLong(String.valueOf(room.get("buildingId")));
			room.remove("buildingId");
		}
		LambdaQueryWrapper<Room> roomLambdaQueryWrapper = new QueryWrapperUtil<Room>().getBasicQueryWrapperByMap(Room::getOrgPosition, room, new Room(), bladeUser, orgPositionClient);
		roomLambdaQueryWrapper.eq(residentialId != null, Room::getResidentialId, residentialId);
		roomLambdaQueryWrapper.eq(buildingId != null, Room::getBuildingId, buildingId);
		roomLambdaQueryWrapper.eq(floorId != null, Room::getFloorId, floorId);
		IPage<Room> pages = roomService.page(Condition.getPage(query), roomLambdaQueryWrapper);
		return R.data(RoomWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 房间表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入room")
	public R<IPage<RoomVO>> page(RoomVO room, Query query) {
		IPage<RoomVO> pages = roomService.selectRoomPage(Condition.getPage(query), room);
		return R.data(pages);
	}

	/**
	 * 新增 房间表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入room")
	public R save(@Valid @RequestBody Room room) {
		return R.status(roomService.save(room));
	}

	/**
	 * 修改 房间表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入room")
	public R update(@Valid @RequestBody Room room) {
		return R.status(roomService.updateById(room));
	}

	/**
	 * 新增或修改 房间表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入room")
	public R submit(@Valid @RequestBody Room room) {
		Residential residential = residentialService.getById(room.getResidentialId());
		Assert.notNull(residential,"选择的小区不存在");

		Building building = buildingService.getById(room.getBuildingId());
		Assert.notNull(building,"选择的楼栋不存在");
		Assert.notNull(room.getFloorId(),"楼层不能为空");
		Unit unit = unitService.getById(room.getUnitId());
		Assert.notNull(unit,"对应的单元不存在");
		Floor floor = floorService.getById(room.getFloorId());
		Assert.notNull(floor,"对应的楼层不存在");

		if (room.getId() == null) {
			residential.setRoomCount(residential.getRoomCount() == null ? 1 : residential.getRoomCount() + 1);
			building.setRoomCount(building.getRoomCount() == null ? 1 : building.getRoomCount() + 1);
			unit.setRoomCount(unit.getRoomCount() == null ? 1 : unit.getRoomCount() + 1);
			floor.setRoomCount(floor.getRoomCount() == null ? 1 : floor.getRoomCount() + 1);
			if (roomService.save(room)) {
				residentialService.updateById(residential);
				buildingService.updateById(building);
				unitService.updateById(unit);
				floorService.updateById(floor);
			}
		}
		room.setAgencyId(floor.getAgencyId());
		room.setAgencyName(floor.getAgencyName());
		room.setResidentialId(floor.getResidentialId());
		room.setResidentialName(floor.getResidentialName());
		room.setBuildingId(floor.getBuildingId());
		room.setBuildingName(floor.getBuildingName());
		room.setUnitId(floor.getUnitId());
		room.setUnitName(floor.getUnitName());
		room.setFloorName(floor.getName());
		room.setOrgPosition(floor.getOrgPosition());
		return R.status(roomService.saveOrUpdateRoom(room));
	}


	/**
	 * 删除 房间表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(roomService.deleteLogic(Func.toLongList(ids)));
	}


	/**
	 * 物理删除，及其所有子节点 楼层...房间
	 */
	@PostMapping("/removeAll")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@Transactional(rollbackFor = Exception.class)
	public R remove(@RequestParam Long id) {
		AgencyMngDelDTO delDTO = new AgencyMngDelDTO();
		delDTO.setRoomId(id);
		//物理删除房间
		roomService.delete(delDTO);
		return R.status(true);
	}


}
