package com.chinaitop.depot.intelligent.ventilation.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.chinaitop.depot.intelligent.utils.JsonToObjectUtils;
import com.chinaitop.depot.intelligent.utils.UuidUtils;
import com.chinaitop.depot.intelligent.ventilation.mapper.TCtldevinfoMapper;
import com.chinaitop.depot.intelligent.ventilation.mapper.TJobMapper;
import com.chinaitop.depot.intelligent.ventilation.mapper.TVentilationMapper;
import com.chinaitop.depot.intelligent.ventilation.model.*;
import com.chinaitop.depot.intelligent.ventilation.service.AerationParameterService;
import com.chinaitop.depot.intelligent.ventilation.service.AerationRecordService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.*;



@Service
public class AerationRecordServiceImpl implements AerationRecordService {
    @Autowired
    private TVentilationMapper tVentilationMapper;
    @Autowired
    private TJobMapper tJobMapper;
    @Autowired
    private TCtldevinfoMapper tCtldevinfoMapper;
    @Autowired
    private AerationParameterService aerationParameterService;

   

    @Override
    public List<TVentilation> getList(String vCfCode, String vDevKindCode) {
    	TVentilationExample tVentilationExample = new TVentilationExample();
    	tVentilationExample.setDistinct(true);
    	TVentilationExample.Criteria criteria = tVentilationExample.createCriteria();
        if (StringUtils.isNotEmpty(vCfCode)) {
            criteria.andVCfCodeEqualTo(vCfCode);
        }
        if (StringUtils.isNotEmpty(vDevKindCode)) {
        	criteria.andVDeviceCodeEqualTo(vDevKindCode);
        }
        tVentilationExample.setOrderByClause("v_update_time desc");
        return tVentilationMapper.selectByExample(tVentilationExample);
    }



  //新增
  	@Override
  	public void save(TCtldevinfo tCtldevinfo, String aerationTaskControl,Integer orgId) {
  		// TODO Auto-generated method stub
  		TVentilation tVentilation = new TVentilation(); //通风记录
  		TJob tJob = new TJob(); //通风作业
  		if(StringUtils.isNotBlank(aerationTaskControl) && tCtldevinfo!=null){
          	TJobApplication tJobApplication = (TJobApplication) JsonToObjectUtils.jsonToObject(aerationTaskControl, "com.chinaitop.depot.intelligent.ventilation.model.TJobApplication");
          	tVentilation.setvCfCode(tJobApplication.getvCfCode()); //仓房编码
          	tVentilation.setvPattern(tJobApplication.getVentilationMode()); //通风模式
          	tVentilation.setOperationId(tJobApplication.getAreationPlanNumber()); //计划编号
          	tVentilation.setOrgId(String.valueOf(orgId)); //orgid
          	tVentilation.setvDevName(tCtldevinfo.getVdevname()); //设备名称
  			tVentilation.setvDeviceCode(tCtldevinfo.getVdevkindcode());//设备类型
  			tVentilation.setvState(Integer.valueOf(tCtldevinfo.getvState())); //状态
  			tVentilation.setvDistinguishState(0); //通风
  			tVentilation.setvUpdateTime(new Date());
  			
  			if(tCtldevinfo.getvState().equals(1)){ //vState的状态是手动的状态  vStatue是系统里的  1是打开
  				String id = UuidUtils.getCode();
  				Date date1 = new Date();
  				tVentilation.setvStartTime(date1); //开始时间
  				tVentilation.setId(id);
  				
  				//通风记录表新增
  				tVentilationMapper.insertSelective(tVentilation);
  				/*//修改通风参数值设置
  				tCtldevinfo.setVstatue(tCtldevinfo.getVstatue().toString());
  				tCtldevinfo.setId(tCtldevinfo.getId());
  				tCtldevinfoMapper.updateByPrimaryKeySelective(tCtldevinfo);*/
  				//通风作业表修改状态
  				tJob.setTaskState(1); //正在进行通风
  				tJob.setTaskStartTime(date1);//开始时间
  				tJob.setAerationPlanNumber(tJobApplication.getAreationPlanNumber());
  				tJobMapper.updateByKey(tJob);
  			}
  			
  			if(tCtldevinfo.getvState().equals(0)){ //0是关闭
  				Date date = new Date();  
  				//记录表   
  				TVentilationExample example = new TVentilationExample();
  		    	TVentilationExample.Criteria criteria = example.createCriteria();
  		        criteria.andOperationIdEqualTo(tJobApplication.getAreationPlanNumber());
  		        List<TVentilation> list = tVentilationMapper.selectByExample(example);
  		        for (int j = 0; j < list.size(); j++) {
  					if(list.get(j).getvEndTime()==null && list.get(j).getvState()==1 && list.get(j).getvDeviceCode().equals(tCtldevinfo.getVdevkindcode())){
  						tVentilation.setvEndTime(date); //结束时间
  						tVentilation.setId(list.get(j).getId());
  						tVentilationMapper.updateByPrimaryKeySelective(tVentilation);  //通风记录表
  						
  						/*//修改通风参数值设置
  						tCtldevinfo.setVstatue(tCtldevinfo.getVstatue().toString());
  						tCtldevinfo.setId(tCtldevinfo.getId());
  						tCtldevinfoMapper.updateByPrimaryKeySelective(tCtldevinfo);*/
  						
  						//通风作业表修改状态
  						tJob.setTaskState(2); //结束通风
  						tJob.setTaskEndTime(date);//结束时间
  						tJob.setAerationPlanNumber(tJobApplication.getAreationPlanNumber());
  						tJobMapper.updateByKey(tJob);
  					}
  				}
  			}
  			
  		}
  	}

	//根据仓房和设备名称获取信息
	@Override
	public TVentilation getByCode(String vcfcode, String vdevname) {
		// TODO Auto-generated method stub
		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("vcfcode", vcfcode);
		modelMap.put("vdevname", vdevname);
		return tVentilationMapper.getByCode(modelMap);
	}



	@Transactional
    @Override
	public void insertBatch(List<TVentilation> ventilations) {
        tVentilationMapper.insertBatch(ventilations);
    }



	@Override
	public List<TVentilation> getByCodeAndTimes(List<String> cDcsCodes, List<Date> cTimes) {
		// TODO Auto-generated method stub
		TVentilationExample example = new TVentilationExample();
        TVentilationExample.Criteria criteria = example.createCriteria();
        if ((cDcsCodes != null && cDcsCodes.size() > 0) && (cTimes != null && cTimes.size() > 0)) {
            criteria.andVCfCodeIn(cDcsCodes);
            criteria.andVUpdateTimeIn(cTimes);
            List<TVentilation> tVentilations = tVentilationMapper.selectByExample(example);
            return tVentilations;
        }
        return null;
	}



}
