package com.ruoyi.app.service.impl;

import com.ruoyi.app.domain.HookRegister;
import com.ruoyi.app.domain.NFIDReader;
import com.ruoyi.app.domain.request.ReqHookRegister;
import com.ruoyi.app.mapper.HookRegisterMapper;
import com.ruoyi.app.service.IHookRegisterService;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Collection;
import java.util.List;

/**
 * 吊钩注册Service业务层处理
 * 
 * @author coede
 * @date 2025-03-19
 */
@Service
public class HookRegisterServiceImpl implements IHookRegisterService
{
    @Autowired
    private HookRegisterMapper hookRegisterMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 项目启动时，初始化到缓存
     */
    @PostConstruct
    public void init()
    {
        resetConfigCache();
    }

    /**
     * 查询吊钩注册
     * 
     * @param id 吊钩注册主键
     * @return 吊钩注册
     */
    @Override
    public HookRegister selectHookRegisterById(Long id)
    {
        return hookRegisterMapper.selectHookRegisterById(id);
    }

    /**
     * 根据芯片编号查询吊钩号
     *
     * @param epcNo 芯片编号
     * @return 参数键值
     */
    @Override
    public String selectHookNoByEpcNo(String epcNo)
    {
        String hookNo = Convert.toStr(redisCache.getCacheObject(getCacheKey(epcNo)));
        if (StringUtils.isNotEmpty(hookNo))
        {
            return hookNo;
        }
        ReqHookRegister req = new ReqHookRegister();
        req.setEpcNo(epcNo);
        HookRegister hookRegister = hookRegisterMapper.selectHookRegisterByEpcNo(req);
        if (StringUtils.isNotNull(hookRegister))
        {
            redisCache.setCacheObject(getCacheKey(epcNo), hookRegister.getHookNo());
            return hookRegister.getHookNo();
        }
        return StringUtils.EMPTY;
    }

    /**
     * 查询吊钩注册列表
     * 
     * @param req 吊钩注册
     * @return 吊钩注册
     */
    @Override
    public List<HookRegister> selectHookRegisterList(ReqHookRegister req)
    {
        return hookRegisterMapper.selectHookRegisterList(req);
    }

    /**
     * 校验吊钩编号是否唯一
     *
     * @param hookRegister 吊钩
     * @return 结果
     */
    @Override
    public boolean checkHookNoUnique(HookRegister hookRegister)
    {
        Long id = StringUtils.isNull(hookRegister.getId()) ? -1L : hookRegister.getId();
        HookRegister info = hookRegisterMapper.checkHookNoUnique(hookRegister.getHookNo());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != id.longValue())
        {
            return false;
        }
        return true;
    }

    /**
     * 校验芯片编号是否唯一
     *
     * @param hookRegister 分类
     * @return 结果
     */
    @Override
    public boolean checkEpcNoUnique(HookRegister hookRegister)
    {
        Long id = StringUtils.isNull(hookRegister.getId()) ? -1L : hookRegister.getId();
        HookRegister info = hookRegisterMapper.checkEpcNoUnique(hookRegister.getEpcNo());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != id.longValue())
        {
            return false;
        }
        return true;
    }

    /**
     * 新增吊钩注册
     * 
     * @param hookRegister 吊钩注册
     * @return 结果
     */
    @Override
    public int insertHookRegister(HookRegister hookRegister)
    {
        hookRegister.setCreateTime(DateUtils.getNowDate());
        int row = hookRegisterMapper.insertHookRegister(hookRegister);
        if (row > 0)
        {
            redisCache.setCacheObject(getCacheKey(hookRegister.getEpcNo()), hookRegister.getHookNo());
        }
        return row;
    }

    /**
     * 修改吊钩注册
     * 
     * @param hookRegister 吊钩注册
     * @return 结果
     */
    @Override
    public int updateHookRegister(HookRegister hookRegister)
    {
        hookRegister.setUpdateTime(DateUtils.getNowDate());
        HookRegister temp = hookRegisterMapper.selectHookRegisterById(hookRegister.getId());
        if (!StringUtils.equals(temp.getEpcNo(), hookRegister.getEpcNo()))
        {
            //缓存的键被改了
            redisCache.deleteObject(getCacheKey(temp.getEpcNo()));
        }

        int row = hookRegisterMapper.updateHookRegister(hookRegister);
        if (row > 0)
        {
            redisCache.setCacheObject(getCacheKey(hookRegister.getEpcNo()), hookRegister.getHookNo());
        }
        return row;
       // return hookRegisterMapper.updateHookRegister(hookRegister);
    }

    /**
     * 批量删除吊钩注册
     * 
     * @param ids 需要删除的吊钩注册主键
     * @return 结果
     */
    @Override
    public int deleteHookRegisterByIds(Long[] ids)
    {
        int rows = 0;
        for (Long id : ids)
        {
            HookRegister hookRegister = hookRegisterMapper.selectHookRegisterById(id);
            int res = hookRegisterMapper.deleteHookRegisterById(id);
            if (res > 0){

                redisCache.deleteObject(getCacheKey(hookRegister.getEpcNo()));
                rows++;
            }

        }
        return rows;
    }

    /**
     * 删除吊钩注册信息
     * 
     * @param id 吊钩注册主键
     * @return 结果
     */
    @Override
    public int deleteHookRegisterById(Long id)
    {

        return hookRegisterMapper.deleteHookRegisterById(id);
    }

    /**
     * 加载参数缓存数据
     */
    @Override
    public void loadingConfigCache()
    {

        List<HookRegister> hookRegisterList = hookRegisterMapper.selectHookRegisterList(new ReqHookRegister());
        for (HookRegister hookRegister : hookRegisterList)
        {
            redisCache.setCacheObject(getCacheKey(hookRegister.getEpcNo()), hookRegister.getHookNo());
        }
    }

    /**
     * 清空参数缓存数据
     */
    @Override
    public void clearConfigCache()
    {
        Collection<String> keys = redisCache.keys(CacheConstants.APP_HOOK_EPC + "*");
        redisCache.deleteObject(keys);
    }

    /**
     * 重置参数缓存数据
     */
    @Override
    public void resetConfigCache()
    {
        clearConfigCache();
        loadingConfigCache();
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    private String getCacheKey(String configKey)
    {
        return CacheConstants.APP_HOOK_EPC + configKey;
    }
}
