package com.ruoyi.app.service.impl;

import java.util.List;

import com.ruoyi.app.DTO.HookBindBatchListDTO;
import com.ruoyi.app.DTO.HookBindDetailDTO;
import com.ruoyi.app.domain.HookRegister;
import com.ruoyi.app.domain.NFIDReader;
import com.ruoyi.app.domain.request.EditHookBindBatch;
import com.ruoyi.app.domain.request.ReqHookBind;
import com.ruoyi.app.domain.request.ReqHookBindList;
import com.ruoyi.app.domain.request.ReqHookRegister;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.bouncycastle.cert.ocsp.Req;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.app.mapper.HookBindMapper;
import com.ruoyi.app.domain.HookBind;
import com.ruoyi.app.service.IHookBindService;

/**
 * 吊钩绑定Service业务层处理
 * 
 * @author coede
 * @date 2025-03-19
 */
@Service
public class HookBindServiceImpl implements IHookBindService 
{
    @Autowired
    private HookBindMapper hookBindMapper;

    @Autowired
    private RedisCache redisCache;
    /**
     * 查询吊钩绑定
     * 
     * @param batchNo 批次号
     * @return 吊钩绑定
     */
    @Override
    public HookBindBatchListDTO selectHookBindByBatch(String batchNo)
    {
        String[] ids = batchNo.split(",");
        return hookBindMapper.selectHookBindByBatch(ids);
    }

    /**
     * 查询吊钩绑定详情
     *
     * @param hookNo 吊钩号号
     * @return 吊钩绑定
     */
    @Override
    public HookBindDetailDTO selectHookBindDetail(String hookNo)
    {
        HookBindDetailDTO cache = redisCache.getCacheObject(getCacheKey(hookNo));
        if (StringUtils.isNotNull(cache))
        {
            return cache;
        }
        ReqHookBind req = new ReqHookBind();
        req.setHookNo(hookNo);
        List<HookBindDetailDTO> res = hookBindMapper.selectHookBindDetail(req);
        if (res != null && res.size() > 0){
            redisCache.setCacheObject(getCacheKey(hookNo), res.get(0));
            redisCache.expire(getCacheKey(hookNo), 60 * 60);//一小时过期
            return res.get(0);
        }else {
            return new HookBindDetailDTO();
        }
    }

    /**
     * 查询吊钩绑定列表
     * 
     * @param hookBind 吊钩绑定
     * @return 吊钩绑定
     */
    @Override
    public List<HookBind> selectHookBindList(HookBind hookBind)
    {
        return hookBindMapper.selectHookBindList(hookBind);
    }

    /**
     * 按批次查询吊钩绑定列表
     *
     * @param req 吊钩绑定
     * @return 吊钩绑定
     */
    @Override
    public List<HookBindBatchListDTO> selectHookBindListByBatch(ReqHookBindList req)
    {
        return hookBindMapper.selectHookBindListByBatch(req);
    }

    /**
     * 校验相同时间吊钩是否已绑定
     *
     * @param hookBind 吊钩绑定
     * @return 结果
     */
    @Override
    public HookBind checkHookNoUnique(HookBind hookBind)
    {
        return hookBindMapper.checkHookNoUnique(hookBind);

    }

    /**
     * 新增吊钩绑定
     * 
     * @param hookBind 吊钩绑定
     * @return 结果
     */
    @Override
    public int insertHookBind(HookBind hookBind)
    {
        hookBind.setCreateTime(DateUtils.getNowDate());
        return hookBindMapper.insertHookBind(hookBind);
    }

    /**
     * 新增吊钩批量绑定
     *
     * @param batch 吊钩绑定
     * @return 结果
     */
    @Override
    public int insertHookBindBatch(List<HookBind> batch)
    {
        return hookBindMapper.insertHookBindBatch(batch);
    }

    /**
     * 修改吊钩绑定
     * 
     * @param hookBind 吊钩绑定
     * @return 结果
     */
    @Override
    public int updateHookBind(HookBind hookBind)
    {
        hookBind.setUpdateTime(DateUtils.getNowDate());
        if(hookBind.getSlaughterCode() !=null){
            hookBind.setIsBind(HookBind.IS_BIND);
            hookBind.setBindTime(DateUtils.getNowDate());
        }
        ReqHookBind req = new ReqHookBind();
        req.setId(hookBind.getId());
        List<HookBindDetailDTO> res = hookBindMapper.selectHookBindDetail(req);
        if (res != null && res.size() > 0){
            HookBindDetailDTO temp = res.get(0);
            if (!StringUtils.equals(temp.getHookNo(), hookBind.getHookNo()))
            {
                //缓存的键被改了
                redisCache.deleteObject(getCacheKey(temp.getHookNo()));
            }
        }
        int row = hookBindMapper.updateHookBind(hookBind);
        if (row > 0)
        {
            redisCache.setCacheObject(getCacheKey(hookBind.getHookNo()), hookBind);
            redisCache.expire(getCacheKey(hookBind.getHookNo()), 60 * 60);//一小时过期
        }
        return row;
    }

    /**
     * 批量修改吊钩绑定
     *
     * @param req 吊钩绑定
     * @return 结果
     */
    @Override
    public int updateHookBindBatch(EditHookBindBatch req)
    {
        req.setUpdateTime(DateUtils.getNowDate());
        if(req.getSlaughterCode() !=null){
            req.setIsBind(HookBind.IS_BIND);
            if(req.getBindTime() == null){
                req.setBindTime(DateUtils.getNowDate());
            }

        }

        int row = hookBindMapper.updateHookBindBatch(req);
        if (row > 0)
        {
            ReqHookBind detailReq = new ReqHookBind();
            detailReq.setIds(req.getGroupIds());
            List<HookBindDetailDTO> res = hookBindMapper.selectHookBindDetail(detailReq);
            if (res != null){
                for (HookBindDetailDTO hookBindDetailDTO : res) {
                    redisCache.deleteObject(getCacheKey(hookBindDetailDTO.getHookNo()));
                    redisCache.setCacheObject(getCacheKey(hookBindDetailDTO.getHookNo()), hookBindDetailDTO);
                    redisCache.expire(getCacheKey(hookBindDetailDTO.getHookNo()), 60 * 60);//一小时过期
                }
            }

        }
        return row;
    }

    /**
     * 批量删除吊钩绑定
     * 
     * @param ids 需要删除的吊钩绑定主键
     * @return 结果
     */
    @Override
    public int deleteHookBindByIds(String[] ids)
    {
        return hookBindMapper.deleteHookBindByIds(ids);
    }

    /**
     * 删除吊钩绑定信息
     * 
     * @param id 吊钩绑定主键
     * @return 结果
     */
    @Override
    public int deleteHookBindById(String id)
    {
        return hookBindMapper.deleteHookBindById(id);
    }

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