100 lines
3.4 KiB
JavaScript
100 lines
3.4 KiB
JavaScript
//
|
|
//基础数据操作类
|
|
//
|
|
|
|
(function (global) {
|
|
|
|
const ylotc = global.ylotc || {};
|
|
const varietyMap = { $init: 0 }, underlyingMap = { $init: true };
|
|
|
|
/**
|
|
* 根据品种ID或代码获取品种信息
|
|
* @param {number|string} varietyID - 必需,参数值为数字则为品种ID否则为品种代码,如果参数值为空返回空对象
|
|
* @param {string} propName - 可选,品种属性名称,如果参数值为空返回整个对象,否则返回品种属性值
|
|
* @returns -- 返回
|
|
*/
|
|
ylotc.getVariety = function (varietyID, propName) {
|
|
if (!Array.isArray(ylotc.varieties)) {
|
|
ylotc.varieties = [];
|
|
alert('ylotc.varieties未正确初始化');
|
|
return;
|
|
}
|
|
|
|
if (!varietyID) return {};
|
|
|
|
//初始化
|
|
if (typeof varietyID === 'number') {
|
|
varietyID = '$' + varietyID;
|
|
if (1 !== (1 & varietyMap.$init)) {
|
|
varietyMap.$init |= 1;
|
|
ylotc.varieties.forEach(x => varietyMap['$' + x.id] = x);
|
|
}
|
|
} else if (2 !== (2 & varietyMap.$init)) {
|
|
varietyMap.$init |= 2;
|
|
ylotc.varieties.forEach(x => varietyMap[x.Code] = x);
|
|
}
|
|
|
|
//返回品种信息
|
|
var variety = varietyMap[varietyID] || {};
|
|
return propName ? variety[propName] : variety;
|
|
};
|
|
|
|
/**
|
|
* 获取品种列表
|
|
* @param {any} predicate
|
|
* @returns 返回
|
|
*/
|
|
ylotc.getVarieties = function (predicate) {
|
|
var varieties = ylotc.varieties;
|
|
if (!Array.isArray(varieties)) {
|
|
ylotc.varieties = [];
|
|
alert('ylotc.varieties未正确初始化');
|
|
return;
|
|
}
|
|
|
|
if (!predicate) return varieties;
|
|
|
|
switch (typeof predicate) {
|
|
case 'function':
|
|
return varieties.filter(predicate);
|
|
case 'object':
|
|
if (Array.isArray(predicate)) {
|
|
var arr = predicate;
|
|
predicate = input => input && arr.some(x => x === input.id || x === input.Code);
|
|
} else {
|
|
//根据请求模型构建匹配函数
|
|
var reqModel = predicate;
|
|
var matches = _.map(reqModel, (val, key) => {
|
|
return input => typeof val === 'function' ? val(input[key]) : val === input[key];
|
|
});
|
|
predicate = input => input && matches.every(x => x(input));
|
|
}
|
|
return varieties.filter(predicate);
|
|
default: return varieties;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 根据标的代码获取品种信息
|
|
* @param {any} underlyingCode - 标的代码
|
|
* @param {any} propName - 可选,标的属性名称,如果参数值为空返回整个对象,否则返回标的属性值
|
|
*/
|
|
ylotc.getUnderlying = function (underlyingCode, propName) {
|
|
if (!Array.isArray(ylotc.underlyings)) {
|
|
ylotc.underlyings = [];
|
|
alert('ylotc.underlyings未正确初始化');
|
|
return;
|
|
}
|
|
if (underlyingMap.$init) {
|
|
underlyingMap.$init = false;
|
|
ylotc.underlyings.forEach(x => underlyingMap[x.Code] = x);
|
|
}
|
|
var underlying = underlyingMap[underlyingCode] || {};
|
|
return propName ? underlying[propName] : underlying;
|
|
};
|
|
|
|
if (!global.ylotc) {
|
|
global.ylotc = ylotc;
|
|
}
|
|
}(window));
|