package com.sptg.device.biz.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.common.core.util.JsonUtils;
import com.sptg.device.api.entity.MqConf;
import com.sptg.device.biz.mapper.MqConfMapper;
import com.sptg.device.biz.service.MqConfServiceInterface;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * title: MqConfServiceImpl
 * package: com.sptg.device.biz.security.impl
 * description: 队列业务层-实现类
 *
 *                  
 *                
 */
@Service
@AllArgsConstructor
public class MqConfServiceImpl extends ServiceImpl<MqConfMapper, MqConf> implements MqConfServiceInterface {


    private final RedisTemplate<String,String> redisTemplate;

    /**
     * 队列服务器缓存名称
     */
    private final static String MQ_CACHE_NAME = "sptg:device:mq";

    /* ------------------------------------- 重写父类查询方法，过滤已删除的数据 ------------------------------------- */
    /* -------------------------------------------------------------------------------------------------------------- */

    @Override
    public MqConf selectById(Serializable id) {
        //  查询缓存
        HashOperations<String, String, String> hashOperations = this.redisTemplate.opsForHash();
        String hashKey = id.toString();
        String json = hashOperations.get(MQ_CACHE_NAME, hashKey);
        if (StringUtils.isEmpty(json)){
            //  查询数据库
            Wrapper<MqConf> wrapper = new EntityWrapper<>();
            wrapper.eq("l_id", id);
            MqConf conf = this.selectOne(wrapper);
            if (null != conf){
                //  保存到缓存
                json = JsonUtils.objectToJson(conf);
                hashOperations.put(MQ_CACHE_NAME,hashKey,json);
            }
            return conf;
        }
        return JsonUtils.jsonToPojo(json,MqConf.class);
    }

    @Override
    public List<MqConf> selectList(Wrapper<MqConf> wrapper) {
        wrapper.notIn("i_status", OperationConstant.MINUS_ONE);
        return super.selectList(wrapper);
    }

    @Override
    public MqConf selectOne(Wrapper<MqConf> wrapper) {
        wrapper.notIn("i_status", OperationConstant.MINUS_ONE);
        return super.selectOne(wrapper);
    }

    @Override
    public Page<MqConf> selectPage(Page<MqConf> page) {
        Wrapper<MqConf> wrapper = new EntityWrapper<>();
        //  排序字段 时间降序
        wrapper.orderBy("t_create_date",false);
        return this.selectPage(page,wrapper);
    }

    @Override
    public Page<MqConf> selectPage(Page<MqConf> page, Wrapper<MqConf> wrapper) {
        wrapper.notIn("i_status", OperationConstant.MINUS_ONE);
        return super.selectPage(page, wrapper);
    }

    /* ----------------------------------------------- 修改时删除缓存 ----------------------------------------------- */
    /* -------------------------------------------------------------------------------------------------------------- */

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public boolean updateById(MqConf entity) {
        boolean result = super.updateById(entity);
        if (result){
            //  删除缓存
            HashOperations<String, String, String> hashOperations = this.redisTemplate.opsForHash();
            hashOperations.delete(MQ_CACHE_NAME,entity.getId().toString());
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public boolean updateBatchById(List<MqConf> entityList) {
        boolean result = super.updateBatchById(entityList);
        if (result){
            //  删除缓存
            HashOperations<String, String, String> hashOperations = this.redisTemplate.opsForHash();
            entityList.forEach(entity -> hashOperations.delete(entity.getId().toString()));
        }
        return result;
    }

    /* -------------------------------------------------- 业务代码 -------------------------------------------------- */
    /* -------------------------------------------------------------------------------------------------------------- */

    @Override
    public boolean insert(MqConf entity) {
        //  当前连接数
        entity.setThreads(OperationConstant.ZERO);
        //  在线状态
        entity.setOnLineStatus(OperationConstant.ZERO);
        //  数据状态
        entity.setStatus(OperationConstant.ZERO);
        //  创建时间
        entity.setCreateDate(new Date());
        return super.insert(entity);
    }


    @Override
    public MqConf selectUsableServer() {
        Wrapper<MqConf> wrapper = new EntityWrapper<>();
        wrapper.eq("i_online_status",OperationConstant.ONE);
        wrapper.and("i_threads < i_max_connections");
        return this.selectOne(wrapper);
    }
}
