package com.bonus.system.service.impl;

import com.bonus.common.core.constant.CacheConstants;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.redis.service.RedisService;
import com.bonus.system.domain.SysIpWhitelist;
import com.bonus.system.mapper.SysIpWhitelistMapper;
import com.bonus.system.service.ISysIpWhitelistService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

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

/**
 * 白名单管理Service业务层处理
 *
 * @author bonus
 * @date 2024-11-06
 */
@Service
public class SysIpWhitelistServiceImpl implements ISysIpWhitelistService {
    @Resource
    private SysIpWhitelistMapper sysIpWhitelistMapper;

    @Resource
    private RedisService redisService;

    @PostConstruct
    public void init() {
        selectSysIpWhitelist();
    }

    /**
     * 查询白名单管理
     *
     * @param id 白名单管理主键
     * @return 白名单管理
     */
    @Override
    public AjaxResult selectSysIpWhitelistById(Long id) {
        try {
            SysIpWhitelist sysIpWhitelist = sysIpWhitelistMapper.selectSysIpWhitelistById(id);
            if (ObjectUtils.isEmpty(sysIpWhitelist)) {
                return AjaxResult.error();
            } else {
                return AjaxResult.success(sysIpWhitelist);
            }
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }

    /**
     * 查询白名单管理列表
     *
     * @param sysIpWhitelist 白名单管理
     * @return 白名单管理
     */
    @Override
    public List<SysIpWhitelist> selectSysIpWhitelistList(SysIpWhitelist sysIpWhitelist) {
        selectSysIpWhitelist();
        return sysIpWhitelistMapper.selectSysIpWhitelistList(sysIpWhitelist);
    }

    /**
     * 新增白名单管理
     *
     * @param sysIpWhitelist 白名单管理
     * @return 结果
     */
    @Override
    public AjaxResult insertSysIpWhitelist(SysIpWhitelist sysIpWhitelist) {
        try {
            int rows = sysIpWhitelistMapper.insertSysIpWhitelist(sysIpWhitelist);
            return rows > 0 ? AjaxResult.success() : AjaxResult.error();
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }

    /**
     * 修改白名单管理
     *
     * @param sysIpWhitelist 白名单管理
     * @return 结果
     */
    @Override
    public AjaxResult updateSysIpWhitelist(SysIpWhitelist sysIpWhitelist) {
        try {
            int rows = sysIpWhitelistMapper.updateSysIpWhitelist(sysIpWhitelist);
            return rows > 0 ? AjaxResult.success() : AjaxResult.error();
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }

    /**
     * 批量删除白名单管理
     *
     * @param ids 需要删除的白名单管理主键
     * @return 结果
     */
    @Override
    public AjaxResult deleteSysIpWhitelistByIds(Long[] ids) {
        try {

            int rows = sysIpWhitelistMapper.deleteSysIpWhitelistByIds(ids);
            return rows > 0 ? AjaxResult.success() : AjaxResult.error();
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }

    /**
     * @param sysIpWhitelist
     * @return
     */
    @Override
    public AjaxResult updateSysIpWhitelistStatus(SysIpWhitelist sysIpWhitelist) {
        try {
            int i = sysIpWhitelistMapper.updateSysIpWhitelistStatus(sysIpWhitelist);
            return i > 0 ? AjaxResult.success() : AjaxResult.error();

        } catch (Exception e) {
            return AjaxResult.error();
        }
    }

    /**
     * @return
     */
    @Override
    public void selectSysIpWhitelist() {
        List<SysIpWhitelist> list = sysIpWhitelistMapper.selectSysIpWhitelist();
        redisService.setCacheObject(CacheConstants.SYS_LOGIN_BLACKIPLIST, list);
    }
}
