package com.vetti.common.core.service;

import com.vetti.common.config.RuoYiConfig;
import com.vetti.common.constant.Constants;
import com.vetti.common.core.domain.BaseEntity;
import com.vetti.common.core.domain.model.LoginUser;

import java.io.File;
import java.util.Date;
import java.util.UUID;

import static com.vetti.common.utils.SecurityUtils.getLoginUser;

/**
 * 基础业务
 *
 * @author ruoyi
 */
public abstract class BaseServiceImpl {

    /**
     * 系统共通字段-填充
     */
    protected void fill(String fillType, BaseEntity baseEntity) {
        Date currentDate = new Date();
        LoginUser loginUser = getLoginUser();
        String currentUserId = "";
        Long deptId = 0L;
        if(loginUser != null){
            currentUserId = String.valueOf(loginUser.getUserId());
            if (currentUserId == null || "".equals(currentUserId)) {
                currentUserId = "system";
            }
            deptId = loginUser.getDeptId();
        }
        switch (fillType) {
            case "INSERT":
                baseEntity.setCreateTime(currentDate);
                baseEntity.setCreateBy(currentUserId);
                baseEntity.setUpdateTime(currentDate);
                baseEntity.setUpdateBy(currentUserId);
                baseEntity.setDelFlag(Constants.DEL_FLAG_ZERO);
//                baseEntity.setVersionNo(0L);
                break;
            case "UPDATE":
                baseEntity.setUpdateTime(currentDate);
                baseEntity.setUpdateBy(currentUserId);
                break;
            default:
        }
    }

    /**
     * 系统共通字段-填充
     */
    protected void fillJob(String fillType, BaseEntity baseEntity) {
        Date currentDate = new Date();
        String currentUserId = "system";
        switch (fillType) {
            case "INSERT":
                baseEntity.setCreateTime(currentDate);
                baseEntity.setCreateBy(currentUserId);
                baseEntity.setUpdateTime(currentDate);
                baseEntity.setUpdateBy(currentUserId);
                baseEntity.setDelFlag(Constants.DEL_FLAG_ZERO);
//                baseEntity.setVersionNo(0L);
                break;
            case "UPDATE":
                baseEntity.setUpdateTime(currentDate);
                baseEntity.setUpdateBy(currentUserId);
                break;
            default:
        }
    }

    /**
     * 编码文件名
     */
    protected String encodingFilename(String filename) {
        filename = UUID.randomUUID().toString() + "_" + filename + ".xls";
        return filename;
    }

    /**
     * 获取下载路径
     *
     * @param filename 文件名称
     */
    protected String getAbsoluteFile(String filename) {
        String downloadPath = RuoYiConfig.getDownloadPath() + filename;
        File desc = new File(downloadPath);
        if (!desc.getParentFile().exists()) {
            desc.getParentFile().mkdirs();
        }
        return downloadPath;
    }


}
