/*
 *      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 com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
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.system.expand.feign.IConfClient;
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.vo.BuildingVO;
import org.springblade.community.wrapper.BuildingWrapper;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.Map;

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

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

	private final IOrgPositionClient orgPositionClient;
	private final IConfClient confClient;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入building")
	public R<BuildingVO> detail(Building building) {
		Building detail = buildingService.getOne(Condition.getQueryWrapper(building));
		return R.data(BuildingWrapper.build().entityVO(detail));
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入building")
	public R<IPage<BuildingVO>> list(@RequestParam Map<String,Object> building, Query query, BladeUser bladeUser) {
		Long residentialId = null;
		if(building.get("residentialId") != null){
			residentialId = Long.parseLong(String.valueOf(building.get("residentialId")));
			building.remove("residentialId");
		}
		LambdaQueryWrapper<Building> buildingLambdaQueryWrapper = new QueryWrapperUtil<Building>().getBasicQueryWrapperByMap(Building::getOrgPosition, building, new Building(), bladeUser, orgPositionClient);
		IPage<Building> pages = buildingService.page(Condition.getPage(query), buildingLambdaQueryWrapper.eq(residentialId != null, Building::getResidentialId, residentialId));
		return R.data(BuildingWrapper.build().pageVO(pages));
	}


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

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

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

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入building")
	public R submit(@Valid @RequestBody Building building) {
		Assert.notNull(building.getResidentialId(),"所属小区不能为空");
		Residential residential = residentialService.getById(building.getResidentialId());
		Assert.notNull(residential,"对应的小区不存在");
		if (building.getId() == null) {
			residential.setBuildingCount(residential.getBuildingCount() == null ? 1 : residential.getBuildingCount() + 1);
			if (buildingService.save(building)) {
				residentialService.updateById(residential);
			}
		}
		building.setAgencyId(residential.getAgencyId());
		building.setAgencyName(residential.getAgencyName());
		building.setResidentialName(residential.getName());
		building.setOrgPosition(residential.getOrgPosition());
		building.setPic(confClient.removePrefix(building.getPic()));
		return R.status(buildingService.saveOrUpdateBuilding(building));
	}


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

	/**
	 * 批量新增
	 */
	@PostMapping("/batchAdd")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "批量添加", notes = "batchAddInfo")
	public R batchAdd(@Valid @RequestBody BatchAddInfo batchAddInfo) {
		return R.status(buildingService.batchAddBuilding(batchAddInfo));
	}



}
