package com.gyee.alarm.service.auto.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyee.alarm.mapper.auto.AlarmTsMapper;
import com.gyee.alarm.model.auto.AlarmTs;
import com.gyee.alarm.model.vo.*;
import com.gyee.alarm.service.auto.IAlarmTsService;
import com.gyee.alarm.util.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

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


@Service
public class AlarmTsServiceImpl extends ServiceImpl<AlarmTsMapper, AlarmTs> implements IAlarmTsService {

    @Resource
    private AlarmTsMapper alarmTsMapper;


    @Override
    public void createSuperTable(String type) {

        alarmTsMapper.createAlarmSuperTable(type);

    }

    @Override
    public void dropSuperTable(String type) {

        alarmTsMapper.dropSuperTable(type);

    }
    @Override
    public int createTable(String tbName, String alarmid, String alarmtype,
                           String characteristic, String components,
                           String description,
                           String deviceid, String devicename,
                           String devicetype, Boolean enabled,
                           String lineid, String linename,
                           String modelId, String projectid,
                           String projectname, Integer rank,
                           Boolean resettable, String stationid,
                           String stationname, String subcomponents,
                           String suffix, String tagid,
                           Integer triggertype, String uniformcode, String superTableName,
                          String name,String nemCode, String faultCause, String resolvent,String faultType,String aname


    ) {
        return alarmTsMapper.createTable(tbName, alarmid, alarmtype,
                characteristic, components,
                description,
                deviceid, devicename,
                devicetype, enabled,
                lineid, linename,
                modelId, projectid,
                projectname, rank,
                resettable, stationid,
                stationname, subcomponents,
                suffix, tagid,
                triggertype, uniformcode,superTableName,   name, nemCode,  faultCause,  resolvent,faultType,aname);
    }

    @Override
    public int updateTable(String tbName, String alarmid, String alarmtype,
                           String characteristic, String components,
                           String description,
                           String deviceid, String devicename,
                           String devicetype, Boolean enabled,
                           String lineid, String linename,
                           String modelId, String projectid,
                           String projectname, Integer rank,
                           Boolean resettable, String stationid,
                           String stationname, String subcomponents,
                           String suffix, String tagid,
                           Integer triggertype, String uniformcode, String superTableName,
                           String name,String nemCode, String faultCause, String resolvent,String faultType,String aname


    ) {
        return alarmTsMapper.updateTable(tbName, alarmid, alarmtype,
                characteristic, components,
                description,
                deviceid, devicename,
                devicetype, enabled,
                lineid, linename,
                modelId, projectid,
                projectname, rank,
                resettable, stationid,
                stationname, subcomponents,
                suffix, tagid,
                triggertype, uniformcode,superTableName,   name, nemCode,  faultCause,  resolvent,faultType,aname);
    }

    @Override
    public int createTable(AlarmVo vo) {
        return alarmTsMapper.createTable(vo);
    }


    @Override
    public int insertOne(AlarmTs one) {
        return alarmTsMapper.insertOne(one);
    }


    @Override
    public void insertList(List<AlarmTag>  alarmTags )  {

        if(StringUtils.notEmp(alarmTags) && !alarmTags.isEmpty())
        {

            createSql(alarmTags);
        }


    }
    @Override
    public void createSql(List<AlarmTag>  alarmTags )   {

        StringBuilder sb = new StringBuilder("INSERT INTO ");

        int i=0;
        for (AlarmTag tag:alarmTags) {

            String tableName = tag.getId();
            StringBuilder sub_sb = new StringBuilder(tableName);

            sub_sb.append( " values ");
            sub_sb.append(" (");
            sub_sb.append(tag.getTs()).append(",").append(tag.getVal().intValue()).append(",").append(tag.getConfirmed()).append(",")
                    .append(tag.getEndts()).append(",").append(tag.getTimeLong());
            sub_sb.append(" ) ");
            i++;
            if (i==100) {
                alarmTsMapper.insertList(String.valueOf(sb));
                sb = new StringBuilder("INSERT INTO ");
                i=0;
            }
            sb.append(sub_sb);

        }

        if(i!=0)
        {
            alarmTsMapper.insertList(String.valueOf(sb));
        }

    }




    @Override
    public List<AlarmVo> findTags(String stbaleName)
    {
        List<AlarmVo> ls=new ArrayList<>();
        if(StringUtils.notEmp(stbaleName))
        {
           ls= alarmTsMapper.findTags(stbaleName);
        }

        return ls;
    }
    @Override
    public List<AlarmTsVo> selectByGroup(String superTableName, String stationid, long begin, long end)
    {
        List<AlarmTsVo> ls=new ArrayList<>();
        if(StringUtils.notEmp(superTableName) && StringUtils.notEmp(stationid) && StringUtils.notEmp(begin) && StringUtils.notEmp(end))
        {
            ls= alarmTsMapper.selectByGroup(superTableName, stationid, begin, end);
        }

        return ls;
    }

    @Override
    public   List<AlarmVo> selectByTbName(String tbName,long limit,long offset)
    {
        List<AlarmVo> ls=new ArrayList<>();
        if(StringUtils.notEmp(tbName) && StringUtils.notEmp(limit) && StringUtils.notEmp(offset))
        {
            ls= alarmTsMapper.selectByTbName(tbName, limit, offset);
        }

        return ls;
    }
    @Override
    public List<AlarmSimpleVo> selectLastRowByTbname(String superTableName, String tbnames){
        List<AlarmSimpleVo> ls=new ArrayList<>();
        if(StringUtils.notEmp(superTableName) && StringUtils.notEmp(tbnames))
        {
            StringBuilder sb= new StringBuilder();
            sb.append(" select last_row(ts) as ts, last_row(val) as val, last_row(confirmed) as confirmed, last_row(endts) as endts, last_row(timelong) as timelong,tbname from ")
            .append(superTableName).append(" where tbname in( ");
            sb.append(tbnames)
                    .append(" ) and endts is null group by tbname");

            ls= alarmTsMapper.selectLastRowByTbname(String.valueOf(sb));
        }

        return ls;
    }
}
