package com.usky.ems.controller.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.usky.common.core.bean.ApiResult;
import com.usky.ems.domain.EmsSystemDictCode;
import com.usky.ems.domain.EmsSystemDictRegion;
import com.usky.ems.domain.EmsSystemDictValue;
import com.usky.ems.service.EmsSystemDictCodeService;
import com.usky.ems.service.EmsSystemDictRegionService;
import com.usky.ems.service.EmsSystemDictValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 系统字典（ems_system_dict_code / ems_system_dict_region / ems_system_dict_value）查询接口
 * 基础路径前缀：/system-dict
 */
@RestController
@RequestMapping("/system-dict")
public class EmsSystemDictController {

    @Autowired
    private EmsSystemDictCodeService emsSystemDictCodeService;
    @Autowired
    private EmsSystemDictRegionService emsSystemDictRegionService;
    @Autowired
    private EmsSystemDictValueService emsSystemDictValueService;

    @GetMapping("/code/list")
    public ApiResult<List<EmsSystemDictCode>> listCodes() {
        return ApiResult.success(emsSystemDictCodeService.list());
    }

    /**
     * @param parent 父级区域编码（如国标省市区上一级 code；省级可传约定根编码，需与字典数据一致）
     */
    @GetMapping("/region/list")
    public ApiResult<List<EmsSystemDictRegion>> listRegions(@RequestParam String parent) {
        return ApiResult.success(emsSystemDictRegionService.listByParentCode(parent));
    }

    @GetMapping("/value/list")
    public ApiResult<List<EmsSystemDictValue>> listValues(@RequestParam String dictCode) {
        return ApiResult.success(emsSystemDictValueService.list(
                new LambdaQueryWrapper<EmsSystemDictValue>()
                        .eq(EmsSystemDictValue::getDictCode, dictCode)
                        .orderByAsc(EmsSystemDictValue::getSort)));
    }
}
