Files
zszq-trs/YLErpWeb/wwwroot/Scripts/app/underlying/EditbasketUnderlying.js
T
2024-05-09 14:06:26 +08:00

343 lines
12 KiB
JavaScript

const consUnderlyings = ylotc.underlyings;
//自动完成
const autocomplete = (function () {
function set(el, opts) {
opts = _.extend({ minChars: 0 }, opts);
const onSelectFn = opts.onSelect;
opts.onSelect = function (suggestion) {
$(this).data('select', '1');
onSelectFn && onSelectFn(suggestion);
};
$(el).autocomplete('dispose');
$(el).val(opts.value).on('focus', function () {
$(this).data('select', '').attr("placeholder", $(this).val()).val('');
}).on('blur', function () {
!$(this).data('select') && $(this).val($(this).attr("placeholder"));
}).on('keydown', function (event) {
if (event.keyCode === 13) {
event.preventDefault();
event.stopPropagation();
}
}).autocomplete(opts);
}
return {
set: set
};
}());
//标的选择组件
const vueUnderlying = function () {
const _suggestionTpl = _.template($('#underlyingSuggestionTpl').html());
return {
props: ['value', 'name'],
data() {
return { showName: '', jqInput: null };
},
mounted() {
var self = this;
this.jqInput = $(this.$el).children(0);
this.showName = this.name;
autocomplete.set(this.jqInput.get(0), {
minChars: 0,
autoSelectFirst: true,
lookup(query, callback) {
var arr = [];
if (!query) {
consUnderlyings.slice(0, 20).forEach(x => {
arr.push({ value: x.Code, data: x });
});
} else {
query = query.toUpperCase();
consUnderlyings.forEach(x => {
if (x.Code.indexOf(query) !== -1
|| x.PinYin && x.PinYin.indexOf(query) !== -1
|| x.Name.toUpperCase().indexOf(query) !== -1) {
arr.push({ value: x.Code, data: x });
}
});
}
if (arr.length < 30) {
arr = _.sortBy(arr, x => x.value);
}
callback({ suggestions: arr });
},
onSelect(suggestion) {
var data = suggestion.data;
if (self.value !== data.Code) {
if (data.Disallow_Swap) {
main.alert(data.BlackWhiteState_Swap === 1 ? "该标的存在于黑名单中" : "该标的不在白名单中");
return;
}
self.value = data.Code;
self.value.code = data.Code;
self.value.name = data.Name;
self.showName = data.Name;
self.$emit('input', self.value);
}
self.jqInput.blur();
},
formatResult(suggestion, currentValue) {
return _suggestionTpl(suggestion.data);
},
value: this.value
});
},
watch: {
value(val, old) {
if (val !== old) {
$(this.$el).val(this.value).data('select', '').attr("placeholder", '');
}
},
name(val, old) {
if (val !== old) {
this.showName = this.name;
}
}
},
methods: {
showInput() {
this.jqInput.show().focus().next().hide();
},
hideInput() {
this.jqInput.hide().next().show();
}
},
destroyed() {
$(this.jqInput).autocomplete('dispose');
},
template: '<div style="width:152px;height:30px;" v-on:click="showInput"><input input v-on:blur="hideInput" style="border:none;display:none;width:95%" type="text" />' +
'<span style="text-align: center;display:block;">{{value}}</span><span style="font-size:80%"></span></div>'
};
};
var vue = new Vue({
el: "#vueDiv",
data: {
underlying_manager: {
id: null,
UnderlyingCode: basketUnderlyingName,
Price: 0,
Items: [],
}
},
mounted: function () {
this.loadData();
},
methods: {
loadData: function () {
var thisObj = this;
if (!main.isEmpty(thisObj.underlying_manager.UnderlyingCode)) {
main.post("/BasketUnderlying/GetBasketUnderlyingByName",
{ name: thisObj.underlying_manager.UnderlyingCode }).done(function (result) {
for (var i = 0; i < result.obj.Items.length; i++) {
thisObj.underlying_manager.Items.push({
code: result.obj.Items[i].code,
Price: result.obj.Items[i].Price,
weight: result.obj.Items[i].weight,
notional: result.obj.Items[i].notional
})
}
thisObj.sumprice();
});
}
thisObj.loadData2();
},
loadData2: function () {
var thisObj = this;
for (var i = 0; i < thisObj.underlying_manager.Items.length; i++) {
$("#Price" + i).text(thisObj.underlying_manager.Items[i].Price);
$("#weight" + i).text(1);
}
},
checked: function (a) {
$('.chek').prop("checked", a.checked);
},
///导入标的数据
uploadSettlementBill: function () {
layer.open({
type: 1,
title: '篮子标的导入',
content: $('#uploadTpl').html(),
area: ["450px", "200px"]
});
},
importDatas: function () {
var thisObj = this;
if (!this.checkSubmitData()) return;
var formData = new FormData($('#formImport')[0]);
main.post('/BasketUnderlying/UploadBasketData', formData, {
cache: false,
contentType: false,
processData: false,
success: function (res) {
if (res.success) {
if (res.obj.Items) {
for (var i = 0; i < res.obj.Items.length; i++) {
thisObj.underlying_manager.Items.push({
code: res.obj.Items[i].code,
weight: res.obj.Items[i].weight,
notional: res.obj.Items[i].notional,
Price: res.obj.Items[i].Price,
});
}
document.getElementById("head").style.display = "";
thisObj.sumprice();
}
main.message("导入成功");
} else if (res.msg) {
main.alert(res.msg);
}
layer.closeAll();
},
error: function (msg) {
main.alert(msg);
layer.closeAll();
}
});
},
checkSubmitData: function () {
//非Excel文件不允许用户导入
var fileName = $('#openFile').val();
if (fileName == "") {
main.message("请选择文件!");
return false;
}
var arr = "xlsx";
var index = fileName.lastIndexOf(".");
var ext = fileName.substr(index + 1);
if (ext == arr) {
return true;
}
else {
main.message("请导入xlsx格式文件!");
}
return true;
},
addUnderlying: function () {
var obj = consUnderlyings.find(function (obj, index) { return !obj.Disallow_Swap; });
if (obj) {
this.underlying_manager.Items.push({
Price: null,
code: obj.Code,
weight: null,
});
var index = this.underlying_manager.Items.length - 1;
this.changeunderlying(index);
this.$forceUpdate();
}
},
changeunderlying: function (obj) {
//行权价获取
var thisObj = this;
var index = obj;
//$(obj).attr("index");
var code = this.underlying_manager.Items[index].code;
main.post("/Pricing/AjaxGetUnderlying", { underlyingCode: code }).done(function (res) {
underlying = res.obj.underlying;
thisObj.underlying_manager.Items[index].weight = 1;
thisObj.underlying_manager.Items[index].Price = res.obj.underlying.Price;
thisObj.sumprice();
})
},
sumprice: function () {
var thisObj = this;
var sumprice = null;
var totalweight = null;
var lenth = [];
var underlyinglenth = thisObj.underlying_manager.Items.length;
for (var i = 0; i < underlyinglenth; i++) {
if (thisObj.underlying_manager.Items[i].code != null) {
var dd = lenth.length;
lenth[dd] = thisObj.underlying_manager.Items[i];
}
}
for (var i = 0; i < lenth.length; i++) {
sumprice += lenth[i].Price * lenth[i].weight;
totalweight += parseFloat(lenth[i].weight);
}
sumprice = sumprice / totalweight;
thisObj.underlying_manager.Price = sumprice.toFixed(4);
},
deleteUnderlying: function () {
var thisObj = this;
main.confirm("确认删除?", function () {
var items = [];
$(".chek:checked").each(function () {
items.push(this.value);
})
if (items.length <= 0) {
main.message("请选中要删除的标的");
return;
}
var ress = thisObj.underlying_manager.Items;
var newindexs = items.map(function (val, idx) {
return val - idx;
})
newindexs.forEach(function (index) {
ress.splice(index, 1);
})
$(".chek").prop("checked", false)
$("#parent").prop("checked", false);
thisObj.sumprice();
})
thisObj.$forceUpdate();
},
save: function () {
var thisObj = this;
if (main.isEmpty(thisObj.underlying_manager.UnderlyingCode)) {
main.message("别名不能为空");
return;
}
for (var index = 0; index < thisObj.underlying_manager.Items.length; index++) {
var currentValue = thisObj.underlying_manager.Items[index];
var arr = thisObj.underlying_manager.Items;
if (arr.some(function (value, i) {
return i != index && currentValue.code == value.code;
})) {
main.message("不能有重复的标的");
return;
}
}
main.confirm("确认修改?", function () {
main.post("/BasketUnderlying/SaveBasketUnderlyingSettingEdit", { req: thisObj.underlying_manager })
.done(function (res) {
if (parent) {
parent.layer.closeAll();
(parent.vue || parent).reloadunderlying_manager();
}
});
});
},
cancel: function () {
if (parent) {
parent.layer.closeAll();
}
},
},
components: {
'vue-underlying': vueUnderlying()
},
})