tender.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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',
  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. // 管理页面只取属于自己创建的标段
  85. 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`,' +
  86. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  87. ' FROM ?? As t ' +
  88. ' Left Join ?? As pa ' +
  89. ' ON t.`user_id` = pa.`id` ' +
  90. ' WHERE t.`project_id` = ? ' + buildStatusFilter + ' AND t.`user_id` = ? ORDER BY CONVERT(t.`name` USING GBK) ASC';
  91. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id, session.sessionUser.accountId];
  92. } else if (getAll === 1 || (permission !== null && permission.tender !== undefined && permission.tender.indexOf('2') !== -1)) {
  93. // 具有查看所有标段权限的用户查阅标段
  94. 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`,' +
  95. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  96. ' FROM ?? As t ' +
  97. ' Left Join ?? As pa ' +
  98. ' ON t.`user_id` = pa.`id` ' +
  99. ' WHERE t.`project_id` = ?' + buildStatusFilter + ' ORDER BY CONVERT(t.`name` USING GBK) ASC';
  100. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id];
  101. } else {
  102. // 根据用户权限查阅标段
  103. // tender 163条数据,project_account 68条数据测试
  104. // 查询两张表耗时0.003s,查询tender左连接project_account耗时0.002s
  105. const changeProjectSql = this.ctx.session.sessionProject.page_show.openChangeProject ? ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  106. ' t.id IN ( SELECT cpa.`tid` FROM ' + this.ctx.service.changeProjectAudit.tableName + ' AS cpa WHERE cpa.`aid` = ' + session.sessionUser.accountId + ' GROUP BY cpa.`tid`))' : '';
  107. const changeApplySql = this.ctx.session.sessionProject.page_show.openChangeApply ? ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  108. ' t.id IN ( SELECT caa.`tid` FROM ' + this.ctx.service.changeApplyAudit.tableName + ' AS caa WHERE caa.`aid` = ' + session.sessionUser.accountId + ' GROUP BY caa.`tid`))' : '';
  109. const changePlanSql = this.ctx.session.sessionProject.page_show.openChangePlan ? ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  110. ' t.id IN ( SELECT cpla.`tid` FROM ' + this.ctx.service.changePlanAudit.tableName + ' AS cpla WHERE cpla.`aid` = ' + session.sessionUser.accountId + ' GROUP BY cpla.`tid`))' : '';
  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` = ? ' + buildStatusFilter + ' 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 sa.`tid` FROM ?? As sa WHERE sa.`ass_user_id` = ? GROUP BY sa.`tid`))' +
  132. // 参与审批 变更令 的标段
  133. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  134. ' t.id IN ( SELECT ca.`tid` FROM ?? AS ca WHERE ca.`uid` = ? GROUP BY ca.`tid`))' +
  135. // 参与审批 台账修订 的标段
  136. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  137. ' t.id IN ( SELECT ra.`tender_id` FROM ?? AS ra WHERE ra.`audit_id` = ? GROUP BY ra.`tender_id`))' +
  138. // 参与审批 材料调差 的标段
  139. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  140. ' t.id IN ( SELECT ma.`tid` FROM ?? AS ma WHERE ma.`aid` = ? GROUP BY ma.`tid`))' +
  141. // 参与审批 预付款 的标段
  142. ' OR (t.id IN ( SELECT ad.`tid` FROM ?? AS ad WHERE ad.`audit_id` = ? GROUP BY ad.`tid`))' +
  143. // 参与审批 变更立项书及变更申请 的标段
  144. changeProjectSql + changeApplySql + changePlanSql + changeProjectXsSql +
  145. // 游客权限的标段
  146. ' OR (t.id IN ( SELECT tt.`tid` FROM ?? AS tt WHERE tt.`user_id` = ?))' +
  147. // 未参与,但可见的标段
  148. ') ORDER BY CONVERT(t.`name` USING GBK) ASC';
  149. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id, session.sessionUser.accountId,
  150. this.ctx.service.ledgerAudit.tableName, session.sessionUser.accountId,
  151. this.ctx.service.stageAudit.tableName, session.sessionUser.accountId,
  152. this.ctx.service.auditAss.tableName, session.sessionUser.accountId,
  153. this.ctx.service.changeAudit.tableName, session.sessionUser.accountId,
  154. this.ctx.service.reviseAudit.tableName, session.sessionUser.accountId,
  155. this.ctx.service.materialAudit.tableName, session.sessionUser.accountId,
  156. this.ctx.service.advanceAudit.tableName, session.sessionUser.accountId,
  157. this.ctx.service.tenderTourist.tableName, session.sessionUser.accountId,
  158. ];
  159. }
  160. const list = await this.db.query(sql, sqlParam);
  161. for (const l of list) {
  162. l.category = l.category && l.category !== '' ? JSON.parse(l.category) : null;
  163. }
  164. return list;
  165. }
  166. /**
  167. * 获取你所参与的标段的列表 - 完工
  168. *
  169. * @param {String} listStatus - 取列表状态,如果是管理页要传
  170. * @param {String} permission - 根据权限取值
  171. * @param {Number} getAll - 是否取所有标段
  172. * @return {Array} - 返回标段数据
  173. */
  174. async getFinishList(listStatus = '', permission = null, getAll = 0) {
  175. const buildStatusFilter = this.db.format(' AND build_status = ?', [tenderConst.buildStatus.status.finish]);
  176. return await this.getList(listStatus, permission, getAll, buildStatusFilter);
  177. }
  178. /**
  179. * 获取你所参与的标段的列表 - 在建
  180. *
  181. * @param {String} listStatus - 取列表状态,如果是管理页要传
  182. * @param {String} permission - 根据权限取值
  183. * @param {Number} getAll - 是否取所有标段
  184. * @return {Array} - 返回标段数据
  185. */
  186. async getBuildList(listStatus = '', permission = null, getAll = 0) {
  187. const buildStatusFilter = this.db.format(' AND build_status = ?', [tenderConst.buildStatus.status.build]);
  188. return await this.getList(listStatus, permission, getAll, buildStatusFilter);
  189. }
  190. async getList4Select(selectType) {
  191. const accountInfo = await this.ctx.service.projectAccount.getDataById(this.ctx.session.sessionUser.accountId);
  192. const userPermission = accountInfo !== undefined && accountInfo.permission !== '' ? JSON.parse(accountInfo.permission) : null;
  193. const tenderList = await this.ctx.service.tender.getList('', userPermission, this.ctx.session.sessionUser.is_admin);
  194. for (const t of tenderList) {
  195. if (t.ledger_status === auditConst.ledger.status.checked) {
  196. t.lastStage = await this.ctx.service.stage.getLastestStage(t.id, false);
  197. t.completeStage = await this.ctx.service.stage.getLastestCompleteStage(t.id);
  198. }
  199. }
  200. switch (selectType) {
  201. case 'ledger': return tenderList.filter(x => {
  202. return x.ledger_status === auditConst.ledger.status.checked;
  203. });
  204. case 'revise': tenderList.filter(x => {
  205. return x.ledger_status === auditConst.ledger.status.checked;
  206. });
  207. case 'stage': return tenderList.filter(x => {
  208. return x.ledger_status === auditConst.ledger.status.checked && !!x.lastStage;
  209. });
  210. case 'stage-checked': return tenderList.filter(x => {
  211. return !!x.completeStage;
  212. });
  213. default: return tenderList;
  214. }
  215. }
  216. async getTender(id, columns = commonQueryColumns) {
  217. this.initSqlBuilder();
  218. this.sqlBuilder.setAndWhere('id', {
  219. value: id,
  220. operate: '=',
  221. });
  222. this.sqlBuilder.columns = columns;
  223. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  224. const tender = await this.db.queryOne(sql, sqlParam);
  225. if (tender && this._.includes(columns, 'category')) {
  226. tender.category = tender.category && tender.category !== '' ? JSON.parse(tender.category) : null;
  227. }
  228. return tender;
  229. }
  230. /**
  231. * 新增标段
  232. *
  233. * @param {Object} data - 提交的数据
  234. * @return {Boolean} - 返回新增结果
  235. */
  236. async add(data) {
  237. let result = false;
  238. this.transaction = await this.db.beginTransaction();
  239. try {
  240. // 获取当前用户信息
  241. const sessionUser = this.ctx.session.sessionUser;
  242. // 获取当前项目信息
  243. const sessionProject = this.ctx.session.sessionProject;
  244. const insertData = {
  245. name: data.name,
  246. status: tenderConst.status.APPROVAL,
  247. project_id: sessionProject.id,
  248. user_id: sessionUser.accountId,
  249. create_time: new Date(),
  250. category: JSON.stringify(data.category),
  251. valuation: data.valuation,
  252. };
  253. const operate = await this.transaction.insert(this.tableName, insertData);
  254. result = operate.insertId > 0;
  255. if (!result) {
  256. throw '新增标段数据失败';
  257. }
  258. await this.ctx.service.tenderCache.insertTenderCache(this.transaction, operate.insertId, sessionUser.accountId);
  259. // 获取合同支付模板 并添加到标段
  260. result = await this.ctx.service.pay.addDefaultPayData(operate.insertId, this.transaction);
  261. if (!result) {
  262. throw '新增合同支付数据失败';
  263. }
  264. await this.ctx.service.tenderTag.addTenderTag(operate.insertId, sessionProject.id, this.transaction);
  265. await this.transaction.commit();
  266. 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`,' +
  267. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  268. ' FROM ?? As t ' +
  269. ' Left Join ?? As pa ' +
  270. ' ON t.`user_id` = pa.`id` ' +
  271. ' WHERE t.`id` = ?';
  272. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, operate.insertId];
  273. const tender = await this.db.queryOne(sql, sqlParam);
  274. if (tender) {
  275. tender.category = tender.category && tender.category !== '' ? JSON.parse(tender.category) : null;
  276. }
  277. // 是否加入到决策大屏中
  278. if (sessionProject.page_show.addDataCollect === 0) {
  279. await this.ctx.service.datacollectTender.add(sessionProject.id, operate.insertId);
  280. }
  281. return tender;
  282. } catch (error) {
  283. await this.transaction.rollback();
  284. throw error;
  285. }
  286. }
  287. /**
  288. * 保存标段
  289. *
  290. * @param {Object} postData - 表单post过来的数据
  291. * @param {Number} id - 用于判断修改还是新增的id
  292. * @return {Boolean} - 返回执行结果
  293. */
  294. async save(postData, id = 0) {
  295. id = parseInt(id);
  296. const rowData = {
  297. id,
  298. name: postData.name,
  299. type: postData.type,
  300. category: JSON.stringify(postData.category),
  301. };
  302. const result = await this.db.update(this.tableName, rowData);
  303. return result.affectedRows > 0;
  304. }
  305. /**
  306. * 假删除
  307. *
  308. * @param {Number} id - 删除的id
  309. * @return {Boolean} - 删除结果
  310. */
  311. async deleteTenderById(id) {
  312. const updateData = {
  313. status: this.status.DISABLE,
  314. id,
  315. };
  316. const result = await this.db.update(this.tableName, updateData);
  317. return result.affectedRows > 0;
  318. }
  319. /**
  320. * 真删除
  321. * @param {Number} id - 删除的标段id
  322. * @return {Promise<boolean>} - 结果
  323. */
  324. async deleteTenderNoBackup(id) {
  325. const transaction = await this.db.beginTransaction();
  326. try {
  327. const tenderMsg = await this.getDataById(id);
  328. // 先删除附件文件
  329. const attList = await this.ctx.service.changeAtt.getAllDataByCondition({ where: { tid: id } });
  330. const newAttList = await this.ctx.service.materialFile.getAllMaterialFiles(id);
  331. const changeProjectAttList = await this.ctx.service.changeProjectAtt.getAllDataByCondition({ where: { tid: id } });
  332. const changeApplyAttList = await this.ctx.service.changeApplyAtt.getAllDataByCondition({ where: { tid: id } });
  333. const changePlanAttList = await this.ctx.service.changePlanAtt.getAllDataByCondition({ where: { tid: id } });
  334. const advanceAttList = await this.ctx.service.advanceFile.getAllDataByCondition({ where: { tid: id } });
  335. attList.concat(newAttList, changeProjectAttList, changeApplyAttList, changePlanAttList, advanceAttList);
  336. await this.ctx.helper.delFiles(attList);
  337. await transaction.delete(this.tableName, { id });
  338. await transaction.delete(this.ctx.service.tenderInfo.tableName, { tid: id });
  339. await transaction.delete(this.ctx.service.tenderCache.tableName, { id });
  340. await transaction.delete(this.ctx.service.tenderTourist.tableName, { tid: id });
  341. await transaction.delete(this.ctx.service.tenderMap.tableName, { tid: id });
  342. await transaction.delete(this.ctx.service.tenderTag.tableName, { tid: id });
  343. await transaction.delete(this.ctx.service.ledger.departTableName(id), { tender_id: id });
  344. await transaction.delete(this.ctx.service.ledgerAudit.tableName, { tender_id: id });
  345. await transaction.delete(this.ctx.service.pos.departTableName(id), { tid: id });
  346. await transaction.delete(this.ctx.service.pay.tableName, { tid: id });
  347. await transaction.delete(this.ctx.service.stage.tableName, { tid: id });
  348. await transaction.delete(this.ctx.service.stageAudit.tableName, { tid: id });
  349. await transaction.delete(this.ctx.service.stageBills.departTableName(id), { tid: id });
  350. await transaction.delete(this.ctx.service.stagePos.departTableName(id), { tid: id });
  351. await transaction.delete(this.ctx.service.stageBillsDgn.tableName, { tid: id });
  352. await transaction.delete(this.ctx.service.stageBillsFinal.departTableName(id), { tid: id });
  353. await transaction.delete(this.ctx.service.stagePosFinal.departTableName(id), { tid: id });
  354. await transaction.delete(this.ctx.service.stageDetail.tableName, { tid: id });
  355. await transaction.delete(this.ctx.service.stagePay.tableName, { tid: id });
  356. await transaction.delete(this.ctx.service.stageChange.tableName, { tid: id });
  357. await transaction.delete(this.ctx.service.stageAtt.tableName, { tid: id });
  358. await transaction.delete(this.ctx.service.stageJgcl.tableName, { tid: id });
  359. await transaction.delete(this.ctx.service.stageBonus.tableName, { tid: id });
  360. await transaction.delete(this.ctx.service.stageOther.tableName, { tid: id });
  361. await transaction.delete(this.ctx.service.stageRela.tableName, { tid: id });
  362. await transaction.delete(this.ctx.service.stageRelaBills.tableName, { tid: id });
  363. await transaction.delete(this.ctx.service.stageRelaBillsFinal.tableName, { tid: id });
  364. await transaction.delete(this.ctx.service.change.tableName, { tid: id });
  365. await transaction.delete(this.ctx.service.changeAudit.tableName, { tid: id });
  366. await transaction.delete(this.ctx.service.changeAuditList.tableName, { tid: id });
  367. await transaction.delete(this.ctx.service.changeCompany.tableName, { tid: id });
  368. await transaction.delete(this.ctx.service.changeLedger.tableName, { tender_id: id });
  369. await transaction.delete(this.ctx.service.changePos.tableName, { tid: id });
  370. await transaction.delete(this.ctx.service.changeReviseLog.tableName, { tid: id });
  371. await transaction.delete(this.ctx.service.changeProject.tableName, { tid: id });
  372. await transaction.delete(this.ctx.service.changeProjectAudit.tableName, { tid: id });
  373. await transaction.delete(this.ctx.service.changeProjectXsAudit.tableName, { tid: id });
  374. await transaction.delete(this.ctx.service.changeProjectAtt.tableName, { tid: id });
  375. await transaction.delete(this.ctx.service.changeApply.tableName, { tid: id });
  376. await transaction.delete(this.ctx.service.changeApplyAudit.tableName, { tid: id });
  377. await transaction.delete(this.ctx.service.changeApplyList.tableName, { tid: id });
  378. await transaction.delete(this.ctx.service.changeApplyAtt.tableName, { tid: id });
  379. await transaction.delete(this.ctx.service.changePlan.tableName, { tid: id });
  380. await transaction.delete(this.ctx.service.changePlanAudit.tableName, { tid: id });
  381. await transaction.delete(this.ctx.service.changePlanList.tableName, { tid: id });
  382. await transaction.delete(this.ctx.service.changePlanAtt.tableName, { tid: id });
  383. await transaction.delete(this.ctx.service.ledgerRevise.tableName, { tid: id });
  384. await transaction.delete(this.ctx.service.reviseAudit.tableName, { tender_id: id });
  385. await transaction.delete(this.ctx.service.reviseBills.departTableName(id), { tender_id: id });
  386. await transaction.delete(this.ctx.service.revisePos.departTableName(id), { tid: id });
  387. await transaction.delete(this.ctx.service.material.tableName, { tid: id });
  388. await transaction.delete(this.ctx.service.materialAudit.tableName, { tid: id });
  389. await transaction.delete(this.ctx.service.materialBills.tableName, { tid: id });
  390. await transaction.delete(this.ctx.service.materialBillsHistory.tableName, { tid: id });
  391. await transaction.delete(this.ctx.service.materialList.tableName, { tid: id });
  392. await transaction.delete(this.ctx.service.materialListNotjoin.tableName, { tid: id });
  393. await transaction.delete(this.ctx.service.materialExponent.tableName, { tid: id });
  394. await transaction.delete(this.ctx.service.materialExponentHistory.tableName, { tid: id });
  395. await transaction.delete(this.ctx.service.materialListGcl.tableName, { tid: id });
  396. await transaction.delete(this.ctx.service.materialListSelf.tableName, { tid: id });
  397. await transaction.delete(this.ctx.service.materialChecklist.tableName, { tid: id });
  398. await transaction.delete(this.ctx.service.materialFile.tableName, { tid: id });
  399. await transaction.delete(this.ctx.service.signatureUsed.tableName, { tender_id: id });
  400. await transaction.delete(this.ctx.service.signatureRole.tableName, { tender_id: id });
  401. await transaction.delete(this.ctx.service.changeAtt.tableName, { tid: id });
  402. // await transaction.delete(this.ctx.service.materialFile.tableName, { tid: id });
  403. await transaction.delete(this.ctx.service.advanceFile.tableName, { tid: id });
  404. await transaction.delete(this.ctx.service.datacollectTender.tableName, { pid: this.ctx.session.sessionProject.id, tid: id });
  405. await transaction.delete(this.ctx.service.schedule.tableName, { tid: id });
  406. await transaction.delete(this.ctx.service.scheduleAudit.tableName, { tid: id });
  407. await transaction.delete(this.ctx.service.scheduleLedger.tableName, { tid: id });
  408. await transaction.delete(this.ctx.service.scheduleLedgerHistory.tableName, { tid: id });
  409. await transaction.delete(this.ctx.service.scheduleLedgerMonth.tableName, { tid: id });
  410. await transaction.delete(this.ctx.service.scheduleMonth.tableName, { tid: id });
  411. await transaction.delete(this.ctx.service.scheduleStage.tableName, { tid: id });
  412. await transaction.delete(this.ctx.service.shenpiAudit.tableName, { tid: id });
  413. // 记录删除日志
  414. await this.ctx.service.projectLog.addProjectLog(transaction, projectLogConst.type.tender, projectLogConst.status.delete, tenderMsg.name, id);
  415. await transaction.commit();
  416. return true;
  417. } catch (err) {
  418. this.ctx.helper.log(err);
  419. await transaction.rollback();
  420. return false;
  421. }
  422. }
  423. async getCheckTender(tid) {
  424. const tender = await this.ctx.service.tender.getTender(tid);
  425. if (tender.measure_type) tender.info = await this.ctx.service.tenderInfo.getTenderInfo(tid);
  426. return tender;
  427. }
  428. async checkTender(tid) {
  429. if (this.ctx.tender) return;
  430. this.ctx.tender = await this.getCheckTender(tid);
  431. }
  432. async setTenderType(tender, type) {
  433. const templateId = await this.ctx.service.valuation.getValuationTemplate(tender.valuation, type);
  434. if (templateId === -1) throw '该模式下,台账模板不存在';
  435. // 获取标段项目节点模板
  436. const tenderNodeTemplateData = await this.ctx.service.tenderNodeTemplate.getData(templateId);
  437. const conn = await this.db.beginTransaction();
  438. try {
  439. await conn.update(this.tableName, { id: tender.id, measure_type: type });
  440. // 复制模板数据到标段数据表
  441. const result = await this.ctx.service.ledger.innerAdd(tenderNodeTemplateData, tender.id, conn);
  442. if (!result) {
  443. throw '初始化台账失败';
  444. }
  445. await conn.commit();
  446. } catch (err) {
  447. await conn.rollback();
  448. throw err;
  449. }
  450. }
  451. async checkTenderCanFinish(tender) {
  452. // 检查台账、台账修订、预付款、计量期、材差期、变更令状态
  453. if (tender.ledger_status !== auditConst.ledger.status.checked) return false;
  454. const lastRevise = await this.ctx.service.ledgerRevise.getLastestRevise(tender.id, true);
  455. if (lastRevise && lastRevise.status !== auditConst.revise.status.checked) return false;
  456. const advanceOn = await this.db.queryOne(`SELECT * FROM ${this.ctx.service.advance.tableName} WHERE tid = ${tender.id} AND status <> ${auditConst.advance.status.checked}`);
  457. if (advanceOn) return false;
  458. const stageOn = await this.db.queryOne(`SELECT * FROM ${this.ctx.service.stage.tableName} WHERE tid = ${tender.id} AND status <> ${auditConst.stage.status.checked}`);
  459. if (stageOn) return false;
  460. const materialOn = await this.db.queryOne(`SELECT * FROM ${this.ctx.service.material.tableName} WHERE tid = ${tender.id} AND status <> ${auditConst.material.status.checked}`);
  461. if (materialOn) return false;
  462. 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}`);
  463. if (changeOn) return false;
  464. const changeApplyOn = await this.db.queryOne(`SELECT * FROM ${this.ctx.service.changeApply.tableName} WHERE tid = ${tender.id} AND status <> ${auditConst.changeApply.status.checked}`);
  465. if (changeApplyOn) return false;
  466. const changeProjectOn = await this.db.queryOne(`SELECT * FROM ${this.ctx.service.changeProject.tableName} WHERE tid = ${tender.id} AND status <> ${auditConst.changeProject.status.checked}`);
  467. if (changeProjectOn) return false;
  468. const changePlanOn = await this.db.queryOne(`SELECT * FROM ${this.ctx.service.changePlan.tableName} WHERE tid = ${tender.id} AND status <> ${auditConst.changePlan.status.checked}`);
  469. if (changePlanOn) return false;
  470. return true;
  471. }
  472. async saveBuildStatus(tender, status) {
  473. const str = tenderConst.buildStatus.statusStr[status];
  474. if (!str) throw '参数错误';
  475. if (status === tenderConst.buildStatus.status.finish) {
  476. const check = await this.checkTenderCanFinish(tender);
  477. if (!check) throw '存在未审批完成的流程,请审批完成后再修改状态';
  478. }
  479. await this.defaultUpdate({ id: tender.id, build_status: status });
  480. }
  481. async saveApiRela(tid, updateData) {
  482. await this.db.update(this.tableName, updateData, {where: { id: tid } });
  483. }
  484. async saveTenderData(tid, updateData) {
  485. return await this.db.update(this.tableName, updateData, { where: { id: tid } });
  486. }
  487. /**
  488. * 获取你所参与的施工标段的列表
  489. *
  490. * @param {String} listStatus - 取列表状态,如果是管理页要传
  491. * @param {String} permission - 根据权限取值
  492. * @param {Number} getAll - 是否取所有标段
  493. * @return {Array} - 返回标段数据
  494. */
  495. async getConstructionList(listStatus = '', permission = null, getAll = 0) {
  496. // 获取当前项目信息
  497. const session = this.ctx.session;
  498. let sql = '';
  499. let sqlParam = [];
  500. if (getAll === 1 || (permission !== null && permission.construction !== undefined && permission.construction.indexOf('1') !== -1)) {
  501. // 具有查看所有标段权限的用户查阅标段
  502. sql = 'SELECT t.`id`, t.`project_id`, t.`name`, t.`status`, t.`category`, t.`user_id`, t.`create_time`,' +
  503. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  504. ' FROM ?? As t ' +
  505. ' Left Join ?? As pa ' +
  506. ' ON t.`user_id` = pa.`id` ' +
  507. ' WHERE t.`project_id` = ? ORDER BY CONVERT(t.`name` USING GBK) ASC';
  508. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id];
  509. } else {
  510. // 根据用户权限查阅标段
  511. sql = 'SELECT t.`id`, t.`project_id`, t.`name`, t.`status`, t.`category`, t.`user_id`, t.`create_time`,' +
  512. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  513. // ' FROM ?? As t, ?? As pa ' +
  514. // ' WHERE t.`project_id` = ? AND t.`user_id` = pa.`id` AND (' +
  515. ' FROM ?? As t ' +
  516. ' Left Join ?? As pa ' +
  517. ' ON t.`user_id` = pa.`id` ' +
  518. ' WHERE t.`project_id` = ? AND ' +
  519. // 参与施工 的标段
  520. ' t.id IN ( SELECT ca.`tid` FROM ?? As ca WHERE ca.`uid` = ?)' +
  521. ' ORDER BY CONVERT(t.`name` USING GBK) ASC';
  522. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id,
  523. this.ctx.service.constructionAudit.tableName, session.sessionUser.accountId,
  524. ];
  525. }
  526. const list = await this.db.query(sql, sqlParam);
  527. for (const l of list) {
  528. l.category = l.category && l.category !== '' ? JSON.parse(l.category) : null;
  529. }
  530. return list;
  531. }
  532. }
  533. return Tender;
  534. };