package com.ltgk.common.constant;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 常量类
 */
@Component
public final class SysConstant extends Constants {

    /**
     * 历史短信状态
     */
    public final static int SMSMESSAGE_STATUS_SUCCESS = 1; //发送成功
    public final static int SMSMESSAGE_STATUS_FAIL = 2; //发送失败
    public final static int SMSMESSAGE_STATUS_RECEIPTING = 3; //回执中

    /**
     * 船短信发送状态
     */
    public final static String SHIP_MESSAGE_STATUS_SUCCESS = "0"; //已成功发送到数据中心

    //审核状态
    public final static String CHECK_STATUS_UNCHECKED = "0";//未审核
    public final static String CHECK_STATUS_PASS = "1";//审核通过
    public final static String CHECK_STATUS_FAIL = "2";//审核失败
    public final static String CHECK_STATUS_DRAFT = "3";//草稿

    /**
     * 登录密码错误缓存前缀
     */
    public final static String LOGIN_PASSWORD_ERROR_PREFIX = "pwdError-";
    //经营人状态
    public final static String OPERATOR_STATUS_UNBUNDLING = "3";//解绑

    //船舶管理人类型
    public final static String BOAT_OPERATOR_SHIPOWNER = "1";//船东
    public final static String BOAT_OPERATOR_CAPTAIN = "船长";//船长

    //合同状态
    public final static String CONTRACT_NOTBEGIN = "0";//未开始
    public final static String CONTRACT_NORMAL = "1";//正常
    public final static String CONTRACT_UNBIND = "2";//解绑

    //渔船状态
    public final static String BOAT_STATUS_NEW = "0";//新建
    public final static String BOAT_STATUS_NORMAL = "1";//正常
    public final static String BOAT_STATUS_LOGOOUT = "6";//已注销
    public final static String BOAT_STATUS_SCRAP = "5";//报废
    public final static String BOAT_STATUS_SL = "2";//失联
    public final static String BOAT_STATUS_CM = "3";//沉没
    public final static String BOAT_STATUS_CJ = "4";//拆解

    //黑名单、重点关注
    public final static String FISH_BLACKLIST_ADD = "是";//新增
    public final static String FISH_BLACKLIST_REMOVE = "否";//移除
    public final static String BOAT_JOIN_BLACKLIST = "blacklist";
    public final static String BOAT_JOIN_FOCUS = "focus";


    // 固定的字典Code值
    public final static String DICT_CODE_EQUIPMENTSTATUS = "equipmentStatus";     // 设备状态
    public static final String DICT_CODE_HOMEWORKTYPE = "homeworkType"; // 渔船状态
    public static final String DICT_CODE_WORKTYPE = "workType"; // 渔船状态

    /**
     * 删除标志
     */
    public static final Integer DELETEFLAG_N = 0;//未删
    public static final Integer DELETEFLAG_Y = 1;//已删

    /**
     * 调度任务状态
     */
    public static final Integer TASK_STATUS_ACCEPT = 1;
    public static final Integer TASK_STATUS_FINISH = 2;

    public static final String RFID_TYPE_ACTIVE = "有源";
    public static final String RFID_TYPE_PASSIVE = "无源";

    public static final String ALARM_SHUTDOWN_DEPARTURE = "离港不开机";
    public static final String ALARM_SHUTDOWN_ARRIVAL = "进港不开机";


    /* --------------------- 获取配置中信息作为常量 -------------------------- */

    /**
     * 文件存放的根目录
     */
    public static String FILE_ROOT = "/files/";

    @Value("${path.templatePath}")
    public void setFileRoot(String rootPath) {
        String basePath = "";
        if (FileUtil.isAbsolutePath(rootPath)) {
            basePath = rootPath;
        } else {
            String jarPath = jarPath();
            basePath = jarPath + "/" + rootPath;
        }
        // 保证文件目录结尾有斜线结束
        FILE_ROOT = basePath + (basePath.endsWith("/") ? "" : "/");
    }


    /**
     * 模板文件的根目录
     */
    public static String TEMPLATE_PATH = "exportTemplate/";
    /**
     * 临时文件的路径，缓存文件
     */
    public static String TEMP_PATH = "temp/";


    /**
     * 文件存储目录
     */
    public static String FILE_PATTERN = "YYYY-MM/";

    @Value("${path.filePattern}")
    public void setFilePattern(String filePattern) {
        FILE_PATTERN = filePattern;
    }

    /**
     * 当前运行jar包的工作目录
     * 结果为：D:\编码实现\HaierResearch
     */
    public static String jarPath() {
//        ApplicationHome home = new ApplicationHome(StarterApplication.class);
//        String homePath = home.getDir().toString();
//        homePath = homePath.replace("\\", "/");
//        return homePath;
        String property = System.getProperty("user.dir");
        property = property.replace("\\", "/");
        return property;
    }

    /**
     * 获取存储用文件的绝对路径,带后缀则为文件，不带后缀则为路径
     *
     * @param fileType 文件类型, 获取路径，则传入：”“
     * @return
     */
    public static String tempPath(String fileType) {
        // 文件名称、路径的处理
        fileType = StringUtils.isBlank(fileType) ? "" : ("." + fileType);

        String tempName = IdUtil.simpleUUID() + fileType;
        String tempPath = FILE_ROOT + TEMP_PATH + tempName;    // 相对路径

        return tempPath;
    }

    /**
     * 返回模板的绝对路径
     *
     * @param templateName 模板名称
     * @return
     */
    public static String templatePath(String templateName) {
        if (StringUtils.isBlank(templateName)) {
            return null;
        }
        return FILE_ROOT + TEMPLATE_PATH + templateName;
    }

    /**
     * 获取存储用文件的相对路径,带后缀则为文件，不带后缀则为路径
     *
     * @param fileType 文件类型, 获取路径，则传入：”“
     * @return
     */
    public static String savePath(String fileType) {
        // 文件名称、路径的处理
        String dirName = DateFormatUtils.format(new Date(), FILE_PATTERN);
        fileType = StringUtils.isBlank(fileType) ? "" : ("." + fileType);

        String saveFileName = IdUtil.simpleUUID() + fileType;
        String relativePath = dirName + saveFileName;    // 相对路径

        return relativePath;
    }
}
