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

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.smartframework.common.ComTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageRowBounds;

import cn.com.countrygarden.bi.main.dao.BigholderRightManagerMapper;
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.dup.CaseFieldMapper;
import cn.com.countrygarden.bi.main.dao.sys.RoleMapper;
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.dao.twerp.hm.TbSysUserMapper;
import cn.com.countrygarden.bi.main.exception.AuthorizeException;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.BigholderRightManager;
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.ExceptionStruts;
import cn.com.countrygarden.bi.main.model.bigholderAduit.eunm.PustThismonthPerdata;
import cn.com.countrygarden.bi.main.model.dup.CaseField;
import cn.com.countrygarden.bi.main.model.sys.Role;
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.model.twerp.hm.TbSysUser;
import cn.com.countrygarden.bi.main.service.AccessTokenService;
import cn.com.countrygarden.bi.main.service.bigholderAduit.BigholderAduitService;
import cn.com.countrygarden.bi.main.service.bigholderSatisfy.BigholderSatisfyService1;

@Service
@Transactional
public class BigholderAduitServiceImpl implements BigholderAduitService {

	public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Autowired
	private CaseFieldMapper caseFieldMapper;

	@Autowired
	private TbSysUserMapper tbSysUserMapper;

	@Autowired
	AccessTokenService accessTokenService;

	@Autowired
	BigholderAduitMapper bigholderAduitMapper;

	@Autowired
	BigholderSatisfyMapper1 bigholderSatisfyMapper1;

	@Autowired
	TbHsprCommunityMapper tbHsprCommunityMapper;

	@Autowired
	TbSysOrganMapper tbSysOrganMapper;

	@Autowired
	BigholderRightManagerMapper bigholderRightManagerMapper;

	@Autowired
	BigholderSatisfyService1 bigholderSatisfyService1;

	@Autowired
	RoleMapper roleMapper;

	@Autowired
	BigholderExceptionInfoMapper bigholderExceptionInfoMapper;

	@Autowired
	BigholderPushPerdataMapper bigholderPushPerdataMapper;

	/**
	 * 大业主总部负责人
	 */
	public static final String BIGHEADQUATERS = "dyz_admin";

	/**
	 * 大业主区域负责人
	 */
	public static final String BIGHEADAREA = "dyz_ordinary";

	public List<String> getHandRight(String area, String tbUserCode) {
		List<String> list = new ArrayList<>();
		List<BigholderRightManager> bigholderRightManagers = bigholderRightManagerMapper
				.getRightManagerByHandmanId(tbUserCode);

		for (BigholderRightManager bigholderRightManager : bigholderRightManagers) {
			if (area != null) {
				if (bigholderRightManager.getArea().equals(area)) {
					list.add(area);
				}
			} else {
				list.add(bigholderRightManager.getArea());
			}
		}
		return list;
	}

	@Override
	public TbSysUser getTbSysUser(String accessToken) {

		if (accessToken == null || "null".equals(accessToken) || "".equals(accessToken)) {
			throw new AuthorizeException("缺少身份令牌");
		}

		AccessToken userAccessToken = accessTokenService.findByToken(accessToken);
		if (userAccessToken == null) {
			throw new AuthorizeException("无效的身份令牌");
		}

		TbSysUser tbSysUser = findByUserCode(userAccessToken.getUserCode());

		return tbSysUser;
	}

	public TbSysUser findByUserCode(String userCode) {
		TbSysUser tbSysUser = tbSysUserMapper.findByUserCode(userCode);
		if (tbSysUser == null) {
			System.out.println(userCode);
			throw new ServiceException("用户不存在，或账户、密码错误。");
		}
		return tbSysUser;
	}

	// @Override
	// public List<BigholderAreaAudit> getBigholderAreaAuditBySId(String
	// satisfactionInfoId) {
	//
	// return
	// bigholderAduitMapper.getBigholderAreaAuditBySId(satisfactionInfoId);
	// }

	@Override
	public List<BigholderAreaAudit> getBigholderAreaAuditAll(String yearMonth) {
		List<BigholderAreaAudit> bigholderAreaAudits = bigholderAduitMapper.getBigholderAreaAuditAll(yearMonth);
		for (BigholderAreaAudit bigholderAreaAudit : bigholderAreaAudits) {
			// if(bigholderAreaAudit)
		}
		return null;
	}

	@Override
	public Boolean getBigholderAreaAduits(String yearMonth, String area, String auditStatus) {
		List<BigholderAreaAudit> bigholderAreaAudits = bigholderAduitMapper.getBigholderAreaAduits(yearMonth, area,
				auditStatus);
		if (bigholderAreaAudits.size() > 0) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public void passAduits(String ids[], String userCode, String yearMonth, String area, String auditStatus) {
		for (String id : ids) {
			BigholderAreaAudit bigholderAreaAudit = bigholderAduitMapper.selectById(id);
			this.getCommPass(bigholderAreaAudit, userCode);
		}
	}

	public void getCommPass(BigholderAreaAudit bigholderAreaAudit, String userCode) {
		List<BigholderSatisfacationInfo> bigholderSatisfacationInfos = bigholderSatisfyMapper1.getSfInfoByAreaOProject(
				bigholderAreaAudit.getArea(), bigholderAreaAudit.getCorrespondProject(),
				bigholderAreaAudit.getYearMonth());
		List<BigholderAreaAudit> bigholderAreaAudits3 = bigholderAduitMapper.getByAreaAduitsCommId(bigholderAreaAudit.getYearMonth(),
				bigholderAreaAudit.getArea(), bigholderAreaAudit.getCorrespondProject(), AuditStatus.PASS);
		List<ErpCommunity> erpCommunitys=tbHsprCommunityMapper.findBaseCommByCommId(bigholderAreaAudit.getCorrespondProject());
		List<BigholderAreaAudit> list = new ArrayList<>();
		TbSysOrgan tbSysOrgan = tbSysOrganMapper.findByOrganName(bigholderAreaAudit.getArea());
		if(bigholderAreaAudits3.size()>0){
			for (BigholderAreaAudit bigholderAreaAudit2 : bigholderAreaAudits3) {
				bigholderAduitMapper.deleteBigholderAduit(bigholderAreaAudit2.getId() + "");
			}
			list=this.saveAuditData(erpCommunitys.get(0), tbSysOrgan, bigholderAreaAudit.getYearMonth(),
					userCode, true, list, bigholderSatisfacationInfos,AuditStatus.PASS);
			if (list.size() > 0) {
				bigholderAduitMapper.insertBigholderAduit(list);
			}
			bigholderAduitMapper.deleteBigholderAduit(bigholderAreaAudit.getId()+"");
		}else{
			if (bigholderSatisfacationInfos.size() > 0) {
				for (BigholderSatisfacationInfo bigholderSatisfacationInfo : bigholderSatisfacationInfos) {
					bigholderSatisfacationInfo.setAuditStatus(AuditStatus.PASS);
					bigholderSatisfacationInfo.setLastUpdateDate(simpleDateFormat.format(new Date()));
					bigholderSatisfacationInfo.setLastUpdateBy(userCode);
					bigholderSatisfyMapper1.updateAuditStatus(bigholderSatisfacationInfo);
				}
			}
			bigholderAreaAudit.setAuditStatus(AuditStatus.PASS);
			bigholderAreaAudit.setLastUpdateBy(userCode);
			bigholderAreaAudit.setLastUpdateTime(new Date());
			bigholderAduitMapper.updateBigholderAduit(bigholderAreaAudit);
		}
	}

	@Override
	public List<Map<String, Object>> getBigholderAreaAduits2(String yearMonth, String area,
			String pass) {
		List<BigholderAreaAudit> bigholderAreaAudits = bigholderAduitMapper.getBigholderAreaAduits1(
				yearMonth, area, pass);
		List<Map<String, Object>> list = new ArrayList<>();
		for (BigholderAreaAudit bigholderAreaAudit : bigholderAreaAudits) {
			String a = bigholderAreaAudit.getExceptionStatus().length() == 0 ? ""
					: bigholderAreaAudit.getExceptionStatus();
			if (!a.equals(ExceptionStruts.EXCEPT)) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", bigholderAreaAudit.getId());
				map.put("area", bigholderAreaAudit.getArea());
				List<ErpCommunity> erpCommunitys = tbHsprCommunityMapper.findBaseCommByCommIdOrArea(
						bigholderAreaAudit.getCorrespondProject(), bigholderAreaAudit.getArea());
				map.put("correspondProject", erpCommunitys.size() > 0 ? erpCommunitys.get(0).getCommName() : "");
				map.put("projectTotal", bigholderAreaAudit.getProjectTotal());
				map.put("marketingTotal", bigholderAreaAudit.getMarketingTotal());
				map.put("auditStatus", bigholderAreaAudit.getAuditStatus().equals(AuditStatus.WAITAUDIT) ? "待提交"
						: bigholderAreaAudit.getAuditStatus().equals(AuditStatus.PASS) ? "已审核"
								: bigholderAreaAudit.getAuditStatus().equals(AuditStatus.REJECTED) ? "已审核" : "");
				map.put("openSatisfaction", bigholderAreaAudit.getOpenSatisfaction());
				map.put("satisfactionScores", bigholderAreaAudit.getSatisfactionScores() == null ? ""
						: ComTool.accuracy(bigholderAreaAudit.getSatisfactionScores(), 1));
				map.put("a41", bigholderAreaAudit.getA41());
				map.put("a42", bigholderAreaAudit.getA42());
				map.put("a43", bigholderAreaAudit.getA43());
				map.put("a44", bigholderAreaAudit.getA44());
				map.put("a51", bigholderAreaAudit.getA51());
				map.put("a52", bigholderAreaAudit.getA52());
				map.put("a53", bigholderAreaAudit.getA53());
				map.put("a54", bigholderAreaAudit.getA54());
				map.put("a61", bigholderAreaAudit.getA61());
				map.put("a62", bigholderAreaAudit.getA62());
				map.put("a71", bigholderAreaAudit.getA71());
				map.put("a72", bigholderAreaAudit.getA72());
				map.put("a73", bigholderAreaAudit.getA73());
				list.add(map);
			}
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> getBigholderAreaAduits1(String yearMonth, String area,
			String auditStatus) {
		List<BigholderAreaAudit> bigholderAreaAudits = bigholderAduitMapper.getBigholderAreaAduits1(
				yearMonth, area, auditStatus);
		List<Map<String, Object>> list = new ArrayList<>();
		for (BigholderAreaAudit bigholderAreaAudit : bigholderAreaAudits) {
			String a = bigholderAreaAudit.getExceptionStatus().length() == 0 ? ""
					: bigholderAreaAudit.getExceptionStatus();
			if (!a.equals(ExceptionStruts.EXCEPT)) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", bigholderAreaAudit.getId());
				map.put("area", bigholderAreaAudit.getArea());
				List<ErpCommunity> erpCommunitys = tbHsprCommunityMapper.findBaseCommByCommIdOrArea(
						bigholderAreaAudit.getCorrespondProject(), bigholderAreaAudit.getArea());
				map.put("correspondProject", erpCommunitys.size() > 0 ? erpCommunitys.get(0).getCommName() : "");
				map.put("projectTotal", bigholderAreaAudit.getProjectTotal());
				map.put("marketingTotal", bigholderAreaAudit.getMarketingTotal());
				map.put("auditStatus", bigholderAreaAudit.getAuditStatus().equals(AuditStatus.WAITAUDIT) ? "待审核"
						: bigholderAreaAudit.getAuditStatus().equals(AuditStatus.PASS) ? "已审核"
								: bigholderAreaAudit.getAuditStatus().equals(AuditStatus.REJECTED) ? "已审核" : "");
				map.put("openSatisfaction", bigholderAreaAudit.getOpenSatisfaction());
				map.put("satisfactionScores", bigholderAreaAudit.getSatisfactionScores() == null ? ""
						: ComTool.accuracy(bigholderAreaAudit.getSatisfactionScores(), 1));
				map.put("a41", bigholderAreaAudit.getA41());
				map.put("a42", bigholderAreaAudit.getA42());
				map.put("a43", bigholderAreaAudit.getA43());
				map.put("a44", bigholderAreaAudit.getA44());
				map.put("a51", bigholderAreaAudit.getA51());
				map.put("a52", bigholderAreaAudit.getA52());
				map.put("a53", bigholderAreaAudit.getA53());
				map.put("a54", bigholderAreaAudit.getA54());
				map.put("a61", bigholderAreaAudit.getA61());
				map.put("a62", bigholderAreaAudit.getA62());
				map.put("a71", bigholderAreaAudit.getA71());
				map.put("a72", bigholderAreaAudit.getA72());
				map.put("a73", bigholderAreaAudit.getA73());
				list.add(map);
			}
		}
		return list;
	}

	/**
	 * 该负责人的平均满意度
	 */
	public String getAvgSatisfactionScores(String yearMonth, String tbUserCode) {
		List<String> list = new ArrayList<>();

		List<BigholderRightManager> bigholderRightManagers = bigholderRightManagerMapper
				.getRightManagerByHandmanId(tbUserCode);
		for (BigholderRightManager bigholderRightManager : bigholderRightManagers) {
			list.add(bigholderRightManager.getArea());
		}
		List<String> a = new ArrayList<>();
		a.add(yearMonth);

		Double satisfactionScores = 0.0;
		int count = 0;
		for (String area : list) {
			Set<String> arSet = new HashSet<>();
			List<BigholderSatisfacationInfo> bigholderSatisfacationInfos = bigholderSatisfyMapper1
					.getSelectByMonAndArea(yearMonth, area);
			for (BigholderSatisfacationInfo bigholderSatisfacationInfo : bigholderSatisfacationInfos) {
				arSet.add(bigholderSatisfacationInfo.getCorrespondProject());
			}
			for (String ar : arSet) {
				List<BigholderSatisfacationInfo> bigholderSatisfacationInfos1 = bigholderSatisfyMapper1
						.getSfInfoByAreaOProject(area, ar, yearMonth);
				String satisfactionScore = bigholderSatisfyService1.getSatisfaction(bigholderSatisfacationInfos1, a);
				if (!satisfactionScore.equals("")) {
					satisfactionScores += Double.valueOf(satisfactionScore);
					count++;
				}
			}
		}
		if (count != 0) {
			satisfactionScores = satisfactionScores / count;
		}
		return satisfactionScores + "";
	}

	@Override
	public String insertBigholderAduit(String yearMonth, String area, String tbUserCode, boolean flag1) {

		List<BigholderAreaAudit> list = new ArrayList<>();

		List<TbSysOrgan> tbSysOrgans = new ArrayList<>();
		List<String> areaList = this.getHandRight(area, tbUserCode);
		for (String area1 : areaList) {
			TbSysOrgan tbSysOrgan = tbSysOrganMapper.findByOrganName(area1);
			tbSysOrgans.add(tbSysOrgan);
		}
		String satisfactionScores = this.getAvgSatisfactionScores(yearMonth, tbUserCode);
		for (TbSysOrgan tbSysOrgan : tbSysOrgans) {
			List<BigholderSatisfacationInfo> bigholderSatisfacationInfos1 = bigholderSatisfyMapper1
					.getSelectByMonAndArea(yearMonth, tbSysOrgan.getOrganName());
			Set<String> corresondPro = new HashSet<>();
			for (BigholderSatisfacationInfo bigholderSatisfacationInfo : bigholderSatisfacationInfos1) {
				if(!AuditStatus.PASS.equals(bigholderSatisfacationInfo.getAuditStatus())
						&&!AuditStatus.WAITAUDIT.equals(bigholderSatisfacationInfo.getAuditStatus())){
					if (bigholderSatisfacationInfo.getCorrespondProject() != null) {
						corresondPro.add(bigholderSatisfacationInfo.getCorrespondProject());
					}
				}
			}
			Set<ErpCommunity> erpCommunitys1 = new HashSet<>();
			for (String c : corresondPro) {
				List<ErpCommunity> erpCommunitys = tbHsprCommunityMapper.findBaseCommByCommIdOrArea(c,
						tbSysOrgan.getOrganName());
				if (erpCommunitys.size() > 0) {
					erpCommunitys1.add(erpCommunitys.get(0));
				}
			}
			for (ErpCommunity erpCommunity : erpCommunitys1) {
				List<BigholderAreaAudit> bigholderAreaAudits = bigholderAduitMapper.getByAreaAduitsCommId(yearMonth,
						tbSysOrgan.getOrganName(), erpCommunity.getCommId(), AuditStatus.REJECTED);

				List<BigholderAreaAudit> bigholderAreaAudits1 = bigholderAduitMapper.getByAreaAduitsCommId(yearMonth,
						area, erpCommunity.getCommId(), AuditStatus.NOAUDIT);
				
				List<BigholderAreaAudit> bigholderAreaAudits2 = bigholderAduitMapper.getByAreaAduitsCommId(yearMonth,
						tbSysOrgan.getOrganName(), erpCommunity.getCommId(), AuditStatus.WAITAUDIT);

				if (bigholderAreaAudits.size() > 0) {
					bigholderAduitMapper.deleteBigholderAduit(bigholderAreaAudits.get(0).getId() + "");
				}
				if (bigholderAreaAudits1.size() > 0) {
					for (BigholderAreaAudit bigholderAreaAudit : bigholderAreaAudits1) {
						bigholderAduitMapper.deleteBigholderAduit(bigholderAreaAudit.getId() + "");
					}
				}
				List<BigholderSatisfacationInfo> bigholderSatisfacationInfos = bigholderSatisfyMapper1
						.selectByErpCommunity(erpCommunity.getCommId(), yearMonth, tbSysOrgan.getOrganName());
				if (bigholderAreaAudits2.size() == 0) {
					list = this.saveAuditData(erpCommunity, tbSysOrgan, yearMonth, tbUserCode, flag1, list,
							bigholderSatisfacationInfos,AuditStatus.WAITAUDIT);
				} else {
					for (BigholderAreaAudit bigholderAreaAudit : bigholderAreaAudits2) {
						bigholderAduitMapper.deleteBigholderAduit(bigholderAreaAudit.getId() + "");
						list = this.saveAuditData(erpCommunity, tbSysOrgan, yearMonth, tbUserCode, flag1, list,
								bigholderSatisfacationInfos,AuditStatus.WAITAUDIT);
					}
				}
			}
		}
		if (flag1) {
			if (list.size() > 0) {
				bigholderAduitMapper.insertBigholderAduit(list);
			}
		}
		return ComTool.accuracy(satisfactionScores, 2);
	}

	public List<BigholderAreaAudit> saveAuditData(ErpCommunity erpCommunity, TbSysOrgan tbSysOrgan, String yearMonth,
			String tbUserCode, boolean flag1, List<BigholderAreaAudit> list,
			List<BigholderSatisfacationInfo> bigholderSatisfacationInfos,String auditStatus) {
		BigholderAreaAudit bigholderAreaAudit = new BigholderAreaAudit();
		bigholderAreaAudit.setArea(tbSysOrgan.getOrganName());
		bigholderAreaAudit.setCorrespondProject(erpCommunity.getCommId());
		bigholderAreaAudit.setAuditStatus(auditStatus);
		List<BigholderAreaAudit> bigholderAreaAudit1 = bigholderAduitMapper.getBigholderAreaAduits(yearMonth,
				tbSysOrgan.getOrganName(), null);
		bigholderAreaAudit.setVersion(bigholderAreaAudit1.size() > 0 ? bigholderAreaAudit1.get(0).getVersion() + 1 : 1);

		if (bigholderSatisfacationInfos.size() > 0) {
			// 项目总
			int projectTotal = 0;
			// 营销总
			int marketTotal = 0;

			int a41 = 0;
			int a42 = 0;
			int a43 = 0;
			int a44 = 0;
			int a51 = 0;
			int a52 = 0;
			int a53 = 0;
			int a54 = 0;
			int a61 = 0;
			int a62 = 0;
			int a71 = 0;
			int a72 = 0;
			int a73 = 0;
			for (BigholderSatisfacationInfo bigholderSatisfacationInfo : bigholderSatisfacationInfos) {
				// String a =
				// bigholderSatisfacationInfo.getAuditStatus().length() == 0 ?
				// ""
				// : bigholderSatisfacationInfo.getAuditStatus();
				// if (a.equals("") || a.equals(AuditStatus.NOAUDIT) ||
				// a.equals(AuditStatus.REJECTED)) {
				if (flag1) {
					bigholderSatisfacationInfo.setAuditStatus(auditStatus);
					bigholderSatisfyMapper1.updateAuditStatus(bigholderSatisfacationInfo);
				}
				// }

				String jobs = bigholderSatisfacationInfo.getJob().length() != 0 ? bigholderSatisfacationInfo.getJob()
						: "";
				if (jobs.equals("项目总经理/副总经理")) {
					projectTotal++;
				} else if (jobs.equals("项目营销负责人")) {
					marketTotal++;
				}
				a41 += bigholderSatisfacationInfo.getA41();
				a42 += bigholderSatisfacationInfo.getA42();
				a43 += bigholderSatisfacationInfo.getA43();
				a44 += bigholderSatisfacationInfo.getA44();
				a51 += bigholderSatisfacationInfo.getA51();
				a52 += bigholderSatisfacationInfo.getA52();
				a53 += bigholderSatisfacationInfo.getA53();
				a54 += bigholderSatisfacationInfo.getA54();
				a61 += bigholderSatisfacationInfo.getA61();
				a62 += bigholderSatisfacationInfo.getA62();
				a71 += bigholderSatisfacationInfo.getA71();
				a72 += bigholderSatisfacationInfo.getA72();
				a73 += bigholderSatisfacationInfo.getA73();
			}
			bigholderAreaAudit.setProjectTotal(projectTotal + "");
			bigholderAreaAudit.setMarketingTotal(marketTotal + "");

			List<CaseField> caseFields = caseFieldMapper.queryByKeysNoPage(
					(tbSysOrgan != null ? tbSysOrgan.getOrganCode() : null), erpCommunity.getCommId(), yearMonth);
			Double openSatisfactions = 0.0;
			int openCount = 0;
			for (CaseField caseField : caseFields) {
				if (caseField.getOpenCoordinateRate() != null && caseField.getOpenCoordinateRate().length() > 0) {
					Double openSatisfaction = Double.valueOf(caseField.getOpenCoordinateRate());
					openSatisfactions += openSatisfaction;
					openCount++;
				}
			}
			// 开盘配合满意度
			if (caseFields.size() > 0) {
				if(openCount!=0){
					bigholderAreaAudit.setOpenSatisfaction(ComTool.accuracy(openSatisfactions / openCount, 2));
				}else{
					bigholderAreaAudit.setOpenSatisfaction("");
				}
			} else {
				bigholderAreaAudit.setOpenSatisfaction("");
			}
			// 满意度
			List<String> a = new ArrayList<>();
			a.add(yearMonth);
			String satisfactionScore = bigholderSatisfyService1.getSatisfaction(bigholderSatisfacationInfos, a);
			// satisfactionScores+=satisfactionScore;
			if (!openSatisfactions.equals(0.0)) {
				if (!satisfactionScore.equals("") && satisfactionScore != null) {
					bigholderAreaAudit.setSatisfactionScores(
							ComTool.accuracy((Double.valueOf(satisfactionScore) + openSatisfactions / openCount) / 2,2));
				} else {
					bigholderAreaAudit.setSatisfactionScores(null);
				}
			} else {
				if (!satisfactionScore.equals("") && satisfactionScore != null) {
					bigholderAreaAudit.setSatisfactionScores(satisfactionScore);
				} else {
					bigholderAreaAudit.setSatisfactionScores(null);
				}
			}
			bigholderAreaAudit.setCreateBy(tbUserCode);
			bigholderAreaAudit.setYearMonth(yearMonth);
			bigholderAreaAudit.setExceptionStatus(ExceptionStruts.NORMAL);
			bigholderAreaAudit.setRejectedMessage(bigholderSatisfacationInfos.size() != 0
					? bigholderSatisfacationInfos.get(0).getRejectedMessage() : null);

			bigholderAreaAudit.setA41(
					bigholderSatisfacationInfos.size() == 0 ? "0" : a41 / bigholderSatisfacationInfos.size() + "");
			bigholderAreaAudit.setA42(
					bigholderSatisfacationInfos.size() == 0 ? "0" : a42 / bigholderSatisfacationInfos.size() + "");
			bigholderAreaAudit.setA43(
					bigholderSatisfacationInfos.size() == 0 ? "0" : a43 / bigholderSatisfacationInfos.size() + "");
			bigholderAreaAudit.setA44(
					bigholderSatisfacationInfos.size() == 0 ? "0" : a44 / bigholderSatisfacationInfos.size() + "");
			bigholderAreaAudit.setA51(
					bigholderSatisfacationInfos.size() == 0 ? "0" : a51 / bigholderSatisfacationInfos.size() + "");
			bigholderAreaAudit.setA52(
					bigholderSatisfacationInfos.size() == 0 ? "0" : a52 / bigholderSatisfacationInfos.size() + "");
			bigholderAreaAudit.setA53(
					bigholderSatisfacationInfos.size() == 0 ? "0" : a53 / bigholderSatisfacationInfos.size() + "");
			bigholderAreaAudit.setA54(
					bigholderSatisfacationInfos.size() == 0 ? "0" : a54 / bigholderSatisfacationInfos.size() + "");
			bigholderAreaAudit.setA61(
					bigholderSatisfacationInfos.size() == 0 ? "0" : a61 / bigholderSatisfacationInfos.size() + "");
			bigholderAreaAudit.setA62(
					bigholderSatisfacationInfos.size() == 0 ? "0" : a62 / bigholderSatisfacationInfos.size() + "");
			bigholderAreaAudit.setA71(
					bigholderSatisfacationInfos.size() == 0 ? "0" : a71 / bigholderSatisfacationInfos.size() + "");
			bigholderAreaAudit.setA72(
					bigholderSatisfacationInfos.size() == 0 ? "0" : a72 / bigholderSatisfacationInfos.size() + "");
			bigholderAreaAudit.setA73(
					bigholderSatisfacationInfos.size() == 0 ? "0" : a73 / bigholderSatisfacationInfos.size() + "");

			bigholderAreaAudit.setCreateTime(new Date());
			bigholderAreaAudit.setLastUpdateTime(new Date());
			bigholderAreaAudit.setCreateBy(tbUserCode);
			bigholderAreaAudit.setLastUpdateBy(tbUserCode);
			list.add(bigholderAreaAudit);
		}
		return list;
	}

	@Override
	public void stripsNoPassAudit(String ids[], String rejectedMessage, String userCode, String yearMonth,
			String area) {
		if (ids != null) {
			for (String id : ids) {
				BigholderAreaAudit bigholderAreaAudit = bigholderAduitMapper.selectById(id);
				this.commRejunt(bigholderAreaAudit, rejectedMessage, userCode);
			}
		} else {
			List<BigholderAreaAudit> bigholderAreaAudits = bigholderAduitMapper.getBigholderAreaAduits(yearMonth, area,
					null);
			for (BigholderAreaAudit bigholderAreaAudit : bigholderAreaAudits) {
				this.commRejunt(bigholderAreaAudit, rejectedMessage, userCode);
			}
		}
	}

	public void commRejunt(BigholderAreaAudit bigholderAreaAudit, String rejectedMessage, String userCode) {
		bigholderAreaAudit.setAuditStatus(AuditStatus.REJECTED);
		bigholderAreaAudit.setRejectedMessage(rejectedMessage);
		// bigholderAreaAudit.setExceptionStatus(ExceptionStruts.EXCEPT);
		bigholderAreaAudit.setLastUpdateTime(new Date());
		bigholderAreaAudit.setLastUpdateBy(userCode);
		bigholderAduitMapper.updateRejectedInfo(bigholderAreaAudit);
		List<BigholderSatisfacationInfo> bigholderSatisfacationInfos = bigholderSatisfyMapper1.getSfInfoByAreaOProject(
				bigholderAreaAudit.getArea(), bigholderAreaAudit.getCorrespondProject(),
				bigholderAreaAudit.getYearMonth());
		if (bigholderSatisfacationInfos.size() > 0) {
			for (BigholderSatisfacationInfo bigholderSatisfacationInfo : bigholderSatisfacationInfos) {
				bigholderSatisfacationInfo.setAuditStatus(AuditStatus.REJECTED);
				bigholderSatisfacationInfo.setRejectedMessage(rejectedMessage);
				// bigholderSatisfacationInfo.setExceptionStruts(ExceptionStruts.EXCEPT);
				bigholderSatisfacationInfo.setLastUpdateDate(simpleDateFormat.format(new Date()));
				bigholderSatisfacationInfo.setLastUpdateBy(userCode);
				bigholderSatisfyMapper1.updateRejectedInfo(bigholderSatisfacationInfo);
			}
		}
	}

	public List<Map<String, Object>> comT1(String area, List<Map<String, Object>> list) {

		Set<ErpCommunity> erpCommunitys2 = new HashSet<>();

		TbSysOrgan tbSysOrgan = tbSysOrganMapper.findByOrganName(area);
		if(tbSysOrgan!=null){
			List<ErpCommunity> erpCommunitys = tbHsprCommunityMapper
					.slectErpCommunityByOrganCode(tbSysOrgan.getOrganCode());
			for (ErpCommunity erpCommunity : erpCommunitys) {
				erpCommunitys2.add(erpCommunity);
			}
		}

		for (ErpCommunity erpCommunity : erpCommunitys2) {
			Map<String, Object> map = new HashMap<>();
			if(erpCommunity.getCommId()!=null&&erpCommunity.getCommName()!=null){
				map.put("text", erpCommunity.getCommId());
				map.put("value", erpCommunity.getCommName());
			}
			list.add(map);
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> findCommBaseByOrgCodes(String area, String userCode) {
		// List<Role> roles = roleMapper.findByUserCode(userCode);
		List<Map<String, Object>> list = new ArrayList<>();
		list = this.comT1(area, list);
		// for (Role role : roles) {
		// // 判断是否是总部
		// if (role.getCode().equals(BIGHEADQUATERS)) {
		// list=this.comT1(area, list);
		// } else if (role.getCode().equals(BIGHEADAREA)) {
		// List<Map<String, Object>> list1 = new ArrayList<>();
		// for (Role role1 : roles) {
		// // 判断是否是总部
		// if (role1.getCode().equals(BIGHEADQUATERS)) {
		// list1=this.comT1(area, list1);
		// return list1;
		// }
		// }
		// List<BigholderRightManager> bigholderRightManagers =
		// bigholderRightManagerMapper
		// .getRightManagerByHandmanId(userCode);
		// if (area != null) {
		// Set<ErpCommunity> erpCommunitys2 = new HashSet<>();
		// for (BigholderRightManager bigholderRightManager :
		// bigholderRightManagers) {
		// if (bigholderRightManager.getArea().equals(area)) {
		// TbSysOrgan tbSysOrgan = tbSysOrganMapper.findByOrganName(area);
		// List<ErpCommunity> erpCommunitys = tbHsprCommunityMapper
		// .slectErpCommunityByOrganCode(tbSysOrgan.getOrganCode());
		// for (ErpCommunity erpCommunity : erpCommunitys) {
		// erpCommunitys2.add(erpCommunity);
		// }
		// }
		// }
		// for (ErpCommunity erpCommunity : erpCommunitys2) {
		// Map<String, Object> map = new HashMap<>();
		// map.put("text", erpCommunity.getCommId());
		// map.put("value", erpCommunity.getCommName());
		// list.add(map);
		// }
		// } else {
		// Set<ErpCommunity> erpCommunitys2 = new HashSet<>();
		// for (BigholderRightManager bigholderRightManager :
		// bigholderRightManagers) {
		// List<ErpCommunity> erpCommunitys = tbHsprCommunityMapper
		// .slectErpCommunityByOrganCode(bigholderRightManager.getAreaId());
		// for (ErpCommunity erpCommunity : erpCommunitys) {
		// erpCommunitys2.add(erpCommunity);
		// }
		// }
		// for (ErpCommunity erpCommunity : erpCommunitys2) {
		// Map<String, Object> map = new HashMap<>();
		// map.put("text", erpCommunity.getCommId());
		// map.put("value", erpCommunity.getCommName());
		// list.add(map);
		// }
		// }
		// }
		// }
		return list;
	}

	// public void comVommunity(String area) {
	// TbSysOrgan tbSysOrgan = tbSysOrganMapper.findByOrganName(area);
	// List<ErpCommunity> erpCommunitys = tbHsprCommunityMapper
	// .slectErpCommunityByOrganCode(tbSysOrgan.getOrganCode());
	// for (ErpCommunity erpCommunity : erpCommunitys) {
	//// Map<String, Object> map = new HashMap<>();
	//// map.put("text", erpCommunity.getCommId());
	//// map.put("value", erpCommunity.getCommName());
	//// list.add(map);
	// }
	// return list;
	// }
	/**
	 * 导入数据的时候修改异常，审核及项目总问卷的审核状态及推送状态
	 */
	public void updateAllStatus(String yearMonth) {
		// 项目总问卷
		List<BigholderSatisfacationInfo> bigholderSatisfacationInfos = bigholderSatisfyMapper1.findAll(yearMonth);
		for (BigholderSatisfacationInfo bigholderSatisfacationInfo : bigholderSatisfacationInfos) {
			if (AuditStatus.NOAUDIT.equals(bigholderSatisfacationInfo.getAuditStatus())
					&& ExceptionStruts.NORMAL.equals(bigholderSatisfacationInfo.getExceptionStruts())
					&& PustThismonthPerdata.NOPUST.equals(bigholderSatisfacationInfo.getPushPerdataStruts())) {
			} else {
				bigholderSatisfacationInfo.setAuditStatus(AuditStatus.NOAUDIT);
				bigholderSatisfacationInfo.setExceptionStruts(ExceptionStruts.NORMAL);
				bigholderSatisfacationInfo.setPushPerdataStruts(PustThismonthPerdata.NOPUST);
				bigholderSatisfyMapper1.updateAllStatus(bigholderSatisfacationInfo);
			}
		}

		// 审核
		List<BigholderAreaAudit> bigholderAreaAudits = bigholderAduitMapper.getBigholderAreaAuditAll(yearMonth);
		for (BigholderAreaAudit bigholderAreaAudit : bigholderAreaAudits) {
			if (AuditStatus.NOAUDIT.equals(bigholderAreaAudit.getAuditStatus())
					&& ExceptionStruts.NORMAL.equals(bigholderAreaAudit.getExceptionStatus())
					&& PustThismonthPerdata.NOPUST.equals(bigholderAreaAudit.getPushPerdataStruts())) {
			} else {
				bigholderAreaAudit.setAuditStatus(AuditStatus.NOAUDIT);
				bigholderAreaAudit.setExceptionStatus(ExceptionStruts.NORMAL);
				bigholderAreaAudit.setPushPerdataStruts(PustThismonthPerdata.NOPUST);
				bigholderAduitMapper.updateAllStatus(bigholderAreaAudit);
			}
		}
		// 异常
		List<BigholderExceptionInfo> bigholderExceptionInfos = bigholderExceptionInfoMapper.findExceptionAll(yearMonth);
		for (BigholderExceptionInfo bigholderExceptionInfo : bigholderExceptionInfos) {
			if (!ExceptionStruts.NORMAL.equals(bigholderExceptionInfo)) {
				bigholderExceptionInfo.setExceptionAduitStatus(ExceptionStruts.NORMAL);
				bigholderExceptionInfoMapper.updateExceptionStatus(bigholderExceptionInfo);
			}
		}
		// 推送
		List<BigholderPushPerdata> bigholderPushPerdatas = bigholderPushPerdataMapper.getAll(yearMonth,
				PustThismonthPerdata.PUST);
		for (BigholderPushPerdata bigholderPushPerdata : bigholderPushPerdatas) {
			bigholderPushPerdata.setPushStatus(PustThismonthPerdata.NOPUST);
			bigholderPushPerdataMapper.updatePushStatus(bigholderPushPerdata);
		}
	}
}