package org.springblade.app.controller;

import lombok.AllArgsConstructor;
import org.springblade.community.entity.Residential;
import org.springblade.community.feign.IResidentialClient;
import org.springblade.core.mp.support.BladePage;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.enterprise.entity.Staff;
import org.springblade.enterprise.feign.IStaffClient;
import org.springblade.enterprise.vo.StaffVO;
import org.springblade.estate.dto.CarDTO;
import org.springblade.estate.feign.IApplyUserClient;
import org.springblade.estate.feign.ICarClient;
import org.springblade.estate.vo.CarVO;
import org.springblade.person.entity.Member;
import org.springblade.person.feign.IHouseUserClient;
import org.springblade.person.feign.IMemberClient;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * Created By lidexi in 2021/4/30
 **/
@RestController
@AllArgsConstructor
@RequestMapping("/app/car")
public class CarController {

	private final ICarClient carClient;
	private final IResidentialClient residentialClient;
	private final IStaffClient staffClient;
	private final IMemberClient memberClient;

	@PostMapping(value = "/saveOrUpdate")
	public Map<String,Object> saveOrUpdate(@RequestBody CarDTO carDto){
		Map<String,Object> map = new HashMap<>();
		Residential searchResidential = new Residential();
		searchResidential.setId(carDto.getResidentialId());
		Residential residential = this.residentialClient.detail(searchResidential);
		Assert.notNull(residential,"小区不存在");
		carDto.setAgencyId(residential.getAgencyId());
		carDto.setTenantId(residential.getTenantId());
		if(Objects.equals(carDto.getTenantType(),1)){
			//园区
			Staff searchEnterpriseStaff = new Staff();
			searchEnterpriseStaff.setId(carDto.getPersonId());
			StaffVO enterpriseStaffVO = staffClient.detail(searchEnterpriseStaff);
			Assert.notNull(enterpriseStaffVO,"找不到对应的员工");
			carDto.setPersonName(enterpriseStaffVO.getName());
			carDto.setPersonPhone(enterpriseStaffVO.getPhone());
			carDto.setOrgPosition(enterpriseStaffVO.getOrgPosition());
		}
		carDto.setAuditStatus(0);//新增或者重新编辑都需要重新审核

		if(carDto.getId() == null){//新增
			boolean save = this.carClient.save(carDto);
			map.put("result",save);
		}else{//修改
//            carDto.set
			boolean update= this.carClient.update(carDto);
			map.put("result",update);
		}

		return map;
	}


	@GetMapping(value = "/page/member")
	public R<BladePage<CarVO>> getMemberCarPage(@RequestParam(value = "residentialId") Long residentialId, @RequestParam(value = "memberId") Long memberId, Query query){
		Member member = memberClient.getById(memberId);
		Assert.notNull(member, "会员不存在");
		CarDTO carDTO = new CarDTO();
		carDTO.setQuery(query);
		carDTO.setMemberId(memberId);
		carDTO.setResidentialId(residentialId);
		carDTO.setUserIdCard(member.getIdcard());
		return carClient.page(carDTO);
	}

	@GetMapping(value = "/page/admin")
	public R<BladePage<CarVO>> getAdminCarPage(@RequestParam(value = "residentialId") Long residentialId, Query query){
		CarDTO carDTO = new CarDTO();
		carDTO.setQuery(query);
		carDTO.setResidentialId(residentialId);
		return carClient.page(carDTO);
	}
}
