package cn.com.countrygarden.bi.main.service.bigholderAduit.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.countrygarden.bi.main.dao.bigholderAduit.BigholderAduitMapper;
import cn.com.countrygarden.bi.main.dao.bigholderAduit.BigholderExceptionInfoMapper;
import cn.com.countrygarden.bi.main.dao.bigholderAduit.BigholderPushPerdataMapper;
import cn.com.countrygarden.bi.main.dao.bigholderSatisfy.BigholderSatisfyMapper1;
import cn.com.countrygarden.bi.main.dao.twerp.hm.TbHsprCommunityMapper;
import cn.com.countrygarden.bi.main.dao.twerp.hm.TbSysOrganMapper;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.model.BigholderSatisfacationInfo;
import cn.com.countrygarden.bi.main.model.bigholderAduit.BigholderAreaAudit;
import cn.com.countrygarden.bi.main.model.bigholderAduit.BigholderExceptionInfo;
import cn.com.countrygarden.bi.main.model.bigholderAduit.BigholderPushPerdata;
import cn.com.countrygarden.bi.main.model.bigholderAduit.eunm.AuditStatus;
import cn.com.countrygarden.bi.main.model.bigholderAduit.eunm.PustThismonthPerdata;
import cn.com.countrygarden.bi.main.model.twerp.ErpCommunity;
import cn.com.countrygarden.bi.main.model.twerp.hm.TbSysOrgan;
import cn.com.countrygarden.bi.main.service.bigholderAduit.BigholderPushPerdataService;

@Service
@Transactional
public class BigholderPushPerdataServiceImpl implements BigholderPushPerdataService {

	@Autowired
	BigholderExceptionInfoMapper bigholderExceptionInfoMapper;

	@Autowired
	BigholderAduitMapper bigholderAduitMapper;

	@Autowired
	BigholderPushPerdataMapper bigholderPushPerdataMapper;

	@Autowired
	BigholderSatisfyMapper1 bigholderSatisfyMapper1;
	
	@Autowired
	TbSysOrganMapper tbSysOrganMapper;
	
	@Autowired
	TbHsprCommunityMapper tbHsprCommunityMapper;

	@Override
	public String pushStatus(String yearMonth){
		List<BigholderPushPerdata> bigholderPushPerdatas = bigholderPushPerdataMapper.getAll(yearMonth,
				PustThismonthPerdata.PUST);
		if(bigholderPushPerdatas.size()>0){
			return "0";
		}else{
			return "1";
		}
	}
	
	@Override
	public List<Map<String,Object>> insertBigholderPushPerdata(String yearMonth, String area, String tbUserId,
			String exceptionInfoAuditStruts) {
		List<Map<String,Object>> list1=new ArrayList<>();
		List<BigholderExceptionInfo> bigholderExceptionInfos = bigholderExceptionInfoMapper
				.getBigholderExceptionInfo(yearMonth, area, exceptionInfoAuditStruts);
		if (bigholderExceptionInfos.size() > 0) {
			Map<String,Object> map=new HashMap<>();
			map.put("pushErrorInfo", "您还有异常数据没有处理，推送不成功");
			list1.add(map);
			return list1;
		}
		
		List<BigholderSatisfacationInfo> bigholderSatisfacationInfos2=bigholderSatisfyMapper1.findAll(yearMonth);
		Set<String> arSet=new HashSet<>();
		for (BigholderSatisfacationInfo bigholderSatisfacationInfo : bigholderSatisfacationInfos2) {
			if(AuditStatus.NOAUDIT.equals(bigholderSatisfacationInfo.getAuditStatus())
					||bigholderSatisfacationInfo.getAuditStatus().length()==0
					||AuditStatus.REJECTED.equals(bigholderSatisfacationInfo.getAuditStatus())
					){
				TbSysOrgan tbSysOrgan=tbSysOrganMapper.findByOrganName(bigholderSatisfacationInfo.getEstateArea());
				if(tbSysOrgan==null){
					Map<String,Object> map=new HashMap<>();
					map.put("pushErrorInfo", "【"+bigholderSatisfacationInfo.getEstateArea()+"】不存在，请在权限控制中修改正确的区域");
					list1.add(map);
				}else{
					arSet.add(bigholderSatisfacationInfo.getEstateArea());
				}
			}
		}
		
		List<BigholderAreaAudit> bigholderAreaAudits=bigholderAduitMapper.getBigholderAreaAuditAll(yearMonth);
		for (BigholderAreaAudit bigholderAreaAudit : bigholderAreaAudits) {
			if(AuditStatus.WAITAUDIT.equals(bigholderAreaAudit.getAuditStatus())){
				Map<String,Object> map=new HashMap<>();
				List<ErpCommunity> erpCommunity=tbHsprCommunityMapper.findBaseCommByCommId(bigholderAreaAudit.getCorrespondProject());
				String corrPro=erpCommunity.size()>0?erpCommunity.get(0).getCommName():"";
				map.put("pushErrorInfo", "【"+bigholderAreaAudit.getArea()+"】"+"的对应项目名称为【"+corrPro+"】还没有审核通过");
				list1.add(map);
			}
		}
		
		for (String area1 : arSet) {
			List<BigholderSatisfacationInfo> bigholderSatisfacationInfos=bigholderSatisfyMapper1.getSelectByMonAndArea(yearMonth, area1);
			Set<String> projectNameSet = new HashSet<>();
			for (BigholderSatisfacationInfo bigholderSatisfacationInfo : bigholderSatisfacationInfos) {
				projectNameSet.add(bigholderSatisfacationInfo.getProjectName());
			}
			for (String proName : projectNameSet) {
				Map<String,Object> map=new HashMap<>();
				map.put("pushErrorInfo", "【"+area1+"】"+"的原始项目名称为【"+proName+"】还没有审核通过");
				list1.add(map);
			}
		}
		if(list1.size()>0){
			return list1;
		}
		
		int version = 0;
		List<BigholderPushPerdata> bigholderPushPerdatas = bigholderPushPerdataMapper.getAll(yearMonth,
				PustThismonthPerdata.PUST);
		if (bigholderPushPerdatas.size() > 0) {
			Map<String,Object> map=new HashMap<>();
			map.put("pushErrorInfo", "您已经推送数据了，不能再次推送");
			list1.add(map);
			return list1;
		}

		if (bigholderPushPerdatas.size() > 0) {
			version = bigholderPushPerdatas.get(0).getVersion();
		}

		List<BigholderAreaAudit> bigholderAreaAudits2 = bigholderAduitMapper.getBigholderAreaAduits(yearMonth, area,
				AuditStatus.PASS);
		List<BigholderPushPerdata> list = new ArrayList<>();
		for (BigholderAreaAudit bigholderAreaAudit : bigholderAreaAudits2) {
			if (!PustThismonthPerdata.PUST.equals(bigholderAreaAudit.getPushPerdataStruts())) {
				bigholderAreaAudit.setPushPerdataStruts(PustThismonthPerdata.PUST);

				List<BigholderSatisfacationInfo> bigholderSatisfacationInfos = bigholderSatisfyMapper1
						.getSfInfoByAreaOProject(bigholderAreaAudit.getArea(),
								bigholderAreaAudit.getCorrespondProject(), bigholderAreaAudit.getYearMonth());
				for (BigholderSatisfacationInfo bigholderSatisfacationInfo : bigholderSatisfacationInfos) {
					bigholderSatisfacationInfo.setPushPerdataStruts(PustThismonthPerdata.PUST);
					bigholderAduitMapper.updateBigholderAduit(bigholderAreaAudit);
					bigholderSatisfyMapper1.updateAuditIsPush(bigholderSatisfacationInfo);
				}

				BigholderPushPerdata bigholderPushPerdata = new BigholderPushPerdata();
				bigholderPushPerdata.setArea(bigholderAreaAudit.getArea());
				bigholderPushPerdata.setYearMon(yearMonth);
				bigholderPushPerdata.setPushStatus(PustThismonthPerdata.PUST);
				bigholderPushPerdata.setCorrespondProject(bigholderAreaAudit.getCorrespondProject());
				bigholderPushPerdata.setProjectTotal(bigholderAreaAudit.getProjectTotal());
				bigholderPushPerdata.setMarketingTotal(bigholderAreaAudit.getMarketingTotal());
				bigholderPushPerdata.setAreaAduitId(bigholderAreaAudit.getId() + "");
				bigholderPushPerdata.setOpenSatisfaction(bigholderAreaAudit.getOpenSatisfaction());
				bigholderPushPerdata.setSatisfactionScores(bigholderAreaAudit.getSatisfactionScores());
				bigholderPushPerdata.setVersion(version + 1);
				bigholderPushPerdata.setA41(bigholderAreaAudit.getA41());
				bigholderPushPerdata.setA42(bigholderAreaAudit.getA42());
				bigholderPushPerdata.setA43(bigholderAreaAudit.getA43());
				bigholderPushPerdata.setA44(bigholderAreaAudit.getA44());
				bigholderPushPerdata.setA51(bigholderAreaAudit.getA51());
				bigholderPushPerdata.setA52(bigholderAreaAudit.getA52());
				bigholderPushPerdata.setA53(bigholderAreaAudit.getA53());
				bigholderPushPerdata.setA54(bigholderAreaAudit.getA54());
				bigholderPushPerdata.setA61(bigholderAreaAudit.getA61());
				bigholderPushPerdata.setA62(bigholderAreaAudit.getA62());
				bigholderPushPerdata.setA71(bigholderAreaAudit.getA71());
				bigholderPushPerdata.setA72(bigholderAreaAudit.getA72());
				bigholderPushPerdata.setA73(bigholderAreaAudit.getA73());
				list.add(bigholderPushPerdata);
			}
		}
		if(list.size()>0){
			bigholderPushPerdataMapper.insertBigholderPushPerdata(list);
		}
		return list1;
	}
}