material_audit.js 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2019/2/27
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').material;
  10. const auditType = require('../const/audit').auditType;
  11. const pushType = require('../const/audit').pushType;
  12. const pushOperate = require('../const/spec_3f').pushOperate;
  13. const smsTypeConst = require('../const/sms_type');
  14. const wxConst = require('../const/wechat_template');
  15. const shenpiConst = require('../const/shenpi');
  16. const materialConst = require('../const/material');
  17. module.exports = app => {
  18. class MaterialAudit extends app.BaseService {
  19. /**
  20. * 构造函数
  21. *
  22. * @param {Object} ctx - egg全局变量
  23. * @return {void}
  24. */
  25. constructor(ctx) {
  26. super(ctx);
  27. this.tableName = 'material_audit';
  28. }
  29. /**
  30. * 获取 审核人信息
  31. *
  32. * @param {Number} materialId - 材料调差期id
  33. * @param {Number} auditorId - 审核人id
  34. * @param {Number} times - 第几次审批
  35. * @return {Promise<*>}
  36. */
  37. async getAuditor(materialId, auditorId, times = 1) {
  38. 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` ' +
  39. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  40. ' WHERE la.`mid` = ? and la.`aid` = ? and la.`times` = ? ORDER BY la.`order` DESC';
  41. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, auditorId, times];
  42. return await this.db.queryOne(sql, sqlParam);
  43. }
  44. /**
  45. * 获取 审核列表信息
  46. *
  47. * @param {Number} materialId - 材料调差期id
  48. * @param {Number} times - 第几次审批
  49. * @return {Promise<*>}
  50. */
  51. async getAuditors(materialId, times = 1, order_sort = 'asc') {
  52. const sql = 'SELECT la.id, la.aid, la.times, la.order, la.status, la.opinion, la.is_old, la.begin_time, la.end_time, la.audit_type, la.audit_order,' +
  53. ' pa.name, pa.company, pa.role, pa.mobile, pa.telephone, pa.sign_path' +
  54. ` FROM ${this.tableName} la LEFT JOIN ${this.ctx.service.projectAccount.tableName} pa ON la.aid = pa.id` +
  55. ' WHERE la.mid = ? AND la.times = ?' +
  56. ' ORDER BY la.order ' + order_sort;
  57. const sqlParam = [materialId, times];
  58. const result = await this.db.query(sql, sqlParam);
  59. const max_sort = this._.max(result.map(x => { return x.audit_order; }));
  60. for (const i in result) {
  61. result[i].max_sort = max_sort;
  62. }
  63. return result;
  64. }
  65. async getLastestAuditors(materialId, times, status) {
  66. const sql =
  67. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.audit_type, la.audit_order,' +
  68. ' la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  69. ' FROM ' + this.tableName + ' AS la' +
  70. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' AS pa ON la.`aid` = pa.`id`' +
  71. ' WHERE la.`mid` = ? and la.`status` = ? and la.`times` = ?' +
  72. ' ORDER BY `order` DESC';
  73. const sqlParam = [materialId, status, times ? times: 1];
  74. const result = await this.db.query(sql, sqlParam);
  75. if (result.length === 0) return [];
  76. return result.filter(x => { return x.order === result[0].order });
  77. }
  78. async getFinalAuditGroup(materialId, times = 1) {
  79. const sql =
  80. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, pa.`sign_path`, la.`times`, la.`mid`, la.`audit_order`, la.`audit_type`, Max(la.`order`) as max_order, GROUP_CONCAT(la.tp_data) as tp_data ' +
  81. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  82. ' WHERE la.`mid` = ? and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
  83. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, times];
  84. const result = await this.db.query(sql, sqlParam);
  85. for (const r of result) {
  86. const auditor = await this.getDataByCondition({mid: materialId, times: r.times, order: r.max_order});
  87. r.status = auditor.status;
  88. r.opinion = auditor.opinion;
  89. r.begin_time = auditor.begin_time;
  90. r.end_time = auditor.end_time;
  91. }
  92. return result;
  93. }
  94. /**
  95. * 获取 当前审核人
  96. *
  97. * @param {Number} materialId - 材料调差期id
  98. * @param {Number} times - 第几次审批
  99. * @return {Promise<*>}
  100. */
  101. async getCurAuditor(materialId, times = 1) {
  102. const sql = 'SELECT la.`id`, 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_order ' +
  103. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  104. ' WHERE la.`mid` = ? and la.`status` = ? and la.`times` = ?';
  105. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, auditConst.status.checking, times];
  106. return await this.db.queryOne(sql, sqlParam);
  107. }
  108. async getCurAuditors(materialId, times = 1) {
  109. const sql =
  110. '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 ' +
  111. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  112. ' WHERE la.`mid` = ? and la.`status` = ? and la.`times` = ?';
  113. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, auditConst.status.checking, times];
  114. return await this.db.query(sql, sqlParam);
  115. }
  116. /**
  117. * 获取 最新审核顺序
  118. *
  119. * @param {Number} materialId - 材料调差期id
  120. * @param {Number} times - 第几次审批
  121. * @return {Promise<number>}
  122. */
  123. async getNewOrder(materialId, times = 1) {
  124. const sql = 'SELECT Max(??) As max_order, Max(audit_order) As max_audit_order FROM ?? Where `mid` = ? and `times` = ?';
  125. const sqlParam = ['order', this.tableName, materialId, times];
  126. const result = await this.db.queryOne(sql, sqlParam);
  127. return result && result.max_order ? [result.max_order + 1, result.max_audit_order + 1] : [1, 1];
  128. }
  129. /**
  130. * 新增审核人
  131. *
  132. * @param {Number} materialId - 材料调差期id
  133. * @param {Number} auditorId - 审核人id
  134. * @param {Number} times - 第几次审批
  135. * @return {Promise<number>}
  136. */
  137. async addAuditor(materialId, auditorId, times = 1, is_gdzs = 0) {
  138. const transaction = await this.db.beginTransaction();
  139. try {
  140. let [newOrder, newAuditOrder] = await this.getNewOrder(materialId, times);
  141. // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
  142. newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
  143. newAuditOrder = is_gdzs === 1 ? newAuditOrder - 1 : newAuditOrder;
  144. if (is_gdzs) await this._syncOrderByDelete(transaction, materialId, newOrder, times, '+');
  145. const data = {
  146. tid: this.ctx.tender.id,
  147. mid: materialId,
  148. aid: auditorId,
  149. times,
  150. order: newOrder,
  151. status: auditConst.status.uncheck,
  152. audit_order: newAuditOrder,
  153. };
  154. const result = await transaction.insert(this.tableName, data);
  155. await transaction.commit();
  156. return result.effectRows = 1;
  157. } catch (err) {
  158. await transaction.rollback();
  159. throw err;
  160. }
  161. return false;
  162. }
  163. /**
  164. * 移除审核人时,同步其后审核人order
  165. * @param transaction - 事务
  166. * @param {Number} materialId - 材料调差期id
  167. * @param {Number} auditorId - 审核人id
  168. * @param {Number} times - 第几次审批
  169. * @return {Promise<*>}
  170. * @private
  171. */
  172. async _syncOrderByDelete(transaction, materialId, order, times, selfOperate = '-') {
  173. this.initSqlBuilder();
  174. this.sqlBuilder.setAndWhere('mid', {
  175. value: materialId,
  176. operate: '=',
  177. });
  178. this.sqlBuilder.setAndWhere('order', {
  179. value: order,
  180. operate: '>=',
  181. });
  182. this.sqlBuilder.setAndWhere('times', {
  183. value: times,
  184. operate: '=',
  185. });
  186. this.sqlBuilder.setUpdateData('order', {
  187. value: 1,
  188. selfOperate: selfOperate,
  189. });
  190. this.sqlBuilder.setUpdateData('audit_order', {
  191. value: 1,
  192. selfOperate: selfOperate,
  193. });
  194. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  195. const data = await transaction.query(sql, sqlParam);
  196. return data;
  197. }
  198. /**
  199. * 移除审核人
  200. *
  201. * @param {Number} materialId - 材料调差期id
  202. * @param {Number} auditorId - 审核人id
  203. * @param {Number} times - 第几次审批
  204. * @return {Promise<boolean>}
  205. */
  206. async deleteAuditor(materialId, auditorId, times = 1) {
  207. const transaction = await this.db.beginTransaction();
  208. try {
  209. const condition = { mid: materialId, aid: auditorId, times };
  210. const auditor = await this.getDataByCondition(condition);
  211. if (!auditor) {
  212. throw '该审核人不存在';
  213. }
  214. await transaction.delete(this.tableName, { mid: materialId, order: auditor.order, times});
  215. await this._syncOrderByDelete(transaction, materialId, auditor.order, times);
  216. await transaction.commit();
  217. } catch (err) {
  218. await transaction.rollback();
  219. throw err;
  220. }
  221. return true;
  222. }
  223. async getTpData(transaction, materialId, decimal = (this.ctx.material.decimal ? this.ctx.material.decimal : materialConst.decimal)) {
  224. const materialInfo = await transaction.get(this.ctx.service.material.tableName, { id: materialId });
  225. const tp_data = {
  226. m_tp: materialInfo.m_tp !== null ? this.ctx.helper.round(materialInfo.m_tp, decimal.tp) : null,
  227. tax_tp: materialInfo.material_tax ? (materialInfo.m_tax_tp ? materialInfo.m_tax_tp : materialInfo.m_tp) :
  228. (materialInfo.m_tp !== null ? this.ctx.helper.round(this.ctx.helper.mul(materialInfo.m_tp, 1+materialInfo.rate/100), decimal.tp) : null),
  229. ex_tp: materialInfo.ex_tp !== null ? this.ctx.helper.round(materialInfo.ex_tp, decimal.tp) : null,
  230. ex_tax_tp: materialInfo.ex_tp !== null ? this.ctx.helper.round(this.ctx.helper.mul(materialInfo.ex_tp, 1+materialInfo.exponent_rate/100), decimal.tp) : null,
  231. }
  232. tp_data.total_tp = this.ctx.helper.add(tp_data.m_tp, tp_data.ex_tp);
  233. tp_data.total_tax_tp = !materialInfo.material_tax ? this.ctx.helper.add(tp_data.tax_tp, tp_data.ex_tax_tp) : tp_data.ex_tax_tp;
  234. if (materialInfo.is_stage_self) {
  235. const materialStageList = await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: materialId } });
  236. const stage_tp = [];
  237. for (const ms of materialStageList) {
  238. stage_tp.push({
  239. id: ms.id,
  240. sid: ms.sid,
  241. order: ms.order,
  242. m_tp: this.ctx.helper.round(ms.m_tp, decimal.tp),
  243. m_tax_tp: this.ctx.helper.round(ms.m_tax_tp, decimal.tp),
  244. });
  245. }
  246. tp_data.stage_tp = stage_tp;
  247. }
  248. return tp_data;
  249. }
  250. /**
  251. * 开始审批
  252. * @param {Number} materialId - 材料调差期id
  253. * @param {Number} times - 第几次审批
  254. * @return {Promise<boolean>}
  255. */
  256. async start(materialId, times = 1) {
  257. const audits = await this.getAllDataByCondition({ where: { mid: materialId, times, order: 1 } });
  258. if (audits.length === 0) {
  259. if(this.ctx.tender.info.shenpi.material === shenpiConst.sp_status.gdspl) {
  260. throw '请联系管理员添加审批人';
  261. } else {
  262. throw '请先选择审批人,再上报数据';
  263. }
  264. }
  265. const transaction = await this.db.beginTransaction();
  266. try {
  267. const tp_data = await this.getTpData(transaction, materialId);
  268. const begin_time = new Date();
  269. const updateData = audits.map(x => { return { id: x.id, status: auditConst.status.checking, begin_time, tp_data: JSON.stringify(tp_data) } });
  270. await transaction.updateRows(this.tableName, updateData);
  271. await transaction.update(this.ctx.service.material.tableName, {
  272. id: materialId, status: auditConst.status.checking, tp_data: JSON.stringify(tp_data),
  273. });
  274. // 本期一些必要数据(如应耗数量和上期调差金额)插入到material_bills_history表里
  275. const materialBillsData = await this.ctx.service.materialBills.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  276. if (materialBillsData.length === 0 && this.ctx.material.ex_expr === null) {
  277. throw '请至少使用一种调差方式';
  278. }
  279. // 微信模板通知
  280. const materialInfo = await this.ctx.service.material.getDataById(materialId);
  281. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/material/' + materialInfo.order);
  282. const material_decimal = materialInfo && materialInfo.decimal ? JSON.parse(materialInfo.decimal) : materialConst.decimal;
  283. const users = this._.map(audits, 'aid');
  284. const wechatData = {
  285. wap_url: shenpiUrl,
  286. qi: materialInfo.order,
  287. status: wxConst.status.check,
  288. tips: wxConst.tips.check,
  289. begin_time: Date.parse(new Date()),
  290. m_tp: this.ctx.helper.add(this.ctx.helper.round(materialInfo.m_tp, material_decimal.tp), this.ctx.helper.round(materialInfo.ex_tp, material_decimal.tp)),
  291. hs_m_tp: this.ctx.helper.add(this.ctx.helper.round(this.ctx.helper.mul(materialInfo.m_tp, 1+materialInfo.rate/100), material_decimal.tp), this.ctx.helper.round(this.ctx.helper.mul(materialInfo.ex_tp, 1+materialInfo.exponent_rate/100), material_decimal.tp)),
  292. };
  293. await this.ctx.helper.sendWechat(users, smsTypeConst.const.TC, smsTypeConst.judge.approval.toString(), wxConst.template.material, wechatData);
  294. for (const audit of audits) {
  295. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.TC, {
  296. pid: this.ctx.session.sessionProject.id,
  297. tid: this.ctx.tender.id,
  298. uid: audit.aid,
  299. sp_type: 'material',
  300. sp_id: audit.id,
  301. table_name: this.tableName,
  302. template: wxConst.template.material,
  303. wx_data: wechatData,
  304. });
  305. }
  306. // todo 更新标段tender状态 ?
  307. // 检查三方特殊推送
  308. await this.ctx.service.specMsg.addMaterialMsg(transaction, this.ctx.session.sessionProject.id, materialInfo, pushOperate.material.flow);
  309. await transaction.commit();
  310. } catch (err) {
  311. await transaction.rollback();
  312. throw err;
  313. }
  314. return true;
  315. }
  316. async _checked(pid, materialId, checkData, times) {
  317. const accountId = this.ctx.session.sessionUser.accountId;
  318. const time = new Date();
  319. // 整理当前流程审核人状态更新
  320. const audits = await this.getAllDataByCondition({ where: { mid: materialId, times, status: auditConst.status.checking } });
  321. if (audits.length === 0) throw '审核数据错误';
  322. const selfAudit = audits.find(x => { return x.aid === accountId; });
  323. if (!selfAudit) throw '当前标段您无权审批';
  324. const flowAudits = await this.getAllDataByCondition({ where: { mid: materialId, times, order: selfAudit.order } });
  325. const nextAudits = await this.getAllDataByCondition({ where: { mid: materialId, times, order: selfAudit.order + 1 } });
  326. const transaction = await this.db.beginTransaction();
  327. try {
  328. // 获取当前总金额及独立单价期的金额,添加到tp_data中,报表使用
  329. const tp_data = await this.getTpData(transaction, materialId);
  330. await transaction.update(this.tableName, {
  331. id: selfAudit.id,
  332. status: checkData.checkType,
  333. opinion: checkData.opinion,
  334. end_time: time,
  335. tp_data: JSON.stringify(tp_data),
  336. });
  337. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, selfAudit.id);
  338. // 获取推送必要信息
  339. const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, materialId, accountId, checkData.opinion);
  340. const auditors = await this.getAuditorsWithOwner(materialId, times);
  341. // 添加推送
  342. const records = [];
  343. // const records = [{ pid, type: pushType.material, uid: this.ctx.material.user_id, status: auditConst.status.checked, content: noticeContent }];
  344. auditors.forEach(audit => {
  345. records.push({ pid, tid: selfAudit.tid, type: pushType.material, uid: audit.aid, status: auditConst.status.checked, content: noticeContent });
  346. });
  347. await transaction.insert('zh_notice', records);
  348. const materialInfo = await this.ctx.service.material.getDataById(materialId);
  349. const material_decimal = materialInfo && materialInfo.decimal ? JSON.parse(materialInfo.decimal) : materialConst.decimal;
  350. if (audits.length === 1 || selfAudit.audit_type === auditType.key.or) {
  351. // 或签更新他人审批状态
  352. if (selfAudit.audit_type === auditType.key.or) {
  353. const updateOther = [];
  354. for (const audit of audits) {
  355. if (audit.aid === selfAudit.aid) continue;
  356. updateOther.push({
  357. id: audit.id,
  358. status: auditConst.status.checkSkip,
  359. opinion: '',
  360. end_time: time,
  361. });
  362. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, audit.id);
  363. }
  364. if (updateOther.length > 0) transaction.updateRows(this.tableName, updateOther);
  365. }
  366. // 无下一审核人表示,审核结束
  367. if (nextAudits.length > 0) {
  368. // 复制一份下一审核人数据
  369. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.order, transaction);
  370. // 流程至下一审批人
  371. const updateData = nextAudits.map(x => {
  372. return {
  373. id: x.id,
  374. status: auditConst.status.checking,
  375. begin_time: time,
  376. tp_data: JSON.stringify(tp_data)
  377. };
  378. });
  379. await transaction.updateRows(this.tableName, updateData);
  380. // 同步 期信息
  381. await transaction.update(this.ctx.service.material.tableName, {
  382. id: materialId, status: auditConst.status.checking,
  383. });
  384. const begin_audit = await this.getDataByCondition({
  385. mid: materialId,
  386. order: 1,
  387. });
  388. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/material/' + materialInfo.order);
  389. // 微信模板通知
  390. const wechatData = {
  391. wap_url: shenpiUrl,
  392. qi: materialInfo.order,
  393. status: wxConst.status.check,
  394. tips: wxConst.tips.check,
  395. begin_time: Date.parse(begin_audit.begin_time),
  396. m_tp: this.ctx.helper.add(this.ctx.helper.round(materialInfo.m_tp, material_decimal.tp), this.ctx.helper.round(materialInfo.ex_tp, material_decimal.tp)),
  397. hs_m_tp: this.ctx.helper.add(this.ctx.helper.round(this.ctx.helper.mul(materialInfo.m_tp, 1 + materialInfo.rate / 100), material_decimal.tp), this.ctx.helper.round(this.ctx.helper.mul(materialInfo.ex_tp, 1 + materialInfo.exponent_rate / 100), material_decimal.tp)),
  398. };
  399. await this.ctx.helper.sendWechat(this._.map(nextAudits, 'aid'), smsTypeConst.const.TC, smsTypeConst.judge.approval.toString(), wxConst.template.material, wechatData);
  400. // 重新发送配置
  401. for (const a of nextAudits) {
  402. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.TC, {
  403. pid: this.ctx.session.sessionProject.id,
  404. tid: this.ctx.tender.id,
  405. uid: a.aid,
  406. sp_type: 'material',
  407. sp_id: a.id,
  408. table_name: this.tableName,
  409. template: wxConst.template.material,
  410. wx_data: wechatData,
  411. });
  412. }
  413. // 检查三方特殊推送
  414. await this.ctx.service.specMsg.addMaterialMsg(transaction, pid, materialInfo, pushOperate.material.flow);
  415. } else {
  416. // 本期结束
  417. // 同步 期信息
  418. await transaction.update(this.ctx.service.material.tableName, {
  419. id: materialId, status: checkData.checkType,
  420. });
  421. // 处理旧数据,防止重复插入到历史表
  422. await transaction.delete(this.ctx.service.materialBillsHistory.tableName, {
  423. tid: this.ctx.tender.id,
  424. order: this.ctx.material.order
  425. });
  426. const mbhList = [];
  427. const materialBillsData = await this.ctx.service.materialBills.getAllDataByCondition({where: {tid: this.ctx.tender.id}});
  428. for (const mb of materialBillsData) {
  429. if (mb.code === '') {
  430. throw '调差工料编号不能为空';
  431. }
  432. const newMbh = {
  433. tid: this.ctx.tender.id,
  434. mid: this.ctx.material.id,
  435. order: this.ctx.material.order,
  436. mb_id: mb.id,
  437. quantity: mb.quantity,
  438. expr: mb.expr,
  439. msg_tp: mb.msg_tp,
  440. msg_times: mb.msg_times,
  441. msg_spread: mb.msg_spread,
  442. m_up_risk: mb.m_up_risk,
  443. m_down_risk: mb.m_down_risk,
  444. m_spread: mb.m_spread,
  445. m_tp: mb.m_tp,
  446. pre_tp: mb.pre_tp,
  447. m_tax_tp: mb.m_tax_tp,
  448. tax_pre_tp: mb.tax_pre_tp,
  449. origin: mb.origin,
  450. is_summary: mb.is_summary,
  451. m_tax: mb.m_tax,
  452. };
  453. mbhList.push(newMbh);
  454. }
  455. if (mbhList.length !== 0) await transaction.insert(this.ctx.service.materialBillsHistory.tableName, mbhList);
  456. // 处理旧数据,防止重复插入到历史表
  457. await transaction.delete(this.ctx.service.materialExponentHistory.tableName, {
  458. tid: this.ctx.tender.id,
  459. order: this.ctx.material.order
  460. });
  461. const materialExponentData = await this.ctx.service.materialExponent.getAllDataByCondition({where: {tid: this.ctx.tender.id}});
  462. const mehList = [];
  463. for (const me of materialExponentData) {
  464. const newMeh = {
  465. tid: this.ctx.tender.id,
  466. mid: this.ctx.material.id,
  467. order: this.ctx.material.order,
  468. me_id: me.id,
  469. type: me.type,
  470. weight_num: me.weight_num,
  471. basic_price: me.basic_price,
  472. basic_times: me.basic_times,
  473. m_price: me.m_price,
  474. calc_num: me.calc_num,
  475. is_summary: me.is_summary,
  476. };
  477. mehList.push(newMeh);
  478. }
  479. if (mehList.length !== 0) await transaction.insert(this.ctx.service.materialExponentHistory.tableName, mehList);
  480. // 微信模板通知
  481. const begin_audit = await this.getDataByCondition({
  482. mid: materialId,
  483. order: 1,
  484. });
  485. const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), materialInfo.user_id));
  486. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/material/' + materialInfo.order);
  487. const wechatData = {
  488. wap_url: shenpiUrl,
  489. qi: materialInfo.order,
  490. status: wxConst.status.success,
  491. tips: wxConst.tips.success,
  492. begin_time: Date.parse(begin_audit.begin_time),
  493. m_tp: this.ctx.helper.add(this.ctx.helper.round(materialInfo.m_tp, material_decimal.tp), this.ctx.helper.round(materialInfo.ex_tp, material_decimal.tp)),
  494. hs_m_tp: this.ctx.helper.add(this.ctx.helper.round(this.ctx.helper.mul(materialInfo.m_tp, 1 + materialInfo.rate / 100), material_decimal.tp), this.ctx.helper.round(this.ctx.helper.mul(materialInfo.ex_tp, 1 + materialInfo.exponent_rate / 100), material_decimal.tp)),
  495. };
  496. await this.ctx.helper.sendWechat(users, smsTypeConst.const.TC, smsTypeConst.judge.result.toString(), wxConst.template.material, wechatData);
  497. // 检查三方特殊推送
  498. await this.ctx.service.specMsg.addMaterialMsg(transaction, pid, materialInfo, pushOperate.material.flow);
  499. await this.ctx.service.specMsg.addMaterialMsg(transaction, pid, materialInfo, pushOperate.material.checked);
  500. }
  501. } else {
  502. // 同步 期信息
  503. await transaction.update(this.ctx.service.material.tableName, {
  504. id: materialId,
  505. status: auditConst.status.checking,
  506. });
  507. }
  508. await transaction.commit();
  509. } catch (err) {
  510. await transaction.rollback();
  511. throw err;
  512. }
  513. }
  514. async _checkNo(pid, materialId, checkData, times) {
  515. const accountId = this.ctx.session.sessionUser.accountId;
  516. const time = new Date();
  517. // 整理当前流程审核人状态更新
  518. const audits = await this.getAllDataByCondition({ where: { mid: materialId, times, status: auditConst.status.checking } });
  519. if (!audits) throw '审核数据错误';
  520. const selfAudit = audits.find(x => { return x.aid === accountId; });
  521. if (!selfAudit) throw '当前标段您无权审批';
  522. const flowAudits = await this.getAllDataByCondition({ where: { mid: materialId, times: selfAudit.times, order: selfAudit.order }});
  523. const auditors = await this.getUniqAuditor(materialId, times); // 全部参与的审批人
  524. const newAuditors = auditors.map(x => {
  525. return {
  526. aid: x.aid, tid: selfAudit.tid, mid: selfAudit.mid,
  527. times: times + 1, order: x.audit_order, status: auditConst.status.uncheck,
  528. audit_type: x.audit_type, audit_order: x.audit_order,
  529. }
  530. });
  531. const transaction = await this.db.beginTransaction();
  532. try {
  533. const tp_data = await this.getTpData(transaction, materialId);
  534. const updateData = [];
  535. audits.forEach(x => {
  536. updateData.push({
  537. id: x.id,
  538. status: x.aid === selfAudit.aid ? checkData.checkType : auditConst.status.checkSkip,
  539. opinion: x.aid === selfAudit.aid ? checkData.opinion : '',
  540. end_time: x.aid === selfAudit.aid ? time : null,
  541. tp_data: JSON.stringify(tp_data),
  542. });
  543. });
  544. await transaction.updateRows(this.tableName, updateData);
  545. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  546. // 添加到消息推送表
  547. const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, materialId, selfAudit.aid, checkData.opinion);
  548. const records = [{ pid, tid: selfAudit.tid, type: pushType.material, uid: this.ctx.material.user_id, status: auditConst.status.checkNo, content: noticeContent }];
  549. auditors.forEach(audit => {
  550. records.push({ pid, tid: selfAudit.tid, type: pushType.material, uid: audit.aid, status: auditConst.status.checkNo, content: noticeContent });
  551. });
  552. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  553. // 同步期信息
  554. await transaction.update(this.ctx.service.material.tableName, {
  555. id: materialId, status: checkData.checkType,
  556. times: times + 1,
  557. tp_data: JSON.stringify(tp_data),
  558. });
  559. // 拷贝新一次审核流程列表
  560. await transaction.insert(this.tableName, newAuditors);
  561. // 微信模板通知
  562. const begin_audit = await this.getDataByCondition({
  563. mid: materialId,
  564. order: 1,
  565. });
  566. const materialInfo = await this.ctx.service.material.getDataById(materialId);
  567. const material_decimal = materialInfo && materialInfo.decimal ? JSON.parse(materialInfo.decimal) : materialConst.decimal;
  568. const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), materialInfo.user_id));
  569. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/material/' + materialInfo.order);
  570. const wechatData = {
  571. wap_url: shenpiUrl,
  572. qi: materialInfo.order,
  573. status: wxConst.status.back,
  574. tips: wxConst.tips.back,
  575. begin_time: Date.parse(begin_audit.begin_time),
  576. m_tp: this.ctx.helper.add(this.ctx.helper.round(materialInfo.m_tp, material_decimal.tp), this.ctx.helper.round(materialInfo.ex_tp, material_decimal.tp)),
  577. hs_m_tp: this.ctx.helper.add(this.ctx.helper.round(this.ctx.helper.mul(materialInfo.m_tp, 1+materialInfo.rate/100), material_decimal.tp), this.ctx.helper.round(this.ctx.helper.mul(materialInfo.ex_tp, 1+materialInfo.exponent_rate/100), material_decimal.tp)),
  578. };
  579. await this.ctx.helper.sendWechat(users, smsTypeConst.const.TC, smsTypeConst.judge.result.toString(), wxConst.template.material, wechatData);
  580. // 检查三方特殊推送
  581. await this.ctx.service.specMsg.addMaterialMsg(transaction, pid, materialInfo, pushOperate.material.flow);
  582. await transaction.commit();
  583. } catch (err) {
  584. await transaction.rollback();
  585. throw err;
  586. }
  587. }
  588. async _checkNoPre(pid, materialId, checkData, times) {
  589. const accountId = this.ctx.session.sessionUser.accountId;
  590. const time = new Date();
  591. // 整理当前流程审核人状态更新
  592. const audits = await this.getAllDataByCondition({ where: { mid: materialId, times, status: auditConst.status.checking } });
  593. if (audits.length === 0 || audits[0].order <= 1) throw '审核数据错误';
  594. const selfAudit = audits.find(x => { return x.aid === accountId; });
  595. if (!selfAudit) throw '当前标段您无权审批';
  596. const flowAudits = await this.getAllDataByCondition({ where: { mid: materialId, times: selfAudit.times, order: selfAudit.order }});
  597. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  598. const auditors2 = await this.getAuditGroupByList(materialId, times);
  599. const preAuditors = auditors2.filter(x => { return x.audit_order === selfAudit.audit_order - 1});
  600. const transaction = await this.db.beginTransaction();
  601. try {
  602. const tp_data = await this.getTpData(transaction, materialId);
  603. // 添加到消息推送表
  604. const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, materialId, selfAudit.aid, checkData.opinion);
  605. const records = [{ pid, tid: selfAudit.tid, type: pushType.material, uid: this.ctx.material.user_id, status: auditConst.status.checkNoPre, content: noticeContent }];
  606. auditors2.forEach(audit => {
  607. records.push({ pid, tid: selfAudit.tid, type: pushType.material, uid: audit.aid, status: auditConst.status.checkNoPre, content: noticeContent });
  608. });
  609. await transaction.insert('zh_notice', records);
  610. const updateData = [];
  611. for (const audit of audits) {
  612. if (audit.aid === selfAudit.aid) {
  613. updateData.push({
  614. id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time, tp_data: JSON.stringify(tp_data)
  615. });
  616. } else {
  617. updateData.push({
  618. id: audit.id, status: auditConst.status.checkSkip, opinion: '', end_time: null, tp_data: JSON.stringify(tp_data)
  619. });
  620. }
  621. }
  622. await transaction.updateRows(this.tableName, updateData);
  623. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  624. // 顺移其后审核人流程顺序
  625. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE mid = ? AND times = ? AND `order` > ?';
  626. await transaction.query(sql, [materialId, selfAudit.times, selfAudit.order]);
  627. const newAuditors = [];
  628. preAuditors.forEach(x => {
  629. newAuditors.push({
  630. tid: selfAudit.tid, mid: materialId, aid: x.aid,
  631. times: x.times, order: selfAudit.order + 1,
  632. status: auditConst.status.checking, begin_time: time,
  633. audit_type: x.audit_type, audit_order: x.audit_order,
  634. tp_data: JSON.stringify(tp_data),
  635. });
  636. });
  637. const newAuditors_result = await transaction.insert(this.tableName, newAuditors);
  638. // 获取刚批量添加的所有list
  639. for (let j = 0; j < preAuditors.length; j++) {
  640. newAuditors[j].id = newAuditors_result.insertId + j;
  641. }
  642. const newFlowAuditors = [];
  643. flowAudits.forEach(x => {
  644. newFlowAuditors.push({
  645. tid: selfAudit.tid, mid: materialId, aid: x.aid,
  646. times: x.times, order: selfAudit.order + 2,
  647. status: auditConst.status.uncheck,
  648. audit_type: x.audit_type, audit_order: x.audit_order,
  649. });
  650. });
  651. await transaction.insert(this.tableName, newFlowAuditors);
  652. // 微信模板通知
  653. const begin_audit = await this.getDataByCondition({
  654. mid: materialId,
  655. order: 1,
  656. });
  657. const materialInfo = await this.ctx.service.material.getDataById(materialId);
  658. const material_decimal = materialInfo && materialInfo.decimal ? JSON.parse(materialInfo.decimal) : materialConst.decimal;
  659. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/material/' + materialInfo.order);
  660. const wechatData = {
  661. wap_url: shenpiUrl,
  662. qi: materialInfo.order,
  663. status: wxConst.status.check,
  664. tips: wxConst.tips.check,
  665. begin_time: Date.parse(begin_audit.begin_time),
  666. m_tp: this.ctx.helper.add(this.ctx.helper.round(materialInfo.m_tp, material_decimal.tp), this.ctx.helper.round(materialInfo.ex_tp, material_decimal.tp)),
  667. hs_m_tp: this.ctx.helper.add(this.ctx.helper.round(this.ctx.helper.mul(materialInfo.m_tp, 1+materialInfo.rate/100), material_decimal.tp), this.ctx.helper.round(this.ctx.helper.mul(materialInfo.ex_tp, 1+materialInfo.exponent_rate/100), material_decimal.tp)),
  668. };
  669. await this.ctx.helper.sendWechat(this._.map(preAuditors, 'aid'), smsTypeConst.const.TC, smsTypeConst.judge.approval.toString(), wxConst.template.material, wechatData);
  670. for (const a of newAuditors) {
  671. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.TC, {
  672. pid: this.ctx.session.sessionProject.id,
  673. tid: this.ctx.tender.id,
  674. uid: a.aid,
  675. sp_type: 'material',
  676. sp_id: a.id,
  677. table_name: this.tableName,
  678. template: wxConst.template.material,
  679. wx_data: wechatData,
  680. });
  681. }
  682. // 检查三方特殊推送
  683. await this.ctx.service.specMsg.addMaterialMsg(transaction, pid, materialInfo, pushOperate.material.flow);
  684. await transaction.commit();
  685. } catch (error) {
  686. await transaction.rollback();
  687. throw error;
  688. }
  689. }
  690. /**
  691. * 审批
  692. * @param {Number} materialId - 材料调差期id
  693. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  694. * @param {Number} times - 第几次审批
  695. * @return {Promise<void>}
  696. */
  697. async check(materialId, checkData, times = 1) {
  698. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  699. throw '提交数据错误';
  700. }
  701. const pid = this.ctx.session.sessionProject.id;
  702. switch (checkData.checkType) {
  703. case auditConst.status.checked:
  704. await this._checked(pid, materialId, checkData, times);
  705. break;
  706. case auditConst.status.checkNo:
  707. await this._checkNo(pid, materialId, checkData, times);
  708. break;
  709. case auditConst.status.checkNoPre:
  710. await this._checkNoPre(pid, materialId, checkData, times);
  711. break;
  712. default:
  713. throw '无效审批操作';
  714. }
  715. }
  716. /**
  717. * 用于添加推送所需的content内容
  718. * @param {Number} pid 项目id
  719. * @param {Number} tid 台账id
  720. * @param {Number} mid 期id
  721. * @param {Number} uid 审批人id
  722. */
  723. async getNoticeContent(pid, tid, mid, uid, opinion = '') {
  724. const noticeSql = 'SELECT * FROM (SELECT ' +
  725. ' t.`id` As `tid`, ma.`mid`, t.`name`, m.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  726. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  727. ' LEFT JOIN ?? As m On t.`id` = m.`tid` AND m.`id` = ?' +
  728. ' LEFT JOIN ?? As ma ON m.`id` = ma.`mid`' +
  729. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  730. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  731. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.material.tableName, mid, this.tableName, this.ctx.service.projectAccount.tableName, uid, pid];
  732. const content = await this.db.query(noticeSql, noticeSqlParam);
  733. if (content.length) {
  734. content[0].opinion = opinion;
  735. }
  736. return content.length ? JSON.stringify(content[0]) : '';
  737. }
  738. /**
  739. * 审批
  740. * @param {Number} materialId - 材料调差期id
  741. * @param {Number} times - 第几次审批
  742. * @return {Promise<void>}
  743. */
  744. async checkAgain(materialId, times = 1) {
  745. const accountId = this.ctx.session.sessionUser.accountId;
  746. const time = new Date();
  747. // 整理当前流程审核人状态更新
  748. const auditors = await this.getAllDataByCondition({
  749. where: { mid: materialId, times },
  750. orders: [['order', 'asc']],
  751. });
  752. const maxOrder = auditors[auditors.length - 1].order;
  753. const audits = auditors.filter(x => { return x.order === maxOrder});
  754. if (!audits || audits.length === 0 || maxOrder < 1) throw '审核数据错误';
  755. const selfAudit = audits.find(x => { return x.aid === accountId; });
  756. if (!selfAudit) throw '当前标段您无权审批';
  757. const finalAudit = selfAudit || audits[0];
  758. const transaction = await this.db.beginTransaction();
  759. try {
  760. const materialInfo = await this.ctx.service.material.getDataById(materialId);
  761. const material_decimal = materialInfo && materialInfo.decimal ? JSON.parse(materialInfo.decimal) : materialConst.decimal;
  762. const tp_data = await this.getTpData(transaction, materialId, material_decimal);
  763. const checkAgainAuditors = [], checkingAuditors = [];
  764. audits.forEach(x => {
  765. checkAgainAuditors.push({
  766. tid: x.tid, mid: x.mid, aid: x.aid,
  767. times: x.times, order: x.order + 1,
  768. status: !selfAudit || x.aid === selfAudit.aid ? auditConst.status.checkAgain : auditConst.status.checkSkip,
  769. begin_time: time, end_time: time, opinion: '',
  770. audit_type: x.audit_type, audit_order: x.audit_order,
  771. });
  772. });
  773. audits.forEach(x => {
  774. checkingAuditors.push({
  775. tid: x.tid, mid: x.mid, aid: x.aid,
  776. times: x.times, order: x.order + 2,
  777. status: auditConst.status.checking,
  778. begin_time: time, end_time: time, opinion: '',
  779. audit_type: x.audit_type, audit_order: x.audit_order,
  780. tp_data: JSON.stringify(tp_data),
  781. });
  782. });
  783. await transaction.insert(this.tableName, checkAgainAuditors);
  784. const checkingAuditors_result = await transaction.insert(this.tableName, checkingAuditors);
  785. // 获取刚批量添加的所有list
  786. for (let j = 0; j < checkingAuditors.length; j++) {
  787. checkingAuditors[j].id = checkingAuditors_result.insertId + j;
  788. }
  789. // 本期结束
  790. // 同步 期信息
  791. await transaction.update(this.ctx.service.material.tableName, {
  792. id: materialId, status: auditConst.status.checking,
  793. final_auditor_str: '',
  794. });
  795. // 微信模板通知
  796. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/material/' + materialInfo.order);
  797. const wechatData = {
  798. wap_url: shenpiUrl,
  799. qi: materialInfo.order,
  800. status: wxConst.status.check,
  801. tips: wxConst.tips.check,
  802. begin_time: Date.parse(new Date()),
  803. m_tp: this.ctx.helper.add(this.ctx.helper.round(materialInfo.m_tp, material_decimal.tp), this.ctx.helper.round(materialInfo.ex_tp, material_decimal.tp)),
  804. hs_m_tp: this.ctx.helper.add(this.ctx.helper.round(this.ctx.helper.mul(materialInfo.m_tp, 1+materialInfo.rate/100), material_decimal.tp), this.ctx.helper.round(this.ctx.helper.mul(materialInfo.ex_tp, 1+materialInfo.exponent_rate/100), material_decimal.tp)),
  805. };
  806. await this.ctx.helper.sendWechat(this._.map(checkingAuditors, 'aid'), smsTypeConst.const.TC, smsTypeConst.judge.approval.toString(), wxConst.template.material, wechatData);
  807. for (const a of checkingAuditors) {
  808. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.TC, {
  809. pid: this.ctx.session.sessionProject.id,
  810. tid: this.ctx.tender.id,
  811. uid: a.aid,
  812. sp_type: 'material',
  813. sp_id: a.id,
  814. table_name: this.tableName,
  815. template: wxConst.template.material,
  816. wx_data: wechatData,
  817. });
  818. }
  819. // 检查三方特殊推送
  820. await this.ctx.service.specMsg.addMaterialMsg(transaction, this.ctx.session.sessionProject.id, materialInfo, pushOperate.material.flow);
  821. await transaction.commit();
  822. } catch (err) {
  823. await transaction.rollback();
  824. throw err;
  825. }
  826. }
  827. /**
  828. * 获取审核人需要审核的期列表
  829. *
  830. * @param auditorId
  831. * @return {Promise<*>}
  832. */
  833. async getAuditMaterial(auditorId, spid = '') {
  834. const spSql = spid ? ' and t.`spid` = "' + spid + '"' : '';
  835. const sql = 'SELECT ma.`aid`, ma.`times`, ma.`order`, ma.`begin_time`, ma.`end_time`, ma.`tid`, ma.`mid`,' +
  836. ' m.`order` As `morder`, m.`status` As `mstatus`,' +
  837. ' t.`name`, t.`project_id`, t.`type`, t.`user_id`, t.`spid` ' +
  838. ' FROM ?? AS ma' +
  839. ' LEFT JOIN ?? AS m On ma.mid = m.id' +
  840. ' LEFT JOIN ?? As t On m.tid = t.id' +
  841. ' WHERE ((ma.`aid` = ? and ma.`status` = ?) OR (m.`user_id` = ? and ma.`status` = ? and m.`status` = ? and ma.`times` = (m.`times`-1)))' + spSql +
  842. ' ORDER BY ma.`begin_time` DESC';
  843. const sqlParam = [this.tableName, this.ctx.service.material.tableName, this.ctx.service.tender.tableName, auditorId, auditConst.status.checking, auditorId, auditConst.status.checkNo, auditConst.status.checkNo];
  844. const result = await this.db.query(sql, sqlParam);
  845. // 过滤result中存在重复sid的值, 保留最新的一条
  846. const filterResult = [];
  847. const midArr = [];
  848. for (const r of result) {
  849. if (midArr.indexOf(r.mid) === -1) {
  850. filterResult.push(r);
  851. midArr.push(r.mid);
  852. }
  853. }
  854. return filterResult;
  855. }
  856. /**
  857. * 获取审核人审核的次数
  858. *
  859. * @param auditorId
  860. * @return {Promise<*>}
  861. */
  862. async getCountByChecked(auditorId, spid = '') {
  863. if (spid) {
  864. 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, auditConst.status.checkNoPre]) + ') AND t.`spid` = ?';
  865. const sqlParam = [this.tableName, this.ctx.service.tender.tableName, auditorId, spid];
  866. const result = await this.db.queryOne(sql, sqlParam);
  867. return result.count ? result.count : 0;
  868. }
  869. return await this.db.count(this.tableName, { aid: auditorId, status: [auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre] });
  870. }
  871. /**
  872. * 获取最近一次审批结束时间
  873. *
  874. * @param auditorId
  875. * @return {Promise<*>}
  876. */
  877. async getLastEndTimeByChecked(auditorId, spid = '') {
  878. const sqSql = spid ? ' AND t.`spid` = "' + spid + '"' : '';
  879. const sql = 'SELECT a.`end_time` FROM ?? AS a LEFT JOIN ?? AS t ON a.`tid` = t.`id` WHERE a.`aid` = ? ' +
  880. 'AND a.`status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo]) + ')' + sqSql +
  881. ' ORDER BY a.`end_time` DESC';
  882. const sqlParam = [this.tableName, this.ctx.service.tender.tableName, auditorId];
  883. const result = await this.db.queryOne(sql, sqlParam);
  884. return result ? result.end_time : null;
  885. }
  886. /**
  887. * 获取 某时间后 审批进度 更新的期
  888. * @param {Number} pid - 查询标段
  889. * @param {Number} uid - 查询人
  890. * @param {Date} time - 查询时间
  891. * @return {Promise<*>}
  892. */
  893. async getNoticeMaterial(pid, uid, time) {
  894. // const sql = 'SELECT * FROM (SELECT t.`name`, t.`project_id`, t.`type`, t.`user_id`, ' +
  895. // ' m.`order` As `m_order`, m.`status` As `m_status`, ' +
  896. // ' ma.`aid`, ma.`times`, ma.`order`, ma.`end_time`, ma.`tid`, ma.`mid`, ma.`status`, ' +
  897. // ' pa.`name` As `su_name`, pa.role As `su_role`, pa.company As `su_company`' +
  898. // ' FROM (SELECT * FROM ?? WHERE `user_id` = ? OR `id` in (SELECT `tid` FROM ?? WHERE `aid` = ? GROUP BY `tid`)) As t' +
  899. // ' LEFT JOIN ?? As m On t.`id` = m.`tid`' +
  900. // ' LEFT JOIN ?? As ma ON m.`id` = ma.`mid`' +
  901. // ' LEFT JOIN ?? As pa ON ma.`aid` = pa.`id`' +
  902. // ' WHERE ma.`end_time` > ? and t.`project_id` = ?' +
  903. // ' ORDER By ma.`end_time` DESC LIMIT 1000) as new_t GROUP BY new_t.`tid`' +
  904. // ' ORDER BY new_t.`end_time`';
  905. // const sqlParam = [this.ctx.service.tender.tableName, uid, this.tableName, uid, this.ctx.service.material.tableName, this.tableName,
  906. // this.ctx.service.projectAccount.tableName, time, pid];
  907. // return await this.db.query(sql, sqlParam);
  908. let notice = await this.db.select('zh_notice', {
  909. where: { pid, type: pushType.material, uid },
  910. orders: [['create_time', 'desc']],
  911. limit: 10, offset: 0,
  912. });
  913. notice = notice.map(v => {
  914. const extra = JSON.parse(v.content);
  915. delete v.content;
  916. return { ...v, ...extra };
  917. });
  918. return notice;
  919. }
  920. /**
  921. * 获取审核人流程列表
  922. *
  923. * @param auditorId
  924. * @return {Promise<*>}
  925. */
  926. async getAuditGroupByList(materialId, times) {
  927. // const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`tid`, la.`mid`, la.`aid`, la.`order`, la.`status`, la.audit_type, la.audit_order ' +
  928. // ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  929. // ' WHERE la.`mid` = ? and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
  930. // const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, times];
  931. const sql =
  932. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`order`, la.`status`, la.audit_type, la.audit_order ' +
  933. ' FROM (SELECT `aid`, max(`order`) `order` FROM ?? WHERE `mid` = ? and `times` = ? and `is_old` = ? GROUP BY aid) sa' +
  934. ' LEFT JOIN ?? la ON sa.`aid` = la.`aid` AND sa.`order` = la.`order`' +
  935. ' Left JOIN ?? AS pa On la.`aid` = pa.`id` WHERE la.`mid` = ? and la.`times` = ? and la.`is_old` = ? order BY la.`order`';
  936. const sqlParam = [this.tableName, materialId, times, 0, this.tableName, this.ctx.service.projectAccount.tableName, materialId, times, 0];
  937. return await this.db.query(sql, sqlParam);
  938. }
  939. /**
  940. * 获取审核人流程列表(包括原报)
  941. * @param {Number} materialId 调差id
  942. * @param {Number} times 审核次数
  943. * @return {Promise<Array>} 查询结果集(包括原报)
  944. */
  945. async getAuditorsWithOwner(materialId, times = 1) {
  946. const result = await this.getAuditGroupByList(materialId, times);
  947. const sql =
  948. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As mid, 0 As `order`' +
  949. ' FROM ' +
  950. this.ctx.service.material.tableName +
  951. ' As s' +
  952. ' LEFT JOIN ' +
  953. this.ctx.service.projectAccount.tableName +
  954. ' As pa' +
  955. ' ON s.user_id = pa.id' +
  956. ' WHERE s.id = ?';
  957. const sqlParam = [times, materialId, materialId];
  958. const user = await this.db.queryOne(sql, sqlParam);
  959. result.unshift(user);
  960. return result;
  961. }
  962. /**
  963. * 获取tender下所有的审核列表(报表用)
  964. * @param {Number} tenderId
  965. */
  966. async getAuditorsByTender(tenderId) {
  967. const sql = 'SELECT la.`tid`, la.`mid`, la.`aid`, la.`order`, la.`times`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`' +
  968. ' FROM ?? AS la ' +
  969. ' WHERE la.`tid` = ?';
  970. const sqlParam = [this.tableName, tenderId];
  971. return await this.db.query(sql, sqlParam);
  972. }
  973. /**
  974. * 复制上一期的审批人列表给最新一期
  975. *
  976. * @param transaction - 新增一期的事务
  977. * @param {Object} preMaterial - 上一期
  978. * @param {Object} newaMaterial - 最新一期
  979. * @return {Promise<*>}
  980. */
  981. async copyPreMaterialAuditors(transaction, preMaterial, newMaterial) {
  982. const auditors = await this.getAuditGroupByList(preMaterial.id, preMaterial.times);
  983. const newAuditors = [];
  984. for (const a of auditors) {
  985. const na = {
  986. tid: preMaterial.tid,
  987. mid: newMaterial.id,
  988. aid: a.aid,
  989. times: newMaterial.times,
  990. order: a.audit_order,
  991. status: auditConst.status.uncheck,
  992. audit_type: a.audit_type,
  993. audit_order: a.audit_order,
  994. };
  995. newAuditors.push(na);
  996. }
  997. if (newAuditors.length > 0) {
  998. const result = await transaction.insert(this.tableName, newAuditors);
  999. return (result.effectRows = auditors.length);
  1000. } else {
  1001. return true;
  1002. }
  1003. }
  1004. /**
  1005. * 移除审核人
  1006. *
  1007. * @param {Number} materialId - 材料调差期id
  1008. * @param {Number} status - 期状态
  1009. * @param {Number} status - 期次数
  1010. * @return {Promise<boolean>}
  1011. */
  1012. async getAuditorByStatus(materialId, status, times = 1) {
  1013. let auditor = null;
  1014. let sql = '';
  1015. let sqlParam = '';
  1016. switch (status) {
  1017. case auditConst.status.checking :
  1018. case auditConst.status.checked :
  1019. case auditConst.status.checkNoPre :
  1020. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  1021. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1022. ' WHERE la.`mid` = ? and la.`status` = ? ' +
  1023. ' ORDER BY la.`times` desc, la.`order` desc';
  1024. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, status];
  1025. auditor = await this.db.queryOne(sql, sqlParam);
  1026. break;
  1027. case auditConst.status.checkNo :
  1028. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  1029. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  1030. ' WHERE la.`mid` = ? and la.`status` = ? and la.`times` = ?' +
  1031. ' ORDER BY la.`times` desc, la.`order` desc';
  1032. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, auditConst.status.checkNo, parseInt(times) - 1];
  1033. auditor = await this.db.queryOne(sql, sqlParam);
  1034. break;
  1035. case auditConst.status.uncheck :
  1036. default:break;
  1037. }
  1038. return auditor;
  1039. }
  1040. async getAuditorsByStatus(materialId, status, times = 1) {
  1041. let auditor = [];
  1042. let sql = '';
  1043. let sqlParam = '';
  1044. let cur;
  1045. switch (status) {
  1046. case auditConst.status.checking:
  1047. case auditConst.status.checked:
  1048. case auditConst.status.checkNoPre:
  1049. cur = await this.db.queryOne(`SELECT * From ${this.tableName} where mid = ? AND times = ? AND status = ? ORDER By times DESC, ` + '`order` DESC', [materialId, times, status]);
  1050. if (!cur) return [];
  1051. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`order`, la.`status`, la.audit_order, la.audit_type ' +
  1052. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1053. ' WHERE la.`mid` = ? and la.`order` = ? and times = ?';
  1054. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, cur.order, times];
  1055. auditor = await this.db.query(sql, sqlParam);
  1056. break;
  1057. case auditConst.status.checkNo:
  1058. cur = await this.db.queryOne(`SELECT * From ${this.tableName} where mid = ? AND times = ? AND status = ? ORDER By times DESC, ` + '`order` DESC', [materialId, parseInt(times) - 1, status]);
  1059. if (!cur) return [];
  1060. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`order`, la.`status`, la.audit_order, la.audit_type ' +
  1061. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1062. ' WHERE la.`mid` = ? and la.`order` = ? and la.`times` = ?';
  1063. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, cur.order, parseInt(times) - 1];
  1064. auditor = await this.db.query(sql, sqlParam);
  1065. break;
  1066. case auditConst.status.uncheck:
  1067. default:
  1068. break;
  1069. }
  1070. return auditor;
  1071. }
  1072. async getAllAuditors(tenderId) {
  1073. const sql = 'SELECT ma.aid, ma.tid FROM ' + this.tableName + ' ma' +
  1074. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On ma.tid = t.id' +
  1075. ' WHERE t.id = ?' +
  1076. ' GROUP BY ma.aid';
  1077. const sqlParam = [tenderId];
  1078. return this.db.query(sql, sqlParam);
  1079. }
  1080. /**
  1081. * 取待审批期列表(wap用)
  1082. *
  1083. * @param auditorId
  1084. * @return {Promise<*>}
  1085. */
  1086. async getAuditMaterialByWap(auditorId) {
  1087. const sql =
  1088. 'SELECT sa.`aid`, sa.`times`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`mid`,' +
  1089. // ' s.`order` As `sorder`, s.`status` As `sstatus`, s.`s_time`, s.`contract_tp`, s.`qc_tp`, s.`pre_contract_tp`, s.`pre_qc_tp`, s.`yf_tp`, s.`pre_yf_tp`, ' +
  1090. ' s.*,' +
  1091. ' t.`name`, t.`project_id`, t.`type`, t.`user_id`, t.`spid`,' +
  1092. ' ti.`deal_info` ' +
  1093. ' FROM ?? AS sa' +
  1094. ' Left Join ?? AS s On sa.`mid` = s.`id`' +
  1095. ' Left Join ?? As t On sa.`tid` = t.`id`' +
  1096. ' Left Join ?? AS ti ON ti.`tid` = t.`id`' +
  1097. ' WHERE sa.`aid` = ? and sa.`status` = ?';
  1098. const sqlParam = [
  1099. this.tableName,
  1100. this.ctx.service.material.tableName,
  1101. this.ctx.service.tender.tableName,
  1102. this.ctx.service.tenderInfo.tableName,
  1103. auditorId,
  1104. auditConst.status.checking,
  1105. ];
  1106. return await this.db.query(sql, sqlParam);
  1107. }
  1108. async updateNewAuditList(material, newList) {
  1109. const transaction = await this.db.beginTransaction();
  1110. try {
  1111. // 先删除旧的审批流,再添加新的
  1112. await transaction.delete(this.tableName, { mid: material.id, times: material.times });
  1113. const newAuditors = [];
  1114. for (const auditor of newList) {
  1115. newAuditors.push({
  1116. tid: material.tid, mid: material.id, aid: auditor.audit_id,
  1117. times: material.times, order: auditor.audit_order, status: auditConst.status.uncheck,
  1118. audit_type: auditor.audit_type, audit_order: auditor.audit_order,
  1119. });
  1120. }
  1121. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  1122. await transaction.commit();
  1123. } catch (err) {
  1124. await transaction.rollback();
  1125. throw err;
  1126. }
  1127. }
  1128. async updateLastAudit(material, auditList, lastId) {
  1129. const transaction = await this.db.beginTransaction();
  1130. try {
  1131. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  1132. const existAudit = auditList.find(x => { return x.aid === lastId });
  1133. let order = auditList.length > 0 ? auditList.reduce((rst, a) => { return Math.max(rst, a.order)}, 0) + 1 : 1; // 最大值 + 1
  1134. // const idList = this._.map(auditList, 'aid');
  1135. // let order = idList.length + 1;
  1136. if (existAudit) {
  1137. await transaction.delete(this.tableName, {mid: material.id, times: material.times, aid: lastId});
  1138. const sameOrder = auditList.filter(x => { return x.order === existAudit.order });
  1139. if (sameOrder.length === 1) {
  1140. const updateData = [];
  1141. auditList.forEach(x => {
  1142. if (x.order <= existAudit.order) return;
  1143. updateData.push({id: x.id, order: x.order - 1, audit_order: x.audit_order - 1});
  1144. });
  1145. if (updateData.length > 0) {
  1146. await transaction.updateRows(updateData);
  1147. }
  1148. order = order - 1;
  1149. }
  1150. }
  1151. // 添加终审
  1152. const newAuditor = {
  1153. tid: material.tid, mid: material.id, aid: lastId,
  1154. times: material.times, order, status: auditConst.status.uncheck,
  1155. audit_type: auditType.key.common, audit_order: order,
  1156. };
  1157. await transaction.insert(this.tableName, newAuditor);
  1158. await transaction.commit();
  1159. } catch (err) {
  1160. await transaction.rollback();
  1161. throw err;
  1162. }
  1163. }
  1164. async getNumByMonth(tid, startMonth, endMonth) {
  1165. const sql = 'SELECT COUNT(*) as num FROM ?? t1 JOIN (SELECT MAX(id) as max_id FROM ?? WHERE tid = ? GROUP BY mid) t2 ON t1.id = t2.max_id WHERE t1.status = ? AND t1.end_time between ? and ?';
  1166. // const sql = 'SELECT COUNT(*) as num FROM ?? WHERE id in (SELECT MAX(id) FROM ?? WHERE tid = ? GROUP BY mid) AND status = ? AND end_time between ? and ?';
  1167. const sqlParam = [this.tableName, this.tableName, tid, auditConst.status.checked, startMonth, endMonth];
  1168. const result = await this.db.queryOne(sql, sqlParam);
  1169. return result ? result.num : 0;
  1170. }
  1171. /**
  1172. * 删除本次审批流程
  1173. * @param {Number} materialId - 调差id
  1174. * @param {Number} times - 第几次审批
  1175. * @param {Object} data - 更改参数
  1176. * @return {Promise<void>}
  1177. */
  1178. async saveAudit(materialId, times, sp_group, data) {
  1179. const transaction = await this.db.beginTransaction();
  1180. try {
  1181. const auditors = await this.getAuditGroupByList(materialId, times);
  1182. const now_audit = this._.find(auditors, { aid: data.old_aid });
  1183. if (data.operate !== 'del') {
  1184. const exist = await this.getDataByCondition({ mid: materialId, times, aid: data.new_aid, is_old: 0 });
  1185. if (exist) throw '该审核人已存在,请勿重复添加';
  1186. }
  1187. if (data.operate === 'add') {
  1188. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  1189. throw '当前人下无法操作新增';
  1190. }
  1191. const newAudit = {
  1192. tid: this.ctx.tender.id,
  1193. mid: materialId,
  1194. aid: data.new_aid,
  1195. order: now_audit.order + 1,
  1196. audit_order: now_audit.audit_order + 1,
  1197. audit_type: auditType.key.common,
  1198. times: times,
  1199. status: 1
  1200. };
  1201. // order+1
  1202. await this._syncOrderByDelete(transaction, materialId, now_audit.order+1, times, '+');
  1203. await transaction.insert(this.tableName, newAudit);
  1204. // 更新审批流程页数据,如果存在
  1205. } else if (data.operate === 'add-sibling') {
  1206. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  1207. throw '当前人下无法操作新增';
  1208. }
  1209. const newAudit = {
  1210. tid: this.ctx.tender.id,
  1211. mid: materialId,
  1212. aid: data.new_aid,
  1213. order: now_audit.order,
  1214. audit_order: now_audit.audit_order,
  1215. audit_type: now_audit.audit_type,
  1216. times: times,
  1217. status: 1
  1218. };
  1219. await transaction.insert(this.tableName, newAudit);
  1220. } else if (data.operate === 'del') {
  1221. if (now_audit.status !== auditConst.status.uncheck) {
  1222. throw '当前人无法操作删除';
  1223. }
  1224. const flowAuditors = auditors.filter(x => { return x.order === now_audit.order; });
  1225. await transaction.delete(this.tableName, { mid: materialId, times, aid: now_audit.aid, order: now_audit.order });
  1226. if (flowAuditors.length === 1) await this._syncOrderByDelete(transaction, materialId, now_audit.order, times);
  1227. // 旧的更新为is_old为1
  1228. await transaction.update(this.tableName, { is_old: 1 }, {
  1229. where: {
  1230. mid: materialId,
  1231. times,
  1232. aid: data.old_aid,
  1233. }
  1234. });
  1235. } else if (data.operate === 'change') {
  1236. const nowAudit = await this.getDataByCondition({ mid: materialId, times, aid: now_audit.aid, order: now_audit.order });
  1237. if (now_audit.status !== auditConst.status.uncheck || !nowAudit) {
  1238. throw '当前人无法操作替换';
  1239. }
  1240. nowAudit.aid = data.new_aid;
  1241. await transaction.update(this.tableName, nowAudit);
  1242. // 旧的更新为is_old为1
  1243. await transaction.update(this.tableName, { is_old: 1 }, {
  1244. where: {
  1245. mid: materialId,
  1246. times,
  1247. aid: data.old_aid,
  1248. }
  1249. });
  1250. }
  1251. if (this.ctx.tender.info.shenpi.material === shenpiConst.sp_status.gdspl) {
  1252. const newAuditors = await transaction.select(this.tableName, { where: { mid: materialId, times } });
  1253. const newAuditorGroup = this.ctx.helper.groupAuditors(newAuditors.filter(x => { return x.is_old === 0; }));
  1254. const uniqNewAuditorGroup = this.ctx.helper.groupAuditorsUniq(newAuditorGroup);
  1255. await this.ctx.service.shenpiAudit.updateAuditListWithAuditType(transaction, this.ctx.tender.id, this.ctx.tender.info.shenpi.material, shenpiConst.sp_type.material, uniqNewAuditorGroup, sp_group);
  1256. } else if (this.ctx.tender.info.shenpi.material === shenpiConst.sp_status.gdzs) {
  1257. const newAuditors = await this.getAuditGroupByList(materialId, times, transaction);
  1258. await this.ctx.service.shenpiAudit.updateAuditList(transaction, this.ctx.tender.id, this.ctx.tender.info.shenpi.material, shenpiConst.sp_type.material, this._.map(newAuditors, 'aid'));
  1259. }
  1260. // 更新到审批流程方法
  1261. await transaction.commit();
  1262. } catch (err) {
  1263. await transaction.rollback();
  1264. throw err;
  1265. }
  1266. }
  1267. async getAuditorGroup(materialId, times) {
  1268. const auditors = await this.getAuditors(materialId, times); // 全部参与的审批人
  1269. const newAuditors = auditors.filter(x => { return x.is_old === 0; });
  1270. return this.ctx.helper.groupAuditors(newAuditors);
  1271. }
  1272. async getUserGroup(materialId, times) {
  1273. const group = await this.getAuditorGroup(materialId, times);
  1274. const sql =
  1275. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As mid, 0 As `order`, 1 As audit_type, 0 As audit_order' +
  1276. ' FROM ' + this.ctx.service.material.tableName + ' As m' +
  1277. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  1278. ' ON m.user_id = pa.id' +
  1279. ' WHERE m.id = ?';
  1280. const sqlParam = [times, materialId, materialId];
  1281. const user = await this.db.queryOne(sql, sqlParam);
  1282. group.unshift([ user ]);
  1283. return group;
  1284. }
  1285. async getUniqUserGroup(materialId, times) {
  1286. const group = await this.getAuditorGroup(materialId, times);
  1287. const sql =
  1288. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As mid, 0 As `order`, 1 As audit_type, 0 As audit_order' +
  1289. ' FROM ' + this.ctx.service.material.tableName + ' As m' +
  1290. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  1291. ' ON m.user_id = pa.id' +
  1292. ' WHERE m.id = ?';
  1293. const sqlParam = [times, materialId, materialId];
  1294. const user = await this.db.queryOne(sql, sqlParam);
  1295. user.audit_order = 0;
  1296. group.unshift([ user ]);
  1297. return this.ctx.helper.groupAuditorsUniq(group);
  1298. }
  1299. async getAuditorHistory(materialId, times, reverse = false) {
  1300. const history = [];
  1301. if (times >= 1) {
  1302. for (let i = 1; i <= times; i++) {
  1303. const auditors = await this.getAuditors(materialId, i);
  1304. const group = this.ctx.helper.groupAuditors(auditors);
  1305. const historyGroup = [];
  1306. const max_order = group.length > 0 && group[group.length - 1].length > 0 ? group[group.length - 1][0].audit_order : -1;
  1307. for (const g of group) {
  1308. const his = {
  1309. beginYear: '', beginDate: '', beginTime: '', endYear: '', endDate: '', endTime: '', begin_time: null, end_time: null,
  1310. audit_type: g[0].audit_type, audit_order: g[0].audit_order,
  1311. auditors: g
  1312. };
  1313. if (his.audit_type === auditType.key.common) {
  1314. his.name = g[0].name;
  1315. } else {
  1316. his.name = this.ctx.helper.transFormToChinese(his.audit_order) + '审';
  1317. }
  1318. his.is_final = his.audit_order === max_order;
  1319. if (g[0].begin_time) {
  1320. his.begin_time = g[0].begin_time;
  1321. const beginTime = this.ctx.moment(g[0].begin_time);
  1322. his.beginYear = beginTime.format('YYYY');
  1323. his.beginDate = beginTime.format('MM-DD');
  1324. his.beginTime = beginTime.format('HH:mm:ss');
  1325. }
  1326. let end_time;
  1327. g.forEach(x => {
  1328. if (x.status === auditConst.status.checkSkip) return;
  1329. if (!his.status || x.status === auditConst.status.checking) his.status = x.status;
  1330. if (x.end_time && (!end_time || x.end_time > end_time)) {
  1331. end_time = x.end_time;
  1332. if (his.status !== auditConst.status.checking) his.status = x.status;
  1333. }
  1334. });
  1335. if (end_time) {
  1336. his.end_time = end_time;
  1337. const endTime = this.ctx.moment(end_time);
  1338. his.endYear = endTime.format('YYYY');
  1339. his.endDate = endTime.format('MM-DD');
  1340. his.endTime = endTime.format('HH:mm:ss');
  1341. }
  1342. historyGroup.push(his);
  1343. }
  1344. if (reverse) {
  1345. history.push(historyGroup.reverse());
  1346. } else {
  1347. history.push(historyGroup);
  1348. }
  1349. }
  1350. }
  1351. return history;
  1352. }
  1353. async getUniqAuditor(materialId, times) {
  1354. const auditors = await this.getAuditors(materialId, times); // 全部参与的审批人
  1355. const result = [];
  1356. auditors.forEach(x => {
  1357. if (result.findIndex(r => { return x.aid === r.aid && x.audit_order === r.audit_order; }) < 0) {
  1358. result.push(x);
  1359. }
  1360. });
  1361. return result;
  1362. }
  1363. /**
  1364. * 获取审核人已经审核过的审批信息(包括退回,通过,重新审批等)
  1365. *
  1366. * @param auditorId
  1367. * @return {Promise<*>}
  1368. */
  1369. async getDonesByAudit(auditorId, spid = '') {
  1370. const spSql = spid ? ' and t.`spid` = "' + spid + '"' : '';
  1371. const status = [auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre, auditConst.status.checkAgain];
  1372. const sql =
  1373. 'SELECT la.`status`, la.`end_time` as `shenpi_time`, la.`mid`, t.`id`, m.`order` As `morder`, t.`name`, t.`spid` ' +
  1374. ' FROM ?? AS la Left Join ?? AS t ON la.`tid` = t.`id` LEFT JOIN ?? AS m ON la.`mid` = m.`id`' +
  1375. ' WHERE la.`aid` = ? AND la.`status` in (' + this.ctx.helper.getInArrStrSqlFilter(status) +')' + spSql +
  1376. ' ORDER BY la.`end_time` DESC';
  1377. const sqlParam = [
  1378. this.tableName,
  1379. this.ctx.service.tender.tableName,
  1380. this.ctx.service.material.tableName,
  1381. auditorId,
  1382. ];
  1383. return await this.db.query(sql, sqlParam);
  1384. }
  1385. }
  1386. return MaterialAudit;
  1387. };