package com.cool.store.controller.webc;

import com.cool.store.request.dict.DictColumnQueryRequest;
import com.cool.store.request.dict.DictGroupQueryRequest;
import com.cool.store.request.dict.DictTableQueryRequest;
import com.cool.store.response.ResponseResult;
import com.cool.store.service.dict.DictColumnService;
import com.cool.store.service.dict.DictGroupService;
import com.cool.store.service.dict.DictTableInfoService;
import com.cool.store.vo.dict.DictColumnSimpleVO;
import com.cool.store.vo.dict.DictColumnVO;
import com.cool.store.vo.dict.DictGroupVO;
import com.cool.store.vo.dict.DictTableVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典管理 前端控制器
 * </p>
 *
 * @author wangff
 * @since 2025/2/20
 */
@Api(tags = "Mini字典管理")
@RestController
@RequestMapping("/mini/sys/dict")
@RequiredArgsConstructor
public class MiniDictManagerController {
    private final DictGroupService groupService;
    private final DictTableInfoService tableService;
    private final DictColumnService columnService;

    @ApiOperation("字典分组-查询")
    @GetMapping("/group/{groupId}")
    public ResponseResult<DictGroupVO> groupGet(@PathVariable("groupId") Long groupId) {
        return ResponseResult.success(groupService.getById(groupId));
    }

    @ApiOperation("字典分组-列表")
    @GetMapping("/group/list")
    public ResponseResult<List<DictGroupVO>> groupList(DictGroupQueryRequest dto) {
        return ResponseResult.success(groupService.getList(dto));
    }

    @ApiOperation("字典表-查询")
    @GetMapping("/table/{tableId}")
    public ResponseResult<DictTableVO> tableGet(@PathVariable("tableId") Long tableId) {
        return ResponseResult.success(tableService.getById(tableId));
    }

    @ApiOperation("字典表-列表")
    @GetMapping("/table/list")
    public ResponseResult<List<DictTableVO>> tableList(DictTableQueryRequest dto) {
        return ResponseResult.success(tableService.getList(dto));
    }

    @ApiOperation("字典项-分页查询")
    @GetMapping("/column/page")
    public ResponseResult<PageInfo<DictColumnVO>> columnPage(DictColumnQueryRequest dto) {
        return ResponseResult.success(columnService.getPage(dto));
    }

    @ApiOperation("字典项-查询")
    @GetMapping("/column/{columnId}")
    public ResponseResult<DictColumnVO> columnGet(@PathVariable("columnId") Long columnId) {
        return ResponseResult.success(columnService.getById(columnId));
    }

    @ApiOperation("字典项-根据字典项code查询已启用的字典项")
    @GetMapping("/column/getByColumnCode")
    public ResponseResult<DictColumnSimpleVO> getColumnByColumnCode(@NotBlank(message = "字典项编码不能为空") String columnCode) {
        return ResponseResult.success(columnService.getColumnByColumnCode(columnCode));
    }

    @ApiOperation("字典项-根据字典表code查询已启用的字典项列表")
    @PostMapping("/column/getMapByTableCodes")
    public ResponseResult<Map<String, List<DictColumnSimpleVO>>> getColumnByTableCode(@RequestBody List<String> tableCodes) {
        return ResponseResult.success(columnService.getMapListByTableCode(tableCodes));
    }
}
