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

import com.chinaitop.depot.intelligent.basicdata.mapper.TQtdevinfoMapper;
import com.chinaitop.depot.intelligent.basicdata.model.TQtdevinfo;
import com.chinaitop.depot.intelligent.basicdata.model.TQtdevinfoExample;
import com.chinaitop.depot.intelligent.common.ConstantClass;
import com.chinaitop.depot.intelligent.factoryObject.factoryInterface.StringFactory;
import com.chinaitop.depot.intelligent.grainsituation.mapper.TDcsdataMapper;
import com.chinaitop.depot.intelligent.grainsituation.mapper.TGasDataMapper;
import com.chinaitop.depot.intelligent.grainsituation.mapper.TQtdevinfoValueMapper;
import com.chinaitop.depot.intelligent.grainsituation.model.*;
import com.chinaitop.depot.intelligent.grainsituation.service.GasConfigService;
import com.chinaitop.depot.intelligent.grainsituation.service.GasDetectionService;
import com.chinaitop.depot.intelligent.utils.DateUtils;
import com.chinaitop.depot.intelligent.utils.ParameterUtil;
import com.chinaitop.depot.intelligent.utils.UuidUtils;
import com.google.common.collect.Lists;
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.*;

@Service("gasDetectionService")
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);
    }

    // --------------------------衡水测气列表-------------------------
    @Resource
    private TGasDataMapper gasDataMapper;
    @Resource
    private StringFactory stringFactory;


    @Override
    public List<TGasData> getGasList(String storeCode, String startTime, String endTime) {
        TGasDataExample example = new TGasDataExample();
        TGasDataExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(storeCode))
            criteria.andStoreCodeEqualTo(storeCode);
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            Date startDate = DateUtils.stringToData(stringFactory.joinString(1, startTime, DateUtils.startTime));
            Date endDate = DateUtils.stringToData(stringFactory.joinString(1, endTime, DateUtils.endTime));
            criteria.andResultTimeBetween(startDate, endDate);
        }
        example.setOrderByClause("result_time desc");
        return gasDataMapper.selectByExample(example);
    }

    @Override
    public List<TGasData> getGasCheckGroupList(String storeCode) {
        return gasDataMapper.getGasCheckGroupList(storeCode);
    }

    @Resource
    private GasConfigService gasConfigService;

    @Override
    public void saveGasData(List<List<Float>> objects1, String ip) {
        if (objects1.size() > 0 && StringUtils.isNotBlank(ip)) {
            // 根据ip获取仓房信息
            TGasConfig gasConfig = gasConfigService.getList(null, ip).getList().get(ConstantClass.TYPE_ZRO);
            String orgId = gasConfig.getOrgId();
            String storeCode = gasConfig.getStoreCode();
            // 数据list
            ArrayList<TGasData> gases = Lists.newArrayList();
            for (List<Float> data : objects1) {
                TGasData gasData = new TGasData();
                gasData.setNowPh3(data.get(0)); // 磷化氢实时
                gasData.setNowO2(data.get(1)); // 氧气实时
                gasData.setSouthO2(data.get(2)); // 南检测结果 氧气
                gasData.setSouthPh3(data.get(3)); // 南检测结果 磷化氢
                gasData.setEastO2(data.get(4)); // 东检测结果 氧气
                gasData.setEastPh3(data.get(5)); // 东检测结果 磷化氢
                gasData.setNorthO2(data.get(6)); // 北检测结果 氧气
                gasData.setNorthPh3(data.get(7)); // 北检测结果 磷化氢
                gasData.setWestO2(data.get(8)); // 西检测结果 氧气
                gasData.setWestPh3(data.get(9)); // 西检测结果 磷化氢
                gasData.setInO2(data.get(10)); // 中检测结果 氧气
                gasData.setInPh3(data.get(11)); // 中检测结果 磷化氢
                gases.add(gasData);
            }
            // 保存对象
            TGasData saveOnlyGasData = new TGasData();
            saveOnlyGasData.setId(UuidUtils.getCode()); // id
            saveOnlyGasData.setOrgId(orgId); // 组织机构编码
            saveOnlyGasData.setStoreCode(storeCode); // 仓房编码
            saveOnlyGasData.setResultTime(DateUtils.getDate());
            // 计算平均值
            float avgNowO2 = (float) gases.stream().mapToDouble(TGasData::getNowO2).average().getAsDouble();
            float avgNowPh3 = (float) gases.stream().mapToDouble(TGasData::getNowPh3).average().getAsDouble();
            float avgSouthO2 = (float) gases.stream().mapToDouble(TGasData::getSouthO2).average().getAsDouble();
            float avgSouthPh3 = (float) gases.stream().mapToDouble(TGasData::getSouthPh3).average().getAsDouble();
            float avgEastO2 = (float) gases.stream().mapToDouble(TGasData::getEastO2).average().getAsDouble();
            float avgEastPh3 = (float) gases.stream().mapToDouble(TGasData::getEastPh3).average().getAsDouble();
            float avgNorthO2 = (float) gases.stream().mapToDouble(TGasData::getNorthO2).average().getAsDouble();
            float avgNorthPh3 = (float) gases.stream().mapToDouble(TGasData::getNorthPh3).average().getAsDouble();
            float avgWestO2 = (float) gases.stream().mapToDouble(TGasData::getWestO2).average().getAsDouble();
            float avgWestPh3 = (float) gases.stream().mapToDouble(TGasData::getWestPh3).average().getAsDouble();
            float avgInO2 = (float) gases.stream().mapToDouble(TGasData::getInO2).average().getAsDouble();
            float avgInPh3 = (float) gases.stream().mapToDouble(TGasData::getInPh3).average().getAsDouble();
            saveOnlyGasData.setNowO2(avgNowO2);
            saveOnlyGasData.setNowPh3(avgNowPh3);
            saveOnlyGasData.setSouthO2(avgSouthO2);
            saveOnlyGasData.setSouthPh3(avgSouthPh3);
            saveOnlyGasData.setEastO2(avgEastO2);
            saveOnlyGasData.setEastPh3(avgEastPh3);
            saveOnlyGasData.setNorthO2(avgNorthO2);
            saveOnlyGasData.setNorthPh3(avgNorthPh3);
            saveOnlyGasData.setWestO2(avgWestO2);
            saveOnlyGasData.setWestPh3(avgWestPh3);
            saveOnlyGasData.setInO2(avgInO2);
            saveOnlyGasData.setInPh3(avgInPh3);
            // 保存
            gasDataMapper.insert(saveOnlyGasData);
        }
    }

}
