package shop.alien.lawyer.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.LawyerCommonQuestion;
import shop.alien.lawyer.service.LawyerCommonQuestionService;
import shop.alien.util.myBaticsPlus.QueryBuilder;

import java.util.List;

/**
 * 常见问题 前端控制器
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"律师平台-常见问题"})
@ApiSort(18)
@CrossOrigin
@RestController
@RequestMapping("/lawyer/commonQuestion")
@RequiredArgsConstructor
public class LawyerCommonQuestionController {

    private final LawyerCommonQuestionService commonQuestionService;

    @ApiOperation("新增常见问题")
    @ApiOperationSupport(order = 1)
    @PostMapping("/addCommonQuestion")
    public R<LawyerCommonQuestion> addCommonQuestion(@RequestBody LawyerCommonQuestion commonQuestion) {
        log.info("LawyerCommonQuestionController.addCommonQuestion?commonQuestion={}", commonQuestion);
        return commonQuestionService.addCommonQuestion(commonQuestion);
    }

    @ApiOperation("编辑常见问题")
    @ApiOperationSupport(order = 2)
    @PostMapping("/editCommonQuestion")
    public R<LawyerCommonQuestion> editCommonQuestion(@RequestBody LawyerCommonQuestion commonQuestion) {
        log.info("LawyerCommonQuestionController.editCommonQuestion?commonQuestion={}", commonQuestion);
        return commonQuestionService.editCommonQuestion(commonQuestion);
    }

    @ApiOperation("删除常见问题")
    @ApiOperationSupport(order = 3)
    @DeleteMapping("/deleteCommonQuestion")
    public R<Boolean> deleteCommonQuestion(@RequestParam(value = "id") Integer id) {
        log.info("LawyerCommonQuestionController.deleteCommonQuestion?id={}", id);
        return commonQuestionService.deleteCommonQuestion(id);
    }

    @ApiOperation("保存或更新常见问题")
    @ApiOperationSupport(order = 4)
    @PostMapping("/saveOrUpdate")
    public R<LawyerCommonQuestion> saveOrUpdate(@RequestBody LawyerCommonQuestion commonQuestion) {
        log.info("LawyerCommonQuestionController.saveOrUpdate?commonQuestion={}", commonQuestion);
        boolean result = commonQuestionService.saveOrUpdate(commonQuestion);
        if (result) {
            return R.data(commonQuestion);
        }
        return R.fail("操作失败");
    }

    @ApiOperation("通用列表查询")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "questionText", value = "问题文本（支持模糊查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "categoryType", value = "分类类型, 0:推荐, 1:常见问题", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "problemScenarioId", value = "法律问题场景ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态, 0:禁用, 1:启用", 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<LawyerCommonQuestion>> getList(@ModelAttribute LawyerCommonQuestion commonQuestion) {
        log.info("LawyerCommonQuestionController.getList?commonQuestion={}", commonQuestion);
        List<LawyerCommonQuestion> list = QueryBuilder.of(commonQuestion)
                .likeFields("questionText")  // 问题文本支持模糊查询
                .build()
                .list(commonQuestionService);
        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 = "questionText", value = "问题文本（支持模糊查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "categoryType", value = "分类类型, 0:推荐, 1:常见问题", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "problemScenarioId", value = "法律问题场景ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态, 0:禁用, 1:启用", 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<LawyerCommonQuestion>> getPage(@ModelAttribute LawyerCommonQuestion commonQuestion,
                                                  @RequestParam(defaultValue = "1") int page,
                                                  @RequestParam(defaultValue = "10") int size) {
        log.info("LawyerCommonQuestionController.getPage?commonQuestion={},page={},size={}", commonQuestion, page, size);
        int pageNum = page > 0 ? page : 1;
        int pageSize = size > 0 ? size : 10;
        IPage<LawyerCommonQuestion> pageResult = QueryBuilder.of(commonQuestion)
                .likeFields("questionText")  // 问题文本支持模糊查询
                .page(pageNum, pageSize)
                .build()
                .page(commonQuestionService);
        return R.data(pageResult);
    }

    @ApiOperation("根据数量获取常见问题列表")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "num", value = "获取数量", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getListByNum")
    public R<List<LawyerCommonQuestion>> getListByNum(@RequestParam Integer num) {
        log.info("LawyerCommonQuestionController.getListByNum?num={}", num);
        return commonQuestionService.getCommonQuestionListByNum(num);
    }

}

