| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521 | '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 auditType = require('../const/audit').auditType;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;const spreadSetting = require('../lib/spread_setting');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.subProject.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, state);            if (changes !== null) {                for (const c of changes) {                    if (c.status !== audit.change.status.checked || !c.final_auditor_str) {                        c.curAuditors = await ctx.service.changeAudit.getAuditorsByStatus(c.cid, c.status, c.times);                        if (c.status === audit.change.status.checkNoPre) {                            c.curAuditors2 = await ctx.service.stageAudit.getAuditorsByStatus(c.cid, audit.change.status.checking, c.times);                        }                        if (c.status === audit.change.status.checkNo || c.status === audit.change.status.revise) {                            const changeUsedData = await ctx.service.stageChange.getAllFinalUsedData(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);                        }                        if (c.status === audit.change.status.checked && c.curAuditors.length > 0) {                            const final_auditor_str = c.curAuditors[0].audit_type === auditType.key.common                                ? c.curAuditors[0].name + (c.curAuditors[0].role ? '-' + c.curAuditors[0].role : '')                                : ctx.helper.transFormToChinese(c.curAuditors[0].audit_order) + '审';                            await ctx.service.change.defaultUpdate({ final_auditor_str }, { where: { cid: c.cid } });                        }                    }                    const user = await ctx.service.projectAccount.getDataById(c.uid);                    c.username = user ? user.name : '';                    page_total = ctx.helper.add(page_total, c.total_price);                }            }            // 分页相关            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 need_checking = await ctx.service.change.getCheckingList(ctx.tender.id, ctx.session.sessionUser.accountId, true);            const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);            const userPermission = pa && pa.permission ? JSON.parse(pa.permission) : null;            const addPermission = !!(userPermission && userPermission.change !== undefined && userPermission.change.indexOf('3') !== -1);            const renderData = {                moment,                tender,                // tenderList,                pageInfo,                changes,                page_total,                tp,                filter,                status,                codeRule,                dealCode: ctx.tender.info.deal_info.dealCode,                auditConst: audit.change,                changeConst,                shenpiConst,                state,                ruleType: codeRuleConst.ruleType.change,                ruleConst: codeRuleConst.measure,                tenderMenu: this.menu.tenderMenu,                preUrl: '/tender/' + tenderId,                tpUnit: ctx.tender.info.decimal.tp,                changePlanList,                apLists,                auditType,                precision: ctx.tender.info.precision,                need_checking,                addPermission,            };            if (ctx.subProject.page_show.openChangeState) {                // 工程变更类别读取                const fun_set = this.ctx.subProject.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');            }        }        /**         * 审批流程(Get)         * @param ctx         * @return {Promise<void>}         */        async changeAuditors(ctx) {            try {                const cid = JSON.parse(ctx.request.body.data).cid;                const tenderId = ctx.params.id;                const changeInfo = await ctx.service.change.getDataByCondition({ cid });                await ctx.service.change.loadChangeUser(changeInfo);                await ctx.service.change.loadChangeAuditViewData(changeInfo);                ctx.body = { err: 0, msg: '', data: changeInfo };            } catch (error) {                this.log(error);                ctx.body = { err: 1, msg: error.toString(), data: null };            }        }        /**         *         * @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 if (data.type === codeRuleConst.ruleField[codeRuleConst.ruleType.plan]) {                        changeCount = await ctx.service.changePlan.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 && parseInt(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 fun_set = this.ctx.subProject.fun_set;                const stateInfo = ctx.helper._.find(fun_set.change_state, { order: 3 });                // 在生成新变更令后,需要copy前一个变更令报表的签名信息                const lastChange = await ctx.service.change.getLastChange(tenderId);                const change = await ctx.service.change.add(tenderId, ctx.session.sessionUser.accountId, data.code, data.plan_code, data.name, data.add_plan_list, stateInfo.value);                await ctx.service.roleRptRel.createRoleRelationshipFromOtherBz(tenderId, '-300', change.cid, lastChange ? lastChange.cid : null);                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');            }        }        async batchChangeFun(ctx) {            try {                const data = JSON.parse(ctx.request.body.data);                const responseData = {                    err: 0,                    msg: '',                    data: {},                };                switch (data.type) {                    case 'get_report_list':                        responseData.data.shenpi_status = ctx.tender.info.shenpi.change;                        responseData.data.uncheckList = await ctx.service.change.getUncheckList(ctx.tender.id, ctx.tender.info.shenpi.change);                        if (ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdspl) {                            responseData.data.spGroupList = await ctx.service.shenpiGroup.getGroupListByChangeType(ctx.tender.id, shenpiConst.sp_type.change, 'change');                        } else {                            // 获取所有项目参与者                            const accountList = await ctx.service.projectAccount.getAllSubProjectAccount(ctx.subProject);                            responseData.data.accountList = accountList;                            const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });                            responseData.data.accountGroup = unitList.map(item => {                                const groupList = accountList.filter(item1 => item1.company === item.name);                                return { groupName: item.name, groupList };                            }).filter(x => { return x.groupList.length > 0; });                        }                        break;                    case 'get_shenpi_list':                        responseData.data.checkingList = await ctx.service.change.getCheckingList(ctx.tender.id, ctx.session.sessionUser.accountId);                        break;                    case 'set_shenpi_list':                        await ctx.service.changeAudit.batchSetAudits(ctx.tender.id, data.cids, data.from_cid);                        responseData.data.uncheckList = await ctx.service.change.getUncheckList(ctx.tender.id, ctx.tender.info.shenpi.change);                        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>}         * @private         */        async _getChangeAuditViewData(ctx) {            await ctx.service.change.loadChangeAuditViewData(ctx.change);            // await this._checkStageStart(ctx);        }        /**         * 变更信息 新版本页面 (Get)         *         * @param {Object} ctx - egg全局变量         * @return {void}         */        async information(ctx) {            try {                const whiteList = this.ctx.app.config.multipart.whitelist;                await this._getChangeAuditViewData(ctx);                const tender = ctx.tender;                const tenderData = await ctx.service.tender.getDataById(tender.id);                const change = ctx.change;                // 获取附件列表                const attList = await ctx.service.changeAtt.getChangeAttachment(change.cid, 'desc');                // 获取其他变更令数据                const othersChange = await ctx.service.change.getOthersChange(ctx.tender.id, change.cid);                // 获取变更方案的清单                let planList = [];                let showPlanBtn = false;                if (ctx.subProject.page_show.openChangePlan) {                    const planInfo = change.plan_code ? await ctx.service.changePlan.getDataByCondition({ tid: 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 changeList = await ctx.service.changeAuditList.getList(change.cid);                if (change.status === audit.change.status.checked || change.status === audit.change.status.checking || change.status === audit.change.status.checkNoPre) {                    for (const cl of changeList) {                        const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';                        // 清单表页赋值                        for (const au in change.userGroups) {                            if (change.userGroups[au][0].audit_order !== 0) {                                cl['audit_amount_' + change.userGroups[au][0].audit_order] =                                (change.userGroups[au][0].audit_type === auditType.key.and &&                                    ctx.helper._.findIndex(change.userGroups[au], { status: audit.change.status.checking }) !== -1 &&                                    ctx.helper._.findIndex(change.userGroups[au], { uid: ctx.session.sessionUser.accountId, status: audit.change.status.checked }) !== -1) ||                                    (change.shenpiPower && ctx.helper._.findIndex(change.userGroups[au], { uid: ctx.session.sessionUser.accountId }) !== -1) ?                                    cl.spamount : (audit_amount[au - 1] !== undefined ? parseFloat(audit_amount[au - 1]) : null);                            }                        }                        if (change.readOnly && !change.shenpiPower) {                            cl.checked_amount = audit_amount.length > 0 ? parseFloat(audit_amount[audit_amount.length - 1]) : cl.camount;                        }                    }                }                // 工程变更类别读取                const fun_set = this.ctx.subProject.fun_set;                // 获取用户人验证手机号                const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);                const auth_mobile = pa.auth_mobile;                const userPermission = pa && pa.permission ? JSON.parse(pa.permission) : null;                const deleteFilePermission = !!(userPermission && userPermission.change !== undefined && userPermission.change.indexOf('2') !== -1);                const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });                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 removeSettleNum = change.status !== audit.change.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,                    auditConst: audit.change,                    ledgerConsts: audit.ledger.status,                    auditType,                    auditorGroups: change.userGroups,                    attList,                    whiteList,                    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,                    deleteFilePermission,                    changeMode: tenderData.c_mode,                };                // 获取是否已存在调用变更令                let changeUsedData = await ctx.service.stageChange.getAllFinalUsedData(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) { // 防止未创建期时调用                    for (const cu of changeUsedData) {                        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); }));                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.changeUnits = changeConst.units;                if (!change.readOnly || ctx.session.sessionUser.is_admin) {                    // 获取所有项目参与者                    const accountList = await ctx.service.projectAccount.getAllSubProjectAccount(ctx.subProject);                    renderData.accountList = accountList;                    renderData.accountGroup = unitList.map(item => {                        const groupList = accountList.filter(item1 => item1.company === item.name);                        return { groupName: item.name, groupList };                    }).filter(x => { return x.groupList.length > 0; });                    // 获取公司列表                    const companyList = await ctx.service.changeCompany.getAllDataByCondition({ where: { tid: ctx.tender.id } });                    renderData.companyList = companyList;                    const stateInfo = ctx.helper._.find(fun_set.change_state, { order: ctx.change.state });                    renderData.deLimit = stateInfo.value;                    if (!change.readOnly) {                        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});                        }                        // 获取固定审批流列表                        if (tender.info.shenpi.change === shenpiConst.sp_status.gdspl) {                            renderData.spGroupList = await ctx.service.shenpiGroup.getGroupListByChangeType(tender.id, shenpiConst.sp_type.change, 'change');                        }                    }                }                renderData.changeList = changeList;                await this.layout('change/information.ejs', renderData, 'change/information_modal.ejs');            } catch (err) {                this.log(err);                ctx.session.postError = err.toString();                ctx.redirect('/tender/' + ctx.params.id + '/change');            }        }        /**         * 变更关联数据 - 操作 (Ajax)         * @param ctx         * @return {Promise<void>}         */        async relation(ctx) {            try {                if (!ctx.subProject.page_show.openChangePlan) {                    throw '该功能已关闭';                }                // await this._getChangeAuditViewData(ctx);                const tender = ctx.tender;                const change = ctx.change;                const renderData = {                    tender,                    change,                };                renderData.changePlanInfo = null;                renderData.changeApplyInfo = null;                renderData.changeProjectInfo = null;                if (change.plan_code) {                    renderData.changePlanInfo = await ctx.service.changePlan.getDataByCondition({ tid: tender.id, code: change.plan_code });                    if (renderData.changePlanInfo && renderData.changePlanInfo.apply_code) {                        renderData.changeApplyInfo = await ctx.service.changeApply.getDataByCondition({ tid: tender.id, code: renderData.changePlanInfo.apply_code });                        if (renderData.changeApplyInfo && renderData.changeApplyInfo.project_code) {                            renderData.changeProjectInfo = await ctx.service.changeProject.getDataByCondition({ tid: tender.id, code: renderData.changeApplyInfo.project_code });                        }                    }                }                await this.layout('change/relation.ejs', renderData);            } catch (err) {                this.log(err);                ctx.session.postError = err.toString();                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.subProject.page_show.openChangeWhiteList) {                            throw '空白清单添加功能未开启。';                        }                        const fun_set = this.ctx.subProject.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.getAllFinalUsedData(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 fun_set = this.ctx.subProject.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);                        //         }                        //     }                        // }                        for (const cl of changeList) {                            const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';                            // 清单表页赋值                            for (const au in ctx.change.userGroups) {                                if (ctx.change.userGroups[au][0].audit_order !== 0) {                                    cl['audit_amount_' + ctx.change.userGroups[au][0].audit_order] =                                        (ctx.change.userGroups[au][0].audit_type === auditType.key.and &&                                            ctx.helper._.findIndex(ctx.change.userGroups[au], { status: audit.change.status.checking }) !== -1 &&                                            ctx.helper._.findIndex(ctx.change.userGroups[au], { uid: ctx.session.sessionUser.accountId, status: audit.change.status.checked }) !== -1) ||                                        (ctx.change.shenpiPower && ctx.helper._.findIndex(ctx.change.userGroups[au], { uid: ctx.session.sessionUser.accountId }) !== -1) ?                                            cl.spamount : (audit_amount[au - 1] !== undefined ? parseFloat(audit_amount[au - 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.updateOrderList);                        responseData.data = result;                        break;                    case 'order_site':                        await ctx.service.change.defaultUpdate({ order_site: data.listid }, { where: { cid: ctx.change.cid } });                        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;                    // 从新增部位页新增清单                    case 'add-change-list':                        if (ctx.change.status !== audit.change.status.uncheck && ctx.change.status !== audit.change.status.checkNo && ctx.change.status !== audit.change.status.revise) {                            throw '该变更令正在审批中或已完成,无法操作清单数据';                        }                        const fun_set2 = this.ctx.subProject.fun_set;                        const stateInfo2 = ctx.helper._.find(fun_set2.change_state, { order: ctx.change.state });                        await ctx.service.changeAuditList.adds(data.postData, stateInfo2.value);                        responseData.data = { changeList: await ctx.service.changeAuditList.getList(ctx.change.cid) };                        break;                    // 从新增部位页删除清单                    case 'del-change-list':                        if (ctx.change.status !== audit.change.status.uncheck && ctx.change.status !== audit.change.status.checkNo && ctx.change.status !== audit.change.status.revise) {                            throw '该变更令正在审批中或已完成,无法操作清单数据';                        }                        await ctx.service.changeAuditList.dels(data);                        responseData.data = { changeList: await ctx.service.changeAuditList.getList(ctx.change.cid) };                        break;                    case 'get-revise-list':                        responseData.data.changeLedgerList = await ctx.service.changeLedger.getAllDataByCondition({ where: { tender_id: ctx.tender.id } });                        responseData.data.changePosList = await ctx.service.changePos.getAllDataByCondition({ where: { tid: ctx.tender.id } });                        break;                    case 'plan_code':                        responseData.data.planInfo = null;                        responseData.data.applyInfo = null;                        responseData.data.projectInfo = null;                        await ctx.service.change.defaultUpdate({ plan_code: data.plan_code }, { where: { cid: ctx.change.cid } });                        if (data.plan_code) {                            responseData.data.planInfo = await ctx.service.changePlan.getDataByCondition({ tid: ctx.tender.id, code: data.plan_code });                            if (responseData.data.planInfo && responseData.data.planInfo.apply_code) {                                responseData.data.applyInfo = await ctx.service.changeApply.getDataByCondition({ tid: ctx.tender.id, code: responseData.data.planInfo.apply_code });                                if (responseData.data.applyInfo && responseData.data.applyInfo.project_code) {                                    responseData.data.projectInfo = await ctx.service.changeProject.getDataByCondition({ tid: ctx.tender.id, code: responseData.data.applyInfo.project_code });                                }                            }                        }                        break;                    case 'change_mode':                        await ctx.service.tender.defaultUpdate({                            id: ctx.tender.id,                            c_mode: data.updateData ? 1 : 0,                        });                        const changeList2 = await ctx.service.changeAuditList.getList(ctx.change.cid);                        if (ctx.change.readOnly) {                            const auditList2 = await ctx.service.changeAudit.getListGroupByTimes(ctx.change.cid, ctx.change.times);                            for (const cl of changeList2) {                                const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';                                // 清单表页赋值                                for (const au in ctx.change.userGroups) {                                    if (ctx.change.userGroups[au][0].audit_order !== 0) {                                        cl['audit_amount_' + ctx.change.userGroups[au][0].audit_order] =                                            (ctx.change.userGroups[au][0].audit_type === auditType.key.and &&                                                ctx.helper._.findIndex(ctx.change.userGroups[au], { status: audit.change.status.checking }) !== -1 &&                                                ctx.helper._.findIndex(ctx.change.userGroups[au], { uid: ctx.session.sessionUser.accountId, status: audit.change.status.checked }) !== -1) ||                                            (ctx.change.shenpiPower && ctx.helper._.findIndex(ctx.change.userGroups[au], { uid: ctx.session.sessionUser.accountId }) !== -1) ?                                                cl.spamount : (audit_amount[au - 1] !== undefined ? parseFloat(audit_amount[au - 1]) : null);                                    }                                }                            }                        }                        // 取所有工料表                        responseData.data = changeList2;                        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) {            const responseData = {                err: 0,                msg: '',                data: {},            };            try {                // 检查权限等                if (!ctx.change) {                    throw '数据错误';                }                if (ctx.change.uid !== ctx.session.sessionUser.accountId) {                    throw '您无权上报该变更令数据';                }                if (!(ctx.change.status === audit.change.status.uncheck || ctx.change.status === audit.change.status.checkNo || ctx.change.status === audit.change.status.revise)) {                    throw '该变更令当前无法上报';                }                // 判断是否是修订,判断有无审批人员作弊                if ((ctx.change.status === audit.change.status.revise || ctx.change.is_revise) && ctx.tender.info.shenpi.change === shenpiConst.sp_status.sqspr) {                    // 获取上一次的审批人流程                    const lastUserList = await ctx.service.changeAudit.getUniqAuditor(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.change.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);                ctx.body = responseData;            } catch (err) {                this.log(err);                // ctx.session.postError = err.toString();                // ctx.redirect(ctx.request.header.referer);                ctx.body = { err: 1, msg: err.toString(), data: null };            }        }        // 审批相关        /**         * 添加审批人         * @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.change.status.checking || ctx.change.status === audit.change.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.getUserGroup(ctx.change.cid, 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 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 = await ctx.service.changeAudit.getUserGroup(ctx.change.cid, ctx.change.times);                ctx.body = { err: 0, msg: '', data: auditors };            } catch (err) {                ctx.body = { err: 1, msg: err.toString(), data: null };            }        }        async changeSpGroup(ctx) {            try {                const data = JSON.parse(ctx.request.body.data);                const result = await ctx.service.changeAudit.changeSpGroup(ctx.change, data.sp_group);                if (!result) {                    throw '切换审批组失败';                }                const auditors = await ctx.service.changeAudit.getUserGroup(ctx.change.cid, ctx.change.times);                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 = 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 = 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 } }) : [];                const stages = data.from === 'revise' ? await ctx.service.stage.getAllDataByCondition({                    where: { tid: ctx.tender.id },                    orders: [['order', 'desc']],                }) : [];                if (stages.length > 0) {                    const usedBills = [], usedPos = [];                    for (const s of stages) {                        if (s.status === audit.stage.status.checked) {                            const usedPreBills = await ctx.service.stageBillsFinal.getUsedBills(ctx.tender.id, s.order);                            usedBills.push(...usedPreBills);                            const usedPrePos = await ctx.service.stagePosFinal.getUsedPos(ctx.tender.id, s.order);                            usedPos.push(...usedPrePos);                            break;                        } else {                            const usedCurBills = await ctx.service.stageBills.getStageUsedBills(ctx.tender.id, s.id);                            usedBills.push(...usedCurBills);                            const usedCurPos = await ctx.service.stagePos.getStageUsedPos(ctx.tender.id, s.id);                            usedPos.push(...usedCurPos);                        }                    }                    for (const b of ledgerData) {                        b.used = usedBills.indexOf(b.id) >= 0;                    }                    for (const p of posData) {                        p.used = usedPos.indexOf(p.id) >= 0;                    }                }                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' },                    { 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 });                            if (data) data.cid = 1;                        }                    }                }                const bodyData = { bills: ctx.helper._.concat(ledgerData, changeLedgerData), pos: ctx.helper._.concat(posData, changePosData), dealBills };                if (data.from === 'batch') {                    bodyData.changeLedgerList = changeLedgerData;                    bodyData.changePosList = changePosData;                }                ctx.body = { err: 0, msg: '', data: bodyData };            } catch (err) {                console.log(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) {            const data = JSON.parse(ctx.request.body.data);            const responseData = {                err: 0,                msg: '',                data: {},            };            try {                const changeData = await ctx.service.change.getDataByCondition({ cid: data.change_id });                if (!changeData) {                    throw '变更令数据错误';                }                const status = parseInt(data.status);                // 判断是否到你审批,如果不是则无法审批                const curAuditor = await ctx.service.changeAudit.getCurAuditors(changeData.cid, changeData.times);                if (!curAuditor || (curAuditor && ctx.helper._.findIndex(curAuditor, { uid: ctx.session.sessionUser.accountId }) === -1)) {                    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, data, changeData);                        break;                    // case 4:// 审批终止                    //     result = await ctx.service.change.approvalStop(ctx.request.body);                    //     break;                    case 5:// 审批退回到原报人                        result = await ctx.service.change.approvalCheckNo(pid, data, changeData);                        break;                    case 6:// 审批退回到上一个审批人                        result = await ctx.service.change.approvalCheckNoPre(pid, data, changeData);                        break;                    default:break;                }                if (!result) {                    throw '审批失败';                }                ctx.body = responseData;            } catch (err) {                console.log(err);                // ctx.session.postError = err.toString();                // ctx.redirect(ctx.request.header.referer);                ctx.body = { err: 1, msg: err.toString(), data: null };            }        }        /**         * 撤回审批         * @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.change.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 '该文件不存在';                }                const accountInfo = await this.ctx.service.projectAccount.getDataById(this.ctx.session.sessionUser.accountId);                const userPermission = accountInfo && accountInfo.permission ? JSON.parse(accountInfo.permission) : null;                const deleteFilePermission = !!(userPermission && userPermission.change !== undefined && userPermission.change.indexOf('2') !== -1);                if (!fileInfo.extra_upload && change.status === audit.change.status.checked && !deleteFilePermission) {                    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 readySettle = await ctx.service.settle.getReadySettle(ctx.change.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 '验证码不正确!';                    }                }                // 重新审批                if ((ctx.change.finalAuditorIds.indexOf(ctx.session.sessionUser.accountId) >= 0) && ctx.change.status === audit.change.status.checked) {                    const result = await ctx.service.change.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: '',                    };                } else {                    throw '您无权进行该操作';                }            } 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 {                if (ctx.change.status !== audit.change.status.checked || ctx.session.sessionUser.accountId !== ctx.change.uid) {                    throw '您无权进行该操作';                }                const readySettle = await ctx.service.settle.getReadySettle(ctx.change.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.getAllFinalUsedData(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(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 cancelRevise(ctx) {            try {                if (!(ctx.change.status === audit.change.status.revise && (ctx.session.sessionUser.accountId === ctx.change.uid || ctx.session.sessionUser.accountId === ctx.session.sessionUser.is_admin))) {                    throw '您无权进行该操作';                }                const readySettle = await ctx.service.settle.getReadySettle(ctx.change.tid);                if (readySettle && readySettle.settle_order !== ctx.tender.data.settle_order) {                    throw '结算数据发生变化,请刷新页面再提交';                }                // 重新审批                const result = await ctx.service.change.cancelRevise(ctx.change.cid, ctx.change.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.change.status.checked) {                    await ctx.service.changeAudit.saveAudit(ctx.change.cid, ctx.change.times, ctx.change.sp_group, data);                    const auditors = await ctx.service.changeAudit.getUniqUserGroup(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);                        responseData.data.sort((a, b) => { return ctx.helper.compareCode(a.code, b.code) });                        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.change.status.uncheck && change.status !== audit.change.status.checkNo && change.status !== audit.change.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;                }                change.order_site_msg = change.order_site ? await ctx.service.changeAuditList.getDataById(change.order_site) : null;                const renderData = await this._getDefaultReviseInfoData(ctx, change, !edit);                // 获取原报dsk数据                const accountInfo = await ctx.service.projectAccount.getDataById(change.uid);                renderData.changeList = await ctx.service.changeAuditList.getList(change.cid);                let changeUsedData = await ctx.service.stageChange.getAllFinalUsedData(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) { // 防止未创建期时调用                    for (const cu of changeUsedData) {                        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;                const readySettle = await ctx.service.settle.getReadySettle(ctx.tender.id);                renderData.settleBills = readySettle ? await this.ctx.service.settleBills.getAllDataByCondition({ where: { settle_id: readySettle.id } }) : [];                renderData.settlePos = readySettle ? await this.ctx.service.settlePos.getAllDataByCondition({ where: { settle_id: readySettle.id } }) : [];                renderData.dskAccountData = accountInfo && accountInfo.dsk_account ? JSON.parse(accountInfo.dsk_account) : {};                renderData.dskProjects = accountInfo && accountInfo.dsk_account && accountInfo.dsk_projects ? JSON.parse(accountInfo.dsk_projects) : [];                // 台账只读、使用数据                renderData.readOnly = !edit;                renderData.changing = changing;                renderData.revising = revising;                renderData.isYb = change.uid === ctx.session.sessionUser.accountId;                await this.layout('change/revise.ejs', renderData, 'change/revise_modal.ejs');            } catch (err) {                this.log(err);                ctx.redirect(ctx.request.header.referer);            }        }        /**         * 修订 详细页面(添加台账清单-清单模式)(Get)         * @param ctx         * @return {Promise<void>}         */        async addListInfo(ctx) {            try {                const change = ctx.change;                let edit = true;                let changing = false;                if (change.status !== audit.change.status.uncheck && change.status !== audit.change.status.checkNo && change.status !== audit.change.status.revise) {                    // throw '本条变更审批中或已完成,无法操作台账数据';                    edit = false;                    changing = true;                }                change.order_site_msg = change.order_site ? await ctx.service.changeAuditList.getDataById(change.order_site) : null;                const renderData = {                    change,                    tpUnit: change.tp_decimal ? change.tp_decimal : ctx.tender.info.decimal.tp,                    upUnit: change.up_decimal ? change.up_decimal : ctx.tender.info.decimal.up,                    precision: ctx.tender.info.precision,                    tender: ctx.tender.data,                    preUrl: '/tender/' + ctx.tender.id + '/change/' + ctx.change.cid + '/information',                    jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.addList),                    auditConst: audit.change,                    settleStatus: ctx.service.settle.settleStatus,                };                // 获取原报dsk数据                const accountInfo = await ctx.service.projectAccount.getDataById(change.uid);                renderData.changeList = await ctx.service.changeAuditList.getList(change.cid);                let changeUsedData = await ctx.service.stageChange.getAllFinalUsedData(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) { // 防止未创建期时调用                    for (const cu of changeUsedData) {                        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;                const readySettle = await ctx.service.settle.getReadySettle(ctx.tender.id);                renderData.settleBills = readySettle ? await this.ctx.service.settleBills.getAllDataByCondition({ where: { settle_id: readySettle.id } }) : [];                renderData.settlePos = readySettle ? await this.ctx.service.settlePos.getAllDataByCondition({ where: { settle_id: readySettle.id } }) : [];                // 台账只读、使用数据                renderData.readOnly = !edit;                renderData.changing = changing;                renderData.isYb = change.uid === ctx.session.sessionUser.accountId;                await this.layout('change/addlist.ejs', renderData, 'change/addlist_modal.ejs');            } catch (err) {                this.log(err);                ctx.redirect(ctx.request.header.referer);            }        }        async _getDefaultReviseInfoData(ctx, change, edit) {            const [ledgerSpread, posSpread] = await spreadSetting.getLedgerSpreadSetting(ctx, change.tid, edit);            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,                precision: ctx.tender.info.precision,            };        }        async updateRevise(ctx) {            try {                if (!ctx.tender.data) 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.change.status.uncheck && change.status !== audit.change.status.checkNo && change.status !== audit.change.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 '未知操作';                }                responseData.data.changeList = await ctx.service.changeAuditList.getList(ctx.change.cid);                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.dealBills.quantity && data.dealBills.unit) {                const precision = ctx.helper.findPrecision(ctx.tender.info.precision, data.dealBills.unit);                data.dealBills.quantity = ctx.helper.round(data.dealBills.quantity, precision.value);                data.dealBills.sgfh_qty = data.dealBills.quantity;                data.dealBills.total_price = ctx.helper.mul(data.dealBills.quantity, data.dealBills.unit_price, ctx.tender.info.decimal.tp);                data.dealBills.sgfh_tp = data.dealBills.total_price;            }            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) {                if (c.status !== audit.change.status.checked || !c.final_auditor_str) {                    c.curAuditors = await ctx.service.changeProjectAudit.getAuditorsByStatus(c.id, c.status, c.times);                    if (c.status === audit.change.status.checked && c.curAuditors.length > 0) {                        const final_auditor_str = c.curAuditors[0].audit_type === auditType.key.common                            ? c.curAuditors[0].name + (c.curAuditors[0].role ? '-' + c.curAuditors[0].role : '')                            : ctx.helper.transFormToChinese(c.curAuditors[0].audit_order) + '审';                        await ctx.service.changeProject.defaultUpdate({ final_auditor_str }, { where: { id: c.id } });                    }                }            }            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,                auditType,                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');            }        }        /**         * 审批流程(Get)         * @param ctx         * @return {Promise<void>}         */        async changeProjectAuditors(ctx) {            try {                const id = JSON.parse(ctx.request.body.data).id;                const tenderId = ctx.params.id;                const changeInfo = await ctx.service.changeProject.getDataById(id);                await ctx.service.changeProject.loadChangeUser(changeInfo);                await ctx.service.changeProject.loadChangeAuditViewData(changeInfo);                ctx.body = { err: 0, msg: '', data: changeInfo };            } catch (error) {                this.log(error);                ctx.body = { err: 1, msg: error.toString(), data: null };            }        }        /**         * 新增变更立项 (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 '变更立项书编号不能为空';                }                // 在生成新变更立项后,需要copy前一个变更立项报表的签名信息                const lastChange = await ctx.service.changeProject.getLastChange(tenderId);                const change = await ctx.service.changeProject.add(tenderId, ctx.session.sessionUser.accountId, data.code, data.name);                await ctx.service.roleRptRel.createRoleRelationshipFromOtherBz(tenderId, '-302', change.id, lastChange ? lastChange.id : null);                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) {            await ctx.service.changeProject.loadChangeAuditViewData(ctx.change);        }        // 变更类别获取及更新        async _getOrUpdateClass(ctx, serviceName) {            // 工程变更类别读取            const fun_set = this.ctx.subProject.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 userPermission = pa && pa.permission ? JSON.parse(pa.permission) : null;                const deleteFilePermission = !!(userPermission && userPermission.change !== undefined && userPermission.change.indexOf('2') !== -1);                // 判断并更新                const renderData = {                    tender: ctx.tender,                    change: ctx.change,                    changeConst,                    changeClass,                    auditConst: audit.changeProject,                    fileList,                    whiteList,                    authMobile: auth_mobile,                    deleteFilePermission,                    auditType,                    shenpiConst,                    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.getAllSubProjectAccount(ctx.subProject);                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 };                }).filter(x => { return x.groupList.length > 0; });                // 获取固定审批流列表                if (ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdspl) {                    renderData.spGroupList = await ctx.service.shenpiGroup.getGroupListByChangeType(ctx.tender.id, shenpiConst.sp_type.change, 'changeProject');                }                // }                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 changeProjectSpGroup(ctx) {            try {                const data = JSON.parse(ctx.request.body.data);                const result = await ctx.service.changeProjectAudit.changeSpGroup(ctx.change, data.sp_group);                if (!result) {                    throw '切换审批组失败';                }                const auditors = await ctx.service.changeProjectAudit.getUserGroup(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 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.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.changeProjectAudit.addAuditor(ctx.change.id, id, ctx.change.times, is_gdzs);                if (!result) {                    throw '添加审核人失败';                }                const auditors = await ctx.service.changeProjectAudit.getUserGroup(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 };            }        }        async saveProjectAudit(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.change.status.checked) {                    await ctx.service.changeProjectAudit.saveAudit(ctx.change.id, ctx.change.times, ctx.change.sp_group, data);                    const auditors = await ctx.service.changeProjectAudit.getUniqUserGroup(ctx.change.id, 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 上下文         */        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.curAuditorIds.length === 0 || ctx.change.curAuditorIds.indexOf(ctx.session.sessionUser.accountId) === -1) {                    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) {                if (ctx.change.status !== audit.changeProject.status.checked) {                    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 '验证码不正确!';                    }                }                if ((ctx.change.finalAuditorIds.indexOf(ctx.session.sessionUser.accountId) >= 0) && ctx.change.status === audit.changeProject.status.checked) {                    // 重新审批                    const result = await ctx.service.changeProjectAudit.checkAgain(ctx.change);                    if (!result) {                        throw '重新审批失败';                    }                } else {                    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) {                if (c.status !== audit.change.status.checked || !c.final_auditor_str) {                    c.curAuditors = await ctx.service.changeApplyAudit.getAuditorsByStatus(c.id, c.status, c.times);                    if (c.status === audit.change.status.checked && c.curAuditors.length > 0) {                        const final_auditor_str = c.curAuditors[0].audit_type === auditType.key.common                            ? c.curAuditors[0].name + (c.curAuditors[0].role ? '-' + c.curAuditors[0].role : '')                            : ctx.helper.transFormToChinese(c.curAuditors[0].audit_order) + '审';                        await ctx.service.changeApply.defaultUpdate({ final_auditor_str }, { where: { id: c.id } });                    }                }                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,                auditType,                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');            }        }        /**         * 审批流程(Get)         * @param ctx         * @return {Promise<void>}         */        async changeApplyAuditors(ctx) {            try {                const id = JSON.parse(ctx.request.body.data).id;                const tenderId = ctx.params.id;                const changeInfo = await ctx.service.changeApply.getDataById(id);                await ctx.service.changeApply.loadChangeUser(changeInfo);                await ctx.service.changeApply.loadChangeAuditViewData(changeInfo);                ctx.body = { err: 0, msg: '', data: changeInfo };            } catch (error) {                this.log(error);                ctx.body = { err: 1, msg: error.toString(), data: null };            }        }        async changeApplySpGroup(ctx) {            try {                const data = JSON.parse(ctx.request.body.data);                const result = await ctx.service.changeApplyAudit.changeSpGroup(ctx.change, data.sp_group);                if (!result) {                    throw '切换审批组失败';                }                const auditors = await ctx.service.changeApplyAudit.getUserGroup(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 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 };            }        }        /**         * 新增变更申请 (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 '变更申请编号不能为空';                }                // 在生成新变更申请后,需要copy前一个变更申请报表的签名信息                const lastChange = await ctx.service.changeApply.getLastChange(tenderId);                const change = await ctx.service.changeApply.add(tenderId, ctx.session.sessionUser.accountId, data.code, data.project_code, data.name);                await ctx.service.roleRptRel.createRoleRelationshipFromOtherBz(tenderId, '-303', change.id, lastChange ? lastChange.id : null);                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) {            await ctx.service.changeApply.loadChangeAuditViewData(ctx.change);        }        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 userPermission = pa && pa.permission ? JSON.parse(pa.permission) : null;                const deleteFilePermission = !!(userPermission && userPermission.change !== undefined && userPermission.change.indexOf('2') !== -1);                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,                    deleteFilePermission,                    auditType,                    shenpiConst,                    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.getAllSubProjectAccount(ctx.subProject);                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 };                }).filter(x => { return x.groupList.length > 0; });                // }                // 获取固定审批流列表                if (ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdspl) {                    renderData.spGroupList = await ctx.service.shenpiGroup.getGroupListByChangeType(ctx.tender.id, shenpiConst.sp_type.change, 'changeApply');                }                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 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 pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);                const userPermission = pa && pa.permission ? JSON.parse(pa.permission) : null;                const deleteFilePermission = !!(userPermission && userPermission.change !== undefined && userPermission.change.indexOf('2') !== -1);                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',                    deleteFilePermission,                };                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.getUserGroup(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 };            }        }        async saveApplyAudit(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.change.status.checked) {                    await ctx.service.changeApplyAudit.saveAudit(ctx.change.id, ctx.change.times, ctx.change.sp_group, data);                    const auditors = await ctx.service.changeApplyAudit.getUniqUserGroup(ctx.change.id, 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 上下文         */        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.curAuditorIds.length === 0 || ctx.change.curAuditorIds.indexOf(ctx.session.sessionUser.accountId) === -1) {                    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 {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) {                    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 '验证码不正确!';                    }                }                if ((ctx.change.finalAuditorIds.indexOf(ctx.session.sessionUser.accountId) >= 0) && ctx.change.status === audit.changeApply.status.checked) {                    // 重新审批                    const result = await ctx.service.changeApplyAudit.checkAgain(ctx.change);                    if (!result) {                        throw '重新审批失败';                    }                } else {                    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) {                if (c.status !== audit.change.status.checked || !c.final_auditor_str) {                    c.curAuditors = await ctx.service.changePlanAudit.getAuditorsByStatus(c.id, c.status, c.times);                    // if (c.status === audit.changePlan.status.checkNoPre) {                    //     c.curAuditors2 = await ctx.service.changePlanAudit.getAuditorsByStatus(c.id, audit.changePlan.status.checking, c.times);                    // }                    if (c.status === audit.change.status.checked && c.curAuditors.length > 0) {                        const final_auditor_str = c.curAuditors[0].audit_type === auditType.key.common                            ? c.curAuditors[0].name + (c.curAuditors[0].role ? '-' + c.curAuditors[0].role : '')                            : ctx.helper.transFormToChinese(c.curAuditors[0].audit_order) + '审';                        await ctx.service.changePlan.defaultUpdate({ final_auditor_str }, { where: { id: c.id } });                    }                }                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,                auditType,                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');            }        }        /**         * 审批流程(Get)         * @param ctx         * @return {Promise<void>}         */        async changePlanAuditors(ctx) {            try {                const id = JSON.parse(ctx.request.body.data).id;                const tenderId = ctx.params.id;                const changeInfo = await ctx.service.changePlan.getDataById(id);                await ctx.service.changePlan.loadChangeUser(changeInfo);                await ctx.service.changePlan.loadChangeAuditViewData(changeInfo);                ctx.body = { err: 0, msg: '', data: changeInfo };            } catch (error) {                this.log(error);                ctx.body = { err: 1, msg: error.toString(), data: null };            }        }        /**         * 新增变更方案 (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 '变更方案编号不能为空';                }                // 在生成新变更方案后,需要copy前一个变更方案报表的签名信息                const lastChange = await ctx.service.changePlan.getLastChange(tenderId);                const change = await ctx.service.changePlan.add(tenderId, ctx.session.sessionUser.accountId, data.code, data.apply_code, data.name);                await ctx.service.roleRptRel.createRoleRelationshipFromOtherBz(tenderId, '-301', change.id, lastChange ? lastChange.id : null);                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) {            await ctx.service.changePlan.loadChangeAuditViewData(ctx.change);        }        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 au in ctx.change.userGroups) {                            if (ctx.change.userGroups[au][0].audit_order !== 0) {                                cl['audit_amount_' + ctx.change.userGroups[au][0].audit_order] =                                    (ctx.change.userGroups[au][0].audit_type === auditType.key.and &&                                        ctx.helper._.findIndex(ctx.change.userGroups[au], { aid: ctx.session.sessionUser.accountId, status: audit.changePlan.status.checked }) !== -1) ||                                    (ctx.change.shenpiPower && ctx.helper._.findIndex(ctx.change.userGroups[au], { aid: ctx.session.sessionUser.accountId }) !== -1) ?                                        cl.spamount : (audit_amount[au - 1] !== undefined ? parseFloat(audit_amount[au - 1]) : null);                            }                        }                    }                }                // 获取用户人验证手机号                const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);                const auth_mobile = pa.auth_mobile;                const userPermission = pa && pa.permission ? JSON.parse(pa.permission) : null;                const deleteFilePermission = !!(userPermission && userPermission.change !== undefined && userPermission.change.indexOf('2') !== -1);                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,                    deleteFilePermission,                    auditType,                    shenpiConst,                    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.getAllSubProjectAccount(ctx.subProject);                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 };                }).filter(x => { return x.groupList.length > 0; });                // 获取固定审批流列表                if (ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdspl) {                    renderData.spGroupList = await ctx.service.shenpiGroup.getGroupListByChangeType(ctx.tender.id, shenpiConst.sp_type.change, 'changePlan');                }                // }                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 changePlanSpGroup(ctx) {            try {                const data = JSON.parse(ctx.request.body.data);                const result = await ctx.service.changePlanAudit.changeSpGroup(ctx.change, data.sp_group);                if (!result) {                    throw '切换审批组失败';                }                const auditors = await ctx.service.changePlanAudit.getUserGroup(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 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.getUserGroup(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 };            }        }        async savePlanAudit(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.change.status.checked) {                    await ctx.service.changePlanAudit.saveAudit(ctx.change.id, ctx.change.times, ctx.change.sp_group, data);                    const auditors = await ctx.service.changePlanAudit.getUniqUserGroup(ctx.change.id, 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 上下文         */        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.curAuditorIds.length === 0 || ctx.change.curAuditorIds.indexOf(ctx.session.sessionUser.accountId) === -1) {                    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) {                    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 '验证码不正确!';                    }                }                if ((ctx.change.finalAuditorIds.indexOf(ctx.session.sessionUser.accountId) >= 0) && ctx.change.status === audit.changePlan.status.checked) {                    // 重新审批                    const result = await ctx.service.changePlanAudit.checkAgain(ctx.change);                    if (!result) {                        throw '重新审批失败';                    }                } else {                    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;};
 |