package com.unis.base.server.service;

import com.alibaba.fastjson.JSON;
import com.unis.base.server.dao.DYgxxsjyDao;
import com.unis.base.server.dao.TzSlqyDao;
import com.unis.base.server.entity.DTankDO;
import com.unis.base.server.entity.DYgxxsjyDO;
import com.unis.base.server.entity.TzSlqyDO;
import com.unis.common.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;


/**
 * 每天获取油罐数据
 * Created by admin on 2017/12/4.
 */
@Component
@Slf4j
@Lazy(false)
public class YgxxTimer {

	@Autowired
	private DYgxxsjyDao ygxxsjyDao;
	@Autowired
	TzSlqyDao tzSlqyMapper;

	//@Scheduled(cron = "0 0 2 * * ?")//每天晚上12点05执行
	public  void dowork(){
		String sign= DateUtil.getCurrentStringDate();
		log.info("------油罐获取开始",sign);
		try {
			execute(sign);
		} catch (Exception e) {
			e.printStackTrace();
		}
		log.info("---油罐获取结束",sign);

	}

	public void execute(String sign) throws Exception{
		log.info("===================================定时油罐获取开始======================================================================");
		//抽取油罐信息到base库的D_YGXXSJY表
		//先删除所有数据 在把库级的数据全部添加进去 全量操作 因为库级的煞笔不给标识
		ygxxsjyDao.deleteAll();
		List<DTankDO> list = ygxxsjyDao.selectTankList();
		List<TzSlqyDO> tzSlqyDOS = tzSlqyMapper.getSlqyByQybmAll();
		Map<String,TzSlqyDO> map = new HashMap<>();
		for(TzSlqyDO tzSlqyDO:tzSlqyDOS){
			if(map.get(tzSlqyDO.getQybm())!=null){
			}else{
				map.put(tzSlqyDO.getQybm(),tzSlqyDO);
			}
		}
		for(DTankDO tankDO:list){
            log.info(JSON.toJSONString(tankDO));
			TzSlqyDO tzSlqyDO = map.get(tankDO.getOrg());
			DYgxxsjyDO ygxxsjyDO = new DYgxxsjyDO();
			ygxxsjyDO.setId(tankDO.getId());
			ygxxsjyDO.setYgbh(tankDO.getStoragetankCode());
			ygxxsjyDO.setKddm(tzSlqyDO.getZzjgdm()+"001");
			ygxxsjyDO.setKdmc(tzSlqyDO.getQymc());
			ygxxsjyDO.setYgmc(tankDO.getStoragetankName());
			if(tankDO.getTankCapacity()!=null) {
				ygxxsjyDO.setSjgr(tankDO.getTankCapacity().doubleValue());
			}
			if(tankDO.getDiameter()!=null) {
				ygxxsjyDO.setYgzj(tankDO.getDiameter().doubleValue());
			}
			if(tankDO.getHeight()!=null) {
				ygxxsjyDO.setYggd(tankDO.getHeight().doubleValue());
			}
			if(tankDO.getDesignHeight()!=null){
				ygxxsjyDO.setZygd(tankDO.getDesignHeight().doubleValue());
			}
			ygxxsjyDO.setYglx(tankDO.getYglx());
			ygxxsjyDO.setSjnd(tankDO.getBuilddate());
			ygxxsjyDO.setYgsyzt(Optional.ofNullable(tankDO.getTankApply()).map(String::valueOf).orElse(""));
			ygxxsjyDO.setYgzt(tankDO.getCrkStatus());
			ygxxsjyDO.setYglx(String.valueOf(tankDO.getTankType()));
			if(tankDO.getBottomArea()!=null) {
				ygxxsjyDO.setYgdmj(tankDO.getBottomArea().doubleValue());
			}
			if(tankDO.getDiameter()!=null) {
				ygxxsjyDO.setYgnzj(tankDO.getDiameter().doubleValue());
			}
			if(tankDO.getOilHeight()!=null) {
				ygxxsjyDO.setYgcykgd(tankDO.getOilHeight().doubleValue());
			}
			if(tankDO.getInsportHeight()!=null) {
				ygxxsjyDO.setYgjckgd(tankDO.getInsportHeight().doubleValue());
			}
			ygxxsjyDO.setYgpmt(tankDO.getTankChart());
			ygxxsjyDO.setYgkqt(tankDO.getTanklibChart());
			ygxxsjyDO.setSqzt("0");
			// 增加出库状态信息
			ygxxsjyDO.setCrkStatus(tankDO.getCrkStatus());
			ygxxsjyDao.insertSelective(ygxxsjyDO);
		}
		log.info("===================================定时油罐获取结束======================================================================");
	}
}
