package com.njcn.supervision.controller.databank;

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.oss.utils.FileStorageUtil;
import com.njcn.redis.utils.RedisUtil;
import com.njcn.supervision.pojo.param.databank.LibCaseParam;
import com.njcn.supervision.pojo.po.databank.LibCase;
import com.njcn.supervision.service.databank.LibCaseService;
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.*;

/**
 * Description:
 * Date: 2024/9/10  16:49【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/libcase")
@Api(tags = "案例库")
@AllArgsConstructor
public class LibCaseController extends BaseController {

    private final LibCaseService libCaseService;
    private final RedisUtil redisUtil;
    private final FileStorageUtil fileStorageUtil;
    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增")
    @ApiImplicitParam(name = "libCaseParam", value = "实体参数", required = true)
    public HttpResult<LibCase> add(@RequestBody @Validated LibCaseParam libCaseParam) {
        String methodDescribe = getMethodDescribe("add");
        LibCase add = libCaseService.add(libCaseParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, add, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @PostMapping("/addStandardCase")
    @ApiOperation("新增标准案例")
    public HttpResult<String> addStandardCase(@RequestParam String caseUrl) {
        String methodDescribe = getMethodDescribe("addStandardCase");
        redisUtil.saveByKey("StandardCase",caseUrl);
        String fileUrl = fileStorageUtil.getFileUrl(caseUrl);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, fileUrl, methodDescribe);
    }
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryStandardCase")
    @ApiOperation("获取标准案例")
    public HttpResult<String> queryStandardCase() {
        String methodDescribe = getMethodDescribe("queryStandardCase");
        String standardCase = redisUtil.getStringByKey("StandardCase");
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, standardCase, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/updateCaseLibrary")
    @ApiOperation("更新")
    @ApiImplicitParam(name = "paramUpdate", value = "实体参数", required = true)
    public HttpResult<String> updateCaseLibrary(@RequestBody @Validated LibCaseParam.LibCaseParamUpdate paramUpdate) {
        String methodDescribe = getMethodDescribe("updateCaseLibrary");
        String id = libCaseService.updateCaseLibrary(paramUpdate);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, id, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON,operateType = OperateType.DELETE)
    @GetMapping("/deleteyById")
    @ApiOperation("通过id删除")
    public HttpResult<Boolean> deleteyById(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("deleteyById");
        boolean update = libCaseService.lambdaUpdate().set(LibCase::getStatus, 0).eq(LibCase::getId, id).update();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, update, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/queryById")
    @ApiOperation("根据id查询")
    public HttpResult<LibCase> queryById(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("queryById");
        LibCase result = libCaseService.lambdaQuery().eq(LibCase::getId,id).one();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/pageQuery")
    @ApiOperation("分页查询案例库")
    @ApiImplicitParam(name = "param", value = "参数", required = true)
    public HttpResult<Page<LibCase>> pageQuery(@RequestBody @Validated LibCaseParam.LibCaseParamQuery param) {
        String methodDescribe = getMethodDescribe("pageQuery");
        Page<LibCase> list = libCaseService.pageQuery(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }



}
