package com.njcn.harmonic.controller.algorithm;

import cn.hutool.core.bean.BeanUtil;
import com.njcn.common.pojo.annotation.OperateInfo;
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.harmonic.pojo.param.RStatFileVO;
import com.njcn.harmonic.pojo.po.RStatFile;
import com.njcn.harmonic.service.algorithm.RStatFileService;
import com.njcn.oss.constant.OssPath;
import com.njcn.oss.utils.FileStorageUtil;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

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

import java.util.*;


/**
 * @author hongawen
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "算法")
@RequestMapping("/algorithm")
public class AlgorithmController extends BaseController {

    private final RStatFileService rStatFileService;

    private final FileStorageUtil fileStorageUtil;




    /**
     * 算法保存
     */
    @PostMapping("/getAlgorithmSave")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("算法保存")
    @ApiImplicitParam(name = "param", value = "算法内容", required = true)
    public HttpResult<Boolean> getAlgorithmSave(@RequestBody RStatFileVO param) {
        String methodDescribe = getMethodDescribe("getAlgorithmSave");
        RStatFile rStatFile = BeanUtil.copyProperties(param, RStatFile.class);
        boolean b = rStatFileService.updateById(rStatFile);
        if (b) {
//            minIoUtils.removeObjects( minIoProperties.getBucket(),param.getAddress());
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

    @PostMapping("/getAlgorithmAll")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("算法查询")
    public HttpResult<List<RStatFile>> getAlgorithmAll() {
        String methodDescribe = getMethodDescribe("getAlgorithmAll");
        List<RStatFile> list = rStatFileService.list();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);

    }

    /**
     * 通用上传请求
     */
    @PostMapping("/common/upload")
    @ResponseBody
    public HttpResult<String> uploadFile(MultipartFile file) {
        try {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,fileStorageUtil.getFileUrl(fileStorageUtil.uploadMultipart(file,OssPath.ALGORITHM)), null);
        } catch (Exception e) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, null);
        }
    }

}
