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

import com.chinaitop.depot.intelligent.grainsituation.mapper.TWarningThresholdHistoryMapper;
import com.chinaitop.depot.intelligent.grainsituation.model.TWarningThresholdHistory;
import com.chinaitop.depot.intelligent.grainsituation.model.TWarningThresholdHistoryExample;
import com.chinaitop.depot.intelligent.grainsituation.service.TWarningThresholdHistoryService;
import com.chinaitop.depot.intelligent.utils.DateUtils;
import com.chinaitop.depot.intelligent.utils.ParameterUtil;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TWarningThresholdHistoryServiceImpl implements TWarningThresholdHistoryService {
    @Resource
    private TWarningThresholdHistoryMapper tWarningThresholdHistoryMapper;

    @Override
    public List<TWarningThresholdHistory> getList(String vCfCode, Integer state, String searchStartDate, String searchEndDate, String showType, String wName,Integer orgId) {
        TWarningThresholdHistoryExample example = new TWarningThresholdHistoryExample();
        TWarningThresholdHistoryExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(vCfCode)){
            criteria.andVCfCodeEqualTo(vCfCode);
        }
        if (state != null){
            criteria.andWStateEqualTo(state);
        }
        if(ParameterUtil.isnotnull(showType) && ParameterUtil.isequal(showType,"CH")){//虫害
            criteria.andWModuleEqualTo("6973");
        }else if(ParameterUtil.isnotnull(showType) && ParameterUtil.isequal(showType,"QT")){//气体
            criteria.andWModuleEqualTo("6971");
        }else if(ParameterUtil.isnotnull(showType) && ParameterUtil.isequal(showType,"WD")){//温度
            criteria.andWModuleEqualTo("6972");
        }
        if (StringUtils.isNotBlank(searchStartDate) && StringUtils.isNotBlank(searchEndDate)){
            criteria.andWTimeBetween(DateUtils.stringToData(searchStartDate+DateUtils.startTime),DateUtils.stringToData(searchEndDate+DateUtils.endTime));
        }
        if (StringUtils.isNotBlank(wName)) {
            criteria.andWNameEqualTo(wName);
        }
        if(ParameterUtil.isnotnull(orgId)){
            criteria.andOrgIdEqualTo(orgId+"");
        }
        criteria.andWWarningEqualTo("0");
        example.setOrderByClause("w_time DESC");
        List<TWarningThresholdHistory> tWarningThresholdHistories = tWarningThresholdHistoryMapper.selectByExample(example);
        return tWarningThresholdHistories;
    }

    @Override
    public Map<String, List<TWarningThresholdHistory>> getCfCdoeAndTimes(List cfcodes, List times) {
        TWarningThresholdHistoryExample example = new TWarningThresholdHistoryExample();
        TWarningThresholdHistoryExample.Criteria criteria = example.createCriteria();
        if (cfcodes != null){
            criteria.andVCfCodeIn(cfcodes);
        }
        if (times!= null){
            criteria.andWTimeIn(times);
        }
        example.setDistinct(true);
        List<TWarningThresholdHistory> tWarningThresholdHistories = tWarningThresholdHistoryMapper.selectByExample(example);
        Map<String, List<TWarningThresholdHistory>> collect = tWarningThresholdHistories.stream().collect(Collectors.groupingBy(t -> t.getvCfCode()));
        return collect;
    }

    /**
     * 测温记录检测概览查询
     * @param vCfCode
     * @param wTime
     * @param wMode
     * @return
     */
    @Override
    public List<TWarningThresholdHistory> getWarningInfoList(String vCfCode, String wTime, String wMode,Integer orgId) {
        TWarningThresholdHistoryExample example = new TWarningThresholdHistoryExample();
        TWarningThresholdHistoryExample.Criteria criteria = example.createCriteria();
        criteria.andVCfCodeEqualTo(vCfCode);
        criteria.andWTimeEqualTo(DateUtils.stringToData(wTime));
        if(ParameterUtil.isnotnull(orgId)){
            criteria.andOrgIdEqualTo(orgId+"");
        }

        tWarningThresholdHistoryMapper.selectByExample(example);
        return tWarningThresholdHistoryMapper.selectByExample(example);
    }

    @Override
    public void updateById(TWarningThresholdHistory history) {
        TWarningThresholdHistoryExample example = new TWarningThresholdHistoryExample();
        TWarningThresholdHistoryExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(history.getId());
        if(ParameterUtil.isnotnull(history.getOrgId())){
            criteria.andOrgIdEqualTo(history.getOrgId());
        }
        tWarningThresholdHistoryMapper.updateByExample(history,example);
    }
}
