123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date
- * @version
- */
- const audit = require('../const/audit').common;
- module.exports = app => {
- class PhasePay extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'phase_pay';
- }
- analysisPhasePay(data) {
- const datas = data instanceof Array ? data : [data];
- datas.forEach(x => {
- x.rela_stage = x.rela_stage ? JSON.parse(x.rela_stage) : [];
- x.calc_base = x.calc_base ? JSON.parse(x.calc_base) : [];
- });
- }
- calculatePhasePay(data) {
- const helper = this.ctx.helper;
- const datas = data instanceof Array ? data : [data];
- const formatNum = this.ctx.tender.info.display.thousandth ? this.ctx.helper.formatNum : function(num) { return num ? num + '' : ''; };
- datas.forEach(x => {
- x.end_calc_tp = helper.add(x.calc_tp, x.pre_calc_tp);
- x.end_pay_tp = helper.add(x.pay_tp, x.pre_pay_tp);
- x.end_cut_tp = helper.add(x.cut_tp, x.pre_cut_tp);
- x.end_sf_tp = helper.add(x.sf_tp, x.pre_sf_tp);
- x.end_yf_tp = helper.add(x.yf_tp, x.pre_yf_tp);
- if (thousandth)
- for (const prop in x) {
- if (prop.indexOf('_tp') > 0) {
- x['display_' + prop] = formatNum(x[prop]);
- }
- }
- });
- }
- /**
- * 获取全部修订
- * @param tid
- * @returns {Promise<*>}
- */
- async getAllPhasePay (tid, sort = 'ASC') {
- const result = await this.getAllDataByCondition({
- where: {tid: tid},
- orders: [['phase_order', sort]],
- });
- this.analysisPhasePay(result);
- return result;
- }
- async getPhasePay(id) {
- const result = await this.getDataById(id);
- this.analysisPhasePay(result);
- return result;
- }
- async getPhasePayByOrder(tid, phaseOrder) {
- const result = await this.getDataByCondition({ tid, phase_order: phaseOrder });
- this.analysisPhasePay(result);
- return result;
- }
- async loadUser(phasePay) {
- // todo 加载审批人
- }
- async getNewOrder(tid) {
- const sql = 'SELECT Max(`phase_order`) As max_order FROM ' + this.tableName + ' Where `tid` = ?';
- const sqlParam = [tid];
- const result = await this.db.queryOne(sql, sqlParam);
- return result.max_order || 0;
- }
- async _checkRelaStageConflict(relaStage, phasePay) {
- const pays = this.getAllPhasePay(phasePay.tid);
- for (const p of pays) {
- if (p.id === phasePay.id) continue;
- for (const s of relaStage) {
- if (p.rela_stage.find(x => { return x.id === s.id; })) return true;
- }
- }
- return false;
- }
- async getCalcBase(relaStage) {
- // todo 获取计算基数
- }
- async add(tid, relaStage, phaseDate, memo) {
- if (!tid) throw '数据错误';
- const user_id = this.ctx.session.sessionUser.accountId;
- const maxOrder = await this.getNewOrder(tid);
- const data = {
- id: this.uuid.v4(), tid: tid, create_user_id: user_id, update_user_id: user_id,
- phase_order: maxOrder + 1, phase_date: phaseDate, memo,
- audit_times: 1, audit_status: audit.status.uncheck,
- rela_stage: JSON.stringify(relaStage.map(s => { return {stage_id: s.id, stage_order: s.order}; })),
- };
- if (await this._checkRelaStageConflict(relaStage, data)) throw '选择的计量期,已被调用,请刷新页面后选择计量期新增合同支付';
- const calcBase = await this.getCalcBase(relaStage);
- data.calc_base = JSON.stringify(calcBase);
- const transaction = await this.db.beginTransaction();
- try {
- const result = await transaction.insert(this.tableName, data);
- if (result.affectedRows !== 1) throw '新增合同支付失败';
- await this.ctx.service.phasePayDetail.initPhaseData(data);
- await transaction.commit();
- return data;
- } catch(err) {
- await transaction.rollback();
- throw err;
- }
- }
- async refreshCalcBase(id) {
- const curPay = this.getPhasePay(id);
- if (!curPay) throw '合同支付不存在, 请刷新页面重试';
- const calcBase = await this.getCalcBase(relaStage);
- await this.defaultUpdate({
- id, update_user_id: this.ctx.session.sessionUser.accountId,
- calc_base: JSON.stringify(calcBase),
- rela_stage: JSON.stringify(relaStage.map(s => { return {stage_id: s.id, stage_order: s.order}; })),
- calc_base_time: new Date()
- });
- }
- async resetRelaStageId(id, relaStage) {
- const curPay = this.getPhasePay(id);
- if (!curPay) throw '合同支付不存在, 请刷新页面重试';
- if (await this._checkRelaStageConflict(relaStage, curPay)) throw '选择的计量期,已被调用,请刷新页面后选择计量期新增合同支付';
- const calcBase = await this.getCalcBase(relaStage);
- await this.defaultUpdate({
- id, update_user_id: this.ctx.session.sessionUser.accountId,
- calc_base: JSON.stringify(calcBase),
- rela_stage: JSON.stringify(relaStage.map(s => { return {stage_id: s.id, stage_order: s.order}; })),
- calc_base_time: new Date()
- });
- }
- }
- return PhasePay;
- };
|