package com.dsk.cscec.controller;

import cn.hutool.core.lang.Assert;
import com.dsk.common.annotation.Log;
import com.dsk.common.core.controller.BaseController;
import com.dsk.common.core.domain.R;
import com.dsk.common.enums.BusinessType;
import com.dsk.common.exception.ServiceException;
import com.dsk.cscec.domain.CbProjectFile;
import com.dsk.cscec.domain.bo.AddProjectFileUploadBo;
import com.dsk.cscec.domain.vo.QueryProjectFileUploadDetailVo;
import com.dsk.cscec.service.CbProjectFileService;
import com.dsk.system.domain.vo.SysOssVo;
import com.dsk.system.service.ISysOssService;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.io.UnsupportedEncodingException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 项目成本文件表(CbProjectFile)表控制层
 *
 * @author sxk
 * @since 2024-02-05 14:01:03
 */
@RestController
@RequestMapping("cbProjectFile")
public class CbProjectFileController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private CbProjectFileService baseService;
    @Resource
    private ISysOssService iSysOssService;

    /**
     * 上传项目台账文件
     */
    @Log(title = "项目台账上传OSS对象存储", businessType = BusinessType.INSERT)
    @PostMapping(value = "/uploadCbProjectFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Map<String, String>> uploadCbProjectFile(@Validated @ModelAttribute AddProjectFileUploadBo fileUploadBo) throws UnsupportedEncodingException {
        MultipartFile file = fileUploadBo.getFile();
        Assert.notNull(file, "上传文件不能为空");
        //校验同一成本类型下是否存在相同文件
        baseService.checkProjectFileExist(fileUploadBo);
        SysOssVo oss = iSysOssService.upload(file);
        //添加项目上传文件记录
        CbProjectFile cbProjectFile;
        try {
            cbProjectFile = baseService.addProjectFile(fileUploadBo, oss);
        } catch (ServiceException e) {
            //删除oss中的文件
            iSysOssService.deleteWithValidByIds(Collections.singletonList(oss.getOssId()), true);
            throw e;
        }
        Map<String, String> map = new HashMap<>(2);
        map.put("fileId", cbProjectFile.getId().toString());
        map.put("fileName", oss.getOriginalName());
        map.put("fileOssId", oss.getOssId().toString());
        map.put("fileOssUrl", oss.getUrl());
        return R.ok(map);
    }

    /**
     * 删除项目台账文件
     */
    @DeleteMapping("/deleteCbProjectFile/{fileId}")
    public R<Void> deleteCbProjectFile(@NotNull(message = "文件ID不能为空") @PathVariable Long fileId) {
        return toAjax(baseService.deleteProjectFile(new Long[]{fileId}));
    }

    /**
     * 获取项目文件上传详情
     */
    @GetMapping("/getProjectFileUploadDetail/{projectId}")
    public R<QueryProjectFileUploadDetailVo> getProjectFileUploadDetail(@NotNull(message = "项目ID不能为空") @PathVariable Long projectId) {
        return R.ok(baseService.getProjectFileUploadDetail(projectId));
    }
}