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

import com.chinaitop.depot.intelligent.grainsituation.mapper.TLqdataMapper;
import com.chinaitop.depot.intelligent.grainsituation.mapper.TTestdataMapper;
import com.chinaitop.depot.intelligent.grainsituation.model.*;
import com.chinaitop.depot.intelligent.grainsituation.service.GasDetectionService;
import com.chinaitop.depot.intelligent.grainsituation.service.GrainCheckAnalyzeService;
import com.chinaitop.depot.intelligent.grainsituation.service.InsectPestDetectionService;
import com.chinaitop.depot.intelligent.grainsituation.service.TWarningThresholdHistoryService;
import com.chinaitop.depot.intelligent.utils.DateUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author qingsong.han
 * @description: 粮情检查分析
 * @create 2018-12-16 17:01
 */
@Service
public class GrainCheckAnalyzeServiceImpl implements GrainCheckAnalyzeService {

    @Resource
    TLqdataMapper tLqdataMapper;

    @Resource
    private TTestdataMapper tTestdataMapper;

    @Resource
    private GasDetectionService gasDetectionService;

    @Resource
    private InsectPestDetectionService insectPestDetectionService;

    @Resource
    private GrainCheckAnalyzeService grainCheckAnalyzeService;

    @Resource
    private TWarningThresholdHistoryService tWarningThresholdHistoryService;

    /**
     * 检测时间查询列表
     *
     * @param vDataTime
     * @return
     */
    @Override
    public List<TLqdata> getList(String vDataTime) {
        TLqdataExample example = new TLqdataExample();
        TLqdataExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(vDataTime)) {
            criteria.andVDataTimeEqualTo(DateUtils.stringToData(vDataTime));
        }
        example.setOrderByClause("v_data_time DESC");
        return tLqdataMapper.selectByExample(example);
    }

    @Override
    public Map<String, TTestdata> getByCfCdoes(Map<String, Object> mapDate) {
        List<TTestdata> byCfCdoes = tTestdataMapper.getByCfCdoes(mapDate);
        if (byCfCdoes.size() > 0) {
            HashMap<String, TTestdata> map = Maps.newHashMap();
            for (TTestdata t : byCfCdoes) {
                map.put(t.getStorehouse(), t);
            }
            return map;
        }
        return null;
    }

    @Override
    public Map<String, Object> getAllTesting(Map<String, Object> mapDate) {
        HashMap<String, Object> map = Maps.newHashMap();
        ArrayList<String> cfCodes = Lists.newArrayList();
        ArrayList<Date> times = Lists.newArrayList();
        //获取粮温数据
        Map<String, TTestdata> grain = grainCheckAnalyzeService.getByCfCdoes(mapDate);
        if (grain != null) {
            map.put("grain", grain);
            for (TTestdata value : grain.values()) {
                cfCodes.add(value.getStorehouse());
                times.add(value.getTime());
            }
        }
        //获取气体数据
        Map<String, Map<String, Object>> gas = gasDetectionService.getByCfCdoes(mapDate);
        if (gas != null) {
            map.put("gas", gas);
            for (Object value : gas.values()) {
                Map<String, Object> v = (Map<String, Object>) value;
                cfCodes.add(v.get("vCfCode").toString());
                times.add(DateUtils.stringToData(v.get("vValueTime").toString()));
            }
        }
        //获取虫害数据
        Map<String, Map<String, Object>> insect = insectPestDetectionService.getByCfCodes(mapDate);
        if (insect != null) {
            map.put("insect", insect);
            for (Object value : insect.values()) {
                Map<String, Object> v = (Map<String, Object>) value;
                cfCodes.add(v.get("vCfCode").toString());
                times.add(DateUtils.stringToData(v.get("vUpdateTime").toString()));
            }
        }

        //获取报警阈值
        Map<String, List<TWarningThresholdHistory>> cfCdoeAndTimes = tWarningThresholdHistoryService.getCfCdoeAndTimes(cfCodes, times);
        if (cfCdoeAndTimes != null) {
            map.put("warning", cfCdoeAndTimes);
        }
        return map;
    }

    /**
     * 时实数据查询列表,查询最新数据
     * @param storehouse
     * @return
     */
    @Override
    public List<TTestdata> getNewest(String storehouse) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(storehouse))
            map.put("storehouse",storehouse);
        return tTestdataMapper.getNewest(map);
    }

    @Override
    public List<TTestdata> getByCfCode(String storehouse) {
        TTestdataExample example = new TTestdataExample();
        TTestdataExample.Criteria criteria = example.createCriteria();
        criteria.andStorehouseEqualTo(storehouse);
        example.setOrderByClause("time desc");
        List<TTestdata> tTestdata = tTestdataMapper.selectByExample(example);
        return tTestdata;
    }

}
