change_controller.js 174 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/8/14
  7. * @version
  8. */
  9. const stdDataAddType = {
  10. withParent: 1,
  11. child: 2,
  12. next: 3,
  13. };
  14. const moment = require('moment');
  15. const sendToWormhole = require('stream-wormhole');
  16. const fs = require('fs');
  17. const path = require('path');
  18. const audit = require('../const/audit');
  19. const codeRuleConst = require('../const/code_rule');
  20. const changeConst = require('../const/change');
  21. const accountGroup = require('../const/account_group').group;
  22. const shenpiConst = require('../const/shenpi');
  23. const tenderMenu = require('../../config/menu').tenderMenu;
  24. const measureType = require('../const/tender').measureType;
  25. const spreadConst = require('../const/spread');
  26. // const tenderMenu = require('../../config/menu').tenderMenu;
  27. module.exports = app => {
  28. class ChangeController extends app.BaseController {
  29. /**
  30. * 构造函数
  31. *
  32. * @param {Object} ctx - egg全局变量
  33. * @return {void}
  34. */
  35. constructor(ctx) {
  36. super(ctx);
  37. ctx.showProject = true;
  38. ctx.showTender = true;
  39. ctx.showTitle = true;
  40. }
  41. async _filterChanges(ctx, status = 0) {
  42. const tenderId = ctx.params.id;
  43. ctx.session.sessionUser.tenderId = tenderId;
  44. const tender = await this.service.tender.getDataById(tenderId);
  45. // const tenderList = await this.service.tender.getList();
  46. const page = ctx.page;
  47. const sorts = ctx.query.sort ? ctx.query.sort : 0;
  48. const orders = ctx.query.order ? ctx.query.order : 0;
  49. const changes = await ctx.service.change.getListByStatus(tender.id, status, 1, sorts, orders);
  50. const total = await ctx.service.change.getCountByStatus(tender.id, status);
  51. let page_total = 0;
  52. const tp = await ctx.service.change.getTp(tender.id, status);
  53. if (changes !== null) {
  54. let i = 0;
  55. for (const c of changes) {
  56. page_total = ctx.helper.add(page_total, c.total_price);
  57. const status = c.status === audit.flow.status.uncheck ? 0 : 1;
  58. // 根据审批人对当前变更令的状态取不同的展示方式。
  59. let changeAudit = '';
  60. let auditStatus = 0;
  61. switch (c.status) {
  62. case 1:
  63. auditStatus = 1;
  64. break;
  65. case 2:
  66. changeAudit = await ctx.service.changeAudit.getLastUser(c.cid, c.times, status);
  67. auditStatus = changeAudit.uid === ctx.session.sessionUser.accountId ? 1 : 0;
  68. break;
  69. case 3:
  70. case 4:
  71. auditStatus = 0;
  72. changeAudit = await ctx.service.changeAudit.getLastUser(c.cid, c.times, status);
  73. break;
  74. case 5:
  75. changeAudit = await ctx.service.changeAudit.getLastUser(c.cid, c.times - 1, status);
  76. auditStatus = c.uid === ctx.session.sessionUser.accountId ? 1 : 0;
  77. const back_changeUsedData = await ctx.service.stageChange.getFinalUsedData(ctx.tender.id, c.cid);
  78. c.stageChangeNum = this.ctx.helper.sum(back_changeUsedData.map(x => { return Math.abs(x.used_qty); }));
  79. break;
  80. case 6:
  81. changeAudit = await ctx.service.changeAudit.getLastBackUser(c.cid, c.times);
  82. const checkingAudit = await ctx.service.changeAudit.getLastUser(c.cid, c.times, status);
  83. auditStatus = checkingAudit.uid === ctx.session.sessionUser.accountId ? 1 : 0;
  84. break;
  85. case 9:
  86. auditStatus = 9;
  87. const changeUsedData = await ctx.service.stageChange.getFinalUsedData(ctx.tender.id, c.cid);
  88. c.stageChangeNum = this.ctx.helper.sum(changeUsedData.map(x => { return Math.abs(x.used_qty); }));
  89. break;
  90. default:
  91. break;
  92. }
  93. changes[i].changeAudit = changeAudit;
  94. changes[i].auditStatus = auditStatus;
  95. i++;
  96. }
  97. }
  98. // 分页相关
  99. const pageInfo = {
  100. page,
  101. total: Math.ceil(total / app.config.pageSize),
  102. queryData: JSON.stringify(ctx.urlInfo.query),
  103. };
  104. const filter = JSON.parse(JSON.stringify(audit.filter));
  105. filter.count = [];
  106. 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);
  107. 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);
  108. 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);
  109. 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);
  110. 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);
  111. const codeRule = tender.c_rule ? JSON.parse(tender.c_rule) : [];
  112. for (const rule of codeRule) {
  113. switch (rule.rule_type) {
  114. case codeRuleConst.measure.ruleType.dealCode:
  115. rule.preview = ctx.tender.info.deal_info.dealCode;
  116. break;
  117. case codeRuleConst.measure.ruleType.tenderName:
  118. rule.preview = tender.name;
  119. break;
  120. case codeRuleConst.measure.ruleType.inDate:
  121. rule.preview = moment().format('YYYY');
  122. break;
  123. case codeRuleConst.measure.ruleType.text:
  124. rule.preview = rule.text;
  125. break;
  126. case codeRuleConst.measure.ruleType.addNo:
  127. const s = '0000000000';
  128. rule.preview = s.substr(s.length - rule.format);
  129. break;
  130. default: break;
  131. }
  132. }
  133. const renderData = {
  134. uid: ctx.session.sessionUser.accountId,
  135. moment,
  136. tender,
  137. // tenderList,
  138. pageInfo,
  139. changes,
  140. page_total,
  141. tp,
  142. filter,
  143. status,
  144. codeRule,
  145. dealCode: ctx.tender.info.deal_info.dealCode,
  146. auditConst: audit.flow,
  147. changeConst,
  148. ruleType: codeRuleConst.ruleType.change,
  149. ruleConst: codeRuleConst.measure,
  150. tenderMenu: this.menu.tenderMenu,
  151. preUrl: '/tender/' + tenderId,
  152. tpUnit: ctx.tender.info.decimal.tp,
  153. };
  154. await this.layout('change/index.ejs', renderData, 'change/modal.ejs');
  155. }
  156. /**
  157. * 变更管理 页面 (Get)
  158. *
  159. * @param {Object} ctx - egg全局变量
  160. * @return {void}
  161. */
  162. async index(ctx) {
  163. try {
  164. await this._filterChanges(ctx);
  165. } catch (err) {
  166. this.log(err);
  167. ctx.redirect('/dashboard');
  168. }
  169. }
  170. /**
  171. *
  172. * @param {Object} ctx - egg全局变量
  173. * @return {void}
  174. */
  175. async newCode(ctx) {
  176. const responseData = {
  177. err: 0,
  178. msg: '',
  179. data: '',
  180. };
  181. try {
  182. const tenderId = ctx.params.id;
  183. if (!tenderId) {
  184. throw '当前未打开标段';
  185. }
  186. const tenderData = await ctx.service.tender.getDataById(tenderId);
  187. const data = JSON.parse(ctx.request.body.data);
  188. let cCodeRule;
  189. let cConnector;
  190. let changeCount = 0;
  191. if (data && data.type) {
  192. const c_code_rules = tenderData.c_code_rules !== null ? JSON.parse(tenderData.c_code_rules) : null;
  193. cCodeRule = c_code_rules && c_code_rules[data.type + '_rule'] ? c_code_rules[data.type + '_rule'] : [];
  194. cConnector = c_code_rules && c_code_rules[data.type + '_connector'] ? c_code_rules[data.type + '_connector'] : null;
  195. if (data.type === codeRuleConst.ruleField[codeRuleConst.ruleType.apply]) {
  196. changeCount = await ctx.service.changeApply.count({ tid: tenderId });
  197. } else {
  198. changeCount = await ctx.service.changeProject.count({ tid: tenderId, type: codeRuleConst.ruleType[data.type] });
  199. }
  200. } else {
  201. cCodeRule = tenderData.c_rule !== null ? JSON.parse(tenderData.c_rule) : [];
  202. cConnector = tenderData.c_connector;
  203. changeCount = await ctx.service.change.count({ tid: tenderId });
  204. }
  205. const code = [];
  206. for (const rule of cCodeRule) {
  207. switch (rule.rule_type) {
  208. case codeRuleConst.measure.ruleType.dealCode:
  209. code.push(ctx.tender.info.deal_info.dealCode);
  210. break;
  211. case codeRuleConst.measure.ruleType.tenderName:
  212. code.push(tenderData.name);
  213. break;
  214. case codeRuleConst.measure.ruleType.text:
  215. code.push(rule.text);
  216. break;
  217. case codeRuleConst.measure.ruleType.inDate:
  218. code.push(moment().format('YYYY'));
  219. break;
  220. case codeRuleConst.measure.ruleType.addNo:
  221. let s = '0000000000';
  222. const count = rule.start + changeCount;
  223. s = s + count;
  224. code.push(s.substr(s.length - rule.format));
  225. break;
  226. default: break;
  227. }
  228. }
  229. responseData.data = code.join(cConnector !== null && cConnector !== 3 ? codeRuleConst.measure.connectorString[cConnector] : '');
  230. } catch (err) {
  231. responseData.err = 1;
  232. responseData.msg = err;
  233. }
  234. ctx.body = responseData;
  235. }
  236. /**
  237. * 新增变更 (Post)
  238. *
  239. * @param {Object} ctx - egg全局变量
  240. * @return {void}
  241. */
  242. async add(ctx) {
  243. try {
  244. const tenderId = ctx.params.id;
  245. if (!tenderId) {
  246. throw '当前未打开标段';
  247. }
  248. const data = JSON.parse(ctx.request.body.data);
  249. if (!data.code || data.code === '' || !data.name || data.name === '') {
  250. throw '变更令号不能为空';
  251. }
  252. const change = await ctx.service.change.add(tenderId, ctx.session.sessionUser.accountId, data.code, data.name);
  253. ctx.body = { err: 0, msg: '', data: change };
  254. } catch (err) {
  255. this.log(err);
  256. ctx.body = { err: 1, msg: err.toString() };
  257. }
  258. }
  259. /**
  260. * 变更管理 状态筛选 页面 (Get)
  261. * @param {Object} ctx - egg全局变量
  262. * @return {void}
  263. */
  264. async status(ctx) {
  265. try {
  266. const status = parseInt(ctx.params.status);
  267. await this._filterChanges(ctx, status);
  268. } catch (err) {
  269. this.logger.error(err);
  270. ctx.redirect('/tender/' + ctx.params.id + '/change');
  271. }
  272. }
  273. /**
  274. * 变更信息 页面 (Get)
  275. *
  276. * @param {Object} ctx - egg全局变量
  277. * @return {void}
  278. */
  279. async info(ctx) {
  280. try {
  281. const whiteList = this.ctx.app.config.multipart.whitelist;
  282. const tenderid = ctx.params.id !== undefined ? ctx.params.id : ctx.session.sessionUser.tenderId;
  283. ctx.session.sessionUser.tenderId = tenderid;
  284. const tender = await ctx.service.tender.getDataById(tenderid);
  285. const change = await ctx.service.change.getDataByCondition({ cid: ctx.params.cid });
  286. // 后台判断当前人查看info状态
  287. const auditStatus = await ctx.service.changeAudit.getStatusByChange(change);
  288. // 获取附件列表
  289. const attList = await ctx.service.changeAtt.getChangeAttachment(ctx.params.cid);
  290. // 获取其他变更令数据
  291. const othersChange = await ctx.service.change.getOthersChange(ctx.params.id, ctx.params.cid);
  292. // 根据auditStatus获取审批人列表
  293. const auditList = await ctx.service.changeAudit.getListByStatus(change, auditStatus);
  294. // 获取已选清单
  295. let changeList = await ctx.service.changeAuditList.getAllDataByCondition({ where: { cid: ctx.params.cid } });
  296. // 获取用户人验证手机号
  297. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  298. const auth_mobile = pa.auth_mobile;
  299. const renderData = {
  300. uid: ctx.session.sessionUser.accountId,
  301. tender,
  302. change,
  303. othersChange,
  304. changeConst,
  305. auditStatus,
  306. auditConst: audit.flow,
  307. ledgerConsts: audit.ledger.status,
  308. attList,
  309. whiteList,
  310. auditList,
  311. changeList,
  312. tpUnit: change.tp_decimal ? change.tp_decimal : ctx.tender.info.decimal.tp,
  313. upUnit: change.up_decimal ? change.up_decimal : ctx.tender.info.decimal.up,
  314. authMobile: auth_mobile,
  315. shenpiConst,
  316. };
  317. // 根据auditStatus状态获取的不同的数据
  318. if (auditStatus === 1 || auditStatus === 2 || auditStatus === 9) {
  319. renderData.changeUnits = changeConst.units;
  320. renderData.precision = ctx.tender.info.precision;
  321. // renderData.accountGroup = accountGroup;
  322. // 获取所有项目参与者
  323. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  324. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  325. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group'],
  326. });
  327. renderData.accountList = accountList;
  328. renderData.accountGroup = accountGroup.map((item, idx) => {
  329. const groupList = accountList.filter(item => item.account_group === idx);
  330. return { groupName: item, groupList };
  331. });
  332. // 重新上报获取审批流程
  333. if (auditStatus === 2 || auditStatus === 9) {
  334. const auditList2 = await ctx.service.changeAudit.getListByBack(change.cid, change.times);
  335. // 展示页右侧审批流程列表
  336. const auditList3 = [];
  337. for (let time = 1; time <= change.times - 1; time++) {
  338. const auditTimeList = [];
  339. let max_sort = 1;
  340. for (const al of auditList2) {
  341. if (al.times === time) {
  342. auditTimeList.push(al);
  343. if (al.usite > max_sort) {
  344. max_sort = al.usite;
  345. }
  346. }
  347. }
  348. for (const i in auditTimeList) {
  349. auditTimeList[i].max_sort = max_sort;
  350. }
  351. auditList3.push(auditTimeList);
  352. }
  353. renderData.auditList3 = auditList3;
  354. }
  355. // 根据清单获取提交数据和计算总金额
  356. const changeListData = [];
  357. const changeWhiteListData = [];
  358. let ototalCost = 0;
  359. let ctotalCost = 0;
  360. for (const cl of changeList) {
  361. const cLArray = [
  362. cl.code,
  363. cl.name,
  364. cl.bwmx,
  365. cl.unit,
  366. cl.unit_price,
  367. cl.oamount,
  368. cl.camount,
  369. cl.detail,
  370. cl.lid,
  371. cl.xmj_code,
  372. cl.xmj_jldy,
  373. cl.gcl_id,
  374. ];
  375. ototalCost += cl.unit_price === null ? 0 : ctx.helper.mul(cl.unit_price, cl.oamount, ctx.tender.info.decimal.tp);
  376. ctotalCost += cl.unit_price === null ? 0 : ctx.helper.mul(cl.unit_price, cl.camount, ctx.tender.info.decimal.tp);
  377. if (cl.lid !== '0') {
  378. changeListData.push(cLArray.join('*;*'));
  379. } else {
  380. changeWhiteListData.push(cLArray.join('*;*'));
  381. }
  382. }
  383. renderData.changeListData = changeListData.join('^_^');
  384. renderData.changeWhiteListData = changeWhiteListData.join('^_^');
  385. renderData.ototalCost = ototalCost;
  386. renderData.ctotalCost = ctotalCost;
  387. // 获取公司列表
  388. const companyList = await ctx.service.changeCompany.getAllDataByCondition({ where: { tid: tenderid } });
  389. renderData.companyList = companyList;
  390. } else if (auditStatus === 3 || auditStatus === 4 || auditStatus === 5 || auditStatus === 7) {
  391. // 展示页左侧审批流程列表和清单审批列表数据
  392. const times = change.status === audit.flow.status.back ?
  393. change.times - 1 : change.times;
  394. const auditList2 = await ctx.service.changeAudit.getListGroupByTimes(change.cid, times);
  395. // 展示页右侧审批流程列表
  396. const auditList3 = [];
  397. for (let time = 1; time <= times; time++) {
  398. const auditTimeList = [];
  399. let max_sort = 1;
  400. for (const al of auditList) {
  401. if (al.times === time) {
  402. auditTimeList.push(al);
  403. if (al.usite > max_sort) {
  404. max_sort = al.usite;
  405. }
  406. }
  407. }
  408. for (const i in auditTimeList) {
  409. auditTimeList[i].max_sort = max_sort;
  410. }
  411. auditList3.push(auditTimeList);
  412. }
  413. renderData.auditList3 = auditList3;
  414. changeList = JSON.parse(JSON.stringify(changeList.sort())).sort().sort();
  415. renderData.changeList = changeList;
  416. let ototalCost = 0;
  417. let ctotalCost = 0;
  418. let stotalCost = 0;
  419. const auditTotalCost = [];
  420. for (const cl of changeList) {
  421. // ototalCost += cl.unit_price === null ? 0 : parseFloat(ctx.helper.roundNum(ctx.helper.accMul(cl.unit_price, cl.oamount), renderData.tpUnit));
  422. ototalCost += cl.unit_price === null ? 0 : ctx.helper.mul(cl.unit_price, cl.oamount, renderData.tpUnit);
  423. // ctotalCost += cl.unit_price === null ? 0 : parseFloat(ctx.helper.roundNum(ctx.helper.accMul(cl.unit_price, cl.camount), renderData.tpUnit));
  424. ctotalCost += cl.unit_price === null ? 0 : ctx.helper.mul(cl.unit_price, cl.camount, renderData.tpUnit);
  425. // stotalCost += cl.samount !== '' && cl.unit_price !== null ? parseFloat(ctx.helper.roundNum(ctx.helper.accMul(cl.unit_price, cl.samount), renderData.tpUnit)) : 0;
  426. stotalCost += cl.samount !== '' && cl.unit_price !== null ? ctx.helper.mul(cl.unit_price, cl.samount, renderData.tpUnit) : 0;
  427. const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
  428. auditTotalCost.push(audit_amount);
  429. }
  430. renderData.ototalCost = ototalCost;
  431. renderData.ctotalCost = ctotalCost;
  432. renderData.stotalCost = stotalCost;
  433. // 清单表页赋值
  434. for (const [index, au] of auditList2.entries()) {
  435. if (au.usite !== 0) {
  436. au.list_amount = [];
  437. au.totalCost = 0;
  438. for (const [auindex, at] of auditTotalCost.entries()) {
  439. au.list_amount.push(at[index - 1]);
  440. // au.totalCost += at[index - 1] !== undefined && changeList[auindex].unit_price !== null ? parseFloat(ctx.helper.roundNum(ctx.helper.accMul(changeList[auindex].unit_price, at[index - 1]), renderData.tpUnit)) : 0;
  441. au.totalCost += at[index - 1] !== undefined && changeList[auindex].unit_price !== null ? ctx.helper.mul(changeList[auindex].unit_price, at[index - 1], renderData.tpUnit) : 0;
  442. }
  443. }
  444. }
  445. renderData.auditList2 = auditList2;
  446. } else if (auditStatus === 6) {
  447. // 展示页左侧审批流程列表和清单审批列表数据
  448. const auditList2 = await ctx.service.changeAudit.getListGroupByTimes(change.cid, change.times);
  449. renderData.auditList2 = auditList2;
  450. const auditList3 = await ctx.service.changeAudit.getListOrderByTimes(change.cid, change.times);
  451. for (const i in auditList3) {
  452. auditList3[i].max_sort = auditList2.length - 1;
  453. }
  454. renderData.auditList3 = auditList3;
  455. // 展示页右侧审批流程列表
  456. const auditList5 = await ctx.service.changeAudit.getListByBack(change.cid, change.times);
  457. const auditList4 = [];
  458. for (let time = 1; time <= change.times; time++) {
  459. const auditTimeList = [];
  460. let max_sort = 1;
  461. for (const al of auditList5) {
  462. if (al.times === time) {
  463. auditTimeList.push(al);
  464. if (al.usite > max_sort) {
  465. max_sort = al.usite;
  466. }
  467. }
  468. }
  469. for (const i in auditTimeList) {
  470. auditTimeList[i].max_sort = max_sort;
  471. }
  472. if (auditTimeList.length > 0) {
  473. auditList4.push(auditTimeList);
  474. }
  475. }
  476. renderData.auditList4 = auditList4;
  477. changeList = JSON.parse(JSON.stringify(changeList.sort())).sort().sort();
  478. renderData.changeList = changeList;
  479. let ototalCost = 0;
  480. let ctotalCost = 0;
  481. const auditTotalCost = [];
  482. const auditUnit = [];
  483. for (const cl of changeList) {
  484. // ototalCost += cl.unit_price === null ? 0 : parseFloat(ctx.helper.roundNum(ctx.helper.accMul(cl.unit_price, cl.oamount), renderData.tpUnit));
  485. ototalCost += cl.unit_price === null ? 0 : ctx.helper.mul(cl.unit_price, cl.oamount, renderData.tpUnit);
  486. // ctotalCost += cl.unit_price === null ? 0 : parseFloat(ctx.helper.roundNum(ctx.helper.accMul(cl.unit_price, cl.camount), renderData.tpUnit));
  487. ctotalCost += cl.unit_price === null ? 0 : ctx.helper.mul(cl.unit_price, cl.camount, renderData.tpUnit);
  488. const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
  489. auditTotalCost.push(audit_amount);
  490. }
  491. renderData.ototalCost = ototalCost;
  492. renderData.ctotalCost = ctotalCost;
  493. // 清单表页赋值
  494. for (const [index, au] of auditList.entries()) {
  495. if (au.usite !== 0) {
  496. au.list_amount = [];
  497. au.totalCost = 0;
  498. if (au.uid === renderData.uid) {
  499. for (const [auindex, at] of auditTotalCost.entries()) {
  500. // if (at[index - 2] !== undefined) {
  501. // au.list_amount.push(at[index - 2]);
  502. // au.totalCost += parseFloat(ctx.helper.roundNum(ctx.helper.accMul(changeList[auindex].unit_price, at[index - 2]), renderData.tpUnit));
  503. // } else if (at[index - 2] === undefined) {
  504. // au.list_amount.push(changeList[auindex].camount);
  505. // au.totalCost += parseFloat(ctx.helper.roundNum(ctx.helper.accMul(changeList[auindex].unit_price, changeList[auindex].camount), renderData.tpUnit));
  506. // }
  507. au.list_amount.push(changeList[auindex].spamount);
  508. // au.totalCost += changeList[auindex].unit_price === null ? 0 : parseFloat(ctx.helper.roundNum(ctx.helper.accMul(changeList[auindex].unit_price, changeList[auindex].spamount), renderData.tpUnit));
  509. au.totalCost += changeList[auindex].unit_price === null ? 0 : ctx.helper.mul(changeList[auindex].unit_price, changeList[auindex].spamount, renderData.tpUnit);
  510. }
  511. } else {
  512. for (const [auindex, at] of auditTotalCost.entries()) {
  513. au.list_amount.push(at[index - 1]);
  514. // au.totalCost += at[index - 1] !== undefined && changeList[auindex].unit_price !== null ? parseFloat(ctx.helper.roundNum(ctx.helper.accMul(changeList[auindex].unit_price, at[index - 1]), renderData.tpUnit)) : 0;
  515. au.totalCost += at[index - 1] !== undefined && changeList[auindex].unit_price !== null ? ctx.helper.mul(changeList[auindex].unit_price, at[index - 1], renderData.tpUnit) : 0;
  516. }
  517. }
  518. }
  519. }
  520. }
  521. renderData.auditList = auditList;
  522. // 获取是否已存在调用变更令
  523. const changeUsedData = await ctx.service.stageChange.getFinalUsedData(ctx.tender.id, change.cid);
  524. renderData.stageChangeNum = this.ctx.helper.sum(changeUsedData.map(x => { return Math.abs(x.used_qty); }));
  525. await this.layout('change/info.ejs', renderData, 'change/info_modal.ejs');
  526. } catch (err) {
  527. this.log(err);
  528. ctx.redirect('/tender/' + ctx.params.id + '/change');
  529. }
  530. }
  531. /**
  532. * 变更信息 新版本页面 (Get)
  533. *
  534. * @param {Object} ctx - egg全局变量
  535. * @return {void}
  536. */
  537. async information(ctx) {
  538. try {
  539. const whiteList = this.ctx.app.config.multipart.whitelist;
  540. const tender = await ctx.service.tender.getDataById(ctx.tender.id);
  541. const change = ctx.change;
  542. // 后台判断当前人查看info状态
  543. const auditStatus = await ctx.service.changeAudit.getStatusByChange(change);
  544. // 获取附件列表
  545. const attList = await ctx.service.changeAtt.getChangeAttachment(change.cid, 'desc');
  546. // 获取其他变更令数据
  547. const othersChange = await ctx.service.change.getOthersChange(ctx.tender.id, change.cid);
  548. // 根据auditStatus获取审批人列表
  549. const auditList = await ctx.service.changeAudit.getListByStatus(change, auditStatus);
  550. // 获取用户人验证手机号
  551. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  552. const auth_mobile = pa.auth_mobile;
  553. const renderData = {
  554. tender,
  555. change,
  556. othersChange,
  557. changeConst,
  558. auditStatus,
  559. auditConst: audit.flow,
  560. ledgerConsts: audit.ledger.status,
  561. attList,
  562. whiteList,
  563. auditList,
  564. tpUnit: change.tp_decimal ? change.tp_decimal : ctx.tender.info.decimal.tp,
  565. upUnit: change.up_decimal ? change.up_decimal : ctx.tender.info.decimal.up,
  566. authMobile: auth_mobile,
  567. shenpiConst,
  568. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.information),
  569. preUrl: '/tender/' + ctx.tender.id + '/change/' + ctx.change.cid + '/information',
  570. precision: ctx.tender.info.precision,
  571. };
  572. // 根据auditStatus状态获取的不同的数据
  573. if (auditStatus === 1 || auditStatus === 2 || auditStatus === 9) {
  574. renderData.changeUnits = changeConst.units;
  575. // renderData.accountGroup = accountGroup;
  576. // 获取所有项目参与者
  577. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  578. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  579. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group'],
  580. });
  581. renderData.accountList = accountList;
  582. renderData.accountGroup = accountGroup.map((item, idx) => {
  583. const groupList = accountList.filter(item => item.account_group === idx);
  584. return { groupName: item, groupList };
  585. });
  586. // 重新上报获取审批流程
  587. if (auditStatus === 2 || auditStatus === 9) {
  588. const auditList2 = await ctx.service.changeAudit.getListByBack(change.cid, change.times);
  589. // 展示页右侧审批流程列表
  590. const auditList3 = [];
  591. for (let time = 1; time <= change.times - 1; time++) {
  592. const auditTimeList = [];
  593. let max_sort = 1;
  594. for (const al of auditList2) {
  595. if (al.times === time) {
  596. auditTimeList.push(al);
  597. if (al.usite > max_sort) {
  598. max_sort = al.usite;
  599. }
  600. }
  601. }
  602. for (const i in auditTimeList) {
  603. auditTimeList[i].max_sort = max_sort;
  604. }
  605. auditList3.push(auditTimeList);
  606. }
  607. renderData.auditList3 = auditList3;
  608. }
  609. // 获取公司列表
  610. const companyList = await ctx.service.changeCompany.getAllDataByCondition({ where: { tid: ctx.tender.id } });
  611. renderData.companyList = companyList;
  612. // 获取已选清单
  613. const changeList = await ctx.service.changeAuditList.getList(change.cid);
  614. renderData.changeList = changeList;
  615. renderData.changeLedgerList = await ctx.service.changeLedger.getAllDataByCondition({ where: { tender_id: ctx.tender.id } });
  616. renderData.changePosList = await ctx.service.changePos.getAllDataByCondition({ where: { tid: ctx.tender.id } });
  617. } else if (auditStatus === 3 || auditStatus === 4 || auditStatus === 5 || auditStatus === 7 || auditStatus === 8) {
  618. // 展示页左侧审批流程列表和清单审批列表数据
  619. const times = change.status === audit.flow.status.back ?
  620. change.times - 1 : change.times;
  621. const auditList2 = await ctx.service.changeAudit.getListGroupByTimes(change.cid, times);
  622. // 展示页右侧审批流程列表
  623. const auditList3 = [];
  624. for (let time = 1; time <= times; time++) {
  625. const auditTimeList = [];
  626. let max_sort = 1;
  627. for (const al of auditList) {
  628. if (al.times === time) {
  629. auditTimeList.push(al);
  630. if (al.usite > max_sort) {
  631. max_sort = al.usite;
  632. }
  633. }
  634. }
  635. for (const i in auditTimeList) {
  636. auditTimeList[i].max_sort = max_sort;
  637. }
  638. auditList3.push(auditTimeList);
  639. }
  640. renderData.auditList3 = auditList3;
  641. // 获取已选清单
  642. // let changeList = await ctx.service.changeAuditList.getAllDataByCondition({ where: { cid: ctx.params.cid } });
  643. const changeList = await ctx.service.changeAuditList.getList(change.cid);
  644. // changeList = JSON.parse(JSON.stringify(changeList.sort())).sort().sort();
  645. for (const cl of changeList) {
  646. const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
  647. // 清单表页赋值
  648. for (const [index, au] of auditList2.entries()) {
  649. if (au.usite !== 0) {
  650. cl['audit_amount_' + au.uid] = audit_amount[index - 1] ? audit_amount[index - 1] : null;
  651. }
  652. }
  653. }
  654. renderData.changeList = changeList;
  655. renderData.auditList2 = auditList2;
  656. } else if (auditStatus === 6) {
  657. // 展示页左侧审批流程列表和清单审批列表数据
  658. const auditList2 = await ctx.service.changeAudit.getListGroupByTimes(change.cid, change.times);
  659. renderData.auditList2 = auditList2;
  660. const auditList3 = await ctx.service.changeAudit.getListOrderByTimes(change.cid, change.times);
  661. for (const i in auditList3) {
  662. auditList3[i].max_sort = auditList2.length - 1;
  663. }
  664. renderData.auditList3 = auditList3;
  665. // 展示页右侧审批流程列表
  666. const auditList5 = await ctx.service.changeAudit.getListByBack(change.cid, change.times);
  667. const auditList4 = [];
  668. for (let time = 1; time <= change.times; time++) {
  669. const auditTimeList = [];
  670. let max_sort = 1;
  671. for (const al of auditList5) {
  672. if (al.times === time) {
  673. auditTimeList.push(al);
  674. if (al.usite > max_sort) {
  675. max_sort = al.usite;
  676. }
  677. }
  678. }
  679. for (const i in auditTimeList) {
  680. auditTimeList[i].max_sort = max_sort;
  681. }
  682. if (auditTimeList.length > 0) {
  683. auditList4.push(auditTimeList);
  684. }
  685. }
  686. renderData.auditList4 = auditList4;
  687. // 获取已选清单
  688. // let changeList = await ctx.service.changeAuditList.getAllDataByCondition({ where: { cid: ctx.params.cid } });
  689. const changeList = await ctx.service.changeAuditList.getList(change.cid);
  690. // changeList = JSON.parse(JSON.stringify(changeList.sort())).sort().sort();
  691. for (const cl of changeList) {
  692. const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
  693. // 清单表页赋值
  694. for (const [index, au] of auditList2.entries()) {
  695. if (au.usite !== 0) {
  696. cl['audit_amount_' + au.uid] = au.uid === ctx.session.sessionUser.accountId ? cl.spamount : (audit_amount[index - 1] ? audit_amount[index - 1] : null);
  697. }
  698. }
  699. }
  700. renderData.changeList = changeList;
  701. renderData.changeLedgerList = await ctx.service.changeLedger.getAllDataByCondition({ where: { tender_id: ctx.tender.id } });
  702. renderData.changePosList = await ctx.service.changePos.getAllDataByCondition({ where: { tid: ctx.tender.id } });
  703. }
  704. renderData.auditList = auditList;
  705. // 获取是否已存在调用变更令
  706. let changeUsedData = await ctx.service.stageChange.getFinalUsedData(ctx.tender.id, change.cid);
  707. changeUsedData = ctx.helper._.filter(changeUsedData, function(item) {
  708. return item.used_qty !== null;
  709. })
  710. renderData.changeUsedData = changeUsedData;
  711. renderData.stageChangeNum = this.ctx.helper.sum(changeUsedData.map(x => { return Math.abs(x.used_qty); }));
  712. await this.layout('change/information.ejs', renderData, 'change/information_modal.ejs');
  713. } catch (err) {
  714. this.log(err);
  715. ctx.redirect('/tender/' + ctx.params.id + '/change');
  716. }
  717. }
  718. /**
  719. * 变更清单 - 操作 (Ajax)
  720. * @param ctx
  721. * @return {Promise<void>}
  722. */
  723. async saveListsData(ctx) {
  724. try {
  725. const data = JSON.parse(ctx.request.body.data);
  726. const responseData = {
  727. err: 0,
  728. msg: '',
  729. data: {},
  730. };
  731. switch (data.type) {
  732. case 'add':
  733. responseData.data = await ctx.service.changeAuditList.add(data.postData);
  734. break;
  735. case 'batchadd':
  736. responseData.data = await ctx.service.changeAuditList.batchAdd(data);
  737. break;
  738. case 'del':
  739. await ctx.service.changeAuditList.del(data.id);
  740. break;
  741. case 'update':
  742. await ctx.service.changeAuditList.save(data.updateData);
  743. break;
  744. case 'paste':
  745. await ctx.service.changeAuditList.saveDatas(data.updateData);
  746. // 取所有工料表
  747. responseData.data = await ctx.service.changeAuditList.getList(ctx.change.cid);
  748. break;
  749. case 'ledger_list':
  750. await ctx.service.changeAuditList.saveLedgerListDatas(data.updateData);
  751. // 取所有工料表
  752. responseData.data = { changeList: await ctx.service.changeAuditList.getList(ctx.change.cid),
  753. usedList: await ctx.service.stageChange.getFinalUsedData(ctx.tender.id, ctx.change.cid) };
  754. break;
  755. case 'remove_list':
  756. await ctx.service.changeAuditList.removeLedgerListDatas(data.updateData);
  757. // 取所有工料表
  758. responseData.data = await ctx.service.changeAuditList.getList(ctx.change.cid);
  759. break;
  760. case 'update_list':
  761. await ctx.service.changeAuditList.saveDatas(data.updateData);
  762. // 取所有工料表
  763. // responseData.data = await ctx.service.changeAuditList.getList(ctx.change.cid);
  764. break;
  765. case 'info':
  766. await ctx.service.change.saveInfo(data.updateData);
  767. // 取所有工料表
  768. responseData.data = '保存成功';
  769. break;
  770. case 'paste_amount_rows':
  771. await ctx.service.changeAuditList.saveDatas(data.updateData);
  772. let changeList = await ctx.service.changeAuditList.getList(ctx.change.cid);
  773. const auditList2 = await ctx.service.changeAudit.getListGroupByTimes(ctx.change.cid, ctx.change.times);
  774. changeList = JSON.parse(JSON.stringify(changeList.sort())).sort().sort();
  775. for (const cl of changeList) {
  776. const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
  777. // 清单表页赋值
  778. for (const [index, au] of auditList2.entries()) {
  779. if (au.usite !== 0) {
  780. cl['audit_amount_' + au.uid] = au.uid === ctx.session.sessionUser.accountId ? cl.spamount : (audit_amount[index - 1] ? audit_amount[index - 1] : null);
  781. }
  782. }
  783. }
  784. // 取所有工料表
  785. responseData.data = changeList;
  786. break;
  787. default: throw '参数有误';
  788. }
  789. ctx.body = responseData;
  790. } catch (err) {
  791. this.log(err);
  792. ctx.body = { err: 1, msg: err.toString(), data: null };
  793. }
  794. }
  795. /**
  796. * 上报和重新上报
  797. * @param ctx
  798. * @return {Promise<void>}
  799. */
  800. async startAudit(ctx) {
  801. try {
  802. // 检查权限等
  803. if (!ctx.change) {
  804. throw '数据错误';
  805. }
  806. if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
  807. throw '您无权上报该变更令数据';
  808. }
  809. if (!(ctx.change.status === audit.flow.status.uncheck || ctx.change.status === audit.flow.status.back || ctx.change.status === audit.flow.status.revise)) {
  810. throw '该变更令当前无法上报';
  811. }
  812. // 判断是否是修订,判断有无审批人员作弊
  813. if ((ctx.change.status === audit.flow.status.revise || ctx.change.is_revise) && ctx.tender.info.shenpi.change === shenpiConst.sp_status.sqspr) {
  814. // 获取上一次的审批人流程
  815. const lastUserList = await ctx.service.changeAudit.getListGroupByTimes(ctx.change.cid, ctx.change.times - 1);
  816. const lastUidList = ctx.helper._.map(lastUserList, 'uid');
  817. // 判断上一次审批是否为非原报为审批人
  818. const nowUidList = ctx.helper._.map(ctx.change.auditors, 'uid');
  819. // 判断条件修订可上报条件
  820. // 1.有原报,有其他人 可以添加原报作为审核人,但是不能只原报上报
  821. // 2.有原报,无其他人 可以添加原报作为审核人,可以只原报上报
  822. // 3.无原报,有其他人 不可以添加原报
  823. const noYBUidList = ctx.change.status === audit.flow.status.revise ? ctx.helper._.initial(ctx.helper._.tail(lastUidList)) : ctx.helper._.tail(lastUidList);
  824. if (!ctx.helper._.isEqual(lastUidList, nowUidList) && ctx.helper._.includes(noYBUidList, lastUidList[0]) && nowUidList.length === 2 && nowUidList[0] === nowUidList[1]) {
  825. throw '该变更令不能指定原报人为单独审批人';
  826. }
  827. if (!ctx.helper._.isEqual(lastUidList, nowUidList) && !ctx.helper._.includes(noYBUidList, lastUidList[0]) && ctx.helper._.includes(ctx.helper._.tail(nowUidList), nowUidList[0])) {
  828. throw '该变更令不能指定原报为审批人,请移除';
  829. }
  830. }
  831. await ctx.service.changeAudit.start(ctx.change.cid, ctx.change.times);
  832. ctx.redirect(ctx.request.header.referer);
  833. } catch (err) {
  834. this.log(err);
  835. ctx.session.postError = err.toString();
  836. ctx.redirect(ctx.request.header.referer);
  837. }
  838. }
  839. // 审批相关
  840. /**
  841. * 添加审批人
  842. * @param ctx
  843. * @return {Promise<void>}
  844. */
  845. async addAudit(ctx) {
  846. try {
  847. const data = JSON.parse(ctx.request.body.data);
  848. const id = this.app._.toInteger(data.auditorId);
  849. if (isNaN(id) || id <= 0) {
  850. throw '参数错误';
  851. }
  852. // 检查权限等
  853. if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
  854. throw '您无权添加审核人';
  855. }
  856. if (ctx.change.status === audit.flow.status.checking || ctx.change.status === audit.flow.status.checked) {
  857. throw '当前不允许添加审核人';
  858. }
  859. const auditorList = await ctx.service.changeAudit.getAuditors(ctx.change.cid, ctx.change.times);
  860. // 检查审核人是否已存在
  861. const exist = this.app._.find(auditorList, { uid: id });
  862. if (exist) {
  863. throw '该审核人已存在,请勿重复添加';
  864. }
  865. const shenpiInfo = await ctx.service.shenpiAudit.getDataByCondition({ tid: ctx.tender.id, sp_type: shenpiConst.sp_type.change, sp_status: shenpiConst.sp_status.gdzs });
  866. const is_gdzs = shenpiInfo && ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdzs ? 1 : 0;
  867. const result = await ctx.service.changeAudit.addAuditor(ctx.change.cid, id, ctx.change.times, is_gdzs);
  868. if (!result) {
  869. throw '添加审核人失败';
  870. }
  871. // const auditors = await ctx.service.changeAudit.getAuditorsWithOwner(ctx.change.id, ctx.change.times);
  872. const auditors = null;
  873. ctx.body = { err: 0, msg: '', data: auditors };
  874. } catch (err) {
  875. this.log(err);
  876. ctx.body = { err: 1, msg: err.toString(), data: null };
  877. }
  878. }
  879. /**
  880. * 移除审批人
  881. * @param ctx
  882. * @return {Promise<void>}
  883. */
  884. async deleteAudit(ctx) {
  885. try {
  886. const data = JSON.parse(ctx.request.body.data);
  887. const id = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
  888. if (isNaN(id) || id <= 0) {
  889. throw '参数错误';
  890. }
  891. const result = await ctx.service.changeAudit.deleteAuditor(ctx.change.cid, id, ctx.change.times);
  892. if (!result) {
  893. throw '移除审核人失败';
  894. }
  895. const auditors = null;
  896. ctx.body = { err: 0, msg: '', data: auditors };
  897. } catch (err) {
  898. ctx.body = { err: 1, msg: err.toString(), data: null };
  899. }
  900. }
  901. async defaultBills(ctx) {
  902. try {
  903. // 判断是否台账修订中,修订中则不获取changeLedger及changePos值
  904. const lastRevise = await ctx.service.ledgerRevise.getLastestRevise(ctx.tender.id);
  905. const data = JSON.parse(ctx.request.body.data);
  906. const ledgerData = await ctx.service.ledger.getData(ctx.tender.id);
  907. const changeLedgerData = !ctx.session.sessionProject.page_show.openChangeRevise ? [] : (lastRevise && lastRevise.status !== audit.revise.status.checked && data.from !== 'revise' ? [] : await ctx.service.changeLedger.getData(ctx.tender.id));
  908. const posData = await ctx.service.pos.getPosData({ tid: ctx.tender.id });
  909. const changePosData = !ctx.session.sessionProject.page_show.openChangeRevise ? [] : (lastRevise && lastRevise.status !== audit.revise.status.checked && data.from !== 'revise' ? [] : await ctx.service.changePos.getPosData({ tid: ctx.tender.id }));
  910. const dealBills = await ctx.service.dealBills.getAllDataByCondition({ where: { tender_id: ctx.tender.id } });
  911. // 标记ledger,搜索需求
  912. if (changePosData.length > 0) {
  913. const cplIdList = ctx.helper._.uniq(ctx.helper._.map(changePosData, 'lid'));
  914. const cLIdList = ctx.helper._.map(changeLedgerData, 'id');
  915. const ledgerIdList = [];
  916. for (const cp of cplIdList) {
  917. if (ctx.helper._.indexOf(cLIdList, cp) === -1) {
  918. ledgerIdList.push(cp);
  919. }
  920. }
  921. if (ledgerIdList.length > 0) {
  922. for (const lid of ledgerIdList) {
  923. const data = ctx.helper._.find(ledgerData, { id: lid });
  924. data.cid = 1;
  925. }
  926. }
  927. }
  928. ctx.body = { err: 0, msg: '', data: { bills: ctx.helper._.concat(ledgerData, changeLedgerData), pos: ctx.helper._.concat(posData, changePosData), dealBills } };
  929. } catch (err) {
  930. this.log(err);
  931. ctx.body = { err: 1, msg: err.toString(), data: [] };
  932. }
  933. }
  934. /**
  935. * 变更令上报和保存
  936. * @param {Object} ctx - egg全局变量
  937. * @return {void}
  938. */
  939. async save(ctx) {
  940. // 变更令信息
  941. const changeInfo = await ctx.service.change.getDataByCondition({ cid: ctx.request.body.cid });
  942. try {
  943. const result = await ctx.service.change.save(ctx.request.body, changeInfo.tid);
  944. if (!result) {
  945. throw '上报失败';
  946. }
  947. if (ctx.request.body.changestatus !== undefined && parseInt(ctx.request.body.changestatus) === 2) {
  948. ctx.body = { err: 0, msg: '保存成功' };
  949. } else {
  950. ctx.redirect('/tender/' + changeInfo.tid + '/change');
  951. }
  952. } catch (err) {
  953. this.log(err);
  954. if (ctx.request.body.changestatus !== undefined && parseInt(ctx.request.body.changestatus) === 2) {
  955. ctx.body = { err: 1, msg: err.toString() };
  956. } else {
  957. ctx.redirect('/tender/' + changeInfo.tid + '/change/' + ctx.request.body.cid + '/info');
  958. }
  959. }
  960. }
  961. /**
  962. * 变更令审批
  963. * @param {Object} ctx - egg全局变量
  964. * @return {void}
  965. */
  966. async approval(ctx) {
  967. try {
  968. const changeData = await ctx.service.change.getDataByCondition({ cid: ctx.request.body.change_id });
  969. if (!changeData) {
  970. throw '变更令数据错误';
  971. }
  972. const status = parseInt(ctx.request.body.status);
  973. let result = false;
  974. const pid = this.ctx.session.sessionProject.id;
  975. switch (status) {
  976. case 3:// 审批通过
  977. result = await ctx.service.change.approvalSuccess(pid, ctx.request.body, changeData);
  978. break;
  979. case 4:// 审批终止
  980. result = await ctx.service.change.approvalStop(ctx.request.body);
  981. break;
  982. case 5:// 审批退回到原报人
  983. result = await ctx.service.change.approvalBack(pid, ctx.request.body, changeData);
  984. break;
  985. case 6:// 审批退回到上一个审批人
  986. result = await ctx.service.change.approvalBackNew(pid, ctx.request.body, changeData);
  987. break;
  988. default:break;
  989. }
  990. if (!result) {
  991. throw '审批失败';
  992. }
  993. ctx.redirect(ctx.request.header.referer);
  994. } catch (err) {
  995. console.log(err);
  996. ctx.redirect(ctx.request.header.referer);
  997. }
  998. }
  999. /**
  1000. * 变更公司管理
  1001. * @param {Object} ctx - egg全局变量
  1002. * @return {void}
  1003. */
  1004. async updateCompany(ctx) {
  1005. const responseData = {
  1006. err: 0,
  1007. msg: '',
  1008. data: '',
  1009. };
  1010. try {
  1011. const data = JSON.parse(ctx.request.body.data);
  1012. if (data.tid === undefined || data.uci === undefined || data.uc === undefined || data.ac === undefined) {
  1013. throw '参数有误';
  1014. }
  1015. const [addCompany, selectCompany] = await ctx.service.changeCompany.setCompanyList(data);
  1016. responseData.data = { add: addCompany, select: selectCompany };
  1017. } catch (err) {
  1018. responseData.err = 1;
  1019. responseData.msg = err;
  1020. }
  1021. ctx.body = responseData;
  1022. }
  1023. /**
  1024. * 上传附件
  1025. * @param {Object} ctx - egg全局变量
  1026. * @return {void}
  1027. */
  1028. async uploadFile(ctx) {
  1029. const responseData = {
  1030. err: 0,
  1031. msg: '',
  1032. data: [],
  1033. };
  1034. let stream;
  1035. try {
  1036. const parts = ctx.multipart({ autoFields: true });
  1037. const files = [];
  1038. let index = 0;
  1039. const change = await ctx.service.change.getDataByCondition({ cid: ctx.params.cid });
  1040. const extra_upload = change.status === audit.flow.status.checked;
  1041. while ((stream = await parts()) !== undefined) {
  1042. // 判断用户是否选择上传文件
  1043. if (!stream.filename) {
  1044. throw '请选择上传的文件!';
  1045. }
  1046. // const create_time = Date.parse(new Date()) / 1000;
  1047. // const fileInfo = path.parse(stream.filename);
  1048. // const dirName = 'app/public/upload/changes/' + moment().format('YYYYMMDD');
  1049. // const fileName = 'changes' + create_time + '_' + index + fileInfo.ext;
  1050. // // 判断文件夹是否存在,不存在则直接创建文件夹
  1051. // if (!fs.existsSync(path.join(this.app.baseDir, dirName))) {
  1052. // await fs.mkdirSync(path.join(this.app.baseDir, dirName));
  1053. // }
  1054. // // 保存文件
  1055. // await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, dirName, fileName));
  1056. const fileInfo = path.parse(stream.filename);
  1057. const create_time = Date.parse(new Date()) / 1000;
  1058. // const filepath = `app/public/upload/change/fujian_${create_time + index.toString() + fileInfo.ext}`;
  1059. const filepath = `app/public/upload/${parts.field.tid}/change/fujian_${create_time + index.toString() + fileInfo.ext}`;
  1060. // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, filepath));
  1061. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  1062. await sendToWormhole(stream);
  1063. // 保存数据到att表
  1064. const fileData = {
  1065. in_time: create_time,
  1066. filename: fileInfo.name,
  1067. fileext: fileInfo.ext,
  1068. filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
  1069. filepath,
  1070. extra_upload,
  1071. };
  1072. const result = await ctx.service.changeAtt.save(parts.field, fileData, ctx.session.sessionUser.accountId);
  1073. if (!result) {
  1074. throw '导入数据库保存失败';
  1075. }
  1076. // fileData.in_time = moment(create_time * 1000).format('YYYY-MM-DD');
  1077. // fileData.filesize = await ctx.helper.bytesToSize(fileData.filesize);
  1078. fileData.uid = ctx.session.sessionUser.accountId;
  1079. fileData.id = result.insertId;
  1080. fileData.orginpath = ctx.app.config.fujianOssPath + filepath;
  1081. delete fileData.filepath;
  1082. files.push(fileData);
  1083. ++index;
  1084. }
  1085. responseData.data = files;
  1086. } catch (err) {
  1087. this.log(err);
  1088. // 失败需要消耗掉stream 以防卡死
  1089. if (stream) {
  1090. await sendToWormhole(stream);
  1091. }
  1092. this.setMessage(err.toString(), this.messageType.ERROR);
  1093. }
  1094. ctx.body = responseData;
  1095. }
  1096. /**
  1097. * 下载附件
  1098. * @param {Object} ctx - egg全局变量
  1099. * @return {void}
  1100. */
  1101. async downloadFile(ctx) {
  1102. const id = ctx.params.id;
  1103. if (id) {
  1104. try {
  1105. const fileInfo = await ctx.service.changeAtt.getDataById(id);
  1106. if (fileInfo !== undefined && fileInfo !== '') {
  1107. // const fileName = path.join(this.app.baseDir, fileInfo.filepath);
  1108. // 解决中文无法下载问题
  1109. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  1110. let disposition = '';
  1111. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  1112. disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename + fileInfo.fileext);
  1113. } else if (userAgent.indexOf('firefox') >= 0) {
  1114. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename + fileInfo.fileext) + '"';
  1115. } else {
  1116. /* safari等其他非主流浏览器只能自求多福了 */
  1117. disposition = 'attachment; filename=' + new Buffer(fileInfo.filename + fileInfo.fileext).toString('binary');
  1118. }
  1119. ctx.response.set({
  1120. 'Content-Type': 'application/octet-stream',
  1121. 'Content-Disposition': disposition,
  1122. 'Content-Length': fileInfo.filesize,
  1123. });
  1124. // ctx.body = await fs.createReadStream(fileName);
  1125. ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
  1126. } else {
  1127. throw '不存在该文件';
  1128. }
  1129. } catch (err) {
  1130. this.log(err);
  1131. this.setMessage(err.toString(), this.messageType.ERROR);
  1132. }
  1133. }
  1134. }
  1135. /**
  1136. * 批量下载 - 压缩成zip文件返回
  1137. * @param {Oject} ctx - 全局上下文
  1138. */
  1139. async downloadZip(ctx) {
  1140. const time = Date.now();
  1141. const zipPath = `app/public/upload/change/fu_jian_zip${time}.zip`;
  1142. const responseData = {
  1143. err: 0,
  1144. msg: '',
  1145. };
  1146. try {
  1147. // const fileIds = JSON.parse(ctx.request.query.fileIds);
  1148. const { fileIds = [] } = JSON.parse(ctx.request.body.data);
  1149. const { name: changeName } = await ctx.service.changeAtt.getChangeName(ctx.params.cid);
  1150. const zipFilename = `${ctx.tender.data.name}-工程变更-${changeName}-附件.zip`;
  1151. const size = await ctx.service.changeAtt.compressedFile(fileIds, zipPath);
  1152. // 解决中文无法下载问题
  1153. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  1154. let disposition = '';
  1155. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  1156. disposition = 'attachment; filename=' + encodeURIComponent(zipFilename);
  1157. } else if (userAgent.indexOf('firefox') >= 0) {
  1158. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(zipFilename) + '"';
  1159. } else {
  1160. /* safari等其他非主流浏览器只能自求多福了 */
  1161. disposition = 'attachment; filename=' + new Buffer(zipFilename).toString('binary');
  1162. }
  1163. ctx.response.set({
  1164. 'Content-Type': 'application/octet-stream',
  1165. 'Content-Disposition': disposition,
  1166. 'Content-Length': size,
  1167. });
  1168. const readStream = fs.createReadStream(path.join(this.app.baseDir, zipPath));
  1169. ctx.body = readStream;
  1170. // ctx.body = fs.readFileSync(path.resolve(this.app.baseDir, zipPath));
  1171. readStream.on('close', () => {
  1172. if (fs.existsSync(path.resolve(this.app.baseDir, zipPath))) {
  1173. fs.unlinkSync(path.resolve(this.app.baseDir, zipPath));
  1174. }
  1175. });
  1176. // fs的错误不能被try catch捕捉
  1177. readStream.on('error', err => {
  1178. this.log(err);
  1179. if (fs.existsSync(path.resolve(this.app.baseDir, zipPath))) {
  1180. fs.unlinkSync(path.resolve(this.app.baseDir, zipPath));
  1181. }
  1182. responseData.err = 1;
  1183. responseData.msg = err.toString();
  1184. ctx.body = responseData;
  1185. });
  1186. } catch (err) {
  1187. this.log(err);
  1188. if (fs.existsSync(path.resolve(this.app.baseDir, zipPath))) {
  1189. fs.unlinkSync(path.resolve(this.app.baseDir, zipPath));
  1190. }
  1191. this.setMessage(err.toString(), this.messageType.ERROR);
  1192. responseData.err = 1;
  1193. responseData.msg = err.toString();
  1194. ctx.body = responseData;
  1195. }
  1196. }
  1197. /**
  1198. * 删除附件
  1199. * @param {Object} ctx - egg全局变量
  1200. * @return {void}
  1201. */
  1202. async deleteFile(ctx) {
  1203. const responseData = {
  1204. err: 0,
  1205. msg: '',
  1206. data: '',
  1207. };
  1208. try {
  1209. const data = JSON.parse(ctx.request.body.data);
  1210. const change = await ctx.service.change.getDataByCondition({ cid: ctx.params.cid });
  1211. const fileInfo = await ctx.service.changeAtt.getDataById(data.id);
  1212. if (!fileInfo || !Object.keys(fileInfo).length) {
  1213. throw '该文件不存在';
  1214. }
  1215. if (!fileInfo.extra_upload && change.status === audit.flow.status.checked) {
  1216. throw '无权限删除';
  1217. }
  1218. if (fileInfo !== undefined && fileInfo !== '') {
  1219. // 先删除文件
  1220. // await fs.unlinkSync(path.join(this.app.baseDir, fileInfo.filepath));
  1221. await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
  1222. // 再删除数据库
  1223. await ctx.service.changeAtt.deleteById(data.id);
  1224. responseData.data = '';
  1225. } else {
  1226. throw '不存在该文件';
  1227. }
  1228. // if (data.tid === undefined || data.uci === undefined || data.uc === undefined || data.ac === undefined) {
  1229. // throw '参数有误';
  1230. // }
  1231. // const [addCompany, selectCompany] = await ctx.service.changeCompany.setCompanyList(data);
  1232. // responseData.data = { add: addCompany, select: selectCompany };
  1233. } catch (err) {
  1234. responseData.err = 1;
  1235. responseData.msg = err;
  1236. }
  1237. ctx.body = responseData;
  1238. }
  1239. /**
  1240. * 查看附件
  1241. * @param {Object} ctx - egg全局变量
  1242. * @return {void}
  1243. */
  1244. async checkFile(ctx) {
  1245. const responseData = { err: 0, msg: '' };
  1246. const id = parseInt(ctx.params.id);
  1247. if (id) {
  1248. try {
  1249. const fileInfo = await ctx.service.changeAtt.getDataById(id);
  1250. if (fileInfo && Object.keys(fileInfo).length) {
  1251. let filepath = fileInfo.filepath;
  1252. if (!ctx.helper.canPreview(fileInfo.fileext)) {
  1253. filepath = `/change/download/file/${fileInfo.id}`;
  1254. } else {
  1255. filepath = filepath.replace(/^app|\/app/, '');
  1256. }
  1257. fileInfo.filepath && (responseData.data = { filepath });
  1258. }
  1259. } catch (error) {
  1260. this.log(error);
  1261. this.setMessage(error.toString(), this.messageType.ERROR);
  1262. responseData.err = 1;
  1263. responseData.msg = error.toString();
  1264. }
  1265. }
  1266. ctx.body = responseData;
  1267. }
  1268. /**
  1269. * 删除变更令
  1270. * @param {Object} ctx - egg全局变量
  1271. * @return {void}
  1272. */
  1273. async delete(ctx) {
  1274. try {
  1275. const result = await ctx.service.change.delete(ctx.request.body.cid);
  1276. if (!result) {
  1277. throw '删除变更令失败';
  1278. }
  1279. ctx.redirect(ctx.request.header.referer);
  1280. } catch (err) {
  1281. console.log(err);
  1282. ctx.redirect(ctx.request.header.referer);
  1283. }
  1284. }
  1285. /**
  1286. * 变更令重新审批
  1287. * @param {Object} ctx - egg全局变量
  1288. * @return {void}
  1289. */
  1290. async checkAgain(ctx) {
  1291. try {
  1292. const changeData = await ctx.service.change.getDataByCondition({ cid: ctx.request.body.cid });
  1293. if (!changeData) {
  1294. throw '变更令数据错误';
  1295. }
  1296. // 获取终审
  1297. const auditInfo = (await this.ctx.service.changeAudit.getAllDataByCondition({ where: { cid: changeData.cid }, orders: [['usort', 'desc']], limit: 1, offset: 0 }))[0];
  1298. if (changeData.status !== audit.flow.status.checked || ctx.session.sessionUser.accountId !== auditInfo.uid) {
  1299. throw '您无权进行该操作';
  1300. }
  1301. if (ctx.session.sessionUser.loginStatus === 0) {
  1302. const code = ctx.request.body.code;
  1303. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  1304. if (!pa.auth_mobile) {
  1305. throw '未绑定手机号';
  1306. }
  1307. const cacheKey = 'smsCode:' + ctx.session.sessionUser.accountId;
  1308. const cacheCode = await app.redis.get(cacheKey);
  1309. // console.log(cacheCode);
  1310. if (cacheCode === null || code === undefined || cacheCode !== (code + pa.auth_mobile)) {
  1311. throw '验证码不正确!';
  1312. }
  1313. }
  1314. // 获取是否已存在调用变更令
  1315. const changeUsedData = await ctx.service.stageChange.getFinalUsedData(ctx.tender.id, changeData.cid);
  1316. const stageChangeNum = this.ctx.helper.sum(changeUsedData.map(x => { return Math.abs(x.used_qty); }));
  1317. if (stageChangeNum !== 0) {
  1318. throw '该变更令已被调用,无法重新审批';
  1319. }
  1320. // 重新审批
  1321. const result = await ctx.service.change.checkAgain(changeData.cid);
  1322. if (!result) {
  1323. throw '重新审批失败';
  1324. }
  1325. // ctx.redirect('/tender/' + changeData.tid + '/change/' + changeData.cid + '/info');
  1326. ctx.body = {
  1327. err: 0,
  1328. url: ctx.request.header.referer,
  1329. msg: '',
  1330. };
  1331. } catch (err) {
  1332. console.log(err);
  1333. // ctx.redirect(ctx.request.header.referer);
  1334. ctx.body = {
  1335. err: 1,
  1336. // url: ctx.request.header.referer,
  1337. msg: err,
  1338. };
  1339. }
  1340. }
  1341. /**
  1342. * 变更令修订重新上报
  1343. * @param {Object} ctx - egg全局变量
  1344. * @return {void}
  1345. */
  1346. async checkRevise(ctx) {
  1347. try {
  1348. const changeData = await ctx.service.change.getDataByCondition({ cid: ctx.request.body.cid });
  1349. if (!changeData) {
  1350. throw '变更令数据错误';
  1351. }
  1352. if (changeData.status !== audit.flow.status.checked || ctx.session.sessionUser.accountId !== changeData.uid) {
  1353. throw '您无权进行该操作';
  1354. }
  1355. if (ctx.session.sessionUser.loginStatus === 0) {
  1356. const code = ctx.request.body.code;
  1357. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  1358. if (!pa.auth_mobile) {
  1359. throw '未绑定手机号';
  1360. }
  1361. const cacheKey = 'smsCode:' + ctx.session.sessionUser.accountId;
  1362. const cacheCode = await app.redis.get(cacheKey);
  1363. // console.log(cacheCode);
  1364. if (cacheCode === null || code === undefined || cacheCode !== (code + pa.auth_mobile)) {
  1365. throw '验证码不正确!';
  1366. }
  1367. }
  1368. // 获取是否已存在调用变更令
  1369. // const changeUsedData = await ctx.service.stageChange.getFinalUsedData(ctx.tender.id, changeData.cid);
  1370. // const stageChangeNum = this.ctx.helper.sum(changeUsedData.map(x => { return Math.abs(x.used_qty); }));
  1371. // if (stageChangeNum !== 0) {
  1372. // throw '该变更令已被调用,无法重新审批';
  1373. // }
  1374. // 重新审批
  1375. const result = await ctx.service.change.checkRevise(changeData.cid);
  1376. if (!result) {
  1377. throw '修订发起失败';
  1378. }
  1379. // ctx.redirect('/tender/' + changeData.tid + '/change/' + changeData.cid + '/info');
  1380. ctx.body = {
  1381. err: 0,
  1382. url: ctx.request.header.referer,
  1383. msg: '',
  1384. };
  1385. } catch (err) {
  1386. console.log(err);
  1387. // ctx.redirect(ctx.request.header.referer);
  1388. ctx.body = {
  1389. err: 1,
  1390. // url: ctx.request.header.referer,
  1391. msg: err,
  1392. };
  1393. }
  1394. }
  1395. /**
  1396. * 获取变更清单
  1397. * @param ctx
  1398. * @return {Promise<void>}
  1399. */
  1400. async bills(ctx) {
  1401. try {
  1402. const data = JSON.parse(ctx.request.body.data);
  1403. const responseData = { err: 0, msg: '', data: [] };
  1404. switch (data.type) {
  1405. case 'gather':
  1406. responseData.data = await ctx.service.changeAuditList.gatherBgBills(ctx.tender.id);
  1407. break;
  1408. default:
  1409. throw '查询的数据不存在';
  1410. }
  1411. ctx.body = responseData;
  1412. } catch (err) {
  1413. this.log(err);
  1414. this.ajaxErrorBody(err, '获取变更清单失败');
  1415. }
  1416. }
  1417. /**
  1418. * 最后审批人审批成功检查批复编号和其它变更令是否出现重名情况
  1419. * @param ctx
  1420. * @return {Promise<void>}
  1421. */
  1422. async checkCodeRepeat(ctx) {
  1423. const responseData = {
  1424. err: 0,
  1425. msg: '',
  1426. data: '',
  1427. };
  1428. try {
  1429. const tenderId = ctx.params.id;
  1430. const cid = ctx.params.cid;
  1431. const data = JSON.parse(ctx.request.body.data);
  1432. const result = await ctx.service.change.isRepeat(cid, data.p_code, tenderId);
  1433. if (result) {
  1434. throw '该变更令号(批复编号)已使用';
  1435. }
  1436. } catch (err) {
  1437. responseData.err = 1;
  1438. responseData.msg = err;
  1439. }
  1440. ctx.body = responseData;
  1441. }
  1442. /**
  1443. * 拷贝其他变更令
  1444. * @param {object} ctx - 全局上下文
  1445. */
  1446. async copyChange(ctx) {
  1447. const responseData = {
  1448. err: 0,
  1449. msg: '',
  1450. data: '',
  1451. };
  1452. try {
  1453. const cid = ctx.params.cid;
  1454. const copy_cid = JSON.parse(ctx.request.body.data);
  1455. const result = await ctx.service.change.handleCopyChange(cid, copy_cid);
  1456. if (!result) {
  1457. responseData.err = 1;
  1458. responseData.msg = '拷贝失败!';
  1459. }
  1460. } catch (error) {
  1461. responseData.err = 1;
  1462. responseData.msg = error;
  1463. }
  1464. ctx.body = responseData;
  1465. }
  1466. /**
  1467. * 修订 详细页面(Get)
  1468. * @param ctx
  1469. * @return {Promise<void>}
  1470. */
  1471. async reviseInfo(ctx) {
  1472. try {
  1473. if (!ctx.session.sessionProject.page_show.openChangeRevise) {
  1474. throw '该功能已关闭';
  1475. }
  1476. const change = ctx.change;
  1477. let edit = true;
  1478. let changing = false;
  1479. if (change.status !== audit.flow.status.uncheck && change.status !== audit.flow.status.back && change.status !== audit.flow.status.revise) {
  1480. // throw '本条变更审批中或已完成,无法操作台账数据';
  1481. edit = false;
  1482. changing = true;
  1483. }
  1484. let revising = false;
  1485. // 判断是否在修订中,是则无法操作本页
  1486. const lastRevise = await ctx.service.ledgerRevise.getLastestRevise(ctx.tender.id);
  1487. if (lastRevise && lastRevise.status !== audit.revise.status.checked) {
  1488. // throw '台账修订中,无法操作台账数据';
  1489. edit = false;
  1490. revising = true;
  1491. }
  1492. const renderData = await this._getDefaultReviseInfoData(ctx, change, edit);
  1493. // 台账只读、使用数据
  1494. renderData.readOnly = !edit;
  1495. renderData.changing = changing;
  1496. renderData.revising = revising;
  1497. await this.layout('change/revise.ejs', renderData, 'change/revise_modal.ejs');
  1498. } catch (err) {
  1499. this.log(err);
  1500. ctx.redirect(ctx.request.header.referer);
  1501. }
  1502. }
  1503. async _getDefaultReviseInfoData(ctx, change, edit) {
  1504. const [ledgerSpread, posSpread] = this._getSpreadSetting(change, edit);
  1505. const sjsRela = await this.ctx.service.project.getSjsRela(ctx.session.sessionProject.id);
  1506. this.ctx.helper.refreshSpreadShow(sjsRela.ledgerCol, [ledgerSpread, posSpread]);
  1507. const [stdBills, stdChapters] = await this.ctx.service.valuation.getValuationStdList(
  1508. ctx.tender.data.valuation, ctx.tender.data.measure_type);
  1509. return {
  1510. change,
  1511. tender: ctx.tender.data,
  1512. ledgerSpread, posSpread, tenderMenu, measureType,
  1513. preUrl: '/tender/' + ctx.tender.id + '/change/' + ctx.change.cid + '/information',
  1514. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.revise),
  1515. stdBills,
  1516. auditConst: audit.flow,
  1517. audit: audit.flow,
  1518. stdChapters,
  1519. };
  1520. }
  1521. /**
  1522. * 获取SpreadSetting
  1523. * @private
  1524. */
  1525. _getSpreadSetting(change, edit) {
  1526. const _ = this.app._;
  1527. function removeFieldCols(setting, cols) {
  1528. _.remove(setting.cols, function(c) {
  1529. return cols.indexOf(c.field) > -1;
  1530. });
  1531. }
  1532. const tender = this.ctx.tender;
  1533. const setting = tender.data.measure_type === measureType.tz.value
  1534. ? (tender.info.display.ledger.clQty ? spreadConst.withCl : spreadConst.withoutCl)
  1535. : (tender.info.display.ledger.clQty ? spreadConst.withClGcl : spreadConst.withoutClGcl);
  1536. const ledger = JSON.parse(JSON.stringify(setting.ledger));
  1537. const pos = setting.pos ? JSON.parse(JSON.stringify(setting.pos)) : spreadConst.blank;
  1538. // if (change.status === audit.flow.status.checking || change.status === audit.flow.status.checked) {
  1539. // ledger.readOnly = true;
  1540. // pos.readOnly = true;
  1541. // }
  1542. if (!edit) {
  1543. ledger.readOnly = true;
  1544. pos.readOnly = true;
  1545. }
  1546. if (tender.data.measure_type === measureType.tz.value) {
  1547. removeFieldCols(ledger, spreadConst.filterCols.tzWithoutCols);
  1548. }
  1549. if (!tender.info.display.ledger.dgnQty) {
  1550. removeFieldCols(ledger, spreadConst.filterCols.dgnCols);
  1551. }
  1552. return [ledger, pos];
  1553. }
  1554. async updateRevise(ctx) {
  1555. try {
  1556. if (!ctx.tender.data) throw '标段数据错误';
  1557. if (!ctx.session.sessionProject.page_show.openChangeRevise) {
  1558. throw '该功能已关闭';
  1559. }
  1560. const data = JSON.parse(ctx.request.body.data);
  1561. if (!data.postType || !data.postData) throw '数据错误';
  1562. const responseData = { err: 0, msg: '', data: {} };
  1563. const change = ctx.change;
  1564. if (change.status !== audit.flow.status.uncheck && change.status !== audit.flow.status.back && change.status !== audit.flow.status.revise) {
  1565. throw '该变更令正在审批中或已完成,无法操作新增部位数据';
  1566. }
  1567. // 判断是否在修订中,是则无法操作本页
  1568. const lastRevise = await ctx.service.ledgerRevise.getLastestRevise(ctx.tender.id);
  1569. if (lastRevise && lastRevise.status !== audit.revise.status.checked) {
  1570. throw '台账修订中,无法操作新增部位数据';
  1571. }
  1572. // const revise = await this.checkRevise(ctx);
  1573. //
  1574. switch (data.postType) {
  1575. case 'add':
  1576. case 'delete':
  1577. case 'up-move':
  1578. case 'down-move':
  1579. case 'up-level':
  1580. case 'down-level':
  1581. responseData.data = await this._billsBase(ctx, data.postType, data.postData);
  1582. break;
  1583. case 'update':
  1584. ctx.helper.checkDgnQtyPrecision(data.postData);
  1585. responseData.data = await ctx.service.changeLedger.updateCalc(ctx.tender.id, data.postData);
  1586. break;
  1587. case 'paste-block':
  1588. responseData.data = await this._pasteBlock(ctx, data.postData);
  1589. break;
  1590. case 'add-std':
  1591. responseData.data = await this._addStd(ctx, data.postData);
  1592. break;
  1593. case 'add-deal':
  1594. responseData.data = await this._addDeal(ctx, data.postData);
  1595. break;
  1596. // case 'batch-insert':
  1597. // responseData.data = await this._batchInsert(ctx, data.postData);
  1598. // break;
  1599. case 'pos':
  1600. responseData.data = await this._updatePos(ctx, data);
  1601. break;
  1602. default:
  1603. throw '未知操作';
  1604. }
  1605. ctx.body = responseData;
  1606. } catch (err) {
  1607. this.log(err);
  1608. ctx.body = this.ajaxErrorBody(err, '数据错误');
  1609. }
  1610. }
  1611. async _billsBase(ctx, type, data) {
  1612. if (isNaN(data.id) || data.id <= 0) throw '数据错误';
  1613. if (type !== 'add') {
  1614. if (isNaN(data.count) || data.count <= 0) data.count = 1;
  1615. }
  1616. switch (type) {
  1617. case 'add':
  1618. return await ctx.service.changeLedger.addNodeBatch(ctx.tender.id, data.id, { ccid: ctx.change.cid, check_calc: 1 }, data.count);
  1619. case 'delete':
  1620. return await ctx.service.changeLedger.delete(ctx.tender.id, data.id, data.count);
  1621. case 'up-move':
  1622. return await ctx.service.changeLedger.upMoveNode(ctx.tender.id, data.id, data.count);
  1623. case 'down-move':
  1624. return await ctx.service.changeLedger.downMoveNode(ctx.tender.id, data.id, data.count);
  1625. case 'up-level':
  1626. return await ctx.service.changeLedger.upLevelNode(ctx.tender.id, data.id, data.count);
  1627. case 'down-level':
  1628. return await ctx.service.changeLedger.downLevelNode(ctx.tender.id, data.id, data.count);
  1629. default:
  1630. throw '未知操作';
  1631. }
  1632. }
  1633. /**
  1634. * 复制粘贴整块
  1635. *
  1636. * @param ctx
  1637. * @return {Promise<void>}
  1638. */
  1639. async _pasteBlock(ctx, data) {
  1640. if ((isNaN(data.id) || data.id <= 0) ||
  1641. (!data.tid && data.tid <= 0) ||
  1642. (!data.block || data.block.length <= 0)) throw '参数错误';
  1643. return await ctx.service.changeLedger.pasteBlockData(ctx.tender.id, data.id, data.block);
  1644. }
  1645. /**
  1646. * 从标准项目表添加数据
  1647. * @param ctx
  1648. * @return {Promise<void>}
  1649. */
  1650. async _addStd(ctx, data) {
  1651. if ((isNaN(data.id) || data.id <= 0) || !data.stdType || !data.stdNode) throw '参数错误';
  1652. // todo 校验项目是否使用该库的权限
  1653. let stdLib,
  1654. addType;
  1655. switch (data.stdType) {
  1656. case 'xmj':
  1657. stdLib = ctx.service.stdXmj;
  1658. addType = stdDataAddType.withParent;
  1659. break;
  1660. case 'gcl':
  1661. stdLib = ctx.service.stdGcl;
  1662. const selectNode = await ctx.service.changeLedger.getDataByNodeId(ctx.tender.id, data.id) || await ctx.service.ledger.getDataByNodeId(ctx.tender.id, data.id);
  1663. if (selectNode.b_code) {
  1664. addType = stdDataAddType.next;
  1665. } else {
  1666. addType = stdDataAddType.child;
  1667. }
  1668. break;
  1669. default:
  1670. throw '未知标准库';
  1671. }
  1672. const stdData = await stdLib.getDataByDataId(data.stdLibId, data.stdNode);
  1673. switch (addType) {
  1674. case stdDataAddType.child:
  1675. return await ctx.service.changeLedger.addStdNodeAsChild(ctx.tender.id, data.id, stdData, ctx.change.cid);
  1676. case stdDataAddType.next:
  1677. return await ctx.service.changeLedger.addStdNode(ctx.tender.id, data.id, stdData, ctx.change.cid);
  1678. case stdDataAddType.withParent:
  1679. return await ctx.service.changeLedger.addStdNodeWithParent(ctx.tender.id, stdData, stdLib, ctx.change.cid);
  1680. default:
  1681. throw '未知添加方式';
  1682. }
  1683. }
  1684. /**
  1685. * 从签约清单添加节点
  1686. * @param ctx
  1687. * @return {Promise<void>}
  1688. */
  1689. async _addDeal(ctx, data) {
  1690. if (!data.type || !data.dealBills) throw '数据错误';
  1691. data.dealBills.unit_price = this.ctx.helper.round(data.dealBills.unit_price, ctx.tender.info.decimal.up);
  1692. if (data.type === 'child') {
  1693. return await ctx.service.changeLedger.addChild(ctx.tender.id, data.id, data.dealBills, ctx.change.cid);
  1694. } else if (data.type === 'next') {
  1695. data.dealBills.ccid = ctx.change.cid;
  1696. return await ctx.service.changeLedger.addNode(ctx.tender.id, data.id, data.dealBills);
  1697. }
  1698. throw '数据错误';
  1699. }
  1700. /**
  1701. * 批量插入数据
  1702. *
  1703. * data = {id, batchData, batchType}
  1704. * data.batchType = 'batchInsertChild'/'batchInsertNext'
  1705. * data.batchData = [{name, children}] -- 项目节列表
  1706. * data.batchData.children = [{code, name, unit, unit_price, quantity}] -- 工程量清单列表
  1707. *
  1708. * @param ctx
  1709. * @return {Promise<void>}
  1710. */
  1711. async _batchInsert(ctx, data) {
  1712. if ((isNaN(data.id) || data.id <= 0) || !data.batchType) throw '参数错误';
  1713. switch (data.batchType) {
  1714. case 'child':
  1715. return await ctx.service.changeLedger.batchInsertChild(ctx.tender.id, data.id, data.batchData);
  1716. case 'next':
  1717. return await ctx.service.changeLedger.batchInsertNext(ctx.tender.id, data.id, data.batchData);
  1718. default:
  1719. throw '参数错误';
  1720. }
  1721. }
  1722. async _updatePos(ctx, data) {
  1723. await this.checkMeasureType(measureType.tz.value);
  1724. if (!data.posPostType) throw '数据错误';
  1725. switch (data.posPostType) {
  1726. case 'add':
  1727. return await this.ctx.service.changePos.addPos(ctx.tender.id, ctx.change.cid, data.postData);
  1728. case 'update':
  1729. if (data.postData instanceof Array) {
  1730. return await this.ctx.service.changePos.updatePosArr(ctx.tender.id, data.postData);
  1731. }
  1732. return await this.ctx.service.changePos.updatePos(ctx.tender.id, data.postData);
  1733. case 'delete':
  1734. return await this.ctx.service.changePos.deletePos(ctx.tender.id, data.postData);
  1735. case 'paste':
  1736. return await this.ctx.service.changePos.pastePosData(ctx.tender.id, ctx.change.cid, data.postData);
  1737. default:
  1738. throw '未知操作';
  1739. }
  1740. }
  1741. async _filterChangesProject(ctx, status = 0) {
  1742. const tenderId = ctx.params.id;
  1743. ctx.session.sessionUser.tenderId = tenderId;
  1744. const tender = await this.service.tender.getDataById(ctx.tender.id);
  1745. // const tender = ctx.tender;
  1746. // const tenderList = await this.service.tender.getList();
  1747. const page = ctx.page;
  1748. const sorts = ctx.query.sort ? ctx.query.sort : 0;
  1749. const orders = ctx.query.order ? ctx.query.order : 0;
  1750. const changes = await ctx.service.changeProject.getListByStatus(tender.id, status, 1, sorts, orders);
  1751. const total = await ctx.service.changeProject.getCountByStatus(tender.id, status);
  1752. for (const c of changes) {
  1753. c.curAuditor = await ctx.service.changeProjectAudit.getAuditorByStatus(c.id, c.status, c.times);
  1754. }
  1755. const accountInfo = await this.ctx.service.projectAccount.getDataById(this.ctx.session.sessionUser.accountId);
  1756. const userPermission = accountInfo !== undefined && accountInfo.permission !== ''
  1757. ? JSON.parse(accountInfo.permission) : null;
  1758. // 分页相关
  1759. const pageInfo = {
  1760. page,
  1761. total: Math.ceil(total / app.config.pageSize),
  1762. queryData: JSON.stringify(ctx.urlInfo.query),
  1763. };
  1764. const filter = JSON.parse(JSON.stringify(audit.changeProject.filter));
  1765. filter.count = [];
  1766. 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);
  1767. 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);
  1768. 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);
  1769. 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);
  1770. 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);
  1771. let codeRule = [];
  1772. let c_connector = '1';
  1773. let c_rule_first = 1;
  1774. const rule_type = tender.user_id === ctx.session.sessionUser.accountId ? 'suggestion' : 'will';
  1775. if (tender.c_code_rules) {
  1776. const c_code_rules = JSON.parse(tender.c_code_rules);
  1777. codeRule = c_code_rules[rule_type + '_rule'] !== undefined ? c_code_rules[rule_type + '_rule'] : [];
  1778. c_connector = c_code_rules[rule_type + '_connector'] !== undefined ? c_code_rules[rule_type + '_connector'] : '1';
  1779. c_rule_first = c_code_rules[rule_type + '_rule_first'] !== undefined ? c_code_rules[rule_type + '_rule_first'] : 1;
  1780. }
  1781. for (const rule of codeRule) {
  1782. switch (rule.rule_type) {
  1783. case codeRuleConst.measure.ruleType.dealCode:
  1784. rule.preview = ctx.tender.info.deal_info.dealCode;
  1785. break;
  1786. case codeRuleConst.measure.ruleType.tenderName:
  1787. rule.preview = tender.name;
  1788. break;
  1789. case codeRuleConst.measure.ruleType.inDate:
  1790. rule.preview = moment().format('YYYY');
  1791. break;
  1792. case codeRuleConst.measure.ruleType.text:
  1793. rule.preview = rule.text;
  1794. break;
  1795. case codeRuleConst.measure.ruleType.addNo:
  1796. const s = '0000000000';
  1797. rule.preview = s.substr(s.length - rule.format);
  1798. break;
  1799. default: break;
  1800. }
  1801. }
  1802. const renderData = {
  1803. uid: ctx.session.sessionUser.accountId,
  1804. userPermission,
  1805. tender,
  1806. pageInfo,
  1807. changes,
  1808. status,
  1809. rule_type,
  1810. codeRule,
  1811. c_connector,
  1812. c_rule_first,
  1813. filter,
  1814. ruleType: codeRuleConst.ruleType[rule_type],
  1815. dealCode: ctx.tender.info.deal_info.dealCode,
  1816. auditConst: audit.changeProject,
  1817. ruleConst: codeRuleConst.measure,
  1818. changeConst,
  1819. tenderMenu: this.menu.tenderMenu,
  1820. preUrl: '/tender/' + tender.id,
  1821. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.project),
  1822. };
  1823. await this.layout('change/project.ejs', renderData, 'change/project_modal.ejs');
  1824. }
  1825. /**
  1826. * 变更立项列表 页面 (Get)
  1827. *
  1828. * @param {Object} ctx - egg全局变量
  1829. * @return {void}
  1830. */
  1831. async project(ctx) {
  1832. try {
  1833. await this._filterChangesProject(ctx);
  1834. } catch (err) {
  1835. this.log(err);
  1836. ctx.redirect('/dashboard');
  1837. }
  1838. }
  1839. /**
  1840. * 新增变更立项 (Post)
  1841. *
  1842. * @param {Object} ctx - egg全局变量
  1843. * @return {void}
  1844. */
  1845. async projectAdd(ctx) {
  1846. try {
  1847. const tenderId = ctx.params.id;
  1848. if (!tenderId) {
  1849. throw '当前未打开标段';
  1850. }
  1851. const data = JSON.parse(ctx.request.body.data);
  1852. if (!data.code || data.code === '' || !data.name || data.name === '') {
  1853. throw '变更立项书编号不能为空';
  1854. }
  1855. const change = await ctx.service.changeProject.add(tenderId, ctx.session.sessionUser.accountId, data.code, data.name);
  1856. ctx.body = { err: 0, msg: '', data: change };
  1857. } catch (err) {
  1858. this.log(err);
  1859. ctx.body = { err: 1, msg: err.toString() };
  1860. }
  1861. }
  1862. /**
  1863. * 删除变更立项
  1864. * @param {Object} ctx - egg全局变量
  1865. * @return {void}
  1866. */
  1867. async projectDelete(ctx) {
  1868. try {
  1869. const result = await ctx.service.changeProject.delete(ctx.request.body.cpid);
  1870. if (!result) {
  1871. throw '删除变更立项失败';
  1872. }
  1873. ctx.redirect(ctx.request.header.referer);
  1874. } catch (err) {
  1875. console.log(err);
  1876. ctx.redirect(ctx.request.header.referer);
  1877. }
  1878. }
  1879. /**
  1880. * 变更管理 状态筛选 页面 (Get)
  1881. * @param {Object} ctx - egg全局变量
  1882. * @return {void}
  1883. */
  1884. async projectStatus(ctx) {
  1885. try {
  1886. const status = parseInt(ctx.params.status);
  1887. await this._filterChangesProject(ctx, status);
  1888. } catch (err) {
  1889. this.logger.error(err);
  1890. ctx.redirect('/tender/' + ctx.params.id + '/change/project');
  1891. }
  1892. }
  1893. /**
  1894. * 获取审批界面所需的 原报、审批人数据等
  1895. * @param ctx
  1896. * @return {Promise<void>}
  1897. * @private
  1898. */
  1899. async _getChangeProjectAuditViewData(ctx) {
  1900. const auditConst = audit.changeProject;
  1901. const times = ctx.change.status === auditConst.status.back ? ctx.change.times - 1 : ctx.change.times;
  1902. ctx.change.user = await ctx.service.projectAccount.getAccountInfoById(ctx.change.uid);
  1903. ctx.change.auditHistory = [];
  1904. if (times >= 1) {
  1905. for (let i = 1; i <= times; i++) {
  1906. ctx.change.auditHistory.push(await ctx.service.changeProjectAudit.getAuditors(ctx.change.id, i));
  1907. }
  1908. }
  1909. // 获取审批流程中左边列表
  1910. ctx.change.auditors2 = ctx.change.status === auditConst.status.back && ctx.change.uid !== ctx.session.sessionUser.accountId ?
  1911. await ctx.service.changeProjectAudit.getAuditorsWithOwner(ctx.change.id, times) :
  1912. await ctx.service.changeProjectAudit.getAuditorsWithOwner(ctx.change.id, ctx.change.times);
  1913. if (ctx.change.status === auditConst.status.uncheck || ctx.change.status === auditConst.status.back) {
  1914. ctx.change.auditorList = await ctx.service.changeProjectAudit.getAuditors(ctx.change.id, ctx.change.times);
  1915. }
  1916. ctx.change.xsAuditors = await ctx.service.changeProjectXsAudit.getAuditList(ctx.change.id);
  1917. }
  1918. async projectInformation(ctx) {
  1919. try {
  1920. const whiteList = this.ctx.app.config.multipart.whitelist;
  1921. const tender = await ctx.service.tender.getDataById(ctx.tender.id);
  1922. // 获取附件列表
  1923. const fileList = await ctx.service.changeProjectAtt.getAllChangeProjectAtt(ctx.tender.id, ctx.change.id);
  1924. await this._getChangeProjectAuditViewData(ctx);
  1925. const renderData = {
  1926. tender,
  1927. change: ctx.change,
  1928. changeConst,
  1929. auditConst: audit.changeProject,
  1930. fileList,
  1931. whiteList,
  1932. returnUrl: this.app._.includes(ctx.request.headers.referer, '/tender/' + ctx.tender.id + '/change/project') ? ctx.request.headers.referer : null,
  1933. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.project_information),
  1934. preUrl: '/tender/' + ctx.tender.id + '/change/project/' + ctx.change.id + '/information',
  1935. };
  1936. // 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)) {
  1937. // data.accountGroup = accountGroup;
  1938. // 获取所有项目参与者
  1939. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  1940. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  1941. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  1942. });
  1943. renderData.accountList = accountList;
  1944. renderData.accountGroup = accountGroup.map((item, idx) => {
  1945. const groupList = accountList.filter(item => item.account_group === idx);
  1946. return { groupName: item, groupList };
  1947. });
  1948. // }
  1949. await this.layout('change/project_information.ejs', renderData, 'change/project_information_modal.ejs');
  1950. } catch (err) {
  1951. this.log(err);
  1952. ctx.redirect('/tender/' + ctx.params.id + '/change');
  1953. }
  1954. }
  1955. // 审批相关
  1956. /**
  1957. * 添加审批人
  1958. * @param ctx
  1959. * @return {Promise<void>}
  1960. */
  1961. async addProjectAudit(ctx) {
  1962. try {
  1963. const auditConst = audit.changeProject;
  1964. const data = JSON.parse(ctx.request.body.data);
  1965. const id = this.app._.toInteger(data.auditorId);
  1966. if (isNaN(id) || id <= 0) {
  1967. throw '参数错误';
  1968. }
  1969. // 检查权限等
  1970. if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
  1971. throw '您无权添加审核人';
  1972. }
  1973. if (ctx.change.status === auditConst.status.checking || ctx.change.status === auditConst.status.checked) {
  1974. throw '当前不允许添加审核人';
  1975. }
  1976. // 判断是否是协审人,是则无法添加到审批人中
  1977. const xsAuditorList = await ctx.service.changeProjectXsAudit.getAuditList(ctx.change.id);
  1978. const xsAidList = this.app._.map(xsAuditorList, 'aid');
  1979. if (this.app._.indexOf(xsAidList, id) !== -1) {
  1980. throw '该用户已添加到协审人列表中,请删除该协审人再添加';
  1981. }
  1982. ctx.change.auditorList = await ctx.service.changeProjectAudit.getAuditors(ctx.change.id, ctx.change.times);
  1983. // 检查审核人是否已存在
  1984. const exist = this.app._.find(ctx.change.auditorList, { aid: id });
  1985. if (exist) {
  1986. throw '该审核人已存在,请勿重复添加';
  1987. }
  1988. // const shenpiInfo = await ctx.service.shenpiAudit.getDataByCondition({ tid: ctx.tender.id, sp_type: shenpiConst.sp_type.material, sp_status: shenpiConst.sp_status.gdzs });
  1989. // const is_gdzs = shenpiInfo && ctx.tender.info.shenpi.material === shenpiConst.sp_status.gdzs ? 1 : 0;
  1990. const result = await ctx.service.changeProjectAudit.addAuditor(ctx.change.id, id, ctx.change.times);
  1991. if (!result) {
  1992. throw '添加审核人失败';
  1993. }
  1994. const auditors = await ctx.service.changeProjectAudit.getAuditorsWithOwner(ctx.change.id, ctx.change.times);
  1995. ctx.body = { err: 0, msg: '', data: auditors };
  1996. } catch (err) {
  1997. this.log(err);
  1998. ctx.body = { err: 1, msg: err.toString(), data: null };
  1999. }
  2000. }
  2001. /**
  2002. * 移除审批人
  2003. * @param ctx
  2004. * @return {Promise<void>}
  2005. */
  2006. async deleteProjectAudit(ctx) {
  2007. try {
  2008. const data = JSON.parse(ctx.request.body.data);
  2009. const id = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
  2010. if (isNaN(id) || id <= 0) {
  2011. throw '参数错误';
  2012. }
  2013. const result = await ctx.service.changeProjectAudit.deleteAuditor(ctx.change.id, id, ctx.change.times);
  2014. if (!result) {
  2015. throw '移除审核人失败';
  2016. }
  2017. const auditors = await ctx.service.changeProjectAudit.getAuditors(ctx.change.id, ctx.change.times);
  2018. ctx.body = { err: 0, msg: '', data: auditors };
  2019. } catch (err) {
  2020. ctx.body = { err: 1, msg: err.toString(), data: null };
  2021. }
  2022. }
  2023. /**
  2024. * 添加协审人
  2025. * @param ctx
  2026. * @return {Promise<void>}
  2027. */
  2028. async addProjectXsAudit(ctx) {
  2029. try {
  2030. const auditConst = audit.changeProject;
  2031. const data = JSON.parse(ctx.request.body.data);
  2032. const id = this.app._.toInteger(data.auditorId);
  2033. if (isNaN(id) || id <= 0) {
  2034. throw '参数错误';
  2035. }
  2036. const times = ctx.change.status === auditConst.status.back ? ctx.change.times - 1 : ctx.change.times;
  2037. const spAuditors = await ctx.service.changeProjectAudit.getAuditorsWithOwner(ctx.change.id, times);
  2038. const aidLists = this.app._.map(spAuditors, 'aid');
  2039. // 检查权限等
  2040. if (this.app._.indexOf(aidLists, ctx.session.sessionUser.accountId) === -1) {
  2041. throw '您无权添加协审人';
  2042. }
  2043. // 如果已经是审批人则无法添加此人为协审
  2044. if (this.app._.indexOf(aidLists, id) !== -1) {
  2045. throw '该用户为审批人或原报,无需添加至协审人';
  2046. }
  2047. const auditorList = await ctx.service.changeProjectXsAudit.getAuditList(ctx.change.id);
  2048. // 检查审核人是否已存在
  2049. const exist = this.app._.find(auditorList, { aid: id });
  2050. if (exist) {
  2051. throw '该协审人已存在,请勿重复添加';
  2052. }
  2053. const result = await ctx.service.changeProjectXsAudit.addAuditor(ctx.change.id, id);
  2054. if (!result) {
  2055. throw '添加协审人失败';
  2056. }
  2057. const auditors = await ctx.service.changeProjectXsAudit.getOneAudit(ctx.change.id, id);
  2058. ctx.body = { err: 0, msg: '', data: auditors };
  2059. } catch (err) {
  2060. this.log(err);
  2061. ctx.body = { err: 1, msg: err.toString(), data: null };
  2062. }
  2063. }
  2064. /**
  2065. * 移除协审人
  2066. * @param ctx
  2067. * @return {Promise<void>}
  2068. */
  2069. async deleteProjectXsAudit(ctx) {
  2070. try {
  2071. const data = JSON.parse(ctx.request.body.data);
  2072. const id = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
  2073. if (isNaN(id) || id <= 0) {
  2074. throw '参数错误';
  2075. }
  2076. const result = await ctx.service.changeProjectXsAudit.deleteAuditor(ctx.change.id, id);
  2077. if (!result) {
  2078. throw '移除协审人失败';
  2079. }
  2080. const auditors = await ctx.service.changeProjectXsAudit.getAuditList(ctx.change.id);
  2081. ctx.body = { err: 0, msg: '', data: auditors };
  2082. } catch (err) {
  2083. ctx.body = { err: 1, msg: err.toString(), data: null };
  2084. }
  2085. }
  2086. /**
  2087. * 上传附件
  2088. * @param {*} ctx 上下文
  2089. */
  2090. async uploadProjectFile(ctx) {
  2091. let stream;
  2092. try {
  2093. const auditConst = audit.changeProject;
  2094. // this._checkAdvanceFileCanModify(ctx);
  2095. const parts = this.ctx.multipart({
  2096. autoFields: true,
  2097. });
  2098. const files = [];
  2099. const create_time = Date.parse(new Date()) / 1000;
  2100. let idx = 0;
  2101. const extra_upload = ctx.change.status === auditConst.status.checked;
  2102. while ((stream = await parts()) !== undefined) {
  2103. if (!stream.filename) {
  2104. // 如果没有传入直接返回
  2105. return;
  2106. }
  2107. const fileInfo = path.parse(stream.filename);
  2108. const filepath = `app/public/upload/${this.ctx.tender.id.toString()}/change_project/fujian_${create_time + idx.toString() + fileInfo.ext}`;
  2109. // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, 'app', filepath));
  2110. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  2111. files.push({ filepath, name: stream.filename, ext: fileInfo.ext });
  2112. ++idx;
  2113. stream && (await sendToWormhole(stream));
  2114. }
  2115. const in_time = new Date();
  2116. const payload = files.map(file => {
  2117. let idx;
  2118. if (Array.isArray(parts.field.name)) {
  2119. idx = parts.field.name.findIndex(name => name === file.name);
  2120. } else {
  2121. idx = 'isString';
  2122. }
  2123. const newFile = {
  2124. tid: ctx.tender.id,
  2125. cpid: ctx.change.id,
  2126. uid: ctx.session.sessionUser.accountId,
  2127. filename: file.name,
  2128. fileext: file.ext,
  2129. filesize: ctx.helper.bytesToSize(idx === 'isString' ? parts.field.size : parts.field.size[idx]),
  2130. filepath: file.filepath,
  2131. upload_time: in_time,
  2132. extra_upload,
  2133. };
  2134. return newFile;
  2135. });
  2136. // 执行文件信息写入数据库
  2137. await ctx.service.changeProjectAtt.saveFileMsgToDb(payload);
  2138. // 将最新的当前标段的所有文件信息返回
  2139. const data = await ctx.service.changeProjectAtt.getAllChangeProjectAtt(ctx.tender.id, ctx.change.id);
  2140. ctx.body = { err: 0, msg: '', data };
  2141. } catch (err) {
  2142. stream && (await sendToWormhole(stream));
  2143. this.log(err);
  2144. ctx.body = { err: 1, msg: err.toString(), data: null };
  2145. }
  2146. }
  2147. /**
  2148. * 删除附件
  2149. * @param {Ojbect} ctx 上下文
  2150. */
  2151. async deleteProjectFile(ctx) {
  2152. try {
  2153. const { id } = JSON.parse(ctx.request.body.data);
  2154. const fileInfo = await ctx.service.changeProjectAtt.getDataById(id);
  2155. if (fileInfo || Object.keys(fileInfo).length) {
  2156. // 先删除文件
  2157. // await fs.unlinkSync(path.resolve(this.app.baseDir, './app', fileInfo.filepath));
  2158. await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
  2159. // 再删除数据库
  2160. await ctx.service.changeProjectAtt.delete(id);
  2161. } else {
  2162. throw '不存在该文件';
  2163. }
  2164. const data = await ctx.service.changeProjectAtt.getAllChangeProjectAtt(ctx.tender.id, ctx.change.id);
  2165. ctx.body = { err: 0, msg: '请求成功', data };
  2166. } catch (err) {
  2167. this.log(err);
  2168. ctx.body = { err: 1, msg: err.toString(), data: null };
  2169. }
  2170. }
  2171. /**
  2172. * 下载附件
  2173. * @param {Object} ctx - egg全局变量
  2174. * @return {void}
  2175. */
  2176. async downloadProjectFile(ctx) {
  2177. const id = ctx.params.fid;
  2178. if (id) {
  2179. try {
  2180. const fileInfo = await ctx.service.changeProjectAtt.getDataById(id);
  2181. if (fileInfo !== undefined && fileInfo !== '') {
  2182. // const fileName = path.join(__dirname, '../', fileInfo.filepath);
  2183. // 解决中文无法下载问题
  2184. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  2185. let disposition = '';
  2186. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  2187. disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename);
  2188. } else if (userAgent.indexOf('firefox') >= 0) {
  2189. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename) + '"';
  2190. } else {
  2191. /* safari等其他非主流浏览器只能自求多福了 */
  2192. disposition = 'attachment; filename=' + new Buffer(fileInfo.filename).toString('binary');
  2193. }
  2194. ctx.response.set({
  2195. 'Content-Type': 'application/octet-stream',
  2196. 'Content-Disposition': disposition,
  2197. 'Content-Length': fileInfo.filesize,
  2198. });
  2199. // ctx.body = await fs.createReadStream(fileName);
  2200. ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
  2201. } else {
  2202. throw '不存在该文件';
  2203. }
  2204. } catch (err) {
  2205. this.log(err);
  2206. this.setMessage(err.toString(), this.messageType.ERROR);
  2207. }
  2208. }
  2209. }
  2210. async projectInformationSave(ctx) {
  2211. try {
  2212. const data = JSON.parse(ctx.request.body.data);
  2213. if (data.name === 'code') {
  2214. const info = await ctx.service.changeProject.isRepeat(ctx.change.id, data.val, ctx.tender.id, ctx.change.type);
  2215. if (info) {
  2216. throw '该编号已存在';
  2217. }
  2218. }
  2219. const result = await ctx.service.changeProject.saveInfo(ctx.change.id, data);
  2220. if (!result) {
  2221. throw '修改失败';
  2222. }
  2223. ctx.body = { err: 0, msg: '请求成功', data: null };
  2224. } catch (err) {
  2225. this.log(err);
  2226. ctx.body = { err: 1, msg: err.toString(), data: null };
  2227. }
  2228. }
  2229. /**
  2230. * 上报和重新上报
  2231. * @param ctx
  2232. * @return {Promise<void>}
  2233. */
  2234. async startProjectAudit(ctx) {
  2235. try {
  2236. const auditConst = audit.changeProject;
  2237. // 检查权限等
  2238. if (!ctx.change) {
  2239. throw '数据错误';
  2240. }
  2241. if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
  2242. throw '您无权上报该期数据';
  2243. }
  2244. if (ctx.change.status === auditConst.status.checking || ctx.change.status === auditConst.status.checked) {
  2245. throw '该材料调差期数据当前无法上报';
  2246. }
  2247. await ctx.service.changeProjectAudit.start(ctx.change.id, ctx.change.times);
  2248. ctx.redirect(ctx.request.header.referer);
  2249. } catch (err) {
  2250. this.log(err);
  2251. ctx.session.postError = err.toString();
  2252. ctx.redirect(ctx.request.header.referer);
  2253. }
  2254. }
  2255. /**
  2256. * 审批
  2257. * @param ctx
  2258. * @return {Promise<void>}
  2259. */
  2260. async checkProjectAudit(ctx) {
  2261. try {
  2262. const auditConst = audit.changeProject;
  2263. if (!ctx.change || ctx.change.status !== auditConst.status.checking) {
  2264. throw '当前材料调差期数据有误';
  2265. }
  2266. if (!ctx.change.curAuditor || ctx.change.curAuditor.aid !== ctx.session.sessionUser.accountId) {
  2267. throw '您无权进行该操作';
  2268. }
  2269. const data = {
  2270. checkType: parseInt(ctx.request.body.checkType),
  2271. opinion: ctx.request.body.opinion,
  2272. };
  2273. if (!data.checkType || isNaN(data.checkType)) {
  2274. throw '提交数据错误';
  2275. }
  2276. // if (data.checkType === auditConst.status.checkNo) {
  2277. // if (!data.checkType || isNaN(data.checkType)) {
  2278. // throw '提交数据错误';
  2279. // }
  2280. // }
  2281. await ctx.service.changeProjectAudit.check(ctx.change.id, data, ctx.change.times);
  2282. ctx.redirect(ctx.request.header.referer);
  2283. } catch (err) {
  2284. this.log(err);
  2285. ctx.session.postError = err.toString();
  2286. ctx.redirect(ctx.request.header.referer);
  2287. }
  2288. }
  2289. async _filterChangesApply(ctx, status = 0) {
  2290. const tenderId = ctx.params.id;
  2291. ctx.session.sessionUser.tenderId = tenderId;
  2292. const tender = await this.service.tender.getDataById(ctx.tender.id);
  2293. // const tender = ctx.tender;
  2294. // const tenderList = await this.service.tender.getList();
  2295. const page = ctx.page;
  2296. const sorts = ctx.query.sort ? ctx.query.sort : 0;
  2297. const orders = ctx.query.order ? ctx.query.order : 0;
  2298. const changes = await ctx.service.changeApply.getListByStatus(tender.id, status, 1, sorts, orders);
  2299. const total = await ctx.service.changeApply.getCountByStatus(tender.id, status);
  2300. let page_total = 0;
  2301. const tp = await ctx.service.changeApply.getTp(tender.id, status);
  2302. for (const c of changes) {
  2303. c.curAuditor = await ctx.service.changeApplyAudit.getAuditorByStatus(c.id, c.status, c.times);
  2304. page_total = ctx.helper.add(page_total, c.total_price);
  2305. }
  2306. const tender_userInfo = await ctx.service.projectAccount.getDataById(ctx.tender.data.user_id);
  2307. // 分页相关
  2308. const pageInfo = {
  2309. page,
  2310. total: Math.ceil(total / app.config.pageSize),
  2311. queryData: JSON.stringify(ctx.urlInfo.query),
  2312. };
  2313. const filter = JSON.parse(JSON.stringify(audit.changeApply.filter));
  2314. filter.count = [];
  2315. 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);
  2316. 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);
  2317. 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);
  2318. 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);
  2319. // 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);
  2320. let codeRule = [];
  2321. let c_connector = '1';
  2322. let c_rule_first = 1;
  2323. const rule_type = 'apply';
  2324. if (tender.c_code_rules) {
  2325. const c_code_rules = JSON.parse(tender.c_code_rules);
  2326. codeRule = c_code_rules[rule_type + '_rule'] !== undefined ? c_code_rules[rule_type + '_rule'] : [];
  2327. c_connector = c_code_rules[rule_type + '_connector'] !== undefined ? c_code_rules[rule_type + '_connector'] : '1';
  2328. c_rule_first = c_code_rules[rule_type + '_rule_first'] !== undefined ? c_code_rules[rule_type + '_rule_first'] : 1;
  2329. }
  2330. for (const rule of codeRule) {
  2331. switch (rule.rule_type) {
  2332. case codeRuleConst.measure.ruleType.dealCode:
  2333. rule.preview = ctx.tender.info.deal_info.dealCode;
  2334. break;
  2335. case codeRuleConst.measure.ruleType.tenderName:
  2336. rule.preview = tender.name;
  2337. break;
  2338. case codeRuleConst.measure.ruleType.inDate:
  2339. rule.preview = moment().format('YYYY');
  2340. break;
  2341. case codeRuleConst.measure.ruleType.text:
  2342. rule.preview = rule.text;
  2343. break;
  2344. case codeRuleConst.measure.ruleType.addNo:
  2345. const s = '0000000000';
  2346. rule.preview = s.substr(s.length - rule.format);
  2347. break;
  2348. default: break;
  2349. }
  2350. }
  2351. const changeProjectList = await ctx.service.changeProject.getAllDataByCondition({ where: { tid: tender.id, status: audit.changeProject.status.checked } });
  2352. const allProjectCodes = await ctx.service.changeApply.getAllDataByCondition({
  2353. columns: ['project_code'],
  2354. where: {
  2355. tid: tender.id,
  2356. },
  2357. });
  2358. const pcLists = allProjectCodes.length > 0 ? ctx.app._.uniq(ctx.app._.map(allProjectCodes, 'project_code')) : [];
  2359. const renderData = {
  2360. uid: ctx.session.sessionUser.accountId,
  2361. tender,
  2362. pageInfo,
  2363. changes,
  2364. status,
  2365. rule_type,
  2366. codeRule,
  2367. c_connector,
  2368. c_rule_first,
  2369. filter,
  2370. apply_username: tender_userInfo ? tender_userInfo.name : null,
  2371. ruleType: codeRuleConst.ruleType[rule_type],
  2372. dealCode: ctx.tender.info.deal_info.dealCode,
  2373. auditConst: audit.changeApply,
  2374. ruleConst: codeRuleConst.measure,
  2375. changeConst,
  2376. changeProjectList,
  2377. pcLists,
  2378. tp,
  2379. page_total,
  2380. tenderMenu: this.menu.tenderMenu,
  2381. preUrl: '/tender/' + tender.id,
  2382. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.apply),
  2383. };
  2384. await this.layout('change/apply.ejs', renderData, 'change/apply_modal.ejs');
  2385. }
  2386. /**
  2387. * 变更申请列表 页面 (Get)
  2388. *
  2389. * @param {Object} ctx - egg全局变量
  2390. * @return {void}
  2391. */
  2392. async apply(ctx) {
  2393. try {
  2394. await this._filterChangesApply(ctx);
  2395. } catch (err) {
  2396. this.log(err);
  2397. ctx.redirect('/dashboard');
  2398. }
  2399. }
  2400. /**
  2401. * 变更管理 状态筛选 页面 (Get)
  2402. * @param {Object} ctx - egg全局变量
  2403. * @return {void}
  2404. */
  2405. async applyStatus(ctx) {
  2406. try {
  2407. const status = parseInt(ctx.params.status);
  2408. await this._filterChangesApply(ctx, status);
  2409. } catch (err) {
  2410. this.logger.error(err);
  2411. ctx.redirect('/tender/' + ctx.params.id + '/change/apply');
  2412. }
  2413. }
  2414. /**
  2415. * 新增变更立项 (Post)
  2416. *
  2417. * @param {Object} ctx - egg全局变量
  2418. * @return {void}
  2419. */
  2420. async applyAdd(ctx) {
  2421. try {
  2422. const tenderId = ctx.params.id;
  2423. if (!tenderId) {
  2424. throw '当前未打开标段';
  2425. }
  2426. const data = JSON.parse(ctx.request.body.data);
  2427. if (!data.code || data.code === '') {
  2428. throw '变更申请编号不能为空';
  2429. }
  2430. const change = await ctx.service.changeApply.add(tenderId, ctx.session.sessionUser.accountId, data.code, data.project_code, data.name);
  2431. ctx.body = { err: 0, msg: '', data: change };
  2432. } catch (err) {
  2433. this.log(err);
  2434. ctx.body = { err: 1, msg: err.toString() };
  2435. }
  2436. }
  2437. /**
  2438. * 删除变更立项
  2439. * @param {Object} ctx - egg全局变量
  2440. * @return {void}
  2441. */
  2442. async applyDelete(ctx) {
  2443. try {
  2444. const result = await ctx.service.changeApply.delete(ctx.request.body.caid);
  2445. if (!result) {
  2446. throw '删除变更立项失败';
  2447. }
  2448. ctx.redirect(ctx.request.header.referer);
  2449. } catch (err) {
  2450. console.log(err);
  2451. ctx.redirect(ctx.request.header.referer);
  2452. }
  2453. }
  2454. /**
  2455. * 获取审批界面所需的 原报、审批人数据等
  2456. * @param ctx
  2457. * @return {Promise<void>}
  2458. * @private
  2459. */
  2460. async _getChangeApplyAuditViewData(ctx) {
  2461. const auditConst = audit.changeApply;
  2462. const times = ctx.change.status === auditConst.status.checkNo ? ctx.change.times - 1 : ctx.change.times;
  2463. ctx.change.user = await ctx.service.projectAccount.getAccountInfoById(ctx.change.uid);
  2464. ctx.change.auditHistory = [];
  2465. if (times >= 1) {
  2466. for (let i = 1; i <= times; i++) {
  2467. ctx.change.auditHistory.push(await ctx.service.changeApplyAudit.getAuditors(ctx.change.id, i));
  2468. }
  2469. }
  2470. // 获取审批流程中左边列表
  2471. ctx.change.auditors2 = ctx.change.status === auditConst.status.checkNo && ctx.change.uid !== ctx.session.sessionUser.accountId ?
  2472. await ctx.service.changeApplyAudit.getAuditorsWithOwner(ctx.change.id, times) :
  2473. await ctx.service.changeApplyAudit.getAuditorsWithOwner(ctx.change.id, ctx.change.times);
  2474. if (ctx.change.status === auditConst.status.uncheck || ctx.change.status === auditConst.status.checkNo) {
  2475. ctx.change.auditorList = await ctx.service.changeApplyAudit.getAuditors(ctx.change.id, ctx.change.times);
  2476. }
  2477. }
  2478. async applyInformation(ctx) {
  2479. try {
  2480. const whiteList = this.ctx.app.config.multipart.whitelist;
  2481. const tender = await ctx.service.tender.getDataById(ctx.tender.id);
  2482. await this._getChangeApplyAuditViewData(ctx);
  2483. // 获取清单列表
  2484. const changeList = await ctx.service.changeApplyList.getList(ctx.change.id);
  2485. // 获取附件列表
  2486. const fileList = await ctx.service.changeApplyAtt.getAllChangeApplyAtt(ctx.tender.id, ctx.change.id);
  2487. const renderData = {
  2488. tender,
  2489. change: ctx.change,
  2490. listRule: ctx.change.list_rule ? JSON.parse(ctx.change.list_rule) : { source: 1, rule: ['unit', 'unit_price'] },
  2491. changeList,
  2492. changeConst,
  2493. auditConst: audit.changeApply,
  2494. fileList,
  2495. whiteList,
  2496. tpUnit: ctx.tender.info.decimal.tp,
  2497. upUnit: ctx.tender.info.decimal.up,
  2498. changeUnits: changeConst.units,
  2499. precision: ctx.tender.info.precision,
  2500. 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,
  2501. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.apply_information),
  2502. preUrl: '/tender/' + ctx.tender.id + '/change/apply/' + ctx.change.id + '/information',
  2503. };
  2504. if ((ctx.change.status === audit.changeApply.status.uncheck || ctx.change.status === audit.changeApply.status.checkNo) && (ctx.session.sessionUser.accountId === ctx.change.uid || ctx.tender.isTourist)) {
  2505. // data.accountGroup = accountGroup;
  2506. // 获取所有项目参与者
  2507. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  2508. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  2509. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  2510. });
  2511. renderData.accountList = accountList;
  2512. renderData.accountGroup = accountGroup.map((item, idx) => {
  2513. const groupList = accountList.filter(item => item.account_group === idx);
  2514. return { groupName: item, groupList };
  2515. });
  2516. }
  2517. await this.layout('change/apply_information.ejs', renderData, 'change/apply_information_modal.ejs');
  2518. } catch (err) {
  2519. this.log(err);
  2520. ctx.redirect('/tender/' + ctx.params.id + '/change');
  2521. }
  2522. }
  2523. async applyInformationNotice(ctx) {
  2524. try {
  2525. const tender = await ctx.service.tender.getDataById(ctx.tender.id);
  2526. // 获取附件列表
  2527. const fileList = await ctx.service.changeApplyAtt.getAllChangeApplyAtt(ctx.tender.id, ctx.change.id);
  2528. await this._getChangeApplyAuditViewData(ctx);
  2529. const renderData = {
  2530. tender,
  2531. change: ctx.change,
  2532. changeConst,
  2533. auditConst: audit.changeApply,
  2534. fileList,
  2535. returnUrl: this.app._.includes(ctx.request.headers.referer, '/tender/' + ctx.tender.id + '/change/apply') ? ctx.request.headers.referer : null,
  2536. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.apply_information_notice),
  2537. preUrl: '/tender/' + ctx.tender.id + '/change/apply/' + ctx.change.id + '/information/notice',
  2538. };
  2539. await this.layout('change/apply_information_notice.ejs', renderData, 'change/apply_information_notice_modal.ejs');
  2540. } catch (err) {
  2541. this.log(err);
  2542. ctx.redirect('/tender/' + ctx.params.id + '/change');
  2543. }
  2544. }
  2545. // 审批相关
  2546. /**
  2547. * 添加审批人
  2548. * @param ctx
  2549. * @return {Promise<void>}
  2550. */
  2551. async addApplyAudit(ctx) {
  2552. try {
  2553. const auditConst = audit.changeApply;
  2554. const data = JSON.parse(ctx.request.body.data);
  2555. const id = this.app._.toInteger(data.auditorId);
  2556. if (isNaN(id) || id <= 0) {
  2557. throw '参数错误';
  2558. }
  2559. // 检查权限等
  2560. if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
  2561. throw '您无权添加审核人';
  2562. }
  2563. if (ctx.change.status === auditConst.status.checking || ctx.change.status === auditConst.status.checked) {
  2564. throw '当前不允许添加审核人';
  2565. }
  2566. ctx.change.auditorList = await ctx.service.changeApplyAudit.getAuditors(ctx.change.id, ctx.change.times);
  2567. // 检查审核人是否已存在
  2568. const exist = this.app._.find(ctx.change.auditorList, { aid: id });
  2569. if (exist) {
  2570. throw '该审核人已存在,请勿重复添加';
  2571. }
  2572. // const shenpiInfo = await ctx.service.shenpiAudit.getDataByCondition({ tid: ctx.tender.id, sp_type: shenpiConst.sp_type.material, sp_status: shenpiConst.sp_status.gdzs });
  2573. // const is_gdzs = shenpiInfo && ctx.tender.info.shenpi.material === shenpiConst.sp_status.gdzs ? 1 : 0;
  2574. const result = await ctx.service.changeApplyAudit.addAuditor(ctx.change.id, id, ctx.change.times);
  2575. if (!result) {
  2576. throw '添加审核人失败';
  2577. }
  2578. const auditors = await ctx.service.changeApplyAudit.getAuditorsWithOwner(ctx.change.id, ctx.change.times);
  2579. ctx.body = { err: 0, msg: '', data: auditors };
  2580. } catch (err) {
  2581. this.log(err);
  2582. ctx.body = { err: 1, msg: err.toString(), data: null };
  2583. }
  2584. }
  2585. /**
  2586. * 移除审批人
  2587. * @param ctx
  2588. * @return {Promise<void>}
  2589. */
  2590. async deleteApplyAudit(ctx) {
  2591. try {
  2592. const data = JSON.parse(ctx.request.body.data);
  2593. const id = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
  2594. if (isNaN(id) || id <= 0) {
  2595. throw '参数错误';
  2596. }
  2597. const result = await ctx.service.changeApplyAudit.deleteAuditor(ctx.change.id, id, ctx.change.times);
  2598. if (!result) {
  2599. throw '移除审核人失败';
  2600. }
  2601. const auditors = await ctx.service.changeApplyAudit.getAuditors(ctx.change.id, ctx.change.times);
  2602. ctx.body = { err: 0, msg: '', data: auditors };
  2603. } catch (err) {
  2604. ctx.body = { err: 1, msg: err.toString(), data: null };
  2605. }
  2606. }
  2607. /**
  2608. * 上传附件
  2609. * @param {*} ctx 上下文
  2610. */
  2611. async uploadApplyFile(ctx) {
  2612. let stream;
  2613. try {
  2614. const auditConst = audit.changeApply;
  2615. // this._checkAdvanceFileCanModify(ctx);
  2616. const parts = this.ctx.multipart({
  2617. autoFields: true,
  2618. });
  2619. const files = [];
  2620. const create_time = Date.parse(new Date()) / 1000;
  2621. let idx = 0;
  2622. const extra_upload = ctx.change.status === auditConst.status.checked;
  2623. while ((stream = await parts()) !== undefined) {
  2624. if (!stream.filename) {
  2625. // 如果没有传入直接返回
  2626. return;
  2627. }
  2628. const fileInfo = path.parse(stream.filename);
  2629. const filepath = `app/public/upload/${this.ctx.tender.id.toString()}/change_apply/fujian_${create_time + idx.toString() + fileInfo.ext}`;
  2630. // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, 'app', filepath));
  2631. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  2632. files.push({ filepath, name: stream.filename, ext: fileInfo.ext });
  2633. ++idx;
  2634. stream && (await sendToWormhole(stream));
  2635. }
  2636. const in_time = new Date();
  2637. const payload = files.map(file => {
  2638. let idx;
  2639. if (Array.isArray(parts.field.name)) {
  2640. idx = parts.field.name.findIndex(name => name === file.name);
  2641. } else {
  2642. idx = 'isString';
  2643. }
  2644. const newFile = {
  2645. tid: ctx.tender.id,
  2646. caid: ctx.change.id,
  2647. uid: ctx.session.sessionUser.accountId,
  2648. type: parts.field.type,
  2649. filename: file.name,
  2650. fileext: file.ext,
  2651. filesize: ctx.helper.bytesToSize(idx === 'isString' ? parts.field.size : parts.field.size[idx]),
  2652. filepath: file.filepath,
  2653. upload_time: in_time,
  2654. extra_upload,
  2655. };
  2656. return newFile;
  2657. });
  2658. // 执行文件信息写入数据库
  2659. await ctx.service.changeApplyAtt.saveFileMsgToDb(payload);
  2660. // 将最新的当前标段的所有文件信息返回
  2661. const data = await ctx.service.changeApplyAtt.getAllChangeApplyAtt(ctx.tender.id, ctx.change.id);
  2662. ctx.body = { err: 0, msg: '', data };
  2663. } catch (err) {
  2664. stream && (await sendToWormhole(stream));
  2665. this.log(err);
  2666. ctx.body = { err: 1, msg: err.toString(), data: null };
  2667. }
  2668. }
  2669. /**
  2670. * 删除附件
  2671. * @param {Ojbect} ctx 上下文
  2672. */
  2673. async deleteApplyFile(ctx) {
  2674. try {
  2675. const { id } = JSON.parse(ctx.request.body.data);
  2676. const fileInfo = await ctx.service.changeApplyAtt.getDataById(id);
  2677. if (fileInfo || Object.keys(fileInfo).length) {
  2678. // 先删除文件
  2679. // await fs.unlinkSync(path.resolve(this.app.baseDir, './app', fileInfo.filepath));
  2680. await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
  2681. // 再删除数据库
  2682. await ctx.service.changeApplyAtt.delete(id);
  2683. } else {
  2684. throw '不存在该文件';
  2685. }
  2686. const data = await ctx.service.changeApplyAtt.getAllChangeApplyAtt(ctx.tender.id, ctx.change.id);
  2687. ctx.body = { err: 0, msg: '请求成功', data };
  2688. } catch (err) {
  2689. this.log(err);
  2690. ctx.body = { err: 1, msg: err.toString(), data: null };
  2691. }
  2692. }
  2693. /**
  2694. * 下载附件
  2695. * @param {Object} ctx - egg全局变量
  2696. * @return {void}
  2697. */
  2698. async downloadApplyFile(ctx) {
  2699. const id = ctx.params.fid;
  2700. if (id) {
  2701. try {
  2702. const fileInfo = await ctx.service.changeApplyAtt.getDataById(id);
  2703. if (fileInfo !== undefined && fileInfo !== '') {
  2704. // const fileName = path.join(__dirname, '../', fileInfo.filepath);
  2705. // 解决中文无法下载问题
  2706. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  2707. let disposition = '';
  2708. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  2709. disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename);
  2710. } else if (userAgent.indexOf('firefox') >= 0) {
  2711. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename) + '"';
  2712. } else {
  2713. /* safari等其他非主流浏览器只能自求多福了 */
  2714. disposition = 'attachment; filename=' + new Buffer(fileInfo.filename).toString('binary');
  2715. }
  2716. ctx.response.set({
  2717. 'Content-Type': 'application/octet-stream',
  2718. 'Content-Disposition': disposition,
  2719. 'Content-Length': fileInfo.filesize,
  2720. });
  2721. // ctx.body = await fs.createReadStream(fileName);
  2722. ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
  2723. } else {
  2724. throw '不存在该文件';
  2725. }
  2726. } catch (err) {
  2727. this.log(err);
  2728. this.setMessage(err.toString(), this.messageType.ERROR);
  2729. }
  2730. }
  2731. }
  2732. async applyInformationSave(ctx) {
  2733. try {
  2734. const data = JSON.parse(ctx.request.body.data);
  2735. if (data.name === 'code') {
  2736. const info = await ctx.service.changeApply.isRepeat(ctx.change.id, data.val, ctx.tender.id);
  2737. if (info) {
  2738. throw '该编号已存在';
  2739. }
  2740. }
  2741. const result = await ctx.service.changeApply.saveInfo(ctx.change.id, data);
  2742. if (!result) {
  2743. throw '修改失败';
  2744. }
  2745. ctx.body = { err: 0, msg: '请求成功', data: null };
  2746. } catch (err) {
  2747. this.log(err);
  2748. ctx.body = { err: 1, msg: err.toString(), data: null };
  2749. }
  2750. }
  2751. /**
  2752. * 上报和重新上报
  2753. * @param ctx
  2754. * @return {Promise<void>}
  2755. */
  2756. async startApplyAudit(ctx) {
  2757. try {
  2758. const auditConst = audit.changeApply;
  2759. // 检查权限等
  2760. if (!ctx.change) {
  2761. throw '数据错误';
  2762. }
  2763. if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
  2764. throw '您无权上报该期数据';
  2765. }
  2766. if (ctx.change.status === auditConst.status.checking || ctx.change.status === auditConst.status.checked) {
  2767. throw '该材料调差期数据当前无法上报';
  2768. }
  2769. await ctx.service.changeApplyAudit.start(ctx.change.id, ctx.change.times);
  2770. ctx.redirect(ctx.request.header.referer);
  2771. } catch (err) {
  2772. this.log(err);
  2773. ctx.session.postError = err.toString();
  2774. ctx.redirect(ctx.request.header.referer);
  2775. }
  2776. }
  2777. /**
  2778. * 审批
  2779. * @param ctx
  2780. * @return {Promise<void>}
  2781. */
  2782. async checkApplyAudit(ctx) {
  2783. try {
  2784. const auditConst = audit.changeApply;
  2785. if (!ctx.change || ctx.change.status !== auditConst.status.checking) {
  2786. throw '当前材料调差期数据有误';
  2787. }
  2788. if (!ctx.change.curAuditor || ctx.change.curAuditor.aid !== ctx.session.sessionUser.accountId) {
  2789. throw '您无权进行该操作';
  2790. }
  2791. const data = {
  2792. checkType: parseInt(ctx.request.body.checkType),
  2793. opinion: ctx.request.body.opinion,
  2794. };
  2795. if (!data.checkType || isNaN(data.checkType)) {
  2796. throw '提交数据错误';
  2797. }
  2798. if (ctx.request.body.notice_uid) {
  2799. data.notice_code = ctx.request.body.notice_code;
  2800. data.notice_uid = ctx.request.body.notice_uid;
  2801. }
  2802. // if (data.checkType === auditConst.status.checkNo) {
  2803. // if (!data.checkType || isNaN(data.checkType)) {
  2804. // throw '提交数据错误';
  2805. // }
  2806. // }
  2807. await ctx.service.changeApplyAudit.check(ctx.change.id, data, ctx.change.times);
  2808. ctx.redirect(ctx.request.header.referer);
  2809. } catch (err) {
  2810. this.log(err);
  2811. ctx.session.postError = err.toString();
  2812. ctx.redirect(ctx.request.header.referer);
  2813. }
  2814. }
  2815. /**
  2816. * 变更清单 - 操作 (Ajax)
  2817. * @param ctx
  2818. * @return {Promise<void>}
  2819. */
  2820. async saveApplyListsData(ctx) {
  2821. try {
  2822. const data = JSON.parse(ctx.request.body.data);
  2823. const responseData = {
  2824. err: 0,
  2825. msg: '',
  2826. data: {},
  2827. };
  2828. switch (data.type) {
  2829. case 'add':
  2830. responseData.data = await ctx.service.changeApplyList.add(data.updateData);
  2831. break;
  2832. case 'batchadd':
  2833. responseData.data = await ctx.service.changeApplyList.batchAdd(data);
  2834. break;
  2835. case 'del':
  2836. await ctx.service.changeApplyList.del(data.ids);
  2837. // 取所有工料表
  2838. responseData.data = await ctx.service.changeApplyList.getList(ctx.change.id);
  2839. break;
  2840. case 'update':
  2841. // if (data.updateData.code === '' || data.updateData.code === null) {
  2842. // throw '请先输入编号';
  2843. // }
  2844. await ctx.service.changeApplyList.save(data.updateData);
  2845. break;
  2846. case 'paste':
  2847. await ctx.service.changeApplyList.saveDatas(data.updateData);
  2848. // 取所有工料表
  2849. responseData.data = await ctx.service.changeApplyList.getList(ctx.change.id);
  2850. break;
  2851. case 'list_rule':
  2852. const result = await ctx.service.changeApply.saveInfo(ctx.change.id, { name: 'list_rule', val: data.postData });
  2853. if (!result) {
  2854. throw '修改失败';
  2855. }
  2856. break;
  2857. default: throw '参数有误';
  2858. }
  2859. ctx.body = responseData;
  2860. } catch (err) {
  2861. this.log(err);
  2862. ctx.body = { err: 1, msg: err.toString(), data: null };
  2863. }
  2864. }
  2865. // 变更方案
  2866. async _filterChangesPlan(ctx, status = 0) {
  2867. const tenderId = ctx.params.id;
  2868. ctx.session.sessionUser.tenderId = tenderId;
  2869. const tender = await this.service.tender.getDataById(ctx.tender.id);
  2870. // const tender = ctx.tender;
  2871. // const tenderList = await this.service.tender.getList();
  2872. const page = ctx.page;
  2873. const sorts = ctx.query.sort ? ctx.query.sort : 0;
  2874. const orders = ctx.query.order ? ctx.query.order : 0;
  2875. const changes = await ctx.service.changePlan.getListByStatus(tender.id, status, 1, sorts, orders);
  2876. const total = await ctx.service.changePlan.getCountByStatus(tender.id, status);
  2877. let page_total = 0;
  2878. const tp = await ctx.service.changePlan.getTp(tender.id, status);
  2879. for (const c of changes) {
  2880. c.curAuditor = await ctx.service.changePlanAudit.getAuditorByStatus(c.id, c.status, c.times);
  2881. page_total = ctx.helper.add(page_total, c.total_price);
  2882. }
  2883. const tender_userInfo = await ctx.service.projectAccount.getDataById(ctx.tender.data.user_id);
  2884. // 分页相关
  2885. const pageInfo = {
  2886. page,
  2887. total: Math.ceil(total / app.config.pageSize),
  2888. queryData: JSON.stringify(ctx.urlInfo.query),
  2889. };
  2890. const filter = JSON.parse(JSON.stringify(audit.changeApply.filter));
  2891. filter.count = [];
  2892. 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);
  2893. 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);
  2894. 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);
  2895. 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);
  2896. // 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);
  2897. let codeRule = [];
  2898. let c_connector = '1';
  2899. let c_rule_first = 1;
  2900. const rule_type = 'plan';
  2901. if (tender.c_code_rules) {
  2902. const c_code_rules = JSON.parse(tender.c_code_rules);
  2903. codeRule = c_code_rules[rule_type + '_rule'] !== undefined ? c_code_rules[rule_type + '_rule'] : [];
  2904. c_connector = c_code_rules[rule_type + '_connector'] !== undefined ? c_code_rules[rule_type + '_connector'] : '1';
  2905. c_rule_first = c_code_rules[rule_type + '_rule_first'] !== undefined ? c_code_rules[rule_type + '_rule_first'] : 1;
  2906. }
  2907. for (const rule of codeRule) {
  2908. switch (rule.rule_type) {
  2909. case codeRuleConst.measure.ruleType.dealCode:
  2910. rule.preview = ctx.tender.info.deal_info.dealCode;
  2911. break;
  2912. case codeRuleConst.measure.ruleType.tenderName:
  2913. rule.preview = tender.name;
  2914. break;
  2915. case codeRuleConst.measure.ruleType.inDate:
  2916. rule.preview = moment().format('YYYY');
  2917. break;
  2918. case codeRuleConst.measure.ruleType.text:
  2919. rule.preview = rule.text;
  2920. break;
  2921. case codeRuleConst.measure.ruleType.addNo:
  2922. const s = '0000000000';
  2923. rule.preview = s.substr(s.length - rule.format);
  2924. break;
  2925. default: break;
  2926. }
  2927. }
  2928. const changeApplyList = await ctx.service.changeApply.getAllDataByCondition({ where: { tid: tender.id, status: audit.changeApply.status.checked } });
  2929. const allApplyCodes = await ctx.service.changePlan.getAllDataByCondition({
  2930. columns: ['apply_code'],
  2931. where: {
  2932. tid: tender.id,
  2933. },
  2934. });
  2935. const acLists = allApplyCodes.length > 0 ? ctx.app._.uniq(ctx.app._.map(allApplyCodes, 'apply_code')) : [];
  2936. const renderData = {
  2937. uid: ctx.session.sessionUser.accountId,
  2938. tender,
  2939. pageInfo,
  2940. changes,
  2941. status,
  2942. rule_type,
  2943. codeRule,
  2944. c_connector,
  2945. c_rule_first,
  2946. filter,
  2947. plan_username: tender_userInfo ? tender_userInfo.name : null,
  2948. ruleType: codeRuleConst.ruleType[rule_type],
  2949. dealCode: ctx.tender.info.deal_info.dealCode,
  2950. auditConst: audit.changeApply,
  2951. ruleConst: codeRuleConst.measure,
  2952. changeConst,
  2953. changeApplyList,
  2954. acLists,
  2955. page_total,
  2956. tp,
  2957. tenderMenu: this.menu.tenderMenu,
  2958. preUrl: '/tender/' + tender.id,
  2959. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.plan),
  2960. };
  2961. await this.layout('change/plan.ejs', renderData, 'change/plan_modal.ejs');
  2962. }
  2963. /**
  2964. * 变更申请列表 页面 (Get)
  2965. *
  2966. * @param {Object} ctx - egg全局变量
  2967. * @return {void}
  2968. */
  2969. async plan(ctx) {
  2970. try {
  2971. await this._filterChangesPlan(ctx);
  2972. } catch (err) {
  2973. this.log(err);
  2974. ctx.redirect('/dashboard');
  2975. }
  2976. }
  2977. /**
  2978. * 变更管理 状态筛选 页面 (Get)
  2979. * @param {Object} ctx - egg全局变量
  2980. * @return {void}
  2981. */
  2982. async planStatus(ctx) {
  2983. try {
  2984. const status = parseInt(ctx.params.status);
  2985. await this._filterChangesPlan(ctx, status);
  2986. } catch (err) {
  2987. this.logger.error(err);
  2988. ctx.redirect('/tender/' + ctx.params.id + '/change/plan');
  2989. }
  2990. }
  2991. /**
  2992. * 新增变更立项 (Post)
  2993. *
  2994. * @param {Object} ctx - egg全局变量
  2995. * @return {void}
  2996. */
  2997. async planAdd(ctx) {
  2998. try {
  2999. const tenderId = ctx.params.id;
  3000. if (!tenderId) {
  3001. throw '当前未打开标段';
  3002. }
  3003. const data = JSON.parse(ctx.request.body.data);
  3004. if (!data.code || data.code === '') {
  3005. throw '变更方案编号不能为空';
  3006. }
  3007. const change = await ctx.service.changePlan.add(tenderId, ctx.session.sessionUser.accountId, data.code, data.apply_code, data.name);
  3008. ctx.body = { err: 0, msg: '', data: change };
  3009. } catch (err) {
  3010. this.log(err);
  3011. ctx.body = { err: 1, msg: err.toString() };
  3012. }
  3013. }
  3014. /**
  3015. * 删除变更立项
  3016. * @param {Object} ctx - egg全局变量
  3017. * @return {void}
  3018. */
  3019. async planDelete(ctx) {
  3020. try {
  3021. const result = await ctx.service.changePlan.delete(ctx.request.body.cpid);
  3022. if (!result) {
  3023. throw '删除变更方案失败';
  3024. }
  3025. ctx.redirect(ctx.request.header.referer);
  3026. } catch (err) {
  3027. console.log(err);
  3028. ctx.redirect(ctx.request.header.referer);
  3029. }
  3030. }
  3031. /**
  3032. * 获取审批界面所需的 原报、审批人数据等
  3033. * @param ctx
  3034. * @return {Promise<void>}
  3035. * @private
  3036. */
  3037. async _getChangePlanAuditViewData(ctx) {
  3038. const auditConst = audit.changeApply;
  3039. const times = ctx.change.status === auditConst.status.checkNo ? ctx.change.times - 1 : ctx.change.times;
  3040. ctx.change.user = await ctx.service.projectAccount.getAccountInfoById(ctx.change.uid);
  3041. ctx.change.auditHistory = [];
  3042. if (times >= 1) {
  3043. for (let i = 1; i <= times; i++) {
  3044. ctx.change.auditHistory.push(await ctx.service.changePlanAudit.getAuditors(ctx.change.id, i));
  3045. }
  3046. }
  3047. // 获取审批流程中左边列表
  3048. ctx.change.auditors2 = ctx.change.status === auditConst.status.checkNo && ctx.change.uid !== ctx.session.sessionUser.accountId ?
  3049. await ctx.service.changePlanAudit.getAuditorsWithOwner(ctx.change.id, times) :
  3050. await ctx.service.changePlanAudit.getAuditorsWithOwner(ctx.change.id, ctx.change.times);
  3051. if (ctx.change.status === auditConst.status.uncheck || ctx.change.status === auditConst.status.checkNo) {
  3052. ctx.change.auditorList = await ctx.service.changePlanAudit.getAuditors(ctx.change.id, ctx.change.times);
  3053. }
  3054. }
  3055. async planInformation(ctx) {
  3056. try {
  3057. const whiteList = this.ctx.app.config.multipart.whitelist;
  3058. const tender = await ctx.service.tender.getDataById(ctx.tender.id);
  3059. await this._getChangePlanAuditViewData(ctx);
  3060. // 获取附件列表
  3061. const fileList = await ctx.service.changePlanAtt.getAllChangePlanAtt(ctx.tender.id, ctx.change.id);
  3062. // 获取清单列表
  3063. const changeList = await ctx.service.changePlanList.getList(ctx.change.id);
  3064. if (ctx.change.status === audit.changeApply.status.checking || ctx.change.status === audit.changeApply.status.checked) {
  3065. const listAudits = await ctx.service.changePlanAudit.getAuditGroupByList(ctx.change.id, ctx.change.times);
  3066. ctx.change.listAudits = listAudits;
  3067. for (const cl of changeList) {
  3068. const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
  3069. // 清单表页赋值
  3070. for (const [index, au] of listAudits.entries()) {
  3071. cl['audit_amount_' + au.aid] = ctx.change.shenpiPower && au.aid === ctx.session.sessionUser.accountId ? cl.spamount : (audit_amount[index] ? parseFloat(audit_amount[index]) : null);
  3072. }
  3073. }
  3074. }
  3075. const renderData = {
  3076. tender,
  3077. change: ctx.change,
  3078. listRule: ctx.change.list_rule ? JSON.parse(ctx.change.list_rule) : { source: 1, rule: ['unit', 'unit_price'] },
  3079. changeList,
  3080. changeConst,
  3081. auditConst: audit.changeApply,
  3082. fileList,
  3083. whiteList,
  3084. // tpUnit: change.tp_decimal ? change.tp_decimal : ctx.tender.info.decimal.tp,
  3085. // upUnit: change.up_decimal ? change.up_decimal : ctx.tender.info.decimal.up,
  3086. tpUnit: ctx.tender.info.decimal.tp,
  3087. upUnit: ctx.tender.info.decimal.up,
  3088. changeUnits: changeConst.units,
  3089. precision: ctx.tender.info.precision,
  3090. 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,
  3091. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.change.plan_information),
  3092. preUrl: '/tender/' + ctx.tender.id + '/change/plan/' + ctx.change.id + '/information',
  3093. };
  3094. if ((ctx.change.status === audit.changeApply.status.uncheck || ctx.change.status === audit.changeApply.status.checkNo) && (ctx.session.sessionUser.accountId === ctx.change.uid || ctx.tender.isTourist)) {
  3095. // data.accountGroup = accountGroup;
  3096. // 获取所有项目参与者
  3097. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  3098. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  3099. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  3100. });
  3101. renderData.accountList = accountList;
  3102. renderData.accountGroup = accountGroup.map((item, idx) => {
  3103. const groupList = accountList.filter(item => item.account_group === idx);
  3104. return { groupName: item, groupList };
  3105. });
  3106. }
  3107. await this.layout('change/plan_information.ejs', renderData, 'change/plan_information_modal.ejs');
  3108. } catch (err) {
  3109. this.log(err);
  3110. ctx.redirect('/tender/' + ctx.params.id + '/change');
  3111. }
  3112. }
  3113. // 审批相关
  3114. /**
  3115. * 添加审批人
  3116. * @param ctx
  3117. * @return {Promise<void>}
  3118. */
  3119. async addPlanAudit(ctx) {
  3120. try {
  3121. const auditConst = audit.changeApply;
  3122. const data = JSON.parse(ctx.request.body.data);
  3123. const id = this.app._.toInteger(data.auditorId);
  3124. if (isNaN(id) || id <= 0) {
  3125. throw '参数错误';
  3126. }
  3127. // 检查权限等
  3128. if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
  3129. throw '您无权添加审核人';
  3130. }
  3131. if (ctx.change.status === auditConst.status.checking || ctx.change.status === auditConst.status.checked) {
  3132. throw '当前不允许添加审核人';
  3133. }
  3134. ctx.change.auditorList = await ctx.service.changePlanAudit.getAuditors(ctx.change.id, ctx.change.times);
  3135. // 检查审核人是否已存在
  3136. const exist = this.app._.find(ctx.change.auditorList, { aid: id });
  3137. if (exist) {
  3138. throw '该审核人已存在,请勿重复添加';
  3139. }
  3140. // const shenpiInfo = await ctx.service.shenpiAudit.getDataByCondition({ tid: ctx.tender.id, sp_type: shenpiConst.sp_type.material, sp_status: shenpiConst.sp_status.gdzs });
  3141. // const is_gdzs = shenpiInfo && ctx.tender.info.shenpi.material === shenpiConst.sp_status.gdzs ? 1 : 0;
  3142. const result = await ctx.service.changePlanAudit.addAuditor(ctx.change.id, id, ctx.change.times);
  3143. if (!result) {
  3144. throw '添加审核人失败';
  3145. }
  3146. const auditors = await ctx.service.changePlanAudit.getAuditorsWithOwner(ctx.change.id, ctx.change.times);
  3147. ctx.body = { err: 0, msg: '', data: auditors };
  3148. } catch (err) {
  3149. this.log(err);
  3150. ctx.body = { err: 1, msg: err.toString(), data: null };
  3151. }
  3152. }
  3153. /**
  3154. * 移除审批人
  3155. * @param ctx
  3156. * @return {Promise<void>}
  3157. */
  3158. async deletePlanAudit(ctx) {
  3159. try {
  3160. const data = JSON.parse(ctx.request.body.data);
  3161. const id = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
  3162. if (isNaN(id) || id <= 0) {
  3163. throw '参数错误';
  3164. }
  3165. const result = await ctx.service.changePlanAudit.deleteAuditor(ctx.change.id, id, ctx.change.times);
  3166. if (!result) {
  3167. throw '移除审核人失败';
  3168. }
  3169. const auditors = await ctx.service.changePlanAudit.getAuditors(ctx.change.id, ctx.change.times);
  3170. ctx.body = { err: 0, msg: '', data: auditors };
  3171. } catch (err) {
  3172. ctx.body = { err: 1, msg: err.toString(), data: null };
  3173. }
  3174. }
  3175. /**
  3176. * 上传附件
  3177. * @param {*} ctx 上下文
  3178. */
  3179. async uploadPlanFile(ctx) {
  3180. let stream;
  3181. try {
  3182. const auditConst = audit.changeApply;
  3183. // this._checkAdvanceFileCanModify(ctx);
  3184. const parts = this.ctx.multipart({
  3185. autoFields: true,
  3186. });
  3187. const files = [];
  3188. const create_time = Date.parse(new Date()) / 1000;
  3189. let idx = 0;
  3190. const extra_upload = ctx.change.status === auditConst.status.checked;
  3191. while ((stream = await parts()) !== undefined) {
  3192. if (!stream.filename) {
  3193. // 如果没有传入直接返回
  3194. return;
  3195. }
  3196. const fileInfo = path.parse(stream.filename);
  3197. const filepath = `app/public/upload/${this.ctx.tender.id.toString()}/change_plan/fujian_${create_time + idx.toString() + fileInfo.ext}`;
  3198. // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, 'app', filepath));
  3199. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  3200. files.push({ filepath, name: stream.filename, ext: fileInfo.ext });
  3201. ++idx;
  3202. stream && (await sendToWormhole(stream));
  3203. }
  3204. const in_time = new Date();
  3205. const payload = files.map(file => {
  3206. let idx;
  3207. if (Array.isArray(parts.field.name)) {
  3208. idx = parts.field.name.findIndex(name => name === file.name);
  3209. } else {
  3210. idx = 'isString';
  3211. }
  3212. const newFile = {
  3213. tid: ctx.tender.id,
  3214. cpid: ctx.change.id,
  3215. uid: ctx.session.sessionUser.accountId,
  3216. filename: file.name,
  3217. fileext: file.ext,
  3218. filesize: ctx.helper.bytesToSize(idx === 'isString' ? parts.field.size : parts.field.size[idx]),
  3219. filepath: file.filepath,
  3220. upload_time: in_time,
  3221. extra_upload,
  3222. };
  3223. return newFile;
  3224. });
  3225. // 执行文件信息写入数据库
  3226. await ctx.service.changePlanAtt.saveFileMsgToDb(payload);
  3227. // 将最新的当前标段的所有文件信息返回
  3228. const data = await ctx.service.changePlanAtt.getAllChangePlanAtt(ctx.tender.id, ctx.change.id);
  3229. ctx.body = { err: 0, msg: '', data };
  3230. } catch (err) {
  3231. stream && (await sendToWormhole(stream));
  3232. this.log(err);
  3233. ctx.body = { err: 1, msg: err.toString(), data: null };
  3234. }
  3235. }
  3236. /**
  3237. * 删除附件
  3238. * @param {Ojbect} ctx 上下文
  3239. */
  3240. async deletePlanFile(ctx) {
  3241. try {
  3242. const { id } = JSON.parse(ctx.request.body.data);
  3243. const fileInfo = await ctx.service.changePlanAtt.getDataById(id);
  3244. if (fileInfo || Object.keys(fileInfo).length) {
  3245. // 先删除文件
  3246. // await fs.unlinkSync(path.resolve(this.app.baseDir, './app', fileInfo.filepath));
  3247. await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
  3248. // 再删除数据库
  3249. await ctx.service.changePlanAtt.delete(id);
  3250. } else {
  3251. throw '不存在该文件';
  3252. }
  3253. const data = await ctx.service.changePlanAtt.getAllChangePlanAtt(ctx.tender.id, ctx.change.id);
  3254. ctx.body = { err: 0, msg: '请求成功', data };
  3255. } catch (err) {
  3256. this.log(err);
  3257. ctx.body = { err: 1, msg: err.toString(), data: null };
  3258. }
  3259. }
  3260. /**
  3261. * 下载附件
  3262. * @param {Object} ctx - egg全局变量
  3263. * @return {void}
  3264. */
  3265. async downloadPlanFile(ctx) {
  3266. const id = ctx.params.fid;
  3267. if (id) {
  3268. try {
  3269. const fileInfo = await ctx.service.changePlanAtt.getDataById(id);
  3270. if (fileInfo !== undefined && fileInfo !== '') {
  3271. // const fileName = path.join(__dirname, '../', fileInfo.filepath);
  3272. // 解决中文无法下载问题
  3273. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  3274. let disposition = '';
  3275. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  3276. disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename);
  3277. } else if (userAgent.indexOf('firefox') >= 0) {
  3278. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename) + '"';
  3279. } else {
  3280. /* safari等其他非主流浏览器只能自求多福了 */
  3281. disposition = 'attachment; filename=' + new Buffer(fileInfo.filename).toString('binary');
  3282. }
  3283. ctx.response.set({
  3284. 'Content-Type': 'application/octet-stream',
  3285. 'Content-Disposition': disposition,
  3286. 'Content-Length': fileInfo.filesize,
  3287. });
  3288. // ctx.body = await fs.createReadStream(fileName);
  3289. ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
  3290. } else {
  3291. throw '不存在该文件';
  3292. }
  3293. } catch (err) {
  3294. this.log(err);
  3295. this.setMessage(err.toString(), this.messageType.ERROR);
  3296. }
  3297. }
  3298. }
  3299. async planInformationSave(ctx) {
  3300. try {
  3301. const data = JSON.parse(ctx.request.body.data);
  3302. if (data.name === 'code') {
  3303. const info = await ctx.service.changePlan.isRepeat(ctx.change.id, data.val, ctx.tender.id);
  3304. if (info) {
  3305. throw '该编号已存在';
  3306. }
  3307. }
  3308. const result = await ctx.service.changePlan.saveInfo(ctx.change.id, data);
  3309. if (!result) {
  3310. throw '修改失败';
  3311. }
  3312. ctx.body = { err: 0, msg: '请求成功', data: null };
  3313. } catch (err) {
  3314. this.log(err);
  3315. ctx.body = { err: 1, msg: err.toString(), data: null };
  3316. }
  3317. }
  3318. /**
  3319. * 上报和重新上报
  3320. * @param ctx
  3321. * @return {Promise<void>}
  3322. */
  3323. async startPlanAudit(ctx) {
  3324. try {
  3325. const auditConst = audit.changeApply;
  3326. // 检查权限等
  3327. if (!ctx.change) {
  3328. throw '数据错误';
  3329. }
  3330. if (ctx.change.uid !== ctx.session.sessionUser.accountId) {
  3331. throw '您无权上报该期数据';
  3332. }
  3333. if (ctx.change.status === auditConst.status.checking || ctx.change.status === auditConst.status.checked) {
  3334. throw '该材料调差期数据当前无法上报';
  3335. }
  3336. await ctx.service.changePlanAudit.start(ctx.change.id, ctx.change.times);
  3337. ctx.redirect(ctx.request.header.referer);
  3338. } catch (err) {
  3339. this.log(err);
  3340. ctx.session.postError = err.toString();
  3341. ctx.redirect(ctx.request.header.referer);
  3342. }
  3343. }
  3344. /**
  3345. * 审批
  3346. * @param ctx
  3347. * @return {Promise<void>}
  3348. */
  3349. async checkPlanAudit(ctx) {
  3350. try {
  3351. const auditConst = audit.changeApply;
  3352. if (!ctx.change || ctx.change.status !== auditConst.status.checking) {
  3353. throw '当前材料调差期数据有误';
  3354. }
  3355. if (!ctx.change.curAuditor || ctx.change.curAuditor.aid !== ctx.session.sessionUser.accountId) {
  3356. throw '您无权进行该操作';
  3357. }
  3358. const data = {
  3359. checkType: parseInt(ctx.request.body.checkType),
  3360. opinion: ctx.request.body.opinion,
  3361. };
  3362. if (!data.checkType || isNaN(data.checkType)) {
  3363. throw '提交数据错误';
  3364. }
  3365. // if (data.checkType === auditConst.status.checkNo) {
  3366. // if (!data.checkType || isNaN(data.checkType)) {
  3367. // throw '提交数据错误';
  3368. // }
  3369. // }
  3370. await ctx.service.changePlanAudit.check(ctx.change.id, data, ctx.change.times);
  3371. ctx.redirect(ctx.request.header.referer);
  3372. } catch (err) {
  3373. this.log(err);
  3374. ctx.session.postError = err.toString();
  3375. ctx.redirect(ctx.request.header.referer);
  3376. }
  3377. }
  3378. /**
  3379. * 变更清单 - 操作 (Ajax)
  3380. * @param ctx
  3381. * @return {Promise<void>}
  3382. */
  3383. async savePlanListsData(ctx) {
  3384. try {
  3385. const data = JSON.parse(ctx.request.body.data);
  3386. const responseData = {
  3387. err: 0,
  3388. msg: '',
  3389. data: {},
  3390. };
  3391. switch (data.type) {
  3392. case 'add':
  3393. responseData.data = await ctx.service.changePlanList.add(data.updateData);
  3394. break;
  3395. case 'batchadd':
  3396. responseData.data = await ctx.service.changePlanList.batchAdd(data);
  3397. break;
  3398. case 'del':
  3399. await ctx.service.changePlanList.del(data.ids);
  3400. // 取所有工料表
  3401. responseData.data = await ctx.service.changePlanList.getList(ctx.change.id);
  3402. break;
  3403. case 'update':
  3404. // if (data.updateData.code === '' || data.updateData.code === null) {
  3405. // throw '请先输入编号';
  3406. // }
  3407. await ctx.service.changePlanList.save(data.updateData);
  3408. break;
  3409. case 'paste':
  3410. await ctx.service.changePlanList.saveDatas(data.updateData);
  3411. // 取所有工料表
  3412. responseData.data = await ctx.service.changePlanList.getList(ctx.change.id);
  3413. break;
  3414. case 'list_rule':
  3415. const result = await ctx.service.changePlan.saveInfo(ctx.change.id, { name: 'list_rule', val: data.postData });
  3416. if (!result) {
  3417. throw '修改失败';
  3418. }
  3419. break;
  3420. case 'paste_amount_rows':
  3421. await ctx.service.changePlanList.saveDatas(data.updateData);
  3422. const changeList = await ctx.service.changePlanList.getList(ctx.change.id);
  3423. const listAudits = await ctx.service.changePlanAudit.getAuditGroupByList(ctx.change.id, ctx.change.times);
  3424. for (const cl of changeList) {
  3425. // 清单表页赋值
  3426. const audit_amount = cl.audit_amount !== null && cl.audit_amount !== '' ? cl.audit_amount.split(',') : '';
  3427. // 清单表页赋值
  3428. for (const [index, au] of listAudits.entries()) {
  3429. cl['audit_amount_' + au.aid] = ctx.change.shenpiPower && au.aid === ctx.session.sessionUser.accountId ? cl.spamount : (audit_amount[index] ? parseFloat(audit_amount[index]) : null);
  3430. }
  3431. }
  3432. // 取所有工料表
  3433. responseData.data = changeList;
  3434. break;
  3435. default: throw '参数有误';
  3436. }
  3437. ctx.body = responseData;
  3438. } catch (err) {
  3439. this.log(err);
  3440. ctx.body = { err: 1, msg: err.toString(), data: null };
  3441. }
  3442. }
  3443. }
  3444. return ChangeController;
  3445. };