package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LawyerConsultationReview;
import shop.alien.store.service.LawyerConsultationReviewService;
import shop.alien.util.myBaticsPlus.QueryBuilder;

import java.util.List;

/**
 * 咨询评价 前端控制器
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"律师平台-咨询评价"})
@ApiSort(19)
@CrossOrigin
@RestController
@RequestMapping("/lawyer/consultationReview")
@RequiredArgsConstructor
public class LawyerConsultationReviewController {

    private final LawyerConsultationReviewService consultationReviewService;

    @ApiOperation("新增咨询评价")
    @ApiOperationSupport(order = 1)
    @PostMapping("/addConsultationReview")
    public R<LawyerConsultationReview> addConsultationReview(@RequestBody LawyerConsultationReview consultationReview) {
        log.info("LawyerConsultationReviewController.addConsultationReview?consultationReview={}", consultationReview);
        return consultationReviewService.addConsultationReview(consultationReview);
    }

    @ApiOperation("编辑咨询评价")
    @ApiOperationSupport(order = 2)
    @PostMapping("/editConsultationReview")
    public R<LawyerConsultationReview> editConsultationReview(@RequestBody LawyerConsultationReview consultationReview) {
        log.info("LawyerConsultationReviewController.editConsultationReview?consultationReview={}", consultationReview);
        return consultationReviewService.editConsultationReview(consultationReview);
    }

    @ApiOperation("删除咨询评价")
    @ApiOperationSupport(order = 3)
    @DeleteMapping("/deleteConsultationReview")
    public R<Boolean> deleteConsultationReview(@RequestParam(value = "id") Integer id) {
        log.info("LawyerConsultationReviewController.deleteConsultationReview?id={}", id);
        return consultationReviewService.deleteConsultationReview(id);
    }

    @ApiOperation("保存或更新咨询评价")
    @ApiOperationSupport(order = 4)
    @PostMapping("/saveOrUpdate")
    public R<LawyerConsultationReview> saveOrUpdate(@RequestBody LawyerConsultationReview consultationReview) {
        log.info("LawyerConsultationReviewController.saveOrUpdate?consultationReview={}", consultationReview);
        boolean result = consultationReviewService.saveOrUpdate(consultationReview);
        if (result) {
            return R.data(consultationReview);
        }
        return R.fail("操作失败");
    }

    @ApiOperation("通用列表查询")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "consultationOrderId", value = "咨询订单ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "lawyerUserId", value = "律师用户ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "clientUserId", value = "客户端用户ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "rating", value = "评分, 1-5星", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "createdTime_Start", value = "创建时间开始（范围查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createdTime_End", value = "创建时间结束（范围查询）", dataType = "String", paramType = "query")
    })
    @GetMapping("/getList")
    public R<List<LawyerConsultationReview>> getList(@ModelAttribute LawyerConsultationReview consultationReview) {
        log.info("LawyerConsultationReviewController.getList?consultationReview={}", consultationReview);
        List<LawyerConsultationReview> list = QueryBuilder.of(consultationReview)
                .build()
                .list(consultationReviewService);
        return R.data(list);
    }

    @ApiOperation("通用分页查询")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数（默认1）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页容（默认10）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "consultationOrderId", value = "咨询订单ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "lawyerUserId", value = "律师用户ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "clientUserId", value = "客户端用户ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "rating", value = "评分, 1-5星", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "createdTime_Start", value = "创建时间开始（范围查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createdTime_End", value = "创建时间结束（范围查询）", dataType = "String", paramType = "query")
    })
    @GetMapping("/getPage")
    public R<IPage<LawyerConsultationReview>> getPage(@ModelAttribute LawyerConsultationReview consultationReview,
                                                       @RequestParam(defaultValue = "1") int page,
                                                       @RequestParam(defaultValue = "10") int size) {
        log.info("LawyerConsultationReviewController.getPage?consultationReview={},page={},size={}", consultationReview, page, size);
        int pageNum = page > 0 ? page : 1;
        int pageSize = size > 0 ? size : 10;
        IPage<LawyerConsultationReview> pageResult = QueryBuilder.of(consultationReview)
                .page(pageNum, pageSize)
                .build()
                .page(consultationReviewService);
        return R.data(pageResult);
    }
}

