/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.community.controller;

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.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.community.entity.Agency;
import org.springblade.community.entity.Room;
import org.springblade.community.service.IAgencyService;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
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.springblade.third.doordevice.feign.IDoorDeviceButtClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
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;

/**
 * 小区表
 *
 * @author BladeX
 * @since 2021-03-29
 */
@RestController
@AllArgsConstructor
@RequestMapping("/residential")
@Api(value = "小区表", tags = "小区表")
public class ResidentialController extends BladeController {

	private final IResidentialService residentialService;
	private final IAgencyService agencyService;
	private final IOrgPositionClient orgPositionClient;
	private final ICompanyClient companyClient;
	private final IDoorDeviceButtClient doorDeviceButtClient;
	private final IConfClient confClient;

	/**
	 * 查看详情，返回基本信息加所属物业名称和物业电话
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入residential")
	public R<ResidentialVO> detail(Residential residential) {
		Residential detail = residentialService.getOne(Condition.getQueryWrapper(residential));
		return R.data(ResidentialWrapper.build(companyClient, confClient).entityVO(detail));
	}

	/**
	 *
	 */
	@GetMapping("/findPage")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "sql分页查询", notes = "传入residential")
	public R<List<ResidentialVO>> findPage(Long current,Long size,String agencyId,String name) {
		List<ResidentialVO> page = residentialService.findPage(current-1, size, agencyId, name);
		return R.data(page);
	}

	/**
	 * 分页 小区表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入residential")
	public R<IPage<ResidentialVO>> list(@RequestParam Map<String,Object> residential, Query query, BladeUser bladeUser) {
		QueryWrapper<Residential> queryWrapper = Condition.getQueryWrapper(residential, Residential.class);
		IPage<Residential> pages = residentialService.page(Condition.getPage(query), queryWrapper);
		return R.data(ResidentialWrapper.build(companyClient, confClient).pageVO(pages));
	}


	/**
	 * 自定义分页 小区表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入residential")
	public R<IPage<ResidentialVO>> page(ResidentialVO residential, Query query) {
		IPage<ResidentialVO> pages = residentialService.selectResidentialPage(Condition.getPage(query), residential);
		return R.data(pages);
	}

	/**
	 * 新增 小区表，
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入residential")
	public R save(@Valid @RequestBody Residential residential) {
		return R.status(residentialService.save(residential));
	}

	/**
	 * 修改 小区表，
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入residential")
	public R update(@Valid @RequestBody Residential residential) {
		return R.status(residentialService.updateById(residential));
	}

	/**
	 * 新增或修改 小区表，新建小区的时候，小区也要作为一个机构存入到机构表里面，机构的编号就使用当前小区的编号
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入residential")
	public R 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());
		boolean isNewResidential = residential.getId() == null? true : false;
		residential.setPic(confClient.removePrefix(residential.getPic()));
		boolean saveOrUpdate = residentialService.saveOrUpdateResidential(residential);
		if(saveOrUpdate && isNewResidential){ //注册小区给第三方对接系统
			R result = doorDeviceButtClient.addResidential(residential);
			residential.setThirdResidentialId(Func.toStr(result.getData()));
			residentialService.saveOrUpdateResidential(residential);
		}
		return R.status(true);
	}


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

	/**
	 * 下拉列表
	 */
	@GetMapping("/select")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "下拉列表", notes = "传入residential")
	public R<List<Residential>> select(@RequestParam Map<String,Object> residential,Integer size, BladeUser bladeUser) {
		LambdaQueryWrapper<Residential> queryWrapper = Condition.getQueryWrapper(residential, Residential.class).lambda();
		queryWrapper.last(size != null, "limit " + size);
		queryWrapper.orderByDesc(Residential::getCreateTime);
		return R.data(residentialService.list(queryWrapper));
	}
}
