package com.persagy.dmp.file.controller;

import cn.hutool.core.util.StrUtil;
import com.persagy.dmp.common.constant.ResponseCode;
import com.persagy.dmp.common.constant.ValidEnum;
import com.persagy.dmp.common.exception.BusinessException;
import com.persagy.dmp.common.lang.PsDateTime;
import com.persagy.dmp.common.model.response.CommonResult;
import com.persagy.dmp.common.utils.ResultHelper;
import com.persagy.dmp.file.model.FileMd5;
import com.persagy.dmp.file.service.FileMd5Service;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 文件管理Controller
 * @author Charlie Yu
 * @date 2021-05-15
 */
@RestController
@RequestMapping("/file/md5")
@RequiredArgsConstructor
public class FileMd5Controller {

    private final FileMd5Service fileMd5Service;

    @PostMapping("/insert")
    public CommonResult<FileMd5> insert(@RequestBody FileMd5 fileMd5) {
        if(StrUtil.isBlank(fileMd5.getFileMd5())
                || StrUtil.isBlank(fileMd5.getFileBucket())) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        fileMd5.setCreationTime(new PsDateTime());
        fileMd5.setValid(ValidEnum.TRUE.getType());
        fileMd5 = fileMd5Service.insertFileMd5(fileMd5);
        return ResultHelper.single(fileMd5);
    }

    @PostMapping("/update")
    public CommonResult<FileMd5> update(@RequestBody FileMd5 fileMd5) {
        if(StrUtil.isBlank(fileMd5.getId())
                || StrUtil.isBlank(fileMd5.getFileMd5())) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        fileMd5.setModifiedTime(new PsDateTime());
        fileMd5 = fileMd5Service.updateFileMd5(fileMd5);
        return ResultHelper.single(fileMd5);
    }

    @PostMapping("/delete")
    public CommonResult<String> delete(@RequestBody String id) {
        if(StrUtil.isBlank(id)) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        FileMd5 fileMd5 = fileMd5Service.load(id);
        if(fileMd5 == null) {
            throw new BusinessException("找不到对应的文件信息");
        }
        fileMd5Service.deleteFileMd5(fileMd5);
        return ResultHelper.success();
    }

    @PostMapping("/load")
    public CommonResult<FileMd5> load(@RequestBody String id) {
        if(StrUtil.isBlank(id)) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        FileMd5 fileMd5 = fileMd5Service.load(id);
        if(fileMd5 == null) {
            throw new BusinessException("找不到对应的文件信息");
        }
        return ResultHelper.single(fileMd5);
    }

    @PostMapping("/loadByMd5")
    public CommonResult<FileMd5> loadByMd5(@RequestBody String md5) {
        if(StrUtil.isBlank(md5)) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        return ResultHelper.single(fileMd5Service.queryFileMd5ByFileMd5(md5));
    }

    @PostMapping("/ensureFilePath")
    public CommonResult<FileMd5> ensureFilePath(@RequestBody FileMd5 fileMd5) {
        if(null==fileMd5) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        fileMd5Service.ensureFilePath(fileMd5,fileMd5.getFileName());
        return ResultHelper.single(fileMd5);
    }



}
