change_controller.js 221 KB

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