tender.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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',
  16. 'total_price', 'deal_tp', 'copy_id', 's2b_gxby_check', 's2b_gxby_limit', 's2b_dagl_check', 's2b_dagl_limit', 'has_rela', 'his_id',
  17. ];
  18. module.exports = app => {
  19. class Tender extends app.BaseService {
  20. /**
  21. * 构造函数
  22. *
  23. * @param {Object} ctx - egg全局变量
  24. * @return {void}
  25. */
  26. constructor(ctx) {
  27. super(ctx);
  28. this.tableName = 'tender';
  29. // 状态相关
  30. this.status = {
  31. TRY: 1,
  32. NORMAL: 2,
  33. DISABLE: 3,
  34. };
  35. this.displayStatus = [];
  36. this.displayStatus[this.status.TRY] = '试用';
  37. this.displayStatus[this.status.NORMAL] = '正常';
  38. this.displayStatus[this.status.DISABLE] = '禁用';
  39. this.statusClass = [];
  40. this.statusClass[this.status.TRY] = 'warning';
  41. this.statusClass[this.status.NORMAL] = 'success';
  42. this.statusClass[this.status.DISABLE] = 'danger';
  43. }
  44. /**
  45. * 数据规则
  46. *
  47. * @param {String} scene - 场景
  48. * @return {Object} - 返回数据规则
  49. */
  50. rule(scene) {
  51. let rule = {};
  52. switch (scene) {
  53. case 'add':
  54. rule = {
  55. name: { type: 'string', required: true, min: 2 },
  56. type: { type: 'string', required: true, min: 1 },
  57. };
  58. break;
  59. case 'save':
  60. rule = {
  61. name: { type: 'string', required: true, min: 2 },
  62. type: { type: 'string', required: true, min: 1 },
  63. };
  64. default:
  65. break;
  66. }
  67. return rule;
  68. }
  69. /**
  70. * 获取你所参与的标段的列表
  71. *
  72. * @param {String} listStatus - 取列表状态,如果是管理页要传
  73. * @param {String} permission - 根据权限取值
  74. * @param {Number} getAll - 是否取所有标段
  75. * @return {Array} - 返回标段数据
  76. */
  77. async getList(listStatus = '', permission = null, getAll = 0) {
  78. // 获取当前项目信息
  79. const session = this.ctx.session;
  80. let sql = '';
  81. let sqlParam = [];
  82. if (listStatus === 'manage') {
  83. // 管理页面只取属于自己创建的标段
  84. 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`,' +
  85. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  86. ' FROM ?? As t ' +
  87. ' Left Join ?? As pa ' +
  88. ' ON t.`user_id` = pa.`id` ' +
  89. ' WHERE t.`project_id` = ? AND t.`user_id` = ? ORDER BY CONVERT(t.`name` USING GBK) ASC';
  90. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id, session.sessionUser.accountId];
  91. } else if (getAll === 1 || (permission !== null && permission.tender !== undefined && permission.tender.indexOf('2') !== -1)) {
  92. // 具有查看所有标段权限的用户查阅标段
  93. 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`,' +
  94. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  95. ' FROM ?? As t ' +
  96. ' Left Join ?? As pa ' +
  97. ' ON t.`user_id` = pa.`id` ' +
  98. ' WHERE t.`project_id` = ? ORDER BY CONVERT(t.`name` USING GBK) ASC';
  99. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id];
  100. } else {
  101. // 根据用户权限查阅标段
  102. // tender 163条数据,project_account 68条数据测试
  103. // 查询两张表耗时0.003s,查询tender左连接project_account耗时0.002s
  104. const changeProjectSql = this.ctx.session.sessionProject.page_show.openChangeProject ? ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  105. ' t.id IN ( SELECT cpa.`tid` FROM ' + this.ctx.service.changeProjectAudit.tableName + ' AS cpa WHERE cpa.`aid` = ' + session.sessionUser.accountId + ' GROUP BY cpa.`tid`))' : '';
  106. const changeApplySql = this.ctx.session.sessionProject.page_show.openChangeApply ? ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  107. ' t.id IN ( SELECT caa.`tid` FROM ' + this.ctx.service.changeApplyAudit.tableName + ' AS caa WHERE caa.`aid` = ' + session.sessionUser.accountId + ' GROUP BY caa.`tid`))' : '';
  108. const changePlanSql = this.ctx.session.sessionProject.page_show.openChangePlan ? ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  109. ' t.id IN ( SELECT cpla.`tid` FROM ' + this.ctx.service.changePlanAudit.tableName + ' AS cpla WHERE cpla.`aid` = ' + session.sessionUser.accountId + ' GROUP BY cpla.`tid`))' : '';
  110. // 协审sql
  111. const changeProjectXsSql = this.ctx.session.sessionProject.page_show.openChangeProject ? ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  112. ' t.id IN ( SELECT cpxa.`tid` FROM ' + this.ctx.service.changeProjectXsAudit.tableName + ' AS cpxa WHERE cpxa.`aid` = ' + session.sessionUser.accountId + ' GROUP BY cpxa.`tid`))' : '';
  113. 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`,' +
  114. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  115. // ' FROM ?? As t, ?? As pa ' +
  116. // ' WHERE t.`project_id` = ? AND t.`user_id` = pa.`id` AND (' +
  117. ' FROM ?? As t ' +
  118. ' Left Join ?? As pa ' +
  119. ' ON t.`user_id` = pa.`id` ' +
  120. ' WHERE t.`project_id` = ? AND (' +
  121. // 创建的标段
  122. ' t.`user_id` = ?' +
  123. // 参与审批 台账 的标段
  124. ' OR (t.`ledger_status` != ' + auditConst.ledger.status.uncheck + ' AND ' +
  125. ' t.id IN ( SELECT la.`tender_id` FROM ?? As la WHERE la.`audit_id` = ? GROUP BY la.`tender_id`))' +
  126. // 参与审批 计量期 的标段
  127. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  128. ' t.id IN ( SELECT sa.`tid` FROM ?? As sa WHERE sa.`aid` = ? GROUP BY sa.`tid`))' +
  129. // 参与审批 变更令 的标段
  130. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  131. ' t.id IN ( SELECT ca.`tid` FROM ?? AS ca WHERE ca.`uid` = ? GROUP BY ca.`tid`))' +
  132. // 参与审批 台账修订 的标段
  133. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  134. ' t.id IN ( SELECT ra.`tender_id` FROM ?? AS ra WHERE ra.`audit_id` = ? GROUP BY ra.`tender_id`))' +
  135. // 参与审批 材料调差 的标段
  136. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  137. ' t.id IN ( SELECT ma.`tid` FROM ?? AS ma WHERE ma.`aid` = ? GROUP BY ma.`tid`))' +
  138. // 参与审批 预付款 的标段
  139. ' OR (t.id IN ( SELECT ad.`tid` FROM ?? AS ad WHERE ad.`audit_id` = ? GROUP BY ad.`tid`))' +
  140. // 参与审批 变更立项书及变更申请 的标段
  141. changeProjectSql + changeApplySql + changePlanSql + changeProjectXsSql +
  142. // 游客权限的标段
  143. ' OR (t.id IN ( SELECT tt.`tid` FROM ?? AS tt WHERE tt.`user_id` = ?))' +
  144. // 未参与,但可见的标段
  145. ') ORDER BY CONVERT(t.`name` USING GBK) ASC';
  146. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id, session.sessionUser.accountId,
  147. this.ctx.service.ledgerAudit.tableName, session.sessionUser.accountId,
  148. this.ctx.service.stageAudit.tableName, session.sessionUser.accountId,
  149. this.ctx.service.changeAudit.tableName, session.sessionUser.accountId,
  150. this.ctx.service.reviseAudit.tableName, session.sessionUser.accountId,
  151. this.ctx.service.materialAudit.tableName, session.sessionUser.accountId,
  152. this.ctx.service.advanceAudit.tableName, session.sessionUser.accountId,
  153. this.ctx.service.tenderTourist.tableName, session.sessionUser.accountId,
  154. ];
  155. }
  156. const list = await this.db.query(sql, sqlParam);
  157. for (const l of list) {
  158. l.category = l.category && l.category !== '' ? JSON.parse(l.category) : null;
  159. }
  160. return list;
  161. }
  162. async getList4Select(selectType) {
  163. const accountInfo = await this.ctx.service.projectAccount.getDataById(this.ctx.session.sessionUser.accountId);
  164. const userPermission = accountInfo !== undefined && accountInfo.permission !== '' ? JSON.parse(accountInfo.permission) : null;
  165. const tenderList = await this.ctx.service.tender.getList('', userPermission);
  166. for (const t of tenderList) {
  167. if (t.ledger_status === auditConst.ledger.status.checked) {
  168. t.lastStage = await this.ctx.service.stage.getLastestStage(t.id, false);
  169. t.completeStage = await this.ctx.service.stage.getLastestCompleteStage(t.id);
  170. }
  171. }
  172. switch (selectType) {
  173. case 'ledger': return tenderList.filter(x => {
  174. return x.ledger_status === auditConst.ledger.status.checked;
  175. });
  176. case 'revise': tenderList.filter(x => {
  177. return x.ledger_status === auditConst.ledger.status.checked;
  178. });
  179. case 'stage': return tenderList.filter(x => {
  180. return x.ledger_status === auditConst.ledger.status.checked && !!x.lastStage;
  181. });
  182. case 'stage-checked': return tenderList.filter(x => {
  183. return !!x.completeStage;
  184. });
  185. default: return tenderList;
  186. }
  187. }
  188. async getTender(id) {
  189. this.initSqlBuilder();
  190. this.sqlBuilder.setAndWhere('id', {
  191. value: id,
  192. operate: '=',
  193. });
  194. this.sqlBuilder.columns = commonQueryColumns;
  195. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  196. const tender = await this.db.queryOne(sql, sqlParam);
  197. if (tender) {
  198. tender.category = tender.category && tender.category !== '' ? JSON.parse(tender.category) : null;
  199. }
  200. return tender;
  201. }
  202. /**
  203. * 新增标段
  204. *
  205. * @param {Object} data - 提交的数据
  206. * @return {Boolean} - 返回新增结果
  207. */
  208. async add(data) {
  209. let result = false;
  210. this.transaction = await this.db.beginTransaction();
  211. try {
  212. // 获取当前用户信息
  213. const sessionUser = this.ctx.session.sessionUser;
  214. // 获取当前项目信息
  215. const sessionProject = this.ctx.session.sessionProject;
  216. const insertData = {
  217. name: data.name,
  218. status: tenderConst.status.APPROVAL,
  219. project_id: sessionProject.id,
  220. user_id: sessionUser.accountId,
  221. create_time: new Date(),
  222. category: JSON.stringify(data.category),
  223. valuation: data.valuation,
  224. };
  225. const operate = await this.transaction.insert(this.tableName, insertData);
  226. result = operate.insertId > 0;
  227. if (!result) {
  228. throw '新增标段数据失败';
  229. }
  230. // 获取合同支付模板 并添加到标段
  231. result = await this.ctx.service.pay.addDefaultPayData(operate.insertId, this.transaction);
  232. if (!result) {
  233. throw '新增合同支付数据失败';
  234. }
  235. await this.ctx.service.tenderTag.addTenderTag(operate.insertId, sessionProject.id, this.transaction);
  236. await this.transaction.commit();
  237. 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`,' +
  238. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  239. ' FROM ?? As t ' +
  240. ' Left Join ?? As pa ' +
  241. ' ON t.`user_id` = pa.`id` ' +
  242. ' WHERE t.`id` = ?';
  243. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, operate.insertId];
  244. const tender = await this.db.queryOne(sql, sqlParam);
  245. if (tender) {
  246. tender.category = tender.category && tender.category !== '' ? JSON.parse(tender.category) : null;
  247. }
  248. // 是否加入到决策大屏中
  249. if (sessionProject.page_show.addDataCollect === 0) {
  250. await this.ctx.service.datacollectTender.add(sessionProject.id, operate.insertId);
  251. }
  252. return tender;
  253. } catch (error) {
  254. await this.transaction.rollback();
  255. throw error;
  256. }
  257. }
  258. /**
  259. * 保存标段
  260. *
  261. * @param {Object} postData - 表单post过来的数据
  262. * @param {Number} id - 用于判断修改还是新增的id
  263. * @return {Boolean} - 返回执行结果
  264. */
  265. async save(postData, id = 0) {
  266. id = parseInt(id);
  267. const rowData = {
  268. id,
  269. name: postData.name,
  270. type: postData.type,
  271. category: JSON.stringify(postData.category),
  272. };
  273. const result = await this.db.update(this.tableName, rowData);
  274. return result.affectedRows > 0;
  275. }
  276. /**
  277. * 假删除
  278. *
  279. * @param {Number} id - 删除的id
  280. * @return {Boolean} - 删除结果
  281. */
  282. async deleteTenderById(id) {
  283. const updateData = {
  284. status: this.status.DISABLE,
  285. id,
  286. };
  287. const result = await this.db.update(this.tableName, updateData);
  288. return result.affectedRows > 0;
  289. }
  290. /**
  291. * 真删除
  292. * @param {Number} id - 删除的标段id
  293. * @return {Promise<boolean>} - 结果
  294. */
  295. async deleteTenderNoBackup(id) {
  296. const transaction = await this.db.beginTransaction();
  297. try {
  298. const tenderMsg = await this.getDataById(id);
  299. // 先删除附件文件
  300. const attList = await this.ctx.service.changeAtt.getAllDataByCondition({ where: { tid: id } });
  301. const newAttList = await this.ctx.service.materialFile.getAllMaterialFiles(id);
  302. const changeProjectAttList = await this.ctx.service.changeProjectAtt.getAllDataByCondition({ where: { tid: id } });
  303. const changeApplyAttList = await this.ctx.service.changeApplyAtt.getAllDataByCondition({ where: { tid: id } });
  304. const changePlanAttList = await this.ctx.service.changePlanAtt.getAllDataByCondition({ where: { tid: id } });
  305. const advanceAttList = await this.ctx.service.advanceFile.getAllDataByCondition({ where: { tid: id } });
  306. attList.concat(newAttList, changeProjectAttList, changeApplyAttList, changePlanAttList, advanceAttList);
  307. await this.ctx.helper.delFiles(attList);
  308. await transaction.delete(this.tableName, { id });
  309. await transaction.delete(this.ctx.service.tenderInfo.tableName, { tid: id });
  310. await transaction.delete(this.ctx.service.tenderTourist.tableName, { tid: id });
  311. await transaction.delete(this.ctx.service.tenderMap.tableName, { tid: id });
  312. await transaction.delete(this.ctx.service.tenderTag.tableName, { tid: id });
  313. await transaction.delete(this.ctx.service.ledger.departTableName(id), { tender_id: id });
  314. await transaction.delete(this.ctx.service.ledgerAudit.tableName, { tender_id: id });
  315. await transaction.delete(this.ctx.service.pos.departTableName(id), { tid: id });
  316. await transaction.delete(this.ctx.service.pay.tableName, { tid: id });
  317. await transaction.delete(this.ctx.service.stage.tableName, { tid: id });
  318. await transaction.delete(this.ctx.service.stageAudit.tableName, { tid: id });
  319. await transaction.delete(this.ctx.service.stageBills.departTableName(id), { tid: id });
  320. await transaction.delete(this.ctx.service.stagePos.departTableName(id), { tid: id });
  321. await transaction.delete(this.ctx.service.stageBillsDgn.tableName, { tid: id });
  322. await transaction.delete(this.ctx.service.stageBillsFinal.departTableName(id), { tid: id });
  323. await transaction.delete(this.ctx.service.stagePosFinal.departTableName(id), { tid: id });
  324. await transaction.delete(this.ctx.service.stageDetail.tableName, { tid: id });
  325. await transaction.delete(this.ctx.service.stagePay.tableName, { tid: id });
  326. await transaction.delete(this.ctx.service.stageChange.tableName, { tid: id });
  327. await transaction.delete(this.ctx.service.stageAtt.tableName, { tid: id });
  328. await transaction.delete(this.ctx.service.stageJgcl.tableName, { tid: id });
  329. await transaction.delete(this.ctx.service.stageBonus.tableName, { tid: id });
  330. await transaction.delete(this.ctx.service.stageOther.tableName, { tid: id });
  331. await transaction.delete(this.ctx.service.stageRela.tableName, { tid: id });
  332. await transaction.delete(this.ctx.service.stageRelaBills.tableName, { tid: id });
  333. await transaction.delete(this.ctx.service.stageRelaBillsFinal.tableName, { tid: id });
  334. await transaction.delete(this.ctx.service.change.tableName, { tid: id });
  335. await transaction.delete(this.ctx.service.changeAudit.tableName, { tid: id });
  336. await transaction.delete(this.ctx.service.changeAuditList.tableName, { tid: id });
  337. await transaction.delete(this.ctx.service.changeCompany.tableName, { tid: id });
  338. await transaction.delete(this.ctx.service.changeLedger.tableName, { tender_id: id });
  339. await transaction.delete(this.ctx.service.changePos.tableName, { tid: id });
  340. await transaction.delete(this.ctx.service.changeReviseLog.tableName, { tid: id });
  341. await transaction.delete(this.ctx.service.changeProject.tableName, { tid: id });
  342. await transaction.delete(this.ctx.service.changeProjectAudit.tableName, { tid: id });
  343. await transaction.delete(this.ctx.service.changeProjectXsAudit.tableName, { tid: id });
  344. await transaction.delete(this.ctx.service.changeProjectAtt.tableName, { tid: id });
  345. await transaction.delete(this.ctx.service.changeApply.tableName, { tid: id });
  346. await transaction.delete(this.ctx.service.changeApplyAudit.tableName, { tid: id });
  347. await transaction.delete(this.ctx.service.changeApplyList.tableName, { tid: id });
  348. await transaction.delete(this.ctx.service.changeApplyAtt.tableName, { tid: id });
  349. await transaction.delete(this.ctx.service.changePlan.tableName, { tid: id });
  350. await transaction.delete(this.ctx.service.changePlanAudit.tableName, { tid: id });
  351. await transaction.delete(this.ctx.service.changePlanList.tableName, { tid: id });
  352. await transaction.delete(this.ctx.service.changePlanAtt.tableName, { tid: id });
  353. await transaction.delete(this.ctx.service.ledgerRevise.tableName, { tid: id });
  354. await transaction.delete(this.ctx.service.reviseAudit.tableName, { tender_id: id });
  355. await transaction.delete(this.ctx.service.reviseBills.departTableName(id), { tender_id: id });
  356. await transaction.delete(this.ctx.service.revisePos.departTableName(id), { tid: id });
  357. await transaction.delete(this.ctx.service.material.tableName, { tid: id });
  358. await transaction.delete(this.ctx.service.materialAudit.tableName, { tid: id });
  359. await transaction.delete(this.ctx.service.materialBills.tableName, { tid: id });
  360. await transaction.delete(this.ctx.service.materialBillsHistory.tableName, { tid: id });
  361. await transaction.delete(this.ctx.service.materialList.tableName, { tid: id });
  362. await transaction.delete(this.ctx.service.materialListNotjoin.tableName, { tid: id });
  363. await transaction.delete(this.ctx.service.materialExponent.tableName, { tid: id });
  364. await transaction.delete(this.ctx.service.materialExponentHistory.tableName, { tid: id });
  365. await transaction.delete(this.ctx.service.materialListGcl.tableName, { tid: id });
  366. await transaction.delete(this.ctx.service.materialListSelf.tableName, { tid: id });
  367. await transaction.delete(this.ctx.service.materialChecklist.tableName, { tid: id });
  368. await transaction.delete(this.ctx.service.materialFile.tableName, { tid: id });
  369. await transaction.delete(this.ctx.service.signatureUsed.tableName, { tender_id: id });
  370. await transaction.delete(this.ctx.service.signatureRole.tableName, { tender_id: id });
  371. await transaction.delete(this.ctx.service.changeAtt.tableName, { tid: id });
  372. // await transaction.delete(this.ctx.service.materialFile.tableName, { tid: id });
  373. await transaction.delete(this.ctx.service.advanceFile.tableName, { tid: id });
  374. await transaction.delete(this.ctx.service.datacollectTender.tableName, { pid: this.ctx.session.sessionProject.id, tid: id });
  375. await transaction.delete(this.ctx.service.schedule.tableName, { tid: id });
  376. await transaction.delete(this.ctx.service.scheduleAudit.tableName, { tid: id });
  377. await transaction.delete(this.ctx.service.scheduleLedger.tableName, { tid: id });
  378. await transaction.delete(this.ctx.service.scheduleLedgerHistory.tableName, { tid: id });
  379. await transaction.delete(this.ctx.service.scheduleLedgerMonth.tableName, { tid: id });
  380. await transaction.delete(this.ctx.service.scheduleMonth.tableName, { tid: id });
  381. await transaction.delete(this.ctx.service.scheduleStage.tableName, { tid: id });
  382. await transaction.delete(this.ctx.service.shenpiAudit.tableName, { tid: id });
  383. // 记录删除日志
  384. await this.ctx.service.projectLog.addProjectLog(transaction, projectLogConst.type.tender, projectLogConst.status.delete, tenderMsg.name, id);
  385. await transaction.commit();
  386. return true;
  387. } catch (err) {
  388. this.ctx.helper.log(err);
  389. await transaction.rollback();
  390. return false;
  391. }
  392. }
  393. async getCheckTender(tid) {
  394. const tender = await this.ctx.service.tender.getTender(tid);
  395. if (tender.measure_type) tender.info = await this.ctx.service.tenderInfo.getTenderInfo(tid);
  396. return tender;
  397. }
  398. async checkTender(tid) {
  399. if (this.ctx.tender) return;
  400. this.ctx.tender = await this.getCheckTender(tid);
  401. }
  402. async setTenderType(tender, type) {
  403. const templateId = await this.ctx.service.valuation.getValuationTemplate(tender.valuation, type);
  404. if (templateId === -1) throw '该模式下,台账模板不存在';
  405. // 获取标段项目节点模板
  406. const tenderNodeTemplateData = await this.ctx.service.tenderNodeTemplate.getData(templateId);
  407. const conn = await this.db.beginTransaction();
  408. try {
  409. await conn.update(this.tableName, { id: tender.id, measure_type: type });
  410. // 复制模板数据到标段数据表
  411. const result = await this.ctx.service.ledger.innerAdd(tenderNodeTemplateData, tender.id, conn);
  412. if (!result) {
  413. throw '初始化台账失败';
  414. }
  415. await conn.commit();
  416. } catch (err) {
  417. await conn.rollback();
  418. throw err;
  419. }
  420. }
  421. async saveApiRela(tid, updateData) {
  422. await this.db.update(this.tableName, updateData, {where: { id: tid } });
  423. }
  424. async saveTenderData(tid, updateData) {
  425. return await this.db.update(this.tableName, updateData, { where: { id: tid } });
  426. }
  427. }
  428. return Tender;
  429. };