package com.persagy.dmp.codetable.controller;

import cn.hutool.core.util.StrUtil;
import com.persagy.dmp.basic.dto.RequestData;
import com.persagy.dmp.codetable.entity.CodeTableData;
import com.persagy.dmp.codetable.service.ICodeTableDataService;
import com.persagy.dmp.common.context.AppContext;
import com.persagy.dmp.common.model.response.CommonResult;
import com.persagy.dmp.common.utils.ResultHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 码表数据 Controller
 * @author Charlie Yu
 * @date 2021-07-27
 */
@RestController
@RequestMapping(value = "/dic/code/")
public class CodeTableDataController {

    @Autowired
    private ICodeTableDataService dataService;

    /**
     * 根据码表类型查询所有字典数据
     * @param typeCode 码表类型编码
     * @return List<CodeTableDetail>
     */
    @GetMapping("/queryAll/{typeCode}")
    public CommonResult<List<CodeTableData>> queryCodeTableDetailListByTypeCode(@PathVariable String typeCode) {
        if (StrUtil.isBlank(typeCode)) {
            return ResultHelper.multi(new ArrayList<>(),0L);
        }
        List<CodeTableData> dataList = dataService.queryAllByTypeCode(AppContext.getContext().getProjectId(), typeCode);
        return ResultHelper.multi(dataList);
    }
    /**
     * 根据码表类型及码表数据code查询字典详情
     * @return List<CodeTableDetail>
     */
    @PostMapping("/queryCodeTableDatasByTypeCodeAndCodes")
    public CommonResult<List<CodeTableData>> queryCodeTableDetailListByTypeCodeAndCodes(@RequestBody RequestData requestData) {
        if (StrUtil.isBlank(requestData.getTypeCode())) {
            return ResultHelper.multi(new ArrayList<>(),0L);
        }
        List<CodeTableData> dataList = dataService
                .queryCodeTableDatasByTypeCodeAndCodes(AppContext.getContext().getProjectId(), requestData.getTypeCode(), requestData);
        return ResultHelper.multi(dataList);
    }
    /***
     * Description: 根据父码表code查询其下的码表数据
     * @param requestData : 请求参数
     * @return : java.util.List<com.persagy.dmp.codetable.entity.CodeTableData>
     * @author : lijie
     * @date :2021/8/30 22:58
     * Update By lijie 2021/8/30 22:58
     */
    @PostMapping("/queryCodeTableDatasByParentCode")
    public CommonResult<List<CodeTableData>> queryCodeTableDatasByParentCode(@RequestBody RequestData requestData) {
        if (StrUtil.isBlank(requestData.getTypeCode()) || StrUtil.isBlank(requestData.getParentCode())) {
            return ResultHelper.multi(new ArrayList<>(),0L);
        }
        List<CodeTableData> dataList = dataService
                .queryCodeTableDatasByParentCode(AppContext.getContext().getProjectId(), requestData);
        return ResultHelper.multi(dataList);
    }
}
