package org.springblade.community.feign;

import cn.hutool.core.lang.Assert;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.community.entity.Building;
import org.springblade.community.entity.Residential;
import org.springblade.community.entity.Unit;
import org.springblade.community.service.IBuildingService;
import org.springblade.community.service.IResidentialService;
import org.springblade.community.service.IUnitService;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import org.springblade.community.entity.Floor;
import org.springblade.community.vo.FloorVO;
import org.springblade.community.wrapper.FloorWrapper;
import org.springblade.community.service.IFloorService;
import org.springblade.core.boot.ctrl.BladeController;
import java.util.List;
/**
 *  client
 *
 * @author lianghanqiang
 * @since 2021-03-29
 */

@AllArgsConstructor
@RestController
@RequestMapping("/feign/floor")

public class FloorClient implements IFloorClient {

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

        /**
     * 详情
     */
    @Override
    @GetMapping("/detail")
    public FloorVO detail(@SpringQueryMap Floor floor) {
        Floor detail = floorService.getOne(Condition.getQueryWrapper(floor));
        return FloorWrapper.build().entityVO(detail);
        }

	@Override
	@GetMapping("/getById")
	public Floor getById(@RequestParam Long id) {
		return this.floorService.getById(id);
	}

	/**
     * 查询
     */
    @Override
    @GetMapping("/list")
    public List<FloorVO> list(@SpringQueryMap Floor floor) {
        return FloorWrapper.build().listVO(floorService.list(Condition.getQueryWrapper(floor)));
        }



    /**
     * 新增
     */
    @Override
    @PostMapping("/save")
    public boolean save(@Valid @RequestBody Floor floor) {
        return floorService.save(floor);
        }

    /**
     * 修改
     */
    @Override
    @PostMapping("/update")
    public boolean update(@Valid @RequestBody Floor floor) {
        return floorService.updateById(floor);
        }

    /**
     * 新增或修改
     */
    @Override
    @PostMapping("/submit")
    public boolean 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 floorService.saveOrUpdateFloor(floor);
        }


    /**
     * 删除
     */
    @Override
    @PostMapping("/remove")
    public boolean remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return floorService.deleteLogic(Func.toLongList(ids));
        }


}
