package org.springblade.system.service;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.core.domain.entity.SysLogsVo;
import org.springblade.common.utils.DateUtils;
import org.springblade.common.utils.global.SystemGlobal;
import org.springblade.system.mapper.SysLogMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
 * @author 黑子
 */
@Configuration
@Slf4j
public class ExportSqlService {

    public static String TABLE_NAME = "sys_logs";
    /**
     * 存储的路径
     */
    @Value("${sql.filePath}")
    private String filePath;

    @Resource
    private SysLogMapper mapper;


    @SneakyThrows
    public String export(String logType) {

        String dateTimeNow = DateUtils.dateTimeNow();
        String path = filePath + TABLE_NAME + dateTimeNow;
        List<SysLogsVo> list = mapper.getLogsLists(logType);
        // 创建文件并写入
        try {
            FileWriter writer = new FileWriter(path + SystemGlobal.TEXT_FIX);
            list.forEach(logs -> {
                try {
                    // 根据你的表结构生成你需要的SQL行
                    String rowSql = "INSERT INTO sys_logs (log_id, opera_user_name,ip,user_id,model" +
                            ",oper_time,method,params,operate_detail,oper_type,oper_uri,log_type,result,times,failure_reason,grade,err_type,method_type,title,result_data) VALUES ('"
                            + isNotNull(logs.getLogId()) + "', '"
                            + isNotNull(logs.getOperaUserName()) + "', '"
                            + isNotNull(logs.getIp()) + "', '"
                            + isNotNull(logs.getUserId()) + "', '"
                            + isNotNull(logs.getModel()) + "', '"
                            + isNotNull(logs.getOperTime()) + "', '"
                            + isNotNull(logs.getMethod()) + "', '"
                            + isNotNull(logs.getParams()) + "', '"
                            + isNotNull(logs.getOperateDetail()) + "', '"
                            + isNotNull(logs.getOperType()) + "', '"
                            + isNotNull(logs.getOperUri()) + "', '"
                            + isNotNull(logs.getLogType()) + "', '"
                            + isNotNull(logs.getFailureReason()) + "', '"
                            + isNotNull(logs.getTimes()) + "', '"
                            + isNotNull(logs.getFailureReason()) + "', '"
                            + isNotNull(logs.getGrade()) + "', '"
                            + isNotNull(logs.getErrType()) + "', '"
                            + isNotNull(logs.getMethodType()) + "', '"
                            + isNotNull(logs.getTitle()) + "', '" +
                            isNotNull(logs.getResultData()) + "');";
                    writer.write(rowSql + System.lineSeparator());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

            });

        } catch (Exception e) {
            log.error(e.toString(), e);
        }

        return dateTimeNow;
    }

    public String isNotNull(Object object) {
        if (object == null) {
            return "";
        }
        return object.toString();

    }


}

