package com.crm.custom.service.impl.base;

import com.crm.custom.service.SysConfigService;
import com.crm.rely.backend.configuration.LocaleMessage;
import com.crm.rely.backend.core.constant.GlobalConfigConstants;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.pojo.table.SysConfigTable;
import com.crm.rely.backend.core.exception.ServiceException;
import com.crm.rely.backend.util.FileProcessUtil;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

public class BaseUploadServiceImpl {
    @Autowired
    private LocaleMessage localeMessage;

    @Autowired
    private SysConfigService sysConfigService;

    @Value("${web.upload-path:./upload}")
    private String UPLOAD;

    public void validated(MultipartFile file) throws ServiceException {
        if (file == null) {
            throw ServiceException.exception(localeMessage.getMessage(Constants.UPLOAD_FILE_NOT_NULL, Constants.UPLOAD_FILE_NOT_NULL));
        }
        List<String> codes = new ArrayList<>(2);
        codes.add(GlobalConfigConstants.UPLOAD_SIZE_MAX);
        codes.add(GlobalConfigConstants.UPLOAD_REGEX);
        List<SysConfigTable> sysConfigTables = sysConfigService.getByCodes(codes);
        if (sysConfigTables == null || sysConfigTables.size() != 2) {
            throw ServiceException.exception(Constants.SYSTEM_ERROR);
        }
        int size = 0;
        String regex = null;
        for (SysConfigTable sysConfigTable : sysConfigTables) {
            if (GlobalConfigConstants.UPLOAD_SIZE_MAX.equals(sysConfigTable.getCode())) {
                try {
                    size = Integer.parseInt(sysConfigTable.getValue());
                } catch (NumberFormatException e) {
                    throw new ServiceException(Constants.SYSTEM_ERROR);
                }
            } else if (GlobalConfigConstants.UPLOAD_REGEX.equals(sysConfigTable.getCode())) {
                regex = sysConfigTable.getValue();
            }
        }

        validated(file, size, regex);
    }

    public void validatedVideo(MultipartFile file) throws ServiceException {
        if (file == null) {
            throw ServiceException.exception(localeMessage.getMessage(Constants.UPLOAD_FILE_NOT_NULL, Constants.UPLOAD_FILE_NOT_NULL));
        }
        List<String> codes = new ArrayList<>(2);
        codes.add(GlobalConfigConstants.UPLOAD_VIDEO_SIZE_MAX);
        codes.add(GlobalConfigConstants.UPLOAD_VIDEO_REGEX);
        List<SysConfigTable> sysConfigTables = sysConfigService.getByCodes(codes);
        if (sysConfigTables == null || sysConfigTables.size() != 2) {
            throw ServiceException.exception(Constants.SYSTEM_ERROR);
        }
        int size = 0;
        String regex = null;
        for (SysConfigTable sysConfigTable : sysConfigTables) {
            if (GlobalConfigConstants.UPLOAD_VIDEO_SIZE_MAX.equals(sysConfigTable.getCode())) {
                try {
                    size = Integer.parseInt(sysConfigTable.getValue());
                } catch (NumberFormatException e) {
                    throw new ServiceException(Constants.SYSTEM_ERROR);
                }
            } else if (GlobalConfigConstants.UPLOAD_VIDEO_REGEX.equals(sysConfigTable.getCode())) {
                regex = sysConfigTable.getValue();
            }
        }

        validated(file, size, regex);
    }

    private void validated(MultipartFile file, int size, String regex) throws ServiceException {
        //验证文件大小
        if (file.getSize() > size * 1000 * 1024) {
            throw new ServiceException(localeMessage.getMessage(Constants.UPLOAD_FILE_SIZE_MAX) + size + "MB");
        }
        //文件格式
        String fileName = file.getOriginalFilename().toLowerCase();
        if (!fileName
                .substring(fileName.lastIndexOf(".") + 1, fileName.length())
                .matches(regex)) {
            throw ServiceException.exception(localeMessage.getMessage(Constants.UPLOAD_FILE_REGEX, Constants.UPLOAD_FILE_REGEX) + " " + regex);

        }
    }


    protected String saveFile(String middlePath, MultipartFile file) {
        if (!Strings.isNullOrEmpty(middlePath) && !middlePath.startsWith("/")) {
            middlePath = "/" + middlePath;
        }
        String savePath = UPLOAD + (Strings.isNullOrEmpty(middlePath) ? ("/file/common/") : middlePath);

        String path = FileProcessUtil.upload(file, savePath);
        return (Strings.isNullOrEmpty(middlePath) ? ("/file/common/") : middlePath) + path;
    }
}
