tender.js 51 KB

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