contract_sp_audit.js 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/8/14
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').contract;
  10. const auditType = require('../const/audit').auditType;
  11. // const pushType = require('../const/audit').pushType;
  12. const shenpiConst = require('../const/sp_shenpi');
  13. const contractConst = require('../const/contract');
  14. module.exports = app => {
  15. class ContractSpAudit extends app.BaseService {
  16. /**
  17. * 构造函数
  18. *
  19. * @param {Object} ctx - egg全局变量
  20. * @return {void}
  21. */
  22. constructor(ctx) {
  23. super(ctx);
  24. this.tableName = 'contract_sp_audit';
  25. }
  26. async checkShenpi(contract) {
  27. const cid = contract.cid ? contract.cid : contract.id;
  28. const cpid = contract.cid ? contract.id : null;
  29. const change_type = contract.cid ? contractConst.typeMap[contract.contract_type] : 'contract';
  30. const tableName = contract.cid ? this.ctx.service.contractPay : this.ctx.service.contract;
  31. const status = auditConst.status;
  32. let shenpi = '';
  33. if (this.ctx.contract_tender) {
  34. const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(this.ctx.contract.id);
  35. shenpi = tenderInfo ? tenderInfo.shenpi : '';
  36. } else {
  37. shenpi = this.ctx.subProject.shenpi;
  38. }
  39. const shenpi_status = shenpi.contract;
  40. if ((contract.status === status.uncheck || contract.status === status.checkNo) && shenpi_status !== shenpiConst.sp_status.sqspr) {
  41. // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审
  42. const auditList = await this.getAllDataByCondition({ where: { cid, cpid, times: contract.times }, orders: [['order', 'asc']] });
  43. // auditList.shift();
  44. if (shenpi_status === shenpiConst.sp_status.gdspl) {
  45. // 判断并获取审批组
  46. const group = await this.ctx.service.shenpiGroup.getSelectGroupByChangeType(this.ctx.contract.id, shenpiConst.sp_type.contract, change_type, contract.sp_group);
  47. if ((group && contract.sp_group !== group.id) || (!group && contract.sp_group !== 0)) {
  48. contract.sp_group = group ? group.id : 0;
  49. await tableName.defaultUpdate({ id: contract.id, sp_group: contract.sp_group });
  50. }
  51. const condition = this._.assign({ sp_type: shenpiConst.sp_type.contract, sp_status: shenpi_status, sp_group: contract.sp_group }, this.ctx.contractOptions);
  52. const shenpiList = await this.ctx.service.shenpiAudit.getAllDataByCondition({ where: condition, orders: [['audit_order', 'asc']] });
  53. if (contract.sp_group !== 0 || shenpiList.length !== 0) {
  54. // const shenpiIdList = _.map(shenpiList, 'audit_id');
  55. // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
  56. let sameAudit = auditList.length === shenpiList.length;
  57. if (sameAudit) {
  58. for (const audit of auditList) {
  59. const shenpi = shenpiList.find(x => { return x.audit_id === audit.aid; });
  60. if (!shenpi || shenpi.audit_order !== audit.audit_order || shenpi.audit_type !== audit.audit_type) {
  61. sameAudit = false;
  62. break;
  63. }
  64. }
  65. }
  66. if (!sameAudit) {
  67. await this.updateNewAuditList(contract, shenpiList);
  68. await this.loadUser(contract);
  69. await this.loadAuditViewData(contract);
  70. }
  71. }
  72. } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
  73. const shenpiInfo = await this.service.shenpiAudit.getDataByCondition(this._.assign({ sp_type: shenpiConst.sp_type.contract, sp_status: shenpi_status }, this.ctx.contractOptions));
  74. // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
  75. const lastAuditors = auditList.filter(x => { return x.audit_order === auditList.length - 1; });
  76. if (shenpiInfo && (lastAuditors.length === 0 || (lastAuditors.length > 1 || shenpiInfo.audit_id !== lastAuditors[0].aid))) {
  77. await this.updateLastAudit(contract, auditList, shenpiInfo.audit_id);
  78. } else if (!shenpiInfo) {
  79. // 不存在终审人的状态下这里恢复为授权审批人
  80. shenpi.contract = shenpiConst.sp_status.sqspr;
  81. }
  82. }
  83. }
  84. }
  85. async loadUser(contract) {
  86. const status = auditConst.status;
  87. const accountId = this.ctx.session.sessionUser.accountId;
  88. contract.user = await this.ctx.service.projectAccount.getAccountInfoById(contract.uid);
  89. contract.auditors = await this.getAuditors(contract.cid ? contract.cid : contract.id, contract.cid ? contract.id : null, contract.times); // 全部参与的审批人
  90. contract.auditorIds = this._.map(contract.auditors, 'aid');
  91. contract.curAuditors = contract.auditors.filter(x => { return x.status === status.checking; }); // 当前流程中审批中的审批人
  92. contract.curAuditorIds = this._.map(contract.curAuditors, 'aid');
  93. contract.flowAuditors = contract.curAuditors.length > 0 ? contract.auditors.filter(x => { return x.order === contract.curAuditors[0].order; }) : []; // 当前流程中参与的审批人(包含会签时,审批通过的人)
  94. contract.flowAuditorIds = this._.map(contract.flowAuditors, 'aid');
  95. contract.nextAuditors = contract.curAuditors.length > 0 ? contract.auditors.filter(x => { return x.order === contract.curAuditors[0].order + 1; }) : [];
  96. contract.nextAuditorIds = this._.map(contract.nextAuditors, 'aid');
  97. contract.auditorGroups = this.ctx.helper.groupAuditors(contract.auditors);
  98. contract.userGroups = this.ctx.helper.groupAuditorsUniq(contract.auditorGroups);
  99. contract.finalAuditorIds = this._.map(contract.userGroups[contract.userGroups.length - 1], 'aid');
  100. }
  101. async loadAuditViewData(contract) {
  102. const times = contract.status === auditConst.status.checkNo ? contract.times - 1 : contract.times;
  103. if (!contract.user) contract.user = await this.ctx.service.projectAccount.getAccountInfoById(contract.uid);
  104. contract.auditHistory = await this.getAuditorHistory(contract.cid ? contract.cid : contract.id, contract.cid ? contract.id : null, times);
  105. // 获取审批流程中左边列表
  106. // if ((contract.status === auditConst.status.checkNo) && !(contract.uid === this.ctx.session.sessionUser.accountId || this.ctx.contract_audit_permission.permission_edit_contract || this.ctx.session.sessionUser.is_admin)) {
  107. const auditors = await this.getAuditors(contract.cid ? contract.cid : contract.id, contract.cid ? contract.id : null, times); // 全部参与的审批人
  108. const auditorGroups = this.ctx.helper.groupAuditors(auditors);
  109. contract.auditors2 = this.ctx.helper.groupAuditorsUniq(auditorGroups);
  110. // } else {
  111. // contract.auditors2 = contract.userGroups;
  112. // }
  113. if (contract.status === auditConst.status.uncheck || contract.status === auditConst.status.checkNo) {
  114. contract.auditorList = await this.getAuditors(contract.cid ? contract.cid : contract.id, contract.cid ? contract.id : null, contract.times);
  115. }
  116. }
  117. /**
  118. * 获取 审核列表信息
  119. *
  120. * @param {Number} cpId - 变更立项id
  121. * @param {Number} times - 第几次审批
  122. * @return {Promise<*>}
  123. */
  124. async getAuditors(cid, cpid = null, times = 1, order_sort = 'asc', noYB = false) {
  125. // const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`, g.`sort` ' +
  126. // 'FROM ?? AS la, ?? AS pa, (SELECT t1.`aid`,(@i:=@i+1) as `sort` FROM (SELECT t.`aid`, t.`order` FROM (select `aid`, `order` from ?? WHERE `cpid` = ? AND `times` = ? ORDER BY `order` LIMIT 200) t GROUP BY t.`aid` ORDER BY t.`order`) t1, (select @i:=0) as it) as g ' +
  127. // 'WHERE la.`cpid` = ? and la.`times` = ? and la.`aid` = pa.`id` and g.`aid` = la.`aid` order by la.`order`';
  128. // const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.tableName, cpId, times, cpId, times];
  129. // const result = await this.db.query(sql, sqlParam);
  130. // const sql2 = 'SELECT COUNT(a.`aid`) as num FROM (SELECT `aid` FROM ?? WHERE `cpid` = ? AND `times` = ? GROUP BY `aid`) as a';
  131. // const sqlParam2 = [this.tableName, cpId, times];
  132. // const count = await this.db.queryOne(sql2, sqlParam2);
  133. // for (const i in result) {
  134. // result[i].max_sort = count.num;
  135. // }
  136. const ybSql = noYB ? ' AND la.audit_order != 0' : '';
  137. const cpidSql = cpid ? ' AND la.cpid = ' + cpid : ' AND la.cpid is null';
  138. const sql = 'SELECT la.id, la.aid, la.times, la.order, la.status, la.opinion, la.begin_time, la.end_time, la.audit_type, la.audit_order,' +
  139. ' pa.name, pa.company, pa.role, pa.mobile, pa.telephone' +
  140. ` FROM ${this.tableName} la LEFT JOIN ${this.ctx.service.projectAccount.tableName} pa ON la.aid = pa.id` +
  141. ' WHERE la.cid = ?' + cpidSql + ' AND la.times = ?' + ybSql +
  142. ' ORDER BY la.order ' + order_sort;
  143. const sqlParam = [cid, times];
  144. const result = await this.db.query(sql, sqlParam);
  145. const max_sort = this._.max(result.map(x => { return x.audit_order; }));
  146. for (const i in result) {
  147. result[i].max_sort = max_sort;
  148. }
  149. return result;
  150. }
  151. /**
  152. * 获取 当前审核人
  153. *
  154. * @param {Number} cpId - 变更立项id
  155. * @param {Number} times - 第几次审批
  156. * @return {Promise<*>}
  157. */
  158. async getCurAuditor(cid, cpid = null, times = 1) {
  159. const cpidSql = cpid ? ' AND la.cpid = ' + cpid : ' AND la.cpid is null';
  160. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  161. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  162. ' WHERE la.`cid` = ?' + cpidSql + ' and la.`status` = ? and la.`times` = ? and la.`audit_order` != 0';
  163. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cid, auditConst.status.checking, times];
  164. return await this.db.queryOne(sql, sqlParam);
  165. }
  166. async getCurAuditors(cid, cpid = null, times = 1) {
  167. const cpidSql = cpid ? ' AND la.cpid = ' + cpid : ' AND la.cpid is null';
  168. const sql =
  169. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`, la.audit_type, la.audit_order ' +
  170. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  171. ' WHERE la.`cid` = ?' + cpidSql + ' and la.`status` = ? and la.`times` = ? and la.`audit_order` != 0';
  172. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cid, auditConst.status.checking, times];
  173. return await this.db.query(sql, sqlParam);
  174. }
  175. /**
  176. * 获取审核人流程列表
  177. *
  178. * @param auditorId
  179. * @return {Promise<*>}
  180. */
  181. // async getAuditGroupByList(cid, cpid = null, times, noYB = true, transaction = false) {
  182. // const cpidSql = cpid ? ' AND la.cpid = ' + cpid : ' AND la.cpid is null';
  183. // const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cid`, la.`cpid`, la.`aid`, la.`order`, la.`status`, la.audit_type, la.audit_order ' +
  184. // ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  185. // ' WHERE la.`cid` = ?' + cpidSql + ' and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
  186. // const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cid, times];
  187. // return transaction !== false ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
  188. // }
  189. /**
  190. * 获取审核人流程列表(除去原报)
  191. *
  192. * @param auditorId
  193. * @return {Promise<*>}
  194. */
  195. async getAuditGroupByList(cid, cpid = null, times, transaction = false) {
  196. // const sql =
  197. // 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order`, la.`status`' +
  198. // ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  199. // ' WHERE la.`sid` = ? and la.`times` = ? and la.`is_old` = 0 GROUP BY la.`aid` ORDER BY la.`order`';
  200. // const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  201. const cpidSql = cpid ? ' AND la.`cpid` = ' + cpid : ' AND la.`cpid` is null';
  202. const sql =
  203. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cid`, la.`cpid`, la.`aid`, la.`order`, la.`status`, la.audit_type, la.audit_order ' +
  204. ' FROM (SELECT `aid`, max(`order`) `order` FROM ?? WHERE `cid` = ? and `cpid` ' + (cpid ? '= ' + cpid : 'is null') + ' and `times` = ? and `audit_order` != 0 GROUP BY aid) sa' +
  205. ' LEFT JOIN ?? la ON sa.`aid` = la.`aid` AND sa.`order` = la.`order`' +
  206. ' Left JOIN ?? AS pa On la.`aid` = pa.`id` WHERE la.`cid` = ?' + cpidSql + ' and la.`times` = ? and la.`audit_order` != 0 order BY la.`order`';
  207. const sqlParam = [this.tableName, cid, times, this.tableName, this.ctx.service.projectAccount.tableName, cid, times];
  208. return transaction !== false ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
  209. }
  210. /**
  211. * 移除审核人
  212. *
  213. * @param {Number} materialId - 材料调差期id
  214. * @param {Number} status - 期状态
  215. * @param {Number} status - 期次数
  216. * @return {Promise<boolean>}
  217. */
  218. async getAuditorByStatus(cid, cpid = null, status, times = 1) {
  219. const cpidSql = cpid ? ' AND la.cpid = ' + cpid : ' AND la.cpid is null';
  220. let auditor = null;
  221. let sql = '';
  222. let sqlParam = '';
  223. switch (status) {
  224. case auditConst.status.checking :
  225. case auditConst.status.checked :
  226. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cid`, la.`cpid`, la.`aid`, la.`order`, la.`status` ' +
  227. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  228. ' WHERE la.`cid` = ?' + cpidSql + ' and la.`status` = ? ' +
  229. ' ORDER BY la.`times` desc, la.`order` desc';
  230. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cid, status];
  231. auditor = await this.db.queryOne(sql, sqlParam);
  232. break;
  233. case auditConst.status.checkNo :
  234. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cid`, la.`cpid`, la.`aid`, la.`order`, la.`status` ' +
  235. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  236. ' WHERE la.`cid` = ?' + cpidSql + ' and la.`status` = ? and la.`times` = ?' +
  237. ' ORDER BY la.`times` desc, la.`order` desc';
  238. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cid, auditConst.status.checkNo, parseInt(times) - 1];
  239. auditor = await this.db.queryOne(sql, sqlParam);
  240. break;
  241. case auditConst.status.uncheck :
  242. break;
  243. default:break;
  244. }
  245. return auditor;
  246. }
  247. async getAuditorsByStatus(cid, cpid = null, status, times = 1) {
  248. const cpidSql = cpid ? ' AND la.cpid = ' + cpid : ' AND la.cpid is null';
  249. let auditor = [];
  250. let sql = '';
  251. let sqlParam = '';
  252. let cur;
  253. switch (status) {
  254. case auditConst.status.checking :
  255. case auditConst.status.checked :
  256. case auditConst.status.checkNoPre:
  257. cur = await this.db.queryOne('SELECT * From ?? where cid = ? AND cpid ' + (cpid ? '= ' + cpid : 'is null') + ' AND times = ? AND status = ? AND audit_order != 0 ORDER By times DESC, `order` DESC', [this.tableName, cid, times, status]);
  258. if (!cur) return [];
  259. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cid`, la.`cpid`, la.`order`, la.`status`, la.`audit_order`, la.`audit_type` ' +
  260. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  261. ' WHERE la.`cid` = ?' + cpidSql + ' and la.`order` = ? and times = ?';
  262. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cid, cur.order, times];
  263. auditor = await this.db.query(sql, sqlParam);
  264. break;
  265. case auditConst.status.checkNo :
  266. cur = await this.db.queryOne('SELECT * From ?? where cid = ? AND cpid ' + (cpid ? '= ' + cpid : 'is null') + ' AND times = ? AND status = ? AND audit_order != 0 ORDER By times DESC, `order` DESC', [this.tableName, cid, parseInt(times) - 1, status]);
  267. if (!cur) return [];
  268. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cid`, la.`cpid`, la.`order`, la.`status`, la.`audit_order`, la.`audit_type` ' +
  269. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  270. ' WHERE la.`cid` = ?' + cpidSql + ' and la.`order` = ? and la.`times` = ?';
  271. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cid, cur.order, parseInt(times) - 1];
  272. auditor = await this.db.query(sql, sqlParam);
  273. break;
  274. case auditConst.status.uncheck:
  275. default:
  276. break;
  277. }
  278. return auditor;
  279. }
  280. async getLastAudit(cid, cpid = null, times, transaction = null) {
  281. const cpidSql = cpid ? ' AND cpid = ' + cpid : ' AND cpid is null';
  282. const sql = 'SELECT * FROM ?? WHERE `cid` = ?' + cpidSql + ' AND `times` = ? ORDER BY `order` DESC';
  283. const sqlParam = [this.tableName, cid, times];
  284. return transaction ? await transaction.queryOne(sql, sqlParam) : await this.db.queryOne(sql, sqlParam);
  285. }
  286. /**
  287. * 获取审核人流程列表(包括原报)
  288. * @param {Number} materialId 调差id
  289. * @param {Number} times 审核次数
  290. * @return {Promise<Array>} 查询结果集(包括原报)
  291. */
  292. async getAuditorsWithOwner(cid, cpid = null, times = 1) {
  293. // const cpidSql = cpid ? ' AND la.cpid = ' + cpid : ' AND la.cpid is null';
  294. const result = await this.getAuditGroupByList(cid, cpid, times);
  295. // const sql =
  296. // 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As cpid, 0 As `order`' +
  297. // ' FROM ' +
  298. // this.ctx.service.changePlan.tableName +
  299. // ' As s' +
  300. // ' LEFT JOIN ' +
  301. // this.ctx.service.projectAccount.tableName +
  302. // ' As pa' +
  303. // ' ON s.uid = pa.id' +
  304. // ' WHERE s.id = ?';
  305. // const sqlParam = [times, cid, cid];
  306. // const user = await this.db.queryOne(sql, sqlParam);
  307. // result.unshift(user);
  308. return result;
  309. }
  310. /**
  311. * 新增审核人
  312. *
  313. * @param {Number} cpId - 方案id
  314. * @param {Number} auditorId - 审核人id
  315. * @param {Number} times - 第几次审批
  316. * @return {Promise<number>}
  317. */
  318. async addAuditor(options, cid, cpid = null, auditorId, times = 1, is_gdzs = 0) {
  319. const transaction = await this.db.beginTransaction();
  320. let flag = false;
  321. try {
  322. let [newOrder, newAuditOrder] = await this.getNewOrder(cid, cpid, times);
  323. // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
  324. newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
  325. newAuditOrder = is_gdzs === 1 ? newAuditOrder - 1 : newAuditOrder;
  326. if (is_gdzs) await this._syncOrderByDelete(transaction, cid, cpid, newOrder, times, '+');
  327. const data = {
  328. spid: options.spid || null,
  329. tid: options.tid || null,
  330. cid,
  331. cpid,
  332. aid: auditorId,
  333. times,
  334. order: newOrder,
  335. status: auditConst.status.uncheck,
  336. audit_order: newAuditOrder,
  337. };
  338. const result = await transaction.insert(this.tableName, data);
  339. await this.ctx.service.contractAudit.saveAudits(options, [{ id: auditorId }], transaction);
  340. await transaction.commit();
  341. flag = result.effectRows = 1;
  342. } catch (err) {
  343. await transaction.rollback();
  344. throw err;
  345. }
  346. return flag;
  347. }
  348. /**
  349. * 获取 最新审核顺序
  350. *
  351. * @param {Number} cpId - 方案id
  352. * @param {Number} times - 第几次审批
  353. * @return {Promise<number>}
  354. */
  355. async getNewOrder(cid, cpid = null, times = 1) {
  356. const cpidSql = cpid ? ' AND cpid = ' + cpid : ' AND cpid is null';
  357. const sql = 'SELECT Max(`order`) As max_order, Max(audit_order) As max_audit_order FROM ?? Where `cid` = ?' + cpidSql + ' and `times` = ?';
  358. const sqlParam = [this.tableName, cid, times];
  359. const result = await this.db.queryOne(sql, sqlParam);
  360. return result && result.max_order ? [result.max_order + 1, result.max_audit_order + 1] : [1, 1];
  361. }
  362. /**
  363. * 移除审核人
  364. *
  365. * @param {Number} cpId - 变更方案id
  366. * @param {Number} auditorId - 审核人id
  367. * @param {Number} times - 第几次审批
  368. * @return {Promise<boolean>}
  369. */
  370. async deleteAuditor(cid, cpid = null, auditorId, times = 1) {
  371. const transaction = await this.db.beginTransaction();
  372. try {
  373. const cpidSql = cpid ? ' AND cpid = ' + cpid : ' AND cpid is null';
  374. const sql = 'SELECT * FROM ?? WHERE `cid` = ?' + cpidSql + ' and `times` = ? and `aid` = ? and `audit_order` != 0 ORDER BY `order` DESC';
  375. const sqlParam = [this.tableName, cid, times, auditorId];
  376. const auditor = await transaction.queryOne(sql, sqlParam);
  377. // const condition = { cid, cpid, aid: auditorId, times };
  378. // const auditor = await this.getDataByCondition(condition);
  379. if (!auditor) {
  380. throw '该审核人不存在';
  381. }
  382. await transaction.delete(this.tableName, { cid, cpid, order: auditor.order, times });
  383. await this._syncOrderByDelete(transaction, cid, cpid, auditor.order, times);
  384. await transaction.commit();
  385. } catch (err) {
  386. await transaction.rollback();
  387. throw err;
  388. }
  389. return true;
  390. }
  391. /**
  392. * 移除审核人时,同步其后审核人order
  393. * @param transaction - 事务
  394. * @param {Number} cpId - 变更方案id
  395. * @param {Number} auditorId - 审核人id
  396. * @param {Number} times - 第几次审批
  397. * @return {Promise<*>}
  398. * @private
  399. */
  400. async _syncOrderByDelete(transaction, cid, cpid, order, times, selfOperate = '-') {
  401. // this.initSqlBuilder();
  402. // this.sqlBuilder.setAndWhere('cid', {
  403. // value: this.db.escape(cid),
  404. // operate: '=',
  405. // });
  406. // this.sqlBuilder.setAndWhere('order', {
  407. // value: order,
  408. // operate: '>=',
  409. // });
  410. // this.sqlBuilder.setAndWhere('times', {
  411. // value: times,
  412. // operate: '=',
  413. // });
  414. // this.sqlBuilder.setUpdateData('order', {
  415. // value: 1,
  416. // selfOperate,
  417. // });
  418. // this.sqlBuilder.setUpdateData('audit_order', {
  419. // value: 1,
  420. // selfOperate,
  421. // });
  422. // const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  423. // const data = await transaction.query(sql, sqlParam);
  424. const sql = 'UPDATE ?? SET `order` = `order` ' + selfOperate + ' ?, `audit_order` = `audit_order` ' + selfOperate + ' ? WHERE `cid` = ? AND `cpid` ' + (cpid ? '= ' + cpid : 'is null') + ' AND `order` >= ? AND `times` = ?';
  425. const sqlParam = [this.tableName, 1, 1, cid, order, times];
  426. const data = await transaction.query(sql, sqlParam);
  427. return data;
  428. }
  429. /**
  430. * 开始审批
  431. * @param {Number} cpId - 方案id
  432. * @param {Number} times - 第几次审批
  433. * @return {Promise<boolean>}
  434. */
  435. async start(options, shenpi, cid, cpid = null, times = 1) {
  436. const audits = await this.getAllDataByCondition({ where: { cid, cpid, times, order: 1 } });
  437. if (audits.length === 0) {
  438. if (shenpi.contract === shenpiConst.sp_status.gdspl) {
  439. throw '请联系管理员添加审批人';
  440. } else {
  441. throw '请先选择审批人,再上报数据';
  442. }
  443. }
  444. const transaction = await this.db.beginTransaction();
  445. try {
  446. const begin_time = new Date();
  447. const data = {
  448. spid: options.spid || null,
  449. tid: options.tid || null,
  450. cid,
  451. cpid,
  452. aid: this.ctx.session.sessionUser.accountId,
  453. times,
  454. order: 0,
  455. status: auditConst.status.checked,
  456. begin_time,
  457. end_time: begin_time,
  458. audit_order: 0,
  459. };
  460. const result = await transaction.insert(this.tableName, data);
  461. const updateData = audits.map(x => { return { id: x.id, status: auditConst.status.checking, begin_time }; });
  462. await transaction.updateRows(this.tableName, updateData);
  463. if (cpid) {
  464. await transaction.update(this.ctx.service.contractPay.tableName, {
  465. id: cpid, status: auditConst.status.checking,
  466. });
  467. } else {
  468. await transaction.update(this.ctx.service.contract.tableName, {
  469. id: cid, status: auditConst.status.checking,
  470. });
  471. }
  472. await transaction.commit();
  473. } catch (err) {
  474. await transaction.rollback();
  475. throw err;
  476. }
  477. return true;
  478. }
  479. /**
  480. * 获取审核人需要审核的期列表
  481. *
  482. * @param auditorId
  483. * @return {Promise<*>}
  484. */
  485. async getAuditList(options, auditorId) {
  486. const optionsSql = options.spid ? 'c.spid = "' + options.spid + '"' : 'c.tid = ' + options.tid;
  487. const sql = 'SELECT c.`id`, c.`uid`, c.`status`' +
  488. ' FROM ?? AS c LEFT JOIN ?? AS ca ON c.`id` = ca.`cid`' +
  489. ' WHERE ' + optionsSql + ' AND ca.`aid` = ?';
  490. const sqlParam = [this.ctx.service.contract.tableName, this.tableName, auditorId];
  491. const result = await this.db.query(sql, sqlParam);
  492. // 过滤result中存在重复sid的值, 保留最新的一条
  493. const filterResult = [];
  494. const idArr = [];
  495. for (const r of result) {
  496. if (idArr.indexOf(r.id) === -1 && r.status !== auditConst.status.uncheck) {
  497. filterResult.push(r);
  498. idArr.push(r.id);
  499. }
  500. }
  501. return filterResult;
  502. }
  503. /**
  504. * 获取审核人审核的次数
  505. *
  506. * @param auditorId
  507. * @return {Promise<*>}
  508. */
  509. async getCountByChecked(auditorId, spid = '') {
  510. if (spid) {
  511. const sql = 'SELECT count(*) AS count FROM ?? AS a LEFT JOIN ?? AS t ON a.tid = t.id WHERE a.`aid` = ? AND a.`status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo]) + ') AND t.`spid` = ?';
  512. const sqlParam = [this.tableName, this.ctx.service.tender.tableName, auditorId, spid];
  513. const result = await this.db.queryOne(sql, sqlParam);
  514. return result.count ? result.count : 0;
  515. }
  516. return await this.db.count(this.tableName, { aid: auditorId, status: [auditConst.status.checked, auditConst.status.checkNo] });
  517. }
  518. /**
  519. * 获取最近一次审批结束时间
  520. *
  521. * @param auditorId
  522. * @return {Promise<*>}
  523. */
  524. async getLastEndTimeByChecked(auditorId, spid = '') {
  525. const sqSql = spid ? ' AND t.`spid` = "' + spid + '"' : '';
  526. const sql = 'SELECT a.`end_time` FROM ?? AS a LEFT JOIN ?? AS t ON a.`tid` = t.`id` WHERE a.`aid` = ? ' +
  527. 'AND a.`status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo]) + ')' + sqSql +
  528. ' ORDER BY a.`end_time` DESC';
  529. const sqlParam = [this.tableName, this.ctx.service.tender.tableName, auditorId];
  530. const result = await this.db.queryOne(sql, sqlParam);
  531. return result ? result.end_time : null;
  532. }
  533. /**
  534. * 用于添加推送所需的content内容
  535. * @param {Number} pid 项目id
  536. * @param {Number} tid 台账id
  537. * @param {Number} cpId 方案id
  538. * @param {Number} uid 审批人id
  539. */
  540. async getNoticeContent(pid, tid, cpId, uid, opinion = '') {
  541. const noticeSql = 'SELECT * FROM (SELECT ' +
  542. ' t.`id` As `tid`, ma.`cpid`, m.`code` as `c_code`, t.`name`, pa.`name` As `su_name`, pa.role As `su_role`' +
  543. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  544. ' LEFT JOIN ?? As m On t.`id` = m.`tid` AND m.`id` = ?' +
  545. ' LEFT JOIN ?? As ma ON m.`id` = ma.`cpid`' +
  546. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  547. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  548. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.changePlan.tableName, cpId, this.tableName, this.ctx.service.projectAccount.tableName, uid, pid];
  549. const content = await this.db.query(noticeSql, noticeSqlParam);
  550. if (content.length) {
  551. content[0].opinion = opinion;
  552. }
  553. return content.length ? JSON.stringify(content[0]) : '';
  554. }
  555. /**
  556. * 审批
  557. * @param {Number} cpId - 方案id
  558. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  559. * @param {Number} times - 第几次审批
  560. * @return {Promise<void>}
  561. */
  562. async check(contract, checkData) {
  563. if (!this._.includes([auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre], checkData.checkType)) {
  564. throw '提交数据错误';
  565. }
  566. const pid = this.ctx.session.sessionProject.id;
  567. switch (checkData.checkType) {
  568. case auditConst.status.checked:
  569. await this._checked(pid, contract, checkData);
  570. break;
  571. case auditConst.status.checkNo:
  572. await this._checkNo(pid, contract, checkData);
  573. break;
  574. case auditConst.status.checkNoPre:
  575. await this._checkNoPre(pid, contract, checkData);
  576. break;
  577. default:
  578. throw '无效审批操作';
  579. }
  580. }
  581. async _checked(pid, contract, checkData) {
  582. const accountId = this.ctx.session.sessionUser.accountId;
  583. const time = new Date();
  584. const tableName = contract.cid !== undefined ? this.ctx.service.contractPay.tableName : this.ctx.service.contract.tableName;
  585. // 整理当前流程审核人状态更新
  586. if (contract.curAuditorIds.length === 0) throw '审核数据错误';
  587. if (!this._.includes(contract.curAuditorIds, accountId)) throw '当前标段您无权审批';
  588. const flowAudits = contract.flowAuditors;
  589. const selfAudit = this._.find(flowAudits, { aid: accountId });
  590. const nextAudits = contract.nextAuditors;
  591. const transaction = await this.db.beginTransaction();
  592. try {
  593. // 更新本人审批状态
  594. await transaction.update(this.tableName, {
  595. id: selfAudit.id,
  596. status: checkData.checkType,
  597. opinion: checkData.opinion,
  598. end_time: time,
  599. });
  600. // await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, selfAudit.id);
  601. // 获取推送必要信息
  602. // const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, cpId, selfAudit.aid, checkData.opinion);
  603. // 添加推送
  604. // const records = [];
  605. // const auditors = await this.getAuditorsWithOwner(cpId, times);
  606. // auditors.forEach(audit => {
  607. // records.push({ pid, tid: selfAudit.tid, type: pushType.changePlan, uid: audit.aid, status: auditConst.status.checked, content: noticeContent });
  608. // });
  609. // await transaction.insert('zh_notice', records);
  610. if (contract.curAuditors.length === 1 || selfAudit.audit_type !== auditType.key.and) {
  611. // 或签更新他人审批状态
  612. if (selfAudit.audit_type === auditType.key.or) {
  613. const updateOther = [];
  614. for (const audit of flowAudits) {
  615. if (audit.aid === selfAudit.aid) continue;
  616. updateOther.push({
  617. id: audit.id,
  618. status: auditConst.status.checkSkip,
  619. opinion: '',
  620. end_time: time,
  621. });
  622. // await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, audit.id);
  623. }
  624. if (updateOther.length > 0) transaction.updateRows(this.tableName, updateOther);
  625. }
  626. // 无下一审核人表示,审核结束
  627. if (nextAudits.length > 0) {
  628. // 流程至下一审批人
  629. const updateData = nextAudits.map(x => { return { id: x.id, status: auditConst.status.checking, begin_time: time }; });
  630. await transaction.updateRows(this.tableName, updateData);
  631. // 同步 期信息
  632. await transaction.update(tableName, {
  633. id: contract.id, status: auditConst.status.checking,
  634. });
  635. } else {
  636. // 本期结束
  637. // 生成截止本期数据 final数据
  638. // 同步 期信息
  639. const final_auditor_str = flowAudits[0].audit_type === auditType.key.common
  640. ? flowAudits[0].name + (flowAudits[0].role ? '-' + flowAudits[0].role : '')
  641. : this.ctx.helper.transFormToChinese(flowAudits[0].audit_order) + '审';
  642. await transaction.update(tableName, {
  643. id: contract.id, status: checkData.checkType, final_auditor_str,
  644. });
  645. }
  646. } else {
  647. await transaction.update(tableName, {
  648. id: contract.id, status: auditConst.status.checking,
  649. });
  650. }
  651. await transaction.commit();
  652. } catch (err) {
  653. await transaction.rollback();
  654. throw err;
  655. }
  656. }
  657. async _checkNo(pid, contract, checkData) {
  658. const accountId = this.ctx.session.sessionUser.accountId;
  659. const time = new Date();
  660. const tableName = contract.cid !== undefined ? this.ctx.service.contractPay.tableName : this.ctx.service.contract.tableName;
  661. const audits = contract.curAuditors;
  662. if (audits.length === 0) throw '审核数据错误';
  663. const selfAudit = audits.find(x => { return x.aid === accountId; });
  664. if (!selfAudit) throw '当前标段您无权审批';
  665. const auditors = await this.getUniqAuditor(contract.cid || contract.id, contract.cid ? contract.id : null, contract.times, true); // 全部参与的审批人
  666. const newAuditors = auditors.map(x => {
  667. return {
  668. aid: x.aid, spid: contract.spid || null, tid: contract.tid || null, cid: contract.cid ? contract.cid : contract.id, cpid: contract.cid ? contract.id : null,
  669. times: contract.times + 1, order: x.audit_order, status: auditConst.status.uncheck,
  670. audit_type: x.audit_type, audit_order: x.audit_order,
  671. };
  672. });
  673. const transaction = await this.db.beginTransaction();
  674. try {
  675. const updateData = [];
  676. audits.forEach(x => {
  677. updateData.push({
  678. id: x.id,
  679. status: x.aid === selfAudit.aid ? checkData.checkType : auditConst.status.checkSkip,
  680. opinion: x.aid === selfAudit.aid ? checkData.opinion : '',
  681. end_time: x.aid === selfAudit.aid ? time : null,
  682. });
  683. });
  684. await transaction.updateRows(this.tableName, updateData);
  685. // await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  686. // 添加到消息推送表
  687. // const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, cpId, selfAudit.aid, checkData.opinion);
  688. // const records = [{ pid, tid: selfAudit.tid, type: pushType.changePlan, uid: this.ctx.change.uid, status: auditConst.status.checkNo, content: noticeContent }];
  689. // auditors.forEach(audit => {
  690. // records.push({ pid, tid: selfAudit.tid, type: pushType.changePlan, uid: audit.aid, status: auditConst.status.checkNo, content: noticeContent });
  691. // });
  692. // await transaction.insert(this.ctx.service.noticePush.tableName, records);
  693. // 同步期信息
  694. await transaction.update(tableName, {
  695. id: contract.id, status: checkData.checkType, times: contract.times + 1,
  696. });
  697. // 拷贝新一次审核流程列表
  698. await transaction.insert(this.tableName, newAuditors);
  699. await transaction.commit();
  700. } catch (err) {
  701. await transaction.rollback();
  702. throw err;
  703. }
  704. }
  705. /**
  706. * 审批退回到上一个审批人
  707. * @param {Number} pid 项目id
  708. * @param {int} postData - 表单提交的数据
  709. * @param {int} changeData - 变更令的数据
  710. * @return {void}
  711. */
  712. async _checkNoPre(pid, contract, checkData) {
  713. const accountId = this.ctx.session.sessionUser.accountId;
  714. const time = new Date();
  715. const tableName = contract.cid !== undefined ? this.ctx.service.contractPay.tableName : this.ctx.service.contract.tableName;
  716. // 整理当前流程审核人状态更新
  717. const audits = contract.curAuditors;
  718. if (audits.length === 0 || audits[0].order <= 1) throw '审核数据错误';
  719. const selfAudit = audits.find(x => { return x.aid === accountId; });
  720. if (!selfAudit) throw '当前标段您无权审批';
  721. const flowAudits = contract.flowAuditors;
  722. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  723. // const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
  724. const auditors2 = await this.getAuditGroupByList(contract.cid || contract.id, contract.cid ? contract.id : null, contract.times);
  725. const preAuditors = auditors2.filter(x => { return x.audit_order === selfAudit.audit_order - 1});
  726. const transaction = await this.db.beginTransaction();
  727. try {
  728. // 添加通知
  729. // const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, inspection.id, selfAudit.aid, checkData.opinion);
  730. // const defaultNoticeRecord = {
  731. // pid,
  732. // tid: selfAudit.tid,
  733. // type: pushType.inspection,
  734. // status: auditConst.status.checkNoPre,
  735. // content: noticeContent,
  736. // };
  737. // const records = [
  738. // {
  739. // uid: inspection.uid,
  740. // ...defaultNoticeRecord
  741. // },
  742. // ];
  743. // auditors2.forEach(audit => {
  744. // records.push({
  745. // uid: audit.aid,
  746. // ...defaultNoticeRecord
  747. // });
  748. // });
  749. // await transaction.insert('zh_notice', records);
  750. // 更新同一流程所有审批人状态
  751. const updateData = [];
  752. for (const audit of audits) {
  753. if (audit.aid === selfAudit.aid) {
  754. updateData.push({
  755. id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time,
  756. });
  757. } else {
  758. updateData.push({
  759. id: audit.id, status: auditConst.status.checkSkip, opinion: '', end_time: null,
  760. });
  761. }
  762. }
  763. await transaction.updateRows(this.tableName, updateData);
  764. // await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  765. // 顺移其后审核人流程顺序
  766. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE cid = ? AND cpid' + (contract.cid !== undefined ? ' = ' + contract.id : ' is null') + ' AND times = ? AND `order` > ?';
  767. await transaction.query(sql, [contract.cid ? contract.cid : contract.id, selfAudit.times, selfAudit.order]);
  768. // 上一审批人,当前审批人 再次添加至流程
  769. const newAuditors = [];
  770. preAuditors.forEach(x => {
  771. newAuditors.push({
  772. spid: contract.spid || null, tid: contract.tid || null, cid: contract.cid ? contract.cid : contract.id, cpid: contract.cid ? contract.id : null, aid: x.aid,
  773. times: x.times, order: selfAudit.order + 1,
  774. status: auditConst.status.checking, begin_time: time,
  775. audit_type: x.audit_type, audit_order: x.audit_order,
  776. });
  777. });
  778. // 获取ids值
  779. const newAuditors_result = await transaction.insert(this.tableName, newAuditors);
  780. // 获取刚批量添加的所有list
  781. for (let j = 0; j < preAuditors.length; j++) {
  782. newAuditors[j].id = newAuditors_result.insertId + j;
  783. }
  784. const newFlowAuditors = [];
  785. flowAudits.forEach(x => {
  786. newFlowAuditors.push({
  787. spid: contract.spid || null, tid: contract.tid || null, cid: contract.cid ? contract.cid : contract.id, cpid: contract.cid ? contract.id : null, aid: x.aid,
  788. times: x.times, order: selfAudit.order + 2,
  789. status: auditConst.status.uncheck,
  790. audit_type: x.audit_type, audit_order: x.audit_order,
  791. });
  792. });
  793. await transaction.insert(this.tableName, newFlowAuditors);
  794. // 同步 期信息
  795. await transaction.update(tableName, {
  796. id: contract.id,
  797. status: checkData.checkType,
  798. });
  799. await transaction.commit();
  800. } catch (err) {
  801. await transaction.rollback();
  802. throw err;
  803. }
  804. }
  805. async getAuditorGroup(cid, cpid, times) {
  806. const auditors = await this.getAuditors(cid, cpid, times); // 全部参与的审批人
  807. return this.ctx.helper.groupAuditors(auditors, 'order');
  808. }
  809. async getUserGroup(cid, cpid, times) {
  810. const group = await this.getAuditorGroup(cid, cpid, times);
  811. // const sql =
  812. // 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As cpid, 0 As `order`, 1 As audit_type, 0 As audit_order' +
  813. // ' FROM ' + this.ctx.service.changePlan.tableName + ' As s' +
  814. // ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  815. // ' ON s.uid = pa.id' +
  816. // ' WHERE s.id = ?';
  817. // const sqlParam = [times, cpId, cpId];
  818. // const user = await this.db.queryOne(sql, sqlParam);
  819. // user.audit_order = 0;
  820. // group.unshift([ user ]);
  821. return group;
  822. }
  823. async getUniqUserGroup(cid, cpid, times) {
  824. const group = await this.getAuditorGroup(cid, cpid, times);
  825. // const sql =
  826. // 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As cpid, 0 As `order`, 1 As audit_type, 0 As audit_order' +
  827. // ' FROM ' + this.ctx.service.changePlan.tableName + ' As s' +
  828. // ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  829. // ' ON s.uid = pa.id' +
  830. // ' WHERE s.id = ?';
  831. // const sqlParam = [times, cpId, cpId];
  832. // const user = await this.db.queryOne(sql, sqlParam);
  833. // user.audit_order = 0;
  834. // group.unshift([ user ]);
  835. return this.ctx.helper.groupAuditorsUniq(group);
  836. }
  837. async getAuditorHistory(cid, cpid, times, reverse = false) {
  838. const history = [];
  839. if (times >= 1) {
  840. for (let i = 1; i <= times; i++) {
  841. const auditors = await this.getAuditors(cid, cpid, i);
  842. const group = this.ctx.helper.groupAuditors(auditors);
  843. const historyGroup = [];
  844. // 找出group里audit_order最大值
  845. const max_info = group.length > 0 ? this._.maxBy(group, function(item) {
  846. return item && item[0] && item[0].audit_order;
  847. }) : null;
  848. const max_order = max_info ? max_info[0].audit_order : -1;
  849. for (const g of group) {
  850. const his = {
  851. beginYear: '', beginDate: '', beginTime: '', endYear: '', endDate: '', endTime: '', begin_time: null, end_time: null,
  852. audit_type: g[0].audit_type, audit_order: g[0].audit_order,
  853. auditors: g,
  854. };
  855. if (his.audit_type === auditType.key.common) {
  856. his.name = g[0].name;
  857. } else {
  858. his.name = this.ctx.helper.transFormToChinese(his.audit_order) + '审';
  859. }
  860. his.is_final = his.audit_order === max_order;
  861. if (g[0].begin_time) {
  862. his.begin_time = g[0].begin_time;
  863. const beginTime = this.ctx.moment(g[0].begin_time);
  864. his.beginYear = beginTime.format('YYYY');
  865. his.beginDate = beginTime.format('MM-DD');
  866. his.beginTime = beginTime.format('HH:mm:ss');
  867. }
  868. let end_time;
  869. g.forEach(x => {
  870. if (x.status === auditConst.status.checkSkip) return;
  871. if (!his.status || x.status === auditConst.status.checking) his.status = x.status;
  872. if (x.end_time && (!end_time || x.end_time > end_time)) {
  873. end_time = x.end_time;
  874. if (his.status !== auditConst.status.checking) his.status = x.status;
  875. }
  876. });
  877. if (end_time) {
  878. his.end_time = end_time;
  879. const endTime = this.ctx.moment(end_time);
  880. his.endYear = endTime.format('YYYY');
  881. his.endDate = endTime.format('MM-DD');
  882. his.endTime = endTime.format('HH:mm:ss');
  883. }
  884. historyGroup.push(his);
  885. }
  886. if (reverse) {
  887. history.push(historyGroup.reverse());
  888. } else {
  889. history.push(historyGroup);
  890. }
  891. }
  892. }
  893. return history;
  894. }
  895. async getUniqAuditor(cid, cpid, times, noYb = false) {
  896. const auditors = await this.getAuditors(cid, cpid, times, 'asc', noYb); // 全部参与的审批人
  897. const result = [];
  898. auditors.forEach(x => {
  899. if (result.findIndex(r => { return x.aid === r.aid && x.audit_order === r.audit_order; }) < 0) {
  900. result.push(x);
  901. }
  902. });
  903. return result;
  904. }
  905. async makeAudits(transaction, options, cid, cpid = null, uid) {
  906. // 创建审批人列表
  907. const lastContractInfo = await this.getHaveAuditLastInfo(options, cid, cpid, uid);
  908. if (lastContractInfo) {
  909. // 再获取非原报审批人
  910. const auditList = await this.getUniqAuditor(lastContractInfo.cid ? lastContractInfo.cid : lastContractInfo.id, lastContractInfo.cid ? lastContractInfo.id : null, lastContractInfo.times, true); // 全部参与的审批人
  911. if (auditList.length > 0) {
  912. const spAudits = [];
  913. for (const x of auditList) {
  914. if (x.audit_order !== 0) {
  915. spAudits.push({
  916. spid: options.spid || null,
  917. tid: options.tid || null,
  918. cid, cpid, aid: x.aid,
  919. times: 1, order: x.audit_order, status: auditConst.status.uncheck,
  920. audit_type: x.audit_type, audit_order: x.audit_order,
  921. });
  922. }
  923. }
  924. if (spAudits.length > 0) await transaction.insert(this.ctx.service.contractSpAudit.tableName, spAudits);
  925. }
  926. }
  927. }
  928. async getHaveAuditLastInfo(options, cid, cpid, uid) {
  929. const optionsSql = options.spid ? 'c.spid = "' + options.spid + '"' : 'c.tid = ' + options.tid;
  930. const tableName = cpid ? this.ctx.service.contractPay.tableName : this.ctx.service.contract.tableName;
  931. const joinSql = cpid ? 'ca.cpid' : 'ca.cid';
  932. const cpidSql = cpid ? 'AND ca.`cpid` is not null' : 'AND ca.`cpid` is null';
  933. const sql = 'SELECT c.* FROM ?? as c LEFT JOIN ?? as ca ON c.`id` = ' + joinSql + ' WHERE ' + optionsSql + ' AND c.`contract_type` = ? AND c.`uid` = ? ' + cpidSql + ' AND ca.`audit_order` > 0 ORDER BY c.`create_time` DESC';
  934. const sqlParam = [tableName, this.tableName, options.contract_type, uid];
  935. return await this.db.queryOne(sql, sqlParam);
  936. }
  937. async saveAudit(contract, times, sp_group, data) {
  938. const transaction = await this.db.beginTransaction();
  939. try {
  940. const auditors = await this.getAuditGroupByList(contract.cid || contract.id, contract.cid ? contract.id : null, times);
  941. const now_audit = this._.find(auditors, { aid: data.old_aid });
  942. if (data.operate !== 'del') {
  943. // const exist = await this.getDataByCondition({ cpid: cpId, times, aid: data.new_aid });
  944. // if (exist) throw '该审核人已存在,请勿重复添加';
  945. const groupAuditors = this._.flattenDeep(contract.userGroups);
  946. const exist = this._.find(groupAuditors, { aid: data.new_aid });
  947. if (exist) throw '该审核人已存在,请勿重复添加';
  948. }
  949. if (data.operate === 'add') {
  950. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  951. throw '当前人下无法操作新增';
  952. }
  953. const newAudit = {
  954. spid: contract.spid || null, tid: contract.tid || null, cid: contract.cid || contract.id, cpid: contract.cid ? contract.id : null, aid: data.new_aid,
  955. order: now_audit.order + 1,
  956. audit_order: now_audit.audit_order + 1, audit_type: auditType.key.common,
  957. times, status: auditConst.status.uncheck,
  958. };
  959. // order+1
  960. await this._syncOrderByDelete(transaction, contract.cid || contract.id, contract.cid ? contract.id : null, now_audit.order + 1, times, '+');
  961. await transaction.insert(this.tableName, newAudit);
  962. // 更新审批流程页数据,如果存在
  963. } else if (data.operate === 'add-sibling') {
  964. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  965. throw '当前人下无法操作新增';
  966. }
  967. const newAudit = {
  968. spid: contract.spid || null, tid: contract.tid || null, cid: contract.cid || contract.id, cpid: contract.cid ? contract.id : null, aid: data.new_aid,
  969. order: now_audit.order,
  970. audit_order: now_audit.audit_order, audit_type: now_audit.audit_type,
  971. times, status: auditConst.status.uncheck,
  972. };
  973. await transaction.insert(this.tableName, newAudit);
  974. } else if (data.operate === 'del') {
  975. if (now_audit.status !== auditConst.status.uncheck) {
  976. throw '当前人无法操作删除';
  977. }
  978. const flowAuditors = auditors.filter(x => { return x.audit_order === now_audit.audit_order; });
  979. await transaction.delete(this.tableName, { cid: contract.cid || contract.id, cpid: contract.cid ? contract.id : null, times, aid: now_audit.aid, order: now_audit.order });
  980. if (flowAuditors.length === 1) await this._syncOrderByDelete(transaction, contract.cid || contract.id, contract.cid ? contract.id : null, now_audit.order, times);
  981. // 旧的更新为is_old为1
  982. // await transaction.update(this.tableName, { is_old: 1 }, {
  983. // where: {
  984. // sid: stageId,
  985. // times,
  986. // aid: data.old_aid,
  987. // }
  988. // });
  989. } else if (data.operate === 'change') {
  990. const nowAudit = await this.getDataByCondition({ cid: contract.cid || contract.id, cpid: contract.cid ? contract.id : null, times, aid: now_audit.aid, order: now_audit.order });
  991. if (now_audit.status !== auditConst.status.uncheck || !nowAudit) {
  992. throw '当前人无法操作替换';
  993. }
  994. nowAudit.aid = data.new_aid;
  995. await transaction.update(this.tableName, nowAudit);
  996. // 旧的更新为is_old为1
  997. // await transaction.update(this.tableName, { is_old: 1 }, {
  998. // where: {
  999. // sid: stageId,
  1000. // times,
  1001. // aid: data.old_aid,
  1002. // }
  1003. // });
  1004. }
  1005. if (this.ctx.contract_tender) {
  1006. const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(this.ctx.contract.id);
  1007. if (tenderInfo.shenpi.contract === shenpiConst.sp_status.gdspl) {
  1008. const newAuditors = await transaction.select(this.tableName, { where: { cid: contract.cid || contract.id, cpid: contract.cid ? contract.id : null, times }, orders: [['order', 'asc']] });
  1009. newAuditors.shift();
  1010. const newAuditorGroup = this.ctx.helper.groupAuditors(newAuditors, 'order');
  1011. const uniqNewAuditorGroup = this.ctx.helper.groupAuditorsUniq(newAuditorGroup);
  1012. await this.ctx.service.shenpiAudit.updateAuditListWithAuditType(transaction, this.ctx.contract.id, tenderInfo.shenpi.contract, shenpiConst.sp_type.contract, uniqNewAuditorGroup, sp_group);
  1013. } else if (tenderInfo.shenpi.contract === shenpiConst.sp_status.gdzs) {
  1014. const newAuditors = await this.getAuditGroupByList(contract.cid || contract.id, contract.cid ? contract.id : null, times, transaction);
  1015. await this.ctx.service.shenpiAudit.updateAuditList(transaction, this.ctx.contract.id, tenderInfo.shenpi.contract, shenpiConst.sp_type.contract, this._.map(newAuditors, 'aid'));
  1016. }
  1017. } else {
  1018. if (this.ctx.subProject.shenpi.contract === shenpiConst.sp_status.gdspl) {
  1019. const newAuditors = await transaction.select(this.tableName, { where: { cid: contract.cid || contract.id, cpid: contract.cid ? contract.id : null, times }, orders: [['order', 'asc']] });
  1020. newAuditors.shift();
  1021. const newAuditorGroup = this.ctx.helper.groupAuditors(newAuditors, 'order');
  1022. const uniqNewAuditorGroup = this.ctx.helper.groupAuditorsUniq(newAuditorGroup);
  1023. await this.ctx.service.shenpiAudit.updateAuditListWithAuditType(transaction, this.ctx.subProject.id, this.ctx.subProject.shenpi.contract, shenpiConst.sp_type.contract, uniqNewAuditorGroup, sp_group);
  1024. } else if (this.ctx.subProject.shenpi.contract === shenpiConst.sp_status.gdzs) {
  1025. const newAuditors = await this.getAuditGroupByList(contract.cid || contract.id, contract.cid ? contract.id : null, times, transaction);
  1026. await this.ctx.service.shenpiAudit.updateAuditList(transaction, this.ctx.subProject.id, this.ctx.subProject.shenpi.contract, shenpiConst.sp_type.contract, this._.map(newAuditors, 'aid'));
  1027. }
  1028. }
  1029. // 更新到审批流程方法
  1030. await transaction.commit();
  1031. } catch (err) {
  1032. await transaction.rollback();
  1033. throw err;
  1034. }
  1035. }
  1036. async changeSpGroup(contract, sp_group) {
  1037. const transaction = await this.db.beginTransaction();
  1038. try {
  1039. const group = await this.ctx.service.shenpiGroup.getDataById(sp_group);
  1040. if (!group) {
  1041. throw '该固定审批组不存在,请刷新页面重新获取';
  1042. }
  1043. const condition = { sp_type: shenpiConst.sp_type.contract, sp_status: shenpiConst.sp_status.gdspl, sp_group: group.id };
  1044. if (this.ctx.contract_tender) {
  1045. condition.tid = contract.tid;
  1046. } else {
  1047. condition.spid = contract.spid;
  1048. }
  1049. const shenpiList = await this.ctx.service.shenpiAudit.getAllDataByCondition({ where: condition });
  1050. // await this.ctx.service.shenpiAudit.noYbShenpiList(change.uid, shenpiList);
  1051. await this.updateNewAuditors(contract, shenpiList, transaction);
  1052. if (contract.cid === undefined) {
  1053. await transaction.update(this.ctx.service.contract.tableName, { id: contract.id, sp_group: group.id });
  1054. } else {
  1055. await transaction.update(this.ctx.service.contractPay.tableName, { id: contract.id, sp_group: group.id });
  1056. }
  1057. await transaction.commit();
  1058. } catch (err) {
  1059. await transaction.rollback();
  1060. throw err;
  1061. }
  1062. return true;
  1063. }
  1064. async updateNewAuditList(contract, newList) {
  1065. const transaction = await this.db.beginTransaction();
  1066. try {
  1067. await this.updateNewAuditors(contract, newList, transaction);
  1068. await transaction.commit();
  1069. } catch (err) {
  1070. await transaction.rollback();
  1071. throw err;
  1072. }
  1073. }
  1074. async updateNewAuditors(contract, newList, transaction) {
  1075. const condition = { times: contract.times };
  1076. if (contract.cid !== undefined) {
  1077. condition.cid = contract.cid;
  1078. condition.cpid = contract.id;
  1079. } else {
  1080. condition.cid = contract.id;
  1081. condition.cpid = null;
  1082. }
  1083. // 先删除旧的审批流,再添加新的
  1084. await transaction.delete(this.tableName, condition);
  1085. const newAuditors = [];
  1086. for (const auditor of newList) {
  1087. newAuditors.push({
  1088. spid: contract.spid || null, tid: contract.tid || null, cid: contract.cid || contract.id, cpid: contract.cid ? contract.id : null, aid: auditor.audit_id,
  1089. times: contract.times, order: auditor.audit_order, status: auditConst.status.uncheck,
  1090. audit_type: auditor.audit_type, audit_order: auditor.audit_order,
  1091. });
  1092. }
  1093. if (newAuditors.length > 0) {
  1094. await transaction.insert(this.tableName, newAuditors);
  1095. // 增加到权限表
  1096. const options = {};
  1097. if (contract.spid) options.spid = contract.spid;
  1098. if (contract.tid) options.tid = contract.tid;
  1099. const auditorIds = this._.map(newAuditors, 'aid');
  1100. const list = [];
  1101. for (const aid of auditorIds) {
  1102. list.push({ id: aid });
  1103. }
  1104. await this.ctx.service.contractAudit.saveAudits(options, list, transaction);
  1105. }
  1106. }
  1107. async updateLastAudit(contract, auditList, lastId) {
  1108. const transaction = await this.db.beginTransaction();
  1109. try {
  1110. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  1111. const existAudit = auditList.find(x => { return x.aid === lastId; });
  1112. let order = auditList.length > 0 ? auditList.reduce((rst, a) => { return Math.max(rst, a.order); }, 0) + 1 : 1; // 最大值 + 1
  1113. if (existAudit) {
  1114. const condition = { times: contract.times, aid: lastId };
  1115. if (contract.cid !== undefined) {
  1116. condition.cid = contract.cid;
  1117. condition.cpid = contract.id;
  1118. } else {
  1119. condition.cid = contract.id;
  1120. condition.cpid = null;
  1121. }
  1122. await transaction.delete(this.tableName, condition);
  1123. const sameOrder = auditList.filter(x => { return x.order === existAudit.order; });
  1124. if (sameOrder.length === 1) {
  1125. const updateData = [];
  1126. auditList.forEach(x => {
  1127. if (x.order <= existAudit.order) return;
  1128. updateData.push({ id: x.id, order: x.order - 1, audit_order: x.audit_order - 1 });
  1129. });
  1130. if (updateData.length > 0) {
  1131. await transaction.updateRows(this.tableName, updateData);
  1132. }
  1133. order = order - 1;
  1134. }
  1135. }
  1136. // 添加终审
  1137. const newAuditor = {
  1138. spid: contract.spid || null, tid: contract.tid || null, cid: contract.cid || contract.id, cpid: contract.cid ? contract.id : null, aid: lastId,
  1139. times: contract.times, order, status: auditConst.status.uncheck,
  1140. audit_type: auditType.key.common, audit_order: order,
  1141. };
  1142. await transaction.insert(this.tableName, newAuditor);
  1143. await transaction.commit();
  1144. } catch (err) {
  1145. await transaction.rollback();
  1146. throw err;
  1147. }
  1148. }
  1149. /**
  1150. * 获取审核人已经审核过的审批信息(包括退回,通过,重新审批等)
  1151. *
  1152. * @param auditorId
  1153. * @return {Promise<*>}
  1154. */
  1155. // async getDonesByAudit(auditorId, spid = '') {
  1156. // const spSql = spid ? ' and t.`spid` = "' + spid + '"' : '';
  1157. // const status = [auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre];
  1158. // const sql =
  1159. // 'SELECT la.`status`, la.`end_time` as `shenpi_time`, la.`cid`, la.`cpid`, t.`id`, cp.`code`, t.`name`, t.`spid` ' +
  1160. // ' FROM ?? AS la Left Join ?? AS t ON la.`tid` = t.`id` LEFT JOIN ?? AS cp ON la.`cpid` = cp.`id`' +
  1161. // ' WHERE la.`aid` = ? AND la.`status` in (' + this.ctx.helper.getInArrStrSqlFilter(status) + ')' + spSql +
  1162. // ' ORDER BY la.`end_time` DESC';
  1163. // const sqlParam = [
  1164. // this.tableName,
  1165. // this.ctx.service.tender.tableName,
  1166. // this.ctx.service.contract.tableName,
  1167. // auditorId,
  1168. // ];
  1169. // return await this.db.query(sql, sqlParam);
  1170. // }
  1171. }
  1172. return ContractSpAudit;
  1173. };