123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- 'use strict';
- /**
- * 标段数据模型
- *
- * @author CaiAoLin
- * @date 2017/11/30
- * @version
- */
- const tenderConst = require('../const/tender');
- const auditConst = require('../const/audit');
- const fs = require('fs');
- const path = require('path');
- const commonQueryColumns = ['id', 'project_id', 'name', 'status', 'category', 'ledger_times', 'ledger_status', 'measure_type', 'user_id', 'valuation', 'total_price', 'deal_tp'];
- module.exports = app => {
- class Tender extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'tender';
- // 状态相关
- this.status = {
- TRY: 1,
- NORMAL: 2,
- DISABLE: 3,
- };
- this.displayStatus = [];
- this.displayStatus[this.status.TRY] = '试用';
- this.displayStatus[this.status.NORMAL] = '正常';
- this.displayStatus[this.status.DISABLE] = '禁用';
- this.statusClass = [];
- this.statusClass[this.status.TRY] = 'warning';
- this.statusClass[this.status.NORMAL] = 'success';
- this.statusClass[this.status.DISABLE] = 'danger';
- }
- /**
- * 数据规则
- *
- * @param {String} scene - 场景
- * @return {Object} - 返回数据规则
- */
- rule(scene) {
- let rule = {};
- switch (scene) {
- case 'add':
- rule = {
- name: { type: 'string', required: true, min: 2 },
- type: { type: 'string', required: true, min: 1 },
- };
- break;
- case 'save':
- rule = {
- name: { type: 'string', required: true, min: 2 },
- type: { type: 'string', required: true, min: 1 },
- };
- default:
- break;
- }
- return rule;
- }
- /**
- * 获取你所参与的标段的列表
- *
- * @return {Array} - 返回标段数据
- */
- async getList(listStatus = '', permission = null) {
- // 获取当前项目信息
- const session = this.ctx.session;
- let sql = '';
- let sqlParam = [];
- if (listStatus === 'manage') {
- // 管理页面只取属于自己创建的标段
- sql = 'SELECT t.`id`, t.`project_id`, t.`name`, t.`status`, t.`category`, t.`ledger_times`, t.`ledger_status`, t.`measure_type`, t.`user_id`, t.`create_time`, t.`total_price`, t.`deal_tp`,' +
- ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
- ' FROM ?? As t ' +
- ' Left Join ?? As pa ' +
- ' ON t.`user_id` = pa.`id` ' +
- ' WHERE t.`project_id` = ? AND t.`user_id` = ?';
- sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id, session.sessionUser.accountId];
- } else if (permission !== null && permission.tender !== undefined && permission.tender.indexOf('2') !== -1) {
- // 具有查看所有标段权限的用户查阅标段
- sql = 'SELECT t.`id`, t.`project_id`, t.`name`, t.`status`, t.`category`, t.`ledger_times`, t.`ledger_status`, t.`measure_type`, t.`user_id`, t.`create_time`, t.`total_price`, t.`deal_tp`,' +
- ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
- ' FROM ?? As t ' +
- ' Left Join ?? As pa ' +
- ' ON t.`user_id` = pa.`id` ' +
- ' WHERE t.`project_id` = ?';
- sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id];
- } else {
- // 根据用户权限查阅标段
- // tender 163条数据,project_account 68条数据测试
- // 查询两张表耗时0.003s,查询tender左连接project_account耗时0.002s
- sql = 'SELECT t.`id`, t.`project_id`, t.`name`, t.`status`, t.`category`, t.`ledger_times`, t.`ledger_status`, t.`measure_type`, t.`user_id`, t.`create_time`, t.`total_price`, t.`deal_tp`,' +
- ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
- // ' FROM ?? As t, ?? As pa ' +
- // ' WHERE t.`project_id` = ? AND t.`user_id` = pa.`id` AND (' +
- ' FROM ?? As t ' +
- ' Left Join ?? As pa ' +
- ' ON t.`user_id` = pa.`id` ' +
- ' WHERE t.`project_id` = ? AND (' +
- // 创建的标段
- ' t.`user_id` = ?' +
- // 参与审批 台账 的标段
- ' OR (t.`ledger_status` != ' + auditConst.ledger.status.uncheck + ' AND ' +
- ' t.id IN ( SELECT la.`tender_id` FROM ?? As la WHERE la.`audit_id` = ? GROUP BY la.`tender_id`))' +
- // 参与审批 计量期 的标段
- ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
- ' t.id IN ( SELECT sa.`tid` FROM ?? As sa WHERE sa.`aid` = ? GROUP BY sa.`tid`))' +
- // 参与审批 变更令 的标段
- ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
- ' t.id IN ( SELECT ca.`tid` FROM ?? AS ca WHERE ca.`uid` = ? GROUP BY ca.`tid`))' +
- // 未参与,但可见的标段
- ')';
- sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id, session.sessionUser.accountId,
- this.ctx.service.ledgerAudit.tableName, session.sessionUser.accountId,
- this.ctx.service.stageAudit.tableName, session.sessionUser.accountId,
- this.ctx.service.changeAudit.tableName, session.sessionUser.accountId];
- }
- const list = await this.db.query(sql, sqlParam);
- for (const l of list) {
- l.category = l.category && l.category !== '' ? JSON.parse(l.category) : null;
- }
- return list;
- }
- async getTender (id) {
- this.initSqlBuilder();
- this.sqlBuilder.setAndWhere('id', {
- value: id,
- operate: '=',
- });
- this.sqlBuilder.columns = commonQueryColumns;
- const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
- const tender = await this.db.queryOne(sql, sqlParam);
- if (tender) {
- tender.category = tender.category && tender.category !== '' ? JSON.parse(tender.category) : null;
- }
- return tender;
- }
- /**
- * 新增标段
- *
- * @param {Object} data - 提交的数据
- * @return {Boolean} - 返回新增结果
- */
- async add(data) {
- let result = false;
- const templateId = await this.ctx.service.valuation.getValuationTemplate(data.valuation);
- this.transaction = await this.db.beginTransaction();
- try {
- // 获取当前用户信息
- const sessionUser = this.ctx.session.sessionUser;
- // 获取当前项目信息
- const sessionProject = this.ctx.session.sessionProject;
- const insertData = {
- name: data.name,
- status: tenderConst.status.APPROVAL,
- project_id: sessionProject.id,
- user_id: sessionUser.accountId,
- create_time: new Date(),
- category: JSON.stringify(data.category),
- valuation: data.valuation,
- };
- const operate = await this.transaction.insert(this.tableName, insertData);
- result = operate.insertId > 0;
- if (!result) {
- throw '新增标段数据失败';
- }
- // 获取标段项目节点模板
- const tenderNodeTemplateData = await this.ctx.service.tenderNodeTemplate.getData(templateId);
- // 复制模板数据到标段数据表
- result = await this.ctx.service.ledger.innerAdd(tenderNodeTemplateData, operate.insertId, this.transaction);
- if (!result) {
- throw '新增标段项目节点失败';
- }
- // 获取合同支付模板 并添加到标段
- result = await this.ctx.service.pay.addDefaultPayData(operate.insertId, this.transaction);
- if (!result) {
- throw '新增合同支付数据失败';
- }
- await this.transaction.commit();
- const sql = 'SELECT t.`id`, t.`project_id`, t.`name`, t.`status`, t.`category`, t.`ledger_times`, t.`ledger_status`, t.`measure_type`, t.`user_id`, t.`create_time`, t.`total_price`, t.`deal_tp`,' +
- ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
- ' FROM ?? As t ' +
- ' Left Join ?? As pa ' +
- ' ON t.`user_id` = pa.`id` ' +
- ' WHERE t.`id` = ?';
- const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, operate.insertId];
- const tender = await this.db.queryOne(sql, sqlParam);
- if (tender) {
- tender.category = tender.category && tender.category !== '' ? JSON.parse(tender.category) : null;
- }
- return tender;
- } catch (error) {
- await this.transaction.rollback();
- throw error;
- }
- }
- /**
- * 保存标段
- *
- * @param {Object} postData - 表单post过来的数据
- * @param {Number} id - 用于判断修改还是新增的id
- * @return {Boolean} - 返回执行结果
- */
- async save(postData, id = 0) {
- id = parseInt(id);
- const rowData = {
- id: id,
- name: postData.name,
- type: postData.type,
- category: JSON.stringify(postData.category),
- };
- const result = await this.db.update(this.tableName, rowData);
- return result.affectedRows > 0;
- }
- /**
- * 假删除
- *
- * @param {Number} id - 删除的id
- * @return {Boolean} - 删除结果
- */
- async deleteTenderById(id) {
- const updateData = {
- status: this.status.DISABLE,
- id,
- };
- const result = await this.db.update(this.tableName, updateData);
- return result.affectedRows > 0;
- }
- /**
- * 真删除
- * @param {Number} id - 删除的标段id
- * @returns {Promise<boolean>} - 结果
- */
- async deleteTenderNoBackup(id) {
- const transaction = await this.db.beginTransaction();
- try {
- await transaction.delete(this.tableName, {id: id});
- await transaction.delete(this.ctx.service.tenderInfo.tableName, {tid: id});
- await transaction.delete(this.ctx.service.ledger.tableName, {tender_id: id});
- await transaction.delete(this.ctx.service.ledgerAudit.tableName, {tender_id: id});
- await transaction.delete(this.ctx.service.pos.tableName, {tid: id});
- await transaction.delete(this.ctx.service.pay.tableName, {tid: id});
- await transaction.delete(this.ctx.service.stage.tableName, {tid: id});
- await transaction.delete(this.ctx.service.stageAudit.tableName, {tid: id});
- await transaction.delete(this.ctx.service.stageBills.tableName, {tid: id});
- await transaction.delete(this.ctx.service.stagePos.tableName, {tid: id});
- await transaction.delete(this.ctx.service.stageDetail.tableName, {tid: id});
- await transaction.delete(this.ctx.service.stagePay.tableName, {tid: id});
- await transaction.delete(this.ctx.service.change.tableName, {tid: id});
- await transaction.delete(this.ctx.service.changeAudit.tableName, {tid: id});
- await transaction.delete(this.ctx.service.changeAuditList.tableName, {tid: id});
- await transaction.delete(this.ctx.service.changeCompany.tableName, {tid: id});
- // 先删除附件文件
- const attList = await this.ctx.service.changeAtt.getAllDataByCondition({ where: { tid: id } });
- if (attList.length !== 0) {
- for (const att of attList) {
- if (fs.existsSync(path.join(this.app.baseDir, att.filepath))) {
- await fs.unlinkSync(path.join(this.app.baseDir, att.filepath));
- }
- }
- }
- await transaction.delete(this.ctx.service.changeAtt.tableName, {tid: id});
- await transaction.commit();
- return true;
- } catch (err) {
- this.ctx.helper.log(err);
- await transaction.rollback();
- return false;
- }
- }
- /**
- * 切换标段
- *
- * @param {Number} tenderId - 标段id
- * @return {Boolean} - 返回切换结果
- */
- async switchTender(tenderId) {
- // 获取该用户拥有的项目数据
- const sessionUser = this.ctx.session.sessionUser;
- const tenderInfo = await this.ctx.service.projectAccount.getProjectInfoByAccount(sessionUser.account);
- let result = false;
- // 判断切换的标段是否属于对应用户
- if (tenderInfo.length < 0) {
- return result;
- }
- let targetTender = {};
- for (const tmp of tenderInfo) {
- if (tmp.id === tenderId) {
- result = true;
- targetTender = tmp;
- }
- }
- // 成功后更改session
- if (result) {
- this.ctx.session.sessionTender = {
- id: targetTender.id,
- name: targetTender.name,
- userAccount: targetTender.user_account,
- };
- }
- return result;
- }
- async checkTender(tid) {
- if (this.ctx.tender) return;
- const tender = await this.ctx.service.tender.getTender(tid);
- tender.info = await this.ctx.service.tenderInfo.getTenderInfo(tid);
- this.ctx.tender = tender;
- }
- }
- return Tender;
- };
|