package com.jiayue.ssi.job;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiayue.ssi.entity.SysPolicy;
import com.jiayue.ssi.service.SysPolicyService;
import com.jiayue.ssi.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.File;
import java.math.BigDecimal;
import java.text.DecimalFormat;
/**
* 自动备份审计日志
*
* @author xsl
* @since 2023/04/06
*/
@Service
@EnableScheduling
@Slf4j
public class AuditBakPathCapCheck {
    @Autowired
    SysPolicyService sysPolicyService;

    /**
     * 每5分钟执行执行一次
     */
    @Scheduled(cron = "5 0/5 * * * ?")
    public void auditCheck() throws Exception {

        DecimalFormat df = new DecimalFormat("#.00");//设置保留两位小数
        SysPolicy sysPolicy = sysPolicyService.getOne(new QueryWrapper<>());
        float logSpaceWarnCap = sysPolicy.getAuditLogBakCapAlarm() *  Convert.toFloat(txfloat(sysPolicy.getLogSpaceWarn(),100));
//        log.debug("日志容量阈值：{} GB参数设置：磁盘上限：{} GB，预警阈值百分比：{}%",df.format(logSpaceWarnCap),sysPolicy.getAuditLogBakCapAlarm(),sysPolicy.getLogSpaceWarn());
        long size = FileUtils.sizeOfDirectory(new File(FileUtil.getAuditBackUpPath()));
//        log.debug("审查日志备份路径大小：{} ,转换后大小：{} GB",size,getSizeToGb(size));

        if(Convert.toFloat((Convert.toFloat(sysPolicy.getAuditLogBakCapAlarm(),0f) - Convert.toFloat(getSizeToGb(size),0f)))< logSpaceWarnCap){
            log.info("发送邮箱日志存储告警\r\n" +
                    "↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓邮箱内容↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓"+
                    "\r\n  审计日志存储剩余容量低于额定容量"+sysPolicy.getLogSpaceWarn()+"%,邮箱告警！ \r\n"     +
                    "↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑邮箱内容↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑");
//            log.info("发送邮箱通知系统管理员后台输出======> 审计日志存储剩余容量低于额定容量"+sysPolicy.getLogSpaceWarn()+"%,邮箱告警！");
        }else{
//            log.info("检测审计日志备份目录大小正常。");
        }
    }

    /**
     * 传入一个字符串类型的数字大小，实现文件大小的转化，如 B - KB - MB - GB
     * @param
     * @return
     */
    public static String getSizeToGb(long i){
        String result = "";
        long kb = 1024;
        long mb = kb * 1024;
        long gb = mb * 1024;

        /*实现保留小数点两位*/
        DecimalFormat df = new DecimalFormat("#.00");

        result =  df.format((float) i / gb) + "GB";

        return result;
    }
    /**
     *  除法运算，保留小数
     * @param a 被除数
     * @param b 除数
     * @return 商
     */
    public static String txfloat(int a,int b) {
        // TODO 自动生成的方法存根

        DecimalFormat df=new DecimalFormat("0.00");//设置保留位数

        return df.format((float)a/b);

    }
    /**
     * 传入一个字符串类型的数字大小，实现文件大小的转化，如 B - KB - MB - GB
     * @param
     * @return
     */
    public static String getSize(long i){
        String result = "";
        long kb = 1024;
        long mb = kb * 1024;
        long gb = mb * 1024;

        /*实现保留小数点两位*/
        DecimalFormat df = new DecimalFormat("#.00");

        if (i >= gb){
            result =  df.format((float) i / gb) + "GB";
        }else if(i >= mb){
            result =  df.format((float) i / mb) + "MB";
        }else if(i >= kb){
            result = String.format("%.2f", (float) i / kb) + "KB";
        }else {
            result =  i + "B";
        }
        return result;
    }





}
