change_controller.js 222 KB

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