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.FileInfo;
import com.persagy.dmp.file.service.IFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

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

    @Autowired
    IFileService fileService;

    @PostMapping("/insert")
    public CommonResult<FileInfo> insert(@RequestBody FileInfo fileInfo) {
        if(StrUtil.isBlank(fileInfo.getFileName()) ||
                StrUtil.isBlank(fileInfo.getGroupCode()) ||
                StrUtil.isBlank(fileInfo.getFileBucket())) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        fileInfo.setCreationTime(new PsDateTime());
        fileInfo.setValid(ValidEnum.TRUE.getType());
        fileInfo = fileService.insertFile(fileInfo);
        return ResultHelper.single(fileInfo);
    }

    @PostMapping("/update")
    public CommonResult<FileInfo> update(@RequestBody FileInfo fileInfo) {
        if(StrUtil.isBlank(fileInfo.getId()) ||
                StrUtil.isBlank(fileInfo.getFileName()) ||
                StrUtil.isBlank(fileInfo.getGroupCode()) ||
                StrUtil.isBlank(fileInfo.getFileBucket())) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        fileInfo.setModifiedTime(new PsDateTime());
        fileInfo = fileService.updateFile(fileInfo);
        return ResultHelper.single(fileInfo);
    }

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

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

    @PostMapping("/queryByBusiness")
    public CommonResult<List<FileInfo>> queryByBusiness(@RequestBody String businessId) {
        if(StrUtil.isBlank(businessId)) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        List<FileInfo> voList = fileService.queryByBusinessId(businessId);
        return ResultHelper.multi(voList);
    }
}
