123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date 2021/11/9
- * @version
- */
- module.exports = app => {
- class Budget extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'budget';
- }
- /**
- * 数据规则
- *
- * @param {String} scene - 场景
- * @return {Object} - 返回数据规则
- */
- rule(scene) {
- let rule = {};
- switch (scene) {
- case 'add':
- rule = {
- name: { type: 'string', required: true, min: 2 },
- std_id: { type: 'string', required: true, min: 1 },
- };
- break;
- case 'save':
- rule = {
- name: { type: 'string', required: true, min: 2, max: 100, },
- };
- default:
- break;
- }
- return rule;
- }
- /**
- * 新增标段
- *
- * @param {Object} data - 提交的数据
- * @return {Boolean} - 返回新增结果
- */
- async add(data) {
- const budgetStd = await this.ctx.service.budgetStd.getDataById(data.std_id);
- if (!budgetStd) throw '选择的概算标准不存在,请刷新页面重试';
- const conn = await this.db.beginTransaction();
- try {
- // 获取当前用户信息
- const sessionUser = this.ctx.session.sessionUser;
- // 获取当前项目信息
- const sessionProject = this.ctx.session.sessionProject;
- const insertData = {
- pid: sessionProject.id, user_id: sessionUser.accountId, in_time: new Date(),
- name: data.name, std_id: data.std_id,
- };
- const operate = await conn.insert(this.tableName, insertData);
- if (operate.insertId === 0) throw '新增标段数据失败';
- // 获取合同支付模板 并添加到标段
- await this.ctx.service.budgetGu.initByTemplate(conn, operate.insertId, budgetStd.gu_template_id);
- await this.ctx.service.budgetGai.initByTemplate(conn, operate.insertId, budgetStd.gu_template_id);
- await this.ctx.service.budgetYu.initByTemplate(conn, operate.insertId, budgetStd.gu_template_id);
- await conn.commit();
- return await this.getDataById(operate.insertId);
- } catch (error) {
- await conn.rollback();
- throw error;
- }
- }
- /**
- * 保存标段
- *
- * @param {Number} id
- * @param {Object} postData - 表单post过来的数
- * @return {Boolean} - 返回执行结果
- */
- async save(id, postData) {
- const rowData = { id, name: postData.name };
- const result = await this.db.update(this.tableName, rowData);
- return result.affectedRows > 0;
- }
- /**
- * 假删除
- *
- * @param {Number} id - 删除的id
- * @return {Boolean} - 删除结果
- */
- async deleteBudget(id) {
- const updateData = { id, status: this.status.DISABLE };
- const result = await this.db.update(this.tableName, updateData);
- return result.affectedRows > 0;
- }
- /**
- * 真删除
- * @param {Number} id - 删除的标段id
- * @return {Promise<boolean>} - 结果
- */
- async deleteBudgetNoBackup(id) {
- const transaction = await this.db.beginTransaction();
- try {
- await transaction.delete(this.tableName, { id });
- await transaction.delete(this.ctx.service.guBudget.tableName, { bid: id });
- await transaction.delete(this.ctx.service.gaiBudget.tableName, { bid: id });
- await transaction.delete(this.ctx.service.yuBudget.tableName, { bid: id });
- await transaction.commit();
- return true;
- } catch (err) {
- this.ctx.log(err);
- await transaction.rollback();
- return false;
- }
- }
- }
- return Budget;
- };
|