package com.fjhx.iot.service.tda.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.iot.entity.tda.dto.TdaConfigDto;
import com.fjhx.iot.entity.tda.dto.TdaConfigSelectDto;
import com.fjhx.iot.entity.tda.po.TdaConfig;
import com.fjhx.iot.entity.tda.vo.TdaConfigVo;
import com.fjhx.iot.listener.DtaListener;
import com.fjhx.iot.mapper.tda.TdaConfigMapper;
import com.fjhx.iot.service.tda.TdaConfigService;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.common.utils.wrapper.SqlField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.jms.JMSException;
import javax.jms.MessageConsumer;


/**
 * <p>
 * iot配置 服务实现类
 * </p>
 *
 * @author zlj
 * @since 2023-03-21
 */
@Service
public class TdaConfigServiceImpl extends ServiceImpl<TdaConfigMapper, TdaConfig> implements TdaConfigService {

    @Lazy
    @Autowired
    private DtaListener dtaListener;

    @Override
    public Page<TdaConfigVo> getPage(TdaConfigSelectDto dto) {
        IWrapper<TdaConfig> wrapper = getWrapper();
        wrapper.keyword(dto, new SqlField("tc.name"));
        wrapper.orderByDesc("tc", TdaConfig::getId);
        Page<TdaConfigVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        return page;
    }

    @Override
    public TdaConfigVo detail(Long id) {
        TdaConfig TdaConfig = this.getById(id);
        TdaConfigVo result = BeanUtil.toBean(TdaConfig, TdaConfigVo.class);
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(TdaConfigDto tdaConfigDto) {
        this.save(tdaConfigDto);
        MessageConsumer messageConsumer = dtaListener.getMessageConsumer(tdaConfigDto);
        DtaListener.listenerMap.put(tdaConfigDto.getId(), messageConsumer);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(TdaConfigDto tdaConfigDto) {
        this.updateById(tdaConfigDto);

        MessageConsumer messageConsumer = dtaListener.getMessageConsumer(tdaConfigDto);
        DtaListener.listenerMap.put(tdaConfigDto.getId(), messageConsumer);

        MessageConsumer oldMessageConsumer = DtaListener.listenerMap.get(tdaConfigDto.getId());
        if (oldMessageConsumer != null) {
            try {
                oldMessageConsumer.close();
            } catch (JMSException e) {
                log.error("关闭失败");
            }
            DtaListener.listenerMap.remove(tdaConfigDto.getId());
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Long id) {
        this.removeById(id);
        MessageConsumer oldMessageConsumer = DtaListener.listenerMap.get(id);
        if (oldMessageConsumer != null) {
            try {
                oldMessageConsumer.close();
            } catch (JMSException e) {
                log.error("关闭失败");
            }
            DtaListener.listenerMap.remove(id);
        }
    }

}
