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.ConstUtils;
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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
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 orgId,String vCfCode, Integer state, String searchStartDate, String searchEndDate, String showType) {
        TWarningThresholdHistoryExample example = new TWarningThresholdHistoryExample();
        TWarningThresholdHistoryExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(orgId)) {
            criteria.andOrgIdEqualTo(orgId);
        }
        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(searchEndDate)) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = null;
            try {
                date = dateFormat.parse(searchEndDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            criteria.andWTimeLessThanOrEqualTo(date);
        }
        criteria.andWWarningEqualTo(ConstUtils.ZERO);
        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) {
        TWarningThresholdHistoryExample example = new TWarningThresholdHistoryExample();
        TWarningThresholdHistoryExample.Criteria criteria = example.createCriteria();
        criteria.andVCfCodeEqualTo(vCfCode);
        criteria.andWTimeEqualTo(DateUtils.stringToData(wTime));
        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());
        tWarningThresholdHistoryMapper.updateByExample(history, example);
    }

    @Override
    public List<TWarningThresholdHistory> getAbnormalEquipmentList(String orgId, String vCfCode, String searchStartDate, String searchEndDate) {
        TWarningThresholdHistoryExample example = new TWarningThresholdHistoryExample();
        TWarningThresholdHistoryExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(orgId)) {
            criteria.andOrgIdEqualTo(orgId);
        }
        if (StringUtils.isNotBlank(vCfCode)) {
            criteria.andVCfCodeEqualTo(vCfCode);
        }
        if (StringUtils.isNotBlank(searchStartDate) && StringUtils.isNotBlank(searchEndDate)) {
            criteria.andWTimeBetween(DateUtils.stringToData(searchStartDate + DateUtils.startTime), DateUtils.stringToData(searchEndDate + DateUtils.endTime));
        }
        criteria.andWWarningEqualTo(ConstUtils.STR_TWO);
        example.setOrderByClause("w_time DESC");
        List<TWarningThresholdHistory> tWarningThresholdHistories = tWarningThresholdHistoryMapper.selectByExample(example);
        return tWarningThresholdHistories;
    }
}
