package com.hzs.system.config.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.CacheConstants;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.domain.system.config.BdLabel;

import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.config.mapper.BdLabelMapper;
import com.hzs.system.config.service.IBdLabelService;
import com.hzs.system.config.vo.BdLabelVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 商品标签数据 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2022-10-20
 */
@Service
public class BdLabelServiceImpl extends ServiceImpl<BdLabelMapper, BdLabel> implements IBdLabelService {

    @Autowired
    private RedisService redisService;

    @Override
    public BdLabel getByPkId(Integer labelId) {
        String redisKey = CacheConstants.BD_LABEL + labelId;
        BdLabel label = redisService.getCacheObject(redisKey);
        if (label == null) {
            label = baseMapper.selectById(labelId);
            if (null != label) {
                redisService.setCacheObject(redisKey, label);
            }
        }
        return label;
    }

    @Override
    public List<BdLabel> getLabelByPkCountry(BdLabelVO bdLabelVO) {
        List<BdLabel> bdLabelList = baseMapper.getLabelByPkCountry(bdLabelVO);
        return bdLabelList;
    }


    @Override
    public Boolean deleteLabelById(Integer pkId) {
        UpdateWrapper<BdLabel> queryWrapper = new UpdateWrapper<>();
        queryWrapper.eq("PK_ID", pkId);
        queryWrapper.set("PK_MODIFIED", SecurityUtils.getUserId());
        queryWrapper.set("MODIFIED_TIME", new Date());
        queryWrapper.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        Boolean rows = this.update(queryWrapper);
        if (rows) {
            String redisKey = CacheConstants.BD_LABEL + pkId;
            redisService.deleteObjectByKey(redisKey);
        }
        return rows;
    }


    @Override
    public List<BdLabel> findLabelByType(Integer labelType, Integer pkCountry) {
        String redisKey = CacheConstants.BD_LABEL + ":" + pkCountry + labelType;
        List<BdLabel> labels = redisService.getCacheList(redisKey);
        if (CollUtil.isEmpty(labels)) {
            labels = baseMapper.findLabelByType(labelType, pkCountry);
            if (CollUtil.isNotEmpty(labels)) {
                redisService.setCacheList(redisKey, labels);
            }
        }
        return labels;
    }

    @Override
    public Boolean updateLabelById(BdLabel bdLabel) {
        Boolean rows = updateById(bdLabel);
        if (rows) {
            String redisKey = CacheConstants.BD_LABEL + bdLabel.getPkId();
            redisService.deleteObjectByKey(redisKey);
        }
        return rows;
    }
}
