package cn.iocoder.byzs.module.web.controller.admin.reportprogress;

import cn.iocoder.byzs.framework.common.pojo.CommonResult;
import cn.iocoder.byzs.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.byzs.module.web.controller.admin.reportprogress.vo.WebReportProgressDO;
import cn.iocoder.byzs.module.web.controller.admin.reportprogress.vo.WebReportProgressVO;
import cn.iocoder.byzs.module.web.service.reportprogress.WebReportProgressServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import jodd.util.StringUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

import static cn.iocoder.byzs.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.byzs.framework.common.pojo.CommonResult.success;
import static cn.iocoder.byzs.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.byzs.module.bjdx.enums.ErrorCodeConstants.PROGRESS_NOT_EXISTS;


@Tag(name = "管理后台 - 评估报告-课程开课率")
@RestController
@RequestMapping("/bjdxReport/progress")
@Validated
public class WebReportProgressController {

    @Resource
    private WebReportProgressServiceImpl progressService;

    @PermitAll
    @TenantIgnore
    @PostMapping("/saveReportProgress")
    @Operation(summary = "保存评估报告进度")
    public CommonResult<Boolean> saveReportProgress(@Valid @RequestBody WebReportProgressDO reportProgressDO) {
        // 校验
        if (StringUtil.isBlank(reportProgressDO.getBrpType()) ||reportProgressDO.getBrpType().equals("courseQuest") && Objects.isNull(reportProgressDO.getBrpCourseConfigId())) {
            throw exception(PROGRESS_NOT_EXISTS);
        }
        //用户id
        reportProgressDO.setBrpUserId(getLoginUserId());
        // 插入
        progressService.saveReportProgress(reportProgressDO);
        return success(true);
    }

    @PermitAll
    @TenantIgnore
    @GetMapping("/getReportProgress")
    @Operation(summary = "取评估报告进度")
    public WebReportProgressVO getReportProgress(@Valid WebReportProgressDO reportProgressDO) {
        //用户id
        reportProgressDO.setBrpUserId(getLoginUserId());
        // 插入
        return progressService.getReportProgress(reportProgressDO);
    }

}