package cn.com.countrygarden.bi.main.controller.mediaResource;


import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.com.countrygarden.bi.main.configurer.ErpToBasicInfo;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.core.ResultGenerator;
import cn.com.countrygarden.bi.main.dto.BasicInfoDto;
import cn.com.countrygarden.bi.main.dto.mediaResource.AreaDto;
import cn.com.countrygarden.bi.main.model.mediaResource.BasicInfo;
import cn.com.countrygarden.bi.main.service.mediaResource.BasicInfoService;
import cn.com.countrygarden.bi.main.service.mediaResource.MediaCommonService;
import cn.com.countrygarden.bi.main.util.DateUtil;
import cn.com.countrygarden.bi.main.util.ExcelUtil;

@RestController
@RequestMapping("/api/media/basic")
public class BasicInfoController {

	@Autowired
	private BasicInfoService basicInfoService;
	
	@Autowired
	private ErpToBasicInfo erpToBasicInfo;
	
	@Autowired
	private MediaCommonService mediaCommonService;
	
	@PostMapping("/findBasicInfoPage")
	public Result findBasicInfoPage(HttpServletRequest request,BasicInfoDto dto) {
		try {
			String accessToken = request.getParameter("AccessToken");
			return ResultGenerator.genSuccessResult(basicInfoService.finBasicByPage(accessToken,dto));
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult("获取基础信息列表错误！");
		}
	}
	
	/**
	 * 物业区域下拉框
	 * @param dto
	 * @return
	 */
	@GetMapping("/propertyAreaData")
	public Result propertyAreaData(HttpServletRequest request,BasicInfoDto dto) {
		try {
			String accessToken = request.getParameter("AccessToken");
			return ResultGenerator.genSuccessResult(basicInfoService.propertyAreaData(accessToken,dto));
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult("获取基础信息物业区域错误！");
		}
	}
	
	@GetMapping("/propertyProjectData")
	public Result propertyProjectData(BasicInfoDto dto) {
		try {
			return ResultGenerator.genSuccessResult(basicInfoService.propertyProjectData(dto));
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult("获取基础信息物业项目错误！");
		}
	}
	
	
	@GetMapping("/provinceData")
	public Result provinceData(BasicInfoDto dto) {
		try {
			return ResultGenerator.genSuccessResult(basicInfoService.provinceData(dto));
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult("获取基础信息省份错误！");
		}
	}
	
	@GetMapping("/cityData")
	public Result cityData(BasicInfoDto dto) {
		try {
			return ResultGenerator.genSuccessResult(basicInfoService.cityData(dto));
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult("获取基础信息城市错误！");
		}
	}
	
	@GetMapping("/provinces")
	public Result provinces(AreaDto dto){
		try {
			List<Map<String, Object>> provinces = basicInfoService.provinces(dto);
			return ResultGenerator.genSuccessResult(provinces);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult("获取省市区错误！");
		}
	}
	
	
	@GetMapping("/findOne")
	public Result findOne(BasicInfo po) {
		try {
			if (null==po.getCommInfoId()) {
				return  ResultGenerator.genSuccessResult("基础信息标识为空");
			}
			return ResultGenerator.genSuccessResult(basicInfoService.findOne(po));
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult("获取单个基础信息错误！");
		}
	}
	
	@PostMapping("/saveOne")
	public Result saveOne(HttpServletRequest request,BasicInfoDto dto) {
		try {
			if (StringUtils.isEmpty(dto.getCommInfoId())) {
				return  ResultGenerator.genFailResult("基础信息标识为空");
			}
			String accessToken = request.getParameter("AccessToken");
			return basicInfoService.saveOne(accessToken,dto);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult(e.getMessage());
		}
	}
	
	@GetMapping("/export")
	public void export(HttpServletRequest request, HttpServletResponse response,BasicInfoDto dto) {
		try {
			String accessToken = request.getParameter("AccessToken");
			List<Map<String,Object>> findBasicList = basicInfoService.findBasic(accessToken,dto);
			String[] fields = {"organName","organCode","commName","commId","developUnitName","province",
					"city","county","actualProperty","plannedOpen","actualOpen","contractProperty","actualEnterTime","propertyName","developmentSubjectNames",
					"propertyForm","developmentState","takeoverState","stateProperty","totalResidentialArea","realityArea","takeRoomNumber","oftenNumber",
					"planNumber","takeNumber","occupancyRate","upCarPark","downCarPark","passengerElevatorNumber","freightElevatorNumber","apartment",
					"elevatorDoorNumber","elevatorRoomNumber","elevatorAmount","stairsRoomNumber","stairsDoorNumber","villaAmount"};
			String[] title = {"物业区域","区域编号","物业项目","项目编号","项目开发主体","省份","市/自治州","区/县","首次收楼日期","计划开盘时间",
					"实际开盘时间","合同收楼时间","实际进驻时间","项目营业执照的物业公司全称","开发主体(房产核算全称)","物业业态","项目开发状态","接管状态","收楼状态",
					"建筑面积(接管住宅建筑面积)","实际交付面积(已收楼住宅建筑面积)","收楼户数","常住户数","规划户数","接管户数","入住率","地上车位数","地下车位数",
					"载人电梯数量(部)","货物电梯数量(部)","公寓","电梯门洞数","电梯房数量(栋)","电梯总数(部)","步梯楼数量(栋)","步梯楼门洞数(个)","别墅数量(栋)"};
			String fileName="项目基础数据"+ DateUtil.simpleFormat(new Date());
			ExcelUtil.exportExcel(fileName, "sheet1", null, title, findBasicList, response, fields);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@PostMapping("/exportIn")
	public Result exportIn(HttpServletRequest request,Result rt,@RequestParam("file")MultipartFile file) {
		try {
			String accessToken = request.getParameter("AccessToken");
			return basicInfoService.exportIn(accessToken,rt,file);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult("导入社区媒体基础信息错误");
		}
	}
	
	@GetMapping("/regional")
	public Result regional(HttpServletRequest request) {
		try {
			String accessToken = request.getParameter("AccessToken");
			//basicInfoService.regional(accessToken);
			return ResultGenerator.genSuccessResult(mediaCommonService.findCompany(accessToken));
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult(e.getMessage());
		}
	}
	
	
	/*@Autowired
	private CommonService commonService;*/
	
	@GetMapping("/ldldk")
	public Result ldldk() {
		try {
			erpToBasicInfo.erpTobasicInfoTarget();
			//return ResultGenerator.genSuccessResult(commonService.getCommTree("00001"));
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult(e.getMessage());
		}
	}
	
	
}
