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.LawyerServiceArea;

import shop.alien.lawyer.service.LawyerServiceAreaService;
import shop.alien.util.myBaticsPlus.QueryBuilder;

import java.util.List;

/**
 * 律师服务领域关联 前端控制器
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"律师平台-律师服务领域关联"})
@ApiSort(17)
@CrossOrigin
@RestController
@RequestMapping("/lawyer/serviceArea")
@RequiredArgsConstructor
public class LawyerServiceAreaController {

    private final LawyerServiceAreaService lawyerServiceAreaService;

    @ApiOperation("新增律师服务领域")
    @ApiOperationSupport(order = 1)
    @PostMapping("/addLawyerServiceArea")
    public R<LawyerServiceArea> addLawyerServiceArea(@RequestBody LawyerServiceArea lawyerServiceArea) {
        log.info("LawyerServiceAreaController.addLawyerServiceArea?lawyerServiceArea={}", lawyerServiceArea);
        return lawyerServiceAreaService.addLawyerServiceArea(lawyerServiceArea);
    }

    @ApiOperation("编辑律师服务领域")
    @ApiOperationSupport(order = 2)
    @PostMapping("/editLawyerServiceArea")
    public R<LawyerServiceArea> editLawyerServiceArea(@RequestBody LawyerServiceArea lawyerServiceArea) {
        log.info("LawyerServiceAreaController.editLawyerServiceArea?lawyerServiceArea={}", lawyerServiceArea);
        return lawyerServiceAreaService.editLawyerServiceArea(lawyerServiceArea);
    }

    @ApiOperation("删除律师服务领域")
    @ApiOperationSupport(order = 3)
    @DeleteMapping("/deleteLawyerServiceArea")
    public R<Boolean> deleteLawyerServiceArea(@RequestParam(value = "id") Integer id) {
        log.info("LawyerServiceAreaController.deleteLawyerServiceArea?id={}", id);
        return lawyerServiceAreaService.deleteLawyerServiceArea(id);
    }

    @ApiOperation("保存或更新律师服务领域")
    @ApiOperationSupport(order = 4)
    @PostMapping("/saveOrUpdate")
    public R<LawyerServiceArea> saveOrUpdate(@RequestBody LawyerServiceArea lawyerServiceArea) {
        log.info("LawyerServiceAreaController.saveOrUpdate?lawyerServiceArea={}", lawyerServiceArea);
        boolean result = lawyerServiceAreaService.saveOrUpdate(lawyerServiceArea);
        if (result) {
            return R.data(lawyerServiceArea);
        }
        return R.fail("操作失败");
    }

    @ApiOperation("通用列表查询")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "lawyerUserId", value = "律师用户ID", 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<LawyerServiceArea>> getList(@ModelAttribute LawyerServiceArea lawyerServiceArea) {
        log.info("LawyerServiceAreaController.getList?lawyerServiceArea={}", lawyerServiceArea);
        List<LawyerServiceArea> list = QueryBuilder.of(lawyerServiceArea)
                .build()
                .list(lawyerServiceAreaService);
        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 = "lawyerUserId", value = "律师用户ID", 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<LawyerServiceArea>> getPage(@ModelAttribute LawyerServiceArea lawyerServiceArea,
                                               @RequestParam(defaultValue = "1") int page,
                                               @RequestParam(defaultValue = "10") int size) {
        log.info("LawyerServiceAreaController.getPage?lawyerServiceArea={},page={},size={}", lawyerServiceArea, page, size);
        int pageNum = page > 0 ? page : 1;
        int pageSize = size > 0 ? size : 10;
        IPage<LawyerServiceArea> pageResult = QueryBuilder.of(lawyerServiceArea)
                .page(pageNum, pageSize)
                .build()
                .page(lawyerServiceAreaService);
        return R.data(pageResult);
    }
}

