package org.springblade.community.feign;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.community.entity.*;
import org.springblade.community.service.*;
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.vo.RoomVO;
import org.springblade.community.wrapper.RoomWrapper;
import org.springblade.core.boot.ctrl.BladeController;
import java.util.List;
/**
 * 房间表 client
 *
 * @author lianghanqiang
 * @since 2021-03-29
 */

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

public class RoomClient implements IRoomClient {

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

        /**
     * 详情
     */
    @Override
    @GetMapping("/detail")
    public RoomVO detail(@SpringQueryMap Room room) {
        Room detail = roomService.getOne(Condition.getQueryWrapper(room));
        return RoomWrapper.build().entityVO(detail);
        }

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

	/**
     * 查询 房间表
     */
    @Override
    @GetMapping("/list")
    public List<RoomVO> list(@SpringQueryMap Room room) {
        return RoomWrapper.build().listVO(roomService.list(Condition.getQueryWrapper(room)));
        }



    /**
     * 新增 房间表
     */
    @Override
    @PostMapping("/save")
    public boolean save(@Valid @RequestBody Room room) {
        return roomService.save(room);
        }

    /**
     * 修改 房间表
     */
    @Override
    @PostMapping("/update")
    public boolean update(@Valid @RequestBody Room room) {
        return roomService.updateById(room);
        }

    /**
     * 新增或修改 房间表
     */
    @Override
    @PostMapping("/submit")
    public boolean 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 roomService.saveOrUpdateRoom(room);
        }


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

	@Override
	@PostMapping("/getListByRoomIds")
	public List<RoomVO> getListByRoomIds(List<Long> ids) {
		return RoomWrapper.build().listVO(roomService.getBaseMapper().selectList(new QueryWrapper<>(new Room()).lambda().in(Room::getId,ids)));
	}


}
