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

import cn.com.countrygarden.bi.main.dao.project.BiApplnoteMapper;
import cn.com.countrygarden.bi.main.dao.project.BiEmergencyMapper;
import cn.com.countrygarden.bi.main.dao.project.TbDupContractTimedEmergencyMapper;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.project.BiApplnote;
import cn.com.countrygarden.bi.main.model.project.BiEmergency;
import cn.com.countrygarden.bi.main.model.project.TbDupContractTimedEmergency;
import cn.com.countrygarden.bi.main.service.project.BiEmergencyService;
import cn.com.countrygarden.bi.main.service.project.impl.vo.outburst.ApplNote;
import cn.com.countrygarden.bi.main.service.project.impl.vo.outburst.BaseData;
import cn.com.countrygarden.bi.main.service.project.impl.vo.outburst.ModelData;
import com.github.pagehelper.PageRowBounds;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Slf4j
@Service
@Transactional
public class BiEmergencyServiceImpl implements BiEmergencyService{
	 @Autowired
	BiEmergencyMapper biEmergencyMapper;
	 @Autowired
	 BiApplnoteMapper biApplnoteMapper;
	 @Autowired
	 TbDupContractTimedEmergencyMapper tbDupContractTimedEmergencyMapper;
	 @Override
	public void json2Date(List<BaseData> blist) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for (BaseData baseData : blist) {
			log.info("-------执行bi突发事件定时任务---01-----  ");
			BiEmergency bi = new BiEmergency();
			ModelData m= baseData.modelData;
			bi.setOrgName(m.fd_363ef871e7e56a_text);
			bi.setCommName(m.fd_363ef875e1059e_text);
			bi.setDepartmentName(m.fd_363bdf4ca77096.name);
			if(null ==m.fd_363bdf6cd10a92.time || m.fd_363bdf6cd10a92.time ==0) {
				bi.setHappenTime(null);
			}else {
				bi.setHappenTime(sdf.format(new Date(m.fd_363bdf6cd10a92.time)));
			}
			bi.setFunctionaryName(m.fd_363bdf97c6af4c);
			bi.setFunctionaryCall(m.fd_363bdf858825fa.name);
			if(null != m.fd_363be020f89efe) {
				bi.setLevelTwo(m.fd_363be020f89efe.toString());
			}
			if(null != m.fd_363bdfc8fb4bb0) {
				bi.setLevelOne(m.fd_363bdfc8fb4bb0.toString());
			}
			bi.setTheme(m.fd_363be06cabff94);
			if(null != m.fd_363be09600f598) {
				if(m.fd_363be09600f598==1) {
					bi.setIsCasualties("是");
				}else {
					bi.setIsCasualties("否");
				}
			}
			bi.setCasualtiesName(m.fd_363be5d79502d6);
			bi.setOldId(m.fd_363ef875e1059e); 
			bi.setCorrelation(m.fd_363be1aced6284);
			bi.setCasualtiesOther(m.fd_363be5f69ea106);
			if(null != m.fd_363be11a30b2c2) {
				if(m.fd_363be11a30b2c2==1) {
					bi.setIsProperty("是");
				}else {
					bi.setIsProperty("否");
				}
			}
			bi.setLossParty (m.fd_363be346921f72);
			
			if(null != m.fd_363bfab29845e6) {
				bi.setFuture(m.fd_363bfab29845e6.toString());
			}
			
			bi.setSketch(m.fd_363be163c2fa20);
			bi.setMeasure(m.fd_363be194414c32);
			bi.setRisk(m.fd_363be16d793db6);
			//bi.setCorrelation(m.fd_363be16d793db6);
			BiEmergency bie = new BiEmergency();
			bie.setCommName(bi.getCommName());
			bie.setTheme(bi.getTheme());
			bie.setFunctionaryCall(bi.getFunctionaryCall());
			bie.setHappenTime(bi.getHappenTime());
			bie.setOrgName(bi.getOrgName());
			bie.setLevelOne(bi.getLevelOne());
			bie.setLevelTwo(bi.getLevelTwo());
			bie.setIsCasualties(bi.getIsCasualties());
			bie.setCasualtiesName(bi.getCasualtiesName());
			bie.setCasualtiesOther(bi.getCasualtiesOther());
			bie.setIsProperty(bi.getIsProperty());
			bie.setLossParty(bi.getLossParty());
			bie.setFuture(bi.getFuture());
			bie.setSketch(bi.getSketch());
			bie.setMeasure(bi.getMeasure());
			bie.setRisk(bi.getRisk());
			bie.setCorrelation(bi.getCorrelation());
			List<BiEmergency> selectOne = biEmergencyMapper.select(bie);
			if(selectOne.size()==0) {
			biEmergencyMapper.insert(bi);
			
			
			List<ApplNote> aList= baseData.applNote;
				log.info("-------执行bi突发事件定时任务---02-----  ");
				for (ApplNote applNote : aList) {
					BiApplnote b = new BiApplnote ();
					b.setEid(bi.getId());
					b.setFdAuditNote(applNote.fdAuditNote);
					b.setFdCreateTime(applNote.fdCreateTime);
					b.setHandlerName(applNote.handlerName);
					biApplnoteMapper.insert(b);

					log.info("-------执行bi突发事件定时任务---03-----  ");
				}
			
			}
			
		}
		
	}
	 
	 @Override
		public PageData search(Map m,Integer page,Integer size) {
			 PageRowBounds pageRowBounds = new PageRowBounds(page,size);
			List<BiEmergency> plist = biEmergencyMapper.search(m, pageRowBounds);
			for (BiEmergency biEmergency : plist) {
				if(biEmergency.getLevelOne()==null) {
					
				}else
				if("1".equals(biEmergency.getLevelOne())) {
					biEmergency.setLevelOne("业务管理类");
				}else
				if("2".equals(biEmergency.getLevelOne())) {
					biEmergency.setLevelOne("被诉案件类");
				}else
				if("3".equals(biEmergency.getLevelOne())) {
					biEmergency.setLevelOne("员工相关类");
				}else
				if("4".equals(biEmergency.getLevelOne())) {
					biEmergency.setLevelOne("地产相关类");
				}else
				if("5".equals(biEmergency.getLevelOne())) {
					biEmergency.setLevelOne("自然灾害类");
				}else
				if("6".equals(biEmergency.getLevelOne())) {
					biEmergency.setLevelOne("停车收费类");
				}else
				if("7".equals(biEmergency.getLevelOne())) {
					biEmergency.setLevelOne("其他类");
				}else {
					biEmergency.setLevelOne("");
				}
				
				if(biEmergency.getLevelTwo()==null) {
				
				}else
				if("1".equals(biEmergency.getLevelTwo())) {
					biEmergency.setLevelTwo("工程管理类");
				}else
				if("2".equals(biEmergency.getLevelTwo())) {
					biEmergency.setLevelTwo("安全管理类");
				}else
				if("3".equals(biEmergency.getLevelTwo())) {
					biEmergency.setLevelTwo("清洁管理类");
				}else
				if("4".equals(biEmergency.getLevelTwo())) {
					biEmergency.setLevelTwo("绿化管理类");
				}else
				if("5".equals(biEmergency.getLevelTwo())) {
					biEmergency.setLevelTwo("综合服务类-普通服务");
				}else
				if("6".equals(biEmergency.getLevelTwo())) {
					biEmergency.setLevelTwo("被诉讼案件");
				}else
				if("7".equals(biEmergency.getLevelTwo())) {
					biEmergency.setLevelTwo("员工相关类");
				}else
				if("8".equals(biEmergency.getLevelTwo())) {
					biEmergency.setLevelTwo("案场管理类");
				}else
				if("9".equals(biEmergency.getLevelTwo())) {
					biEmergency.setLevelTwo("前期介入类");
				}else
				if("10".equals(biEmergency.getLevelTwo())) {
					biEmergency.setLevelTwo("自然灾害类");
				}else
				if("11".equals(biEmergency.getLevelTwo())) {
					biEmergency.setLevelTwo("停车收费类");
				}else
				if("12".equals(biEmergency.getLevelTwo())) {
					biEmergency.setLevelTwo("其他类");
				}else
				if("13".equals(biEmergency.getLevelTwo())) {
					biEmergency.setLevelTwo("综合服务类-重大热点投诉");
				}else {
					biEmergency.setLevelTwo("");
				}
				if(biEmergency.getLossParty()==null) {
					
				}else
				if("1".equals(biEmergency.getLossParty())) {
					biEmergency.setLossParty("业户");
				}else if("2".equals(biEmergency.getLossParty())) {
					biEmergency.setLossParty("公司自有财产");
				}else {
					biEmergency.setLossParty("");
				}
			}
			return new PageData( plist,pageRowBounds);
		}
	 @Override
	 public BiEmergency findOne(Integer id) {
		 BiEmergency bi = new BiEmergency();
		 bi.setId(id);
		 BiEmergency biEmergency = biEmergencyMapper.selectOne(bi);
		 if(biEmergency.getLevelOne()==null) {
				
			}else
			if("1".equals(biEmergency.getLevelOne())) {
				biEmergency.setLevelOne("业务管理类");
			}else
			if("2".equals(biEmergency.getLevelOne())) {
				biEmergency.setLevelOne("被诉案件类");
			}else
			if("3".equals(biEmergency.getLevelOne())) {
				biEmergency.setLevelOne("员工相关类");
			}else
			if("4".equals(biEmergency.getLevelOne())) {
				biEmergency.setLevelOne("地产相关类");
			}else
			if("5".equals(biEmergency.getLevelOne())) {
				biEmergency.setLevelOne("自然灾害类");
			}else
			if("6".equals(biEmergency.getLevelOne())) {
				biEmergency.setLevelOne("停车收费类");
			}else
			if("7".equals(biEmergency.getLevelOne())) {
				biEmergency.setLevelOne("其他类");
			}else {
				biEmergency.setLevelOne("");
			}
			
			if(biEmergency.getLevelTwo()==null) {
			
			}else
			if("1".equals(biEmergency.getLevelTwo())) {
				biEmergency.setLevelTwo("工程管理类");
			}else
			if("2".equals(biEmergency.getLevelTwo())) {
				biEmergency.setLevelTwo("安全管理类");
			}else
			if("3".equals(biEmergency.getLevelTwo())) {
				biEmergency.setLevelTwo("清洁管理类");
			}else
			if("4".equals(biEmergency.getLevelTwo())) {
				biEmergency.setLevelTwo("绿化管理类");
			}else
			if("5".equals(biEmergency.getLevelTwo())) {
				biEmergency.setLevelTwo("综合服务类-普通服务");
			}else
			if("6".equals(biEmergency.getLevelTwo())) {
				biEmergency.setLevelTwo("被诉讼案件");
			}else
			if("7".equals(biEmergency.getLevelTwo())) {
				biEmergency.setLevelTwo("员工相关类");
			}else
			if("8".equals(biEmergency.getLevelTwo())) {
				biEmergency.setLevelTwo("案场管理类");
			}else
			if("9".equals(biEmergency.getLevelTwo())) {
				biEmergency.setLevelTwo("前期介入类");
			}else
			if("10".equals(biEmergency.getLevelTwo())) {
				biEmergency.setLevelTwo("自然灾害类");
			}else
			if("11".equals(biEmergency.getLevelTwo())) {
				biEmergency.setLevelTwo("停车收费类");
			}else
			if("12".equals(biEmergency.getLevelTwo())) {
				biEmergency.setLevelTwo("其他类");
			}else
			if("13".equals(biEmergency.getLevelTwo())) {
				biEmergency.setLevelTwo("综合服务类-重大热点投诉");
			}else {
				biEmergency.setLevelTwo("");
			}
			
			if(biEmergency.getLossParty()==null) {
				
			}else
			if("1".equals(biEmergency.getLossParty())) {
				biEmergency.setLossParty("业户");
			}else if("2".equals(biEmergency.getLossParty())) {
				biEmergency.setLossParty("公司自有财产");
			}else {
				biEmergency.setLossParty("");
			}
		 return biEmergency;
	 }
	 @Override
	 public List<BiApplnote> findAppList(Integer id) {
		 BiApplnote ba = new BiApplnote();
		 ba.setEid(id);
		 return biApplnoteMapper.select(ba);
	 }
	 
	 @SuppressWarnings("null")
		@Override
		public TbDupContractTimedEmergency findDateStart() {
		 TbDupContractTimedEmergency bipDto = null;
		 TbDupContractTimedEmergency bipDtos = new TbDupContractTimedEmergency();
			String now = null;
			// 查库动作--获取最新的数据
			bipDto = tbDupContractTimedEmergencyMapper.findDateStart();
			if (bipDto == null) {
				TbDupContractTimedEmergency contractTimedExecutes =new TbDupContractTimedEmergency();
				contractTimedExecutes.setStartDate("2017-01-01");
				// 当前时间转换字符串
				now = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
				System.out.println(now);
				contractTimedExecutes.setEndDate(now);
			bipDtos.setCreateDate(new Date());
			bipDtos.setStartDate(now);
			bipDtos.setEndDate(findDate(now));
			// 插入下一次查询的时间
			tbDupContractTimedEmergencyMapper.insterContractTimedExecute(bipDtos);
			return contractTimedExecutes;
			}
			//准备下一次查询的时间
			bipDtos.setCreateDate(new Date());
			bipDtos.setStartDate(bipDto.getEndDate());
			bipDtos.setEndDate(findDate(bipDto.getEndDate()));
			// 插入下一次查询的时间
			 int a = tbDupContractTimedEmergencyMapper.insterContractTimedExecute(bipDtos);
			return bipDto;
		}
	 
	// 时间+1天
		public String findDate(String dates) {
			Date date;
			try {
				date = new SimpleDateFormat("yyyy-MM-dd").parse(dates);
				Format f = new SimpleDateFormat("yyyy-MM-dd");
				System.out.println("今天是:" + dates);
				Calendar c = Calendar.getInstance();
				c.setTime(date);
				c.add(Calendar.DAY_OF_MONTH, 1);// 今天+1天
				Date tomorrow = c.getTime();
				System.out.println("明天是:" + f.format(tomorrow));

				return f.format(tomorrow);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
}
