package cn.com.countrygarden.bi.main.configurer;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import cn.com.countrygarden.bi.main.model.mediaResource.BasicInfo;
import cn.com.countrygarden.bi.main.model.mediaResource.ErpBasicInfo;
import cn.com.countrygarden.bi.main.model.mediaResource.ErpPlanInfo;
import cn.com.countrygarden.bi.main.service.mediaResource.BasicInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;

@Slf4j
@Configuration
public class ErpToBasicInfo {
	
	@Autowired
	private BasicInfoService basicInfoService;
	//@Scheduled(cron = "0 0/1 * * * ?")// {x}分钟开始每分钟执行1次
	@Scheduled(cron = "0 0 23 1/1 * ?")// 23点执行一次
	public void erpTobasicInfoTarget() {
		log.info("========  ========");
			List<ErpBasicInfo> erpBasicInfoList = basicInfoService.findErpBasicInfo();
			List<BasicInfo> dbBasicList = basicInfoService.finddbBasicInfo();
			Map<String, ErpBasicInfo> erpMap = erpBasicInfoList.stream().collect(Collectors.toMap(ErpBasicInfo::getBasicsId,ErpBasicInfo->ErpBasicInfo));
			List<BasicInfo> updateList = new ArrayList<>();
			List<BasicInfo> insertList = new ArrayList<>();
			if (!dbBasicList.isEmpty()) {
				for (BasicInfo db : dbBasicList) {
					String dbasicsId = db.getBasicsId();
					if (erpMap.containsKey(db.getBasicsId())) {
						ErpBasicInfo erpBasic = erpMap.get(dbasicsId);
						updateList.add(assign(db,erpBasic));
					}
				}
				
				List<String> edbIds = erpBasicInfoList.stream().map(ErpBasicInfo::getBasicsId).collect(Collectors.toList());
				List<String> ddbIds = dbBasicList.stream().map(BasicInfo::getBasicsId).collect(Collectors.toList());
				edbIds.removeAll(ddbIds);
				if (!edbIds.isEmpty()) {
					for (String eid : edbIds) {
						ErpBasicInfo erpBasicInfo = erpMap.get(eid);
						insertList.add(assign(new BasicInfo(),erpBasicInfo));
					}
				}
				
			}else {
				for (ErpBasicInfo erpBasic : erpBasicInfoList) {
					insertList.add(assign(new BasicInfo(),erpBasic));
				}
			}
			
			if (!insertList.isEmpty()) {
				basicInfoService.saveBasicInfo(insertList);
			}
			
			if (!updateList.isEmpty()) {
				basicInfoService.updateBasicInfo(updateList);
			}
			
			log.info("========  ========");
	}
	
	private BasicInfo assign(BasicInfo po,ErpBasicInfo epo) {
		int planNumber = 0;
		po.setBasicsId(epo.getBasicsId());
		po.setCommId(epo.getCommId());
		po.setCommName(epo.getPropertyProject());
		po.setOrganCode(epo.getOrganCode());
		po.setOrganName(epo.getPropertyArea());
		po.setDevelopUnitName(epo.getDevelopmentSubject());
		po.setCommAddress(epo.getRegion());
		po.setProvince(epo.getProvince());
		po.setCity(epo.getCity());
		po.setCounty(epo.getCounty());
		po.setActualProperty(epo.getActualProperty());
		po.setOccupancyRate(epo.getOccRate());
		//12-27
		System.out.println(epo.getPropertyName());
		po.setPropertyName(epo.getPropertyName());
		po.setDevelopmentSubjectNames(epo.getDevelopmentSubjectNames());
		po.setPropertyForm(epo.getPropertyForm());
		po.setDevelopmentState(epo.getDevelopmentState());
		po.setTakeoverState(epo.getTakeoverState());
		po.setActualEnterTime(epo.getActualEnterTime());
		po.setStateProperty(epo.getStateProperty());
		po.setTotalResidentialArea(epo.getTotalResidentialArea());
		po.setPlannedOpen(epo.getPlannedOpen());
		po.setActualOpen(epo.getActualOpen());
		po.setContractProperty(epo.getContractProperty());
		//12-27
		
		po.setPlanNumber(0);
		po.setTakeNumber(0);
		po.setVersion(po.getVersion()+1);
		List<ErpPlanInfo> planInfos = epo.getPlanInfos();
		if (!planInfos.isEmpty()) {
			for (ErpPlanInfo planNumbers : planInfos) {
				planNumber+=planNumbers.getPlanNumbers();
			}
			po.setPlanNumber(planNumber);
			BigDecimal takeNumber = new BigDecimal(planNumber);
			takeNumber = takeNumber.multiply(po.getOccupancyRate());
			po.setTakeNumber(takeNumber.intValue());
		}
		return po;
	}

}
