package com.prolog.rdc.tms.util;

import cn.hutool.core.util.ObjectUtil;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.exception.PrologException;
import com.prolog.rdc.tms.model.entity.SysRefdatadModel;
import com.prolog.rdc.tms.model.vo.basicData.SysRefdatadQueryVO;
import com.prolog.rdc.tms.rmi.feign.MaindataFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ConvertorUtil {

	@Autowired
	private MaindataFeign maindataTmsResource;

	public List<SysRefdatadModel> getDictionary(String[] objectcodes) {
		SysRefdatadQueryVO vo = new SysRefdatadQueryVO();
		vo.setObjectcodes(objectcodes);
		RestMessage<List<SysRefdatadModel>> data = maindataTmsResource.getSysRefdatadDatas(vo);
		if (!data.isSuccess()) {
			throw new PrologException("通用字典获取失败");
		}
		return data.getData();
	}

	/**
	 * 通过code翻译name
	 * 
	 * @param list
	 * @param objectcode
	 * @param code
	 * @return
	 */
	public static String getName(List<SysRefdatadModel> list, String objectcode, String code) {
		if (ObjectUtil.isEmpty(objectcode) || ObjectUtil.isEmpty(code) || ObjectUtil.equal("null",code)) {
			return null;
		}
		String name = null;
		for (SysRefdatadModel model : list) {
			if (ObjectUtil.equal(model.getObjectcode(), objectcode) && ObjectUtil.equal(model.getCode(), code)) {
				name = model.getName();
				break;
			}
		}
		return name == null ? code : name;
	}

	/**
	 * 通过name查询code
	 * 
	 * @param list
	 * @param objectcode
	 * @param name
	 * @return
	 */
	public static String getCode(List<SysRefdatadModel> list, String objectcode, String name) {
		if (ObjectUtil.isEmpty(objectcode) || ObjectUtil.isEmpty(name)) {
			return null;
		}
		String code = null;
		for (SysRefdatadModel model : list) {
			if (ObjectUtil.equal(model.getObjectcode(), objectcode) && ObjectUtil.equal(model.getName(), name)) {
				code = model.getCode();
				break;
			}
		}
		return code == null ? name : code;
	}

}
