Stock.go 173 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454
  1. package controllers
  2. import (
  3. "ERP_storage/Nats"
  4. "ERP_storage/Nats/NatsServer"
  5. "ERP_storage/conf"
  6. "ERP_storage/dto"
  7. "ERP_storage/logs"
  8. "ERP_storage/models/Account"
  9. "ERP_storage/models/Basic"
  10. "ERP_storage/models/Contract"
  11. "ERP_storage/models/Stock"
  12. "errors"
  13. "fmt"
  14. "math"
  15. "os"
  16. "sort"
  17. "strconv"
  18. "strings"
  19. "time"
  20. "github.com/beego/beego/v2/adapter/orm"
  21. beego "github.com/beego/beego/v2/server/web"
  22. "github.com/robfig/cron/v3"
  23. "github.com/xuri/excelize/v2"
  24. natslibs "gogs.baozhida.cn/zoie/ERP_libs/Nats"
  25. userlibs "gogs.baozhida.cn/zoie/ERP_libs/User"
  26. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  27. )
  28. type StockController struct {
  29. beego.Controller
  30. User userlibs.User
  31. }
  32. func (c *StockController) Prepare() {
  33. c.User = *Account.User_r
  34. }
  35. func (c *StockController) Device_List() {
  36. // 分页参数 初始化
  37. page, _ := c.GetInt("page")
  38. if page < 1 {
  39. page = 1
  40. }
  41. page_z, _ := c.GetInt("page_z")
  42. if page_z < 1 {
  43. page_z = conf.Page_size
  44. }
  45. T_state, _ := c.GetInt("T_state")
  46. T_name := c.GetString("T_name")
  47. T_product_name := c.GetString("T_product_name") // 产品名称
  48. T_product_model := c.GetString("T_product_model") // 产品型号
  49. DeviceDao := Stock.NewDevice(orm.NewOrm())
  50. R_List, R_cnt := DeviceDao.Read_Device_List(T_name, T_product_name, T_product_model, T_state, page, page_z)
  51. var r_jsons lib.R_JSONS
  52. r_jsons.Num = R_cnt
  53. r_jsons.Data = R_List
  54. r_jsons.Page = page
  55. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  56. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  57. c.ServeJSON()
  58. return
  59. }
  60. func (c *StockController) Device_Excel() {
  61. T_state, _ := c.GetInt("T_state")
  62. T_name := c.GetString("T_name")
  63. T_product_name := c.GetString("T_product_name") // 产品名称
  64. T_product_model := c.GetString("T_product_model") // 产品型号
  65. DeviceDao := Stock.NewDevice(orm.NewOrm())
  66. R_List, _ := DeviceDao.Read_Device_List(T_name, T_product_name, T_product_model, T_state, 0, 9999)
  67. // 生成Excel文件名
  68. filename := fmt.Sprintf("设备列表(%s)", lib.GetRandstring(6, "0123456789", 0))
  69. // 创建Excel文件
  70. f := excelize.NewFile()
  71. // 设置样式
  72. Style1, _ := f.NewStyle(
  73. &excelize.Style{
  74. Font: &excelize.Font{Bold: true, Size: 14, Family: "宋体"},
  75. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  76. })
  77. Style2, _ := f.NewStyle(
  78. &excelize.Style{
  79. Font: &excelize.Font{Bold: true, Size: 10, Family: "宋体"},
  80. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  81. Border: []excelize.Border{
  82. {Type: "left", Color: "000000", Style: 1},
  83. {Type: "top", Color: "000000", Style: 1},
  84. {Type: "bottom", Color: "000000", Style: 1},
  85. {Type: "right", Color: "000000", Style: 1},
  86. },
  87. })
  88. Style3, _ := f.NewStyle(
  89. &excelize.Style{
  90. Font: &excelize.Font{Size: 10, Family: "宋体"},
  91. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  92. Border: []excelize.Border{
  93. {Type: "left", Color: "000000", Style: 1},
  94. {Type: "top", Color: "000000", Style: 1},
  95. {Type: "bottom", Color: "000000", Style: 1},
  96. {Type: "right", Color: "000000", Style: 1},
  97. },
  98. })
  99. // 设置工作表名称
  100. f.SetSheetName("Sheet1", "设备列表")
  101. // 设置标题
  102. f.MergeCell("设备列表", "A1", "M1")
  103. f.SetRowStyle("设备列表", 1, 1, Style1)
  104. f.SetCellValue("设备列表", "A1", "宝智达科技设备列表")
  105. f.SetRowHeight("设备列表", 1, 30)
  106. // 设置表头
  107. f.SetCellStyle("设备列表", "A2", "M2", Style2)
  108. f.SetRowHeight("设备列表", 2, 20)
  109. // 设置表头内容
  110. f.SetCellValue("设备列表", "A2", "序号")
  111. f.SetCellValue("设备列表", "B2", "合同编号")
  112. f.SetCellValue("设备列表", "C2", "出库单号")
  113. f.SetCellValue("设备列表", "D2", "关联项目")
  114. f.SetCellValue("设备列表", "E2", "产品名称")
  115. f.SetCellValue("设备列表", "F2", "产品分类")
  116. f.SetCellValue("设备列表", "G2", "产品型号")
  117. f.SetCellValue("设备列表", "H2", "产品规格")
  118. f.SetCellValue("设备列表", "I2", "设备SN")
  119. f.SetCellValue("设备列表", "J2", "模组imei")
  120. f.SetCellValue("设备列表", "K2", "物联网卡号")
  121. f.SetCellValue("设备列表", "L2", "设备编号")
  122. f.SetCellValue("设备列表", "M2", "状态")
  123. // 设置列宽
  124. f.SetColWidth("设备列表", "A", "A", 8) // 序号
  125. f.SetColWidth("设备列表", "B", "B", 15) // 合同编号
  126. f.SetColWidth("设备列表", "C", "C", 15) // 出库单号
  127. f.SetColWidth("设备列表", "D", "D", 20) // 关联项目
  128. f.SetColWidth("设备列表", "E", "E", 20) // 产品名称
  129. f.SetColWidth("设备列表", "F", "F", 15) // 产品分类
  130. f.SetColWidth("设备列表", "G", "G", 15) // 产品型号
  131. f.SetColWidth("设备列表", "H", "H", 15) // 产品规格
  132. f.SetColWidth("设备列表", "I", "I", 20) // 设备SN
  133. f.SetColWidth("设备列表", "J", "J", 15) // 模组imei
  134. f.SetColWidth("设备列表", "K", "K", 20) // 物联网卡号
  135. f.SetColWidth("设备列表", "L", "L", 18) // 设备编号
  136. f.SetColWidth("设备列表", "M", "M", 10) // 状态
  137. // 填充数据
  138. for i, device := range R_List {
  139. row := i + 3 // 从第3行开始填充数据
  140. // 状态转换
  141. stateText := "未出库"
  142. if device.T_State == 1 {
  143. stateText = "已出库"
  144. }
  145. f.SetCellStyle("设备列表", fmt.Sprintf("A%d", row), fmt.Sprintf("M%d", row), Style3)
  146. //f.SetRowHeight("设备列表", row, 20)
  147. f.SetCellValue("设备列表", fmt.Sprintf("A%d", row), i+1) // 序号
  148. f.SetCellValue("设备列表", fmt.Sprintf("B%d", row), device.T_contract_number) // 合同编号
  149. f.SetCellValue("设备列表", fmt.Sprintf("C%d", row), device.T_out_number) // 出库单号
  150. f.SetCellValue("设备列表", fmt.Sprintf("D%d", row), device.T_project) // 关联项目
  151. f.SetCellValue("设备列表", fmt.Sprintf("E%d", row), device.T_product_name) // 产品名称
  152. f.SetCellValue("设备列表", fmt.Sprintf("F%d", row), device.T_product_class_name) // 产品分类
  153. f.SetCellValue("设备列表", fmt.Sprintf("G%d", row), device.T_product_model) // 产品型号
  154. f.SetCellValue("设备列表", fmt.Sprintf("H%d", row), device.T_product_spec) // 产品规格
  155. f.SetCellValue("设备列表", fmt.Sprintf("I%d", row), device.T_sn) // 设备SN
  156. f.SetCellValue("设备列表", fmt.Sprintf("J%d", row), device.T_imei) // 模组imei
  157. f.SetCellValue("设备列表", fmt.Sprintf("K%d", row), device.T_iccid) // 物联网卡号
  158. f.SetCellValue("设备列表", fmt.Sprintf("L%d", row), device.T_device_number) // 设备编号
  159. f.SetCellValue("设备列表", fmt.Sprintf("M%d", row), stateText) // 状态
  160. }
  161. // 保存文件
  162. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  163. fmt.Println(err)
  164. }
  165. var url string
  166. //// 上传 OSS
  167. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  168. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  169. if !is {
  170. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  171. c.ServeJSON()
  172. return
  173. }
  174. //删除目录
  175. err := os.Remove("ofile/" + filename + ".xlsx")
  176. if err != nil {
  177. logs.Error(lib.FuncName(), err)
  178. }
  179. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  180. c.ServeJSON()
  181. return
  182. }
  183. func (c *StockController) Device_Check() {
  184. T_sn := c.GetString("T_sn")
  185. T_type, _ := c.GetInt("T_type") //1-出库 2-入库
  186. T_product_id, _ := c.GetInt("T_product_id")
  187. T_date := c.GetString("T_date")
  188. // 检查MQTT授权
  189. //mqtt := Stock.Read_MqttUser(T_sn)
  190. //if len(mqtt.Username) == 0 {
  191. // c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】未授权,请先授权!", T_sn)}
  192. // c.ServeJSON()
  193. // return
  194. //}
  195. DeviceDao := Stock.NewDevice(orm.NewOrm())
  196. date, is := lib.DateStrToTime(T_date)
  197. if !is {
  198. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  199. c.ServeJSON()
  200. return
  201. }
  202. checkTime := date.Format("2006-01-02") + time.Now().Format(" 15:04:05")
  203. // T_State 1-已出库 2-未出库/入库
  204. // 查询checkTime时间前最后一条记录
  205. lastBeforeCheckTime, errBefore := DeviceDao.Read_Device_LastBeforeCheckTime(T_sn, checkTime)
  206. // 查询checkTime时间后第一条记录
  207. firstAfterCheckTime, errAfter := DeviceDao.Read_Device_FirstAfterCheckTime(T_sn, checkTime)
  208. // 如果是入库操作
  209. if T_type == 2 {
  210. // 根据表格逻辑判断:
  211. // 1. 时间前无数据,时间后有入库记录
  212. if (errBefore != nil || lastBeforeCheckTime.Id == 0) &&
  213. errAfter == nil && firstAfterCheckTime.T_State == 2 && len(firstAfterCheckTime.T_in_number) > 0 {
  214. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】已入库,请勿重复提交!", T_sn)}
  215. c.ServeJSON()
  216. return
  217. }
  218. // 2. 时间前已入库
  219. if errBefore == nil && lastBeforeCheckTime.T_State == 2 && len(lastBeforeCheckTime.T_in_number) > 0 {
  220. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】已入库,请勿重复提交!", T_sn)}
  221. c.ServeJSON()
  222. return
  223. }
  224. // 3. 时间前已出库,时间后有入库记录
  225. if errBefore == nil && lastBeforeCheckTime.T_State == 1 && len(lastBeforeCheckTime.T_out_number) > 0 &&
  226. errAfter == nil && firstAfterCheckTime.T_State == 2 && len(firstAfterCheckTime.T_in_number) > 0 {
  227. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】已入库,请勿重复提交!", T_sn)}
  228. c.ServeJSON()
  229. return
  230. }
  231. }
  232. // 如果是出库操作
  233. if T_type == 1 {
  234. // 根据表格逻辑判断:
  235. // 1. 时间前无数据 -> 未入库
  236. if errBefore != nil || lastBeforeCheckTime.Id == 0 {
  237. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】未入库,请先入库!", T_sn)}
  238. c.ServeJSON()
  239. return
  240. }
  241. // 2. 时间前已入库,时间后有出库记录
  242. if lastBeforeCheckTime.T_State == 2 && len(lastBeforeCheckTime.T_in_number) > 0 &&
  243. errAfter == nil && firstAfterCheckTime.T_State == 1 && len(firstAfterCheckTime.T_out_number) > 0 {
  244. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】已出库,请勿重复提交!", T_sn)}
  245. c.ServeJSON()
  246. return
  247. }
  248. // 3. 时间前已出库
  249. if lastBeforeCheckTime.T_State == 1 && len(lastBeforeCheckTime.T_out_number) > 0 {
  250. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】已出库,请勿重复提交!", T_sn)}
  251. c.ServeJSON()
  252. return
  253. }
  254. // 检查产品是否一致
  255. if lastBeforeCheckTime.T_product_id != T_product_id {
  256. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库产品与出库产品不一致,请检查!"}
  257. c.ServeJSON()
  258. return
  259. }
  260. }
  261. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  262. c.ServeJSON()
  263. return
  264. }
  265. func (c *StockController) Device_Take_Stock() {
  266. T_product_name := c.GetString("T_product_name") // 产品名称
  267. T_product_model := c.GetString("T_product_model") // 产品型号
  268. T_sn_list := c.GetString("T_sn_list") // sn列表
  269. DeviceDao := Stock.NewDevice(orm.NewOrm())
  270. // 获取库存中的所有设备(如果有产品名称和型号过滤)
  271. R_List, _ := DeviceDao.Read_Device_List("", T_product_name, T_product_model, 0, 0, 9999)
  272. // 创建设备SN到设备信息的映射,用于快速查找
  273. deviceMap := make(map[string]Stock.Device_R)
  274. // 保存在库设备列表
  275. var inStockDevices []Stock.Device_R
  276. for _, device := range R_List {
  277. deviceMap[device.T_sn] = device
  278. // 收集所有在库设备(状态为2)
  279. if device.T_State == 2 {
  280. inStockDevices = append(inStockDevices, device)
  281. }
  282. }
  283. // 创建结果数据结构
  284. type DeviceCheckResult struct {
  285. T_sn string `json:"t_sn"` // 设备SN
  286. T_exists bool `json:"t_exists"` // 是否存在于库存
  287. T_state int `json:"t_state"` // 设备状态:1-已出库 2-未出库/入库
  288. T_state_desc string `json:"t_state_desc"` // 状态描述
  289. T_product_id int `json:"t_product_id"` // 产品ID
  290. T_product_name string `json:"t_product_name"` // 产品名称
  291. T_product_model string `json:"t_product_model"` // 产品型号
  292. T_iccid string `json:"t_iccid"` // SIM卡号
  293. T_imei string `json:"t_imei"` // 模组IMEI
  294. T_in_number string `json:"t_in_number"` // 入库编号
  295. T_out_number string `json:"t_out_number"` // 出库编号
  296. T_from_input bool `json:"t_from_input"` // 是否来自输入列表
  297. }
  298. // 处理每个输入的SN
  299. var results []DeviceCheckResult
  300. var processedSNs = make(map[string]bool) // 用于跟踪已处理的SN
  301. // 解析输入的SN列表(如果有)
  302. var snList []string
  303. if len(T_sn_list) > 0 {
  304. snList = lib.SplitString(T_sn_list, ",")
  305. for _, sn := range snList {
  306. sn = strings.TrimSpace(sn)
  307. if len(sn) == 0 {
  308. continue
  309. }
  310. result := DeviceCheckResult{
  311. T_sn: sn,
  312. T_exists: false,
  313. T_from_input: true, // 标记为来自输入列表
  314. }
  315. // 检查设备是否存在于库存
  316. if device, exists := deviceMap[sn]; exists {
  317. result.T_exists = true
  318. result.T_state = device.T_State
  319. result.T_product_id = device.T_product_id
  320. result.T_product_name = device.T_product_name
  321. result.T_product_model = device.T_product_model
  322. result.T_iccid = device.T_iccid
  323. result.T_imei = device.T_imei
  324. result.T_in_number = device.T_in_number
  325. result.T_out_number = device.T_out_number
  326. // 设置状态描述
  327. if device.T_State == 1 {
  328. result.T_state_desc = "已出库"
  329. } else if device.T_State == 2 {
  330. result.T_state_desc = "在库"
  331. } else {
  332. result.T_state_desc = "未知状态"
  333. }
  334. } else {
  335. // 设备不存在于库存
  336. result.T_state_desc = "未入库"
  337. }
  338. results = append(results, result)
  339. processedSNs[sn] = true
  340. }
  341. }
  342. // 添加所有在库设备(如果还没有处理过)
  343. for _, device := range inStockDevices {
  344. // 如果这个SN已经处理过(来自输入列表),则跳过
  345. if _, processed := processedSNs[device.T_sn]; processed {
  346. continue
  347. }
  348. result := DeviceCheckResult{
  349. T_sn: device.T_sn,
  350. T_exists: true,
  351. T_state: device.T_State,
  352. T_state_desc: "在库",
  353. T_product_id: device.T_product_id,
  354. T_product_name: device.T_product_name,
  355. T_product_model: device.T_product_model,
  356. T_iccid: device.T_iccid,
  357. T_imei: device.T_imei,
  358. T_in_number: device.T_in_number,
  359. T_out_number: device.T_out_number,
  360. T_from_input: false, // 标记为不是来自输入列表
  361. }
  362. results = append(results, result)
  363. }
  364. // 统计结果
  365. totalInputCount := 0
  366. existsCount := 0
  367. inStockCount := 0
  368. outStockCount := 0
  369. notExistsCount := 0
  370. for _, result := range results {
  371. if result.T_from_input {
  372. totalInputCount++
  373. if result.T_exists {
  374. existsCount++
  375. if result.T_state == 2 {
  376. inStockCount++
  377. } else if result.T_state == 1 {
  378. outStockCount++
  379. }
  380. } else {
  381. notExistsCount++
  382. }
  383. }
  384. }
  385. // 组装返回数据
  386. r_jsons := map[string]interface{}{
  387. "devices": results,
  388. "stats": map[string]interface{}{
  389. "total_input": totalInputCount,
  390. "exists": existsCount,
  391. "in_stock": inStockCount,
  392. "out_stock": outStockCount,
  393. "not_exists": notExistsCount,
  394. "total_in_stock": len(inStockDevices),
  395. },
  396. }
  397. c.Data["json"] = lib.JSONS{Code: 200, Msg: "盘点完成", Data: r_jsons}
  398. c.ServeJSON()
  399. return
  400. }
  401. func (c *StockController) Stock_List() {
  402. // 分页参数 初始化
  403. page, _ := c.GetInt("page")
  404. if page < 1 {
  405. page = 1
  406. }
  407. page_z, _ := c.GetInt("page_z")
  408. if page_z < 1 {
  409. page_z = conf.Page_size
  410. }
  411. // 查询
  412. T_depot_id, _ := c.GetInt("T_depot_id")
  413. // 查询
  414. T_product_name := c.GetString("T_product_name")
  415. T_product_model := c.GetString("T_product_model")
  416. T_product_class, _ := c.GetInt("T_product_class")
  417. userList, _ := NatsServer.Read_User_List_All()
  418. Account.Read_User_All_Map(userList)
  419. Basic.Read_Depot_All_Map()
  420. StockDao := Stock.NewStock(orm.NewOrm())
  421. R_List, R_cnt := StockDao.Read_Stock_List(T_depot_id, T_product_class, T_product_name, T_product_model, page, page_z)
  422. var r_jsons lib.R_JSONS
  423. r_jsons.Num = R_cnt
  424. r_jsons.Data = R_List
  425. r_jsons.Page = page
  426. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  427. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  428. c.ServeJSON()
  429. return
  430. }
  431. // 修改库存排序
  432. func (c *StockController) Stock_Edit_Sort() {
  433. T_id, _ := c.GetInt("T_id")
  434. T_sort, _ := c.GetInt("T_sort")
  435. StockDao := Stock.NewStock(orm.NewOrm())
  436. Stock_r, err := StockDao.Read_Stock_ById(T_id)
  437. if err != nil {
  438. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  439. c.ServeJSON()
  440. return
  441. }
  442. Stock_r.T_sort = T_sort
  443. if err = StockDao.Update_Stock(Stock_r, "T_sort"); err != nil {
  444. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  445. c.ServeJSON()
  446. return
  447. }
  448. NatsServer.AddUserLogs(c.User.T_uuid, "库存", "修改", Stock_r)
  449. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  450. c.ServeJSON()
  451. return
  452. }
  453. func (c *StockController) Stock_Detail_List() {
  454. // 查询
  455. T_depot_id, _ := c.GetInt("T_depot_id")
  456. // 查询
  457. T_product_id, _ := c.GetInt("T_product_id")
  458. T_start_date := c.GetString("T_start_date")
  459. T_end_date := c.GetString("T_end_date")
  460. now := time.Now()
  461. if len(T_start_date) == 0 {
  462. T_start_date = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local).Format("2006-01")
  463. } else {
  464. T_start_date_t, _ := lib.DateStrToTime(T_start_date)
  465. T_start_date = T_start_date_t.Format("2006-01")
  466. }
  467. if len(T_end_date) == 0 {
  468. T_end_date = now.Format("2006-01")
  469. } else {
  470. T_end_date_t, _ := lib.DateStrToTime(T_end_date)
  471. T_end_date = T_end_date_t.Format("2006-01")
  472. }
  473. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  474. StockMonthDao := Stock.NewStockMonth(orm.NewOrm())
  475. R_List := StockMonthDao.Read_StockMonth_List(T_depot_id, T_product_id, 0, T_start_date, T_end_date)
  476. for i := 0; i < len(R_List); i++ {
  477. R_List[i].T_project = strings.Join(StockOutDao.Read_StockOut_T_project(T_depot_id, R_List[i].T_product_id, R_List[i].T_month), "|")
  478. }
  479. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: R_List}
  480. c.ServeJSON()
  481. return
  482. }
  483. func (c *StockController) Stock_Detail_Excel() {
  484. // 查询
  485. T_depot_id, _ := c.GetInt("T_depot_id")
  486. T_product_id, _ := c.GetInt("T_product_id")
  487. // 查询
  488. T_start_date := c.GetString("T_start_date")
  489. T_end_date := c.GetString("T_end_date")
  490. now := time.Now()
  491. if len(T_start_date) == 0 {
  492. T_start_date = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local).Format("2006-01")
  493. } else {
  494. T_start_date_t, _ := lib.DateStrToTime(T_start_date)
  495. T_start_date = T_start_date_t.Format("2006-01")
  496. }
  497. if len(T_end_date) == 0 {
  498. T_end_date = now.Format("2006-01")
  499. } else {
  500. T_end_date_t, _ := lib.DateStrToTime(T_end_date)
  501. T_end_date = T_end_date_t.Format("2006-01")
  502. }
  503. var class_List []Basic.ProductClass_R
  504. filename := fmt.Sprintf("进销存(%s)", lib.GetRandstring(6, "0123456789", 0))
  505. if T_product_id > 0 {
  506. product, _ := Basic.Read_Product_ById(T_product_id)
  507. filename = fmt.Sprintf("进销存-%s明细(%s)", product.T_name, lib.GetRandstring(6, "0123456789", 0))
  508. class, _ := Basic.Read_ProductClass_ById(product.T_class)
  509. class_List = append(class_List, Basic.ProductClassToProductClass_R(class))
  510. } else {
  511. class_List, _ = Basic.Read_ProductClass_List("", 0, 0, 9999)
  512. }
  513. f := excelize.NewFile() // 设置单元格的值
  514. Style1, _ := f.NewStyle(
  515. &excelize.Style{
  516. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  517. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  518. })
  519. Style2, _ := f.NewStyle(
  520. &excelize.Style{
  521. Font: &excelize.Font{Bold: true, Size: 10, Family: "宋体"},
  522. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  523. Border: []excelize.Border{
  524. {Type: "left", Color: "000000", Style: 1},
  525. {Type: "top", Color: "000000", Style: 1},
  526. {Type: "bottom", Color: "000000", Style: 1},
  527. {Type: "right", Color: "000000", Style: 1},
  528. },
  529. })
  530. Style3, _ := f.NewStyle(
  531. &excelize.Style{
  532. Font: &excelize.Font{Bold: true, Size: 10, Family: "宋体", Color: "FF0000"},
  533. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  534. Border: []excelize.Border{
  535. {Type: "left", Color: "000000", Style: 1},
  536. {Type: "top", Color: "000000", Style: 1},
  537. {Type: "bottom", Color: "000000", Style: 1},
  538. {Type: "right", Color: "000000", Style: 1},
  539. },
  540. })
  541. Style4, _ := f.NewStyle(
  542. &excelize.Style{
  543. Font: &excelize.Font{Size: 10, Family: "宋体"},
  544. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  545. Border: []excelize.Border{
  546. {Type: "left", Color: "000000", Style: 1},
  547. {Type: "top", Color: "000000", Style: 1},
  548. {Type: "bottom", Color: "000000", Style: 1},
  549. {Type: "right", Color: "000000", Style: 1},
  550. },
  551. })
  552. StockMonthDao := Stock.NewStockMonth(orm.NewOrm())
  553. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  554. var j = 0
  555. for _, r := range class_List {
  556. StockList := StockMonthDao.Read_StockMonth_List(T_depot_id, T_product_id, r.Id, T_start_date, T_end_date)
  557. if len(StockList) == 0 {
  558. continue
  559. }
  560. if j == 0 {
  561. f.SetSheetName("Sheet1", r.T_name)
  562. } else {
  563. f.NewSheet(r.T_name)
  564. }
  565. j += 1
  566. f.MergeCell(r.T_name, "A1", "K1")
  567. f.SetRowStyle(r.T_name, 1, 1, Style1)
  568. f.SetCellValue(r.T_name, "A1", fmt.Sprintf("宝智达科技产品进销存统计表"))
  569. f.SetRowHeight(r.T_name, 1, 30)
  570. // 这里设置表头
  571. f.SetCellStyle(r.T_name, "A2", "K2", Style2)
  572. f.SetRowHeight(r.T_name, 2, 20)
  573. f.SetCellValue(r.T_name, "A2", "序号")
  574. f.SetCellValue(r.T_name, "B2", "产品名称")
  575. f.SetCellValue(r.T_name, "C2", "型号")
  576. f.SetCellValue(r.T_name, "D2", "规格")
  577. f.SetCellValue(r.T_name, "E2", "月份")
  578. f.SetCellValue(r.T_name, "F2", "期初库存")
  579. f.SetCellValue(r.T_name, "G2", "当月入库(正常生产新购入库产品)")
  580. f.SetCellValue(r.T_name, "H2", "退货入库(退还重新入库产品)")
  581. f.SetCellValue(r.T_name, "I2", "当月出库")
  582. f.SetCellValue(r.T_name, "J2", "期末库存")
  583. f.SetCellValue(r.T_name, "K2", "出库项目")
  584. //f.SetCellValue(r.T_name, "K2", "备注")
  585. // 设置列宽
  586. f.SetColWidth(r.T_name, "A", "A", 10)
  587. f.SetColWidth(r.T_name, "B", "B", 10)
  588. f.SetColWidth(r.T_name, "C", "D", 10)
  589. f.SetColWidth(r.T_name, "D", "D", 10)
  590. f.SetColWidth(r.T_name, "E", "E", 10)
  591. f.SetColWidth(r.T_name, "F", "F", 10)
  592. f.SetColWidth(r.T_name, "G", "G", 10)
  593. f.SetColWidth(r.T_name, "H", "H", 10)
  594. f.SetColWidth(r.T_name, "I", "I", 10)
  595. f.SetColWidth(r.T_name, "J", "J", 10)
  596. f.SetColWidth(r.T_name, "K", "K", 70)
  597. //f.SetColWidth(r.T_name, "K", "K", 10)
  598. line := 2
  599. // 循环写入数据
  600. for _, v := range StockList {
  601. line++
  602. product, _ := Basic.Read_Product_ById(v.T_product_id)
  603. f.SetCellValue(r.T_name, fmt.Sprintf("A%d", line), line-2)
  604. f.SetCellValue(r.T_name, fmt.Sprintf("B%d", line), product.T_name)
  605. f.SetCellValue(r.T_name, fmt.Sprintf("C%d", line), product.T_model)
  606. f.SetCellValue(r.T_name, fmt.Sprintf("D%d", line), product.T_spec)
  607. f.SetCellValue(r.T_name, fmt.Sprintf("E%d", line), v.T_month)
  608. f.SetCellValue(r.T_name, fmt.Sprintf("F%d", line), v.T_beginning)
  609. f.SetCellValue(r.T_name, fmt.Sprintf("G%d", line), v.T_in)
  610. f.SetCellValue(r.T_name, fmt.Sprintf("H%d", line), v.T_return)
  611. f.SetCellValue(r.T_name, fmt.Sprintf("I%d", line), v.T_out)
  612. f.SetCellValue(r.T_name, fmt.Sprintf("J%d", line), v.T_ending)
  613. if product.T_early_warning > v.T_ending {
  614. f.SetCellStyle(r.T_name, fmt.Sprintf("J%d", line), fmt.Sprintf("J%d", line), Style3)
  615. } else {
  616. f.SetCellStyle(r.T_name, fmt.Sprintf("J%d", line), fmt.Sprintf("J%d", line), Style4)
  617. }
  618. T_project := StockOutDao.Read_StockOut_T_project(T_depot_id, product.Id, v.T_month)
  619. //f.SetCellValue(r.T_name, fmt.Sprintf("J%d", line), v.T_project)
  620. f.SetCellValue(r.T_name, fmt.Sprintf("K%d", line), strings.Join(T_project, "\r\n"))
  621. }
  622. f.SetCellStyle(r.T_name, "A2", fmt.Sprintf("I%d", line), Style4)
  623. Style5, _ := f.NewStyle(
  624. &excelize.Style{
  625. Font: &excelize.Font{Size: 10, Family: "宋体"},
  626. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  627. Border: []excelize.Border{
  628. {Type: "left", Color: "000000", Style: 1},
  629. {Type: "top", Color: "000000", Style: 1},
  630. {Type: "bottom", Color: "000000", Style: 1},
  631. {Type: "right", Color: "000000", Style: 1},
  632. },
  633. })
  634. f.SetCellStyle(r.T_name, "K2", fmt.Sprintf("K%d", line), Style5)
  635. }
  636. // 保存文件
  637. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  638. fmt.Println(err)
  639. }
  640. var url string
  641. //// 上传 OSS
  642. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  643. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  644. if !is {
  645. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  646. c.ServeJSON()
  647. return
  648. }
  649. //删除目录
  650. err := os.Remove("ofile/" + filename + ".xlsx")
  651. if err != nil {
  652. logs.Error(lib.FuncName(), err)
  653. }
  654. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  655. c.ServeJSON()
  656. return
  657. }
  658. func (c *StockController) StockIn_List() {
  659. // 分页参数 初始化
  660. page, _ := c.GetInt("page")
  661. if page < 1 {
  662. page = 1
  663. }
  664. page_z, _ := c.GetInt("page_z")
  665. if page_z < 1 {
  666. page_z = conf.Page_size
  667. }
  668. // 查询
  669. T_depot_id, _ := c.GetInt("T_depot_id")
  670. T_start_date := c.GetString("T_start_date")
  671. T_end_date := c.GetString("T_end_date")
  672. T_name := c.GetString("T_name")
  673. T_batch_number := c.GetString("T_batch_number")
  674. userList, _ := NatsServer.Read_User_List_All()
  675. Account.Read_User_All_Map(userList)
  676. Basic.Read_Depot_All_Map()
  677. StockInDao := Stock.NewStockIn(orm.NewOrm())
  678. R_List, R_cnt := StockInDao.Read_StockIn_List(T_batch_number, T_name, T_depot_id, T_start_date, T_end_date, page, page_z)
  679. var r_jsons lib.R_JSONS
  680. r_jsons.Num = R_cnt
  681. r_jsons.Data = R_List
  682. r_jsons.Page = page
  683. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  684. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  685. c.ServeJSON()
  686. return
  687. }
  688. func (c *StockController) StockIn_List_Product() {
  689. // 分页参数 初始化
  690. page, _ := c.GetInt("page")
  691. if page < 1 {
  692. page = 1
  693. }
  694. page_z, _ := c.GetInt("page_z")
  695. if page_z < 1 {
  696. page_z = conf.Page_size
  697. }
  698. // 查询
  699. T_depot_id, _ := c.GetInt("T_depot_id")
  700. T_name := c.GetString("T_name")
  701. T_start_date := c.GetString("T_start_date")
  702. T_end_date := c.GetString("T_end_date")
  703. T_product_name := c.GetString("T_product_name") // 产品名称
  704. T_product_model := c.GetString("T_product_model") // 产品型号
  705. userList, _ := NatsServer.Read_User_List_All()
  706. Account.Read_User_All_Map(userList)
  707. Basic.Read_Depot_All_Map()
  708. StockInDao := Stock.NewStockIn(orm.NewOrm())
  709. R_List, R_cnt := StockInDao.Read_StockInProduct_List(T_name, T_start_date, T_end_date, T_depot_id, T_product_name, T_product_model, page, page_z)
  710. var r_jsons lib.R_JSONS
  711. r_jsons.Num = R_cnt
  712. r_jsons.Data = R_List
  713. r_jsons.Page = page
  714. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  715. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  716. c.ServeJSON()
  717. return
  718. }
  719. func (c *StockController) StockIn_List_Product_Excel() {
  720. // 查询
  721. T_depot_id, _ := c.GetInt("T_depot_id")
  722. T_name := c.GetString("T_name")
  723. T_start_date := c.GetString("T_start_date")
  724. T_end_date := c.GetString("T_end_date")
  725. T_product_name := c.GetString("T_product_name") // 产品名称
  726. T_product_model := c.GetString("T_product_model") // 产品型号
  727. userList, _ := NatsServer.Read_User_List_All()
  728. Account.Read_User_All_Map(userList)
  729. Basic.Read_Depot_All_Map()
  730. StockInDao := Stock.NewStockIn(orm.NewOrm())
  731. R_List, _ := StockInDao.Read_StockInProduct_List(T_name, T_start_date, T_end_date, T_depot_id, T_product_name, T_product_model, 0, 9999)
  732. filename := fmt.Sprintf("入库明细单(%s)", lib.GetRandstring(6, "0123456789", 0))
  733. f := excelize.NewFile() // 设置单元格的值
  734. Style1, _ := f.NewStyle(
  735. &excelize.Style{
  736. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  737. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  738. })
  739. Style2, _ := f.NewStyle(
  740. &excelize.Style{
  741. Font: &excelize.Font{Bold: true, Size: 11, Family: "宋体"},
  742. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  743. Border: []excelize.Border{
  744. {Type: "left", Color: "000000", Style: 1},
  745. {Type: "top", Color: "000000", Style: 1},
  746. {Type: "bottom", Color: "000000", Style: 1},
  747. {Type: "right", Color: "000000", Style: 1},
  748. },
  749. })
  750. Style3, _ := f.NewStyle(
  751. &excelize.Style{
  752. Font: &excelize.Font{Size: 11, Family: "宋体"},
  753. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  754. Border: []excelize.Border{
  755. {Type: "left", Color: "000000", Style: 1},
  756. {Type: "top", Color: "000000", Style: 1},
  757. {Type: "bottom", Color: "000000", Style: 1},
  758. {Type: "right", Color: "000000", Style: 1},
  759. },
  760. })
  761. f.MergeCell("Sheet1", "A1", "L1")
  762. f.SetRowStyle("Sheet1", 1, 1, Style1)
  763. f.SetCellValue("Sheet1", "A1", fmt.Sprintf("入库明细单"))
  764. f.SetRowHeight("Sheet1", 1, 30)
  765. // 这里设置表头
  766. f.SetCellStyle("Sheet1", "A2", "L2", Style2)
  767. f.SetRowHeight("Sheet1", 2, 20)
  768. f.SetCellValue("Sheet1", "A2", "序号")
  769. f.SetCellValue("Sheet1", "B2", "入库单号")
  770. f.SetCellValue("Sheet1", "C2", "经办人")
  771. f.SetCellValue("Sheet1", "D2", "入库日期")
  772. f.SetCellValue("Sheet1", "E2", "入库仓库")
  773. f.SetCellValue("Sheet1", "F2", "入库类型")
  774. f.SetCellValue("Sheet1", "G2", "产品名称")
  775. f.SetCellValue("Sheet1", "H2", "产品型号")
  776. f.SetCellValue("Sheet1", "I2", "数量")
  777. f.SetCellValue("Sheet1", "J2", "是否关联SN")
  778. f.SetCellValue("Sheet1", "K2", "备注")
  779. f.SetCellValue("Sheet1", "L2", "SN")
  780. // 设置列宽
  781. f.SetColWidth("Sheet1", "A", "A", 10)
  782. f.SetColWidth("Sheet1", "B", "B", 15)
  783. f.SetColWidth("Sheet1", "C", "D", 10)
  784. f.SetColWidth("Sheet1", "D", "D", 10)
  785. f.SetColWidth("Sheet1", "E", "E", 10)
  786. f.SetColWidth("Sheet1", "F", "F", 10)
  787. f.SetColWidth("Sheet1", "G", "G", 20)
  788. f.SetColWidth("Sheet1", "H", "H", 15)
  789. f.SetColWidth("Sheet1", "I", "I", 10)
  790. f.SetColWidth("Sheet1", "J", "J", 12)
  791. f.SetColWidth("Sheet1", "K", "K", 30)
  792. f.SetColWidth("Sheet1", "L", "L", 100)
  793. line := 2
  794. for _, product := range R_List {
  795. line += 1
  796. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-2)
  797. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), product.T_number)
  798. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), product.T_submit_name)
  799. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), product.T_date)
  800. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), product.T_depot_name)
  801. var type_str string
  802. if product.T_type == 1 {
  803. type_str = "入库"
  804. } else {
  805. type_str = "退库"
  806. }
  807. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), type_str)
  808. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), product.T_product_name)
  809. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), product.T_product_model)
  810. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), product.T_num)
  811. var relation_sn string
  812. if product.T_product_relation_sn == 1 {
  813. relation_sn = "是"
  814. } else {
  815. relation_sn = "否"
  816. }
  817. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), relation_sn)
  818. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), product.T_remark)
  819. f.SetCellValue("Sheet1", fmt.Sprintf("L%d", line), product.T_relation_sn)
  820. }
  821. f.SetCellStyle("Sheet1", "A3", fmt.Sprintf("L%d", line), Style3)
  822. // 保存文件
  823. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  824. fmt.Println(err)
  825. }
  826. var url string
  827. //// 上传 OSS
  828. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  829. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  830. if !is {
  831. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  832. c.ServeJSON()
  833. return
  834. }
  835. //删除目录
  836. err := os.Remove("ofile/" + filename + ".xlsx")
  837. if err != nil {
  838. logs.Error(lib.FuncName(), err)
  839. }
  840. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  841. c.ServeJSON()
  842. return
  843. }
  844. func (c *StockController) StockIn_Get() {
  845. // 查询
  846. T_number := c.GetString("T_number")
  847. o := orm.NewOrm()
  848. StockInDao := Stock.NewStockIn(o)
  849. StockInProductDao := Stock.NewStockInProduct(o)
  850. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  851. if err != nil {
  852. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  853. c.ServeJSON()
  854. return
  855. }
  856. productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  857. var pList []Stock.StockInProduct_R
  858. for _, v := range productList {
  859. pList = append(pList, Stock.StockInProductToStockInProduct_R(v))
  860. }
  861. userList, _ := NatsServer.Read_User_List_All()
  862. Account.Read_User_All_Map(userList)
  863. Basic.Read_Depot_All_Map()
  864. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Stock.StockInToStockIn_Detail(stockIn, pList)}
  865. c.ServeJSON()
  866. return
  867. }
  868. func (c *StockController) StockIn_generate_number() {
  869. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  870. rand_x := 0
  871. T_number := ""
  872. for true {
  873. T_number = "RK-" + lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x))
  874. _, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  875. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  876. break
  877. }
  878. rand_x += 1
  879. }
  880. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  881. c.ServeJSON()
  882. return
  883. }
  884. func (c *StockController) StockIn_Add() {
  885. o := orm.NewOrm()
  886. o.Begin()
  887. StockInDao := Stock.NewStockIn(o)
  888. T_number := c.GetString("T_number")
  889. // 检查前端传入的T_number是否已提交过
  890. _, err := StockInDao.Read_StockIn_ByT_number(T_number)
  891. if err != nil && err.Error() != orm.ErrNoRows.Error() {
  892. // 数据库查询错误
  893. o.Rollback()
  894. c.Data["json"] = lib.JSONS{Code: 203, Msg: "查询入库编号失败"}
  895. c.ServeJSON()
  896. return
  897. }
  898. if err == nil {
  899. // 入库编号已存在,不需要做任何处理
  900. o.Rollback()
  901. c.Data["json"] = lib.JSONS{Code: 200, Msg: "入库编号已存在,无需重复提交!", Data: T_number}
  902. c.ServeJSON()
  903. return
  904. }
  905. T_depot_id, _ := c.GetInt("T_depot_id")
  906. T_type, _ := c.GetInt("T_type") // 1-入库 2-退库
  907. T_date := c.GetString("T_date")
  908. T_product := c.GetString("T_product")
  909. T_remark := c.GetString("T_remark")
  910. T_project := c.GetString("T_project")
  911. T_return_user := c.GetString("T_return_user")
  912. T_batch_number := c.GetString("T_batch_number")
  913. T_company_name := c.GetString("T_company_name")
  914. date, is := lib.DateStrToTime(T_date)
  915. if !is {
  916. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  917. c.ServeJSON()
  918. return
  919. }
  920. if T_type == 2 && len(T_project) == 0 {
  921. c.Data["json"] = lib.JSONS{Code: 202, Msg: "退库时,项目不能为空!"}
  922. c.ServeJSON()
  923. return
  924. }
  925. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "入库", T_product)
  926. var_ := Stock.StockIn{
  927. T_number: T_number,
  928. T_depot_id: T_depot_id,
  929. T_type: T_type,
  930. T_date: T_date,
  931. T_remark: T_remark,
  932. T_submit: c.User.T_uuid,
  933. T_project: T_project,
  934. T_return_user: T_return_user,
  935. T_batch_number: T_batch_number,
  936. T_state: Stock.StockInWarehouse,
  937. T_warehouse: c.User.T_uuid,
  938. T_company_name: T_company_name,
  939. }
  940. StockInProductDao := Stock.NewStockInProduct(o)
  941. StockDao := Stock.NewStock(o)
  942. DeviceDao := Stock.NewDevice(o)
  943. _, err = StockInDao.Add_StockIn(var_)
  944. if err != nil {
  945. o.Rollback()
  946. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  947. c.ServeJSON()
  948. return
  949. }
  950. if len(T_product) == 0 {
  951. o.Rollback()
  952. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  953. c.ServeJSON()
  954. return
  955. }
  956. productList := lib.SplitString(T_product, "|")
  957. allProductList := []int{}
  958. for _, v := range productList {
  959. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  960. allProductList = append(allProductList, product_id)
  961. product, _ := Basic.Read_Product_ById(product_id)
  962. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  963. T_relation_sn := strings.Split(v, "-")[2]
  964. if T_relation_sn == "" && product.T_relation_sn == 1 {
  965. o.Rollback()
  966. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  967. c.ServeJSON()
  968. return
  969. }
  970. if len(T_relation_sn) > 0 {
  971. snList := strings.Split(strings.Trim(T_relation_sn, ","), ",")
  972. num = len(snList)
  973. for _, sn := range snList {
  974. mqtt := Stock.Read_MqttUser(sn)
  975. // 添加设备
  976. device := Stock.Device{
  977. T_contract_number: "",
  978. T_out_number: "",
  979. T_product_id: product_id,
  980. T_in_number: T_number,
  981. T_sn: sn,
  982. T_iccid: mqtt.Iccid,
  983. T_imei: mqtt.Imei,
  984. T_State: 2,
  985. T_batch_number: T_batch_number,
  986. CreateTime: date,
  987. }
  988. _, err = DeviceDao.AddOrUpdate_Device(device, 2)
  989. if err != nil {
  990. o.Rollback()
  991. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  992. c.ServeJSON()
  993. return
  994. }
  995. }
  996. }
  997. stockInProduct := Stock.StockInProduct{
  998. T_number: T_number,
  999. T_product_id: product_id,
  1000. T_depot_id: T_depot_id,
  1001. T_num: num, // 入库数量
  1002. T_date: T_date, // 入库日期
  1003. T_relation_sn: T_relation_sn,
  1004. T_batch_number: T_batch_number,
  1005. }
  1006. _, err = StockInProductDao.Add_StockInProduct(stockInProduct)
  1007. if err != nil {
  1008. o.Rollback()
  1009. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  1010. c.ServeJSON()
  1011. return
  1012. }
  1013. _, err = StockDao.AddOrUpdate_Stock(T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 2)
  1014. if err != nil {
  1015. o.Rollback()
  1016. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  1017. c.ServeJSON()
  1018. return
  1019. }
  1020. }
  1021. // 批量更新所有相关设备的批次号
  1022. err = DeviceDao.Update_Device_BatchNumber_ByInNumber(T_number, T_batch_number)
  1023. if err != nil {
  1024. o.Rollback()
  1025. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新设备批次号失败"}
  1026. c.ServeJSON()
  1027. return
  1028. }
  1029. o.Commit()
  1030. StockIn_Edit_StockMonth(T_date, T_depot_id, allProductList)
  1031. NatsServer.AddUserLogs(c.User.T_uuid, "入库", "入库", var_)
  1032. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1033. c.ServeJSON()
  1034. return
  1035. }
  1036. func StockIn_Edit_StockMonth(T_date string, T_depot_id int, allProductList []int) error {
  1037. if len(T_date) == 0 {
  1038. return errors.New("日期不能为空")
  1039. }
  1040. date, _ := lib.DateStrToTime(T_date)
  1041. //if date.Year() == time.Now().Year() && date.Month() == time.Now().Month() {
  1042. // // 修改本月数据不同步库存
  1043. // return nil
  1044. //}
  1045. //T_month := date.Format("2006-01")
  1046. months := generateMonthList(date, "in")
  1047. o := orm.NewOrm()
  1048. StockOutDao := Stock.NewStockOut(o)
  1049. StockMonthDao := Stock.NewStockMonth(o)
  1050. StockOutProductDao := Stock.NewStockOutProduct(o)
  1051. StockInProductDao := Stock.NewStockInProduct(o)
  1052. for _, T_product_id := range allProductList {
  1053. for _, T_month := range months {
  1054. // 获取当前产品本月出入库数量
  1055. lastMonth := StockMonthDao.Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  1056. stockMonth := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  1057. if stockMonth.Id > 0 {
  1058. // 修改
  1059. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(T_depot_id, T_product_id, T_month)
  1060. stockMonth.T_return = StockInProductDao.Read_StockReturn_Total(T_depot_id, T_product_id, T_month)
  1061. if len(lastMonth) > 0 {
  1062. stockMonth.T_beginning = lastMonth[0].T_ending
  1063. } else {
  1064. stockMonth.T_beginning = 0
  1065. }
  1066. // 期末库存 = 期初库存+入库-出库
  1067. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  1068. err := StockMonthDao.Update_StockMonth(stockMonth, "T_in", "T_return", "T_beginning", "T_ending")
  1069. if err != nil {
  1070. return err
  1071. }
  1072. } else {
  1073. product, _ := Basic.Read_Product_ById(T_product_id)
  1074. // 添加库存记录
  1075. stockMonth = Stock.StockMonth{
  1076. T_depot_id: T_depot_id,
  1077. T_product_id: T_product_id,
  1078. T_product_class: product.T_class,
  1079. T_month: T_month,
  1080. }
  1081. // 获取当前产品本月出入库数量
  1082. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(T_depot_id, T_product_id, T_month)
  1083. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(T_depot_id, T_product_id, T_month)
  1084. stockMonth.T_return = StockInProductDao.Read_StockReturn_Total(T_depot_id, T_product_id, T_month)
  1085. if len(lastMonth) > 0 {
  1086. stockMonth.T_beginning = lastMonth[0].T_ending
  1087. } else {
  1088. stockMonth.T_beginning = 0
  1089. }
  1090. // 期末库存 = 期初库存+入库-出库
  1091. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  1092. stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(T_depot_id, T_product_id, T_month)
  1093. _, err := StockMonthDao.Add_StockMonth(stockMonth)
  1094. if err != nil {
  1095. return err
  1096. }
  1097. }
  1098. }
  1099. }
  1100. return nil
  1101. }
  1102. func (c *StockController) StockIn_Edit() {
  1103. T_number := c.GetString("T_number") // 入库单号
  1104. //T_depot_id, _ := c.GetInt("T_depot_id")
  1105. T_date := c.GetString("T_date")
  1106. T_product := c.GetString("T_product")
  1107. T_remark := c.GetString("T_remark")
  1108. T_project := c.GetString("T_project")
  1109. T_return_user := c.GetString("T_return_user")
  1110. T_batch_number := c.GetString("T_batch_number")
  1111. T_company_name := c.GetString("T_company_name")
  1112. date, is := lib.DateStrToTime(T_date)
  1113. if !is {
  1114. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  1115. c.ServeJSON()
  1116. return
  1117. }
  1118. if date.Year() != time.Now().Year() && date.Month() != time.Now().Month() {
  1119. T_date = ""
  1120. }
  1121. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "修改入库", T_product)
  1122. o := orm.NewOrm()
  1123. o.Begin()
  1124. StockInDao := Stock.NewStockIn(o)
  1125. StockInProductDao := Stock.NewStockInProduct(o)
  1126. StockDao := Stock.NewStock(o)
  1127. DeviceDao := Stock.NewDevice(o)
  1128. // 查询入库信息
  1129. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1130. if err != nil {
  1131. o.Rollback()
  1132. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1133. c.ServeJSON()
  1134. return
  1135. }
  1136. T_old_date := stockIn.T_date
  1137. // 查询入库产品信息
  1138. productOldList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1139. var oldProductList []dto.StockProduct
  1140. var newProductList []dto.StockProduct
  1141. allProductListMap := make(map[int]struct{})
  1142. productOldMap := map[int]dto.StockProduct{}
  1143. for _, product := range productOldList {
  1144. stockProduct := dto.StockProduct{
  1145. T_product_id: product.T_product_id,
  1146. T_num: product.T_num,
  1147. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  1148. }
  1149. oldProductList = append(oldProductList, stockProduct)
  1150. productOldMap[product.T_product_id] = stockProduct
  1151. if _, ok := allProductListMap[product.T_product_id]; !ok {
  1152. allProductListMap[product.T_product_id] = struct{}{}
  1153. }
  1154. }
  1155. productNewList := lib.SplitString(T_product, "|")
  1156. productNewMap := map[int]dto.StockProduct{}
  1157. for _, v := range productNewList {
  1158. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  1159. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  1160. T_relation_sn := strings.Split(v, "-")[2]
  1161. snList := lib.SplitString(T_relation_sn, ",")
  1162. if len(snList) > 0 {
  1163. num = len(snList)
  1164. }
  1165. stockProduct := dto.StockProduct{
  1166. T_product_id: product_id,
  1167. T_num: num,
  1168. T_relation_sn: snList,
  1169. }
  1170. newProductList = append(newProductList, stockProduct)
  1171. productNewMap[product_id] = stockProduct
  1172. if _, ok := allProductListMap[product_id]; !ok {
  1173. allProductListMap[product_id] = struct{}{}
  1174. }
  1175. }
  1176. // 判断产品列表信息是否相同
  1177. StockProductListIsEqual, needDelete, needAdd, needEdit, snDiff := dto.StockProductListsEqual(oldProductList, newProductList)
  1178. // 两次提交的数据相同,则不用修改
  1179. if !StockProductListIsEqual {
  1180. if len(needDelete) > 0 {
  1181. // 删除入库产品列表
  1182. for _, v := range needDelete {
  1183. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1184. // 删除入库产品列表
  1185. err = StockInProductDao.Delete_StockInProduct(stockIn.T_number, stockIn.T_depot_id, v.T_product_id)
  1186. if err != nil {
  1187. o.Rollback()
  1188. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  1189. c.ServeJSON()
  1190. return
  1191. }
  1192. if product.T_relation_sn == 1 {
  1193. // 删除该入库单关联sn
  1194. for _, sn := range v.T_relation_sn {
  1195. err = DeviceDao.Delete_Device_ByT_in_number(stockIn.T_number, sn)
  1196. if err != nil {
  1197. o.Rollback()
  1198. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库设备失败"}
  1199. c.ServeJSON()
  1200. return
  1201. }
  1202. }
  1203. }
  1204. // 减少库存
  1205. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  1206. if err != nil {
  1207. o.Rollback()
  1208. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  1209. c.ServeJSON()
  1210. return
  1211. }
  1212. }
  1213. }
  1214. if len(needAdd) > 0 {
  1215. // 新增入库产品列表
  1216. for _, v := range needAdd {
  1217. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1218. if product.T_relation_sn == 1 && len(v.T_relation_sn) == 0 {
  1219. o.Rollback()
  1220. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  1221. c.ServeJSON()
  1222. return
  1223. }
  1224. // 添加该入库单关联sn
  1225. for _, sn := range v.T_relation_sn {
  1226. mqtt := Stock.Read_MqttUser(sn)
  1227. // 添加设备
  1228. device := Stock.Device{
  1229. T_contract_number: "",
  1230. T_out_number: "",
  1231. T_product_id: v.T_product_id,
  1232. T_in_number: stockIn.T_number,
  1233. T_sn: sn,
  1234. T_iccid: mqtt.Iccid,
  1235. T_imei: mqtt.Imei,
  1236. T_State: 2,
  1237. T_batch_number: T_batch_number,
  1238. CreateTime: date,
  1239. UpdateTime: stockIn.UpdateTime,
  1240. }
  1241. _, err = DeviceDao.AddOrUpdate_Device(device, 2)
  1242. if err != nil {
  1243. o.Rollback()
  1244. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  1245. c.ServeJSON()
  1246. return
  1247. }
  1248. }
  1249. stockInProduct := Stock.StockInProduct{
  1250. T_number: T_number,
  1251. T_product_id: v.T_product_id,
  1252. T_depot_id: stockIn.T_depot_id,
  1253. T_num: v.T_num, // 入库数量
  1254. T_date: stockIn.T_date, // 入库日期
  1255. T_relation_sn: strings.Join(v.T_relation_sn, ","),
  1256. }
  1257. _, err = StockInProductDao.Add_StockInProduct(stockInProduct)
  1258. if err != nil {
  1259. o.Rollback()
  1260. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  1261. c.ServeJSON()
  1262. return
  1263. }
  1264. // 增加库存
  1265. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 2)
  1266. if err != nil {
  1267. o.Rollback()
  1268. c.Data["json"] = lib.JSONS{Code: 203, Msg: "增加库存失败"}
  1269. c.ServeJSON()
  1270. return
  1271. }
  1272. }
  1273. }
  1274. if len(needEdit) > 0 {
  1275. for _, v := range needEdit {
  1276. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1277. stockInProduct := Stock.StockInProduct{
  1278. T_number: stockIn.T_number,
  1279. T_product_id: v.T_product_id,
  1280. T_depot_id: stockIn.T_depot_id,
  1281. T_num: productNewMap[v.T_product_id].T_num, // 入库数量
  1282. T_date: stockIn.T_date, // 入库日期
  1283. }
  1284. // 入库数量比之前多,增加库存
  1285. var T_type int
  1286. var T_num int
  1287. if productNewMap[v.T_product_id].T_num > productOldMap[v.T_product_id].T_num {
  1288. // 增加库存
  1289. T_type = 2
  1290. T_num = productNewMap[v.T_product_id].T_num - productOldMap[v.T_product_id].T_num
  1291. } else {
  1292. // 减少库存
  1293. T_type = 1
  1294. T_num = productOldMap[v.T_product_id].T_num - productNewMap[v.T_product_id].T_num
  1295. }
  1296. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, T_num, T_type)
  1297. if err != nil {
  1298. o.Rollback()
  1299. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  1300. c.ServeJSON()
  1301. return
  1302. }
  1303. // 更新产品库存表
  1304. err = StockInProductDao.Update_StockInProduct(stockInProduct)
  1305. if err != nil {
  1306. o.Rollback()
  1307. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库产品信息失败"}
  1308. c.ServeJSON()
  1309. return
  1310. }
  1311. }
  1312. }
  1313. for _, diff := range snDiff {
  1314. product, _ := Basic.Read_Product_ById(diff.T_product_id)
  1315. // 删除减少的sn
  1316. if len(diff.T_delete_relation_sn) > 0 {
  1317. for _, sn := range diff.T_delete_relation_sn {
  1318. err = DeviceDao.Delete_Device_ByT_in_number(stockIn.T_number, sn)
  1319. if err != nil {
  1320. o.Rollback()
  1321. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  1322. c.ServeJSON()
  1323. return
  1324. }
  1325. }
  1326. // 减少库存
  1327. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, len(diff.T_delete_relation_sn), 1)
  1328. if err != nil {
  1329. o.Rollback()
  1330. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  1331. c.ServeJSON()
  1332. return
  1333. }
  1334. }
  1335. if len(diff.T_add_relation_sn) > 0 {
  1336. for _, sn := range diff.T_add_relation_sn {
  1337. mqtt := Stock.Read_MqttUser(sn)
  1338. // 添加设备
  1339. device := Stock.Device{
  1340. T_contract_number: "",
  1341. T_out_number: "",
  1342. T_product_id: diff.T_product_id,
  1343. T_in_number: stockIn.T_number,
  1344. T_sn: sn,
  1345. T_iccid: mqtt.Iccid,
  1346. T_imei: mqtt.Imei,
  1347. T_State: 2,
  1348. CreateTime: date,
  1349. UpdateTime: stockIn.UpdateTime,
  1350. }
  1351. _, err = DeviceDao.AddOrUpdate_Device(device, 2)
  1352. if err != nil {
  1353. o.Rollback()
  1354. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  1355. c.ServeJSON()
  1356. return
  1357. }
  1358. }
  1359. // 增加库存
  1360. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, len(diff.T_add_relation_sn), 2)
  1361. if err != nil {
  1362. o.Rollback()
  1363. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  1364. c.ServeJSON()
  1365. return
  1366. }
  1367. }
  1368. stockInProduct := Stock.StockInProduct{
  1369. T_number: stockIn.T_number,
  1370. T_product_id: diff.T_product_id,
  1371. T_depot_id: stockIn.T_depot_id,
  1372. T_num: productNewMap[diff.T_product_id].T_num, // 入库数量
  1373. T_date: stockIn.T_date, // 入库日期
  1374. T_relation_sn: strings.Join(productNewMap[diff.T_product_id].T_relation_sn, ","),
  1375. }
  1376. // 更新产品库存表
  1377. err = StockInProductDao.Update_StockInProduct(stockInProduct)
  1378. if err != nil {
  1379. o.Rollback()
  1380. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库产品信息失败"}
  1381. c.ServeJSON()
  1382. return
  1383. }
  1384. }
  1385. }
  1386. stockIn.T_remark = T_remark
  1387. if len(T_date) > 0 {
  1388. stockIn.T_date = T_date
  1389. }
  1390. if len(T_project) > 0 {
  1391. stockIn.T_project = T_project
  1392. }
  1393. if len(T_return_user) > 0 {
  1394. stockIn.T_return_user = T_return_user
  1395. }
  1396. if len(T_batch_number) > 0 {
  1397. stockIn.T_batch_number = T_batch_number
  1398. }
  1399. if len(T_company_name) > 0 {
  1400. stockIn.T_company_name = T_company_name
  1401. }
  1402. err = StockInDao.Update_StockIn(stockIn, "T_remark", "T_date", "T_project", "T_return_user", "T_batch_number", "T_company_name")
  1403. if err != nil {
  1404. o.Rollback()
  1405. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库失败"}
  1406. c.ServeJSON()
  1407. return
  1408. }
  1409. o.Commit()
  1410. if len(T_date) > 0 && T_old_date != T_date {
  1411. // 修改出库产品日期
  1412. StockInProductDao.Update_StockInProduct_T_date(stockIn.T_number, T_date)
  1413. DeviceDao.Update_Device_CreateTimeByT_in_number(stockIn.T_number, T_date)
  1414. }
  1415. // 修改出库产品日期
  1416. StockInProductDao.Update_StockInProduct_T_batch_number(stockIn.T_number, T_batch_number)
  1417. // 修改设备批次号
  1418. DeviceDao.Update_Device_BatchNumber_ByInNumber(stockIn.T_number, T_batch_number)
  1419. // 更新月份统计表
  1420. allProductList := []int{}
  1421. for i, _ := range allProductListMap {
  1422. allProductList = append(allProductList, i)
  1423. }
  1424. StockIn_Edit_StockMonth(stockIn.T_date, stockIn.T_depot_id, allProductList)
  1425. NatsServer.AddUserLogs(c.User.T_uuid, "入库", "修改", stockIn)
  1426. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1427. c.ServeJSON()
  1428. return
  1429. }
  1430. // 删除入库
  1431. func (c *StockController) StockIn_Del() {
  1432. T_number := c.GetString("T_number") // 入库单号
  1433. o := orm.NewOrm()
  1434. o.Begin()
  1435. StockInDao := Stock.NewStockIn(o)
  1436. StockInProductDao := Stock.NewStockInProduct(o)
  1437. StockDao := Stock.NewStock(o)
  1438. DeviceDao := Stock.NewDevice(o)
  1439. // 查询入库信息
  1440. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1441. if err != nil {
  1442. o.Rollback()
  1443. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1444. c.ServeJSON()
  1445. return
  1446. }
  1447. // 查询入库产品信息
  1448. productOldList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1449. var oldProductList []dto.StockProduct
  1450. allProductList := []int{}
  1451. for _, product := range productOldList {
  1452. oldProductList = append(oldProductList, dto.StockProduct{
  1453. T_product_id: product.T_product_id,
  1454. T_num: product.T_num,
  1455. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  1456. })
  1457. allProductList = append(allProductList, product.T_product_id)
  1458. }
  1459. // 删除入库产品列表
  1460. for _, v := range oldProductList {
  1461. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1462. // 删除入库产品列表
  1463. err = StockInProductDao.Delete_StockInProduct(stockIn.T_number, stockIn.T_depot_id, v.T_product_id)
  1464. if err != nil {
  1465. o.Rollback()
  1466. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  1467. c.ServeJSON()
  1468. return
  1469. }
  1470. if product.T_relation_sn == 1 {
  1471. // 删除该入库单关联sn
  1472. for _, sn := range v.T_relation_sn {
  1473. err = DeviceDao.Delete_Device_ByT_in_number(stockIn.T_number, sn)
  1474. if err != nil {
  1475. o.Rollback()
  1476. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库设备失败"}
  1477. c.ServeJSON()
  1478. return
  1479. }
  1480. }
  1481. }
  1482. // 减少库存
  1483. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  1484. if err != nil {
  1485. o.Rollback()
  1486. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  1487. c.ServeJSON()
  1488. return
  1489. }
  1490. }
  1491. err = StockInDao.Delete_StockIn(stockIn)
  1492. if err != nil {
  1493. o.Rollback()
  1494. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库失败"}
  1495. c.ServeJSON()
  1496. return
  1497. }
  1498. o.Commit()
  1499. // 更新月份统计表
  1500. StockIn_Edit_StockMonth(stockIn.T_date, stockIn.T_depot_id, allProductList)
  1501. NatsServer.AddUserLogs(c.User.T_uuid, "入库", "删除", stockIn)
  1502. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1503. c.ServeJSON()
  1504. return
  1505. }
  1506. // 导出出库单
  1507. func (c *StockController) StockIn_Excel() {
  1508. // 查询
  1509. T_number := c.GetString("T_number")
  1510. o := orm.NewOrm()
  1511. StockInDao := Stock.NewStockIn(o)
  1512. StockInProductDao := Stock.NewStockInProduct(o)
  1513. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1514. if err != nil {
  1515. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1516. c.ServeJSON()
  1517. return
  1518. }
  1519. productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1520. var pList []Stock.StockInProduct_R
  1521. for _, v := range productList {
  1522. pList = append(pList, Stock.StockInProductToStockInProduct_R(v))
  1523. }
  1524. userList, _ := NatsServer.Read_User_List_All()
  1525. Account.Read_User_All_Map(userList)
  1526. stockInDetail := Stock.StockInToStockIn_Detail(stockIn, pList)
  1527. T_date, _ := lib.DateStrToTime(stockIn.T_date)
  1528. filename := fmt.Sprintf("冷链产品入库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  1529. f := excelize.NewFile() // 设置单元格的值
  1530. Style1, _ := f.NewStyle(
  1531. &excelize.Style{
  1532. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  1533. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1534. })
  1535. Style2, _ := f.NewStyle(
  1536. &excelize.Style{
  1537. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1538. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  1539. })
  1540. Style3, _ := f.NewStyle(
  1541. &excelize.Style{
  1542. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  1543. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1544. Border: []excelize.Border{
  1545. {Type: "left", Color: "000000", Style: 1},
  1546. {Type: "top", Color: "000000", Style: 1},
  1547. {Type: "bottom", Color: "000000", Style: 1},
  1548. {Type: "right", Color: "000000", Style: 1},
  1549. },
  1550. })
  1551. Style4, _ := f.NewStyle(
  1552. &excelize.Style{
  1553. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1554. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1555. Border: []excelize.Border{
  1556. {Type: "left", Color: "000000", Style: 1},
  1557. {Type: "top", Color: "000000", Style: 1},
  1558. {Type: "bottom", Color: "000000", Style: 1},
  1559. {Type: "right", Color: "000000", Style: 1},
  1560. },
  1561. })
  1562. productList2 := lib.ChunkBy(productList, 10)
  1563. for i, products := range productList2 {
  1564. if len(products) < 10 {
  1565. products = append(products, make([]Stock.StockInProduct, 10-len(products))...)
  1566. }
  1567. row := i*17 + 1
  1568. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1569. f.SetRowStyle("Sheet1", row, row, Style1)
  1570. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  1571. f.SetRowHeight("Sheet1", row, 20)
  1572. row += 1
  1573. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1574. f.SetRowStyle("Sheet1", row, row, Style1)
  1575. title := "硬件入库单"
  1576. if len(productList2) > 1 {
  1577. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  1578. }
  1579. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  1580. f.SetRowHeight("Sheet1", row, 20)
  1581. row += 1
  1582. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1583. f.SetRowStyle("Sheet1", row, row, Style2)
  1584. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库单号:%s", stockIn.T_number))
  1585. f.SetRowHeight("Sheet1", row, 20)
  1586. row += 1
  1587. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1588. f.SetRowStyle("Sheet1", row, row, Style2)
  1589. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockIn.T_remark))
  1590. f.SetRowHeight("Sheet1", row, 20)
  1591. row += 1
  1592. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1593. f.SetRowStyle("Sheet1", row, row, Style2)
  1594. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库时间:%s", T_date.Format("2006年01月02日")))
  1595. f.SetRowHeight("Sheet1", row, 20)
  1596. // 这里设置表头
  1597. row += 1
  1598. f.SetRowHeight("Sheet1", row, 20)
  1599. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  1600. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  1601. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  1602. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  1603. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  1604. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  1605. // 设置列宽
  1606. f.SetColWidth("Sheet1", "A", "A", 5)
  1607. f.SetColWidth("Sheet1", "B", "B", 20)
  1608. f.SetColWidth("Sheet1", "C", "C", 12)
  1609. f.SetColWidth("Sheet1", "D", "D", 10)
  1610. f.SetColWidth("Sheet1", "E", "E", 10)
  1611. f.SetColWidth("Sheet1", "F", "F", 15)
  1612. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  1613. sRow := row
  1614. // 循环写入数据
  1615. for j, v := range products {
  1616. row += 1
  1617. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1618. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  1619. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  1620. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  1621. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  1622. if v.T_num > 0 {
  1623. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  1624. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockIn.T_remark)
  1625. }
  1626. }
  1627. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  1628. row += 1
  1629. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  1630. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  1631. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  1632. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("经办人:%s", stockInDetail.T_submit_name))
  1633. f.SetRowStyle("Sheet1", row, row, Style2)
  1634. f.SetRowHeight("Sheet1", row, 20)
  1635. }
  1636. // 保存文件
  1637. if err = f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  1638. fmt.Println(err)
  1639. }
  1640. var url string
  1641. //// 上传 OSS
  1642. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  1643. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  1644. if !is {
  1645. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1646. c.ServeJSON()
  1647. return
  1648. }
  1649. //删除目录
  1650. err = os.Remove("ofile/" + filename + ".xlsx")
  1651. if err != nil {
  1652. logs.Error(lib.FuncName(), err)
  1653. }
  1654. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1655. c.ServeJSON()
  1656. return
  1657. }
  1658. // 批量导出出库单
  1659. func (c *StockController) StockIn_Excel_Batch() {
  1660. // 查询
  1661. T_number_list := c.GetString("T_number_list")
  1662. o := orm.NewOrm()
  1663. StockInDao := Stock.NewStockIn(o)
  1664. StockInProductDao := Stock.NewStockInProduct(o)
  1665. list := strings.Split(strings.Trim(T_number_list, "|"), "|")
  1666. userList, _ := NatsServer.Read_User_List_All()
  1667. Account.Read_User_All_Map(userList)
  1668. filename := fmt.Sprintf("冷链产品入库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  1669. f := excelize.NewFile() // 设置单元格的值
  1670. Style1, _ := f.NewStyle(
  1671. &excelize.Style{
  1672. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  1673. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1674. })
  1675. Style2, _ := f.NewStyle(
  1676. &excelize.Style{
  1677. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1678. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  1679. })
  1680. Style3, _ := f.NewStyle(
  1681. &excelize.Style{
  1682. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  1683. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1684. Border: []excelize.Border{
  1685. {Type: "left", Color: "000000", Style: 1},
  1686. {Type: "top", Color: "000000", Style: 1},
  1687. {Type: "bottom", Color: "000000", Style: 1},
  1688. {Type: "right", Color: "000000", Style: 1},
  1689. },
  1690. })
  1691. Style4, _ := f.NewStyle(
  1692. &excelize.Style{
  1693. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1694. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1695. Border: []excelize.Border{
  1696. {Type: "left", Color: "000000", Style: 1},
  1697. {Type: "top", Color: "000000", Style: 1},
  1698. {Type: "bottom", Color: "000000", Style: 1},
  1699. {Type: "right", Color: "000000", Style: 1},
  1700. },
  1701. })
  1702. var num int
  1703. for _, T_number := range list {
  1704. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1705. if err != nil {
  1706. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1707. c.ServeJSON()
  1708. return
  1709. }
  1710. productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1711. var pList []Stock.StockInProduct_R
  1712. for _, v := range productList {
  1713. pList = append(pList, Stock.StockInProductToStockInProduct_R(v))
  1714. }
  1715. stockInDetail := Stock.StockInToStockIn_Detail(stockIn, pList)
  1716. T_date, _ := lib.DateStrToTime(stockIn.T_date)
  1717. productList2 := lib.ChunkBy(productList, 10)
  1718. for i, products := range productList2 {
  1719. if len(products) < 10 {
  1720. products = append(products, make([]Stock.StockInProduct, 10-len(products))...)
  1721. }
  1722. row := num*18 + 1
  1723. num += 1
  1724. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1725. f.SetRowStyle("Sheet1", row, row, Style1)
  1726. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  1727. f.SetRowHeight("Sheet1", row, 20)
  1728. row += 1
  1729. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1730. f.SetRowStyle("Sheet1", row, row, Style1)
  1731. title := "硬件入库单"
  1732. if len(productList2) > 1 {
  1733. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  1734. }
  1735. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  1736. f.SetRowHeight("Sheet1", row, 20)
  1737. row += 1
  1738. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1739. f.SetRowStyle("Sheet1", row, row, Style2)
  1740. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库单号:%s", stockIn.T_number))
  1741. f.SetRowHeight("Sheet1", row, 20)
  1742. row += 1
  1743. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1744. f.SetRowStyle("Sheet1", row, row, Style2)
  1745. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockIn.T_remark))
  1746. f.SetRowHeight("Sheet1", row, 20)
  1747. row += 1
  1748. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1749. f.SetRowStyle("Sheet1", row, row, Style2)
  1750. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库时间:%s", T_date.Format("2006年01月02日")))
  1751. f.SetRowHeight("Sheet1", row, 20)
  1752. // 这里设置表头
  1753. row += 1
  1754. f.SetRowHeight("Sheet1", row, 20)
  1755. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  1756. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  1757. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  1758. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  1759. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  1760. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  1761. // 设置列宽
  1762. f.SetColWidth("Sheet1", "A", "A", 5)
  1763. f.SetColWidth("Sheet1", "B", "B", 20)
  1764. f.SetColWidth("Sheet1", "C", "C", 12)
  1765. f.SetColWidth("Sheet1", "D", "D", 10)
  1766. f.SetColWidth("Sheet1", "E", "E", 10)
  1767. f.SetColWidth("Sheet1", "F", "F", 15)
  1768. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  1769. sRow := row
  1770. // 循环写入数据
  1771. for j, v := range products {
  1772. row += 1
  1773. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1774. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  1775. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  1776. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  1777. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  1778. if v.T_num > 0 {
  1779. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  1780. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockIn.T_remark)
  1781. }
  1782. }
  1783. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  1784. row += 1
  1785. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  1786. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  1787. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  1788. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("经办人:%s", stockInDetail.T_submit_name))
  1789. f.SetRowStyle("Sheet1", row, row, Style2)
  1790. f.SetRowHeight("Sheet1", row, 20)
  1791. // 空一行
  1792. row += 1
  1793. //f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1794. //f.SetRowStyle("Sheet1", row, row, Style1)
  1795. //f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "")
  1796. // 插入下一页分页符
  1797. f.InsertPageBreak("Sheet1", fmt.Sprintf("A%d", row))
  1798. }
  1799. }
  1800. // 保存文件
  1801. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  1802. fmt.Println(err)
  1803. }
  1804. var url string
  1805. //// 上传 OSS
  1806. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  1807. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  1808. if !is {
  1809. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1810. c.ServeJSON()
  1811. return
  1812. }
  1813. //删除目录
  1814. err := os.Remove("ofile/" + filename + ".xlsx")
  1815. if err != nil {
  1816. logs.Error(lib.FuncName(), err)
  1817. }
  1818. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1819. c.ServeJSON()
  1820. return
  1821. }
  1822. // 退库申请
  1823. func (c *StockController) StockIn_Apply_List() {
  1824. // 分页参数 初始化
  1825. page, _ := c.GetInt("page")
  1826. if page < 1 {
  1827. page = 1
  1828. }
  1829. page_z, _ := c.GetInt("page_z")
  1830. if page_z < 1 {
  1831. page_z = conf.Page_size
  1832. }
  1833. // 查询
  1834. T_depot_id, _ := c.GetInt("T_depot_id")
  1835. T_start_date := c.GetString("T_start_date")
  1836. T_end_date := c.GetString("T_end_date")
  1837. T_name := c.GetString("T_name")
  1838. T_state := c.GetString("T_state")
  1839. userList, _ := NatsServer.Read_User_List_All()
  1840. Account.Read_User_All_Map(userList)
  1841. Basic.Read_Depot_All_Map()
  1842. StockInDao := Stock.NewStockIn(orm.NewOrm())
  1843. R_List, R_cnt := StockInDao.Read_StockIn_Apply_List(Stock.StockInTypeApply, c.User.T_uuid, T_state, T_name, T_depot_id, T_start_date, T_end_date, page, page_z)
  1844. var r_jsons lib.R_JSONS
  1845. r_jsons.Num = R_cnt
  1846. r_jsons.Data = R_List
  1847. r_jsons.Page = page
  1848. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  1849. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1850. c.ServeJSON()
  1851. return
  1852. }
  1853. func (c *StockController) StockIn_Apply_Warehouse_List() {
  1854. // 分页参数 初始化
  1855. page, _ := c.GetInt("page")
  1856. if page < 1 {
  1857. page = 1
  1858. }
  1859. page_z, _ := c.GetInt("page_z")
  1860. if page_z < 1 {
  1861. page_z = conf.Page_size
  1862. }
  1863. // 查询
  1864. T_depot_id, _ := c.GetInt("T_depot_id")
  1865. T_start_date := c.GetString("T_start_date")
  1866. T_end_date := c.GetString("T_end_date")
  1867. T_name := c.GetString("T_name")
  1868. T_state := c.GetString("T_state")
  1869. T_uuid := c.GetString("T_uuid")
  1870. userList, _ := NatsServer.Read_User_List_All()
  1871. Account.Read_User_All_Map(userList)
  1872. Basic.Read_Depot_All_Map()
  1873. StockInDao := Stock.NewStockIn(orm.NewOrm())
  1874. R_List, R_cnt := StockInDao.Read_StockIn_Apply_List(Stock.StockInTypeWarehouse, T_uuid, T_state, T_name, T_depot_id, T_start_date, T_end_date, page, page_z)
  1875. var r_jsons lib.R_JSONS
  1876. r_jsons.Num = R_cnt
  1877. r_jsons.Data = R_List
  1878. r_jsons.Page = page
  1879. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  1880. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1881. c.ServeJSON()
  1882. return
  1883. }
  1884. func (c *StockController) StockIn_Apply() {
  1885. o := orm.NewOrm()
  1886. o.Begin()
  1887. StockInDao := Stock.NewStockIn(o)
  1888. T_number := c.GetString("T_number")
  1889. // 检查前端传入的T_number是否已提交过
  1890. _, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1891. if err != nil && err.Error() != orm.ErrNoRows.Error() {
  1892. // 数据库查询错误
  1893. o.Rollback()
  1894. c.Data["json"] = lib.JSONS{Code: 203, Msg: "查询入库编号失败"}
  1895. c.ServeJSON()
  1896. return
  1897. }
  1898. if err == nil {
  1899. // 入库编号已存在,不需要做任何处理
  1900. o.Rollback()
  1901. c.Data["json"] = lib.JSONS{Code: 200, Msg: "入库编号已存在,无需重复提交!", Data: T_number}
  1902. c.ServeJSON()
  1903. return
  1904. }
  1905. T_depot_id, _ := c.GetInt("T_depot_id")
  1906. T_product := c.GetString("T_product")
  1907. T_remark := c.GetString("T_remark")
  1908. T_project := c.GetString("T_project")
  1909. T_company_name := c.GetString("T_company_name")
  1910. T_return_user := c.GetString("T_return_user")
  1911. if len(T_return_user) == 0 {
  1912. T_return_user = c.User.T_uuid
  1913. }
  1914. if len(T_project) == 0 {
  1915. c.Data["json"] = lib.JSONS{Code: 202, Msg: "项目不能为空!"}
  1916. c.ServeJSON()
  1917. return
  1918. }
  1919. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "退库", T_product)
  1920. var_ := Stock.StockIn{
  1921. T_number: T_number,
  1922. T_depot_id: T_depot_id,
  1923. T_type: 2,
  1924. T_remark: T_remark,
  1925. T_submit: c.User.T_uuid,
  1926. T_project: T_project,
  1927. T_return_user: T_return_user,
  1928. T_company_name: T_company_name,
  1929. T_state: 1,
  1930. T_application_date: time.Now().Format("2006-01-02"),
  1931. }
  1932. StockInProductDao := Stock.NewStockInProduct(o)
  1933. _, err = StockInDao.Add_StockIn(var_)
  1934. if err != nil {
  1935. o.Rollback()
  1936. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  1937. c.ServeJSON()
  1938. return
  1939. }
  1940. if len(T_product) == 0 {
  1941. o.Rollback()
  1942. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  1943. c.ServeJSON()
  1944. return
  1945. }
  1946. productList := lib.SplitString(T_product, "|")
  1947. allProductList := []int{}
  1948. for _, v := range productList {
  1949. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  1950. allProductList = append(allProductList, product_id)
  1951. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  1952. stockInProduct := Stock.StockInProduct{
  1953. T_number: T_number,
  1954. T_product_id: product_id,
  1955. T_depot_id: T_depot_id,
  1956. T_num: num, // 入库数量
  1957. }
  1958. _, err = StockInProductDao.Add_StockInProduct(stockInProduct)
  1959. if err != nil {
  1960. o.Rollback()
  1961. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  1962. c.ServeJSON()
  1963. return
  1964. }
  1965. }
  1966. o.Commit()
  1967. NatsServer.AddUserLogs(c.User.T_uuid, "退库申请", "退库申请", var_)
  1968. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1969. c.ServeJSON()
  1970. return
  1971. }
  1972. func (c *StockController) StockIn_Apply_Edit() {
  1973. T_number := c.GetString("T_number") // 入库单号
  1974. //T_depot_id, _ := c.GetInt("T_depot_id")
  1975. T_product := c.GetString("T_product")
  1976. T_remark := c.GetString("T_remark")
  1977. T_project := c.GetString("T_project")
  1978. T_batch_number := c.GetString("T_batch_number")
  1979. T_company_name := c.GetString("T_company_name")
  1980. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "修改入库", T_product)
  1981. o := orm.NewOrm()
  1982. o.Begin()
  1983. StockInDao := Stock.NewStockIn(o)
  1984. StockInProductDao := Stock.NewStockInProduct(o)
  1985. // 查询入库信息
  1986. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1987. if err != nil {
  1988. o.Rollback()
  1989. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1990. c.ServeJSON()
  1991. return
  1992. }
  1993. // 查询入库产品信息
  1994. productOldList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1995. var oldProductList []dto.StockProduct
  1996. var newProductList []dto.StockProduct
  1997. allProductListMap := make(map[int]struct{})
  1998. productOldMap := map[int]dto.StockProduct{}
  1999. for _, product := range productOldList {
  2000. stockProduct := dto.StockProduct{
  2001. T_product_id: product.T_product_id,
  2002. T_num: product.T_num,
  2003. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  2004. }
  2005. oldProductList = append(oldProductList, stockProduct)
  2006. productOldMap[product.T_product_id] = stockProduct
  2007. if _, ok := allProductListMap[product.T_product_id]; !ok {
  2008. allProductListMap[product.T_product_id] = struct{}{}
  2009. }
  2010. }
  2011. productNewList := lib.SplitString(T_product, "|")
  2012. productNewMap := map[int]dto.StockProduct{}
  2013. for _, v := range productNewList {
  2014. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  2015. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  2016. T_relation_sn := strings.Split(v, "-")[2]
  2017. snList := lib.SplitString(T_relation_sn, ",")
  2018. if len(snList) > 0 {
  2019. num = len(snList)
  2020. }
  2021. stockProduct := dto.StockProduct{
  2022. T_product_id: product_id,
  2023. T_num: num,
  2024. T_relation_sn: snList,
  2025. }
  2026. newProductList = append(newProductList, stockProduct)
  2027. productNewMap[product_id] = stockProduct
  2028. if _, ok := allProductListMap[product_id]; !ok {
  2029. allProductListMap[product_id] = struct{}{}
  2030. }
  2031. }
  2032. // 判断产品列表信息是否相同
  2033. StockProductListIsEqual, needDelete, needAdd, needEdit, _ := dto.StockProductListsEqual(oldProductList, newProductList)
  2034. // 两次提交的数据相同,则不用修改
  2035. if !StockProductListIsEqual {
  2036. if len(needDelete) > 0 {
  2037. // 删除入库产品列表
  2038. for _, v := range needDelete {
  2039. // 删除入库产品列表
  2040. err = StockInProductDao.Delete_StockInProduct(stockIn.T_number, stockIn.T_depot_id, v.T_product_id)
  2041. if err != nil {
  2042. o.Rollback()
  2043. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  2044. c.ServeJSON()
  2045. return
  2046. }
  2047. }
  2048. }
  2049. if len(needAdd) > 0 {
  2050. // 新增入库产品列表
  2051. for _, v := range needAdd {
  2052. stockInProduct := Stock.StockInProduct{
  2053. T_number: T_number,
  2054. T_product_id: v.T_product_id,
  2055. T_depot_id: stockIn.T_depot_id,
  2056. T_num: v.T_num, // 入库数量
  2057. T_date: stockIn.T_date, // 入库日期
  2058. T_relation_sn: strings.Join(v.T_relation_sn, ","),
  2059. }
  2060. _, err = StockInProductDao.Add_StockInProduct(stockInProduct)
  2061. if err != nil {
  2062. o.Rollback()
  2063. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  2064. c.ServeJSON()
  2065. return
  2066. }
  2067. }
  2068. }
  2069. if len(needEdit) > 0 {
  2070. for _, v := range needEdit {
  2071. stockInProduct := Stock.StockInProduct{
  2072. T_number: stockIn.T_number,
  2073. T_product_id: v.T_product_id,
  2074. T_depot_id: stockIn.T_depot_id,
  2075. T_num: productNewMap[v.T_product_id].T_num, // 入库数量
  2076. T_date: stockIn.T_date, // 入库日期
  2077. }
  2078. // 更新产品库存表
  2079. err = StockInProductDao.Update_StockInProduct(stockInProduct)
  2080. if err != nil {
  2081. o.Rollback()
  2082. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库产品信息失败"}
  2083. c.ServeJSON()
  2084. return
  2085. }
  2086. }
  2087. }
  2088. }
  2089. stockIn.T_remark = T_remark
  2090. if len(T_project) > 0 {
  2091. stockIn.T_project = T_project
  2092. }
  2093. if len(T_batch_number) > 0 {
  2094. stockIn.T_batch_number = T_batch_number
  2095. }
  2096. if len(T_company_name) > 0 {
  2097. stockIn.T_company_name = T_company_name
  2098. }
  2099. err = StockInDao.Update_StockIn(stockIn, "T_remark", "T_project", "T_batch_number", "T_company_name")
  2100. if err != nil {
  2101. o.Rollback()
  2102. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库失败"}
  2103. c.ServeJSON()
  2104. return
  2105. }
  2106. o.Commit()
  2107. NatsServer.AddUserLogs(c.User.T_uuid, "退库", "修改", stockIn)
  2108. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2109. c.ServeJSON()
  2110. return
  2111. }
  2112. // 删除入库
  2113. func (c *StockController) StockIn_Apply_Del() {
  2114. T_number := c.GetString("T_number") // 入库单号
  2115. o := orm.NewOrm()
  2116. o.Begin()
  2117. StockInDao := Stock.NewStockIn(o)
  2118. StockInProductDao := Stock.NewStockInProduct(o)
  2119. // 查询入库信息
  2120. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  2121. if err != nil {
  2122. o.Rollback()
  2123. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2124. c.ServeJSON()
  2125. return
  2126. }
  2127. // 查询入库产品信息
  2128. productOldList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  2129. var oldProductList []dto.StockProduct
  2130. allProductList := []int{}
  2131. for _, product := range productOldList {
  2132. oldProductList = append(oldProductList, dto.StockProduct{
  2133. T_product_id: product.T_product_id,
  2134. T_num: product.T_num,
  2135. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  2136. })
  2137. allProductList = append(allProductList, product.T_product_id)
  2138. }
  2139. // 删除入库产品列表
  2140. for _, v := range oldProductList {
  2141. // 删除入库产品列表
  2142. err = StockInProductDao.Delete_StockInProduct(stockIn.T_number, stockIn.T_depot_id, v.T_product_id)
  2143. if err != nil {
  2144. o.Rollback()
  2145. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  2146. c.ServeJSON()
  2147. return
  2148. }
  2149. }
  2150. err = StockInDao.Delete_StockIn(stockIn)
  2151. if err != nil {
  2152. o.Rollback()
  2153. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库失败"}
  2154. c.ServeJSON()
  2155. return
  2156. }
  2157. o.Commit()
  2158. NatsServer.AddUserLogs(c.User.T_uuid, "入库申请", "删除", stockIn)
  2159. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2160. c.ServeJSON()
  2161. return
  2162. }
  2163. func (c *StockController) StockIn_Warehouse() {
  2164. T_number := c.GetString("T_number")
  2165. T_date := c.GetString("T_date")
  2166. T_remark := c.GetString("T_remark")
  2167. T_project := c.GetString("T_project")
  2168. T_product := c.GetString("T_product")
  2169. date, is := lib.DateStrToTime(T_date)
  2170. if !is {
  2171. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  2172. c.ServeJSON()
  2173. return
  2174. }
  2175. if len(T_project) == 0 {
  2176. c.Data["json"] = lib.JSONS{Code: 202, Msg: "项目不能为空!"}
  2177. c.ServeJSON()
  2178. return
  2179. }
  2180. NatsServer.AddUserLogs(c.User.T_uuid, "退库申请", "确认入库", T_product)
  2181. o := orm.NewOrm()
  2182. o.Begin()
  2183. StockInDao := Stock.NewStockIn(o)
  2184. StockInProductDao := Stock.NewStockInProduct(o)
  2185. StockDao := Stock.NewStock(o)
  2186. DeviceDao := Stock.NewDevice(o)
  2187. // 查询入库信息
  2188. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  2189. if err != nil {
  2190. o.Rollback()
  2191. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2192. c.ServeJSON()
  2193. return
  2194. }
  2195. productList := lib.SplitString(T_product, "|")
  2196. allProductList := []int{}
  2197. for _, v := range productList {
  2198. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  2199. allProductList = append(allProductList, product_id)
  2200. product, _ := Basic.Read_Product_ById(product_id)
  2201. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  2202. if num == 0 {
  2203. o.Rollback()
  2204. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s出库数量不能为0!", product.T_name)}
  2205. c.ServeJSON()
  2206. return
  2207. }
  2208. T_relation_sn := strings.Split(v, "-")[2]
  2209. if T_relation_sn == "" && product.T_relation_sn == 1 {
  2210. o.Rollback()
  2211. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  2212. c.ServeJSON()
  2213. return
  2214. }
  2215. if len(T_relation_sn) > 0 {
  2216. snList := strings.Split(T_relation_sn, ",")
  2217. if product.T_relation_sn == 1 && num != len(snList) {
  2218. o.Rollback()
  2219. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s SN数量与出库数量不一致,请核查!", product.T_name)}
  2220. c.ServeJSON()
  2221. return
  2222. }
  2223. if len(snList) > 0 {
  2224. for _, sn := range snList {
  2225. mqtt := Stock.Read_MqttUser(sn)
  2226. // 根据IMEI查询上次入库的批次号
  2227. batchNumber, err := DeviceDao.Read_Device_BatchNumber_ByImei(mqtt.Imei)
  2228. if err != nil {
  2229. logs.Error(lib.FuncName(), err)
  2230. }
  2231. device := Stock.Device{
  2232. T_product_id: product_id,
  2233. T_in_number: T_number,
  2234. T_sn: sn,
  2235. T_iccid: mqtt.Iccid,
  2236. T_imei: mqtt.Imei,
  2237. T_State: 2,
  2238. T_project: stockIn.T_project,
  2239. T_batch_number: batchNumber,
  2240. CreateTime: date,
  2241. }
  2242. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  2243. if err != nil {
  2244. o.Rollback()
  2245. c.Data["json"] = lib.JSONS{Code: 202, Msg: "出库失败"}
  2246. c.ServeJSON()
  2247. return
  2248. }
  2249. }
  2250. }
  2251. }
  2252. // 3、添加出库产品清单
  2253. stockInProduct := Stock.StockInProduct{
  2254. T_number: T_number,
  2255. T_product_id: product_id,
  2256. T_depot_id: stockIn.T_depot_id,
  2257. T_num: num, // 入库数量
  2258. T_date: T_date, // 入库日期
  2259. T_relation_sn: T_relation_sn,
  2260. T_state: 1,
  2261. }
  2262. // 更新产品表
  2263. err = StockInProductDao.Update_StockInProduct(stockInProduct)
  2264. if err != nil {
  2265. o.Rollback()
  2266. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  2267. c.ServeJSON()
  2268. return
  2269. }
  2270. // 5、更新产品库存列表、占用库存
  2271. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 2)
  2272. if err != nil {
  2273. o.Rollback()
  2274. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  2275. c.ServeJSON()
  2276. return
  2277. }
  2278. }
  2279. if len(T_remark) > 0 {
  2280. stockIn.T_remark = T_remark
  2281. }
  2282. if len(T_project) > 0 {
  2283. stockIn.T_project = T_project
  2284. }
  2285. stockIn.T_warehouse = c.User.T_uuid
  2286. stockIn.T_state = Stock.StockInWarehouse
  2287. stockIn.T_date = T_date
  2288. err = StockInDao.Update_StockIn(stockIn, "T_state", "T_date", "T_warehouse", "T_project", "T_remark")
  2289. if err != nil {
  2290. o.Rollback()
  2291. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库审批失败"}
  2292. c.ServeJSON()
  2293. return
  2294. }
  2295. o.Commit()
  2296. // 更新月份统计表
  2297. StockIn_Edit_StockMonth(stockIn.T_date, stockIn.T_depot_id, allProductList)
  2298. NatsServer.AddUserLogs(c.User.T_uuid, "入库申请", "确认出库", stockIn)
  2299. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2300. c.ServeJSON()
  2301. return
  2302. }
  2303. func (c *StockController) StockOut_List() {
  2304. // 分页参数 初始化
  2305. page, _ := c.GetInt("page")
  2306. if page < 1 {
  2307. page = 1
  2308. }
  2309. page_z, _ := c.GetInt("page_z")
  2310. if page_z < 1 {
  2311. page_z = conf.Page_size
  2312. }
  2313. // 查询
  2314. T_depot_id, _ := c.GetInt("T_depot_id")
  2315. T_contract_number := c.GetString("T_contract_number")
  2316. T_start_date := c.GetString("T_start_date")
  2317. T_end_date := c.GetString("T_end_date")
  2318. T_name := c.GetString("T_name")
  2319. userList, _ := NatsServer.Read_User_List_All()
  2320. Account.Read_User_All_Map(userList)
  2321. Basic.Read_Depot_All_Map()
  2322. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  2323. R_List, R_cnt := StockOutDao.Read_StockOut_List(T_name, T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  2324. var r_jsons lib.R_JSONS
  2325. r_jsons.Num = R_cnt
  2326. r_jsons.Data = R_List
  2327. r_jsons.Page = page
  2328. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  2329. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  2330. c.ServeJSON()
  2331. return
  2332. }
  2333. func (c *StockController) StockOut_List_Product() {
  2334. // 分页参数 初始化
  2335. page, _ := c.GetInt("page")
  2336. if page < 1 {
  2337. page = 1
  2338. }
  2339. page_z, _ := c.GetInt("page_z")
  2340. if page_z < 1 {
  2341. page_z = conf.Page_size
  2342. }
  2343. // 查询
  2344. T_depot_id, _ := c.GetInt("T_depot_id")
  2345. T_name := c.GetString("T_name")
  2346. T_start_date := c.GetString("T_start_date")
  2347. T_end_date := c.GetString("T_end_date")
  2348. T_product_name := c.GetString("T_product_name") // 产品名称
  2349. T_product_model := c.GetString("T_product_model") // 产品型号
  2350. userList, _ := NatsServer.Read_User_List_All()
  2351. Account.Read_User_All_Map(userList)
  2352. Basic.Read_Depot_All_Map()
  2353. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  2354. R_List, R_cnt := StockOutDao.Read_StockOutProduct_List(T_name, T_start_date, T_end_date, T_depot_id, T_product_name, T_product_model, page, page_z)
  2355. var r_jsons lib.R_JSONS
  2356. r_jsons.Num = R_cnt
  2357. r_jsons.Data = R_List
  2358. r_jsons.Page = page
  2359. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  2360. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  2361. c.ServeJSON()
  2362. return
  2363. }
  2364. func (c *StockController) StockOut_List_Product_Excel() {
  2365. // 查询
  2366. T_depot_id, _ := c.GetInt("T_depot_id")
  2367. T_name := c.GetString("T_name")
  2368. T_start_date := c.GetString("T_start_date")
  2369. T_end_date := c.GetString("T_end_date")
  2370. T_product_name := c.GetString("T_product_name") // 产品名称
  2371. T_product_model := c.GetString("T_product_model") // 产品型号
  2372. userList, _ := NatsServer.Read_User_List_All()
  2373. Account.Read_User_All_Map(userList)
  2374. Basic.Read_Depot_All_Map()
  2375. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  2376. R_List, _ := StockOutDao.Read_StockOutProduct_List(T_name, T_start_date, T_end_date, T_depot_id, T_product_name, T_product_model, 0, 9999)
  2377. filename := fmt.Sprintf("出库明细单(%s)", lib.GetRandstring(6, "0123456789", 0))
  2378. f := excelize.NewFile() // 设置单元格的值
  2379. Style1, _ := f.NewStyle(
  2380. &excelize.Style{
  2381. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  2382. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  2383. })
  2384. Style2, _ := f.NewStyle(
  2385. &excelize.Style{
  2386. Font: &excelize.Font{Bold: true, Size: 11, Family: "宋体"},
  2387. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  2388. Border: []excelize.Border{
  2389. {Type: "left", Color: "000000", Style: 1},
  2390. {Type: "top", Color: "000000", Style: 1},
  2391. {Type: "bottom", Color: "000000", Style: 1},
  2392. {Type: "right", Color: "000000", Style: 1},
  2393. },
  2394. })
  2395. Style3, _ := f.NewStyle(
  2396. &excelize.Style{
  2397. Font: &excelize.Font{Size: 11, Family: "宋体"},
  2398. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  2399. Border: []excelize.Border{
  2400. {Type: "left", Color: "000000", Style: 1},
  2401. {Type: "top", Color: "000000", Style: 1},
  2402. {Type: "bottom", Color: "000000", Style: 1},
  2403. {Type: "right", Color: "000000", Style: 1},
  2404. },
  2405. })
  2406. f.MergeCell("Sheet1", "A1", "L1")
  2407. f.SetRowStyle("Sheet1", 1, 1, Style1)
  2408. f.SetCellValue("Sheet1", "A1", fmt.Sprintf("出库明细单"))
  2409. f.SetRowHeight("Sheet1", 1, 30)
  2410. // 这里设置表头
  2411. f.SetCellStyle("Sheet1", "A2", "L2", Style2)
  2412. f.SetRowHeight("Sheet1", 2, 20)
  2413. f.SetCellValue("Sheet1", "A2", "序号")
  2414. f.SetCellValue("Sheet1", "B2", "出库单号")
  2415. f.SetCellValue("Sheet1", "C2", "领取人")
  2416. f.SetCellValue("Sheet1", "D2", "出库日期")
  2417. f.SetCellValue("Sheet1", "E2", "出库仓库")
  2418. f.SetCellValue("Sheet1", "F2", "关联项目")
  2419. f.SetCellValue("Sheet1", "G2", "产品名称")
  2420. f.SetCellValue("Sheet1", "H2", "产品型号")
  2421. f.SetCellValue("Sheet1", "I2", "数量")
  2422. f.SetCellValue("Sheet1", "J2", "是否关联SN")
  2423. f.SetCellValue("Sheet1", "K2", "备注")
  2424. f.SetCellValue("Sheet1", "L2", "SN")
  2425. // 设置列宽
  2426. f.SetColWidth("Sheet1", "A", "A", 10)
  2427. f.SetColWidth("Sheet1", "B", "B", 15)
  2428. f.SetColWidth("Sheet1", "C", "D", 10)
  2429. f.SetColWidth("Sheet1", "D", "D", 10)
  2430. f.SetColWidth("Sheet1", "E", "E", 10)
  2431. f.SetColWidth("Sheet1", "F", "F", 20)
  2432. f.SetColWidth("Sheet1", "G", "G", 20)
  2433. f.SetColWidth("Sheet1", "H", "H", 15)
  2434. f.SetColWidth("Sheet1", "I", "I", 10)
  2435. f.SetColWidth("Sheet1", "J", "J", 12)
  2436. f.SetColWidth("Sheet1", "K", "K", 30)
  2437. f.SetColWidth("Sheet1", "L", "L", 100)
  2438. line := 2
  2439. for _, product := range R_List {
  2440. line += 1
  2441. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-2)
  2442. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), product.T_number)
  2443. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), product.T_receive_name)
  2444. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), product.T_date)
  2445. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), product.T_depot_name)
  2446. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), product.T_project)
  2447. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), product.T_product_name)
  2448. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), product.T_product_model)
  2449. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), product.T_num)
  2450. var relation_sn string
  2451. if product.T_product_relation_sn == 1 {
  2452. relation_sn = "是"
  2453. } else {
  2454. relation_sn = "否"
  2455. }
  2456. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), relation_sn)
  2457. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), product.T_remark)
  2458. f.SetCellValue("Sheet1", fmt.Sprintf("L%d", line), product.T_relation_sn)
  2459. }
  2460. f.SetCellStyle("Sheet1", "A3", fmt.Sprintf("L%d", line), Style3)
  2461. // 保存文件
  2462. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  2463. fmt.Println(err)
  2464. }
  2465. var url string
  2466. //// 上传 OSS
  2467. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  2468. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  2469. if !is {
  2470. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  2471. c.ServeJSON()
  2472. return
  2473. }
  2474. //删除目录
  2475. err := os.Remove("ofile/" + filename + ".xlsx")
  2476. if err != nil {
  2477. logs.Error(lib.FuncName(), err)
  2478. }
  2479. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  2480. c.ServeJSON()
  2481. return
  2482. }
  2483. func (c *StockController) StockOut_Get() {
  2484. // 查询
  2485. T_number := c.GetString("T_number")
  2486. o := orm.NewOrm()
  2487. StockOutDao := Stock.NewStockOut(o)
  2488. StockOutProductDao := Stock.NewStockOutProduct(o)
  2489. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2490. if err != nil {
  2491. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2492. c.ServeJSON()
  2493. return
  2494. }
  2495. productList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  2496. var pList []Stock.StockOutProduct_R
  2497. for _, v := range productList {
  2498. pList = append(pList, Stock.StockOutProductToStockOutProduct_R(v))
  2499. }
  2500. userList, _ := NatsServer.Read_User_List_All()
  2501. Account.Read_User_All_Map(userList)
  2502. Basic.Read_Depot_All_Map()
  2503. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Stock.StockOutToStockOut_Detail(stockOut, pList)}
  2504. c.ServeJSON()
  2505. return
  2506. }
  2507. func (c *StockController) StockOut_generate_number() {
  2508. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  2509. rand_x := 0
  2510. T_number := ""
  2511. for true {
  2512. T_number = "CK-" + lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x))
  2513. _, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2514. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  2515. break
  2516. }
  2517. rand_x += 1
  2518. }
  2519. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2520. c.ServeJSON()
  2521. return
  2522. }
  2523. func (c *StockController) StockOut_Add() {
  2524. o := orm.NewOrm()
  2525. o.Begin()
  2526. StockOutDao := Stock.NewStockOut(o)
  2527. // 检查前端传入的T_number是否已提交过
  2528. T_number := c.GetString("T_number")
  2529. _, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2530. if err == nil {
  2531. // 入库编号已存在,不需要做任何处理
  2532. o.Rollback()
  2533. c.Data["json"] = lib.JSONS{Code: 200, Msg: "出库编号已存在,无需重复提交!", Data: T_number}
  2534. c.ServeJSON()
  2535. return
  2536. }
  2537. if err != nil && err.Error() != orm.ErrNoRows.Error() {
  2538. // 数据库查询错误
  2539. o.Rollback()
  2540. c.Data["json"] = lib.JSONS{Code: 203, Msg: "查询入库编号失败"}
  2541. c.ServeJSON()
  2542. return
  2543. }
  2544. T_contract_number := c.GetString("T_contract_number")
  2545. T_depot_id, _ := c.GetInt("T_depot_id")
  2546. T_type, _ := c.GetInt("T_type") // 出库类型 1-领料出库 2-销售出库
  2547. T_date := c.GetString("T_date")
  2548. T_receive := c.GetString("T_receive")
  2549. T_product := c.GetString("T_product")
  2550. T_remark := c.GetString("T_remark")
  2551. T_project := c.GetString("T_project")
  2552. T_delivery_type, _ := c.GetInt("T_delivery_type")
  2553. T_signer_unit := c.GetString("T_signer_unit")
  2554. T_signer := c.GetString("T_signer")
  2555. T_signer_phone := c.GetString("T_signer_phone")
  2556. T_signer_date := c.GetString("T_signer_date")
  2557. T_courier_number := c.GetString("T_courier_number")
  2558. date, is := lib.DateStrToTime(T_date)
  2559. if !is {
  2560. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  2561. c.ServeJSON()
  2562. return
  2563. }
  2564. var_ := Stock.StockOut{
  2565. T_number: T_number,
  2566. T_contract_number: T_contract_number,
  2567. T_depot_id: T_depot_id,
  2568. T_type: T_type,
  2569. T_date: T_date,
  2570. T_receive: T_receive,
  2571. T_remark: T_remark,
  2572. T_project: T_project,
  2573. T_submit: c.User.T_uuid,
  2574. // 销售出库
  2575. T_delivery_type: T_delivery_type, // 1-自送 2-自提 3-快递
  2576. T_signer_unit: T_signer_unit,
  2577. T_signer: T_signer,
  2578. T_signer_phone: T_signer_phone,
  2579. T_signer_date: T_signer_date,
  2580. T_courier_number: T_courier_number,
  2581. T_state: Stock.StockOutAlreadyOut,
  2582. }
  2583. StockOutProductDao := Stock.NewStockOutProduct(o)
  2584. StockDao := Stock.NewStock(o)
  2585. DeviceDao := Stock.NewDevice(o)
  2586. ContractDao := Contract.NewContract(o)
  2587. ContractProductDao := Contract.NewContractProduct(o)
  2588. _, err = StockOutDao.Add_StockOut(var_)
  2589. if err != nil {
  2590. o.Rollback()
  2591. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  2592. c.ServeJSON()
  2593. return
  2594. }
  2595. // 1、添加出库单
  2596. contract, _ := ContractDao.Read_Contract_ByT_number(T_contract_number)
  2597. if contract.Id == 0 && T_type == 2 {
  2598. o.Rollback()
  2599. c.Data["json"] = lib.JSONS{Code: 203, Msg: "合同编号错误!"}
  2600. c.ServeJSON()
  2601. return
  2602. }
  2603. if len(T_product) == 0 {
  2604. o.Rollback()
  2605. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  2606. c.ServeJSON()
  2607. return
  2608. }
  2609. productList := lib.SplitString(T_product, "|")
  2610. allProductList := []int{}
  2611. for _, v := range productList {
  2612. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  2613. allProductList = append(allProductList, product_id)
  2614. product, _ := Basic.Read_Product_ById(product_id)
  2615. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  2616. T_relation_sn := strings.Split(v, "-")[2]
  2617. if T_relation_sn == "" && product.T_relation_sn == 1 {
  2618. o.Rollback()
  2619. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  2620. c.ServeJSON()
  2621. return
  2622. }
  2623. if len(T_relation_sn) > 0 {
  2624. snList := lib.SplitString(T_relation_sn, ",")
  2625. num = len(snList)
  2626. for _, sn := range snList {
  2627. mqtt := Stock.Read_MqttUser(sn)
  2628. // 根据IMEI查询上次入库的批次号
  2629. batchNumber, err := DeviceDao.Read_Device_BatchNumber_ByImei(mqtt.Imei)
  2630. if err != nil {
  2631. logs.Error(lib.FuncName(), err)
  2632. }
  2633. device := Stock.Device{
  2634. T_contract_number: T_contract_number,
  2635. T_product_id: product_id,
  2636. T_out_number: T_number,
  2637. T_sn: sn,
  2638. T_iccid: mqtt.Iccid,
  2639. T_imei: mqtt.Imei,
  2640. T_State: 1,
  2641. T_project: T_project,
  2642. T_batch_number: batchNumber,
  2643. CreateTime: date,
  2644. }
  2645. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  2646. if err != nil {
  2647. o.Rollback()
  2648. c.Data["json"] = lib.JSONS{Code: 202, Msg: "出库失败"}
  2649. c.ServeJSON()
  2650. return
  2651. }
  2652. }
  2653. }
  2654. // 3、添加出库产品清单
  2655. stockOutProduct := Stock.StockOutProduct{
  2656. T_number: T_number,
  2657. T_product_id: product_id,
  2658. T_depot_id: T_depot_id,
  2659. T_num: num, // 出库数量
  2660. T_date: T_date, // 出库日期
  2661. T_relation_sn: T_relation_sn,
  2662. T_state: 1,
  2663. }
  2664. _, err = StockOutProductDao.Add_StockOutProduct(stockOutProduct)
  2665. if err != nil {
  2666. o.Rollback()
  2667. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  2668. c.ServeJSON()
  2669. return
  2670. }
  2671. // 4、更改合同产品列表清单
  2672. contractProduct, _ := ContractProductDao.Read_ContractProduct_ByT_number_T_product_id(T_contract_number, product_id)
  2673. contractProduct.T_product_out += num
  2674. if contractProduct.T_product_out > contractProduct.T_product_total && T_type == 2 {
  2675. o.Rollback()
  2676. p, _ := Basic.Read_Product_ById(product_id)
  2677. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】出库数量超过预计出库数量!", p.T_name)}
  2678. c.ServeJSON()
  2679. return
  2680. }
  2681. contractProduct.T_State = 2
  2682. if contractProduct.T_product_out == 0 {
  2683. contractProduct.T_State = 1
  2684. }
  2685. if contractProduct.T_product_out == contractProduct.T_product_total {
  2686. contractProduct.T_State = 3
  2687. }
  2688. err = ContractProductDao.Update_ContractProduct(contractProduct, "T_product_out", "T_State")
  2689. if err != nil {
  2690. o.Rollback()
  2691. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新合同产品清单失败"}
  2692. c.ServeJSON()
  2693. return
  2694. }
  2695. // 5、更新产品库存列表
  2696. _, err = StockDao.AddOrUpdate_Stock(T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 1)
  2697. if err != nil {
  2698. o.Rollback()
  2699. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存失败" + err.Error()}
  2700. c.ServeJSON()
  2701. return
  2702. }
  2703. }
  2704. o.Commit()
  2705. o2 := orm.NewOrm()
  2706. o2.Begin()
  2707. ContractDao2 := Contract.NewContract(o2)
  2708. ContractProductDao2 := Contract.NewContractProduct(o2)
  2709. // 5、更新合同出库状态
  2710. var T_out int
  2711. if contract.T_State == 1 {
  2712. T_out = 2
  2713. // 查询合同产品清单是否全部都为已出库
  2714. state := ContractProductDao2.Read_ContractProduct_T_State_List(contract.T_number)
  2715. if state == 1 {
  2716. T_out = 1
  2717. }
  2718. if state == 3 {
  2719. T_out = 3
  2720. }
  2721. }
  2722. if T_out != contract.T_out {
  2723. contract.T_out = T_out
  2724. err = ContractDao2.Update_Contract(contract, "T_out")
  2725. if err != nil {
  2726. o2.Rollback()
  2727. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新合同出库状态失败"}
  2728. c.ServeJSON()
  2729. return
  2730. }
  2731. }
  2732. o2.Commit()
  2733. StockOut_Edit_StockMonth(T_date, T_depot_id, allProductList)
  2734. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "出库", var_)
  2735. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2736. c.ServeJSON()
  2737. return
  2738. }
  2739. func (c *StockController) StockOut_Edit_Delivery() {
  2740. T_number := c.GetString("T_number")
  2741. T_remark := c.GetString("T_remark")
  2742. T_project := c.GetString("T_project")
  2743. T_delivery_type, _ := c.GetInt("T_delivery_type")
  2744. T_signer_unit := c.GetString("T_signer_unit")
  2745. T_signer := c.GetString("T_signer")
  2746. T_signer_phone := c.GetString("T_signer_phone")
  2747. T_signer_date := c.GetString("T_signer_date")
  2748. T_courier_number := c.GetString("T_courier_number")
  2749. o := orm.NewOrm()
  2750. StockOutDao := Stock.NewStockOut(o)
  2751. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2752. if err != nil {
  2753. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库编号错误!"}
  2754. c.ServeJSON()
  2755. return
  2756. }
  2757. if len(T_remark) > 0 {
  2758. stockOut.T_remark = T_remark
  2759. }
  2760. if len(T_project) > 0 {
  2761. stockOut.T_project = T_project
  2762. }
  2763. if T_delivery_type > 0 {
  2764. stockOut.T_delivery_type = T_delivery_type
  2765. }
  2766. if len(T_signer_unit) > 0 {
  2767. stockOut.T_signer_unit = T_signer_unit
  2768. }
  2769. if len(T_signer) > 0 {
  2770. stockOut.T_signer = T_signer
  2771. }
  2772. if len(T_signer_phone) > 0 {
  2773. stockOut.T_signer_phone = T_signer_phone
  2774. }
  2775. if len(T_signer_date) > 0 {
  2776. stockOut.T_signer_date = T_signer_date
  2777. }
  2778. if len(T_courier_number) > 0 {
  2779. stockOut.T_courier_number = T_courier_number
  2780. }
  2781. err = StockOutDao.Update_StockOut(stockOut, "T_remark", "T_delivery_type", "T_signer_unit", "T_signer", "T_signer_phone", "T_signer_date", "T_courier_number", "T_project")
  2782. if err != nil {
  2783. o.Rollback()
  2784. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  2785. c.ServeJSON()
  2786. return
  2787. }
  2788. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "修改发货单", stockOut)
  2789. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2790. c.ServeJSON()
  2791. return
  2792. }
  2793. func (c *StockController) StockOut_Edit() {
  2794. T_number := c.GetString("T_number") // 出库单号
  2795. //T_depot_id, _ := c.GetInt("T_depot_id")
  2796. T_date := c.GetString("T_date")
  2797. T_receive := c.GetString("T_receive")
  2798. T_project := c.GetString("T_project")
  2799. T_product := c.GetString("T_product")
  2800. T_remark := c.GetString("T_remark")
  2801. date, is := lib.DateStrToTime(T_date)
  2802. if !is {
  2803. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  2804. c.ServeJSON()
  2805. return
  2806. }
  2807. if date.Year() != time.Now().Year() && date.Month() != time.Now().Month() {
  2808. T_date = ""
  2809. }
  2810. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "修改出库", T_product)
  2811. o := orm.NewOrm()
  2812. o.Begin()
  2813. StockOutDao := Stock.NewStockOut(o)
  2814. StockOutProductDao := Stock.NewStockOutProduct(o)
  2815. StockDao := Stock.NewStock(o)
  2816. DeviceDao := Stock.NewDevice(o)
  2817. // 查询出库信息
  2818. StockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2819. if err != nil {
  2820. o.Rollback()
  2821. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2822. c.ServeJSON()
  2823. return
  2824. }
  2825. T_old_date := StockOut.T_date
  2826. // 查询出库产品信息
  2827. productOldList := StockOutProductDao.Read_StockOutProduct_List(StockOut.T_number)
  2828. var oldProductList []dto.StockProduct
  2829. var newProductList []dto.StockProduct
  2830. productOldMap := map[int]dto.StockProduct{}
  2831. allProductListMap := make(map[int]struct{})
  2832. for _, product := range productOldList {
  2833. stockProduct := dto.StockProduct{
  2834. T_product_id: product.T_product_id,
  2835. T_num: product.T_num,
  2836. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  2837. }
  2838. oldProductList = append(oldProductList, stockProduct)
  2839. productOldMap[product.T_product_id] = stockProduct
  2840. if _, ok := allProductListMap[product.T_product_id]; !ok {
  2841. allProductListMap[product.T_product_id] = struct{}{}
  2842. }
  2843. }
  2844. productNewList := lib.SplitString(T_product, "|")
  2845. productNewMap := map[int]dto.StockProduct{}
  2846. for _, v := range productNewList {
  2847. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  2848. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  2849. T_relation_sn := strings.Split(v, "-")[2]
  2850. snList := lib.SplitString(T_relation_sn, ",")
  2851. if len(snList) > 0 {
  2852. num = len(snList)
  2853. }
  2854. stockProduct := dto.StockProduct{
  2855. T_product_id: product_id,
  2856. T_num: num,
  2857. T_relation_sn: snList,
  2858. }
  2859. newProductList = append(newProductList, stockProduct)
  2860. productNewMap[product_id] = stockProduct
  2861. if _, ok := allProductListMap[product_id]; !ok {
  2862. allProductListMap[product_id] = struct{}{}
  2863. }
  2864. }
  2865. // 判断产品列表信息是否相同
  2866. StockProductListIsEqual, needDelete, needAdd, needEdit, snDiff := dto.StockProductListsEqual(oldProductList, newProductList)
  2867. // 两次提交的数据相同,则不用修改
  2868. if !StockProductListIsEqual {
  2869. if len(needDelete) > 0 {
  2870. // 删除入库产品列表
  2871. for _, v := range needDelete {
  2872. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2873. // 删除入库产品列表
  2874. err = StockOutProductDao.Delete_StockOutProduct(StockOut.T_number, StockOut.T_depot_id, v.T_product_id)
  2875. if err != nil {
  2876. o.Rollback()
  2877. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  2878. c.ServeJSON()
  2879. return
  2880. }
  2881. if product.T_relation_sn == 1 {
  2882. // 删除该入库单关联sn
  2883. for _, sn := range v.T_relation_sn {
  2884. err = DeviceDao.Delete_Device_ByT_out_number(StockOut.T_number, sn)
  2885. if err != nil {
  2886. o.Rollback()
  2887. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库设备失败"}
  2888. c.ServeJSON()
  2889. return
  2890. }
  2891. }
  2892. }
  2893. // 增加库存
  2894. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 2)
  2895. if err != nil {
  2896. o.Rollback()
  2897. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  2898. c.ServeJSON()
  2899. return
  2900. }
  2901. }
  2902. }
  2903. if len(needAdd) > 0 {
  2904. // 新增入库产品列表
  2905. for _, v := range needAdd {
  2906. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2907. if product.T_relation_sn == 1 && len(v.T_relation_sn) == 0 {
  2908. o.Rollback()
  2909. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  2910. c.ServeJSON()
  2911. return
  2912. }
  2913. // 添加该入库单关联sn
  2914. for _, sn := range v.T_relation_sn {
  2915. mqtt := Stock.Read_MqttUser(sn)
  2916. // 添加设备
  2917. device := Stock.Device{
  2918. T_contract_number: "",
  2919. T_out_number: StockOut.T_number,
  2920. T_product_id: v.T_product_id,
  2921. T_in_number: "",
  2922. T_sn: sn,
  2923. T_iccid: mqtt.Iccid,
  2924. T_imei: mqtt.Imei,
  2925. T_State: 1,
  2926. CreateTime: date,
  2927. UpdateTime: StockOut.UpdateTime,
  2928. }
  2929. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  2930. if err != nil {
  2931. o.Rollback()
  2932. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  2933. c.ServeJSON()
  2934. return
  2935. }
  2936. }
  2937. StockOutProduct := Stock.StockOutProduct{
  2938. T_number: T_number,
  2939. T_product_id: v.T_product_id,
  2940. T_depot_id: StockOut.T_depot_id,
  2941. T_num: v.T_num, // 入库数量
  2942. T_date: StockOut.T_date, // 入库日期
  2943. T_relation_sn: strings.Join(v.T_relation_sn, ","),
  2944. T_state: 1,
  2945. }
  2946. _, err = StockOutProductDao.Add_StockOutProduct(StockOutProduct)
  2947. if err != nil {
  2948. o.Rollback()
  2949. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  2950. c.ServeJSON()
  2951. return
  2952. }
  2953. // 减少库存
  2954. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  2955. if err != nil {
  2956. o.Rollback()
  2957. c.Data["json"] = lib.JSONS{Code: 203, Msg: "增加库存失败"}
  2958. c.ServeJSON()
  2959. return
  2960. }
  2961. }
  2962. }
  2963. if len(needEdit) > 0 {
  2964. for _, v := range needEdit {
  2965. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2966. StockOutProduct := Stock.StockOutProduct{
  2967. T_number: StockOut.T_number,
  2968. T_product_id: v.T_product_id,
  2969. T_depot_id: StockOut.T_depot_id,
  2970. T_num: productNewMap[v.T_product_id].T_num, // 入库数量
  2971. T_date: StockOut.T_date, // 入库日期
  2972. T_state: 1,
  2973. }
  2974. // 出库数量比之前多,减少库存
  2975. var T_type int
  2976. var T_num int
  2977. if productNewMap[v.T_product_id].T_num > productOldMap[v.T_product_id].T_num {
  2978. // 减少库存
  2979. T_type = 1
  2980. T_num = productNewMap[v.T_product_id].T_num - productOldMap[v.T_product_id].T_num
  2981. } else {
  2982. // 增加库存
  2983. T_type = 2
  2984. T_num = productOldMap[v.T_product_id].T_num - productNewMap[v.T_product_id].T_num
  2985. }
  2986. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, T_num, T_type)
  2987. if err != nil {
  2988. o.Rollback()
  2989. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  2990. c.ServeJSON()
  2991. return
  2992. }
  2993. // 更新产品库存表
  2994. err = StockOutProductDao.Update_StockOutProduct(StockOutProduct)
  2995. if err != nil {
  2996. o.Rollback()
  2997. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  2998. c.ServeJSON()
  2999. return
  3000. }
  3001. }
  3002. }
  3003. for _, diff := range snDiff {
  3004. product, _ := Basic.Read_Product_ById(diff.T_product_id)
  3005. // 删除减少的sn
  3006. if len(diff.T_delete_relation_sn) > 0 {
  3007. for _, sn := range diff.T_delete_relation_sn {
  3008. err = DeviceDao.Delete_Device_ByT_out_number(StockOut.T_number, sn)
  3009. if err != nil {
  3010. o.Rollback()
  3011. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  3012. c.ServeJSON()
  3013. return
  3014. }
  3015. }
  3016. // 增加库存
  3017. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, len(diff.T_delete_relation_sn), 2)
  3018. if err != nil {
  3019. o.Rollback()
  3020. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  3021. c.ServeJSON()
  3022. return
  3023. }
  3024. }
  3025. if len(diff.T_add_relation_sn) > 0 {
  3026. for _, sn := range diff.T_add_relation_sn {
  3027. mqtt := Stock.Read_MqttUser(sn)
  3028. // 添加设备
  3029. device := Stock.Device{
  3030. T_contract_number: "",
  3031. T_out_number: StockOut.T_number,
  3032. T_product_id: diff.T_product_id,
  3033. T_in_number: "",
  3034. T_sn: sn,
  3035. T_iccid: mqtt.Iccid,
  3036. T_imei: mqtt.Imei,
  3037. T_State: 1,
  3038. CreateTime: date,
  3039. UpdateTime: StockOut.UpdateTime,
  3040. }
  3041. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  3042. if err != nil {
  3043. o.Rollback()
  3044. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  3045. c.ServeJSON()
  3046. return
  3047. }
  3048. }
  3049. // 减少库存
  3050. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, len(diff.T_add_relation_sn), 1)
  3051. if err != nil {
  3052. o.Rollback()
  3053. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  3054. c.ServeJSON()
  3055. return
  3056. }
  3057. }
  3058. StockOutProduct := Stock.StockOutProduct{
  3059. T_number: StockOut.T_number,
  3060. T_product_id: diff.T_product_id,
  3061. T_depot_id: StockOut.T_depot_id,
  3062. T_num: productNewMap[diff.T_product_id].T_num, // 入库数量
  3063. T_date: StockOut.T_date, // 入库日期
  3064. T_relation_sn: strings.Join(productNewMap[diff.T_product_id].T_relation_sn, ","),
  3065. T_state: 1,
  3066. }
  3067. // 更新产品库存表
  3068. err = StockOutProductDao.Update_StockOutProduct(StockOutProduct)
  3069. if err != nil {
  3070. o.Rollback()
  3071. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  3072. c.ServeJSON()
  3073. return
  3074. }
  3075. }
  3076. }
  3077. StockOut.T_remark = T_remark
  3078. if len(T_receive) > 0 {
  3079. StockOut.T_receive = T_receive
  3080. }
  3081. if len(T_project) > 0 {
  3082. StockOut.T_project = T_project
  3083. }
  3084. if len(T_date) > 0 {
  3085. StockOut.T_date = T_date
  3086. }
  3087. err = StockOutDao.Update_StockOut(StockOut, "T_remark", "T_receive", "T_project", "T_date")
  3088. if err != nil {
  3089. o.Rollback()
  3090. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库失败"}
  3091. c.ServeJSON()
  3092. return
  3093. }
  3094. o.Commit()
  3095. if len(T_date) > 0 && T_old_date != T_date {
  3096. // 修改出库产品日期
  3097. StockOutProductDao.Update_StockOutProduct_T_date(StockOut.T_number, T_date)
  3098. DeviceDao.Update_Device_CreateTimeByT_out_number(StockOut.T_number, T_date)
  3099. }
  3100. if len(T_project) > 0 {
  3101. DeviceDao.Update_Device_ByT_out_number_T_project(StockOut.T_number, T_project)
  3102. }
  3103. // 更新月份统计表
  3104. allProductList := []int{}
  3105. for i, _ := range allProductListMap {
  3106. allProductList = append(allProductList, i)
  3107. }
  3108. StockOut_Edit_StockMonth(StockOut.T_date, StockOut.T_depot_id, allProductList)
  3109. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "修改", StockOut)
  3110. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  3111. c.ServeJSON()
  3112. return
  3113. }
  3114. // 删除出库
  3115. func (c *StockController) StockOut_Del() {
  3116. T_number := c.GetString("T_number") // 入库单号
  3117. o := orm.NewOrm()
  3118. o.Begin()
  3119. StockOutDao := Stock.NewStockOut(o)
  3120. StockOutProductDao := Stock.NewStockOutProduct(o)
  3121. StockDao := Stock.NewStock(o)
  3122. DeviceDao := Stock.NewDevice(o)
  3123. // 查询出库信息
  3124. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  3125. if err != nil {
  3126. o.Rollback()
  3127. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  3128. c.ServeJSON()
  3129. return
  3130. }
  3131. // 查询出库产品信息
  3132. productOldList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  3133. var oldProductList []dto.StockProduct
  3134. allProductList := []int{}
  3135. for _, product := range productOldList {
  3136. oldProductList = append(oldProductList, dto.StockProduct{
  3137. T_product_id: product.T_product_id,
  3138. T_num: product.T_num,
  3139. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  3140. })
  3141. allProductList = append(allProductList, product.T_product_id)
  3142. }
  3143. // 删除入库产品列表
  3144. for _, v := range oldProductList {
  3145. product, _ := Basic.Read_Product_ById(v.T_product_id)
  3146. // 删除入库产品列表
  3147. err = StockOutProductDao.Delete_StockOutProduct(stockOut.T_number, stockOut.T_depot_id, v.T_product_id)
  3148. if err != nil {
  3149. o.Rollback()
  3150. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品出库信息失败!"}
  3151. c.ServeJSON()
  3152. return
  3153. }
  3154. if product.T_relation_sn == 1 {
  3155. // 删除该入库单关联sn
  3156. for _, sn := range v.T_relation_sn {
  3157. err = DeviceDao.Delete_Device_ByT_out_number(stockOut.T_number, sn)
  3158. if err != nil {
  3159. o.Rollback()
  3160. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除出库设备失败"}
  3161. c.ServeJSON()
  3162. return
  3163. }
  3164. }
  3165. }
  3166. // 增加库存
  3167. _, err = StockDao.AddOrUpdate_Stock(stockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 2)
  3168. if err != nil {
  3169. o.Rollback()
  3170. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  3171. c.ServeJSON()
  3172. return
  3173. }
  3174. }
  3175. err = StockOutDao.Delete_StockOut(stockOut)
  3176. if err != nil {
  3177. o.Rollback()
  3178. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除出库失败"}
  3179. c.ServeJSON()
  3180. return
  3181. }
  3182. o.Commit()
  3183. // 更新月份统计表
  3184. StockOut_Edit_StockMonth(stockOut.T_date, stockOut.T_depot_id, allProductList)
  3185. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "删除", stockOut)
  3186. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  3187. c.ServeJSON()
  3188. return
  3189. }
  3190. // 导出出库单
  3191. func (c *StockController) StockOut_Excel() {
  3192. // 查询
  3193. T_number := c.GetString("T_number")
  3194. o := orm.NewOrm()
  3195. StockOutDao := Stock.NewStockOut(o)
  3196. StockOutProductDao := Stock.NewStockOutProduct(o)
  3197. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  3198. if err != nil {
  3199. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  3200. c.ServeJSON()
  3201. return
  3202. }
  3203. productList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  3204. //productList = append(productList, productList...)
  3205. //productList = append(productList, productList...)
  3206. //productList = append(productList, productList...)
  3207. var pList []Stock.StockOutProduct_R
  3208. for _, v := range productList {
  3209. pList = append(pList, Stock.StockOutProductToStockOutProduct_R(v))
  3210. }
  3211. userList, _ := NatsServer.Read_User_List_All()
  3212. Account.Read_User_All_Map(userList)
  3213. stockOutDetail := Stock.StockOutToStockOut_Detail(stockOut, pList)
  3214. T_date, _ := lib.DateStrToTime(stockOut.T_date)
  3215. filename := fmt.Sprintf("冷链产品出库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  3216. f := excelize.NewFile() // 设置单元格的值
  3217. Style1, _ := f.NewStyle(
  3218. &excelize.Style{
  3219. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  3220. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  3221. })
  3222. Style2, _ := f.NewStyle(
  3223. &excelize.Style{
  3224. Font: &excelize.Font{Size: 11, Family: "宋体"},
  3225. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  3226. })
  3227. Style3, _ := f.NewStyle(
  3228. &excelize.Style{
  3229. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  3230. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  3231. Border: []excelize.Border{
  3232. {Type: "left", Color: "000000", Style: 1},
  3233. {Type: "top", Color: "000000", Style: 1},
  3234. {Type: "bottom", Color: "000000", Style: 1},
  3235. {Type: "right", Color: "000000", Style: 1},
  3236. },
  3237. })
  3238. Style4, _ := f.NewStyle(
  3239. &excelize.Style{
  3240. Font: &excelize.Font{Size: 11, Family: "宋体"},
  3241. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  3242. Border: []excelize.Border{
  3243. {Type: "left", Color: "000000", Style: 1},
  3244. {Type: "top", Color: "000000", Style: 1},
  3245. {Type: "bottom", Color: "000000", Style: 1},
  3246. {Type: "right", Color: "000000", Style: 1},
  3247. },
  3248. })
  3249. productList2 := lib.ChunkBy(productList, 10)
  3250. for i, products := range productList2 {
  3251. if len(products) < 10 {
  3252. products = append(products, make([]Stock.StockOutProduct, 10-len(products))...)
  3253. }
  3254. row := i*17 + 1
  3255. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3256. f.SetRowStyle("Sheet1", row, row, Style1)
  3257. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  3258. f.SetRowHeight("Sheet1", row, 20)
  3259. row += 1
  3260. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3261. f.SetRowStyle("Sheet1", row, row, Style1)
  3262. title := "硬件出库单"
  3263. if len(productList2) > 1 {
  3264. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  3265. }
  3266. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  3267. f.SetRowHeight("Sheet1", row, 20)
  3268. row += 1
  3269. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3270. f.SetRowStyle("Sheet1", row, row, Style2)
  3271. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("出库单号:%s", stockOut.T_number))
  3272. f.SetRowHeight("Sheet1", row, 20)
  3273. row += 1
  3274. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3275. f.SetRowStyle("Sheet1", row, row, Style2)
  3276. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockOut.T_project))
  3277. f.SetRowHeight("Sheet1", row, 20)
  3278. row += 1
  3279. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3280. f.SetRowStyle("Sheet1", row, row, Style2)
  3281. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("领用时间:%s", T_date.Format("2006年01月02日")))
  3282. f.SetRowHeight("Sheet1", row, 20)
  3283. // 这里设置表头
  3284. row += 1
  3285. f.SetRowHeight("Sheet1", row, 20)
  3286. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  3287. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  3288. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  3289. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  3290. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  3291. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  3292. // 设置列宽
  3293. f.SetColWidth("Sheet1", "A", "A", 5)
  3294. f.SetColWidth("Sheet1", "B", "B", 20)
  3295. f.SetColWidth("Sheet1", "C", "C", 12)
  3296. f.SetColWidth("Sheet1", "D", "D", 10)
  3297. f.SetColWidth("Sheet1", "E", "E", 10)
  3298. f.SetColWidth("Sheet1", "F", "F", 15)
  3299. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  3300. sRow := row
  3301. // 循环写入数据
  3302. for j, v := range products {
  3303. row += 1
  3304. product, _ := Basic.Read_Product_ById(v.T_product_id)
  3305. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  3306. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  3307. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  3308. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  3309. if v.T_num > 0 {
  3310. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  3311. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockOut.T_remark)
  3312. }
  3313. }
  3314. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  3315. row += 1
  3316. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  3317. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  3318. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  3319. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("领用人:%s", stockOutDetail.T_receive_name))
  3320. f.SetRowStyle("Sheet1", row, row, Style2)
  3321. f.SetRowHeight("Sheet1", row, 20)
  3322. }
  3323. // 保存文件
  3324. if err = f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  3325. fmt.Println(err)
  3326. }
  3327. var url string
  3328. //// 上传 OSS
  3329. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  3330. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  3331. if !is {
  3332. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  3333. c.ServeJSON()
  3334. return
  3335. }
  3336. //删除目录
  3337. err = os.Remove("ofile/" + filename + ".xlsx")
  3338. if err != nil {
  3339. logs.Error(lib.FuncName(), err)
  3340. }
  3341. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  3342. c.ServeJSON()
  3343. return
  3344. }
  3345. func (c *StockController) StockOut_Excel_Batch() {
  3346. // 查询
  3347. T_number_list := c.GetString("T_number_list")
  3348. o := orm.NewOrm()
  3349. StockOutDao := Stock.NewStockOut(o)
  3350. StockOutProductDao := Stock.NewStockOutProduct(o)
  3351. list := strings.Split(strings.Trim(T_number_list, "|"), "|")
  3352. userList, _ := NatsServer.Read_User_List_All()
  3353. Account.Read_User_All_Map(userList)
  3354. filename := fmt.Sprintf("冷链产品出库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  3355. f := excelize.NewFile() // 设置单元格的值
  3356. Style1, _ := f.NewStyle(
  3357. &excelize.Style{
  3358. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  3359. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  3360. })
  3361. Style2, _ := f.NewStyle(
  3362. &excelize.Style{
  3363. Font: &excelize.Font{Size: 11, Family: "宋体"},
  3364. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  3365. })
  3366. Style3, _ := f.NewStyle(
  3367. &excelize.Style{
  3368. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  3369. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  3370. Border: []excelize.Border{
  3371. {Type: "left", Color: "000000", Style: 1},
  3372. {Type: "top", Color: "000000", Style: 1},
  3373. {Type: "bottom", Color: "000000", Style: 1},
  3374. {Type: "right", Color: "000000", Style: 1},
  3375. },
  3376. })
  3377. Style4, _ := f.NewStyle(
  3378. &excelize.Style{
  3379. Font: &excelize.Font{Size: 11, Family: "宋体"},
  3380. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  3381. Border: []excelize.Border{
  3382. {Type: "left", Color: "000000", Style: 1},
  3383. {Type: "top", Color: "000000", Style: 1},
  3384. {Type: "bottom", Color: "000000", Style: 1},
  3385. {Type: "right", Color: "000000", Style: 1},
  3386. },
  3387. })
  3388. var num int
  3389. for _, T_number := range list {
  3390. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  3391. if err != nil {
  3392. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  3393. c.ServeJSON()
  3394. return
  3395. }
  3396. productList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  3397. var pList []Stock.StockOutProduct_R
  3398. for _, v := range productList {
  3399. pList = append(pList, Stock.StockOutProductToStockOutProduct_R(v))
  3400. }
  3401. stockOutDetail := Stock.StockOutToStockOut_Detail(stockOut, pList)
  3402. T_date, _ := lib.DateStrToTime(stockOut.T_date)
  3403. productList2 := lib.ChunkBy(productList, 10)
  3404. for i, products := range productList2 {
  3405. if len(products) < 10 {
  3406. products = append(products, make([]Stock.StockOutProduct, 10-len(products))...)
  3407. }
  3408. row := num*18 + 1
  3409. num += 1
  3410. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3411. f.SetRowStyle("Sheet1", row, row, Style1)
  3412. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  3413. f.SetRowHeight("Sheet1", row, 20)
  3414. row += 1
  3415. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3416. f.SetRowStyle("Sheet1", row, row, Style1)
  3417. title := "硬件出库单"
  3418. if len(productList2) > 1 {
  3419. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  3420. }
  3421. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  3422. f.SetRowHeight("Sheet1", row, 20)
  3423. row += 1
  3424. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3425. f.SetRowStyle("Sheet1", row, row, Style2)
  3426. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("出库单号:%s", stockOut.T_number))
  3427. f.SetRowHeight("Sheet1", row, 20)
  3428. row += 1
  3429. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3430. f.SetRowStyle("Sheet1", row, row, Style2)
  3431. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockOut.T_project))
  3432. f.SetRowHeight("Sheet1", row, 20)
  3433. row += 1
  3434. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3435. f.SetRowStyle("Sheet1", row, row, Style2)
  3436. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("领用时间:%s", T_date.Format("2006年01月02日")))
  3437. f.SetRowHeight("Sheet1", row, 20)
  3438. // 这里设置表头
  3439. row += 1
  3440. f.SetRowHeight("Sheet1", row, 20)
  3441. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  3442. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  3443. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  3444. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  3445. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  3446. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  3447. // 设置列宽
  3448. f.SetColWidth("Sheet1", "A", "A", 5)
  3449. f.SetColWidth("Sheet1", "B", "B", 20)
  3450. f.SetColWidth("Sheet1", "C", "C", 12)
  3451. f.SetColWidth("Sheet1", "D", "D", 10)
  3452. f.SetColWidth("Sheet1", "E", "E", 10)
  3453. f.SetColWidth("Sheet1", "F", "F", 15)
  3454. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  3455. sRow := row
  3456. // 循环写入数据
  3457. for j, v := range products {
  3458. row += 1
  3459. product, _ := Basic.Read_Product_ById(v.T_product_id)
  3460. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  3461. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  3462. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  3463. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  3464. if v.T_num > 0 {
  3465. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  3466. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockOut.T_remark)
  3467. }
  3468. }
  3469. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  3470. row += 1
  3471. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  3472. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  3473. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  3474. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("领用人:%s", stockOutDetail.T_receive_name))
  3475. f.SetRowStyle("Sheet1", row, row, Style2)
  3476. f.SetRowHeight("Sheet1", row, 20)
  3477. // 空一行
  3478. row += 1
  3479. //f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3480. //f.SetRowStyle("Sheet1", row, row, Style1)
  3481. //f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "")
  3482. // 插入下一页分页符
  3483. f.InsertPageBreak("Sheet1", fmt.Sprintf("A%d", row))
  3484. }
  3485. }
  3486. // 保存文件
  3487. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  3488. fmt.Println(err)
  3489. }
  3490. var url string
  3491. //// 上传 OSS
  3492. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  3493. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  3494. if !is {
  3495. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  3496. c.ServeJSON()
  3497. return
  3498. }
  3499. //删除目录
  3500. err := os.Remove("ofile/" + filename + ".xlsx")
  3501. if err != nil {
  3502. logs.Error(lib.FuncName(), err)
  3503. }
  3504. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  3505. c.ServeJSON()
  3506. return
  3507. }
  3508. func StockOut_Edit_StockMonth(T_date string, T_depot_id int, allProductList []int) error {
  3509. date, _ := lib.DateStrToTime(T_date)
  3510. //if date.Year() == time.Now().Year() && date.Month() == time.Now().Month() {
  3511. // // 修改本月数据不同步库存
  3512. // return nil
  3513. //}
  3514. //T_month := date.Format("2006-01")
  3515. months := generateMonthList(date, "out")
  3516. o := orm.NewOrm()
  3517. StockOutDao := Stock.NewStockOut(o)
  3518. StockMonthDao := Stock.NewStockMonth(o)
  3519. StockOutProductDao := Stock.NewStockOutProduct(o)
  3520. StockInProductDao := Stock.NewStockInProduct(o)
  3521. for _, T_product_id := range allProductList {
  3522. for _, T_month := range months {
  3523. // 获取当前产品本月出入库数量
  3524. lastMonth := StockMonthDao.Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  3525. stockMonth := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  3526. if stockMonth.Id > 0 {
  3527. // 修改
  3528. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(T_depot_id, T_product_id, T_month)
  3529. stockMonth.T_return = StockInProductDao.Read_StockReturn_Total(T_depot_id, T_product_id, T_month)
  3530. if len(lastMonth) > 0 {
  3531. stockMonth.T_beginning = lastMonth[0].T_ending
  3532. } else {
  3533. stockMonth.T_beginning = 0
  3534. }
  3535. // 期末库存 = 期初库存+入库-出库
  3536. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  3537. err := StockMonthDao.Update_StockMonth(stockMonth, "T_out", "T_return", "T_beginning", "T_ending")
  3538. if err != nil {
  3539. return err
  3540. }
  3541. } else {
  3542. product, _ := Basic.Read_Product_ById(T_product_id)
  3543. // 添加库存记录
  3544. stockMonth = Stock.StockMonth{
  3545. T_depot_id: T_depot_id,
  3546. T_product_id: T_product_id,
  3547. T_product_class: product.T_class,
  3548. T_month: T_month,
  3549. }
  3550. // 获取当前产品本月出入库数量
  3551. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(T_depot_id, T_product_id, T_month)
  3552. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(T_depot_id, T_product_id, T_month)
  3553. stockMonth.T_return = StockInProductDao.Read_StockReturn_Total(T_depot_id, T_product_id, T_month)
  3554. if len(lastMonth) > 0 {
  3555. stockMonth.T_beginning = lastMonth[0].T_ending
  3556. } else {
  3557. stockMonth.T_beginning = 0
  3558. }
  3559. // 期末库存 = 期初库存+入库-出库
  3560. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  3561. stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(T_depot_id, T_product_id, T_month)
  3562. _, err := StockMonthDao.Add_StockMonth(stockMonth)
  3563. if err != nil {
  3564. return err
  3565. }
  3566. }
  3567. }
  3568. }
  3569. return nil
  3570. }
  3571. func Cron_StockMonth() {
  3572. //创建一个定时任务对象
  3573. c := cron.New(cron.WithSeconds())
  3574. //给对象增加定时任务
  3575. // @monthly 每月运行一次,每月第一天午夜 0 0 0 1 * *
  3576. //c.AddFunc("0 */1 * * * ?", Cron_StockMonth_Add)
  3577. c.AddFunc("@monthly", Cron_StockMonth_Add)
  3578. //启动定时任务
  3579. c.Start()
  3580. defer c.Stop()
  3581. //查询语句,阻塞,让main函数不退出,保持程序运行
  3582. select {}
  3583. }
  3584. // 保存每月入库出库明细
  3585. func Cron_StockMonth_Add() {
  3586. T_month := time.Now().Format("2006-01")
  3587. //T_month := time.Now().AddDate(0, -1, 0).Format("2006-01")
  3588. logs.Info("开始统计" + T_month + "库存明细数据")
  3589. o := orm.NewOrm()
  3590. StockDao := Stock.NewStock(o)
  3591. StockOutDao := Stock.NewStockOut(o)
  3592. StockMonthDao := Stock.NewStockMonth(o)
  3593. StockOutProductDao := Stock.NewStockOutProduct(o)
  3594. StockInProductDao := Stock.NewStockInProduct(o)
  3595. stockList, _ := StockDao.Read_Stock_List(0, 0, "", "", 0, 9999)
  3596. for _, stock := range stockList {
  3597. lastMonth := StockMonthDao.Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(stock.T_depot_id, stock.T_product_id, T_month)
  3598. stockMonth := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(stock.T_depot_id, stock.T_product_id, T_month)
  3599. if stockMonth.Id > 0 {
  3600. // 修改
  3601. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(stock.T_depot_id, stock.T_product_id, T_month)
  3602. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(stock.T_depot_id, stock.T_product_id, T_month)
  3603. stockMonth.T_return = StockInProductDao.Read_StockReturn_Total(stock.T_depot_id, stock.T_product_id, T_month)
  3604. if len(lastMonth) > 0 {
  3605. stockMonth.T_beginning = lastMonth[0].T_ending
  3606. } else {
  3607. stockMonth.T_beginning = 0
  3608. }
  3609. // 期末库存 = 期初库存+入库-出库
  3610. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  3611. err := StockMonthDao.Update_StockMonth(stockMonth, "T_in", "T_out", "T_beginning", "T_ending", "T_return")
  3612. if err != nil {
  3613. NatsServer.AddSysLogs("库存明细统计", fmt.Sprintf("库存明细统计失败%s:%d:%d", T_month, stock.T_depot_id, stock.T_product_id), stockMonth)
  3614. }
  3615. } else {
  3616. stockMonth = Stock.StockMonth{
  3617. T_depot_id: stock.T_depot_id,
  3618. T_product_id: stock.T_product_id,
  3619. T_product_class: stock.T_product_class,
  3620. T_month: T_month,
  3621. }
  3622. // 获取当前产品本月出入库数量
  3623. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(stock.T_depot_id, stock.T_product_id, T_month)
  3624. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(stock.T_depot_id, stock.T_product_id, T_month)
  3625. if len(lastMonth) > 0 {
  3626. stockMonth.T_beginning = lastMonth[0].T_ending
  3627. } else {
  3628. stockMonth.T_beginning = 0
  3629. }
  3630. // 期末库存 = 期初库存+入库-出库
  3631. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  3632. stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(stock.T_depot_id, stock.T_product_id, T_month)
  3633. _, err := StockMonthDao.Add_StockMonth(stockMonth)
  3634. if err != nil {
  3635. NatsServer.AddSysLogs("库存明细统计", fmt.Sprintf("库存明细统计失败%s:%d:%d", T_month, stock.T_depot_id, stock.T_product_id), stockMonth)
  3636. }
  3637. }
  3638. }
  3639. }
  3640. func generateMonthList(startMonth time.Time, t_type string) []string {
  3641. var months []string
  3642. endMonth := time.Now()
  3643. if t_type == "in" {
  3644. endMonth = endMonth.AddDate(0, 1, 0)
  3645. }
  3646. for month := startMonth; month.Before(endMonth); month = month.AddDate(0, 1, 0) {
  3647. months = append(months, month.Format("2006-01"))
  3648. }
  3649. return months
  3650. }
  3651. // 我的申请列表
  3652. func (c *StockController) StockOut_Apply_List() {
  3653. // 分页参数 初始化
  3654. page, _ := c.GetInt("page")
  3655. if page < 1 {
  3656. page = 1
  3657. }
  3658. page_z, _ := c.GetInt("page_z")
  3659. if page_z < 1 {
  3660. page_z = conf.Page_size
  3661. }
  3662. // 查询
  3663. T_depot_id, _ := c.GetInt("T_depot_id")
  3664. T_state, _ := c.GetInt("T_state")
  3665. T_contract_number := c.GetString("T_contract_number")
  3666. T_start_date := c.GetString("T_start_date")
  3667. T_end_date := c.GetString("T_end_date")
  3668. T_name := c.GetString("T_name")
  3669. userList, _ := NatsServer.Read_User_List_All()
  3670. Account.Read_User_All_Map(userList)
  3671. Basic.Read_Depot_All_Map()
  3672. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  3673. R_List, R_cnt := StockOutDao.Read_StockOut_Audit_List(c.User.T_uuid, T_name, Stock.StockInTypeApply, T_state, T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  3674. var r_jsons lib.R_JSONS
  3675. r_jsons.Num = R_cnt
  3676. r_jsons.Data = R_List
  3677. r_jsons.Page = page
  3678. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  3679. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  3680. c.ServeJSON()
  3681. return
  3682. }
  3683. // 出库申请
  3684. func (c *StockController) StockOut_Apply() {
  3685. rand_x := 0
  3686. T_number := ""
  3687. o := orm.NewOrm()
  3688. StockOutDao := Stock.NewStockOut(o)
  3689. for true {
  3690. T_number = "CK-" + lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x))
  3691. _, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  3692. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  3693. break
  3694. }
  3695. rand_x += 1
  3696. }
  3697. T_contract_number := c.GetString("T_contract_number")
  3698. T_depot_id, _ := c.GetInt("T_depot_id")
  3699. T_product := c.GetString("T_product")
  3700. T_remark := c.GetString("T_remark")
  3701. T_project := c.GetString("T_project")
  3702. T_company_name := c.GetString("T_company_name")
  3703. T_payment_method := c.GetString("T_payment_method")
  3704. T_receive := c.GetString("T_receive")
  3705. if len(T_company_name) == 0 {
  3706. c.Data["json"] = lib.JSONS{Code: 202, Msg: "公司名称不能为空"}
  3707. c.ServeJSON()
  3708. return
  3709. }
  3710. if len(T_payment_method) == 0 {
  3711. c.Data["json"] = lib.JSONS{Code: 202, Msg: "付款方式不能为空"}
  3712. c.ServeJSON()
  3713. return
  3714. }
  3715. if len(T_receive) == 0 {
  3716. T_receive = c.User.T_uuid
  3717. }
  3718. var_ := Stock.StockOut{
  3719. T_number: T_number,
  3720. T_contract_number: T_contract_number,
  3721. T_depot_id: T_depot_id,
  3722. T_type: 1,
  3723. T_application_date: time.Now().Format("2006-01-02"),
  3724. T_receive: T_receive,
  3725. T_remark: T_remark,
  3726. T_project: T_project,
  3727. T_submit: c.User.T_uuid,
  3728. T_state: Stock.StockOutWaitAudit,
  3729. T_company_name: T_company_name,
  3730. T_payment_method: T_payment_method,
  3731. }
  3732. o.Begin()
  3733. StockOutProductDao := Stock.NewStockOutProduct(o)
  3734. StockDao := Stock.NewStock(o)
  3735. _, err := StockOutDao.Add_StockOut(var_)
  3736. if err != nil {
  3737. o.Rollback()
  3738. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  3739. c.ServeJSON()
  3740. return
  3741. }
  3742. // 1、添加出库单
  3743. if len(T_product) == 0 {
  3744. o.Rollback()
  3745. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  3746. c.ServeJSON()
  3747. return
  3748. }
  3749. productList := lib.SplitString(T_product, "|")
  3750. allProductList := []int{}
  3751. for _, v := range productList {
  3752. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  3753. allProductList = append(allProductList, product_id)
  3754. product, _ := Basic.Read_Product_ById(product_id)
  3755. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  3756. if num == 0 {
  3757. o.Rollback()
  3758. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s出库数量不能为0!", product.T_name)}
  3759. c.ServeJSON()
  3760. return
  3761. }
  3762. // 2、添加出库产品清单
  3763. stockOutProduct := Stock.StockOutProduct{
  3764. T_number: T_number,
  3765. T_product_id: product_id,
  3766. T_depot_id: T_depot_id,
  3767. T_num: num, // 出库数量
  3768. T_state: 2,
  3769. }
  3770. _, err = StockOutProductDao.Add_StockOutProduct(stockOutProduct)
  3771. if err != nil {
  3772. o.Rollback()
  3773. c.Data["json"] = lib.JSONS{Code: 203, Msg: "申请出库失败"}
  3774. c.ServeJSON()
  3775. return
  3776. }
  3777. // 3、增加占用库存
  3778. _, err = StockDao.AddOrUpdate_Occupy_Stock(T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 2)
  3779. if err != nil {
  3780. o.Rollback()
  3781. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新占用库存失败" + err.Error()}
  3782. c.ServeJSON()
  3783. return
  3784. }
  3785. }
  3786. o.Commit()
  3787. NatsServer.AddUserLogs(c.User.T_uuid, "出库申请", "出库申请", var_)
  3788. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  3789. c.ServeJSON()
  3790. return
  3791. }
  3792. // 修改出库申请
  3793. func (c *StockController) StockOut_Apply_Edit() {
  3794. T_number := c.GetString("T_number") // 出库单号
  3795. //T_depot_id, _ := c.GetInt("T_depot_id")
  3796. T_project := c.GetString("T_project")
  3797. T_product := c.GetString("T_product")
  3798. T_remark := c.GetString("T_remark")
  3799. T_receive := c.GetString("T_receive")
  3800. T_company_name := c.GetString("T_company_name")
  3801. T_payment_method := c.GetString("T_payment_method")
  3802. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "修改出库", T_product)
  3803. o := orm.NewOrm()
  3804. o.Begin()
  3805. StockOutDao := Stock.NewStockOut(o)
  3806. StockOutProductDao := Stock.NewStockOutProduct(o)
  3807. StockDao := Stock.NewStock(o)
  3808. // 查询入库信息
  3809. StockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  3810. if err != nil {
  3811. o.Rollback()
  3812. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  3813. c.ServeJSON()
  3814. return
  3815. }
  3816. if StockOut.T_state == Stock.StockOutAlreadyOut {
  3817. o.Rollback()
  3818. c.Data["json"] = lib.JSONS{Code: 202, Msg: "当前出库单已出库,不能修改!"}
  3819. c.ServeJSON()
  3820. return
  3821. }
  3822. // 查询入库产品信息
  3823. productOldList := StockOutProductDao.Read_StockOutProduct_List(StockOut.T_number)
  3824. var oldProductList []dto.StockProduct
  3825. var newProductList []dto.StockProduct
  3826. productOldMap := map[int]dto.StockProduct{}
  3827. allProductListMap := make(map[int]struct{})
  3828. for _, product := range productOldList {
  3829. stockProduct := dto.StockProduct{
  3830. T_product_id: product.T_product_id,
  3831. T_num: product.T_num,
  3832. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  3833. }
  3834. oldProductList = append(oldProductList, stockProduct)
  3835. productOldMap[product.T_product_id] = stockProduct
  3836. if _, ok := allProductListMap[product.T_product_id]; !ok {
  3837. allProductListMap[product.T_product_id] = struct{}{}
  3838. }
  3839. if StockOut.T_state != Stock.StockOutAuditManagerUnPass && StockOut.T_state != Stock.StockOutAuditFinanceUnPass {
  3840. // 减少占用库存
  3841. product2, _ := Basic.Read_Product_ById(product.T_product_id)
  3842. _, err = StockDao.AddOrUpdate_Occupy_Stock(StockOut.T_depot_id, product2.Id, product2.T_class, product2.T_name, product2.T_model, product.T_num, 1)
  3843. if err != nil {
  3844. o.Rollback()
  3845. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新占用库存失败"}
  3846. c.ServeJSON()
  3847. return
  3848. }
  3849. }
  3850. }
  3851. productNewList := lib.SplitString(T_product, "|")
  3852. productNewMap := map[int]dto.StockProduct{}
  3853. for _, v := range productNewList {
  3854. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  3855. product, _ := Basic.Read_Product_ById(product_id)
  3856. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  3857. if num == 0 {
  3858. o.Rollback()
  3859. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s出库数量不能为0!", product.T_name)}
  3860. c.ServeJSON()
  3861. return
  3862. }
  3863. T_relation_sn := strings.Split(v, "-")[2]
  3864. stockProduct := dto.StockProduct{
  3865. T_product_id: product_id,
  3866. T_num: num,
  3867. T_relation_sn: lib.SplitString(T_relation_sn, ","),
  3868. }
  3869. newProductList = append(newProductList, stockProduct)
  3870. productNewMap[product_id] = stockProduct
  3871. if _, ok := allProductListMap[product_id]; !ok {
  3872. allProductListMap[product_id] = struct{}{}
  3873. }
  3874. // 增加占用库存
  3875. _, err = StockDao.AddOrUpdate_Occupy_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 2)
  3876. if err != nil {
  3877. o.Rollback()
  3878. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新占用库存失败"}
  3879. c.ServeJSON()
  3880. return
  3881. }
  3882. }
  3883. // 判断产品列表信息是否相同
  3884. StockProductListIsEqual, needDelete, needAdd, needEdit, _ := dto.StockProductListsEqual(oldProductList, newProductList)
  3885. // 两次提交的数据相同,则不用修改
  3886. if !StockProductListIsEqual {
  3887. if len(needDelete) > 0 {
  3888. // 删除入库产品列表
  3889. for _, v := range needDelete {
  3890. // 删除入库产品列表
  3891. err = StockOutProductDao.Delete_StockOutProduct(StockOut.T_number, StockOut.T_depot_id, v.T_product_id)
  3892. if err != nil {
  3893. o.Rollback()
  3894. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  3895. c.ServeJSON()
  3896. return
  3897. }
  3898. }
  3899. }
  3900. if len(needAdd) > 0 {
  3901. // 新增入库产品列表
  3902. for _, v := range needAdd {
  3903. StockOutProduct := Stock.StockOutProduct{
  3904. T_number: T_number,
  3905. T_product_id: v.T_product_id,
  3906. T_depot_id: StockOut.T_depot_id,
  3907. T_num: v.T_num, // 出库数量
  3908. T_state: 1,
  3909. }
  3910. _, err = StockOutProductDao.Add_StockOutProduct(StockOutProduct)
  3911. if err != nil {
  3912. o.Rollback()
  3913. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  3914. c.ServeJSON()
  3915. return
  3916. }
  3917. }
  3918. }
  3919. if len(needEdit) > 0 {
  3920. for _, v := range needEdit {
  3921. StockOutProduct := Stock.StockOutProduct{
  3922. T_number: StockOut.T_number,
  3923. T_product_id: v.T_product_id,
  3924. T_depot_id: StockOut.T_depot_id,
  3925. T_num: productNewMap[v.T_product_id].T_num, // 入库数量
  3926. T_state: 1,
  3927. }
  3928. // 更新产品表
  3929. err = StockOutProductDao.Update_StockOutProduct(StockOutProduct)
  3930. if err != nil {
  3931. o.Rollback()
  3932. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  3933. c.ServeJSON()
  3934. return
  3935. }
  3936. }
  3937. }
  3938. }
  3939. StockOut.T_remark = T_remark
  3940. if len(T_project) > 0 {
  3941. StockOut.T_project = T_project
  3942. }
  3943. if len(T_company_name) > 0 {
  3944. StockOut.T_company_name = T_company_name
  3945. }
  3946. if len(T_payment_method) > 0 {
  3947. StockOut.T_payment_method = T_payment_method
  3948. }
  3949. if len(T_receive) > 0 {
  3950. StockOut.T_receive = T_receive
  3951. }
  3952. StockOut.T_state = Stock.StockOutWaitAudit
  3953. err = StockOutDao.Update_StockOut(StockOut, "T_remark", "T_receive", "T_project", "T_company_name", "T_payment_method", "T_state")
  3954. if err != nil {
  3955. o.Rollback()
  3956. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库失败"}
  3957. c.ServeJSON()
  3958. return
  3959. }
  3960. o.Commit()
  3961. NatsServer.AddUserLogs(c.User.T_uuid, "出库申请", "修改", StockOut)
  3962. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  3963. c.ServeJSON()
  3964. return
  3965. }
  3966. // 删除出库申请
  3967. func (c *StockController) StockOut_Apply_Del() {
  3968. T_number := c.GetString("T_number") // 入库单号
  3969. o := orm.NewOrm()
  3970. o.Begin()
  3971. StockOutDao := Stock.NewStockOut(o)
  3972. StockOutProductDao := Stock.NewStockOutProduct(o)
  3973. StockDao := Stock.NewStock(o)
  3974. // 查询出库信息
  3975. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  3976. if err != nil {
  3977. o.Rollback()
  3978. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  3979. c.ServeJSON()
  3980. return
  3981. }
  3982. // 查询出库产品信息
  3983. productOldList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  3984. var oldProductList []dto.StockProduct
  3985. allProductList := []int{}
  3986. for _, product := range productOldList {
  3987. oldProductList = append(oldProductList, dto.StockProduct{
  3988. T_product_id: product.T_product_id,
  3989. T_num: product.T_num,
  3990. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  3991. })
  3992. allProductList = append(allProductList, product.T_product_id)
  3993. }
  3994. // 删除入库产品列表
  3995. for _, v := range oldProductList {
  3996. product, _ := Basic.Read_Product_ById(v.T_product_id)
  3997. // 删除入库产品列表
  3998. err = StockOutProductDao.Delete_StockOutProduct(stockOut.T_number, stockOut.T_depot_id, v.T_product_id)
  3999. if err != nil {
  4000. o.Rollback()
  4001. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品出库信息失败!"}
  4002. c.ServeJSON()
  4003. return
  4004. }
  4005. // 只有在未被财务/总经理驳回的状态下才减少占用库存,避免重复扣减
  4006. if stockOut.T_state != Stock.StockOutAuditFinanceUnPass && stockOut.T_state != Stock.StockOutAuditManagerUnPass {
  4007. _, err = StockDao.AddOrUpdate_Occupy_Stock(stockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  4008. if err != nil {
  4009. o.Rollback()
  4010. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新占用库存失败" + err.Error()}
  4011. c.ServeJSON()
  4012. return
  4013. }
  4014. }
  4015. }
  4016. err = StockOutDao.Delete_StockOut(stockOut)
  4017. if err != nil {
  4018. o.Rollback()
  4019. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除出库失败"}
  4020. c.ServeJSON()
  4021. return
  4022. }
  4023. o.Commit()
  4024. NatsServer.AddUserLogs(c.User.T_uuid, "出库申请", "删除", stockOut)
  4025. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  4026. c.ServeJSON()
  4027. return
  4028. }
  4029. // 财务
  4030. func (c *StockController) StockOut_Finance_List() {
  4031. // 分页参数 初始化
  4032. page, _ := c.GetInt("page")
  4033. if page < 1 {
  4034. page = 1
  4035. }
  4036. page_z, _ := c.GetInt("page_z")
  4037. if page_z < 1 {
  4038. page_z = conf.Page_size
  4039. }
  4040. // 查询
  4041. T_depot_id, _ := c.GetInt("T_depot_id")
  4042. T_state, _ := c.GetInt("T_state")
  4043. T_contract_number := c.GetString("T_contract_number")
  4044. T_start_date := c.GetString("T_start_date")
  4045. T_end_date := c.GetString("T_end_date")
  4046. T_name := c.GetString("T_name")
  4047. userList, _ := NatsServer.Read_User_List_All()
  4048. Account.Read_User_All_Map(userList)
  4049. Basic.Read_Depot_All_Map()
  4050. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  4051. R_List, R_cnt := StockOutDao.Read_StockOut_Audit_List("", T_name, Stock.StockOutTypeFinance, T_state, T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  4052. var r_jsons lib.R_JSONS
  4053. r_jsons.Num = R_cnt
  4054. r_jsons.Data = R_List
  4055. r_jsons.Page = page
  4056. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  4057. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  4058. c.ServeJSON()
  4059. return
  4060. }
  4061. // 管理员
  4062. func (c *StockController) StockOut_Manager_List() {
  4063. // 分页参数 初始化
  4064. page, _ := c.GetInt("page")
  4065. if page < 1 {
  4066. page = 1
  4067. }
  4068. page_z, _ := c.GetInt("page_z")
  4069. if page_z < 1 {
  4070. page_z = conf.Page_size
  4071. }
  4072. // 查询
  4073. T_depot_id, _ := c.GetInt("T_depot_id")
  4074. T_state, _ := c.GetInt("T_state")
  4075. T_contract_number := c.GetString("T_contract_number")
  4076. T_start_date := c.GetString("T_start_date")
  4077. T_end_date := c.GetString("T_end_date")
  4078. T_name := c.GetString("T_name")
  4079. userList, _ := NatsServer.Read_User_List_All()
  4080. Account.Read_User_All_Map(userList)
  4081. Basic.Read_Depot_All_Map()
  4082. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  4083. R_List, R_cnt := StockOutDao.Read_StockOut_Audit_List("", T_name, Stock.StockOutTypeManager, T_state, T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  4084. var r_jsons lib.R_JSONS
  4085. r_jsons.Num = R_cnt
  4086. r_jsons.Data = R_List
  4087. r_jsons.Page = page
  4088. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  4089. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  4090. c.ServeJSON()
  4091. return
  4092. }
  4093. // 仓管
  4094. func (c *StockController) StockOut_Warehouse_List() {
  4095. // 分页参数 初始化
  4096. page, _ := c.GetInt("page")
  4097. if page < 1 {
  4098. page = 1
  4099. }
  4100. page_z, _ := c.GetInt("page_z")
  4101. if page_z < 1 {
  4102. page_z = conf.Page_size
  4103. }
  4104. // 查询
  4105. T_depot_id, _ := c.GetInt("T_depot_id")
  4106. T_state, _ := c.GetInt("T_state")
  4107. T_contract_number := c.GetString("T_contract_number")
  4108. T_start_date := c.GetString("T_start_date")
  4109. T_end_date := c.GetString("T_end_date")
  4110. T_name := c.GetString("T_name")
  4111. userList, _ := NatsServer.Read_User_List_All()
  4112. Account.Read_User_All_Map(userList)
  4113. Basic.Read_Depot_All_Map()
  4114. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  4115. R_List, R_cnt := StockOutDao.Read_StockOut_Audit_List(c.User.T_uuid, T_name, Stock.StockOutTypeWarehouse, T_state, T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  4116. var r_jsons lib.R_JSONS
  4117. r_jsons.Num = R_cnt
  4118. r_jsons.Data = R_List
  4119. r_jsons.Page = page
  4120. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  4121. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  4122. c.ServeJSON()
  4123. return
  4124. }
  4125. // 财务/总经理审核
  4126. func (c *StockController) StockOut_Audit() {
  4127. T_number := c.GetString("T_number") // 出库单号
  4128. //T_depot_id, _ := c.GetInt("T_depot_id")
  4129. T_audit, _ := c.GetInt("T_audit") // 审核 2财务通过 3财务不通过 4总经理通过 5总经理不通过 6已出库
  4130. T_approval_opinion := c.GetString("T_approval_opinion") // 审批意见
  4131. T_approval_img := c.GetString("T_approval_img") // 审批意见
  4132. T_type := c.GetString("T_type") // 类型 Finance Manager
  4133. o := orm.NewOrm()
  4134. o.Begin()
  4135. StockOutDao := Stock.NewStockOut(o)
  4136. StockDao := Stock.NewStock(o)
  4137. StockOutProductDao := Stock.NewStockOutProduct(o)
  4138. StockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  4139. if err != nil {
  4140. o.Rollback()
  4141. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  4142. c.ServeJSON()
  4143. return
  4144. }
  4145. StockOut.T_state = T_audit
  4146. if T_type == Stock.StockOutTypeFinance {
  4147. StockOut.T_finance_approval_opinion = T_approval_opinion
  4148. StockOut.T_finance_approval_img = T_approval_img
  4149. } else {
  4150. StockOut.T_manager_approval_opinion = T_approval_opinion
  4151. StockOut.T_manager_approval_img = T_approval_img
  4152. }
  4153. if T_audit == Stock.StockOutAuditFinanceUnPass || T_audit == Stock.StockOutAuditManagerUnPass {
  4154. productList := StockOutProductDao.Read_StockOutProduct_List(StockOut.T_number)
  4155. for _, v := range productList {
  4156. product, _ := Basic.Read_Product_ById(v.T_product_id)
  4157. _, err = StockDao.AddOrUpdate_Occupy_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  4158. if err != nil {
  4159. o.Rollback()
  4160. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  4161. c.ServeJSON()
  4162. return
  4163. }
  4164. }
  4165. }
  4166. err = StockOutDao.Update_StockOut(StockOut, "T_state", "T_finance_approval_opinion", "T_manager_approval_opinion", "T_finance_approval_img", "T_manager_approval_img")
  4167. if err != nil {
  4168. o.Rollback()
  4169. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库审批失败"}
  4170. c.ServeJSON()
  4171. return
  4172. }
  4173. o.Commit()
  4174. NatsServer.AddUserLogs(c.User.T_uuid, "出库申请", "审批", StockOut)
  4175. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  4176. c.ServeJSON()
  4177. return
  4178. }
  4179. // 仓管出库
  4180. func (c *StockController) StockOut_Warehouse() {
  4181. T_number := c.GetString("T_number") // 出库单号
  4182. T_date := c.GetString("T_date")
  4183. date, is := lib.DateStrToTime(T_date)
  4184. if !is {
  4185. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  4186. c.ServeJSON()
  4187. return
  4188. }
  4189. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "确认出库", T_number)
  4190. o := orm.NewOrm()
  4191. o.Begin()
  4192. StockOutDao := Stock.NewStockOut(o)
  4193. StockOutProductDao := Stock.NewStockOutProduct(o)
  4194. DeviceDao := Stock.NewDevice(o)
  4195. StockDao := Stock.NewStock(o)
  4196. // 查询入库信息
  4197. StockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  4198. if err != nil {
  4199. o.Rollback()
  4200. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  4201. c.ServeJSON()
  4202. return
  4203. }
  4204. T_product := c.GetString("T_product")
  4205. productList := lib.SplitString(T_product, "|")
  4206. allProductList := []int{}
  4207. for _, v := range productList {
  4208. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  4209. allProductList = append(allProductList, product_id)
  4210. product, _ := Basic.Read_Product_ById(product_id)
  4211. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  4212. if num == 0 {
  4213. o.Rollback()
  4214. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s出库数量不能为0!", product.T_name)}
  4215. c.ServeJSON()
  4216. return
  4217. }
  4218. T_relation_sn := strings.Split(v, "-")[2]
  4219. if T_relation_sn == "" && product.T_relation_sn == 1 {
  4220. o.Rollback()
  4221. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  4222. c.ServeJSON()
  4223. return
  4224. }
  4225. if len(T_relation_sn) > 0 {
  4226. snList := strings.Split(T_relation_sn, ",")
  4227. if product.T_relation_sn == 1 && num != len(snList) {
  4228. o.Rollback()
  4229. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s SN数量与出库数量不一致,请核查!", product.T_name)}
  4230. c.ServeJSON()
  4231. return
  4232. }
  4233. if len(snList) > 0 {
  4234. for _, sn := range snList {
  4235. mqtt := Stock.Read_MqttUser(sn)
  4236. // 根据IMEI查询上次入库的批次号
  4237. batchNumber, err := DeviceDao.Read_Device_BatchNumber_ByImei(mqtt.Imei)
  4238. if err != nil {
  4239. logs.Error(lib.FuncName(), err)
  4240. }
  4241. device := Stock.Device{
  4242. T_contract_number: StockOut.T_contract_number,
  4243. T_product_id: product_id,
  4244. T_out_number: T_number,
  4245. T_sn: sn,
  4246. T_iccid: mqtt.Iccid,
  4247. T_imei: mqtt.Imei,
  4248. T_State: 1,
  4249. T_project: StockOut.T_project,
  4250. T_batch_number: batchNumber,
  4251. CreateTime: date,
  4252. }
  4253. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  4254. if err != nil {
  4255. o.Rollback()
  4256. c.Data["json"] = lib.JSONS{Code: 202, Msg: "出库失败"}
  4257. c.ServeJSON()
  4258. return
  4259. }
  4260. }
  4261. }
  4262. }
  4263. // 3、添加出库产品清单
  4264. stockOutProduct := Stock.StockOutProduct{
  4265. T_number: T_number,
  4266. T_product_id: product_id,
  4267. T_depot_id: StockOut.T_depot_id,
  4268. T_num: num, // 出库数量
  4269. T_date: T_date, // 出库日期
  4270. T_relation_sn: T_relation_sn,
  4271. T_state: 1,
  4272. }
  4273. // 更新产品表
  4274. err = StockOutProductDao.Update_StockOutProduct(stockOutProduct)
  4275. if err != nil {
  4276. o.Rollback()
  4277. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  4278. c.ServeJSON()
  4279. return
  4280. }
  4281. // 5、更新产品库存列表、占用库存
  4282. _, err = StockDao.Stock_Warehouse(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num)
  4283. if err != nil {
  4284. o.Rollback()
  4285. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  4286. c.ServeJSON()
  4287. return
  4288. }
  4289. }
  4290. StockOut.T_warehouse = c.User.T_uuid
  4291. StockOut.T_state = Stock.StockOutAlreadyOut
  4292. StockOut.T_date = T_date
  4293. err = StockOutDao.Update_StockOut(StockOut, "T_state", "T_date", "T_warehouse")
  4294. if err != nil {
  4295. o.Rollback()
  4296. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库审批失败"}
  4297. c.ServeJSON()
  4298. return
  4299. }
  4300. o.Commit()
  4301. StockOut_Edit_StockMonth(StockOut.T_date, StockOut.T_depot_id, allProductList)
  4302. NatsServer.AddUserLogs(c.User.T_uuid, "出库申请", "出库", StockOut)
  4303. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  4304. c.ServeJSON()
  4305. return
  4306. }
  4307. // StockMonth_Update_Return 批量更新所有StockMonth的退库数据
  4308. func (c *StockController) StockMonth_Update_Return() {
  4309. // 可选参数:指定仓库和时间范围
  4310. T_depot_id, _ := c.GetInt("T_depot_id")
  4311. T_start_date := c.GetString("T_start_date") // 格式:2024-01
  4312. T_end_date := c.GetString("T_end_date") // 格式:2024-12
  4313. // 调用后台更新函数
  4314. count, err := UpdateAllStockMonthReturn(T_depot_id, T_start_date, T_end_date)
  4315. if err != nil {
  4316. c.Data["json"] = lib.JSONS{Code: 203, Msg: fmt.Sprintf("更新失败:%v", err)}
  4317. c.ServeJSON()
  4318. return
  4319. }
  4320. NatsServer.AddUserLogs(c.User.T_uuid, "库存月度统计", "批量更新退库数据", fmt.Sprintf("更新了%d条记录", count))
  4321. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: fmt.Sprintf("成功更新%d条记录", count)}
  4322. c.ServeJSON()
  4323. }
  4324. // UpdateAllStockMonthReturn 批量更新所有StockMonth记录的退库数量
  4325. // T_depot_id: 仓库ID,0表示所有仓库
  4326. // T_start_date: 开始月份,格式:2024-01,为空则不限制
  4327. // T_end_date: 结束月份,格式:2024-12,为空则不限制
  4328. func UpdateAllStockMonthReturn(T_depot_id int, T_start_date, T_end_date string) (count int, err error) {
  4329. o := orm.NewOrm()
  4330. StockMonthDao := Stock.NewStockMonth(o)
  4331. StockInProductDao := Stock.NewStockInProduct(o)
  4332. // 读取所有StockMonth记录
  4333. allStockMonths, err := StockMonthDao.Read_All_StockMonth()
  4334. if err != nil {
  4335. logs.Error(lib.FuncName(), err)
  4336. return 0, err
  4337. }
  4338. count = 0
  4339. for _, stockMonth := range allStockMonths {
  4340. // 如果指定了仓库ID,则过滤
  4341. if T_depot_id > 0 && stockMonth.T_depot_id != T_depot_id {
  4342. continue
  4343. }
  4344. // 如果指定了开始月份,则过滤
  4345. if len(T_start_date) > 0 && stockMonth.T_month < T_start_date {
  4346. continue
  4347. }
  4348. // 如果指定了结束月份,则过滤
  4349. if len(T_end_date) > 0 && stockMonth.T_month > T_end_date {
  4350. continue
  4351. }
  4352. // 查询该产品在该月份的退库数量
  4353. returnTotal := StockInProductDao.Read_StockReturn_Total(stockMonth.T_depot_id, stockMonth.T_product_id, stockMonth.T_month)
  4354. // 如果退库数量与原值不同,则更新
  4355. if stockMonth.T_return != returnTotal {
  4356. stockMonth.T_return = returnTotal
  4357. err = StockMonthDao.Update_StockMonth(stockMonth, "T_return")
  4358. if err != nil {
  4359. logs.Error(lib.FuncName(), fmt.Sprintf("更新失败:仓库%d,产品%d,月份%s", stockMonth.T_depot_id, stockMonth.T_product_id, stockMonth.T_month), err)
  4360. continue
  4361. }
  4362. count++
  4363. }
  4364. }
  4365. logs.Info(fmt.Sprintf("批量更新退库数据完成,共更新%d条记录", count))
  4366. return count, nil
  4367. }
  4368. // Company_Device_Statistics 查询公司借出和退回设备统计
  4369. func (c *StockController) Company_Device_Statistics() {
  4370. // 查询参数(对前端返回扁平化分组列表,不再返回分页包装)
  4371. T_company_name := c.GetString("T_company_name") // 公司名称
  4372. T_depot_id, _ := c.GetInt("T_depot_id") // 仓库ID
  4373. T_start_date := c.GetString("T_start_date") // 开始日期
  4374. T_end_date := c.GetString("T_end_date") // 结束日期
  4375. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  4376. R_List, _ := StockOutDao.Read_Company_Device_Statistics(T_company_name, T_start_date, T_end_date, T_depot_id, 1, 9999)
  4377. // 1) 先按公司分组并排序,顺序与导出一致
  4378. companyMap := make(map[string][]Stock.CompanyDeviceStatistics)
  4379. for _, stat := range R_List {
  4380. if len(stat.T_stock_out_list) == 0 {
  4381. continue
  4382. }
  4383. companyMap[stat.T_company_name] = append(companyMap[stat.T_company_name], stat)
  4384. }
  4385. companyNames := make([]string, 0, len(companyMap))
  4386. for name := range companyMap {
  4387. companyNames = append(companyNames, name)
  4388. }
  4389. sort.Strings(companyNames)
  4390. // 2) 构造前端友好的结构:按公司返回 rows 列表 + 汇总
  4391. type RowItem struct {
  4392. Seq int `json:"seq"`
  4393. OutNumber string `json:"out_number"`
  4394. ProductName string `json:"product_name"`
  4395. DeviceSN string `json:"device_sn"`
  4396. Num int `json:"num"`
  4397. Unit string `json:"unit"`
  4398. Remark string `json:"remark"`
  4399. ReturnNumber string `json:"return_number"`
  4400. ReturnDeviceSN string `json:"return_device_sn"`
  4401. ReturnRemark string `json:"return_remark"`
  4402. }
  4403. type CompanyBlock struct {
  4404. CompanyName string `json:"company_name"`
  4405. StatisticDateLabel string `json:"statistic_date_label"`
  4406. OutTotal int `json:"out_total"`
  4407. ReturnTotal int `json:"return_total"`
  4408. Rows []RowItem `json:"rows"`
  4409. }
  4410. resp := make([]CompanyBlock, 0, len(companyNames))
  4411. for _, companyName := range companyNames {
  4412. statList := companyMap[companyName]
  4413. // 构建退库 SN -> 信息 的映射
  4414. type ReturnInfo struct {
  4415. ReturnNumber string
  4416. SN string
  4417. Num int
  4418. Unit string
  4419. Total float64
  4420. Remark string
  4421. }
  4422. returnSNMap := make(map[string]ReturnInfo)
  4423. for _, stat := range statList {
  4424. for _, ret := range stat.T_stock_return_list {
  4425. for _, sn := range ret.T_device_list {
  4426. if len(sn) > 0 {
  4427. returnSNMap[sn] = ReturnInfo{
  4428. ReturnNumber: ret.T_stock_return_number,
  4429. SN: sn,
  4430. Num: ret.T_num,
  4431. Unit: ret.T_unit,
  4432. Total: ret.T_total,
  4433. Remark: ret.T_remark,
  4434. }
  4435. }
  4436. }
  4437. }
  4438. }
  4439. seqNo := 1
  4440. cumulativeOutTotal := 0
  4441. cumulativeReturnTotal := 0
  4442. rows := make([]RowItem, 0)
  4443. for _, stat := range statList {
  4444. for _, out := range stat.T_stock_out_list {
  4445. validSNs := make([]string, 0)
  4446. for _, sn := range out.T_device_list {
  4447. if len(sn) > 0 {
  4448. validSNs = append(validSNs, sn)
  4449. }
  4450. }
  4451. if len(validSNs) == 0 {
  4452. continue
  4453. }
  4454. cumulativeOutTotal += out.T_num
  4455. currentReturnNum := 0
  4456. returnRemark := ""
  4457. for _, sn := range validSNs {
  4458. if retInfo, exists := returnSNMap[sn]; exists {
  4459. currentReturnNum++
  4460. if len(returnRemark) == 0 {
  4461. returnRemark = retInfo.Remark
  4462. }
  4463. }
  4464. }
  4465. cumulativeReturnTotal += currentReturnNum
  4466. for idx, sn := range validSNs {
  4467. row := RowItem{
  4468. DeviceSN: sn,
  4469. }
  4470. if retInfo, ok := returnSNMap[sn]; ok {
  4471. row.ReturnDeviceSN = sn
  4472. row.ReturnNumber = retInfo.ReturnNumber
  4473. }
  4474. if idx == 0 {
  4475. row.Seq = seqNo
  4476. row.ProductName = out.T_product_name
  4477. row.OutNumber = stat.T_stock_out_number
  4478. row.Num = out.T_num
  4479. row.Unit = out.T_unit
  4480. row.Remark = out.T_remark
  4481. if len(returnRemark) > 0 {
  4482. row.ReturnRemark = returnRemark
  4483. }
  4484. }
  4485. rows = append(rows, row)
  4486. }
  4487. seqNo++
  4488. }
  4489. }
  4490. resp = append(resp, CompanyBlock{
  4491. OutTotal: cumulativeOutTotal,
  4492. ReturnTotal: cumulativeReturnTotal,
  4493. Rows: rows,
  4494. })
  4495. }
  4496. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: resp}
  4497. c.ServeJSON()
  4498. return
  4499. }
  4500. // Company_Device_Statistics_Excel 导出公司借出和退回设备统计Excel
  4501. func (c *StockController) Company_Device_Statistics_Excel() {
  4502. // 查询参数
  4503. T_company_name := c.GetString("T_company_name") // 公司名称
  4504. T_depot_id, _ := c.GetInt("T_depot_id") // 仓库ID
  4505. T_start_date := c.GetString("T_start_date") // 开始日期
  4506. T_end_date := c.GetString("T_end_date") // 结束日期
  4507. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  4508. R_List, _ := StockOutDao.Read_Company_Device_Statistics(T_company_name, T_start_date, T_end_date, T_depot_id, 1, 9999)
  4509. // 生成Excel文件名
  4510. filename := fmt.Sprintf("公司设备借出退回统计(%s)", lib.GetRandstring(6, "0123456789", 0))
  4511. // 创建Excel文件
  4512. f := excelize.NewFile()
  4513. // 设置样式
  4514. StyleTitle, _ := f.NewStyle(
  4515. &excelize.Style{
  4516. Font: &excelize.Font{Bold: true, Size: 14, Family: "宋体"},
  4517. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  4518. })
  4519. StyleHeader, _ := f.NewStyle(
  4520. &excelize.Style{
  4521. Font: &excelize.Font{Bold: true, Size: 10, Family: "宋体"},
  4522. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  4523. Border: []excelize.Border{
  4524. {Type: "left", Color: "000000", Style: 1},
  4525. {Type: "top", Color: "000000", Style: 1},
  4526. {Type: "bottom", Color: "000000", Style: 1},
  4527. {Type: "right", Color: "000000", Style: 1},
  4528. },
  4529. Fill: excelize.Fill{Type: "pattern", Color: []string{"#E0E0E0"}, Pattern: 1},
  4530. })
  4531. StyleCell, _ := f.NewStyle(
  4532. &excelize.Style{
  4533. Font: &excelize.Font{Size: 10, Family: "宋体"},
  4534. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  4535. Border: []excelize.Border{
  4536. {Type: "left", Color: "000000", Style: 1},
  4537. {Type: "top", Color: "000000", Style: 1},
  4538. {Type: "bottom", Color: "000000", Style: 1},
  4539. {Type: "right", Color: "000000", Style: 1},
  4540. },
  4541. })
  4542. currentRow := 1
  4543. sheetName := "Sheet1"
  4544. // 按公司名称分组数据
  4545. companyMap := make(map[string][]Stock.CompanyDeviceStatistics)
  4546. for _, stat := range R_List {
  4547. if len(stat.T_stock_out_list) == 0 {
  4548. continue
  4549. }
  4550. companyMap[stat.T_company_name] = append(companyMap[stat.T_company_name], stat)
  4551. }
  4552. // 提取并排序公司名称,确保导出顺序稳定
  4553. companyNames := make([]string, 0, len(companyMap))
  4554. for name := range companyMap {
  4555. companyNames = append(companyNames, name)
  4556. }
  4557. sort.Strings(companyNames)
  4558. // 按排序后的公司名称遍历
  4559. for _, companyName := range companyNames {
  4560. statList := companyMap[companyName]
  4561. // 合并标题行
  4562. f.MergeCell(sheetName, fmt.Sprintf("A%d", currentRow), fmt.Sprintf("L%d", currentRow))
  4563. f.SetCellValue(sheetName, fmt.Sprintf("A%d", currentRow), "出库申请单/退库单")
  4564. f.SetCellStyle(sheetName, fmt.Sprintf("A%d", currentRow), fmt.Sprintf("L%d", currentRow), StyleTitle)
  4565. f.SetRowHeight(sheetName, currentRow, 25)
  4566. currentRow++
  4567. // 出库信息行
  4568. f.MergeCell(sheetName, fmt.Sprintf("A%d", currentRow), fmt.Sprintf("H%d", currentRow))
  4569. f.SetCellValue(sheetName, fmt.Sprintf("A%d", currentRow), "公司名称:"+companyName)
  4570. f.MergeCell(sheetName, fmt.Sprintf("I%d", currentRow), fmt.Sprintf("L%d", currentRow))
  4571. f.SetCellValue(sheetName, fmt.Sprintf("I%d", currentRow), "")
  4572. f.SetCellStyle(sheetName, fmt.Sprintf("A%d", currentRow), fmt.Sprintf("L%d", currentRow), StyleCell)
  4573. currentRow++
  4574. // 日期行
  4575. f.MergeCell(sheetName, fmt.Sprintf("A%d", currentRow), fmt.Sprintf("H%d", currentRow))
  4576. f.SetCellValue(sheetName, fmt.Sprintf("A%d", currentRow), "统计日期:"+time.Now().Format("2006-01-02"))
  4577. f.MergeCell(sheetName, fmt.Sprintf("I%d", currentRow), fmt.Sprintf("L%d", currentRow))
  4578. f.SetCellValue(sheetName, fmt.Sprintf("I%d", currentRow), "")
  4579. f.SetCellStyle(sheetName, fmt.Sprintf("A%d", currentRow), fmt.Sprintf("L%d", currentRow), StyleCell)
  4580. currentRow++
  4581. // 表头(增加出库单号和退库单号列)
  4582. headers := []string{"序号", "出库单号", "品名", "设备编号", "数量", "单位", "累计", "备注", "退库单号", "退库设备编号", "累计", "备注"}
  4583. for i, header := range headers {
  4584. cell := fmt.Sprintf("%s%d", string(rune('A'+i)), currentRow)
  4585. f.SetCellValue(sheetName, cell, header)
  4586. f.SetCellStyle(sheetName, cell, cell, StyleHeader)
  4587. }
  4588. currentRow++
  4589. // 构建出库SN到退库信息的映射(包含退库单号)
  4590. type ReturnInfo struct {
  4591. ReturnNumber string // 退库单号
  4592. SN string
  4593. Num int
  4594. Unit string
  4595. Total float64
  4596. Remark string
  4597. }
  4598. returnSNMap := make(map[string]ReturnInfo) // 出库SN -> 退库信息
  4599. // 收集该公司所有的退库信息
  4600. for _, stat := range statList {
  4601. for _, ret := range stat.T_stock_return_list {
  4602. for _, sn := range ret.T_device_list {
  4603. if len(sn) > 0 {
  4604. returnSNMap[sn] = ReturnInfo{
  4605. ReturnNumber: ret.T_stock_return_number, // 关联的退库单号
  4606. SN: sn,
  4607. Num: ret.T_num,
  4608. Unit: ret.T_unit,
  4609. Total: ret.T_total,
  4610. Remark: ret.T_remark,
  4611. }
  4612. }
  4613. }
  4614. }
  4615. }
  4616. // 遍历该公司所有出库单的出库明细,按SN展开
  4617. seqNo := 1
  4618. cumulativeOutTotal := 0 // 累计出库总数
  4619. cumulativeReturnTotal := 0 // 累计退库总数
  4620. cumulativeStartRow := currentRow
  4621. // 记录每个出库单号的起始行和结束行,用于后续合并
  4622. type OutNumberRange struct {
  4623. StartRow int
  4624. EndRow int
  4625. Number string
  4626. }
  4627. outNumberRanges := make([]OutNumberRange, 0)
  4628. // 记录每个退库单号的起始行和结束行
  4629. type ReturnNumberRange struct {
  4630. StartRow int
  4631. EndRow int
  4632. Number string
  4633. }
  4634. //returnNumberRanges := make([]ReturnNumberRange, 0)
  4635. for _, stat := range statList {
  4636. outNumberStartRow := currentRow // 记录当前出库单号的起始行
  4637. //hasReturnInThisOut := false // 标记当前出库单是否有退库
  4638. //returnNumberStartRow := currentRow
  4639. for _, out := range stat.T_stock_out_list {
  4640. // 过滤掉空的SN
  4641. validSNs := make([]string, 0)
  4642. for _, sn := range out.T_device_list {
  4643. if len(sn) > 0 {
  4644. validSNs = append(validSNs, sn)
  4645. }
  4646. }
  4647. if len(validSNs) == 0 {
  4648. continue
  4649. }
  4650. // 累加当前产品的出库数量
  4651. cumulativeOutTotal += out.T_num
  4652. // 计算当前产品的退库数量和获取退库备注
  4653. currentReturnNum := 0
  4654. returnRemark := ""
  4655. for _, sn := range validSNs {
  4656. if retInfo, exists := returnSNMap[sn]; exists {
  4657. currentReturnNum++
  4658. if len(returnRemark) == 0 {
  4659. returnRemark = retInfo.Remark // 使用第一个退库记录的备注
  4660. }
  4661. }
  4662. }
  4663. cumulativeReturnTotal += currentReturnNum
  4664. // 标记该出库单是否有退库
  4665. //if currentReturnNum > 0 {
  4666. // hasReturnInThisOut = true
  4667. //}
  4668. startRow := currentRow
  4669. // 为每个SN创建一行
  4670. for idx, sn := range validSNs {
  4671. // 出库单号(C列)和设备编号(D列)
  4672. f.SetCellValue(sheetName, fmt.Sprintf("D%d", currentRow), sn)
  4673. // 退库信息(如果该SN已退库,则显示)
  4674. if retInfo, exists := returnSNMap[sn]; exists {
  4675. f.SetCellValue(sheetName, fmt.Sprintf("J%d", currentRow), sn) // 退库设备编号
  4676. f.SetCellValue(sheetName, fmt.Sprintf("I%d", currentRow), retInfo.ReturnNumber)
  4677. // 退库单号只在第一行显示
  4678. // if idx == 0 && currentReturnNum > 0 {
  4679. // f.SetCellValue(sheetName, fmt.Sprintf("I%d", currentRow), retInfo.ReturnNumber)
  4680. // }
  4681. }
  4682. // 只在第一行设置序号、品名、出库单号、数量、单位、累计、备注
  4683. if idx == 0 {
  4684. f.SetCellValue(sheetName, fmt.Sprintf("A%d", currentRow), seqNo)
  4685. f.SetCellValue(sheetName, fmt.Sprintf("C%d", currentRow), out.T_product_name)
  4686. f.SetCellValue(sheetName, fmt.Sprintf("B%d", currentRow), stat.T_stock_out_number) // 出库单号
  4687. f.SetCellValue(sheetName, fmt.Sprintf("E%d", currentRow), out.T_num)
  4688. f.SetCellValue(sheetName, fmt.Sprintf("F%d", currentRow), out.T_unit)
  4689. //f.SetCellValue(sheetName, fmt.Sprintf("G%d", currentRow), cumulativeOutTotal) // 显示累计出库总数
  4690. f.SetCellValue(sheetName, fmt.Sprintf("H%d", currentRow), out.T_remark)
  4691. // 退库数量、单位、累计合计、备注
  4692. if currentReturnNum > 0 {
  4693. //f.SetCellValue(sheetName, fmt.Sprintf("K%d", currentRow), currentReturnNum)
  4694. //f.SetCellValue(sheetName, fmt.Sprintf("L%d", currentRow), out.T_unit)
  4695. //f.SetCellValue(sheetName, fmt.Sprintf("M%d", currentRow), cumulativeReturnTotal) // 显示累计退库总数
  4696. f.SetCellValue(sheetName, fmt.Sprintf("L%d", currentRow), returnRemark) // 显示退库备注
  4697. }
  4698. }
  4699. // 设置单元格样式
  4700. for col := 'A'; col <= 'L'; col++ {
  4701. cell := fmt.Sprintf("%s%d", string(col), currentRow)
  4702. f.SetCellStyle(sheetName, cell, cell, StyleCell)
  4703. }
  4704. currentRow++
  4705. }
  4706. // 如果有多个SN,合并序号、品名、数量、单位、累计、备注列(出库单号C列和退库单号I列在外层统一合并)
  4707. if len(validSNs) > 1 {
  4708. endRow := currentRow - 1
  4709. f.MergeCell(sheetName, fmt.Sprintf("A%d", startRow), fmt.Sprintf("A%d", endRow))
  4710. f.MergeCell(sheetName, fmt.Sprintf("C%d", startRow), fmt.Sprintf("C%d", endRow))
  4711. f.MergeCell(sheetName, fmt.Sprintf("E%d", startRow), fmt.Sprintf("E%d", endRow))
  4712. f.MergeCell(sheetName, fmt.Sprintf("F%d", startRow), fmt.Sprintf("F%d", endRow))
  4713. f.MergeCell(sheetName, fmt.Sprintf("G%d", startRow), fmt.Sprintf("G%d", endRow))
  4714. f.MergeCell(sheetName, fmt.Sprintf("H%d", startRow), fmt.Sprintf("H%d", endRow))
  4715. }
  4716. seqNo++
  4717. }
  4718. // 记录当前出库单号的范围
  4719. if currentRow > outNumberStartRow {
  4720. outNumberRanges = append(outNumberRanges, OutNumberRange{
  4721. StartRow: outNumberStartRow,
  4722. EndRow: currentRow - 1,
  4723. Number: stat.T_stock_out_number,
  4724. })
  4725. }
  4726. }
  4727. // 合并相同出库单号的单元格(C列)
  4728. for _, rng := range outNumberRanges {
  4729. if rng.EndRow > rng.StartRow {
  4730. f.MergeCell(sheetName, fmt.Sprintf("B%d", rng.StartRow), fmt.Sprintf("B%d", rng.EndRow))
  4731. }
  4732. }
  4733. // 合并相同退库单号的单元格(I列)
  4734. // for _, rng := range returnNumberRanges {
  4735. // if rng.EndRow > rng.StartRow {
  4736. // f.MergeCell(sheetName, fmt.Sprintf("I%d", rng.StartRow), fmt.Sprintf("I%d", rng.EndRow))
  4737. // }
  4738. // }
  4739. // 合并出库累计
  4740. f.MergeCell(sheetName, fmt.Sprintf("G%d", cumulativeStartRow), fmt.Sprintf("G%d", currentRow-1))
  4741. f.SetCellValue(sheetName, fmt.Sprintf("G%d", cumulativeStartRow), cumulativeOutTotal) // 显示累计出库总数
  4742. // 合并退库累计
  4743. f.MergeCell(sheetName, fmt.Sprintf("K%d", cumulativeStartRow), fmt.Sprintf("K%d", currentRow-1))
  4744. f.SetCellValue(sheetName, fmt.Sprintf("K%d", cumulativeStartRow), cumulativeReturnTotal) // 显示累计退库总数
  4745. // 添加空行
  4746. currentRow++
  4747. }
  4748. // 设置列宽(14列)
  4749. f.SetColWidth(sheetName, "A", "A", 8) // 序号
  4750. f.SetColWidth(sheetName, "B", "B", 20) // 品名
  4751. f.SetColWidth(sheetName, "C", "C", 20) // 出库单号
  4752. f.SetColWidth(sheetName, "D", "D", 30) // 设备编号
  4753. f.SetColWidth(sheetName, "E", "F", 10) // 数量、单位
  4754. f.SetColWidth(sheetName, "G", "G", 12) // 累计
  4755. f.SetColWidth(sheetName, "H", "H", 20) // 备注
  4756. f.SetColWidth(sheetName, "I", "I", 20) // 退库单号
  4757. f.SetColWidth(sheetName, "J", "J", 30) // 退库设备编号
  4758. f.SetColWidth(sheetName, "K", "K", 12) // 累计
  4759. f.SetColWidth(sheetName, "L", "L", 20) // 备注
  4760. // 保存文件
  4761. filepath := "./ofile/" + filename + ".xlsx"
  4762. if err := f.SaveAs(filepath); err != nil {
  4763. logs.Error(lib.FuncName(), err)
  4764. c.Data["json"] = lib.JSONS{Code: 201, Msg: "生成Excel失败"}
  4765. c.ServeJSON()
  4766. return
  4767. }
  4768. var url string
  4769. //// 上传 OSS
  4770. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  4771. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  4772. if !is {
  4773. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  4774. c.ServeJSON()
  4775. return
  4776. }
  4777. //删除目录
  4778. err := os.Remove("ofile/" + filename + ".xlsx")
  4779. if err != nil {
  4780. logs.Error(lib.FuncName(), err)
  4781. }
  4782. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  4783. c.ServeJSON()
  4784. return
  4785. }
  4786. // Company_Name_List 获取所有出库的公司名称列表
  4787. func (c *StockController) Stock_Out_Company_Name_List() {
  4788. T_name := c.GetString("T_name") // 公司名称(模糊查询)
  4789. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  4790. R_List, err := StockOutDao.Read_Company_Name_List(T_name)
  4791. if err != nil {
  4792. c.Data["json"] = lib.JSONS{Code: 201, Msg: "查询失败"}
  4793. c.ServeJSON()
  4794. return
  4795. }
  4796. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: R_List}
  4797. c.ServeJSON()
  4798. return
  4799. }
  4800. // Company_Name_List 获取所有出库的公司名称列表
  4801. func (c *StockController) Stock_Out_Project_List() {
  4802. T_name := c.GetString("T_name") // 公司名称(模糊查询)
  4803. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  4804. R_List, err := StockOutDao.Read_Project_List(T_name)
  4805. if err != nil {
  4806. c.Data["json"] = lib.JSONS{Code: 201, Msg: "查询失败"}
  4807. c.ServeJSON()
  4808. return
  4809. }
  4810. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: R_List}
  4811. c.ServeJSON()
  4812. return
  4813. }