change_controller.js 174 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/8/14
  7. * @version
  8. */
  9. const stdDataAddType = {
  10. withParent: 1,
  11. child: 2,
  12. next: 3,
  13. };
  14. const moment = require('moment');
  15. const sendToWormhole = require('stream-wormhole');
  16. const fs = require('fs');
  17. const path = require('path');
  18. const audit = require('../const/audit');
  19. const codeRuleConst = require('../const/code_rule');
  20. const changeConst = require('../const/change');
  21. const accountGroup = require('../const/account_group').group;
  22. const shenpiConst = require('../const/shenpi');
  23. const tenderMenu = require('../../config/menu').tenderMenu;
  24. const measureType = require('../const/tender').measureType;
  25. const spreadConst = require('../const/spread');
  26. // const tenderMenu = require('../../config/menu').tenderMenu;
  27. module.exports = app => {
  28. class ChangeController extends app.BaseController {
  29. /**
  30. * 构造函数
  31. *
  32. * @param {Object} ctx - egg全局变量
  33. * @return {void}
  34. */
  35. constructor(ctx) {
  36. super(ctx);
  37. ctx.showProject = true;
  38. ctx.showTender = true;
  39. ctx.showTitle = true;
  40. }
  41. async _filterChanges(ctx, status = 0) {
  42. const tenderId = ctx.params.id;
  43. ctx.session.sessionUser.tenderId = tenderId;
  44. const tender = await this.service.tender.getDataById(tenderId);
  45. // const tenderList = await this.service.tender.getList();
  46. const page = ctx.page;
  47. const sorts = ctx.query.sort ? ctx.query.sort : 0;
  48. const orders = ctx.query.order ? ctx.query.order : 0;
  49. const changes = await ctx.service.change.getListByStatus(tender.id, status, 1, sorts, orders);
  50. const total = await ctx.service.change.getCountByStatus(tender.id, status);
  51. let page_total = 0;
  52. const tp = await ctx.service.change.getTp(tender.id, status);
  53. if (changes !== null) {
  54. let i = 0;
  55. for (const c of changes) {
  56. page_total = ctx.helper.add(page_total, c.total_price);
  57. const status = c.status === audit.flow.status.uncheck ? 0 : 1;
  58. // 根据审批人对当前变更令的状态取不同的展示方式。
  59. let changeAudit = '';
  60. let auditStatus = 0;
  61. switch (c.status) {
  62. case 1:
  63. auditStatus = 1;
  64. break;
  65. case 2:
  66. changeAudit = await ctx.service.changeAudit.getLastUser(c.cid, c.times, status);
  67. auditStatus = changeAudit.uid === ctx.session.sessionUser.accountId ? 1 : 0;
  68. break;
  69. case 3:
  70. case 4:
  71. auditStatus = 0;
  72. changeAudit = await ctx.service.changeAudit.getLastUser(c.cid, c.times, status);
  73. break;
  74. case 5:
  75. changeAudit = await ctx.service.changeAudit.getLastUser(c.cid, c.times - 1, status);
  76. auditStatus = c.uid === ctx.session.sessionUser.accountId ? 1 : 0;
  77. const back_changeUsedData = await ctx.service.stageChange.getFinalUsedData(ctx.tender.id, c.cid);
  78. c.stageChangeNum = this.ctx.helper.sum(back_changeUsedData.map(x => { return Math.abs(x.used_qty); }));
  79. break;
  80. case 6:
  81. changeAudit = await ctx.service.changeAudit.getLastBackUser(c.cid, c.times);
  82. const checkingAudit = await ctx.service.changeAudit.getLastUser(c.cid, c.times, status);
  83. auditStatus = checkingAudit.uid === ctx.session.sessionUser.accountId ? 1 : 0;
  84. break;
  85. case 9:
  86. auditStatus = 9;
  87. const changeUsedData = await ctx.service.stageChange.getFinalUsedData(ctx.tender.id, c.cid);
  88. c.stageChangeNum = this.ctx.helper.sum(changeUsedData.map(x => { return Math.abs(x.used_qty); }));
  89. break;
  90. default:
  91. break;
  92. }
  93. changes[i].changeAudit = changeAudit;
  94. changes[i].auditStatus = auditStatus;
  95. i++;
  96. }
  97. }
  98. // 分页相关
  99. const pageInfo = {
  100. page,
  101. total: Math.ceil(total / app.config.pageSize),
  102. queryData: JSON.stringify(ctx.urlInfo.query),
  103. };
  104. const filter = JSON.parse(JSON.stringify(audit.filter));
  105. filter.count = [];
  106. filter.count[filter.status.pending] = await ctx.service.change.getCountByStatus(tender.id, filter.status.pending);// await ctx.service.change.pendingDatas(tender.id, ctx.session.sessionUser.accountId);
  107. filter.count[filter.status.uncheck] = await ctx.service.change.getCountByStatus(tender.id, filter.status.uncheck);// await ctx.service.change.checkingDatas(tender.id, ctx.session.sessionUser.accountId);
  108. filter.count[filter.status.checking] = await ctx.service.change.getCountByStatus(tender.id, filter.status.checking);// await ctx.service.change.checkedDatas(tender.id, ctx.session.sessionUser.accountId);
  109. filter.count[filter.status.checked] = await ctx.service.change.getCountByStatus(tender.id, filter.status.checked);// await ctx.service.change.pendingDatas(tender.id, ctx.session.sessionUser.accountId);
  110. filter.count[filter.status.checkNo] = await ctx.service.change.getCountByStatus(tender.id, filter.status.checkNo);// await ctx.service.change.pendingDatas(tender.id, ctx.session.sessionUser.accountId);
  111. const codeRule = tender.c_rule ? JSON.parse(tender.c_rule) : [];
  112. for (const rule of codeRule) {
  113. switch (rule.rule_type) {
  114. case codeRuleConst.measure.ruleType.dealCode:
  115. rule.preview = ctx.tender.info.deal_info.dealCode;
  116. break;
  117. case codeRuleConst.measure.ruleType.tenderName:
  118. rule.preview = tender.name;
  119. break;
  120. case codeRuleConst.measure.ruleType.inDate:
  121. rule.preview = moment().format('YYYY');
  122. break;
  123. case codeRuleConst.measure.ruleType.text:
  124. rule.preview = rule.text;
  125. break;
  126. case codeRuleConst.measure.ruleType.addNo:
  127. const s = '0000000000';
  128. rule.preview = s.substr(s.length - rule.format);
  129. break;
  130. default: break;
  131. }
  132. }
  133. const renderData = {
  134. uid: ctx.session.sessionUser.accountId,
  135. moment,
  136. tender,
  137. // tenderList,
  138. pageInfo,
  139. changes,
  140. page_total,
  141. tp,
  142. filter,
  143. status,
  144. codeRule,
  145. dealCode: ctx.tender.info.deal_info.dealCode,
  146. auditConst: audit.flow,
  147. changeConst,
  148. ruleType: codeRuleConst.ruleType.change,
  149. ruleConst: codeRuleConst.measure,
  150. tenderMenu: this.menu.tenderMenu,
  151. preUrl: '/tender/' + tenderId,
  152. tpUnit: ctx.tender.info.decimal.tp,
  153. };
  154. await this.layout('change/index.ejs', renderData, 'change/modal.ejs');
  155. }
  156. /**
  157. * 变更管理 页面 (Get)
  158. *
  159. * @param {Object} ctx - egg全局变量
  160. * @return {void}
  161. */
  162. async index(ctx) {
  163. try {
  164. await this._filterChanges(ctx);
  165. } catch (err) {
  166. this.log(err);
  167. ctx.redirect('/dashboard');
  168. }
  169. }
  170. /**
  171. *
  172. * @param {Object} ctx - egg全局变量
  173. * @return {void}
  174. */
  175. async newCode(ctx) {
  176. const responseData = {
  177. err: 0,
  178. msg: '',
  179. data: '',
  180. };
  181. try {
  182. const tenderId = ctx.params.id;
  183. if (!tenderId) {
  184. throw '当前未打开标段';
  185. }
  186. const tenderData = await ctx.service.tender.getDataById(tenderId);
  187. const data = JSON.parse(ctx.request.body.data);
  188. let cCodeRule;
  189. let cConnector;
  190. let changeCount = 0;
  191. if (data && data.type) {
  192. const c_code_rules = tenderData.c_code_rules !== null ? JSON.parse(tenderData.c_code_rules) : null;
  193. cCodeRule = c_code_rules && c_code_rules[data.type + '_rule'] ? c_code_rules[data.type + '_rule'] : [];
  194. cConnector = c_code_rules && c_code_rules[data.type + '_connector'] ? c_code_rules[data.type + '_connector'] : null;
  195. if (data.type === codeRuleConst.ruleField[codeRuleConst.ruleType.apply]) {
  196. changeCount = await ctx.service.changeApply.count({ tid: tenderId });
  197. } else {
  198. changeCount = await ctx.service.changeProject.count({ tid: tenderId, type: codeRuleConst.ruleType[data.type] });
  199. }
  200. } else {
  201. cCodeRule = tenderData.c_rule !== null ? JSON.parse(tenderData.c_rule) : [];
  202. cConnector = tenderData.c_connector;
  203. changeCount = await ctx.service.change.count({ tid: tenderId });
  204. }
  205. const code = [];
  206. for (const rule of cCodeRule) {
  207. switch (rule.rule_type) {
  208. case codeRuleConst.measure.ruleType.dealCode:
  209. code.push(ctx.tender.info.deal_info.dealCode);
  210. break;
  211. case codeRuleConst.measure.ruleType.tenderName:
  212. code.push(tenderData.name);
  213. break;
  214. case codeRuleConst.measure.ruleType.text:
  215. code.push(rule.text);
  216. break;
  217. case codeRuleConst.measure.ruleType.inDate:
  218. code.push(moment().format('YYYY'));
  219. break;
  220. case codeRuleConst.measure.ruleType.addNo:
  221. let s = '0000000000';
  222. const count = rule.start + changeCount;
  223. s = s + count;
  224. code.push(s.substr(s.length - rule.format));
  225. break;
  226. default: break;
  227. }
  228. }
  229. responseData.data = code.join(cConnector !== null && cConnector !== 3 ? codeRuleConst.measure.connectorString[cConnector] : '');
  230. } catch (err) {
  231. responseData.err = 1;
  232. responseData.msg = err;
  233. }
  234. ctx.body = responseData;
  235. }
  236. /**
  237. * 新增变更 (Post)
  238. *
  239. * @param {Object} ctx - egg全局变量
  240. * @return {void}
  241. */
  242. async add(ctx) {
  243. try {
  244. const tenderId = ctx.params.id;
  245. if (!tenderId) {
  246. throw '当前未打开标段';
  247. }
  248. const data = JSON.parse(ctx.request.body.data);
  249. if (!data.code || data.code === '' || !data.name || data.name === '') {
  250. throw '变更令号不能为空';
  251. }
  252. const change = await ctx.service.change.add(tenderId, ctx.session.sessionUser.accountId, data.code, data.name);
  253. ctx.body = { err: 0, msg: '', data: change };
  254. } catch (err) {
  255. this.log(err);
  256. ctx.body = { err: 1, msg: err.toString() };
  257. }
  258. }
  259. /**
  260. * 变更管理 状态筛选 页面 (Get)
  261. * @param {Object} ctx - egg全局变量
  262. * @return {void}
  263. */
  264. async status(ctx) {
  265. try {
  266. const status = parseInt(ctx.params.status);
  267. await this._filterChanges(ctx, status);
  268. } catch (err) {
  269. this.logger.error(err);
  270. ctx.redirect('/tender/' + ctx.params.id + '/change');
  271. }
  272. }
  273. /**
  274. * 变更信息 页面 (Get)
  275. *
  276. * @param {Object} ctx - egg全局变量
  277. * @return {void}
  278. */
  279. async info(ctx) {
  280. try {
  281. const whiteList = this.ctx.app.config.multipart.whitelist;
  282. const tenderid = ctx.params.id !== undefined ? ctx.params.id : ctx.session.sessionUser.tenderId;
  283. ctx.session.sessionUser.tenderId = tenderid;
  284. const tender = await ctx.service.tender.getDataById(tenderid);
  285. const change = await ctx.service.change.getDataByCondition({ cid: ctx.params.cid });
  286. // 后台判断当前人查看info状态
  287. const auditStatus = await ctx.service.changeAudit.getStatusByChange(change);
  288. // 获取附件列表
  289. const attList = await ctx.service.changeAtt.getChangeAttachment(ctx.params.cid);
  290. // 获取其他变更令数据
  291. const othersChange = await ctx.service.change.getOthersChange(ctx.params.id, ctx.params.cid);
  292. // 根据auditStatus获取审批人列表
  293. const auditList = await ctx.service.changeAudit.getListByStatus(change, auditStatus);
  294. // 获取已选清单
  295. let changeList = await ctx.service.changeAuditList.getAllDataByCondition({ where: { cid: ctx.params.cid } });
  296. // 获取用户人验证手机号
  297. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  298. const auth_mobile = pa.auth_mobile;
  299. const renderData = {
  300. uid: ctx.session.sessionUser.accountId,
  301. tender,
  302. change,
  303. othersChange,
  304. changeConst,
  305. auditStatus,
  306. auditConst: audit.flow,
  307. ledgerConsts: audit.ledger.status,
  308. attList,
  309. whiteList,
  310. auditList,
  311. changeList,
  312. tpUnit: change.tp_decimal ? change.tp_decimal : ctx.tender.info.decimal.tp,
  313. upUnit: change.up_decimal ? change.up_decimal : ctx.tender.info.decimal.up,
  314. authMobile: auth_mobile,
  315. shenpiConst,
  316. };
  317. // 根据auditStatus状态获取的不同的数据
  318. if (auditStatus === 1 || auditStatus === 2 || auditStatus === 9) {
  319. renderData.changeUnits = changeConst.units;
  320. renderData.precision = ctx.tender.info.precision;
  321. // renderData.accountGroup = accountGroup;
  322. // 获取所有项目参与者
  323. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  324. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  325. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group'],
  326. });
  327. renderData.accountList = accountList;
  328. renderData.accountGroup = accountGroup.map((item, idx) => {
  329. const groupList = accountList.filter(item => item.account_group === idx);
  330. return { groupName: item, groupList };
  331. });
  332. // 重新上报获取审批流程
  333. if (auditStatus === 2 || auditStatus === 9) {
  334. const auditList2 = await ctx.service.changeAudit.getListByBack(change.cid, change.times);
  335. // 展示页右侧审批流程列表
  336. const auditList3 = [];
  337. for (let time = 1; time <= change.times - 1; time++) {
  338. const auditTimeList = [];
  339. let max_sort = 1;
  340. for (const al of auditList2) {
  341. if (al.times === time) {
  342. auditTimeList.push(al);
  343. if (al.usite > max_sort) {
  344. max_sort = al.usite;
  345. }
  346. }
  347. }
  348. for (const i in auditTimeList) {
  349. auditTimeList[i].max_sort = max_sort;
  350. }
  351. auditList3.push(auditTimeList);
  352. }
  353. renderData.auditList3 = auditList3;
  354. }
  355. // 根据清单获取提交数据和计算总金额
  356. const changeListData = [];
  357. const changeWhiteListData = [];
  358. let ototalCost = 0;
  359. let ctotalCost = 0;
  360. for (const cl of changeList) {
  361. const cLArray = [
  362. cl.code,
  363. cl.name,
  364. cl.bwmx,
  365. cl.unit,
  366. cl.unit_price,
  367. cl.oamount,
  368. cl.camount,
  369. cl.detail,
  370. cl.lid,
  371. cl.xmj_code,
  372. cl.xmj_jldy,
  373. cl.gcl_id,
  374. ];
  375. ototalCost += cl.unit_price === null ? 0 : ctx.helper.mul(cl.unit_price, cl.oamount, ctx.tender.info.decimal.tp);
  376. ctotalCost += cl.unit_price === null ? 0 : ctx.helper.mul(cl.unit_price, cl.camount, ctx.tender.info.decimal.tp);
  377. if (cl.lid !== '0') {
  378. changeListData.push(cLArray.join('*;*'));
  379. } else {
  380. changeWhiteListData.push(cLArray.join('*;*'));
  381. }
  382. }
  383. renderData.changeListData = changeListData.join('^_^');
  384. renderData.changeWhiteListData = changeWhiteListData.join('^_^');
  385. renderData.ototalCost = ototalCost;
  386. renderData.ctotalCost = ctotalCost;
  387. // 获取公司列表
  388. const companyList = await ctx.service.changeCompany.getAllDataByCondition({ where: { tid: tenderid } });
  389. renderData.companyList = companyList;
  390. } else if (auditStatus === 3 || auditStatus === 4 || auditStatus === 5 || auditStatus === 7) {
  391. // 展示页左侧审批流程列表和清单审批列表数据
  392. const times = change.status === audit.flow.status.back ?
  393. change.times - 1 : change.times;
  394. const auditList2 = await ctx.service.changeAudit.getListGroupByTimes(change.cid, times);
  395. // 展示页右侧审批流程列表
  396. const auditList3 = [];
  397. for (let time = 1; time <= times; time++) {
  398. const auditTimeList = [];
  399. let max_sort = 1;
  400. for (const al of auditList) {
  401. if (al.times === time) {
  402. auditTimeList.push(al);
  403. if (al.usite > max_sort) {
  404. max_sort = al.usite;
  405. }
  406. }
  407. }
  408. for (const i in auditTimeList) {
  409. auditTimeList[i].max_sort = max_sort;
  410. }
  411. auditList3.push(auditTimeList);
  412. }
  413. renderData.auditList3 = auditList3;
  414. changeList = JSON.parse(JSON.stringify(changeList.sort())).sort().sort();
  415. renderData.changeList = changeList;
  416. let ototalCost = 0;
  417. let ctotalCost = 0;
  418. let stotalCost = 0;
  419. const auditTotalCost = [];
  420. for (const cl of changeList) {
  421. // ototalCost += cl.unit_price === null ? 0 : parseFloat(ctx.helper.roundNum(ctx.helper.accMul(cl.unit_price, cl.oamount), renderData.tpUnit));
  422. ototalCost += cl.unit_price === null ? 0 : ctx.helper.mul(cl.unit_price, cl.oamount, renderData.tpUnit);
  423. // ctotalCost += cl.unit_price === null ? 0 : parseFloat(ctx.helper.roundNum(ctx.helper.accMul(cl.unit_price, cl.camount), renderData.tpUnit));
  424. ctotalCost += cl.unit_price === null ? 0 : ctx.helper.mul(cl.unit_price, cl.camount, renderData.tpUnit);
  425. // stotalCost += cl.samount !== '' && cl.unit_price !== null ? parseFloat(ctx.helper.roundNum(ctx.helper.accMul(cl.unit_price, cl.samount), renderData.tpUnit)) : 0;
  426. stotalCost += cl.samount !== '' && cl.unit_price !== null ? ctx.helper.mul(cl.unit_price, cl.samount, renderData.tpUnit) : 0;
  427. const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
  428. auditTotalCost.push(audit_amount);
  429. }
  430. renderData.ototalCost = ototalCost;
  431. renderData.ctotalCost = ctotalCost;
  432. renderData.stotalCost = stotalCost;
  433. // 清单表页赋值
  434. for (const [index, au] of auditList2.entries()) {
  435. if (au.usite !== 0) {
  436. au.list_amount = [];
  437. au.totalCost = 0;
  438. for (const [auindex, at] of auditTotalCost.entries()) {
  439. au.list_amount.push(at[index - 1]);
  440. // au.totalCost += at[index - 1] !== undefined && changeList[auindex].unit_price !== null ? parseFloat(ctx.helper.roundNum(ctx.helper.accMul(changeList[auindex].unit_price, at[index - 1]), renderData.tpUnit)) : 0;
  441. au.totalCost += at[index - 1] !== undefined && changeList[auindex].unit_price !== null ? ctx.helper.mul(changeList[auindex].unit_price, at[index - 1], renderData.tpUnit) : 0;
  442. }
  443. }
  444. }
  445. renderData.auditList2 = auditList2;
  446. } else if (auditStatus === 6) {
  447. // 展示页左侧审批流程列表和清单审批列表数据
  448. const auditList2 = await ctx.service.changeAudit.getListGroupByTimes(change.cid, change.times);
  449. renderData.auditList2 = auditList2;
  450. const auditList3 = await ctx.service.changeAudit.getListOrderByTimes(change.cid, change.times);
  451. for (const i in auditList3) {
  452. auditList3[i].max_sort = auditList2.length - 1;
  453. }
  454. renderData.auditList3 = auditList3;
  455. // 展示页右侧审批流程列表
  456. const auditList5 = await ctx.service.changeAudit.getListByBack(change.cid, change.times);
  457. const auditList4 = [];
  458. for (let time = 1; time <= change.times; time++) {
  459. const auditTimeList = [];
  460. let max_sort = 1;
  461. for (const al of auditList5) {
  462. if (al.times === time) {
  463. auditTimeList.push(al);
  464. if (al.usite > max_sort) {
  465. max_sort = al.usite;
  466. }
  467. }
  468. }
  469. for (const i in auditTimeList) {
  470. auditTimeList[i].max_sort = max_sort;
  471. }
  472. if (auditTimeList.length > 0) {
  473. auditList4.push(auditTimeList);
  474. }
  475. }
  476. renderData.auditList4 = auditList4;
  477. changeList = JSON.parse(JSON.stringify(changeList.sort())).sort().sort();
  478. renderData.changeList = changeList;
  479. let ototalCost = 0;
  480. let ctotalCost = 0;
  481. const auditTotalCost = [];
  482. const auditUnit = [];
  483. for (const cl of changeList) {
  484. // ototalCost += cl.unit_price === null ? 0 : parseFloat(ctx.helper.roundNum(ctx.helper.accMul(cl.unit_price, cl.oamount), renderData.tpUnit));
  485. ototalCost += cl.unit_price === null ? 0 : ctx.helper.mul(cl.unit_price, cl.oamount, renderData.tpUnit);
  486. // ctotalCost += cl.unit_price === null ? 0 : parseFloat(ctx.helper.roundNum(ctx.helper.accMul(cl.unit_price, cl.camount), renderData.tpUnit));
  487. ctotalCost += cl.unit_price === null ? 0 : ctx.helper.mul(cl.unit_price, cl.camount, renderData.tpUnit);
  488. const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
  489. auditTotalCost.push(audit_amount);
  490. }
  491. renderData.ototalCost = ototalCost;
  492. renderData.ctotalCost = ctotalCost;
  493. // 清单表页赋值
  494. for (const [index, au] of auditList.entries()) {
  495. if (au.usite !== 0) {
  496. au.list_amount = [];
  497. au.totalCost = 0;
  498. if (au.uid === renderData.uid) {
  499. for (const [auindex, at] of auditTotalCost.entries()) {
  500. // if (at[index - 2] !== undefined) {
  501. // au.list_amount.push(at[index - 2]);
  502. // au.totalCost += parseFloat(ctx.helper.roundNum(ctx.helper.accMul(changeList[auindex].unit_price, at[index - 2]), renderData.tpUnit));
  503. // } else if (at[index - 2] === undefined) {
  504. // au.list_amount.push(changeList[auindex].camount);
  505. // au.totalCost += parseFloat(ctx.helper.roundNum(ctx.helper.accMul(changeList[auindex].unit_price, changeList[auindex].camount), renderData.tpUnit));
  506. // }
  507. au.list_amount.push(changeList[auindex].spamount);
  508. // au.totalCost += changeList[auindex].unit_price === null ? 0 : parseFloat(ctx.helper.roundNum(ctx.helper.accMul(changeList[auindex].unit_price, changeList[auindex].spamount), renderData.tpUnit));
  509. au.totalCost += changeList[auindex].unit_price === null ? 0 : ctx.helper.mul(changeList[auindex].unit_price, changeList[auindex].spamount, renderData.tpUnit);
  510. }
  511. } else {
  512. for (const [auindex, at] of auditTotalCost.entries()) {
  513. au.list_amount.push(at[index - 1]);
  514. // au.totalCost += at[index - 1] !== undefined && changeList[auindex].unit_price !== null ? parseFloat(ctx.helper.roundNum(ctx.helper.accMul(changeList[auindex].unit_price, at[index - 1]), renderData.tpUnit)) : 0;
  515. au.totalCost += at[index - 1] !== undefined && changeList[auindex].unit_price !== null ? ctx.helper.mul(changeList[auindex].unit_price, at[index - 1], renderData.tpUnit) : 0;
  516. }
  517. }
  518. }
  519. }
  520. }
  521. renderData.auditList = auditList;
  522. // 获取是否已存在调用变更令
  523. const changeUsedData = await ctx.service.stageChange.getFinalUsedData(ctx.tender.id, change.cid);
  524. renderData.stageChangeNum = this.ctx.helper.sum(changeUsedData.map(x => { return Math.abs(x.used_qty); }));
  525. await this.layout('change/info.ejs', renderData, 'change/info_modal.ejs');
  526. } catch (err) {
  527. this.log(err);
  528. ctx.redirect('/tender/' + ctx.params.id + '/change');
  529. }
  530. }
  531. /**
  532. * 变更信息 新版本页面 (Get)
  533. *
  534. * @param {Object} ctx - egg全局变量
  535. * @return {void}
  536. */
  537. async information(ctx) {
  538. try {
  539. const whiteList = this.ctx.app.config.multipart.whitelist;
  540. const tender = await ctx.service.tender.getDataById(ctx.tender.id);
  541. const change = ctx.change;
  542. // 后台判断当前人查看info状态
  543. const auditStatus = await ctx.service.changeAudit.getStatusByChange(change);
  544. // 获取附件列表
  545. const attList = await ctx.service.changeAtt.getChangeAttachment(change.cid, 'desc');
  546. // 获取其他变更令数据
  547. const othersChange = await ctx.service.change.getOthersChange(ctx.tender.id, change.cid);
  548. // 根据auditStatus获取审批人列表
  549. const auditList = await ctx.service.changeAudit.getListByStatus(change, auditStatus);
  550. // 获取用户人验证手机号
  551. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  552. const auth_mobile = pa.auth_mobile;
  553. const renderData = {
  554. tender,
  555. change,
  556. othersChange,
  557. changeConst,
  558. auditStatus,
  559. auditConst: audit.flow,
  560. ledgerConsts: audit.ledger.status,
  561. attList,
  562. whiteList,
  563. auditList,
  564. tpUnit: change.tp_decimal ? change.tp_decimal : ctx.tender.info.decimal.tp,
  565. upUnit: change.up_decimal ? change.up_decimal : ctx.tender.info.decimal.up,
  566. authMobile: auth_mobile,
  567. shenpiConst,
  568. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.information),
  569. preUrl: '/tender/' + ctx.tender.id + '/change/' + ctx.change.cid + '/information',
  570. precision: ctx.tender.info.precision,
  571. };
  572. // 根据auditStatus状态获取的不同的数据
  573. if (auditStatus === 1 || auditStatus === 2 || auditStatus === 9) {
  574. renderData.changeUnits = changeConst.units;
  575. // renderData.accountGroup = accountGroup;
  576. // 获取所有项目参与者
  577. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  578. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  579. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group'],
  580. });
  581. renderData.accountList = accountList;
  582. renderData.accountGroup = accountGroup.map((item, idx) => {
  583. const groupList = accountList.filter(item => item.account_group === idx);
  584. return { groupName: item, groupList };
  585. });
  586. // 重新上报获取审批流程
  587. if (auditStatus === 2 || auditStatus === 9) {
  588. const auditList2 = await ctx.service.changeAudit.getListByBack(change.cid, change.times);
  589. // 展示页右侧审批流程列表
  590. const auditList3 = [];
  591. for (let time = 1; time <= change.times - 1; time++) {
  592. const auditTimeList = [];
  593. let max_sort = 1;
  594. for (const al of auditList2) {
  595. if (al.times === time) {
  596. auditTimeList.push(al);
  597. if (al.usite > max_sort) {
  598. max_sort = al.usite;
  599. }
  600. }
  601. }
  602. for (const i in auditTimeList) {
  603. auditTimeList[i].max_sort = max_sort;
  604. }
  605. auditList3.push(auditTimeList);
  606. }
  607. renderData.auditList3 = auditList3;
  608. }
  609. // 获取公司列表
  610. const companyList = await ctx.service.changeCompany.getAllDataByCondition({ where: { tid: ctx.tender.id } });
  611. renderData.companyList = companyList;
  612. // 获取已选清单
  613. const changeList = await ctx.service.changeAuditList.getList(change.cid);
  614. renderData.changeList = changeList;
  615. renderData.changeLedgerList = await ctx.service.changeLedger.getAllDataByCondition({ where: { tender_id: ctx.tender.id } });
  616. renderData.changePosList = await ctx.service.changePos.getAllDataByCondition({ where: { tid: ctx.tender.id } });
  617. } else if (auditStatus === 3 || auditStatus === 4 || auditStatus === 5 || auditStatus === 7 || auditStatus === 8) {
  618. // 展示页左侧审批流程列表和清单审批列表数据
  619. const times = change.status === audit.flow.status.back ?
  620. change.times - 1 : change.times;
  621. const auditList2 = await ctx.service.changeAudit.getListGroupByTimes(change.cid, times);
  622. // 展示页右侧审批流程列表
  623. const auditList3 = [];
  624. for (let time = 1; time <= times; time++) {
  625. const auditTimeList = [];
  626. let max_sort = 1;
  627. for (const al of auditList) {
  628. if (al.times === time) {
  629. auditTimeList.push(al);
  630. if (al.usite > max_sort) {
  631. max_sort = al.usite;
  632. }
  633. }
  634. }
  635. for (const i in auditTimeList) {
  636. auditTimeList[i].max_sort = max_sort;
  637. }
  638. auditList3.push(auditTimeList);
  639. }
  640. renderData.auditList3 = auditList3;
  641. // 获取已选清单
  642. // let changeList = await ctx.service.changeAuditList.getAllDataByCondition({ where: { cid: ctx.params.cid } });
  643. const changeList = await ctx.service.changeAuditList.getList(change.cid);
  644. // changeList = JSON.parse(JSON.stringify(changeList.sort())).sort().sort();
  645. for (const cl of changeList) {
  646. const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
  647. // 清单表页赋值
  648. for (const [index, au] of auditList2.entries()) {
  649. if (au.usite !== 0) {
  650. cl['audit_amount_' + au.uid] = audit_amount[index - 1] ? audit_amount[index - 1] : null;
  651. }
  652. }
  653. }
  654. renderData.changeList = changeList;
  655. renderData.auditList2 = auditList2;
  656. } else if (auditStatus === 6) {
  657. // 展示页左侧审批流程列表和清单审批列表数据
  658. const auditList2 = await ctx.service.changeAudit.getListGroupByTimes(change.cid, change.times);
  659. renderData.auditList2 = auditList2;
  660. const auditList3 = await ctx.service.changeAudit.getListOrderByTimes(change.cid, change.times);
  661. for (const i in auditList3) {
  662. auditList3[i].max_sort = auditList2.length - 1;
  663. }
  664. renderData.auditList3 = auditList3;
  665. // 展示页右侧审批流程列表
  666. const auditList5 = await ctx.service.changeAudit.getListByBack(change.cid, change.times);
  667. const auditList4 = [];
  668. for (let time = 1; time <= change.times; time++) {
  669. const auditTimeList = [];
  670. let max_sort = 1;
  671. for (const al of auditList5) {
  672. if (al.times === time) {
  673. auditTimeList.push(al);
  674. if (al.usite > max_sort) {
  675. max_sort = al.usite;
  676. }
  677. }
  678. }
  679. for (const i in auditTimeList) {
  680. auditTimeList[i].max_sort = max_sort;
  681. }
  682. if (auditTimeList.length > 0) {
  683. auditList4.push(auditTimeList);
  684. }
  685. }
  686. renderData.auditList4 = auditList4;
  687. // 获取已选清单
  688. // let changeList = await ctx.service.changeAuditList.getAllDataByCondition({ where: { cid: ctx.params.cid } });
  689. const changeList = await ctx.service.changeAuditList.getList(change.cid);
  690. // changeList = JSON.parse(JSON.stringify(changeList.sort())).sort().sort();
  691. for (const cl of changeList) {
  692. const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
  693. // 清单表页赋值
  694. for (const [index, au] of auditList2.entries()) {
  695. if (au.usite !== 0) {
  696. cl['audit_amount_' + au.uid] = au.uid === ctx.session.sessionUser.accountId ? cl.spamount : (audit_amount[index - 1] ? audit_amount[index - 1] : null);
  697. }
  698. }
  699. }
  700. renderData.changeList = changeList;
  701. renderData.changeLedgerList = await ctx.service.changeLedger.getAllDataByCondition({ where: { tender_id: ctx.tender.id } });
  702. renderData.changePosList = await ctx.service.changePos.getAllDataByCondition({ where: { tid: ctx.tender.id } });
  703. }
  704. renderData.auditList = auditList;
  705. // 获取是否已存在调用变更令
  706. let changeUsedData = await ctx.service.stageChange.getFinalUsedData(ctx.tender.id, change.cid);
  707. changeUsedData = ctx.helper._.filter(changeUsedData, function(item) {
  708. return item.used_qty !== null;
  709. })
  710. renderData.changeUsedData = changeUsedData;
  711. renderData.stageChangeNum = this.ctx.helper.sum(changeUsedData.map(x => { return Math.abs(x.used_qty); }));
  712. await this.layout('change/information.ejs', renderData, 'change/information_modal.ejs');
  713. } catch (err) {
  714. this.log(err);
  715. ctx.redirect('/tender/' + ctx.params.id + '/change');
  716. }
  717. }
  718. /**
  719. * 变更清单 - 操作 (Ajax)
  720. * @param ctx
  721. * @return {Promise<void>}
  722. */
  723. async saveListsData(ctx) {
  724. try {
  725. const data = JSON.parse(ctx.request.body.data);
  726. const responseData = {
  727. err: 0,
  728. msg: '',
  729. data: {},
  730. };
  731. switch (data.type) {
  732. case 'add':
  733. responseData.data = await ctx.service.changeAuditList.add(data.postData);
  734. break;
  735. case 'batchadd':
  736. responseData.data = await ctx.service.changeAuditList.batchAdd(data);
  737. break;
  738. case 'del':
  739. await ctx.service.changeAuditList.del(data.id);
  740. break;
  741. case 'update':
  742. await ctx.service.changeAuditList.save(data.updateData);
  743. break;
  744. case 'paste':
  745. await ctx.service.changeAuditList.saveDatas(data.updateData);
  746. // 取所有工料表
  747. responseData.data = await ctx.service.changeAuditList.getList(ctx.change.cid);
  748. break;
  749. case 'ledger_list':
  750. await ctx.service.changeAuditList.saveLedgerListDatas(data.updateData);
  751. // 取所有工料表
  752. responseData.data = { changeList: await ctx.service.changeAuditList.getList(ctx.change.cid),
  753. usedList: await ctx.service.stageChange.getFinalUsedData(ctx.tender.id, ctx.change.cid) };
  754. break;
  755. case 'remove_list':
  756. await ctx.service.changeAuditList.removeLedgerListDatas(data.updateData);
  757. // 取所有工料表
  758. responseData.data = await ctx.service.changeAuditList.getList(ctx.change.cid);
  759. break;
  760. case 'update_list':
  761. await ctx.service.changeAuditList.saveDatas(data.updateData);
  762. // 取所有工料表
  763. // responseData.data = await ctx.service.changeAuditList.getList(ctx.change.cid);
  764. break;
  765. case 'info':
  766. await ctx.service.change.saveInfo(data.updateData);
  767. // 取所有工料表
  768. responseData.data = '保存成功';
  769. break;
  770. case 'paste_amount_rows':
  771. await ctx.service.changeAuditList.saveDatas(data.updateData);
  772. let changeList = await ctx.service.changeAuditList.getList(ctx.change.cid);
  773. const auditList2 = await ctx.service.changeAudit.getListGroupByTimes(ctx.change.cid, ctx.change.times);
  774. changeList = JSON.parse(JSON.stringify(changeList.sort())).sort().sort();
  775. for (const cl of changeList) {
  776. const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
  777. // 清单表页赋值
  778. for (const [index, au] of auditList2.entries()) {
  779. if (au.usite !== 0) {
  780. cl['audit_amount_' + au.uid] = au.uid === ctx.session.sessionUser.accountId ? cl.spamount : (audit_amount[index - 1] ? audit_amount[index - 1] : null);
  781. }
  782. }
  783. }
  784. // 取所有工料表
  785. responseData.data = changeList;
  786. break;
  787. default: throw '参数有误';
  788. }
  789. ctx.body = responseData;
  790. } catch (err) {
  791. this.log(err);
  792. ctx.body = { err: 1, msg: err.toString(), data: null };
  793. }
  794. }
  795. /**
  796. * 上报和重新上报
  797. * @param ctx
  798. * @return {Promise<void>}
  799. */
  800. async startAudit(ctx) {
  801. try {
  802. // 检查权限等
  803. if (!ctx.change) {
  804. throw '数据错误';
  805. }
  806. if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
  807. throw '您无权上报该变更令数据';
  808. }
  809. if (!(ctx.change.status === audit.flow.status.uncheck || ctx.change.status === audit.flow.status.back || ctx.change.status === audit.flow.status.revise)) {
  810. throw '该变更令当前无法上报';
  811. }
  812. // 判断是否是修订,判断有无审批人员作弊
  813. if ((ctx.change.status === audit.flow.status.revise || ctx.change.is_revise) && ctx.tender.info.shenpi.change === shenpiConst.sp_status.sqspr) {
  814. // 获取上一次的审批人流程
  815. const lastUserList = await ctx.service.changeAudit.getListGroupByTimes(ctx.change.cid, ctx.change.times - 1);
  816. const lastUidList = ctx.helper._.map(lastUserList, 'uid');
  817. // 判断上一次审批是否为非原报为审批人
  818. const nowUidList = ctx.helper._.map(ctx.change.auditors, 'uid');
  819. // 判断条件修订可上报条件
  820. // 1.有原报,有其他人 可以添加原报作为审核人,但是不能只原报上报
  821. // 2.有原报,无其他人 可以添加原报作为审核人,可以只原报上报
  822. // 3.无原报,有其他人 不可以添加原报
  823. const noYBUidList = ctx.change.status === audit.flow.status.revise ? ctx.helper._.initial(ctx.helper._.tail(lastUidList)) : ctx.helper._.tail(lastUidList);
  824. if (!ctx.helper._.isEqual(lastUidList, nowUidList) && ctx.helper._.includes(noYBUidList, lastUidList[0]) && nowUidList.length === 2 && nowUidList[0] === nowUidList[1]) {
  825. throw '该变更令不能指定原报人为单独审批人';
  826. }
  827. if (!ctx.helper._.isEqual(lastUidList, nowUidList) && !ctx.helper._.includes(noYBUidList, lastUidList[0]) && ctx.helper._.includes(ctx.helper._.tail(nowUidList), nowUidList[0])) {
  828. throw '该变更令不能指定原报为审批人,请移除';
  829. }
  830. }
  831. await ctx.service.changeAudit.start(ctx.change.cid, ctx.change.times);
  832. ctx.redirect(ctx.request.header.referer);
  833. } catch (err) {
  834. this.log(err);
  835. ctx.session.postError = err.toString();
  836. ctx.redirect(ctx.request.header.referer);
  837. }
  838. }
  839. // 审批相关
  840. /**
  841. * 添加审批人
  842. * @param ctx
  843. * @return {Promise<void>}
  844. */
  845. async addAudit(ctx) {
  846. try {
  847. const data = JSON.parse(ctx.request.body.data);
  848. const id = this.app._.toInteger(data.auditorId);
  849. if (isNaN(id) || id <= 0) {
  850. throw '参数错误';
  851. }
  852. // 检查权限等
  853. if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
  854. throw '您无权添加审核人';
  855. }
  856. if (ctx.change.status === audit.flow.status.checking || ctx.change.status === audit.flow.status.checked) {
  857. throw '当前不允许添加审核人';
  858. }
  859. const auditorList = await ctx.service.changeAudit.getAuditors(ctx.change.cid, ctx.change.times);
  860. // 检查审核人是否已存在
  861. const exist = this.app._.find(auditorList, { uid: id });
  862. if (exist) {
  863. throw '该审核人已存在,请勿重复添加';
  864. }
  865. const shenpiInfo = await ctx.service.shenpiAudit.getDataByCondition({ tid: ctx.tender.id, sp_type: shenpiConst.sp_type.change, sp_status: shenpiConst.sp_status.gdzs });
  866. const is_gdzs = shenpiInfo && ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdzs ? 1 : 0;
  867. const result = await ctx.service.changeAudit.addAuditor(ctx.change.cid, id, ctx.change.times, is_gdzs);
  868. if (!result) {
  869. throw '添加审核人失败';
  870. }
  871. // const auditors = await ctx.service.changeAudit.getAuditorsWithOwner(ctx.change.id, ctx.change.times);
  872. const auditors = null;
  873. ctx.body = { err: 0, msg: '', data: auditors };
  874. } catch (err) {
  875. this.log(err);
  876. ctx.body = { err: 1, msg: err.toString(), data: null };
  877. }
  878. }
  879. /**
  880. * 移除审批人
  881. * @param ctx
  882. * @return {Promise<void>}
  883. */
  884. async deleteAudit(ctx) {
  885. try {
  886. const data = JSON.parse(ctx.request.body.data);
  887. const id = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
  888. if (isNaN(id) || id <= 0) {
  889. throw '参数错误';
  890. }
  891. const result = await ctx.service.changeAudit.deleteAuditor(ctx.change.cid, id, ctx.change.times);
  892. if (!result) {
  893. throw '移除审核人失败';
  894. }
  895. const auditors = null;
  896. ctx.body = { err: 0, msg: '', data: auditors };
  897. } catch (err) {
  898. ctx.body = { err: 1, msg: err.toString(), data: null };
  899. }
  900. }
  901. async defaultBills(ctx) {
  902. try {
  903. // 判断是否台账修订中,修订中则不获取changeLedger及changePos值
  904. const lastRevise = await ctx.service.ledgerRevise.getLastestRevise(ctx.tender.id);
  905. const data = JSON.parse(ctx.request.body.data);
  906. const ledgerData = await ctx.service.ledger.getData(ctx.tender.id);
  907. const changeLedgerData = !ctx.session.sessionProject.page_show.openChangeRevise ? [] : (lastRevise && lastRevise.status !== audit.revise.status.checked && data.from !== 'revise' ? [] : await ctx.service.changeLedger.getData(ctx.tender.id));
  908. const posData = await ctx.service.pos.getPosData({ tid: ctx.tender.id });
  909. const changePosData = !ctx.session.sessionProject.page_show.openChangeRevise ? [] : (lastRevise && lastRevise.status !== audit.revise.status.checked && data.from !== 'revise' ? [] : await ctx.service.changePos.getPosData({ tid: ctx.tender.id }));
  910. const dealBills = await ctx.service.dealBills.getAllDataByCondition({ where: { tender_id: ctx.tender.id } });
  911. // 标记ledger,搜索需求
  912. if (changePosData.length > 0) {
  913. const cplIdList = ctx.helper._.uniq(ctx.helper._.map(changePosData, 'lid'));
  914. const cLIdList = ctx.helper._.map(changeLedgerData, 'id');
  915. const ledgerIdList = [];
  916. for (const cp of cplIdList) {
  917. if (ctx.helper._.indexOf(cLIdList, cp) === -1) {
  918. ledgerIdList.push(cp);
  919. }
  920. }
  921. if (ledgerIdList.length > 0) {
  922. for (const lid of ledgerIdList) {
  923. const data = ctx.helper._.find(ledgerData, { id: lid });
  924. data.cid = 1;
  925. }
  926. }
  927. }
  928. ctx.body = { err: 0, msg: '', data: { bills: ctx.helper._.concat(ledgerData, changeLedgerData), pos: ctx.helper._.concat(posData, changePosData), dealBills } };
  929. } catch (err) {
  930. this.log(err);
  931. ctx.body = { err: 1, msg: err.toString(), data: [] };
  932. }
  933. }
  934. /**
  935. * 变更令上报和保存
  936. * @param {Object} ctx - egg全局变量
  937. * @return {void}
  938. */
  939. async save(ctx) {
  940. // 变更令信息
  941. const changeInfo = await ctx.service.change.getDataByCondition({ cid: ctx.request.body.cid });
  942. try {
  943. const result = await ctx.service.change.save(ctx.request.body, changeInfo.tid);
  944. if (!result) {
  945. throw '上报失败';
  946. }
  947. if (ctx.request.body.changestatus !== undefined && parseInt(ctx.request.body.changestatus) === 2) {
  948. ctx.body = { err: 0, msg: '保存成功' };
  949. } else {
  950. ctx.redirect('/tender/' + changeInfo.tid + '/change');
  951. }
  952. } catch (err) {
  953. this.log(err);
  954. if (ctx.request.body.changestatus !== undefined && parseInt(ctx.request.body.changestatus) === 2) {
  955. ctx.body = { err: 1, msg: err.toString() };
  956. } else {
  957. ctx.redirect('/tender/' + changeInfo.tid + '/change/' + ctx.request.body.cid + '/info');
  958. }
  959. }
  960. }
  961. /**
  962. * 变更令审批
  963. * @param {Object} ctx - egg全局变量
  964. * @return {void}
  965. */
  966. async approval(ctx) {
  967. try {
  968. const changeData = await ctx.service.change.getDataByCondition({ cid: ctx.request.body.change_id });
  969. if (!changeData) {
  970. throw '变更令数据错误';
  971. }
  972. const status = parseInt(ctx.request.body.status);
  973. let result = false;
  974. const pid = this.ctx.session.sessionProject.id;
  975. switch (status) {
  976. case 3:// 审批通过
  977. result = await ctx.service.change.approvalSuccess(pid, ctx.request.body, changeData);
  978. break;
  979. case 4:// 审批终止
  980. result = await ctx.service.change.approvalStop(ctx.request.body);
  981. break;
  982. case 5:// 审批退回到原报人
  983. result = await ctx.service.change.approvalBack(pid, ctx.request.body, changeData);
  984. break;
  985. case 6:// 审批退回到上一个审批人
  986. result = await ctx.service.change.approvalBackNew(pid, ctx.request.body, changeData);
  987. break;
  988. default:break;
  989. }
  990. if (!result) {
  991. throw '审批失败';
  992. }
  993. ctx.redirect(ctx.request.header.referer);
  994. } catch (err) {
  995. console.log(err);
  996. ctx.redirect(ctx.request.header.referer);
  997. }
  998. }
  999. /**
  1000. * 变更公司管理
  1001. * @param {Object} ctx - egg全局变量
  1002. * @return {void}
  1003. */
  1004. async updateCompany(ctx) {
  1005. const responseData = {
  1006. err: 0,
  1007. msg: '',
  1008. data: '',
  1009. };
  1010. try {
  1011. const data = JSON.parse(ctx.request.body.data);
  1012. if (data.tid === undefined || data.uci === undefined || data.uc === undefined || data.ac === undefined) {
  1013. throw '参数有误';
  1014. }
  1015. const [addCompany, selectCompany] = await ctx.service.changeCompany.setCompanyList(data);
  1016. responseData.data = { add: addCompany, select: selectCompany };
  1017. } catch (err) {
  1018. responseData.err = 1;
  1019. responseData.msg = err;
  1020. }
  1021. ctx.body = responseData;
  1022. }
  1023. /**
  1024. * 上传附件
  1025. * @param {Object} ctx - egg全局变量
  1026. * @return {void}
  1027. */
  1028. async uploadFile(ctx) {
  1029. const responseData = {
  1030. err: 0,
  1031. msg: '',
  1032. data: [],
  1033. };
  1034. let stream;
  1035. try {
  1036. const parts = ctx.multipart({ autoFields: true });
  1037. const files = [];
  1038. let index = 0;
  1039. const change = await ctx.service.change.getDataByCondition({ cid: ctx.params.cid });
  1040. const extra_upload = change.status === audit.flow.status.checked;
  1041. while ((stream = await parts()) !== undefined) {
  1042. // 判断用户是否选择上传文件
  1043. if (!stream.filename) {
  1044. throw '请选择上传的文件!';
  1045. }
  1046. // const create_time = Date.parse(new Date()) / 1000;
  1047. // const fileInfo = path.parse(stream.filename);
  1048. // const dirName = 'app/public/upload/changes/' + moment().format('YYYYMMDD');
  1049. // const fileName = 'changes' + create_time + '_' + index + fileInfo.ext;
  1050. // // 判断文件夹是否存在,不存在则直接创建文件夹
  1051. // if (!fs.existsSync(path.join(this.app.baseDir, dirName))) {
  1052. // await fs.mkdirSync(path.join(this.app.baseDir, dirName));
  1053. // }
  1054. // // 保存文件
  1055. // await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, dirName, fileName));
  1056. const fileInfo = path.parse(stream.filename);
  1057. const create_time = Date.parse(new Date()) / 1000;
  1058. // const filepath = `app/public/upload/change/fujian_${create_time + index.toString() + fileInfo.ext}`;
  1059. const filepath = `app/public/upload/${parts.field.tid}/change/fujian_${create_time + index.toString() + fileInfo.ext}`;
  1060. // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, filepath));
  1061. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  1062. await sendToWormhole(stream);
  1063. // 保存数据到att表
  1064. const fileData = {
  1065. in_time: create_time,
  1066. filename: fileInfo.name,
  1067. fileext: fileInfo.ext,
  1068. filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
  1069. filepath,
  1070. extra_upload,
  1071. };
  1072. const result = await ctx.service.changeAtt.save(parts.field, fileData, ctx.session.sessionUser.accountId);
  1073. if (!result) {
  1074. throw '导入数据库保存失败';
  1075. }
  1076. // fileData.in_time = moment(create_time * 1000).format('YYYY-MM-DD');
  1077. // fileData.filesize = await ctx.helper.bytesToSize(fileData.filesize);
  1078. fileData.uid = ctx.session.sessionUser.accountId;
  1079. fileData.id = result.insertId;
  1080. fileData.orginpath = ctx.app.config.fujianOssPath + filepath;
  1081. delete fileData.filepath;
  1082. files.push(fileData);
  1083. ++index;
  1084. }
  1085. responseData.data = files;
  1086. } catch (err) {
  1087. this.log(err);
  1088. // 失败需要消耗掉stream 以防卡死
  1089. if (stream) {
  1090. await sendToWormhole(stream);
  1091. }
  1092. this.setMessage(err.toString(), this.messageType.ERROR);
  1093. }
  1094. ctx.body = responseData;
  1095. }
  1096. /**
  1097. * 下载附件
  1098. * @param {Object} ctx - egg全局变量
  1099. * @return {void}
  1100. */
  1101. async downloadFile(ctx) {
  1102. const id = ctx.params.id;
  1103. if (id) {
  1104. try {
  1105. const fileInfo = await ctx.service.changeAtt.getDataById(id);
  1106. if (fileInfo !== undefined && fileInfo !== '') {
  1107. // const fileName = path.join(this.app.baseDir, fileInfo.filepath);
  1108. // 解决中文无法下载问题
  1109. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  1110. let disposition = '';
  1111. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  1112. disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename + fileInfo.fileext);
  1113. } else if (userAgent.indexOf('firefox') >= 0) {
  1114. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename + fileInfo.fileext) + '"';
  1115. } else {
  1116. /* safari等其他非主流浏览器只能自求多福了 */
  1117. disposition = 'attachment; filename=' + new Buffer(fileInfo.filename + fileInfo.fileext).toString('binary');
  1118. }
  1119. ctx.response.set({
  1120. 'Content-Type': 'application/octet-stream',
  1121. 'Content-Disposition': disposition,
  1122. 'Content-Length': fileInfo.filesize,
  1123. });
  1124. // ctx.body = await fs.createReadStream(fileName);
  1125. ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
  1126. } else {
  1127. throw '不存在该文件';
  1128. }
  1129. } catch (err) {
  1130. this.log(err);
  1131. this.setMessage(err.toString(), this.messageType.ERROR);
  1132. }
  1133. }
  1134. }
  1135. /**
  1136. * 批量下载 - 压缩成zip文件返回
  1137. * @param {Oject} ctx - 全局上下文
  1138. */
  1139. async downloadZip(ctx) {
  1140. const time = Date.now();
  1141. const zipPath = `app/public/upload/change/fu_jian_zip${time}.zip`;
  1142. const responseData = {
  1143. err: 0,
  1144. msg: '',
  1145. };
  1146. try {
  1147. // const fileIds = JSON.parse(ctx.request.query.fileIds);
  1148. const { fileIds = [] } = JSON.parse(ctx.request.body.data);
  1149. const { name: changeName } = await ctx.service.changeAtt.getChangeName(ctx.params.cid);
  1150. const zipFilename = `${ctx.tender.data.name}-工程变更-${changeName}-附件.zip`;
  1151. const size = await ctx.service.changeAtt.compressedFile(fileIds, zipPath);
  1152. // 解决中文无法下载问题
  1153. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  1154. let disposition = '';
  1155. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  1156. disposition = 'attachment; filename=' + encodeURIComponent(zipFilename);
  1157. } else if (userAgent.indexOf('firefox') >= 0) {
  1158. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(zipFilename) + '"';
  1159. } else {
  1160. /* safari等其他非主流浏览器只能自求多福了 */
  1161. disposition = 'attachment; filename=' + new Buffer(zipFilename).toString('binary');
  1162. }
  1163. ctx.response.set({
  1164. 'Content-Type': 'application/octet-stream',
  1165. 'Content-Disposition': disposition,
  1166. 'Content-Length': size,
  1167. });
  1168. const readStream = fs.createReadStream(path.join(this.app.baseDir, zipPath));
  1169. ctx.body = readStream;
  1170. // ctx.body = fs.readFileSync(path.resolve(this.app.baseDir, zipPath));
  1171. readStream.on('close', () => {
  1172. if (fs.existsSync(path.resolve(this.app.baseDir, zipPath))) {
  1173. fs.unlinkSync(path.resolve(this.app.baseDir, zipPath));
  1174. }
  1175. });
  1176. // fs的错误不能被try catch捕捉
  1177. readStream.on('error', err => {
  1178. this.log(err);
  1179. if (fs.existsSync(path.resolve(this.app.baseDir, zipPath))) {
  1180. fs.unlinkSync(path.resolve(this.app.baseDir, zipPath));
  1181. }
  1182. responseData.err = 1;
  1183. responseData.msg = err.toString();
  1184. ctx.body = responseData;
  1185. });
  1186. } catch (err) {
  1187. this.log(err);
  1188. if (fs.existsSync(path.resolve(this.app.baseDir, zipPath))) {
  1189. fs.unlinkSync(path.resolve(this.app.baseDir, zipPath));
  1190. }
  1191. this.setMessage(err.toString(), this.messageType.ERROR);
  1192. responseData.err = 1;
  1193. responseData.msg = err.toString();
  1194. ctx.body = responseData;
  1195. }
  1196. }
  1197. /**
  1198. * 删除附件
  1199. * @param {Object} ctx - egg全局变量
  1200. * @return {void}
  1201. */
  1202. async deleteFile(ctx) {
  1203. const responseData = {
  1204. err: 0,
  1205. msg: '',
  1206. data: '',
  1207. };
  1208. try {
  1209. const data = JSON.parse(ctx.request.body.data);
  1210. const change = await ctx.service.change.getDataByCondition({ cid: ctx.params.cid });
  1211. const fileInfo = await ctx.service.changeAtt.getDataById(data.id);
  1212. if (!fileInfo || !Object.keys(fileInfo).length) {
  1213. throw '该文件不存在';
  1214. }
  1215. if (!fileInfo.extra_upload && change.status === audit.flow.status.checked) {
  1216. throw '无权限删除';
  1217. }
  1218. if (fileInfo !== undefined && fileInfo !== '') {
  1219. // 先删除文件
  1220. // await fs.unlinkSync(path.join(this.app.baseDir, fileInfo.filepath));
  1221. await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
  1222. // 再删除数据库
  1223. await ctx.service.changeAtt.deleteById(data.id);
  1224. responseData.data = '';
  1225. } else {
  1226. throw '不存在该文件';
  1227. }
  1228. // if (data.tid === undefined || data.uci === undefined || data.uc === undefined || data.ac === undefined) {
  1229. // throw '参数有误';
  1230. // }
  1231. // const [addCompany, selectCompany] = await ctx.service.changeCompany.setCompanyList(data);
  1232. // responseData.data = { add: addCompany, select: selectCompany };
  1233. } catch (err) {
  1234. responseData.err = 1;
  1235. responseData.msg = err;
  1236. }
  1237. ctx.body = responseData;
  1238. }
  1239. /**
  1240. * 查看附件
  1241. * @param {Object} ctx - egg全局变量
  1242. * @return {void}
  1243. */
  1244. async checkFile(ctx) {
  1245. const responseData = { err: 0, msg: '' };
  1246. const id = parseInt(ctx.params.id);
  1247. if (id) {
  1248. try {
  1249. const fileInfo = await ctx.service.changeAtt.getDataById(id);
  1250. if (fileInfo && Object.keys(fileInfo).length) {
  1251. let filepath = fileInfo.filepath;
  1252. if (!ctx.helper.canPreview(fileInfo.fileext)) {
  1253. filepath = `/change/download/file/${fileInfo.id}`;
  1254. } else {
  1255. filepath = filepath.replace(/^app|\/app/, '');
  1256. }
  1257. fileInfo.filepath && (responseData.data = { filepath });
  1258. }
  1259. } catch (error) {
  1260. this.log(error);
  1261. this.setMessage(error.toString(), this.messageType.ERROR);
  1262. responseData.err = 1;
  1263. responseData.msg = error.toString();
  1264. }
  1265. }
  1266. ctx.body = responseData;
  1267. }
  1268. /**
  1269. * 删除变更令
  1270. * @param {Object} ctx - egg全局变量
  1271. * @return {void}
  1272. */
  1273. async delete(ctx) {
  1274. try {
  1275. const result = await ctx.service.change.delete(ctx.request.body.cid);
  1276. if (!result) {
  1277. throw '删除变更令失败';
  1278. }
  1279. ctx.redirect(ctx.request.header.referer);
  1280. } catch (err) {
  1281. console.log(err);
  1282. ctx.redirect(ctx.request.header.referer);
  1283. }
  1284. }
  1285. /**
  1286. * 变更令重新审批
  1287. * @param {Object} ctx - egg全局变量
  1288. * @return {void}
  1289. */
  1290. async checkAgain(ctx) {
  1291. try {
  1292. const changeData = await ctx.service.change.getDataByCondition({ cid: ctx.request.body.cid });
  1293. if (!changeData) {
  1294. throw '变更令数据错误';
  1295. }
  1296. // 获取终审
  1297. const auditInfo = (await this.ctx.service.changeAudit.getAllDataByCondition({ where: { cid: changeData.cid }, orders: [['usort', 'desc']], limit: 1, offset: 0 }))[0];
  1298. if (changeData.status !== audit.flow.status.checked || ctx.session.sessionUser.accountId !== auditInfo.uid) {
  1299. throw '您无权进行该操作';
  1300. }
  1301. if (ctx.session.sessionUser.loginStatus === 0) {
  1302. const code = ctx.request.body.code;
  1303. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  1304. if (!pa.auth_mobile) {
  1305. throw '未绑定手机号';
  1306. }
  1307. const cacheKey = 'smsCode:' + ctx.session.sessionUser.accountId;
  1308. const cacheCode = await app.redis.get(cacheKey);
  1309. // console.log(cacheCode);
  1310. if (cacheCode === null || code === undefined || cacheCode !== (code + pa.auth_mobile)) {
  1311. throw '验证码不正确!';
  1312. }
  1313. }
  1314. // 获取是否已存在调用变更令
  1315. const changeUsedData = await ctx.service.stageChange.getFinalUsedData(ctx.tender.id, changeData.cid);
  1316. const stageChangeNum = this.ctx.helper.sum(changeUsedData.map(x => { return Math.abs(x.used_qty); }));
  1317. if (stageChangeNum !== 0) {
  1318. throw '该变更令已被调用,无法重新审批';
  1319. }
  1320. // 重新审批
  1321. const result = await ctx.service.change.checkAgain(changeData.cid);
  1322. if (!result) {
  1323. throw '重新审批失败';
  1324. }
  1325. // ctx.redirect('/tender/' + changeData.tid + '/change/' + changeData.cid + '/info');
  1326. ctx.body = {
  1327. err: 0,
  1328. url: ctx.request.header.referer,
  1329. msg: '',
  1330. };
  1331. } catch (err) {
  1332. console.log(err);
  1333. // ctx.redirect(ctx.request.header.referer);
  1334. ctx.body = {
  1335. err: 1,
  1336. // url: ctx.request.header.referer,
  1337. msg: err,
  1338. };
  1339. }
  1340. }
  1341. /**
  1342. * 变更令修订重新上报
  1343. * @param {Object} ctx - egg全局变量
  1344. * @return {void}
  1345. */
  1346. async checkRevise(ctx) {
  1347. try {
  1348. const changeData = await ctx.service.change.getDataByCondition({ cid: ctx.request.body.cid });
  1349. if (!changeData) {
  1350. throw '变更令数据错误';
  1351. }
  1352. if (changeData.status !== audit.flow.status.checked || ctx.session.sessionUser.accountId !== changeData.uid) {
  1353. throw '您无权进行该操作';
  1354. }
  1355. if (ctx.session.sessionUser.loginStatus === 0) {
  1356. const code = ctx.request.body.code;
  1357. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  1358. if (!pa.auth_mobile) {
  1359. throw '未绑定手机号';
  1360. }
  1361. const cacheKey = 'smsCode:' + ctx.session.sessionUser.accountId;
  1362. const cacheCode = await app.redis.get(cacheKey);
  1363. // console.log(cacheCode);
  1364. if (cacheCode === null || code === undefined || cacheCode !== (code + pa.auth_mobile)) {
  1365. throw '验证码不正确!';
  1366. }
  1367. }
  1368. // 获取是否已存在调用变更令
  1369. // const changeUsedData = await ctx.service.stageChange.getFinalUsedData(ctx.tender.id, changeData.cid);
  1370. // const stageChangeNum = this.ctx.helper.sum(changeUsedData.map(x => { return Math.abs(x.used_qty); }));
  1371. // if (stageChangeNum !== 0) {
  1372. // throw '该变更令已被调用,无法重新审批';
  1373. // }
  1374. // 重新审批
  1375. const result = await ctx.service.change.checkRevise(changeData.cid);
  1376. if (!result) {
  1377. throw '修订发起失败';
  1378. }
  1379. // ctx.redirect('/tender/' + changeData.tid + '/change/' + changeData.cid + '/info');
  1380. ctx.body = {
  1381. err: 0,
  1382. url: ctx.request.header.referer,
  1383. msg: '',
  1384. };
  1385. } catch (err) {
  1386. console.log(err);
  1387. // ctx.redirect(ctx.request.header.referer);
  1388. ctx.body = {
  1389. err: 1,
  1390. // url: ctx.request.header.referer,
  1391. msg: err,
  1392. };
  1393. }
  1394. }
  1395. /**
  1396. * 获取变更清单
  1397. * @param ctx
  1398. * @return {Promise<void>}
  1399. */
  1400. async bills(ctx) {
  1401. try {
  1402. const data = JSON.parse(ctx.request.body.data);
  1403. const responseData = { err: 0, msg: '', data: [] };
  1404. switch (data.type) {
  1405. case 'gather':
  1406. responseData.data = await ctx.service.changeAuditList.gatherBgBills(ctx.tender.id);
  1407. break;
  1408. default:
  1409. throw '查询的数据不存在';
  1410. }
  1411. ctx.body = responseData;
  1412. } catch (err) {
  1413. this.log(err);
  1414. this.ajaxErrorBody(err, '获取变更清单失败');
  1415. }
  1416. }
  1417. /**
  1418. * 最后审批人审批成功检查批复编号和其它变更令是否出现重名情况
  1419. * @param ctx
  1420. * @return {Promise<void>}
  1421. */
  1422. async checkCodeRepeat(ctx) {
  1423. const responseData = {
  1424. err: 0,
  1425. msg: '',
  1426. data: '',
  1427. };
  1428. try {
  1429. const tenderId = ctx.params.id;
  1430. const cid = ctx.params.cid;
  1431. const data = JSON.parse(ctx.request.body.data);
  1432. const result = await ctx.service.change.isRepeat(cid, data.p_code, tenderId);
  1433. if (result) {
  1434. throw '该变更令号(批复编号)已使用';
  1435. }
  1436. } catch (err) {
  1437. responseData.err = 1;
  1438. responseData.msg = err;
  1439. }
  1440. ctx.body = responseData;
  1441. }
  1442. /**
  1443. * 拷贝其他变更令
  1444. * @param {object} ctx - 全局上下文
  1445. */
  1446. async copyChange(ctx) {
  1447. const responseData = {
  1448. err: 0,
  1449. msg: '',
  1450. data: '',
  1451. };
  1452. try {
  1453. const cid = ctx.params.cid;
  1454. const copy_cid = JSON.parse(ctx.request.body.data);
  1455. const result = await ctx.service.change.handleCopyChange(cid, copy_cid);
  1456. if (!result) {
  1457. responseData.err = 1;
  1458. responseData.msg = '拷贝失败!';
  1459. }
  1460. } catch (error) {
  1461. responseData.err = 1;
  1462. responseData.msg = error;
  1463. }
  1464. ctx.body = responseData;
  1465. }
  1466. /**
  1467. * 修订 详细页面(Get)
  1468. * @param ctx
  1469. * @return {Promise<void>}
  1470. */
  1471. async reviseInfo(ctx) {
  1472. try {
  1473. if (!ctx.session.sessionProject.page_show.openChangeRevise) {
  1474. throw '该功能已关闭';
  1475. }
  1476. const change = ctx.change;
  1477. let edit = true;
  1478. let changing = false;
  1479. if (change.status !== audit.flow.status.uncheck && change.status !== audit.flow.status.back && change.status !== audit.flow.status.revise) {
  1480. // throw '本条变更审批中或已完成,无法操作台账数据';
  1481. edit = false;
  1482. changing = true;
  1483. }
  1484. let revising = false;
  1485. // 判断是否在修订中,是则无法操作本页
  1486. const lastRevise = await ctx.service.ledgerRevise.getLastestRevise(ctx.tender.id);
  1487. if (lastRevise && lastRevise.status !== audit.revise.status.checked) {
  1488. // throw '台账修订中,无法操作台账数据';
  1489. edit = false;
  1490. revising = true;
  1491. }
  1492. const renderData = await this._getDefaultReviseInfoData(ctx, change, edit);
  1493. // 台账只读、使用数据
  1494. renderData.readOnly = !edit;
  1495. renderData.changing = changing;
  1496. renderData.revising = revising;
  1497. await this.layout('change/revise.ejs', renderData, 'change/revise_modal.ejs');
  1498. } catch (err) {
  1499. this.log(err);
  1500. ctx.redirect(ctx.request.header.referer);
  1501. }
  1502. }
  1503. async _getDefaultReviseInfoData(ctx, change, edit) {
  1504. const [ledgerSpread, posSpread] = this._getSpreadSetting(change, edit);
  1505. const sjsRela = await this.ctx.service.project.getSjsRela(ctx.session.sessionProject.id);
  1506. this.ctx.helper.refreshSpreadShow(sjsRela.ledgerCol, [ledgerSpread, posSpread]);
  1507. const [stdBills, stdChapters] = await this.ctx.service.valuation.getValuationStdList(
  1508. ctx.tender.data.valuation, ctx.tender.data.measure_type);
  1509. return {
  1510. change,
  1511. tender: ctx.tender.data,
  1512. ledgerSpread, posSpread, tenderMenu, measureType,
  1513. preUrl: '/tender/' + ctx.tender.id + '/change/' + ctx.change.cid + '/information',
  1514. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.revise),
  1515. stdBills,
  1516. auditConst: audit.flow,
  1517. audit: audit.flow,
  1518. stdChapters,
  1519. };
  1520. }
  1521. /**
  1522. * 获取SpreadSetting
  1523. * @private
  1524. */
  1525. _getSpreadSetting(change, edit) {
  1526. const _ = this.app._;
  1527. function removeFieldCols(setting, cols) {
  1528. _.remove(setting.cols, function(c) {
  1529. return cols.indexOf(c.field) > -1;
  1530. });
  1531. }
  1532. const tender = this.ctx.tender;
  1533. const setting = tender.data.measure_type === measureType.tz.value
  1534. ? (tender.info.display.ledger.clQty ? spreadConst.withCl : spreadConst.withoutCl)
  1535. : (tender.info.display.ledger.clQty ? spreadConst.withClGcl : spreadConst.withoutClGcl);
  1536. const ledger = JSON.parse(JSON.stringify(setting.ledger));
  1537. const pos = setting.pos ? JSON.parse(JSON.stringify(setting.pos)) : spreadConst.blank;
  1538. // if (change.status === audit.flow.status.checking || change.status === audit.flow.status.checked) {
  1539. // ledger.readOnly = true;
  1540. // pos.readOnly = true;
  1541. // }
  1542. if (!edit) {
  1543. ledger.readOnly = true;
  1544. pos.readOnly = true;
  1545. }
  1546. if (tender.data.measure_type === measureType.tz.value) {
  1547. removeFieldCols(ledger, spreadConst.filterCols.tzWithoutCols);
  1548. }
  1549. if (!tender.info.display.ledger.dgnQty) {
  1550. removeFieldCols(ledger, spreadConst.filterCols.dgnCols);
  1551. }
  1552. return [ledger, pos];
  1553. }
  1554. async updateRevise(ctx) {
  1555. try {
  1556. if (!ctx.tender.data) throw '标段数据错误';
  1557. if (!ctx.session.sessionProject.page_show.openChangeRevise) {
  1558. throw '该功能已关闭';
  1559. }
  1560. const data = JSON.parse(ctx.request.body.data);
  1561. if (!data.postType || !data.postData) throw '数据错误';
  1562. const responseData = { err: 0, msg: '', data: {} };
  1563. const change = ctx.change;
  1564. if (change.status !== audit.flow.status.uncheck && change.status !== audit.flow.status.back && change.status !== audit.flow.status.revise) {
  1565. throw '该变更令正在审批中或已完成,无法操作新增部位数据';
  1566. }
  1567. // 判断是否在修订中,是则无法操作本页
  1568. const lastRevise = await ctx.service.ledgerRevise.getLastestRevise(ctx.tender.id);
  1569. if (lastRevise && lastRevise.status !== audit.revise.status.checked) {
  1570. throw '台账修订中,无法操作新增部位数据';
  1571. }
  1572. // const revise = await this.checkRevise(ctx);
  1573. //
  1574. switch (data.postType) {
  1575. case 'add':
  1576. case 'delete':
  1577. case 'up-move':
  1578. case 'down-move':
  1579. case 'up-level':
  1580. case 'down-level':
  1581. responseData.data = await this._billsBase(ctx, data.postType, data.postData);
  1582. break;
  1583. case 'update':
  1584. ctx.helper.checkDgnQtyPrecision(data.postData);
  1585. responseData.data = await ctx.service.changeLedger.updateCalc(ctx.tender.id, data.postData);
  1586. break;
  1587. case 'paste-block':
  1588. responseData.data = await this._pasteBlock(ctx, data.postData);
  1589. break;
  1590. case 'add-std':
  1591. responseData.data = await this._addStd(ctx, data.postData);
  1592. break;
  1593. case 'add-deal':
  1594. responseData.data = await this._addDeal(ctx, data.postData);
  1595. break;
  1596. // case 'batch-insert':
  1597. // responseData.data = await this._batchInsert(ctx, data.postData);
  1598. // break;
  1599. case 'pos':
  1600. responseData.data = await this._updatePos(ctx, data);
  1601. break;
  1602. default:
  1603. throw '未知操作';
  1604. }
  1605. ctx.body = responseData;
  1606. } catch (err) {
  1607. this.log(err);
  1608. ctx.body = this.ajaxErrorBody(err, '数据错误');
  1609. }
  1610. }
  1611. async _billsBase(ctx, type, data) {
  1612. if (isNaN(data.id) || data.id <= 0) throw '数据错误';
  1613. if (type !== 'add') {
  1614. if (isNaN(data.count) || data.count <= 0) data.count = 1;
  1615. }
  1616. switch (type) {
  1617. case 'add':
  1618. return await ctx.service.changeLedger.addNodeBatch(ctx.tender.id, data.id, { ccid: ctx.change.cid, check_calc: 1 }, data.count);
  1619. case 'delete':
  1620. return await ctx.service.changeLedger.delete(ctx.tender.id, data.id, data.count);
  1621. case 'up-move':
  1622. return await ctx.service.changeLedger.upMoveNode(ctx.tender.id, data.id, data.count);
  1623. case 'down-move':
  1624. return await ctx.service.changeLedger.downMoveNode(ctx.tender.id, data.id, data.count);
  1625. case 'up-level':
  1626. return await ctx.service.changeLedger.upLevelNode(ctx.tender.id, data.id, data.count);
  1627. case 'down-level':
  1628. return await ctx.service.changeLedger.downLevelNode(ctx.tender.id, data.id, data.count);
  1629. default:
  1630. throw '未知操作';
  1631. }
  1632. }
  1633. /**
  1634. * 复制粘贴整块
  1635. *
  1636. * @param ctx
  1637. * @return {Promise<void>}
  1638. */
  1639. async _pasteBlock(ctx, data) {
  1640. if ((isNaN(data.id) || data.id <= 0) ||
  1641. (!data.tid && data.tid <= 0) ||
  1642. (!data.block || data.block.length <= 0)) throw '参数错误';
  1643. return await ctx.service.changeLedger.pasteBlockData(ctx.tender.id, data.id, data.block);
  1644. }
  1645. /**
  1646. * 从标准项目表添加数据
  1647. * @param ctx
  1648. * @return {Promise<void>}
  1649. */
  1650. async _addStd(ctx, data) {
  1651. if ((isNaN(data.id) || data.id <= 0) || !data.stdType || !data.stdNode) throw '参数错误';
  1652. // todo 校验项目是否使用该库的权限
  1653. let stdLib,
  1654. addType;
  1655. switch (data.stdType) {
  1656. case 'xmj':
  1657. stdLib = ctx.service.stdXmj;
  1658. addType = stdDataAddType.withParent;
  1659. break;
  1660. case 'gcl':
  1661. stdLib = ctx.service.stdGcl;
  1662. const selectNode = await ctx.service.changeLedger.getDataByNodeId(ctx.tender.id, data.id) || await ctx.service.ledger.getDataByNodeId(ctx.tender.id, data.id);
  1663. if (selectNode.b_code) {
  1664. addType = stdDataAddType.next;
  1665. } else {
  1666. addType = stdDataAddType.child;
  1667. }
  1668. break;
  1669. default:
  1670. throw '未知标准库';
  1671. }
  1672. const stdData = await stdLib.getDataByDataId(data.stdLibId, data.stdNode);
  1673. switch (addType) {
  1674. case stdDataAddType.child:
  1675. return await ctx.service.changeLedger.addStdNodeAsChild(ctx.tender.id, data.id, stdData, ctx.change.cid);
  1676. case stdDataAddType.next:
  1677. return await ctx.service.changeLedger.addStdNode(ctx.tender.id, data.id, stdData, ctx.change.cid);
  1678. case stdDataAddType.withParent:
  1679. return await ctx.service.changeLedger.addStdNodeWithParent(ctx.tender.id, stdData, stdLib, ctx.change.cid);
  1680. default:
  1681. throw '未知添加方式';
  1682. }
  1683. }
  1684. /**
  1685. * 从签约清单添加节点
  1686. * @param ctx
  1687. * @return {Promise<void>}
  1688. */
  1689. async _addDeal(ctx, data) {
  1690. if (!data.type || !data.dealBills) throw '数据错误';
  1691. data.dealBills.unit_price = this.ctx.helper.round(data.dealBills.unit_price, ctx.tender.info.decimal.up);
  1692. if (data.type === 'child') {
  1693. return await ctx.service.changeLedger.addChild(ctx.tender.id, data.id, data.dealBills, ctx.change.cid);
  1694. } else if (data.type === 'next') {
  1695. data.dealBills.ccid = ctx.change.cid;
  1696. return await ctx.service.changeLedger.addNode(ctx.tender.id, data.id, data.dealBills);
  1697. }
  1698. throw '数据错误';
  1699. }
  1700. /**
  1701. * 批量插入数据
  1702. *
  1703. * data = {id, batchData, batchType}
  1704. * data.batchType = 'batchInsertChild'/'batchInsertNext'
  1705. * data.batchData = [{name, children}] -- 项目节列表
  1706. * data.batchData.children = [{code, name, unit, unit_price, quantity}] -- 工程量清单列表
  1707. *
  1708. * @param ctx
  1709. * @return {Promise<void>}
  1710. */
  1711. async _batchInsert(ctx, data) {
  1712. if ((isNaN(data.id) || data.id <= 0) || !data.batchType) throw '参数错误';
  1713. switch (data.batchType) {
  1714. case 'child':
  1715. return await ctx.service.changeLedger.batchInsertChild(ctx.tender.id, data.id, data.batchData);
  1716. case 'next':
  1717. return await ctx.service.changeLedger.batchInsertNext(ctx.tender.id, data.id, data.batchData);
  1718. default:
  1719. throw '参数错误';
  1720. }
  1721. }
  1722. async _updatePos(ctx, data) {
  1723. await this.checkMeasureType(measureType.tz.value);
  1724. if (!data.posPostType) throw '数据错误';
  1725. switch (data.posPostType) {
  1726. case 'add':
  1727. return await this.ctx.service.changePos.addPos(ctx.tender.id, ctx.change.cid, data.postData);
  1728. case 'update':
  1729. if (data.postData instanceof Array) {
  1730. return await this.ctx.service.changePos.updatePosArr(ctx.tender.id, data.postData);
  1731. }
  1732. return await this.ctx.service.changePos.updatePos(ctx.tender.id, data.postData);
  1733. case 'delete':
  1734. return await this.ctx.service.changePos.deletePos(ctx.tender.id, data.postData);
  1735. case 'paste':
  1736. return await this.ctx.service.changePos.pastePosData(ctx.tender.id, ctx.change.cid, data.postData);
  1737. default:
  1738. throw '未知操作';
  1739. }
  1740. }
  1741. async _filterChangesProject(ctx, status = 0) {
  1742. const tenderId = ctx.params.id;
  1743. ctx.session.sessionUser.tenderId = tenderId;
  1744. const tender = await this.service.tender.getDataById(ctx.tender.id);
  1745. // const tender = ctx.tender;
  1746. // const tenderList = await this.service.tender.getList();
  1747. const page = ctx.page;
  1748. const sorts = ctx.query.sort ? ctx.query.sort : 0;
  1749. const orders = ctx.query.order ? ctx.query.order : 0;
  1750. const changes = await ctx.service.changeProject.getListByStatus(tender.id, status, 1, sorts, orders);
  1751. const total = await ctx.service.changeProject.getCountByStatus(tender.id, status);
  1752. for (const c of changes) {
  1753. c.curAuditor = await ctx.service.changeProjectAudit.getAuditorByStatus(c.id, c.status, c.times);
  1754. }
  1755. const accountInfo = await this.ctx.service.projectAccount.getDataById(this.ctx.session.sessionUser.accountId);
  1756. const userPermission = accountInfo !== undefined && accountInfo.permission !== ''
  1757. ? JSON.parse(accountInfo.permission) : null;
  1758. // 分页相关
  1759. const pageInfo = {
  1760. page,
  1761. total: Math.ceil(total / app.config.pageSize),
  1762. queryData: JSON.stringify(ctx.urlInfo.query),
  1763. };
  1764. const filter = JSON.parse(JSON.stringify(audit.changeProject.filter));
  1765. filter.count = [];
  1766. filter.count[filter.status.pending] = await ctx.service.changeProject.getCountByStatus(tender.id, filter.status.pending);// await ctx.service.change.pendingDatas(tender.id, ctx.session.sessionUser.accountId);
  1767. filter.count[filter.status.uncheck] = await ctx.service.changeProject.getCountByStatus(tender.id, filter.status.uncheck);// await ctx.service.change.checkingDatas(tender.id, ctx.session.sessionUser.accountId);
  1768. filter.count[filter.status.checking] = await ctx.service.changeProject.getCountByStatus(tender.id, filter.status.checking);// await ctx.service.change.checkedDatas(tender.id, ctx.session.sessionUser.accountId);
  1769. filter.count[filter.status.checked] = await ctx.service.changeProject.getCountByStatus(tender.id, filter.status.checked);// await ctx.service.change.pendingDatas(tender.id, ctx.session.sessionUser.accountId);
  1770. filter.count[filter.status.checkNo] = await ctx.service.changeProject.getCountByStatus(tender.id, filter.status.checkNo);// await ctx.service.change.pendingDatas(tender.id, ctx.session.sessionUser.accountId);
  1771. let codeRule = [];
  1772. let c_connector = '1';
  1773. let c_rule_first = 1;
  1774. const rule_type = tender.user_id === ctx.session.sessionUser.accountId ? 'suggestion' : 'will';
  1775. if (tender.c_code_rules) {
  1776. const c_code_rules = JSON.parse(tender.c_code_rules);
  1777. codeRule = c_code_rules[rule_type + '_rule'] !== undefined ? c_code_rules[rule_type + '_rule'] : [];
  1778. c_connector = c_code_rules[rule_type + '_connector'] !== undefined ? c_code_rules[rule_type + '_connector'] : '1';
  1779. c_rule_first = c_code_rules[rule_type + '_rule_first'] !== undefined ? c_code_rules[rule_type + '_rule_first'] : 1;
  1780. }
  1781. for (const rule of codeRule) {
  1782. switch (rule.rule_type) {
  1783. case codeRuleConst.measure.ruleType.dealCode:
  1784. rule.preview = ctx.tender.info.deal_info.dealCode;
  1785. break;
  1786. case codeRuleConst.measure.ruleType.tenderName:
  1787. rule.preview = tender.name;
  1788. break;
  1789. case codeRuleConst.measure.ruleType.inDate:
  1790. rule.preview = moment().format('YYYY');
  1791. break;
  1792. case codeRuleConst.measure.ruleType.text:
  1793. rule.preview = rule.text;
  1794. break;
  1795. case codeRuleConst.measure.ruleType.addNo:
  1796. const s = '0000000000';
  1797. rule.preview = s.substr(s.length - rule.format);
  1798. break;
  1799. default: break;
  1800. }
  1801. }
  1802. const renderData = {
  1803. uid: ctx.session.sessionUser.accountId,
  1804. userPermission,
  1805. tender,
  1806. pageInfo,
  1807. changes,
  1808. status,
  1809. rule_type,
  1810. codeRule,
  1811. c_connector,
  1812. c_rule_first,
  1813. filter,
  1814. ruleType: codeRuleConst.ruleType[rule_type],
  1815. dealCode: ctx.tender.info.deal_info.dealCode,
  1816. auditConst: audit.changeProject,
  1817. ruleConst: codeRuleConst.measure,
  1818. changeConst,
  1819. tenderMenu: this.menu.tenderMenu,
  1820. preUrl: '/tender/' + tender.id,
  1821. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.project),
  1822. };
  1823. await this.layout('change/project.ejs', renderData, 'change/project_modal.ejs');
  1824. }
  1825. /**
  1826. * 变更立项列表 页面 (Get)
  1827. *
  1828. * @param {Object} ctx - egg全局变量
  1829. * @return {void}
  1830. */
  1831. async project(ctx) {
  1832. try {
  1833. await this._filterChangesProject(ctx);
  1834. } catch (err) {
  1835. this.log(err);
  1836. ctx.redirect('/dashboard');
  1837. }
  1838. }
  1839. /**
  1840. * 新增变更立项 (Post)
  1841. *
  1842. * @param {Object} ctx - egg全局变量
  1843. * @return {void}
  1844. */
  1845. async projectAdd(ctx) {
  1846. try {
  1847. const tenderId = ctx.params.id;
  1848. if (!tenderId) {
  1849. throw '当前未打开标段';
  1850. }
  1851. const data = JSON.parse(ctx.request.body.data);
  1852. if (!data.code || data.code === '' || !data.name || data.name === '') {
  1853. throw '变更立项书编号不能为空';
  1854. }
  1855. const change = await ctx.service.changeProject.add(tenderId, ctx.session.sessionUser.accountId, data.code, data.name);
  1856. ctx.body = { err: 0, msg: '', data: change };
  1857. } catch (err) {
  1858. this.log(err);
  1859. ctx.body = { err: 1, msg: err.toString() };
  1860. }
  1861. }
  1862. /**
  1863. * 删除变更立项
  1864. * @param {Object} ctx - egg全局变量
  1865. * @return {void}
  1866. */
  1867. async projectDelete(ctx) {
  1868. try {
  1869. const result = await ctx.service.changeProject.delete(ctx.request.body.cpid);
  1870. if (!result) {
  1871. throw '删除变更立项失败';
  1872. }
  1873. ctx.redirect(ctx.request.header.referer);
  1874. } catch (err) {
  1875. console.log(err);
  1876. ctx.redirect(ctx.request.header.referer);
  1877. }
  1878. }
  1879. /**
  1880. * 变更管理 状态筛选 页面 (Get)
  1881. * @param {Object} ctx - egg全局变量
  1882. * @return {void}
  1883. */
  1884. async projectStatus(ctx) {
  1885. try {
  1886. const status = parseInt(ctx.params.status);
  1887. await this._filterChangesProject(ctx, status);
  1888. } catch (err) {
  1889. this.logger.error(err);
  1890. ctx.redirect('/tender/' + ctx.params.id + '/change/project');
  1891. }
  1892. }
  1893. /**
  1894. * 获取审批界面所需的 原报、审批人数据等
  1895. * @param ctx
  1896. * @return {Promise<void>}
  1897. * @private
  1898. */
  1899. async _getChangeProjectAuditViewData(ctx) {
  1900. const auditConst = audit.changeProject;
  1901. const times = ctx.change.status === auditConst.status.back ? ctx.change.times - 1 : ctx.change.times;
  1902. ctx.change.user = await ctx.service.projectAccount.getAccountInfoById(ctx.change.uid);
  1903. ctx.change.auditHistory = [];
  1904. if (times >= 1) {
  1905. for (let i = 1; i <= times; i++) {
  1906. ctx.change.auditHistory.push(await ctx.service.changeProjectAudit.getAuditors(ctx.change.id, i));
  1907. }
  1908. }
  1909. // 获取审批流程中左边列表
  1910. ctx.change.auditors2 = ctx.change.status === auditConst.status.back && ctx.change.uid !== ctx.session.sessionUser.accountId ?
  1911. await ctx.service.changeProjectAudit.getAuditorsWithOwner(ctx.change.id, times) :
  1912. await ctx.service.changeProjectAudit.getAuditorsWithOwner(ctx.change.id, ctx.change.times);
  1913. if (ctx.change.status === auditConst.status.uncheck || ctx.change.status === auditConst.status.back) {
  1914. ctx.change.auditorList = await ctx.service.changeProjectAudit.getAuditors(ctx.change.id, ctx.change.times);
  1915. }
  1916. ctx.change.xsAuditors = await ctx.service.changeProjectXsAudit.getAuditList(ctx.change.id);
  1917. }
  1918. async projectInformation(ctx) {
  1919. try {
  1920. const whiteList = this.ctx.app.config.multipart.whitelist;
  1921. const tender = await ctx.service.tender.getDataById(ctx.tender.id);
  1922. // 获取附件列表
  1923. const fileList = await ctx.service.changeProjectAtt.getAllChangeProjectAtt(ctx.tender.id, ctx.change.id);
  1924. await this._getChangeProjectAuditViewData(ctx);
  1925. const renderData = {
  1926. tender,
  1927. change: ctx.change,
  1928. changeConst,
  1929. auditConst: audit.changeProject,
  1930. fileList,
  1931. whiteList,
  1932. returnUrl: this.app._.includes(ctx.request.headers.referer, '/tender/' + ctx.tender.id + '/change/project') ? ctx.request.headers.referer : null,
  1933. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.project_information),
  1934. preUrl: '/tender/' + ctx.tender.id + '/change/project/' + ctx.change.id + '/information',
  1935. };
  1936. // if ((ctx.change.status === audit.changeProject.status.uncheck || ctx.change.status === audit.changeProject.status.back) && (ctx.session.sessionUser.accountId === ctx.change.uid || ctx.tender.isTourist)) {
  1937. // data.accountGroup = accountGroup;
  1938. // 获取所有项目参与者
  1939. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  1940. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  1941. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  1942. });
  1943. renderData.accountList = accountList;
  1944. renderData.accountGroup = accountGroup.map((item, idx) => {
  1945. const groupList = accountList.filter(item => item.account_group === idx);
  1946. return { groupName: item, groupList };
  1947. });
  1948. // }
  1949. await this.layout('change/project_information.ejs', renderData, 'change/project_information_modal.ejs');
  1950. } catch (err) {
  1951. this.log(err);
  1952. ctx.redirect('/tender/' + ctx.params.id + '/change');
  1953. }
  1954. }
  1955. // 审批相关
  1956. /**
  1957. * 添加审批人
  1958. * @param ctx
  1959. * @return {Promise<void>}
  1960. */
  1961. async addProjectAudit(ctx) {
  1962. try {
  1963. const auditConst = audit.changeProject;
  1964. const data = JSON.parse(ctx.request.body.data);
  1965. const id = this.app._.toInteger(data.auditorId);
  1966. if (isNaN(id) || id <= 0) {
  1967. throw '参数错误';
  1968. }
  1969. // 检查权限等
  1970. if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
  1971. throw '您无权添加审核人';
  1972. }
  1973. if (ctx.change.status === auditConst.status.checking || ctx.change.status === auditConst.status.checked) {
  1974. throw '当前不允许添加审核人';
  1975. }
  1976. // 判断是否是协审人,是则无法添加到审批人中
  1977. const xsAuditorList = await ctx.service.changeProjectXsAudit.getAuditList(ctx.change.id);
  1978. const xsAidList = this.app._.map(xsAuditorList, 'aid');
  1979. if (this.app._.indexOf(xsAidList, id) !== -1) {
  1980. throw '该用户已添加到协审人列表中,请删除该协审人再添加';
  1981. }
  1982. ctx.change.auditorList = await ctx.service.changeProjectAudit.getAuditors(ctx.change.id, ctx.change.times);
  1983. // 检查审核人是否已存在
  1984. const exist = this.app._.find(ctx.change.auditorList, { aid: id });
  1985. if (exist) {
  1986. throw '该审核人已存在,请勿重复添加';
  1987. }
  1988. // const shenpiInfo = await ctx.service.shenpiAudit.getDataByCondition({ tid: ctx.tender.id, sp_type: shenpiConst.sp_type.material, sp_status: shenpiConst.sp_status.gdzs });
  1989. // const is_gdzs = shenpiInfo && ctx.tender.info.shenpi.material === shenpiConst.sp_status.gdzs ? 1 : 0;
  1990. const result = await ctx.service.changeProjectAudit.addAuditor(ctx.change.id, id, ctx.change.times);
  1991. if (!result) {
  1992. throw '添加审核人失败';
  1993. }
  1994. const auditors = await ctx.service.changeProjectAudit.getAuditorsWithOwner(ctx.change.id, ctx.change.times);
  1995. ctx.body = { err: 0, msg: '', data: auditors };
  1996. } catch (err) {
  1997. this.log(err);
  1998. ctx.body = { err: 1, msg: err.toString(), data: null };
  1999. }
  2000. }
  2001. /**
  2002. * 移除审批人
  2003. * @param ctx
  2004. * @return {Promise<void>}
  2005. */
  2006. async deleteProjectAudit(ctx) {
  2007. try {
  2008. const data = JSON.parse(ctx.request.body.data);
  2009. const id = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
  2010. if (isNaN(id) || id <= 0) {
  2011. throw '参数错误';
  2012. }
  2013. const result = await ctx.service.changeProjectAudit.deleteAuditor(ctx.change.id, id, ctx.change.times);
  2014. if (!result) {
  2015. throw '移除审核人失败';
  2016. }
  2017. const auditors = await ctx.service.changeProjectAudit.getAuditors(ctx.change.id, ctx.change.times);
  2018. ctx.body = { err: 0, msg: '', data: auditors };
  2019. } catch (err) {
  2020. ctx.body = { err: 1, msg: err.toString(), data: null };
  2021. }
  2022. }
  2023. /**
  2024. * 添加协审人
  2025. * @param ctx
  2026. * @return {Promise<void>}
  2027. */
  2028. async addProjectXsAudit(ctx) {
  2029. try {
  2030. const auditConst = audit.changeProject;
  2031. const data = JSON.parse(ctx.request.body.data);
  2032. const id = this.app._.toInteger(data.auditorId);
  2033. if (isNaN(id) || id <= 0) {
  2034. throw '参数错误';
  2035. }
  2036. const times = ctx.change.status === auditConst.status.back ? ctx.change.times - 1 : ctx.change.times;
  2037. const spAuditors = await ctx.service.changeProjectAudit.getAuditorsWithOwner(ctx.change.id, times);
  2038. const aidLists = this.app._.map(spAuditors, 'aid');
  2039. // 检查权限等
  2040. if (this.app._.indexOf(aidLists, ctx.session.sessionUser.accountId) === -1) {
  2041. throw '您无权添加协审人';
  2042. }
  2043. // 如果已经是审批人则无法添加此人为协审
  2044. if (this.app._.indexOf(aidLists, id) !== -1) {
  2045. throw '该用户为审批人或原报,无需添加至协审人';
  2046. }
  2047. const auditorList = await ctx.service.changeProjectXsAudit.getAuditList(ctx.change.id);
  2048. // 检查审核人是否已存在
  2049. const exist = this.app._.find(auditorList, { aid: id });
  2050. if (exist) {
  2051. throw '该协审人已存在,请勿重复添加';
  2052. }
  2053. const result = await ctx.service.changeProjectXsAudit.addAuditor(ctx.change.id, id);
  2054. if (!result) {
  2055. throw '添加协审人失败';
  2056. }
  2057. const auditors = await ctx.service.changeProjectXsAudit.getOneAudit(ctx.change.id, id);
  2058. ctx.body = { err: 0, msg: '', data: auditors };
  2059. } catch (err) {
  2060. this.log(err);
  2061. ctx.body = { err: 1, msg: err.toString(), data: null };
  2062. }
  2063. }
  2064. /**
  2065. * 移除协审人
  2066. * @param ctx
  2067. * @return {Promise<void>}
  2068. */
  2069. async deleteProjectXsAudit(ctx) {
  2070. try {
  2071. const data = JSON.parse(ctx.request.body.data);
  2072. const id = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
  2073. if (isNaN(id) || id <= 0) {
  2074. throw '参数错误';
  2075. }
  2076. const result = await ctx.service.changeProjectXsAudit.deleteAuditor(ctx.change.id, id);
  2077. if (!result) {
  2078. throw '移除协审人失败';
  2079. }
  2080. const auditors = await ctx.service.changeProjectXsAudit.getAuditList(ctx.change.id);
  2081. ctx.body = { err: 0, msg: '', data: auditors };
  2082. } catch (err) {
  2083. ctx.body = { err: 1, msg: err.toString(), data: null };
  2084. }
  2085. }
  2086. /**
  2087. * 上传附件
  2088. * @param {*} ctx 上下文
  2089. */
  2090. async uploadProjectFile(ctx) {
  2091. let stream;
  2092. try {
  2093. const auditConst = audit.changeProject;
  2094. // this._checkAdvanceFileCanModify(ctx);
  2095. const parts = this.ctx.multipart({
  2096. autoFields: true,
  2097. });
  2098. const files = [];
  2099. const create_time = Date.parse(new Date()) / 1000;
  2100. let idx = 0;
  2101. const extra_upload = ctx.change.status === auditConst.status.checked;
  2102. while ((stream = await parts()) !== undefined) {
  2103. if (!stream.filename) {
  2104. // 如果没有传入直接返回
  2105. return;
  2106. }
  2107. const fileInfo = path.parse(stream.filename);
  2108. const filepath = `app/public/upload/${this.ctx.tender.id.toString()}/change_project/fujian_${create_time + idx.toString() + fileInfo.ext}`;
  2109. // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, 'app', filepath));
  2110. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  2111. files.push({ filepath, name: stream.filename, ext: fileInfo.ext });
  2112. ++idx;
  2113. stream && (await sendToWormhole(stream));
  2114. }
  2115. const in_time = new Date();
  2116. const payload = files.map(file => {
  2117. let idx;
  2118. if (Array.isArray(parts.field.name)) {
  2119. idx = parts.field.name.findIndex(name => name === file.name);
  2120. } else {
  2121. idx = 'isString';
  2122. }
  2123. const newFile = {
  2124. tid: ctx.tender.id,
  2125. cpid: ctx.change.id,
  2126. uid: ctx.session.sessionUser.accountId,
  2127. filename: file.name,
  2128. fileext: file.ext,
  2129. filesize: ctx.helper.bytesToSize(idx === 'isString' ? parts.field.size : parts.field.size[idx]),
  2130. filepath: file.filepath,
  2131. upload_time: in_time,
  2132. extra_upload,
  2133. };
  2134. return newFile;
  2135. });
  2136. // 执行文件信息写入数据库
  2137. await ctx.service.changeProjectAtt.saveFileMsgToDb(payload);
  2138. // 将最新的当前标段的所有文件信息返回
  2139. const data = await ctx.service.changeProjectAtt.getAllChangeProjectAtt(ctx.tender.id, ctx.change.id);
  2140. ctx.body = { err: 0, msg: '', data };
  2141. } catch (err) {
  2142. stream && (await sendToWormhole(stream));
  2143. this.log(err);
  2144. ctx.body = { err: 1, msg: err.toString(), data: null };
  2145. }
  2146. }
  2147. /**
  2148. * 删除附件
  2149. * @param {Ojbect} ctx 上下文
  2150. */
  2151. async deleteProjectFile(ctx) {
  2152. try {
  2153. const { id } = JSON.parse(ctx.request.body.data);
  2154. const fileInfo = await ctx.service.changeProjectAtt.getDataById(id);
  2155. if (fileInfo || Object.keys(fileInfo).length) {
  2156. // 先删除文件
  2157. // await fs.unlinkSync(path.resolve(this.app.baseDir, './app', fileInfo.filepath));
  2158. await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
  2159. // 再删除数据库
  2160. await ctx.service.changeProjectAtt.delete(id);
  2161. } else {
  2162. throw '不存在该文件';
  2163. }
  2164. const data = await ctx.service.changeProjectAtt.getAllChangeProjectAtt(ctx.tender.id, ctx.change.id);
  2165. ctx.body = { err: 0, msg: '请求成功', data };
  2166. } catch (err) {
  2167. this.log(err);
  2168. ctx.body = { err: 1, msg: err.toString(), data: null };
  2169. }
  2170. }
  2171. /**
  2172. * 下载附件
  2173. * @param {Object} ctx - egg全局变量
  2174. * @return {void}
  2175. */
  2176. async downloadProjectFile(ctx) {
  2177. const id = ctx.params.fid;
  2178. if (id) {
  2179. try {
  2180. const fileInfo = await ctx.service.changeProjectAtt.getDataById(id);
  2181. if (fileInfo !== undefined && fileInfo !== '') {
  2182. // const fileName = path.join(__dirname, '../', fileInfo.filepath);
  2183. // 解决中文无法下载问题
  2184. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  2185. let disposition = '';
  2186. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  2187. disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename);
  2188. } else if (userAgent.indexOf('firefox') >= 0) {
  2189. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename) + '"';
  2190. } else {
  2191. /* safari等其他非主流浏览器只能自求多福了 */
  2192. disposition = 'attachment; filename=' + new Buffer(fileInfo.filename).toString('binary');
  2193. }
  2194. ctx.response.set({
  2195. 'Content-Type': 'application/octet-stream',
  2196. 'Content-Disposition': disposition,
  2197. 'Content-Length': fileInfo.filesize,
  2198. });
  2199. // ctx.body = await fs.createReadStream(fileName);
  2200. ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
  2201. } else {
  2202. throw '不存在该文件';
  2203. }
  2204. } catch (err) {
  2205. this.log(err);
  2206. this.setMessage(err.toString(), this.messageType.ERROR);
  2207. }
  2208. }
  2209. }
  2210. async projectInformationSave(ctx) {
  2211. try {
  2212. const data = JSON.parse(ctx.request.body.data);
  2213. if (data.name === 'code') {
  2214. const info = await ctx.service.changeProject.isRepeat(ctx.change.id, data.val, ctx.tender.id, ctx.change.type);
  2215. if (info) {
  2216. throw '该编号已存在';
  2217. }
  2218. }
  2219. const result = await ctx.service.changeProject.saveInfo(ctx.change.id, data);
  2220. if (!result) {
  2221. throw '修改失败';
  2222. }
  2223. ctx.body = { err: 0, msg: '请求成功', data: null };
  2224. } catch (err) {
  2225. this.log(err);
  2226. ctx.body = { err: 1, msg: err.toString(), data: null };
  2227. }
  2228. }
  2229. /**
  2230. * 上报和重新上报
  2231. * @param ctx
  2232. * @return {Promise<void>}
  2233. */
  2234. async startProjectAudit(ctx) {
  2235. try {
  2236. const auditConst = audit.changeProject;
  2237. // 检查权限等
  2238. if (!ctx.change) {
  2239. throw '数据错误';
  2240. }
  2241. if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
  2242. throw '您无权上报该期数据';
  2243. }
  2244. if (ctx.change.status === auditConst.status.checking || ctx.change.status === auditConst.status.checked) {
  2245. throw '该材料调差期数据当前无法上报';
  2246. }
  2247. await ctx.service.changeProjectAudit.start(ctx.change.id, ctx.change.times);
  2248. ctx.redirect(ctx.request.header.referer);
  2249. } catch (err) {
  2250. this.log(err);
  2251. ctx.session.postError = err.toString();
  2252. ctx.redirect(ctx.request.header.referer);
  2253. }
  2254. }
  2255. /**
  2256. * 审批
  2257. * @param ctx
  2258. * @return {Promise<void>}
  2259. */
  2260. async checkProjectAudit(ctx) {
  2261. try {
  2262. const auditConst = audit.changeProject;
  2263. if (!ctx.change || ctx.change.status !== auditConst.status.checking) {
  2264. throw '当前材料调差期数据有误';
  2265. }
  2266. if (!ctx.change.curAuditor || ctx.change.curAuditor.aid !== ctx.session.sessionUser.accountId) {
  2267. throw '您无权进行该操作';
  2268. }
  2269. const data = {
  2270. checkType: parseInt(ctx.request.body.checkType),
  2271. opinion: ctx.request.body.opinion,
  2272. };
  2273. if (!data.checkType || isNaN(data.checkType)) {
  2274. throw '提交数据错误';
  2275. }
  2276. // if (data.checkType === auditConst.status.checkNo) {
  2277. // if (!data.checkType || isNaN(data.checkType)) {
  2278. // throw '提交数据错误';
  2279. // }
  2280. // }
  2281. await ctx.service.changeProjectAudit.check(ctx.change.id, data, ctx.change.times);
  2282. ctx.redirect(ctx.request.header.referer);
  2283. } catch (err) {
  2284. this.log(err);
  2285. ctx.session.postError = err.toString();
  2286. ctx.redirect(ctx.request.header.referer);
  2287. }
  2288. }
  2289. async _filterChangesApply(ctx, status = 0) {
  2290. const tenderId = ctx.params.id;
  2291. ctx.session.sessionUser.tenderId = tenderId;
  2292. const tender = await this.service.tender.getDataById(ctx.tender.id);
  2293. // const tender = ctx.tender;
  2294. // const tenderList = await this.service.tender.getList();
  2295. const page = ctx.page;
  2296. const sorts = ctx.query.sort ? ctx.query.sort : 0;
  2297. const orders = ctx.query.order ? ctx.query.order : 0;
  2298. const changes = await ctx.service.changeApply.getListByStatus(tender.id, status, 1, sorts, orders);
  2299. const total = await ctx.service.changeApply.getCountByStatus(tender.id, status);
  2300. let page_total = 0;
  2301. const tp = await ctx.service.changeApply.getTp(tender.id, status);
  2302. for (const c of changes) {
  2303. c.curAuditor = await ctx.service.changeApplyAudit.getAuditorByStatus(c.id, c.status, c.times);
  2304. page_total = ctx.helper.add(page_total, c.total_price);
  2305. }
  2306. const tender_userInfo = await ctx.service.projectAccount.getDataById(ctx.tender.data.user_id);
  2307. // 分页相关
  2308. const pageInfo = {
  2309. page,
  2310. total: Math.ceil(total / app.config.pageSize),
  2311. queryData: JSON.stringify(ctx.urlInfo.query),
  2312. };
  2313. const filter = JSON.parse(JSON.stringify(audit.changeApply.filter));
  2314. filter.count = [];
  2315. filter.count[filter.status.pending] = await ctx.service.changeApply.getCountByStatus(tender.id, filter.status.pending);// await ctx.service.change.pendingDatas(tender.id, ctx.session.sessionUser.accountId);
  2316. filter.count[filter.status.uncheck] = await ctx.service.changeApply.getCountByStatus(tender.id, filter.status.uncheck);// await ctx.service.change.checkingDatas(tender.id, ctx.session.sessionUser.accountId);
  2317. filter.count[filter.status.checking] = await ctx.service.changeApply.getCountByStatus(tender.id, filter.status.checking);// await ctx.service.change.checkedDatas(tender.id, ctx.session.sessionUser.accountId);
  2318. filter.count[filter.status.checked] = await ctx.service.changeApply.getCountByStatus(tender.id, filter.status.checked);// await ctx.service.change.pendingDatas(tender.id, ctx.session.sessionUser.accountId);
  2319. // filter.count[filter.status.checkNo] = await ctx.service.changeApply.getCountByStatus(tender.id, filter.status.checkNo);// await ctx.service.change.pendingDatas(tender.id, ctx.session.sessionUser.accountId);
  2320. let codeRule = [];
  2321. let c_connector = '1';
  2322. let c_rule_first = 1;
  2323. const rule_type = 'apply';
  2324. if (tender.c_code_rules) {
  2325. const c_code_rules = JSON.parse(tender.c_code_rules);
  2326. codeRule = c_code_rules[rule_type + '_rule'] !== undefined ? c_code_rules[rule_type + '_rule'] : [];
  2327. c_connector = c_code_rules[rule_type + '_connector'] !== undefined ? c_code_rules[rule_type + '_connector'] : '1';
  2328. c_rule_first = c_code_rules[rule_type + '_rule_first'] !== undefined ? c_code_rules[rule_type + '_rule_first'] : 1;
  2329. }
  2330. for (const rule of codeRule) {
  2331. switch (rule.rule_type) {
  2332. case codeRuleConst.measure.ruleType.dealCode:
  2333. rule.preview = ctx.tender.info.deal_info.dealCode;
  2334. break;
  2335. case codeRuleConst.measure.ruleType.tenderName:
  2336. rule.preview = tender.name;
  2337. break;
  2338. case codeRuleConst.measure.ruleType.inDate:
  2339. rule.preview = moment().format('YYYY');
  2340. break;
  2341. case codeRuleConst.measure.ruleType.text:
  2342. rule.preview = rule.text;
  2343. break;
  2344. case codeRuleConst.measure.ruleType.addNo:
  2345. const s = '0000000000';
  2346. rule.preview = s.substr(s.length - rule.format);
  2347. break;
  2348. default: break;
  2349. }
  2350. }
  2351. const changeProjectList = await ctx.service.changeProject.getAllDataByCondition({ where: { tid: tender.id, status: audit.changeProject.status.checked } });
  2352. const allProjectCodes = await ctx.service.changeApply.getAllDataByCondition({
  2353. columns: ['project_code'],
  2354. where: {
  2355. tid: tender.id,
  2356. },
  2357. });
  2358. const pcLists = allProjectCodes.length > 0 ? ctx.app._.uniq(ctx.app._.map(allProjectCodes, 'project_code')) : [];
  2359. const renderData = {
  2360. uid: ctx.session.sessionUser.accountId,
  2361. tender,
  2362. pageInfo,
  2363. changes,
  2364. status,
  2365. rule_type,
  2366. codeRule,
  2367. c_connector,
  2368. c_rule_first,
  2369. filter,
  2370. apply_username: tender_userInfo ? tender_userInfo.name : null,
  2371. ruleType: codeRuleConst.ruleType[rule_type],
  2372. dealCode: ctx.tender.info.deal_info.dealCode,
  2373. auditConst: audit.changeApply,
  2374. ruleConst: codeRuleConst.measure,
  2375. changeConst,
  2376. changeProjectList,
  2377. pcLists,
  2378. tp,
  2379. page_total,
  2380. tenderMenu: this.menu.tenderMenu,
  2381. preUrl: '/tender/' + tender.id,
  2382. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.apply),
  2383. };
  2384. await this.layout('change/apply.ejs', renderData, 'change/apply_modal.ejs');
  2385. }
  2386. /**
  2387. * 变更申请列表 页面 (Get)
  2388. *
  2389. * @param {Object} ctx - egg全局变量
  2390. * @return {void}
  2391. */
  2392. async apply(ctx) {
  2393. try {
  2394. await this._filterChangesApply(ctx);
  2395. } catch (err) {
  2396. this.log(err);
  2397. ctx.redirect('/dashboard');
  2398. }
  2399. }
  2400. /**
  2401. * 变更管理 状态筛选 页面 (Get)
  2402. * @param {Object} ctx - egg全局变量
  2403. * @return {void}
  2404. */
  2405. async applyStatus(ctx) {
  2406. try {
  2407. const status = parseInt(ctx.params.status);
  2408. await this._filterChangesApply(ctx, status);
  2409. } catch (err) {
  2410. this.logger.error(err);
  2411. ctx.redirect('/tender/' + ctx.params.id + '/change/apply');
  2412. }
  2413. }
  2414. /**
  2415. * 新增变更立项 (Post)
  2416. *
  2417. * @param {Object} ctx - egg全局变量
  2418. * @return {void}
  2419. */
  2420. async applyAdd(ctx) {
  2421. try {
  2422. const tenderId = ctx.params.id;
  2423. if (!tenderId) {
  2424. throw '当前未打开标段';
  2425. }
  2426. const data = JSON.parse(ctx.request.body.data);
  2427. if (!data.code || data.code === '') {
  2428. throw '变更申请编号不能为空';
  2429. }
  2430. const change = await ctx.service.changeApply.add(tenderId, ctx.session.sessionUser.accountId, data.code, data.project_code, data.name);
  2431. ctx.body = { err: 0, msg: '', data: change };
  2432. } catch (err) {
  2433. this.log(err);
  2434. ctx.body = { err: 1, msg: err.toString() };
  2435. }
  2436. }
  2437. /**
  2438. * 删除变更立项
  2439. * @param {Object} ctx - egg全局变量
  2440. * @return {void}
  2441. */
  2442. async applyDelete(ctx) {
  2443. try {
  2444. const result = await ctx.service.changeApply.delete(ctx.request.body.caid);
  2445. if (!result) {
  2446. throw '删除变更立项失败';
  2447. }
  2448. ctx.redirect(ctx.request.header.referer);
  2449. } catch (err) {
  2450. console.log(err);
  2451. ctx.redirect(ctx.request.header.referer);
  2452. }
  2453. }
  2454. /**
  2455. * 获取审批界面所需的 原报、审批人数据等
  2456. * @param ctx
  2457. * @return {Promise<void>}
  2458. * @private
  2459. */
  2460. async _getChangeApplyAuditViewData(ctx) {
  2461. const auditConst = audit.changeApply;
  2462. const times = ctx.change.status === auditConst.status.checkNo ? ctx.change.times - 1 : ctx.change.times;
  2463. ctx.change.user = await ctx.service.projectAccount.getAccountInfoById(ctx.change.uid);
  2464. ctx.change.auditHistory = [];
  2465. if (times >= 1) {
  2466. for (let i = 1; i <= times; i++) {
  2467. ctx.change.auditHistory.push(await ctx.service.changeApplyAudit.getAuditors(ctx.change.id, i));
  2468. }
  2469. }
  2470. // 获取审批流程中左边列表
  2471. ctx.change.auditors2 = ctx.change.status === auditConst.status.checkNo && ctx.change.uid !== ctx.session.sessionUser.accountId ?
  2472. await ctx.service.changeApplyAudit.getAuditorsWithOwner(ctx.change.id, times) :
  2473. await ctx.service.changeApplyAudit.getAuditorsWithOwner(ctx.change.id, ctx.change.times);
  2474. if (ctx.change.status === auditConst.status.uncheck || ctx.change.status === auditConst.status.checkNo) {
  2475. ctx.change.auditorList = await ctx.service.changeApplyAudit.getAuditors(ctx.change.id, ctx.change.times);
  2476. }
  2477. }
  2478. async applyInformation(ctx) {
  2479. try {
  2480. const whiteList = this.ctx.app.config.multipart.whitelist;
  2481. const tender = await ctx.service.tender.getDataById(ctx.tender.id);
  2482. await this._getChangeApplyAuditViewData(ctx);
  2483. // 获取清单列表
  2484. const changeList = await ctx.service.changeApplyList.getList(ctx.change.id);
  2485. // 获取附件列表
  2486. const fileList = await ctx.service.changeApplyAtt.getAllChangeApplyAtt(ctx.tender.id, ctx.change.id);
  2487. const renderData = {
  2488. tender,
  2489. change: ctx.change,
  2490. listRule: tender.c_apply_list_rule ? JSON.parse(tender.c_apply_list_rule) : { source: 1, rule: ['unit', 'unit_price'] },
  2491. changeList,
  2492. changeConst,
  2493. auditConst: audit.changeApply,
  2494. fileList,
  2495. whiteList,
  2496. tpUnit: ctx.change.applyDecimal ? ctx.change.applyDecimal.tp : ctx.tender.info.decimal.tp,
  2497. upUnit: ctx.change.applyDecimal ? ctx.change.applyDecimal.up : ctx.tender.info.decimal.up,
  2498. changeUnits: changeConst.units,
  2499. precision: ctx.tender.info.precision,
  2500. returnUrl: this.app._.includes(ctx.request.headers.referer, '/tender/' + ctx.tender.id + '/change/apply') && !this.app._.includes(ctx.request.headers.referer, '/tender/' + ctx.tender.id + '/change/apply/' + ctx.change.id + '/information') ? ctx.request.headers.referer : null,
  2501. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.apply_information),
  2502. preUrl: '/tender/' + ctx.tender.id + '/change/apply/' + ctx.change.id + '/information',
  2503. };
  2504. if ((ctx.change.status === audit.changeApply.status.uncheck || ctx.change.status === audit.changeApply.status.checkNo) && (ctx.session.sessionUser.accountId === ctx.change.uid || ctx.tender.isTourist)) {
  2505. // data.accountGroup = accountGroup;
  2506. // 获取所有项目参与者
  2507. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  2508. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  2509. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  2510. });
  2511. renderData.accountList = accountList;
  2512. renderData.accountGroup = accountGroup.map((item, idx) => {
  2513. const groupList = accountList.filter(item => item.account_group === idx);
  2514. return { groupName: item, groupList };
  2515. });
  2516. }
  2517. await this.layout('change/apply_information.ejs', renderData, 'change/apply_information_modal.ejs');
  2518. } catch (err) {
  2519. this.log(err);
  2520. ctx.redirect('/tender/' + ctx.params.id + '/change');
  2521. }
  2522. }
  2523. async applyInformationNotice(ctx) {
  2524. try {
  2525. const tender = await ctx.service.tender.getDataById(ctx.tender.id);
  2526. // 获取附件列表
  2527. const fileList = await ctx.service.changeApplyAtt.getAllChangeApplyAtt(ctx.tender.id, ctx.change.id);
  2528. await this._getChangeApplyAuditViewData(ctx);
  2529. const renderData = {
  2530. tender,
  2531. change: ctx.change,
  2532. changeConst,
  2533. auditConst: audit.changeApply,
  2534. fileList,
  2535. returnUrl: this.app._.includes(ctx.request.headers.referer, '/tender/' + ctx.tender.id + '/change/apply') ? ctx.request.headers.referer : null,
  2536. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.apply_information_notice),
  2537. preUrl: '/tender/' + ctx.tender.id + '/change/apply/' + ctx.change.id + '/information/notice',
  2538. };
  2539. await this.layout('change/apply_information_notice.ejs', renderData, 'change/apply_information_notice_modal.ejs');
  2540. } catch (err) {
  2541. this.log(err);
  2542. ctx.redirect('/tender/' + ctx.params.id + '/change');
  2543. }
  2544. }
  2545. // 审批相关
  2546. /**
  2547. * 添加审批人
  2548. * @param ctx
  2549. * @return {Promise<void>}
  2550. */
  2551. async addApplyAudit(ctx) {
  2552. try {
  2553. const auditConst = audit.changeApply;
  2554. const data = JSON.parse(ctx.request.body.data);
  2555. const id = this.app._.toInteger(data.auditorId);
  2556. if (isNaN(id) || id <= 0) {
  2557. throw '参数错误';
  2558. }
  2559. // 检查权限等
  2560. if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
  2561. throw '您无权添加审核人';
  2562. }
  2563. if (ctx.change.status === auditConst.status.checking || ctx.change.status === auditConst.status.checked) {
  2564. throw '当前不允许添加审核人';
  2565. }
  2566. ctx.change.auditorList = await ctx.service.changeApplyAudit.getAuditors(ctx.change.id, ctx.change.times);
  2567. // 检查审核人是否已存在
  2568. const exist = this.app._.find(ctx.change.auditorList, { aid: id });
  2569. if (exist) {
  2570. throw '该审核人已存在,请勿重复添加';
  2571. }
  2572. // const shenpiInfo = await ctx.service.shenpiAudit.getDataByCondition({ tid: ctx.tender.id, sp_type: shenpiConst.sp_type.material, sp_status: shenpiConst.sp_status.gdzs });
  2573. // const is_gdzs = shenpiInfo && ctx.tender.info.shenpi.material === shenpiConst.sp_status.gdzs ? 1 : 0;
  2574. const result = await ctx.service.changeApplyAudit.addAuditor(ctx.change.id, id, ctx.change.times);
  2575. if (!result) {
  2576. throw '添加审核人失败';
  2577. }
  2578. const auditors = await ctx.service.changeApplyAudit.getAuditorsWithOwner(ctx.change.id, ctx.change.times);
  2579. ctx.body = { err: 0, msg: '', data: auditors };
  2580. } catch (err) {
  2581. this.log(err);
  2582. ctx.body = { err: 1, msg: err.toString(), data: null };
  2583. }
  2584. }
  2585. /**
  2586. * 移除审批人
  2587. * @param ctx
  2588. * @return {Promise<void>}
  2589. */
  2590. async deleteApplyAudit(ctx) {
  2591. try {
  2592. const data = JSON.parse(ctx.request.body.data);
  2593. const id = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
  2594. if (isNaN(id) || id <= 0) {
  2595. throw '参数错误';
  2596. }
  2597. const result = await ctx.service.changeApplyAudit.deleteAuditor(ctx.change.id, id, ctx.change.times);
  2598. if (!result) {
  2599. throw '移除审核人失败';
  2600. }
  2601. const auditors = await ctx.service.changeApplyAudit.getAuditors(ctx.change.id, ctx.change.times);
  2602. ctx.body = { err: 0, msg: '', data: auditors };
  2603. } catch (err) {
  2604. ctx.body = { err: 1, msg: err.toString(), data: null };
  2605. }
  2606. }
  2607. /**
  2608. * 上传附件
  2609. * @param {*} ctx 上下文
  2610. */
  2611. async uploadApplyFile(ctx) {
  2612. let stream;
  2613. try {
  2614. const auditConst = audit.changeApply;
  2615. // this._checkAdvanceFileCanModify(ctx);
  2616. const parts = this.ctx.multipart({
  2617. autoFields: true,
  2618. });
  2619. const files = [];
  2620. const create_time = Date.parse(new Date()) / 1000;
  2621. let idx = 0;
  2622. const extra_upload = ctx.change.status === auditConst.status.checked;
  2623. while ((stream = await parts()) !== undefined) {
  2624. if (!stream.filename) {
  2625. // 如果没有传入直接返回
  2626. return;
  2627. }
  2628. const fileInfo = path.parse(stream.filename);
  2629. const filepath = `app/public/upload/${this.ctx.tender.id.toString()}/change_apply/fujian_${create_time + idx.toString() + fileInfo.ext}`;
  2630. // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, 'app', filepath));
  2631. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  2632. files.push({ filepath, name: stream.filename, ext: fileInfo.ext });
  2633. ++idx;
  2634. stream && (await sendToWormhole(stream));
  2635. }
  2636. const in_time = new Date();
  2637. const payload = files.map(file => {
  2638. let idx;
  2639. if (Array.isArray(parts.field.name)) {
  2640. idx = parts.field.name.findIndex(name => name === file.name);
  2641. } else {
  2642. idx = 'isString';
  2643. }
  2644. const newFile = {
  2645. tid: ctx.tender.id,
  2646. caid: ctx.change.id,
  2647. uid: ctx.session.sessionUser.accountId,
  2648. type: parts.field.type,
  2649. filename: file.name,
  2650. fileext: file.ext,
  2651. filesize: ctx.helper.bytesToSize(idx === 'isString' ? parts.field.size : parts.field.size[idx]),
  2652. filepath: file.filepath,
  2653. upload_time: in_time,
  2654. extra_upload,
  2655. };
  2656. return newFile;
  2657. });
  2658. // 执行文件信息写入数据库
  2659. await ctx.service.changeApplyAtt.saveFileMsgToDb(payload);
  2660. // 将最新的当前标段的所有文件信息返回
  2661. const data = await ctx.service.changeApplyAtt.getAllChangeApplyAtt(ctx.tender.id, ctx.change.id);
  2662. ctx.body = { err: 0, msg: '', data };
  2663. } catch (err) {
  2664. stream && (await sendToWormhole(stream));
  2665. this.log(err);
  2666. ctx.body = { err: 1, msg: err.toString(), data: null };
  2667. }
  2668. }
  2669. /**
  2670. * 删除附件
  2671. * @param {Ojbect} ctx 上下文
  2672. */
  2673. async deleteApplyFile(ctx) {
  2674. try {
  2675. const { id } = JSON.parse(ctx.request.body.data);
  2676. const fileInfo = await ctx.service.changeApplyAtt.getDataById(id);
  2677. if (fileInfo || Object.keys(fileInfo).length) {
  2678. // 先删除文件
  2679. // await fs.unlinkSync(path.resolve(this.app.baseDir, './app', fileInfo.filepath));
  2680. await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
  2681. // 再删除数据库
  2682. await ctx.service.changeApplyAtt.delete(id);
  2683. } else {
  2684. throw '不存在该文件';
  2685. }
  2686. const data = await ctx.service.changeApplyAtt.getAllChangeApplyAtt(ctx.tender.id, ctx.change.id);
  2687. ctx.body = { err: 0, msg: '请求成功', data };
  2688. } catch (err) {
  2689. this.log(err);
  2690. ctx.body = { err: 1, msg: err.toString(), data: null };
  2691. }
  2692. }
  2693. /**
  2694. * 下载附件
  2695. * @param {Object} ctx - egg全局变量
  2696. * @return {void}
  2697. */
  2698. async downloadApplyFile(ctx) {
  2699. const id = ctx.params.fid;
  2700. if (id) {
  2701. try {
  2702. const fileInfo = await ctx.service.changeApplyAtt.getDataById(id);
  2703. if (fileInfo !== undefined && fileInfo !== '') {
  2704. // const fileName = path.join(__dirname, '../', fileInfo.filepath);
  2705. // 解决中文无法下载问题
  2706. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  2707. let disposition = '';
  2708. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  2709. disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename);
  2710. } else if (userAgent.indexOf('firefox') >= 0) {
  2711. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename) + '"';
  2712. } else {
  2713. /* safari等其他非主流浏览器只能自求多福了 */
  2714. disposition = 'attachment; filename=' + new Buffer(fileInfo.filename).toString('binary');
  2715. }
  2716. ctx.response.set({
  2717. 'Content-Type': 'application/octet-stream',
  2718. 'Content-Disposition': disposition,
  2719. 'Content-Length': fileInfo.filesize,
  2720. });
  2721. // ctx.body = await fs.createReadStream(fileName);
  2722. ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
  2723. } else {
  2724. throw '不存在该文件';
  2725. }
  2726. } catch (err) {
  2727. this.log(err);
  2728. this.setMessage(err.toString(), this.messageType.ERROR);
  2729. }
  2730. }
  2731. }
  2732. async applyInformationSave(ctx) {
  2733. try {
  2734. const data = JSON.parse(ctx.request.body.data);
  2735. if (data.name === 'code') {
  2736. const info = await ctx.service.changeApply.isRepeat(ctx.change.id, data.val, ctx.tender.id);
  2737. if (info) {
  2738. throw '该编号已存在';
  2739. }
  2740. }
  2741. const result = await ctx.service.changeApply.saveInfo(ctx.change.id, data);
  2742. if (!result) {
  2743. throw '修改失败';
  2744. }
  2745. ctx.body = { err: 0, msg: '请求成功', data: null };
  2746. } catch (err) {
  2747. this.log(err);
  2748. ctx.body = { err: 1, msg: err.toString(), data: null };
  2749. }
  2750. }
  2751. /**
  2752. * 上报和重新上报
  2753. * @param ctx
  2754. * @return {Promise<void>}
  2755. */
  2756. async startApplyAudit(ctx) {
  2757. try {
  2758. const auditConst = audit.changeApply;
  2759. // 检查权限等
  2760. if (!ctx.change) {
  2761. throw '数据错误';
  2762. }
  2763. if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
  2764. throw '您无权上报该期数据';
  2765. }
  2766. if (ctx.change.status === auditConst.status.checking || ctx.change.status === auditConst.status.checked) {
  2767. throw '该材料调差期数据当前无法上报';
  2768. }
  2769. await ctx.service.changeApplyAudit.start(ctx.change.id, ctx.change.times);
  2770. ctx.redirect(ctx.request.header.referer);
  2771. } catch (err) {
  2772. this.log(err);
  2773. ctx.session.postError = err.toString();
  2774. ctx.redirect(ctx.request.header.referer);
  2775. }
  2776. }
  2777. /**
  2778. * 审批
  2779. * @param ctx
  2780. * @return {Promise<void>}
  2781. */
  2782. async checkApplyAudit(ctx) {
  2783. try {
  2784. const auditConst = audit.changeApply;
  2785. if (!ctx.change || ctx.change.status !== auditConst.status.checking) {
  2786. throw '当前材料调差期数据有误';
  2787. }
  2788. if (!ctx.change.curAuditor || ctx.change.curAuditor.aid !== ctx.session.sessionUser.accountId) {
  2789. throw '您无权进行该操作';
  2790. }
  2791. const data = {
  2792. checkType: parseInt(ctx.request.body.checkType),
  2793. opinion: ctx.request.body.opinion,
  2794. };
  2795. if (!data.checkType || isNaN(data.checkType)) {
  2796. throw '提交数据错误';
  2797. }
  2798. if (ctx.request.body.notice_uid) {
  2799. data.notice_code = ctx.request.body.notice_code;
  2800. data.notice_uid = ctx.request.body.notice_uid;
  2801. }
  2802. // if (data.checkType === auditConst.status.checkNo) {
  2803. // if (!data.checkType || isNaN(data.checkType)) {
  2804. // throw '提交数据错误';
  2805. // }
  2806. // }
  2807. await ctx.service.changeApplyAudit.check(ctx.change.id, data, ctx.change.times);
  2808. ctx.redirect(ctx.request.header.referer);
  2809. } catch (err) {
  2810. this.log(err);
  2811. ctx.session.postError = err.toString();
  2812. ctx.redirect(ctx.request.header.referer);
  2813. }
  2814. }
  2815. /**
  2816. * 变更清单 - 操作 (Ajax)
  2817. * @param ctx
  2818. * @return {Promise<void>}
  2819. */
  2820. async saveApplyListsData(ctx) {
  2821. try {
  2822. const data = JSON.parse(ctx.request.body.data);
  2823. const responseData = {
  2824. err: 0,
  2825. msg: '',
  2826. data: {},
  2827. };
  2828. switch (data.type) {
  2829. case 'add':
  2830. responseData.data = await ctx.service.changeApplyList.add(data.updateData);
  2831. break;
  2832. case 'batchadd':
  2833. responseData.data = await ctx.service.changeApplyList.batchAdd(data);
  2834. break;
  2835. case 'del':
  2836. await ctx.service.changeApplyList.del(data.ids);
  2837. // 取所有工料表
  2838. responseData.data = await ctx.service.changeApplyList.getList(ctx.change.id);
  2839. break;
  2840. case 'update':
  2841. // if (data.updateData.code === '' || data.updateData.code === null) {
  2842. // throw '请先输入编号';
  2843. // }
  2844. await ctx.service.changeApplyList.save(data.updateData);
  2845. break;
  2846. case 'paste':
  2847. await ctx.service.changeApplyList.saveDatas(data.updateData);
  2848. // 取所有工料表
  2849. responseData.data = await ctx.service.changeApplyList.getList(ctx.change.id);
  2850. break;
  2851. case 'list_rule':
  2852. const result = await ctx.service.tender.saveTenderData(ctx.tender.id, { c_apply_list_rule: data.postData });
  2853. if (!result) {
  2854. throw '修改失败';
  2855. }
  2856. break;
  2857. default: throw '参数有误';
  2858. }
  2859. ctx.body = responseData;
  2860. } catch (err) {
  2861. this.log(err);
  2862. ctx.body = { err: 1, msg: err.toString(), data: null };
  2863. }
  2864. }
  2865. // 变更方案
  2866. async _filterChangesPlan(ctx, status = 0) {
  2867. const tenderId = ctx.params.id;
  2868. ctx.session.sessionUser.tenderId = tenderId;
  2869. const tender = await this.service.tender.getDataById(ctx.tender.id);
  2870. // const tender = ctx.tender;
  2871. // const tenderList = await this.service.tender.getList();
  2872. const page = ctx.page;
  2873. const sorts = ctx.query.sort ? ctx.query.sort : 0;
  2874. const orders = ctx.query.order ? ctx.query.order : 0;
  2875. const changes = await ctx.service.changePlan.getListByStatus(tender.id, status, 1, sorts, orders);
  2876. const total = await ctx.service.changePlan.getCountByStatus(tender.id, status);
  2877. let page_total = 0;
  2878. const tp = await ctx.service.changePlan.getTp(tender.id, status);
  2879. for (const c of changes) {
  2880. c.curAuditor = await ctx.service.changePlanAudit.getAuditorByStatus(c.id, c.status, c.times);
  2881. page_total = ctx.helper.add(page_total, c.total_price);
  2882. }
  2883. const tender_userInfo = await ctx.service.projectAccount.getDataById(ctx.tender.data.user_id);
  2884. // 分页相关
  2885. const pageInfo = {
  2886. page,
  2887. total: Math.ceil(total / app.config.pageSize),
  2888. queryData: JSON.stringify(ctx.urlInfo.query),
  2889. };
  2890. const filter = JSON.parse(JSON.stringify(audit.changeApply.filter));
  2891. filter.count = [];
  2892. filter.count[filter.status.pending] = await ctx.service.changePlan.getCountByStatus(tender.id, filter.status.pending);// await ctx.service.change.pendingDatas(tender.id, ctx.session.sessionUser.accountId);
  2893. filter.count[filter.status.uncheck] = await ctx.service.changePlan.getCountByStatus(tender.id, filter.status.uncheck);// await ctx.service.change.checkingDatas(tender.id, ctx.session.sessionUser.accountId);
  2894. filter.count[filter.status.checking] = await ctx.service.changePlan.getCountByStatus(tender.id, filter.status.checking);// await ctx.service.change.checkedDatas(tender.id, ctx.session.sessionUser.accountId);
  2895. filter.count[filter.status.checked] = await ctx.service.changePlan.getCountByStatus(tender.id, filter.status.checked);// await ctx.service.change.pendingDatas(tender.id, ctx.session.sessionUser.accountId);
  2896. // filter.count[filter.status.checkNo] = await ctx.service.changeApply.getCountByStatus(tender.id, filter.status.checkNo);// await ctx.service.change.pendingDatas(tender.id, ctx.session.sessionUser.accountId);
  2897. let codeRule = [];
  2898. let c_connector = '1';
  2899. let c_rule_first = 1;
  2900. const rule_type = 'plan';
  2901. if (tender.c_code_rules) {
  2902. const c_code_rules = JSON.parse(tender.c_code_rules);
  2903. codeRule = c_code_rules[rule_type + '_rule'] !== undefined ? c_code_rules[rule_type + '_rule'] : [];
  2904. c_connector = c_code_rules[rule_type + '_connector'] !== undefined ? c_code_rules[rule_type + '_connector'] : '1';
  2905. c_rule_first = c_code_rules[rule_type + '_rule_first'] !== undefined ? c_code_rules[rule_type + '_rule_first'] : 1;
  2906. }
  2907. for (const rule of codeRule) {
  2908. switch (rule.rule_type) {
  2909. case codeRuleConst.measure.ruleType.dealCode:
  2910. rule.preview = ctx.tender.info.deal_info.dealCode;
  2911. break;
  2912. case codeRuleConst.measure.ruleType.tenderName:
  2913. rule.preview = tender.name;
  2914. break;
  2915. case codeRuleConst.measure.ruleType.inDate:
  2916. rule.preview = moment().format('YYYY');
  2917. break;
  2918. case codeRuleConst.measure.ruleType.text:
  2919. rule.preview = rule.text;
  2920. break;
  2921. case codeRuleConst.measure.ruleType.addNo:
  2922. const s = '0000000000';
  2923. rule.preview = s.substr(s.length - rule.format);
  2924. break;
  2925. default: break;
  2926. }
  2927. }
  2928. const changeApplyList = await ctx.service.changeApply.getAllDataByCondition({ where: { tid: tender.id, status: audit.changeApply.status.checked } });
  2929. const allApplyCodes = await ctx.service.changePlan.getAllDataByCondition({
  2930. columns: ['apply_code'],
  2931. where: {
  2932. tid: tender.id,
  2933. },
  2934. });
  2935. const acLists = allApplyCodes.length > 0 ? ctx.app._.uniq(ctx.app._.map(allApplyCodes, 'apply_code')) : [];
  2936. const renderData = {
  2937. uid: ctx.session.sessionUser.accountId,
  2938. tender,
  2939. pageInfo,
  2940. changes,
  2941. status,
  2942. rule_type,
  2943. codeRule,
  2944. c_connector,
  2945. c_rule_first,
  2946. filter,
  2947. plan_username: tender_userInfo ? tender_userInfo.name : null,
  2948. ruleType: codeRuleConst.ruleType[rule_type],
  2949. dealCode: ctx.tender.info.deal_info.dealCode,
  2950. auditConst: audit.changeApply,
  2951. ruleConst: codeRuleConst.measure,
  2952. changeConst,
  2953. changeApplyList,
  2954. acLists,
  2955. page_total,
  2956. tp,
  2957. tenderMenu: this.menu.tenderMenu,
  2958. preUrl: '/tender/' + tender.id,
  2959. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.plan),
  2960. };
  2961. await this.layout('change/plan.ejs', renderData, 'change/plan_modal.ejs');
  2962. }
  2963. /**
  2964. * 变更申请列表 页面 (Get)
  2965. *
  2966. * @param {Object} ctx - egg全局变量
  2967. * @return {void}
  2968. */
  2969. async plan(ctx) {
  2970. try {
  2971. await this._filterChangesPlan(ctx);
  2972. } catch (err) {
  2973. this.log(err);
  2974. ctx.redirect('/dashboard');
  2975. }
  2976. }
  2977. /**
  2978. * 变更管理 状态筛选 页面 (Get)
  2979. * @param {Object} ctx - egg全局变量
  2980. * @return {void}
  2981. */
  2982. async planStatus(ctx) {
  2983. try {
  2984. const status = parseInt(ctx.params.status);
  2985. await this._filterChangesPlan(ctx, status);
  2986. } catch (err) {
  2987. this.logger.error(err);
  2988. ctx.redirect('/tender/' + ctx.params.id + '/change/plan');
  2989. }
  2990. }
  2991. /**
  2992. * 新增变更立项 (Post)
  2993. *
  2994. * @param {Object} ctx - egg全局变量
  2995. * @return {void}
  2996. */
  2997. async planAdd(ctx) {
  2998. try {
  2999. const tenderId = ctx.params.id;
  3000. if (!tenderId) {
  3001. throw '当前未打开标段';
  3002. }
  3003. const data = JSON.parse(ctx.request.body.data);
  3004. if (!data.code || data.code === '') {
  3005. throw '变更方案编号不能为空';
  3006. }
  3007. const change = await ctx.service.changePlan.add(tenderId, ctx.session.sessionUser.accountId, data.code, data.apply_code, data.name);
  3008. ctx.body = { err: 0, msg: '', data: change };
  3009. } catch (err) {
  3010. this.log(err);
  3011. ctx.body = { err: 1, msg: err.toString() };
  3012. }
  3013. }
  3014. /**
  3015. * 删除变更立项
  3016. * @param {Object} ctx - egg全局变量
  3017. * @return {void}
  3018. */
  3019. async planDelete(ctx) {
  3020. try {
  3021. const result = await ctx.service.changePlan.delete(ctx.request.body.cpid);
  3022. if (!result) {
  3023. throw '删除变更方案失败';
  3024. }
  3025. ctx.redirect(ctx.request.header.referer);
  3026. } catch (err) {
  3027. console.log(err);
  3028. ctx.redirect(ctx.request.header.referer);
  3029. }
  3030. }
  3031. /**
  3032. * 获取审批界面所需的 原报、审批人数据等
  3033. * @param ctx
  3034. * @return {Promise<void>}
  3035. * @private
  3036. */
  3037. async _getChangePlanAuditViewData(ctx) {
  3038. const auditConst = audit.changeApply;
  3039. const times = ctx.change.status === auditConst.status.checkNo ? ctx.change.times - 1 : ctx.change.times;
  3040. ctx.change.user = await ctx.service.projectAccount.getAccountInfoById(ctx.change.uid);
  3041. ctx.change.auditHistory = [];
  3042. if (times >= 1) {
  3043. for (let i = 1; i <= times; i++) {
  3044. ctx.change.auditHistory.push(await ctx.service.changePlanAudit.getAuditors(ctx.change.id, i));
  3045. }
  3046. }
  3047. // 获取审批流程中左边列表
  3048. ctx.change.auditors2 = ctx.change.status === auditConst.status.checkNo && ctx.change.uid !== ctx.session.sessionUser.accountId ?
  3049. await ctx.service.changePlanAudit.getAuditorsWithOwner(ctx.change.id, times) :
  3050. await ctx.service.changePlanAudit.getAuditorsWithOwner(ctx.change.id, ctx.change.times);
  3051. if (ctx.change.status === auditConst.status.uncheck || ctx.change.status === auditConst.status.checkNo) {
  3052. ctx.change.auditorList = await ctx.service.changePlanAudit.getAuditors(ctx.change.id, ctx.change.times);
  3053. }
  3054. }
  3055. async planInformation(ctx) {
  3056. try {
  3057. const whiteList = this.ctx.app.config.multipart.whitelist;
  3058. const tender = await ctx.service.tender.getDataById(ctx.tender.id);
  3059. await this._getChangePlanAuditViewData(ctx);
  3060. // 获取附件列表
  3061. const fileList = await ctx.service.changePlanAtt.getAllChangePlanAtt(ctx.tender.id, ctx.change.id);
  3062. // 获取清单列表
  3063. const changeList = await ctx.service.changePlanList.getList(ctx.change.id);
  3064. if (ctx.change.status === audit.changeApply.status.checking || ctx.change.status === audit.changeApply.status.checked) {
  3065. const listAudits = await ctx.service.changePlanAudit.getAuditGroupByList(ctx.change.id, ctx.change.times);
  3066. ctx.change.listAudits = listAudits;
  3067. for (const cl of changeList) {
  3068. const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
  3069. // 清单表页赋值
  3070. for (const [index, au] of listAudits.entries()) {
  3071. cl['audit_amount_' + au.aid] = ctx.change.shenpiPower && au.aid === ctx.session.sessionUser.accountId ? cl.spamount : (audit_amount[index] ? parseFloat(audit_amount[index]) : null);
  3072. }
  3073. }
  3074. }
  3075. const renderData = {
  3076. tender,
  3077. change: ctx.change,
  3078. listRule: tender.c_plan_list_rule ? JSON.parse(tender.c_plan_list_rule) : { source: 1, rule: ['unit', 'unit_price'] },
  3079. changeList,
  3080. changeConst,
  3081. auditConst: audit.changeApply,
  3082. fileList,
  3083. whiteList,
  3084. tpUnit: ctx.change.decimal ? ctx.change.decimal.tp : ctx.tender.info.decimal.tp,
  3085. upUnit: ctx.change.decimal ? ctx.change.decimal.up : ctx.tender.info.decimal.up,
  3086. changeUnits: changeConst.units,
  3087. precision: ctx.tender.info.precision,
  3088. returnUrl: this.app._.includes(ctx.request.headers.referer, '/tender/' + ctx.tender.id + '/change/plan') && !this.app._.includes(ctx.request.headers.referer, '/tender/' + ctx.tender.id + '/change/plan/' + ctx.change.id + '/information') ? ctx.request.headers.referer : null,
  3089. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.plan_information),
  3090. preUrl: '/tender/' + ctx.tender.id + '/change/plan/' + ctx.change.id + '/information',
  3091. };
  3092. if ((ctx.change.status === audit.changeApply.status.uncheck || ctx.change.status === audit.changeApply.status.checkNo) && (ctx.session.sessionUser.accountId === ctx.change.uid || ctx.tender.isTourist)) {
  3093. // data.accountGroup = accountGroup;
  3094. // 获取所有项目参与者
  3095. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  3096. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  3097. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  3098. });
  3099. renderData.accountList = accountList;
  3100. renderData.accountGroup = accountGroup.map((item, idx) => {
  3101. const groupList = accountList.filter(item => item.account_group === idx);
  3102. return { groupName: item, groupList };
  3103. });
  3104. }
  3105. await this.layout('change/plan_information.ejs', renderData, 'change/plan_information_modal.ejs');
  3106. } catch (err) {
  3107. this.log(err);
  3108. ctx.redirect('/tender/' + ctx.params.id + '/change');
  3109. }
  3110. }
  3111. // 审批相关
  3112. /**
  3113. * 添加审批人
  3114. * @param ctx
  3115. * @return {Promise<void>}
  3116. */
  3117. async addPlanAudit(ctx) {
  3118. try {
  3119. const auditConst = audit.changeApply;
  3120. const data = JSON.parse(ctx.request.body.data);
  3121. const id = this.app._.toInteger(data.auditorId);
  3122. if (isNaN(id) || id <= 0) {
  3123. throw '参数错误';
  3124. }
  3125. // 检查权限等
  3126. if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
  3127. throw '您无权添加审核人';
  3128. }
  3129. if (ctx.change.status === auditConst.status.checking || ctx.change.status === auditConst.status.checked) {
  3130. throw '当前不允许添加审核人';
  3131. }
  3132. ctx.change.auditorList = await ctx.service.changePlanAudit.getAuditors(ctx.change.id, ctx.change.times);
  3133. // 检查审核人是否已存在
  3134. const exist = this.app._.find(ctx.change.auditorList, { aid: id });
  3135. if (exist) {
  3136. throw '该审核人已存在,请勿重复添加';
  3137. }
  3138. // const shenpiInfo = await ctx.service.shenpiAudit.getDataByCondition({ tid: ctx.tender.id, sp_type: shenpiConst.sp_type.material, sp_status: shenpiConst.sp_status.gdzs });
  3139. // const is_gdzs = shenpiInfo && ctx.tender.info.shenpi.material === shenpiConst.sp_status.gdzs ? 1 : 0;
  3140. const result = await ctx.service.changePlanAudit.addAuditor(ctx.change.id, id, ctx.change.times);
  3141. if (!result) {
  3142. throw '添加审核人失败';
  3143. }
  3144. const auditors = await ctx.service.changePlanAudit.getAuditorsWithOwner(ctx.change.id, ctx.change.times);
  3145. ctx.body = { err: 0, msg: '', data: auditors };
  3146. } catch (err) {
  3147. this.log(err);
  3148. ctx.body = { err: 1, msg: err.toString(), data: null };
  3149. }
  3150. }
  3151. /**
  3152. * 移除审批人
  3153. * @param ctx
  3154. * @return {Promise<void>}
  3155. */
  3156. async deletePlanAudit(ctx) {
  3157. try {
  3158. const data = JSON.parse(ctx.request.body.data);
  3159. const id = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
  3160. if (isNaN(id) || id <= 0) {
  3161. throw '参数错误';
  3162. }
  3163. const result = await ctx.service.changePlanAudit.deleteAuditor(ctx.change.id, id, ctx.change.times);
  3164. if (!result) {
  3165. throw '移除审核人失败';
  3166. }
  3167. const auditors = await ctx.service.changePlanAudit.getAuditors(ctx.change.id, ctx.change.times);
  3168. ctx.body = { err: 0, msg: '', data: auditors };
  3169. } catch (err) {
  3170. ctx.body = { err: 1, msg: err.toString(), data: null };
  3171. }
  3172. }
  3173. /**
  3174. * 上传附件
  3175. * @param {*} ctx 上下文
  3176. */
  3177. async uploadPlanFile(ctx) {
  3178. let stream;
  3179. try {
  3180. const auditConst = audit.changeApply;
  3181. // this._checkAdvanceFileCanModify(ctx);
  3182. const parts = this.ctx.multipart({
  3183. autoFields: true,
  3184. });
  3185. const files = [];
  3186. const create_time = Date.parse(new Date()) / 1000;
  3187. let idx = 0;
  3188. const extra_upload = ctx.change.status === auditConst.status.checked;
  3189. while ((stream = await parts()) !== undefined) {
  3190. if (!stream.filename) {
  3191. // 如果没有传入直接返回
  3192. return;
  3193. }
  3194. const fileInfo = path.parse(stream.filename);
  3195. const filepath = `app/public/upload/${this.ctx.tender.id.toString()}/change_plan/fujian_${create_time + idx.toString() + fileInfo.ext}`;
  3196. // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, 'app', filepath));
  3197. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  3198. files.push({ filepath, name: stream.filename, ext: fileInfo.ext });
  3199. ++idx;
  3200. stream && (await sendToWormhole(stream));
  3201. }
  3202. const in_time = new Date();
  3203. const payload = files.map(file => {
  3204. let idx;
  3205. if (Array.isArray(parts.field.name)) {
  3206. idx = parts.field.name.findIndex(name => name === file.name);
  3207. } else {
  3208. idx = 'isString';
  3209. }
  3210. const newFile = {
  3211. tid: ctx.tender.id,
  3212. cpid: ctx.change.id,
  3213. uid: ctx.session.sessionUser.accountId,
  3214. filename: file.name,
  3215. fileext: file.ext,
  3216. filesize: ctx.helper.bytesToSize(idx === 'isString' ? parts.field.size : parts.field.size[idx]),
  3217. filepath: file.filepath,
  3218. upload_time: in_time,
  3219. extra_upload,
  3220. };
  3221. return newFile;
  3222. });
  3223. // 执行文件信息写入数据库
  3224. await ctx.service.changePlanAtt.saveFileMsgToDb(payload);
  3225. // 将最新的当前标段的所有文件信息返回
  3226. const data = await ctx.service.changePlanAtt.getAllChangePlanAtt(ctx.tender.id, ctx.change.id);
  3227. ctx.body = { err: 0, msg: '', data };
  3228. } catch (err) {
  3229. stream && (await sendToWormhole(stream));
  3230. this.log(err);
  3231. ctx.body = { err: 1, msg: err.toString(), data: null };
  3232. }
  3233. }
  3234. /**
  3235. * 删除附件
  3236. * @param {Ojbect} ctx 上下文
  3237. */
  3238. async deletePlanFile(ctx) {
  3239. try {
  3240. const { id } = JSON.parse(ctx.request.body.data);
  3241. const fileInfo = await ctx.service.changePlanAtt.getDataById(id);
  3242. if (fileInfo || Object.keys(fileInfo).length) {
  3243. // 先删除文件
  3244. // await fs.unlinkSync(path.resolve(this.app.baseDir, './app', fileInfo.filepath));
  3245. await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
  3246. // 再删除数据库
  3247. await ctx.service.changePlanAtt.delete(id);
  3248. } else {
  3249. throw '不存在该文件';
  3250. }
  3251. const data = await ctx.service.changePlanAtt.getAllChangePlanAtt(ctx.tender.id, ctx.change.id);
  3252. ctx.body = { err: 0, msg: '请求成功', data };
  3253. } catch (err) {
  3254. this.log(err);
  3255. ctx.body = { err: 1, msg: err.toString(), data: null };
  3256. }
  3257. }
  3258. /**
  3259. * 下载附件
  3260. * @param {Object} ctx - egg全局变量
  3261. * @return {void}
  3262. */
  3263. async downloadPlanFile(ctx) {
  3264. const id = ctx.params.fid;
  3265. if (id) {
  3266. try {
  3267. const fileInfo = await ctx.service.changePlanAtt.getDataById(id);
  3268. if (fileInfo !== undefined && fileInfo !== '') {
  3269. // const fileName = path.join(__dirname, '../', fileInfo.filepath);
  3270. // 解决中文无法下载问题
  3271. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  3272. let disposition = '';
  3273. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  3274. disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename);
  3275. } else if (userAgent.indexOf('firefox') >= 0) {
  3276. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename) + '"';
  3277. } else {
  3278. /* safari等其他非主流浏览器只能自求多福了 */
  3279. disposition = 'attachment; filename=' + new Buffer(fileInfo.filename).toString('binary');
  3280. }
  3281. ctx.response.set({
  3282. 'Content-Type': 'application/octet-stream',
  3283. 'Content-Disposition': disposition,
  3284. 'Content-Length': fileInfo.filesize,
  3285. });
  3286. // ctx.body = await fs.createReadStream(fileName);
  3287. ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
  3288. } else {
  3289. throw '不存在该文件';
  3290. }
  3291. } catch (err) {
  3292. this.log(err);
  3293. this.setMessage(err.toString(), this.messageType.ERROR);
  3294. }
  3295. }
  3296. }
  3297. async planInformationSave(ctx) {
  3298. try {
  3299. const data = JSON.parse(ctx.request.body.data);
  3300. if (data.name === 'code') {
  3301. const info = await ctx.service.changePlan.isRepeat(ctx.change.id, data.val, ctx.tender.id);
  3302. if (info) {
  3303. throw '该编号已存在';
  3304. }
  3305. }
  3306. const result = await ctx.service.changePlan.saveInfo(ctx.change.id, data);
  3307. if (!result) {
  3308. throw '修改失败';
  3309. }
  3310. ctx.body = { err: 0, msg: '请求成功', data: null };
  3311. } catch (err) {
  3312. this.log(err);
  3313. ctx.body = { err: 1, msg: err.toString(), data: null };
  3314. }
  3315. }
  3316. /**
  3317. * 上报和重新上报
  3318. * @param ctx
  3319. * @return {Promise<void>}
  3320. */
  3321. async startPlanAudit(ctx) {
  3322. try {
  3323. const auditConst = audit.changeApply;
  3324. // 检查权限等
  3325. if (!ctx.change) {
  3326. throw '数据错误';
  3327. }
  3328. if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
  3329. throw '您无权上报该期数据';
  3330. }
  3331. if (ctx.change.status === auditConst.status.checking || ctx.change.status === auditConst.status.checked) {
  3332. throw '该材料调差期数据当前无法上报';
  3333. }
  3334. await ctx.service.changePlanAudit.start(ctx.change.id, ctx.change.times);
  3335. ctx.redirect(ctx.request.header.referer);
  3336. } catch (err) {
  3337. this.log(err);
  3338. ctx.session.postError = err.toString();
  3339. ctx.redirect(ctx.request.header.referer);
  3340. }
  3341. }
  3342. /**
  3343. * 审批
  3344. * @param ctx
  3345. * @return {Promise<void>}
  3346. */
  3347. async checkPlanAudit(ctx) {
  3348. try {
  3349. const auditConst = audit.changeApply;
  3350. if (!ctx.change || ctx.change.status !== auditConst.status.checking) {
  3351. throw '当前材料调差期数据有误';
  3352. }
  3353. if (!ctx.change.curAuditor || ctx.change.curAuditor.aid !== ctx.session.sessionUser.accountId) {
  3354. throw '您无权进行该操作';
  3355. }
  3356. const data = {
  3357. checkType: parseInt(ctx.request.body.checkType),
  3358. opinion: ctx.request.body.opinion,
  3359. };
  3360. if (!data.checkType || isNaN(data.checkType)) {
  3361. throw '提交数据错误';
  3362. }
  3363. // if (data.checkType === auditConst.status.checkNo) {
  3364. // if (!data.checkType || isNaN(data.checkType)) {
  3365. // throw '提交数据错误';
  3366. // }
  3367. // }
  3368. await ctx.service.changePlanAudit.check(ctx.change.id, data, ctx.change.times);
  3369. ctx.redirect(ctx.request.header.referer);
  3370. } catch (err) {
  3371. this.log(err);
  3372. ctx.session.postError = err.toString();
  3373. ctx.redirect(ctx.request.header.referer);
  3374. }
  3375. }
  3376. /**
  3377. * 变更清单 - 操作 (Ajax)
  3378. * @param ctx
  3379. * @return {Promise<void>}
  3380. */
  3381. async savePlanListsData(ctx) {
  3382. try {
  3383. const data = JSON.parse(ctx.request.body.data);
  3384. const responseData = {
  3385. err: 0,
  3386. msg: '',
  3387. data: {},
  3388. };
  3389. switch (data.type) {
  3390. case 'add':
  3391. responseData.data = await ctx.service.changePlanList.add(data.updateData);
  3392. break;
  3393. case 'batchadd':
  3394. responseData.data = await ctx.service.changePlanList.batchAdd(data);
  3395. break;
  3396. case 'del':
  3397. await ctx.service.changePlanList.del(data.ids);
  3398. // 取所有工料表
  3399. responseData.data = await ctx.service.changePlanList.getList(ctx.change.id);
  3400. break;
  3401. case 'update':
  3402. // if (data.updateData.code === '' || data.updateData.code === null) {
  3403. // throw '请先输入编号';
  3404. // }
  3405. await ctx.service.changePlanList.save(data.updateData);
  3406. break;
  3407. case 'paste':
  3408. await ctx.service.changePlanList.saveDatas(data.updateData);
  3409. // 取所有工料表
  3410. responseData.data = await ctx.service.changePlanList.getList(ctx.change.id);
  3411. break;
  3412. case 'list_rule':
  3413. const result = await ctx.service.tender.saveTenderData(ctx.tender.id, { c_plan_list_rule: data.postData });
  3414. if (!result) {
  3415. throw '修改失败';
  3416. }
  3417. break;
  3418. case 'paste_amount_rows':
  3419. await ctx.service.changePlanList.saveDatas(data.updateData);
  3420. const changeList = await ctx.service.changePlanList.getList(ctx.change.id);
  3421. const listAudits = await ctx.service.changePlanAudit.getAuditGroupByList(ctx.change.id, ctx.change.times);
  3422. for (const cl of changeList) {
  3423. // 清单表页赋值
  3424. const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
  3425. // 清单表页赋值
  3426. for (const [index, au] of listAudits.entries()) {
  3427. cl['audit_amount_' + au.aid] = ctx.change.shenpiPower && au.aid === ctx.session.sessionUser.accountId ? cl.spamount : (audit_amount[index] ? parseFloat(audit_amount[index]) : null);
  3428. }
  3429. }
  3430. // 取所有工料表
  3431. responseData.data = changeList;
  3432. break;
  3433. default: throw '参数有误';
  3434. }
  3435. ctx.body = responseData;
  3436. } catch (err) {
  3437. this.log(err);
  3438. ctx.body = { err: 1, msg: err.toString(), data: null };
  3439. }
  3440. }
  3441. }
  3442. return ChangeController;
  3443. };