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

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

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 com.mysql.fabric.xmlrpc.base.Array;

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.bigholderSatisfy.BigholderSatisfyMapper1;
import cn.com.countrygarden.bi.main.dao.dup.CaseFieldMapper;
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.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.eunm.AuditStatus;
import cn.com.countrygarden.bi.main.model.bigholderAduit.eunm.ExceptionStruts;
import cn.com.countrygarden.bi.main.model.common.Organ;
import cn.com.countrygarden.bi.main.model.dup.CaseField;
import cn.com.countrygarden.bi.main.model.twerp.ErpCommunity;
import cn.com.countrygarden.bi.main.model.twerp.hm.TbHsprCommunity;
import cn.com.countrygarden.bi.main.model.twerp.hm.TbSysOrgan;
import cn.com.countrygarden.bi.main.service.bigholderAduit.BigholderExceptionInfoService;
import cn.com.countrygarden.bi.main.service.bigholderSatisfy.BigholderSatisfyService1;

@Service
@Transactional
public class BigholderExceptionInfoServiceImpl implements BigholderExceptionInfoService {

	@Autowired
	BigholderExceptionInfoMapper bigholderExceptionInfoMapper;

	@Autowired
	BigholderSatisfyMapper1 bigholderSatisfyMapper1;

	@Autowired
	TbHsprCommunityMapper tbHsprCommunityMapper;

	@Autowired
	TbSysOrganMapper tbSysOrganMapper;

	@Autowired
	BigholderRightManagerMapper bigholderRightManagerMapper;

	@Autowired
	BigholderAduitMapper bigholderAduitMapper;
	
	@Autowired
	private CaseFieldMapper caseFieldMapper;
	
	@Autowired
	BigholderSatisfyService1 bigholderSatisfyService1;

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

		// if(bigholderRightManagers.size()==0){
		// throw new ServiceException("没有配置区域权限");
		// }

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

	@Override
	public List<Map<String, Object>> bigholderExceptionInfoList(String yearMonth,
			String area, String tbUserId, String exceptionInfoAuditStruts, boolean flag) {
		List<BigholderExceptionInfo> bigholderExceptionInfos = new ArrayList<>();

		if (!flag) {
			List<String> areaList = this.getHandRight(area, tbUserId);
			areaList = areaList.size() == 0 ? null : areaList;
			if (areaList != null) {
				bigholderExceptionInfos = bigholderExceptionInfoMapper.getExByAreasOrOther(yearMonth, areaList,
						exceptionInfoAuditStruts);
			}
		} else {
			bigholderExceptionInfos = bigholderExceptionInfoMapper.getBigholderExceptionInfo(yearMonth,
					area, exceptionInfoAuditStruts);
		}
		List<Map<String, Object>> lists = new ArrayList<>();
		for (BigholderExceptionInfo bigholderExceptionInfo : bigholderExceptionInfos) {
			String a = bigholderExceptionInfo.getExceptionAduitStatus() == null ? ""
					: bigholderExceptionInfo.getExceptionAduitStatus();
			if (a.equals(ExceptionStruts.EXCEPT)) {
				Map<String, Object> data = new HashMap<>();
				List<ErpCommunity> erpCommunitys = tbHsprCommunityMapper
						.findBaseCommByCommId(bigholderExceptionInfo.getCorrespondProject());
				data.put("id", bigholderExceptionInfo.getId());
				data.put("exceptionStatus", a);
				data.put("area", bigholderExceptionInfo.getArea());
				data.put("originalProjectName", bigholderExceptionInfo.getOriginalProjectName());
				data.put("correspondProject", erpCommunitys.size() > 0 ? (erpCommunitys.get(0).getCommName()) : "");
				data.put("correspondProjectId", erpCommunitys.size() > 0 ? (erpCommunitys.get(0).getCommId()) : "");
				data.put("jobs", bigholderExceptionInfo.getJobs());
				data.put("totalScore", bigholderExceptionInfo.getTotalScore());
				data.put("exceptionMessage", bigholderExceptionInfo.getExceptionMessage());
				lists.add(data);
			}
		}
		return lists;
	}
	@Override
	public String updateBigholderExceptionInfo(String id, String correspondProject, String area, String exceptionMessage,boolean flag) {
		BigholderExceptionInfo bigholderExceptionInfo = bigholderExceptionInfoMapper.selectById(id + "");
		if (bigholderExceptionInfo != null) {
			BigholderSatisfacationInfo bigholderSatisfacationInfo = bigholderSatisfyMapper1
					.getViewInfo(bigholderExceptionInfo.getSatisfactionInfoId());
			TbSysOrgan tbSysOrgan=tbSysOrganMapper.findByOrganName(area);
			int count=0;
			if(tbSysOrgan!=null){
				List<ErpCommunity> erpCommunitys = tbHsprCommunityMapper
						.slectErpCommunityByOrganCode(tbSysOrgan.getOrganCode());
				for (ErpCommunity erpCommunity : erpCommunitys) {
					if(erpCommunity.getCommId()!=null){
						if((erpCommunity.getCommId()+"").equals(correspondProject)){
							count++;
						}
					}
				}
			}
			if(count==0){
				correspondProject=null;
			}
			if (!flag) {
				bigholderSatisfacationInfo.setCorrespondProject(correspondProject);
				bigholderSatisfacationInfo.setEstateArea(area);
			}
			if (flag) {
				bigholderSatisfacationInfo.setExceptionStruts(ExceptionStruts.NORMAL);
				bigholderSatisfacationInfo.setExceptionMessage(exceptionMessage);
			}
			bigholderSatisfyMapper1.updateAreaOrProjectName(bigholderSatisfacationInfo);
		}
		if (flag) {
			bigholderExceptionInfoMapper.updateBigholderExceptionInfo(id, correspondProject, area,
					ExceptionStruts.NORMAL,exceptionMessage);
		}
		if (!flag) {
			bigholderExceptionInfoMapper.updateBigholderExceptionInfo(id, correspondProject, area,
					ExceptionStruts.EXCEPT,null);
		}
		return "OK";
	}

	@Override
	public void getBigholderExInfo(String id, String correspondProject, String area) {
		BigholderExceptionInfo bigholderExceptionInfo = bigholderExceptionInfoMapper.selectById(id);
		if (bigholderExceptionInfo != null) {
			BigholderSatisfacationInfo bigholderSatisfacationInfo = bigholderSatisfyMapper1
					.getViewInfo(bigholderExceptionInfo.getSatisfactionInfoId());
			bigholderSatisfacationInfo.setExceptionStruts(ExceptionStruts.NORMAL);
			bigholderSatisfacationInfo.setCorrespondProject(correspondProject);
			bigholderSatisfacationInfo.setArea(area);
		}
	}

	@Override
	public Boolean bigholderExceptionInfoLists(String yearMonth, String area, String tbUserId,
			String exceptionInfoAuditStruts) {
		if(area==null){
			List<BigholderExceptionInfo> bigholderExceptionInfos = bigholderExceptionInfoMapper
				.getBigholderExceptionInfo(yearMonth, area, exceptionInfoAuditStruts);
			if (bigholderExceptionInfos.size() > 0) {
				return false;
			}
		}else{
			List<String> areaList=bigholderRightManagerMapper.findAreasByUserCode(tbUserId);
			for (String alist : areaList) {
				List<BigholderExceptionInfo> bigholderExceptionInfos = bigholderExceptionInfoMapper
						.getBigholderExceptionInfo(yearMonth, area, exceptionInfoAuditStruts);
				if (bigholderExceptionInfos.size() > 0) {
					return false;
				}
			}
		}
		return true;
	}

	@Override
	public void deleteBigholderExInfo(String id,String userCode) {
		BigholderExceptionInfo bigholderExceptionInfo = bigholderExceptionInfoMapper.selectById(id);
		BigholderSatisfacationInfo bigholderSatisfacationInfo=bigholderSatisfyMapper1.getViewInfo(bigholderExceptionInfo.getSatisfactionInfoId());
		this.updateRejectData(bigholderExceptionInfo, bigholderSatisfacationInfo,userCode);
		if (bigholderExceptionInfo != null) {
			if (bigholderExceptionInfo.getSatisfactionInfoId() != null) {
				bigholderSatisfyMapper1.deleteExceptionStatus(bigholderExceptionInfo.getSatisfactionInfoId());
			}
		}
		bigholderExceptionInfoMapper.deleteBigholderExInfo(Integer.valueOf(id));
	}

	/**
	 * 修改审核表里的拒绝数据
	 */
	public void updateRejectData(BigholderExceptionInfo bigholderExceptionInfo,BigholderSatisfacationInfo bigholderSatisfacationInfo1,String userCode) {
		List<BigholderAreaAudit> list=new ArrayList<>();
		if (bigholderExceptionInfo != null
				&& AuditStatus.REJECTED.equals(bigholderSatisfacationInfo1.getAuditStatus())) {
			List<BigholderAreaAudit> bigholderAreaAudits=bigholderAduitMapper.getByAreaAduitsCommId(bigholderExceptionInfo.getYearMonth(),
					bigholderExceptionInfo.getArea(), bigholderExceptionInfo.getCorrespondProject(),
					AuditStatus.REJECTED);
			for (BigholderAreaAudit bigholderAreaAudit : bigholderAreaAudits) {
				bigholderAduitMapper.deleteBigholderAduit(bigholderAreaAudit.getId()+"");
			}
		}
	}
}