package com.miaxis.pc.controller;

import java.util.List;
import java.util.Arrays;

import com.miaxis.common.config.MiaxisConfig;
import com.miaxis.common.exception.CustomException;
import com.miaxis.common.utils.file.FileUploadUtils;
import com.miaxis.framework.config.ServerConfig;
import com.miaxis.system.dto.common.UploadFileDTO;
import io.swagger.annotations.*;
import com.miaxis.common.core.domain.Response;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.miaxis.common.annotation.Log;
import com.miaxis.common.core.controller.BaseController;
import com.miaxis.common.enums.BusinessTypeEnum;
import com.miaxis.file.domain.FileInfo;
import com.miaxis.file.service.IFileInfoService;
import com.miaxis.common.utils.poi.ExcelUtil;
import com.miaxis.common.core.page.ResponsePageInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 【文件】Controller
 *
 * @author miaxis
 * @date 2023-01-12
 */
@RestController
@RequestMapping("/file/info")
@Api(tags={"【pc-报价相关文件】"})
public class FileInfoController extends BaseController{
    @Autowired
    private IFileInfoService fileInfoService;
    @Autowired
    private ServerConfig serverConfig;


    /**
     * 查询文件列表
     */
//    @PreAuthorize("@ss.hasPermi('file:info:list')")
    @GetMapping("/list")
    @ApiOperation("查询文件列表")
        @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页码" ,dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "pageSize",value = "每页数据量" , dataType = "int", paramType = "query", required = false),
    })
    public ResponsePageInfo<FileInfo> list(@ModelAttribute FileInfo fileInfo){
        startPage();
        List<FileInfo> list = fileInfoService.selectFileInfoList(fileInfo);
        return toResponsePageInfo(list);
    }


    /**
     * 获取文件详细信息
     */
//    @PreAuthorize("@ss.hasPermi('file:info:query')")
    @GetMapping(value = "/{fileId}")
    @ApiOperation("获取文件详细信息")
    public Response<FileInfo> getInfo(
            @ApiParam(name = "fileId", value = "文件参数", required = true)
            @PathVariable("fileId") Long fileId
    ){
        return Response.success(fileInfoService.getById(fileId));
    }

    /**
     * 上传文件
     */
//    @PreAuthorize("@ss.hasPermi('file:info:add')")
    @Log(title = "上传文件", businessType = BusinessTypeEnum.INSERT)
    @PostMapping
    @ApiOperation("上传文件")
    public Response add(@RequestPart("file") MultipartFile file,@RequestParam Long id,
                        @ApiParam(name = "type", value = "文件类型 1-总报价 2-动力报价 3-控制器报价 4-发电机50hz报价 4-发电机60hz报价",
                                required = true) @RequestParam Integer type){
        try
        {
            // 上传文件路径
            String filePath = MiaxisConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            FileInfo fileInfo = new FileInfo();
            fileInfo.setFilePath(fileName);
            fileInfo.setFileUrl(url);
            if (type ==1 ){
                fileInfo.setPriceId(id);
            }else if (type ==2){
                fileInfo.setEngineId(id);
            }else if (type ==3){
                fileInfo.setControllerId(id);
            }else if (type ==4){
                fileInfo.setFdj50hzId(id);
            }else if (type ==5){
                fileInfo.setFdj60hzId(id);
            }else {
                throw new CustomException("参数值有误");
            }

            fileInfoService.save(fileInfo);
            return Response.success();
        }
        catch (Exception e)
        {
            return Response.error().setMsg(e.getMessage());
        }

    }


    /**
     * 删除文件
     */
//    @PreAuthorize("@ss.hasPermi('file:info:remove')")
    @Log(title = "文件", businessType = BusinessTypeEnum.DELETE)
	@DeleteMapping("/{fileIds}")
    @ApiOperation("删除文件")
    public  Response<Integer> remove(
            @ApiParam(name = "fileIds", value = "文件ids参数", required = true)
            @PathVariable Long[] fileIds
    ){
        return toResponse(fileInfoService.removeByIds(Arrays.asList(fileIds)) ? 1 : 0);
    }
}
