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.LawyerExpertiseArea;
import shop.alien.store.service.LawyerExpertiseAreaService;
import shop.alien.util.myBaticsPlus.QueryBuilder;

import java.util.List;

/**
 * 领域信息 前端控制器
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"律师平台-领域信息"})
@ApiSort(15)
@CrossOrigin
@RestController
@RequestMapping("/lawyer/expertiseArea")
@RequiredArgsConstructor
public class LawyerExpertiseAreaController {

    private final LawyerExpertiseAreaService expertiseAreaService;

    @ApiOperation("分页查询领域信息列表")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数（默认1）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页容（默认10）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "expertiseAreaInfo", value = "领域信息（支持模糊查询）", dataType = "String", paramType = "query")
    })
    @GetMapping("/getExpertiseAreaList")
    public R<IPage<LawyerExpertiseArea>> getExpertiseAreaList(@RequestParam(defaultValue = "1") int page,
                                                             @RequestParam(defaultValue = "10") int size,
                                                             @RequestParam(required = false) String expertiseAreaInfo) {
        log.info("LawyerExpertiseAreaController.getExpertiseAreaList?page={},size={},expertiseAreaInfo={}",
                page, size, expertiseAreaInfo);
        int pageNum = page > 0 ? page : 1;
        int pageSize = size > 0 ? size : 10;
        return expertiseAreaService.getExpertiseAreaList(pageNum, pageSize, expertiseAreaInfo);
    }

    @ApiOperation("新增领域信息")
    @ApiOperationSupport(order = 2)
    @PostMapping("/addExpertiseArea")
    public R<LawyerExpertiseArea> addExpertiseArea(@RequestBody LawyerExpertiseArea expertiseArea) {
        log.info("LawyerExpertiseAreaController.addExpertiseArea?expertiseArea={}", expertiseArea);
        return expertiseAreaService.addExpertiseArea(expertiseArea);
    }

    @ApiOperation("编辑领域信息")
    @ApiOperationSupport(order = 3)
    @PostMapping("/editExpertiseArea")
    public R<LawyerExpertiseArea> editExpertiseArea(@RequestBody LawyerExpertiseArea expertiseArea) {
        log.info("LawyerExpertiseAreaController.editExpertiseArea?expertiseArea={}", expertiseArea);
        
        // 参数校验：ID不能为空
        if (expertiseArea == null || expertiseArea.getId() == null) {
            log.warn("编辑领域信息失败：ID为空");
            return R.fail("ID不能为空");
        }
        
        return expertiseAreaService.editExpertiseArea(expertiseArea);
    }

    @ApiOperation("删除领域信息")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Integer", paramType = "query", required = true)
    })
    @DeleteMapping("/deleteExpertiseArea")
    public R<Boolean> deleteExpertiseArea(@RequestParam(value = "id", required = true) Integer id) {
        log.info("LawyerExpertiseAreaController.deleteExpertiseArea?id={}", id);
        if (id == null) {
            log.warn("删除领域信息失败：ID为空");
            return R.fail("ID不能为空");
        }
        return expertiseAreaService.deleteExpertiseArea(id);
    }

    @ApiOperation("保存或更新领域信息")
    @ApiOperationSupport(order = 5)
    @PostMapping("/saveOrUpdate")
    public R<LawyerExpertiseArea> saveOrUpdate(@RequestBody LawyerExpertiseArea expertiseArea) {
        log.info("LawyerExpertiseAreaController.saveOrUpdate?expertiseArea={}", expertiseArea);
        boolean result = expertiseAreaService.saveOrUpdate(expertiseArea);
        if (result) {
            return R.data(expertiseArea);
        }
        return R.fail("操作失败");
    }

    @ApiOperation("通用列表查询")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "expertiseAreaInfo", value = "领域信息（支持模糊查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "deleteFlag", 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<LawyerExpertiseArea>> getList(@ModelAttribute LawyerExpertiseArea expertiseArea) {
        log.info("LawyerExpertiseAreaController.getList?expertiseArea={}", expertiseArea);
        List<LawyerExpertiseArea> list = QueryBuilder.of(expertiseArea)
                .likeFields("expertiseAreaInfo")  // 领域信息支持模糊查询
                .build()
                .list(expertiseAreaService);
        return R.data(list);
    }

    @ApiOperation("通用分页查询")
    @ApiOperationSupport(order = 7)
    @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 = "expertiseAreaInfo", value = "领域信息（支持模糊查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "deleteFlag", 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<LawyerExpertiseArea>> getPage(@ModelAttribute LawyerExpertiseArea expertiseArea,
                                                 @RequestParam(defaultValue = "1") int page,
                                                 @RequestParam(defaultValue = "10") int size) {
        log.info("LawyerExpertiseAreaController.getPage?expertiseArea={},page={},size={}", expertiseArea, page, size);
        int pageNum = page > 0 ? page : 1;
        int pageSize = size > 0 ? size : 10;
        IPage<LawyerExpertiseArea> pageResult = QueryBuilder.of(expertiseArea)
                .likeFields("expertiseAreaInfo")  // 领域信息支持模糊查询
                .page(pageNum, pageSize)
                .build()
                .page(expertiseAreaService);
        return R.data(pageResult);
    }
}

