/*
 * Copyright (c) 京北方信息技术股份有限公司 Corporation 2021 . All rights reserved.
 *
 */
package net.northking.cctp.platform.pub;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.common.exception.PlatformRuntimeException;
import net.northking.cctp.common.http.ResultWrapper;
import net.northking.cctp.common.security.authentication.NKSecurityContext;
import net.northking.cctp.platform.api.dict.service.CltDictCatalogApiService;
import net.northking.cctp.platform.constants.PlatformAppError;
import net.northking.cctp.platform.db.entity.CltDictItem;
import net.northking.cctp.platform.pub.dto.PubDto;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Api(tags = "字典目录")
@RequestMapping("/pub")
@RestController
public class CltDictCatalogPubQueryCtrl {
    /**
     * 日志
     */
    private final static Logger logger = LoggerFactory.getLogger(CltDictCatalogPubQueryCtrl.class);

    private CltDictCatalogApiService apiService;

    @Autowired
    public void setCltDictCatalogRestfulService(CltDictCatalogApiService apiService) {
        this.apiService = apiService;
    }

    /**
     * 查询获取字典条目
     *
     * @param pubDto 字典code集合
     * @return 数据列表
     */
    @ApiOperation(value = "获取字典条目")
    @PostMapping(value = "/query/dict-catalog-item")
    public ResultWrapper<Map<String, Object>> query(
            @RequestBody PubDto pubDto) {
        NKSecurityContext.setTenantId(pubDto.getTenantId());
        long l = System.currentTimeMillis();
        logger.info("行93开始:0");
        if (StringUtils.isBlank(pubDto.getTenantId())){
            throw new PlatformRuntimeException(PlatformAppError.TENANT_ID_IS_NULL);
        }
        List<String> catalogCodeList = pubDto.getResultList();
        logger.info("-----pub获取字典条目接收参数------"+pubDto.getTenantId()+":"+(catalogCodeList.isEmpty()?null:catalogCodeList.get(0)));
        ResultWrapper<Map<String, Object>> wrapper = new ResultWrapper<>();
        Map<String, Object> result = new HashMap<>();
        for (String s : catalogCodeList) {
            List<CltDictItem> list = apiService.query(NKSecurityContext.getTenantId(), s);
            result.put(s, list);
        }
        logger.info("行93结束:"+(System.currentTimeMillis()-l));
        return wrapper.success(result);
    }

}


