package com.ruoyi.bst.app.service.impl;

import com.ruoyi.bst.app.domain.App;
import com.ruoyi.bst.app.domain.AppQuery;
import com.ruoyi.bst.app.domain.AppVO;
import com.ruoyi.bst.app.domain.vo.AppAllVO;
import com.ruoyi.bst.app.mapper.AppMapper;
import com.ruoyi.bst.app.service.AppService;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.Collections;
import java.util.List;

/**
 * APP信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-08
 */
@Service
public class AppServiceImpl implements AppService
{
    @Autowired
    private AppMapper appMapper;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private TransactionTemplate transactionTemplate;

    /**
     * 查询APP信息
     *
     * @param id APP信息主键
     * @return APP信息
     */
    @Override
    public AppVO selectAppById(Long id)
    {
        return appMapper.selectAppById(id);
    }

    /**
     * 查询APP信息列表
     *
     * @param app APP信息
     * @return APP信息
     */
    @Override
    public List<AppVO> selectAppList(AppQuery app)
    {
        return appMapper.selectAppList(app);
    }

    /**
     * 新增APP信息
     *
     * @param app APP信息
     * @return 结果
     */
    @Override
    public int insertApp(App app)
    {
        Integer result = transactionTemplate.execute(status -> {
            app.setCreateTime(DateUtils.getNowDate());
            int insert = appMapper.insertApp(app);
            if (insert > 0) {
                clearCache();
            }
            return insert;
        });
        return result;
    }

    /**
     * 修改APP信息
     *
     * @param app APP信息
     * @return 结果
     */
    @Override
    public int updateApp(App app)
    {
        Integer result = transactionTemplate.execute(status -> {
            int update = appMapper.updateApp(app);
            if (update > 0) {
                clearCache();
            }
            return update;
        });
        return result;
    }

    /**
     * 批量删除APP信息
     *
     * @param ids 需要删除的APP信息主键
     * @return 结果
     */
    @Override
    public int deleteAppByIds(Long[] ids)
    {
        Integer result = transactionTemplate.execute(status -> {
            int delete = appMapper.deleteAppByIds(ids);
            if (delete > 0) {
                clearCache();
            }
            return delete;
        });
        return result;
    }

    /**
     * 删除APP信息信息
     *
     * @param id APP信息主键
     * @return 结果
     */
    @Override
    public int deleteAppById(Long id)
    {
        Integer result = transactionTemplate.execute(status -> {
            int delete = appMapper.deleteAppById(id);
            if (delete > 0) {
                clearCache();
            }
            return delete;
        });
        return result;
    }

    @Override
    public List<AppVO> selectByIds(List<Long> ids) {
        if (CollectionUtils.isEmptyElement(ids)) {
            return Collections.emptyList();
        }
        AppQuery query = new AppQuery();
        query.setIds(ids);
        return appMapper.selectAppList(query);
    }

    @Override
    public List<AppAllVO> selectAll() {
        List<AppAllVO> list = redisCache.getCacheList(CacheConstants.ALL_APP_LIST);
        if (CollectionUtils.isEmptyElement(list)) {
            list = appMapper.selectAllList();
            if (CollectionUtils.isNotEmptyElement(list)) {
                redisCache.setCacheList(CacheConstants.ALL_APP_LIST, list);
            }
        }
        return list;
    }

    /**
     * 清除缓存
     */
    private void clearCache() {
        redisCache.deleteObject(CacheConstants.ALL_APP_LIST);
    }
}
