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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyee.alarm.init.CacheContext;
import com.gyee.alarm.mapper.auto.ProEconAlarmPlanMapper;
import com.gyee.alarm.mapper.auto.ProEconAlarmRealMapper;
import com.gyee.alarm.mapper.auto.ProEconAlarmPlanMapper;
import com.gyee.alarm.model.auto.*;
import com.gyee.alarm.model.auto.ProEconAlarmPlan;
import com.gyee.alarm.model.vo.AlarmConfigurationVo;
import com.gyee.alarm.service.RuleUpdateEventService;
import com.gyee.alarm.service.auto.IProEconAlarmPlanService;
import com.gyee.alarm.util.SnowflakeGenerator;
import com.gyee.alarm.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shilin
 * @since 2024-10-28
 */
@Service
public class ProEconAlarmPlanServiceImpl extends ServiceImpl<ProEconAlarmPlanMapper, ProEconAlarmPlan> implements IProEconAlarmPlanService {


    @Resource
    private ProEconAlarmRealMapper proEconAlarmRealMapper;

    @Resource
    private ProEconAlarmPlanMapper proEconAlarmPlanMapper;
      public void  updatePlanlistById(String alarmId, String planIds)
    {
        List<ProEconAlarmReal>  ls= proEconAlarmRealMapper.alarmPlanlistById(alarmId);

        if(!ls.isEmpty())
        {
            ProEconAlarmReal ar=ls.get(0);

            ar.setAlarmPlan(planIds);

            proEconAlarmRealMapper.updateProEconAlarmReal(ar.getAlarmId(),ar.getAlarmPlan(),ar.getId());
        }else
        {
            ProEconAlarmReal ar=new ProEconAlarmReal();

            ar.setId(String.valueOf(SnowflakeGenerator.generateId()));
            ar.setAlarmId(alarmId);
            ar.setAlarmPlan(planIds);

            proEconAlarmRealMapper.insert(ar);
        }
    }
    public  List<ProEconAlarmPlan>  alarmPlanlistById(String alarmId) {

        List<ProEconAlarmPlan> vos=new ArrayList();

        List<ProEconAlarmReal>  ls= proEconAlarmRealMapper.alarmPlanlistById( alarmId);

        if(!ls.isEmpty())
        {
            ProEconAlarmReal ar=ls.get(0);

            if(StringUtils.notEmp(ar.getAlarmPlan()))
            {
               String[] strs= ar.getAlarmPlan().split(",");

                List<String> alarmPlanListId = Arrays.stream(strs).filter(s -> StringUtils.notEmp(s)).collect(Collectors.toList());

                QueryWrapper<ProEconAlarmPlan> qw = new QueryWrapper<>();


                if (alarmPlanListId != null && !alarmPlanListId.isEmpty()) {
                    qw.in("id", alarmPlanListId);
                }
                List<ProEconAlarmPlan> list = baseMapper.selectList(qw);
                return list;
            }
        }



        return vos;
    }


    public int saveAndUpdateAlertrule(ProEconAlarmPlan alarmRule) throws Exception {

        int result = 0;

        if (StringUtils.notEmp(alarmRule) && null !=alarmRule.getName()&& null !=alarmRule.getTypes()) {

            if(StringUtils.notEmp(alarmRule.getId()))
            {
                ProEconAlarmPlan oldRulel = proEconAlarmPlanMapper.selectById(alarmRule.getId());
                if(StringUtils.notEmp(oldRulel))
                {

                    if (oldRulel != null) {
                        alarmRule.setId(oldRulel.getId());
                        result = proEconAlarmPlanMapper.updateByAlertruleId(alarmRule);

                    }
                }else
                {
                    if(StringUtils.empty(alarmRule.getId()))
                    {
                        alarmRule.setId(String.valueOf(SnowflakeGenerator.generateId()));
                    }

                    result = saveAlertrule(alarmRule);
                }
            }else
            {
                if(StringUtils.empty(alarmRule.getId()))
                {
                    alarmRule.setId(String.valueOf(SnowflakeGenerator.generateId()));
                }

                result = saveAlertrule(alarmRule);
            }
        }

        return result;
    }


    private int saveAlertrule(ProEconAlarmPlan alarmRule) throws Exception {

        int result = 0;

        if (StringUtils.notEmp(alarmRule)) {


            alarmRule.setId(String.valueOf(SnowflakeGenerator.generateId()));

            result= proEconAlarmPlanMapper.insert(alarmRule);
        }
//        if (result > 0) {
//            int i = eventService.saveEventAlertRule(alarmRule, oldRule2, userData.getUserName());
        return result;
    }

    public boolean deleteAlertrule( List<String> idls) {


          return removeByIds(idls);
    }


    public IPage<ProEconAlarmPlan> pageQueryAll(Page page, String name,String types) {


        if(StringUtils.notEmp(name))
        {
            name="%"+name+"%";
        }else
        {
            name=null;
        }

        if(StringUtils.notEmp(types))
        {
            types=types;
        }else
        {
            types=null;
        }
        IPage<ProEconAlarmPlan> alertruleIPage = proEconAlarmPlanMapper.pageQueryAll(page, name,types);


        return alertruleIPage;
    }
}
