package org.springblade.app.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springblade.common.constant.OperationConstant;
import org.springblade.community.entity.Residential;
import org.springblade.community.feign.IResidentialClient;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.StringUtil;
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.entity.ApplyUser;
import org.springblade.estate.entity.Car;
import org.springblade.estate.feign.IApplyUserClient;
import org.springblade.estate.feign.ICarClient;
import org.springblade.estate.vo.ApplyUserVO;
import org.springblade.person.entity.HouseUser;
import org.springblade.person.feign.IHouseUserClient;
import org.springblade.person.vo.HouseUserVO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

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

	private final ICarClient carClient;
	private final IHouseUserClient houseUserClient;
	private final IResidentialClient residentialClient;
	private final IStaffClient staffClient;
	private final IApplyUserClient applyUserClient;

	@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("000000".equals(residential.getTenantId())){//园区住户添加车辆
			Assert.notNull(carDto.getIdCard(),"身份证必传");
			ApplyUser applyUser = new ApplyUser();
			applyUser.setMemberId(carDto.getMemberId());
			applyUser.setName(carDto.getPersonName());
			applyUser.setIdCard(carDto.getIdCard());
			applyUser.setAuditStatus(OperationConstant.ONE);
			applyUser.setPhone(carDto.getPersonPhone());
			ApplyUserVO applyUserVO = applyUserClient.detail(applyUser);
			Assert.notNull(applyUserVO,"当前您未完成住户认证");
			Long userId = applyUserVO.getUserId();
			HouseUser searchUser = new HouseUser();
			searchUser.setId(userId);
			HouseUserVO houseUserVO = houseUserClient.detail(searchUser);
			Assert.notNull(houseUserVO,"找不到对应的住户");
			carDto.setOrgPosition(houseUserVO.getOrgPosition());
		}else{//园区企业员工添加车辆
			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;
	}


	@PostMapping(value = "/page")
	@SneakyThrows
	public Map<String,Object> getPage(@RequestBody CarDTO carDto){
		Map<String,Object> map = new HashMap<>();
		R<Page<Car>> page = this.carClient.page(carDto);
		if(page != null){
			map.put("carPage",page.getData());
		}
		return map;
	}
}
