package com.steerinfo.ems.rttable.service.impl;

import com.steerinfo.ems.Utils.DateUtils;
import com.steerinfo.ems.rttable.mapper.RttableMapper;
import com.steerinfo.ems.rttable.model.Rttable;
import com.steerinfo.ems.rttable.service.IRttableService;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Rttable服务实现：
 * @author generator
 * @version 1.0-SNAPSHORT 2021-10-15 03:37
 * 类描述
 * 修订历史：
 * 日期：2021-10-15
 * 作者：generator
 * 参考：
 * 描述：Rttable服务实现
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@Service(value = "rttableService")
public class RttableServiceImpl extends BaseServiceImpl<Rttable, BigDecimal> implements IRttableService {
    private static final Logger logger = LoggerFactory.getLogger(RttableServiceImpl.class);
    @Autowired
    private RttableMapper rttableMapper;

    @Override
    protected IBaseMapper<Rttable, BigDecimal> getMapper() {
        return rttableMapper;
    }

    @Override
    public int dataGeneration() {

        int addSize = 0;
        int updateSize = 0;
        try {
            Rttable model;
            // emsWaterRealtimeMapper.selectByPrimaryKey("1");//.getWaterListByArmMeter();
            //List<EmsWaterRealtime> emsWaterRealtimeList = getWaterListByArmMeter();
            String clock = DateUtils.dateStr(new Date(),"yyyy-MM-dd HH:mm:ss");
            HashMap<String,Object> hashMap = new HashMap<String,Object>();
            hashMap.put("clock",clock);
            List<Rttable> rttableList = rttableMapper.selectPowerAltId();
            if(rttableList!=null && rttableList.size()>0) {
                StringBuffer sub = new StringBuffer();
                String table = "EMS_POWER";
                String[] tens;
                String tag;
                String sql;
                for (Rttable rttable : rttableList) {
                    addSize++;
                    tens = rttable.getNote().split("\\.");
                    tag = tens[1];
                    //多个表生成数据就填加table;
                    //table = tens[0];
                    //多个表生成数据就填加table;
                    sql = "," + tag + " = " + "'" + rttable.getRtvalue() + "'";
                    sub.append(sql);
                }
                hashMap.put("table", table);
                hashMap.put("sql", sub.toString());
                updateSize += rttableMapper.updatePowerMinutes(hashMap);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            logger.info("本次总共新增五分钟动力数据条数：" + addSize);
            logger.info("本次总共更新五分钟动力数据条数：" + updateSize);
        }
        return addSize+updateSize;
    }


    @Override
    public int getPowerTenMinutes() {
        int addSize = 0;
        int updateSize = 0;
        int i = 0;
        try {
            Rttable model;
            // emsWaterRealtimeMapper.selectByPrimaryKey("1");//.getWaterListByArmMeter();
            String clock1 = DateUtils.dateStr(new Date(),"yyyy-MM-dd HH:mm:ss");
            HashMap<String,Object> hashMap = new HashMap<String,Object>(2);
            hashMap.put("clock1",clock1);
            try {
                addSize += rttableMapper.insertPowerTenMinutes(hashMap);
            } catch (Exception e) {
                logger.error("主键已存在");
            } finally {
                StringBuffer sub = new StringBuffer();
                List<Rttable> rttableList = rttableMapper.selectPowerAltId();
                if(rttableList!=null && rttableList.size()>0) {
                    String table = "EMS_POWER_TIMING";
                    String[] tens;
                    String tag;
                    String sql;
                    for (Rttable rttable : rttableList) {
                        i++;
                        tens = rttable.getNote().split("\\.");
                        tag = tens[1];
                        //多个表生成数据就填加table;
                        //table = tens[0];
                        sql = tag + " = " + "'" + rttable.getRtvalue() + "', ";
                        sub.append(sql);
                    }
                    hashMap.put("table", table);
                    hashMap.put("sql", sub.toString());
                    updateSize += rttableMapper.updatePowerTenMinutes(hashMap);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            logger.info("本次总共新增动力十分钟数据条数：" + addSize);
            logger.info("本次总共更新动力十分钟数据条数：" + updateSize);
        }

        return addSize+updateSize;
    }
}
