package com.hfkj.service.impl;

import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.hfkj.common.utils.RedisUtil;
import com.hfkj.dao.SecVersionMapper;
import com.hfkj.entity.SecVersion;
import com.hfkj.entity.SecVersionExample;
import com.hfkj.service.SecVersionService;
import org.apache.commons.collections4.MapUtils;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @className: SecVersionServiceImpl
 * @author: HuRui
 * @date: 2023/6/21
 **/
@Service("secVersionService")
public class SecVersionServiceImpl implements SecVersionService {
    @Resource
    private SecVersionMapper secVersionMapper;

    @Resource
    private RedisUtil redisUtil;
    private final static String redisKey = "SYS_VERSION";
    @Override
    public void editData(SecVersion version) {
        version.setUpdateTime(new Date());

        if (version.getId() == null) {
            version.setCreateTime(new Date());
            secVersionMapper.insert(version);
        } else {
            secVersionMapper.updateByPrimaryKey(version);
        }
    }

    @Override
    public void releaseVersion(SecVersion version) {
        version.setStatus(1);
        version.setReleaseTime(new Date());
        editData(version);
        redisUtil.hset(redisKey, version.getTermType(), version);
    }

    @Override
    public SecVersion getDetailById(Integer id) {
        return secVersionMapper.selectByPrimaryKey(id);
    }

    @Override
    public SecVersion getCurrentVersion(String termType) {
        termType = termType.toUpperCase();
        Object version = redisUtil.hget(redisKey, termType);
        if (version != null) {
            return (SecVersion) version;
        }
        SecVersionExample example = new SecVersionExample();
        example.createCriteria().andTermTypeEqualTo(termType).andStatusEqualTo(1);
        example.setOrderByClause("release_time desc");
        List<SecVersion> list = secVersionMapper.selectByExample(example);
        if (list.size() > 0) {
            redisUtil.hset(redisKey, termType, list.get(0));
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<SecVersion> getList(Map<String, Object> param) {
        SecVersionExample example = new SecVersionExample();
        SecVersionExample.Criteria criteria = example.createCriteria().andStatusNotEqualTo(0);

        if (MapUtils.getString(param, "termType") != null) {
            criteria.andTermTypeEqualTo(MapUtils.getString(param, "termType"));
        }

        if (MapUtils.getInteger(param, "versionType") != null) {
            criteria.andVersionTypeEqualTo(MapUtils.getInteger(param, "versionType"));
        }

        if (MapUtils.getInteger(param, "status") != null) {
            criteria.andStatusEqualTo(MapUtils.getInteger(param, "status"));
        }

        example.setOrderByClause("create_time desc");
        return secVersionMapper.selectByExample(example);
    }
}
