package cn.iocoder.yudao.module.infra.api.file;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.infra.api.file.dto.FileCreateReqDTO;
import cn.iocoder.yudao.module.infra.api.file.dto.UserFileUpdateReqDTO;
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.BpmFileUploadReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.UserFileUpdateReqVO;
import cn.iocoder.yudao.module.infra.service.file.FileService;
import lombok.SneakyThrows;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
public class FileApiImpl implements FileApi {

    @Resource
    private FileService fileService;

    @Override
    public CommonResult<String> uploadBpmFileProcessInstanceId(FileCreateReqDTO createReqDTO) {
        BpmFileUploadReqVO reqVO = new BpmFileUploadReqVO() ;
        reqVO.setProcessInstanceId(createReqDTO.getProcessInstanceId()) ;
        reqVO.setUrls(createReqDTO.getUrls()) ;
        try {
            fileService.uploadBpmFileProcessInstanceId(reqVO);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return success("suceess") ;
    }

    @Override
    public CommonResult<String> createFile(FileCreateReqDTO createReqDTO) {
        return success(fileService.createFile(createReqDTO.getName(), createReqDTO.getPath(),
                createReqDTO.getContent()));
    }

    @Override
    public CommonResult<String> getUserSignImgPath(Long userId){
        return success(fileService.getUserSignImgPath(userId)) ;
    }

    @Override
    public CommonResult<Map<Long, String>> getUserSignImgPathMap(List<Long> userIds) {

        return success(fileService.getUserSignImgPathMap(userIds));
    }

    @Override
    public String createBusinessFile(Long bussinessType, String name, byte[] content) {
        return fileService.createBusinessFile(bussinessType, name, content) ;
    }

    @Override
    public String updateBusinessFileContent(String url, Long businessType, String name, byte[] content) {
        return fileService.updateBusinessFileContent(url, businessType, name, content) ;
    }

    @Override
    @SneakyThrows
    public CommonResult<Boolean> updateBusinessFile(List<String> url, String businessInstanceId) {

        fileService.uploadBusinessFile(url, businessInstanceId);
        return success(true);
    }

    @Override
    public CommonResult<Boolean> deleteBpmFile(String url) throws Exception {

        fileService.deleteFile(url);

        return success(true);
    }

    @Override
    public CommonResult<Boolean> updateUserFileUserId(UserFileUpdateReqDTO updateReqVO) {

        UserFileUpdateReqVO reqVO = BeanUtils.toBean(updateReqVO, UserFileUpdateReqVO.class);
        fileService.updateUserFileUserId(reqVO);
        return success(true);
    }
}
