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

import com.alibaba.fastjson.JSON;
import com.chinaitop.depot.intelligent.grainsituation.mapper.TTestdataLayerMapper;
import com.chinaitop.depot.intelligent.grainsituation.mapper.TTestdataMapper;
import com.chinaitop.depot.intelligent.grainsituation.model.*;
import com.chinaitop.depot.intelligent.grainsituation.service.TemperatureRecordService;
import com.chinaitop.depot.intelligent.utils.DateUtils;
import com.chinaitop.depot.intelligent.utils.JsonToObjectUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@SuppressWarnings("all")
@Service
public class TemperatureRecordServiceImpl implements TemperatureRecordService {
    @Resource
    private TTestdataMapper tTestdataMapper;

    @Resource
    private TTestdataLayerMapper tTestdataLayerMapper;

    @Override
    public List<TTestdata> getList(String vCfCode, String vDatatime, String startTime, String endTime, String tType, String orgId) {
        TTestdataExample testdataExample = new TTestdataExample();
        testdataExample.setDistinct(true);
        TTestdataExample.Criteria criteria = testdataExample.createCriteria();
        if (StringUtils.isNotEmpty(vCfCode)) {
            criteria.andStorehouseEqualTo(vCfCode);
        }
        if (StringUtils.isNotEmpty(vDatatime)) {
            criteria.andTimeBetween(DateUtils.stringToData(vDatatime + DateUtils.startTime), DateUtils.stringToData(vDatatime + DateUtils.endTime));
        }
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            criteria.andTimeBetween(DateUtils.stringToData(startTime + DateUtils.startTime), DateUtils.stringToData(endTime + DateUtils.endTime));
        }
        if (StringUtils.isNotEmpty(tType)) {
            criteria.andIlqYqEqualTo(tType);
        }
        if (StringUtils.isNotEmpty(orgId)) {
            criteria.andOrgIdEqualTo(orgId);
        }
        testdataExample.setOrderByClause("time desc");
        return tTestdataMapper.selectByExample(testdataExample);
    }


    @Override
    public List<TTestdataLayer> findByHouseAndTime(String lqId, String vCfCode, String vDatatime, String orgId) {
        TTestdataLayerExample tTestdataLayerExample = new TTestdataLayerExample();
        tTestdataLayerExample.setDistinct(true);
        TTestdataLayerExample.Criteria criteria = tTestdataLayerExample.createCriteria();
        if (StringUtils.isNotBlank(lqId))
            criteria.andLqIdEqualTo(lqId);
        if (StringUtils.isNotBlank(vCfCode))
            criteria.andLhouseEqualTo(vCfCode);
        if (StringUtils.isNotBlank(vDatatime))
            criteria.andLtimeEqualTo(DateUtils.stringToData(vDatatime));
        if (StringUtils.isNotBlank(orgId))
            criteria.andOrgIdEqualTo(orgId);
        tTestdataLayerExample.setOrderByClause("layerNumber ASC");
        List<TTestdataLayer> tTestdata = tTestdataLayerMapper.selectByExample(tTestdataLayerExample);
        return tTestdata;
    }

    @Override
    public ArrayList<Object> findByHouseAndTimes(String lqId, String vCfCode, String vDatatime, boolean viewFlag) {
        TTestdata byId = tTestdataMapper.getById(lqId).get(0);
        String s = byId.gettPoints();
        ArrayList<Object> tP = JsonToObjectUtils.jsonToObjects(s, TPoints.class);
        ArrayList<TPoints> tPoints = Lists.newArrayList();
        tP.forEach(t -> {
            tPoints.add((TPoints) t);
        });
        ArrayList<Object> objects = Lists.newArrayList();
        if (tPoints.size() > 0) {
            Map<Integer, Map<Integer, Map<Integer, List<TPoints>>>> collect1 = Maps.newLinkedHashMap();
            if (viewFlag) // true: 表格数据
                collect1 = tPoints.stream().collect(Collectors.groupingBy(t -> Integer.parseInt(t.getY()), Collectors.groupingBy(t -> Integer.parseInt(t.getZ()), Collectors.groupingBy(t -> Integer.parseInt(t.getX())))));
            else // false: 3D三温图数据
                collect1 = tPoints.stream().collect(Collectors.groupingBy(t -> Integer.parseInt(t.getZ()), Collectors.groupingBy(t -> Integer.parseInt(t.getY()), Collectors.groupingBy(t -> Integer.parseInt(t.getX())))));
            // 拼接数据
            for (Map.Entry<Integer, Map<Integer, Map<Integer, List<TPoints>>>> entryG : collect1.entrySet()) {
                //每层数据
                Map<Integer, Map<Integer, List<TPoints>>> layerGroupV = entryG.getValue();
                for (Map.Entry<Integer, Map<Integer, List<TPoints>>> rows : layerGroupV.entrySet()) {
                    HashMap<Object, Object> objectObjectHashMap = Maps.newLinkedHashMap();
                    //每一行数据
                    Map<Integer, List<TPoints>> value = rows.getValue();
                    int a = 1;
                    objectObjectHashMap.put(a, entryG.getKey() + "-" + rows.getKey());
                    for (Map.Entry<Integer, List<TPoints>> orws : value.entrySet()) {
                        //列数据
                        List<TPoints> column = orws.getValue();
                        TPoints t = column.get(0);
                        objectObjectHashMap.put(++a, t.getTemp());
                    }
                    objects.add(objectObjectHashMap);
                }
            }
        }
        return objects;
    }

    @Override
    public List<TTestdata> getComparison(String vCfCodeLeft, String startTimeLeft, String endTimeLeft, String vCfCodeRight, String startTimeRight, String endTimeRight, String tType, String orgId) {
        TTestdataExample example = new TTestdataExample();
        example.setDistinct(true);
        TTestdataExample.Criteria criteria = example.createCriteria();
        // 左图查询条件
        if (StringUtils.isNotBlank(vCfCodeLeft)) {
            criteria.andStorehouseEqualTo(vCfCodeLeft);
        }
        // 右图查询条件
        if (StringUtils.isNotBlank(vCfCodeRight)) {
            criteria.andStorehouseEqualTo(vCfCodeRight);
        }
        // 时间条件
        if (StringUtils.isNotBlank(startTimeLeft) && StringUtils.isNotBlank(endTimeLeft)) {
            criteria.andTimeBetween(DateUtils.stringToData(startTimeLeft + DateUtils.startTime), DateUtils.stringToData(endTimeLeft + DateUtils.endTime));
        } else if (StringUtils.isNotBlank(startTimeRight) && StringUtils.isNotBlank(endTimeRight)) {
            criteria.andTimeBetween(DateUtils.stringToData(startTimeRight + DateUtils.startTime), DateUtils.stringToData(endTimeRight + DateUtils.endTime));
        } else {
            criteria.andTimeBetween(DateUtils.stringToData(DateUtils.formatDate(DateUtils.getDate()) + DateUtils.startTime), DateUtils.stringToData(DateUtils.formatDate(DateUtils.getDate()) + DateUtils.endTime));
        }
        if (StringUtils.isNotBlank(orgId))
            criteria.andOrgIdEqualTo(orgId);
        // 检测类型0测温，1粮油,2测水分
        criteria.andIlqYqEqualTo(tType);
        example.setOrderByClause("time desc");
        return tTestdataMapper.selectByExample(example);
    }

    @Transactional
    @Override
    public void insertBatch(List<TTestdata> testData) {
        tTestdataMapper.insertBatch(testData);
    }

    @Override
    public List<TTestdata> getByCodeAndTimes(List<String> cDcsCodes, List<Date> cTimes, List<String> lqTypes) {
        TTestdataExample example = new TTestdataExample();
        TTestdataExample.Criteria criteria = example.createCriteria();
        if ((cDcsCodes != null && cDcsCodes.size() > 0) && (cTimes != null && cTimes.size() > 0) && (lqTypes != null && lqTypes.size() > 0)) {
            criteria.andStorehouseIn(cDcsCodes);
            criteria.andTimeIn(cTimes);
            criteria.andIlqYqIn(lqTypes);
            List<TTestdata> tTestdata = tTestdataMapper.selectByExample(example);
            return tTestdata;
        }
        return null;
    }

    @Override
    public String getOrgId(String cfCode) {
        return tTestdataMapper.getOrgId(cfCode);
    }

    @Override
    public TTestdata getById(String id) {
        TTestdataExample example = new TTestdataExample();
        TTestdataExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        TTestdata tTestdata = tTestdataMapper.selectByExample(example).get(0);
        return tTestdata;
    }

    @Override
    public String getRemoteUrl(String orgId) {
        return tTestdataMapper.getRemoteUrl(orgId);
    }

}
