tender.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. 'use strict';
  2. /**
  3. * 标段数据模型
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/11/30
  7. * @version
  8. */
  9. const tenderConst = require('../const/tender');
  10. const auditConst = require('../const/audit');
  11. const projectLogConst = require('../const/project_log');
  12. const fs = require('fs');
  13. const path = require('path');
  14. const commonQueryColumns = [
  15. 'id', 'project_id', 'name', 'status', 'category', 'ledger_times', 'ledger_status', 'measure_type', 'user_id', 'valuation', 'create_time',
  16. 'total_price', 'deal_tp', 'copy_id', 's2b_gxby_check', 's2b_gxby_limit', 's2b_dagl_check', 's2b_dagl_limit', 'has_rela', 'his_id', 'rpt_show_level',
  17. 'build_status', 'settle_order', 'spid',
  18. ];
  19. module.exports = app => {
  20. class Tender extends app.BaseService {
  21. /**
  22. * 构造函数
  23. *
  24. * @param {Object} ctx - egg全局变量
  25. * @return {void}
  26. */
  27. constructor(ctx) {
  28. super(ctx);
  29. this.tableName = 'tender';
  30. // 状态相关
  31. this.status = {
  32. TRY: 1,
  33. NORMAL: 2,
  34. DISABLE: 3,
  35. };
  36. this.displayStatus = [];
  37. this.displayStatus[this.status.TRY] = '试用';
  38. this.displayStatus[this.status.NORMAL] = '正常';
  39. this.displayStatus[this.status.DISABLE] = '禁用';
  40. this.statusClass = [];
  41. this.statusClass[this.status.TRY] = 'warning';
  42. this.statusClass[this.status.NORMAL] = 'success';
  43. this.statusClass[this.status.DISABLE] = 'danger';
  44. }
  45. /**
  46. * 数据规则
  47. *
  48. * @param {String} scene - 场景
  49. * @return {Object} - 返回数据规则
  50. */
  51. rule(scene) {
  52. let rule = {};
  53. switch (scene) {
  54. case 'add':
  55. rule = {
  56. name: { type: 'string', required: true, min: 2 },
  57. type: { type: 'string', required: true, min: 1 },
  58. };
  59. break;
  60. case 'save':
  61. rule = {
  62. name: { type: 'string', required: true, min: 2 },
  63. type: { type: 'string', required: true, min: 1 },
  64. };
  65. default:
  66. break;
  67. }
  68. return rule;
  69. }
  70. /**
  71. * 获取你所参与的标段的列表
  72. *
  73. * @param {String} listStatus - 取列表状态,如果是管理页要传
  74. * @param {String} permission - 根据权限取值
  75. * @param {Number} getAll - 是否取所有标段
  76. * @return {Array} - 返回标段数据
  77. */
  78. async getList(listStatus = '', permission = null, getAll = 0, buildStatusFilter = '') {
  79. // 获取当前项目信息
  80. const session = this.ctx.session;
  81. let sql = '';
  82. let sqlParam = [];
  83. if (listStatus === 'manage') {
  84. const userFilter = getAll ? '' : this.db.format(' And t.user_id = ? ', [session.sessionUser.accountId]);
  85. // 管理页面只取属于自己创建的标段
  86. 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`, t.`spid`,' +
  87. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  88. ' FROM ?? As t ' +
  89. ' Left Join ?? As pa ' +
  90. ' ON t.`user_id` = pa.`id` ' +
  91. ' WHERE t.`project_id` = ? ' + buildStatusFilter + userFilter + ' ORDER BY CONVERT(t.`name` USING GBK) ASC';
  92. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id];
  93. } else if (getAll === 1 || (permission !== null && permission.tender !== undefined && permission.tender.indexOf('2') !== -1)) {
  94. // 具有查看所有标段权限的用户查阅标段
  95. 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`, t.`spid`,' +
  96. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  97. ' FROM ?? As t ' +
  98. ' Left Join ?? As pa ' +
  99. ' ON t.`user_id` = pa.`id` ' +
  100. ' WHERE t.`project_id` = ?' + buildStatusFilter + ' ORDER BY CONVERT(t.`name` USING GBK) ASC';
  101. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id];
  102. } else {
  103. // 根据用户权限查阅标段
  104. // tender 163条数据,project_account 68条数据测试
  105. // 查询两张表耗时0.003s,查询tender左连接project_account耗时0.002s
  106. const changeProjectSql = this.ctx.session.sessionProject.page_show.openChangeProject ? ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  107. ' t.id IN ( SELECT cpa.`tid` FROM ' + this.ctx.service.changeProjectAudit.tableName + ' AS cpa WHERE cpa.`aid` = ' + session.sessionUser.accountId + ' GROUP BY cpa.`tid`))' : '';
  108. const changeApplySql = this.ctx.session.sessionProject.page_show.openChangeApply ? ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  109. ' t.id IN ( SELECT caa.`tid` FROM ' + this.ctx.service.changeApplyAudit.tableName + ' AS caa WHERE caa.`aid` = ' + session.sessionUser.accountId + ' GROUP BY caa.`tid`))' : '';
  110. const changePlanSql = this.ctx.session.sessionProject.page_show.openChangePlan ? ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  111. ' t.id IN ( SELECT cpla.`tid` FROM ' + this.ctx.service.changePlanAudit.tableName + ' AS cpla WHERE cpla.`aid` = ' + session.sessionUser.accountId + ' GROUP BY cpla.`tid`))' : '';
  112. const changeProjectXsSql = this.ctx.session.sessionProject.page_show.openChangeProject ? ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  113. ' t.id IN ( SELECT cpxa.`tid` FROM ' + this.ctx.service.changeProjectXsAudit.tableName + ' AS cpxa WHERE cpxa.`aid` = ' + session.sessionUser.accountId + ' GROUP BY cpxa.`tid`))' : '';
  114. 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`,' +
  115. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  116. // ' FROM ?? As t, ?? As pa ' +
  117. // ' WHERE t.`project_id` = ? AND t.`user_id` = pa.`id` AND (' +
  118. ' FROM ?? As t ' +
  119. ' Left Join ?? As pa ' +
  120. ' ON t.`user_id` = pa.`id` ' +
  121. ' WHERE t.`project_id` = ? ' + buildStatusFilter + ' AND (' +
  122. // 创建的标段
  123. ' t.`user_id` = ?' +
  124. // 参与审批 台账 的标段
  125. ' OR (t.`ledger_status` != ' + auditConst.ledger.status.uncheck + ' AND ' +
  126. ' t.id IN ( SELECT la.`tender_id` FROM ?? As la WHERE la.`audit_id` = ? GROUP BY la.`tender_id`))' +
  127. // 参与审批 计量期 的标段
  128. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  129. ' t.id IN ( SELECT sa.`tid` FROM ?? As sa WHERE sa.`aid` = ? GROUP BY sa.`tid`))' +
  130. // 参与协审
  131. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  132. ' t.id IN ( SELECT sa.`tid` FROM ?? As sa WHERE sa.`ass_user_id` = ? GROUP BY sa.`tid`))' +
  133. // 参与审批 结算期 的标段
  134. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  135. ' t.id IN ( SELECT sa.`tid` FROM ?? As sa WHERE sa.`audit_id` = ? GROUP BY sa.`tid`))' +
  136. // 参与审批 变更令 的标段
  137. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  138. ' t.id IN ( SELECT ca.`tid` FROM ?? AS ca WHERE ca.`uid` = ? GROUP BY ca.`tid`))' +
  139. // 参与审批 台账修订 的标段
  140. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  141. ' t.id IN ( SELECT ra.`tender_id` FROM ?? AS ra WHERE ra.`audit_id` = ? GROUP BY ra.`tender_id`))' +
  142. // 参与审批 材料调差 的标段
  143. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  144. ' t.id IN ( SELECT ma.`tid` FROM ?? AS ma WHERE ma.`aid` = ? GROUP BY ma.`tid`))' +
  145. // 参与审批 预付款 的标段
  146. ' OR (t.id IN ( SELECT ad.`tid` FROM ?? AS ad WHERE ad.`audit_id` = ? GROUP BY ad.`tid`))' +
  147. // 参与审批 变更立项书及变更申请 的标段
  148. changeProjectSql + changeApplySql + changePlanSql + changeProjectXsSql +
  149. // 游客权限的标段
  150. ' OR (t.id IN ( SELECT tt.`tid` FROM ?? AS tt WHERE tt.`user_id` = ?))' +
  151. // 未参与,但可见的标段
  152. ') ORDER BY CONVERT(t.`name` USING GBK) ASC';
  153. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id, session.sessionUser.accountId,
  154. this.ctx.service.ledgerAudit.tableName, session.sessionUser.accountId,
  155. this.ctx.service.stageAudit.tableName, session.sessionUser.accountId,
  156. this.ctx.service.auditAss.tableName, session.sessionUser.accountId,
  157. this.ctx.service.settleAudit.tableName, session.sessionUser.accountId,
  158. this.ctx.service.changeAudit.tableName, session.sessionUser.accountId,
  159. this.ctx.service.reviseAudit.tableName, session.sessionUser.accountId,
  160. this.ctx.service.materialAudit.tableName, session.sessionUser.accountId,
  161. this.ctx.service.advanceAudit.tableName, session.sessionUser.accountId,
  162. this.ctx.service.tenderTourist.tableName, session.sessionUser.accountId,
  163. ];
  164. }
  165. const list = await this.db.query(sql, sqlParam);
  166. for (const l of list) {
  167. l.category = l.category && l.category !== '' ? JSON.parse(l.category) : null;
  168. }
  169. return list;
  170. }
  171. /**
  172. * 获取你所参与的标段的列表 - 完工
  173. *
  174. * @param {String} listStatus - 取列表状态,如果是管理页要传
  175. * @param {String} permission - 根据权限取值
  176. * @param {Number} getAll - 是否取所有标段
  177. * @return {Array} - 返回标段数据
  178. */
  179. async getFinishList(listStatus = '', permission = null, getAll = 0) {
  180. const buildStatusFilter = this.db.format(' AND build_status = ?', [tenderConst.buildStatus.status.finish]);
  181. return await this.getList(listStatus, permission, getAll, buildStatusFilter);
  182. }
  183. /**
  184. * 获取你所参与的标段的列表 - 在建
  185. *
  186. * @param {String} listStatus - 取列表状态,如果是管理页要传
  187. * @param {String} permission - 根据权限取值
  188. * @param {Number} getAll - 是否取所有标段
  189. * @return {Array} - 返回标段数据
  190. */
  191. async getBuildList(listStatus = '', permission = null, getAll = 0) {
  192. const buildStatusFilter = this.db.format(' AND build_status = ?', [tenderConst.buildStatus.status.build]);
  193. return await this.getList(listStatus, permission, getAll, buildStatusFilter);
  194. }
  195. async getList4Select(selectType) {
  196. const accountInfo = await this.ctx.service.projectAccount.getDataById(this.ctx.session.sessionUser.accountId);
  197. const userPermission = accountInfo !== undefined && accountInfo.permission !== '' ? JSON.parse(accountInfo.permission) : null;
  198. const tenderList = await this.ctx.service.tender.getList('', userPermission, this.ctx.session.sessionUser.is_admin);
  199. for (const t of tenderList) {
  200. if (t.ledger_status === auditConst.ledger.status.checked) {
  201. t.lastStage = await this.ctx.service.stage.getLastestStage(t.id, false);
  202. t.completeStage = await this.ctx.service.stage.getLastestCompleteStage(t.id);
  203. }
  204. }
  205. switch (selectType) {
  206. case 'ledger': return tenderList.filter(x => {
  207. return x.ledger_status === auditConst.ledger.status.checked;
  208. });
  209. case 'revise': tenderList.filter(x => {
  210. return x.ledger_status === auditConst.ledger.status.checked;
  211. });
  212. case 'stage': return tenderList.filter(x => {
  213. return x.ledger_status === auditConst.ledger.status.checked && !!x.lastStage;
  214. });
  215. case 'stage-checked': return tenderList.filter(x => {
  216. return !!x.completeStage;
  217. });
  218. default: return tenderList;
  219. }
  220. }
  221. async getTender(id, columns = commonQueryColumns) {
  222. this.initSqlBuilder();
  223. this.sqlBuilder.setAndWhere('id', {
  224. value: id,
  225. operate: '=',
  226. });
  227. this.sqlBuilder.columns = columns;
  228. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  229. const tender = await this.db.queryOne(sql, sqlParam);
  230. if (tender && this._.includes(columns, 'category')) {
  231. tender.category = tender.category && tender.category !== '' ? JSON.parse(tender.category) : null;
  232. }
  233. return tender;
  234. }
  235. /**
  236. * 新增标段
  237. *
  238. * @param {Object} data - 提交的数据
  239. * @return {Boolean} - 返回新增结果
  240. */
  241. async add(data) {
  242. let result = false;
  243. this.transaction = await this.db.beginTransaction();
  244. try {
  245. // 获取当前用户信息
  246. const sessionUser = this.ctx.session.sessionUser;
  247. // 获取当前项目信息
  248. const sessionProject = this.ctx.session.sessionProject;
  249. const insertData = {
  250. name: data.name,
  251. status: tenderConst.status.APPROVAL,
  252. project_id: sessionProject.id,
  253. user_id: sessionUser.accountId,
  254. create_time: new Date(),
  255. category: JSON.stringify(data.category),
  256. valuation: data.valuation,
  257. spid: data.spid,
  258. };
  259. const operate = await this.transaction.insert(this.tableName, insertData);
  260. result = operate.insertId > 0;
  261. if (!result) {
  262. throw '新增标段数据失败';
  263. }
  264. await this.ctx.service.tenderCache.insertTenderCache(this.transaction, operate.insertId, sessionUser.accountId);
  265. if (data.spid) await this.ctx.service.subProject.addRelaTender(this.transaction, data.spid, operate.insertId);
  266. // 获取合同支付模板 并添加到标段
  267. result = await this.ctx.service.pay.addDefaultPayData(operate.insertId, this.transaction);
  268. if (!result) {
  269. throw '新增合同支付数据失败';
  270. }
  271. await this.ctx.service.tenderTag.addTenderTag(operate.insertId, sessionProject.id, this.transaction);
  272. await this.transaction.commit();
  273. 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`,' +
  274. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  275. ' FROM ?? As t ' +
  276. ' Left Join ?? As pa ' +
  277. ' ON t.`user_id` = pa.`id` ' +
  278. ' WHERE t.`id` = ?';
  279. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, operate.insertId];
  280. const tender = await this.db.queryOne(sql, sqlParam);
  281. if (tender) {
  282. tender.category = tender.category && tender.category !== '' ? JSON.parse(tender.category) : null;
  283. }
  284. // 是否加入到决策大屏中
  285. if (sessionProject.page_show.addDataCollect === 0) {
  286. await this.ctx.service.datacollectTender.add(sessionProject.id, operate.insertId);
  287. }
  288. return tender;
  289. } catch (error) {
  290. await this.transaction.rollback();
  291. throw error;
  292. }
  293. }
  294. /**
  295. * 保存标段
  296. *
  297. * @param {Object} postData - 表单post过来的数据
  298. * @param {Number} id - 用于判断修改还是新增的id
  299. * @return {Boolean} - 返回执行结果
  300. */
  301. async save(postData, id = 0) {
  302. id = parseInt(id);
  303. const tender = await this.getDataById(id);
  304. const rowData = {
  305. id,
  306. name: postData.name,
  307. type: postData.type,
  308. category: JSON.stringify(postData.category),
  309. };
  310. const conn = await this.db.beginTransaction();
  311. try {
  312. if (tender.spid !== postData.spid) {
  313. if (postData.spid) await this.ctx.service.subProject.addRelaTender(conn, postData.spid, id);
  314. if (tender.spid) await this.ctx.service.subProject.removeRelaTender(conn, tender.spid, id);
  315. }
  316. rowData.spid = postData.spid || '';
  317. const result = await conn.update(this.tableName, rowData);
  318. await conn.commit();
  319. return result.affectedRows > 0;
  320. } catch(err) {
  321. await conn.rollback();
  322. throw err;
  323. }
  324. }
  325. /**
  326. * 假删除
  327. *
  328. * @param {Number} id - 删除的id
  329. * @return {Boolean} - 删除结果
  330. */
  331. async deleteTenderById(id) {
  332. const updateData = {
  333. status: this.status.DISABLE,
  334. id,
  335. };
  336. const result = await this.db.update(this.tableName, updateData);
  337. return result.affectedRows > 0;
  338. }
  339. /**
  340. * 真删除
  341. * @param {Number} id - 删除的标段id
  342. * @return {Promise<boolean>} - 结果
  343. */
  344. async deleteTenderNoBackup(id) {
  345. const transaction = await this.db.beginTransaction();
  346. try {
  347. const tenderMsg = await this.getDataById(id);
  348. // 先删除附件文件
  349. const attList = await this.ctx.service.changeAtt.getAllDataByCondition({ where: { tid: id } });
  350. const newAttList = await this.ctx.service.materialFile.getAllMaterialFiles(id);
  351. const changeProjectAttList = await this.ctx.service.changeProjectAtt.getAllDataByCondition({ where: { tid: id } });
  352. const changeApplyAttList = await this.ctx.service.changeApplyAtt.getAllDataByCondition({ where: { tid: id } });
  353. const changePlanAttList = await this.ctx.service.changePlanAtt.getAllDataByCondition({ where: { tid: id } });
  354. const advanceAttList = await this.ctx.service.advanceFile.getAllDataByCondition({ where: { tid: id } });
  355. attList.concat(newAttList, changeProjectAttList, changeApplyAttList, changePlanAttList, advanceAttList);
  356. await this.ctx.helper.delFiles(attList);
  357. await transaction.delete(this.tableName, { id });
  358. await transaction.delete(this.ctx.service.tenderInfo.tableName, { tid: id });
  359. await transaction.delete(this.ctx.service.tenderCache.tableName, { id });
  360. await transaction.delete(this.ctx.service.tenderTourist.tableName, { tid: id });
  361. await transaction.delete(this.ctx.service.tenderMap.tableName, { tid: id });
  362. await transaction.delete(this.ctx.service.tenderTag.tableName, { tid: id });
  363. await transaction.delete(this.ctx.service.ledger.departTableName(id), { tender_id: id });
  364. await transaction.delete(this.ctx.service.ledgerAudit.tableName, { tender_id: id });
  365. await transaction.delete(this.ctx.service.pos.departTableName(id), { tid: id });
  366. await transaction.delete(this.ctx.service.pay.tableName, { tid: id });
  367. await transaction.delete(this.ctx.service.stage.tableName, { tid: id });
  368. await transaction.delete(this.ctx.service.stageAudit.tableName, { tid: id });
  369. await transaction.delete(this.ctx.service.stageBills.departTableName(id), { tid: id });
  370. await transaction.delete(this.ctx.service.stagePos.departTableName(id), { tid: id });
  371. await transaction.delete(this.ctx.service.stageBillsDgn.tableName, { tid: id });
  372. await transaction.delete(this.ctx.service.stageBillsFinal.departTableName(id), { tid: id });
  373. await transaction.delete(this.ctx.service.stagePosFinal.departTableName(id), { tid: id });
  374. await transaction.delete(this.ctx.service.stageDetail.tableName, { tid: id });
  375. await transaction.delete(this.ctx.service.stagePay.tableName, { tid: id });
  376. await transaction.delete(this.ctx.service.stageChange.tableName, { tid: id });
  377. await transaction.delete(this.ctx.service.stageAtt.tableName, { tid: id });
  378. await transaction.delete(this.ctx.service.stageJgcl.tableName, { tid: id });
  379. await transaction.delete(this.ctx.service.stageBonus.tableName, { tid: id });
  380. await transaction.delete(this.ctx.service.stageOther.tableName, { tid: id });
  381. await transaction.delete(this.ctx.service.stageRela.tableName, { tid: id });
  382. await transaction.delete(this.ctx.service.stageRelaBills.tableName, { tid: id });
  383. await transaction.delete(this.ctx.service.stageRelaBillsFinal.tableName, { tid: id });
  384. await transaction.delete(this.ctx.service.change.tableName, { tid: id });
  385. await transaction.delete(this.ctx.service.changeAudit.tableName, { tid: id });
  386. await transaction.delete(this.ctx.service.changeAuditList.tableName, { tid: id });
  387. await transaction.delete(this.ctx.service.changeCompany.tableName, { tid: id });
  388. await transaction.delete(this.ctx.service.changeLedger.tableName, { tender_id: id });
  389. await transaction.delete(this.ctx.service.changePos.tableName, { tid: id });
  390. await transaction.delete(this.ctx.service.changeReviseLog.tableName, { tid: id });
  391. await transaction.delete(this.ctx.service.changeProject.tableName, { tid: id });
  392. await transaction.delete(this.ctx.service.changeProjectAudit.tableName, { tid: id });
  393. await transaction.delete(this.ctx.service.changeProjectXsAudit.tableName, { tid: id });
  394. await transaction.delete(this.ctx.service.changeProjectAtt.tableName, { tid: id });
  395. await transaction.delete(this.ctx.service.changeApply.tableName, { tid: id });
  396. await transaction.delete(this.ctx.service.changeApplyAudit.tableName, { tid: id });
  397. await transaction.delete(this.ctx.service.changeApplyList.tableName, { tid: id });
  398. await transaction.delete(this.ctx.service.changeApplyAtt.tableName, { tid: id });
  399. await transaction.delete(this.ctx.service.changePlan.tableName, { tid: id });
  400. await transaction.delete(this.ctx.service.changePlanAudit.tableName, { tid: id });
  401. await transaction.delete(this.ctx.service.changePlanList.tableName, { tid: id });
  402. await transaction.delete(this.ctx.service.changePlanAtt.tableName, { tid: id });
  403. await transaction.delete(this.ctx.service.ledgerRevise.tableName, { tid: id });
  404. await transaction.delete(this.ctx.service.reviseAudit.tableName, { tender_id: id });
  405. await transaction.delete(this.ctx.service.reviseBills.departTableName(id), { tender_id: id });
  406. await transaction.delete(this.ctx.service.revisePos.departTableName(id), { tid: id });
  407. await transaction.delete(this.ctx.service.material.tableName, { tid: id });
  408. await transaction.delete(this.ctx.service.materialAudit.tableName, { tid: id });
  409. await transaction.delete(this.ctx.service.materialBills.tableName, { tid: id });
  410. await transaction.delete(this.ctx.service.materialBillsHistory.tableName, { tid: id });
  411. await transaction.delete(this.ctx.service.materialList.tableName, { tid: id });
  412. await transaction.delete(this.ctx.service.materialListNotjoin.tableName, { tid: id });
  413. await transaction.delete(this.ctx.service.materialExponent.tableName, { tid: id });
  414. await transaction.delete(this.ctx.service.materialExponentHistory.tableName, { tid: id });
  415. await transaction.delete(this.ctx.service.materialListGcl.tableName, { tid: id });
  416. await transaction.delete(this.ctx.service.materialListSelf.tableName, { tid: id });
  417. await transaction.delete(this.ctx.service.materialChecklist.tableName, { tid: id });
  418. await transaction.delete(this.ctx.service.materialFile.tableName, { tid: id });
  419. await transaction.delete(this.ctx.service.signatureUsed.tableName, { tender_id: id });
  420. await transaction.delete(this.ctx.service.signatureRole.tableName, { tender_id: id });
  421. await transaction.delete(this.ctx.service.changeAtt.tableName, { tid: id });
  422. // await transaction.delete(this.ctx.service.materialFile.tableName, { tid: id });
  423. await transaction.delete(this.ctx.service.advanceFile.tableName, { tid: id });
  424. await transaction.delete(this.ctx.service.datacollectTender.tableName, { pid: this.ctx.session.sessionProject.id, tid: id });
  425. await transaction.delete(this.ctx.service.schedule.tableName, { tid: id });
  426. await transaction.delete(this.ctx.service.scheduleAudit.tableName, { tid: id });
  427. await transaction.delete(this.ctx.service.scheduleLedger.tableName, { tid: id });
  428. await transaction.delete(this.ctx.service.scheduleLedgerHistory.tableName, { tid: id });
  429. await transaction.delete(this.ctx.service.scheduleLedgerMonth.tableName, { tid: id });
  430. await transaction.delete(this.ctx.service.scheduleMonth.tableName, { tid: id });
  431. await transaction.delete(this.ctx.service.scheduleStage.tableName, { tid: id });
  432. await transaction.delete(this.ctx.service.shenpiAudit.tableName, { tid: id });
  433. // 记录删除日志
  434. await this.ctx.service.projectLog.addProjectLog(transaction, projectLogConst.type.tender, projectLogConst.status.delete, tenderMsg.name, id);
  435. await transaction.commit();
  436. return true;
  437. } catch (err) {
  438. this.ctx.helper.log(err);
  439. await transaction.rollback();
  440. return false;
  441. }
  442. }
  443. async getCheckTender(tid) {
  444. const tender = await this.ctx.service.tender.getTender(tid);
  445. if (tender.measure_type) tender.info = await this.ctx.service.tenderInfo.getTenderInfo(tid);
  446. return tender;
  447. }
  448. async checkTender(tid) {
  449. if (this.ctx.tender) return;
  450. this.ctx.tender = await this.getCheckTender(tid);
  451. }
  452. async setTenderType(tender, type) {
  453. const templateId = await this.ctx.service.valuation.getValuationTemplate(tender.valuation, type);
  454. if (templateId === -1) throw '该模式下,台账模板不存在';
  455. // 获取标段项目节点模板
  456. const tenderNodeTemplateData = await this.ctx.service.tenderNodeTemplate.getData(templateId);
  457. const conn = await this.db.beginTransaction();
  458. try {
  459. await conn.update(this.tableName, { id: tender.id, measure_type: type });
  460. // 复制模板数据到标段数据表
  461. const result = await this.ctx.service.ledger.innerAdd(tenderNodeTemplateData, tender.id, conn);
  462. if (!result) {
  463. throw '初始化台账失败';
  464. }
  465. await conn.commit();
  466. } catch (err) {
  467. await conn.rollback();
  468. throw err;
  469. }
  470. }
  471. async checkTenderCanFinish(tender) {
  472. // 检查台账、台账修订、预付款、计量期、材差期、变更令状态
  473. if (tender.ledger_status !== auditConst.ledger.status.checked) return false;
  474. const lastRevise = await this.ctx.service.ledgerRevise.getLastestRevise(tender.id, true);
  475. if (lastRevise && lastRevise.status !== auditConst.revise.status.checked) return false;
  476. const advanceOn = await this.db.queryOne(`SELECT * FROM ${this.ctx.service.advance.tableName} WHERE tid = ${tender.id} AND status <> ${auditConst.advance.status.checked}`);
  477. if (advanceOn) return false;
  478. const stageOn = await this.db.queryOne(`SELECT * FROM ${this.ctx.service.stage.tableName} WHERE tid = ${tender.id} AND status <> ${auditConst.stage.status.checked}`);
  479. if (stageOn) return false;
  480. const materialOn = await this.db.queryOne(`SELECT * FROM ${this.ctx.service.material.tableName} WHERE tid = ${tender.id} AND status <> ${auditConst.material.status.checked}`);
  481. if (materialOn) return false;
  482. const changeOn = await this.db.queryOne(`SELECT * FROM ${this.ctx.service.change.tableName} WHERE tid = ${tender.id} AND valid = 1 AND status <> ${auditConst.flow.status.checked}`);
  483. if (changeOn) return false;
  484. const changeApplyOn = await this.db.queryOne(`SELECT * FROM ${this.ctx.service.changeApply.tableName} WHERE tid = ${tender.id} AND status <> ${auditConst.changeApply.status.checked}`);
  485. if (changeApplyOn) return false;
  486. const changeProjectOn = await this.db.queryOne(`SELECT * FROM ${this.ctx.service.changeProject.tableName} WHERE tid = ${tender.id} AND status <> ${auditConst.changeProject.status.checked}`);
  487. if (changeProjectOn) return false;
  488. const changePlanOn = await this.db.queryOne(`SELECT * FROM ${this.ctx.service.changePlan.tableName} WHERE tid = ${tender.id} AND status <> ${auditConst.changePlan.status.checked}`);
  489. if (changePlanOn) return false;
  490. return true;
  491. }
  492. async saveBuildStatus(tender, status) {
  493. const str = tenderConst.buildStatus.statusStr[status];
  494. if (!str) throw '参数错误';
  495. if (status === tenderConst.buildStatus.status.finish) {
  496. const check = await this.checkTenderCanFinish(tender);
  497. if (!check) throw '存在未审批完成的流程,请审批完成后再修改状态';
  498. }
  499. await this.defaultUpdate({ id: tender.id, build_status: status });
  500. }
  501. async saveApiRela(tid, updateData) {
  502. await this.db.update(this.tableName, updateData, {where: { id: tid } });
  503. }
  504. async saveTenderData(tid, updateData) {
  505. return await this.db.update(this.tableName, updateData, { where: { id: tid } });
  506. }
  507. /**
  508. * 获取你所参与的施工标段的列表
  509. *
  510. * @param {String} listStatus - 取列表状态,如果是管理页要传
  511. * @param {String} permission - 根据权限取值
  512. * @param {Number} getAll - 是否取所有标段
  513. * @return {Array} - 返回标段数据
  514. */
  515. async getConstructionList(listStatus = '', permission = null, getAll = 0) {
  516. // 获取当前项目信息
  517. const session = this.ctx.session;
  518. let sql = '';
  519. let sqlParam = [];
  520. if (getAll === 1 || (permission !== null && permission.construction !== undefined && permission.construction.indexOf('1') !== -1)) {
  521. // 具有查看所有标段权限的用户查阅标段
  522. sql = 'SELECT t.`id`, t.`project_id`, t.`name`, t.`status`, t.`category`, t.`user_id`, t.`create_time`,' +
  523. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  524. ' FROM ?? As t ' +
  525. ' Left Join ?? As pa ' +
  526. ' ON t.`user_id` = pa.`id` ' +
  527. ' WHERE t.`project_id` = ? ORDER BY CONVERT(t.`name` USING GBK) ASC';
  528. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id];
  529. } else {
  530. // 根据用户权限查阅标段
  531. sql = 'SELECT t.`id`, t.`project_id`, t.`name`, t.`status`, t.`category`, t.`user_id`, t.`create_time`,' +
  532. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  533. // ' FROM ?? As t, ?? As pa ' +
  534. // ' WHERE t.`project_id` = ? AND t.`user_id` = pa.`id` AND (' +
  535. ' FROM ?? As t ' +
  536. ' Left Join ?? As pa ' +
  537. ' ON t.`user_id` = pa.`id` ' +
  538. ' WHERE t.`project_id` = ? AND ' +
  539. // 参与施工 的标段
  540. ' t.id IN ( SELECT ca.`tid` FROM ?? As ca WHERE ca.`uid` = ?)' +
  541. ' ORDER BY CONVERT(t.`name` USING GBK) ASC';
  542. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id,
  543. this.ctx.service.constructionAudit.tableName, session.sessionUser.accountId,
  544. ];
  545. }
  546. const list = await this.db.query(sql, sqlParam);
  547. for (const l of list) {
  548. l.category = l.category && l.category !== '' ? JSON.parse(l.category) : null;
  549. }
  550. return list;
  551. }
  552. /**
  553. * 获取你所参与的合同标段的列表
  554. *
  555. * @param {String} listStatus - 取列表状态,如果是管理页要传
  556. * @param {String} permission - 根据权限取值
  557. * @param {Number} getAll - 是否取所有标段
  558. * @return {Array} - 返回标段数据
  559. */
  560. async getContractList(listStatus = '', permission = null, getAll = 0) {
  561. // 获取当前项目信息
  562. const session = this.ctx.session;
  563. let sql = '';
  564. let sqlParam = [];
  565. if (getAll === 1) {
  566. // 具有查看所有标段权限的用户查阅标段
  567. sql = 'SELECT t.`id`, t.`project_id`, t.`name`, t.`status`, t.`category`, t.`user_id`, t.`create_time`,' +
  568. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company`, t.`spid` ' +
  569. ' FROM ?? As t ' +
  570. ' Left Join ?? As pa ' +
  571. ' ON t.`user_id` = pa.`id` ' +
  572. ' WHERE t.`project_id` = ? AND t.`spid` != ? ORDER BY CONVERT(t.`name` USING GBK) ASC';
  573. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id, ''];
  574. } else {
  575. // 根据用户权限查阅标段
  576. sql = 'SELECT t.`id`, t.`project_id`, t.`name`, t.`status`, t.`category`, t.`user_id`, t.`create_time`,' +
  577. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company`, t.`spid` ' +
  578. // ' FROM ?? As t, ?? As pa ' +
  579. // ' WHERE t.`project_id` = ? AND t.`user_id` = pa.`id` AND (' +
  580. ' FROM ?? As t ' +
  581. ' Left Join ?? As pa ' +
  582. ' ON t.`user_id` = pa.`id` ' +
  583. ' WHERE t.`project_id` = ? AND ' +
  584. // 参与施工 的标段
  585. ' t.id IN ( SELECT ca.`tid` FROM ?? As ca WHERE ca.`uid` = ?)' +
  586. ' AND t.`spid` != ? ORDER BY CONVERT(t.`name` USING GBK) ASC';
  587. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id,
  588. this.ctx.service.contractAudit.tableName, session.sessionUser.accountId, '',
  589. ];
  590. }
  591. const list = await this.db.query(sql, sqlParam);
  592. for (const l of list) {
  593. l.category = l.category && l.category !== '' ? JSON.parse(l.category) : null;
  594. }
  595. return list;
  596. }
  597. }
  598. return Tender;
  599. };