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

import com.steerinfo.auth.utils.JwtUtil;
import com.steerinfo.ems.Utils.DateUtils;
import com.steerinfo.ems.emswaterrealtime.mapper.EmsWaterRealtimeMapper;
import com.steerinfo.ems.emswaterrealtime.model.EmsWaterRealtime;
import com.steerinfo.ems.emswaterrealtime.service.IEmsWaterRealtimeService;
import com.steerinfo.ems.formula.model.Formula;
import com.steerinfo.ems.formula.service.IFormulaService;
import com.steerinfo.ems.ifmesemsswapfile.service.impl.IfMesEmsSwapfileServiceImpl;
import com.steerinfo.ems.trmcalpoint.model.TRmCalpoint;
import com.steerinfo.framework.datasource.DataSourceKey;
import com.steerinfo.framework.datasource.TargetDataSource;
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.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * EmsWaterRealtime服务实现：
 * @author generator
 * @version 1.0-SNAPSHORT 2021-08-24 07:52
 * 类描述
 * 修订历史：
 * 日期：2021-08-24
 * 作者：generator
 * 参考：
 * 描述：EmsWaterRealtime服务实现
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@Service(value = "emsWaterRealtimeService")
public class EmsWaterRealtimeServiceImpl extends BaseServiceImpl<EmsWaterRealtime, String> implements IEmsWaterRealtimeService {

    private static final Logger logger = LoggerFactory.getLogger(IfMesEmsSwapfileServiceImpl.class);

    @Autowired
    private EmsWaterRealtimeMapper emsWaterRealtimeMapper;

    @Override
    protected IBaseMapper<EmsWaterRealtime, String> getMapper() {
        return emsWaterRealtimeMapper;
    }

    List<EmsWaterRealtime> emsWaterRealtimeList;

    @Override
    @TargetDataSource(dataSourceKey = DataSourceKey.DB_OTHER)
    public List<EmsWaterRealtime> getWaterListByArmMeter() {
           // emsWaterRealtimeMapper.selectByPrimaryKey("1");//.getWaterListByArmMeter();
        //List<EmsWaterRealtime>
                emsWaterRealtimeList = emsWaterRealtimeMapper.getWaterListByArmMeter();
        return emsWaterRealtimeList;
    }

    @Override
    public int dataGeneration() {

        int addSize = 0;
        int updateSize = 0;
        try {
            EmsWaterRealtime model;
            // emsWaterRealtimeMapper.selectByPrimaryKey("1");//.getWaterListByArmMeter();
            //List<EmsWaterRealtime> emsWaterRealtimeList = getWaterListByArmMeter();
            for (EmsWaterRealtime emsWaterRealtime : emsWaterRealtimeList){
                emsWaterRealtime.setReadtime(DateUtils.dateStr(new Date(),"yyyy-MM-dd hh:mm:ss"));
                model = emsWaterRealtimeMapper.selectByPrimaryKey(emsWaterRealtime.getMeterid());
                if(model !=null){
                    emsWaterRealtimeMapper.updateByPrimaryKey(emsWaterRealtime);
                    updateSize+=1;
                    continue;
                }else {
                    addSize+= emsWaterRealtimeMapper.insert(emsWaterRealtime);
                }
            }
            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<EmsWaterRealtime> emsWaterRealtimeList = emsWaterRealtimeMapper.getWaterTenMinutes();
            StringBuffer sub = new StringBuffer();
            String table="EMS_WATER";
            for (EmsWaterRealtime emsWaterRealtime : emsWaterRealtimeList){
                String[] tens = emsWaterRealtime.getmMaterial().split("\\.");
                String tag = tens[1];
                //多个表生成数据就填加table;
                table = tens[0];
                String sql = ","+tag + " = " + "'" + emsWaterRealtime.getSumvalue() + "'";
                sub.append(sql);
            }
            hashMap.put("table",table);
            hashMap.put("sql",sub.toString());
            updateSize += emsWaterRealtimeMapper.updateWaterMinutes(hashMap);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        logger.info("本次总共新增水数据条数：" + addSize);
        logger.info("本次总共更新水数据条数：" + updateSize);
        return addSize+updateSize;
    }


    @Override
    public void getWaterTenMinutes() {
        int addSize = 0;
        int updateSize = 0;
        int i = 0;
        try {
            EmsWaterRealtime model;
            // emsWaterRealtimeMapper.selectByPrimaryKey("1");//.getWaterListByArmMeter();
            String clock1 = DateUtils.dateStr(new Date(),"yyyy-MM-dd hh:mm:ss");
            String clock2 = DateUtils.dateStr(new Date(),"yyyy-MM-dd hh:mm:ss");
            HashMap<String,Object> hashMap = new HashMap<String,Object>();
            hashMap.put("clock1",clock1);
            hashMap.put("clock2",clock2);
            addSize += emsWaterRealtimeMapper.insertWaterTenMinutes(hashMap);
            List<EmsWaterRealtime> emsWaterRealtimeList = emsWaterRealtimeMapper.getWaterTenMinutes();
            StringBuffer sub = new StringBuffer();
            String table="EMS_WATER_TIMING";
            for (EmsWaterRealtime emsWaterRealtime : emsWaterRealtimeList){
                i++;
                String[] tens = emsWaterRealtime.getmType().split("\\.");
                String tag = tens[1];
                //多个表生成数据就填加table;
                table = tens[0];
                String sql = ","+tag + " = " + "'" + emsWaterRealtime.getSumvalue() + "'";
                sub.append(sql);
            }
            clock2 = DateUtils.dateStr(new Date(),"yyyy-MM-dd hh:mm:ss");
            hashMap.put("table",table);
            hashMap.put("clock2",clock2);
            hashMap.put("sql",sub.toString());
            updateSize += emsWaterRealtimeMapper.updateWaterTenMinutes(hashMap);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        logger.info("本次总共新增水数据条数：" + addSize);
        logger.info("本次总共更新水数据条数：" + updateSize);
        //return addSize+updateSize;
    }

    @Autowired
    IFormulaService formulaService;

    @Override
    public void getcalpoint() {
        List<TRmCalpoint> tRmCalpoints = emsWaterRealtimeMapper.getcalpoint();
        for(TRmCalpoint model : tRmCalpoints){
            Formula formula = new Formula();
            formula.setCode(model.getItemid());
            String itemdesc = model.getItemdesc(); // 项目描述
            formula.setDescription(itemdesc!=null && !itemdesc.trim().isEmpty() ? itemdesc : model.getTagAlias());
            // formula.setClock(DateUtils.getCurrentTime("yyyyMM"));
            formula.setClock("202001");
            formula.setIsavailable("1");
            formula.setRevisetime(DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss"));
            formula.setTablename("T_RM_CALPOINT_VALUE");
            formula.setSourcecode("R(T_RM_CALPOINT_VALUE," + model.getItemid() + ")");
            formula.setRevisor(JwtUtil.getUseridByToken());

            formulaService.add(formula);
        }

    }
}
