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.AlarmSuperTalbeType;
import com.gyee.alarm.model.vo.AlarmTag;
import com.gyee.alarm.model.vo.AlarmTsVo;
import com.gyee.alarm.model.vo.AlarmVo;
import com.gyee.alarm.service.auto.IAlarmTsService;
import com.gyee.alarm.util.StringUtils;
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;



    public void createSuperTable(String type) {

        alarmTsMapper.createAlarmSuperTable(type);

    }


    public void dropSuperTable(String type) {

        alarmTsMapper.dropSuperTable(type);

    }

    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

    ) {
        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);
    }


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



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



    public void insertList(List<AlarmTag>  alarmTags )  {

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

            createSql(alarmTags);
        }


    }

    public void createSql(List<AlarmTag>  alarmTags )   {

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

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

            if(tag.getVal()==1)
            {
                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.getOval().intValue()).append(",").append(false);
                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));
        }

    }

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

        return ls;
    }

    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;
    }


    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;
    }
}
