|
@@ -76,6 +76,152 @@ module.exports = app => {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
+ * 导入工料
|
|
|
|
+ * @return {void}
|
|
|
|
+ */
|
|
|
|
+ async exportData(datas, includeSpec = false, ms_id = null) {
|
|
|
|
+ if (!this.ctx.tender || !this.ctx.material) {
|
|
|
|
+ throw '数据错误';
|
|
|
|
+ }
|
|
|
|
+ let order = await this._getMaxOrder(this.ctx.tender.id);
|
|
|
|
+ const transaction = await this.db.beginTransaction();
|
|
|
|
+ try {
|
|
|
|
+ const resultData = {};
|
|
|
|
+ let needUpdateTp = false;
|
|
|
|
+ // 找出需要导入的工料
|
|
|
|
+ const bills = await this.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
|
|
|
|
+ const newBills = this._.differenceWith(datas, bills, function(item1, item2) {
|
|
|
|
+ return item1.code.toString() === item2.code.toString() && item1.name === item2.name && item1.unit === item2.unit && (!includeSpec || (includeSpec && item1.spec.toString() === item2.spec.toString()));
|
|
|
|
+ });
|
|
|
|
+ const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
|
|
|
|
+ if (newBills.length !== 0) {
|
|
|
|
+ const newBillsData = [];
|
|
|
|
+ const newMonthList = [];
|
|
|
|
+ for (const t of newBills) {
|
|
|
|
+ // 判断msg_tp是否为数字,不是则报错
|
|
|
|
+ if (t.msg_tp && !this._.isNumber(t.msg_tp)) {
|
|
|
|
+ throw '信息价数据存在非数字字符,无法导入';
|
|
|
|
+ }
|
|
|
|
+ t.msg_tp = t.msg_tp ? this.ctx.helper.round(t.msg_tp, this.ctx.material.decimal.up) : null;
|
|
|
|
+ const newBill = {
|
|
|
|
+ tid: this.ctx.tender.id,
|
|
|
|
+ mid: this.ctx.material.id,
|
|
|
|
+ order: order + 1,
|
|
|
|
+ code: t.code.toString(),
|
|
|
|
+ name: t.name,
|
|
|
|
+ unit: t.unit,
|
|
|
|
+ spec: t.spec,
|
|
|
|
+ msg_tp: this.ctx.material.is_stage_self ? null : t.msg_tp,
|
|
|
|
+ msg_times: this.ctx.material.is_stage_self ? null : t.msg_times,
|
|
|
|
+ msg_spread: this.ctx.material.is_stage_self ? null : t.msg_tp,
|
|
|
|
+ m_spread: this.ctx.material.is_stage_self ? null : t.msg_tp,
|
|
|
|
+ m_tp: 0,
|
|
|
|
+ m_tax_tp: 0,
|
|
|
|
+ in_time: new Date(),
|
|
|
|
+ };
|
|
|
|
+ order = order + 1;
|
|
|
|
+ newBillsData.push(newBill);
|
|
|
|
+ }
|
|
|
|
+ const result = await transaction.insert(this.tableName, newBillsData);
|
|
|
|
+ resultData.addNum = newBillsData.length;
|
|
|
|
+ for (let j = 0; j < newBills.length; j++) {
|
|
|
|
+ newBills[j].id = result.insertId + j;
|
|
|
|
+ if (material_month.length > 0) {
|
|
|
|
+ for (const ym of material_month) {
|
|
|
|
+ const one_month = {
|
|
|
|
+ tid: this.ctx.tender.id,
|
|
|
|
+ mid: this.ctx.material.id,
|
|
|
|
+ mb_id: newBills[j].id,
|
|
|
|
+ msg_tp: newBills[j].msg_tp,
|
|
|
|
+ yearmonth: ym,
|
|
|
|
+ };
|
|
|
|
+ newMonthList.push(one_month);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (this.ctx.material.is_stage_self) {
|
|
|
|
+ // 获取刚批量添加的所有list
|
|
|
|
+ await this.ctx.service.materialStageBills.adds(transaction, newBills, null, ms_id);
|
|
|
|
+ }
|
|
|
|
+ if (newMonthList.length > 0) await transaction.insert(this.ctx.service.materialMonth.tableName, newMonthList);
|
|
|
|
+ }
|
|
|
|
+ // 找出需要更新的工料
|
|
|
|
+ const updateBills = this._.intersectionWith(bills, datas, function(item1, item2) {
|
|
|
|
+ return item1.code.toString() === item2.code.toString() && item1.name === item2.name && item1.unit === item2.unit && (!includeSpec || (includeSpec && item1.spec.toString() === item2.spec.toString()));
|
|
|
|
+ });
|
|
|
|
+ if (updateBills.length !== 0) {
|
|
|
|
+ const updateDatas = [];
|
|
|
|
+ const updateMonthList = [];
|
|
|
|
+ for (const u of updateBills) {
|
|
|
|
+ const oneData = this._.find(datas, function(item) {
|
|
|
|
+ return item.code.toString() === u.code.toString() && item.name === u.name && item.unit === u.unit && (!includeSpec || (includeSpec && item.spec.toString() === u.spec.toString()));
|
|
|
|
+ });
|
|
|
|
+ const upd = { id: u.id };
|
|
|
|
+ oneData.msg_tp = this.ctx.helper.round(oneData.msg_tp, this.ctx.material.decimal.up);
|
|
|
|
+ // 判断msg_tp是否为数字,不是则报错
|
|
|
|
+ if (oneData.msg_tp && !this._.isNumber(oneData.msg_tp)) {
|
|
|
|
+ throw '信息价数据存在非数字字符,无法导入';
|
|
|
|
+ }
|
|
|
|
+ if (!this.ctx.material.is_stage_self) {
|
|
|
|
+ if (oneData.msg_tp !== u.msg_tp) {
|
|
|
|
+ needUpdateTp = true;
|
|
|
|
+ upd.msg_tp = oneData.msg_tp;
|
|
|
|
+ const [newmsg_spread, newm_spread] = await this.getSpread(u, oneData.msg_tp);
|
|
|
|
+ upd.msg_spread = newmsg_spread;
|
|
|
|
+ upd.m_spread = newm_spread;
|
|
|
|
+ const newTp = this.ctx.helper.round(this.ctx.helper.mul(u.quantity, newm_spread), this.ctx.material.decimal.tp);
|
|
|
|
+ upd.m_tp = newTp;
|
|
|
|
+ upd.m_tax_tp = this.ctx.helper.round(this.ctx.helper.mul(newTp, (1 + this.ctx.helper.div(u.m_tax, 100))), this.ctx.material.decimal.tp);
|
|
|
|
+ }
|
|
|
|
+ if (oneData.msg_times !== u.msg_times) {
|
|
|
|
+ upd.msg_times = oneData.msg_times;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (!includeSpec && oneData.spec !== u.spec) {
|
|
|
|
+ upd.spec = oneData.spec;
|
|
|
|
+ }
|
|
|
|
+ if (!this._.isEqual(upd, { id: u.id })) updateDatas.push(upd);
|
|
|
|
+ // 判断是否有月信息价,如果有则msg_tp都更改为统一值
|
|
|
|
+ if (material_month.length > 0) {
|
|
|
|
+ const monthList = await transaction.select(this.ctx.service.materialMonth.tableName, { where: { mb_id: u.id, mid: this.ctx.material.id } });
|
|
|
|
+ if (monthList.length !== 0) {
|
|
|
|
+ for (const m of monthList) {
|
|
|
|
+ // 更新月信息单价小数位
|
|
|
|
+ if (oneData.msg_tp !== m.msg_tp) {
|
|
|
|
+ m.msg_tp = oneData.msg_tp;
|
|
|
|
+ updateMonthList.push({ id: m.id, msg_tp: m.msg_tp });
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (updateDatas.length !== 0) await transaction.updateRows(this.tableName, updateDatas);
|
|
|
|
+ resultData.updateNum = updateDatas.length;
|
|
|
|
+ if (updateMonthList.length !== 0) await transaction.updateRows(this.ctx.service.materialMonth.tableName, updateMonthList);
|
|
|
|
+ if (this.ctx.material.is_stage_self) {
|
|
|
|
+ needUpdateTp = await this.ctx.service.materialStageBills.updateByExport(transaction, updateBills, datas, includeSpec, ms_id);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // 更新materialStage 值
|
|
|
|
+ if (this.ctx.material.is_stage_self) {
|
|
|
|
+ resultData.stageBillsData = await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id } });
|
|
|
|
+ resultData.stageData = await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } });
|
|
|
|
+ }
|
|
|
|
+ resultData.billsData = await transaction.select(this.tableName, { where: { tid: this.ctx.tender.id }, orders: [['order', 'asc']] });
|
|
|
|
+ if (material_month.length > 0) {
|
|
|
|
+ resultData.monthsList = await this.ctx.service.materialMonth.getMonthList(resultData.billsData, transaction);
|
|
|
|
+ }
|
|
|
|
+ resultData.m_tp = needUpdateTp ? await this.calcMaterialMTp(transaction) : this.ctx.material.m_tp;
|
|
|
|
+ await transaction.commit();
|
|
|
|
+ return resultData;
|
|
|
|
+ } catch (error) {
|
|
|
|
+ console.log(error);
|
|
|
|
+ await transaction.rollback();
|
|
|
|
+ throw error;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
* 添加工料
|
|
* 添加工料
|
|
* @return {void}
|
|
* @return {void}
|
|
*/
|
|
*/
|