package com.xindazhou.business.controller.admin.service.evaluation;

import com.xindazhou.business.api.service.EvaluationApi;
import com.xindazhou.business.controller.admin.service.evaluation.vo.EvaluationPageReqVO;
import com.xindazhou.business.controller.admin.service.evaluation.vo.EvaluationRespVO;
import com.xindazhou.framework.common.pojo.CommonResult;
import com.xindazhou.framework.common.pojo.PageResult;
import com.xindazhou.framework.common.util.json.JsonUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static com.xindazhou.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - 服务评价代理 Controller（统一前端调用路径）
 *
 * @author xindazhou
 */
@Tag(name = "管理后台 - 服务评价管理")
@RestController("serviceEvaluationProxyController")
@RequestMapping("/admin/service/evaluation")
@Validated
@Slf4j
public class EvaluationProxyController {

    @Resource
    private EvaluationApi evaluationApi;

    @GetMapping("/page")
    @Operation(summary = "获得服务评价分页列表")
    @PreAuthorize("@ss.hasPermission('service:evaluation:query')")
    public CommonResult<PageResult<EvaluationRespVO>> getEvaluationPage(@Valid EvaluationPageReqVO pageReqVO) {
        CommonResult<PageResult<Map<String, Object>>> result = evaluationApi.getEvaluationPage(
                pageReqVO.getPageNo(),
                pageReqVO.getPageSize(),
                pageReqVO.getKeyword(),
                pageReqVO.getRating(),
                pageReqVO.getCreateTimeStart() != null ? pageReqVO.getCreateTimeStart().toString() : null,
                pageReqVO.getCreateTimeEnd() != null ? pageReqVO.getCreateTimeEnd().toString() : null
        );
        // 转换为VO
        PageResult<Map<String, Object>> data = result.getData();
        PageResult<EvaluationRespVO> pageResult = JsonUtils.parseObject(JsonUtils.toJsonString(data), PageResult.class);
        return success(pageResult);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获得服务评价")
    @Parameter(name = "id", description = "评价ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('service:evaluation:query')")
    public CommonResult<EvaluationRespVO> getEvaluation(@PathVariable("id") Long id) {
        CommonResult<Map<String, Object>> result = evaluationApi.getEvaluation(id);
        EvaluationRespVO evaluation = JsonUtils.parseObject(JsonUtils.toJsonString(result.getData()), EvaluationRespVO.class);
        return success(evaluation);
    }

}

