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.Residential;
import org.springblade.community.service.IResidentialService;
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.Building;
import org.springblade.community.vo.BuildingVO;
import org.springblade.community.wrapper.BuildingWrapper;
import org.springblade.community.service.IBuildingService;
import org.springblade.core.boot.ctrl.BladeController;
import java.util.List;
/**
 *  client
 *
 * @author lianghanqiang
 * @since 2021-03-29
 */

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

public class BuildingClient implements IBuildingClient {

    private final IBuildingService buildingService;
    private final IResidentialService residentialService;

        /**
     * 详情
     */
    @Override
    @GetMapping("/detail")
    public BuildingVO detail(@SpringQueryMap Building building) {
        Building detail = buildingService.getOne(Condition.getQueryWrapper(building));
        return BuildingWrapper.build().entityVO(detail);
        }

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

	/**
     * 查询
     */
    @Override
    @GetMapping("/list")
    public List<BuildingVO> list(@SpringQueryMap Building building) {
        return BuildingWrapper.build().listVO(buildingService.list(Condition.getQueryWrapper(building)));
        }



    /**
     * 新增
     */
    @Override
    @PostMapping("/save")
    public boolean save(@Valid @RequestBody Building building) {
        return buildingService.save(building);
        }

    /**
     * 修改
     */
    @Override
    @PostMapping("/update")
    public boolean update(@Valid @RequestBody Building building) {
        return buildingService.updateById(building);
        }

    /**
     * 新增或修改
     */
    @Override
    @PostMapping("/submit")
    public boolean 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());
		return buildingService.saveOrUpdateBuilding(building);
        }


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


}
