package org.springblade.community.feign;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.Agency;
import org.springblade.community.service.IAgencyService;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.device.feign.IElevatorDeviceClient;
import org.springblade.estate.feign.ICompanyClient;
import org.springblade.system.expand.feign.IConfClient;
import org.springblade.system.expand.feign.IOrgPositionClient;
import org.springblade.system.expand.util.QueryWrapperUtil;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import org.springblade.community.entity.Residential;
import org.springblade.community.vo.ResidentialVO;
import org.springblade.community.wrapper.ResidentialWrapper;
import org.springblade.community.service.IResidentialService;
import org.springblade.core.boot.ctrl.BladeController;
import java.util.List;
import java.util.Map;

/**
 * 小区表，新建小区的时候，小区也要作为一个机构存入到机构表里面，机构的编号就使用当前小区的编号 client
 *
 * @author lianghanqiang
 * @since 2021-03-29
 */

@AllArgsConstructor
@RestController
@RequestMapping("/feign/residential")
public class ResidentialClient implements IResidentialClient {

    private final IResidentialService residentialService;
	private IOrgPositionClient orgPositionClient;
	private final ICompanyClient companyClient;
	private final IAgencyService agencyService;
	private final IElevatorDeviceClient elevatorDeviceClient;
	private final IConfClient confClient;


	/**
     * 详情
     */
    @Override
    @GetMapping("/detail")
    public ResidentialVO detail(@SpringQueryMap Residential residential) {
        Residential detail = residentialService.getOne(Condition.getQueryWrapper(residential));
        return ResidentialWrapper.build(companyClient, confClient).entityVO(detail);
        }

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

	/**
     * 查询 小区表，新建小区的时候，小区也要作为一个机构存入到机构表里面，机构的编号就使用当前小区的编号
     */
    @Override
    @GetMapping("/list")
    public List<ResidentialVO> list(@RequestParam Map<String,Object>  residential) {
		LambdaQueryWrapper<Residential> wrapper = new QueryWrapperUtil<Residential>().getBasicQueryWrapperByMap(
			Residential::getOrgPosition, residential, new Residential(), AuthUtil.getUser(), orgPositionClient
		);
		return ResidentialWrapper.build(companyClient, confClient).listVO(residentialService.list(wrapper));
        }



    /**
     * 新增 小区表，新建小区的时候，小区也要作为一个机构存入到机构表里面，机构的编号就使用当前小区的编号
     */
    @Override
    @PostMapping("/save")
    public boolean save(@Valid @RequestBody Residential residential) {
        return residentialService.save(residential);
        }

    /**
     * 修改 小区表，新建小区的时候，小区也要作为一个机构存入到机构表里面，机构的编号就使用当前小区的编号
     */
    @Override
    @PostMapping("/update")
    public boolean update(@Valid @RequestBody Residential residential) {
        return residentialService.updateById(residential);
        }

    /**
     * 新增或修改 小区表，新建小区的时候，小区也要作为一个机构存入到机构表里面，机构的编号就使用当前小区的编号
     */
    @Override
    @PostMapping("/submit")
    public boolean submit(@Valid @RequestBody Residential residential) {
		Assert.notNull(residential.getAgencyId(),"园区不能为空");
		Agency agency = agencyService.getById(residential.getAgencyId());
		Assert.notNull(agency,"小区对应的园区不存在");
		residential.setAgencyName(agency.getName());
		residential.setRegionProvince(agency.getRegionProvince());
		residential.setRegionCity(agency.getRegionCity());
		residential.setRegionArea(agency.getRegionArea());
		residential.setRegionStreet(agency.getRegionStreet());
		residential.setOrgPosition(agency.getOrgPosition());
		residential.setTenantId(agency.getTenantId());
		return residentialService.saveOrUpdateResidential(residential);
        }


    /**
     * 删除 小区表，新建小区的时候，小区也要作为一个机构存入到机构表里面，机构的编号就使用当前小区的编号
     */
    @Override
    @PostMapping("/remove")
    public boolean remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return residentialService.deleteLogic(Func.toLongList(ids));
        }

	@Override
	public List<Residential> listForOrg(Object[] params) {
		LambdaQueryWrapper<Residential> residentialLambdaQueryWrapper =
			new QueryWrapperUtil<Residential>()
				.getBasicQueryWrapper(
					Residential::getOrgPosition,
					BeanUtil.toBean((Map<String, Object>) params[0],Residential.class),
					BeanUtil.toBean((Map<String, Object>) params[1], BladeUser.class),
					orgPositionClient
				);
		return residentialService.getBaseMapper().selectList(residentialLambdaQueryWrapper);
	}

	@Override
	public List<Residential> getListWithYtelevatorXq(@RequestParam List<Long> residentialIds) {
		return residentialService.getListWithYtelevatorXq(residentialIds);
	}

	@Override
	@GetMapping("/getParkResidential")
	public List<Residential> getParkResidential() {
		return residentialService.getBaseMapper().selectList(new QueryWrapper<>(new Residential()).lambda().ne(Residential::getTenantId,"000000"));
	}

	@Override
	@GetMapping("/listWithOutCompany")
	public List<Residential> listWithOutCompany(@RequestParam Map<String, Object> residential) {
		LambdaQueryWrapper<Residential> wrapper = new QueryWrapperUtil<Residential>().getBasicQueryWrapperByMap(
			Residential::getOrgPosition, residential, new Residential(), AuthUtil.getUser(), orgPositionClient
		);
		return residentialService.list(wrapper);
	}

	@Override
	@GetMapping("/listByIds")
	public List<Residential> listByIds(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		List<Residential> residentialList = residentialService.listByIds(Func.toLongList(ids));
		return residentialList;
	}


}
