package com.njcn.product.event.transientes.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.product.event.transientes.mapper.MsgEventConfigMapper;
import com.njcn.product.event.transientes.pojo.po.MsgEventConfig;
import com.njcn.product.event.transientes.service.MsgEventConfigService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * @Author: cdf
 * @CreateTime: 2025-06-27
 * @Description:
 */
@Service
@Lazy(false) // 确保服务在启动时立即初始化
public class MsgEventConfigServiceImpl  extends ServiceImpl<MsgEventConfigMapper, MsgEventConfig> implements MsgEventConfigService {

    /**
     * 暂降类型
     */
    public List<String> eventType = Stream.of("1","3").collect(Collectors.toList());

    /**
     * 暂降残余电压阈值 只查询小于0.7的暂降事件
     */
    public Float eventValue =  0.7f;

    /**
     * 暂降残余电压阈值 只查询大于50ms的暂降事件
     */
    public Integer eventDuration =  50;



    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean eventConfig(MsgEventConfig msgEventConfig) {
        this.remove(new LambdaQueryWrapper<>());
        msgEventConfig.setId(IdUtil.simpleUUID());
        String tem = String.join(StrUtil.COMMA, msgEventConfig.getEventTypeList());
        msgEventConfig.setEventType(tem);
        this.save(msgEventConfig);
        eventType = msgEventConfig.getEventTypeList();
        eventValue = msgEventConfig.getEventValue();
        eventDuration= msgEventConfig.getEventDuration();
        return true;
    }

    @Override
    public MsgEventConfig queryConfig() {
        MsgEventConfig msgEventConfig = this.getOne(new LambdaQueryWrapper<>());
        msgEventConfig.setEventTypeList(Arrays.asList(msgEventConfig.getEventType().split(StrUtil.COMMA)));
        return msgEventConfig;
    }


    @PostConstruct
    public void init() {
        System.out.println("------------------------------------------------------------------------------");
        MsgEventConfig config = this.getOne(new LambdaQueryWrapper<>());
        if(Objects.nonNull(config)){
            if (StrUtil.isNotBlank(config.getEventType())) {
                eventType = Arrays.asList(config.getEventType().split(StrUtil.COMMA));
            }
            if(Objects.nonNull(config.getEventValue())){
                eventValue = config.getEventValue();
            }
            if(Objects.nonNull(config.getEventDuration())){
                eventDuration = config.getEventDuration();
            }
        }
        System.out.println(config);
    }


    @Override
    public List<String> getEventType() {
        return eventType;
    }

    @Override
    public Float getEventValue() {
        return eventValue;
    }

    @Override
    public Integer getEventDuration() {
        return eventDuration;
    }


}
