package com.mcwl.resource.service.impl;

import com.mcwl.common.constant.HttpStatus;
import com.mcwl.common.core.domain.AjaxResult;
import com.mcwl.common.core.domain.R;
import com.mcwl.common.utils.StringUtils;
import com.mcwl.resource.domain.ModelVersion;
import com.mcwl.resource.domain.WorkFlowVersion;
import com.mcwl.resource.domain.request.RequestFile;
import com.mcwl.resource.mapper.ModelVersionMapper;
import com.mcwl.resource.mapper.WorkFlowVersionMapper;
import com.mcwl.resource.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 文件实现层
 * @author DaiZibo
 * @date 2025/2/14
 * @apiNote
 */

@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private ModelVersionMapper versionMapper;

    @Autowired
    private WorkFlowVersionMapper workFlowVersionMapper;

    @Override
    public AjaxResult selectFileName(String name, String type) {

        if (StringUtils.isEmpty(type)){

            return AjaxResult.error(HttpStatus.SHOW_ERROR_MSG,"请输入type");
        }

        if (type.equals("model")) {

            ModelVersion modelVersion = versionMapper.selectByEncryptionFileName(name);
            if (modelVersion == null) {

                return AjaxResult.error(HttpStatus.SHOW_ERROR_MSG,"文件不存在");
            }

            return AjaxResult.success(modelVersion);
        } else if (type.equals("workflow")){
            WorkFlowVersion workFlowVersion = workFlowVersionMapper.selectByFileName(name);
            if (workFlowVersion == null) {

                return AjaxResult.error(HttpStatus.SHOW_ERROR_MSG,"文件不存在");
            }

            return AjaxResult.success(workFlowVersion);
        }

        return AjaxResult.error(HttpStatus.SHOW_ERROR_MSG,"type不合法");
    }

    @Override
    public AjaxResult selectFile(String name, String type) {

        if (StringUtils.isEmpty(type)){

            return AjaxResult.error(HttpStatus.SHOW_ERROR_MSG,"请输入type");
        }

        if (type.equals("model")) {

            ModelVersion modelVersion = versionMapper.selectByFileName(name);
            if (modelVersion != null) {

                return AjaxResult.success(0);
            }

            return AjaxResult.success(1);
        }else if (type.equals("workflow")){
            WorkFlowVersion workFlowVersion = workFlowVersionMapper.selectByFileName(name);
            if (workFlowVersion != null) {

                return AjaxResult.success(0);
            }
            return AjaxResult.success(1);
        }

        return AjaxResult.error(HttpStatus.SHOW_ERROR_MSG,"type不合法");

    }

    @Override
    public R selectHash(String hashCode, Integer type) {

        //0模型 1工作流
        if (type == 0){

            ModelVersion modelVersion = versionMapper.selectByHash(hashCode);
            if (modelVersion != null){

                return R.ok(0);
            }
            return R.ok(1);
        }else {

            WorkFlowVersion workFlowVersion = workFlowVersionMapper.selectByHash(hashCode);
            if (workFlowVersion != null){

                return R.ok(0);
            }
            return R.ok(1);
        }
    }

    @Override
    public R updateFileData(RequestFile requestFile) {

        //校验是否是模型
        if (requestFile.getFileType().equals("model")){

            //修改模型
            ModelVersion modelVersion = ModelVersion.builder().id(requestFile.getId())
                    .keyRate(requestFile.getKeyRate())
                    .encryptionFilePath(requestFile.getEncryptionFilePath())
                    .encryptionObjectKey(requestFile.getEncryptionObjectKey())
                    .encryptionFileName(requestFile.getEncryptionFileName())
                    .encryptionFileSize(requestFile.getEncryptionFileSize()).build();

            versionMapper.updateById(modelVersion);

            return R.ok();
        }else {

            //修改工作流
//            WorkFlowVersion workFlowVersion = WorkFlowVersion.builder().id(requestFile.getId())
//                    .keyRate(requestFile.getKeyRate())
//                    .encryptionFilePath(requestFile.getEncryptionFilePath())
//                    .encryptionObjectKey(requestFile.getEncryptionObjectKey()).build();
//
//            versionMapper.updateById(modelVersion);

            return R.ok();
        }
    }
}
