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

import com.chinaitop.depot.intelligent.basicdata.mapper.TDcsqtinfoMapper;
import com.chinaitop.depot.intelligent.basicdata.mapper.TQtdevinfoMapper;
import com.chinaitop.depot.intelligent.basicdata.model.TDcsqtinfo;
import com.chinaitop.depot.intelligent.basicdata.model.TDcsqtinfoExample;
import com.chinaitop.depot.intelligent.basicdata.model.TQtdevinfo;
import com.chinaitop.depot.intelligent.basicdata.model.TQtdevinfoExample;
import com.chinaitop.depot.intelligent.grainsituation.mapper.TDcsdataMapper;
import com.chinaitop.depot.intelligent.grainsituation.mapper.TQtdevinfoValueMapper;
import com.chinaitop.depot.intelligent.grainsituation.model.TDcsdata;
import com.chinaitop.depot.intelligent.grainsituation.model.TDcsdataExample;
import com.chinaitop.depot.intelligent.grainsituation.model.TQtdevinfoValue;
import com.chinaitop.depot.intelligent.grainsituation.model.TQtdevinfoValueExample;
import com.chinaitop.depot.intelligent.grainsituation.service.GasDetectionService;
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 org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GasDetectionServiceImpl implements GasDetectionService {
    @Resource
    private TQtdevinfoValueMapper tQtdevinfoValueMapper;

    @Resource
    private TDcsdataMapper tDcsdataMapper;

    @Resource
    private TQtdevinfoMapper tQtdevinfoMapper;

    @Override
    public List getList(String vCfCode, String startTime, String endTime,String id) {
        Map<String, String> map = Maps.newHashMap();
        if (StringUtils.isNotBlank(vCfCode)) {
            map.put("vCfCode", vCfCode);
        }
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            map.put("searchStartDate", startTime + DateUtils.startTime);
            map.put("searchEndDate", endTime + DateUtils.endTime);
        }
        if (ParameterUtil.isnotnull(id)) {
            map.put("id", "("+id+")");
        }
        return tQtdevinfoValueMapper.getByCfCdoe(map);
    }

    @Override
    public List<TQtdevinfo> getByCode(List<String> code) {
        TQtdevinfoExample example = new TQtdevinfoExample();
        TQtdevinfoExample.Criteria criteria = example.createCriteria();
        if (code != null) {
            criteria.andVdevcodeIn(code);
        }
        List<TQtdevinfo> tDcsqtinfos = tQtdevinfoMapper.selectByExample(example);
        return tDcsqtinfos;
    }

    @Override
    public Map<String, Map<String, Object>> getByCfCdoes(Map<String, Object> mapDate) {
        List<Map<String, Object>> byCfCdoes = tQtdevinfoValueMapper.getByCfCdoes(mapDate);
        if (byCfCdoes.size()>0){
            HashMap<String, Map<String, Object>> map = Maps.newHashMap();
            for (Map<String, Object> objectMap : byCfCdoes){
                map.put(objectMap.get("vCfCode").toString(),objectMap);
            }
            return map;
        }
        return null;
    }

    @Override
    public List<TDcsdata> getByCodeAndTimes(List<String> codes, List<Date> times) {
        TDcsdataExample example = new TDcsdataExample();
        TDcsdataExample.Criteria criteria = example.createCriteria();
        if ((codes != null && codes.size() > 0) && (times != null && times.size() > 0)) {
            criteria.andVDcsCodeIn(codes);
            criteria.andVUpdateTimeIn(times);
            List<TDcsdata> tDcsdata = tDcsdataMapper.selectByExample(example);
            return tDcsdata;
        }
        return null;
    }

    @Transactional
    @Override
    public void insertBatch(List<TDcsdata> tDcsdata) {
        tDcsdataMapper.insertBatch(tDcsdata);
    }

    @Override
    public List getListGroup(String vCfCode) {
        return tQtdevinfoValueMapper.getByCfCdoeGroup(vCfCode);
    }
}
