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


import com.chinaitop.depot.intelligent.supervise.mapper.TDepotStyleMapper;
import com.chinaitop.depot.intelligent.supervise.model.TDepotStyle;
import com.chinaitop.depot.intelligent.supervise.model.TDepotStyleExample;
import com.chinaitop.depot.intelligent.supervise.service.depotStyleService;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;

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

@Service
public class depotStyleServiceImpl implements depotStyleService {

    @Resource
    private TDepotStyleMapper depotStyleMapper;

    @Override
    public void addPoint(TDepotStyle depotStyle) {
        depotStyleMapper.insert(depotStyle);
    }
    @Override
    public void deletePoint(TDepotStyle style) {
        TDepotStyleExample example = new TDepotStyleExample();
        TDepotStyleExample.Criteria criteria= example.createCriteria();
        criteria.andTypeEqualTo(style.getType());
        criteria.andIndexidEqualTo(style.getIndexid());
        depotStyleMapper.deleteByExample(example);
    }
    @Override
    public List<TDepotStyle> selectPoint(TDepotStyleExample example) {
        return depotStyleMapper.selectByExampleWithBLOBs(example);
    }

    @Override
    public List<Map<String,Object>> getGrainsituation(Map<String,Object> map) {
        return depotStyleMapper.selectByGrainSituaiton(map);
    }

    @Override
    public List<Map<String, Object>> getQT(Map<String, Object> map) {
        return depotStyleMapper.selectByQT(map);
    }

    @Override
    public List<Map<String, Object>> getInsect(Map<String, Object> map) {
        return depotStyleMapper.selectByInsect(map);
    }

    @Override
    public List<Map<String, Object>> getWarning(Map<String, Object> map) {
        List cfCodes = Lists.newArrayList();
        List times = Lists.newArrayList();

        map.put("type","2");
        List<Map<String, Object>> grains = depotStyleMapper.selectByGrainSituaiton(map);
        if (grains.size()>0){
            Map<String, Object> grain;
            for (int i=0;i<grains.size();i++){
                grain = grains.get(i);
                if (grain.get("vCfCode") != null && grain.get("time") != null){
                    cfCodes.add(grain.get("vCfCode"));
                    times.add(grain.get("time"));
                }
            }
        }
        map.put("type","1");
        List<Map<String, Object>> qts = depotStyleMapper.selectByQT(map);
        if (qts.size()>0){
            Map<String, Object> qt;
            for (int i=0;i<qts.size();i++){
                qt = qts.get(i);
                if (qt.get("vCfCode") != null && qt.get("time") != null){
                    cfCodes.add(qt.get("vCfCode"));
                    times.add(qt.get("time"));
                }
            }
        }
        map.put("type","3");
        List<Map<String, Object>> insects = depotStyleMapper.selectByInsect(map);
        if (insects.size()>0){
            Map<String, Object> insect;
            for (int i=0;i<insects.size();i++){
                insect = insects.get(i);
                if (insect.get("vCfCode") != null && insect.get("time") != null){
                    cfCodes.add(insect.get("vCfCode"));
                    times.add(insect.get("time"));
                }
            }
        }
        if (times.size() > 0 ){
            map.put("times",times);
        }
        if(cfCodes.size() >0){
            map.put("cfCodes",cfCodes);
        }
        map.put("type","4");
        return depotStyleMapper.selectByWarning(map);
    }
}
