|
@@ -155,32 +155,54 @@ module.exports = app => {
|
|
|
* @returns {Promise<*>}
|
|
|
*/
|
|
|
async calcQuantityByMB(transaction, mid, mb, materialCalculator) {
|
|
|
+ const [newmsg_spread, newm_spread] = await this.getSpread(mb);
|
|
|
if (mb.t_type === materialConst.t_type[0].value) {
|
|
|
const sql = 'SELECT SUM(`gather_qty`*`quantity`) as quantity FROM ' + this.ctx.service.materialList.tableName + ' WHERE `mid`=? AND `mb_id`=? AND `is_join`=1';
|
|
|
const sqlParam = [mid, mb.id];
|
|
|
const mb_quantity = await transaction.queryOne(sql, sqlParam);
|
|
|
console.log(mb_quantity);
|
|
|
- // 取历史期记录获取截止上期调差金额
|
|
|
+ // 取历史期记录获取截止上期调差金额,并清空本期单价和时间,重新计算价差和有效价差
|
|
|
const updateData = {
|
|
|
id: mb.id,
|
|
|
quantity: this.ctx.helper.round(mb_quantity.quantity, 3),
|
|
|
- pre_tp: mb.quantity && mb.m_spread !== null ? this.ctx.helper.round(this.ctx.helper.add(mb.pre_tp, this.ctx.helper.mul(mb.quantity, mb.m_spread)), 2) : mb.pre_tp,
|
|
|
+ msg_tp: null,
|
|
|
+ msg_times: null,
|
|
|
+ msg_spread: newmsg_spread,
|
|
|
+ m_spread: newm_spread,
|
|
|
+ pre_tp: mb.m_tp !== null ? this.ctx.helper.round(this.ctx.helper.add(mb.pre_tp, mb.m_tp), 2) : mb.pre_tp,
|
|
|
};
|
|
|
await transaction.update(this.tableName, updateData);
|
|
|
- return await this.ctx.helper.round(this.ctx.helper.mul(mb_quantity.quantity, mb.m_spread), 2);
|
|
|
+ return await this.ctx.helper.round(this.ctx.helper.mul(mb_quantity.quantity, newm_spread), 2);
|
|
|
} else if (mb.t_type === materialConst.t_type[1].value) {
|
|
|
const quantity = await materialCalculator.calculateExpr(mb.expr);
|
|
|
const updateData = {
|
|
|
id: mb.id,
|
|
|
quantity: quantity !== 0 && quantity !== null ? this.ctx.helper.round(quantity, 3) : null,
|
|
|
- pre_tp: mb.quantity && mb.m_spread !== null ? this.ctx.helper.round(this.ctx.helper.add(mb.pre_tp, this.ctx.helper.mul(mb.quantity, mb.m_spread)), 2) : mb.pre_tp,
|
|
|
+ msg_tp: null,
|
|
|
+ msg_times: null,
|
|
|
+ msg_spread: newmsg_spread,
|
|
|
+ m_spread: newm_spread,
|
|
|
+ pre_tp: mb.m_tp !== null ? this.ctx.helper.round(this.ctx.helper.add(mb.pre_tp, mb.m_tp), 2) : mb.pre_tp,
|
|
|
};
|
|
|
await transaction.update(this.tableName, updateData);
|
|
|
- return await this.ctx.helper.round(this.ctx.helper.mul(quantity, mb.m_spread), 2);
|
|
|
+ return await this.ctx.helper.round(this.ctx.helper.mul(quantity, newm_spread), 2);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 清空本期信息价后更新价差和有效价差
|
|
|
+ * @param data
|
|
|
+ * @returns {Promise<void>}
|
|
|
+ */
|
|
|
+ async getSpread(data) {
|
|
|
+ data.msg_tp = null;
|
|
|
+ const msg_spread = this.ctx.helper.round(this.ctx.helper.sub(data.msg_tp, data.basic_price), 3);
|
|
|
+ const cor = msg_spread >= 0 ? this.ctx.helper.mul(data.basic_price, this.ctx.helper.div(data.m_up_risk, 100)) : this.ctx.helper.mul(data.basic_price, this.ctx.helper.div(data.m_down_risk, 100));
|
|
|
+ const m_spread = Math.abs(msg_spread) > Math.abs(cor) ? (msg_spread > 0 ? this.ctx.helper.round(this.ctx.helper.sub(msg_spread, cor), 3) : this.ctx.helper.round(this.ctx.helper.add(msg_spread, cor), 3)) : 0;
|
|
|
+ return [msg_spread, m_spread];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 修改 expr和quantity值,返回本期金额和单条数据
|
|
|
* @param data
|
|
|
* @returns {Promise<void>}
|