change_controller.js 222 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545
  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 auditType = require('../const/audit').auditType;
  22. const accountGroup = require('../const/account_group').group;
  23. const shenpiConst = require('../const/shenpi');
  24. const tenderMenu = require('../../config/menu').tenderMenu;
  25. const measureType = require('../const/tender').measureType;
  26. const spreadConst = require('../const/spread');
  27. const stdConst = require('../const/standard');
  28. // const tenderMenu = require('../../config/menu').tenderMenu;
  29. const spreadSetting = require('../lib/spread_setting');
  30. module.exports = app => {
  31. class ChangeController extends app.BaseController {
  32. /**
  33. * 构造函数
  34. *
  35. * @param {Object} ctx - egg全局变量
  36. * @return {void}
  37. */
  38. constructor(ctx) {
  39. super(ctx);
  40. ctx.showProject = true;
  41. ctx.showTender = true;
  42. ctx.showTitle = true;
  43. }
  44. async _filterChanges(ctx, status = 0) {
  45. const tenderId = ctx.params.id;
  46. ctx.session.sessionUser.tenderId = tenderId;
  47. const tender = await this.service.tender.getDataById(tenderId);
  48. // const tenderList = await this.service.tender.getList();
  49. const page = ctx.page;
  50. const pageSize = ctx.pageSize;
  51. const sorts = ctx.query.sort ? ctx.query.sort : 0;
  52. const orders = ctx.query.order ? ctx.query.order : 0;
  53. const state = ctx.session.sessionProject.page_show.openChangeState && ctx.query.state ? parseInt(ctx.query.state) : 0;
  54. const changes = await ctx.service.change.getListByStatus(tender.id, status, 1, sorts, orders, state);
  55. const total = await ctx.service.change.getCountByStatus(tender.id, status, state);
  56. let page_total = 0;
  57. const tp = await ctx.service.change.getTp(tender.id, status, state);
  58. if (changes !== null) {
  59. for (const c of changes) {
  60. if (c.status !== audit.change.status.checked || !c.final_auditor_str) {
  61. c.curAuditors = await ctx.service.changeAudit.getAuditorsByStatus(c.cid, c.status, c.times);
  62. if (c.status === audit.change.status.checkNoPre) {
  63. c.curAuditors2 = await ctx.service.stageAudit.getAuditorsByStatus(c.cid, audit.change.status.checking, c.times);
  64. }
  65. if (c.status === audit.change.status.checkNo || c.status === audit.change.status.revise) {
  66. const changeUsedData = await ctx.service.stageChange.getAllFinalUsedData(ctx.tender.id, c.cid);
  67. c.stageChangeNum = this.ctx.helper.sum(changeUsedData.map(x => { return Math.abs(x.qty); }));
  68. c.isSettle = await ctx.service.changeSettleList.isSettle(c.cid);
  69. }
  70. if (c.status === audit.change.status.checked && c.curAuditors.length > 0) {
  71. const final_auditor_str = c.curAuditors[0].audit_type === auditType.key.common
  72. ? c.curAuditors[0].name + (c.curAuditors[0].role ? '-' + c.curAuditors[0].role : '')
  73. : ctx.helper.transFormToChinese(c.curAuditors[0].audit_order) + '审';
  74. await ctx.service.change.defaultUpdate({ final_auditor_str }, { where: { cid: c.cid } });
  75. }
  76. }
  77. const user = await ctx.service.projectAccount.getDataById(c.uid);
  78. c.username = user ? user.name : '';
  79. page_total = ctx.helper.add(page_total, c.total_price);
  80. }
  81. }
  82. // 分页相关
  83. const pageInfo = {
  84. page,
  85. pageSizeSelect: 1,
  86. pageSize,
  87. total_num: total,
  88. total: Math.ceil(total / pageSize),
  89. queryData: JSON.stringify(ctx.urlInfo.query),
  90. };
  91. const filter = JSON.parse(JSON.stringify(audit.filter));
  92. filter.count = [];
  93. 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);
  94. 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);
  95. 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);
  96. 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);
  97. 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);
  98. const codeRule = tender.c_rule ? JSON.parse(tender.c_rule) : [];
  99. for (const rule of codeRule) {
  100. switch (rule.rule_type) {
  101. case codeRuleConst.measure.ruleType.dealCode:
  102. rule.preview = ctx.tender.info.deal_info.dealCode;
  103. break;
  104. case codeRuleConst.measure.ruleType.tenderName:
  105. rule.preview = tender.name;
  106. break;
  107. case codeRuleConst.measure.ruleType.inDate:
  108. rule.preview = moment().format('YYYY');
  109. break;
  110. case codeRuleConst.measure.ruleType.text:
  111. rule.preview = rule.text;
  112. break;
  113. case codeRuleConst.measure.ruleType.addNo:
  114. const s = '0000000000';
  115. rule.preview = s.substr(s.length - rule.format);
  116. break;
  117. default: break;
  118. }
  119. }
  120. const changePlanList = await ctx.service.changePlan.getAllDataByCondition({ where: { tid: tender.id, status: audit.changePlan.status.checked } });
  121. const allPlanCodes = await ctx.service.change.getAllDataByCondition({
  122. columns: ['plan_code'],
  123. where: {
  124. tid: tender.id,
  125. },
  126. });
  127. const apLists = allPlanCodes.length > 0 ? ctx.app._.uniq(ctx.app._.map(allPlanCodes, 'plan_code')) : [];
  128. const need_checking = await ctx.service.change.getCheckingList(ctx.tender.id, ctx.session.sessionUser.accountId, true);
  129. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  130. const userPermission = pa && pa.permission ? JSON.parse(pa.permission) : null;
  131. const addPermission = !!(userPermission && userPermission.change !== undefined && userPermission.change.indexOf('3') !== -1);
  132. const renderData = {
  133. moment,
  134. tender,
  135. // tenderList,
  136. pageInfo,
  137. changes,
  138. page_total,
  139. tp,
  140. filter,
  141. status,
  142. codeRule,
  143. dealCode: ctx.tender.info.deal_info.dealCode,
  144. auditConst: audit.change,
  145. changeConst,
  146. shenpiConst,
  147. state,
  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. changePlanList,
  154. apLists,
  155. auditType,
  156. precision: ctx.tender.info.precision,
  157. need_checking,
  158. addPermission,
  159. };
  160. if (ctx.session.sessionProject.page_show.openChangeState) {
  161. // 工程变更类别读取
  162. const projectData = await ctx.service.project.getDataById(ctx.session.sessionProject.id);
  163. const fun_set = await ctx.service.project.getFunSet(projectData.fun_set);
  164. const changeState = fun_set.change_state;
  165. for (const cs of changeState) {
  166. cs.count = await ctx.service.change.getCountByStatus(tender.id, status, cs.order);
  167. }
  168. renderData.changeState = changeState;
  169. }
  170. await this.layout('change/index.ejs', renderData, 'change/modal.ejs');
  171. }
  172. /**
  173. * 变更管理 页面 (Get)
  174. *
  175. * @param {Object} ctx - egg全局变量
  176. * @return {void}
  177. */
  178. async index(ctx) {
  179. try {
  180. await this._filterChanges(ctx);
  181. } catch (err) {
  182. this.log(err);
  183. ctx.redirect('/dashboard');
  184. }
  185. }
  186. /**
  187. * 审批流程(Get)
  188. * @param ctx
  189. * @return {Promise<void>}
  190. */
  191. async changeAuditors(ctx) {
  192. try {
  193. const cid = JSON.parse(ctx.request.body.data).cid;
  194. const tenderId = ctx.params.id;
  195. const changeInfo = await ctx.service.change.getDataByCondition({ cid });
  196. await ctx.service.change.loadChangeUser(changeInfo);
  197. await ctx.service.change.loadChangeAuditViewData(changeInfo);
  198. ctx.body = { err: 0, msg: '', data: changeInfo };
  199. } catch (error) {
  200. this.log(error);
  201. ctx.body = { err: 1, msg: error.toString(), data: null };
  202. }
  203. }
  204. /**
  205. *
  206. * @param {Object} ctx - egg全局变量
  207. * @return {void}
  208. */
  209. async newCode(ctx) {
  210. const responseData = {
  211. err: 0,
  212. msg: '',
  213. data: '',
  214. };
  215. try {
  216. const tenderId = ctx.params.id;
  217. if (!tenderId) {
  218. throw '当前未打开标段';
  219. }
  220. const tenderData = await ctx.service.tender.getDataById(tenderId);
  221. const data = JSON.parse(ctx.request.body.data);
  222. let cCodeRule;
  223. let cConnector;
  224. let changeCount = 0;
  225. if (data && data.type) {
  226. const c_code_rules = tenderData.c_code_rules !== null ? JSON.parse(tenderData.c_code_rules) : null;
  227. cCodeRule = c_code_rules && c_code_rules[data.type + '_rule'] ? c_code_rules[data.type + '_rule'] : [];
  228. cConnector = c_code_rules && c_code_rules[data.type + '_connector'] ? c_code_rules[data.type + '_connector'] : null;
  229. if (data.type === codeRuleConst.ruleField[codeRuleConst.ruleType.apply]) {
  230. changeCount = await ctx.service.changeApply.count({ tid: tenderId });
  231. } else if (data.type === codeRuleConst.ruleField[codeRuleConst.ruleType.plan]) {
  232. changeCount = await ctx.service.changePlan.count({ tid: tenderId });
  233. } else {
  234. changeCount = await ctx.service.changeProject.count({ tid: tenderId, type: codeRuleConst.ruleType[data.type] });
  235. }
  236. } else {
  237. cCodeRule = tenderData.c_rule !== null ? JSON.parse(tenderData.c_rule) : [];
  238. cConnector = tenderData.c_connector;
  239. changeCount = await ctx.service.change.count({ tid: tenderId });
  240. }
  241. const code = [];
  242. for (const rule of cCodeRule) {
  243. switch (rule.rule_type) {
  244. case codeRuleConst.measure.ruleType.dealCode:
  245. code.push(ctx.tender.info.deal_info.dealCode);
  246. break;
  247. case codeRuleConst.measure.ruleType.tenderName:
  248. code.push(tenderData.name);
  249. break;
  250. case codeRuleConst.measure.ruleType.text:
  251. code.push(rule.text);
  252. break;
  253. case codeRuleConst.measure.ruleType.inDate:
  254. code.push(moment().format('YYYY'));
  255. break;
  256. case codeRuleConst.measure.ruleType.addNo:
  257. let s = '0000000000';
  258. const count = rule.start + changeCount;
  259. s = s + count;
  260. code.push(s.substr(s.length - rule.format));
  261. break;
  262. default: break;
  263. }
  264. }
  265. responseData.data = code.join(cConnector !== null && parseInt(cConnector) !== 3 ? codeRuleConst.measure.connectorString[cConnector] : '');
  266. } catch (err) {
  267. responseData.err = 1;
  268. responseData.msg = err;
  269. }
  270. ctx.body = responseData;
  271. }
  272. /**
  273. * 新增变更 (Post)
  274. *
  275. * @param {Object} ctx - egg全局变量
  276. * @return {void}
  277. */
  278. async add(ctx) {
  279. try {
  280. const tenderId = ctx.params.id;
  281. if (!tenderId) {
  282. throw '当前未打开标段';
  283. }
  284. const data = JSON.parse(ctx.request.body.data);
  285. if (!data.code || data.code === '') {
  286. throw '变更令号不能为空';
  287. }
  288. const projectData = await ctx.service.project.getDataById(ctx.session.sessionProject.id);
  289. const fun_set = await ctx.service.project.getFunSet(projectData.fun_set);
  290. const stateInfo = ctx.helper._.find(fun_set.change_state, { order: 3 });
  291. // 在生成新变更令后,需要copy前一个变更令报表的签名信息
  292. const lastChange = await ctx.service.change.getLastChange(tenderId);
  293. const change = await ctx.service.change.add(tenderId, ctx.session.sessionUser.accountId, data.code, data.plan_code, data.name, data.add_plan_list, stateInfo.value);
  294. await ctx.service.roleRptRel.createRoleRelationshipFromOtherBz(tenderId, '-300', change.cid, lastChange ? lastChange.cid : null);
  295. ctx.body = { err: 0, msg: '', data: change };
  296. } catch (err) {
  297. this.log(err);
  298. ctx.body = { err: 1, msg: err.toString() };
  299. }
  300. }
  301. /**
  302. * 变更管理 状态筛选 页面 (Get)
  303. * @param {Object} ctx - egg全局变量
  304. * @return {void}
  305. */
  306. async status(ctx) {
  307. try {
  308. const status = parseInt(ctx.params.status);
  309. await this._filterChanges(ctx, status);
  310. } catch (err) {
  311. this.logger.error(err);
  312. ctx.redirect('/tender/' + ctx.params.id + '/change');
  313. }
  314. }
  315. async batchChangeFun(ctx) {
  316. try {
  317. const data = JSON.parse(ctx.request.body.data);
  318. const responseData = {
  319. err: 0,
  320. msg: '',
  321. data: {},
  322. };
  323. switch (data.type) {
  324. case 'get_report_list':
  325. responseData.data.shenpi_status = ctx.tender.info.shenpi.change;
  326. responseData.data.uncheckList = await ctx.service.change.getUncheckList(ctx.tender.id, ctx.tender.info.shenpi.change);
  327. if (ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdspl) {
  328. responseData.data.spGroupList = await ctx.service.shenpiGroup.getGroupListByChangeType(ctx.tender.id, shenpiConst.sp_type.change, 'change');
  329. } else {
  330. // 获取所有项目参与者
  331. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  332. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  333. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  334. });
  335. responseData.data.accountList = accountList;
  336. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  337. responseData.data.accountGroup = unitList.map(item => {
  338. const groupList = accountList.filter(item1 => item1.company === item.name);
  339. return { groupName: item.name, groupList };
  340. });
  341. }
  342. break;
  343. case 'get_shenpi_list':
  344. responseData.data.checkingList = await ctx.service.change.getCheckingList(ctx.tender.id, ctx.session.sessionUser.accountId);
  345. break;
  346. case 'set_shenpi_list':
  347. await ctx.service.changeAudit.batchSetAudits(ctx.tender.id, data.cids, data.from_cid);
  348. responseData.data.uncheckList = await ctx.service.change.getUncheckList(ctx.tender.id, ctx.tender.info.shenpi.change);
  349. break;
  350. default: throw '参数有误';
  351. }
  352. ctx.body = responseData;
  353. } catch (err) {
  354. this.log(err);
  355. ctx.body = { err: 1, msg: err.toString(), data: null };
  356. }
  357. }
  358. /**
  359. * 获取审批界面所需的 原报、审批人数据等
  360. * @param ctx
  361. * @return {Promise<void>}
  362. * @private
  363. */
  364. async _getChangeAuditViewData(ctx) {
  365. await ctx.service.change.loadChangeAuditViewData(ctx.change);
  366. // await this._checkStageStart(ctx);
  367. }
  368. /**
  369. * 变更信息 新版本页面 (Get)
  370. *
  371. * @param {Object} ctx - egg全局变量
  372. * @return {void}
  373. */
  374. async information(ctx) {
  375. try {
  376. const whiteList = this.ctx.app.config.multipart.whitelist;
  377. await this._getChangeAuditViewData(ctx);
  378. const tender = ctx.tender;
  379. const tenderData = await ctx.service.tender.getDataById(tender.id);
  380. const change = ctx.change;
  381. // 获取附件列表
  382. const attList = await ctx.service.changeAtt.getChangeAttachment(change.cid, 'desc');
  383. // 获取其他变更令数据
  384. const othersChange = await ctx.service.change.getOthersChange(ctx.tender.id, change.cid);
  385. // 获取变更方案的清单
  386. let planList = [];
  387. let showPlanBtn = false;
  388. if (ctx.session.sessionProject.page_show.openChangePlan) {
  389. const planInfo = change.plan_code ? await ctx.service.changePlan.getDataByCondition({ tid: tender.id, code: change.plan_code }) : null;
  390. showPlanBtn = change.plan_code !== null && change.plan_code !== '';
  391. if (planInfo && planInfo.id) {
  392. planList = await ctx.service.changePlanList.getAllDataByCondition({ where: { cpid: planInfo.id } });
  393. }
  394. }
  395. // 获取清单
  396. const changeList = await ctx.service.changeAuditList.getList(change.cid);
  397. if (change.status === audit.change.status.checked || change.status === audit.change.status.checking || change.status === audit.change.status.checkNoPre) {
  398. for (const cl of changeList) {
  399. const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
  400. // 清单表页赋值
  401. for (const au in change.userGroups) {
  402. if (change.userGroups[au][0].audit_order !== 0) {
  403. cl['audit_amount_' + change.userGroups[au][0].audit_order] =
  404. (change.userGroups[au][0].audit_type === auditType.key.and &&
  405. ctx.helper._.findIndex(change.userGroups[au], { status: audit.change.status.checking }) !== -1 &&
  406. ctx.helper._.findIndex(change.userGroups[au], { uid: ctx.session.sessionUser.accountId, status: audit.change.status.checked }) !== -1) ||
  407. (change.shenpiPower && ctx.helper._.findIndex(change.userGroups[au], { uid: ctx.session.sessionUser.accountId }) !== -1) ?
  408. cl.spamount : (audit_amount[au - 1] !== undefined ? parseFloat(audit_amount[au - 1]) : null);
  409. }
  410. }
  411. if (change.readOnly && !change.shenpiPower) {
  412. cl.checked_amount = audit_amount.length > 0 ? parseFloat(audit_amount[audit_amount.length - 1]) : cl.camount;
  413. }
  414. }
  415. }
  416. // 工程变更类别读取
  417. const projectData = await ctx.service.project.getDataById(ctx.session.sessionProject.id);
  418. const fun_set = await ctx.service.project.getFunSet(projectData.fun_set);
  419. // 获取用户人验证手机号
  420. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  421. const auth_mobile = pa.auth_mobile;
  422. const userPermission = pa && pa.permission ? JSON.parse(pa.permission) : null;
  423. const deleteFilePermission = !!(userPermission && userPermission.change !== undefined && userPermission.change.indexOf('2') !== -1);
  424. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  425. const settleBills = ctx.change.readySettle ? await ctx.service.settleBills.getAllDataByCondition({ where: { settle_id: ctx.change.readySettle.id } }) : [];
  426. const settlePos = ctx.change.readySettle ? await ctx.service.settlePos.getAllDataByCondition({ where: { settle_id: ctx.change.readySettle.id } }) : [];
  427. // 判断并是否更新结算清单数据
  428. await ctx.service.change.checkSettleUpdate(ctx.tender.id, ctx.change.readySettle);
  429. // 处理清单数据
  430. const removeSettleNum = change.status !== audit.change.status.checked ? await ctx.service.changeSettleList.updateChangeList(change.cid, settleBills, settlePos, changeList) : 0;
  431. const renderData = {
  432. tender,
  433. change,
  434. othersChange,
  435. changeConst,
  436. changeClass: fun_set.change_class,
  437. changeState: fun_set.change_state,
  438. auditConst: audit.change,
  439. ledgerConsts: audit.ledger.status,
  440. auditType,
  441. auditorGroups: change.userGroups,
  442. attList,
  443. whiteList,
  444. showPlanBtn,
  445. planList,
  446. tpUnit: change.tp_decimal ? change.tp_decimal : ctx.tender.info.decimal.tp,
  447. upUnit: change.up_decimal ? change.up_decimal : ctx.tender.info.decimal.up,
  448. authMobile: auth_mobile,
  449. shenpiConst,
  450. settleStatus: ctx.service.settle.settleStatus,
  451. unitList,
  452. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.information),
  453. preUrl: '/tender/' + ctx.tender.id + '/change/' + ctx.change.cid + '/information',
  454. precision: ctx.tender.info.precision,
  455. settleBills,
  456. settlePos,
  457. removeSettleNum,
  458. deleteFilePermission,
  459. changeMode: tenderData.c_mode,
  460. };
  461. // 获取是否已存在调用变更令
  462. let changeUsedData = await ctx.service.stageChange.getAllFinalUsedData(ctx.tender.id, change.cid);
  463. changeUsedData = ctx.helper._.orderBy(ctx.helper._.filter(changeUsedData, function(item) {
  464. return item.qty !== null;
  465. }), ['sorder'], ['desc']);
  466. const useChangeUsedData = [];
  467. if (changeUsedData.length > 0) { // 防止未创建期时调用
  468. for (const cu of changeUsedData) {
  469. const index = ctx.helper._.findIndex(useChangeUsedData, { cbid: cu.cbid });
  470. if (index !== -1) {
  471. useChangeUsedData[index].qty = ctx.helper.add(useChangeUsedData[index].qty, cu.qty);
  472. } else {
  473. useChangeUsedData.push(cu);
  474. }
  475. }
  476. }
  477. renderData.changeUsedData = useChangeUsedData;
  478. renderData.stageChangeNum = this.ctx.helper.sum(changeUsedData.map(x => { return Math.abs(x.qty); }));
  479. renderData.changeLedgerList = await ctx.service.changeLedger.getAllDataByCondition({ where: { tender_id: ctx.tender.id } });
  480. renderData.changePosList = await ctx.service.changePos.getAllDataByCondition({ where: { tid: ctx.tender.id } });
  481. renderData.changeUnits = changeConst.units;
  482. if (!change.readOnly || ctx.session.sessionUser.is_admin) {
  483. // 获取所有项目参与者
  484. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  485. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  486. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  487. });
  488. renderData.accountList = accountList;
  489. renderData.accountGroup = unitList.map(item => {
  490. const groupList = accountList.filter(item1 => item1.company === item.name);
  491. return { groupName: item.name, groupList };
  492. });
  493. // 获取公司列表
  494. const companyList = await ctx.service.changeCompany.getAllDataByCondition({ where: { tid: ctx.tender.id } });
  495. renderData.companyList = companyList;
  496. const stateInfo = ctx.helper._.find(fun_set.change_state, { order: ctx.change.state });
  497. renderData.deLimit = stateInfo.value;
  498. if (!change.readOnly) {
  499. if (stateInfo.value !== ctx.change.delimit) {
  500. // 需要更新所有的计量上限值,除了已调用的值
  501. const updateList = [];
  502. for (const cl of changeList) {
  503. const one = {
  504. id: cl.id,
  505. };
  506. if (useChangeUsedData.length > 0 && ctx.helper._.findIndex(useChangeUsedData, {cbid: cl.id}) !== -1) {
  507. // 获取比例值
  508. const uc = ctx.helper._.find(useChangeUsedData, {cbid: cl.id});
  509. const minLimit = Math.ceil(ctx.helper.mul(ctx.helper.div(uc.qty, cl.camount), 100));
  510. if (minLimit <= renderData.deLimit) {
  511. one.delimit = renderData.deLimit;
  512. cl.delimit = renderData.deLimit;
  513. }
  514. } else if (cl.delimit !== renderData.deLimit) {
  515. one.delimit = renderData.deLimit;
  516. cl.delimit = renderData.deLimit;
  517. }
  518. if (!ctx.helper._.isEqual(one, {id: cl.id})) updateList.push(one);
  519. }
  520. console.log(updateList);
  521. if (updateList.length > 0) await ctx.service.changeAuditList.defaultUpdateRows(updateList);
  522. await ctx.service.change.defaultUpdate({delimit: stateInfo.value}, {where: {cid: change.cid}});
  523. }
  524. // 判断是否更新变更类别
  525. if (ctx.helper._.findIndex(fun_set.change_class, {value: change.class, checked: true}) === -1) {
  526. renderData.change.class = ctx.helper._.find(fun_set.change_class, {checked: true}).value;
  527. await ctx.service.change.saveInfo({class: ctx.helper._.find(fun_set.change_class, {checked: true}).value});
  528. }
  529. // 获取固定审批流列表
  530. if (tender.info.shenpi.change === shenpiConst.sp_status.gdspl) {
  531. renderData.spGroupList = await ctx.service.shenpiGroup.getGroupListByChangeType(tender.id, shenpiConst.sp_type.change, 'change');
  532. }
  533. }
  534. }
  535. renderData.changeList = changeList;
  536. await this.layout('change/information.ejs', renderData, 'change/information_modal.ejs');
  537. } catch (err) {
  538. this.log(err);
  539. ctx.session.postError = err.toString();
  540. ctx.redirect('/tender/' + ctx.params.id + '/change');
  541. }
  542. }
  543. /**
  544. * 变更关联数据 - 操作 (Ajax)
  545. * @param ctx
  546. * @return {Promise<void>}
  547. */
  548. async relation(ctx) {
  549. try {
  550. if (!ctx.session.sessionProject.page_show.openChangePlan) {
  551. throw '该功能已关闭';
  552. }
  553. // await this._getChangeAuditViewData(ctx);
  554. const tender = ctx.tender;
  555. const change = ctx.change;
  556. const renderData = {
  557. tender,
  558. change,
  559. };
  560. renderData.changePlanInfo = null;
  561. renderData.changeApplyInfo = null;
  562. renderData.changeProjectInfo = null;
  563. if (change.plan_code) {
  564. renderData.changePlanInfo = await ctx.service.changePlan.getDataByCondition({ tid: tender.id, code: change.plan_code });
  565. if (renderData.changePlanInfo && renderData.changePlanInfo.apply_code) {
  566. renderData.changeApplyInfo = await ctx.service.changeApply.getDataByCondition({ tid: tender.id, code: renderData.changePlanInfo.apply_code });
  567. if (renderData.changeApplyInfo && renderData.changeApplyInfo.project_code) {
  568. renderData.changeProjectInfo = await ctx.service.changeProject.getDataByCondition({ tid: tender.id, code: renderData.changeApplyInfo.project_code });
  569. }
  570. }
  571. }
  572. await this.layout('change/relation.ejs', renderData);
  573. } catch (err) {
  574. this.log(err);
  575. ctx.session.postError = err.toString();
  576. ctx.redirect('/tender/' + ctx.params.id + '/change');
  577. }
  578. }
  579. /**
  580. * 变更清单 - 操作 (Ajax)
  581. * @param ctx
  582. * @return {Promise<void>}
  583. */
  584. async saveListsData(ctx) {
  585. try {
  586. const data = JSON.parse(ctx.request.body.data);
  587. const responseData = {
  588. err: 0,
  589. msg: '',
  590. data: {},
  591. };
  592. switch (data.type) {
  593. case 'add':
  594. case 'batchadd':
  595. if (!ctx.session.sessionProject.page_show.openChangeWhiteList) {
  596. throw '空白清单添加功能未开启。';
  597. }
  598. const projectData = await ctx.service.project.getDataById(ctx.session.sessionProject.id);
  599. const fun_set = await ctx.service.project.getFunSet(projectData.fun_set);
  600. const stateInfo = ctx.helper._.find(fun_set.change_state, { order: ctx.change.state });
  601. if (data.type === 'add') {
  602. responseData.data = await ctx.service.changeAuditList.add(data.postData, stateInfo.value);
  603. } else {
  604. responseData.data = await ctx.service.changeAuditList.batchAdd(data, stateInfo.value);
  605. }
  606. break;
  607. case 'del':
  608. await ctx.service.changeAuditList.del(data);
  609. break;
  610. case 'update':
  611. await ctx.service.changeAuditList.save(data.updateData);
  612. break;
  613. case 'paste':
  614. await ctx.service.changeAuditList.saveDatas(data.updateData);
  615. // 取所有工料表
  616. responseData.data = await ctx.service.changeAuditList.getList(ctx.change.cid);
  617. break;
  618. case 'ledger_list':
  619. await ctx.service.changeAuditList.saveLedgerListDatas(data.updateData, data.postData);
  620. // 取所有工料表
  621. responseData.data = { changeList: await ctx.service.changeAuditList.getList(ctx.change.cid),
  622. usedList: await ctx.service.stageChange.getAllFinalUsedData(ctx.tender.id, ctx.change.cid) };
  623. break;
  624. case 'remove_list':
  625. await ctx.service.changeAuditList.removeLedgerListDatas(data.updateData);
  626. // 取所有工料表
  627. responseData.data = await ctx.service.changeAuditList.getList(ctx.change.cid);
  628. break;
  629. case 'update_list':
  630. await ctx.service.changeAuditList.saveDatas(data.updateData);
  631. // 取所有工料表
  632. // responseData.data = await ctx.service.changeAuditList.getList(ctx.change.cid);
  633. break;
  634. case 'info':
  635. let value = 100;
  636. if (data.updateData.state && parseInt(data.updateData.state) !== ctx.change.state) {
  637. const projectData = await ctx.service.project.getDataById(ctx.session.sessionProject.id);
  638. const fun_set = await ctx.service.project.getFunSet(projectData.fun_set);
  639. const stateInfo = ctx.helper._.find(fun_set.change_state, { order: parseInt(data.updateData.state) });
  640. value = stateInfo.value;
  641. }
  642. responseData.data = await ctx.service.change.saveInfo(data.updateData, value);
  643. // 取所有工料表
  644. // responseData.data = '保存成功';
  645. break;
  646. case 'paste_amount_rows':
  647. await ctx.service.changeAuditList.saveDatas(data.updateData);
  648. const changeList = await ctx.service.changeAuditList.getList(ctx.change.cid);
  649. const auditList2 = await ctx.service.changeAudit.getListGroupByTimes(ctx.change.cid, ctx.change.times);
  650. // changeList = JSON.parse(JSON.stringify(changeList.sort())).sort().sort();
  651. // for (const cl of changeList) {
  652. // const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
  653. // // 清单表页赋值
  654. // for (const [index, au] of auditList2.entries()) {
  655. // if (au.usite !== 0) {
  656. // cl['audit_amount_' + au.uid] = au.uid === ctx.session.sessionUser.accountId ? cl.spamount : (audit_amount[index - 1] ? audit_amount[index - 1] : null);
  657. // }
  658. // }
  659. // }
  660. for (const cl of changeList) {
  661. const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
  662. // 清单表页赋值
  663. for (const au in ctx.change.userGroups) {
  664. if (ctx.change.userGroups[au][0].audit_order !== 0) {
  665. cl['audit_amount_' + ctx.change.userGroups[au][0].audit_order] =
  666. (ctx.change.userGroups[au][0].audit_type === auditType.key.and &&
  667. ctx.helper._.findIndex(ctx.change.userGroups[au], { status: audit.change.status.checking }) !== -1 &&
  668. ctx.helper._.findIndex(ctx.change.userGroups[au], { uid: ctx.session.sessionUser.accountId, status: audit.change.status.checked }) !== -1) ||
  669. (ctx.change.shenpiPower && ctx.helper._.findIndex(ctx.change.userGroups[au], { uid: ctx.session.sessionUser.accountId }) !== -1) ?
  670. cl.spamount : (audit_amount[au - 1] !== undefined ? parseFloat(audit_amount[au - 1]) : null);
  671. }
  672. }
  673. }
  674. // 取所有工料表
  675. responseData.data = changeList;
  676. break;
  677. case 'update_tp':
  678. await ctx.service.change.saveInfo(data.updateData);
  679. break;
  680. case 'order_by':
  681. const result = await ctx.service.change.saveOrderBy(data.updateData, data.updateOrderList);
  682. responseData.data = result;
  683. break;
  684. case 'order_site':
  685. await ctx.service.change.defaultUpdate({ order_site: data.listid }, { where: { cid: ctx.change.cid } });
  686. break;
  687. case 'changeOrder':
  688. await ctx.service.changeAuditList.changeOrder(data.postData);
  689. break;
  690. case 'set_all_valuation':
  691. await ctx.service.changeAuditList.setAllValuation(ctx.change.cid, data.ids, data.is_valuation);
  692. // 取所有工料表
  693. responseData.data = await ctx.service.changeAuditList.getList(ctx.change.cid);
  694. break;
  695. // 从新增部位页新增清单
  696. case 'add-change-list':
  697. if (ctx.change.status !== audit.change.status.uncheck && ctx.change.status !== audit.change.status.checkNo && ctx.change.status !== audit.change.status.revise) {
  698. throw '该变更令正在审批中或已完成,无法操作清单数据';
  699. }
  700. const projectData2 = await ctx.service.project.getDataById(ctx.session.sessionProject.id);
  701. const fun_set2 = await ctx.service.project.getFunSet(projectData2.fun_set);
  702. const stateInfo2 = ctx.helper._.find(fun_set2.change_state, { order: ctx.change.state });
  703. await ctx.service.changeAuditList.adds(data.postData, stateInfo2.value);
  704. responseData.data = { changeList: await ctx.service.changeAuditList.getList(ctx.change.cid) };
  705. break;
  706. // 从新增部位页删除清单
  707. case 'del-change-list':
  708. if (ctx.change.status !== audit.change.status.uncheck && ctx.change.status !== audit.change.status.checkNo && ctx.change.status !== audit.change.status.revise) {
  709. throw '该变更令正在审批中或已完成,无法操作清单数据';
  710. }
  711. await ctx.service.changeAuditList.dels(data);
  712. responseData.data = { changeList: await ctx.service.changeAuditList.getList(ctx.change.cid) };
  713. break;
  714. case 'get-revise-list':
  715. responseData.data.changeLedgerList = await ctx.service.changeLedger.getAllDataByCondition({ where: { tender_id: ctx.tender.id } });
  716. responseData.data.changePosList = await ctx.service.changePos.getAllDataByCondition({ where: { tid: ctx.tender.id } });
  717. break;
  718. case 'plan_code':
  719. responseData.data.planInfo = null;
  720. responseData.data.applyInfo = null;
  721. responseData.data.projectInfo = null;
  722. await ctx.service.change.defaultUpdate({ plan_code: data.plan_code }, { where: { cid: ctx.change.cid } });
  723. if (data.plan_code) {
  724. responseData.data.planInfo = await ctx.service.changePlan.getDataByCondition({ tid: ctx.tender.id, code: data.plan_code });
  725. if (responseData.data.planInfo && responseData.data.planInfo.apply_code) {
  726. responseData.data.applyInfo = await ctx.service.changeApply.getDataByCondition({ tid: ctx.tender.id, code: responseData.data.planInfo.apply_code });
  727. if (responseData.data.applyInfo && responseData.data.applyInfo.project_code) {
  728. responseData.data.projectInfo = await ctx.service.changeProject.getDataByCondition({ tid: ctx.tender.id, code: responseData.data.applyInfo.project_code });
  729. }
  730. }
  731. }
  732. break;
  733. case 'change_mode':
  734. await ctx.service.tender.defaultUpdate({
  735. id: ctx.tender.id,
  736. c_mode: data.updateData ? 1 : 0,
  737. });
  738. const changeList2 = await ctx.service.changeAuditList.getList(ctx.change.cid);
  739. if (ctx.change.readOnly) {
  740. const auditList2 = await ctx.service.changeAudit.getListGroupByTimes(ctx.change.cid, ctx.change.times);
  741. for (const cl of changeList2) {
  742. const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
  743. // 清单表页赋值
  744. for (const au in ctx.change.userGroups) {
  745. if (ctx.change.userGroups[au][0].audit_order !== 0) {
  746. cl['audit_amount_' + ctx.change.userGroups[au][0].audit_order] =
  747. (ctx.change.userGroups[au][0].audit_type === auditType.key.and &&
  748. ctx.helper._.findIndex(ctx.change.userGroups[au], { status: audit.change.status.checking }) !== -1 &&
  749. ctx.helper._.findIndex(ctx.change.userGroups[au], { uid: ctx.session.sessionUser.accountId, status: audit.change.status.checked }) !== -1) ||
  750. (ctx.change.shenpiPower && ctx.helper._.findIndex(ctx.change.userGroups[au], { uid: ctx.session.sessionUser.accountId }) !== -1) ?
  751. cl.spamount : (audit_amount[au - 1] !== undefined ? parseFloat(audit_amount[au - 1]) : null);
  752. }
  753. }
  754. }
  755. }
  756. // 取所有工料表
  757. responseData.data = changeList2;
  758. break;
  759. default: throw '参数有误';
  760. }
  761. ctx.body = responseData;
  762. } catch (err) {
  763. this.log(err);
  764. ctx.body = { err: 1, msg: err.toString(), data: null };
  765. }
  766. }
  767. /**
  768. * 上报和重新上报
  769. * @param ctx
  770. * @return {Promise<void>}
  771. */
  772. async startAudit(ctx) {
  773. const responseData = {
  774. err: 0,
  775. msg: '',
  776. data: {},
  777. };
  778. try {
  779. // 检查权限等
  780. if (!ctx.change) {
  781. throw '数据错误';
  782. }
  783. if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
  784. throw '您无权上报该变更令数据';
  785. }
  786. if (!(ctx.change.status === audit.change.status.uncheck || ctx.change.status === audit.change.status.checkNo || ctx.change.status === audit.change.status.revise)) {
  787. throw '该变更令当前无法上报';
  788. }
  789. // 判断是否是修订,判断有无审批人员作弊
  790. if ((ctx.change.status === audit.change.status.revise || ctx.change.is_revise) && ctx.tender.info.shenpi.change === shenpiConst.sp_status.sqspr) {
  791. // 获取上一次的审批人流程
  792. const lastUserList = await ctx.service.changeAudit.getUniqAuditor(ctx.change.cid, ctx.change.times - 1);
  793. const lastUidList = ctx.helper._.map(lastUserList, 'uid');
  794. // 判断上一次审批是否为非原报为审批人
  795. const nowUidList = ctx.helper._.map(ctx.change.auditors, 'uid');
  796. // 判断条件修订可上报条件
  797. // 1.有原报,不管有无其他人 可以添加原报作为审核人,但是不能只原报上报
  798. // 2.无原报,有其他人 不可以添加原报
  799. // const noYBUidList = ctx.change.status === audit.change.status.revise ? ctx.helper._.initial(ctx.helper._.tail(lastUidList)) : ctx.helper._.tail(lastUidList);
  800. const noYBUidList = ctx.helper._.tail(lastUidList);
  801. // if (!ctx.helper._.isEqual(lastUidList, nowUidList) && ctx.helper._.includes(noYBUidList, lastUidList[0]) && nowUidList.length === 2 && nowUidList[0] === nowUidList[1]) {
  802. if (nowUidList.length === 2 && nowUidList[0] === nowUidList[1]) {
  803. throw '该变更令不能指定原报人为单独审批人';
  804. }
  805. if (!ctx.helper._.isEqual(lastUidList, nowUidList) && !ctx.helper._.includes(noYBUidList, lastUidList[0]) && ctx.helper._.includes(ctx.helper._.tail(nowUidList), nowUidList[0])) {
  806. throw '该变更令不能指定原报为审批人,请移除';
  807. }
  808. }
  809. await ctx.service.changeAudit.start(ctx.change.cid, ctx.change.times);
  810. // ctx.redirect(ctx.request.header.referer);
  811. ctx.body = responseData;
  812. } catch (err) {
  813. this.log(err);
  814. // ctx.session.postError = err.toString();
  815. // ctx.redirect(ctx.request.header.referer);
  816. ctx.body = { err: 1, msg: err.toString(), data: null };
  817. }
  818. }
  819. // 审批相关
  820. /**
  821. * 添加审批人
  822. * @param ctx
  823. * @return {Promise<void>}
  824. */
  825. async addAudit(ctx) {
  826. try {
  827. const data = JSON.parse(ctx.request.body.data);
  828. const id = this.app._.toInteger(data.auditorId);
  829. if (isNaN(id) || id <= 0) {
  830. throw '参数错误';
  831. }
  832. // 检查权限等
  833. if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
  834. throw '您无权添加审核人';
  835. }
  836. if (ctx.change.status === audit.change.status.checking || ctx.change.status === audit.change.status.checked) {
  837. throw '当前不允许添加审核人';
  838. }
  839. const auditorList = await ctx.service.changeAudit.getAuditors(ctx.change.cid, ctx.change.times);
  840. // 检查审核人是否已存在
  841. const exist = this.app._.find(auditorList, { uid: id });
  842. if (exist) {
  843. throw '该审核人已存在,请勿重复添加';
  844. }
  845. const shenpiInfo = await ctx.service.shenpiAudit.getDataByCondition({ tid: ctx.tender.id, sp_type: shenpiConst.sp_type.change, sp_status: shenpiConst.sp_status.gdzs });
  846. const is_gdzs = shenpiInfo && ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdzs ? 1 : 0;
  847. const result = await ctx.service.changeAudit.addAuditor(ctx.change.cid, id, ctx.change.times, is_gdzs);
  848. if (!result) {
  849. throw '添加审核人失败';
  850. }
  851. const auditors = await ctx.service.changeAudit.getUserGroup(ctx.change.cid, ctx.change.times);
  852. ctx.body = { err: 0, msg: '', data: auditors };
  853. } catch (err) {
  854. this.log(err);
  855. ctx.body = { err: 1, msg: err.toString(), data: null };
  856. }
  857. }
  858. /**
  859. * 移除审批人
  860. * @param ctx
  861. * @return {Promise<void>}
  862. */
  863. async deleteAudit(ctx) {
  864. try {
  865. const data = JSON.parse(ctx.request.body.data);
  866. const id = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
  867. if (isNaN(id) || id <= 0) {
  868. throw '参数错误';
  869. }
  870. const result = await ctx.service.changeAudit.deleteAuditor(ctx.change.cid, id, ctx.change.times);
  871. if (!result) {
  872. throw '移除审核人失败';
  873. }
  874. const auditors = await ctx.service.changeAudit.getUserGroup(ctx.change.cid, ctx.change.times);
  875. ctx.body = { err: 0, msg: '', data: auditors };
  876. } catch (err) {
  877. ctx.body = { err: 1, msg: err.toString(), data: null };
  878. }
  879. }
  880. async changeSpGroup(ctx) {
  881. try {
  882. const data = JSON.parse(ctx.request.body.data);
  883. const result = await ctx.service.changeAudit.changeSpGroup(ctx.change, data.sp_group);
  884. if (!result) {
  885. throw '切换审批组失败';
  886. }
  887. const auditors = await ctx.service.changeAudit.getUserGroup(ctx.change.cid, ctx.change.times);
  888. ctx.body = { err: 0, msg: '', data: auditors };
  889. } catch (err) {
  890. ctx.body = { err: 1, msg: err.toString(), data: null };
  891. }
  892. }
  893. async defaultBills(ctx) {
  894. try {
  895. // 判断是否台账修订中,修订中则不获取changeLedger及changePos值
  896. const lastRevise = await ctx.service.ledgerRevise.getLastestRevise(ctx.tender.id);
  897. const data = JSON.parse(ctx.request.body.data);
  898. const readySettle = await ctx.service.settle.getReadySettle(ctx.tender.id);
  899. if (data.from === 'revise') await ctx.service.change.checkSettleUpdate(ctx.tender.id, readySettle);
  900. const ledgerData = await ctx.service.ledger.getData(ctx.tender.id);
  901. 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));
  902. const posData = await ctx.service.pos.getPosData({ tid: ctx.tender.id });
  903. 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 }));
  904. const dealBills = await ctx.service.dealBills.getAllDataByCondition({ where: { tender_id: ctx.tender.id } });
  905. // const settleStatus = ctx.service.settle.settleStatus;
  906. const settleBills = readySettle ? await this.ctx.service.settleBills.getAllDataByCondition({ where: { settle_id: readySettle.id } }) : [];
  907. const stages = data.from === 'revise' ? await ctx.service.stage.getAllDataByCondition({
  908. where: { tid: ctx.tender.id },
  909. orders: [['order', 'desc']],
  910. }) : [];
  911. if (stages.length > 0) {
  912. const usedBills = [], usedPos = [];
  913. for (const s of stages) {
  914. if (s.status === audit.stage.status.checked) {
  915. const usedPreBills = await ctx.service.stageBillsFinal.getUsedBills(ctx.tender.id, s.order);
  916. usedBills.push(...usedPreBills);
  917. const usedPrePos = await ctx.service.stagePosFinal.getUsedPos(ctx.tender.id, s.order);
  918. usedPos.push(...usedPrePos);
  919. break;
  920. } else {
  921. const usedCurBills = await ctx.service.stageBills.getStageUsedBills(ctx.tender.id, s.id);
  922. usedBills.push(...usedCurBills);
  923. const usedCurPos = await ctx.service.stagePos.getStageUsedPos(ctx.tender.id, s.id);
  924. usedPos.push(...usedCurPos);
  925. }
  926. }
  927. for (const b of ledgerData) {
  928. b.used = usedBills.indexOf(b.id) >= 0;
  929. }
  930. for (const p of posData) {
  931. p.used = usedPos.indexOf(p.id) >= 0;
  932. }
  933. }
  934. ctx.helper.assignRelaData(ledgerData, [
  935. { data: settleBills, fields: ['settle_status'], prefix: '', relaId: 'lid' },
  936. ]);
  937. const settlePos = readySettle ? await this.ctx.service.settlePos.getAllDataByCondition({ where: { settle_id: readySettle.id } }) : [];
  938. this.ctx.helper.assignRelaData(posData, [
  939. { data: settlePos, fields: ['settle_status'], prefix: '', relaId: 'pid' },
  940. { data: settlePos, fields: ['settle_status'], prefix: '', relaId: 'pid' },
  941. ]);
  942. // 标记ledger,搜索需求
  943. if (changePosData.length > 0) {
  944. const cplIdList = ctx.helper._.uniq(ctx.helper._.map(changePosData, 'lid'));
  945. const cLIdList = ctx.helper._.map(changeLedgerData, 'id');
  946. const ledgerIdList = [];
  947. for (const cp of cplIdList) {
  948. if (ctx.helper._.indexOf(cLIdList, cp) === -1) {
  949. ledgerIdList.push(cp);
  950. }
  951. }
  952. if (ledgerIdList.length > 0) {
  953. for (const lid of ledgerIdList) {
  954. const data = ctx.helper._.find(ledgerData, { id: lid });
  955. if (data) data.cid = 1;
  956. }
  957. }
  958. }
  959. const bodyData = { bills: ctx.helper._.concat(ledgerData, changeLedgerData), pos: ctx.helper._.concat(posData, changePosData), dealBills };
  960. if (data.from === 'batch') {
  961. bodyData.changeLedgerList = changeLedgerData;
  962. bodyData.changePosList = changePosData;
  963. }
  964. ctx.body = { err: 0, msg: '', data: bodyData };
  965. } catch (err) {
  966. console.log(err);
  967. this.log(err);
  968. ctx.body = { err: 1, msg: err.toString(), data: [] };
  969. }
  970. }
  971. /**
  972. * 变更令上报和保存
  973. * @param {Object} ctx - egg全局变量
  974. * @return {void}
  975. */
  976. async save(ctx) {
  977. // 变更令信息
  978. const changeInfo = await ctx.service.change.getDataByCondition({ cid: ctx.request.body.cid });
  979. try {
  980. const result = await ctx.service.change.save(ctx.request.body, changeInfo.tid);
  981. if (!result) {
  982. throw '上报失败';
  983. }
  984. if (ctx.request.body.changestatus !== undefined && parseInt(ctx.request.body.changestatus) === 2) {
  985. ctx.body = { err: 0, msg: '保存成功' };
  986. } else {
  987. ctx.redirect('/tender/' + changeInfo.tid + '/change');
  988. }
  989. } catch (err) {
  990. this.log(err);
  991. if (ctx.request.body.changestatus !== undefined && parseInt(ctx.request.body.changestatus) === 2) {
  992. ctx.body = { err: 1, msg: err.toString() };
  993. } else {
  994. ctx.redirect('/tender/' + changeInfo.tid + '/change/' + ctx.request.body.cid + '/info');
  995. }
  996. }
  997. }
  998. /**
  999. * 变更令审批
  1000. * @param {Object} ctx - egg全局变量
  1001. * @return {void}
  1002. */
  1003. async approval(ctx) {
  1004. const data = JSON.parse(ctx.request.body.data);
  1005. const responseData = {
  1006. err: 0,
  1007. msg: '',
  1008. data: {},
  1009. };
  1010. try {
  1011. const changeData = await ctx.service.change.getDataByCondition({ cid: data.change_id });
  1012. if (!changeData) {
  1013. throw '变更令数据错误';
  1014. }
  1015. const status = parseInt(data.status);
  1016. // 判断是否到你审批,如果不是则无法审批
  1017. const curAuditor = await ctx.service.changeAudit.getCurAuditors(changeData.cid, changeData.times);
  1018. if (!curAuditor || (curAuditor && ctx.helper._.findIndex(curAuditor, { uid: ctx.session.sessionUser.accountId }) === -1)) {
  1019. throw '该变更令当前您无权操作';
  1020. }
  1021. const readySettle = await ctx.service.settle.getReadySettle(changeData.tid);
  1022. if (readySettle && readySettle.settle_order !== ctx.tender.data.settle_order) {
  1023. throw '结算数据发生变化,请刷新页面再提交';
  1024. }
  1025. let result = false;
  1026. const pid = this.ctx.session.sessionProject.id;
  1027. switch (status) {
  1028. case 3:// 审批通过
  1029. result = await ctx.service.change.approvalSuccess(pid, data, changeData);
  1030. break;
  1031. // case 4:// 审批终止
  1032. // result = await ctx.service.change.approvalStop(ctx.request.body);
  1033. // break;
  1034. case 5:// 审批退回到原报人
  1035. result = await ctx.service.change.approvalCheckNo(pid, data, changeData);
  1036. break;
  1037. case 6:// 审批退回到上一个审批人
  1038. result = await ctx.service.change.approvalCheckNoPre(pid, data, changeData);
  1039. break;
  1040. default:break;
  1041. }
  1042. if (!result) {
  1043. throw '审批失败';
  1044. }
  1045. ctx.body = responseData;
  1046. } catch (err) {
  1047. console.log(err);
  1048. // ctx.session.postError = err.toString();
  1049. // ctx.redirect(ctx.request.header.referer);
  1050. ctx.body = { err: 1, msg: err.toString(), data: null };
  1051. }
  1052. }
  1053. /**
  1054. * 撤回审批
  1055. * @param ctx
  1056. * @return {Promise<void>}
  1057. */
  1058. async checkAuditCancel(ctx) {
  1059. try {
  1060. if (!ctx.change.cancancel) throw '您无权进行该操作';
  1061. if (ctx.change.readySettle && ctx.change.readySettle.settle_order !== ctx.tender.data.settle_order) {
  1062. throw '结算数据发生变化,请刷新页面再提交';
  1063. }
  1064. await ctx.service.changeAudit.checkCancel(ctx.change);
  1065. ctx.body = { err: 0, url: ctx.request.header.referer, msg: '' };
  1066. } catch (err) {
  1067. this.log(err);
  1068. ctx.body = { err: 1, msg: err };
  1069. }
  1070. }
  1071. /**
  1072. * 变更公司管理
  1073. * @param {Object} ctx - egg全局变量
  1074. * @return {void}
  1075. */
  1076. async updateCompany(ctx) {
  1077. const responseData = {
  1078. err: 0,
  1079. msg: '',
  1080. data: '',
  1081. };
  1082. try {
  1083. const data = JSON.parse(ctx.request.body.data);
  1084. if (data.tid === undefined || data.uci === undefined || data.uc === undefined || data.ac === undefined) {
  1085. throw '参数有误';
  1086. }
  1087. const [addCompany, selectCompany] = await ctx.service.changeCompany.setCompanyList(data);
  1088. responseData.data = { add: addCompany, select: selectCompany };
  1089. } catch (err) {
  1090. responseData.err = 1;
  1091. responseData.msg = err;
  1092. }
  1093. ctx.body = responseData;
  1094. }
  1095. /**
  1096. * 上传附件
  1097. * @param {Object} ctx - egg全局变量
  1098. * @return {void}
  1099. */
  1100. async uploadFile(ctx) {
  1101. const responseData = {
  1102. err: 0,
  1103. msg: '',
  1104. data: [],
  1105. };
  1106. let stream;
  1107. try {
  1108. const parts = ctx.multipart({ autoFields: true });
  1109. const files = [];
  1110. let index = 0;
  1111. const change = await ctx.service.change.getDataByCondition({ cid: ctx.params.cid });
  1112. const extra_upload = change.status === audit.change.status.checked;
  1113. while ((stream = await parts()) !== undefined) {
  1114. // 判断用户是否选择上传文件
  1115. if (!stream.filename) {
  1116. throw '请选择上传的文件!';
  1117. }
  1118. // const create_time = Date.parse(new Date()) / 1000;
  1119. // const fileInfo = path.parse(stream.filename);
  1120. // const dirName = 'app/public/upload/changes/' + moment().format('YYYYMMDD');
  1121. // const fileName = 'changes' + create_time + '_' + index + fileInfo.ext;
  1122. // // 判断文件夹是否存在,不存在则直接创建文件夹
  1123. // if (!fs.existsSync(path.join(this.app.baseDir, dirName))) {
  1124. // await fs.mkdirSync(path.join(this.app.baseDir, dirName));
  1125. // }
  1126. // // 保存文件
  1127. // await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, dirName, fileName));
  1128. const fileInfo = path.parse(stream.filename);
  1129. const create_time = Date.parse(new Date()) / 1000;
  1130. // const filepath = `app/public/upload/change/fujian_${create_time + index.toString() + fileInfo.ext}`;
  1131. const filepath = `app/public/upload/${parts.field.tid}/change/fujian_${create_time + index.toString() + fileInfo.ext}`;
  1132. // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, filepath));
  1133. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  1134. await sendToWormhole(stream);
  1135. // 保存数据到att表
  1136. const fileData = {
  1137. in_time: create_time,
  1138. filename: fileInfo.name,
  1139. fileext: fileInfo.ext,
  1140. filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
  1141. filepath,
  1142. extra_upload,
  1143. };
  1144. const result = await ctx.service.changeAtt.save(parts.field, fileData, ctx.session.sessionUser.accountId);
  1145. if (!result) {
  1146. throw '导入数据库保存失败';
  1147. }
  1148. // fileData.in_time = moment(create_time * 1000).format('YYYY-MM-DD');
  1149. // fileData.filesize = await ctx.helper.bytesToSize(fileData.filesize);
  1150. fileData.uid = ctx.session.sessionUser.accountId;
  1151. fileData.id = result.insertId;
  1152. fileData.orginpath = ctx.app.config.fujianOssPath + filepath;
  1153. delete fileData.filepath;
  1154. if (!ctx.helper.canPreview(fileData.fileext)) {
  1155. fileData.filepath = `/change/download/file/${fileData.id}`;
  1156. } else {
  1157. fileData.filepath = ctx.app.config.fujianOssPath + filepath;
  1158. }
  1159. files.push(fileData);
  1160. ++index;
  1161. }
  1162. responseData.data = files;
  1163. } catch (err) {
  1164. this.log(err);
  1165. // 失败需要消耗掉stream 以防卡死
  1166. if (stream) {
  1167. await sendToWormhole(stream);
  1168. }
  1169. this.setMessage(err.toString(), this.messageType.ERROR);
  1170. }
  1171. ctx.body = responseData;
  1172. }
  1173. /**
  1174. * 下载附件
  1175. * @param {Object} ctx - egg全局变量
  1176. * @return {void}
  1177. */
  1178. async downloadFile(ctx) {
  1179. const id = ctx.params.id;
  1180. if (id) {
  1181. try {
  1182. const fileInfo = await ctx.service.changeAtt.getDataById(id);
  1183. if (fileInfo !== undefined && fileInfo !== '') {
  1184. // const fileName = path.join(this.app.baseDir, fileInfo.filepath);
  1185. // 解决中文无法下载问题
  1186. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  1187. let disposition = '';
  1188. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  1189. disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename + fileInfo.fileext);
  1190. } else if (userAgent.indexOf('firefox') >= 0) {
  1191. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename + fileInfo.fileext) + '"';
  1192. } else {
  1193. /* safari等其他非主流浏览器只能自求多福了 */
  1194. disposition = 'attachment; filename=' + new Buffer(fileInfo.filename + fileInfo.fileext).toString('binary');
  1195. }
  1196. ctx.response.set({
  1197. 'Content-Type': 'application/octet-stream',
  1198. 'Content-Disposition': disposition,
  1199. 'Content-Length': fileInfo.filesize,
  1200. });
  1201. // ctx.body = await fs.createReadStream(fileName);
  1202. ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
  1203. } else {
  1204. throw '不存在该文件';
  1205. }
  1206. } catch (err) {
  1207. this.log(err);
  1208. this.setMessage(err.toString(), this.messageType.ERROR);
  1209. }
  1210. }
  1211. }
  1212. /**
  1213. * 批量下载 - 压缩成zip文件返回
  1214. * @param {Oject} ctx - 全局上下文
  1215. */
  1216. async downloadZip(ctx) {
  1217. const time = Date.now();
  1218. const zipPath = `app/public/upload/change/fu_jian_zip${time}.zip`;
  1219. const responseData = {
  1220. err: 0,
  1221. msg: '',
  1222. };
  1223. try {
  1224. // const fileIds = JSON.parse(ctx.request.query.fileIds);
  1225. const { fileIds = [] } = JSON.parse(ctx.request.body.data);
  1226. const { name: changeName } = await ctx.service.changeAtt.getChangeName(ctx.params.cid);
  1227. const zipFilename = `${ctx.tender.data.name}-工程变更-${changeName}-附件.zip`;
  1228. const size = await ctx.service.changeAtt.compressedFile(fileIds, zipPath);
  1229. // 解决中文无法下载问题
  1230. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  1231. let disposition = '';
  1232. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  1233. disposition = 'attachment; filename=' + encodeURIComponent(zipFilename);
  1234. } else if (userAgent.indexOf('firefox') >= 0) {
  1235. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(zipFilename) + '"';
  1236. } else {
  1237. /* safari等其他非主流浏览器只能自求多福了 */
  1238. disposition = 'attachment; filename=' + new Buffer(zipFilename).toString('binary');
  1239. }
  1240. ctx.response.set({
  1241. 'Content-Type': 'application/octet-stream',
  1242. 'Content-Disposition': disposition,
  1243. 'Content-Length': size,
  1244. });
  1245. const readStream = fs.createReadStream(path.join(this.app.baseDir, zipPath));
  1246. ctx.body = readStream;
  1247. // ctx.body = fs.readFileSync(path.resolve(this.app.baseDir, zipPath));
  1248. readStream.on('close', () => {
  1249. if (fs.existsSync(path.resolve(this.app.baseDir, zipPath))) {
  1250. fs.unlinkSync(path.resolve(this.app.baseDir, zipPath));
  1251. }
  1252. });
  1253. // fs的错误不能被try catch捕捉
  1254. readStream.on('error', err => {
  1255. this.log(err);
  1256. if (fs.existsSync(path.resolve(this.app.baseDir, zipPath))) {
  1257. fs.unlinkSync(path.resolve(this.app.baseDir, zipPath));
  1258. }
  1259. responseData.err = 1;
  1260. responseData.msg = err.toString();
  1261. ctx.body = responseData;
  1262. });
  1263. } catch (err) {
  1264. this.log(err);
  1265. if (fs.existsSync(path.resolve(this.app.baseDir, zipPath))) {
  1266. fs.unlinkSync(path.resolve(this.app.baseDir, zipPath));
  1267. }
  1268. this.setMessage(err.toString(), this.messageType.ERROR);
  1269. responseData.err = 1;
  1270. responseData.msg = err.toString();
  1271. ctx.body = responseData;
  1272. }
  1273. }
  1274. /**
  1275. * 删除附件
  1276. * @param {Object} ctx - egg全局变量
  1277. * @return {void}
  1278. */
  1279. async deleteFile(ctx) {
  1280. const responseData = {
  1281. err: 0,
  1282. msg: '',
  1283. data: '',
  1284. };
  1285. try {
  1286. const data = JSON.parse(ctx.request.body.data);
  1287. const change = await ctx.service.change.getDataByCondition({ cid: ctx.params.cid });
  1288. const fileInfo = await ctx.service.changeAtt.getDataById(data.id);
  1289. if (!fileInfo || !Object.keys(fileInfo).length) {
  1290. throw '该文件不存在';
  1291. }
  1292. const accountInfo = await this.ctx.service.projectAccount.getDataById(this.ctx.session.sessionUser.accountId);
  1293. const userPermission = accountInfo && accountInfo.permission ? JSON.parse(accountInfo.permission) : null;
  1294. const deleteFilePermission = !!(userPermission && userPermission.change !== undefined && userPermission.change.indexOf('2') !== -1);
  1295. if (!fileInfo.extra_upload && change.status === audit.change.status.checked && !deleteFilePermission) {
  1296. throw '无权限删除';
  1297. }
  1298. if (fileInfo !== undefined && fileInfo !== '') {
  1299. // 先删除文件
  1300. // await fs.unlinkSync(path.join(this.app.baseDir, fileInfo.filepath));
  1301. await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
  1302. // 再删除数据库
  1303. await ctx.service.changeAtt.deleteById(data.id);
  1304. responseData.data = '';
  1305. } else {
  1306. throw '不存在该文件';
  1307. }
  1308. // if (data.tid === undefined || data.uci === undefined || data.uc === undefined || data.ac === undefined) {
  1309. // throw '参数有误';
  1310. // }
  1311. // const [addCompany, selectCompany] = await ctx.service.changeCompany.setCompanyList(data);
  1312. // responseData.data = { add: addCompany, select: selectCompany };
  1313. } catch (err) {
  1314. responseData.err = 1;
  1315. responseData.msg = err;
  1316. }
  1317. ctx.body = responseData;
  1318. }
  1319. /**
  1320. * 查看附件
  1321. * @param {Object} ctx - egg全局变量
  1322. * @return {void}
  1323. */
  1324. async checkFile(ctx) {
  1325. const responseData = { err: 0, msg: '' };
  1326. const id = parseInt(ctx.params.id);
  1327. if (id) {
  1328. try {
  1329. const fileInfo = await ctx.service.changeAtt.getDataById(id);
  1330. if (fileInfo && Object.keys(fileInfo).length) {
  1331. let filepath = fileInfo.filepath;
  1332. if (!ctx.helper.canPreview(fileInfo.fileext)) {
  1333. filepath = `/change/download/file/${fileInfo.id}`;
  1334. } else {
  1335. filepath = filepath.replace(/^app|\/app/, '');
  1336. }
  1337. fileInfo.filepath && (responseData.data = { filepath });
  1338. }
  1339. } catch (error) {
  1340. this.log(error);
  1341. this.setMessage(error.toString(), this.messageType.ERROR);
  1342. responseData.err = 1;
  1343. responseData.msg = error.toString();
  1344. }
  1345. }
  1346. ctx.body = responseData;
  1347. }
  1348. /**
  1349. * 删除变更令
  1350. * @param {Object} ctx - egg全局变量
  1351. * @return {void}
  1352. */
  1353. async delete(ctx) {
  1354. try {
  1355. const result = await ctx.service.change.delete(ctx.request.body.cid);
  1356. if (!result) {
  1357. throw '删除变更令失败';
  1358. }
  1359. ctx.redirect(ctx.request.header.referer);
  1360. } catch (err) {
  1361. console.log(err);
  1362. ctx.redirect(ctx.request.header.referer);
  1363. }
  1364. }
  1365. /**
  1366. * 变更令重新审批
  1367. * @param {Object} ctx - egg全局变量
  1368. * @return {void}
  1369. */
  1370. async checkAgain(ctx) {
  1371. try {
  1372. const readySettle = await ctx.service.settle.getReadySettle(ctx.change.tid);
  1373. if (readySettle && readySettle.settle_order !== ctx.tender.data.settle_order) {
  1374. throw '结算数据发生变化,请刷新页面再提交';
  1375. }
  1376. if (ctx.session.sessionUser.loginStatus === 0) {
  1377. const code = ctx.request.body.code;
  1378. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  1379. if (!pa.auth_mobile) {
  1380. throw '未绑定手机号';
  1381. }
  1382. const cacheKey = 'smsCode:' + ctx.session.sessionUser.accountId;
  1383. const cacheCode = await app.redis.get(cacheKey);
  1384. // console.log(cacheCode);
  1385. if (cacheCode === null || code === undefined || cacheCode !== (code + pa.auth_mobile)) {
  1386. throw '验证码不正确!';
  1387. }
  1388. }
  1389. // 重新审批
  1390. if ((ctx.change.finalAuditorIds.indexOf(ctx.session.sessionUser.accountId) >= 0) && ctx.change.status === audit.change.status.checked) {
  1391. const result = await ctx.service.change.checkAgain(ctx.change);
  1392. if (!result) {
  1393. throw '重新审批失败';
  1394. }
  1395. // ctx.redirect('/tender/' + changeData.tid + '/change/' + changeData.cid + '/info');
  1396. ctx.body = {
  1397. err: 0,
  1398. url: ctx.request.header.referer,
  1399. msg: '',
  1400. };
  1401. } else {
  1402. throw '您无权进行该操作';
  1403. }
  1404. } catch (err) {
  1405. console.log(err);
  1406. // ctx.redirect(ctx.request.header.referer);
  1407. ctx.body = {
  1408. err: 1,
  1409. // url: ctx.request.header.referer,
  1410. msg: err,
  1411. };
  1412. }
  1413. }
  1414. /**
  1415. * 变更令修订重新上报
  1416. * @param {Object} ctx - egg全局变量
  1417. * @return {void}
  1418. */
  1419. async checkRevise(ctx) {
  1420. try {
  1421. if (ctx.change.status !== audit.change.status.checked || ctx.session.sessionUser.accountId !== ctx.change.uid) {
  1422. throw '您无权进行该操作';
  1423. }
  1424. const readySettle = await ctx.service.settle.getReadySettle(ctx.change.tid);
  1425. if (readySettle && readySettle.settle_order !== ctx.tender.data.settle_order) {
  1426. throw '结算数据发生变化,请刷新页面再提交';
  1427. }
  1428. if (ctx.session.sessionUser.loginStatus === 0) {
  1429. const code = ctx.request.body.code;
  1430. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  1431. if (!pa.auth_mobile) {
  1432. throw '未绑定手机号';
  1433. }
  1434. const cacheKey = 'smsCode:' + ctx.session.sessionUser.accountId;
  1435. const cacheCode = await app.redis.get(cacheKey);
  1436. // console.log(cacheCode);
  1437. if (cacheCode === null || code === undefined || cacheCode !== (code + pa.auth_mobile)) {
  1438. throw '验证码不正确!';
  1439. }
  1440. }
  1441. // 获取是否已存在调用变更令
  1442. // const changeUsedData = await ctx.service.stageChange.getAllFinalUsedData(ctx.tender.id, changeData.cid);
  1443. // const stageChangeNum = this.ctx.helper.sum(changeUsedData.map(x => { return Math.abs(x.qty); }));
  1444. // if (stageChangeNum !== 0) {
  1445. // throw '该变更令已被调用,无法重新审批';
  1446. // }
  1447. // 重新审批
  1448. const result = await ctx.service.change.checkRevise(ctx.change);
  1449. if (!result) {
  1450. throw '修订发起失败';
  1451. }
  1452. // ctx.redirect('/tender/' + changeData.tid + '/change/' + changeData.cid + '/info');
  1453. ctx.body = {
  1454. err: 0,
  1455. url: ctx.request.header.referer,
  1456. msg: '',
  1457. };
  1458. } catch (err) {
  1459. console.log(err);
  1460. // ctx.redirect(ctx.request.header.referer);
  1461. ctx.body = {
  1462. err: 1,
  1463. // url: ctx.request.header.referer,
  1464. msg: err,
  1465. };
  1466. }
  1467. }
  1468. /**
  1469. * 变更令撤销修订
  1470. * @param {Object} ctx - egg全局变量
  1471. * @return {void}
  1472. */
  1473. async cancelRevise(ctx) {
  1474. try {
  1475. if (!(ctx.change.status === audit.change.status.revise && (ctx.session.sessionUser.accountId === ctx.change.uid || ctx.session.sessionUser.accountId === ctx.session.sessionUser.is_admin))) {
  1476. throw '您无权进行该操作';
  1477. }
  1478. const readySettle = await ctx.service.settle.getReadySettle(ctx.change.tid);
  1479. if (readySettle && readySettle.settle_order !== ctx.tender.data.settle_order) {
  1480. throw '结算数据发生变化,请刷新页面再提交';
  1481. }
  1482. // 重新审批
  1483. const result = await ctx.service.change.cancelRevise(ctx.change.cid, ctx.change.times);
  1484. if (!result) {
  1485. throw '撤销修订失败';
  1486. }
  1487. ctx.body = {
  1488. err: 0,
  1489. url: ctx.request.header.referer,
  1490. msg: '',
  1491. };
  1492. } catch (err) {
  1493. console.log(err);
  1494. ctx.body = {
  1495. err: 1,
  1496. msg: err,
  1497. };
  1498. }
  1499. }
  1500. async saveAudit(ctx) {
  1501. try {
  1502. // if (ctx.change.revising) {
  1503. // throw '台账修订中,请勿修改提交期数据';
  1504. // }
  1505. const data = JSON.parse(ctx.request.body.data);
  1506. if (ctx.session.sessionUser.is_admin && ctx.change.status !== audit.change.status.checked) {
  1507. await ctx.service.changeAudit.saveAudit(ctx.change.cid, ctx.change.times, ctx.change.sp_group, data);
  1508. const auditors = await ctx.service.changeAudit.getUniqUserGroup(ctx.change.cid, ctx.change.times);
  1509. ctx.body = { err: 0, msg: '', data: auditors };
  1510. } else {
  1511. throw '您无权进行该操作';
  1512. }
  1513. } catch (err) {
  1514. this.log(err);
  1515. // ctx.session.postError = err.toString();
  1516. // ctx.redirect(ctx.request.header.referer);
  1517. ctx.body = {
  1518. err: 1,
  1519. // url: ctx.request.header.referer,
  1520. msg: err,
  1521. };
  1522. }
  1523. }
  1524. /**
  1525. * 获取变更清单
  1526. * @param ctx
  1527. * @return {Promise<void>}
  1528. */
  1529. async bills(ctx) {
  1530. try {
  1531. const data = JSON.parse(ctx.request.body.data);
  1532. const responseData = { err: 0, msg: '', data: [] };
  1533. switch (data.type) {
  1534. case 'gather':
  1535. responseData.data = await ctx.service.changeAuditList.gatherBgBills(ctx.tender.id);
  1536. responseData.data.sort((a, b) => { return ctx.helper.compareCode(a.code, b.code) });
  1537. break;
  1538. default:
  1539. throw '查询的数据不存在';
  1540. }
  1541. ctx.body = responseData;
  1542. } catch (err) {
  1543. this.log(err);
  1544. this.ajaxErrorBody(err, '获取变更清单失败');
  1545. }
  1546. }
  1547. /**
  1548. * 最后审批人审批成功检查批复编号和其它变更令是否出现重名情况
  1549. * @param ctx
  1550. * @return {Promise<void>}
  1551. */
  1552. async checkCodeRepeat(ctx) {
  1553. const responseData = {
  1554. err: 0,
  1555. msg: '',
  1556. data: '',
  1557. };
  1558. try {
  1559. const tenderId = ctx.params.id;
  1560. const cid = ctx.params.cid;
  1561. const data = JSON.parse(ctx.request.body.data);
  1562. const result = await ctx.service.change.isRepeat(cid, data.p_code, tenderId);
  1563. if (result) {
  1564. throw '该变更令号(批复编号)已使用';
  1565. }
  1566. } catch (err) {
  1567. responseData.err = 1;
  1568. responseData.msg = err;
  1569. }
  1570. ctx.body = responseData;
  1571. }
  1572. /**
  1573. * 拷贝其他变更令
  1574. * @param {object} ctx - 全局上下文
  1575. */
  1576. async copyChange(ctx) {
  1577. const responseData = {
  1578. err: 0,
  1579. msg: '',
  1580. data: '',
  1581. };
  1582. try {
  1583. const cid = ctx.params.cid;
  1584. const copy_cid = JSON.parse(ctx.request.body.data);
  1585. const result = await ctx.service.change.handleCopyChange(cid, copy_cid);
  1586. if (!result) {
  1587. responseData.err = 1;
  1588. responseData.msg = '拷贝失败!';
  1589. }
  1590. } catch (error) {
  1591. responseData.err = 1;
  1592. responseData.msg = error;
  1593. }
  1594. ctx.body = responseData;
  1595. }
  1596. /**
  1597. * 修订 详细页面(添加台账清单-台账模式)(Get)
  1598. * @param ctx
  1599. * @return {Promise<void>}
  1600. */
  1601. async reviseInfo(ctx) {
  1602. try {
  1603. // if (!ctx.session.sessionProject.page_show.openChangeRevise) {
  1604. // throw '该功能已关闭';
  1605. // }
  1606. const change = ctx.change;
  1607. let edit = true;
  1608. let changing = false;
  1609. if (change.status !== audit.change.status.uncheck && change.status !== audit.change.status.checkNo && change.status !== audit.change.status.revise) {
  1610. // throw '本条变更审批中或已完成,无法操作台账数据';
  1611. edit = false;
  1612. changing = true;
  1613. }
  1614. let revising = false;
  1615. // 判断是否在修订中,是则无法操作本页
  1616. const lastRevise = await ctx.service.ledgerRevise.getLastestRevise(ctx.tender.id);
  1617. if (lastRevise && lastRevise.status !== audit.revise.status.checked) {
  1618. // throw '台账修订中,无法操作台账数据';
  1619. edit = false;
  1620. revising = true;
  1621. }
  1622. change.order_site_msg = change.order_site ? await ctx.service.changeAuditList.getDataById(change.order_site) : null;
  1623. const renderData = await this._getDefaultReviseInfoData(ctx, change, !edit);
  1624. // 获取原报dsk数据
  1625. const accountInfo = await ctx.service.projectAccount.getDataById(change.uid);
  1626. renderData.changeList = await ctx.service.changeAuditList.getList(change.cid);
  1627. let changeUsedData = await ctx.service.stageChange.getAllFinalUsedData(ctx.tender.id, change.cid);
  1628. changeUsedData = ctx.helper._.orderBy(ctx.helper._.filter(changeUsedData, function(item) {
  1629. return item.qty !== null;
  1630. }), ['sorder'], ['desc']);
  1631. const useChangeUsedData = [];
  1632. if (changeUsedData.length > 0) { // 防止未创建期时调用
  1633. for (const cu of changeUsedData) {
  1634. const index = ctx.helper._.findIndex(useChangeUsedData, { cbid: cu.cbid });
  1635. if (index !== -1) {
  1636. useChangeUsedData[index].qty = ctx.helper.add(useChangeUsedData[index].qty, cu.qty);
  1637. } else {
  1638. useChangeUsedData.push(cu);
  1639. }
  1640. }
  1641. }
  1642. renderData.changeUsedData = useChangeUsedData;
  1643. const readySettle = await ctx.service.settle.getReadySettle(ctx.tender.id);
  1644. renderData.settleBills = readySettle ? await this.ctx.service.settleBills.getAllDataByCondition({ where: { settle_id: readySettle.id } }) : [];
  1645. renderData.settlePos = readySettle ? await this.ctx.service.settlePos.getAllDataByCondition({ where: { settle_id: readySettle.id } }) : [];
  1646. renderData.dskAccountData = accountInfo && accountInfo.dsk_account ? JSON.parse(accountInfo.dsk_account) : {};
  1647. renderData.dskProjects = accountInfo && accountInfo.dsk_account && accountInfo.dsk_projects ? JSON.parse(accountInfo.dsk_projects) : [];
  1648. // 台账只读、使用数据
  1649. renderData.readOnly = !edit;
  1650. renderData.changing = changing;
  1651. renderData.revising = revising;
  1652. renderData.isYb = change.uid === ctx.session.sessionUser.accountId;
  1653. await this.layout('change/revise.ejs', renderData, 'change/revise_modal.ejs');
  1654. } catch (err) {
  1655. this.log(err);
  1656. ctx.redirect(ctx.request.header.referer);
  1657. }
  1658. }
  1659. /**
  1660. * 修订 详细页面(添加台账清单-清单模式)(Get)
  1661. * @param ctx
  1662. * @return {Promise<void>}
  1663. */
  1664. async addListInfo(ctx) {
  1665. try {
  1666. const change = ctx.change;
  1667. let edit = true;
  1668. let changing = false;
  1669. if (change.status !== audit.change.status.uncheck && change.status !== audit.change.status.checkNo && change.status !== audit.change.status.revise) {
  1670. // throw '本条变更审批中或已完成,无法操作台账数据';
  1671. edit = false;
  1672. changing = true;
  1673. }
  1674. change.order_site_msg = change.order_site ? await ctx.service.changeAuditList.getDataById(change.order_site) : null;
  1675. const renderData = {
  1676. change,
  1677. tpUnit: change.tp_decimal ? change.tp_decimal : ctx.tender.info.decimal.tp,
  1678. upUnit: change.up_decimal ? change.up_decimal : ctx.tender.info.decimal.up,
  1679. precision: ctx.tender.info.precision,
  1680. tender: ctx.tender.data,
  1681. preUrl: '/tender/' + ctx.tender.id + '/change/' + ctx.change.cid + '/information',
  1682. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.addList),
  1683. auditConst: audit.change,
  1684. settleStatus: ctx.service.settle.settleStatus,
  1685. };
  1686. // 获取原报dsk数据
  1687. const accountInfo = await ctx.service.projectAccount.getDataById(change.uid);
  1688. renderData.changeList = await ctx.service.changeAuditList.getList(change.cid);
  1689. let changeUsedData = await ctx.service.stageChange.getAllFinalUsedData(ctx.tender.id, change.cid);
  1690. changeUsedData = ctx.helper._.orderBy(ctx.helper._.filter(changeUsedData, function(item) {
  1691. return item.qty !== null;
  1692. }), ['sorder'], ['desc']);
  1693. const useChangeUsedData = [];
  1694. if (changeUsedData.length > 0) { // 防止未创建期时调用
  1695. for (const cu of changeUsedData) {
  1696. const index = ctx.helper._.findIndex(useChangeUsedData, { cbid: cu.cbid });
  1697. if (index !== -1) {
  1698. useChangeUsedData[index].qty = ctx.helper.add(useChangeUsedData[index].qty, cu.qty);
  1699. } else {
  1700. useChangeUsedData.push(cu);
  1701. }
  1702. }
  1703. }
  1704. renderData.changeUsedData = useChangeUsedData;
  1705. const readySettle = await ctx.service.settle.getReadySettle(ctx.tender.id);
  1706. renderData.settleBills = readySettle ? await this.ctx.service.settleBills.getAllDataByCondition({ where: { settle_id: readySettle.id } }) : [];
  1707. renderData.settlePos = readySettle ? await this.ctx.service.settlePos.getAllDataByCondition({ where: { settle_id: readySettle.id } }) : [];
  1708. // 台账只读、使用数据
  1709. renderData.readOnly = !edit;
  1710. renderData.changing = changing;
  1711. renderData.isYb = change.uid === ctx.session.sessionUser.accountId;
  1712. await this.layout('change/addlist.ejs', renderData, 'change/addlist_modal.ejs');
  1713. } catch (err) {
  1714. this.log(err);
  1715. ctx.redirect(ctx.request.header.referer);
  1716. }
  1717. }
  1718. async _getDefaultReviseInfoData(ctx, change, edit) {
  1719. const [ledgerSpread, posSpread] = await spreadSetting.getLedgerSpreadSetting(ctx, change.tid, edit);
  1720. const [stdBills, stdChapters] = await this.ctx.service.valuation.getValuationStdList(
  1721. ctx.tender.data.valuation, ctx.tender.data.measure_type);
  1722. return {
  1723. change,
  1724. tender: ctx.tender.data,
  1725. ledgerSpread, posSpread, tenderMenu, measureType,
  1726. preUrl: '/tender/' + ctx.tender.id + '/change/' + ctx.change.cid + '/information',
  1727. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.revise),
  1728. stdBills,
  1729. auditConst: audit.flow,
  1730. audit: audit.flow,
  1731. stdChapters,
  1732. nodeType: stdConst.nodeType,
  1733. settleStatus: ctx.service.settle.settleStatus,
  1734. };
  1735. }
  1736. async updateRevise(ctx) {
  1737. try {
  1738. if (!ctx.tender.data) throw '标段数据错误';
  1739. if (!ctx.session.sessionProject.page_show.openChangeRevise) {
  1740. throw '该功能已关闭';
  1741. }
  1742. const data = JSON.parse(ctx.request.body.data);
  1743. if (!data.postType || !data.postData) throw '数据错误';
  1744. const responseData = { err: 0, msg: '', data: {} };
  1745. const change = ctx.change;
  1746. if (change.status !== audit.change.status.uncheck && change.status !== audit.change.status.checkNo && change.status !== audit.change.status.revise) {
  1747. throw '该变更令正在审批中或已完成,无法操作新增部位数据';
  1748. }
  1749. // 判断是否在修订中,是则无法操作本页
  1750. const lastRevise = await ctx.service.ledgerRevise.getLastestRevise(ctx.tender.id);
  1751. if (lastRevise && lastRevise.status !== audit.revise.status.checked) {
  1752. throw '台账修订中,无法操作新增部位数据';
  1753. }
  1754. // const revise = await this.checkRevise(ctx);
  1755. //
  1756. switch (data.postType) {
  1757. case 'add':
  1758. case 'delete':
  1759. case 'up-move':
  1760. case 'down-move':
  1761. case 'up-level':
  1762. case 'down-level':
  1763. responseData.data = await this._billsBase(ctx, data.postType, data.postData);
  1764. break;
  1765. case 'update':
  1766. ctx.helper.checkDgnQtyPrecision(data.postData);
  1767. responseData.data = await ctx.service.changeLedger.updateCalc(ctx.tender.id, data.postData);
  1768. break;
  1769. case 'paste-block':
  1770. responseData.data = await this._pasteBlock(ctx, data.postData);
  1771. break;
  1772. case 'add-std':
  1773. responseData.data = await this._addStd(ctx, data.postData);
  1774. break;
  1775. case 'add-deal':
  1776. responseData.data = await this._addDeal(ctx, data.postData);
  1777. break;
  1778. // case 'batch-insert':
  1779. // responseData.data = await this._batchInsert(ctx, data.postData);
  1780. // break;
  1781. case 'pos':
  1782. responseData.data = await this._updatePos(ctx, data);
  1783. break;
  1784. default:
  1785. throw '未知操作';
  1786. }
  1787. responseData.data.changeList = await ctx.service.changeAuditList.getList(ctx.change.cid);
  1788. ctx.body = responseData;
  1789. } catch (err) {
  1790. console.log(err);
  1791. this.log(err);
  1792. ctx.body = this.ajaxErrorBody(err, '数据错误');
  1793. }
  1794. }
  1795. async _billsBase(ctx, type, data) {
  1796. if (isNaN(data.id) || data.id <= 0) throw '数据错误';
  1797. if (type !== 'add') {
  1798. if (isNaN(data.count) || data.count <= 0) data.count = 1;
  1799. }
  1800. switch (type) {
  1801. case 'add':
  1802. return await ctx.service.changeLedger.addNodeBatch(ctx.tender.id, data.id, { ccid: ctx.change.cid, check_calc: 1 }, data.count);
  1803. case 'delete':
  1804. return await ctx.service.changeLedger.delete(ctx.tender.id, data.id, data.count);
  1805. case 'up-move':
  1806. return await ctx.service.changeLedger.upMoveNode(ctx.tender.id, data.id, data.count);
  1807. case 'down-move':
  1808. return await ctx.service.changeLedger.downMoveNode(ctx.tender.id, data.id, data.count);
  1809. case 'up-level':
  1810. return await ctx.service.changeLedger.upLevelNode(ctx.tender.id, data.id, data.count);
  1811. case 'down-level':
  1812. return await ctx.service.changeLedger.downLevelNode(ctx.tender.id, data.id, data.count);
  1813. default:
  1814. throw '未知操作';
  1815. }
  1816. }
  1817. /**
  1818. * 复制粘贴整块
  1819. *
  1820. * @param ctx
  1821. * @return {Promise<void>}
  1822. */
  1823. async _pasteBlock(ctx, data) {
  1824. if ((isNaN(data.id) || data.id <= 0) ||
  1825. (!data.tid && data.tid <= 0) ||
  1826. (!data.block || data.block.length <= 0)) throw '参数错误';
  1827. return await ctx.service.changeLedger.pasteBlockData(ctx.tender.id, data.id, data.block);
  1828. }
  1829. /**
  1830. * 从标准项目表添加数据
  1831. * @param ctx
  1832. * @return {Promise<void>}
  1833. */
  1834. async _addStd(ctx, data) {
  1835. if ((isNaN(data.id) || data.id <= 0) || !data.stdType || !data.stdNode) throw '参数错误';
  1836. // todo 校验项目是否使用该库的权限
  1837. let stdLib,
  1838. addType;
  1839. switch (data.stdType) {
  1840. case 'xmj':
  1841. stdLib = ctx.service.stdXmj;
  1842. addType = stdDataAddType.withParent;
  1843. break;
  1844. case 'gcl':
  1845. stdLib = ctx.service.stdGcl;
  1846. const selectNode = await ctx.service.changeLedger.getDataByNodeId(ctx.tender.id, data.id) || await ctx.service.ledger.getDataByNodeId(ctx.tender.id, data.id);
  1847. if (selectNode.b_code) {
  1848. addType = stdDataAddType.next;
  1849. } else {
  1850. addType = stdDataAddType.child;
  1851. }
  1852. break;
  1853. default:
  1854. throw '未知标准库';
  1855. }
  1856. const stdData = await stdLib.getDataByDataId(data.stdLibId, data.stdNode);
  1857. switch (addType) {
  1858. case stdDataAddType.child:
  1859. return await ctx.service.changeLedger.addStdNodeAsChild(ctx.tender.id, data.id, stdData, ctx.change.cid);
  1860. case stdDataAddType.next:
  1861. return await ctx.service.changeLedger.addStdNode(ctx.tender.id, data.id, stdData, ctx.change.cid);
  1862. case stdDataAddType.withParent:
  1863. return await ctx.service.changeLedger.addStdNodeWithParent(ctx.tender.id, stdData, stdLib, ctx.change.cid);
  1864. default:
  1865. throw '未知添加方式';
  1866. }
  1867. }
  1868. /**
  1869. * 从签约清单添加节点
  1870. * @param ctx
  1871. * @return {Promise<void>}
  1872. */
  1873. async _addDeal(ctx, data) {
  1874. if (!data.type || !data.dealBills) throw '数据错误';
  1875. data.dealBills.unit_price = this.ctx.helper.round(data.dealBills.unit_price, ctx.tender.info.decimal.up);
  1876. if (data.dealBills.quantity && data.dealBills.unit) {
  1877. const precision = ctx.helper.findPrecision(ctx.tender.info.precision, data.dealBills.unit);
  1878. data.dealBills.quantity = ctx.helper.round(data.dealBills.quantity, precision.value);
  1879. data.dealBills.sgfh_qty = data.dealBills.quantity;
  1880. data.dealBills.total_price = ctx.helper.mul(data.dealBills.quantity, data.dealBills.unit_price, ctx.tender.info.decimal.tp);
  1881. data.dealBills.sgfh_tp = data.dealBills.total_price;
  1882. }
  1883. if (data.type === 'child') {
  1884. return await ctx.service.changeLedger.addChild(ctx.tender.id, data.id, data.dealBills, ctx.change.cid);
  1885. } else if (data.type === 'next') {
  1886. data.dealBills.ccid = ctx.change.cid;
  1887. return await ctx.service.changeLedger.addNode(ctx.tender.id, data.id, data.dealBills);
  1888. }
  1889. throw '数据错误';
  1890. }
  1891. /**
  1892. * 批量插入数据
  1893. *
  1894. * data = {id, batchData, batchType}
  1895. * data.batchType = 'batchInsertChild'/'batchInsertNext'
  1896. * data.batchData = [{name, children}] -- 项目节列表
  1897. * data.batchData.children = [{code, name, unit, unit_price, quantity}] -- 工程量清单列表
  1898. *
  1899. * @param ctx
  1900. * @return {Promise<void>}
  1901. */
  1902. async _batchInsert(ctx, data) {
  1903. if ((isNaN(data.id) || data.id <= 0) || !data.batchType) throw '参数错误';
  1904. switch (data.batchType) {
  1905. case 'child':
  1906. return await ctx.service.changeLedger.batchInsertChild(ctx.tender.id, data.id, data.batchData);
  1907. case 'next':
  1908. return await ctx.service.changeLedger.batchInsertNext(ctx.tender.id, data.id, data.batchData);
  1909. default:
  1910. throw '参数错误';
  1911. }
  1912. }
  1913. async _updatePos(ctx, data) {
  1914. await this.checkMeasureType(measureType.tz.value);
  1915. if (!data.posPostType) throw '数据错误';
  1916. switch (data.posPostType) {
  1917. case 'add':
  1918. return await this.ctx.service.changePos.addPos(ctx.tender.id, ctx.change.cid, data.postData);
  1919. case 'update':
  1920. if (data.postData instanceof Array) {
  1921. return await this.ctx.service.changePos.updatePosArr(ctx.tender.id, data.postData);
  1922. }
  1923. return await this.ctx.service.changePos.updatePos(ctx.tender.id, data.postData);
  1924. case 'delete':
  1925. return await this.ctx.service.changePos.deletePos(ctx.tender.id, data.postData);
  1926. case 'paste':
  1927. return await this.ctx.service.changePos.pastePosData(ctx.tender.id, ctx.change.cid, data.postData);
  1928. default:
  1929. throw '未知操作';
  1930. }
  1931. }
  1932. async _filterChangesProject(ctx, status = 0) {
  1933. const tenderId = ctx.params.id;
  1934. ctx.session.sessionUser.tenderId = tenderId;
  1935. const tender = await this.service.tender.getDataById(ctx.tender.id);
  1936. // const tender = ctx.tender;
  1937. // const tenderList = await this.service.tender.getList();
  1938. const page = ctx.page;
  1939. const pageSize = ctx.pageSize;
  1940. const sorts = ctx.query.sort ? ctx.query.sort : 0;
  1941. const orders = ctx.query.order ? ctx.query.order : 0;
  1942. const changes = await ctx.service.changeProject.getListByStatus(tender.id, status, 1, sorts, orders);
  1943. const total = await ctx.service.changeProject.getCountByStatus(tender.id, status);
  1944. for (const c of changes) {
  1945. if (c.status !== audit.change.status.checked || !c.final_auditor_str) {
  1946. c.curAuditors = await ctx.service.changeProjectAudit.getAuditorsByStatus(c.id, c.status, c.times);
  1947. if (c.status === audit.change.status.checked && c.curAuditors.length > 0) {
  1948. const final_auditor_str = c.curAuditors[0].audit_type === auditType.key.common
  1949. ? c.curAuditors[0].name + (c.curAuditors[0].role ? '-' + c.curAuditors[0].role : '')
  1950. : ctx.helper.transFormToChinese(c.curAuditors[0].audit_order) + '审';
  1951. await ctx.service.changeProject.defaultUpdate({ final_auditor_str }, { where: { id: c.id } });
  1952. }
  1953. }
  1954. }
  1955. const accountInfo = await this.ctx.service.projectAccount.getDataById(this.ctx.session.sessionUser.accountId);
  1956. const userPermission = accountInfo !== undefined && accountInfo.permission !== ''
  1957. ? JSON.parse(accountInfo.permission) : null;
  1958. // 分页相关
  1959. const pageInfo = {
  1960. page,
  1961. pageSizeSelect: 1,
  1962. pageSize,
  1963. total_num: total,
  1964. total: Math.ceil(total / pageSize),
  1965. queryData: JSON.stringify(ctx.urlInfo.query),
  1966. };
  1967. const filter = JSON.parse(JSON.stringify(audit.changeProject.filter));
  1968. filter.count = [];
  1969. 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);
  1970. 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);
  1971. 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);
  1972. 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);
  1973. 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);
  1974. let codeRule = [];
  1975. let c_connector = '1';
  1976. let c_rule_first = 1;
  1977. const rule_type = tender.user_id === ctx.session.sessionUser.accountId ? 'suggestion' : 'will';
  1978. if (tender.c_code_rules) {
  1979. const c_code_rules = JSON.parse(tender.c_code_rules);
  1980. codeRule = c_code_rules[rule_type + '_rule'] !== undefined ? c_code_rules[rule_type + '_rule'] : [];
  1981. c_connector = c_code_rules[rule_type + '_connector'] !== undefined ? c_code_rules[rule_type + '_connector'] : '1';
  1982. c_rule_first = c_code_rules[rule_type + '_rule_first'] !== undefined ? c_code_rules[rule_type + '_rule_first'] : 1;
  1983. }
  1984. for (const rule of codeRule) {
  1985. switch (rule.rule_type) {
  1986. case codeRuleConst.measure.ruleType.dealCode:
  1987. rule.preview = ctx.tender.info.deal_info.dealCode;
  1988. break;
  1989. case codeRuleConst.measure.ruleType.tenderName:
  1990. rule.preview = tender.name;
  1991. break;
  1992. case codeRuleConst.measure.ruleType.inDate:
  1993. rule.preview = moment().format('YYYY');
  1994. break;
  1995. case codeRuleConst.measure.ruleType.text:
  1996. rule.preview = rule.text;
  1997. break;
  1998. case codeRuleConst.measure.ruleType.addNo:
  1999. const s = '0000000000';
  2000. rule.preview = s.substr(s.length - rule.format);
  2001. break;
  2002. default: break;
  2003. }
  2004. }
  2005. const renderData = {
  2006. uid: ctx.session.sessionUser.accountId,
  2007. userPermission,
  2008. tender,
  2009. pageInfo,
  2010. changes,
  2011. status,
  2012. rule_type,
  2013. codeRule,
  2014. c_connector,
  2015. c_rule_first,
  2016. filter,
  2017. ruleType: codeRuleConst.ruleType[rule_type],
  2018. dealCode: ctx.tender.info.deal_info.dealCode,
  2019. auditConst: audit.changeProject,
  2020. ruleConst: codeRuleConst.measure,
  2021. changeConst,
  2022. auditType,
  2023. tenderMenu: this.menu.tenderMenu,
  2024. preUrl: '/tender/' + tender.id,
  2025. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.project),
  2026. };
  2027. await this.layout('change/project.ejs', renderData, 'change/project_modal.ejs');
  2028. }
  2029. /**
  2030. * 变更立项列表 页面 (Get)
  2031. *
  2032. * @param {Object} ctx - egg全局变量
  2033. * @return {void}
  2034. */
  2035. async project(ctx) {
  2036. try {
  2037. await this._filterChangesProject(ctx);
  2038. } catch (err) {
  2039. this.log(err);
  2040. ctx.redirect('/dashboard');
  2041. }
  2042. }
  2043. /**
  2044. * 审批流程(Get)
  2045. * @param ctx
  2046. * @return {Promise<void>}
  2047. */
  2048. async changeProjectAuditors(ctx) {
  2049. try {
  2050. const id = JSON.parse(ctx.request.body.data).id;
  2051. const tenderId = ctx.params.id;
  2052. const changeInfo = await ctx.service.changeProject.getDataById(id);
  2053. await ctx.service.changeProject.loadChangeUser(changeInfo);
  2054. await ctx.service.changeProject.loadChangeAuditViewData(changeInfo);
  2055. ctx.body = { err: 0, msg: '', data: changeInfo };
  2056. } catch (error) {
  2057. this.log(error);
  2058. ctx.body = { err: 1, msg: error.toString(), data: null };
  2059. }
  2060. }
  2061. /**
  2062. * 新增变更立项 (Post)
  2063. *
  2064. * @param {Object} ctx - egg全局变量
  2065. * @return {void}
  2066. */
  2067. async projectAdd(ctx) {
  2068. try {
  2069. const tenderId = ctx.params.id;
  2070. if (!tenderId) {
  2071. throw '当前未打开标段';
  2072. }
  2073. const data = JSON.parse(ctx.request.body.data);
  2074. if (!data.code || data.code === '' || !data.name || data.name === '') {
  2075. throw '变更立项书编号不能为空';
  2076. }
  2077. // 在生成新变更立项后,需要copy前一个变更立项报表的签名信息
  2078. const lastChange = await ctx.service.changeProject.getLastChange(tenderId);
  2079. const change = await ctx.service.changeProject.add(tenderId, ctx.session.sessionUser.accountId, data.code, data.name);
  2080. await ctx.service.roleRptRel.createRoleRelationshipFromOtherBz(tenderId, '-302', change.id, lastChange ? lastChange.id : null);
  2081. ctx.body = { err: 0, msg: '', data: change };
  2082. } catch (err) {
  2083. this.log(err);
  2084. ctx.body = { err: 1, msg: err.toString() };
  2085. }
  2086. }
  2087. /**
  2088. * 删除变更立项
  2089. * @param {Object} ctx - egg全局变量
  2090. * @return {void}
  2091. */
  2092. async projectDelete(ctx) {
  2093. try {
  2094. const result = await ctx.service.changeProject.delete(ctx.request.body.cpid);
  2095. if (!result) {
  2096. throw '删除变更立项失败';
  2097. }
  2098. ctx.redirect(ctx.request.header.referer);
  2099. } catch (err) {
  2100. console.log(err);
  2101. ctx.redirect(ctx.request.header.referer);
  2102. }
  2103. }
  2104. /**
  2105. * 变更管理 状态筛选 页面 (Get)
  2106. * @param {Object} ctx - egg全局变量
  2107. * @return {void}
  2108. */
  2109. async projectStatus(ctx) {
  2110. try {
  2111. const status = parseInt(ctx.params.status);
  2112. await this._filterChangesProject(ctx, status);
  2113. } catch (err) {
  2114. this.logger.error(err);
  2115. ctx.redirect('/tender/' + ctx.params.id + '/change/project');
  2116. }
  2117. }
  2118. /**
  2119. * 获取审批界面所需的 原报、审批人数据等
  2120. * @param ctx
  2121. * @return {Promise<void>}
  2122. * @private
  2123. */
  2124. async _getChangeProjectAuditViewData(ctx) {
  2125. await ctx.service.changeProject.loadChangeAuditViewData(ctx.change);
  2126. }
  2127. // 变更类别获取及更新
  2128. async _getOrUpdateClass(ctx, serviceName) {
  2129. // 工程变更类别读取
  2130. const projectData = await ctx.service.project.getDataById(ctx.session.sessionProject.id);
  2131. const fun_set = await ctx.service.project.getFunSet(projectData.fun_set);
  2132. if (ctx.change.status === audit[serviceName].status.uncheck ||
  2133. (serviceName === 'changeProject' && ctx.change.status === audit[serviceName].status.back) ||
  2134. (serviceName !== 'changeProject' && ctx.change.status === audit[serviceName].status.checkNo)) {
  2135. const classInfo = ctx.helper._.find(fun_set.change_class, function(item) {
  2136. return item.checked && (item.name === ctx.change.class || item.new_name === ctx.change.class);
  2137. });
  2138. if (!classInfo) {
  2139. const classInfo2 = ctx.helper._.find(fun_set.change_class, { checked: true });
  2140. ctx.change.class = classInfo2.new_name ? classInfo2.new_name : classInfo2.name;
  2141. await ctx.service[serviceName].defaultUpdate({
  2142. id: ctx.change.id,
  2143. class: ctx.change.class,
  2144. });
  2145. } else if (classInfo && classInfo.new_name && ctx.change.class !== classInfo.new_name) {
  2146. ctx.change.class = classInfo.new_name;
  2147. await ctx.service[serviceName].defaultUpdate({
  2148. id: ctx.change.id,
  2149. class: ctx.change.class,
  2150. });
  2151. }
  2152. }
  2153. return fun_set.change_class;
  2154. }
  2155. async projectInformation(ctx) {
  2156. try {
  2157. const whiteList = this.ctx.app.config.multipart.whitelist;
  2158. // const tender = await ctx.service.tender.getDataById(ctx.tender.id);
  2159. // 获取附件列表
  2160. const fileList = await ctx.service.changeProjectAtt.getAllChangeProjectAtt(ctx.tender.id, ctx.change.id);
  2161. await this._getChangeProjectAuditViewData(ctx);
  2162. const changeClass = await this._getOrUpdateClass(ctx, 'changeProject');
  2163. // 获取用户人验证手机号
  2164. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  2165. const auth_mobile = pa.auth_mobile;
  2166. const userPermission = pa && pa.permission ? JSON.parse(pa.permission) : null;
  2167. const deleteFilePermission = !!(userPermission && userPermission.change !== undefined && userPermission.change.indexOf('2') !== -1);
  2168. // 判断并更新
  2169. const renderData = {
  2170. tender: ctx.tender,
  2171. change: ctx.change,
  2172. changeConst,
  2173. changeClass,
  2174. auditConst: audit.changeProject,
  2175. fileList,
  2176. whiteList,
  2177. authMobile: auth_mobile,
  2178. deleteFilePermission,
  2179. auditType,
  2180. shenpiConst,
  2181. returnUrl: this.app._.includes(ctx.request.headers.referer, '/tender/' + ctx.tender.id + '/change/project') ? ctx.request.headers.referer : null,
  2182. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.project_information),
  2183. preUrl: '/tender/' + ctx.tender.id + '/change/project/' + ctx.change.id + '/information',
  2184. };
  2185. // 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)) {
  2186. // data.accountGroup = accountGroup;
  2187. // 获取所有项目参与者
  2188. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  2189. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  2190. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  2191. });
  2192. renderData.accountList = accountList;
  2193. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  2194. renderData.accountGroup = unitList.map(item => {
  2195. const groupList = accountList.filter(item1 => item1.company === item.name);
  2196. return { groupName: item.name, groupList };
  2197. });
  2198. // 获取固定审批流列表
  2199. if (ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdspl) {
  2200. renderData.spGroupList = await ctx.service.shenpiGroup.getGroupListByChangeType(ctx.tender.id, shenpiConst.sp_type.change, 'changeProject');
  2201. }
  2202. // }
  2203. await this.layout('change/project_information.ejs', renderData, 'change/project_information_modal.ejs');
  2204. } catch (err) {
  2205. this.log(err);
  2206. ctx.redirect('/tender/' + ctx.params.id + '/change/project');
  2207. }
  2208. }
  2209. async changeProjectSpGroup(ctx) {
  2210. try {
  2211. const data = JSON.parse(ctx.request.body.data);
  2212. const result = await ctx.service.changeProjectAudit.changeSpGroup(ctx.change, data.sp_group);
  2213. if (!result) {
  2214. throw '切换审批组失败';
  2215. }
  2216. const auditors = await ctx.service.changeProjectAudit.getUserGroup(ctx.change.id, ctx.change.times);
  2217. ctx.body = { err: 0, msg: '', data: auditors };
  2218. } catch (err) {
  2219. ctx.body = { err: 1, msg: err.toString(), data: null };
  2220. }
  2221. }
  2222. // 审批相关
  2223. /**
  2224. * 添加审批人
  2225. * @param ctx
  2226. * @return {Promise<void>}
  2227. */
  2228. async addProjectAudit(ctx) {
  2229. try {
  2230. const auditConst = audit.changeProject;
  2231. const data = JSON.parse(ctx.request.body.data);
  2232. const id = this.app._.toInteger(data.auditorId);
  2233. if (isNaN(id) || id <= 0) {
  2234. throw '参数错误';
  2235. }
  2236. // 检查权限等
  2237. if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
  2238. throw '您无权添加审核人';
  2239. }
  2240. if (ctx.change.status === auditConst.status.checking || ctx.change.status === auditConst.status.checked) {
  2241. throw '当前不允许添加审核人';
  2242. }
  2243. // 判断是否是协审人,是则无法添加到审批人中
  2244. const xsAuditorList = await ctx.service.changeProjectXsAudit.getAuditList(ctx.change.id);
  2245. const xsAidList = this.app._.map(xsAuditorList, 'aid');
  2246. if (this.app._.indexOf(xsAidList, id) !== -1) {
  2247. throw '该用户已添加到协审人列表中,请删除该协审人再添加';
  2248. }
  2249. ctx.change.auditorList = await ctx.service.changeProjectAudit.getAuditors(ctx.change.id, ctx.change.times);
  2250. // 检查审核人是否已存在
  2251. const exist = this.app._.find(ctx.change.auditorList, { aid: id });
  2252. if (exist) {
  2253. throw '该审核人已存在,请勿重复添加';
  2254. }
  2255. const shenpiInfo = await ctx.service.shenpiAudit.getDataByCondition({ tid: ctx.tender.id, sp_type: shenpiConst.sp_type.change, sp_status: shenpiConst.sp_status.gdzs });
  2256. const is_gdzs = shenpiInfo && ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdzs ? 1 : 0;
  2257. const result = await ctx.service.changeProjectAudit.addAuditor(ctx.change.id, id, ctx.change.times, is_gdzs);
  2258. if (!result) {
  2259. throw '添加审核人失败';
  2260. }
  2261. const auditors = await ctx.service.changeProjectAudit.getUserGroup(ctx.change.id, ctx.change.times);
  2262. ctx.body = { err: 0, msg: '', data: auditors };
  2263. } catch (err) {
  2264. this.log(err);
  2265. ctx.body = { err: 1, msg: err.toString(), data: null };
  2266. }
  2267. }
  2268. /**
  2269. * 移除审批人
  2270. * @param ctx
  2271. * @return {Promise<void>}
  2272. */
  2273. async deleteProjectAudit(ctx) {
  2274. try {
  2275. const data = JSON.parse(ctx.request.body.data);
  2276. const id = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
  2277. if (isNaN(id) || id <= 0) {
  2278. throw '参数错误';
  2279. }
  2280. const result = await ctx.service.changeProjectAudit.deleteAuditor(ctx.change.id, id, ctx.change.times);
  2281. if (!result) {
  2282. throw '移除审核人失败';
  2283. }
  2284. const auditors = await ctx.service.changeProjectAudit.getAuditors(ctx.change.id, ctx.change.times);
  2285. ctx.body = { err: 0, msg: '', data: auditors };
  2286. } catch (err) {
  2287. ctx.body = { err: 1, msg: err.toString(), data: null };
  2288. }
  2289. }
  2290. /**
  2291. * 添加协审人
  2292. * @param ctx
  2293. * @return {Promise<void>}
  2294. */
  2295. async addProjectXsAudit(ctx) {
  2296. try {
  2297. const auditConst = audit.changeProject;
  2298. const data = JSON.parse(ctx.request.body.data);
  2299. const id = this.app._.toInteger(data.auditorId);
  2300. if (isNaN(id) || id <= 0) {
  2301. throw '参数错误';
  2302. }
  2303. const times = ctx.change.status === auditConst.status.back ? ctx.change.times - 1 : ctx.change.times;
  2304. const spAuditors = await ctx.service.changeProjectAudit.getAuditorsWithOwner(ctx.change.id, times);
  2305. const aidLists = this.app._.map(spAuditors, 'aid');
  2306. // 检查权限等
  2307. if (this.app._.indexOf(aidLists, ctx.session.sessionUser.accountId) === -1) {
  2308. throw '您无权添加协审人';
  2309. }
  2310. // 如果已经是审批人则无法添加此人为协审
  2311. if (this.app._.indexOf(aidLists, id) !== -1) {
  2312. throw '该用户为审批人或原报,无需添加至协审人';
  2313. }
  2314. const auditorList = await ctx.service.changeProjectXsAudit.getAuditList(ctx.change.id);
  2315. // 检查审核人是否已存在
  2316. const exist = this.app._.find(auditorList, { aid: id });
  2317. if (exist) {
  2318. throw '该协审人已存在,请勿重复添加';
  2319. }
  2320. const result = await ctx.service.changeProjectXsAudit.addAuditor(ctx.change.id, id);
  2321. if (!result) {
  2322. throw '添加协审人失败';
  2323. }
  2324. const auditors = await ctx.service.changeProjectXsAudit.getOneAudit(ctx.change.id, id);
  2325. ctx.body = { err: 0, msg: '', data: auditors };
  2326. } catch (err) {
  2327. this.log(err);
  2328. ctx.body = { err: 1, msg: err.toString(), data: null };
  2329. }
  2330. }
  2331. /**
  2332. * 移除协审人
  2333. * @param ctx
  2334. * @return {Promise<void>}
  2335. */
  2336. async deleteProjectXsAudit(ctx) {
  2337. try {
  2338. const data = JSON.parse(ctx.request.body.data);
  2339. const id = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
  2340. if (isNaN(id) || id <= 0) {
  2341. throw '参数错误';
  2342. }
  2343. const result = await ctx.service.changeProjectXsAudit.deleteAuditor(ctx.change.id, id);
  2344. if (!result) {
  2345. throw '移除协审人失败';
  2346. }
  2347. const auditors = await ctx.service.changeProjectXsAudit.getAuditList(ctx.change.id);
  2348. ctx.body = { err: 0, msg: '', data: auditors };
  2349. } catch (err) {
  2350. ctx.body = { err: 1, msg: err.toString(), data: null };
  2351. }
  2352. }
  2353. async saveProjectAudit(ctx) {
  2354. try {
  2355. // if (ctx.change.revising) {
  2356. // throw '台账修订中,请勿修改提交期数据';
  2357. // }
  2358. const data = JSON.parse(ctx.request.body.data);
  2359. if (ctx.session.sessionUser.is_admin && ctx.change.status !== audit.change.status.checked) {
  2360. await ctx.service.changeProjectAudit.saveAudit(ctx.change.id, ctx.change.times, ctx.change.sp_group, data);
  2361. const auditors = await ctx.service.changeProjectAudit.getUniqUserGroup(ctx.change.id, ctx.change.times);
  2362. ctx.body = { err: 0, msg: '', data: auditors };
  2363. } else {
  2364. throw '您无权进行该操作';
  2365. }
  2366. } catch (err) {
  2367. this.log(err);
  2368. // ctx.session.postError = err.toString();
  2369. // ctx.redirect(ctx.request.header.referer);
  2370. ctx.body = {
  2371. err: 1,
  2372. // url: ctx.request.header.referer,
  2373. msg: err,
  2374. };
  2375. }
  2376. }
  2377. /**
  2378. * 上传附件
  2379. * @param {*} ctx 上下文
  2380. */
  2381. async uploadProjectFile(ctx) {
  2382. let stream;
  2383. try {
  2384. const auditConst = audit.changeProject;
  2385. // this._checkAdvanceFileCanModify(ctx);
  2386. const parts = this.ctx.multipart({
  2387. autoFields: true,
  2388. });
  2389. const files = [];
  2390. const create_time = Date.parse(new Date()) / 1000;
  2391. let idx = 0;
  2392. const extra_upload = ctx.change.status === auditConst.status.checked;
  2393. while ((stream = await parts()) !== undefined) {
  2394. if (!stream.filename) {
  2395. // 如果没有传入直接返回
  2396. return;
  2397. }
  2398. const fileInfo = path.parse(stream.filename);
  2399. const filepath = `app/public/upload/${this.ctx.tender.id.toString()}/change_project/fujian_${create_time + idx.toString() + fileInfo.ext}`;
  2400. // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, 'app', filepath));
  2401. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  2402. files.push({ filepath, name: stream.filename, ext: fileInfo.ext });
  2403. ++idx;
  2404. stream && (await sendToWormhole(stream));
  2405. }
  2406. const in_time = new Date();
  2407. const payload = files.map(file => {
  2408. let idx;
  2409. if (Array.isArray(parts.field.name)) {
  2410. idx = parts.field.name.findIndex(name => name === file.name);
  2411. } else {
  2412. idx = 'isString';
  2413. }
  2414. const newFile = {
  2415. tid: ctx.tender.id,
  2416. cpid: ctx.change.id,
  2417. uid: ctx.session.sessionUser.accountId,
  2418. filename: file.name,
  2419. fileext: file.ext,
  2420. filesize: ctx.helper.bytesToSize(idx === 'isString' ? parts.field.size : parts.field.size[idx]),
  2421. filepath: file.filepath,
  2422. upload_time: in_time,
  2423. extra_upload,
  2424. };
  2425. return newFile;
  2426. });
  2427. // 执行文件信息写入数据库
  2428. await ctx.service.changeProjectAtt.saveFileMsgToDb(payload);
  2429. // 将最新的当前标段的所有文件信息返回
  2430. const data = await ctx.service.changeProjectAtt.getAllChangeProjectAtt(ctx.tender.id, ctx.change.id);
  2431. ctx.body = { err: 0, msg: '', data };
  2432. } catch (err) {
  2433. stream && (await sendToWormhole(stream));
  2434. this.log(err);
  2435. ctx.body = { err: 1, msg: err.toString(), data: null };
  2436. }
  2437. }
  2438. /**
  2439. * 删除附件
  2440. * @param {Ojbect} ctx 上下文
  2441. */
  2442. async deleteProjectFile(ctx) {
  2443. try {
  2444. const { id } = JSON.parse(ctx.request.body.data);
  2445. const fileInfo = await ctx.service.changeProjectAtt.getDataById(id);
  2446. if (fileInfo || Object.keys(fileInfo).length) {
  2447. // 先删除文件
  2448. // await fs.unlinkSync(path.resolve(this.app.baseDir, './app', fileInfo.filepath));
  2449. await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
  2450. // 再删除数据库
  2451. await ctx.service.changeProjectAtt.delete(id);
  2452. } else {
  2453. throw '不存在该文件';
  2454. }
  2455. const data = await ctx.service.changeProjectAtt.getAllChangeProjectAtt(ctx.tender.id, ctx.change.id);
  2456. ctx.body = { err: 0, msg: '请求成功', data };
  2457. } catch (err) {
  2458. this.log(err);
  2459. ctx.body = { err: 1, msg: err.toString(), data: null };
  2460. }
  2461. }
  2462. /**
  2463. * 下载附件
  2464. * @param {Object} ctx - egg全局变量
  2465. * @return {void}
  2466. */
  2467. async downloadProjectFile(ctx) {
  2468. const id = ctx.params.fid;
  2469. if (id) {
  2470. try {
  2471. const fileInfo = await ctx.service.changeProjectAtt.getDataById(id);
  2472. if (fileInfo !== undefined && fileInfo !== '') {
  2473. // const fileName = path.join(__dirname, '../', fileInfo.filepath);
  2474. // 解决中文无法下载问题
  2475. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  2476. let disposition = '';
  2477. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  2478. disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename);
  2479. } else if (userAgent.indexOf('firefox') >= 0) {
  2480. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename) + '"';
  2481. } else {
  2482. /* safari等其他非主流浏览器只能自求多福了 */
  2483. disposition = 'attachment; filename=' + new Buffer(fileInfo.filename).toString('binary');
  2484. }
  2485. ctx.response.set({
  2486. 'Content-Type': 'application/octet-stream',
  2487. 'Content-Disposition': disposition,
  2488. 'Content-Length': fileInfo.filesize,
  2489. });
  2490. // ctx.body = await fs.createReadStream(fileName);
  2491. ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
  2492. } else {
  2493. throw '不存在该文件';
  2494. }
  2495. } catch (err) {
  2496. this.log(err);
  2497. this.setMessage(err.toString(), this.messageType.ERROR);
  2498. }
  2499. }
  2500. }
  2501. async projectInformationSave(ctx) {
  2502. try {
  2503. const data = JSON.parse(ctx.request.body.data);
  2504. if (data.name === 'code') {
  2505. const info = await ctx.service.changeProject.isRepeat(ctx.change.id, data.val, ctx.tender.id, ctx.change.type);
  2506. if (info) {
  2507. throw '该编号已存在';
  2508. }
  2509. }
  2510. const result = await ctx.service.changeProject.saveInfo(ctx.change.id, data);
  2511. if (!result) {
  2512. throw '修改失败';
  2513. }
  2514. ctx.body = { err: 0, msg: '请求成功', data: null };
  2515. } catch (err) {
  2516. this.log(err);
  2517. ctx.body = { err: 1, msg: err.toString(), data: null };
  2518. }
  2519. }
  2520. /**
  2521. * 上报和重新上报
  2522. * @param ctx
  2523. * @return {Promise<void>}
  2524. */
  2525. async startProjectAudit(ctx) {
  2526. try {
  2527. const auditConst = audit.changeProject;
  2528. // 检查权限等
  2529. if (!ctx.change) {
  2530. throw '数据错误';
  2531. }
  2532. if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
  2533. throw '您无权上报该变更立项数据';
  2534. }
  2535. if (ctx.change.status === auditConst.status.checking || ctx.change.status === auditConst.status.checked) {
  2536. throw '该变更立项数据当前无法上报';
  2537. }
  2538. await ctx.service.changeProjectAudit.start(ctx.change.id, ctx.change.times);
  2539. ctx.redirect(ctx.request.header.referer);
  2540. } catch (err) {
  2541. this.log(err);
  2542. ctx.session.postError = err.toString();
  2543. ctx.redirect(ctx.request.header.referer);
  2544. }
  2545. }
  2546. /**
  2547. * 审批
  2548. * @param ctx
  2549. * @return {Promise<void>}
  2550. */
  2551. async checkProjectAudit(ctx) {
  2552. try {
  2553. const auditConst = audit.changeProject;
  2554. if (!ctx.change || ctx.change.status !== auditConst.status.checking) {
  2555. throw '当前变更立项数据有误';
  2556. }
  2557. if (ctx.change.curAuditorIds.length === 0 || ctx.change.curAuditorIds.indexOf(ctx.session.sessionUser.accountId) === -1) {
  2558. throw '您无权进行该操作';
  2559. }
  2560. const data = {
  2561. checkType: parseInt(ctx.request.body.checkType),
  2562. opinion: ctx.request.body.opinion,
  2563. };
  2564. if (!data.checkType || isNaN(data.checkType)) {
  2565. throw '提交数据错误';
  2566. }
  2567. // if (data.checkType === auditConst.status.checkNo) {
  2568. // if (!data.checkType || isNaN(data.checkType)) {
  2569. // throw '提交数据错误';
  2570. // }
  2571. // }
  2572. await ctx.service.changeProjectAudit.check(ctx.change.id, data, ctx.change.times);
  2573. ctx.redirect(ctx.request.header.referer);
  2574. } catch (err) {
  2575. this.log(err);
  2576. ctx.session.postError = err.toString();
  2577. ctx.redirect(ctx.request.header.referer);
  2578. }
  2579. }
  2580. /**
  2581. * 撤回审批
  2582. * @param ctx
  2583. * @return {Promise<void>}
  2584. */
  2585. async checkProjectAuditCancel(ctx) {
  2586. try {
  2587. if (!ctx.change.cancancel) throw '您无权进行该操作';
  2588. await ctx.service.changeProjectAudit.checkCancel(ctx.change);
  2589. ctx.body = { err: 0, url: ctx.request.header.referer, msg: '' };
  2590. } catch (err) {
  2591. this.log(err);
  2592. ctx.body = { err: 1, msg: err };
  2593. }
  2594. }
  2595. /**
  2596. * 变更立项重新审批
  2597. * @param {Object} ctx - egg全局变量
  2598. * @return {void}
  2599. */
  2600. async checkProjectAgain(ctx) {
  2601. try {
  2602. // 获取终审
  2603. // const auditInfo = await this.ctx.service.changeProjectAudit.getAuditorByStatus(ctx.change.id, audit.changeProject.status.checked);
  2604. // if (ctx.change.status !== audit.changeProject.status.checked || ctx.session.sessionUser.accountId !== auditInfo.aid) {
  2605. if (ctx.change.status !== audit.changeProject.status.checked) {
  2606. throw '您无权进行该操作';
  2607. }
  2608. // 判断是否被变更申请调用了,是则无法发起修订
  2609. const projectInfo = await ctx.service.changeApply.getDataByCondition({ tid: ctx.tender.id, project_code: ctx.change.code });
  2610. if (projectInfo) {
  2611. throw '该变更立项已被变更申请调用,无法发起重新审批';
  2612. }
  2613. if (ctx.session.sessionUser.loginStatus === 0) {
  2614. const code = ctx.request.body.code;
  2615. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  2616. if (!pa.auth_mobile) {
  2617. throw '未绑定手机号';
  2618. }
  2619. const cacheKey = 'smsCode:' + ctx.session.sessionUser.accountId;
  2620. const cacheCode = await app.redis.get(cacheKey);
  2621. // console.log(cacheCode);
  2622. if (cacheCode === null || code === undefined || cacheCode !== (code + pa.auth_mobile)) {
  2623. throw '验证码不正确!';
  2624. }
  2625. }
  2626. if ((ctx.change.finalAuditorIds.indexOf(ctx.session.sessionUser.accountId) >= 0) && ctx.change.status === audit.changeProject.status.checked) {
  2627. // 重新审批
  2628. const result = await ctx.service.changeProjectAudit.checkAgain(ctx.change);
  2629. if (!result) {
  2630. throw '重新审批失败';
  2631. }
  2632. } else {
  2633. throw '您无权进行该操作';
  2634. }
  2635. // ctx.redirect('/tender/' + changeData.tid + '/change/' + changeData.cid + '/info');
  2636. ctx.body = {
  2637. err: 0,
  2638. url: ctx.request.header.referer,
  2639. msg: '',
  2640. };
  2641. } catch (err) {
  2642. console.log(err);
  2643. // ctx.redirect(ctx.request.header.referer);
  2644. ctx.body = {
  2645. err: 1,
  2646. // url: ctx.request.header.referer,
  2647. msg: err,
  2648. };
  2649. }
  2650. }
  2651. /**
  2652. * 变更立项修订重新上报
  2653. * @param {Object} ctx - egg全局变量
  2654. * @return {void}
  2655. */
  2656. async checkProjectRevise(ctx) {
  2657. try {
  2658. if (ctx.change.status !== audit.changeProject.status.checked || ctx.session.sessionUser.accountId !== ctx.change.uid) {
  2659. throw '您无权进行该操作';
  2660. }
  2661. // 判断是否被变更申请调用了,是则无法发起修订
  2662. const projectInfo = await ctx.service.changeApply.getDataByCondition({ tid: ctx.tender.id, project_code: ctx.change.code });
  2663. if (projectInfo) {
  2664. throw '该变更立项已被变更申请调用,无法发起修订';
  2665. }
  2666. if (ctx.session.sessionUser.loginStatus === 0) {
  2667. const code = ctx.request.body.code;
  2668. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  2669. if (!pa.auth_mobile) {
  2670. throw '未绑定手机号';
  2671. }
  2672. const cacheKey = 'smsCode:' + ctx.session.sessionUser.accountId;
  2673. const cacheCode = await app.redis.get(cacheKey);
  2674. // console.log(cacheCode);
  2675. if (cacheCode === null || code === undefined || cacheCode !== (code + pa.auth_mobile)) {
  2676. throw '验证码不正确!';
  2677. }
  2678. }
  2679. // 重新审批
  2680. const result = await ctx.service.changeProjectAudit.checkRevise(ctx.change);
  2681. if (!result) {
  2682. throw '修订发起失败';
  2683. }
  2684. // ctx.redirect('/tender/' + changeData.tid + '/change/' + changeData.cid + '/info');
  2685. ctx.body = {
  2686. err: 0,
  2687. url: ctx.request.header.referer,
  2688. msg: '',
  2689. };
  2690. } catch (err) {
  2691. console.log(err);
  2692. // ctx.redirect(ctx.request.header.referer);
  2693. ctx.body = {
  2694. err: 1,
  2695. // url: ctx.request.header.referer,
  2696. msg: err,
  2697. };
  2698. }
  2699. }
  2700. /**
  2701. * 变更令撤销修订
  2702. * @param {Object} ctx - egg全局变量
  2703. * @return {void}
  2704. */
  2705. async cancelProjectRevise(ctx) {
  2706. try {
  2707. if (!(ctx.change.status === audit.changeProject.status.revise && (ctx.session.sessionUser.accountId === ctx.change.uid || ctx.session.sessionUser.accountId === ctx.session.sessionUser.is_admin))) {
  2708. throw '您无权进行该操作';
  2709. }
  2710. // 重新审批
  2711. const result = await ctx.service.changeProjectAudit.cancelRevise(ctx.change);
  2712. if (!result) {
  2713. throw '撤销修订失败';
  2714. }
  2715. ctx.body = {
  2716. err: 0,
  2717. url: ctx.request.header.referer,
  2718. msg: '',
  2719. };
  2720. } catch (err) {
  2721. console.log(err);
  2722. ctx.body = {
  2723. err: 1,
  2724. msg: err,
  2725. };
  2726. }
  2727. }
  2728. async _filterChangesApply(ctx, status = 0) {
  2729. const tenderId = ctx.params.id;
  2730. ctx.session.sessionUser.tenderId = tenderId;
  2731. const tender = await this.service.tender.getDataById(ctx.tender.id);
  2732. // const tender = ctx.tender;
  2733. // const tenderList = await this.service.tender.getList();
  2734. const page = ctx.page;
  2735. const pageSize = ctx.pageSize;
  2736. const sorts = ctx.query.sort ? ctx.query.sort : 0;
  2737. const orders = ctx.query.order ? ctx.query.order : 0;
  2738. const changes = await ctx.service.changeApply.getListByStatus(tender.id, status, 1, sorts, orders);
  2739. const total = await ctx.service.changeApply.getCountByStatus(tender.id, status);
  2740. let page_total = 0;
  2741. const tp = await ctx.service.changeApply.getTp(tender.id, status);
  2742. for (const c of changes) {
  2743. if (c.status !== audit.change.status.checked || !c.final_auditor_str) {
  2744. c.curAuditors = await ctx.service.changeApplyAudit.getAuditorsByStatus(c.id, c.status, c.times);
  2745. if (c.status === audit.change.status.checked && c.curAuditors.length > 0) {
  2746. const final_auditor_str = c.curAuditors[0].audit_type === auditType.key.common
  2747. ? c.curAuditors[0].name + (c.curAuditors[0].role ? '-' + c.curAuditors[0].role : '')
  2748. : ctx.helper.transFormToChinese(c.curAuditors[0].audit_order) + '审';
  2749. await ctx.service.changeApply.defaultUpdate({ final_auditor_str }, { where: { id: c.id } });
  2750. }
  2751. }
  2752. page_total = ctx.helper.add(page_total, c.total_price);
  2753. }
  2754. const tender_userInfo = await ctx.service.projectAccount.getDataById(ctx.tender.data.user_id);
  2755. // 分页相关
  2756. const pageInfo = {
  2757. page,
  2758. pageSizeSelect: 1,
  2759. pageSize,
  2760. total_num: total,
  2761. total: Math.ceil(total / pageSize),
  2762. queryData: JSON.stringify(ctx.urlInfo.query),
  2763. };
  2764. const filter = JSON.parse(JSON.stringify(audit.changeApply.filter));
  2765. filter.count = [];
  2766. 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);
  2767. 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);
  2768. 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);
  2769. 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);
  2770. // 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);
  2771. let codeRule = [];
  2772. let c_connector = '1';
  2773. let c_rule_first = 1;
  2774. const rule_type = 'apply';
  2775. if (tender.c_code_rules) {
  2776. const c_code_rules = JSON.parse(tender.c_code_rules);
  2777. codeRule = c_code_rules[rule_type + '_rule'] !== undefined ? c_code_rules[rule_type + '_rule'] : [];
  2778. c_connector = c_code_rules[rule_type + '_connector'] !== undefined ? c_code_rules[rule_type + '_connector'] : '1';
  2779. c_rule_first = c_code_rules[rule_type + '_rule_first'] !== undefined ? c_code_rules[rule_type + '_rule_first'] : 1;
  2780. }
  2781. for (const rule of codeRule) {
  2782. switch (rule.rule_type) {
  2783. case codeRuleConst.measure.ruleType.dealCode:
  2784. rule.preview = ctx.tender.info.deal_info.dealCode;
  2785. break;
  2786. case codeRuleConst.measure.ruleType.tenderName:
  2787. rule.preview = tender.name;
  2788. break;
  2789. case codeRuleConst.measure.ruleType.inDate:
  2790. rule.preview = moment().format('YYYY');
  2791. break;
  2792. case codeRuleConst.measure.ruleType.text:
  2793. rule.preview = rule.text;
  2794. break;
  2795. case codeRuleConst.measure.ruleType.addNo:
  2796. const s = '0000000000';
  2797. rule.preview = s.substr(s.length - rule.format);
  2798. break;
  2799. default: break;
  2800. }
  2801. }
  2802. const changeProjectList = await ctx.service.changeProject.getAllDataByCondition({ where: { tid: tender.id, status: audit.changeProject.status.checked } });
  2803. const allProjectCodes = await ctx.service.changeApply.getAllDataByCondition({
  2804. columns: ['project_code'],
  2805. where: {
  2806. tid: tender.id,
  2807. },
  2808. });
  2809. const pcLists = allProjectCodes.length > 0 ? ctx.app._.uniq(ctx.app._.map(allProjectCodes, 'project_code')) : [];
  2810. const renderData = {
  2811. uid: ctx.session.sessionUser.accountId,
  2812. tender,
  2813. pageInfo,
  2814. changes,
  2815. status,
  2816. rule_type,
  2817. codeRule,
  2818. c_connector,
  2819. c_rule_first,
  2820. filter,
  2821. apply_username: tender_userInfo ? tender_userInfo.name : null,
  2822. ruleType: codeRuleConst.ruleType[rule_type],
  2823. dealCode: ctx.tender.info.deal_info.dealCode,
  2824. auditConst: audit.changeApply,
  2825. ruleConst: codeRuleConst.measure,
  2826. changeConst,
  2827. auditType,
  2828. changeProjectList,
  2829. pcLists,
  2830. tp,
  2831. page_total,
  2832. tenderMenu: this.menu.tenderMenu,
  2833. preUrl: '/tender/' + tender.id,
  2834. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.apply),
  2835. };
  2836. await this.layout('change/apply.ejs', renderData, 'change/apply_modal.ejs');
  2837. }
  2838. /**
  2839. * 变更申请列表 页面 (Get)
  2840. *
  2841. * @param {Object} ctx - egg全局变量
  2842. * @return {void}
  2843. */
  2844. async apply(ctx) {
  2845. try {
  2846. await this._filterChangesApply(ctx);
  2847. } catch (err) {
  2848. this.log(err);
  2849. ctx.redirect('/dashboard');
  2850. }
  2851. }
  2852. /**
  2853. * 变更管理 状态筛选 页面 (Get)
  2854. * @param {Object} ctx - egg全局变量
  2855. * @return {void}
  2856. */
  2857. async applyStatus(ctx) {
  2858. try {
  2859. const status = parseInt(ctx.params.status);
  2860. await this._filterChangesApply(ctx, status);
  2861. } catch (err) {
  2862. this.logger.error(err);
  2863. ctx.redirect('/tender/' + ctx.params.id + '/change/apply');
  2864. }
  2865. }
  2866. /**
  2867. * 审批流程(Get)
  2868. * @param ctx
  2869. * @return {Promise<void>}
  2870. */
  2871. async changeApplyAuditors(ctx) {
  2872. try {
  2873. const id = JSON.parse(ctx.request.body.data).id;
  2874. const tenderId = ctx.params.id;
  2875. const changeInfo = await ctx.service.changeApply.getDataById(id);
  2876. await ctx.service.changeApply.loadChangeUser(changeInfo);
  2877. await ctx.service.changeApply.loadChangeAuditViewData(changeInfo);
  2878. ctx.body = { err: 0, msg: '', data: changeInfo };
  2879. } catch (error) {
  2880. this.log(error);
  2881. ctx.body = { err: 1, msg: error.toString(), data: null };
  2882. }
  2883. }
  2884. async changeApplySpGroup(ctx) {
  2885. try {
  2886. const data = JSON.parse(ctx.request.body.data);
  2887. const result = await ctx.service.changeApplyAudit.changeSpGroup(ctx.change, data.sp_group);
  2888. if (!result) {
  2889. throw '切换审批组失败';
  2890. }
  2891. const auditors = await ctx.service.changeApplyAudit.getUserGroup(ctx.change.id, ctx.change.times);
  2892. ctx.body = { err: 0, msg: '', data: auditors };
  2893. } catch (err) {
  2894. ctx.body = { err: 1, msg: err.toString(), data: null };
  2895. }
  2896. }
  2897. /**
  2898. * 撤回审批
  2899. * @param ctx
  2900. * @return {Promise<void>}
  2901. */
  2902. async checkApplyAuditCancel(ctx) {
  2903. try {
  2904. if (!ctx.change.cancancel) throw '您无权进行该操作';
  2905. await ctx.service.changeApplyAudit.checkCancel(ctx.change);
  2906. ctx.body = { err: 0, url: ctx.request.header.referer, msg: '' };
  2907. } catch (err) {
  2908. this.log(err);
  2909. ctx.body = { err: 1, msg: err };
  2910. }
  2911. }
  2912. /**
  2913. * 新增变更申请 (Post)
  2914. *
  2915. * @param {Object} ctx - egg全局变量
  2916. * @return {void}
  2917. */
  2918. async applyAdd(ctx) {
  2919. try {
  2920. const tenderId = ctx.params.id;
  2921. if (!tenderId) {
  2922. throw '当前未打开标段';
  2923. }
  2924. const data = JSON.parse(ctx.request.body.data);
  2925. if (!data.code || data.code === '') {
  2926. throw '变更申请编号不能为空';
  2927. }
  2928. // 在生成新变更申请后,需要copy前一个变更申请报表的签名信息
  2929. const lastChange = await ctx.service.changeApply.getLastChange(tenderId);
  2930. const change = await ctx.service.changeApply.add(tenderId, ctx.session.sessionUser.accountId, data.code, data.project_code, data.name);
  2931. await ctx.service.roleRptRel.createRoleRelationshipFromOtherBz(tenderId, '-303', change.id, lastChange ? lastChange.id : null);
  2932. ctx.body = { err: 0, msg: '', data: change };
  2933. } catch (err) {
  2934. this.log(err);
  2935. ctx.body = { err: 1, msg: err.toString() };
  2936. }
  2937. }
  2938. /**
  2939. * 删除变更立项
  2940. * @param {Object} ctx - egg全局变量
  2941. * @return {void}
  2942. */
  2943. async applyDelete(ctx) {
  2944. try {
  2945. const result = await ctx.service.changeApply.delete(ctx.request.body.caid);
  2946. if (!result) {
  2947. throw '删除变更立项失败';
  2948. }
  2949. ctx.redirect(ctx.request.header.referer);
  2950. } catch (err) {
  2951. console.log(err);
  2952. ctx.redirect(ctx.request.header.referer);
  2953. }
  2954. }
  2955. /**
  2956. * 获取审批界面所需的 原报、审批人数据等
  2957. * @param ctx
  2958. * @return {Promise<void>}
  2959. * @private
  2960. */
  2961. async _getChangeApplyAuditViewData(ctx) {
  2962. await ctx.service.changeApply.loadChangeAuditViewData(ctx.change);
  2963. }
  2964. async applyInformation(ctx) {
  2965. try {
  2966. const whiteList = this.ctx.app.config.multipart.whitelist;
  2967. const tender = await ctx.service.tender.getDataById(ctx.tender.id);
  2968. await this._getChangeApplyAuditViewData(ctx);
  2969. // 获取清单列表
  2970. const changeList = await ctx.service.changeApplyList.getList(ctx.change.id);
  2971. // 获取附件列表
  2972. const fileList = await ctx.service.changeApplyAtt.getAllChangeApplyAtt(ctx.tender.id, ctx.change.id);
  2973. const changeClass = await this._getOrUpdateClass(ctx, 'changeApply');
  2974. // 获取用户人验证手机号
  2975. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  2976. const auth_mobile = pa.auth_mobile;
  2977. const userPermission = pa && pa.permission ? JSON.parse(pa.permission) : null;
  2978. const deleteFilePermission = !!(userPermission && userPermission.change !== undefined && userPermission.change.indexOf('2') !== -1);
  2979. const renderData = {
  2980. tender,
  2981. change: ctx.change,
  2982. listRule: tender.c_apply_list_rule ? JSON.parse(tender.c_apply_list_rule) : { source: 1, rule: ['unit', 'unit_price'] },
  2983. changeList,
  2984. changeConst,
  2985. changeClass,
  2986. auditConst: audit.changeApply,
  2987. fileList,
  2988. whiteList,
  2989. authMobile: auth_mobile,
  2990. deleteFilePermission,
  2991. auditType,
  2992. shenpiConst,
  2993. tpUnit: ctx.change.decimal ? ctx.change.decimal.tp : ctx.tender.info.decimal.tp,
  2994. upUnit: ctx.change.decimal ? ctx.change.decimal.up : ctx.tender.info.decimal.up,
  2995. changeUnits: changeConst.units,
  2996. precision: ctx.change.decimal && ctx.change.decimal.precision ? ctx.change.decimal.precision : ctx.tender.info.precision,
  2997. 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,
  2998. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.apply_information),
  2999. preUrl: '/tender/' + ctx.tender.id + '/change/apply/' + ctx.change.id + '/information',
  3000. };
  3001. // if ((ctx.change.status === audit.changeApply.status.uncheck || ctx.change.status === audit.changeApply.status.checkNo || ctx.change.status === audit.changeApply.status.revise) && (ctx.session.sessionUser.accountId === ctx.change.uid || ctx.tender.isTourist)) {
  3002. // data.accountGroup = accountGroup;
  3003. // 获取所有项目参与者
  3004. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  3005. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  3006. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  3007. });
  3008. renderData.accountList = accountList;
  3009. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  3010. renderData.accountGroup = unitList.map(item => {
  3011. const groupList = accountList.filter(item1 => item1.company === item.name);
  3012. return { groupName: item.name, groupList };
  3013. });
  3014. // }
  3015. // 获取固定审批流列表
  3016. if (ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdspl) {
  3017. renderData.spGroupList = await ctx.service.shenpiGroup.getGroupListByChangeType(ctx.tender.id, shenpiConst.sp_type.change, 'changeApply');
  3018. }
  3019. await this.layout('change/apply_information.ejs', renderData, 'change/apply_information_modal.ejs');
  3020. } catch (err) {
  3021. this.log(err);
  3022. ctx.redirect('/tender/' + ctx.params.id + '/change/apply');
  3023. }
  3024. }
  3025. async applyInformationNotice(ctx) {
  3026. try {
  3027. const tender = await ctx.service.tender.getDataById(ctx.tender.id);
  3028. // 获取附件列表
  3029. const fileList = await ctx.service.changeApplyAtt.getAllChangeApplyAtt(ctx.tender.id, ctx.change.id);
  3030. await this._getChangeApplyAuditViewData(ctx);
  3031. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  3032. const userPermission = pa && pa.permission ? JSON.parse(pa.permission) : null;
  3033. const deleteFilePermission = !!(userPermission && userPermission.change !== undefined && userPermission.change.indexOf('2') !== -1);
  3034. const renderData = {
  3035. tender,
  3036. change: ctx.change,
  3037. changeConst,
  3038. auditConst: audit.changeApply,
  3039. fileList,
  3040. returnUrl: this.app._.includes(ctx.request.headers.referer, '/tender/' + ctx.tender.id + '/change/apply') ? ctx.request.headers.referer : null,
  3041. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.apply_information_notice),
  3042. preUrl: '/tender/' + ctx.tender.id + '/change/apply/' + ctx.change.id + '/information/notice',
  3043. deleteFilePermission,
  3044. };
  3045. await this.layout('change/apply_information_notice.ejs', renderData, 'change/apply_information_notice_modal.ejs');
  3046. } catch (err) {
  3047. this.log(err);
  3048. ctx.redirect('/tender/' + ctx.params.id + '/change');
  3049. }
  3050. }
  3051. // 审批相关
  3052. /**
  3053. * 添加审批人
  3054. * @param ctx
  3055. * @return {Promise<void>}
  3056. */
  3057. async addApplyAudit(ctx) {
  3058. try {
  3059. const auditConst = audit.changeApply;
  3060. const data = JSON.parse(ctx.request.body.data);
  3061. const id = this.app._.toInteger(data.auditorId);
  3062. if (isNaN(id) || id <= 0) {
  3063. throw '参数错误';
  3064. }
  3065. // 检查权限等
  3066. if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
  3067. throw '您无权添加审核人';
  3068. }
  3069. if (ctx.change.status === auditConst.status.checking || ctx.change.status === auditConst.status.checked) {
  3070. throw '当前不允许添加审核人';
  3071. }
  3072. ctx.change.auditorList = await ctx.service.changeApplyAudit.getAuditors(ctx.change.id, ctx.change.times);
  3073. // 检查审核人是否已存在
  3074. const exist = this.app._.find(ctx.change.auditorList, { aid: id });
  3075. if (exist) {
  3076. throw '该审核人已存在,请勿重复添加';
  3077. }
  3078. // const shenpiInfo = await ctx.service.shenpiAudit.getDataByCondition({ tid: ctx.tender.id, sp_type: shenpiConst.sp_type.material, sp_status: shenpiConst.sp_status.gdzs });
  3079. // const is_gdzs = shenpiInfo && ctx.tender.info.shenpi.material === shenpiConst.sp_status.gdzs ? 1 : 0;
  3080. const result = await ctx.service.changeApplyAudit.addAuditor(ctx.change.id, id, ctx.change.times);
  3081. if (!result) {
  3082. throw '添加审核人失败';
  3083. }
  3084. const auditors = await ctx.service.changeApplyAudit.getUserGroup(ctx.change.id, ctx.change.times);
  3085. ctx.body = { err: 0, msg: '', data: auditors };
  3086. } catch (err) {
  3087. this.log(err);
  3088. ctx.body = { err: 1, msg: err.toString(), data: null };
  3089. }
  3090. }
  3091. /**
  3092. * 移除审批人
  3093. * @param ctx
  3094. * @return {Promise<void>}
  3095. */
  3096. async deleteApplyAudit(ctx) {
  3097. try {
  3098. const data = JSON.parse(ctx.request.body.data);
  3099. const id = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
  3100. if (isNaN(id) || id <= 0) {
  3101. throw '参数错误';
  3102. }
  3103. const result = await ctx.service.changeApplyAudit.deleteAuditor(ctx.change.id, id, ctx.change.times);
  3104. if (!result) {
  3105. throw '移除审核人失败';
  3106. }
  3107. const auditors = await ctx.service.changeApplyAudit.getAuditors(ctx.change.id, ctx.change.times);
  3108. ctx.body = { err: 0, msg: '', data: auditors };
  3109. } catch (err) {
  3110. ctx.body = { err: 1, msg: err.toString(), data: null };
  3111. }
  3112. }
  3113. async saveApplyAudit(ctx) {
  3114. try {
  3115. // if (ctx.change.revising) {
  3116. // throw '台账修订中,请勿修改提交期数据';
  3117. // }
  3118. const data = JSON.parse(ctx.request.body.data);
  3119. if (ctx.session.sessionUser.is_admin && ctx.change.status !== audit.change.status.checked) {
  3120. await ctx.service.changeApplyAudit.saveAudit(ctx.change.id, ctx.change.times, ctx.change.sp_group, data);
  3121. const auditors = await ctx.service.changeApplyAudit.getUniqUserGroup(ctx.change.id, ctx.change.times);
  3122. ctx.body = { err: 0, msg: '', data: auditors };
  3123. } else {
  3124. throw '您无权进行该操作';
  3125. }
  3126. } catch (err) {
  3127. this.log(err);
  3128. // ctx.session.postError = err.toString();
  3129. // ctx.redirect(ctx.request.header.referer);
  3130. ctx.body = {
  3131. err: 1,
  3132. // url: ctx.request.header.referer,
  3133. msg: err,
  3134. };
  3135. }
  3136. }
  3137. /**
  3138. * 上传附件
  3139. * @param {*} ctx 上下文
  3140. */
  3141. async uploadApplyFile(ctx) {
  3142. let stream;
  3143. try {
  3144. const auditConst = audit.changeApply;
  3145. // this._checkAdvanceFileCanModify(ctx);
  3146. const parts = this.ctx.multipart({
  3147. autoFields: true,
  3148. });
  3149. const files = [];
  3150. const create_time = Date.parse(new Date()) / 1000;
  3151. let idx = 0;
  3152. const extra_upload = ctx.change.status === auditConst.status.checked;
  3153. while ((stream = await parts()) !== undefined) {
  3154. if (!stream.filename) {
  3155. // 如果没有传入直接返回
  3156. return;
  3157. }
  3158. const fileInfo = path.parse(stream.filename);
  3159. const filepath = `app/public/upload/${this.ctx.tender.id.toString()}/change_apply/fujian_${create_time + idx.toString() + fileInfo.ext}`;
  3160. // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, 'app', filepath));
  3161. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  3162. files.push({ filepath, name: stream.filename, ext: fileInfo.ext });
  3163. ++idx;
  3164. stream && (await sendToWormhole(stream));
  3165. }
  3166. const in_time = new Date();
  3167. const payload = files.map(file => {
  3168. let idx;
  3169. if (Array.isArray(parts.field.name)) {
  3170. idx = parts.field.name.findIndex(name => name === file.name);
  3171. } else {
  3172. idx = 'isString';
  3173. }
  3174. const newFile = {
  3175. tid: ctx.tender.id,
  3176. caid: ctx.change.id,
  3177. uid: ctx.session.sessionUser.accountId,
  3178. type: parts.field.type,
  3179. filename: file.name,
  3180. fileext: file.ext,
  3181. filesize: ctx.helper.bytesToSize(idx === 'isString' ? parts.field.size : parts.field.size[idx]),
  3182. filepath: file.filepath,
  3183. upload_time: in_time,
  3184. extra_upload,
  3185. };
  3186. return newFile;
  3187. });
  3188. // 执行文件信息写入数据库
  3189. await ctx.service.changeApplyAtt.saveFileMsgToDb(payload);
  3190. // 将最新的当前标段的所有文件信息返回
  3191. const data = await ctx.service.changeApplyAtt.getAllChangeApplyAtt(ctx.tender.id, ctx.change.id);
  3192. ctx.body = { err: 0, msg: '', data };
  3193. } catch (err) {
  3194. stream && (await sendToWormhole(stream));
  3195. this.log(err);
  3196. ctx.body = { err: 1, msg: err.toString(), data: null };
  3197. }
  3198. }
  3199. /**
  3200. * 删除附件
  3201. * @param {Ojbect} ctx 上下文
  3202. */
  3203. async deleteApplyFile(ctx) {
  3204. try {
  3205. const { id } = JSON.parse(ctx.request.body.data);
  3206. const fileInfo = await ctx.service.changeApplyAtt.getDataById(id);
  3207. if (fileInfo || Object.keys(fileInfo).length) {
  3208. // 先删除文件
  3209. // await fs.unlinkSync(path.resolve(this.app.baseDir, './app', fileInfo.filepath));
  3210. await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
  3211. // 再删除数据库
  3212. await ctx.service.changeApplyAtt.delete(id);
  3213. } else {
  3214. throw '不存在该文件';
  3215. }
  3216. const data = await ctx.service.changeApplyAtt.getAllChangeApplyAtt(ctx.tender.id, ctx.change.id);
  3217. ctx.body = { err: 0, msg: '请求成功', data };
  3218. } catch (err) {
  3219. this.log(err);
  3220. ctx.body = { err: 1, msg: err.toString(), data: null };
  3221. }
  3222. }
  3223. /**
  3224. * 下载附件
  3225. * @param {Object} ctx - egg全局变量
  3226. * @return {void}
  3227. */
  3228. async downloadApplyFile(ctx) {
  3229. const id = ctx.params.fid;
  3230. if (id) {
  3231. try {
  3232. const fileInfo = await ctx.service.changeApplyAtt.getDataById(id);
  3233. if (fileInfo !== undefined && fileInfo !== '') {
  3234. // const fileName = path.join(__dirname, '../', fileInfo.filepath);
  3235. // 解决中文无法下载问题
  3236. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  3237. let disposition = '';
  3238. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  3239. disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename);
  3240. } else if (userAgent.indexOf('firefox') >= 0) {
  3241. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename) + '"';
  3242. } else {
  3243. /* safari等其他非主流浏览器只能自求多福了 */
  3244. disposition = 'attachment; filename=' + new Buffer(fileInfo.filename).toString('binary');
  3245. }
  3246. ctx.response.set({
  3247. 'Content-Type': 'application/octet-stream',
  3248. 'Content-Disposition': disposition,
  3249. 'Content-Length': fileInfo.filesize,
  3250. });
  3251. // ctx.body = await fs.createReadStream(fileName);
  3252. ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
  3253. } else {
  3254. throw '不存在该文件';
  3255. }
  3256. } catch (err) {
  3257. this.log(err);
  3258. this.setMessage(err.toString(), this.messageType.ERROR);
  3259. }
  3260. }
  3261. }
  3262. async applyInformationSave(ctx) {
  3263. try {
  3264. const data = JSON.parse(ctx.request.body.data);
  3265. if (data.name === 'code') {
  3266. const info = await ctx.service.changeApply.isRepeat(ctx.change.id, data.val, ctx.tender.id);
  3267. if (info) {
  3268. throw '该编号已存在';
  3269. }
  3270. }
  3271. const result = await ctx.service.changeApply.saveInfo(ctx.change.id, data);
  3272. if (!result) {
  3273. throw '修改失败';
  3274. }
  3275. ctx.body = { err: 0, msg: '请求成功', data: null };
  3276. } catch (err) {
  3277. this.log(err);
  3278. ctx.body = { err: 1, msg: err.toString(), data: null };
  3279. }
  3280. }
  3281. /**
  3282. * 上报和重新上报
  3283. * @param ctx
  3284. * @return {Promise<void>}
  3285. */
  3286. async startApplyAudit(ctx) {
  3287. try {
  3288. const auditConst = audit.changeApply;
  3289. // 检查权限等
  3290. if (!ctx.change) {
  3291. throw '数据错误';
  3292. }
  3293. if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
  3294. throw '您无权上报该变更申请数据';
  3295. }
  3296. if (ctx.change.status === auditConst.status.checking || ctx.change.status === auditConst.status.checked) {
  3297. throw '该变更申请数据当前无法上报';
  3298. }
  3299. await ctx.service.changeApplyAudit.start(ctx.change.id, ctx.change.times);
  3300. ctx.redirect(ctx.request.header.referer);
  3301. } catch (err) {
  3302. this.log(err);
  3303. ctx.session.postError = err.toString();
  3304. ctx.redirect(ctx.request.header.referer);
  3305. }
  3306. }
  3307. /**
  3308. * 审批
  3309. * @param ctx
  3310. * @return {Promise<void>}
  3311. */
  3312. async checkApplyAudit(ctx) {
  3313. try {
  3314. const auditConst = audit.changeApply;
  3315. if (!ctx.change || ctx.change.status !== auditConst.status.checking) {
  3316. throw '当前变更申请数据有误';
  3317. }
  3318. if (ctx.change.curAuditorIds.length === 0 || ctx.change.curAuditorIds.indexOf(ctx.session.sessionUser.accountId) === -1) {
  3319. throw '您无权进行该操作';
  3320. }
  3321. const data = {
  3322. checkType: parseInt(ctx.request.body.checkType),
  3323. opinion: ctx.request.body.opinion,
  3324. };
  3325. if (!data.checkType || isNaN(data.checkType)) {
  3326. throw '提交数据错误';
  3327. }
  3328. if (ctx.request.body.notice_uid) {
  3329. data.notice_code = ctx.request.body.notice_code;
  3330. data.notice_uid = ctx.request.body.notice_uid;
  3331. }
  3332. // if (data.checkType === auditConst.status.checkNo) {
  3333. // if (!data.checkType || isNaN(data.checkType)) {
  3334. // throw '提交数据错误';
  3335. // }
  3336. // }
  3337. await ctx.service.changeApplyAudit.check(ctx.change.id, data, ctx.change.times);
  3338. ctx.redirect(ctx.request.header.referer);
  3339. } catch (err) {
  3340. this.log(err);
  3341. ctx.session.postError = err.toString();
  3342. ctx.redirect(ctx.request.header.referer);
  3343. }
  3344. }
  3345. /**
  3346. * 变更清单 - 操作 (Ajax)
  3347. * @param ctx
  3348. * @return {Promise<void>}
  3349. */
  3350. async saveApplyListsData(ctx) {
  3351. try {
  3352. const data = JSON.parse(ctx.request.body.data);
  3353. const responseData = {
  3354. err: 0,
  3355. msg: '',
  3356. data: {},
  3357. };
  3358. switch (data.type) {
  3359. case 'add':
  3360. responseData.data = await ctx.service.changeApplyList.add(data.updateData);
  3361. break;
  3362. case 'batchadd':
  3363. responseData.data = await ctx.service.changeApplyList.batchAdd(data);
  3364. break;
  3365. case 'del':
  3366. await ctx.service.changeApplyList.del(data.ids);
  3367. // 取所有工料表
  3368. responseData.data = await ctx.service.changeApplyList.getList(ctx.change.id);
  3369. break;
  3370. case 'update':
  3371. // if (data.updateData.code === '' || data.updateData.code === null) {
  3372. // throw '请先输入编号';
  3373. // }
  3374. await ctx.service.changeApplyList.save(data.updateData);
  3375. break;
  3376. case 'paste':
  3377. await ctx.service.changeApplyList.saveDatas(data.updateData);
  3378. // 取所有工料表
  3379. responseData.data = await ctx.service.changeApplyList.getList(ctx.change.id);
  3380. break;
  3381. case 'paste_amount_rows':
  3382. await ctx.service.changeApplyList.saveDatas(data.updateData);
  3383. responseData.data = await ctx.service.changeApplyList.getList(ctx.change.id);
  3384. break;
  3385. case 'list_rule':
  3386. const result = await ctx.service.tender.saveTenderData(ctx.tender.id, { c_apply_list_rule: data.postData });
  3387. if (!result) {
  3388. throw '修改失败';
  3389. }
  3390. break;
  3391. // case 'update_tp':
  3392. // await ctx.service.changeApply.saveInfo(ctx.change.id, { name: 'total_price', val: data.updateData });
  3393. // break;
  3394. default: throw '参数有误';
  3395. }
  3396. ctx.body = responseData;
  3397. } catch (err) {
  3398. this.log(err);
  3399. ctx.body = { err: 1, msg: err.toString(), data: null };
  3400. }
  3401. }
  3402. /**
  3403. * 变更申请重新审批
  3404. * @param {Object} ctx - egg全局变量
  3405. * @return {void}
  3406. */
  3407. async checkApplyAgain(ctx) {
  3408. try {
  3409. // 获取终审
  3410. // const auditInfo = await this.ctx.service.changeApplyAudit.getAuditorByStatus(ctx.change.id, audit.changeApply.status.checked);
  3411. if (ctx.change.status !== audit.changeApply.status.checked) {
  3412. throw '您无权进行该操作';
  3413. }
  3414. // 判断是否被变更申请调用了,是则无法发起修订
  3415. const projectInfo = await ctx.service.changePlan.getDataByCondition({ tid: ctx.tender.id, apply_code: ctx.change.code });
  3416. if (projectInfo) {
  3417. throw '该变更申请已被变更方案调用,无法发起重新审批';
  3418. }
  3419. if (ctx.session.sessionUser.loginStatus === 0) {
  3420. const code = ctx.request.body.code;
  3421. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  3422. if (!pa.auth_mobile) {
  3423. throw '未绑定手机号';
  3424. }
  3425. const cacheKey = 'smsCode:' + ctx.session.sessionUser.accountId;
  3426. const cacheCode = await app.redis.get(cacheKey);
  3427. // console.log(cacheCode);
  3428. if (cacheCode === null || code === undefined || cacheCode !== (code + pa.auth_mobile)) {
  3429. throw '验证码不正确!';
  3430. }
  3431. }
  3432. if ((ctx.change.finalAuditorIds.indexOf(ctx.session.sessionUser.accountId) >= 0) && ctx.change.status === audit.changeApply.status.checked) {
  3433. // 重新审批
  3434. const result = await ctx.service.changeApplyAudit.checkAgain(ctx.change);
  3435. if (!result) {
  3436. throw '重新审批失败';
  3437. }
  3438. } else {
  3439. throw '您无权进行该操作';
  3440. }
  3441. // ctx.redirect('/tender/' + changeData.tid + '/change/' + changeData.cid + '/info');
  3442. ctx.body = {
  3443. err: 0,
  3444. url: ctx.request.header.referer,
  3445. msg: '',
  3446. };
  3447. } catch (err) {
  3448. console.log(err);
  3449. // ctx.redirect(ctx.request.header.referer);
  3450. ctx.body = {
  3451. err: 1,
  3452. // url: ctx.request.header.referer,
  3453. msg: err,
  3454. };
  3455. }
  3456. }
  3457. /**
  3458. * 变更申请修订重新上报
  3459. * @param {Object} ctx - egg全局变量
  3460. * @return {void}
  3461. */
  3462. async checkApplyRevise(ctx) {
  3463. try {
  3464. if (ctx.change.status !== audit.changeApply.status.checked || ctx.session.sessionUser.accountId !== ctx.change.uid) {
  3465. throw '您无权进行该操作';
  3466. }
  3467. // 判断是否被变更申请调用了,是则无法发起修订
  3468. const projectInfo = await ctx.service.changePlan.getDataByCondition({ tid: ctx.tender.id, apply_code: ctx.change.code });
  3469. if (projectInfo) {
  3470. throw '该变更申请已被变更方案调用,无法发起修订';
  3471. }
  3472. if (ctx.session.sessionUser.loginStatus === 0) {
  3473. const code = ctx.request.body.code;
  3474. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  3475. if (!pa.auth_mobile) {
  3476. throw '未绑定手机号';
  3477. }
  3478. const cacheKey = 'smsCode:' + ctx.session.sessionUser.accountId;
  3479. const cacheCode = await app.redis.get(cacheKey);
  3480. // console.log(cacheCode);
  3481. if (cacheCode === null || code === undefined || cacheCode !== (code + pa.auth_mobile)) {
  3482. throw '验证码不正确!';
  3483. }
  3484. }
  3485. // 重新审批
  3486. const result = await ctx.service.changeApplyAudit.checkRevise(ctx.change);
  3487. if (!result) {
  3488. throw '修订发起失败';
  3489. }
  3490. // ctx.redirect('/tender/' + changeData.tid + '/change/' + changeData.cid + '/info');
  3491. ctx.body = {
  3492. err: 0,
  3493. url: ctx.request.header.referer,
  3494. msg: '',
  3495. };
  3496. } catch (err) {
  3497. console.log(err);
  3498. // ctx.redirect(ctx.request.header.referer);
  3499. ctx.body = {
  3500. err: 1,
  3501. // url: ctx.request.header.referer,
  3502. msg: err,
  3503. };
  3504. }
  3505. }
  3506. /**
  3507. * 变更令撤销修订
  3508. * @param {Object} ctx - egg全局变量
  3509. * @return {void}
  3510. */
  3511. async cancelApplyRevise(ctx) {
  3512. try {
  3513. if (!(ctx.change.status === audit.changeApply.status.revise && (ctx.session.sessionUser.accountId === ctx.change.uid || ctx.session.sessionUser.accountId === ctx.session.sessionUser.is_admin))) {
  3514. throw '您无权进行该操作';
  3515. }
  3516. // 重新审批
  3517. const result = await ctx.service.changeApplyAudit.cancelRevise(ctx.change);
  3518. if (!result) {
  3519. throw '撤销修订失败';
  3520. }
  3521. ctx.body = {
  3522. err: 0,
  3523. url: ctx.request.header.referer,
  3524. msg: '',
  3525. };
  3526. } catch (err) {
  3527. console.log(err);
  3528. ctx.body = {
  3529. err: 1,
  3530. msg: err,
  3531. };
  3532. }
  3533. }
  3534. // 变更方案
  3535. async _filterChangesPlan(ctx, status = 0) {
  3536. const tenderId = ctx.params.id;
  3537. ctx.session.sessionUser.tenderId = tenderId;
  3538. const tender = await this.service.tender.getDataById(ctx.tender.id);
  3539. // const tender = ctx.tender;
  3540. // const tenderList = await this.service.tender.getList();
  3541. const page = ctx.page;
  3542. const pageSize = ctx.pageSize;
  3543. const sorts = ctx.query.sort ? ctx.query.sort : 0;
  3544. const orders = ctx.query.order ? ctx.query.order : 0;
  3545. const changes = await ctx.service.changePlan.getListByStatus(tender.id, status, 1, sorts, orders);
  3546. const total = await ctx.service.changePlan.getCountByStatus(tender.id, status);
  3547. let page_total = 0;
  3548. const tp = await ctx.service.changePlan.getTp(tender.id, status);
  3549. for (const c of changes) {
  3550. if (c.status !== audit.change.status.checked || !c.final_auditor_str) {
  3551. c.curAuditors = await ctx.service.changePlanAudit.getAuditorsByStatus(c.id, c.status, c.times);
  3552. // if (c.status === audit.changePlan.status.checkNoPre) {
  3553. // c.curAuditors2 = await ctx.service.changePlanAudit.getAuditorsByStatus(c.id, audit.changePlan.status.checking, c.times);
  3554. // }
  3555. if (c.status === audit.change.status.checked && c.curAuditors.length > 0) {
  3556. const final_auditor_str = c.curAuditors[0].audit_type === auditType.key.common
  3557. ? c.curAuditors[0].name + (c.curAuditors[0].role ? '-' + c.curAuditors[0].role : '')
  3558. : ctx.helper.transFormToChinese(c.curAuditors[0].audit_order) + '审';
  3559. await ctx.service.changePlan.defaultUpdate({ final_auditor_str }, { where: { id: c.id } });
  3560. }
  3561. }
  3562. page_total = ctx.helper.add(page_total, c.total_price);
  3563. }
  3564. const tender_userInfo = await ctx.service.projectAccount.getDataById(ctx.tender.data.user_id);
  3565. // 分页相关
  3566. const pageInfo = {
  3567. page,
  3568. pageSizeSelect: 1,
  3569. pageSize,
  3570. total_num: total,
  3571. total: Math.ceil(total / pageSize),
  3572. queryData: JSON.stringify(ctx.urlInfo.query),
  3573. };
  3574. const filter = JSON.parse(JSON.stringify(audit.changePlan.filter));
  3575. filter.count = [];
  3576. 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);
  3577. 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);
  3578. 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);
  3579. 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);
  3580. // 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);
  3581. let codeRule = [];
  3582. let c_connector = '1';
  3583. let c_rule_first = 1;
  3584. const rule_type = 'plan';
  3585. if (tender.c_code_rules) {
  3586. const c_code_rules = JSON.parse(tender.c_code_rules);
  3587. codeRule = c_code_rules[rule_type + '_rule'] !== undefined ? c_code_rules[rule_type + '_rule'] : [];
  3588. c_connector = c_code_rules[rule_type + '_connector'] !== undefined ? c_code_rules[rule_type + '_connector'] : '1';
  3589. c_rule_first = c_code_rules[rule_type + '_rule_first'] !== undefined ? c_code_rules[rule_type + '_rule_first'] : 1;
  3590. }
  3591. for (const rule of codeRule) {
  3592. switch (rule.rule_type) {
  3593. case codeRuleConst.measure.ruleType.dealCode:
  3594. rule.preview = ctx.tender.info.deal_info.dealCode;
  3595. break;
  3596. case codeRuleConst.measure.ruleType.tenderName:
  3597. rule.preview = tender.name;
  3598. break;
  3599. case codeRuleConst.measure.ruleType.inDate:
  3600. rule.preview = moment().format('YYYY');
  3601. break;
  3602. case codeRuleConst.measure.ruleType.text:
  3603. rule.preview = rule.text;
  3604. break;
  3605. case codeRuleConst.measure.ruleType.addNo:
  3606. const s = '0000000000';
  3607. rule.preview = s.substr(s.length - rule.format);
  3608. break;
  3609. default: break;
  3610. }
  3611. }
  3612. const changeApplyList = await ctx.service.changeApply.getAllDataByCondition({ where: { tid: tender.id, status: audit.changeApply.status.checked } });
  3613. const allApplyCodes = await ctx.service.changePlan.getAllDataByCondition({
  3614. columns: ['apply_code'],
  3615. where: {
  3616. tid: tender.id,
  3617. },
  3618. });
  3619. const acLists = allApplyCodes.length > 0 ? ctx.app._.uniq(ctx.app._.map(allApplyCodes, 'apply_code')) : [];
  3620. const renderData = {
  3621. uid: ctx.session.sessionUser.accountId,
  3622. tender,
  3623. pageInfo,
  3624. changes,
  3625. status,
  3626. rule_type,
  3627. codeRule,
  3628. c_connector,
  3629. c_rule_first,
  3630. filter,
  3631. plan_username: tender_userInfo ? tender_userInfo.name : null,
  3632. ruleType: codeRuleConst.ruleType[rule_type],
  3633. dealCode: ctx.tender.info.deal_info.dealCode,
  3634. auditConst: audit.changePlan,
  3635. ruleConst: codeRuleConst.measure,
  3636. changeConst,
  3637. auditType,
  3638. changeApplyList,
  3639. acLists,
  3640. page_total,
  3641. tp,
  3642. tenderMenu: this.menu.tenderMenu,
  3643. preUrl: '/tender/' + tender.id,
  3644. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.plan),
  3645. };
  3646. await this.layout('change/plan.ejs', renderData, 'change/plan_modal.ejs');
  3647. }
  3648. /**
  3649. * 变更申请列表 页面 (Get)
  3650. *
  3651. * @param {Object} ctx - egg全局变量
  3652. * @return {void}
  3653. */
  3654. async plan(ctx) {
  3655. try {
  3656. await this._filterChangesPlan(ctx);
  3657. } catch (err) {
  3658. this.log(err);
  3659. ctx.redirect('/dashboard');
  3660. }
  3661. }
  3662. /**
  3663. * 变更管理 状态筛选 页面 (Get)
  3664. * @param {Object} ctx - egg全局变量
  3665. * @return {void}
  3666. */
  3667. async planStatus(ctx) {
  3668. try {
  3669. const status = parseInt(ctx.params.status);
  3670. await this._filterChangesPlan(ctx, status);
  3671. } catch (err) {
  3672. this.logger.error(err);
  3673. ctx.redirect('/tender/' + ctx.params.id + '/change/plan');
  3674. }
  3675. }
  3676. /**
  3677. * 审批流程(Get)
  3678. * @param ctx
  3679. * @return {Promise<void>}
  3680. */
  3681. async changePlanAuditors(ctx) {
  3682. try {
  3683. const id = JSON.parse(ctx.request.body.data).id;
  3684. const tenderId = ctx.params.id;
  3685. const changeInfo = await ctx.service.changePlan.getDataById(id);
  3686. await ctx.service.changePlan.loadChangeUser(changeInfo);
  3687. await ctx.service.changePlan.loadChangeAuditViewData(changeInfo);
  3688. ctx.body = { err: 0, msg: '', data: changeInfo };
  3689. } catch (error) {
  3690. this.log(error);
  3691. ctx.body = { err: 1, msg: error.toString(), data: null };
  3692. }
  3693. }
  3694. /**
  3695. * 新增变更方案 (Post)
  3696. *
  3697. * @param {Object} ctx - egg全局变量
  3698. * @return {void}
  3699. */
  3700. async planAdd(ctx) {
  3701. try {
  3702. const tenderId = ctx.params.id;
  3703. if (!tenderId) {
  3704. throw '当前未打开标段';
  3705. }
  3706. const data = JSON.parse(ctx.request.body.data);
  3707. if (!data.code || data.code === '') {
  3708. throw '变更方案编号不能为空';
  3709. }
  3710. // 在生成新变更方案后,需要copy前一个变更方案报表的签名信息
  3711. const lastChange = await ctx.service.changePlan.getLastChange(tenderId);
  3712. const change = await ctx.service.changePlan.add(tenderId, ctx.session.sessionUser.accountId, data.code, data.apply_code, data.name);
  3713. await ctx.service.roleRptRel.createRoleRelationshipFromOtherBz(tenderId, '-301', change.id, lastChange ? lastChange.id : null);
  3714. ctx.body = { err: 0, msg: '', data: change };
  3715. } catch (err) {
  3716. this.log(err);
  3717. ctx.body = { err: 1, msg: err.toString() };
  3718. }
  3719. }
  3720. /**
  3721. * 删除变更立项
  3722. * @param {Object} ctx - egg全局变量
  3723. * @return {void}
  3724. */
  3725. async planDelete(ctx) {
  3726. try {
  3727. const result = await ctx.service.changePlan.delete(ctx.request.body.cpid);
  3728. if (!result) {
  3729. throw '删除变更方案失败';
  3730. }
  3731. ctx.redirect(ctx.request.header.referer);
  3732. } catch (err) {
  3733. console.log(err);
  3734. ctx.redirect(ctx.request.header.referer);
  3735. }
  3736. }
  3737. /**
  3738. * 获取审批界面所需的 原报、审批人数据等
  3739. * @param ctx
  3740. * @return {Promise<void>}
  3741. * @private
  3742. */
  3743. async _getChangePlanAuditViewData(ctx) {
  3744. await ctx.service.changePlan.loadChangeAuditViewData(ctx.change);
  3745. }
  3746. async planInformation(ctx) {
  3747. try {
  3748. const whiteList = this.ctx.app.config.multipart.whitelist;
  3749. const tender = await ctx.service.tender.getDataById(ctx.tender.id);
  3750. await this._getChangePlanAuditViewData(ctx);
  3751. // 获取附件列表
  3752. const fileList = await ctx.service.changePlanAtt.getAllChangePlanAtt(ctx.tender.id, ctx.change.id);
  3753. // 获取清单列表
  3754. const changeList = await ctx.service.changePlanList.getList(ctx.change.id);
  3755. if (ctx.change.status === audit.changePlan.status.checking || ctx.change.status === audit.changePlan.status.checked) {
  3756. // const listAudits = await ctx.service.changePlanAudit.getAuditGroupByList(ctx.change.id, ctx.change.times);
  3757. // ctx.change.listAudits = listAudits;
  3758. for (const cl of changeList) {
  3759. const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
  3760. // 清单表页赋值
  3761. for (const au in ctx.change.userGroups) {
  3762. if (ctx.change.userGroups[au][0].audit_order !== 0) {
  3763. cl['audit_amount_' + ctx.change.userGroups[au][0].audit_order] =
  3764. (ctx.change.userGroups[au][0].audit_type === auditType.key.and &&
  3765. ctx.helper._.findIndex(ctx.change.userGroups[au], { aid: ctx.session.sessionUser.accountId, status: audit.changePlan.status.checked }) !== -1) ||
  3766. (ctx.change.shenpiPower && ctx.helper._.findIndex(ctx.change.userGroups[au], { aid: ctx.session.sessionUser.accountId }) !== -1) ?
  3767. cl.spamount : (audit_amount[au - 1] !== undefined ? parseFloat(audit_amount[au - 1]) : null);
  3768. }
  3769. }
  3770. }
  3771. }
  3772. // 获取用户人验证手机号
  3773. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  3774. const auth_mobile = pa.auth_mobile;
  3775. const userPermission = pa && pa.permission ? JSON.parse(pa.permission) : null;
  3776. const deleteFilePermission = !!(userPermission && userPermission.change !== undefined && userPermission.change.indexOf('2') !== -1);
  3777. const changeClass = await this._getOrUpdateClass(ctx, 'changePlan');
  3778. const renderData = {
  3779. tender,
  3780. change: ctx.change,
  3781. listRule: tender.c_plan_list_rule ? JSON.parse(tender.c_plan_list_rule) : { source: 1, rule: ['unit', 'unit_price'] },
  3782. changeList,
  3783. changeClass,
  3784. changeConst,
  3785. auditConst: audit.changePlan,
  3786. fileList,
  3787. whiteList,
  3788. authMobile: auth_mobile,
  3789. deleteFilePermission,
  3790. auditType,
  3791. shenpiConst,
  3792. tpUnit: ctx.change.decimal ? ctx.change.decimal.tp : ctx.tender.info.decimal.tp,
  3793. upUnit: ctx.change.decimal ? ctx.change.decimal.up : ctx.tender.info.decimal.up,
  3794. changeUnits: changeConst.units,
  3795. precision: ctx.change.decimal && ctx.change.decimal.precision ? ctx.change.decimal.precision : ctx.tender.info.precision,
  3796. 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,
  3797. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.plan_information),
  3798. preUrl: '/tender/' + ctx.tender.id + '/change/plan/' + ctx.change.id + '/information',
  3799. };
  3800. // if ((ctx.change.status === audit.changePlan.status.uncheck || ctx.change.status === audit.changePlan.status.checkNo || ctx.change.status === audit.changePlan.status.revise) && (ctx.session.sessionUser.accountId === ctx.change.uid || ctx.tender.isTourist)) {
  3801. // data.accountGroup = accountGroup;
  3802. // 获取所有项目参与者
  3803. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  3804. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  3805. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  3806. });
  3807. renderData.accountList = accountList;
  3808. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  3809. renderData.accountGroup = unitList.map(item => {
  3810. const groupList = accountList.filter(item1 => item1.company === item.name);
  3811. return { groupName: item.name, groupList };
  3812. });
  3813. // 获取固定审批流列表
  3814. if (ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdspl) {
  3815. renderData.spGroupList = await ctx.service.shenpiGroup.getGroupListByChangeType(ctx.tender.id, shenpiConst.sp_type.change, 'changePlan');
  3816. }
  3817. // }
  3818. await this.layout('change/plan_information.ejs', renderData, 'change/plan_information_modal.ejs');
  3819. } catch (err) {
  3820. this.log(err);
  3821. ctx.redirect('/tender/' + ctx.params.id + '/change/plan');
  3822. }
  3823. }
  3824. async changePlanSpGroup(ctx) {
  3825. try {
  3826. const data = JSON.parse(ctx.request.body.data);
  3827. const result = await ctx.service.changePlanAudit.changeSpGroup(ctx.change, data.sp_group);
  3828. if (!result) {
  3829. throw '切换审批组失败';
  3830. }
  3831. const auditors = await ctx.service.changePlanAudit.getUserGroup(ctx.change.id, ctx.change.times);
  3832. ctx.body = { err: 0, msg: '', data: auditors };
  3833. } catch (err) {
  3834. ctx.body = { err: 1, msg: err.toString(), data: null };
  3835. }
  3836. }
  3837. // 审批相关
  3838. /**
  3839. * 添加审批人
  3840. * @param ctx
  3841. * @return {Promise<void>}
  3842. */
  3843. async addPlanAudit(ctx) {
  3844. try {
  3845. const auditConst = audit.changePlan;
  3846. const data = JSON.parse(ctx.request.body.data);
  3847. const id = this.app._.toInteger(data.auditorId);
  3848. if (isNaN(id) || id <= 0) {
  3849. throw '参数错误';
  3850. }
  3851. // 检查权限等
  3852. if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
  3853. throw '您无权添加审核人';
  3854. }
  3855. if (ctx.change.status === auditConst.status.checking || ctx.change.status === auditConst.status.checked) {
  3856. throw '当前不允许添加审核人';
  3857. }
  3858. ctx.change.auditorList = await ctx.service.changePlanAudit.getAuditors(ctx.change.id, ctx.change.times);
  3859. // 检查审核人是否已存在
  3860. const exist = this.app._.find(ctx.change.auditorList, { aid: id });
  3861. if (exist) {
  3862. throw '该审核人已存在,请勿重复添加';
  3863. }
  3864. // const shenpiInfo = await ctx.service.shenpiAudit.getDataByCondition({ tid: ctx.tender.id, sp_type: shenpiConst.sp_type.material, sp_status: shenpiConst.sp_status.gdzs });
  3865. // const is_gdzs = shenpiInfo && ctx.tender.info.shenpi.material === shenpiConst.sp_status.gdzs ? 1 : 0;
  3866. const result = await ctx.service.changePlanAudit.addAuditor(ctx.change.id, id, ctx.change.times);
  3867. if (!result) {
  3868. throw '添加审核人失败';
  3869. }
  3870. const auditors = await ctx.service.changePlanAudit.getUserGroup(ctx.change.id, ctx.change.times);
  3871. ctx.body = { err: 0, msg: '', data: auditors };
  3872. } catch (err) {
  3873. this.log(err);
  3874. ctx.body = { err: 1, msg: err.toString(), data: null };
  3875. }
  3876. }
  3877. /**
  3878. * 移除审批人
  3879. * @param ctx
  3880. * @return {Promise<void>}
  3881. */
  3882. async deletePlanAudit(ctx) {
  3883. try {
  3884. const data = JSON.parse(ctx.request.body.data);
  3885. const id = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
  3886. if (isNaN(id) || id <= 0) {
  3887. throw '参数错误';
  3888. }
  3889. const result = await ctx.service.changePlanAudit.deleteAuditor(ctx.change.id, id, ctx.change.times);
  3890. if (!result) {
  3891. throw '移除审核人失败';
  3892. }
  3893. const auditors = await ctx.service.changePlanAudit.getAuditors(ctx.change.id, ctx.change.times);
  3894. ctx.body = { err: 0, msg: '', data: auditors };
  3895. } catch (err) {
  3896. ctx.body = { err: 1, msg: err.toString(), data: null };
  3897. }
  3898. }
  3899. async savePlanAudit(ctx) {
  3900. try {
  3901. // if (ctx.change.revising) {
  3902. // throw '台账修订中,请勿修改提交期数据';
  3903. // }
  3904. const data = JSON.parse(ctx.request.body.data);
  3905. if (ctx.session.sessionUser.is_admin && ctx.change.status !== audit.change.status.checked) {
  3906. await ctx.service.changePlanAudit.saveAudit(ctx.change.id, ctx.change.times, ctx.change.sp_group, data);
  3907. const auditors = await ctx.service.changePlanAudit.getUniqUserGroup(ctx.change.id, ctx.change.times);
  3908. ctx.body = { err: 0, msg: '', data: auditors };
  3909. } else {
  3910. throw '您无权进行该操作';
  3911. }
  3912. } catch (err) {
  3913. this.log(err);
  3914. // ctx.session.postError = err.toString();
  3915. // ctx.redirect(ctx.request.header.referer);
  3916. ctx.body = {
  3917. err: 1,
  3918. // url: ctx.request.header.referer,
  3919. msg: err,
  3920. };
  3921. }
  3922. }
  3923. /**
  3924. * 上传附件
  3925. * @param {*} ctx 上下文
  3926. */
  3927. async uploadPlanFile(ctx) {
  3928. let stream;
  3929. try {
  3930. const auditConst = audit.changePlan;
  3931. // this._checkAdvanceFileCanModify(ctx);
  3932. const parts = this.ctx.multipart({
  3933. autoFields: true,
  3934. });
  3935. const files = [];
  3936. const create_time = Date.parse(new Date()) / 1000;
  3937. let idx = 0;
  3938. const extra_upload = ctx.change.status === auditConst.status.checked;
  3939. while ((stream = await parts()) !== undefined) {
  3940. if (!stream.filename) {
  3941. // 如果没有传入直接返回
  3942. return;
  3943. }
  3944. const fileInfo = path.parse(stream.filename);
  3945. const filepath = `app/public/upload/${this.ctx.tender.id.toString()}/change_plan/fujian_${create_time + idx.toString() + fileInfo.ext}`;
  3946. // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, 'app', filepath));
  3947. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  3948. files.push({ filepath, name: stream.filename, ext: fileInfo.ext });
  3949. ++idx;
  3950. stream && (await sendToWormhole(stream));
  3951. }
  3952. const in_time = new Date();
  3953. const payload = files.map(file => {
  3954. let idx;
  3955. if (Array.isArray(parts.field.name)) {
  3956. idx = parts.field.name.findIndex(name => name === file.name);
  3957. } else {
  3958. idx = 'isString';
  3959. }
  3960. const newFile = {
  3961. tid: ctx.tender.id,
  3962. cpid: ctx.change.id,
  3963. uid: ctx.session.sessionUser.accountId,
  3964. filename: file.name,
  3965. fileext: file.ext,
  3966. filesize: ctx.helper.bytesToSize(idx === 'isString' ? parts.field.size : parts.field.size[idx]),
  3967. filepath: file.filepath,
  3968. upload_time: in_time,
  3969. extra_upload,
  3970. };
  3971. return newFile;
  3972. });
  3973. // 执行文件信息写入数据库
  3974. await ctx.service.changePlanAtt.saveFileMsgToDb(payload);
  3975. // 将最新的当前标段的所有文件信息返回
  3976. const data = await ctx.service.changePlanAtt.getAllChangePlanAtt(ctx.tender.id, ctx.change.id);
  3977. ctx.body = { err: 0, msg: '', data };
  3978. } catch (err) {
  3979. stream && (await sendToWormhole(stream));
  3980. this.log(err);
  3981. ctx.body = { err: 1, msg: err.toString(), data: null };
  3982. }
  3983. }
  3984. /**
  3985. * 删除附件
  3986. * @param {Ojbect} ctx 上下文
  3987. */
  3988. async deletePlanFile(ctx) {
  3989. try {
  3990. const { id } = JSON.parse(ctx.request.body.data);
  3991. const fileInfo = await ctx.service.changePlanAtt.getDataById(id);
  3992. if (fileInfo || Object.keys(fileInfo).length) {
  3993. // 先删除文件
  3994. // await fs.unlinkSync(path.resolve(this.app.baseDir, './app', fileInfo.filepath));
  3995. await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
  3996. // 再删除数据库
  3997. await ctx.service.changePlanAtt.delete(id);
  3998. } else {
  3999. throw '不存在该文件';
  4000. }
  4001. const data = await ctx.service.changePlanAtt.getAllChangePlanAtt(ctx.tender.id, ctx.change.id);
  4002. ctx.body = { err: 0, msg: '请求成功', data };
  4003. } catch (err) {
  4004. this.log(err);
  4005. ctx.body = { err: 1, msg: err.toString(), data: null };
  4006. }
  4007. }
  4008. /**
  4009. * 下载附件
  4010. * @param {Object} ctx - egg全局变量
  4011. * @return {void}
  4012. */
  4013. async downloadPlanFile(ctx) {
  4014. const id = ctx.params.fid;
  4015. if (id) {
  4016. try {
  4017. const fileInfo = await ctx.service.changePlanAtt.getDataById(id);
  4018. if (fileInfo !== undefined && fileInfo !== '') {
  4019. // const fileName = path.join(__dirname, '../', fileInfo.filepath);
  4020. // 解决中文无法下载问题
  4021. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  4022. let disposition = '';
  4023. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  4024. disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename);
  4025. } else if (userAgent.indexOf('firefox') >= 0) {
  4026. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename) + '"';
  4027. } else {
  4028. /* safari等其他非主流浏览器只能自求多福了 */
  4029. disposition = 'attachment; filename=' + new Buffer(fileInfo.filename).toString('binary');
  4030. }
  4031. ctx.response.set({
  4032. 'Content-Type': 'application/octet-stream',
  4033. 'Content-Disposition': disposition,
  4034. 'Content-Length': fileInfo.filesize,
  4035. });
  4036. // ctx.body = await fs.createReadStream(fileName);
  4037. ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
  4038. } else {
  4039. throw '不存在该文件';
  4040. }
  4041. } catch (err) {
  4042. this.log(err);
  4043. this.setMessage(err.toString(), this.messageType.ERROR);
  4044. }
  4045. }
  4046. }
  4047. async planInformationSave(ctx) {
  4048. try {
  4049. const data = JSON.parse(ctx.request.body.data);
  4050. if (data.name === 'code') {
  4051. const info = await ctx.service.changePlan.isRepeat(ctx.change.id, data.val, ctx.tender.id);
  4052. if (info) {
  4053. throw '该编号已存在';
  4054. }
  4055. }
  4056. const result = await ctx.service.changePlan.saveInfo(ctx.change.id, data);
  4057. if (!result) {
  4058. throw '修改失败';
  4059. }
  4060. ctx.body = { err: 0, msg: '请求成功', data: null };
  4061. } catch (err) {
  4062. this.log(err);
  4063. ctx.body = { err: 1, msg: err.toString(), data: null };
  4064. }
  4065. }
  4066. /**
  4067. * 上报和重新上报
  4068. * @param ctx
  4069. * @return {Promise<void>}
  4070. */
  4071. async startPlanAudit(ctx) {
  4072. try {
  4073. const auditConst = audit.changePlan;
  4074. // 检查权限等
  4075. if (!ctx.change) {
  4076. throw '数据错误';
  4077. }
  4078. if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
  4079. throw '您无权上报该变更方案数据';
  4080. }
  4081. if (ctx.change.status === auditConst.status.checking || ctx.change.status === auditConst.status.checked) {
  4082. throw '该变更方案数据当前无法上报';
  4083. }
  4084. await ctx.service.changePlanAudit.start(ctx.change.id, ctx.change.times);
  4085. ctx.redirect(ctx.request.header.referer);
  4086. } catch (err) {
  4087. this.log(err);
  4088. ctx.session.postError = err.toString();
  4089. ctx.redirect(ctx.request.header.referer);
  4090. }
  4091. }
  4092. /**
  4093. * 审批
  4094. * @param ctx
  4095. * @return {Promise<void>}
  4096. */
  4097. async checkPlanAudit(ctx) {
  4098. try {
  4099. const auditConst = audit.changePlan;
  4100. if (!ctx.change || ctx.change.status !== auditConst.status.checking) {
  4101. throw '当前变更方案数据有误';
  4102. }
  4103. if (ctx.change.curAuditorIds.length === 0 || ctx.change.curAuditorIds.indexOf(ctx.session.sessionUser.accountId) === -1) {
  4104. throw '您无权进行该操作';
  4105. }
  4106. const data = {
  4107. checkType: parseInt(ctx.request.body.checkType),
  4108. opinion: ctx.request.body.opinion,
  4109. };
  4110. if (!data.checkType || isNaN(data.checkType)) {
  4111. throw '提交数据错误';
  4112. }
  4113. // if (data.checkType === auditConst.status.checkNo) {
  4114. // if (!data.checkType || isNaN(data.checkType)) {
  4115. // throw '提交数据错误';
  4116. // }
  4117. // }
  4118. await ctx.service.changePlanAudit.check(ctx.change.id, data, ctx.change.times);
  4119. ctx.redirect(ctx.request.header.referer);
  4120. } catch (err) {
  4121. this.log(err);
  4122. console.log(err);
  4123. ctx.session.postError = err.toString();
  4124. ctx.redirect(ctx.request.header.referer);
  4125. }
  4126. }
  4127. /**
  4128. * 变更清单 - 操作 (Ajax)
  4129. * @param ctx
  4130. * @return {Promise<void>}
  4131. */
  4132. async savePlanListsData(ctx) {
  4133. try {
  4134. const data = JSON.parse(ctx.request.body.data);
  4135. const responseData = {
  4136. err: 0,
  4137. msg: '',
  4138. data: {},
  4139. };
  4140. switch (data.type) {
  4141. case 'add':
  4142. responseData.data = await ctx.service.changePlanList.add(data.updateData);
  4143. break;
  4144. case 'batchadd':
  4145. responseData.data = await ctx.service.changePlanList.batchAdd(data);
  4146. break;
  4147. case 'del':
  4148. await ctx.service.changePlanList.del(data.ids);
  4149. // 取所有工料表
  4150. responseData.data = await ctx.service.changePlanList.getList(ctx.change.id);
  4151. break;
  4152. case 'update':
  4153. // if (data.updateData.code === '' || data.updateData.code === null) {
  4154. // throw '请先输入编号';
  4155. // }
  4156. await ctx.service.changePlanList.save(data.updateData);
  4157. break;
  4158. case 'paste':
  4159. await ctx.service.changePlanList.saveDatas(data.updateData);
  4160. // 取所有工料表
  4161. responseData.data = await ctx.service.changePlanList.getList(ctx.change.id);
  4162. if (ctx.change.status === audit.changePlan.status.checking || ctx.change.status === audit.changePlan.status.checked) {
  4163. const listAudits = await ctx.service.changePlanAudit.getAuditGroupByList(ctx.change.id, ctx.change.times);
  4164. for (const cl of responseData.data) {
  4165. const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
  4166. // 清单表页赋值
  4167. for (const [index, au] of listAudits.entries()) {
  4168. cl['audit_amount_' + au.aid] = ctx.change.shenpiPower && au.aid === ctx.session.sessionUser.accountId ? cl.spamount : (audit_amount[index] ? parseFloat(audit_amount[index]) : null);
  4169. }
  4170. }
  4171. }
  4172. break;
  4173. case 'list_rule':
  4174. const result = await ctx.service.tender.saveTenderData(ctx.tender.id, { c_plan_list_rule: data.postData });
  4175. if (!result) {
  4176. throw '修改失败';
  4177. }
  4178. break;
  4179. case 'paste_amount_rows':
  4180. await ctx.service.changePlanList.saveDatas(data.updateData);
  4181. const changeList = await ctx.service.changePlanList.getList(ctx.change.id);
  4182. const listAudits = await ctx.service.changePlanAudit.getAuditGroupByList(ctx.change.id, ctx.change.times);
  4183. for (const cl of changeList) {
  4184. // 清单表页赋值
  4185. const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
  4186. // 清单表页赋值
  4187. for (const [index, au] of listAudits.entries()) {
  4188. cl['audit_amount_' + au.aid] = ctx.change.shenpiPower && au.aid === ctx.session.sessionUser.accountId ? cl.spamount : (audit_amount[index] ? parseFloat(audit_amount[index]) : null);
  4189. }
  4190. }
  4191. // 取所有工料表
  4192. responseData.data = changeList;
  4193. break;
  4194. // case 'update_tp':
  4195. // await ctx.service.changePlan.saveInfo(ctx.change.id, { name: 'total_price', val: data.updateData });
  4196. // break;
  4197. default: throw '参数有误';
  4198. }
  4199. ctx.body = responseData;
  4200. } catch (err) {
  4201. this.log(err);
  4202. ctx.body = { err: 1, msg: err.toString(), data: null };
  4203. }
  4204. }
  4205. /**
  4206. * 撤回审批
  4207. * @param ctx
  4208. * @return {Promise<void>}
  4209. */
  4210. async checkPlanAuditCancel(ctx) {
  4211. try {
  4212. if (!ctx.change.cancancel) throw '您无权进行该操作';
  4213. await ctx.service.changePlanAudit.checkCancel(ctx.change);
  4214. ctx.body = { err: 0, url: ctx.request.header.referer, msg: '' };
  4215. } catch (err) {
  4216. this.log(err);
  4217. ctx.body = { err: 1, msg: err };
  4218. }
  4219. }
  4220. /**
  4221. * 变更申请重新审批
  4222. * @param {Object} ctx - egg全局变量
  4223. * @return {void}
  4224. */
  4225. async checkPlanAgain(ctx) {
  4226. try {
  4227. // 获取终审
  4228. // const auditInfo = await this.ctx.service.changePlanAudit.getAuditorByStatus(ctx.change.id, audit.changePlan.status.checked);
  4229. if (ctx.change.status !== audit.changePlan.status.checked) {
  4230. throw '您无权进行该操作';
  4231. }
  4232. // 判断是否被变更申请调用了,是则无法发起修订
  4233. const projectInfo = await ctx.service.change.getDataByCondition({ tid: ctx.tender.id, plan_code: ctx.change.code });
  4234. if (projectInfo) {
  4235. throw '该变更方案已被变更令调用,无法发起重新审批';
  4236. }
  4237. if (ctx.session.sessionUser.loginStatus === 0) {
  4238. const code = ctx.request.body.code;
  4239. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  4240. if (!pa.auth_mobile) {
  4241. throw '未绑定手机号';
  4242. }
  4243. const cacheKey = 'smsCode:' + ctx.session.sessionUser.accountId;
  4244. const cacheCode = await app.redis.get(cacheKey);
  4245. // console.log(cacheCode);
  4246. if (cacheCode === null || code === undefined || cacheCode !== (code + pa.auth_mobile)) {
  4247. throw '验证码不正确!';
  4248. }
  4249. }
  4250. if ((ctx.change.finalAuditorIds.indexOf(ctx.session.sessionUser.accountId) >= 0) && ctx.change.status === audit.changePlan.status.checked) {
  4251. // 重新审批
  4252. const result = await ctx.service.changePlanAudit.checkAgain(ctx.change);
  4253. if (!result) {
  4254. throw '重新审批失败';
  4255. }
  4256. } else {
  4257. throw '您无权进行该操作';
  4258. }
  4259. // ctx.redirect('/tender/' + changeData.tid + '/change/' + changeData.cid + '/info');
  4260. ctx.body = {
  4261. err: 0,
  4262. url: ctx.request.header.referer,
  4263. msg: '',
  4264. };
  4265. } catch (err) {
  4266. console.log(err);
  4267. // ctx.redirect(ctx.request.header.referer);
  4268. ctx.body = {
  4269. err: 1,
  4270. // url: ctx.request.header.referer,
  4271. msg: err,
  4272. };
  4273. }
  4274. }
  4275. /**
  4276. * 变更申请修订重新上报
  4277. * @param {Object} ctx - egg全局变量
  4278. * @return {void}
  4279. */
  4280. async checkPlanRevise(ctx) {
  4281. try {
  4282. if (ctx.change.status !== audit.changePlan.status.checked || ctx.session.sessionUser.accountId !== ctx.change.uid) {
  4283. throw '您无权进行该操作';
  4284. }
  4285. // 判断是否被变更申请调用了,是则无法发起修订
  4286. const projectInfo = await ctx.service.change.getDataByCondition({ tid: ctx.tender.id, plan_code: ctx.change.code });
  4287. if (projectInfo) {
  4288. throw '该变更方案已被变更令调用,无法发起修订';
  4289. }
  4290. if (ctx.session.sessionUser.loginStatus === 0) {
  4291. const code = ctx.request.body.code;
  4292. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  4293. if (!pa.auth_mobile) {
  4294. throw '未绑定手机号';
  4295. }
  4296. const cacheKey = 'smsCode:' + ctx.session.sessionUser.accountId;
  4297. const cacheCode = await app.redis.get(cacheKey);
  4298. // console.log(cacheCode);
  4299. if (cacheCode === null || code === undefined || cacheCode !== (code + pa.auth_mobile)) {
  4300. throw '验证码不正确!';
  4301. }
  4302. }
  4303. // 重新审批
  4304. const result = await ctx.service.changePlanAudit.checkRevise(ctx.change);
  4305. if (!result) {
  4306. throw '修订发起失败';
  4307. }
  4308. // ctx.redirect('/tender/' + changeData.tid + '/change/' + changeData.cid + '/info');
  4309. ctx.body = {
  4310. err: 0,
  4311. url: ctx.request.header.referer,
  4312. msg: '',
  4313. };
  4314. } catch (err) {
  4315. console.log(err);
  4316. // ctx.redirect(ctx.request.header.referer);
  4317. ctx.body = {
  4318. err: 1,
  4319. // url: ctx.request.header.referer,
  4320. msg: err,
  4321. };
  4322. }
  4323. }
  4324. /**
  4325. * 变更令撤销修订
  4326. * @param {Object} ctx - egg全局变量
  4327. * @return {void}
  4328. */
  4329. async cancelPlanRevise(ctx) {
  4330. try {
  4331. if (!(ctx.change.status === audit.changePlan.status.revise && (ctx.session.sessionUser.accountId === ctx.change.uid || ctx.session.sessionUser.accountId === ctx.session.sessionUser.is_admin))) {
  4332. throw '您无权进行该操作';
  4333. }
  4334. // 重新审批
  4335. const result = await ctx.service.changePlanAudit.cancelRevise(ctx.change);
  4336. if (!result) {
  4337. throw '撤销修订失败';
  4338. }
  4339. ctx.body = {
  4340. err: 0,
  4341. url: ctx.request.header.referer,
  4342. msg: '',
  4343. };
  4344. } catch (err) {
  4345. console.log(err);
  4346. ctx.body = {
  4347. err: 1,
  4348. msg: err,
  4349. };
  4350. }
  4351. }
  4352. }
  4353. return ChangeController;
  4354. };