tender.js 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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',
  17. 's2b_gxby_check', 's2b_gxby_limit', 's2b_dagl_check', 's2b_dagl_limit', 's2b_multi_check', 's2b_multi_limit',
  18. 'has_rela', 'his_id', 'rpt_show_level',
  19. 'build_status', 'settle_order', 'spid',
  20. ];
  21. module.exports = app => {
  22. class Tender extends app.BaseService {
  23. /**
  24. * 构造函数
  25. *
  26. * @param {Object} ctx - egg全局变量
  27. * @return {void}
  28. */
  29. constructor(ctx) {
  30. super(ctx);
  31. this.tableName = 'tender';
  32. // 状态相关
  33. this.status = {
  34. TRY: 1,
  35. NORMAL: 2,
  36. DISABLE: 3,
  37. };
  38. this.displayStatus = [];
  39. this.displayStatus[this.status.TRY] = '试用';
  40. this.displayStatus[this.status.NORMAL] = '正常';
  41. this.displayStatus[this.status.DISABLE] = '禁用';
  42. this.statusClass = [];
  43. this.statusClass[this.status.TRY] = 'warning';
  44. this.statusClass[this.status.NORMAL] = 'success';
  45. this.statusClass[this.status.DISABLE] = 'danger';
  46. }
  47. /**
  48. * 数据规则
  49. *
  50. * @param {String} scene - 场景
  51. * @return {Object} - 返回数据规则
  52. */
  53. rule(scene) {
  54. let rule = {};
  55. switch (scene) {
  56. case 'add':
  57. rule = {
  58. name: { type: 'string', required: true, min: 2 },
  59. type: { type: 'string', required: true, min: 1 },
  60. };
  61. break;
  62. case 'save':
  63. rule = {
  64. name: { type: 'string', required: true, min: 2 },
  65. type: { type: 'string', required: true, min: 1 },
  66. };
  67. default:
  68. break;
  69. }
  70. return rule;
  71. }
  72. /**
  73. * 获取你所参与的标段的列表
  74. *
  75. * @param {String} listStatus - 取列表状态,如果是管理页要传
  76. * @param {String} permission - 根据权限取值
  77. * @param {Number} getAll - 是否取所有标段
  78. * @return {Array} - 返回标段数据
  79. */
  80. async getList(listStatus = '', permission = null, getAll = 0, buildStatusFilter = '', subProject = this.ctx.subProject) {
  81. if (!subProject) return [];
  82. // 获取当前项目信息
  83. const session = this.ctx.session;
  84. let sql = '';
  85. let sqlParam = [];
  86. if (listStatus === 'manage') {
  87. const userFilter = getAll ? '' : this.db.format(' And t.user_id = ? ', [session.sessionUser.accountId]);
  88. // 管理页面只取属于自己创建的标段
  89. 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`, t.filter_budget, t.filter_fund,' +
  90. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  91. ' FROM ?? As t ' +
  92. ' Left Join ?? As pa ' +
  93. ' ON t.`user_id` = pa.`id` ' +
  94. ' WHERE t.`spid` = ? ' + buildStatusFilter + userFilter + ' ORDER BY CONVERT(t.`name` USING GBK) ASC';
  95. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, subProject.id];
  96. } else if (getAll === 1 || (permission !== null && permission.tender !== undefined && permission.tender.indexOf('2') !== -1)) {
  97. // 具有查看所有标段权限的用户查阅标段
  98. 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`, t.filter_budget, t.filter_fund,' +
  99. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  100. ' FROM ?? As t ' +
  101. ' Left Join ?? As pa ' +
  102. ' ON t.`user_id` = pa.`id` ' +
  103. ' WHERE t.`spid` = ?' + buildStatusFilter + ' ORDER BY CONVERT(t.`name` USING GBK) ASC';
  104. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, subProject.id];
  105. } else {
  106. // 根据用户权限查阅标段
  107. // tender 163条数据,project_account 68条数据测试
  108. // 查询两张表耗时0.003s,查询tender左连接project_account耗时0.002s
  109. const changeProjectSql = subProject.page_show.openChangeProject ? ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  110. ' t.id IN ( SELECT cpa.`tid` FROM ' + this.ctx.service.changeProjectAudit.tableName + ' AS cpa WHERE cpa.`aid` = ' + session.sessionUser.accountId + ' GROUP BY cpa.`tid`))' : '';
  111. const changeApplySql = subProject.page_show.openChangeApply ? ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  112. ' t.id IN ( SELECT caa.`tid` FROM ' + this.ctx.service.changeApplyAudit.tableName + ' AS caa WHERE caa.`aid` = ' + session.sessionUser.accountId + ' GROUP BY caa.`tid`))' : '';
  113. const changePlanSql = subProject.page_show.openChangePlan ? ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  114. ' t.id IN ( SELECT cpla.`tid` FROM ' + this.ctx.service.changePlanAudit.tableName + ' AS cpla WHERE cpla.`aid` = ' + session.sessionUser.accountId + ' GROUP BY cpla.`tid`))' : '';
  115. const changeProjectXsSql = subProject.page_show.openChangeProject ? ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  116. ' t.id IN ( SELECT cpxa.`tid` FROM ' + this.ctx.service.changeProjectXsAudit.tableName + ' AS cpxa WHERE cpxa.`aid` = ' + session.sessionUser.accountId + ' GROUP BY cpxa.`tid`))' : '';
  117. const xxjdSql = subProject.page_show.xxjd ? ' OR (t.id IN ( SELECT xxjd.`tid` FROM ' + this.ctx.service.scheduleAudit.tableName + ' AS xxjd WHERE xxjd.`permission` != 0 AND xxjd.`audit_id` = ' + session.sessionUser.accountId + ' GROUP BY xxjd.`tid`))' : '';
  118. 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`,' +
  119. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  120. // ' FROM ?? As t, ?? As pa ' +
  121. // ' WHERE t.`project_id` = ? AND t.`user_id` = pa.`id` AND (' +
  122. ' FROM ?? As t ' +
  123. ' Left Join ?? As pa ' +
  124. ' ON t.`user_id` = pa.`id` ' +
  125. ' WHERE t.`spid` = ? ' + buildStatusFilter + ' AND (' +
  126. // 创建的标段
  127. ' t.`user_id` = ?' +
  128. // 参与审批 台账 的标段
  129. ' OR (t.`ledger_status` != ' + auditConst.ledger.status.uncheck + ' AND ' +
  130. ' t.id IN ( SELECT la.`tender_id` FROM ?? As la WHERE la.`audit_id` = ? GROUP BY la.`tender_id`))' +
  131. // 参与审批 计量期 的标段
  132. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  133. ' t.id IN ( SELECT sa.`tid` FROM ?? As sa WHERE sa.`aid` = ? GROUP BY sa.`tid`))' +
  134. // 参与协审
  135. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  136. ' t.id IN ( SELECT sa.`tid` FROM ?? As sa WHERE sa.`ass_user_id` = ? GROUP BY sa.`tid`))' +
  137. // 参与审批 结算期 的标段
  138. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  139. ' t.id IN ( SELECT sa.`tid` FROM ?? As sa WHERE sa.`audit_id` = ? GROUP BY sa.`tid`))' +
  140. // 参与审批 合同支付期 的标段
  141. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  142. ' t.id IN ( SELECT sa.`tid` FROM ?? As sa WHERE sa.`audit_id` = ? GROUP BY sa.`tid`))' +
  143. // 参与审批 变更令 的标段
  144. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  145. ' t.id IN ( SELECT ca.`tid` FROM ?? AS ca WHERE ca.`uid` = ? GROUP BY ca.`tid`))' +
  146. // 参与审批 台账修订 的标段
  147. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  148. ' t.id IN ( SELECT ra.`tender_id` FROM ?? AS ra WHERE ra.`audit_id` = ? GROUP BY ra.`tender_id`))' +
  149. // 参与审批 材料调差 的标段
  150. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  151. ' t.id IN ( SELECT ma.`tid` FROM ?? AS ma WHERE ma.`aid` = ? GROUP BY ma.`tid`))' +
  152. // 参与审批 预付款 的标段
  153. ' OR (t.id IN ( SELECT ad.`tid` FROM ?? AS ad WHERE ad.`audit_id` = ? GROUP BY ad.`tid`))' +
  154. // 参与审批 变更立项书及变更申请 的标段
  155. changeProjectSql + changeApplySql + changePlanSql + changeProjectXsSql + xxjdSql +
  156. // 游客权限的标段
  157. ' OR (t.id IN ( SELECT tt.`tid` FROM ?? AS tt WHERE tt.`user_id` = ?))' +
  158. // 未参与,但可见的标段
  159. ') ORDER BY CONVERT(t.`name` USING GBK) ASC';
  160. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, subProject.id, session.sessionUser.accountId,
  161. this.ctx.service.ledgerAudit.tableName, session.sessionUser.accountId,
  162. this.ctx.service.stageAudit.tableName, session.sessionUser.accountId,
  163. this.ctx.service.auditAss.tableName, session.sessionUser.accountId,
  164. this.ctx.service.settleAudit.tableName, session.sessionUser.accountId,
  165. this.ctx.service.phasePayAudit.tableName, session.sessionUser.accountId,
  166. this.ctx.service.changeAudit.tableName, session.sessionUser.accountId,
  167. this.ctx.service.reviseAudit.tableName, session.sessionUser.accountId,
  168. this.ctx.service.materialAudit.tableName, session.sessionUser.accountId,
  169. this.ctx.service.advanceAudit.tableName, session.sessionUser.accountId,
  170. this.ctx.service.tenderTourist.tableName, session.sessionUser.accountId,
  171. ];
  172. }
  173. const list = await this.db.query(sql, sqlParam);
  174. for (const l of list) {
  175. l.category = l.category && l.category !== '' ? JSON.parse(l.category) : null;
  176. }
  177. return list;
  178. }
  179. /**
  180. * 获取你所参与的标段的列表 - 完工
  181. *
  182. * @param {String} listStatus - 取列表状态,如果是管理页要传
  183. * @param {String} permission - 根据权限取值
  184. * @param {Number} getAll - 是否取所有标段
  185. * @return {Array} - 返回标段数据
  186. */
  187. async getFinishList(listStatus = '', permission = null, getAll = 0) {
  188. const buildStatusFilter = this.db.format(' AND build_status = ?', [tenderConst.buildStatus.status.finish]);
  189. return await this.getList(listStatus, permission, getAll, buildStatusFilter);
  190. }
  191. /**
  192. * 获取你所参与的标段的列表 - 在建
  193. *
  194. * @param {String} listStatus - 取列表状态,如果是管理页要传
  195. * @param {String} permission - 根据权限取值
  196. * @param {Number} getAll - 是否取所有标段
  197. * @return {Array} - 返回标段数据
  198. */
  199. async getBuildList(listStatus = '', permission = null, getAll = 0) {
  200. const buildStatusFilter = this.db.format(' AND build_status = ?', [tenderConst.buildStatus.status.build]);
  201. return await this.getList(listStatus, permission, getAll, buildStatusFilter);
  202. }
  203. async getList4Select(selectType) {
  204. const accountInfo = await this.ctx.service.projectAccount.getDataById(this.ctx.session.sessionUser.accountId);
  205. const userPermission = accountInfo !== undefined && accountInfo.permission !== '' ? JSON.parse(accountInfo.permission) : null;
  206. const tenderList = await this.ctx.service.tender.getList('', userPermission, this.ctx.session.sessionUser.is_admin);
  207. for (const t of tenderList) {
  208. if (t.ledger_status === auditConst.ledger.status.checked) {
  209. t.lastStage = await this.ctx.service.stage.getLastestStage(t.id, false);
  210. t.completeStage = await this.ctx.service.stage.getLastestCompleteStage(t.id);
  211. }
  212. }
  213. switch (selectType) {
  214. case 'ledger': return tenderList.filter(x => {
  215. return x.ledger_status === auditConst.ledger.status.checked;
  216. });
  217. case 'revise': tenderList.filter(x => {
  218. return x.ledger_status === auditConst.ledger.status.checked;
  219. });
  220. case 'stage': return tenderList.filter(x => {
  221. return x.ledger_status === auditConst.ledger.status.checked && !!x.lastStage;
  222. });
  223. case 'stage-checked': return tenderList.filter(x => {
  224. return !!x.completeStage;
  225. });
  226. default: return tenderList;
  227. }
  228. }
  229. async getTender(id, columns = commonQueryColumns) {
  230. this.initSqlBuilder();
  231. this.sqlBuilder.setAndWhere('id', {
  232. value: id,
  233. operate: '=',
  234. });
  235. this.sqlBuilder.columns = columns;
  236. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  237. const tender = await this.db.queryOne(sql, sqlParam);
  238. if (tender && this._.includes(columns, 'category')) {
  239. tender.category = tender.category && tender.category !== '' ? JSON.parse(tender.category) : null;
  240. }
  241. return tender;
  242. }
  243. async getManageTenderList(projectId) {
  244. return await this.ctx.service.tender.getAllDataByCondition({ where: { project_id: projectId }});
  245. }
  246. /**
  247. * 新增标段
  248. *
  249. * @param {Object} data - 提交的数据
  250. * @return {Boolean} - 返回新增结果
  251. */
  252. async add(data) {
  253. let result = false;
  254. this.transaction = await this.db.beginTransaction();
  255. try {
  256. // 获取当前用户信息
  257. const sessionUser = this.ctx.session.sessionUser;
  258. // 获取当前项目信息
  259. const sessionProject = this.ctx.session.sessionProject;
  260. const insertData = {
  261. name: data.name,
  262. status: tenderConst.status.APPROVAL,
  263. project_id: sessionProject.id,
  264. user_id: sessionUser.accountId,
  265. create_time: new Date(),
  266. category: JSON.stringify(data.category),
  267. valuation: data.valuation,
  268. spid: data.spid,
  269. };
  270. const operate = await this.transaction.insert(this.tableName, insertData);
  271. result = operate.insertId > 0;
  272. if (!result) {
  273. throw '新增标段数据失败';
  274. }
  275. await this.ctx.service.tenderCache.insertTenderCache(this.transaction, operate.insertId, sessionUser.accountId);
  276. if (data.spid) await this.ctx.service.subProject.addRelaTender(this.transaction, data.spid, operate.insertId);
  277. // 获取合同支付模板 并添加到标段
  278. result = await this.ctx.service.pay.addDefaultPayData(operate.insertId, this.transaction);
  279. if (!result) {
  280. throw '新增合同支付数据失败';
  281. }
  282. await this.ctx.service.tenderTag.addTenderTag(operate.insertId, sessionProject.id, this.transaction);
  283. await this.transaction.commit();
  284. 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`, t.`spid`,' +
  285. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  286. ' FROM ?? As t ' +
  287. ' Left Join ?? As pa ' +
  288. ' ON t.`user_id` = pa.`id` ' +
  289. ' WHERE t.`id` = ?';
  290. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, operate.insertId];
  291. const tender = await this.db.queryOne(sql, sqlParam);
  292. if (tender) {
  293. tender.category = tender.category && tender.category !== '' ? JSON.parse(tender.category) : null;
  294. }
  295. // 是否加入到决策大屏中
  296. if (this.ctx.subProject.page_show.addDataCollect === 0) {
  297. await this.ctx.service.datacollectTender.add(sessionProject.id, operate.insertId);
  298. }
  299. return tender;
  300. } catch (error) {
  301. await this.transaction.rollback();
  302. throw error;
  303. }
  304. }
  305. /**
  306. * 保存标段
  307. *
  308. * @param {Object} postData - 表单post过来的数据
  309. * @param {Number} id - 用于判断修改还是新增的id
  310. * @return {Boolean} - 返回执行结果
  311. */
  312. async save(postData, id = 0) {
  313. id = parseInt(id);
  314. const tender = await this.getDataById(id);
  315. const rowData = {
  316. id,
  317. name: postData.name,
  318. type: postData.type,
  319. category: JSON.stringify(postData.category),
  320. };
  321. const conn = await this.db.beginTransaction();
  322. try {
  323. if (tender.spid !== postData.spid) {
  324. if (postData.spid) await this.ctx.service.subProject.addRelaTender(conn, postData.spid, id);
  325. if (tender.spid) await this.ctx.service.subProject.removeRelaTender(conn, tender.spid, id);
  326. const subProject = await this.ctx.service.subProject.getDataById(postData.spid);
  327. await this._addSubProjPermission(conn, subProject, [id]);
  328. }
  329. rowData.spid = postData.spid || '';
  330. const result = await conn.update(this.tableName, rowData);
  331. await conn.commit();
  332. return result.affectedRows > 0;
  333. } catch(err) {
  334. await conn.rollback();
  335. throw err;
  336. }
  337. }
  338. async batchUpdate (data) {
  339. const validFields = ['filter_budget', 'filter_fund'];
  340. const updateData = [];
  341. for (const d of data) {
  342. const ud = { id: d.id };
  343. for (const prop in d) {
  344. if (validFields.indexOf(prop) < 0) continue;
  345. ud[prop] = d[prop];
  346. }
  347. updateData.push(d);
  348. }
  349. await this.db.updateRows(this.tableName, updateData);
  350. return updateData;
  351. }
  352. /**
  353. * 假删除
  354. *
  355. * @param {Number} id - 删除的id
  356. * @return {Boolean} - 删除结果
  357. */
  358. async deleteTenderById(id) {
  359. const updateData = {
  360. status: this.status.DISABLE,
  361. id,
  362. };
  363. const result = await this.db.update(this.tableName, updateData);
  364. return result.affectedRows > 0;
  365. }
  366. /**
  367. * 真删除
  368. * @param {Number} id - 删除的标段id
  369. * @return {Promise<boolean>} - 结果
  370. */
  371. async deleteTenderNoBackup(id) {
  372. const transaction = await this.db.beginTransaction();
  373. try {
  374. const tenderMsg = await this.getDataById(id);
  375. // 先删除附件文件
  376. const attList = await this.ctx.service.changeAtt.getAllDataByCondition({ where: { tid: id } });
  377. const newAttList = await this.ctx.service.materialFile.getAllMaterialFiles(id);
  378. const changeProjectAttList = await this.ctx.service.changeProjectAtt.getAllDataByCondition({ where: { tid: id } });
  379. const changeApplyAttList = await this.ctx.service.changeApplyAtt.getAllDataByCondition({ where: { tid: id } });
  380. const changePlanAttList = await this.ctx.service.changePlanAtt.getAllDataByCondition({ where: { tid: id } });
  381. const advanceAttList = await this.ctx.service.advanceFile.getAllDataByCondition({ where: { tid: id } });
  382. attList.concat(newAttList, changeProjectAttList, changeApplyAttList, changePlanAttList, advanceAttList);
  383. await this.ctx.helper.delFiles(attList);
  384. await transaction.delete(this.tableName, { id });
  385. await transaction.delete(this.ctx.service.tenderInfo.tableName, { tid: id });
  386. await transaction.delete(this.ctx.service.tenderCache.tableName, { id });
  387. await transaction.delete(this.ctx.service.tenderTourist.tableName, { tid: id });
  388. await transaction.delete(this.ctx.service.tenderMap.tableName, { tid: id });
  389. await transaction.delete(this.ctx.service.tenderTag.tableName, { tid: id });
  390. await transaction.delete(this.ctx.service.ledger.departTableName(id), { tender_id: id });
  391. await transaction.delete(this.ctx.service.ledgerAudit.tableName, { tender_id: id });
  392. await transaction.delete(this.ctx.service.pos.departTableName(id), { tid: id });
  393. await transaction.delete(this.ctx.service.pay.tableName, { tid: id });
  394. await transaction.delete(this.ctx.service.stage.tableName, { tid: id });
  395. await transaction.delete(this.ctx.service.stageAudit.tableName, { tid: id });
  396. await transaction.delete(this.ctx.service.stageBills.departTableName(id), { tid: id });
  397. await transaction.delete(this.ctx.service.stagePos.departTableName(id), { tid: id });
  398. await transaction.delete(this.ctx.service.stageBillsDgn.tableName, { tid: id });
  399. await transaction.delete(this.ctx.service.stageBillsFinal.departTableName(id), { tid: id });
  400. await transaction.delete(this.ctx.service.stagePosFinal.departTableName(id), { tid: id });
  401. await transaction.delete(this.ctx.service.stageDetail.tableName, { tid: id });
  402. await transaction.delete(this.ctx.service.stagePay.tableName, { tid: id });
  403. await transaction.delete(this.ctx.service.stageChange.tableName, { tid: id });
  404. await transaction.delete(this.ctx.service.stageAtt.tableName, { tid: id });
  405. await transaction.delete(this.ctx.service.stageJgcl.tableName, { tid: id });
  406. await transaction.delete(this.ctx.service.stageBonus.tableName, { tid: id });
  407. await transaction.delete(this.ctx.service.stageOther.tableName, { tid: id });
  408. await transaction.delete(this.ctx.service.stageRela.tableName, { tid: id });
  409. await transaction.delete(this.ctx.service.stageRelaBills.tableName, { tid: id });
  410. await transaction.delete(this.ctx.service.stageRelaBillsFinal.tableName, { tid: id });
  411. await transaction.delete(this.ctx.service.change.tableName, { tid: id });
  412. await transaction.delete(this.ctx.service.changeAudit.tableName, { tid: id });
  413. await transaction.delete(this.ctx.service.changeAuditList.tableName, { tid: id });
  414. await transaction.delete(this.ctx.service.changeCompany.tableName, { tid: id });
  415. await transaction.delete(this.ctx.service.changeLedger.tableName, { tender_id: id });
  416. await transaction.delete(this.ctx.service.changePos.tableName, { tid: id });
  417. await transaction.delete(this.ctx.service.changeReviseLog.tableName, { tid: id });
  418. await transaction.delete(this.ctx.service.changeProject.tableName, { tid: id });
  419. await transaction.delete(this.ctx.service.changeProjectAudit.tableName, { tid: id });
  420. await transaction.delete(this.ctx.service.changeProjectXsAudit.tableName, { tid: id });
  421. await transaction.delete(this.ctx.service.changeProjectAtt.tableName, { tid: id });
  422. await transaction.delete(this.ctx.service.changeApply.tableName, { tid: id });
  423. await transaction.delete(this.ctx.service.changeApplyAudit.tableName, { tid: id });
  424. await transaction.delete(this.ctx.service.changeApplyList.tableName, { tid: id });
  425. await transaction.delete(this.ctx.service.changeApplyAtt.tableName, { tid: id });
  426. await transaction.delete(this.ctx.service.changePlan.tableName, { tid: id });
  427. await transaction.delete(this.ctx.service.changePlanAudit.tableName, { tid: id });
  428. await transaction.delete(this.ctx.service.changePlanList.tableName, { tid: id });
  429. await transaction.delete(this.ctx.service.changePlanAtt.tableName, { tid: id });
  430. await transaction.delete(this.ctx.service.ledgerRevise.tableName, { tid: id });
  431. await transaction.delete(this.ctx.service.reviseAudit.tableName, { tender_id: id });
  432. await transaction.delete(this.ctx.service.reviseBills.departTableName(id), { tender_id: id });
  433. await transaction.delete(this.ctx.service.revisePos.departTableName(id), { tid: id });
  434. await transaction.delete(this.ctx.service.material.tableName, { tid: id });
  435. await transaction.delete(this.ctx.service.materialAudit.tableName, { tid: id });
  436. await transaction.delete(this.ctx.service.materialBills.tableName, { tid: id });
  437. await transaction.delete(this.ctx.service.materialBillsHistory.tableName, { tid: id });
  438. await transaction.delete(this.ctx.service.materialList.tableName, { tid: id });
  439. await transaction.delete(this.ctx.service.materialListNotjoin.tableName, { tid: id });
  440. await transaction.delete(this.ctx.service.materialExponent.tableName, { tid: id });
  441. await transaction.delete(this.ctx.service.materialExponentHistory.tableName, { tid: id });
  442. await transaction.delete(this.ctx.service.materialListGcl.tableName, { tid: id });
  443. await transaction.delete(this.ctx.service.materialListSelf.tableName, { tid: id });
  444. await transaction.delete(this.ctx.service.materialChecklist.tableName, { tid: id });
  445. await transaction.delete(this.ctx.service.materialFile.tableName, { tid: id });
  446. await transaction.delete(this.ctx.service.signatureUsed.tableName, { tender_id: id });
  447. await transaction.delete(this.ctx.service.signatureRole.tableName, { tender_id: id });
  448. await transaction.delete(this.ctx.service.changeAtt.tableName, { tid: id });
  449. // await transaction.delete(this.ctx.service.materialFile.tableName, { tid: id });
  450. await transaction.delete(this.ctx.service.advanceFile.tableName, { tid: id });
  451. await transaction.delete(this.ctx.service.datacollectTender.tableName, { pid: this.ctx.session.sessionProject.id, tid: id });
  452. await transaction.delete(this.ctx.service.schedule.tableName, { tid: id });
  453. await transaction.delete(this.ctx.service.scheduleAudit.tableName, { tid: id });
  454. await transaction.delete(this.ctx.service.scheduleLedger.tableName, { tid: id });
  455. await transaction.delete(this.ctx.service.scheduleLedgerHistory.tableName, { tid: id });
  456. await transaction.delete(this.ctx.service.scheduleLedgerMonth.tableName, { tid: id });
  457. await transaction.delete(this.ctx.service.scheduleMonth.tableName, { tid: id });
  458. await transaction.delete(this.ctx.service.scheduleStage.tableName, { tid: id });
  459. await transaction.delete(this.ctx.service.shenpiAudit.tableName, { tid: id });
  460. // 记录删除日志
  461. await this.ctx.service.projectLog.addProjectLog(transaction, projectLogConst.type.tender, projectLogConst.status.delete, tenderMsg.name, id);
  462. await transaction.commit();
  463. return true;
  464. } catch (err) {
  465. this.ctx.helper.log(err);
  466. await transaction.rollback();
  467. return false;
  468. }
  469. }
  470. async getCheckTender(tid) {
  471. const tender = await this.ctx.service.tender.getTender(tid);
  472. if (tender.measure_type) tender.info = await this.ctx.service.tenderInfo.getTenderInfo(tid);
  473. return tender;
  474. }
  475. async checkTender(tid) {
  476. if (this.ctx.tender) return this.ctx.tender;
  477. this.ctx.tender = await this.getCheckTender(tid);
  478. return this.ctx.tender;
  479. }
  480. async setTenderType(tender, type) {
  481. const templateId = await this.ctx.service.valuation.getValuationTemplate(tender.valuation, type);
  482. if (templateId === -1) throw '该模式下,台账模板不存在';
  483. // 获取标段项目节点模板
  484. const tenderNodeTemplateData = await this.ctx.service.tenderNodeTemplate.getData(templateId);
  485. const conn = await this.db.beginTransaction();
  486. try {
  487. await conn.update(this.tableName, { id: tender.id, measure_type: type });
  488. // 复制模板数据到标段数据表
  489. const result = await this.ctx.service.ledger.innerAdd(tenderNodeTemplateData, tender.id, conn);
  490. if (!result) {
  491. throw '初始化台账失败';
  492. }
  493. await conn.commit();
  494. } catch (err) {
  495. await conn.rollback();
  496. throw err;
  497. }
  498. }
  499. async checkTenderCanFinish(tender) {
  500. // 检查台账、台账修订、预付款、计量期、材差期、变更令状态
  501. if (tender.ledger_status !== auditConst.ledger.status.checked) return false;
  502. const lastRevise = await this.ctx.service.ledgerRevise.getLastestRevise(tender.id, true);
  503. if (lastRevise && lastRevise.status !== auditConst.revise.status.checked) return false;
  504. const advanceOn = await this.db.queryOne(`SELECT * FROM ${this.ctx.service.advance.tableName} WHERE tid = ${tender.id} AND status <> ${auditConst.advance.status.checked}`);
  505. if (advanceOn) return false;
  506. const stageOn = await this.db.queryOne(`SELECT * FROM ${this.ctx.service.stage.tableName} WHERE tid = ${tender.id} AND status <> ${auditConst.stage.status.checked}`);
  507. if (stageOn) return false;
  508. const materialOn = await this.db.queryOne(`SELECT * FROM ${this.ctx.service.material.tableName} WHERE tid = ${tender.id} AND status <> ${auditConst.material.status.checked}`);
  509. if (materialOn) return false;
  510. 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}`);
  511. if (changeOn) return false;
  512. const changeApplyOn = await this.db.queryOne(`SELECT * FROM ${this.ctx.service.changeApply.tableName} WHERE tid = ${tender.id} AND status <> ${auditConst.changeApply.status.checked}`);
  513. if (changeApplyOn) return false;
  514. const changeProjectOn = await this.db.queryOne(`SELECT * FROM ${this.ctx.service.changeProject.tableName} WHERE tid = ${tender.id} AND status <> ${auditConst.changeProject.status.checked}`);
  515. if (changeProjectOn) return false;
  516. const changePlanOn = await this.db.queryOne(`SELECT * FROM ${this.ctx.service.changePlan.tableName} WHERE tid = ${tender.id} AND status <> ${auditConst.changePlan.status.checked}`);
  517. if (changePlanOn) return false;
  518. return true;
  519. }
  520. async saveBuildStatus(tender, status) {
  521. const str = tenderConst.buildStatus.statusStr[status];
  522. if (!str) throw '参数错误';
  523. if (status === tenderConst.buildStatus.status.finish) {
  524. const check = await this.checkTenderCanFinish(tender);
  525. if (!check) throw '存在未审批完成的流程,请审批完成后再修改状态';
  526. }
  527. await this.defaultUpdate({ id: tender.id, build_status: status });
  528. }
  529. async saveApiRela(tid, updateData) {
  530. await this.db.update(this.tableName, updateData, {where: { id: tid } });
  531. }
  532. async saveTenderData(tid, updateData) {
  533. return await this.db.update(this.tableName, updateData, { where: { id: tid } });
  534. }
  535. // 仅要求授权表用tid/uid授权(条件:存在数据行即授权查看,取消授权直接删除行)
  536. // 如果授权表是牵扯多个模块的授权,则part是模块权限字段
  537. // filter目前仅支持['self', 'all'],可随时增加过滤类型
  538. async getSpecList(specPermissionService, part = '', filter = 'self') {
  539. const partSql = part ? ` AND ${part} <> '' ` : '';
  540. const filterSql = filter === 'all' ? '' : ` AND t.id IN (SELECT tid FROM ${specPermissionService.tableName} WHERE uid = ? ${partSql}) `;
  541. const sql = 'SELECT t.`id`, t.`project_id`, t.`name`, t.`status`, t.`category`, t.`user_id`, t.`create_time`, t.`spid`,' +
  542. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  543. ` FROM ${this.tableName} As t Left Join ${this.ctx.service.projectAccount.tableName} As pa ON t.user_id = pa.id` +
  544. ' WHERE t.`spid` = ? ' + filterSql +
  545. ' ORDER BY CONVERT(t.`name` USING GBK) ASC';
  546. const sqlParam = [this.ctx.subProject.id];
  547. if (filter !== 'all') sqlParam.push(this.ctx.session.sessionUser.accountId);
  548. const list = await this.db.query(sql, sqlParam);
  549. for (const l of list) {
  550. l.category = l.category && l.category !== '' ? JSON.parse(l.category) : null;
  551. }
  552. return list;
  553. }
  554. /**
  555. * 获取你所参与的施工标段的列表
  556. *
  557. * @param {String} listStatus - 取列表状态,如果是管理页要传
  558. * @param {String} permission - 根据权限取值
  559. * @param {Number} getAll - 是否取所有标段
  560. * @return {Array} - 返回标段数据
  561. */
  562. async getConstructionList(listStatus = '', permission = null, getAll = 0) {
  563. // 获取当前项目信息
  564. const session = this.ctx.session;
  565. let sql = '';
  566. let sqlParam = [];
  567. if (getAll === 1 || (permission !== null && permission.construction !== undefined && permission.construction.indexOf('1') !== -1)) {
  568. // 具有查看所有标段权限的用户查阅标段
  569. sql = 'SELECT t.`id`, t.`project_id`, t.`name`, t.`status`, t.`category`, t.`user_id`, t.`create_time`,' +
  570. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  571. ' FROM ?? As t ' +
  572. ' Left Join ?? As pa ' +
  573. ' ON t.`user_id` = pa.`id` ' +
  574. ' WHERE t.`project_id` = ? AND t.`spid` = ? ORDER BY CONVERT(t.`name` USING GBK) ASC';
  575. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id, this.ctx.subProject.id];
  576. } else {
  577. // 根据用户权限查阅标段
  578. sql = 'SELECT t.`id`, t.`project_id`, t.`name`, t.`status`, t.`category`, t.`user_id`, t.`create_time`,' +
  579. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  580. // ' FROM ?? As t, ?? As pa ' +
  581. // ' WHERE t.`project_id` = ? AND t.`user_id` = pa.`id` AND (' +
  582. ' FROM ?? As t ' +
  583. ' Left Join ?? As pa ' +
  584. ' ON t.`user_id` = pa.`id` ' +
  585. ' WHERE t.`project_id` = ? AND t.`spid` = ? AND ' +
  586. // 参与施工 的标段
  587. ' t.id IN ( SELECT ca.`tid` FROM ?? As ca WHERE ca.`uid` = ?)' +
  588. ' ORDER BY CONVERT(t.`name` USING GBK) ASC';
  589. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id, this.ctx.subProject.id,
  590. this.ctx.service.constructionAudit.tableName, session.sessionUser.accountId,
  591. ];
  592. }
  593. const list = await this.db.query(sql, sqlParam);
  594. for (const l of list) {
  595. l.category = l.category && l.category !== '' ? JSON.parse(l.category) : null;
  596. }
  597. return list;
  598. }
  599. /**
  600. * 获取你所参与的合同标段的列表
  601. *
  602. * @param {String} listStatus - 取列表状态,如果是管理页要传
  603. * @param {String} permission - 根据权限取值
  604. * @param {Number} getAll - 是否取所有标段
  605. * @return {Array} - 返回标段数据
  606. */
  607. async getContractList(listStatus = '', permission = null, getAll = 0) {
  608. // 获取当前项目信息
  609. const session = this.ctx.session;
  610. let sql = '';
  611. let sqlParam = [];
  612. if (getAll === 1) {
  613. // 具有查看所有标段权限的用户查阅标段
  614. sql = 'SELECT t.`id`, t.`project_id`, t.`name`, t.`status`, t.`category`, t.`user_id`, t.`create_time`,' +
  615. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company`, t.`spid` ' +
  616. ' FROM ?? As t ' +
  617. ' Left Join ?? As pa ' +
  618. ' ON t.`user_id` = pa.`id` ' +
  619. ' WHERE t.`project_id` = ? AND t.`spid` = ? ORDER BY CONVERT(t.`name` USING GBK) ASC';
  620. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id, this.ctx.subProject.id];
  621. } else {
  622. // 根据用户权限查阅标段
  623. sql = 'SELECT t.`id`, t.`project_id`, t.`name`, t.`status`, t.`category`, t.`user_id`, t.`create_time`,' +
  624. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company`, t.`spid` ' +
  625. // ' FROM ?? As t, ?? As pa ' +
  626. // ' WHERE t.`project_id` = ? AND t.`user_id` = pa.`id` AND (' +
  627. ' FROM ?? As t ' +
  628. ' Left Join ?? As pa ' +
  629. ' ON t.`user_id` = pa.`id` ' +
  630. ' WHERE t.`project_id` = ? AND ' +
  631. // 参与施工 的标段
  632. ' t.id IN ( SELECT ca.`tid` FROM ?? As ca WHERE ca.`uid` = ?)' +
  633. ' AND t.`spid` = ? ORDER BY CONVERT(t.`name` USING GBK) ASC';
  634. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id,
  635. this.ctx.service.contractAudit.tableName, session.sessionUser.accountId, this.ctx.subProject.id,
  636. ];
  637. }
  638. const list = await this.db.query(sql, sqlParam);
  639. for (const l of list) {
  640. l.category = l.category && l.category !== '' ? JSON.parse(l.category) : null;
  641. }
  642. return list;
  643. }
  644. async bindSp(pid, spid, tids) {
  645. const subProject = await this.ctx.service.subProject.getDataById(spid);
  646. const orgRelaTenderId = subProject.rela_tender !== '' ? subProject.rela_tender.split(',') : [];
  647. orgRelaTenderId.push(...tids);
  648. // 更新category
  649. const orgCategoryData = await this.ctx.service.category.getOrgAllCategory(pid);
  650. const newCategoryData = await this.ctx.service.category.getAllCategory(subProject);
  651. const updateTenders = [];
  652. for (const tid of tids) {
  653. const tender = await this.getDataById(tid);
  654. const oneUpdate = {
  655. id: tid,
  656. spid,
  657. };
  658. if (tender.category) {
  659. const newCategory = [];
  660. const orgCategory = JSON.parse(tender.category);
  661. for (const c of orgCategory) {
  662. const orgC = orgCategoryData.find(item => item.id === c.cid);
  663. if (orgC) {
  664. const newC = newCategoryData.find(item => item.name === orgC.name);
  665. if (newC) {
  666. const orgV = orgC.value.find(item => item.id === c.value);
  667. if (orgV) {
  668. const newV = newC.value.find(item => item.value === orgV.value);
  669. if (newV) {
  670. newCategory.push({
  671. cid: newC.id,
  672. value: newV.id,
  673. });
  674. }
  675. }
  676. }
  677. }
  678. }
  679. if (newCategory.length > 0) {
  680. oneUpdate.category = JSON.stringify(newCategory);
  681. }
  682. }
  683. updateTenders.push(oneUpdate);
  684. }
  685. const conn = await this.db.beginTransaction();
  686. try {
  687. await conn.updateRows(this.tableName, updateTenders);
  688. await conn.update(this.ctx.service.subProject.tableName, { id: subProject.id, rela_tender: this._.uniq(orgRelaTenderId).join(',') });
  689. await conn.update(this.ctx.service.budget.tableName, { id: subProject.budget_id, rela_tender: this._.uniq(orgRelaTenderId).join(',') });
  690. await this._addSubProjPermission(conn, subProject, tids);
  691. await conn.commit();
  692. return true;
  693. } catch (error) {
  694. await conn.rollback();
  695. throw error;
  696. }
  697. // return await this.ctx.subProject.setRelaTender({ id: spid, rela_tender: newTids });
  698. }
  699. async _addSubProjPermission(conn, subProject, tids) {
  700. // 需要把所有审批人也迁移至项目下
  701. const subProjPermissionAudits = await this.ctx.service.subProjPermission.getAllDataByCondition({ where: { spid: subProject.id } });
  702. const hadUids = this._.map(subProjPermissionAudits, 'uid');
  703. const newUids = this._.cloneDeep(hadUids) || [];
  704. const tenderAuditsSql = `SELECT user_id FROM ?? WHERE id in (${tids.join(',')}) ${newUids.length > 0 ? ` AND user_id NOT IN (${newUids.join(',')})` : ''} GROUP BY user_id`;
  705. const tenderAuditParams = [this.tableName];
  706. const tenderResult = await conn.query(tenderAuditsSql, tenderAuditParams);
  707. newUids.push(...this._.map(tenderResult, 'user_id'));
  708. const stageAuditsSql = `SELECT aid FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND aid NOT IN (${newUids.join(',')})` : ''} GROUP BY aid`;
  709. const stageAuditParams = [this.ctx.service.stageAudit.tableName];
  710. const stageResult = await conn.query(stageAuditsSql, stageAuditParams);
  711. newUids.push(...this._.map(stageResult, 'aid'));
  712. const ledgerAuditsSql = `SELECT audit_id FROM ?? WHERE tender_id in (${tids.join(',')}) ${newUids.length > 0 ? ` AND audit_id NOT IN (${newUids.join(',')})` : ''} GROUP BY audit_id`;
  713. const ledgerAuditParams = [this.ctx.service.ledgerAudit.tableName];
  714. const ledgerResult = await conn.query(ledgerAuditsSql, ledgerAuditParams);
  715. newUids.push(...this._.map(ledgerResult, 'audit_id'));
  716. const auditAssAuditsSql = `SELECT ass_user_id FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND ass_user_id NOT IN (${newUids.join(',')})` : ''} GROUP BY ass_user_id`;
  717. const auditAssAuditParams = [this.ctx.service.auditAss.tableName];
  718. const auditAssResult = await conn.query(auditAssAuditsSql, auditAssAuditParams);
  719. newUids.push(...this._.map(auditAssResult, 'ass_user_id'));
  720. const settleAuditsSql = `SELECT audit_id FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND audit_id NOT IN (${newUids.join(',')})` : ''} GROUP BY audit_id`;
  721. const settleAuditParams = [this.ctx.service.settleAudit.tableName];
  722. const settleResult = await conn.query(settleAuditsSql, settleAuditParams);
  723. newUids.push(...this._.map(settleResult, 'audit_id'));
  724. const changeAuditsSql = `SELECT uid FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND uid NOT IN (${newUids.join(',')})` : ''} GROUP BY uid`;
  725. const changeAuditParams = [this.ctx.service.changeAudit.tableName];
  726. const changeResult = await conn.query(changeAuditsSql, changeAuditParams);
  727. newUids.push(...this._.map(changeResult, 'uid'));
  728. const changeApplyAuditsSql = `SELECT aid FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND aid NOT IN (${newUids.join(',')})` : ''} GROUP BY aid`;
  729. const changeApplyAuditParams = [this.ctx.service.changeApplyAudit.tableName];
  730. const changeApplyResult = await conn.query(changeApplyAuditsSql, changeApplyAuditParams);
  731. newUids.push(...this._.map(changeApplyResult, 'aid'));
  732. const changeProjectAuditsSql = `SELECT aid FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND aid NOT IN (${newUids.join(',')})` : ''} GROUP BY aid`;
  733. const changeProjectAuditParams = [this.ctx.service.changeProjectAudit.tableName];
  734. const changeProjectResult = await conn.query(changeProjectAuditsSql, changeProjectAuditParams);
  735. newUids.push(...this._.map(changeProjectResult, 'aid'));
  736. const changeProjectXsAuditsSql = `SELECT aid FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND aid NOT IN (${newUids.join(',')})` : ''} GROUP BY aid`;
  737. const changeProjectXsAuditParams = [this.ctx.service.changeProjectXsAudit.tableName];
  738. const changeProjectXsResult = await conn.query(changeProjectXsAuditsSql, changeProjectXsAuditParams);
  739. newUids.push(...this._.map(changeProjectXsResult, 'aid'));
  740. const changePlanAuditsSql = `SELECT aid FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND aid NOT IN (${newUids.join(',')})` : ''} GROUP BY aid`;
  741. const changePlanAuditParams = [this.ctx.service.changePlanAudit.tableName];
  742. const changePlanResult = await conn.query(changePlanAuditsSql, changePlanAuditParams);
  743. newUids.push(...this._.map(changePlanResult, 'aid'));
  744. const reviseAuditsSql = `SELECT audit_id FROM ?? WHERE tender_id in (${tids.join(',')}) ${newUids.length > 0 ? ` AND audit_id NOT IN (${newUids.join(',')})` : ''} GROUP BY audit_id`;
  745. const reviseAuditParams = [this.ctx.service.reviseAudit.tableName];
  746. const reviseResult = await conn.query(reviseAuditsSql, reviseAuditParams);
  747. newUids.push(...this._.map(reviseResult, 'audit_id'));
  748. const materialAuditsSql = `SELECT aid FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND aid NOT IN (${newUids.join(',')})` : ''} GROUP BY aid`;
  749. const materialAuditParams = [this.ctx.service.materialAudit.tableName];
  750. const materialResult = await conn.query(materialAuditsSql, materialAuditParams);
  751. newUids.push(...this._.map(materialResult, 'aid'));
  752. const advanceAuditsSql = `SELECT audit_id FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND audit_id NOT IN (${newUids.join(',')})` : ''} GROUP BY audit_id`;
  753. const advanceAuditParams = [this.ctx.service.advanceAudit.tableName];
  754. const advanceResult = await conn.query(advanceAuditsSql, advanceAuditParams);
  755. newUids.push(...this._.map(advanceResult, 'audit_id'));
  756. const tenderTouristSql = `SELECT user_id FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND user_id NOT IN (${newUids.join(',')})` : ''} GROUP BY user_id`;
  757. const tenderTouristParams = [this.ctx.service.tenderTourist.tableName];
  758. const tenderTouristResult = await conn.query(tenderTouristSql, tenderTouristParams);
  759. newUids.push(...this._.map(tenderTouristResult, 'user_id'));
  760. const xxjdAuditsSql = `SELECT audit_id FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND audit_id NOT IN (${newUids.join(',')})` : ''} GROUP BY audit_id`;
  761. const xxjdAuditParams = [this.ctx.service.scheduleAudit.tableName];
  762. const xxjdResult = await conn.query(xxjdAuditsSql, xxjdAuditParams);
  763. newUids.push(...this._.map(xxjdResult, 'audit_id'));
  764. const diffUids = this._.difference(newUids, hadUids);
  765. if (diffUids.length > 0) {
  766. const insertData = diffUids.map(x => {
  767. return { id: this.uuid.v4(), spid: subProject.id, pid: subProject.project_id, uid: x };
  768. });
  769. await conn.insert(this.ctx.service.subProjPermission.tableName, insertData);
  770. }
  771. }
  772. async getNoSpTenders(pid) {
  773. const list = await this.getAllDataByCondition({ where: { project_id: pid, spid: '' }, orders: [['create_time', 'desc']] });
  774. const accountList = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { project_id: pid } });
  775. for (const l of list) {
  776. const user = accountList.find(item => item.id === l.user_id);
  777. l.user_name = user ? user.name : '';
  778. }
  779. return list;
  780. }
  781. }
  782. return Tender;
  783. };