tender.js 49 KB

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