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.LawyerLegalProblemScenario;
import shop.alien.entity.store.dto.LawyerLegalProblemScenarioStatusDto;
import shop.alien.lawyer.service.LawyerLegalProblemScenarioService;
import shop.alien.mapper.LawyerImgMapper;
import shop.alien.util.myBaticsPlus.QueryBuilder;

import java.util.List;

/**
 * 法律问题场景 前端控制器
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"律师平台-法律问题场景"})
@ApiSort(11)
@CrossOrigin
@RestController
@RequestMapping("/lawyer/legalProblemScenar")
@RequiredArgsConstructor
public class LawyerLegalProblemScenarioController {

    private final LawyerLegalProblemScenarioService lawyerLegalProblemScenarioService;

    private final LawyerImgMapper lawyerImgMapper;

    @ApiOperation("新增法律问题场景")
    @ApiOperationSupport(order = 1)
    @PostMapping("/addLawyerLegalProblemScenar")
    public R<LawyerLegalProblemScenario> addLawyerLegalProblemScenar(@RequestBody LawyerLegalProblemScenario lawyerLegalProblemScenario) {
        log.info("LawyerLegalProblemScenarioController.addLawyerLegalProblemScenar?lawyerLegalProblemScenario={}", lawyerLegalProblemScenario);
        return lawyerLegalProblemScenarioService.addLawyerLegalProblemScenario(lawyerLegalProblemScenario);
    }

    @ApiOperation("编辑法律问题场景")
    @ApiOperationSupport(order = 2)
    @PostMapping("/editLawyerLegalProblemScenar")
    public R<LawyerLegalProblemScenario> editLawyerLegalProblemScenar(@RequestBody LawyerLegalProblemScenario lawyerLegalProblemScenario) {
        log.info("LawyerLegalProblemScenarioController.editLawyerLegalProblemScenar?lawyerLegalProblemScenario={}", lawyerLegalProblemScenario);
        return lawyerLegalProblemScenarioService.editLawyerLegalProblemScenario(lawyerLegalProblemScenario);
    }

    @ApiOperation("删除法律问题场景")
    @ApiOperationSupport(order = 3)
    @DeleteMapping("/deleteLawyerLegalProblemScenar")
    public R<Boolean> deleteLawyerLegalProblemScenar(@RequestParam(value = "id") Integer id) {
        log.info("LawyerLegalProblemScenarioController.deleteLawyerLegalProblemScenar?id={}", id);
        return lawyerLegalProblemScenarioService.deleteLawyerLegalProblemScenario(id);
    }

    @ApiOperation("保存或更新法律问题场景")
    @ApiOperationSupport(order = 4)
    @PostMapping("/saveOrUpdate")
    public R<LawyerLegalProblemScenario> saveOrUpdate(@RequestBody LawyerLegalProblemScenario lawyerLegalProblemScenario) {
        log.info("LawyerLegalProblemScenarioController.saveOrUpdate?lawyerLegalProblemScenario={}", lawyerLegalProblemScenario);
        boolean result = lawyerLegalProblemScenarioService.saveOrUpdate(lawyerLegalProblemScenario);
        if (result) {
            return R.data(lawyerLegalProblemScenario);
        }
        return R.fail("操作失败");
    }

    @ApiOperation("通用列表查询")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称（支持模糊查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "level", value = "问题场景层级", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父类主键", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "parentCode", value = "父类编号", dataType = "String", 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<LawyerLegalProblemScenario>> getList(@ModelAttribute LawyerLegalProblemScenario lawyerLegalProblemScenario) {
        log.info("LawyerLegalProblemScenarioController.getList?lawyerLegalProblemScenario={}", lawyerLegalProblemScenario);
        // 使用 QueryBuilder 工具类进行列表查询
        List<LawyerLegalProblemScenario> list = QueryBuilder.of(lawyerLegalProblemScenario)
                .likeFields("name")  // 指定 name 字段使用模糊查询，其他字段使用等值查询
                .build()
                .list(lawyerLegalProblemScenarioService);

        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 = "code", value = "编号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称（支持模糊查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "level", value = "问题场景层级", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父类主键", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "parentCode", value = "父类编号", dataType = "String", 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<LawyerLegalProblemScenario>> getPage(@ModelAttribute LawyerLegalProblemScenario lawyerLegalProblemScenario,
                                                        @RequestParam(defaultValue = "1") int page,
                                                        @RequestParam(defaultValue = "10") int size) {
        log.info("LawyerLegalProblemScenarioController.getPage?lawyerLegalProblemScenario={},page={},size={}",
                lawyerLegalProblemScenario, page, size);
        // 参数校验（确保大于0）
        int pageNum = page > 0 ? page : 1;
        int pageSize = size > 0 ? size : 10;
        // 使用 QueryBuilder 工具类进行分页查询
        IPage<LawyerLegalProblemScenario> pageResult = QueryBuilder.of(lawyerLegalProblemScenario)
                .likeFields("name")  // 指定 name 字段使用模糊查询，其他字段使用等值查询
                .page(pageNum, pageSize)
                .build()
                .page(lawyerLegalProblemScenarioService);
        return R.data(pageResult);
    }

    @ApiOperation("根据一级场景主键获取分类树（包含一级场景及其下所有二级和三级分类）")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "一级场景主键", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getCategoryTree")
    public R<LawyerLegalProblemScenario> getCategoryTree(@RequestParam Integer id) {
        log.info("LawyerLegalProblemScenarioController.getCategoryTree?id={}", id);
        return lawyerLegalProblemScenarioService.getCategoryTreeByFirstLevelId(id);
    }

    @ApiOperation("设置法律场景启用/禁用状态")
    @ApiOperationSupport(order = 8)
    @PostMapping("/setStatus")
    public R<Boolean> setStatus(@RequestBody LawyerLegalProblemScenarioStatusDto request) {
        log.info("LawyerLegalProblemScenarioController.setStatus?request={}", request);
        return lawyerLegalProblemScenarioService.setStatus(request.getId(), request.getStatus());
    }

}

