package com.dd.admin.business.file.controller;

import cn.hutool.core.bean.BeanUtil;
import com.dd.admin.common.model.result.UploadResultBean;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.dd.admin.common.model.UpdateGroup;
import com.dd.admin.common.model.result.ResultBean;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import com.dd.admin.business.file.entity.File;
import com.dd.admin.business.file.domain.FileVo;
import com.dd.admin.business.file.domain.FileDto;
import com.dd.admin.business.file.service.FileService;

import java.util.HashMap;
import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 文件 前端控制器
 * </p>
 *
 * @author 727869402@qq.com
 * @since 2024-05-23
 */
@Api(tags = "文件")
@RestController
public class FileController {

    @Autowired
    FileService fileService;
    @Autowired
    HttpServletRequest request;
    @Value("${server.port}")
    String port;

    @ApiOperation(value = "文件-分页列表")
    @ApiOperationSupport(order = 1)
    @GetMapping("/admin/file/page")
    public ResultBean<IPage<FileVo>> page(FileDto fileDto) {
        IPage<FileVo> pageInfo = fileService.selectFilePage(fileDto);

        return ResultBean.success(pageInfo);
    }

    @ApiOperation(value = "文件-列表")
    @ApiOperationSupport(order = 2)
    @GetMapping("/admin/file/list")
    public ResultBean<List<FileVo>> list(FileDto fileDto) {
        List<FileVo> list = fileService.selectFileList(fileDto);
        return ResultBean.success(list);
    }

    @ApiOperation(value = "文件-添加")
    @ApiOperationSupport(order = 3)
    @PostMapping("/admin/file/add")
    public ResultBean<File> add(@RequestBody @Validated FileDto fileDto) {
        File file = BeanUtil.copyProperties(fileDto, File.class);
        fileService.save(file);
        return ResultBean.success(file);
    }

    @ApiOperation(value = "文件-查询")
    @ApiOperationSupport(order = 4)
    @GetMapping("/admin/file/{fileId}")
    public ResultBean<FileVo> get(@PathVariable @NotBlank String fileId) {
        File file = fileService.getById(fileId);
        FileVo fileVo = BeanUtil.copyProperties(file,FileVo.class);
        return ResultBean.success(fileVo);
    }

    @ApiOperation(value = "文件-修改")
    @ApiOperationSupport(order = 5)
    @PostMapping("/admin/file/update")
    public ResultBean<File> update(@RequestBody @Validated(UpdateGroup.class) FileDto fileDto) {
        File file = BeanUtil.copyProperties(fileDto, File.class);
        fileService.updateById(file);
        return ResultBean.success(file);
    }

    @ApiOperation(value = "文件-删除")
    @ApiOperationSupport(order = 6)
    @GetMapping("/admin/file/delete/{fileId}")
    public ResultBean<File> delete(@PathVariable @NotBlank String fileId) {
        Boolean b = fileService.removeById(fileId);
        return ResultBean.success(b);
    }

    @ApiOperation(value = "文件-上传")
    @PostMapping("/upload")
    @ResponseBody
    public ResultBean layuiUpload(@RequestPart("file") MultipartFile file, String fileSavePath) {
        FileVo uploadResult = fileService.uploadFile(file,fileSavePath);
        String fileId = uploadResult.getFileId();
        String filePath = uploadResult.getFileSavePath();
        HashMap<String, Object> map = new HashMap<>();
        map.put("fileId", fileId);
        map.put("filePath",filePath);
        return ResultBean.success(map);
    }

    @ApiOperation(value = "文件-上传")
    @PostMapping("/appUpload")
    @ResponseBody
    public UploadResultBean<FileVo> appUpload(@RequestPart("file") MultipartFile file, String fileSavePath) {
        FileVo uploadResult = fileService.uploadFile(file,fileSavePath);
        String fileId = uploadResult.getFileId();
        String filePath = uploadResult.getFileSavePath();
        UploadResultBean resultBean = new UploadResultBean();
        resultBean.setResult(uploadResult);
        String serverName = request.getServerName();
        System.out.println(serverName);
        resultBean.setData("http://" + serverName + ":" + port + filePath);
        return resultBean;
    }
}
