package com.njcn.process.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
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.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.minioss.bo.MinIoUploadResDTO;
import com.njcn.oss.constant.OssPath;
import com.njcn.oss.utils.FileStorageUtil;
import com.njcn.process.pojo.param.RFlowProcessParm;
import com.njcn.process.pojo.param.RFlowProcessQueryParm;
import com.njcn.process.pojo.vo.applyform.RFlowProcessVO;
import com.njcn.process.service.RFlowProcessPOService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * Description:
 * 接口文档访问地址：http://serverIP:port/swagger-ui.html
 * Date: 2022/11/11  14:50【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */

@Slf4j
@RestController
@RequestMapping("/rFlowProcess")
@Api(tags = "申请单")
@AllArgsConstructor
public class RFlowProcessController extends BaseController {

    private final RFlowProcessPOService rFlowProcessPOService;
    private final FileStorageUtil fileStorageUtil;

    /**
     * @Description: 新增申请单
     * @Param: [rGeneralSurveyPlanAddParm]
     * @return: com.njcn.common.pojo.response.HttpResult<java.lang.Boolean>
     * @Author: clam
     * @Date: 2022/11/22
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/add")
    @ApiOperation("新增申请单")
    @ApiImplicitParam(name = "rFlowProcessParm", value = "新增普测计划参数", required = true)
    public HttpResult<Boolean> add(@RequestBody @Validated RFlowProcessParm rFlowProcessParm) {
        String methodDescribe = getMethodDescribe("add");
        Boolean addFlag = rFlowProcessPOService.add(rFlowProcessParm);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, addFlag, methodDescribe);
    }
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/createCheckflow")
    @ApiOperation("提交申请单审核")
    public HttpResult<Boolean> createCheckflow(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("createCheckflow");
        Boolean addFlag = rFlowProcessPOService.createCheckflow(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, addFlag, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/update")
    @ApiOperation("更新申请单")
    @ApiImplicitParam(name = "rFlowProcessParm", value = "新增普测计划参数", required = true)
    public HttpResult<Boolean> update(@RequestBody @Validated RFlowProcessParm.RFlowProcessUpdate rFlowProcessParm) {
        String methodDescribe = getMethodDescribe("update");
        Boolean addFlag = rFlowProcessPOService.update(rFlowProcessParm);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, addFlag, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/check")
    @ApiOperation("审核申请单")
    @ApiImplicitParam(name = "rFlowProcessCheck", value = "新增普测计划参数", required = true)
    public HttpResult<Boolean> check(@RequestBody @Validated RFlowProcessParm.RFlowProcessCheck rFlowProcessCheck) {
        String methodDescribe = getMethodDescribe("check");
        Boolean addFlag = rFlowProcessPOService.check(rFlowProcessCheck);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, addFlag, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/file")
    @ApiOperation("归档")
    public HttpResult<Boolean> file(@RequestParam String id) {
        String methodDescribe = getMethodDescribe("file");
        Boolean addFlag = rFlowProcessPOService.file(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, addFlag, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPLOAD)
    @PostMapping("/uploadFile")
    @ApiOperation("上传文件")
    public HttpResult<MinIoUploadResDTO> uploadFile( MultipartFile file){
        String methodDescribe = getMethodDescribe("uploadFile");
        String filePath = fileStorageUtil.uploadMultipart(file, OssPath.ELECTRICITY_QUALITY);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,new MinIoUploadResDTO(file.getOriginalFilename(),filePath), methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPLOAD)
    @PostMapping("/getFileUrl")
    @ApiOperation("根据文件地址获取文件服务器地址")
    public HttpResult<String> getFileUrl( String filePath){
        String methodDescribe = getMethodDescribe("getFileUrl");
        String url = fileStorageUtil.getFileUrl(filePath);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,url, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getFlowProcess")
    @ApiOperation("查询申请单")
    @ApiImplicitParam(name = "rFlowProcessQueryParm", value = "参数", required = true)
    public HttpResult<IPage<RFlowProcessVO>> getFlowProcess(@RequestBody @Validated RFlowProcessQueryParm rFlowProcessQueryParm){
        String methodDescribe = getMethodDescribe("getFlowProcess");
        Page<RFlowProcessVO> out = rFlowProcessPOService.getFlowProcess(rFlowProcessQueryParm);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, out, methodDescribe);
    }




}
