package com.crm.manager.controller;

import com.crm.manager.service.CommonService;
import com.crm.manager.service.ucard.UcardFileFeignService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.ucard.CardFileDto;
import com.crm.rely.backend.core.entity.base.BaseDeleteEntities;
import com.crm.rely.backend.core.entity.ucard.CardFileAddEntity;
import com.crm.rely.backend.core.entity.ucard.CardFilePageEntity;
import com.crm.rely.backend.core.entity.ucard.CardFileUpdateEntity;
import com.crm.rely.backend.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * ucard文件服务接口
 */
@Slf4j
@RestController
@RequestMapping("/ucard/file")
public class UardFileController {

    @Autowired
    private UcardFileFeignService ucardFileFeignService;
    @Autowired
    private CommonService commonService;

    /**
     * 上传文件
     */
    @PostMapping("/upload")
    public BaseResultDto<String> uploadFile(@RequestParam(value = "file") MultipartFile file) throws Exception {
        String middlePath = "/wasabi";
        ucardFileFeignService.validatedFile(file);
        String path = commonService.uploadFile(middlePath, file);

        return BaseResultDto.success(Constants.UPLOAD_SUCCESS, path);
    }

    /**
     * 上传文件
     */
    @PostMapping("/manager/upload")
    public BaseResultDto<String> managerUploadFile(@RequestParam(value = "file") MultipartFile file) throws Exception {
        String middlePath = "/wasabi";
        String path = commonService.uploadFile(middlePath, file);
        return BaseResultDto.success(Constants.UPLOAD_SUCCESS, path);
    }

    /**
     * 上传视频
     */
    @PostMapping("/upload/video")
    public BaseResultDto cardFileUpload(@RequestParam(value = "file", required =
            false) MultipartFile file) throws Exception {
        String middlePath = "/wasabi/video";
        commonService.validatedVideo(file);
        String path = commonService.uploadFile(middlePath, file);

        return BaseResultDto.success(Constants.UPLOAD_SUCCESS, path);
    }

    /**
     * 添加文件记录
     */
    @PostMapping("/add")
    public BaseResultDto cardFileAdd(@RequestBody @Validated CardFileAddEntity entity) throws ServiceException {
        return ucardFileFeignService.cardFileAdd(entity);
    }

    /**
     * 修改文件
     */
    @PostMapping("/update")
    public BaseResultDto cardFileUpdate(@RequestBody @Validated CardFileUpdateEntity entity) throws ServiceException {
        return ucardFileFeignService.cardFileUpdate(entity);
    }

    /**
     * 删除文件
     */
    @PostMapping("/delete")
    public BaseResultDto cardFileDelete(@RequestBody BaseDeleteEntities entity) throws ServiceException {
        return ucardFileFeignService.cardFileDelete(entity);
    }

    /**
     * 文件分页列表
     */
    @PostMapping("/search/list")
    public ResultWithPagerDto<CardFileDto> cardFileSearchList(@RequestBody CardFilePageEntity entity) throws ServiceException {
        return ucardFileFeignService.cardFileSearchList(entity);
    }

}
