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.service.IBuildingService;
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.Unit;
import org.springblade.community.vo.UnitVO;
import org.springblade.community.wrapper.UnitWrapper;
import org.springblade.community.service.IUnitService;
import org.springblade.core.boot.ctrl.BladeController;
import java.util.List;
/**
 * 单元表 client
 *
 * @author lianghanqiang
 * @since 2021-03-29
 */

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

public class UnitClient implements IUnitClient {

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

        /**
     * 详情
     */
    @Override
    @GetMapping("/detail")
    public UnitVO detail(@SpringQueryMap Unit unit) {
        Unit detail = unitService.getOne(Condition.getQueryWrapper(unit));
        return UnitWrapper.build().entityVO(detail);
        }

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

	/**
     * 查询 单元表
     */
    @Override
    @GetMapping("/list")
    public List<UnitVO> list(@SpringQueryMap Unit unit) {
        return UnitWrapper.build().listVO(unitService.list(Condition.getQueryWrapper(unit)));
        }



    /**
     * 新增 单元表
     */
    @Override
    @PostMapping("/save")
    public boolean save(@Valid @RequestBody Unit unit) {
        return unitService.save(unit);
        }

    /**
     * 修改 单元表
     */
    @Override
    @PostMapping("/update")
    public boolean update(@Valid @RequestBody Unit unit) {
        return unitService.updateById(unit);
        }

    /**
     * 新增或修改 单元表
     */
    @Override
    @PostMapping("/submit")
    public boolean submit(@Valid @RequestBody Unit unit) {
		Residential residential = residentialService.getById(unit.getResidentialId());
		Assert.notNull(residential, "找不到对应小区！");
		Assert.notNull(unit.getBuildingId(),"单元所属楼栋不能为空");
		Building building = buildingService.getById(unit.getBuildingId());
		Assert.notNull(building,"单元对应的楼栋不存在");

		if (unit.getId() == null) {
			residential.setUnitCount(residential.getUnitCount() == null ? 1 : residential.getUnitCount() + 1);
			building.setUnitCount(building.getUnitCount() == null ? 1 : building.getUnitCount() + 1);
			if (unitService.save(unit)) {
				residentialService.updateById(residential);
				buildingService.updateById(building);
			}
		}
		unit.setAgencyId(building.getAgencyId());
		unit.setAgencyName(building.getAgencyName());
		unit.setResidentialId(building.getResidentialId());
		unit.setResidentialName(building.getResidentialName());
		unit.setBuildingName(building.getName());
		unit.setOrgPosition(building.getOrgPosition());
		return unitService.saveOrUpdateUnit(unit);
        }


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


}
