12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date 2018/8/14
- * @version
- */
- const stdDataAddType = {
- withParent: 1,
- child: 2,
- next: 3,
- };
- const moment = require('moment');
- const sendToWormhole = require('stream-wormhole');
- const fs = require('fs');
- const path = require('path');
- const audit = require('../const/audit');
- const codeRuleConst = require('../const/code_rule');
- const changeConst = require('../const/change');
- const accountGroup = require('../const/account_group').group;
- const shenpiConst = require('../const/shenpi');
- const tenderMenu = require('../../config/menu').tenderMenu;
- const measureType = require('../const/tender').measureType;
- const spreadConst = require('../const/spread');
- const stdConst = require('../const/standard');
- // const tenderMenu = require('../../config/menu').tenderMenu;
- module.exports = app => {
- class ChangeController extends app.BaseController {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- ctx.showProject = true;
- ctx.showTender = true;
- ctx.showTitle = true;
- }
- async _filterChanges(ctx, status = 0) {
- const tenderId = ctx.params.id;
- ctx.session.sessionUser.tenderId = tenderId;
- const tender = await this.service.tender.getDataById(tenderId);
- // const tenderList = await this.service.tender.getList();
- const page = ctx.page;
- const pageSize = ctx.pageSize;
- const sorts = ctx.query.sort ? ctx.query.sort : 0;
- const orders = ctx.query.order ? ctx.query.order : 0;
- const state = ctx.session.sessionProject.page_show.openChangeState && ctx.query.state ? parseInt(ctx.query.state) : 0;
- const changes = await ctx.service.change.getListByStatus(tender.id, status, 1, sorts, orders, state);
- const total = await ctx.service.change.getCountByStatus(tender.id, status, state);
- let page_total = 0;
- const tp = await ctx.service.change.getTp(tender.id, status);
- if (changes !== null) {
- let i = 0;
- for (const c of changes) {
- page_total = ctx.helper.add(page_total, c.total_price);
- const status = c.status === audit.flow.status.uncheck ? 0 : 1;
- // 根据审批人对当前变更令的状态取不同的展示方式。
- let changeAudit = '';
- let auditStatus = 0;
- switch (c.status) {
- case 1:
- auditStatus = 1;
- break;
- case 2:
- changeAudit = await ctx.service.changeAudit.getLastUser(c.cid, c.times, status);
- auditStatus = changeAudit.uid === ctx.session.sessionUser.accountId ? 1 : 0;
- break;
- case 3:
- case 4:
- auditStatus = 0;
- changeAudit = await ctx.service.changeAudit.getLastUser(c.cid, c.times, status);
- break;
- case 5:
- changeAudit = await ctx.service.changeAudit.getLastUser(c.cid, c.times - 1, status);
- auditStatus = c.uid === ctx.session.sessionUser.accountId ? 1 : 0;
- const back_changeUsedData = await ctx.service.stageChange.getFinalUsedData(ctx.tender.id, c.cid);
- c.stageChangeNum = this.ctx.helper.sum(back_changeUsedData.map(x => { return Math.abs(x.qty); }));
- c.isSettle = await ctx.service.changeSettleList.isSettle(c.cid);
- break;
- case 6:
- changeAudit = await ctx.service.changeAudit.getLastBackUser(c.cid, c.times);
- const checkingAudit = await ctx.service.changeAudit.getLastUser(c.cid, c.times, status);
- auditStatus = checkingAudit.uid === ctx.session.sessionUser.accountId ? 1 : 0;
- break;
- case 9:
- auditStatus = c.uid === ctx.session.sessionUser.accountId ? 9 : 0;
- const changeUsedData = await ctx.service.stageChange.getFinalUsedData(ctx.tender.id, c.cid);
- c.stageChangeNum = this.ctx.helper.sum(changeUsedData.map(x => { return Math.abs(x.qty); }));
- c.isSettle = await ctx.service.changeSettleList.isSettle(c.cid);
- break;
- default:
- break;
- }
- changes[i].changeAudit = changeAudit;
- changes[i].auditStatus = auditStatus;
- i++;
- }
- }
- // 分页相关
- const pageInfo = {
- page,
- pageSizeSelect: 1,
- pageSize,
- total_num: total,
- total: Math.ceil(total / pageSize),
- queryData: JSON.stringify(ctx.urlInfo.query),
- };
- const filter = JSON.parse(JSON.stringify(audit.filter));
- filter.count = [];
- 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);
- 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);
- 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);
- 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);
- 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);
- const codeRule = tender.c_rule ? JSON.parse(tender.c_rule) : [];
- for (const rule of codeRule) {
- switch (rule.rule_type) {
- case codeRuleConst.measure.ruleType.dealCode:
- rule.preview = ctx.tender.info.deal_info.dealCode;
- break;
- case codeRuleConst.measure.ruleType.tenderName:
- rule.preview = tender.name;
- break;
- case codeRuleConst.measure.ruleType.inDate:
- rule.preview = moment().format('YYYY');
- break;
- case codeRuleConst.measure.ruleType.text:
- rule.preview = rule.text;
- break;
- case codeRuleConst.measure.ruleType.addNo:
- const s = '0000000000';
- rule.preview = s.substr(s.length - rule.format);
- break;
- default: break;
- }
- }
- const changePlanList = await ctx.service.changePlan.getAllDataByCondition({ where: { tid: tender.id, status: audit.changePlan.status.checked } });
- const allPlanCodes = await ctx.service.change.getAllDataByCondition({
- columns: ['plan_code'],
- where: {
- tid: tender.id,
- },
- });
- const apLists = allPlanCodes.length > 0 ? ctx.app._.uniq(ctx.app._.map(allPlanCodes, 'plan_code')) : [];
- const renderData = {
- uid: ctx.session.sessionUser.accountId,
- moment,
- tender,
- // tenderList,
- pageInfo,
- changes,
- page_total,
- tp,
- filter,
- status,
- codeRule,
- dealCode: ctx.tender.info.deal_info.dealCode,
- auditConst: audit.flow,
- changeConst,
- state,
- ruleType: codeRuleConst.ruleType.change,
- ruleConst: codeRuleConst.measure,
- tenderMenu: this.menu.tenderMenu,
- preUrl: '/tender/' + tenderId,
- tpUnit: ctx.tender.info.decimal.tp,
- changePlanList,
- apLists,
- };
- if (ctx.session.sessionProject.page_show.openChangeState) {
- // 工程变更类别读取
- const projectData = await ctx.service.project.getDataById(ctx.session.sessionProject.id);
- const fun_set = await ctx.service.project.getFunSet(projectData.fun_set);
- const changeState = fun_set.change_state;
- for (const cs of changeState) {
- cs.count = await ctx.service.change.getCountByStatus(tender.id, status, cs.order);
- }
- renderData.changeState = changeState;
- }
- await this.layout('change/index.ejs', renderData, 'change/modal.ejs');
- }
- /**
- * 变更管理 页面 (Get)
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async index(ctx) {
- try {
- await this._filterChanges(ctx);
- } catch (err) {
- this.log(err);
- ctx.redirect('/dashboard');
- }
- }
- /**
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async newCode(ctx) {
- const responseData = {
- err: 0,
- msg: '',
- data: '',
- };
- try {
- const tenderId = ctx.params.id;
- if (!tenderId) {
- throw '当前未打开标段';
- }
- const tenderData = await ctx.service.tender.getDataById(tenderId);
- const data = JSON.parse(ctx.request.body.data);
- let cCodeRule;
- let cConnector;
- let changeCount = 0;
- if (data && data.type) {
- const c_code_rules = tenderData.c_code_rules !== null ? JSON.parse(tenderData.c_code_rules) : null;
- cCodeRule = c_code_rules && c_code_rules[data.type + '_rule'] ? c_code_rules[data.type + '_rule'] : [];
- cConnector = c_code_rules && c_code_rules[data.type + '_connector'] ? c_code_rules[data.type + '_connector'] : null;
- if (data.type === codeRuleConst.ruleField[codeRuleConst.ruleType.apply]) {
- changeCount = await ctx.service.changeApply.count({ tid: tenderId });
- } else {
- changeCount = await ctx.service.changeProject.count({ tid: tenderId, type: codeRuleConst.ruleType[data.type] });
- }
- } else {
- cCodeRule = tenderData.c_rule !== null ? JSON.parse(tenderData.c_rule) : [];
- cConnector = tenderData.c_connector;
- changeCount = await ctx.service.change.count({ tid: tenderId });
- }
- const code = [];
- for (const rule of cCodeRule) {
- switch (rule.rule_type) {
- case codeRuleConst.measure.ruleType.dealCode:
- code.push(ctx.tender.info.deal_info.dealCode);
- break;
- case codeRuleConst.measure.ruleType.tenderName:
- code.push(tenderData.name);
- break;
- case codeRuleConst.measure.ruleType.text:
- code.push(rule.text);
- break;
- case codeRuleConst.measure.ruleType.inDate:
- code.push(moment().format('YYYY'));
- break;
- case codeRuleConst.measure.ruleType.addNo:
- let s = '0000000000';
- const count = rule.start + changeCount;
- s = s + count;
- code.push(s.substr(s.length - rule.format));
- break;
- default: break;
- }
- }
- responseData.data = code.join(cConnector !== null && cConnector !== 3 ? codeRuleConst.measure.connectorString[cConnector] : '');
- } catch (err) {
- responseData.err = 1;
- responseData.msg = err;
- }
- ctx.body = responseData;
- }
- /**
- * 新增变更 (Post)
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async add(ctx) {
- try {
- const tenderId = ctx.params.id;
- if (!tenderId) {
- throw '当前未打开标段';
- }
- const data = JSON.parse(ctx.request.body.data);
- if (!data.code || data.code === '') {
- throw '变更令号不能为空';
- }
- const projectData = await ctx.service.project.getDataById(ctx.session.sessionProject.id);
- const fun_set = await ctx.service.project.getFunSet(projectData.fun_set);
- const stateInfo = ctx.helper._.find(fun_set.change_state, { order: 3 });
- const change = await ctx.service.change.add(tenderId, ctx.session.sessionUser.accountId, data.code, data.plan_code, data.name, stateInfo.value);
- ctx.body = { err: 0, msg: '', data: change };
- } catch (err) {
- this.log(err);
- ctx.body = { err: 1, msg: err.toString() };
- }
- }
- /**
- * 变更管理 状态筛选 页面 (Get)
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async status(ctx) {
- try {
- const status = parseInt(ctx.params.status);
- await this._filterChanges(ctx, status);
- } catch (err) {
- this.logger.error(err);
- ctx.redirect('/tender/' + ctx.params.id + '/change');
- }
- }
- /**
- * 变更信息 页面 (Get)
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async info(ctx) {
- try {
- const whiteList = this.ctx.app.config.multipart.whitelist;
- const tenderid = ctx.params.id !== undefined ? ctx.params.id : ctx.session.sessionUser.tenderId;
- ctx.session.sessionUser.tenderId = tenderid;
- const tender = await ctx.service.tender.getDataById(tenderid);
- const change = await ctx.service.change.getDataByCondition({ cid: ctx.params.cid });
- // 后台判断当前人查看info状态
- const auditStatus = await ctx.service.changeAudit.getStatusByChange(change);
- // 获取附件列表
- const attList = await ctx.service.changeAtt.getChangeAttachment(ctx.params.cid);
- // 获取其他变更令数据
- const othersChange = await ctx.service.change.getOthersChange(ctx.params.id, ctx.params.cid);
- // 根据auditStatus获取审批人列表
- const auditList = await ctx.service.changeAudit.getListByStatus(change, auditStatus);
- // 获取已选清单
- let changeList = await ctx.service.changeAuditList.getAllDataByCondition({ where: { cid: ctx.params.cid } });
- // 获取用户人验证手机号
- const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
- const auth_mobile = pa.auth_mobile;
- const renderData = {
- uid: ctx.session.sessionUser.accountId,
- tender,
- change,
- othersChange,
- changeConst,
- auditStatus,
- auditConst: audit.flow,
- ledgerConsts: audit.ledger.status,
- attList,
- whiteList,
- auditList,
- changeList,
- tpUnit: change.tp_decimal !== null ? change.tp_decimal : ctx.tender.info.decimal.tp,
- upUnit: change.up_decimal !== null ? change.up_decimal : ctx.tender.info.decimal.up,
- authMobile: auth_mobile,
- shenpiConst,
- };
- // 根据auditStatus状态获取的不同的数据
- if (auditStatus === 1 || auditStatus === 2 || auditStatus === 9) {
- renderData.changeUnits = changeConst.units;
- renderData.precision = ctx.tender.info.precision;
- // renderData.accountGroup = accountGroup;
- // 获取所有项目参与者
- const accountList = await ctx.service.projectAccount.getAllDataByCondition({
- where: { project_id: ctx.session.sessionProject.id, enable: 1 },
- columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group'],
- });
- renderData.accountList = accountList;
- const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
- renderData.accountGroup = unitList.map(item => {
- const groupList = accountList.filter(item1 => item1.company === item.name);
- return { groupName: item.name, groupList };
- });
- // 重新上报获取审批流程
- if (auditStatus === 2 || auditStatus === 9) {
- const auditList2 = await ctx.service.changeAudit.getListByBack(change.cid, change.times);
- // 展示页右侧审批流程列表
- const auditList3 = [];
- for (let time = 1; time <= change.times - 1; time++) {
- const auditTimeList = [];
- let max_sort = 1;
- for (const al of auditList2) {
- if (al.times === time) {
- auditTimeList.push(al);
- if (al.usite > max_sort) {
- max_sort = al.usite;
- }
- }
- }
- for (const i in auditTimeList) {
- auditTimeList[i].max_sort = max_sort;
- }
- auditList3.push(auditTimeList);
- }
- renderData.auditList3 = auditList3;
- }
- // 根据清单获取提交数据和计算总金额
- const changeListData = [];
- const changeWhiteListData = [];
- let ototalCost = 0;
- let ctotalCost = 0;
- for (const cl of changeList) {
- const cLArray = [
- cl.code,
- cl.name,
- cl.bwmx,
- cl.unit,
- cl.unit_price,
- cl.oamount,
- cl.camount,
- cl.detail,
- cl.lid,
- cl.xmj_code,
- cl.xmj_jldy,
- cl.gcl_id,
- ];
- ototalCost += cl.unit_price === null ? 0 : ctx.helper.mul(cl.unit_price, cl.oamount, ctx.tender.info.decimal.tp);
- ctotalCost += cl.unit_price === null ? 0 : ctx.helper.mul(cl.unit_price, cl.camount, ctx.tender.info.decimal.tp);
- if (cl.lid !== '0') {
- changeListData.push(cLArray.join('*;*'));
- } else {
- changeWhiteListData.push(cLArray.join('*;*'));
- }
- }
- renderData.changeListData = changeListData.join('^_^');
- renderData.changeWhiteListData = changeWhiteListData.join('^_^');
- renderData.ototalCost = ototalCost;
- renderData.ctotalCost = ctotalCost;
- // 获取公司列表
- const companyList = await ctx.service.changeCompany.getAllDataByCondition({ where: { tid: tenderid } });
- renderData.companyList = companyList;
- } else if (auditStatus === 3 || auditStatus === 4 || auditStatus === 5 || auditStatus === 7) {
- // 展示页左侧审批流程列表和清单审批列表数据
- const times = change.status === audit.flow.status.back ?
- change.times - 1 : change.times;
- const auditList2 = await ctx.service.changeAudit.getListGroupByTimes(change.cid, times);
- // 展示页右侧审批流程列表
- const auditList3 = [];
- for (let time = 1; time <= times; time++) {
- const auditTimeList = [];
- let max_sort = 1;
- for (const al of auditList) {
- if (al.times === time) {
- auditTimeList.push(al);
- if (al.usite > max_sort) {
- max_sort = al.usite;
- }
- }
- }
- for (const i in auditTimeList) {
- auditTimeList[i].max_sort = max_sort;
- }
- auditList3.push(auditTimeList);
- }
- renderData.auditList3 = auditList3;
- changeList = JSON.parse(JSON.stringify(changeList.sort())).sort().sort();
- renderData.changeList = changeList;
- let ototalCost = 0;
- let ctotalCost = 0;
- let stotalCost = 0;
- const auditTotalCost = [];
- for (const cl of changeList) {
- // ototalCost += cl.unit_price === null ? 0 : parseFloat(ctx.helper.roundNum(ctx.helper.accMul(cl.unit_price, cl.oamount), renderData.tpUnit));
- ototalCost += cl.unit_price === null ? 0 : ctx.helper.mul(cl.unit_price, cl.oamount, renderData.tpUnit);
- // ctotalCost += cl.unit_price === null ? 0 : parseFloat(ctx.helper.roundNum(ctx.helper.accMul(cl.unit_price, cl.camount), renderData.tpUnit));
- ctotalCost += cl.unit_price === null ? 0 : ctx.helper.mul(cl.unit_price, cl.camount, renderData.tpUnit);
- // stotalCost += cl.samount !== '' && cl.unit_price !== null ? parseFloat(ctx.helper.roundNum(ctx.helper.accMul(cl.unit_price, cl.samount), renderData.tpUnit)) : 0;
- stotalCost += cl.samount !== '' && cl.unit_price !== null ? ctx.helper.mul(cl.unit_price, cl.samount, renderData.tpUnit) : 0;
- const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
- auditTotalCost.push(audit_amount);
- }
- renderData.ototalCost = ototalCost;
- renderData.ctotalCost = ctotalCost;
- renderData.stotalCost = stotalCost;
- // 清单表页赋值
- for (const [index, au] of auditList2.entries()) {
- if (au.usite !== 0) {
- au.list_amount = [];
- au.totalCost = 0;
- for (const [auindex, at] of auditTotalCost.entries()) {
- au.list_amount.push(at[index - 1]);
- // au.totalCost += at[index - 1] !== undefined && changeList[auindex].unit_price !== null ? parseFloat(ctx.helper.roundNum(ctx.helper.accMul(changeList[auindex].unit_price, at[index - 1]), renderData.tpUnit)) : 0;
- au.totalCost += at[index - 1] !== undefined && changeList[auindex].unit_price !== null ? ctx.helper.mul(changeList[auindex].unit_price, at[index - 1], renderData.tpUnit) : 0;
- }
- }
- }
- renderData.auditList2 = auditList2;
- } else if (auditStatus === 6) {
- // 展示页左侧审批流程列表和清单审批列表数据
- const auditList2 = await ctx.service.changeAudit.getListGroupByTimes(change.cid, change.times);
- renderData.auditList2 = auditList2;
- const auditList3 = await ctx.service.changeAudit.getListOrderByTimes(change.cid, change.times);
- for (const i in auditList3) {
- auditList3[i].max_sort = auditList2.length - 1;
- }
- renderData.auditList3 = auditList3;
- // 展示页右侧审批流程列表
- const auditList5 = await ctx.service.changeAudit.getListByBack(change.cid, change.times);
- const auditList4 = [];
- for (let time = 1; time <= change.times; time++) {
- const auditTimeList = [];
- let max_sort = 1;
- for (const al of auditList5) {
- if (al.times === time) {
- auditTimeList.push(al);
- if (al.usite > max_sort) {
- max_sort = al.usite;
- }
- }
- }
- for (const i in auditTimeList) {
- auditTimeList[i].max_sort = max_sort;
- }
- if (auditTimeList.length > 0) {
- auditList4.push(auditTimeList);
- }
- }
- renderData.auditList4 = auditList4;
- changeList = JSON.parse(JSON.stringify(changeList.sort())).sort().sort();
- renderData.changeList = changeList;
- let ototalCost = 0;
- let ctotalCost = 0;
- const auditTotalCost = [];
- const auditUnit = [];
- for (const cl of changeList) {
- // ototalCost += cl.unit_price === null ? 0 : parseFloat(ctx.helper.roundNum(ctx.helper.accMul(cl.unit_price, cl.oamount), renderData.tpUnit));
- ototalCost += cl.unit_price === null ? 0 : ctx.helper.mul(cl.unit_price, cl.oamount, renderData.tpUnit);
- // ctotalCost += cl.unit_price === null ? 0 : parseFloat(ctx.helper.roundNum(ctx.helper.accMul(cl.unit_price, cl.camount), renderData.tpUnit));
- ctotalCost += cl.unit_price === null ? 0 : ctx.helper.mul(cl.unit_price, cl.camount, renderData.tpUnit);
- const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
- auditTotalCost.push(audit_amount);
- }
- renderData.ototalCost = ototalCost;
- renderData.ctotalCost = ctotalCost;
- // 清单表页赋值
- for (const [index, au] of auditList.entries()) {
- if (au.usite !== 0) {
- au.list_amount = [];
- au.totalCost = 0;
- if (au.uid === renderData.uid) {
- for (const [auindex, at] of auditTotalCost.entries()) {
- // if (at[index - 2] !== undefined) {
- // au.list_amount.push(at[index - 2]);
- // au.totalCost += parseFloat(ctx.helper.roundNum(ctx.helper.accMul(changeList[auindex].unit_price, at[index - 2]), renderData.tpUnit));
- // } else if (at[index - 2] === undefined) {
- // au.list_amount.push(changeList[auindex].camount);
- // au.totalCost += parseFloat(ctx.helper.roundNum(ctx.helper.accMul(changeList[auindex].unit_price, changeList[auindex].camount), renderData.tpUnit));
- // }
- au.list_amount.push(changeList[auindex].spamount);
- // au.totalCost += changeList[auindex].unit_price === null ? 0 : parseFloat(ctx.helper.roundNum(ctx.helper.accMul(changeList[auindex].unit_price, changeList[auindex].spamount), renderData.tpUnit));
- au.totalCost += changeList[auindex].unit_price === null ? 0 : ctx.helper.mul(changeList[auindex].unit_price, changeList[auindex].spamount, renderData.tpUnit);
- }
- } else {
- for (const [auindex, at] of auditTotalCost.entries()) {
- au.list_amount.push(at[index - 1]);
- // au.totalCost += at[index - 1] !== undefined && changeList[auindex].unit_price !== null ? parseFloat(ctx.helper.roundNum(ctx.helper.accMul(changeList[auindex].unit_price, at[index - 1]), renderData.tpUnit)) : 0;
- au.totalCost += at[index - 1] !== undefined && changeList[auindex].unit_price !== null ? ctx.helper.mul(changeList[auindex].unit_price, at[index - 1], renderData.tpUnit) : 0;
- }
- }
- }
- }
- }
- renderData.auditList = auditList;
- // 获取是否已存在调用变更令
- const changeUsedData = await ctx.service.stageChange.getFinalUsedData(ctx.tender.id, change.cid);
- renderData.stageChangeNum = this.ctx.helper.sum(changeUsedData.map(x => { return Math.abs(x.qty); }));
- await this.layout('change/info.ejs', renderData, 'change/info_modal.ejs');
- } catch (err) {
- this.log(err);
- ctx.redirect('/tender/' + ctx.params.id + '/change');
- }
- }
- /**
- * 变更信息 新版本页面 (Get)
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async information(ctx) {
- try {
- const whiteList = this.ctx.app.config.multipart.whitelist;
- const tender = await ctx.service.tender.getDataById(ctx.tender.id);
- const change = ctx.change;
- // 后台判断当前人查看info状态
- const auditStatus = await ctx.service.changeAudit.getStatusByChange(change);
- // 获取附件列表
- const attList = await ctx.service.changeAtt.getChangeAttachment(change.cid, 'desc');
- // 获取其他变更令数据
- const othersChange = await ctx.service.change.getOthersChange(ctx.tender.id, change.cid);
- // 根据auditStatus获取审批人列表
- const auditList = await ctx.service.changeAudit.getListByStatus(change, auditStatus);
- // 获取变更方案的清单
- let planList = [];
- let showPlanBtn = false;
- if (ctx.session.sessionProject.page_show.openChangePlan) {
- const planInfo = change.plan_code ? await ctx.service.changePlan.getDataByCondition({ tid: ctx.tender.id, code: change.plan_code }) : null;
- showPlanBtn = change.plan_code !== null && change.plan_code !== '';
- if (planInfo && planInfo.id) {
- planList = await ctx.service.changePlanList.getAllDataByCondition({ where: { cpid: planInfo.id } });
- }
- }
- // 工程变更类别读取
- const projectData = await ctx.service.project.getDataById(ctx.session.sessionProject.id);
- const fun_set = await ctx.service.project.getFunSet(projectData.fun_set);
- // 获取用户人验证手机号
- const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
- const auth_mobile = pa.auth_mobile;
- const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
- if (change && change.status !== audit.flow.status.checked && ctx.session.sessionUser.is_admin) {
- change.auditors2 = await ctx.service.changeAudit.getListGroupByWithoutYB(change.cid, change.times);
- }
- const settleBills = ctx.change.readySettle ? await ctx.service.settleBills.getAllDataByCondition({ where: { settle_id: ctx.change.readySettle.id } }) : [];
- const settlePos = ctx.change.readySettle ? await ctx.service.settlePos.getAllDataByCondition({ where: { settle_id: ctx.change.readySettle.id } }) : [];
- // 判断并是否更新结算清单数据
- await ctx.service.change.checkSettleUpdate(ctx.tender.id, ctx.change.readySettle);
- // 获取清单
- const changeList = await ctx.service.changeAuditList.getList(change.cid);
- // 处理清单数据
- const removeSettleNum = change.status !== audit.flow.status.checked ? await ctx.service.changeSettleList.updateChangeList(change.cid, settleBills, settlePos, changeList) : 0;
- const renderData = {
- tender,
- change,
- othersChange,
- changeConst,
- changeClass: fun_set.change_class,
- changeState: fun_set.change_state,
- auditStatus,
- auditConst: audit.flow,
- ledgerConsts: audit.ledger.status,
- attList,
- whiteList,
- auditList,
- showPlanBtn,
- planList,
- tpUnit: change.tp_decimal ? change.tp_decimal : ctx.tender.info.decimal.tp,
- upUnit: change.up_decimal ? change.up_decimal : ctx.tender.info.decimal.up,
- authMobile: auth_mobile,
- shenpiConst,
- settleStatus: ctx.service.settle.settleStatus,
- unitList,
- jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.information),
- preUrl: '/tender/' + ctx.tender.id + '/change/' + ctx.change.cid + '/information',
- precision: ctx.tender.info.precision,
- settleBills,
- settlePos,
- removeSettleNum,
- };
- // 获取是否已存在调用变更令
- let changeUsedData = await ctx.service.stageChange.getFinalUsedData(ctx.tender.id, change.cid);
- changeUsedData = ctx.helper._.orderBy(ctx.helper._.filter(changeUsedData, function(item) {
- return item.qty !== null;
- }), ['sorder'], ['desc']);
- const useChangeUsedData = [];
- if (changeUsedData.length > 0) { // 防止未创建期时调用
- // const stage = await this.ctx.service.stage.getLastestStage(ctx.tender.id, true);
- for (const cu of changeUsedData) {
- // if (cu.sorder !== 0 ||
- // (cu.sorder === 0 && !(stage.status === audit.stage.status.uncheck || stage.status === audit.stage.status.checkNo))) {
- const index = ctx.helper._.findIndex(useChangeUsedData, { cbid: cu.cbid });
- if (index !== -1) {
- useChangeUsedData[index].qty = ctx.helper.add(useChangeUsedData[index].qty, cu.qty);
- } else {
- useChangeUsedData.push(cu);
- }
- // }
- }
- }
- renderData.changeUsedData = useChangeUsedData;
- renderData.stageChangeNum = this.ctx.helper.sum(changeUsedData.map(x => { return Math.abs(x.qty); }));
- if (auditStatus === 1 || auditStatus === 2 || auditStatus === 9 || ctx.session.sessionUser.is_admin) {
- // 获取所有项目参与者
- const accountList = await ctx.service.projectAccount.getAllDataByCondition({
- where: { project_id: ctx.session.sessionProject.id, enable: 1 },
- columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group'],
- });
- renderData.accountList = accountList;
- renderData.accountGroup = unitList.map(item => {
- const groupList = accountList.filter(item1 => item1.company === item.name);
- return { groupName: item.name, groupList };
- });
- }
- // 根据auditStatus状态获取的不同的数据
- if (auditStatus === 1 || auditStatus === 2 || auditStatus === 9) {
- renderData.changeUnits = changeConst.units;
- // 重新上报获取审批流程
- if (auditStatus === 2 || auditStatus === 9) {
- const auditList2 = await ctx.service.changeAudit.getListByBack(change.cid, change.times);
- // 展示页右侧审批流程列表
- const auditList3 = [];
- for (let time = 1; time <= change.times - 1; time++) {
- const auditTimeList = [];
- let max_sort = 1;
- for (const al of auditList2) {
- if (al.times === time) {
- auditTimeList.push(al);
- if (al.usite > max_sort) {
- max_sort = al.usite;
- }
- }
- }
- for (const i in auditTimeList) {
- auditTimeList[i].max_sort = max_sort;
- }
- auditList3.push(auditTimeList);
- }
- renderData.auditList3 = auditList3;
- }
- // 获取公司列表
- const companyList = await ctx.service.changeCompany.getAllDataByCondition({ where: { tid: ctx.tender.id } });
- renderData.companyList = companyList;
- renderData.changeLedgerList = await ctx.service.changeLedger.getAllDataByCondition({ where: { tender_id: ctx.tender.id } });
- renderData.changePosList = await ctx.service.changePos.getAllDataByCondition({ where: { tid: ctx.tender.id } });
- const stateInfo = ctx.helper._.find(fun_set.change_state, { order: ctx.change.state });
- renderData.deLimit = stateInfo.value;
- if (stateInfo.value !== ctx.change.delimit) {
- // 需要更新所有的计量上限值,除了已调用的值
- const updateList = [];
- for (const cl of changeList) {
- const one = {
- id: cl.id,
- };
- if (useChangeUsedData.length > 0 && ctx.helper._.findIndex(useChangeUsedData, { cbid: cl.id }) !== -1) {
- // 获取比例值
- const uc = ctx.helper._.find(useChangeUsedData, { cbid: cl.id });
- const minLimit = Math.ceil(ctx.helper.mul(ctx.helper.div(uc.qty, cl.camount), 100));
- if (minLimit <= renderData.deLimit) {
- one.delimit = renderData.deLimit;
- cl.delimit = renderData.deLimit;
- }
- } else if (cl.delimit !== renderData.deLimit) {
- one.delimit = renderData.deLimit;
- cl.delimit = renderData.deLimit;
- }
- if (!ctx.helper._.isEqual(one, { id: cl.id })) updateList.push(one);
- }
- console.log(updateList);
- if (updateList.length > 0) await ctx.service.changeAuditList.defaultUpdateRows(updateList);
- await ctx.service.change.defaultUpdate({ delimit: stateInfo.value }, { where: { cid: change.cid } });
- }
- // 判断是否更新变更类别
- if (ctx.helper._.findIndex(fun_set.change_class, { value: change.class, checked: true }) === -1) {
- renderData.change.class = ctx.helper._.find(fun_set.change_class, { checked: true }).value;
- await ctx.service.change.saveInfo({ class: ctx.helper._.find(fun_set.change_class, { checked: true }).value });
- }
- } else if (auditStatus === 3 || auditStatus === 4 || auditStatus === 5 || auditStatus === 7 || auditStatus === 8) {
- // 展示页左侧审批流程列表和清单审批列表数据
- const times = change.status === audit.flow.status.back ?
- change.times - 1 : change.times;
- const auditList2 = await ctx.service.changeAudit.getListGroupByTimes(change.cid, times);
- // 展示页右侧审批流程列表
- const auditList3 = [];
- for (let time = 1; time <= times; time++) {
- const auditTimeList = [];
- let max_sort = 1;
- for (const al of auditList) {
- if (al.times === time) {
- auditTimeList.push(al);
- if (al.usite > max_sort) {
- max_sort = al.usite;
- }
- }
- }
- for (const i in auditTimeList) {
- auditTimeList[i].max_sort = max_sort;
- }
- auditList3.push(auditTimeList);
- }
- renderData.auditList3 = auditList3;
- for (const cl of changeList) {
- const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
- // 清单表页赋值
- for (const [index, au] of auditList2.entries()) {
- if (au.usite !== 0) {
- cl['audit_amount_' + au.uid] = audit_amount[index - 1] !== undefined ? audit_amount[index - 1] : null;
- }
- }
- cl.changed_amount = ctx.helper._.indexOf([audit.flow.status.backnew, audit.flow.status.checking, audit.flow.status.checked], change.status) !== -1 ?
- (audit_amount !== '' ? audit_amount[audit_amount.length - 1] :
- (ctx.helper._.indexOf([audit.flow.status.backnew, audit.flow.status.checking], change.status) !== -1 ? cl.camount : 0)) : cl.camount;
- // cl.changed_amount = ctx.helper.add(cl.oamount ? parseFloat(cl.oamount) : 0, changed_amount ? parseFloat(changed_amount) : 0);
- }
- renderData.auditList2 = auditList2;
- } else if (auditStatus === 6) {
- // 展示页左侧审批流程列表和清单审批列表数据
- const auditList2 = await ctx.service.changeAudit.getListGroupByTimes(change.cid, change.times);
- renderData.auditList2 = auditList2;
- const auditList3 = await ctx.service.changeAudit.getListOrderByTimes(change.cid, change.times);
- for (const i in auditList3) {
- auditList3[i].max_sort = auditList2.length - 1;
- }
- renderData.auditList3 = auditList3;
- // 展示页右侧审批流程列表
- const auditList5 = await ctx.service.changeAudit.getListByBack(change.cid, change.times);
- const auditList4 = [];
- for (let time = 1; time <= change.times; time++) {
- const auditTimeList = [];
- let max_sort = 1;
- for (const al of auditList5) {
- if (al.times === time) {
- auditTimeList.push(al);
- if (al.usite > max_sort) {
- max_sort = al.usite;
- }
- }
- }
- for (const i in auditTimeList) {
- auditTimeList[i].max_sort = max_sort;
- }
- if (auditTimeList.length > 0) {
- auditList4.push(auditTimeList);
- }
- }
- renderData.auditList4 = auditList4;
- for (const cl of changeList) {
- const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
- // 清单表页赋值
- for (const [index, au] of auditList2.entries()) {
- if (au.usite !== 0) {
- cl['audit_amount_' + au.uid] = au.uid === ctx.session.sessionUser.accountId ? cl.spamount : (audit_amount[index - 1] !== undefined ? audit_amount[index - 1] : null);
- }
- }
- cl.changed_amount = ctx.helper._.indexOf([audit.flow.status.backnew, audit.flow.status.checking, audit.flow.status.checked], change.status) !== -1 ?
- (audit_amount !== '' ? audit_amount[audit_amount.length - 1] :
- (ctx.helper._.indexOf([audit.flow.status.backnew, audit.flow.status.checking], change.status) !== -1 ? cl.camount : 0)) : cl.camount;
- // cl.changed_amount = ctx.helper.add(cl.oamount ? parseFloat(cl.oamount) : 0, changed_amount ? parseFloat(changed_amount) : 0);
- }
- renderData.changeLedgerList = await ctx.service.changeLedger.getAllDataByCondition({ where: { tender_id: ctx.tender.id } });
- renderData.changePosList = await ctx.service.changePos.getAllDataByCondition({ where: { tid: ctx.tender.id } });
- }
- renderData.changeList = changeList;
- renderData.auditList = auditList;
- await this.layout('change/information.ejs', renderData, 'change/information_modal.ejs');
- } catch (err) {
- this.log(err);
- ctx.redirect('/tender/' + ctx.params.id + '/change');
- }
- }
- async report(ctx) {
- try {
- const renderData = {
- change: ctx.change,
- tender: ctx.tender,
- jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.report),
- };
- await this.layout('change/report.ejs', renderData);
- } catch (err) {
- this.log(err);
- ctx.redirect('/tender/' + ctx.params.id + '/change');
- }
- }
- /**
- * 变更清单 - 操作 (Ajax)
- * @param ctx
- * @return {Promise<void>}
- */
- async saveListsData(ctx) {
- try {
- const data = JSON.parse(ctx.request.body.data);
- const responseData = {
- err: 0,
- msg: '',
- data: {},
- };
- switch (data.type) {
- case 'add':
- case 'batchadd':
- if (!ctx.session.sessionProject.page_show.openChangeWhiteList) {
- throw '空白清单添加功能未开启。';
- }
- const projectData = await ctx.service.project.getDataById(ctx.session.sessionProject.id);
- const fun_set = await ctx.service.project.getFunSet(projectData.fun_set);
- const stateInfo = ctx.helper._.find(fun_set.change_state, { order: ctx.change.state });
- if (data.type === 'add') {
- responseData.data = await ctx.service.changeAuditList.add(data.postData, stateInfo.value);
- } else {
- responseData.data = await ctx.service.changeAuditList.batchAdd(data, stateInfo.value);
- }
- break;
- case 'del':
- await ctx.service.changeAuditList.del(data);
- break;
- case 'update':
- await ctx.service.changeAuditList.save(data.updateData);
- break;
- case 'paste':
- await ctx.service.changeAuditList.saveDatas(data.updateData);
- // 取所有工料表
- responseData.data = await ctx.service.changeAuditList.getList(ctx.change.cid);
- break;
- case 'ledger_list':
- await ctx.service.changeAuditList.saveLedgerListDatas(data.updateData, data.postData);
- // 取所有工料表
- responseData.data = { changeList: await ctx.service.changeAuditList.getList(ctx.change.cid),
- usedList: await ctx.service.stageChange.getFinalUsedData(ctx.tender.id, ctx.change.cid) };
- break;
- case 'remove_list':
- await ctx.service.changeAuditList.removeLedgerListDatas(data.updateData);
- // 取所有工料表
- responseData.data = await ctx.service.changeAuditList.getList(ctx.change.cid);
- break;
- case 'update_list':
- await ctx.service.changeAuditList.saveDatas(data.updateData);
- // 取所有工料表
- // responseData.data = await ctx.service.changeAuditList.getList(ctx.change.cid);
- break;
- case 'info':
- let value = 100;
- if (data.updateData.state && parseInt(data.updateData.state) !== ctx.change.state) {
- const projectData = await ctx.service.project.getDataById(ctx.session.sessionProject.id);
- const fun_set = await ctx.service.project.getFunSet(projectData.fun_set);
- const stateInfo = ctx.helper._.find(fun_set.change_state, { order: parseInt(data.updateData.state) });
- value = stateInfo.value;
- }
- responseData.data = await ctx.service.change.saveInfo(data.updateData, value);
- // 取所有工料表
- // responseData.data = '保存成功';
- break;
- case 'paste_amount_rows':
- await ctx.service.changeAuditList.saveDatas(data.updateData);
- const changeList = await ctx.service.changeAuditList.getList(ctx.change.cid);
- const auditList2 = await ctx.service.changeAudit.getListGroupByTimes(ctx.change.cid, ctx.change.times);
- // changeList = JSON.parse(JSON.stringify(changeList.sort())).sort().sort();
- for (const cl of changeList) {
- const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
- // 清单表页赋值
- for (const [index, au] of auditList2.entries()) {
- if (au.usite !== 0) {
- cl['audit_amount_' + au.uid] = au.uid === ctx.session.sessionUser.accountId ? cl.spamount : (audit_amount[index - 1] ? audit_amount[index - 1] : null);
- }
- }
- }
- // 取所有工料表
- responseData.data = changeList;
- break;
- case 'update_tp':
- await ctx.service.change.saveInfo(data.updateData);
- break;
- case 'order_by':
- const result = await ctx.service.change.saveOrderBy(data.updateData, data.newLedgerList);
- responseData.data = result;
- break;
- case 'changeOrder':
- await ctx.service.changeAuditList.changeOrder(data.postData);
- break;
- case 'set_all_valuation':
- await ctx.service.changeAuditList.setAllValuation(ctx.change.cid, data.ids, data.is_valuation);
- // 取所有工料表
- responseData.data = await ctx.service.changeAuditList.getList(ctx.change.cid);
- break;
- default: throw '参数有误';
- }
- ctx.body = responseData;
- } catch (err) {
- this.log(err);
- ctx.body = { err: 1, msg: err.toString(), data: null };
- }
- }
- /**
- * 上报和重新上报
- * @param ctx
- * @return {Promise<void>}
- */
- async startAudit(ctx) {
- try {
- // 检查权限等
- if (!ctx.change) {
- throw '数据错误';
- }
- if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
- throw '您无权上报该变更令数据';
- }
- if (!(ctx.change.status === audit.flow.status.uncheck || ctx.change.status === audit.flow.status.back || ctx.change.status === audit.flow.status.revise)) {
- throw '该变更令当前无法上报';
- }
- // 判断是否是修订,判断有无审批人员作弊
- if ((ctx.change.status === audit.flow.status.revise || ctx.change.is_revise) && ctx.tender.info.shenpi.change === shenpiConst.sp_status.sqspr) {
- // 获取上一次的审批人流程
- const lastUserList = await ctx.service.changeAudit.getListGroupByTimes(ctx.change.cid, ctx.change.times - 1);
- const lastUidList = ctx.helper._.map(lastUserList, 'uid');
- // 判断上一次审批是否为非原报为审批人
- const nowUidList = ctx.helper._.map(ctx.change.auditors, 'uid');
- // 判断条件修订可上报条件
- // 1.有原报,不管有无其他人 可以添加原报作为审核人,但是不能只原报上报
- // 2.无原报,有其他人 不可以添加原报
- // const noYBUidList = ctx.change.status === audit.flow.status.revise ? ctx.helper._.initial(ctx.helper._.tail(lastUidList)) : ctx.helper._.tail(lastUidList);
- const noYBUidList = ctx.helper._.tail(lastUidList);
- // if (!ctx.helper._.isEqual(lastUidList, nowUidList) && ctx.helper._.includes(noYBUidList, lastUidList[0]) && nowUidList.length === 2 && nowUidList[0] === nowUidList[1]) {
- if (nowUidList.length === 2 && nowUidList[0] === nowUidList[1]) {
- throw '该变更令不能指定原报人为单独审批人';
- }
- if (!ctx.helper._.isEqual(lastUidList, nowUidList) && !ctx.helper._.includes(noYBUidList, lastUidList[0]) && ctx.helper._.includes(ctx.helper._.tail(nowUidList), nowUidList[0])) {
- throw '该变更令不能指定原报为审批人,请移除';
- }
- }
- await ctx.service.changeAudit.start(ctx.change.cid, ctx.change.times);
- ctx.redirect(ctx.request.header.referer);
- } catch (err) {
- this.log(err);
- ctx.session.postError = err.toString();
- ctx.redirect(ctx.request.header.referer);
- }
- }
- // 审批相关
- /**
- * 添加审批人
- * @param ctx
- * @return {Promise<void>}
- */
- async addAudit(ctx) {
- try {
- const data = JSON.parse(ctx.request.body.data);
- const id = this.app._.toInteger(data.auditorId);
- if (isNaN(id) || id <= 0) {
- throw '参数错误';
- }
- // 检查权限等
- if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
- throw '您无权添加审核人';
- }
- if (ctx.change.status === audit.flow.status.checking || ctx.change.status === audit.flow.status.checked) {
- throw '当前不允许添加审核人';
- }
- const auditorList = await ctx.service.changeAudit.getAuditors(ctx.change.cid, ctx.change.times);
- // 检查审核人是否已存在
- const exist = this.app._.find(auditorList, { uid: id });
- if (exist) {
- throw '该审核人已存在,请勿重复添加';
- }
- const shenpiInfo = await ctx.service.shenpiAudit.getDataByCondition({ tid: ctx.tender.id, sp_type: shenpiConst.sp_type.change, sp_status: shenpiConst.sp_status.gdzs });
- const is_gdzs = shenpiInfo && ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdzs ? 1 : 0;
- const result = await ctx.service.changeAudit.addAuditor(ctx.change.cid, id, ctx.change.times, is_gdzs);
- if (!result) {
- throw '添加审核人失败';
- }
- // const auditors = await ctx.service.changeAudit.getAuditorsWithOwner(ctx.change.id, ctx.change.times);
- const auditors = null;
- ctx.body = { err: 0, msg: '', data: auditors };
- } catch (err) {
- this.log(err);
- ctx.body = { err: 1, msg: err.toString(), data: null };
- }
- }
- /**
- * 移除审批人
- * @param ctx
- * @return {Promise<void>}
- */
- async deleteAudit(ctx) {
- try {
- const data = JSON.parse(ctx.request.body.data);
- const id = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
- if (isNaN(id) || id <= 0) {
- throw '参数错误';
- }
- const result = await ctx.service.changeAudit.deleteAuditor(ctx.change.cid, id, ctx.change.times);
- if (!result) {
- throw '移除审核人失败';
- }
- const auditors = null;
- ctx.body = { err: 0, msg: '', data: auditors };
- } catch (err) {
- ctx.body = { err: 1, msg: err.toString(), data: null };
- }
- }
- async defaultBills(ctx) {
- try {
- // 判断是否台账修订中,修订中则不获取changeLedger及changePos值
- const lastRevise = await ctx.service.ledgerRevise.getLastestRevise(ctx.tender.id);
- const data = JSON.parse(ctx.request.body.data);
- const readySettle = await ctx.service.settle.getReadySettle(ctx.tender.id);
- if (data.from === 'revise') await ctx.service.change.checkSettleUpdate(ctx.tender.id, readySettle);
- const ledgerData = await ctx.service.ledger.getData(ctx.tender.id);
- 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));
- const posData = await ctx.service.pos.getPosData({ tid: ctx.tender.id });
- 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 }));
- const dealBills = await ctx.service.dealBills.getAllDataByCondition({ where: { tender_id: ctx.tender.id } });
- // const settleStatus = ctx.service.settle.settleStatus;
- const settleBills = readySettle ? await this.ctx.service.settleBills.getAllDataByCondition({ where: { settle_id: readySettle.id } }) : [];
- ctx.helper.assignRelaData(ledgerData, [
- { data: settleBills, fields: ['settle_status'], prefix: '', relaId: 'lid' },
- ]);
- const settlePos = readySettle ? await this.ctx.service.settlePos.getAllDataByCondition({ where: { settle_id: readySettle.id } }) : [];
- this.ctx.helper.assignRelaData(posData, [
- { data: settlePos, fields: ['settle_status'], prefix: '', relaId: 'pid' },
- ]);
- // 标记ledger,搜索需求
- if (changePosData.length > 0) {
- const cplIdList = ctx.helper._.uniq(ctx.helper._.map(changePosData, 'lid'));
- const cLIdList = ctx.helper._.map(changeLedgerData, 'id');
- const ledgerIdList = [];
- for (const cp of cplIdList) {
- if (ctx.helper._.indexOf(cLIdList, cp) === -1) {
- ledgerIdList.push(cp);
- }
- }
- if (ledgerIdList.length > 0) {
- for (const lid of ledgerIdList) {
- const data = ctx.helper._.find(ledgerData, { id: lid });
- data.cid = 1;
- }
- }
- }
- ctx.body = { err: 0, msg: '', data: { bills: ctx.helper._.concat(ledgerData, changeLedgerData), pos: ctx.helper._.concat(posData, changePosData), dealBills } };
- } catch (err) {
- this.log(err);
- ctx.body = { err: 1, msg: err.toString(), data: [] };
- }
- }
- /**
- * 变更令上报和保存
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async save(ctx) {
- // 变更令信息
- const changeInfo = await ctx.service.change.getDataByCondition({ cid: ctx.request.body.cid });
- try {
- const result = await ctx.service.change.save(ctx.request.body, changeInfo.tid);
- if (!result) {
- throw '上报失败';
- }
- if (ctx.request.body.changestatus !== undefined && parseInt(ctx.request.body.changestatus) === 2) {
- ctx.body = { err: 0, msg: '保存成功' };
- } else {
- ctx.redirect('/tender/' + changeInfo.tid + '/change');
- }
- } catch (err) {
- this.log(err);
- if (ctx.request.body.changestatus !== undefined && parseInt(ctx.request.body.changestatus) === 2) {
- ctx.body = { err: 1, msg: err.toString() };
- } else {
- ctx.redirect('/tender/' + changeInfo.tid + '/change/' + ctx.request.body.cid + '/info');
- }
- }
- }
- /**
- * 变更令审批
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async approval(ctx) {
- try {
- const changeData = await ctx.service.change.getDataByCondition({ cid: ctx.request.body.change_id });
- if (!changeData) {
- throw '变更令数据错误';
- }
- const status = parseInt(ctx.request.body.status);
- // 判断是否到你审批,如果不是则无法审批
- const curAuditor = await ctx.service.changeAudit.getCurAuditor(changeData.cid, changeData.times);
- if (!curAuditor || (curAuditor && curAuditor.uid !== ctx.session.sessionUser.accountId)) {
- throw '该变更令当前您无权操作';
- }
- const readySettle = await ctx.service.settle.getReadySettle(changeData.tid);
- if (readySettle && readySettle.settle_order !== ctx.tender.data.settle_order) {
- throw '结算数据发生变化,请刷新页面再提交';
- }
- let result = false;
- const pid = this.ctx.session.sessionProject.id;
- switch (status) {
- case 3:// 审批通过
- result = await ctx.service.change.approvalSuccess(pid, ctx.request.body, changeData);
- break;
- case 4:// 审批终止
- result = await ctx.service.change.approvalStop(ctx.request.body);
- break;
- case 5:// 审批退回到原报人
- result = await ctx.service.change.approvalBack(pid, ctx.request.body, changeData);
- break;
- case 6:// 审批退回到上一个审批人
- result = await ctx.service.change.approvalBackNew(pid, ctx.request.body, changeData);
- break;
- default:break;
- }
- if (!result) {
- throw '审批失败';
- }
- ctx.redirect(ctx.request.header.referer);
- } catch (err) {
- console.log(err);
- ctx.session.postError = err.toString();
- ctx.redirect(ctx.request.header.referer);
- }
- }
- /**
- * 撤回审批
- * @param ctx
- * @return {Promise<void>}
- */
- async checkAuditCancel(ctx) {
- try {
- if (!ctx.change.cancancel) throw '您无权进行该操作';
- if (ctx.change.readySettle && ctx.change.readySettle.settle_order !== ctx.tender.data.settle_order) {
- throw '结算数据发生变化,请刷新页面再提交';
- }
- await ctx.service.changeAudit.checkCancel(ctx.change);
- ctx.body = { err: 0, url: ctx.request.header.referer, msg: '' };
- } catch (err) {
- this.log(err);
- ctx.body = { err: 1, msg: err };
- }
- }
- /**
- * 变更公司管理
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async updateCompany(ctx) {
- const responseData = {
- err: 0,
- msg: '',
- data: '',
- };
- try {
- const data = JSON.parse(ctx.request.body.data);
- if (data.tid === undefined || data.uci === undefined || data.uc === undefined || data.ac === undefined) {
- throw '参数有误';
- }
- const [addCompany, selectCompany] = await ctx.service.changeCompany.setCompanyList(data);
- responseData.data = { add: addCompany, select: selectCompany };
- } catch (err) {
- responseData.err = 1;
- responseData.msg = err;
- }
- ctx.body = responseData;
- }
- /**
- * 上传附件
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async uploadFile(ctx) {
- const responseData = {
- err: 0,
- msg: '',
- data: [],
- };
- let stream;
- try {
- const parts = ctx.multipart({ autoFields: true });
- const files = [];
- let index = 0;
- const change = await ctx.service.change.getDataByCondition({ cid: ctx.params.cid });
- const extra_upload = change.status === audit.flow.status.checked;
- while ((stream = await parts()) !== undefined) {
- // 判断用户是否选择上传文件
- if (!stream.filename) {
- throw '请选择上传的文件!';
- }
- // const create_time = Date.parse(new Date()) / 1000;
- // const fileInfo = path.parse(stream.filename);
- // const dirName = 'app/public/upload/changes/' + moment().format('YYYYMMDD');
- // const fileName = 'changes' + create_time + '_' + index + fileInfo.ext;
- // // 判断文件夹是否存在,不存在则直接创建文件夹
- // if (!fs.existsSync(path.join(this.app.baseDir, dirName))) {
- // await fs.mkdirSync(path.join(this.app.baseDir, dirName));
- // }
- // // 保存文件
- // await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, dirName, fileName));
- const fileInfo = path.parse(stream.filename);
- const create_time = Date.parse(new Date()) / 1000;
- // const filepath = `app/public/upload/change/fujian_${create_time + index.toString() + fileInfo.ext}`;
- const filepath = `app/public/upload/${parts.field.tid}/change/fujian_${create_time + index.toString() + fileInfo.ext}`;
- // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, filepath));
- await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
- await sendToWormhole(stream);
- // 保存数据到att表
- const fileData = {
- in_time: create_time,
- filename: fileInfo.name,
- fileext: fileInfo.ext,
- filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
- filepath,
- extra_upload,
- };
- const result = await ctx.service.changeAtt.save(parts.field, fileData, ctx.session.sessionUser.accountId);
- if (!result) {
- throw '导入数据库保存失败';
- }
- // fileData.in_time = moment(create_time * 1000).format('YYYY-MM-DD');
- // fileData.filesize = await ctx.helper.bytesToSize(fileData.filesize);
- fileData.uid = ctx.session.sessionUser.accountId;
- fileData.id = result.insertId;
- fileData.orginpath = ctx.app.config.fujianOssPath + filepath;
- delete fileData.filepath;
- if (!ctx.helper.canPreview(fileData.fileext)) {
- fileData.filepath = `/change/download/file/${fileData.id}`;
- } else {
- fileData.filepath = ctx.app.config.fujianOssPath + filepath;
- }
- files.push(fileData);
- ++index;
- }
- responseData.data = files;
- } catch (err) {
- this.log(err);
- // 失败需要消耗掉stream 以防卡死
- if (stream) {
- await sendToWormhole(stream);
- }
- this.setMessage(err.toString(), this.messageType.ERROR);
- }
- ctx.body = responseData;
- }
- /**
- * 下载附件
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async downloadFile(ctx) {
- const id = ctx.params.id;
- if (id) {
- try {
- const fileInfo = await ctx.service.changeAtt.getDataById(id);
- if (fileInfo !== undefined && fileInfo !== '') {
- // const fileName = path.join(this.app.baseDir, fileInfo.filepath);
- // 解决中文无法下载问题
- const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
- let disposition = '';
- if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
- disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename + fileInfo.fileext);
- } else if (userAgent.indexOf('firefox') >= 0) {
- disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename + fileInfo.fileext) + '"';
- } else {
- /* safari等其他非主流浏览器只能自求多福了 */
- disposition = 'attachment; filename=' + new Buffer(fileInfo.filename + fileInfo.fileext).toString('binary');
- }
- ctx.response.set({
- 'Content-Type': 'application/octet-stream',
- 'Content-Disposition': disposition,
- 'Content-Length': fileInfo.filesize,
- });
- // ctx.body = await fs.createReadStream(fileName);
- ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
- } else {
- throw '不存在该文件';
- }
- } catch (err) {
- this.log(err);
- this.setMessage(err.toString(), this.messageType.ERROR);
- }
- }
- }
- /**
- * 批量下载 - 压缩成zip文件返回
- * @param {Oject} ctx - 全局上下文
- */
- async downloadZip(ctx) {
- const time = Date.now();
- const zipPath = `app/public/upload/change/fu_jian_zip${time}.zip`;
- const responseData = {
- err: 0,
- msg: '',
- };
- try {
- // const fileIds = JSON.parse(ctx.request.query.fileIds);
- const { fileIds = [] } = JSON.parse(ctx.request.body.data);
- const { name: changeName } = await ctx.service.changeAtt.getChangeName(ctx.params.cid);
- const zipFilename = `${ctx.tender.data.name}-工程变更-${changeName}-附件.zip`;
- const size = await ctx.service.changeAtt.compressedFile(fileIds, zipPath);
- // 解决中文无法下载问题
- const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
- let disposition = '';
- if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
- disposition = 'attachment; filename=' + encodeURIComponent(zipFilename);
- } else if (userAgent.indexOf('firefox') >= 0) {
- disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(zipFilename) + '"';
- } else {
- /* safari等其他非主流浏览器只能自求多福了 */
- disposition = 'attachment; filename=' + new Buffer(zipFilename).toString('binary');
- }
- ctx.response.set({
- 'Content-Type': 'application/octet-stream',
- 'Content-Disposition': disposition,
- 'Content-Length': size,
- });
- const readStream = fs.createReadStream(path.join(this.app.baseDir, zipPath));
- ctx.body = readStream;
- // ctx.body = fs.readFileSync(path.resolve(this.app.baseDir, zipPath));
- readStream.on('close', () => {
- if (fs.existsSync(path.resolve(this.app.baseDir, zipPath))) {
- fs.unlinkSync(path.resolve(this.app.baseDir, zipPath));
- }
- });
- // fs的错误不能被try catch捕捉
- readStream.on('error', err => {
- this.log(err);
- if (fs.existsSync(path.resolve(this.app.baseDir, zipPath))) {
- fs.unlinkSync(path.resolve(this.app.baseDir, zipPath));
- }
- responseData.err = 1;
- responseData.msg = err.toString();
- ctx.body = responseData;
- });
- } catch (err) {
- this.log(err);
- if (fs.existsSync(path.resolve(this.app.baseDir, zipPath))) {
- fs.unlinkSync(path.resolve(this.app.baseDir, zipPath));
- }
- this.setMessage(err.toString(), this.messageType.ERROR);
- responseData.err = 1;
- responseData.msg = err.toString();
- ctx.body = responseData;
- }
- }
- /**
- * 删除附件
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async deleteFile(ctx) {
- const responseData = {
- err: 0,
- msg: '',
- data: '',
- };
- try {
- const data = JSON.parse(ctx.request.body.data);
- const change = await ctx.service.change.getDataByCondition({ cid: ctx.params.cid });
- const fileInfo = await ctx.service.changeAtt.getDataById(data.id);
- if (!fileInfo || !Object.keys(fileInfo).length) {
- throw '该文件不存在';
- }
- if (!fileInfo.extra_upload && change.status === audit.flow.status.checked) {
- throw '无权限删除';
- }
- if (fileInfo !== undefined && fileInfo !== '') {
- // 先删除文件
- // await fs.unlinkSync(path.join(this.app.baseDir, fileInfo.filepath));
- await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
- // 再删除数据库
- await ctx.service.changeAtt.deleteById(data.id);
- responseData.data = '';
- } else {
- throw '不存在该文件';
- }
- // if (data.tid === undefined || data.uci === undefined || data.uc === undefined || data.ac === undefined) {
- // throw '参数有误';
- // }
- // const [addCompany, selectCompany] = await ctx.service.changeCompany.setCompanyList(data);
- // responseData.data = { add: addCompany, select: selectCompany };
- } catch (err) {
- responseData.err = 1;
- responseData.msg = err;
- }
- ctx.body = responseData;
- }
- /**
- * 查看附件
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async checkFile(ctx) {
- const responseData = { err: 0, msg: '' };
- const id = parseInt(ctx.params.id);
- if (id) {
- try {
- const fileInfo = await ctx.service.changeAtt.getDataById(id);
- if (fileInfo && Object.keys(fileInfo).length) {
- let filepath = fileInfo.filepath;
- if (!ctx.helper.canPreview(fileInfo.fileext)) {
- filepath = `/change/download/file/${fileInfo.id}`;
- } else {
- filepath = filepath.replace(/^app|\/app/, '');
- }
- fileInfo.filepath && (responseData.data = { filepath });
- }
- } catch (error) {
- this.log(error);
- this.setMessage(error.toString(), this.messageType.ERROR);
- responseData.err = 1;
- responseData.msg = error.toString();
- }
- }
- ctx.body = responseData;
- }
- /**
- * 删除变更令
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async delete(ctx) {
- try {
- const result = await ctx.service.change.delete(ctx.request.body.cid);
- if (!result) {
- throw '删除变更令失败';
- }
- ctx.redirect(ctx.request.header.referer);
- } catch (err) {
- console.log(err);
- ctx.redirect(ctx.request.header.referer);
- }
- }
- /**
- * 变更令重新审批
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async checkAgain(ctx) {
- try {
- const changeData = await ctx.service.change.getDataByCondition({ cid: ctx.request.body.cid });
- if (!changeData) {
- throw '变更令数据错误';
- }
- // 获取终审
- const auditInfo = await ctx.service.changeAudit.getAuditorByStatus(changeData.cid, changeData.times, audit.flow.status.checked);
- if (changeData.status !== audit.flow.status.checked || ctx.session.sessionUser.accountId !== auditInfo.uid) {
- throw '您无权进行该操作';
- }
- const readySettle = await ctx.service.settle.getReadySettle(changeData.tid);
- if (readySettle && readySettle.settle_order !== ctx.tender.data.settle_order) {
- throw '结算数据发生变化,请刷新页面再提交';
- }
- if (ctx.session.sessionUser.loginStatus === 0) {
- const code = ctx.request.body.code;
- const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
- if (!pa.auth_mobile) {
- throw '未绑定手机号';
- }
- const cacheKey = 'smsCode:' + ctx.session.sessionUser.accountId;
- const cacheCode = await app.redis.get(cacheKey);
- // console.log(cacheCode);
- if (cacheCode === null || code === undefined || cacheCode !== (code + pa.auth_mobile)) {
- throw '验证码不正确!';
- }
- }
- // 重新审批
- const result = await ctx.service.change.checkAgain(changeData.cid);
- if (!result) {
- throw '重新审批失败';
- }
- // ctx.redirect('/tender/' + changeData.tid + '/change/' + changeData.cid + '/info');
- ctx.body = {
- err: 0,
- url: ctx.request.header.referer,
- msg: '',
- };
- } catch (err) {
- console.log(err);
- // ctx.redirect(ctx.request.header.referer);
- ctx.body = {
- err: 1,
- // url: ctx.request.header.referer,
- msg: err,
- };
- }
- }
- /**
- * 变更令修订重新上报
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async checkRevise(ctx) {
- try {
- const changeData = await ctx.service.change.getDataByCondition({ cid: ctx.request.body.cid });
- if (!changeData) {
- throw '变更令数据错误';
- }
- if (changeData.status !== audit.flow.status.checked || ctx.session.sessionUser.accountId !== changeData.uid) {
- throw '您无权进行该操作';
- }
- const readySettle = await ctx.service.settle.getReadySettle(changeData.tid);
- if (readySettle && readySettle.settle_order !== ctx.tender.data.settle_order) {
- throw '结算数据发生变化,请刷新页面再提交';
- }
- if (ctx.session.sessionUser.loginStatus === 0) {
- const code = ctx.request.body.code;
- const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
- if (!pa.auth_mobile) {
- throw '未绑定手机号';
- }
- const cacheKey = 'smsCode:' + ctx.session.sessionUser.accountId;
- const cacheCode = await app.redis.get(cacheKey);
- // console.log(cacheCode);
- if (cacheCode === null || code === undefined || cacheCode !== (code + pa.auth_mobile)) {
- throw '验证码不正确!';
- }
- }
- // 获取是否已存在调用变更令
- // const changeUsedData = await ctx.service.stageChange.getFinalUsedData(ctx.tender.id, changeData.cid);
- // const stageChangeNum = this.ctx.helper.sum(changeUsedData.map(x => { return Math.abs(x.qty); }));
- // if (stageChangeNum !== 0) {
- // throw '该变更令已被调用,无法重新审批';
- // }
- // 重新审批
- const result = await ctx.service.change.checkRevise(changeData.cid);
- if (!result) {
- throw '修订发起失败';
- }
- // ctx.redirect('/tender/' + changeData.tid + '/change/' + changeData.cid + '/info');
- ctx.body = {
- err: 0,
- url: ctx.request.header.referer,
- msg: '',
- };
- } catch (err) {
- console.log(err);
- // ctx.redirect(ctx.request.header.referer);
- ctx.body = {
- err: 1,
- // url: ctx.request.header.referer,
- msg: err,
- };
- }
- }
- /**
- * 变更令撤销修订
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async cancelRevise(ctx) {
- try {
- const changeData = await ctx.service.change.getDataByCondition({ cid: ctx.request.body.cid });
- if (!changeData) {
- throw '变更令数据错误';
- }
- if (!(changeData.status === audit.flow.status.revise && (ctx.session.sessionUser.accountId === changeData.uid || ctx.session.sessionUser.accountId === ctx.session.sessionUser.is_admin))) {
- throw '您无权进行该操作';
- }
- const readySettle = await ctx.service.settle.getReadySettle(changeData.tid);
- if (readySettle && readySettle.settle_order !== ctx.tender.data.settle_order) {
- throw '结算数据发生变化,请刷新页面再提交';
- }
- // 重新审批
- const result = await ctx.service.change.cancelRevise(changeData.cid, changeData.times);
- if (!result) {
- throw '撤销修订失败';
- }
- ctx.body = {
- err: 0,
- url: ctx.request.header.referer,
- msg: '',
- };
- } catch (err) {
- console.log(err);
- ctx.body = {
- err: 1,
- msg: err,
- };
- }
- }
- async saveAudit(ctx) {
- try {
- // if (ctx.change.revising) {
- // throw '台账修订中,请勿修改提交期数据';
- // }
- const data = JSON.parse(ctx.request.body.data);
- if (ctx.session.sessionUser.is_admin && ctx.change.status !== audit.flow.status.checked) {
- await ctx.service.changeAudit.saveAudit(ctx.change.cid, ctx.change.times, data);
- const auditors = await ctx.service.changeAudit.getListGroupByTimes(ctx.change.cid, ctx.change.times);
- ctx.body = { err: 0, msg: '', data: auditors };
- } else {
- throw '您无权进行该操作';
- }
- } catch (err) {
- this.log(err);
- // ctx.session.postError = err.toString();
- // ctx.redirect(ctx.request.header.referer);
- ctx.body = {
- err: 1,
- // url: ctx.request.header.referer,
- msg: err,
- };
- }
- }
- /**
- * 获取变更清单
- * @param ctx
- * @return {Promise<void>}
- */
- async bills(ctx) {
- try {
- const data = JSON.parse(ctx.request.body.data);
- const responseData = { err: 0, msg: '', data: [] };
- switch (data.type) {
- case 'gather':
- responseData.data = await ctx.service.changeAuditList.gatherBgBills(ctx.tender.id);
- break;
- default:
- throw '查询的数据不存在';
- }
- ctx.body = responseData;
- } catch (err) {
- this.log(err);
- this.ajaxErrorBody(err, '获取变更清单失败');
- }
- }
- /**
- * 最后审批人审批成功检查批复编号和其它变更令是否出现重名情况
- * @param ctx
- * @return {Promise<void>}
- */
- async checkCodeRepeat(ctx) {
- const responseData = {
- err: 0,
- msg: '',
- data: '',
- };
- try {
- const tenderId = ctx.params.id;
- const cid = ctx.params.cid;
- const data = JSON.parse(ctx.request.body.data);
- const result = await ctx.service.change.isRepeat(cid, data.p_code, tenderId);
- if (result) {
- throw '该变更令号(批复编号)已使用';
- }
- } catch (err) {
- responseData.err = 1;
- responseData.msg = err;
- }
- ctx.body = responseData;
- }
- /**
- * 拷贝其他变更令
- * @param {object} ctx - 全局上下文
- */
- async copyChange(ctx) {
- const responseData = {
- err: 0,
- msg: '',
- data: '',
- };
- try {
- const cid = ctx.params.cid;
- const copy_cid = JSON.parse(ctx.request.body.data);
- const result = await ctx.service.change.handleCopyChange(cid, copy_cid);
- if (!result) {
- responseData.err = 1;
- responseData.msg = '拷贝失败!';
- }
- } catch (error) {
- responseData.err = 1;
- responseData.msg = error;
- }
- ctx.body = responseData;
- }
- /**
- * 修订 详细页面(Get)
- * @param ctx
- * @return {Promise<void>}
- */
- async reviseInfo(ctx) {
- try {
- if (!ctx.session.sessionProject.page_show.openChangeRevise) {
- throw '该功能已关闭';
- }
- const change = ctx.change;
- let edit = true;
- let changing = false;
- if (change.status !== audit.flow.status.uncheck && change.status !== audit.flow.status.back && change.status !== audit.flow.status.revise) {
- // throw '本条变更审批中或已完成,无法操作台账数据';
- edit = false;
- changing = true;
- }
- let revising = false;
- // 判断是否在修订中,是则无法操作本页
- const lastRevise = await ctx.service.ledgerRevise.getLastestRevise(ctx.tender.id);
- if (lastRevise && lastRevise.status !== audit.revise.status.checked) {
- // throw '台账修订中,无法操作台账数据';
- edit = false;
- revising = true;
- }
- const renderData = await this._getDefaultReviseInfoData(ctx, change, edit);
- // 台账只读、使用数据
- renderData.readOnly = !edit;
- renderData.changing = changing;
- renderData.revising = revising;
- await this.layout('change/revise.ejs', renderData, 'change/revise_modal.ejs');
- } catch (err) {
- this.log(err);
- ctx.redirect(ctx.request.header.referer);
- }
- }
- async _getDefaultReviseInfoData(ctx, change, edit) {
- const [ledgerSpread, posSpread] = this._getSpreadSetting(change, edit);
- const sjsRela = await this.ctx.service.project.getTenderSjsRela(ctx.session.sessionProject.id, ctx.tender.info.display.exMemo);
- this.ctx.helper.refreshSpreadShow(sjsRela.ledgerCol, [ledgerSpread, posSpread]);
- const [stdBills, stdChapters] = await this.ctx.service.valuation.getValuationStdList(
- ctx.tender.data.valuation, ctx.tender.data.measure_type);
- return {
- change,
- tender: ctx.tender.data,
- ledgerSpread, posSpread, tenderMenu, measureType,
- preUrl: '/tender/' + ctx.tender.id + '/change/' + ctx.change.cid + '/information',
- jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.revise),
- stdBills,
- auditConst: audit.flow,
- audit: audit.flow,
- stdChapters,
- nodeType: stdConst.nodeType,
- settleStatus: ctx.service.settle.settleStatus,
- };
- }
- /**
- * 获取SpreadSetting
- * @private
- */
- _getSpreadSetting(change, edit) {
- const _ = this.app._;
- function removeFieldCols(setting, cols) {
- _.remove(setting.cols, function(c) {
- return cols.indexOf(c.field) > -1;
- });
- }
- const tender = this.ctx.tender;
- const setting = tender.data.measure_type === measureType.tz.value
- ? (tender.info.display.ledger.clQty ? spreadConst.withCl : spreadConst.withoutCl)
- : (tender.info.display.ledger.clQty ? spreadConst.withClGcl : spreadConst.withoutClGcl);
- const ledger = JSON.parse(JSON.stringify(setting.ledger));
- const pos = setting.pos ? JSON.parse(JSON.stringify(setting.pos)) : spreadConst.blank;
- // if (change.status === audit.flow.status.checking || change.status === audit.flow.status.checked) {
- // ledger.readOnly = true;
- // pos.readOnly = true;
- // }
- if (!edit) {
- ledger.readOnly = true;
- pos.readOnly = true;
- }
- if (tender.data.measure_type === measureType.tz.value) {
- removeFieldCols(ledger, spreadConst.filterCols.dealCols);
- }
- if (!tender.info.display.ledger.dgnQty) {
- removeFieldCols(ledger, spreadConst.filterCols.dgnCols);
- }
- return [ledger, pos];
- }
- async updateRevise(ctx) {
- try {
- if (!ctx.tender.data) throw '标段数据错误';
- if (!ctx.session.sessionProject.page_show.openChangeRevise) {
- throw '该功能已关闭';
- }
- const data = JSON.parse(ctx.request.body.data);
- if (!data.postType || !data.postData) throw '数据错误';
- const responseData = { err: 0, msg: '', data: {} };
- const change = ctx.change;
- if (change.status !== audit.flow.status.uncheck && change.status !== audit.flow.status.back && change.status !== audit.flow.status.revise) {
- throw '该变更令正在审批中或已完成,无法操作新增部位数据';
- }
- // 判断是否在修订中,是则无法操作本页
- const lastRevise = await ctx.service.ledgerRevise.getLastestRevise(ctx.tender.id);
- if (lastRevise && lastRevise.status !== audit.revise.status.checked) {
- throw '台账修订中,无法操作新增部位数据';
- }
- // const revise = await this.checkRevise(ctx);
- //
- switch (data.postType) {
- case 'add':
- case 'delete':
- case 'up-move':
- case 'down-move':
- case 'up-level':
- case 'down-level':
- responseData.data = await this._billsBase(ctx, data.postType, data.postData);
- break;
- case 'update':
- ctx.helper.checkDgnQtyPrecision(data.postData);
- responseData.data = await ctx.service.changeLedger.updateCalc(ctx.tender.id, data.postData);
- break;
- case 'paste-block':
- responseData.data = await this._pasteBlock(ctx, data.postData);
- break;
- case 'add-std':
- responseData.data = await this._addStd(ctx, data.postData);
- break;
- case 'add-deal':
- responseData.data = await this._addDeal(ctx, data.postData);
- break;
- // case 'batch-insert':
- // responseData.data = await this._batchInsert(ctx, data.postData);
- // break;
- case 'pos':
- responseData.data = await this._updatePos(ctx, data);
- break;
- default:
- throw '未知操作';
- }
- ctx.body = responseData;
- } catch (err) {
- console.log(err);
- this.log(err);
- ctx.body = this.ajaxErrorBody(err, '数据错误');
- }
- }
- async _billsBase(ctx, type, data) {
- if (isNaN(data.id) || data.id <= 0) throw '数据错误';
- if (type !== 'add') {
- if (isNaN(data.count) || data.count <= 0) data.count = 1;
- }
- switch (type) {
- case 'add':
- return await ctx.service.changeLedger.addNodeBatch(ctx.tender.id, data.id, { ccid: ctx.change.cid, check_calc: 1 }, data.count);
- case 'delete':
- return await ctx.service.changeLedger.delete(ctx.tender.id, data.id, data.count);
- case 'up-move':
- return await ctx.service.changeLedger.upMoveNode(ctx.tender.id, data.id, data.count);
- case 'down-move':
- return await ctx.service.changeLedger.downMoveNode(ctx.tender.id, data.id, data.count);
- case 'up-level':
- return await ctx.service.changeLedger.upLevelNode(ctx.tender.id, data.id, data.count);
- case 'down-level':
- return await ctx.service.changeLedger.downLevelNode(ctx.tender.id, data.id, data.count);
- default:
- throw '未知操作';
- }
- }
- /**
- * 复制粘贴整块
- *
- * @param ctx
- * @return {Promise<void>}
- */
- async _pasteBlock(ctx, data) {
- if ((isNaN(data.id) || data.id <= 0) ||
- (!data.tid && data.tid <= 0) ||
- (!data.block || data.block.length <= 0)) throw '参数错误';
- return await ctx.service.changeLedger.pasteBlockData(ctx.tender.id, data.id, data.block);
- }
- /**
- * 从标准项目表添加数据
- * @param ctx
- * @return {Promise<void>}
- */
- async _addStd(ctx, data) {
- if ((isNaN(data.id) || data.id <= 0) || !data.stdType || !data.stdNode) throw '参数错误';
- // todo 校验项目是否使用该库的权限
- let stdLib,
- addType;
- switch (data.stdType) {
- case 'xmj':
- stdLib = ctx.service.stdXmj;
- addType = stdDataAddType.withParent;
- break;
- case 'gcl':
- stdLib = ctx.service.stdGcl;
- const selectNode = await ctx.service.changeLedger.getDataByNodeId(ctx.tender.id, data.id) || await ctx.service.ledger.getDataByNodeId(ctx.tender.id, data.id);
- if (selectNode.b_code) {
- addType = stdDataAddType.next;
- } else {
- addType = stdDataAddType.child;
- }
- break;
- default:
- throw '未知标准库';
- }
- const stdData = await stdLib.getDataByDataId(data.stdLibId, data.stdNode);
- switch (addType) {
- case stdDataAddType.child:
- return await ctx.service.changeLedger.addStdNodeAsChild(ctx.tender.id, data.id, stdData, ctx.change.cid);
- case stdDataAddType.next:
- return await ctx.service.changeLedger.addStdNode(ctx.tender.id, data.id, stdData, ctx.change.cid);
- case stdDataAddType.withParent:
- return await ctx.service.changeLedger.addStdNodeWithParent(ctx.tender.id, stdData, stdLib, ctx.change.cid);
- default:
- throw '未知添加方式';
- }
- }
- /**
- * 从签约清单添加节点
- * @param ctx
- * @return {Promise<void>}
- */
- async _addDeal(ctx, data) {
- if (!data.type || !data.dealBills) throw '数据错误';
- data.dealBills.unit_price = this.ctx.helper.round(data.dealBills.unit_price, ctx.tender.info.decimal.up);
- if (data.type === 'child') {
- return await ctx.service.changeLedger.addChild(ctx.tender.id, data.id, data.dealBills, ctx.change.cid);
- } else if (data.type === 'next') {
- data.dealBills.ccid = ctx.change.cid;
- return await ctx.service.changeLedger.addNode(ctx.tender.id, data.id, data.dealBills);
- }
- throw '数据错误';
- }
- /**
- * 批量插入数据
- *
- * data = {id, batchData, batchType}
- * data.batchType = 'batchInsertChild'/'batchInsertNext'
- * data.batchData = [{name, children}] -- 项目节列表
- * data.batchData.children = [{code, name, unit, unit_price, quantity}] -- 工程量清单列表
- *
- * @param ctx
- * @return {Promise<void>}
- */
- async _batchInsert(ctx, data) {
- if ((isNaN(data.id) || data.id <= 0) || !data.batchType) throw '参数错误';
- switch (data.batchType) {
- case 'child':
- return await ctx.service.changeLedger.batchInsertChild(ctx.tender.id, data.id, data.batchData);
- case 'next':
- return await ctx.service.changeLedger.batchInsertNext(ctx.tender.id, data.id, data.batchData);
- default:
- throw '参数错误';
- }
- }
- async _updatePos(ctx, data) {
- await this.checkMeasureType(measureType.tz.value);
- if (!data.posPostType) throw '数据错误';
- switch (data.posPostType) {
- case 'add':
- return await this.ctx.service.changePos.addPos(ctx.tender.id, ctx.change.cid, data.postData);
- case 'update':
- if (data.postData instanceof Array) {
- return await this.ctx.service.changePos.updatePosArr(ctx.tender.id, data.postData);
- }
- return await this.ctx.service.changePos.updatePos(ctx.tender.id, data.postData);
- case 'delete':
- return await this.ctx.service.changePos.deletePos(ctx.tender.id, data.postData);
- case 'paste':
- return await this.ctx.service.changePos.pastePosData(ctx.tender.id, ctx.change.cid, data.postData);
- default:
- throw '未知操作';
- }
- }
- async _filterChangesProject(ctx, status = 0) {
- const tenderId = ctx.params.id;
- ctx.session.sessionUser.tenderId = tenderId;
- const tender = await this.service.tender.getDataById(ctx.tender.id);
- // const tender = ctx.tender;
- // const tenderList = await this.service.tender.getList();
- const page = ctx.page;
- const pageSize = ctx.pageSize;
- const sorts = ctx.query.sort ? ctx.query.sort : 0;
- const orders = ctx.query.order ? ctx.query.order : 0;
- const changes = await ctx.service.changeProject.getListByStatus(tender.id, status, 1, sorts, orders);
- const total = await ctx.service.changeProject.getCountByStatus(tender.id, status);
- for (const c of changes) {
- c.curAuditor = await ctx.service.changeProjectAudit.getAuditorByStatus(c.id, c.status, c.times);
- }
- const accountInfo = await this.ctx.service.projectAccount.getDataById(this.ctx.session.sessionUser.accountId);
- const userPermission = accountInfo !== undefined && accountInfo.permission !== ''
- ? JSON.parse(accountInfo.permission) : null;
- // 分页相关
- const pageInfo = {
- page,
- pageSizeSelect: 1,
- pageSize,
- total_num: total,
- total: Math.ceil(total / pageSize),
- queryData: JSON.stringify(ctx.urlInfo.query),
- };
- const filter = JSON.parse(JSON.stringify(audit.changeProject.filter));
- filter.count = [];
- 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);
- 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);
- 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);
- 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);
- 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);
- let codeRule = [];
- let c_connector = '1';
- let c_rule_first = 1;
- const rule_type = tender.user_id === ctx.session.sessionUser.accountId ? 'suggestion' : 'will';
- if (tender.c_code_rules) {
- const c_code_rules = JSON.parse(tender.c_code_rules);
- codeRule = c_code_rules[rule_type + '_rule'] !== undefined ? c_code_rules[rule_type + '_rule'] : [];
- c_connector = c_code_rules[rule_type + '_connector'] !== undefined ? c_code_rules[rule_type + '_connector'] : '1';
- c_rule_first = c_code_rules[rule_type + '_rule_first'] !== undefined ? c_code_rules[rule_type + '_rule_first'] : 1;
- }
- for (const rule of codeRule) {
- switch (rule.rule_type) {
- case codeRuleConst.measure.ruleType.dealCode:
- rule.preview = ctx.tender.info.deal_info.dealCode;
- break;
- case codeRuleConst.measure.ruleType.tenderName:
- rule.preview = tender.name;
- break;
- case codeRuleConst.measure.ruleType.inDate:
- rule.preview = moment().format('YYYY');
- break;
- case codeRuleConst.measure.ruleType.text:
- rule.preview = rule.text;
- break;
- case codeRuleConst.measure.ruleType.addNo:
- const s = '0000000000';
- rule.preview = s.substr(s.length - rule.format);
- break;
- default: break;
- }
- }
- const renderData = {
- uid: ctx.session.sessionUser.accountId,
- userPermission,
- tender,
- pageInfo,
- changes,
- status,
- rule_type,
- codeRule,
- c_connector,
- c_rule_first,
- filter,
- ruleType: codeRuleConst.ruleType[rule_type],
- dealCode: ctx.tender.info.deal_info.dealCode,
- auditConst: audit.changeProject,
- ruleConst: codeRuleConst.measure,
- changeConst,
- tenderMenu: this.menu.tenderMenu,
- preUrl: '/tender/' + tender.id,
- jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.project),
- };
- await this.layout('change/project.ejs', renderData, 'change/project_modal.ejs');
- }
- /**
- * 变更立项列表 页面 (Get)
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async project(ctx) {
- try {
- await this._filterChangesProject(ctx);
- } catch (err) {
- this.log(err);
- ctx.redirect('/dashboard');
- }
- }
- /**
- * 新增变更立项 (Post)
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async projectAdd(ctx) {
- try {
- const tenderId = ctx.params.id;
- if (!tenderId) {
- throw '当前未打开标段';
- }
- const data = JSON.parse(ctx.request.body.data);
- if (!data.code || data.code === '' || !data.name || data.name === '') {
- throw '变更立项书编号不能为空';
- }
- const change = await ctx.service.changeProject.add(tenderId, ctx.session.sessionUser.accountId, data.code, data.name);
- ctx.body = { err: 0, msg: '', data: change };
- } catch (err) {
- this.log(err);
- ctx.body = { err: 1, msg: err.toString() };
- }
- }
- /**
- * 删除变更立项
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async projectDelete(ctx) {
- try {
- const result = await ctx.service.changeProject.delete(ctx.request.body.cpid);
- if (!result) {
- throw '删除变更立项失败';
- }
- ctx.redirect(ctx.request.header.referer);
- } catch (err) {
- console.log(err);
- ctx.redirect(ctx.request.header.referer);
- }
- }
- /**
- * 变更管理 状态筛选 页面 (Get)
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async projectStatus(ctx) {
- try {
- const status = parseInt(ctx.params.status);
- await this._filterChangesProject(ctx, status);
- } catch (err) {
- this.logger.error(err);
- ctx.redirect('/tender/' + ctx.params.id + '/change/project');
- }
- }
- /**
- * 获取审批界面所需的 原报、审批人数据等
- * @param ctx
- * @return {Promise<void>}
- * @private
- */
- async _getChangeProjectAuditViewData(ctx) {
- const auditConst = audit.changeProject;
- const times = (ctx.change.status === auditConst.status.back || ctx.change.status === auditConst.status.revise) ? ctx.change.times - 1 : ctx.change.times;
- ctx.change.user = await ctx.service.projectAccount.getAccountInfoById(ctx.change.uid);
- ctx.change.auditHistory = [];
- if (times >= 1) {
- for (let i = 1; i <= times; i++) {
- ctx.change.auditHistory.push(await ctx.service.changeProjectAudit.getAuditors(ctx.change.id, i));
- }
- }
- // 获取审批流程中左边列表
- ctx.change.auditors2 = (ctx.change.status === auditConst.status.back || ctx.change.status === auditConst.status.revise) && ctx.change.uid !== ctx.session.sessionUser.accountId ?
- await ctx.service.changeProjectAudit.getAuditorsWithOwner(ctx.change.id, times) :
- await ctx.service.changeProjectAudit.getAuditorsWithOwner(ctx.change.id, ctx.change.times);
- if (ctx.change.status === auditConst.status.uncheck || ctx.change.status === auditConst.status.back || ctx.change.status === auditConst.status.revise) {
- ctx.change.auditorList = await ctx.service.changeProjectAudit.getAuditors(ctx.change.id, ctx.change.times);
- }
- ctx.change.xsAuditors = await ctx.service.changeProjectXsAudit.getAuditList(ctx.change.id);
- }
- // 变更类别获取及更新
- async _getOrUpdateClass(ctx, serviceName) {
- // 工程变更类别读取
- const projectData = await ctx.service.project.getDataById(ctx.session.sessionProject.id);
- const fun_set = await ctx.service.project.getFunSet(projectData.fun_set);
- if (ctx.change.status === audit[serviceName].status.uncheck ||
- (serviceName === 'changeProject' && ctx.change.status === audit[serviceName].status.back) ||
- (serviceName !== 'changeProject' && ctx.change.status === audit[serviceName].status.checkNo)) {
- const classInfo = ctx.helper._.find(fun_set.change_class, function(item) {
- return item.checked && (item.name === ctx.change.class || item.new_name === ctx.change.class);
- });
- if (!classInfo) {
- const classInfo2 = ctx.helper._.find(fun_set.change_class, { checked: true });
- ctx.change.class = classInfo2.new_name ? classInfo2.new_name : classInfo2.name;
- await ctx.service[serviceName].defaultUpdate({
- id: ctx.change.id,
- class: ctx.change.class,
- });
- } else if (classInfo && classInfo.new_name && ctx.change.class !== classInfo.new_name) {
- ctx.change.class = classInfo.new_name;
- await ctx.service[serviceName].defaultUpdate({
- id: ctx.change.id,
- class: ctx.change.class,
- });
- }
- }
- return fun_set.change_class;
- }
- async projectInformation(ctx) {
- try {
- const whiteList = this.ctx.app.config.multipart.whitelist;
- const tender = await ctx.service.tender.getDataById(ctx.tender.id);
- // 获取附件列表
- const fileList = await ctx.service.changeProjectAtt.getAllChangeProjectAtt(ctx.tender.id, ctx.change.id);
- await this._getChangeProjectAuditViewData(ctx);
- const changeClass = await this._getOrUpdateClass(ctx, 'changeProject');
- // 获取用户人验证手机号
- const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
- const auth_mobile = pa.auth_mobile;
- // 判断并更新
- const renderData = {
- tender,
- change: ctx.change,
- changeConst,
- changeClass,
- auditConst: audit.changeProject,
- fileList,
- whiteList,
- authMobile: auth_mobile,
- returnUrl: this.app._.includes(ctx.request.headers.referer, '/tender/' + ctx.tender.id + '/change/project') ? ctx.request.headers.referer : null,
- jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.project_information),
- preUrl: '/tender/' + ctx.tender.id + '/change/project/' + ctx.change.id + '/information',
- };
- // 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)) {
- // data.accountGroup = accountGroup;
- // 获取所有项目参与者
- const accountList = await ctx.service.projectAccount.getAllDataByCondition({
- where: { project_id: ctx.session.sessionProject.id, enable: 1 },
- columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
- });
- renderData.accountList = accountList;
- const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
- renderData.accountGroup = unitList.map(item => {
- const groupList = accountList.filter(item1 => item1.company === item.name);
- return { groupName: item.name, groupList };
- });
- // }
- await this.layout('change/project_information.ejs', renderData, 'change/project_information_modal.ejs');
- } catch (err) {
- this.log(err);
- ctx.redirect('/tender/' + ctx.params.id + '/change/project');
- }
- }
- async projectReport(ctx) {
- try {
- const renderData = {
- change: ctx.change,
- tender: ctx.tender,
- jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.report),
- };
- await this.layout('change/project_report.ejs', renderData);
- } catch (err) {
- this.log(err);
- ctx.redirect('/tender/' + ctx.params.id + '/change/project');
- }
- }
- // 审批相关
- /**
- * 添加审批人
- * @param ctx
- * @return {Promise<void>}
- */
- async addProjectAudit(ctx) {
- try {
- const auditConst = audit.changeProject;
- const data = JSON.parse(ctx.request.body.data);
- const id = this.app._.toInteger(data.auditorId);
- if (isNaN(id) || id <= 0) {
- throw '参数错误';
- }
- // 检查权限等
- if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
- throw '您无权添加审核人';
- }
- if (ctx.change.status === auditConst.status.checking || ctx.change.status === auditConst.status.checked) {
- throw '当前不允许添加审核人';
- }
- // 判断是否是协审人,是则无法添加到审批人中
- const xsAuditorList = await ctx.service.changeProjectXsAudit.getAuditList(ctx.change.id);
- const xsAidList = this.app._.map(xsAuditorList, 'aid');
- if (this.app._.indexOf(xsAidList, id) !== -1) {
- throw '该用户已添加到协审人列表中,请删除该协审人再添加';
- }
- ctx.change.auditorList = await ctx.service.changeProjectAudit.getAuditors(ctx.change.id, ctx.change.times);
- // 检查审核人是否已存在
- const exist = this.app._.find(ctx.change.auditorList, { aid: id });
- if (exist) {
- throw '该审核人已存在,请勿重复添加';
- }
- // const shenpiInfo = await ctx.service.shenpiAudit.getDataByCondition({ tid: ctx.tender.id, sp_type: shenpiConst.sp_type.material, sp_status: shenpiConst.sp_status.gdzs });
- // const is_gdzs = shenpiInfo && ctx.tender.info.shenpi.material === shenpiConst.sp_status.gdzs ? 1 : 0;
- const result = await ctx.service.changeProjectAudit.addAuditor(ctx.change.id, id, ctx.change.times);
- if (!result) {
- throw '添加审核人失败';
- }
- const auditors = await ctx.service.changeProjectAudit.getAuditorsWithOwner(ctx.change.id, ctx.change.times);
- ctx.body = { err: 0, msg: '', data: auditors };
- } catch (err) {
- this.log(err);
- ctx.body = { err: 1, msg: err.toString(), data: null };
- }
- }
- /**
- * 移除审批人
- * @param ctx
- * @return {Promise<void>}
- */
- async deleteProjectAudit(ctx) {
- try {
- const data = JSON.parse(ctx.request.body.data);
- const id = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
- if (isNaN(id) || id <= 0) {
- throw '参数错误';
- }
- const result = await ctx.service.changeProjectAudit.deleteAuditor(ctx.change.id, id, ctx.change.times);
- if (!result) {
- throw '移除审核人失败';
- }
- const auditors = await ctx.service.changeProjectAudit.getAuditors(ctx.change.id, ctx.change.times);
- ctx.body = { err: 0, msg: '', data: auditors };
- } catch (err) {
- ctx.body = { err: 1, msg: err.toString(), data: null };
- }
- }
- /**
- * 添加协审人
- * @param ctx
- * @return {Promise<void>}
- */
- async addProjectXsAudit(ctx) {
- try {
- const auditConst = audit.changeProject;
- const data = JSON.parse(ctx.request.body.data);
- const id = this.app._.toInteger(data.auditorId);
- if (isNaN(id) || id <= 0) {
- throw '参数错误';
- }
- const times = ctx.change.status === auditConst.status.back ? ctx.change.times - 1 : ctx.change.times;
- const spAuditors = await ctx.service.changeProjectAudit.getAuditorsWithOwner(ctx.change.id, times);
- const aidLists = this.app._.map(spAuditors, 'aid');
- // 检查权限等
- if (this.app._.indexOf(aidLists, ctx.session.sessionUser.accountId) === -1) {
- throw '您无权添加协审人';
- }
- // 如果已经是审批人则无法添加此人为协审
- if (this.app._.indexOf(aidLists, id) !== -1) {
- throw '该用户为审批人或原报,无需添加至协审人';
- }
- const auditorList = await ctx.service.changeProjectXsAudit.getAuditList(ctx.change.id);
- // 检查审核人是否已存在
- const exist = this.app._.find(auditorList, { aid: id });
- if (exist) {
- throw '该协审人已存在,请勿重复添加';
- }
- const result = await ctx.service.changeProjectXsAudit.addAuditor(ctx.change.id, id);
- if (!result) {
- throw '添加协审人失败';
- }
- const auditors = await ctx.service.changeProjectXsAudit.getOneAudit(ctx.change.id, id);
- ctx.body = { err: 0, msg: '', data: auditors };
- } catch (err) {
- this.log(err);
- ctx.body = { err: 1, msg: err.toString(), data: null };
- }
- }
- /**
- * 移除协审人
- * @param ctx
- * @return {Promise<void>}
- */
- async deleteProjectXsAudit(ctx) {
- try {
- const data = JSON.parse(ctx.request.body.data);
- const id = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
- if (isNaN(id) || id <= 0) {
- throw '参数错误';
- }
- const result = await ctx.service.changeProjectXsAudit.deleteAuditor(ctx.change.id, id);
- if (!result) {
- throw '移除协审人失败';
- }
- const auditors = await ctx.service.changeProjectXsAudit.getAuditList(ctx.change.id);
- ctx.body = { err: 0, msg: '', data: auditors };
- } catch (err) {
- ctx.body = { err: 1, msg: err.toString(), data: null };
- }
- }
- /**
- * 上传附件
- * @param {*} ctx 上下文
- */
- async uploadProjectFile(ctx) {
- let stream;
- try {
- const auditConst = audit.changeProject;
- // this._checkAdvanceFileCanModify(ctx);
- const parts = this.ctx.multipart({
- autoFields: true,
- });
- const files = [];
- const create_time = Date.parse(new Date()) / 1000;
- let idx = 0;
- const extra_upload = ctx.change.status === auditConst.status.checked;
- while ((stream = await parts()) !== undefined) {
- if (!stream.filename) {
- // 如果没有传入直接返回
- return;
- }
- const fileInfo = path.parse(stream.filename);
- const filepath = `app/public/upload/${this.ctx.tender.id.toString()}/change_project/fujian_${create_time + idx.toString() + fileInfo.ext}`;
- // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, 'app', filepath));
- await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
- files.push({ filepath, name: stream.filename, ext: fileInfo.ext });
- ++idx;
- stream && (await sendToWormhole(stream));
- }
- const in_time = new Date();
- const payload = files.map(file => {
- let idx;
- if (Array.isArray(parts.field.name)) {
- idx = parts.field.name.findIndex(name => name === file.name);
- } else {
- idx = 'isString';
- }
- const newFile = {
- tid: ctx.tender.id,
- cpid: ctx.change.id,
- uid: ctx.session.sessionUser.accountId,
- filename: file.name,
- fileext: file.ext,
- filesize: ctx.helper.bytesToSize(idx === 'isString' ? parts.field.size : parts.field.size[idx]),
- filepath: file.filepath,
- upload_time: in_time,
- extra_upload,
- };
- return newFile;
- });
- // 执行文件信息写入数据库
- await ctx.service.changeProjectAtt.saveFileMsgToDb(payload);
- // 将最新的当前标段的所有文件信息返回
- const data = await ctx.service.changeProjectAtt.getAllChangeProjectAtt(ctx.tender.id, ctx.change.id);
- ctx.body = { err: 0, msg: '', data };
- } catch (err) {
- stream && (await sendToWormhole(stream));
- this.log(err);
- ctx.body = { err: 1, msg: err.toString(), data: null };
- }
- }
- /**
- * 删除附件
- * @param {Ojbect} ctx 上下文
- */
- async deleteProjectFile(ctx) {
- try {
- const { id } = JSON.parse(ctx.request.body.data);
- const fileInfo = await ctx.service.changeProjectAtt.getDataById(id);
- if (fileInfo || Object.keys(fileInfo).length) {
- // 先删除文件
- // await fs.unlinkSync(path.resolve(this.app.baseDir, './app', fileInfo.filepath));
- await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
- // 再删除数据库
- await ctx.service.changeProjectAtt.delete(id);
- } else {
- throw '不存在该文件';
- }
- const data = await ctx.service.changeProjectAtt.getAllChangeProjectAtt(ctx.tender.id, ctx.change.id);
- ctx.body = { err: 0, msg: '请求成功', data };
- } catch (err) {
- this.log(err);
- ctx.body = { err: 1, msg: err.toString(), data: null };
- }
- }
- /**
- * 下载附件
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async downloadProjectFile(ctx) {
- const id = ctx.params.fid;
- if (id) {
- try {
- const fileInfo = await ctx.service.changeProjectAtt.getDataById(id);
- if (fileInfo !== undefined && fileInfo !== '') {
- // const fileName = path.join(__dirname, '../', fileInfo.filepath);
- // 解决中文无法下载问题
- const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
- let disposition = '';
- if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
- disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename);
- } else if (userAgent.indexOf('firefox') >= 0) {
- disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename) + '"';
- } else {
- /* safari等其他非主流浏览器只能自求多福了 */
- disposition = 'attachment; filename=' + new Buffer(fileInfo.filename).toString('binary');
- }
- ctx.response.set({
- 'Content-Type': 'application/octet-stream',
- 'Content-Disposition': disposition,
- 'Content-Length': fileInfo.filesize,
- });
- // ctx.body = await fs.createReadStream(fileName);
- ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
- } else {
- throw '不存在该文件';
- }
- } catch (err) {
- this.log(err);
- this.setMessage(err.toString(), this.messageType.ERROR);
- }
- }
- }
- async projectInformationSave(ctx) {
- try {
- const data = JSON.parse(ctx.request.body.data);
- if (data.name === 'code') {
- const info = await ctx.service.changeProject.isRepeat(ctx.change.id, data.val, ctx.tender.id, ctx.change.type);
- if (info) {
- throw '该编号已存在';
- }
- }
- const result = await ctx.service.changeProject.saveInfo(ctx.change.id, data);
- if (!result) {
- throw '修改失败';
- }
- ctx.body = { err: 0, msg: '请求成功', data: null };
- } catch (err) {
- this.log(err);
- ctx.body = { err: 1, msg: err.toString(), data: null };
- }
- }
- /**
- * 上报和重新上报
- * @param ctx
- * @return {Promise<void>}
- */
- async startProjectAudit(ctx) {
- try {
- const auditConst = audit.changeProject;
- // 检查权限等
- if (!ctx.change) {
- throw '数据错误';
- }
- if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
- throw '您无权上报该期数据';
- }
- if (ctx.change.status === auditConst.status.checking || ctx.change.status === auditConst.status.checked) {
- throw '该材料调差期数据当前无法上报';
- }
- await ctx.service.changeProjectAudit.start(ctx.change.id, ctx.change.times);
- ctx.redirect(ctx.request.header.referer);
- } catch (err) {
- this.log(err);
- ctx.session.postError = err.toString();
- ctx.redirect(ctx.request.header.referer);
- }
- }
- /**
- * 审批
- * @param ctx
- * @return {Promise<void>}
- */
- async checkProjectAudit(ctx) {
- try {
- const auditConst = audit.changeProject;
- if (!ctx.change || ctx.change.status !== auditConst.status.checking) {
- throw '当前材料调差期数据有误';
- }
- if (!ctx.change.curAuditor || ctx.change.curAuditor.aid !== ctx.session.sessionUser.accountId) {
- throw '您无权进行该操作';
- }
- const data = {
- checkType: parseInt(ctx.request.body.checkType),
- opinion: ctx.request.body.opinion,
- };
- if (!data.checkType || isNaN(data.checkType)) {
- throw '提交数据错误';
- }
- // if (data.checkType === auditConst.status.checkNo) {
- // if (!data.checkType || isNaN(data.checkType)) {
- // throw '提交数据错误';
- // }
- // }
- await ctx.service.changeProjectAudit.check(ctx.change.id, data, ctx.change.times);
- ctx.redirect(ctx.request.header.referer);
- } catch (err) {
- this.log(err);
- ctx.session.postError = err.toString();
- ctx.redirect(ctx.request.header.referer);
- }
- }
- /**
- * 撤回审批
- * @param ctx
- * @return {Promise<void>}
- */
- async checkProjectAuditCancel(ctx) {
- try {
- if (!ctx.change.cancancel) throw '您无权进行该操作';
- await ctx.service.changeProjectAudit.checkCancel(ctx.change);
- ctx.body = { err: 0, url: ctx.request.header.referer, msg: '' };
- } catch (err) {
- this.log(err);
- ctx.body = { err: 1, msg: err };
- }
- }
- /**
- * 变更立项重新审批
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async checkProjectAgain(ctx) {
- try {
- // 获取终审
- const auditInfo = await this.ctx.service.changeProjectAudit.getAuditorByStatus(ctx.change.id, audit.changeProject.status.checked);
- if (ctx.change.status !== audit.changeProject.status.checked || ctx.session.sessionUser.accountId !== auditInfo.aid) {
- throw '您无权进行该操作';
- }
- // 判断是否被变更申请调用了,是则无法发起修订
- const projectInfo = await ctx.service.changeApply.getDataByCondition({ tid: ctx.tender.id, project_code: ctx.change.code });
- if (projectInfo) {
- throw '该变更立项已被变更申请调用,无法发起重新审批';
- }
- if (ctx.session.sessionUser.loginStatus === 0) {
- const code = ctx.request.body.code;
- const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
- if (!pa.auth_mobile) {
- throw '未绑定手机号';
- }
- const cacheKey = 'smsCode:' + ctx.session.sessionUser.accountId;
- const cacheCode = await app.redis.get(cacheKey);
- // console.log(cacheCode);
- if (cacheCode === null || code === undefined || cacheCode !== (code + pa.auth_mobile)) {
- throw '验证码不正确!';
- }
- }
- // 重新审批
- const result = await ctx.service.changeProjectAudit.checkAgain(ctx.change);
- if (!result) {
- throw '重新审批失败';
- }
- // ctx.redirect('/tender/' + changeData.tid + '/change/' + changeData.cid + '/info');
- ctx.body = {
- err: 0,
- url: ctx.request.header.referer,
- msg: '',
- };
- } catch (err) {
- console.log(err);
- // ctx.redirect(ctx.request.header.referer);
- ctx.body = {
- err: 1,
- // url: ctx.request.header.referer,
- msg: err,
- };
- }
- }
- /**
- * 变更立项修订重新上报
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async checkProjectRevise(ctx) {
- try {
- if (ctx.change.status !== audit.changeProject.status.checked || ctx.session.sessionUser.accountId !== ctx.change.uid) {
- throw '您无权进行该操作';
- }
- // 判断是否被变更申请调用了,是则无法发起修订
- const projectInfo = await ctx.service.changeApply.getDataByCondition({ tid: ctx.tender.id, project_code: ctx.change.code });
- if (projectInfo) {
- throw '该变更立项已被变更申请调用,无法发起修订';
- }
- if (ctx.session.sessionUser.loginStatus === 0) {
- const code = ctx.request.body.code;
- const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
- if (!pa.auth_mobile) {
- throw '未绑定手机号';
- }
- const cacheKey = 'smsCode:' + ctx.session.sessionUser.accountId;
- const cacheCode = await app.redis.get(cacheKey);
- // console.log(cacheCode);
- if (cacheCode === null || code === undefined || cacheCode !== (code + pa.auth_mobile)) {
- throw '验证码不正确!';
- }
- }
- // 重新审批
- const result = await ctx.service.changeProjectAudit.checkRevise(ctx.change);
- if (!result) {
- throw '修订发起失败';
- }
- // ctx.redirect('/tender/' + changeData.tid + '/change/' + changeData.cid + '/info');
- ctx.body = {
- err: 0,
- url: ctx.request.header.referer,
- msg: '',
- };
- } catch (err) {
- console.log(err);
- // ctx.redirect(ctx.request.header.referer);
- ctx.body = {
- err: 1,
- // url: ctx.request.header.referer,
- msg: err,
- };
- }
- }
- /**
- * 变更令撤销修订
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async cancelProjectRevise(ctx) {
- try {
- if (!(ctx.change.status === audit.changeProject.status.revise && (ctx.session.sessionUser.accountId === ctx.change.uid || ctx.session.sessionUser.accountId === ctx.session.sessionUser.is_admin))) {
- throw '您无权进行该操作';
- }
- // 重新审批
- const result = await ctx.service.changeProjectAudit.cancelRevise(ctx.change);
- if (!result) {
- throw '撤销修订失败';
- }
- ctx.body = {
- err: 0,
- url: ctx.request.header.referer,
- msg: '',
- };
- } catch (err) {
- console.log(err);
- ctx.body = {
- err: 1,
- msg: err,
- };
- }
- }
- async _filterChangesApply(ctx, status = 0) {
- const tenderId = ctx.params.id;
- ctx.session.sessionUser.tenderId = tenderId;
- const tender = await this.service.tender.getDataById(ctx.tender.id);
- // const tender = ctx.tender;
- // const tenderList = await this.service.tender.getList();
- const page = ctx.page;
- const pageSize = ctx.pageSize;
- const sorts = ctx.query.sort ? ctx.query.sort : 0;
- const orders = ctx.query.order ? ctx.query.order : 0;
- const changes = await ctx.service.changeApply.getListByStatus(tender.id, status, 1, sorts, orders);
- const total = await ctx.service.changeApply.getCountByStatus(tender.id, status);
- let page_total = 0;
- const tp = await ctx.service.changeApply.getTp(tender.id, status);
- for (const c of changes) {
- c.curAuditor = await ctx.service.changeApplyAudit.getAuditorByStatus(c.id, c.status, c.times);
- page_total = ctx.helper.add(page_total, c.total_price);
- }
- const tender_userInfo = await ctx.service.projectAccount.getDataById(ctx.tender.data.user_id);
- // 分页相关
- const pageInfo = {
- page,
- pageSizeSelect: 1,
- pageSize,
- total_num: total,
- total: Math.ceil(total / pageSize),
- queryData: JSON.stringify(ctx.urlInfo.query),
- };
- const filter = JSON.parse(JSON.stringify(audit.changeApply.filter));
- filter.count = [];
- 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);
- 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);
- 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);
- 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);
- // 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);
- let codeRule = [];
- let c_connector = '1';
- let c_rule_first = 1;
- const rule_type = 'apply';
- if (tender.c_code_rules) {
- const c_code_rules = JSON.parse(tender.c_code_rules);
- codeRule = c_code_rules[rule_type + '_rule'] !== undefined ? c_code_rules[rule_type + '_rule'] : [];
- c_connector = c_code_rules[rule_type + '_connector'] !== undefined ? c_code_rules[rule_type + '_connector'] : '1';
- c_rule_first = c_code_rules[rule_type + '_rule_first'] !== undefined ? c_code_rules[rule_type + '_rule_first'] : 1;
- }
- for (const rule of codeRule) {
- switch (rule.rule_type) {
- case codeRuleConst.measure.ruleType.dealCode:
- rule.preview = ctx.tender.info.deal_info.dealCode;
- break;
- case codeRuleConst.measure.ruleType.tenderName:
- rule.preview = tender.name;
- break;
- case codeRuleConst.measure.ruleType.inDate:
- rule.preview = moment().format('YYYY');
- break;
- case codeRuleConst.measure.ruleType.text:
- rule.preview = rule.text;
- break;
- case codeRuleConst.measure.ruleType.addNo:
- const s = '0000000000';
- rule.preview = s.substr(s.length - rule.format);
- break;
- default: break;
- }
- }
- const changeProjectList = await ctx.service.changeProject.getAllDataByCondition({ where: { tid: tender.id, status: audit.changeProject.status.checked } });
- const allProjectCodes = await ctx.service.changeApply.getAllDataByCondition({
- columns: ['project_code'],
- where: {
- tid: tender.id,
- },
- });
- const pcLists = allProjectCodes.length > 0 ? ctx.app._.uniq(ctx.app._.map(allProjectCodes, 'project_code')) : [];
- const renderData = {
- uid: ctx.session.sessionUser.accountId,
- tender,
- pageInfo,
- changes,
- status,
- rule_type,
- codeRule,
- c_connector,
- c_rule_first,
- filter,
- apply_username: tender_userInfo ? tender_userInfo.name : null,
- ruleType: codeRuleConst.ruleType[rule_type],
- dealCode: ctx.tender.info.deal_info.dealCode,
- auditConst: audit.changeApply,
- ruleConst: codeRuleConst.measure,
- changeConst,
- changeProjectList,
- pcLists,
- tp,
- page_total,
- tenderMenu: this.menu.tenderMenu,
- preUrl: '/tender/' + tender.id,
- jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.apply),
- };
- await this.layout('change/apply.ejs', renderData, 'change/apply_modal.ejs');
- }
- /**
- * 变更申请列表 页面 (Get)
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async apply(ctx) {
- try {
- await this._filterChangesApply(ctx);
- } catch (err) {
- this.log(err);
- ctx.redirect('/dashboard');
- }
- }
- /**
- * 变更管理 状态筛选 页面 (Get)
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async applyStatus(ctx) {
- try {
- const status = parseInt(ctx.params.status);
- await this._filterChangesApply(ctx, status);
- } catch (err) {
- this.logger.error(err);
- ctx.redirect('/tender/' + ctx.params.id + '/change/apply');
- }
- }
- /**
- * 新增变更立项 (Post)
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async applyAdd(ctx) {
- try {
- const tenderId = ctx.params.id;
- if (!tenderId) {
- throw '当前未打开标段';
- }
- const data = JSON.parse(ctx.request.body.data);
- if (!data.code || data.code === '') {
- throw '变更申请编号不能为空';
- }
- const change = await ctx.service.changeApply.add(tenderId, ctx.session.sessionUser.accountId, data.code, data.project_code, data.name);
- ctx.body = { err: 0, msg: '', data: change };
- } catch (err) {
- this.log(err);
- ctx.body = { err: 1, msg: err.toString() };
- }
- }
- /**
- * 删除变更立项
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async applyDelete(ctx) {
- try {
- const result = await ctx.service.changeApply.delete(ctx.request.body.caid);
- if (!result) {
- throw '删除变更立项失败';
- }
- ctx.redirect(ctx.request.header.referer);
- } catch (err) {
- console.log(err);
- ctx.redirect(ctx.request.header.referer);
- }
- }
- /**
- * 获取审批界面所需的 原报、审批人数据等
- * @param ctx
- * @return {Promise<void>}
- * @private
- */
- async _getChangeApplyAuditViewData(ctx) {
- const auditConst = audit.changeApply;
- const times = ctx.change.status === auditConst.status.checkNo || ctx.change.status === auditConst.status.revise ? ctx.change.times - 1 : ctx.change.times;
- ctx.change.user = await ctx.service.projectAccount.getAccountInfoById(ctx.change.uid);
- ctx.change.auditHistory = [];
- if (times >= 1) {
- for (let i = 1; i <= times; i++) {
- ctx.change.auditHistory.push(await ctx.service.changeApplyAudit.getAuditors(ctx.change.id, i));
- }
- }
- // 获取审批流程中左边列表
- ctx.change.auditors2 = (ctx.change.status === auditConst.status.checkNo || ctx.change.status === auditConst.status.revise) && ctx.change.uid !== ctx.session.sessionUser.accountId ?
- await ctx.service.changeApplyAudit.getAuditorsWithOwner(ctx.change.id, times) :
- await ctx.service.changeApplyAudit.getAuditorsWithOwner(ctx.change.id, ctx.change.times);
- if (ctx.change.status === auditConst.status.uncheck || ctx.change.status === auditConst.status.checkNo || ctx.change.status === auditConst.status.revise) {
- ctx.change.auditorList = await ctx.service.changeApplyAudit.getAuditors(ctx.change.id, ctx.change.times);
- }
- }
- async applyInformation(ctx) {
- try {
- const whiteList = this.ctx.app.config.multipart.whitelist;
- const tender = await ctx.service.tender.getDataById(ctx.tender.id);
- await this._getChangeApplyAuditViewData(ctx);
- // 获取清单列表
- const changeList = await ctx.service.changeApplyList.getList(ctx.change.id);
- // 获取附件列表
- const fileList = await ctx.service.changeApplyAtt.getAllChangeApplyAtt(ctx.tender.id, ctx.change.id);
- const changeClass = await this._getOrUpdateClass(ctx, 'changeApply');
- // 获取用户人验证手机号
- const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
- const auth_mobile = pa.auth_mobile;
- const renderData = {
- tender,
- change: ctx.change,
- listRule: tender.c_apply_list_rule ? JSON.parse(tender.c_apply_list_rule) : { source: 1, rule: ['unit', 'unit_price'] },
- changeList,
- changeConst,
- changeClass,
- auditConst: audit.changeApply,
- fileList,
- whiteList,
- authMobile: auth_mobile,
- tpUnit: ctx.change.decimal ? ctx.change.decimal.tp : ctx.tender.info.decimal.tp,
- upUnit: ctx.change.decimal ? ctx.change.decimal.up : ctx.tender.info.decimal.up,
- changeUnits: changeConst.units,
- precision: ctx.change.decimal && ctx.change.decimal.precision ? ctx.change.decimal.precision : ctx.tender.info.precision,
- 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,
- jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.apply_information),
- preUrl: '/tender/' + ctx.tender.id + '/change/apply/' + ctx.change.id + '/information',
- };
- 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)) {
- // data.accountGroup = accountGroup;
- // 获取所有项目参与者
- const accountList = await ctx.service.projectAccount.getAllDataByCondition({
- where: { project_id: ctx.session.sessionProject.id, enable: 1 },
- columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
- });
- renderData.accountList = accountList;
- const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
- renderData.accountGroup = unitList.map(item => {
- const groupList = accountList.filter(item1 => item1.company === item.name);
- return { groupName: item.name, groupList };
- });
- }
- await this.layout('change/apply_information.ejs', renderData, 'change/apply_information_modal.ejs');
- } catch (err) {
- this.log(err);
- ctx.redirect('/tender/' + ctx.params.id + '/change/apply');
- }
- }
- async applyReport(ctx) {
- try {
- const renderData = {
- change: ctx.change,
- tender: ctx.tender,
- jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.report),
- };
- await this.layout('change/apply_report.ejs', renderData);
- } catch (err) {
- this.log(err);
- ctx.redirect('/tender/' + ctx.params.id + '/change/apply');
- }
- }
- async applyInformationNotice(ctx) {
- try {
- const tender = await ctx.service.tender.getDataById(ctx.tender.id);
- // 获取附件列表
- const fileList = await ctx.service.changeApplyAtt.getAllChangeApplyAtt(ctx.tender.id, ctx.change.id);
- await this._getChangeApplyAuditViewData(ctx);
- const renderData = {
- tender,
- change: ctx.change,
- changeConst,
- auditConst: audit.changeApply,
- fileList,
- returnUrl: this.app._.includes(ctx.request.headers.referer, '/tender/' + ctx.tender.id + '/change/apply') ? ctx.request.headers.referer : null,
- jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.apply_information_notice),
- preUrl: '/tender/' + ctx.tender.id + '/change/apply/' + ctx.change.id + '/information/notice',
- };
- await this.layout('change/apply_information_notice.ejs', renderData, 'change/apply_information_notice_modal.ejs');
- } catch (err) {
- this.log(err);
- ctx.redirect('/tender/' + ctx.params.id + '/change');
- }
- }
- // 审批相关
- /**
- * 添加审批人
- * @param ctx
- * @return {Promise<void>}
- */
- async addApplyAudit(ctx) {
- try {
- const auditConst = audit.changeApply;
- const data = JSON.parse(ctx.request.body.data);
- const id = this.app._.toInteger(data.auditorId);
- if (isNaN(id) || id <= 0) {
- throw '参数错误';
- }
- // 检查权限等
- if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
- throw '您无权添加审核人';
- }
- if (ctx.change.status === auditConst.status.checking || ctx.change.status === auditConst.status.checked) {
- throw '当前不允许添加审核人';
- }
- ctx.change.auditorList = await ctx.service.changeApplyAudit.getAuditors(ctx.change.id, ctx.change.times);
- // 检查审核人是否已存在
- const exist = this.app._.find(ctx.change.auditorList, { aid: id });
- if (exist) {
- throw '该审核人已存在,请勿重复添加';
- }
- // const shenpiInfo = await ctx.service.shenpiAudit.getDataByCondition({ tid: ctx.tender.id, sp_type: shenpiConst.sp_type.material, sp_status: shenpiConst.sp_status.gdzs });
- // const is_gdzs = shenpiInfo && ctx.tender.info.shenpi.material === shenpiConst.sp_status.gdzs ? 1 : 0;
- const result = await ctx.service.changeApplyAudit.addAuditor(ctx.change.id, id, ctx.change.times);
- if (!result) {
- throw '添加审核人失败';
- }
- const auditors = await ctx.service.changeApplyAudit.getAuditorsWithOwner(ctx.change.id, ctx.change.times);
- ctx.body = { err: 0, msg: '', data: auditors };
- } catch (err) {
- this.log(err);
- ctx.body = { err: 1, msg: err.toString(), data: null };
- }
- }
- /**
- * 移除审批人
- * @param ctx
- * @return {Promise<void>}
- */
- async deleteApplyAudit(ctx) {
- try {
- const data = JSON.parse(ctx.request.body.data);
- const id = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
- if (isNaN(id) || id <= 0) {
- throw '参数错误';
- }
- const result = await ctx.service.changeApplyAudit.deleteAuditor(ctx.change.id, id, ctx.change.times);
- if (!result) {
- throw '移除审核人失败';
- }
- const auditors = await ctx.service.changeApplyAudit.getAuditors(ctx.change.id, ctx.change.times);
- ctx.body = { err: 0, msg: '', data: auditors };
- } catch (err) {
- ctx.body = { err: 1, msg: err.toString(), data: null };
- }
- }
- /**
- * 上传附件
- * @param {*} ctx 上下文
- */
- async uploadApplyFile(ctx) {
- let stream;
- try {
- const auditConst = audit.changeApply;
- // this._checkAdvanceFileCanModify(ctx);
- const parts = this.ctx.multipart({
- autoFields: true,
- });
- const files = [];
- const create_time = Date.parse(new Date()) / 1000;
- let idx = 0;
- const extra_upload = ctx.change.status === auditConst.status.checked;
- while ((stream = await parts()) !== undefined) {
- if (!stream.filename) {
- // 如果没有传入直接返回
- return;
- }
- const fileInfo = path.parse(stream.filename);
- const filepath = `app/public/upload/${this.ctx.tender.id.toString()}/change_apply/fujian_${create_time + idx.toString() + fileInfo.ext}`;
- // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, 'app', filepath));
- await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
- files.push({ filepath, name: stream.filename, ext: fileInfo.ext });
- ++idx;
- stream && (await sendToWormhole(stream));
- }
- const in_time = new Date();
- const payload = files.map(file => {
- let idx;
- if (Array.isArray(parts.field.name)) {
- idx = parts.field.name.findIndex(name => name === file.name);
- } else {
- idx = 'isString';
- }
- const newFile = {
- tid: ctx.tender.id,
- caid: ctx.change.id,
- uid: ctx.session.sessionUser.accountId,
- type: parts.field.type,
- filename: file.name,
- fileext: file.ext,
- filesize: ctx.helper.bytesToSize(idx === 'isString' ? parts.field.size : parts.field.size[idx]),
- filepath: file.filepath,
- upload_time: in_time,
- extra_upload,
- };
- return newFile;
- });
- // 执行文件信息写入数据库
- await ctx.service.changeApplyAtt.saveFileMsgToDb(payload);
- // 将最新的当前标段的所有文件信息返回
- const data = await ctx.service.changeApplyAtt.getAllChangeApplyAtt(ctx.tender.id, ctx.change.id);
- ctx.body = { err: 0, msg: '', data };
- } catch (err) {
- stream && (await sendToWormhole(stream));
- this.log(err);
- ctx.body = { err: 1, msg: err.toString(), data: null };
- }
- }
- /**
- * 删除附件
- * @param {Ojbect} ctx 上下文
- */
- async deleteApplyFile(ctx) {
- try {
- const { id } = JSON.parse(ctx.request.body.data);
- const fileInfo = await ctx.service.changeApplyAtt.getDataById(id);
- if (fileInfo || Object.keys(fileInfo).length) {
- // 先删除文件
- // await fs.unlinkSync(path.resolve(this.app.baseDir, './app', fileInfo.filepath));
- await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
- // 再删除数据库
- await ctx.service.changeApplyAtt.delete(id);
- } else {
- throw '不存在该文件';
- }
- const data = await ctx.service.changeApplyAtt.getAllChangeApplyAtt(ctx.tender.id, ctx.change.id);
- ctx.body = { err: 0, msg: '请求成功', data };
- } catch (err) {
- this.log(err);
- ctx.body = { err: 1, msg: err.toString(), data: null };
- }
- }
- /**
- * 下载附件
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async downloadApplyFile(ctx) {
- const id = ctx.params.fid;
- if (id) {
- try {
- const fileInfo = await ctx.service.changeApplyAtt.getDataById(id);
- if (fileInfo !== undefined && fileInfo !== '') {
- // const fileName = path.join(__dirname, '../', fileInfo.filepath);
- // 解决中文无法下载问题
- const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
- let disposition = '';
- if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
- disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename);
- } else if (userAgent.indexOf('firefox') >= 0) {
- disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename) + '"';
- } else {
- /* safari等其他非主流浏览器只能自求多福了 */
- disposition = 'attachment; filename=' + new Buffer(fileInfo.filename).toString('binary');
- }
- ctx.response.set({
- 'Content-Type': 'application/octet-stream',
- 'Content-Disposition': disposition,
- 'Content-Length': fileInfo.filesize,
- });
- // ctx.body = await fs.createReadStream(fileName);
- ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
- } else {
- throw '不存在该文件';
- }
- } catch (err) {
- this.log(err);
- this.setMessage(err.toString(), this.messageType.ERROR);
- }
- }
- }
- async applyInformationSave(ctx) {
- try {
- const data = JSON.parse(ctx.request.body.data);
- if (data.name === 'code') {
- const info = await ctx.service.changeApply.isRepeat(ctx.change.id, data.val, ctx.tender.id);
- if (info) {
- throw '该编号已存在';
- }
- }
- const result = await ctx.service.changeApply.saveInfo(ctx.change.id, data);
- if (!result) {
- throw '修改失败';
- }
- ctx.body = { err: 0, msg: '请求成功', data: null };
- } catch (err) {
- this.log(err);
- ctx.body = { err: 1, msg: err.toString(), data: null };
- }
- }
- /**
- * 上报和重新上报
- * @param ctx
- * @return {Promise<void>}
- */
- async startApplyAudit(ctx) {
- try {
- const auditConst = audit.changeApply;
- // 检查权限等
- if (!ctx.change) {
- throw '数据错误';
- }
- if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
- throw '您无权上报该期数据';
- }
- if (ctx.change.status === auditConst.status.checking || ctx.change.status === auditConst.status.checked) {
- throw '该材料调差期数据当前无法上报';
- }
- await ctx.service.changeApplyAudit.start(ctx.change.id, ctx.change.times);
- ctx.redirect(ctx.request.header.referer);
- } catch (err) {
- this.log(err);
- ctx.session.postError = err.toString();
- ctx.redirect(ctx.request.header.referer);
- }
- }
- /**
- * 审批
- * @param ctx
- * @return {Promise<void>}
- */
- async checkApplyAudit(ctx) {
- try {
- const auditConst = audit.changeApply;
- if (!ctx.change || ctx.change.status !== auditConst.status.checking) {
- throw '当前材料调差期数据有误';
- }
- if (!ctx.change.curAuditor || ctx.change.curAuditor.aid !== ctx.session.sessionUser.accountId) {
- throw '您无权进行该操作';
- }
- const data = {
- checkType: parseInt(ctx.request.body.checkType),
- opinion: ctx.request.body.opinion,
- };
- if (!data.checkType || isNaN(data.checkType)) {
- throw '提交数据错误';
- }
- if (ctx.request.body.notice_uid) {
- data.notice_code = ctx.request.body.notice_code;
- data.notice_uid = ctx.request.body.notice_uid;
- }
- // if (data.checkType === auditConst.status.checkNo) {
- // if (!data.checkType || isNaN(data.checkType)) {
- // throw '提交数据错误';
- // }
- // }
- await ctx.service.changeApplyAudit.check(ctx.change.id, data, ctx.change.times);
- ctx.redirect(ctx.request.header.referer);
- } catch (err) {
- this.log(err);
- ctx.session.postError = err.toString();
- ctx.redirect(ctx.request.header.referer);
- }
- }
- /**
- * 变更清单 - 操作 (Ajax)
- * @param ctx
- * @return {Promise<void>}
- */
- async saveApplyListsData(ctx) {
- try {
- const data = JSON.parse(ctx.request.body.data);
- const responseData = {
- err: 0,
- msg: '',
- data: {},
- };
- switch (data.type) {
- case 'add':
- responseData.data = await ctx.service.changeApplyList.add(data.updateData);
- break;
- case 'batchadd':
- responseData.data = await ctx.service.changeApplyList.batchAdd(data);
- break;
- case 'del':
- await ctx.service.changeApplyList.del(data.ids);
- // 取所有工料表
- responseData.data = await ctx.service.changeApplyList.getList(ctx.change.id);
- break;
- case 'update':
- // if (data.updateData.code === '' || data.updateData.code === null) {
- // throw '请先输入编号';
- // }
- await ctx.service.changeApplyList.save(data.updateData);
- break;
- case 'paste':
- await ctx.service.changeApplyList.saveDatas(data.updateData);
- // 取所有工料表
- responseData.data = await ctx.service.changeApplyList.getList(ctx.change.id);
- break;
- case 'paste_amount_rows':
- await ctx.service.changeApplyList.saveDatas(data.updateData);
- responseData.data = await ctx.service.changeApplyList.getList(ctx.change.id);
- break;
- case 'list_rule':
- const result = await ctx.service.tender.saveTenderData(ctx.tender.id, { c_apply_list_rule: data.postData });
- if (!result) {
- throw '修改失败';
- }
- break;
- // case 'update_tp':
- // await ctx.service.changeApply.saveInfo(ctx.change.id, { name: 'total_price', val: data.updateData });
- // break;
- default: throw '参数有误';
- }
- ctx.body = responseData;
- } catch (err) {
- this.log(err);
- ctx.body = { err: 1, msg: err.toString(), data: null };
- }
- }
- /**
- * 撤回审批
- * @param ctx
- * @return {Promise<void>}
- */
- async checkApplyAuditCancel(ctx) {
- try {
- if (!ctx.change.cancancel) throw '您无权进行该操作';
- await ctx.service.changeApplyAudit.checkCancel(ctx.change);
- ctx.body = { err: 0, url: ctx.request.header.referer, msg: '' };
- } catch (err) {
- this.log(err);
- ctx.body = { err: 1, msg: err };
- }
- }
- /**
- * 变更申请重新审批
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async checkApplyAgain(ctx) {
- try {
- // 获取终审
- const auditInfo = await this.ctx.service.changeApplyAudit.getAuditorByStatus(ctx.change.id, audit.changeApply.status.checked);
- if (ctx.change.status !== audit.changeApply.status.checked || ctx.session.sessionUser.accountId !== auditInfo.aid) {
- throw '您无权进行该操作';
- }
- // 判断是否被变更申请调用了,是则无法发起修订
- const projectInfo = await ctx.service.changePlan.getDataByCondition({ tid: ctx.tender.id, apply_code: ctx.change.code });
- if (projectInfo) {
- throw '该变更申请已被变更方案调用,无法发起重新审批';
- }
- if (ctx.session.sessionUser.loginStatus === 0) {
- const code = ctx.request.body.code;
- const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
- if (!pa.auth_mobile) {
- throw '未绑定手机号';
- }
- const cacheKey = 'smsCode:' + ctx.session.sessionUser.accountId;
- const cacheCode = await app.redis.get(cacheKey);
- // console.log(cacheCode);
- if (cacheCode === null || code === undefined || cacheCode !== (code + pa.auth_mobile)) {
- throw '验证码不正确!';
- }
- }
- // 重新审批
- const result = await ctx.service.changeApplyAudit.checkAgain(ctx.change);
- if (!result) {
- throw '重新审批失败';
- }
- // ctx.redirect('/tender/' + changeData.tid + '/change/' + changeData.cid + '/info');
- ctx.body = {
- err: 0,
- url: ctx.request.header.referer,
- msg: '',
- };
- } catch (err) {
- console.log(err);
- // ctx.redirect(ctx.request.header.referer);
- ctx.body = {
- err: 1,
- // url: ctx.request.header.referer,
- msg: err,
- };
- }
- }
- /**
- * 变更申请修订重新上报
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async checkApplyRevise(ctx) {
- try {
- if (ctx.change.status !== audit.changeApply.status.checked || ctx.session.sessionUser.accountId !== ctx.change.uid) {
- throw '您无权进行该操作';
- }
- // 判断是否被变更申请调用了,是则无法发起修订
- const projectInfo = await ctx.service.changePlan.getDataByCondition({ tid: ctx.tender.id, apply_code: ctx.change.code });
- if (projectInfo) {
- throw '该变更申请已被变更方案调用,无法发起修订';
- }
- if (ctx.session.sessionUser.loginStatus === 0) {
- const code = ctx.request.body.code;
- const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
- if (!pa.auth_mobile) {
- throw '未绑定手机号';
- }
- const cacheKey = 'smsCode:' + ctx.session.sessionUser.accountId;
- const cacheCode = await app.redis.get(cacheKey);
- // console.log(cacheCode);
- if (cacheCode === null || code === undefined || cacheCode !== (code + pa.auth_mobile)) {
- throw '验证码不正确!';
- }
- }
- // 重新审批
- const result = await ctx.service.changeApplyAudit.checkRevise(ctx.change);
- if (!result) {
- throw '修订发起失败';
- }
- // ctx.redirect('/tender/' + changeData.tid + '/change/' + changeData.cid + '/info');
- ctx.body = {
- err: 0,
- url: ctx.request.header.referer,
- msg: '',
- };
- } catch (err) {
- console.log(err);
- // ctx.redirect(ctx.request.header.referer);
- ctx.body = {
- err: 1,
- // url: ctx.request.header.referer,
- msg: err,
- };
- }
- }
- /**
- * 变更令撤销修订
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async cancelApplyRevise(ctx) {
- try {
- if (!(ctx.change.status === audit.changeApply.status.revise && (ctx.session.sessionUser.accountId === ctx.change.uid || ctx.session.sessionUser.accountId === ctx.session.sessionUser.is_admin))) {
- throw '您无权进行该操作';
- }
- // 重新审批
- const result = await ctx.service.changeApplyAudit.cancelRevise(ctx.change);
- if (!result) {
- throw '撤销修订失败';
- }
- ctx.body = {
- err: 0,
- url: ctx.request.header.referer,
- msg: '',
- };
- } catch (err) {
- console.log(err);
- ctx.body = {
- err: 1,
- msg: err,
- };
- }
- }
- // 变更方案
- async _filterChangesPlan(ctx, status = 0) {
- const tenderId = ctx.params.id;
- ctx.session.sessionUser.tenderId = tenderId;
- const tender = await this.service.tender.getDataById(ctx.tender.id);
- // const tender = ctx.tender;
- // const tenderList = await this.service.tender.getList();
- const page = ctx.page;
- const pageSize = ctx.pageSize;
- const sorts = ctx.query.sort ? ctx.query.sort : 0;
- const orders = ctx.query.order ? ctx.query.order : 0;
- const changes = await ctx.service.changePlan.getListByStatus(tender.id, status, 1, sorts, orders);
- const total = await ctx.service.changePlan.getCountByStatus(tender.id, status);
- let page_total = 0;
- const tp = await ctx.service.changePlan.getTp(tender.id, status);
- for (const c of changes) {
- c.curAuditor = await ctx.service.changePlanAudit.getAuditorByStatus(c.id, c.status, c.times);
- page_total = ctx.helper.add(page_total, c.total_price);
- }
- const tender_userInfo = await ctx.service.projectAccount.getDataById(ctx.tender.data.user_id);
- // 分页相关
- const pageInfo = {
- page,
- pageSizeSelect: 1,
- pageSize,
- total_num: total,
- total: Math.ceil(total / pageSize),
- queryData: JSON.stringify(ctx.urlInfo.query),
- };
- const filter = JSON.parse(JSON.stringify(audit.changePlan.filter));
- filter.count = [];
- 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);
- 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);
- 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);
- 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);
- // 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);
- let codeRule = [];
- let c_connector = '1';
- let c_rule_first = 1;
- const rule_type = 'plan';
- if (tender.c_code_rules) {
- const c_code_rules = JSON.parse(tender.c_code_rules);
- codeRule = c_code_rules[rule_type + '_rule'] !== undefined ? c_code_rules[rule_type + '_rule'] : [];
- c_connector = c_code_rules[rule_type + '_connector'] !== undefined ? c_code_rules[rule_type + '_connector'] : '1';
- c_rule_first = c_code_rules[rule_type + '_rule_first'] !== undefined ? c_code_rules[rule_type + '_rule_first'] : 1;
- }
- for (const rule of codeRule) {
- switch (rule.rule_type) {
- case codeRuleConst.measure.ruleType.dealCode:
- rule.preview = ctx.tender.info.deal_info.dealCode;
- break;
- case codeRuleConst.measure.ruleType.tenderName:
- rule.preview = tender.name;
- break;
- case codeRuleConst.measure.ruleType.inDate:
- rule.preview = moment().format('YYYY');
- break;
- case codeRuleConst.measure.ruleType.text:
- rule.preview = rule.text;
- break;
- case codeRuleConst.measure.ruleType.addNo:
- const s = '0000000000';
- rule.preview = s.substr(s.length - rule.format);
- break;
- default: break;
- }
- }
- const changeApplyList = await ctx.service.changeApply.getAllDataByCondition({ where: { tid: tender.id, status: audit.changeApply.status.checked } });
- const allApplyCodes = await ctx.service.changePlan.getAllDataByCondition({
- columns: ['apply_code'],
- where: {
- tid: tender.id,
- },
- });
- const acLists = allApplyCodes.length > 0 ? ctx.app._.uniq(ctx.app._.map(allApplyCodes, 'apply_code')) : [];
- const renderData = {
- uid: ctx.session.sessionUser.accountId,
- tender,
- pageInfo,
- changes,
- status,
- rule_type,
- codeRule,
- c_connector,
- c_rule_first,
- filter,
- plan_username: tender_userInfo ? tender_userInfo.name : null,
- ruleType: codeRuleConst.ruleType[rule_type],
- dealCode: ctx.tender.info.deal_info.dealCode,
- auditConst: audit.changePlan,
- ruleConst: codeRuleConst.measure,
- changeConst,
- changeApplyList,
- acLists,
- page_total,
- tp,
- tenderMenu: this.menu.tenderMenu,
- preUrl: '/tender/' + tender.id,
- jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.plan),
- };
- await this.layout('change/plan.ejs', renderData, 'change/plan_modal.ejs');
- }
- /**
- * 变更申请列表 页面 (Get)
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async plan(ctx) {
- try {
- await this._filterChangesPlan(ctx);
- } catch (err) {
- this.log(err);
- ctx.redirect('/dashboard');
- }
- }
- /**
- * 变更管理 状态筛选 页面 (Get)
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async planStatus(ctx) {
- try {
- const status = parseInt(ctx.params.status);
- await this._filterChangesPlan(ctx, status);
- } catch (err) {
- this.logger.error(err);
- ctx.redirect('/tender/' + ctx.params.id + '/change/plan');
- }
- }
- /**
- * 新增变更立项 (Post)
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async planAdd(ctx) {
- try {
- const tenderId = ctx.params.id;
- if (!tenderId) {
- throw '当前未打开标段';
- }
- const data = JSON.parse(ctx.request.body.data);
- if (!data.code || data.code === '') {
- throw '变更方案编号不能为空';
- }
- const change = await ctx.service.changePlan.add(tenderId, ctx.session.sessionUser.accountId, data.code, data.apply_code, data.name);
- ctx.body = { err: 0, msg: '', data: change };
- } catch (err) {
- this.log(err);
- ctx.body = { err: 1, msg: err.toString() };
- }
- }
- /**
- * 删除变更立项
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async planDelete(ctx) {
- try {
- const result = await ctx.service.changePlan.delete(ctx.request.body.cpid);
- if (!result) {
- throw '删除变更方案失败';
- }
- ctx.redirect(ctx.request.header.referer);
- } catch (err) {
- console.log(err);
- ctx.redirect(ctx.request.header.referer);
- }
- }
- /**
- * 获取审批界面所需的 原报、审批人数据等
- * @param ctx
- * @return {Promise<void>}
- * @private
- */
- async _getChangePlanAuditViewData(ctx) {
- const auditConst = audit.changePlan;
- const times = ctx.change.status === auditConst.status.checkNo || ctx.change.status === auditConst.status.revise ? ctx.change.times - 1 : ctx.change.times;
- ctx.change.user = await ctx.service.projectAccount.getAccountInfoById(ctx.change.uid);
- ctx.change.auditHistory = [];
- if (times >= 1) {
- for (let i = 1; i <= times; i++) {
- ctx.change.auditHistory.push(await ctx.service.changePlanAudit.getAuditors(ctx.change.id, i));
- }
- }
- // 获取审批流程中左边列表
- ctx.change.auditors2 = (ctx.change.status === auditConst.status.checkNo || ctx.change.status === auditConst.status.revise) && ctx.change.uid !== ctx.session.sessionUser.accountId ?
- await ctx.service.changePlanAudit.getAuditorsWithOwner(ctx.change.id, times) :
- await ctx.service.changePlanAudit.getAuditorsWithOwner(ctx.change.id, ctx.change.times);
- if (ctx.change.status === auditConst.status.uncheck || ctx.change.status === auditConst.status.checkNo || ctx.change.status === auditConst.status.revise) {
- ctx.change.auditorList = await ctx.service.changePlanAudit.getAuditors(ctx.change.id, ctx.change.times);
- }
- }
- async planInformation(ctx) {
- try {
- const whiteList = this.ctx.app.config.multipart.whitelist;
- const tender = await ctx.service.tender.getDataById(ctx.tender.id);
- await this._getChangePlanAuditViewData(ctx);
- // 获取附件列表
- const fileList = await ctx.service.changePlanAtt.getAllChangePlanAtt(ctx.tender.id, ctx.change.id);
- // 获取清单列表
- const changeList = await ctx.service.changePlanList.getList(ctx.change.id);
- if (ctx.change.status === audit.changePlan.status.checking || ctx.change.status === audit.changePlan.status.checked) {
- const listAudits = await ctx.service.changePlanAudit.getAuditGroupByList(ctx.change.id, ctx.change.times);
- ctx.change.listAudits = listAudits;
- for (const cl of changeList) {
- const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
- // 清单表页赋值
- for (const [index, au] of listAudits.entries()) {
- cl['audit_amount_' + au.aid] = ctx.change.shenpiPower && au.aid === ctx.session.sessionUser.accountId ? cl.spamount : (audit_amount[index] ? parseFloat(audit_amount[index]) : null);
- }
- }
- }
- // 获取用户人验证手机号
- const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
- const auth_mobile = pa.auth_mobile;
- const changeClass = await this._getOrUpdateClass(ctx, 'changePlan');
- const renderData = {
- tender,
- change: ctx.change,
- listRule: tender.c_plan_list_rule ? JSON.parse(tender.c_plan_list_rule) : { source: 1, rule: ['unit', 'unit_price'] },
- changeList,
- changeClass,
- changeConst,
- auditConst: audit.changePlan,
- fileList,
- whiteList,
- authMobile: auth_mobile,
- tpUnit: ctx.change.decimal ? ctx.change.decimal.tp : ctx.tender.info.decimal.tp,
- upUnit: ctx.change.decimal ? ctx.change.decimal.up : ctx.tender.info.decimal.up,
- changeUnits: changeConst.units,
- precision: ctx.change.decimal && ctx.change.decimal.precision ? ctx.change.decimal.precision : ctx.tender.info.precision,
- 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,
- jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.plan_information),
- preUrl: '/tender/' + ctx.tender.id + '/change/plan/' + ctx.change.id + '/information',
- };
- 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)) {
- // data.accountGroup = accountGroup;
- // 获取所有项目参与者
- const accountList = await ctx.service.projectAccount.getAllDataByCondition({
- where: { project_id: ctx.session.sessionProject.id, enable: 1 },
- columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
- });
- renderData.accountList = accountList;
- const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
- renderData.accountGroup = unitList.map(item => {
- const groupList = accountList.filter(item1 => item1.company === item.name);
- return { groupName: item.name, groupList };
- });
- }
- await this.layout('change/plan_information.ejs', renderData, 'change/plan_information_modal.ejs');
- } catch (err) {
- this.log(err);
- ctx.redirect('/tender/' + ctx.params.id + '/change/plan');
- }
- }
- async planReport(ctx) {
- try {
- const renderData = {
- change: ctx.change,
- tender: ctx.tender,
- jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.report),
- };
- await this.layout('change/plan_report.ejs', renderData);
- } catch (err) {
- this.log(err);
- ctx.redirect('/tender/' + ctx.params.id + '/change/plan');
- }
- }
- // 审批相关
- /**
- * 添加审批人
- * @param ctx
- * @return {Promise<void>}
- */
- async addPlanAudit(ctx) {
- try {
- const auditConst = audit.changePlan;
- const data = JSON.parse(ctx.request.body.data);
- const id = this.app._.toInteger(data.auditorId);
- if (isNaN(id) || id <= 0) {
- throw '参数错误';
- }
- // 检查权限等
- if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
- throw '您无权添加审核人';
- }
- if (ctx.change.status === auditConst.status.checking || ctx.change.status === auditConst.status.checked) {
- throw '当前不允许添加审核人';
- }
- ctx.change.auditorList = await ctx.service.changePlanAudit.getAuditors(ctx.change.id, ctx.change.times);
- // 检查审核人是否已存在
- const exist = this.app._.find(ctx.change.auditorList, { aid: id });
- if (exist) {
- throw '该审核人已存在,请勿重复添加';
- }
- // const shenpiInfo = await ctx.service.shenpiAudit.getDataByCondition({ tid: ctx.tender.id, sp_type: shenpiConst.sp_type.material, sp_status: shenpiConst.sp_status.gdzs });
- // const is_gdzs = shenpiInfo && ctx.tender.info.shenpi.material === shenpiConst.sp_status.gdzs ? 1 : 0;
- const result = await ctx.service.changePlanAudit.addAuditor(ctx.change.id, id, ctx.change.times);
- if (!result) {
- throw '添加审核人失败';
- }
- const auditors = await ctx.service.changePlanAudit.getAuditorsWithOwner(ctx.change.id, ctx.change.times);
- ctx.body = { err: 0, msg: '', data: auditors };
- } catch (err) {
- this.log(err);
- ctx.body = { err: 1, msg: err.toString(), data: null };
- }
- }
- /**
- * 移除审批人
- * @param ctx
- * @return {Promise<void>}
- */
- async deletePlanAudit(ctx) {
- try {
- const data = JSON.parse(ctx.request.body.data);
- const id = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
- if (isNaN(id) || id <= 0) {
- throw '参数错误';
- }
- const result = await ctx.service.changePlanAudit.deleteAuditor(ctx.change.id, id, ctx.change.times);
- if (!result) {
- throw '移除审核人失败';
- }
- const auditors = await ctx.service.changePlanAudit.getAuditors(ctx.change.id, ctx.change.times);
- ctx.body = { err: 0, msg: '', data: auditors };
- } catch (err) {
- ctx.body = { err: 1, msg: err.toString(), data: null };
- }
- }
- /**
- * 上传附件
- * @param {*} ctx 上下文
- */
- async uploadPlanFile(ctx) {
- let stream;
- try {
- const auditConst = audit.changePlan;
- // this._checkAdvanceFileCanModify(ctx);
- const parts = this.ctx.multipart({
- autoFields: true,
- });
- const files = [];
- const create_time = Date.parse(new Date()) / 1000;
- let idx = 0;
- const extra_upload = ctx.change.status === auditConst.status.checked;
- while ((stream = await parts()) !== undefined) {
- if (!stream.filename) {
- // 如果没有传入直接返回
- return;
- }
- const fileInfo = path.parse(stream.filename);
- const filepath = `app/public/upload/${this.ctx.tender.id.toString()}/change_plan/fujian_${create_time + idx.toString() + fileInfo.ext}`;
- // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, 'app', filepath));
- await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
- files.push({ filepath, name: stream.filename, ext: fileInfo.ext });
- ++idx;
- stream && (await sendToWormhole(stream));
- }
- const in_time = new Date();
- const payload = files.map(file => {
- let idx;
- if (Array.isArray(parts.field.name)) {
- idx = parts.field.name.findIndex(name => name === file.name);
- } else {
- idx = 'isString';
- }
- const newFile = {
- tid: ctx.tender.id,
- cpid: ctx.change.id,
- uid: ctx.session.sessionUser.accountId,
- filename: file.name,
- fileext: file.ext,
- filesize: ctx.helper.bytesToSize(idx === 'isString' ? parts.field.size : parts.field.size[idx]),
- filepath: file.filepath,
- upload_time: in_time,
- extra_upload,
- };
- return newFile;
- });
- // 执行文件信息写入数据库
- await ctx.service.changePlanAtt.saveFileMsgToDb(payload);
- // 将最新的当前标段的所有文件信息返回
- const data = await ctx.service.changePlanAtt.getAllChangePlanAtt(ctx.tender.id, ctx.change.id);
- ctx.body = { err: 0, msg: '', data };
- } catch (err) {
- stream && (await sendToWormhole(stream));
- this.log(err);
- ctx.body = { err: 1, msg: err.toString(), data: null };
- }
- }
- /**
- * 删除附件
- * @param {Ojbect} ctx 上下文
- */
- async deletePlanFile(ctx) {
- try {
- const { id } = JSON.parse(ctx.request.body.data);
- const fileInfo = await ctx.service.changePlanAtt.getDataById(id);
- if (fileInfo || Object.keys(fileInfo).length) {
- // 先删除文件
- // await fs.unlinkSync(path.resolve(this.app.baseDir, './app', fileInfo.filepath));
- await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
- // 再删除数据库
- await ctx.service.changePlanAtt.delete(id);
- } else {
- throw '不存在该文件';
- }
- const data = await ctx.service.changePlanAtt.getAllChangePlanAtt(ctx.tender.id, ctx.change.id);
- ctx.body = { err: 0, msg: '请求成功', data };
- } catch (err) {
- this.log(err);
- ctx.body = { err: 1, msg: err.toString(), data: null };
- }
- }
- /**
- * 下载附件
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async downloadPlanFile(ctx) {
- const id = ctx.params.fid;
- if (id) {
- try {
- const fileInfo = await ctx.service.changePlanAtt.getDataById(id);
- if (fileInfo !== undefined && fileInfo !== '') {
- // const fileName = path.join(__dirname, '../', fileInfo.filepath);
- // 解决中文无法下载问题
- const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
- let disposition = '';
- if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
- disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename);
- } else if (userAgent.indexOf('firefox') >= 0) {
- disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename) + '"';
- } else {
- /* safari等其他非主流浏览器只能自求多福了 */
- disposition = 'attachment; filename=' + new Buffer(fileInfo.filename).toString('binary');
- }
- ctx.response.set({
- 'Content-Type': 'application/octet-stream',
- 'Content-Disposition': disposition,
- 'Content-Length': fileInfo.filesize,
- });
- // ctx.body = await fs.createReadStream(fileName);
- ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
- } else {
- throw '不存在该文件';
- }
- } catch (err) {
- this.log(err);
- this.setMessage(err.toString(), this.messageType.ERROR);
- }
- }
- }
- async planInformationSave(ctx) {
- try {
- const data = JSON.parse(ctx.request.body.data);
- if (data.name === 'code') {
- const info = await ctx.service.changePlan.isRepeat(ctx.change.id, data.val, ctx.tender.id);
- if (info) {
- throw '该编号已存在';
- }
- }
- const result = await ctx.service.changePlan.saveInfo(ctx.change.id, data);
- if (!result) {
- throw '修改失败';
- }
- ctx.body = { err: 0, msg: '请求成功', data: null };
- } catch (err) {
- this.log(err);
- ctx.body = { err: 1, msg: err.toString(), data: null };
- }
- }
- /**
- * 上报和重新上报
- * @param ctx
- * @return {Promise<void>}
- */
- async startPlanAudit(ctx) {
- try {
- const auditConst = audit.changePlan;
- // 检查权限等
- if (!ctx.change) {
- throw '数据错误';
- }
- if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
- throw '您无权上报该期数据';
- }
- if (ctx.change.status === auditConst.status.checking || ctx.change.status === auditConst.status.checked) {
- throw '该材料调差期数据当前无法上报';
- }
- await ctx.service.changePlanAudit.start(ctx.change.id, ctx.change.times);
- ctx.redirect(ctx.request.header.referer);
- } catch (err) {
- this.log(err);
- ctx.session.postError = err.toString();
- ctx.redirect(ctx.request.header.referer);
- }
- }
- /**
- * 审批
- * @param ctx
- * @return {Promise<void>}
- */
- async checkPlanAudit(ctx) {
- try {
- const auditConst = audit.changePlan;
- if (!ctx.change || ctx.change.status !== auditConst.status.checking) {
- throw '当前材料调差期数据有误';
- }
- if (!ctx.change.curAuditor || ctx.change.curAuditor.aid !== ctx.session.sessionUser.accountId) {
- throw '您无权进行该操作';
- }
- const data = {
- checkType: parseInt(ctx.request.body.checkType),
- opinion: ctx.request.body.opinion,
- };
- if (!data.checkType || isNaN(data.checkType)) {
- throw '提交数据错误';
- }
- // if (data.checkType === auditConst.status.checkNo) {
- // if (!data.checkType || isNaN(data.checkType)) {
- // throw '提交数据错误';
- // }
- // }
- await ctx.service.changePlanAudit.check(ctx.change.id, data, ctx.change.times);
- ctx.redirect(ctx.request.header.referer);
- } catch (err) {
- this.log(err);
- console.log(err);
- ctx.session.postError = err.toString();
- ctx.redirect(ctx.request.header.referer);
- }
- }
- /**
- * 变更清单 - 操作 (Ajax)
- * @param ctx
- * @return {Promise<void>}
- */
- async savePlanListsData(ctx) {
- try {
- const data = JSON.parse(ctx.request.body.data);
- const responseData = {
- err: 0,
- msg: '',
- data: {},
- };
- switch (data.type) {
- case 'add':
- responseData.data = await ctx.service.changePlanList.add(data.updateData);
- break;
- case 'batchadd':
- responseData.data = await ctx.service.changePlanList.batchAdd(data);
- break;
- case 'del':
- await ctx.service.changePlanList.del(data.ids);
- // 取所有工料表
- responseData.data = await ctx.service.changePlanList.getList(ctx.change.id);
- break;
- case 'update':
- // if (data.updateData.code === '' || data.updateData.code === null) {
- // throw '请先输入编号';
- // }
- await ctx.service.changePlanList.save(data.updateData);
- break;
- case 'paste':
- await ctx.service.changePlanList.saveDatas(data.updateData);
- // 取所有工料表
- responseData.data = await ctx.service.changePlanList.getList(ctx.change.id);
- if (ctx.change.status === audit.changePlan.status.checking || ctx.change.status === audit.changePlan.status.checked) {
- const listAudits = await ctx.service.changePlanAudit.getAuditGroupByList(ctx.change.id, ctx.change.times);
- for (const cl of responseData.data) {
- const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
- // 清单表页赋值
- for (const [index, au] of listAudits.entries()) {
- cl['audit_amount_' + au.aid] = ctx.change.shenpiPower && au.aid === ctx.session.sessionUser.accountId ? cl.spamount : (audit_amount[index] ? parseFloat(audit_amount[index]) : null);
- }
- }
- }
- break;
- case 'list_rule':
- const result = await ctx.service.tender.saveTenderData(ctx.tender.id, { c_plan_list_rule: data.postData });
- if (!result) {
- throw '修改失败';
- }
- break;
- case 'paste_amount_rows':
- await ctx.service.changePlanList.saveDatas(data.updateData);
- const changeList = await ctx.service.changePlanList.getList(ctx.change.id);
- const listAudits = await ctx.service.changePlanAudit.getAuditGroupByList(ctx.change.id, ctx.change.times);
- for (const cl of changeList) {
- // 清单表页赋值
- const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
- // 清单表页赋值
- for (const [index, au] of listAudits.entries()) {
- cl['audit_amount_' + au.aid] = ctx.change.shenpiPower && au.aid === ctx.session.sessionUser.accountId ? cl.spamount : (audit_amount[index] ? parseFloat(audit_amount[index]) : null);
- }
- }
- // 取所有工料表
- responseData.data = changeList;
- break;
- // case 'update_tp':
- // await ctx.service.changePlan.saveInfo(ctx.change.id, { name: 'total_price', val: data.updateData });
- // break;
- default: throw '参数有误';
- }
- ctx.body = responseData;
- } catch (err) {
- this.log(err);
- ctx.body = { err: 1, msg: err.toString(), data: null };
- }
- }
- /**
- * 撤回审批
- * @param ctx
- * @return {Promise<void>}
- */
- async checkPlanAuditCancel(ctx) {
- try {
- if (!ctx.change.cancancel) throw '您无权进行该操作';
- await ctx.service.changePlanAudit.checkCancel(ctx.change);
- ctx.body = { err: 0, url: ctx.request.header.referer, msg: '' };
- } catch (err) {
- this.log(err);
- ctx.body = { err: 1, msg: err };
- }
- }
- /**
- * 变更申请重新审批
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async checkPlanAgain(ctx) {
- try {
- // 获取终审
- const auditInfo = await this.ctx.service.changePlanAudit.getAuditorByStatus(ctx.change.id, audit.changePlan.status.checked);
- if (ctx.change.status !== audit.changePlan.status.checked || ctx.session.sessionUser.accountId !== auditInfo.aid) {
- throw '您无权进行该操作';
- }
- // 判断是否被变更申请调用了,是则无法发起修订
- const projectInfo = await ctx.service.change.getDataByCondition({ tid: ctx.tender.id, plan_code: ctx.change.code });
- if (projectInfo) {
- throw '该变更方案已被变更令调用,无法发起重新审批';
- }
- if (ctx.session.sessionUser.loginStatus === 0) {
- const code = ctx.request.body.code;
- const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
- if (!pa.auth_mobile) {
- throw '未绑定手机号';
- }
- const cacheKey = 'smsCode:' + ctx.session.sessionUser.accountId;
- const cacheCode = await app.redis.get(cacheKey);
- // console.log(cacheCode);
- if (cacheCode === null || code === undefined || cacheCode !== (code + pa.auth_mobile)) {
- throw '验证码不正确!';
- }
- }
- // 重新审批
- const result = await ctx.service.changePlanAudit.checkAgain(ctx.change);
- if (!result) {
- throw '重新审批失败';
- }
- // ctx.redirect('/tender/' + changeData.tid + '/change/' + changeData.cid + '/info');
- ctx.body = {
- err: 0,
- url: ctx.request.header.referer,
- msg: '',
- };
- } catch (err) {
- console.log(err);
- // ctx.redirect(ctx.request.header.referer);
- ctx.body = {
- err: 1,
- // url: ctx.request.header.referer,
- msg: err,
- };
- }
- }
- /**
- * 变更申请修订重新上报
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async checkPlanRevise(ctx) {
- try {
- if (ctx.change.status !== audit.changePlan.status.checked || ctx.session.sessionUser.accountId !== ctx.change.uid) {
- throw '您无权进行该操作';
- }
- // 判断是否被变更申请调用了,是则无法发起修订
- const projectInfo = await ctx.service.change.getDataByCondition({ tid: ctx.tender.id, plan_code: ctx.change.code });
- if (projectInfo) {
- throw '该变更方案已被变更令调用,无法发起修订';
- }
- if (ctx.session.sessionUser.loginStatus === 0) {
- const code = ctx.request.body.code;
- const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
- if (!pa.auth_mobile) {
- throw '未绑定手机号';
- }
- const cacheKey = 'smsCode:' + ctx.session.sessionUser.accountId;
- const cacheCode = await app.redis.get(cacheKey);
- // console.log(cacheCode);
- if (cacheCode === null || code === undefined || cacheCode !== (code + pa.auth_mobile)) {
- throw '验证码不正确!';
- }
- }
- // 重新审批
- const result = await ctx.service.changePlanAudit.checkRevise(ctx.change);
- if (!result) {
- throw '修订发起失败';
- }
- // ctx.redirect('/tender/' + changeData.tid + '/change/' + changeData.cid + '/info');
- ctx.body = {
- err: 0,
- url: ctx.request.header.referer,
- msg: '',
- };
- } catch (err) {
- console.log(err);
- // ctx.redirect(ctx.request.header.referer);
- ctx.body = {
- err: 1,
- // url: ctx.request.header.referer,
- msg: err,
- };
- }
- }
- /**
- * 变更令撤销修订
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async cancelPlanRevise(ctx) {
- try {
- if (!(ctx.change.status === audit.changePlan.status.revise && (ctx.session.sessionUser.accountId === ctx.change.uid || ctx.session.sessionUser.accountId === ctx.session.sessionUser.is_admin))) {
- throw '您无权进行该操作';
- }
- // 重新审批
- const result = await ctx.service.changePlanAudit.cancelRevise(ctx.change);
- if (!result) {
- throw '撤销修订失败';
- }
- ctx.body = {
- err: 0,
- url: ctx.request.header.referer,
- msg: '',
- };
- } catch (err) {
- console.log(err);
- ctx.body = {
- err: 1,
- msg: err,
- };
- }
- }
- }
- return ChangeController;
- };
|