package com.dxhy.config.controller;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.service.DictdetaServcice;
import com.dxhy.common.utils.R;
import com.dxhy.api.model.config.sys.DictdetaModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 业务字典
 *
 * @author jiaohongyang
 */
@RestController
@Slf4j
public class DictDataController extends AbstractController {
    @Resource
    private DictdetaServcice dictdetaServcice;

    @PostMapping("/getDict")
    public ResponseEntity<?> getDict(@RequestBody Map<String, Object> map) {
        String dictType = (String)map.get("dictType");
        if (StringUtils.isBlank(dictType)) {
            return ResponseEntity.ok(R.error().put("data", "字典编码不能为空"));
        }
        String[] codes = dictType.split(";");
        JSONObject json = new JSONObject();
        for (String code : codes) {
            List<DictdetaModel> models = dictdetaServcice.selectDictdeta(code);
            json.put(code, models);
        }
        return ResponseEntity.ok(R.ok().put("data", json));

    }
}
