package com.njcn.process.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.FileUtil;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.process.pojo.param.SupvAlarmParam;
import com.njcn.process.pojo.param.SupvFileParam;
import com.njcn.process.pojo.po.SupvAlarm;
import com.njcn.process.pojo.po.SupvFile;
import com.njcn.process.service.ISupvFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import liquibase.pro.packaged.S;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.njcn.web.controller.BaseController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hongawen
 * @since 2023-06-21
 */
@RestController
@RequestMapping("/supv/file")
@Api(tags = "技术监督附件控制器")
@RequiredArgsConstructor
public class SupvFileController extends BaseController {

    private final ISupvFileService iSupvFileService;


    @PostMapping("planUpload")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPLOAD)
    @ApiOperation("监督计划问题附件上传")
    public HttpResult<Object> planUpload(@ApiParam(value = "文件", required = true) @RequestPart("files") MultipartFile file,
                                         @RequestParam("planId") String planId,
                                         @RequestParam("type") Integer type,
                                         @RequestParam("uploaderId") String uploaderId,
                                         @RequestParam("uploaderName") String uploaderName,
                                         @RequestParam("attachmentType") String attachmentType,
                                         @RequestParam("uploadTime") String uploadTime
    ) {
        String methodDescribe = getMethodDescribe("planUpload");
        String originalFilename = file.getOriginalFilename();
        if (FileUtil.judgeFileIsWord(originalFilename) || FileUtil.judgeFileIsPdf(originalFilename) || FileUtil.judgeFileIsExcel(originalFilename)||FileUtil.judgeFileIsZip(originalFilename)) {
            if (!StrUtil.isAllNotBlank(planId, uploaderId, uploaderName, attachmentType, uploadTime) || type == null) {
                throw new BusinessException("必填字段不能为空");
            }
            iSupvFileService.planUpload(file, planId, type, uploaderId, uploaderName, attachmentType, uploadTime);
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        }

        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FILE_NAME_ERROR, null, methodDescribe);
    }

    @PostMapping("list")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("查询附件信息集合")
    @ApiImplicitParam(name = "param", value = "请求体", required = true)
    public HttpResult<List<SupvFile>> pageAlarm(@RequestBody SupvFileParam param) {
        String methodDescribe = getMethodDescribe("pageAlarm");
        List<SupvFile> supvFiles = iSupvFileService.listFile(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, supvFiles, methodDescribe);
    }

    @PostMapping("detail")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.DOWNLOAD)
    @ApiOperation("监督计划问题附件下载")
    public HttpResult<Object> detail(HttpServletResponse response, @RequestParam("busId") String busId, @RequestParam("type") Integer type, @RequestParam("attachmentType") String attachmentType) {
        String methodDescribe = getMethodDescribe("detail");
        iSupvFileService.detail(response, busId, type, attachmentType);
        return null;
    }


    @PostMapping("delFile")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.DELETE)
    @ApiOperation("监督计划问题附件下载")
    public HttpResult<Object> delFile(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("delFile");
        iSupvFileService.delFile(ids);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);

    }
}

