/*
 *      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.Building;
import org.springblade.community.entity.Residential;
import org.springblade.community.entity.Unit;
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.system.expand.feign.IOrgPositionClient;
import org.springblade.system.expand.util.QueryWrapperUtil;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.community.entity.Floor;
import org.springblade.community.vo.FloorVO;
import org.springblade.community.wrapper.FloorWrapper;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.Map;

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

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

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入floor")
	public R<FloorVO> detail(Floor floor) {
		Floor detail = floorService.getOne(Condition.getQueryWrapper(floor));
		return R.data(FloorWrapper.build().entityVO(detail));
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入floor")
	public R<IPage<FloorVO>> list(@RequestParam Map<String,Object> floor, Query query, BladeUser bladeUser) {
		Long unitId = null;
		if(floor.get("unitId") != null){
			unitId = Long.parseLong(String.valueOf(floor.get("unitId")));
			floor.remove("unitId");
		}
		IPage<Floor> pages = floorService.page(Condition.getPage(query), Condition.getQueryWrapper(floor, Floor.class).lambda().eq(unitId != null, Floor::getUnitId, unitId));
		return R.data(FloorWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入floor")
	public R<IPage<FloorVO>> page(FloorVO floor, Query query) {
		IPage<FloorVO> pages = floorService.selectFloorPage(Condition.getPage(query), floor);
		return R.data(pages);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入floor")
	public R save(@Valid @RequestBody Floor floor) {
		return R.status(floorService.save(floor));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入floor")
	public R update(@Valid @RequestBody Floor floor) {
		return R.status(floorService.updateById(floor));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入floor")
	public R submit(@Valid @RequestBody Floor floor) {
		Residential residential = residentialService.getById(floor.getResidentialId());
		Assert.notNull(residential,"选择的小区不存在");
		Building building = buildingService.getById(floor.getBuildingId());
		Assert.notNull(building,"选择的楼栋不存在");
		Assert.notNull(floor.getUnitId(),"单元不能为空");
		Unit unit = unitService.getById(floor.getUnitId());
		Assert.notNull(unit,"对应的单元不存在");
		if (floor.getId() == null) {
			residential.setFloorCount(residential.getFloorCount() == null ? 1 : residential.getFloorCount() + 1);
			building.setFloorCount(building.getFloorCount() == null ? 1 : building.getFloorCount() + 1);
			unit.setFloorCount(unit.getFloorCount() == null ? 1 : unit.getFloorCount() + 1);
			if (floorService.save(floor)) {
				residentialService.updateById(residential);
				buildingService.updateById(building);
				unitService.updateById(unit);
			}
		}
		floor.setAgencyId(unit.getAgencyId());
		floor.setAgencyName(unit.getAgencyName());
		floor.setResidentialId(unit.getResidentialId());
		floor.setResidentialName(unit.getResidentialName());
		floor.setBuildingId(unit.getBuildingId());
		floor.setBuildingName(unit.getBuildingName());
		floor.setUnitName(unit.getName());
		floor.setOrgPosition(unit.getOrgPosition());
		return R.status(floorService.saveOrUpdateFloor(floor));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(floorService.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.setFloorId(id);
		//物理删除楼层
		floorService.delete(delDTO);
		//物理删除房间
		roomService.delete(delDTO);
		return R.status(true);
	}

}
