stock_service.go 388KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129
  1. package service
  2. import (
  3. "XT_New/models"
  4. "errors"
  5. "fmt"
  6. "github.com/jinzhu/gorm"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. func AddSigleDealer(dealer *models.Dealer) (error, *models.Dealer) {
  12. err := writeDb.Create(&dealer).Error
  13. return err, dealer
  14. }
  15. func ModifyDealer(dealer *models.Dealer) error {
  16. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", dealer.ID).Updates(map[string]interface{}{
  17. "dealer_name": dealer.DealerName,
  18. "contact": dealer.Contact,
  19. "contact_phone": dealer.ContactPhone,
  20. "platform_number": dealer.PlatformNumber,
  21. "email": dealer.Email,
  22. "contact_address": dealer.ContactAddress,
  23. "modifier": dealer.Modifier,
  24. "mtime": time.Now().Unix(),
  25. "remark": dealer.Remark,
  26. "wubi": dealer.WuBi,
  27. "pinyin": dealer.PinYin,
  28. }).Error
  29. return err
  30. }
  31. func FindAllDealerTotal(orgId int64) (total int64) {
  32. db := readDb.Model(&models.Dealer{})
  33. db = db.Where("org_id = ? ", orgId).Count(&total)
  34. return
  35. }
  36. // 获取经销商的名称
  37. func FindAllDealerName(orgId int64) (tmp []*models.Dealer, err error) {
  38. err = readDb.Model(&models.Dealer{}).Where("org_id = ? and status = 1", orgId).Find(&tmp).Error
  39. return
  40. }
  41. func FindAllDealerList(orgId int64, page int64, limit, key int64) (list []*models.Dealer, total int64, err error) {
  42. offset := (page - 1) * limit
  43. db := readDb.Model(&models.Dealer{})
  44. db = db.Where("org_id = ? AND status = 1", orgId)
  45. if key != 0 {
  46. db = db.Where("id = ? ", key)
  47. offset = 0
  48. limit = 10
  49. }
  50. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  51. return
  52. }
  53. func DeleteDealerById(id int64) error {
  54. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  55. return err
  56. }
  57. func FindDealerById(id int64) (*models.Dealer, error) {
  58. dealer := &models.Dealer{}
  59. err := readDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  60. return dealer, err
  61. }
  62. func AddSigleManufacturer(manufacturer *models.Manufacturer) (error, *models.Manufacturer) {
  63. err := writeDb.Create(&manufacturer).Error
  64. return err, manufacturer
  65. }
  66. func ModifyManufacturer(manufacturer *models.Manufacturer) error {
  67. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", manufacturer.ID).Updates(map[string]interface{}{
  68. "manufacturer_name": manufacturer.ManufacturerName,
  69. "contact": manufacturer.Contact,
  70. "contact_phone": manufacturer.ContactPhone,
  71. "platform_number": manufacturer.PlatformNumber,
  72. "email": manufacturer.Email,
  73. "contact_address": manufacturer.ContactAddress,
  74. "modifier": manufacturer.Modifier,
  75. "mtime": time.Now().Unix(),
  76. "remark": manufacturer.Remark,
  77. "wubi": manufacturer.WuBi,
  78. "pinyin": manufacturer.PinYin,
  79. }).Error
  80. return err
  81. }
  82. // 获取生产厂商名称
  83. func FindAllManufacturerName(orgid int64) (tmp []*models.Manufacturer, err error) {
  84. err = readDb.Model(&models.Manufacturer{}).Where("org_id = ? and status = 1", orgid).Find(&tmp).Error
  85. return
  86. }
  87. func FindAllManufacturerList(orgId int64, page int64, limit, key int64) (list []*models.Manufacturer, total int64, err error) {
  88. offset := (page - 1) * limit
  89. db := readDb.Model(&models.Manufacturer{})
  90. db = db.Where("org_id = ? AND status = 1", orgId)
  91. if key != 0 {
  92. db = db.Where("id = ?", key)
  93. offset = 0
  94. limit = 10
  95. }
  96. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  97. return
  98. }
  99. func DeleteManufacturerById(id int64) error {
  100. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  101. return err
  102. }
  103. func FindManufacturerById(id int64) (*models.Manufacturer, error) {
  104. dealer := &models.Manufacturer{}
  105. err := readDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  106. return dealer, err
  107. }
  108. func FindManufacturerTotal(orgId int64) (total int64) {
  109. db := readDb.Model(&models.Manufacturer{})
  110. db = db.Where("org_id = ?", orgId).Count(&total)
  111. return
  112. }
  113. func FindAllDealer(orgId int64) (dealers []*models.Dealer, err error) {
  114. err = readDb.Model(&models.Dealer{}).Where("org_id = ? AND status = 1", orgId).Find(&dealers).Error
  115. return dealers, err
  116. }
  117. func FindAllManufacturer(orgId int64) (manufacturer []*models.Manufacturer, err error) {
  118. err = readDb.Model(&models.Manufacturer{}).Where("org_id = ? AND status = 1", orgId).Find(&manufacturer).Error
  119. return manufacturer, err
  120. }
  121. func FindAllGoodTypeTotal(orgId int64) (total int64) {
  122. db := readDb.Model(&models.GoodsType{})
  123. db = db.Where("org_id = ?", orgId).Count(&total)
  124. return
  125. }
  126. func GetGoodsTypeIsExist(orgid int64) (*models.GoodsType, error) {
  127. goodsType := models.GoodsType{}
  128. err := XTReadDB().Where("org_id = ? and stock_attribute = 2 and status =1", orgid).Find(&goodsType).Error
  129. if err == gorm.ErrRecordNotFound {
  130. return nil, err
  131. }
  132. if err != nil {
  133. return nil, err
  134. }
  135. return &goodsType, nil
  136. }
  137. func GetGoodsTypeIsExistOne(orgid int64) (*models.GoodsType, error) {
  138. goodsType := models.GoodsType{}
  139. err := XTReadDB().Where("org_id = ? and stock_attribute = 3 and status = 1", orgid).Find(&goodsType).Error
  140. if err == gorm.ErrRecordNotFound {
  141. return nil, err
  142. }
  143. if err != nil {
  144. return nil, err
  145. }
  146. return &goodsType, nil
  147. }
  148. func AddSigleGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  149. err := writeDb.Create(&goodType).Error
  150. return err, goodType
  151. }
  152. func ModifyGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  153. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", goodType.ID).Updates(map[string]interface{}{
  154. "modifier": goodType.Modifier,
  155. "mtime": time.Now().Unix(),
  156. "remark": goodType.Remark,
  157. "type_name": goodType.TypeName,
  158. "out_stock": goodType.OutStock,
  159. "stock_attribute": goodType.StockAttribute,
  160. }).Error
  161. return err, goodType
  162. }
  163. func FindAllGoodTypeList(orgId int64, page int64, limit int64, keyword string) (list []*models.GoodsType, total int64, err error) {
  164. offset := (page - 1) * limit
  165. db := readDb.Model(&models.GoodsType{})
  166. db = db.Where("org_id=? AND status = 1", orgId)
  167. if len(keyword) > 0 {
  168. likeKey := "%" + keyword + "%"
  169. db = db.Where("type_code LIKE ? OR type_name LIKE ?", likeKey, likeKey)
  170. }
  171. err = db.Count(&total).Offset(offset).Limit(limit).Order("type desc, number desc,ctime desc").Find(&list).Error
  172. return
  173. }
  174. func DeleteGoodTypeById(id int64, operateId int64) error {
  175. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  176. writeDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId})
  177. return err
  178. }
  179. func FindGoodTypeById(id int64) (*models.GoodsType, error) {
  180. goodType := &models.GoodsType{}
  181. err := readDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).First(&goodType).Error
  182. return goodType, err
  183. }
  184. func FindAllGoodInfoTotal(orgId int64) (total int64) {
  185. db := readDb.Model(&models.GoodInfo{})
  186. db = db.Where("org_id = ?", orgId).Count(&total)
  187. return
  188. }
  189. func AddSigleGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  190. err := writeDb.Create(&goodInfo).Error
  191. return err, goodInfo
  192. }
  193. func ModifyGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  194. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", goodInfo.ID).Updates(map[string]interface{}{
  195. "good_type_id": goodInfo.GoodTypeId,
  196. "modifier": goodInfo.Modifier,
  197. "mtime": time.Now().Unix(),
  198. "remark": goodInfo.Remark,
  199. "specification_name": goodInfo.SpecificationName,
  200. "good_unit": goodInfo.GoodUnit,
  201. "buy_price": goodInfo.BuyPrice,
  202. "sell_price": goodInfo.SellPrice,
  203. "manufacturer": goodInfo.Manufacturer,
  204. "dealer": goodInfo.Dealer,
  205. "expiry_date_warn_day_count": goodInfo.ExpiryDateWarnDayCount,
  206. "stock_warn_count": goodInfo.StockWarnCount,
  207. "is_reuse": goodInfo.IsReuse,
  208. "good_name": goodInfo.GoodName,
  209. "pinyin": goodInfo.Pinyin,
  210. "wubi": goodInfo.Wubi,
  211. "good_kind": goodInfo.GoodKind,
  212. "medical_insurance_level": goodInfo.MedicalInsuranceLevel,
  213. "retail_price": goodInfo.PackingPrice,
  214. "medical_insurance_number": goodInfo.MedicalInsuranceNumber,
  215. "is_special_diseases": goodInfo.IsSpecialDiseases,
  216. "is_record": goodInfo.IsRecord,
  217. "statistics_category": goodInfo.StatisticsCategory,
  218. "good_status": goodInfo.GoodStatus,
  219. "default_count": goodInfo.DefaultCount,
  220. "sign": goodInfo.Sign,
  221. "is_default": goodInfo.IsDefault,
  222. "is_charge_use": goodInfo.IsChargeUse,
  223. "is_charge_predict": goodInfo.IsChargePredict,
  224. "is_statistics_work": goodInfo.IsStatisticsWork,
  225. "sort": goodInfo.Sort,
  226. "is_doctor_use": goodInfo.IsDoctorUse,
  227. "agent": goodInfo.Agent,
  228. "good_number": goodInfo.GoodNumber,
  229. "social_security_directory_code": goodInfo.SocialSecurityDirectoryCode,
  230. "special_medical": goodInfo.SpecialMedical,
  231. "production_type": goodInfo.ProductionType,
  232. "min_number": goodInfo.MinNumber,
  233. "packing_unit": goodInfo.PackingUnit,
  234. "packing_price": goodInfo.PackingPrice,
  235. "default_count_unit": goodInfo.DefaultCountUnit,
  236. "min_unit": goodInfo.MinUnit,
  237. "register_number": goodInfo.RegisterNumber,
  238. "provinces_code": goodInfo.ProvincesCode,
  239. "is_user": goodInfo.IsUser,
  240. "number": goodInfo.Number,
  241. "is_warehouse": goodInfo.IsWarehouse,
  242. "bbx01": goodInfo.Bbx01,
  243. "bby01": goodInfo.Bby01,
  244. "zuobiao_id": goodInfo.Bbx01,
  245. }).Error
  246. writeDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  247. writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  248. writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  249. writeDb.Model(&models.DialysisBeforePrepare{}).Where("good_id = ? AND user_org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  250. writeDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  251. writeDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  252. return err, goodInfo
  253. }
  254. func FindGoodInfoList(orgId int64, page int64, limit int64, keyword string, is_use int64, is_charge int64, good_kind int64, is_mark int64, manufacturer int64, is_type int64) (list []*models.GoodInfo, total int64, err error) {
  255. offset := (page - 1) * limit
  256. db := readDb.Model(&models.GoodInfo{})
  257. db = db.Where("org_id = ? AND status = 1", orgId)
  258. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  259. if len(keyword) > 0 {
  260. likeKey := "%" + keyword + "%"
  261. db = db.Where("good_code LIKE ? OR good_name LIKE ?", likeKey, likeKey)
  262. if is_use > 0 {
  263. if is_use == 2 {
  264. db = db.Where("find_in_set('停用',good_status) = 0")
  265. } else {
  266. db = db.Where("find_in_set('停用',good_status) > 0")
  267. }
  268. }
  269. if is_charge > 0 {
  270. if is_charge == 1 {
  271. db = db.Where("find_in_set('收费',good_status) > 0")
  272. } else {
  273. db = db.Where("find_in_set('收费',good_status) = 0")
  274. }
  275. }
  276. if good_kind > 0 {
  277. db = db.Where(" good_kind = ?", good_kind)
  278. }
  279. if is_mark > 0 {
  280. db = db.Where("is_mark = ?", is_mark)
  281. }
  282. if manufacturer > 0 {
  283. db = db.Where("manufacturer = ?", manufacturer)
  284. }
  285. if is_type > 0 {
  286. db = db.Where("good_type_id = ?", is_type)
  287. }
  288. } else {
  289. if is_use > 0 {
  290. if is_use == 2 {
  291. db = db.Where("find_in_set('停用',good_status) = 0")
  292. } else {
  293. db = db.Where("find_in_set('停用',good_status) > 0")
  294. }
  295. }
  296. if is_charge > 0 {
  297. if is_charge == 1 {
  298. db = db.Where("find_in_set('收费',good_status) > 0")
  299. } else {
  300. db = db.Where("find_in_set('收费',good_status) = 0")
  301. }
  302. }
  303. if good_kind > 0 {
  304. db = db.Where(" good_kind = ?", good_kind)
  305. }
  306. if is_mark > 0 {
  307. db = db.Where("is_mark = ?", is_mark)
  308. }
  309. if manufacturer > 0 {
  310. db = db.Where("manufacturer = ?", manufacturer)
  311. }
  312. if is_type > 0 {
  313. db = db.Where("good_type_id = ?", is_type)
  314. }
  315. }
  316. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  317. return
  318. }
  319. func DeleteGoodInfoById(id int64, operateId int64) error {
  320. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  321. return err
  322. }
  323. func FindGoodInfoByGoodId(id int64) (list []*models.GoodInfo, err error) {
  324. err = readDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  325. return list, err
  326. }
  327. func FindStockOutDetailGoodInfoByGoodId(id int64) (list []*models.AutomaticReduceDetail, err error) {
  328. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  329. return list, err
  330. }
  331. func FindGoodInfoById(id int64) (*models.GoodInfo, error) {
  332. goodInfo := &models.GoodInfo{}
  333. err := readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  334. return goodInfo, err
  335. }
  336. func FindAllGoodType(org_id int64) (goodType []*models.GoodsType, err error) {
  337. err = XTReadDB().Model(&models.GoodsType{}).Where("org_id = ? AND status = 1", org_id).Find(&goodType).Error
  338. return goodType, err
  339. }
  340. func FindAllGoodTypeOne(org_id int64) (goodType []*models.GoodsType, err error) {
  341. err = readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 and out_stock = 1", org_id).Find(&goodType).Error
  342. return goodType, err
  343. }
  344. func FindAllGoodInfo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  345. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1 and is_warehouse = 1 and find_in_set('停用',good_status) = 0", org_id).Find(&goodInfo).Error
  346. return goodInfo, err
  347. }
  348. func FindAllGoodInfoTwo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  349. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id).Find(&goodInfo).Error
  350. return goodInfo, err
  351. }
  352. func FindAllWarehouseTotal(org_id int64) (total int64, err error) {
  353. err = readDb.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  354. return total, err
  355. }
  356. func FindAllWarehouseTotalOne(org_id int64) (total int64, err error) {
  357. err = readDb.Model(&models.XtDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  358. return total, err
  359. }
  360. func AddSigleWarehouse(warehouse *models.Warehousing) error {
  361. err := writeDb.Create(&warehouse).Error
  362. return err
  363. }
  364. func UpdateWarehouse(id int64, storehouse_id int64) error {
  365. err := XTWriteDB().Model(&models.Warehousing{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  366. return err
  367. }
  368. func AddSigleDrugWarehouse(warehouse *models.DrugWarehouse) error {
  369. err := writeDb.Create(&warehouse).Error
  370. return err
  371. }
  372. func UpdateSigleDrugWarehouse(id int64, storehosue_id int64) error {
  373. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehosue_id}).Error
  374. return err
  375. }
  376. func AddSigleSalesReturn(salesReturn *models.SalesReturn) error {
  377. err := writeDb.Create(&salesReturn).Error
  378. return err
  379. }
  380. func AddSigleDrugSalesReturn(salesReturn *models.DrugSalesReturn) error {
  381. err := writeDb.Create(&salesReturn).Error
  382. return err
  383. }
  384. func UpDateGoodReturnStatus(ids []string) {
  385. writeDb.Model(&models.WarehousingInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_return": 1})
  386. }
  387. func UpDateCanCelStockStatus(ids []string) {
  388. writeDb.Model(&models.WarehouseOutInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_cancel": 1})
  389. }
  390. func CreateWarehousingInfo(warehousingInfo []*models.WarehousingInfo) (err error) {
  391. if len(warehousingInfo) > 0 {
  392. utx := writeDb.Begin()
  393. if len(warehousingInfo) > 0 {
  394. thisSQL := "INSERT INTO xt_warehouse_info (warehousing_id, good_id, good_type_id, number, product_date,expiry_date,warehousing_count,price,total_price,dealer,manufacturer,remark,ctime,mtime,status,org_id,warehousing_order,type,stock_count,license_number,packing_price,storehouse_id) VALUES "
  395. insertParams := make([]string, 0)
  396. insertData := make([]interface{}, 0)
  397. for _, info := range warehousingInfo {
  398. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  399. insertData = append(insertData, info.WarehousingId)
  400. insertData = append(insertData, info.GoodId)
  401. insertData = append(insertData, info.GoodTypeId)
  402. insertData = append(insertData, info.Number)
  403. insertData = append(insertData, info.ProductDate)
  404. insertData = append(insertData, info.ExpiryDate)
  405. insertData = append(insertData, info.WarehousingCount)
  406. insertData = append(insertData, info.Price)
  407. insertData = append(insertData, info.TotalPrice)
  408. insertData = append(insertData, info.Dealer)
  409. insertData = append(insertData, info.Manufacturer)
  410. insertData = append(insertData, info.Remark)
  411. insertData = append(insertData, info.Ctime)
  412. insertData = append(insertData, info.Mtime)
  413. insertData = append(insertData, info.Status)
  414. insertData = append(insertData, info.OrgId)
  415. insertData = append(insertData, info.WarehousingOrder)
  416. insertData = append(insertData, info.Type)
  417. insertData = append(insertData, info.WarehousingCount)
  418. insertData = append(insertData, info.LicenseNumber)
  419. insertData = append(insertData, info.PackingPrice)
  420. insertData = append(insertData, info.StorehouseId)
  421. }
  422. thisSQL += strings.Join(insertParams, ", ")
  423. err = utx.Exec(thisSQL, insertData...).Error
  424. if err != nil {
  425. utx.Rollback()
  426. return
  427. }
  428. }
  429. utx.Commit()
  430. }
  431. return
  432. }
  433. func CreateDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  434. if len(warehousingInfo) > 0 {
  435. utx := writeDb.Begin()
  436. if len(warehousingInfo) > 0 {
  437. thisSQL := "INSERT INTO xt_drug_warehouse_info (warehousing_id, drug_id, number, product_date,expiry_date,warehousing_count,price,total_price,dealer,manufacturer,remark,ctime,mtime,status,org_id,warehousing_order,type,retail_price,retail_total_price,stock_max_number,max_unit,min_unit,batch_number,stock_min_number,storehouse_id) VALUES "
  438. insertParams := make([]string, 0)
  439. insertData := make([]interface{}, 0)
  440. for _, info := range warehousingInfo {
  441. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  442. insertData = append(insertData, info.WarehousingId)
  443. insertData = append(insertData, info.DrugId)
  444. insertData = append(insertData, info.Number)
  445. insertData = append(insertData, info.ProductDate)
  446. insertData = append(insertData, info.ExpiryDate)
  447. insertData = append(insertData, info.WarehousingCount)
  448. insertData = append(insertData, info.Price)
  449. insertData = append(insertData, info.TotalPrice)
  450. insertData = append(insertData, info.Dealer)
  451. insertData = append(insertData, info.Manufacturer)
  452. insertData = append(insertData, info.Remark)
  453. insertData = append(insertData, info.Ctime)
  454. insertData = append(insertData, info.Mtime)
  455. insertData = append(insertData, info.Status)
  456. insertData = append(insertData, info.OrgId)
  457. insertData = append(insertData, info.WarehousingOrder)
  458. insertData = append(insertData, info.Type)
  459. insertData = append(insertData, info.RetailPrice)
  460. insertData = append(insertData, info.RetailTotalPrice)
  461. insertData = append(insertData, info.StockMaxNumber)
  462. insertData = append(insertData, info.MaxUnit)
  463. insertData = append(insertData, info.MinUnit)
  464. insertData = append(insertData, info.BatchNumber)
  465. insertData = append(insertData, info.StockMinNumber)
  466. insertData = append(insertData, info.StorehouseId)
  467. }
  468. thisSQL += strings.Join(insertParams, ", ")
  469. err = utx.Exec(thisSQL, insertData...).Error
  470. if err != nil {
  471. utx.Rollback()
  472. return
  473. }
  474. }
  475. utx.Commit()
  476. }
  477. return
  478. }
  479. func CreateDrugWarehousingInfoSix(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  480. if len(warehousingInfo) > 0 {
  481. utx := writeDb.Begin()
  482. if len(warehousingInfo) > 0 {
  483. thisSQL := "INSERT INTO xt_drug_warehouse_info (warehousing_id, drug_id, number, product_date,expiry_date,warehousing_count,price,total_price,dealer,manufacturer,remark,ctime,mtime,status,org_id,warehousing_order,type,retail_price,retail_total_price,stock_max_number,max_unit,min_unit,batch_number,stock_min_number,supply_warehouse_id,supply_warehouse_detail_info,storehouse_id,is_check) VALUES "
  484. insertParams := make([]string, 0)
  485. insertData := make([]interface{}, 0)
  486. for _, info := range warehousingInfo {
  487. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  488. insertData = append(insertData, info.WarehousingId)
  489. insertData = append(insertData, info.DrugId)
  490. insertData = append(insertData, info.Number)
  491. insertData = append(insertData, info.ProductDate)
  492. insertData = append(insertData, info.ExpiryDate)
  493. insertData = append(insertData, info.WarehousingCount)
  494. insertData = append(insertData, info.Price)
  495. insertData = append(insertData, info.TotalPrice)
  496. insertData = append(insertData, info.Dealer)
  497. insertData = append(insertData, info.Manufacturer)
  498. insertData = append(insertData, info.Remark)
  499. insertData = append(insertData, info.Ctime)
  500. insertData = append(insertData, info.Mtime)
  501. insertData = append(insertData, info.Status)
  502. insertData = append(insertData, info.OrgId)
  503. insertData = append(insertData, info.WarehousingOrder)
  504. insertData = append(insertData, info.Type)
  505. insertData = append(insertData, info.RetailPrice)
  506. insertData = append(insertData, info.RetailTotalPrice)
  507. insertData = append(insertData, info.StockMaxNumber)
  508. insertData = append(insertData, info.MaxUnit)
  509. insertData = append(insertData, info.MinUnit)
  510. insertData = append(insertData, info.BatchNumber)
  511. insertData = append(insertData, info.StockMinNumber)
  512. insertData = append(insertData, info.SupplyWarehouseId)
  513. insertData = append(insertData, info.SupplyWarehouseDetailInfo)
  514. insertData = append(insertData, info.StorehouseId)
  515. insertData = append(insertData, info.IsCheck)
  516. }
  517. thisSQL += strings.Join(insertParams, ", ")
  518. err = utx.Exec(thisSQL, insertData...).Error
  519. if err != nil {
  520. utx.Rollback()
  521. return
  522. }
  523. }
  524. utx.Commit()
  525. }
  526. return
  527. }
  528. func UpdateDrugInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  529. var total float64
  530. var min_number int64
  531. for _, item := range warehousingInfo {
  532. //查询刚入库耗材的库存
  533. count, _ := FindDrugWaresingCount(item.DrugId, item.OrgId)
  534. //更新药品库的总量
  535. info, _ := FindeDrugInfo(item.DrugId)
  536. min_number = count.StockMaxNumber * info.MinNumber
  537. str := strconv.FormatInt(min_number, 10)
  538. minFloat, _ := strconv.ParseFloat(str, 64)
  539. total = info.Total + minFloat
  540. lib := models.BaseDrugLib{
  541. Total: total,
  542. }
  543. UpdateBaseDrug(&lib, item.ID)
  544. }
  545. return err
  546. }
  547. func FindDrugWaresingCount(drug_id int64, orgid int64) (models.XtDrugWarehouseInfo, error) {
  548. info := models.XtDrugWarehouseInfo{}
  549. db := XTReadDB().Table("xt_drug_warehouse_info").Where("status = 1")
  550. err = db.Select("stock_max_number,drug_id").Where("drug_id = ? and org_id = ? and status =1", drug_id, orgid).Last(&info).Error
  551. return info, err
  552. }
  553. func FindeDrugInfo(drug_id int64) (models.BaseDrugLib, error) {
  554. lib := models.BaseDrugLib{}
  555. err := XTReadDB().Model(&lib).Where("id = ?", drug_id).Find(&lib).Error
  556. return lib, err
  557. }
  558. func UpdateBaseDrug(lib *models.BaseDrugLib, id int64) error {
  559. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"total": lib.Total}).Error
  560. return err
  561. }
  562. func UpdateBaseDrugOne(lib models.BaseDrugLib, id int64) error {
  563. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"retail_price": lib.RetailPrice, "last_price": lib.LastPrice}).Error
  564. return err
  565. }
  566. func FindeLastWarehouseInfo(id int64) (models.DrugWarehouseInfo, error) {
  567. info := models.DrugWarehouseInfo{}
  568. err := XTReadDB().Model(&info).Where("id=? and status = 1", id).Find(&info).Error
  569. return info, err
  570. }
  571. func GetLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  572. out := models.DrugWarehouseOut{}
  573. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  574. return out, err
  575. }
  576. func CreateDrugFlow(drugflow []*models.DrugFlow) (err error) {
  577. if len(drugflow) > 0 {
  578. utx := writeDb.Begin()
  579. if len(drugflow) > 0 {
  580. thisSQL := "INSERT INTO xt_drug_flow (warehousing_id, drug_id, number,batch_number,count,user_org_id,patient_id,system_time,consumable_type,is_sys,warehousing_order,warehouse_out_id,warehouse_out_order_number,is_edit,cancel_stock_id,cancel_order_number,manufacturer,dealer,creator,update_creator,status,ctime,mtime,price,warehousing_detail_id,warehouse_out_detail_id,cancel_out_detail_id,expire_date,product_date,max_unit,min_unit,storehouse_id) VALUES "
  581. insertParams := make([]string, 0)
  582. insertData := make([]interface{}, 0)
  583. for _, info := range drugflow {
  584. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  585. insertData = append(insertData, info.WarehousingId)
  586. insertData = append(insertData, info.DrugId)
  587. insertData = append(insertData, info.Number)
  588. insertData = append(insertData, info.BatchNumber)
  589. insertData = append(insertData, info.Count)
  590. insertData = append(insertData, info.UserOrgId)
  591. insertData = append(insertData, info.PatientId)
  592. insertData = append(insertData, info.SystemTime)
  593. insertData = append(insertData, info.ConsumableType)
  594. insertData = append(insertData, info.IsSys)
  595. insertData = append(insertData, info.WarehousingOrder)
  596. insertData = append(insertData, info.WarehouseOutId)
  597. insertData = append(insertData, info.WarehouseOutOrderNumber)
  598. insertData = append(insertData, info.IsEdit)
  599. insertData = append(insertData, info.CancelStockId)
  600. insertData = append(insertData, info.CancelOrderNumber)
  601. insertData = append(insertData, info.Manufacturer)
  602. insertData = append(insertData, info.Dealer)
  603. insertData = append(insertData, info.Creator)
  604. insertData = append(insertData, info.UpdateCreator)
  605. insertData = append(insertData, info.Status)
  606. insertData = append(insertData, info.Ctime)
  607. insertData = append(insertData, info.Mtime)
  608. insertData = append(insertData, info.Price)
  609. insertData = append(insertData, info.WarehousingDetailId)
  610. insertData = append(insertData, info.WarehouseOutDetailId)
  611. insertData = append(insertData, info.CancelOutDetailId)
  612. insertData = append(insertData, info.ExpireDate)
  613. insertData = append(insertData, info.ProductDate)
  614. insertData = append(insertData, info.MaxUnit)
  615. insertData = append(insertData, info.MinUnit)
  616. insertData = append(insertData, info.StorehouseId)
  617. }
  618. thisSQL += strings.Join(insertParams, ", ")
  619. err = utx.Exec(thisSQL, insertData...).Error
  620. if err != nil {
  621. utx.Rollback()
  622. return
  623. }
  624. }
  625. utx.Commit()
  626. }
  627. return
  628. }
  629. func GetDrugFlowIsExist(warhouseing int64, drug_id int64) (*models.DrugFlow, error) {
  630. info := models.DrugFlow{}
  631. err = XTReadDB().Where("warehousing_id = ? and drug_id = ? and status = 1", warhouseing, drug_id).Find(&info).Error
  632. if err == gorm.ErrRecordNotFound {
  633. return nil, err
  634. }
  635. if err != nil {
  636. return nil, err
  637. }
  638. return &info, nil
  639. }
  640. func GetDrugByWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  641. info := models.XtDrugWarehouseInfo{}
  642. err := XTReadDB().Where("id=? and status = 1", id).Find(&info).Error
  643. return info, err
  644. }
  645. func UpdateDrugWarehouseingInfo(id int64, info models.XtDrugWarehouseInfo) error {
  646. warehouseInfo := models.XtDrugWarehouseInfo{}
  647. err := XTWriteDB().Model(&warehouseInfo).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_max_number": info.StockMaxNumber}).Error
  648. return err
  649. }
  650. func CreateDrugFlowOne(flow models.DrugFlow) error {
  651. ut := writeDb.Begin()
  652. err := ut.Create(&flow).Error
  653. if err != nil {
  654. ut.Rollback()
  655. return err
  656. }
  657. ut.Commit()
  658. return err
  659. }
  660. func CreateNewDrugFlowOne(flow models.DrugFlow, tx *gorm.DB) error {
  661. err := tx.Create(&flow).Error
  662. if err != nil {
  663. tx.Rollback()
  664. return err
  665. }
  666. return err
  667. }
  668. func UpdateDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64, id int64) (models.DrugAutomaticReduceDetail, error) {
  669. detail := models.DrugAutomaticReduceDetail{}
  670. err := XTWriteDB().Model(&detail).Where("patient_id = ? and record_time = ? and drug_id = ? and status= 1 and advice_id =? ", patientid, recorddate, drugid, advice_id).Updates(map[string]interface{}{"status": 0}).Error
  671. err = XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("patient_id =? and sys_record_time =? and drug_id = ? and status = 1 and advice_id =?", patientid, recorddate, drugid, advice_id).Updates(map[string]interface{}{"status": 0}).Error
  672. return detail, err
  673. }
  674. func UpdateDrugFlowTens(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.DrugFlow, error) {
  675. flow := models.DrugFlow{}
  676. err := XTWriteDB().Model(&flow).Where("patient_id =? and warehouse_out_order_number = ? and drug_id = ? and status = 1 and advice_id =?", patientid, warehouse_out_number, drug_id, advice_id).Updates(map[string]interface{}{"is_read": 1}).Error
  677. return flow, err
  678. }
  679. func CreateDrugFlowTwo(flow *models.DrugFlow) error {
  680. tx := XTWriteDB().Begin()
  681. err := tx.Create(&flow).Error
  682. if err != nil {
  683. tx.Rollback()
  684. return err
  685. }
  686. tx.Commit()
  687. return err
  688. }
  689. func FindLastWarehousingInfo(order string) (info models.WarehousingInfo, err error) {
  690. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  691. return info, err
  692. }
  693. func GetLastWarehousingInfo(goodid int64) (models.WarehousingInfo, error) {
  694. info := models.WarehousingInfo{}
  695. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  696. return info, err
  697. }
  698. func CreatedWarehouseingDetail(info *models.WarehousingInfo) error {
  699. err := XTWriteDB().Create(&info).Error
  700. return err
  701. }
  702. func FindFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  703. if storehouse_id == 0 {
  704. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND good_type_id = ? AND status = 1 and stock_count > 0", good_id, good_type_id).Order("ctime").First(&info).Error
  705. }
  706. if storehouse_id > 0 {
  707. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND good_type_id = ? AND status = 1 and stock_count > 0 and storehouse_id =? ", good_id, good_type_id, storehouse_id).Order("ctime").First(&info).Error
  708. }
  709. return info, err
  710. }
  711. func FindNewFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64, tx *gorm.DB) (info models.WarehousingInfo, err error) {
  712. if storehouse_id == 0 {
  713. err = tx.Model(&models.WarehousingInfo{}).Where("good_id = ? AND good_type_id = ? AND status = 1 and stock_count > 0", good_id, good_type_id).Order("ctime").First(&info).Error
  714. if err != gorm.ErrRecordNotFound {
  715. if err != nil {
  716. tx.Rollback()
  717. return info, err
  718. }
  719. }
  720. }
  721. if storehouse_id > 0 {
  722. err = tx.Model(&models.WarehousingInfo{}).Where("good_id = ? AND good_type_id = ? AND status = 1 and stock_count > 0 and storehouse_id =? ", good_id, good_type_id, storehouse_id).Order("ctime").First(&info).Error
  723. if err != gorm.ErrRecordNotFound {
  724. if err != nil {
  725. tx.Rollback()
  726. return info, err
  727. }
  728. }
  729. }
  730. return info, err
  731. }
  732. func FindFirstWarehousingInfoByStockTwo(good_id int64, good_type_id int64, storehouse_id int64) (info models.VmWarehousingInfo, err error) {
  733. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  734. if good_id > 0 {
  735. db = db.Where("x.good_id = ?", good_id)
  736. }
  737. if good_type_id > 0 {
  738. db = db.Where("x.good_type_id = ?", good_type_id)
  739. }
  740. if storehouse_id > 0 {
  741. db = db.Where("x.storehouse_id = ?", storehouse_id)
  742. }
  743. err = db.Select("sum(x.stock_count) as count").Scan(&info).Error
  744. return info, err
  745. }
  746. func FindWarehousingInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  747. info := models.WarehousingInfo{}
  748. err := XTReadDB().Model(&info).Where("id= ? and status = 1", id).Find(&info).Error
  749. return info, err
  750. }
  751. func FindWarehousingInfoTen(goodId int64, storehouse_id int64) (models.WarehousingInfo, error) {
  752. info := models.WarehousingInfo{}
  753. var err error
  754. if storehouse_id == 0 {
  755. err = XTReadDB().Select(" good_id,sum(stock_count) as stock_count").Where("good_id = ? and status = 1 and is_check = 1", goodId).Find(&info).Error
  756. }
  757. if storehouse_id > 0 {
  758. err = XTReadDB().Select(" good_id,sum(stock_count) as stock_count").Where("good_id = ? and status = 1 and storehouse_id = ? and is_check = 1", goodId, storehouse_id).Find(&info).Error
  759. }
  760. return info, err
  761. }
  762. //药品先进先出,找出最先入库的批次
  763. func FindLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  764. err = readDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) and storehouse_id = ? and is_check = 1", drug_id, storehouse_id).Order("ctime").First(&info).Error
  765. return info, err
  766. }
  767. func FindNewLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64, tx *gorm.DB) (info models.XtDrugWarehouseInfo, err error) {
  768. err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) and storehouse_id = ? and is_check = 1", drug_id, storehouse_id).Order("ctime").First(&info).Error
  769. if err != nil {
  770. tx.Rollback()
  771. return info, err
  772. }
  773. return info, err
  774. }
  775. func FindLastDrugWarehousingInfoByIDSix(drug_id int64, warehouse_out_id int64, is_source int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  776. if is_source == 1 {
  777. err = readDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) and supply_warehouse_id = ? and storehouse_id = ?", drug_id, warehouse_out_id, storehouse_id).Order("ctime").First(&info).Error
  778. }
  779. if is_source == 2 || is_source == 0 {
  780. err = readDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) and storehouse_id = ?", drug_id, storehouse_id).Order("ctime").First(&info).Error
  781. }
  782. return info, err
  783. }
  784. // 出库时先进先出,找出最先入库的批次
  785. func FindLastWarehousingInfoByID(drug_id int64) (info models.WarehousingInfo, err error) {
  786. err = readDb.Model(&models.WarehousingInfo{}).Where("drup_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) ", drug_id).Order("ctime").First(&info).Error
  787. return info, err
  788. }
  789. //
  790. //// 出库时先进先出,找出最先入库的批次
  791. //func (drug_id int64) (info models.WarehousingInfo, err error) {
  792. // err = readDb.Model(&models.WarehousingInfo{}).Where("drup_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) ", drug_id).Order("ctime").First(&info).Error
  793. // return info, err
  794. //}
  795. func FindLastDrugWarehousingInfo(order string) (info models.DrugWarehouseInfo, err error) {
  796. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  797. return info, err
  798. }
  799. func FindAllWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64, storehouse_id int64, check_type int64) (list []*models.Warehousing, total int64, err error) {
  800. offset := (page - 1) * limit
  801. db := readDb.Model(&models.Warehousing{})
  802. db = db.Where("xt_warehouse.org_id = ? AND xt_warehouse.status = 1 AND xt_warehouse.type = ?", orgId, types)
  803. if len(keywords) > 0 {
  804. likeKey := "%" + keywords + "%"
  805. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  806. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_warehouse.warehousing_order LIKE ? or xt_warehouse.id in(?)", likeKey, likeKey, ids).Group("xt_warehouse.id")
  807. }
  808. if startTime > 0 {
  809. db = db.Where("xt_warehouse.operation_time >=?", startTime)
  810. }
  811. if endTime > 0 {
  812. db = db.Where("xt_warehouse.operation_time<= ?", endTime)
  813. }
  814. if storehouse_id > 0 {
  815. db = db.Where("xt_warehouse.storehouse_id = ?", storehouse_id)
  816. }
  817. if check_type == 0 {
  818. db = db.Where("xt_warehouse.is_check = 0 or xt_warehouse.is_check = 1 or xt_warehouse.is_check = 2")
  819. }
  820. if check_type > 0 {
  821. db = db.Where("xt_warehouse.is_check = ?", check_type)
  822. }
  823. err = db.Count(&total).Offset(offset).Limit(limit).Order("xt_warehouse.ctime desc").Find(&list).Error
  824. return
  825. }
  826. func GetGoodInforByGoodName(keyword string, orgid int64) (goodInfo []*models.GoodInfo, err error) {
  827. db := XTReadDB().Model(&goodInfo).Where("status = 1")
  828. if orgid > 0 {
  829. db = db.Where("org_id = ?", orgid)
  830. }
  831. if len(keyword) > 0 {
  832. likeKey := "%" + keyword + "%"
  833. db = db.Where("good_name like ?", likeKey)
  834. }
  835. err = db.Find(&goodInfo).Error
  836. return goodInfo, err
  837. }
  838. func GetWarehoureOrderInfoByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  839. db := XTReadDB().Model(&info).Where("status =1")
  840. if len(good_id) > 0 {
  841. db = db.Where("good_id in (?)", good_id)
  842. }
  843. if startime > 0 {
  844. db = db.Where("ctime >=?", startime)
  845. }
  846. if endtime > 0 {
  847. db = db.Where("ctime<=?", endtime)
  848. }
  849. if orgid > 0 {
  850. db = db.Where("org_id = ?", orgid)
  851. }
  852. if storehouse_id > 0 {
  853. db = db.Where("storehouse_id = ?", storehouse_id)
  854. }
  855. err = db.Find(&info).Error
  856. return info, err
  857. }
  858. func GetWarehoureOrderOutByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehouseOutInfo, err error) {
  859. db := XTReadDB().Model(&info).Where("status =1")
  860. if len(good_id) > 0 {
  861. db = db.Where("good_id in (?)", good_id)
  862. }
  863. if startime > 0 {
  864. db = db.Where("ctime >=?", startime)
  865. }
  866. if endtime > 0 {
  867. db = db.Where("ctime<=?", endtime)
  868. }
  869. if orgid > 0 {
  870. db = db.Where("org_id = ?", orgid)
  871. }
  872. if storehouse_id > 0 {
  873. db = db.Where("storehouse_id = ?", storehouse_id)
  874. }
  875. err = db.Find(&info).Error
  876. return info, err
  877. }
  878. type VMDrugWarehouse struct {
  879. ID int64 `gorm:"column:id" json:"id" form:"id"`
  880. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  881. OperationTime int64 `gorm:"column:operation_time" json:"operation_time" form:"operation_time"`
  882. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  883. Creater int64 `gorm:"column:creater" json:"creater" form:"creater"`
  884. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  885. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  886. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  887. Status int64 `gorm:"column:status" json:"status" form:"status"`
  888. WarehousingTime int64 `gorm:"column:warehousing_time" json:"warehousing_time" form:"warehousing_time"`
  889. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  890. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  891. Type int64 `gorm:"column:type" json:"type" form:"type"`
  892. Manufacturers *models.Manufacturer `gorm:"ForeignKey:Manufacturer;AssociationForeignKey:ID" json:"manufacturers"`
  893. Dealers *models.Dealer `gorm:"ForeignKey:Dealer;AssociationForeignKey:ID" json:"dealers"`
  894. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  895. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  896. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  897. IsCheck int64 `gorm:"column:is_check" json:"is_check" form:"is_check"`
  898. Checker int64 `gorm:"column:checker" json:"checker" form:"checker"`
  899. }
  900. func (VMDrugWarehouse) TableName() string {
  901. return "xt_drug_warehouse"
  902. }
  903. type VMDrugWarehouseInfo struct {
  904. ID int64 `gorm:"column:id" json:"id" form:"id"`
  905. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id" form:"warehousing_id"`
  906. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  907. Number string `gorm:"column:number" json:"number" form:"number"`
  908. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  909. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  910. WarehousingCount int64 `gorm:"column:warehousing_count" json:"warehousing_count" form:"warehousing_count"`
  911. Price float64 `gorm:"column:price" json:"price" form:"price"`
  912. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  913. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  914. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  915. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  916. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  917. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  918. Status int64 `gorm:"column:status" json:"status" form:"status"`
  919. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  920. IsReturn int64 `gorm:"column:is_return" json:"is_return" form:"is_return"`
  921. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  922. Type int64 `gorm:"column:type" json:"type" form:"type"`
  923. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  924. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  925. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  926. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  927. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  928. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  929. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  930. SupplyWarehouseDetailInfo int64 `gorm:"column:supply_warehouse_detail_info" json:"supply_warehouse_detail_info" form:"supply_warehouse_detail_info"`
  931. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  932. }
  933. func (VMDrugWarehouseInfo) TableName() string {
  934. return "xt_drug_warehouse_info"
  935. }
  936. type Drug struct {
  937. ID int64 `gorm:"column:id" json:"id" form:"id"`
  938. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  939. Pinyin string `gorm:"column:pinyin" json:"pinyin" form:"pinyin"`
  940. Wubi string `gorm:"column:wubi" json:"wubi" form:"wubi"`
  941. DrugAlias string `gorm:"column:drug_alias" json:"drug_alias" form:"drug_alias"`
  942. DrugAliasPinyin string `gorm:"column:drug_alias_pinyin" json:"drug_alias_pinyin" form:"drug_alias_pinyin"`
  943. DrugAliasWubi string `gorm:"column:drug_alias_wubi" json:"drug_alias_wubi" form:"drug_alias_wubi"`
  944. DrugCategory int64 `gorm:"column:drug_category" json:"drug_category" form:"drug_category"`
  945. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  946. DrugType int64 `gorm:"column:drug_type" json:"drug_type" form:"drug_type"`
  947. DrugStockLimit string `gorm:"column:drug_stock_limit" json:"drug_stock_limit" form:"drug_stock_limit"`
  948. DrugOriginPlace string `gorm:"column:drug_origin_place" json:"drug_origin_place" form:"drug_origin_place"`
  949. DrugDosageForm int64 `gorm:"column:drug_dosage_form" json:"drug_dosage_form" form:"drug_dosage_form"`
  950. MedicalInsuranceLevel int64 `gorm:"column:medical_insurance_level" json:"medical_insurance_level" form:"medical_insurance_level"`
  951. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  952. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  953. UnitMatrixing string `gorm:"column:unit_matrixing" json:"unit_matrixing" form:"unit_matrixing"`
  954. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  955. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  956. DrugControl int64 `gorm:"column:drug_control" json:"drug_control" form:"drug_control"`
  957. Number string `gorm:"column:number" json:"number" form:"number"`
  958. DrugClassify string `gorm:"column:drug_classify" json:"drug_classify" form:"drug_classify"`
  959. DrugDose float64 `gorm:"column:drug_dose" json:"drug_dose" form:"drug_dose"`
  960. DrugDoseUnit int64 `gorm:"column:drug_dose_unit" json:"drug_dose_unit" form:"drug_dose_unit"`
  961. MedicalInsuranceNumber string `gorm:"column:medical_insurance_number" json:"medical_insurance_number" form:"medical_insurance_number"`
  962. ProvincesCode string `gorm:"column:provinces_code" json:"provinces_code" form:"provinces_code"`
  963. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  964. PharmacologyCategory int64 `gorm:"column:pharmacology_category" json:"pharmacology_category" form:"pharmacology_category"`
  965. StatisticsCategory int64 `gorm:"column:statistics_category" json:"statistics_category" form:"statistics_category"`
  966. Code string `gorm:"column:code" json:"code" form:"code"`
  967. IsSpecialDiseases int64 `gorm:"column:is_special_diseases" json:"is_special_diseases" form:"is_special_diseases"`
  968. IsRecord int64 `gorm:"column:is_record" json:"is_record" form:"is_record"`
  969. Agent string `gorm:"column:agent" json:"agent" form:"agent"`
  970. DrugStatus string `gorm:"column:drug_status" json:"drug_status" form:"drug_status"`
  971. LimitRemark string `gorm:"column:limit_remark" json:"limit_remark" form:"limit_remark"`
  972. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  973. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  974. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  975. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  976. Label int64 `gorm:"column:label" json:"label" form:"label"`
  977. Sort int64 `gorm:"column:sort" json:"sort" form:"sort"`
  978. IsUseDoctorAdvice int64 `gorm:"column:is_use_doctor_advice" json:"is_use_doctor_advice" form:"is_use_doctor_advice"`
  979. IsDefault int64 `gorm:"column:is_default" json:"is_default" form:"is_default"`
  980. IsChargePredict int64 `gorm:"column:is_charge_predict" json:"is_charge_predict" form:"is_charge_predict"`
  981. IsStatisticsWork int64 `gorm:"column:is_statistics_work" json:"is_statistics_work" form:"is_statistics_work"`
  982. IsChargeUse int64 `gorm:"column:is_charge_use" json:"is_charge_use" form:"is_charge_use"`
  983. Status int64 `gorm:"column:status" json:"status" form:"status"`
  984. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  985. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  986. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  987. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  988. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  989. DoseCode string `gorm:"column:dose_code" json:"dose_code" form:"dose_code"`
  990. RecordDate int64 `gorm:"column:record_date" json:"record_date" form:"record_date"`
  991. DrugRemark string `gorm:"column:drug_remark" json:"drug_remark" form:"drug_remark"`
  992. SocialSecurityDirectoryCode string `gorm:"column:social_security_directory_code" json:"social_security_directory_code" form:"social_security_directory_code"`
  993. IsMark int64 `gorm:"column:is_mark" json:"is_mark" form:"is_mark"`
  994. PrescriptionMark int64 `gorm:"column:prescription_mark" json:"prescription_mark" form:"prescription_mark"`
  995. HospApprFlag int64 `gorm:"column:hosp_appr_flag" json:"hosp_appr_flag" form:"hosp_appr_flag"`
  996. LmtUsedFlag int64 `gorm:"column:lmt_used_flag" json:"lmt_used_flag" form:"lmt_used_flag"`
  997. Total float64 `gorm:"column:total" json:"total" form:"total"`
  998. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  999. MinNumber int64 `gorm:"column:min_number" json:"min_number" form:"min_number"`
  1000. Dose string `gorm:"column:dose" json:"dose" form:"dose"`
  1001. DoseUnit string `gorm:"column:dose_unit" json:"dose_unit" form:"dose_unit"`
  1002. DrugDay string `gorm:"column:drug_day" json:"drug_day" form:"drug_day"`
  1003. MinPrice float64 `gorm:"column:min_price" json:"min_price" form:"min_price"`
  1004. }
  1005. func (Drug) TableName() string {
  1006. return "xt_base_drug"
  1007. }
  1008. func FindAllDrugWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*VMDrugWarehouse, total int64, err error) {
  1009. db := readDb.Model(&VMDrugWarehouse{})
  1010. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1011. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1012. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1013. if len(keywords) > 0 {
  1014. likeKey := "%" + keywords + "%"
  1015. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1016. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse.manufacturer")
  1017. db = db.Where("xt_manufacturer.manufacturer_name LIKE ? OR sgj_user_admin_role.user_name LIKE ? OR xt_drug_warehouse.warehousing_order LIKE ?", likeKey, likeKey, likeKey).Group("xt_drug_warehouse.id")
  1018. }
  1019. if startTime > 0 {
  1020. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1021. }
  1022. if endTime > 0 {
  1023. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1024. }
  1025. db = db.Count(&total)
  1026. offset := (page - 1) * limit
  1027. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1028. return
  1029. }
  1030. func FindAllDrugWarehousingListOne(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64, storehouse_id int64, check_type int64) (list []*VMDrugWarehouse, total int64, err error) {
  1031. db := readDb.Model(&VMDrugWarehouse{})
  1032. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1033. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1034. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1035. if len(keywords) > 0 {
  1036. likeKey := "%" + keywords + "%"
  1037. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1038. db = db.Where(" sgj_user_admin_role.user_name LIKE ? OR xt_drug_warehouse.warehousing_order LIKE ? OR xt_drug_warehouse.id in(?)", likeKey, likeKey, ids).Group("xt_drug_warehouse.id")
  1039. }
  1040. if startTime > 0 {
  1041. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1042. }
  1043. if endTime > 0 {
  1044. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1045. }
  1046. if storehouse_id > 0 {
  1047. db = db.Where("xt_drug_warehouse.storehouse_id = ?", storehouse_id)
  1048. }
  1049. if check_type > 0 {
  1050. db = db.Where("xt_drug_warehouse.is_check = ?", check_type)
  1051. }
  1052. db = db.Count(&total)
  1053. offset := (page - 1) * limit
  1054. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1055. return
  1056. }
  1057. func FindWarehousingInfoById(id int64) (list []*models.WarehousingInfo, err error) {
  1058. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1059. return list, err
  1060. }
  1061. func FindWarehousingInfoByIdOne(id []int64) (list []*models.WarehousingInfo, err error) {
  1062. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id in(?) AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1063. return list, err
  1064. }
  1065. func FindDrugWarehousingInfoById(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1066. err = readDb.Model(&VMDrugWarehouseInfo{}).Preload("Drug", "status = 1 AND org_id = ? AND find_in_set('',drug_status) = 0", org_id).Where("warehousing_id = ? AND status = 1", id).Find(&list).Error
  1067. return list, err
  1068. }
  1069. func FindDrugWarehousingInfoByIdThree(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1070. err = readDb.Model(&VMDrugWarehouseInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehousing_id = ? AND status = 1", id).Find(&list).Error
  1071. return list, err
  1072. }
  1073. func FindDrugWarehousingInfoByIdOne(id []string, org_id int64) (list []*models.StDrugWarehouseInfo, err error) {
  1074. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  1075. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  1076. fmt.Println(table)
  1077. if len(id) > 0 {
  1078. db = db.Where("x.warehousing_id in(?)", id)
  1079. }
  1080. if org_id > 0 {
  1081. db = db.Where("x.org_id = ?", org_id)
  1082. }
  1083. err = db.Select("x.id,x.max_unit as limax_unit,x.warehousing_id,x.drug_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.warehouseing_unit,x.stock_max_number,x.stock_min_number,x.price,x.remark,x.batch_number,x.batch_number,x.manufacturer,x.dealer,t.drug_name,t.last_price,t.dose_unit,t.dose,t.min_number,t.min_unit,t.max_unit,t.drug_type").Joins("left join xt_base_drug as t on t.id = x.drug_id").Scan(&list).Error
  1084. return list, err
  1085. }
  1086. func FindWarehousingById(id int64) (warehousing models.Warehousing, err error) {
  1087. err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1088. return warehousing, err
  1089. }
  1090. func FindWarehousingByIdOne(id []string) (warehousing []*models.Warehousing, err error) {
  1091. err = readDb.Model(&models.Warehousing{}).Where("id in(?) and status = 1", id).Find(&warehousing).Error
  1092. //err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1093. return warehousing, err
  1094. }
  1095. func FindDrugWarehousingById(id int64, org_id int64) (warehousing VMDrugWarehouse, err error) {
  1096. err = readDb.Model(&VMDrugWarehouse{}).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ?", org_id).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1097. return warehousing, err
  1098. }
  1099. func FindDrugWarehousingByIdOne(id []string, org_id int64) (warehousing []*models.DrugWarehouse, err error) {
  1100. db := XTReadDB().Table("xt_drug_warehouse as x").Where("x.status = 1")
  1101. if len(id) > 0 {
  1102. db = db.Where("x.id in(?)", id)
  1103. }
  1104. if org_id > 0 {
  1105. db = db.Where("x.org_id = ?", org_id)
  1106. }
  1107. err = db.Select("x.id,x.warehousing_order,x.operation_time,x.org_id,x.creater,x.ctime,x.modifier,x.warehousing_time").Find(&warehousing).Error
  1108. return warehousing, err
  1109. }
  1110. //func FindDrugWarehousingById(id int64) (warehousing models.DrugWarehouse, err error) {
  1111. // err = readDb.Model(&models.DrugWarehouse{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1112. // return warehousing, err
  1113. //
  1114. //}
  1115. func GetInfoByIds(ids []string) (info []*models.WarehousingInfo, err error) {
  1116. err = readDb.Model(&models.WarehousingInfo{}).Preload("Warehousing", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1117. return
  1118. }
  1119. func GetWarehousOutInfoByIds(ids []string) (info []*models.WarehouseOutInfo, err error) {
  1120. err = readDb.Model(&models.WarehouseOutInfo{}).Preload("WarehouseOut", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1121. return
  1122. }
  1123. func CreateSalesReturnInfo(salesReturnInfo []*models.SalesReturnInfo) (err error) {
  1124. if len(salesReturnInfo) > 0 {
  1125. utx := writeDb.Begin()
  1126. if len(salesReturnInfo) > 0 {
  1127. thisSQL := "INSERT INTO xt_sales_return_info (good_id, sales_return_id, good_type_id, count, price,total,ctime,status,org_id,order_number,type,dealer,manufacturer) VALUES "
  1128. insertParams := make([]string, 0)
  1129. insertData := make([]interface{}, 0)
  1130. for _, info := range salesReturnInfo {
  1131. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1132. insertData = append(insertData, info.GoodId)
  1133. insertData = append(insertData, info.SalesReturnId)
  1134. insertData = append(insertData, info.GoodTypeId)
  1135. insertData = append(insertData, info.Count)
  1136. insertData = append(insertData, info.Price)
  1137. insertData = append(insertData, info.Total)
  1138. insertData = append(insertData, info.Ctime)
  1139. insertData = append(insertData, info.Status)
  1140. insertData = append(insertData, info.OrgId)
  1141. insertData = append(insertData, info.OrderNumber)
  1142. insertData = append(insertData, info.Type)
  1143. insertData = append(insertData, info.Dealer)
  1144. insertData = append(insertData, info.Manufacturer)
  1145. }
  1146. thisSQL += strings.Join(insertParams, ", ")
  1147. err = utx.Exec(thisSQL, insertData...).Error
  1148. if err != nil {
  1149. utx.Rollback()
  1150. return
  1151. }
  1152. }
  1153. utx.Commit()
  1154. }
  1155. return
  1156. }
  1157. func CreateDrugSalesReturnInfo(salesReturnInfo []*models.DrugSalesReturnInfo) (err error) {
  1158. if len(salesReturnInfo) > 0 {
  1159. utx := writeDb.Begin()
  1160. if len(salesReturnInfo) > 0 {
  1161. thisSQL := "INSERT INTO xt_drug_sales_return_info (drug_id,sales_return_id, count, price,total,ctime,status,org_id,order_number,type,dealer,manufacturer,retail_price,retail_total_price) VALUES "
  1162. insertParams := make([]string, 0)
  1163. insertData := make([]interface{}, 0)
  1164. for _, info := range salesReturnInfo {
  1165. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1166. insertData = append(insertData, info.DrugId)
  1167. insertData = append(insertData, info.SalesReturnId)
  1168. insertData = append(insertData, info.Count)
  1169. insertData = append(insertData, info.Price)
  1170. insertData = append(insertData, info.Total)
  1171. insertData = append(insertData, info.Ctime)
  1172. insertData = append(insertData, info.Status)
  1173. insertData = append(insertData, info.OrgId)
  1174. insertData = append(insertData, info.OrderNumber)
  1175. insertData = append(insertData, info.Type)
  1176. insertData = append(insertData, info.Dealer)
  1177. insertData = append(insertData, info.Manufacturer)
  1178. insertData = append(insertData, info.RetailPrice)
  1179. insertData = append(insertData, info.RetailTotalPrice)
  1180. }
  1181. thisSQL += strings.Join(insertParams, ", ")
  1182. err = utx.Exec(thisSQL, insertData...).Error
  1183. if err != nil {
  1184. utx.Rollback()
  1185. return
  1186. }
  1187. }
  1188. utx.Commit()
  1189. }
  1190. return
  1191. }
  1192. func FindAllSalesReturnTotal(org_id int64) (total int64, err error) {
  1193. err = readDb.Model(&models.SalesReturn{}).Where("org_id = ?", org_id).Count(&total).Error
  1194. return total, err
  1195. }
  1196. func FindAllCancelStockTotal(org_id int64) (total int64, err error) {
  1197. err = readDb.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1198. return total, err
  1199. }
  1200. func FindNewAllCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1201. err = tx.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1202. if err != gorm.ErrRecordNotFound {
  1203. if err != nil {
  1204. tx.Rollback()
  1205. return total, err
  1206. }
  1207. }
  1208. return total, err
  1209. }
  1210. func FindAllDrugCancelStockTotal(org_id int64) (total int64, err error) {
  1211. err = readDb.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1212. return total, err
  1213. }
  1214. func FindNewAllDrugCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1215. err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1216. if err != nil {
  1217. tx.Rollback()
  1218. return total, err
  1219. }
  1220. return total, err
  1221. }
  1222. //func FindAllDrugCancelStockTotalTX(org_id int64,tx *gorm.DB) (total int64, err error) {
  1223. // err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1224. // return total, err
  1225. //}
  1226. func FindAllReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.SalesReturn, total int64, err error) {
  1227. db := readDb.Model(&models.SalesReturn{})
  1228. db = db.Where("xt_sales_return.org_id = ? AND xt_sales_return.status = 1 AND xt_sales_return.type = ?", orgId, types)
  1229. if len(keywords) > 0 {
  1230. likeKey := "%" + keywords + "%"
  1231. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  1232. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_sales_return.manufacturer")
  1233. db = db.Where("xt_manufacturer.manufacturer_name LIKE ? OR sgj_user_admin_role.user_name LIKE ? OR xt_sales_return.order_number LIKE ?", likeKey, likeKey, likeKey).Group("xt_sales_return.id")
  1234. }
  1235. if startTime > 0 {
  1236. db = db.Where("xt_sales_return.opera_time >=?", startTime)
  1237. }
  1238. if endTime > 0 {
  1239. db = db.Where("xt_sales_return.opera_time<= ?", endTime)
  1240. }
  1241. db = db.Count(&total)
  1242. offset := (page - 1) * limit
  1243. err = db.Offset(offset).Limit(limit).Order("xt_sales_return.ctime desc").Find(&list).Error
  1244. return
  1245. }
  1246. func FindAllDrugReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugSalesReturn, total int64, err error) {
  1247. db := readDb.Model(&models.DrugSalesReturn{})
  1248. db = db.Where("xt_drug_sales_return.org_id = ? AND xt_drug_sales_return.status = 1 ", orgId)
  1249. if len(keywords) > 0 {
  1250. likeKey := "%" + keywords + "%"
  1251. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_sales_return.creater")
  1252. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_sales_return.manufacturer")
  1253. db = db.Where("xt_manufacturer.manufacturer_name LIKE ? OR sgj_user_admin_role.user_name LIKE ? OR xt_drug_sales_return.order_number LIKE ?", likeKey, likeKey, likeKey).Group("xt_drug_sales_return.id")
  1254. }
  1255. if startTime > 0 {
  1256. db = db.Where("xt_drug_sales_return.opera_time >=?", startTime)
  1257. }
  1258. if endTime > 0 {
  1259. db = db.Where("xt_drug_sales_return.opera_time<= ?", endTime)
  1260. }
  1261. db = db.Count(&total)
  1262. offset := (page - 1) * limit
  1263. err = db.Offset(offset).Limit(limit).Order("xt_drug_sales_return.ctime desc").Find(&list).Error
  1264. return
  1265. }
  1266. func FindReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  1267. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  1268. return list, err
  1269. }
  1270. func FindCancelStockInfoById(id int64) (list []*models.CancelStockInfo, err error) {
  1271. err = readDb.Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? AND status = 1", id).Preload("WarehousingInfo", "status = 1").Preload("GoodInfo", "status = 1").Find(&list).Error
  1272. return list, err
  1273. }
  1274. type DrugCancelStockInfo struct {
  1275. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1276. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1277. CancelStockId int64 `gorm:"column:cancel_stock_id" json:"cancel_stock_id" form:"cancel_stock_id"`
  1278. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1279. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1280. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1281. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1282. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1283. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1284. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1285. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1286. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1287. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  1288. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1289. Dealer string `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1290. Manufacturer string `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1291. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1292. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1293. Number string `gorm:"column:number" json:"number" form:"number"`
  1294. RegisterAccount string `gorm:"column:register_account" json:"register_account" form:"register_account"`
  1295. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1296. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1297. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1298. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1299. BatchNumberId int64 `gorm:"column:batch_number_id" json:"batch_number_id" form:"batch_number_id"`
  1300. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1301. DrugWarehouseInfo []*models.XtDrugWarehouseInfo `gorm:"ForeignKey:DrugId;AssociationForeignKey:DrugId" json:"drug_warehouse_info"`
  1302. }
  1303. func (DrugCancelStockInfo) TableName() string {
  1304. return "xt_drug_cancel_stock_info"
  1305. }
  1306. func FindDrugCancelStockInfoById(id int64, org_id int64) (list []*DrugCancelStockInfo, err error) {
  1307. err = readDb.Model(&DrugCancelStockInfo{}).Preload("Drug", "status = 1 AND org_id = ? ", org_id).Preload("DrugWarehouseInfo", "status = 1 AND org_id = ? ", org_id).Where("cancel_stock_id = ? AND status = 1", id).Find(&list).Error
  1308. return list, err
  1309. }
  1310. func FindAllWarehousingInfo(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehousingInfo, total int64, err error) {
  1311. db := readDb.Model(&models.WarehousingInfo{})
  1312. db = db.Where("org_id = ? AND status = 1", orgId)
  1313. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  1314. if startTime > 0 {
  1315. db = db.Where("ctime >=?", startTime)
  1316. }
  1317. if endTime > 0 {
  1318. db = db.Where("ctime<= ?", endTime)
  1319. }
  1320. db = db.Count(&total)
  1321. offset := (page - 1) * limit
  1322. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1323. return list, total, err
  1324. }
  1325. func FindAllWarehouseOut(org_id int64) (total int64, err error) {
  1326. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1327. return total, err
  1328. }
  1329. func FindAllDrugWarehouseOut(org_id int64) (total int64, err error) {
  1330. err = readDb.Model(&models.DrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1331. return total, err
  1332. }
  1333. func AddSigleWarehouseOut(warehouseOut *models.WarehouseOut) error {
  1334. err := writeDb.Create(&warehouseOut).Error
  1335. return err
  1336. }
  1337. func AddNewSigleWarehouseOut(warehouseOut *models.WarehouseOut, tx *gorm.DB) error {
  1338. err := tx.Create(&warehouseOut).Error
  1339. if err != nil {
  1340. tx.Rollback()
  1341. return err
  1342. }
  1343. return err
  1344. }
  1345. func UpdateSingleWarehoseOut(id int64, storehouse_id int64) error {
  1346. err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1347. return err
  1348. }
  1349. func AddSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut) error {
  1350. err := writeDb.Create(&warehouseOut).Error
  1351. return err
  1352. }
  1353. func AddNewSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut, tx *gorm.DB) error {
  1354. err := tx.Create(&warehouseOut).Error
  1355. if err != nil {
  1356. tx.Rollback()
  1357. return err
  1358. }
  1359. return err
  1360. }
  1361. func UpdateSingleDrugWarehouseOut(id int64, storehouse_id int64) error {
  1362. err := XTWriteDB().Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1363. return err
  1364. }
  1365. func CreateWarehousingOutInfo(warehouseOutInfo []*models.WarehouseOutInfo) (err error) {
  1366. if len(warehouseOutInfo) > 0 {
  1367. utx := writeDb.Begin()
  1368. if len(warehouseOutInfo) > 0 {
  1369. thisSQL := "INSERT INTO xt_warehouse_out_info (warehouse_out_id, good_id, good_type_id, product_date,expiry_date,count,price,total_price,remark,ctime,status,org_id,warehouse_out_order_number,type,dealer,manufacturer,number,license_number) VALUES "
  1370. insertParams := make([]string, 0)
  1371. insertData := make([]interface{}, 0)
  1372. for _, info := range warehouseOutInfo {
  1373. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1374. insertData = append(insertData, info.WarehouseOutId)
  1375. insertData = append(insertData, info.GoodId)
  1376. insertData = append(insertData, info.GoodTypeId)
  1377. insertData = append(insertData, info.ProductDate)
  1378. insertData = append(insertData, info.ExpiryDate)
  1379. insertData = append(insertData, info.Count)
  1380. insertData = append(insertData, info.Price)
  1381. insertData = append(insertData, info.TotalPrice)
  1382. insertData = append(insertData, info.Remark)
  1383. insertData = append(insertData, info.Ctime)
  1384. insertData = append(insertData, info.Status)
  1385. insertData = append(insertData, info.OrgId)
  1386. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1387. insertData = append(insertData, info.Type)
  1388. insertData = append(insertData, info.Dealer)
  1389. insertData = append(insertData, info.Manufacturer)
  1390. insertData = append(insertData, info.Number)
  1391. insertData = append(insertData, info.LicenseNumber)
  1392. }
  1393. thisSQL += strings.Join(insertParams, ", ")
  1394. err = utx.Exec(thisSQL, insertData...).Error
  1395. if err != nil {
  1396. utx.Rollback()
  1397. return
  1398. }
  1399. }
  1400. utx.Commit()
  1401. }
  1402. return
  1403. }
  1404. func CreateDrugWarehousingOutInfo(warehouseOutInfo []*models.DrugWarehouseOutInfo) (err error) {
  1405. if len(warehouseOutInfo) > 0 {
  1406. utx := writeDb.Begin()
  1407. if len(warehouseOutInfo) > 0 {
  1408. thisSQL := "INSERT INTO xt_drug_warehouse_out_info (warehouse_out_id, drug_id, product_date,expiry_date,count,price,total_price,remark,ctime,status,org_id,warehouse_out_order_number,type,dealer,manufacturer,retail_price,retail_total_price,number,batch_number,count_unit) VALUES "
  1409. insertParams := make([]string, 0)
  1410. insertData := make([]interface{}, 0)
  1411. for _, info := range warehouseOutInfo {
  1412. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1413. insertData = append(insertData, info.WarehouseOutId)
  1414. insertData = append(insertData, info.DrugId)
  1415. insertData = append(insertData, info.ProductDate)
  1416. insertData = append(insertData, info.ExpiryDate)
  1417. insertData = append(insertData, info.Count)
  1418. insertData = append(insertData, info.Price)
  1419. insertData = append(insertData, info.TotalPrice)
  1420. insertData = append(insertData, info.Remark)
  1421. insertData = append(insertData, info.Ctime)
  1422. insertData = append(insertData, info.Status)
  1423. insertData = append(insertData, info.OrgId)
  1424. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1425. insertData = append(insertData, info.Type)
  1426. insertData = append(insertData, info.Dealer)
  1427. insertData = append(insertData, info.Manufacturer)
  1428. insertData = append(insertData, info.RetailPrice)
  1429. insertData = append(insertData, info.RetailTotalPrice)
  1430. insertData = append(insertData, info.Number)
  1431. insertData = append(insertData, info.BatchNumber)
  1432. insertData = append(insertData, info.CountUnit)
  1433. }
  1434. thisSQL += strings.Join(insertParams, ", ")
  1435. err = utx.Exec(thisSQL, insertData...).Error
  1436. if err != nil {
  1437. utx.Rollback()
  1438. return
  1439. }
  1440. }
  1441. utx.Commit()
  1442. }
  1443. return
  1444. }
  1445. func FindLastWarehousingOutInfo(order string) (info models.WarehouseOutInfo, err error) {
  1446. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1447. return info, err
  1448. }
  1449. func FindLastDrugWarehousingOutInfo(order string) (info models.DrugWarehouseOutInfo, err error) {
  1450. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1451. return info, err
  1452. }
  1453. func FindAllWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.WarehouseOut, total int64, err error) {
  1454. db := readDb.Model(&models.WarehouseOut{})
  1455. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1456. if len(keywords) > 0 {
  1457. likeKey := "%" + keywords + "%"
  1458. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1459. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse_out.manufacturer")
  1460. db = db.Where("xt_manufacturer.manufacturer_name LIKE ? OR sgj_user_admin_role.user_name LIKE ? OR xt_warehouse_out.warehouse_out_order_number LIKE ?", likeKey, likeKey, likeKey).Group("xt_warehouse_out.id")
  1461. }
  1462. if startTime > 0 {
  1463. db = db.Where("xt_warehouse_out.ctime >=?", startTime)
  1464. }
  1465. if endTime > 0 {
  1466. db = db.Where("xt_warehouse_out.ctime<= ?", endTime)
  1467. }
  1468. db = db.Count(&total)
  1469. offset := (page - 1) * limit
  1470. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1471. return list, total, err
  1472. }
  1473. func FindAllWarehouseOutListOne(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64, storehouse_id int64, way_type int64, check_type int64) (list []*models.WarehouseOut, total int64, err error) {
  1474. db := readDb.Model(&models.WarehouseOut{})
  1475. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1476. if len(keywords) > 0 {
  1477. likeKey := "%" + keywords + "%"
  1478. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1479. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_warehouse_out.warehouse_out_order_number LIKE ? or xt_warehouse_out.id in(?)", likeKey, likeKey, ids).Group("xt_warehouse_out.id")
  1480. }
  1481. if startTime > 0 {
  1482. db = db.Where("xt_warehouse_out.warehouse_out_time >=?", startTime)
  1483. }
  1484. if endTime > 0 {
  1485. db = db.Where("xt_warehouse_out.warehouse_out_time<= ?", endTime)
  1486. }
  1487. if storehouse_id > 0 {
  1488. db = db.Where("xt_warehouse_out.storehouse_id = ?", storehouse_id)
  1489. }
  1490. if way_type == 0 {
  1491. db = db.Where("xt_warehouse_out.is_sys = 0 or xt_warehouse_out.is_sys = 1 or xt_warehouse_out.is_sys = 12 or xt_warehouse_out.is_sys = 5")
  1492. }
  1493. if way_type == 1 {
  1494. db = db.Where("xt_warehouse_out.is_sys = 0")
  1495. }
  1496. if way_type == 2 {
  1497. db = db.Where("xt_warehouse_out.is_sys = 1")
  1498. }
  1499. if way_type == 3 {
  1500. db = db.Where("xt_warehouse_out.is_sys = 12")
  1501. }
  1502. if way_type == 4 {
  1503. db = db.Where("xt_warehouse_out.is_sys = 5")
  1504. }
  1505. if check_type > 0 {
  1506. db = db.Where("xt_warehouse_out.is_check = ?", check_type)
  1507. }
  1508. if check_type == 0 {
  1509. db = db.Where("xt_warehouse_out.is_check = 1 or xt_warehouse_out.is_check = 2")
  1510. }
  1511. db = db.Count(&total)
  1512. offset := (page - 1) * limit
  1513. if orgId == 10191 {
  1514. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1515. } else {
  1516. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1517. }
  1518. return list, total, err
  1519. }
  1520. func FindAllDrugWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugWarehouseOut, total int64, err error) {
  1521. db := readDb.Model(&models.DrugWarehouseOut{})
  1522. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1523. if len(keywords) > 0 {
  1524. likeKey := "%" + keywords + "%"
  1525. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1526. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse_out.manufacturer")
  1527. db = db.Where("xt_manufacturer.manufacturer_name LIKE ? OR sgj_user_admin_role.user_name LIKE ? OR xt_drug_warehouse_out.warehouse_out_order_number LIKE ?", likeKey, likeKey, likeKey).Group("xt_drug_warehouse_out.id")
  1528. }
  1529. if startTime > 0 {
  1530. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1531. }
  1532. if endTime > 0 {
  1533. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1534. }
  1535. db = db.Count(&total)
  1536. offset := (page - 1) * limit
  1537. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1538. return list, total, err
  1539. }
  1540. func FindAllDrugWarehouseOutListOne(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64, storehouse_id int64, way_type int64, check_type int64) (list []*models.DrugWarehouseOut, total int64, err error) {
  1541. db := readDb.Model(&models.DrugWarehouseOut{})
  1542. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1543. if len(keywords) > 0 {
  1544. likeKey := "%" + keywords + "%"
  1545. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1546. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_drug_warehouse_out.warehouse_out_order_number LIKE ? or xt_drug_warehouse_out.id in(?) ", likeKey, likeKey, ids).Group("xt_drug_warehouse_out.id")
  1547. }
  1548. if startTime > 0 {
  1549. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1550. }
  1551. if endTime > 0 {
  1552. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1553. }
  1554. if storehouse_id > 0 {
  1555. db = db.Where("xt_drug_warehouse_out.storehouse_id = ?", storehouse_id)
  1556. }
  1557. if check_type > 0 {
  1558. db = db.Where("xt_drug_warehouse_out.is_check = ?", check_type)
  1559. }
  1560. if way_type == 0 {
  1561. db = db.Where("xt_drug_warehouse_out.is_sys = 0 || xt_drug_warehouse_out.is_sys = 1 || xt_drug_warehouse_out.is_sys = 12 || xt_drug_warehouse_out.is_sys = 5")
  1562. }
  1563. if way_type == 1 {
  1564. db = db.Where("xt_drug_warehouse_out.is_sys = 0")
  1565. }
  1566. if way_type == 2 {
  1567. db = db.Where("xt_drug_warehouse_out.is_sys = 1")
  1568. }
  1569. if way_type == 3 {
  1570. db = db.Where("xt_drug_warehouse_out.is_sys = 12")
  1571. }
  1572. if way_type == 4 {
  1573. db = db.Where("xt_drug_warehouse_out.is_sys = 15")
  1574. }
  1575. db = db.Count(&total)
  1576. offset := (page - 1) * limit
  1577. if orgId == 10191 {
  1578. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1579. } else {
  1580. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1581. }
  1582. return list, total, err
  1583. }
  1584. func FindWarehouseOutInfoById(id int64) (list []*models.WarehouseOutInfo, err error) {
  1585. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? AND status = 1 AND count <> 0 AND good_id <> 0", id).Preload("GoodInfo", "status = 1").Order("good_type_id desc").Find(&list).Error
  1586. return list, err
  1587. }
  1588. type DrugWarehouseOutInfo struct {
  1589. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1590. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  1591. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1592. WarehousingOutTarget int64 `gorm:"column:warehousing_out_target" json:"warehousing_out_target" form:"warehousing_out_target"`
  1593. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1594. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  1595. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1596. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  1597. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1598. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1599. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1600. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1601. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1602. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1603. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1604. IsCancel int64 `gorm:"column:is_cancel" json:"is_cancel" form:"is_cancel"`
  1605. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  1606. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1607. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1608. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1609. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  1610. SysRecordTime int64 `gorm:"column:sys_record_time" json:"sys_record_time" form:"sys_record_time"`
  1611. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1612. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1613. Number string `gorm:"column:number" json:"number" form:"number"`
  1614. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1615. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1616. WarehouseInfoId int64 `gorm:"column:warehouse_info_id" json:"warehouse_info_id" form:"warehouse_info_id"`
  1617. SupplyCancelOutId int64 `gorm:"column:supply_cancel_out_id" json:"supply_cancel_out_id" form:"supply_cancel_out_id"`
  1618. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1619. AdminUserId int64 `gorm:"column:admin_user_id" json:"admin_user_id" form:"admin_user_id"`
  1620. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  1621. StockCount string `gorm:"column:stock_count" json:"stock_count" form:"stock_count"`
  1622. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  1623. }
  1624. func (DrugWarehouseOutInfo) TableName() string {
  1625. return "xt_drug_warehouse_out_info"
  1626. }
  1627. func FindDrugWarehouseOutInfoById(id int64, org_id int64) (list []*DrugWarehouseOutInfo, err error) {
  1628. err = readDb.Model(&DrugWarehouseOutInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehouse_out_id = ? AND status = 1 ", id).Find(&list).Error
  1629. return list, err
  1630. }
  1631. func FindWarehouseInfoByGoodId(good_id int64) (int64, *models.WarehousingInfo) {
  1632. info := &models.WarehousingInfo{}
  1633. type WarehouseInfoCountAmount struct {
  1634. Total int64
  1635. }
  1636. var result WarehouseInfoCountAmount
  1637. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(warehousing_count) as total").Scan(&result)
  1638. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).First(info)
  1639. return result.Total, info
  1640. }
  1641. func FindAllSalesReturnCountByGoodId(good_id int64) (int64, error) {
  1642. type SalesReturnCountAmount struct {
  1643. Total int64
  1644. }
  1645. var result SalesReturnCountAmount
  1646. err = readDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1647. return result.Total, err
  1648. }
  1649. func FindAllWarehouseOutInfoByGoodId(good_id int64) (int64, error) {
  1650. type WarehouseOutInfoCountAmount struct {
  1651. Total int64
  1652. }
  1653. var result WarehouseOutInfoCountAmount
  1654. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1655. return result.Total, err
  1656. }
  1657. func FindAllCancleStockInfoByGoodId(good_id int64) (int64, error) {
  1658. type CancleStockoCountAmount struct {
  1659. Total int64
  1660. }
  1661. var result CancleStockoCountAmount
  1662. err = readDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1663. return result.Total, err
  1664. }
  1665. func FindWarehouseInfoByGoodTypeId(good_type_id int64) (models.WarehousingInfo, error) {
  1666. info := models.WarehousingInfo{}
  1667. err := readDb.Model(&models.WarehousingInfo{}).Where("good_type_id = ? AND status = 1", good_type_id).First(&info).Error
  1668. return info, err
  1669. }
  1670. func GetCancelStockDetailByOrderNumber(ordernumber string, orgid int64) (*models.CancelStock, error) {
  1671. stock := models.CancelStock{}
  1672. err := XTReadDB().Where("order_number = ? and org_id = ? and status = 1", ordernumber, orgid).Find(&stock).Error
  1673. if err == gorm.ErrRecordNotFound {
  1674. return nil, err
  1675. }
  1676. if err != nil {
  1677. return nil, err
  1678. }
  1679. return &stock, nil
  1680. }
  1681. func GetCancelStockDetailByOrderNumberOne(return_time int64, orgid int64) (*models.CancelStock, error) {
  1682. stock := models.CancelStock{}
  1683. err := XTReadDB().Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  1684. if err == gorm.ErrRecordNotFound {
  1685. return nil, err
  1686. }
  1687. if err != nil {
  1688. return nil, err
  1689. }
  1690. return &stock, nil
  1691. }
  1692. func GetNewCancelStockDetailByOrderNumberOne(return_time int64, orgid int64, tx *gorm.DB) (*models.CancelStock, error) {
  1693. stock := models.CancelStock{}
  1694. err := tx.Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  1695. if err != gorm.ErrRecordNotFound {
  1696. if err != nil {
  1697. tx.Rollback()
  1698. return &stock, err
  1699. }
  1700. }
  1701. return &stock, err
  1702. }
  1703. func AddSigleCancelStock(cancelStock *models.CancelStock) error {
  1704. err := writeDb.Create(&cancelStock).Error
  1705. return err
  1706. }
  1707. func AddNewSigleCancelStock(cancelStock *models.CancelStock, tx *gorm.DB) error {
  1708. err := tx.Create(&cancelStock).Error
  1709. if err != nil {
  1710. tx.Rollback()
  1711. return err
  1712. }
  1713. return err
  1714. }
  1715. func UpdateSigleCancelStock(id int64, storehouse_id int64) error {
  1716. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1717. return err
  1718. }
  1719. func GetLastCancelStockById(orgid int64) (models.CancelStock, error) {
  1720. stock := models.CancelStock{}
  1721. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1722. return stock, err
  1723. }
  1724. func GetNewLastCancelStockById(orgid int64, tx *gorm.DB) (models.CancelStock, error) {
  1725. stock := models.CancelStock{}
  1726. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1727. if err != gorm.ErrRecordNotFound {
  1728. if err != nil {
  1729. tx.Rollback()
  1730. return stock, err
  1731. }
  1732. }
  1733. return stock, err
  1734. }
  1735. func GetLastDrugCancelStockById(orgid int64) (models.DrugCancelStock, error) {
  1736. stock := models.DrugCancelStock{}
  1737. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1738. return stock, err
  1739. }
  1740. func GetNewLastDrugCancelStockById(orgid int64, tx *gorm.DB) (models.DrugCancelStock, error) {
  1741. stock := models.DrugCancelStock{}
  1742. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1743. if err != gorm.ErrRecordNotFound {
  1744. if err != nil {
  1745. tx.Rollback()
  1746. return stock, err
  1747. }
  1748. }
  1749. return stock, err
  1750. }
  1751. func GetManufactureById(id int64) (models.Manufacturer, error) {
  1752. manufacturer := models.Manufacturer{}
  1753. err := XTReadDB().Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  1754. return manufacturer, err
  1755. }
  1756. func GetNewManufactureById(id int64, tx *gorm.DB) (models.Manufacturer, error) {
  1757. manufacturer := models.Manufacturer{}
  1758. err := tx.Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  1759. if err != nil {
  1760. tx.Rollback()
  1761. return manufacturer, err
  1762. }
  1763. return manufacturer, err
  1764. }
  1765. func GetDealerById(id int64) (models.Dealer, error) {
  1766. dealer := models.Dealer{}
  1767. err := XTReadDB().Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  1768. return dealer, err
  1769. }
  1770. func GetNewDealerById(id int64, tx *gorm.DB) (models.Dealer, error) {
  1771. dealer := models.Dealer{}
  1772. err := tx.Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  1773. if err != gorm.ErrRecordNotFound {
  1774. if err != nil {
  1775. tx.Rollback()
  1776. return dealer, err
  1777. }
  1778. }
  1779. return dealer, err
  1780. }
  1781. func AddSigleDrugCancelStock(cancelStock *models.DrugCancelStock) error {
  1782. err := writeDb.Create(&cancelStock).Error
  1783. return err
  1784. }
  1785. func AddNewSigleDrugCancelStock(cancelStock *models.DrugCancelStock, tx *gorm.DB) error {
  1786. err := tx.Create(&cancelStock).Error
  1787. if err != nil {
  1788. tx.Rollback()
  1789. return err
  1790. }
  1791. return err
  1792. }
  1793. func UpdateSingleDrugCancelStock(id int64, storehouse_id int64) error {
  1794. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1795. return err
  1796. }
  1797. //func AddSigleDrugCancelStockTX(cancelStock *models.DrugCancelStock,tx *gorm.DB) error {
  1798. // err := tx.Create(&cancelStock).Error
  1799. // return err
  1800. //}
  1801. func CreateCancelStockInfo(cancelStockInfo []*models.CancelStockInfo) (err error) {
  1802. if len(cancelStockInfo) > 0 {
  1803. utx := writeDb.Begin()
  1804. if len(cancelStockInfo) > 0 {
  1805. thisSQL := "INSERT INTO xt_cancel_stock_info (good_id, cancel_stock_id, good_type_id, count, price,total,ctime,status,org_id,order_number,type,dealer,manufacturer,number,register_account,remark,product_date,expiry_date,warehouse_info_id,storehouse_id) VALUES "
  1806. insertParams := make([]string, 0)
  1807. insertData := make([]interface{}, 0)
  1808. for _, info := range cancelStockInfo {
  1809. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1810. insertData = append(insertData, info.GoodId)
  1811. insertData = append(insertData, info.CancelStockId)
  1812. insertData = append(insertData, info.GoodTypeId)
  1813. insertData = append(insertData, info.Count)
  1814. insertData = append(insertData, info.Price)
  1815. insertData = append(insertData, info.Total)
  1816. insertData = append(insertData, info.Ctime)
  1817. insertData = append(insertData, info.Status)
  1818. insertData = append(insertData, info.OrgId)
  1819. insertData = append(insertData, info.OrderNumber)
  1820. insertData = append(insertData, info.Type)
  1821. insertData = append(insertData, info.Dealer)
  1822. insertData = append(insertData, info.Manufacturer)
  1823. insertData = append(insertData, info.Number)
  1824. insertData = append(insertData, info.RegisterAccount)
  1825. insertData = append(insertData, info.Remark)
  1826. insertData = append(insertData, info.ProductDate)
  1827. insertData = append(insertData, info.ExpiryDate)
  1828. insertData = append(insertData, info.WarehouseInfoId)
  1829. insertData = append(insertData, info.WarehouseInfoId)
  1830. }
  1831. thisSQL += strings.Join(insertParams, ", ")
  1832. err = utx.Exec(thisSQL, insertData...).Error
  1833. if err != nil {
  1834. utx.Rollback()
  1835. return
  1836. }
  1837. }
  1838. utx.Commit()
  1839. }
  1840. return
  1841. }
  1842. func CreateCancelStockInfoTwo(info *models.DrugCancelStockInfo) error {
  1843. err := XTWriteDB().Create(&info).Error
  1844. return err
  1845. }
  1846. //func CreateCancelStockInfoTwoTX(info *models.DrugCancelStockInfo,tx *gorm.DB) error {
  1847. // err := tx.Create(&info).Error
  1848. // return err
  1849. //}
  1850. func GetLastStockInfoThree(drugid int64) (models.DrugCancelStockInfo, error) {
  1851. info := models.DrugCancelStockInfo{}
  1852. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  1853. return info, err
  1854. }
  1855. //func GetLastStockInfoThreeTX(drugid int64,tx *gorm.DB) (models.DrugCancelStockInfo, error) {
  1856. // info := models.DrugCancelStockInfo{}
  1857. // err := tx.Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  1858. // return info, err
  1859. //}
  1860. func CreateCancelStockInfoOne(info *models.CancelStockInfo) error {
  1861. err := XTWriteDB().Create(&info).Error
  1862. return err
  1863. }
  1864. func CreateNewCancelStockInfoOne(info *models.CancelStockInfo, tx *gorm.DB) error {
  1865. err := XTWriteDB().Create(&info).Error
  1866. if err != nil {
  1867. tx.Rollback()
  1868. return err
  1869. }
  1870. return err
  1871. }
  1872. func GetLastCancelStockInfoByGoodId(goodId int64) (models.CancelStockInfo, error) {
  1873. info := models.CancelStockInfo{}
  1874. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  1875. return info, err
  1876. }
  1877. func GetLastNewCancelStockInfoByGoodId(goodId int64, tx *gorm.DB) (models.CancelStockInfo, error) {
  1878. info := models.CancelStockInfo{}
  1879. err := tx.Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  1880. if err != gorm.ErrRecordNotFound {
  1881. if err != nil {
  1882. tx.Rollback()
  1883. return info, err
  1884. }
  1885. }
  1886. return info, err
  1887. }
  1888. func GetLastDrugCancelStockInfoById(drugid int64) (models.DrugCancelStockInfo, error) {
  1889. info := models.DrugCancelStockInfo{}
  1890. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Last(&info).Error
  1891. return info, err
  1892. }
  1893. func CreateDrugCancelStockInfo(cancelStockInfo []*models.DrugCancelStockInfo) (err error) {
  1894. if len(cancelStockInfo) > 0 {
  1895. utx := writeDb.Begin()
  1896. if len(cancelStockInfo) > 0 {
  1897. thisSQL := "INSERT INTO xt_drug_cancel_stock_info (drug_id,cancel_stock_id, count, price,total,ctime,status,org_id,order_number,type,dealer,manufacturer,retail_price,retail_total_price,register_account,batch_number,remark,max_unit,product_date,expiry_date,batch_number_id) VALUES "
  1898. insertParams := make([]string, 0)
  1899. insertData := make([]interface{}, 0)
  1900. for _, info := range cancelStockInfo {
  1901. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1902. insertData = append(insertData, info.DrugId)
  1903. insertData = append(insertData, info.CancelStockId)
  1904. insertData = append(insertData, info.Count)
  1905. insertData = append(insertData, info.Price)
  1906. insertData = append(insertData, info.Total)
  1907. insertData = append(insertData, info.Ctime)
  1908. insertData = append(insertData, info.Status)
  1909. insertData = append(insertData, info.OrgId)
  1910. insertData = append(insertData, info.OrderNumber)
  1911. insertData = append(insertData, info.Type)
  1912. insertData = append(insertData, info.Dealer)
  1913. insertData = append(insertData, info.Manufacturer)
  1914. insertData = append(insertData, info.RetailPrice)
  1915. insertData = append(insertData, info.RetailTotalPrice)
  1916. insertData = append(insertData, info.RegisterAccount)
  1917. insertData = append(insertData, info.BatchNumber)
  1918. insertData = append(insertData, info.Remark)
  1919. insertData = append(insertData, info.MaxUnit)
  1920. insertData = append(insertData, info.ProductDate)
  1921. insertData = append(insertData, info.ExpiryDate)
  1922. insertData = append(insertData, info.BatchNumberId)
  1923. }
  1924. thisSQL += strings.Join(insertParams, ", ")
  1925. err = utx.Exec(thisSQL, insertData...).Error
  1926. if err != nil {
  1927. utx.Rollback()
  1928. return
  1929. }
  1930. }
  1931. utx.Commit()
  1932. }
  1933. return
  1934. }
  1935. func FindAllCancelStockList(orgId int64) (list []*models.CancelStock, err error) {
  1936. db := readDb.Model(&models.CancelStock{})
  1937. db = db.Where("org_id = ? AND status = 1", orgId)
  1938. err = db.Order("ctime desc").Find(&list).Error
  1939. return
  1940. }
  1941. func FindAllStockOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  1942. db := readDb.Model(&models.WarehousingInfo{})
  1943. db = db.Where("org_id = ? AND status = 1", orgId)
  1944. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  1945. if startTime > 0 {
  1946. db = db.Where("ctime >=?", startTime)
  1947. }
  1948. if endTime > 0 {
  1949. db = db.Where("ctime<= ?", endTime)
  1950. }
  1951. db = db.Count(&total)
  1952. offset := (page - 1) * limit
  1953. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1954. return
  1955. }
  1956. func FindAllStockInfo(orgId int64, page int64, limit int64, keyword string, startime int64, endtime int64, type_name int64) (list []*models.StockInfo, total int64, err error) {
  1957. db := XTReadDB().Model(&models.StockInfo{})
  1958. if startime > 0 {
  1959. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1960. return db.Where("org_id = ? AND status = 1 and sys_record_time>=?", orgId, startime)
  1961. })
  1962. }
  1963. if startime == 0 || endtime == 0 {
  1964. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1965. return db.Where("org_id = ? AND status = 1", orgId)
  1966. })
  1967. }
  1968. if endtime > 0 {
  1969. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1970. return db.Where("org_id = ? AND status = 1 and sys_record_time >=? and sys_record_time<=?", orgId, startime, endtime)
  1971. })
  1972. }
  1973. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgId)
  1974. db = db.Group("xt_good_information.id")
  1975. if startime == 0 || endtime == 0 {
  1976. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  1977. return db.Where("org_id = ? AND status = 1", orgId)
  1978. })
  1979. }
  1980. if startime > 0 {
  1981. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  1982. return db.Where("org_id = ? AND status = 1 and ctime >= ?", orgId, startime)
  1983. })
  1984. }
  1985. if endtime > 0 {
  1986. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  1987. return db.Where("org_id = ? AND status = 1 and ctime>=? and ctime <= ?", orgId, startime, endtime)
  1988. })
  1989. }
  1990. db = db.Preload("QuerySalesReturnInfo", "org_id = ? AND status = 1", orgId)
  1991. if startime == 0 || endtime == 0 {
  1992. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1", orgId)
  1993. }
  1994. if startime > 0 {
  1995. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=?", orgId, startime)
  1996. }
  1997. if endtime > 0 {
  1998. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=? and ctime<=?", orgId, startime, endtime)
  1999. }
  2000. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  2001. if len(keyword) > 0 {
  2002. likeKey := "%" + keyword + "%"
  2003. db = db.Joins("join xt_goods_type on xt_goods_type.id = xt_good_information.good_type_id AND xt_goods_type.org_id = ? AND xt_goods_type.status = 1", orgId)
  2004. db = db.Where("xt_good_information.good_code LIKE ? OR xt_good_information.specification_name LIKE ? OR xt_good_information.good_name LIKE ? OR xt_goods_type.type_name LIKE ?", likeKey, likeKey, likeKey, likeKey).Group("xt_good_information.id")
  2005. }
  2006. if type_name > 0 {
  2007. db = db.Joins("join xt_goods_type on xt_goods_type.id = xt_good_information.good_type_id AND xt_goods_type.org_id = ? AND xt_goods_type.status = 1 and xt_goods_type.id = ?", orgId, type_name)
  2008. }
  2009. db = db.Count(&total)
  2010. offset := (page - 1) * limit
  2011. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2012. return
  2013. }
  2014. func FindWarehouseInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2015. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2016. return
  2017. }
  2018. func FindWarehouseOutInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2019. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2020. return
  2021. }
  2022. func UpDateWarehouse(warehouse *models.Warehousing) (err error) {
  2023. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", warehouse.WarehousingOrder).Update(map[string]interface{}{"operation_time": warehouse.OperationTime, "mtime": warehouse.Mtime, "modifier": warehouse.Modifier}).Error
  2024. return
  2025. }
  2026. func UpDateWarehouseOut(warehouseOut *models.WarehouseOut) (err error) {
  2027. err = readDb.Model(&models.WarehouseOut{}).Where("warehouse_out_order_number = ? AND status = 1", warehouseOut.WarehouseOutOrderNumber).Update(map[string]interface{}{"operation_time": warehouseOut.OperationTime, "mtime": warehouseOut.Mtime, "modifier": warehouseOut.Modifier}).Error
  2028. return
  2029. }
  2030. func UpDateWarehouseInfo(warehouseInfo *models.WarehousingInfo) (err error) {
  2031. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", warehouseInfo.WarehousingOrder).Update(map[string]interface{}{"good_type_id": warehouseInfo.GoodTypeId,
  2032. "good_id": warehouseInfo.GoodId,
  2033. "number": warehouseInfo.Number,
  2034. "product_date": warehouseInfo.ProductDate,
  2035. "expiry_date": warehouseInfo.ExpiryDate,
  2036. "warehousing_count": warehouseInfo.WarehousingCount,
  2037. "price": warehouseInfo.Price,
  2038. "total_price": warehouseInfo.TotalPrice,
  2039. "mtime": warehouseInfo.Mtime,
  2040. "remark": warehouseInfo.Remark,
  2041. }).Error
  2042. return
  2043. }
  2044. func UpDateWarehouseStatus(id int64) (err error) {
  2045. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2046. return
  2047. }
  2048. func UpdatedStockFlowById(id int64) (err error) {
  2049. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("warehousing_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2050. return
  2051. }
  2052. func UpDateDrugWarehouseStatus(id int64) (err error) {
  2053. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2054. return
  2055. }
  2056. func UpdateDrugFlowById(id int64) (err error) {
  2057. err = XTWriteDB().Model(&models.DrugFlow{}).Where("warehousing_detail_id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2058. return
  2059. }
  2060. func DeleteWarehouse(ids []string) (err error) {
  2061. ut := writeDb.Begin()
  2062. err = ut.Model(&models.Warehousing{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2063. if err != nil {
  2064. ut.Rollback()
  2065. return
  2066. }
  2067. err = ut.Model(&models.WarehousingInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2068. if err != nil {
  2069. ut.Rollback()
  2070. return
  2071. }
  2072. ut.Commit()
  2073. return
  2074. }
  2075. func DeleteStockFlow(ids []string) (err error) {
  2076. ut := writeDb.Begin()
  2077. err = ut.Model(&models.VmStockFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2078. if err != nil {
  2079. ut.Rollback()
  2080. return
  2081. }
  2082. ut.Commit()
  2083. return
  2084. }
  2085. func DeleteDrugWarehouse(ids []string) (err error) {
  2086. ut := writeDb.Begin()
  2087. err = ut.Model(&models.DrugWarehouse{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2088. if err != nil {
  2089. ut.Rollback()
  2090. return
  2091. }
  2092. err = ut.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2093. if err != nil {
  2094. ut.Rollback()
  2095. return
  2096. }
  2097. err = ut.Model(&models.DrugFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2098. if err != nil {
  2099. ut.Rollback()
  2100. return
  2101. }
  2102. ut.Commit()
  2103. return
  2104. }
  2105. func FindWareHouseById(orderNumber string) (*models.Warehousing, error) {
  2106. warehouse := &models.Warehousing{}
  2107. err := readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).First(&warehouse).Error
  2108. return warehouse, err
  2109. }
  2110. func FindWareHouseOutById(id int64) (*models.WarehouseOut, error) {
  2111. warehouseOut := &models.WarehouseOut{}
  2112. err := readDb.Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  2113. return warehouseOut, err
  2114. }
  2115. func FindDrugWareHouseOutById(id int64, org_id int64) (*models.DrugWarehouseOut, error) {
  2116. warehouseOut := &models.DrugWarehouseOut{}
  2117. err := readDb.Model(&models.DrugWarehouseOut{}).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ? ", org_id).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  2118. return warehouseOut, err
  2119. }
  2120. func FindWarehouseInfoByOrderNumber(orderNumber string) (info []*models.WarehousingInfo, err error) {
  2121. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", orderNumber).Find(&info).Error
  2122. return
  2123. }
  2124. func FindWarehouseOutInfoByOrderNumber(orderNumber string) (info []*models.WarehouseOutInfo, err error) {
  2125. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", orderNumber).Find(&info).Error
  2126. return
  2127. }
  2128. func DeleteStockInRecordByOrderNumber(orderNumber string, id int64) (err error) {
  2129. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2130. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2131. return
  2132. }
  2133. func DeleteStockInRecordByReocordId(id int64) (err error) {
  2134. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2135. return
  2136. }
  2137. func FindAllGoodByManufactureId(manufacturer_id int64, dealer_id int64, org_id int64) (goodInfo []*models.GoodInfo, err error) {
  2138. db := readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id)
  2139. if manufacturer_id > 0 {
  2140. db = db.Where("manufacturer = ?", manufacturer_id)
  2141. }
  2142. if dealer_id > 0 {
  2143. db = db.Where("dealer = ?", dealer_id)
  2144. }
  2145. err = db.Preload("GoodsType", "status = 1 AND org_id = ?", org_id).Find(&goodInfo).Error
  2146. return goodInfo, err
  2147. }
  2148. func FindAllWarehouseByKeyword(orgId int64, page int64, limit int64, keywords string) (list []*models.Warehousing, total int64, err error) {
  2149. db := readDb.Model(&models.Warehousing{})
  2150. likekey := "%" + keywords + "%"
  2151. db = db.Where("org_id = ? AND status = 1 ", orgId)
  2152. db = db.Where("warehousing_order LIKE ? ", likekey)
  2153. db = db.Count(&total)
  2154. offset := (page - 1) * limit
  2155. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2156. return
  2157. }
  2158. func GetLastWarehousingById(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2159. info := models.WarehousingInfo{}
  2160. err = XTReadDB().Model(&info).Where("warehousing_id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2161. return info, err
  2162. }
  2163. func GetLastWarehousingByIdOne(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2164. info := models.WarehousingInfo{}
  2165. err = XTReadDB().Model(&info).Where("id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2166. return info, err
  2167. }
  2168. func UpDateWarehousingInfo(info *models.WarehousingInfo) (err error) {
  2169. err = writeDb.Save(&info).Error
  2170. return err
  2171. }
  2172. func UpDateDrugWarehousingInfo(info *models.DrugWarehouseInfo) (err error) {
  2173. err = writeDb.Save(&info).Error
  2174. return err
  2175. }
  2176. func UpDateDrugWarehousingInfoTwo(id int64, info *models.DrugWarehouseInfo) (err error) {
  2177. err = writeDb.Model(&info).Where("id= ? and status = 1", id).Updates(map[string]interface{}{"number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "batch_number": info.BatchNumber, "max_unit": info.MaxUnit, "min_unit": info.MinUnit, "retail_price": info.RetailPrice, "stock_max_number": info.StockMaxNumber, "stock_min_number": info.StockMinNumber, "storehouse_id": info.StorehouseId}).Error
  2178. return err
  2179. }
  2180. func UpDateDrugWarehousingInfoSix(id int64, info *models.DrugWarehouseInfo) (err error) {
  2181. err = writeDb.Model(&info).Where("id= ? and status = 1", id).Updates(map[string]interface{}{"number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "batch_number": info.BatchNumber, "max_unit": info.MaxUnit, "min_unit": info.MinUnit, "retail_price": info.RetailPrice, "storehouse_id": info.StorehouseId}).Error
  2182. return err
  2183. }
  2184. func GetDrugWarehouseOrderOne(id int64, drug_id int64) (models.DrugWarehouseInfo, error) {
  2185. info := models.DrugWarehouseInfo{}
  2186. err = XTReadDB().Model(&info).Where("warehousing_id = ? and drug_id = ?", id, drug_id).Find(&info).Error
  2187. return info, err
  2188. }
  2189. func EditWarehousing(warehouse models.Warehousing) {
  2190. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", warehouse.WarehousingOrder).Update(map[string]interface{}{"mtime": time.Now().Unix(), "warehousing_time": warehouse.WarehousingTime, "modifier": warehouse.Modifier, "dealer": warehouse.Dealer, "manufacturer": warehouse.Manufacturer, "storehouse_id": warehouse.StorehouseId}).Error
  2191. }
  2192. func EditDrugWarehousing(warehouse models.DrugWarehouse) {
  2193. err = readDb.Model(&models.DrugWarehouse{}).Where("warehousing_order = ? AND status = 1", warehouse.WarehousingOrder).Update(map[string]interface{}{"mtime": time.Now().Unix(), "warehousing_time": warehouse.WarehousingTime, "modifier": warehouse.Modifier, "dealer": warehouse.Dealer, "manufacturer": warehouse.Manufacturer}).Error
  2194. }
  2195. func FindAllWarehouse(org_id int64, types int64) (list []*models.Warehousing, err error) {
  2196. err = readDb.Model(&models.Warehousing{}).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ? ", org_id).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2197. return list, err
  2198. }
  2199. func FindAllWarehouseInfo(org_id int64, types int64) (list []*models.WarehousingInfoConfig, err error) {
  2200. err = readDb.Model(&models.WarehousingInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2201. return db.Where("status = 1 AND org_id = ?", org_id).Preload("GoodsType", "status = 1 AND org_id = ?", org_id).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ?", org_id)
  2202. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2203. return list, err
  2204. }
  2205. func DeleteSalesReturn(ids []string) (err error) {
  2206. ut := writeDb.Begin()
  2207. err = ut.Model(&models.SalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2208. if err != nil {
  2209. ut.Rollback()
  2210. return
  2211. }
  2212. err = ut.Model(&models.SalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2213. if err != nil {
  2214. ut.Rollback()
  2215. return
  2216. }
  2217. ut.Commit()
  2218. return
  2219. }
  2220. func DeleteDrugSalesReturn(ids []string) (err error) {
  2221. ut := writeDb.Begin()
  2222. err = ut.Model(&models.DrugSalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2223. if err != nil {
  2224. ut.Rollback()
  2225. return
  2226. }
  2227. err = ut.Model(&models.DrugSalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2228. if err != nil {
  2229. ut.Rollback()
  2230. return
  2231. }
  2232. ut.Commit()
  2233. return
  2234. }
  2235. func FindAllSalesReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  2236. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  2237. return
  2238. }
  2239. type DrugSalesReturnInfo struct {
  2240. ID int64 `gorm:"column:id" json:"id" form:"id"`
  2241. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  2242. SalesReturnId int64 `gorm:"column:sales_return_id" json:"sales_return_id" form:"sales_return_id"`
  2243. Count int64 `gorm:"column:count" json:"count" form:"count"`
  2244. Price float64 `gorm:"column:price" json:"price" form:"price"`
  2245. Total float64 `gorm:"column:total" json:"total" form:"total"`
  2246. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  2247. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  2248. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  2249. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  2250. Status int64 `gorm:"column:status" json:"status" form:"status"`
  2251. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  2252. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  2253. Type int64 `gorm:"column:type" json:"type" form:"type"`
  2254. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  2255. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  2256. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  2257. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  2258. Number string `gorm:"column:number" json:"number" form:"number"`
  2259. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  2260. }
  2261. func (DrugSalesReturnInfo) TableName() string {
  2262. return "xt_drug_sales_return_info"
  2263. }
  2264. func FindAllDrugSalesReturnInfoById(id int64, org_id int64) (list []*DrugSalesReturnInfo, err error) {
  2265. err = readDb.Model(&DrugSalesReturnInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("sales_return_id = ? AND status = 1 AND org_id = ?", id, org_id).Find(&list).Error
  2266. return
  2267. }
  2268. func FindAllSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2269. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2270. return
  2271. }
  2272. func FindAllDrugSalesReturnById(id int64, org_id int64) (salesReturn models.DrugSalesReturn, err error) {
  2273. err = readDb.Model(&models.DrugSalesReturn{}).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ?", org_id).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2274. return
  2275. }
  2276. func UpDateSaleReturnStatus(id int64) (err error) {
  2277. err = readDb.Model(&models.SalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2278. return
  2279. }
  2280. func UpDateDrugSaleReturnStatus(id int64) (err error) {
  2281. err = readDb.Model(&models.DrugSalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2282. return
  2283. }
  2284. func FindSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2285. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2286. return salesReturn, err
  2287. }
  2288. func FindDrugSalesReturnById(id int64) (salesReturn models.DrugSalesReturn, err error) {
  2289. err = readDb.Model(&models.DrugSalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2290. return salesReturn, err
  2291. }
  2292. func EditSaleReturn(sales models.SalesReturn) {
  2293. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", sales.ID).Update(map[string]interface{}{"mtime": time.Now().Unix(), "return_time": sales.ReturnTime, "dealer": sales.Dealer, "manufacturer": sales.Manufacturer}).Error
  2294. }
  2295. func EditDrugSaleReturn(sales models.DrugSalesReturn) {
  2296. err = readDb.Model(&models.DrugSalesReturn{}).Where("id = ? AND status = 1", sales.ID).Update(map[string]interface{}{"mtime": time.Now().Unix(), "return_time": sales.ReturnTime, "dealer": sales.Dealer, "manufacturer": sales.Manufacturer}).Error
  2297. }
  2298. func UpDateSalesReturnInfo(info *models.SalesReturnInfo) (err error) {
  2299. err = writeDb.Save(&info).Error
  2300. return err
  2301. }
  2302. func UpDateDrugSalesReturnInfo(info *models.DrugSalesReturnInfo) (err error) {
  2303. err = writeDb.Save(&info).Error
  2304. return err
  2305. }
  2306. func DeleteWarehouseOut(ids []string) (err error) {
  2307. ut := writeDb.Begin()
  2308. err = ut.Model(&models.WarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2309. if err != nil {
  2310. ut.Rollback()
  2311. return
  2312. }
  2313. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2314. if err != nil {
  2315. ut.Rollback()
  2316. return
  2317. }
  2318. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2319. if err != nil {
  2320. ut.Rollback()
  2321. return
  2322. }
  2323. ut.Commit()
  2324. return
  2325. }
  2326. func DeleteDrugWarehouseOut(ids []string) (err error) {
  2327. ut := writeDb.Begin()
  2328. err = ut.Model(&models.DrugWarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2329. if err != nil {
  2330. ut.Rollback()
  2331. return
  2332. }
  2333. err = ut.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2334. if err != nil {
  2335. ut.Rollback()
  2336. return
  2337. }
  2338. err = ut.Model(&models.DrugFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2339. if err != nil {
  2340. ut.Rollback()
  2341. return
  2342. }
  2343. ut.Commit()
  2344. return
  2345. }
  2346. func UpDateWarehouseOutStatus(id int64) (err error) {
  2347. err = readDb.Model(&models.WarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2348. return
  2349. }
  2350. func UpDateDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo) (err error) {
  2351. ut := writeDb.Begin()
  2352. err = ut.Save(&info).Error
  2353. if err != nil {
  2354. ut.Rollback()
  2355. return err
  2356. }
  2357. ut.Commit()
  2358. return err
  2359. }
  2360. func UpDateNewDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo, tx *gorm.DB) (err error) {
  2361. err = tx.Save(&info).Error
  2362. if err != nil {
  2363. tx.Rollback()
  2364. return err
  2365. }
  2366. return err
  2367. }
  2368. func UpDateWarehouseInfoByStock(info *models.WarehousingInfo) (err error) {
  2369. ut := writeDb.Begin()
  2370. err = ut.Save(&info).Error
  2371. if err != nil {
  2372. ut.Rollback()
  2373. return err
  2374. }
  2375. ut.Commit()
  2376. return err
  2377. }
  2378. func UpDateNewWarehouseInfoByStock(info *models.WarehousingInfo, tx *gorm.DB) (err error) {
  2379. err = tx.Save(&info).Error
  2380. if err != nil {
  2381. tx.Rollback()
  2382. return err
  2383. }
  2384. return err
  2385. }
  2386. func UpDateDrugWarehouseOutStatus(id int64) (err error) {
  2387. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2388. return
  2389. }
  2390. func EditWarehouseOut(warehouseOut models.WarehouseOut) {
  2391. err = readDb.Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", warehouseOut.ID).Update(map[string]interface{}{"mtime": time.Now().Unix(), "warehouse_out_time": warehouseOut.WarehouseOutTime, "dealer": warehouseOut.Dealer, "manufacturer": warehouseOut.Manufacturer, "storehouse_id": warehouseOut.StorehouseId}).Error
  2392. }
  2393. func EditDrugWarehouseOut(warehouseOut models.DrugWarehouseOut) {
  2394. err = readDb.Model(&models.DrugWarehouseOut{}).Where("id = ? AND status = 1", warehouseOut.ID).Update(map[string]interface{}{"mtime": time.Now().Unix(), "warehouse_out_time": warehouseOut.WarehouseOutTime, "dealer": warehouseOut.Dealer, "manufacturer": warehouseOut.Manufacturer, "storehouse_id": warehouseOut.StorehouseId}).Error
  2395. }
  2396. func UpDateWarehouseOutInfo(info *models.WarehouseOutInfo) (err error) {
  2397. err = writeDb.Save(&info).Error
  2398. return err
  2399. }
  2400. func FindAllCancelList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, storehouse_id int64, check_type int64) (list []*models.CancelStock, total int64, err error) {
  2401. db := readDb.Model(&models.CancelStock{})
  2402. db = db.Where("xt_cancel_stock.org_id = ? AND xt_cancel_stock.status = 1 AND xt_cancel_stock.type = ?", orgId, types)
  2403. if len(keywords) > 0 {
  2404. likeKey := "%" + keywords + "%"
  2405. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2406. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_cancel_stock.manufacturer")
  2407. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_cancel_stock.order_number LIKE ?", likeKey, likeKey).Group("xt_cancel_stock.id")
  2408. }
  2409. if startTime > 0 {
  2410. db = db.Where("xt_cancel_stock.opera_time >=?", startTime)
  2411. }
  2412. if endTime > 0 {
  2413. db = db.Where("xt_cancel_stock.opera_time<= ?", endTime)
  2414. }
  2415. if storehouse_id > 0 {
  2416. db = db.Where("xt_cancel_stock.storehouse_id = ?", storehouse_id)
  2417. }
  2418. if check_type > 0 {
  2419. db = db.Where("xt_cancel_stock.is_check = ?", check_type)
  2420. }
  2421. db = db.Count(&total)
  2422. offset := (page - 1) * limit
  2423. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock.ctime desc").Find(&list).Error
  2424. return
  2425. }
  2426. func FindAllDrugCancelList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, storehoue_id int64, check_type int64) (list []*models.DrugCancelStock, total int64, err error) {
  2427. db := readDb.Model(&models.DrugCancelStock{})
  2428. db = db.Where("xt_drug_cancel_stock.org_id = ? AND xt_drug_cancel_stock.status = 1", orgId)
  2429. if len(keywords) > 0 {
  2430. likeKey := "%" + keywords + "%"
  2431. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_cancel_stock.creater")
  2432. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_cancel_stock.manufacturer")
  2433. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_drug_cancel_stock.order_number LIKE ?", likeKey, likeKey).Group("xt_drug_cancel_stock.id")
  2434. }
  2435. if startTime > 0 {
  2436. db = db.Where("xt_drug_cancel_stock.opera_time >=?", startTime)
  2437. }
  2438. if endTime > 0 {
  2439. db = db.Where("xt_drug_cancel_stock.opera_time<= ?", endTime)
  2440. }
  2441. if storehoue_id > 0 {
  2442. db = db.Where("xt_drug_cancel_stock.storehouse_id = ?", storehoue_id)
  2443. }
  2444. if check_type > 0 {
  2445. db = db.Where("xt_drug_cancel_stock.is_check = ?", check_type)
  2446. }
  2447. db = db.Count(&total)
  2448. offset := (page - 1) * limit
  2449. err = db.Offset(offset).Limit(limit).Order("xt_drug_cancel_stock.ctime desc").Find(&list).Error
  2450. return
  2451. }
  2452. func GetCancelWarehosueInfo(ids []string) (info []*models.CancelStockInfo, err error) {
  2453. err = XTReadDB().Where("id IN(?) and status = 1", ids).Find(&info).Error
  2454. return info, err
  2455. }
  2456. func DeleteCancelStock(ids []string) (err error) {
  2457. ut := writeDb.Begin()
  2458. err = ut.Model(&models.CancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2459. if err != nil {
  2460. ut.Rollback()
  2461. return
  2462. }
  2463. err = ut.Model(&models.CancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2464. if err != nil {
  2465. ut.Rollback()
  2466. return
  2467. }
  2468. err = ut.Model(&models.VmStockFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2469. if err != nil {
  2470. ut.Rollback()
  2471. return
  2472. }
  2473. ut.Commit()
  2474. return
  2475. }
  2476. func GetDrugWareInfoByCancelId(ids []string) (info []*models.DrugCancelStockInfo, err error) {
  2477. err = XTReadDB().Where("cancel_stock_id IN(?) and status = 1", ids).Find(&info).Error
  2478. return info, err
  2479. }
  2480. func DeleteDrugCancelStock(ids []string) (err error) {
  2481. ut := writeDb.Begin()
  2482. err = ut.Model(&models.DrugCancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2483. if err != nil {
  2484. ut.Rollback()
  2485. return
  2486. }
  2487. err = ut.Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2488. if err != nil {
  2489. ut.Rollback()
  2490. return
  2491. }
  2492. err = ut.Model(&models.DrugFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2493. if err != nil {
  2494. ut.Rollback()
  2495. return
  2496. }
  2497. ut.Commit()
  2498. return
  2499. }
  2500. func UpDateCancleStockStatus(id int64) (err error) {
  2501. err = readDb.Model(&models.CancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2502. return
  2503. }
  2504. func GetCancelStockById(id int64) (models.CancelStockInfo, error) {
  2505. info := models.CancelStockInfo{}
  2506. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  2507. return info, err
  2508. }
  2509. func ModefyWarehouseInfoNight(info models.WarehousingInfo, id int64) error {
  2510. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", info.StockCount)).Error
  2511. return err
  2512. }
  2513. func UpdatedStockFlowByCancelId(id int64, good_id int64) (err error) {
  2514. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("cancel_out_detail_id = ? and status = 1 and good_id = ?", id, good_id).Updates(map[string]interface{}{"status": 0}).Error
  2515. return
  2516. }
  2517. func UpDateDrugCancleStockStatus(id int64) (err error) {
  2518. err = readDb.Model(&models.DrugCancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2519. return
  2520. }
  2521. func UpDateDrugCancelStockById(id int64) (err error) {
  2522. err = XTWriteDB().Model(&models.DrugFlow{}).Where("cancel_out_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2523. return
  2524. }
  2525. func EditCancelStock(sales models.CancelStock) {
  2526. err = readDb.Model(&models.CancelStock{}).Where("id = ? AND status = 1", sales.ID).Update(map[string]interface{}{"mtime": time.Now().Unix(), "return_time": sales.ReturnTime, "dealer": sales.Dealer, "manufacturer": sales.Manufacturer, "storehouse_id": sales.StorehouseId}).Error
  2527. }
  2528. func EditDrugCancelStock(sales models.DrugCancelStock) {
  2529. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", sales.ID).Update(map[string]interface{}{"mtime": time.Now().Unix(), "return_time": sales.ReturnTime, "dealer": sales.Dealer, "manufacturer": sales.Manufacturer, "storehouse_id": sales.StorehouseId}).Error
  2530. }
  2531. func FindCancelStockById(id int64, org_id int64) (cancelStock models.CancelStock, err error) {
  2532. err = readDb.Model(&models.CancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2533. return cancelStock, err
  2534. }
  2535. func FindCancelDrugById(id int64, org_id int64) (cancelStock models.DrugCancelStock, err error) {
  2536. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2537. return cancelStock, err
  2538. }
  2539. func FindDrugCancelStockById(id int64) (cancelStock models.DrugCancelStock, err error) {
  2540. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2541. return cancelStock, err
  2542. }
  2543. func UpDateCancelStockInfo(info *models.CancelStockInfo) (err error) {
  2544. err = writeDb.Save(&info).Error
  2545. return err
  2546. }
  2547. func UpDateDrugCancelStockInfo(info *models.DrugCancelStockInfo) (err error) {
  2548. err = writeDb.Save(&info).Error
  2549. return err
  2550. }
  2551. func FindWarehouseOutList(org_id int64, types int64) (list []*models.WarehouseOut, err error) {
  2552. err = readDb.Model(&models.WarehouseOut{}).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ? ", org_id).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2553. return list, err
  2554. }
  2555. func FindAllWarehouseOutInfo(org_id int64, types int64) (list []*models.WarehousingOutInfoConfig, err error) {
  2556. err = readDb.Model(&models.WarehousingOutInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2557. return db.Where("status = 1 AND org_id = ?", org_id).Preload("GoodsType", "status = 1 AND org_id = ? ", org_id).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ?", org_id)
  2558. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2559. return list, err
  2560. }
  2561. type StockDetailModel struct {
  2562. GoodId int64 `gorm:"good_id" json:"good_id"`
  2563. GoodTypeId int64 `gorm:"good_type_id" json:"good_type_id"`
  2564. WarehousingOrder string `gorm:"warehousing_order" json:"warehousing_order"`
  2565. Price float64 `gorm:"price" json:"price"`
  2566. WarehousingCount int64 `gorm:"warehousing_count" json:"warehousing_count"`
  2567. Type int `gorm:"type" json:"type"`
  2568. Creater int64 `gorm:"creater" json:"creater"`
  2569. WarehousingTime int64 `gorm:"warehousing_time" json:"warehousing_time"`
  2570. Types int64 `gorm:"types" json:"types"`
  2571. }
  2572. func FindStockInDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (list []*models.WarehousingInfo, total int64, err error) {
  2573. db := readDb.Model(&models.WarehousingInfo{})
  2574. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1 AND xt_warehouse_info.is_check = 1", orgId)
  2575. if len(keywords) > 0 {
  2576. likeKey := "%" + keywords + "%"
  2577. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2578. db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND xt_warehouse.warehousing_time >=? AND xt_warehouse.warehousing_time<= ? AND xt_warehouse.status = 1 AND xt_warehouse.org_id = ?", startTime, endTime, orgId)
  2579. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2580. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2581. db = db.Where(" sgj_user_admin_role.user_name LIKE ? OR xt_warehouse_info.warehousing_order LIKE ? OR xt_good_information.specification_name LIKE ? OR xt_good_information.good_name LIKE ?", likeKey, likeKey, likeKey, likeKey).Group("xt_warehouse_info.id")
  2582. } else {
  2583. db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND xt_warehouse.warehousing_time >=? AND xt_warehouse.warehousing_time<= ? AND xt_warehouse.status = 1 AND xt_warehouse.org_id = ?", startTime, endTime, orgId)
  2584. }
  2585. if manufacturer > 0 {
  2586. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2587. }
  2588. if storehouse_id > 0 {
  2589. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  2590. }
  2591. if order_type > 0 {
  2592. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2593. }
  2594. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  2595. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2596. db = db.Count(&total)
  2597. offset := (page - 1) * limit
  2598. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_info.id desc").Find(&list).Error
  2599. return
  2600. }
  2601. func GetStockInDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (err error, totalPrice float64) {
  2602. db := readDb.Model(&models.WarehousingInfo{})
  2603. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId)
  2604. if len(keywords) > 0 {
  2605. likeKey := "%" + keywords + "%"
  2606. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2607. db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND xt_warehouse.warehousing_time >=? AND xt_warehouse.warehousing_time<= ? AND xt_warehouse.status = 1 AND xt_warehouse.org_id = ?", startTime, endTime, orgId)
  2608. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2609. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2610. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_warehouse_info.warehousing_order LIKE ? OR xt_good_information.specification_name LIKE ?", likeKey, likeKey, likeKey).Group("xt_warehouse_info.id")
  2611. } else {
  2612. db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND xt_warehouse.warehousing_time >=? AND xt_warehouse.warehousing_time<= ? AND xt_warehouse.status = 1 AND xt_warehouse.org_id = ?", startTime, endTime, orgId)
  2613. }
  2614. if manufacturer > 0 {
  2615. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2616. }
  2617. if order_type > 0 {
  2618. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2619. }
  2620. if storehouse_id > 0 {
  2621. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  2622. }
  2623. type PriceAmount struct {
  2624. Total float64
  2625. }
  2626. var result []PriceAmount
  2627. err = db.Select("total_price as total").Scan(&result).Error
  2628. var total float64
  2629. for _, value := range result {
  2630. total = total + value.Total
  2631. }
  2632. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  2633. return err, total
  2634. }
  2635. func FloatRound(f float64, n int) float64 {
  2636. format := "%." + strconv.Itoa(n) + "f"
  2637. res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
  2638. return res
  2639. }
  2640. func FindStockOutDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  2641. if orgId == 9671 {
  2642. db := readDb.Model(&models.WarehouseOutInfo{})
  2643. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.is_check = 1 AND xt_warehouse_out_info.good_id <> 0 AND xt_warehouse_out_info.count <> 0 AND xt_warehouse_out_info.sys_record_time>=? AND xt_warehouse_out_info.sys_record_time<=?", orgId, startTime, endTime)
  2644. if len(keywords) > 0 {
  2645. likeKey := "%" + keywords + "%"
  2646. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id AND xt_warehouse_out.warehouse_out_time >=? AND xt_warehouse_out.warehouse_out_time<= ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.org_id = ?", startTime, endTime, orgId)
  2647. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2648. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2649. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_warehouse_out_info.warehouse_out_order_number LIKE ? OR xt_good_information.specification_name LIKE ? OR xt_good_information.good_name LIKE ?", likeKey, likeKey, likeKey, likeKey).Group("xt_warehouse_out_info.id")
  2650. } else {
  2651. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id AND xt_warehouse_out.warehouse_out_time >=? AND xt_warehouse_out.warehouse_out_time<= ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.org_id = ?", startTime, endTime, orgId)
  2652. }
  2653. if manufacturer > 0 {
  2654. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2655. }
  2656. if order_type > 0 {
  2657. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2658. }
  2659. if storehouse_id > 0 {
  2660. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  2661. }
  2662. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2663. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ?", orgId)
  2664. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2665. db = db.Count(&total)
  2666. offset := (page - 1) * limit
  2667. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  2668. return
  2669. } else {
  2670. db := readDb.Model(&models.WarehouseOutInfo{})
  2671. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.is_check = 1 AND xt_warehouse_out_info.good_id <> 0 AND xt_warehouse_out_info.count <> 0 ", orgId)
  2672. if len(keywords) > 0 {
  2673. likeKey := "%" + keywords + "%"
  2674. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id AND xt_warehouse_out.warehouse_out_time >=? AND xt_warehouse_out.warehouse_out_time<= ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.org_id = ?", startTime, endTime, orgId)
  2675. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2676. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2677. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_warehouse_out_info.warehouse_out_order_number LIKE ? OR xt_good_information.specification_name LIKE ? OR xt_good_information.good_name LIKE ?", likeKey, likeKey, likeKey, likeKey).Group("xt_warehouse_out_info.id")
  2678. } else {
  2679. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id AND xt_warehouse_out.warehouse_out_time >=? AND xt_warehouse_out.warehouse_out_time<= ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.org_id = ?", startTime, endTime, orgId)
  2680. }
  2681. if manufacturer > 0 {
  2682. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2683. }
  2684. if order_type > 0 {
  2685. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2686. }
  2687. if storehouse_id > 0 {
  2688. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  2689. }
  2690. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2691. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ?", orgId)
  2692. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2693. db = db.Count(&total)
  2694. offset := (page - 1) * limit
  2695. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  2696. return
  2697. }
  2698. }
  2699. func GetStockOutDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (err error, totalPrice float64) {
  2700. db := readDb.Model(&models.WarehouseOutInfo{})
  2701. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.good_id <> 0 AND xt_warehouse_out_info.count <> 0", orgId)
  2702. if len(keywords) > 0 {
  2703. likeKey := "%" + keywords + "%"
  2704. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id AND xt_warehouse_out.warehouse_out_time >=? AND xt_warehouse_out.warehouse_out_time<= ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.org_id = ?", startTime, endTime, orgId)
  2705. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2706. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2707. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_warehouse_out_info.warehouse_out_order_number LIKE ?", likeKey, likeKey).Group("xt_warehouse_out_info.id")
  2708. } else {
  2709. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id AND xt_warehouse_out.warehouse_out_time >=? AND xt_warehouse_out.warehouse_out_time<= ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.org_id = ?", startTime, endTime, orgId)
  2710. }
  2711. if manufacturer > 0 {
  2712. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2713. }
  2714. if order_type > 0 {
  2715. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2716. }
  2717. if storehouse_id > 0 {
  2718. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  2719. }
  2720. type PriceAmount struct {
  2721. Total float64
  2722. }
  2723. var result []PriceAmount
  2724. err = db.Select("(count * price) as total").Scan(&result).Error
  2725. var total float64
  2726. for _, value := range result {
  2727. total = total + value.Total
  2728. }
  2729. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  2730. fmt.Println(total)
  2731. return err, total
  2732. }
  2733. func FindSalesReturnDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (list []*models.SalesReturnInfo, total int64, err error) {
  2734. db := readDb.Model(&models.SalesReturnInfo{})
  2735. db = db.Where("xt_sales_return_info.org_id = ? AND xt_sales_return_info.status = 1", orgId)
  2736. if len(keywords) > 0 {
  2737. likeKey := "%" + keywords + "%"
  2738. db = db.Joins("join xt_sales_return on xt_sales_return.id = xt_sales_return_info.sales_return_id AND xt_sales_return.return_time >=? AND xt_sales_return.return_time<= ? AND xt_sales_return.status = 1 AND xt_sales_return.org_id = ?", startTime, endTime, orgId)
  2739. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  2740. db = db.Joins("join xt_good_information on xt_good_information.id = xt_sales_return_info.good_id")
  2741. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_sales_return_info.order_number LIKE ? OR xt_good_information.specification_name LIKE ? OR xt_good_information.good_name LIKE ?", likeKey, likeKey, likeKey, likeKey).Group("xt_sales_return_info.id")
  2742. } else {
  2743. db = db.Joins("join xt_sales_return on xt_sales_return.id = xt_sales_return_info.sales_return_id AND xt_sales_return.return_time >=? AND xt_sales_return.return_time<= ? AND xt_sales_return.status = 1 AND xt_sales_return.org_id = ?", startTime, endTime, orgId)
  2744. }
  2745. if manufacturer > 0 {
  2746. db = db.Joins("join xt_sales_return as sr on sr.id = xt_sales_return_info.sales_return_id AND sr.manufacturer =?", manufacturer)
  2747. }
  2748. if order_type > 0 {
  2749. db = db.Where("xt_sales_return_info.type = ?", order_type)
  2750. }
  2751. db = db.Preload("SalesReturn", "status = 1 AND org_id = ?", orgId)
  2752. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2753. offset := (page - 1) * limit
  2754. db = db.Count(&total)
  2755. err = db.Offset(offset).Limit(limit).Order("xt_sales_return_info.ctime desc").Find(&list).Error
  2756. return
  2757. }
  2758. func FindeCancelGroup(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (list []*models.CancelStockInfo, err error) {
  2759. db := readDb.Model(&models.CancelStockInfo{})
  2760. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  2761. if len(keywords) > 0 {
  2762. likeKey := "%" + keywords + "%"
  2763. db = db.Joins("join xt_cancel_stock on xt_cancel_stock.id = xt_cancel_stock_info.cancel_stock_id AND xt_cancel_stock.return_time >=? AND xt_cancel_stock.return_time<= ? AND xt_cancel_stock.status = 1 AND xt_cancel_stock.org_id = ?", startTime, endTime, orgId)
  2764. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2765. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  2766. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_cancel_stock_info.order_number LIKE ? OR xt_good_information.specification_name LIKE ?", likeKey, likeKey, likeKey).Group("xt_cancel_stock_info.id")
  2767. } else {
  2768. db = db.Joins("join xt_cancel_stock on xt_cancel_stock.id = xt_cancel_stock_info.cancel_stock_id AND xt_cancel_stock.return_time >=? AND xt_cancel_stock.return_time<= ? ", startTime, endTime)
  2769. }
  2770. if manufacturer > 0 {
  2771. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  2772. }
  2773. if order_type > 0 {
  2774. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  2775. }
  2776. if storehouse_id > 0 {
  2777. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", order_type)
  2778. }
  2779. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  2780. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2781. err = db.Group("xt_cancel_stock_info.good_id").Order("xt_cancel_stock_info.ctime desc").Find(&list).Error
  2782. return
  2783. }
  2784. func FindCancelDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (list []*models.CancelStockInfo, total int64, err error) {
  2785. db := readDb.Model(&models.CancelStockInfo{})
  2786. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  2787. if len(keywords) > 0 {
  2788. likeKey := "%" + keywords + "%"
  2789. db = db.Joins("join xt_cancel_stock on xt_cancel_stock.id = xt_cancel_stock_info.cancel_stock_id AND xt_cancel_stock.return_time >=? AND xt_cancel_stock.return_time<= ? AND xt_cancel_stock.status = 1 AND xt_cancel_stock.org_id = ?", startTime, endTime, orgId)
  2790. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2791. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  2792. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_cancel_stock_info.order_number LIKE ? OR xt_good_information.specification_name LIKE ? OR xt_good_information.good_name LIKE ?", likeKey, likeKey, likeKey, likeKey).Group("xt_cancel_stock_info.id")
  2793. } else {
  2794. db = db.Joins("join xt_cancel_stock on xt_cancel_stock.id = xt_cancel_stock_info.cancel_stock_id AND xt_cancel_stock.return_time >=? AND xt_cancel_stock.return_time<= ? ", startTime, endTime)
  2795. }
  2796. if manufacturer > 0 {
  2797. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  2798. }
  2799. if order_type > 0 {
  2800. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  2801. }
  2802. if storehouse_id > 0 {
  2803. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", storehouse_id)
  2804. }
  2805. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  2806. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2807. db = db.Count(&total)
  2808. offset := (page - 1) * limit
  2809. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock_info.id desc").Find(&list).Error
  2810. return
  2811. }
  2812. func FindStockInByDealerId(id int64) (total int64, err error) {
  2813. err = readDb.Model(&models.Warehousing{}).Where("dealer = ? AND status = 1", id).Count(&total).Error
  2814. return
  2815. }
  2816. func FindStockInByManufacturerId(id int64) (total int64, err error) {
  2817. err = readDb.Model(&models.Warehousing{}).Where("manufacturer = ? AND status = 1", id).Count(&total).Error
  2818. return
  2819. }
  2820. func FindGoodTypeByName(name string, org_id int64) (total int64) {
  2821. readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 AND type_name = ? ", org_id, name).Count(&total)
  2822. return
  2823. }
  2824. func FindGoodInfoByName(name string, org_id int64, good_type_id int64) (total int64) {
  2825. readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1 AND specification_name = ? AND good_type_id = ? ", org_id, name, good_type_id).Count(&total)
  2826. return
  2827. }
  2828. func FindGoodInfoByNameTwo(name string, org_id int64, good_type_id int64, good_name string) (total int64) {
  2829. readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1 AND specification_name = ? AND good_type_id = ? and good_name = ?", org_id, name, good_type_id, good_name).Count(&total)
  2830. return
  2831. }
  2832. func FindGoodInfoByNameOne(name string, org_id int64, good_type_id int64, id int64, goodname string) (total int64) {
  2833. readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1 AND specification_name = ? AND good_type_id = ? and id <> ? and good_name = ? ", org_id, name, good_type_id, id, goodname).Count(&total)
  2834. return
  2835. }
  2836. func FindAllGoodTypeByType(types int64) (goodType []*models.GoodsType, err error) {
  2837. err = readDb.Model(&models.GoodsType{}).Where("type = ? AND status = 1", types).Find(&goodType).Error
  2838. return goodType, err
  2839. }
  2840. func FindWarehouseInfoByGoodType(good_type_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  2841. err = readDb.Model(&models.WarehousingInfo{}).Preload("GoodInfo", " org_id = ? AND good_type_id = ? AND status = 1 AND specification_name <> '' ", org_id, good_type_id).Where(" org_id = ? AND good_type_id = ? AND status = 1 ", org_id, good_type_id).Find(&info).Error
  2842. return info, err
  2843. }
  2844. func FindStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.WarehouseOut, err error) {
  2845. err = readDb.Model(&models.WarehouseOut{}).Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ?", is_sys, org_id, record_time).First(&out).Error
  2846. return
  2847. }
  2848. func FindStockOutByIsSysSix(org_id int64, is_sys int, record_time int64, second_warehouse_id int64) (out models.WarehouseOut, err error) {
  2849. err = readDb.Model(&models.WarehouseOut{}).Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ? AND second_warehouse_id=?", is_sys, org_id, record_time, second_warehouse_id).First(&out).Error
  2850. return
  2851. }
  2852. func FindDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  2853. err = readDb.Model(&models.DrugWarehouseOut{}).Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ?", is_sys, org_id, record_time).First(&out).Error
  2854. return
  2855. }
  2856. func FindNewDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64, tx *gorm.DB) (out models.DrugWarehouseOut, err error) {
  2857. err = tx.Model(&models.DrugWarehouseOut{}).Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ?", is_sys, org_id, record_time).First(&out).Error
  2858. if err != gorm.ErrRecordNotFound {
  2859. if err != nil {
  2860. tx.Rollback()
  2861. return
  2862. }
  2863. }
  2864. return
  2865. }
  2866. func FindDrugSettleStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  2867. err = readDb.Model(&models.DrugWarehouseOut{}).Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ?", is_sys, org_id, record_time).First(&out).Error
  2868. return
  2869. }
  2870. func FindLastStockInInfoRecord(good_id int64, org_id int64) (in models.WarehousingInfo, err error) {
  2871. err = readDb.Model(&models.WarehousingInfo{}).Where("status = 1 AND org_id = ? AND good_id = ?", org_id, good_id).First(&in).Error
  2872. return
  2873. }
  2874. func FindLastDrugStockInInfoRecord(drug_id int64, org_id int64) (in models.DrugWarehouseOutInfo, err error) {
  2875. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("status = 1 AND org_id = ? AND drug_id = ?", org_id, drug_id).Last(&in).Error
  2876. return
  2877. }
  2878. func GetStockInRecoredByGoodId(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  2879. info := models.WarehouseOutInfo{}
  2880. err := XTReadDB().Model(&info).Where("good_id = ? and good_type_id = ? and sys_record_time = ? and patient_id = ? and org_id = ? and status = 1", goodid, goodtypeid, recordtime, patientid, orgid).Find(&info).Error
  2881. if err == gorm.ErrRecordNotFound {
  2882. return nil, err
  2883. }
  2884. if err != nil {
  2885. return nil, err
  2886. }
  2887. return &info, nil
  2888. }
  2889. func GetStockInRecoredByGoodIdOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64) (*models.WarehouseOutInfo, error) {
  2890. info := models.WarehouseOutInfo{}
  2891. err := XTReadDB().Model(&info).Where("good_id = ? and good_type_id = ? and sys_record_time = ? and org_id = ? and status = 1", goodid, goodtypeid, recordtime, orgid).Find(&info).Error
  2892. if err == gorm.ErrRecordNotFound {
  2893. return nil, err
  2894. }
  2895. if err != nil {
  2896. return nil, err
  2897. }
  2898. return &info, nil
  2899. }
  2900. func UpdateAutoMaticReduceDetail(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  2901. err := XTWriteDB().Model(&prepare).Where("good_id = ? and good_type_id = ? and sys_record_time = ? and patient_id = ? and org_id = ? and status =1", goodid, goodtypeid, recordtime, patientid, orgid).Updates(map[string]interface{}{"count": prepare.Count, "over_count": prepare.OverCount}).Error
  2902. return err
  2903. }
  2904. func UpdateAutoMaticReduceDetailOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  2905. err := XTWriteDB().Model(&prepare).Where("good_id = ? and good_type_id = ? and sys_record_time = ? and org_id = ? and status =1", goodid, goodtypeid, recordtime, orgid).Updates(map[string]interface{}{"count": prepare.Count}).Error
  2906. return err
  2907. }
  2908. func AddSigleWarehouseOutInfo(info *models.WarehouseOutInfo) error {
  2909. err := writeDb.Create(&info).Error
  2910. return err
  2911. }
  2912. func AddNewSigleWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) error {
  2913. err := tx.Create(&info).Error
  2914. if err != nil {
  2915. tx.Rollback()
  2916. return err
  2917. }
  2918. return err
  2919. }
  2920. func UpdateStockOut(warehouseinfoid int64, info models.WarehousingInfo) error {
  2921. err := XTWriteDB().Model(&info).Where("id=? and status = 1", warehouseinfoid).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  2922. return err
  2923. }
  2924. func AddSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo) error {
  2925. err := writeDb.Create(&info).Error
  2926. return err
  2927. }
  2928. func AddNewSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, tx *gorm.DB) error {
  2929. err := tx.Create(&info).Error
  2930. if err != nil {
  2931. tx.Rollback()
  2932. return err
  2933. }
  2934. return err
  2935. }
  2936. func AddSigleAutoReduceRecordInfo(detail *models.AutomaticReduceDetail) error {
  2937. ut := writeDb.Begin()
  2938. err := ut.Create(&detail).Error
  2939. if err != nil {
  2940. ut.Rollback()
  2941. return err
  2942. }
  2943. ut.Commit()
  2944. return err
  2945. }
  2946. func AddSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail) error {
  2947. err := writeDb.Create(&detail).Error
  2948. return err
  2949. }
  2950. func AddNewSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail, tx *gorm.DB) error {
  2951. err := tx.Create(&detail).Error
  2952. if err != nil {
  2953. tx.Rollback()
  2954. return err
  2955. }
  2956. return err
  2957. }
  2958. func FindStockOutInfoByTypeId(org_id int64, good_type_id int64, good_id int64, out_id int64, number string) (out models.WarehouseOutInfo, err error) {
  2959. err = readDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND good_type_id = ? AND good_id = ? AND is_sys = 1 AND (warehouse_out_id = ? or warehouse_out_order_number = ?)", org_id, good_type_id, good_id, out_id, number).First(&out).Error
  2960. return
  2961. }
  2962. func FindStockOutInfoByStock(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (out []*models.WarehouseOutInfo, err error) {
  2963. err = readDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND good_type_id = ? AND good_id = ? AND is_sys = 1 AND sys_record_time = ? AND patient_id = ?", org_id, good_type_id, good_id, record_date, patient_id).Order("ctime desc").Find(&out).Error
  2964. return
  2965. }
  2966. func FindStockOutInfoByStockOne(org_id int64, good_type_id int64, good_id int64, record_date int64) (out []*models.WarehouseOutInfo, err error) {
  2967. err = readDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND good_type_id = ? AND good_id = ? AND is_sys = 1 AND sys_record_time = ?", org_id, good_type_id, good_id, record_date).Order("ctime desc").Find(&out).Error
  2968. return
  2969. }
  2970. func FindStockOutInfoByStockTwo(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (models.WarehouseOutInfo, error) {
  2971. info := models.WarehouseOutInfo{}
  2972. err = readDb.Model(&info).Where("status = 1 AND org_id = ? AND good_type_id = ? AND good_id = ? AND is_sys = 1 AND sys_record_time = ? and patient_id = ?", org_id, good_type_id, good_id, record_date, patient_id).Order("ctime desc").Find(&info).Error
  2973. return info, err
  2974. }
  2975. func FindDrugStockOutInfoByTypeId(org_id int64, drug_id int64, out_id int64, number string) (out models.DrugWarehouseOutInfo, err error) {
  2976. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND drug_id = ? AND is_sys = 1 AND (warehouse_out_id = ? or warehouse_out_order_number = ?)", org_id, drug_id, out_id, number).First(&out).Error
  2977. return
  2978. }
  2979. func UpdateStockOutInfoCount(org_id int64, id int64) {
  2980. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", 1))
  2981. }
  2982. func FindUserDetailById(org_id int64, id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  2983. db := readDb.Model(&models.AutomaticReduceDetail{})
  2984. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  2985. db = db.Preload("GoodsType", "status = 1")
  2986. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  2987. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND good_id != 0 AND count <> 0 ", org_id, id)
  2988. db = db.Count(&total)
  2989. err = db.Find(&user).Error
  2990. return
  2991. }
  2992. type DrugAutomaticReduceDetail struct {
  2993. ID int64 `gorm:"column:id" json:"id" form:"id"`
  2994. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  2995. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  2996. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  2997. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  2998. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  2999. Status int64 `gorm:"column:status" json:"status" form:"status"`
  3000. RecordTime int64 `gorm:"column:record_time" json:"record_time" form:"record_time"`
  3001. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  3002. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  3003. Count int64 `gorm:"column:count" json:"count" form:"count"`
  3004. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  3005. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  3006. Patients *models.Patients `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"user"`
  3007. DrugWarehouseOutInfo []*models.DrugWarehouseOutInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"drugwarehouseoutinfo"`
  3008. }
  3009. func (DrugAutomaticReduceDetail) TableName() string {
  3010. return "xt_drug_automatic_reduce_detail"
  3011. }
  3012. func FindDrugStockUserDetailById(org_id int64, id int64) (user []*DrugAutomaticReduceDetail, err error, total int64) {
  3013. db := readDb.Model(&DrugAutomaticReduceDetail{})
  3014. db = db.Preload("Drug", "org_id = ? AND status = 1", org_id)
  3015. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3016. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND drug_id != 0", org_id, id)
  3017. db = db.Count(&total)
  3018. err = db.Find(&user).Error
  3019. return
  3020. }
  3021. func UpdateStockOutInfoCount2(org_id int64, id int64, count int64) {
  3022. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3023. }
  3024. func UpdateDrugStockOutInfoCount2(org_id int64, id int64, count int64) {
  3025. writeDb.Model(&models.DrugWarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3026. }
  3027. func FindPatientAutomaticReduceRecord(org_id int64, record_time int64, good_id int64, good_type_id int64, patient_id int64) (count int64, err error) {
  3028. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("org_id = ?AND status = 1 AND good_id = ? AND good_type_id = ? AND patient_id = ? AND record_time = ? AND count <> 0", org_id, good_id, good_type_id, patient_id, record_time).Count(&count).Error
  3029. return
  3030. }
  3031. func FindPatientDrugAutomaticReduceRecord(org_id int64, record_time int64, drug_id int64, patient_id int64) (count int64, err error) {
  3032. err = readDb.Model(&models.DrugAutomaticReduceDetail{}).Where("org_id = ? AND status = 1 AND drug_id = ? AND patient_id = ? AND record_time = ?", org_id, drug_id, patient_id, record_time).Count(&count).Error
  3033. return
  3034. }
  3035. type VMWarehousingInfo struct {
  3036. ID int64 `gorm:"column:id" json:"id"`
  3037. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id"`
  3038. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  3039. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  3040. Status int64 `gorm:"column:status" json:"status"`
  3041. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3042. GoodInfo GoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"good_info"`
  3043. }
  3044. func (VMWarehousingInfo) TableName() string {
  3045. return "xt_warehouse_info"
  3046. }
  3047. type GoodInfo struct {
  3048. ID int64 `gorm:"column:id" json:"id"`
  3049. SpecificationName string `gorm:"column:specification_name" json:"specification_name"`
  3050. Status int64 `gorm:"column:status" json:"status"`
  3051. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3052. }
  3053. func (GoodInfo) TableName() string {
  3054. return "xt_good_information"
  3055. }
  3056. func GetStockInGoodInfo(orgId int64) (stockIn []*VMWarehousingInfo, err error) {
  3057. err = readDb.Model(&VMWarehousingInfo{}).Preload("GoodInfo", "status = 1 AND org_id = ?", orgId).Where("org_id = ? AND status = 1", orgId).Find(&stockIn).Error
  3058. return
  3059. }
  3060. func GetAllStockOutUserDetail(patient_id int64, orgId int64, record_time int64) (goodUser []*models.DialysisBeforePrepare, err error) {
  3061. err = readDb.Model(&models.DialysisBeforePrepare{}).Where("patient_id = ? AND user_org_id = ? AND record_date = ? AND status = 1 AND count <> 0 ", patient_id, orgId, record_time).Find(&goodUser).Error
  3062. return
  3063. }
  3064. func PostSearchStock(keyword string, orgid int64) (goods []*models.GoodsType, err error) {
  3065. likeKey := "%" + keyword + "%"
  3066. db := XTReadDB().Model(&goods)
  3067. if len(keyword) > 0 {
  3068. db = db.Where("type_name like ?", likeKey)
  3069. }
  3070. err = db.Where("org_id = ? and status =1", orgid).Find(&goods).Error
  3071. return goods, err
  3072. }
  3073. func GetStockType(orgid int64) (goodstype []*models.GoodsTypeOne, err error) {
  3074. err = XTReadDB().Where("org_id = ? and status = 1 and (stock_attribute = 2 or stock_attribute = 3 or stock_attribute = 4)", orgid).
  3075. Preload("GoodInfo", "org_id = ? and status = 1 and find_in_set('停用',good_status) = 0", orgid).Find(&goodstype).Error
  3076. return goodstype, err
  3077. }
  3078. func GetDialyStockOut(orgid int64, recordtime int64, patientId int64) (prepare []*models.XtDialysisBeforePrepare, err error) {
  3079. db := XTReadDB().Table("dialysis_before_prepare as x").Where("x.status = 1")
  3080. if orgid > 0 {
  3081. db = db.Where("x.user_org_id = ?", orgid)
  3082. }
  3083. if recordtime > 0 {
  3084. db = db.Where("x.record_date = ?", recordtime)
  3085. }
  3086. if patientId > 0 {
  3087. db = db.Where("x.patient_id = ?", patientId)
  3088. }
  3089. err = db.Select("x.id,x.user_org_id,x.patient_id,x.record_date,x.good_id,x.good_type_id,x.count,x.commdity_code,t.specification_name").Joins("left join xt_good_information as t on t.id = x.good_id and t.org_id = ? and t.status = 1", orgid).Scan(&prepare).Error
  3090. return prepare, err
  3091. }
  3092. func GetAutoReduceRecordInfoById(orgid int64, recordtime int64, goodid int64, goodtypeid int64) (*models.XtAutomaticReduceDetail, error) {
  3093. detail := models.XtAutomaticReduceDetail{}
  3094. err := XTReadDB().Model(&detail).Where("org_id = ? and record_time = ? and good_id = ? and good_type_id = ? and status =1", orgid, recordtime, goodid, goodtypeid).Find(&detail).Error
  3095. if err == gorm.ErrRecordNotFound {
  3096. return nil, err
  3097. }
  3098. if err != nil {
  3099. return nil, err
  3100. }
  3101. return &detail, nil
  3102. }
  3103. func GetOutStockTotalCount(wareouttime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3104. err = XTReadDB().Raw("SELECT good_id,SUM(b.count) as count FROM (SELECT DISTINCT x.patient_id,x.good_id,x.count FROM xt_automatic_reduce_detail as x WHERE x.org_id = ? and x.record_time = ? and `status` = 1) as b GROUP BY good_id", orgid, wareouttime).Scan(&autoMatic).Error
  3105. return autoMatic, err
  3106. }
  3107. func GetOutStockTotalCountOne(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3108. err = XTReadDB().Raw("SELECT good_id,SUM(b.count) as count FROM (SELECT DISTINCT x.patient_id,x.good_id,x.count FROM xt_automatic_reduce_detail as x WHERE x.org_id = ? and x.record_time >= ? and x.record_time<=? and `status` = 1) as b GROUP BY good_id", orgid, startime, endtime).Scan(&autoMatic).Error
  3109. return autoMatic, err
  3110. }
  3111. func GetManufacturerName(orgid int64, name string) (*models.Manufacturer, error) {
  3112. var manufacturer models.Manufacturer
  3113. var err error
  3114. err = XTReadDB().Model(&manufacturer).Where("org_id = ? and manufacturer_name = ? and status = 1", orgid, name).Find(&manufacturer).Error
  3115. if err == gorm.ErrRecordNotFound {
  3116. return nil, err
  3117. }
  3118. if err != nil {
  3119. return nil, err
  3120. }
  3121. return &manufacturer, nil
  3122. }
  3123. func CreateManufacturer(manufacturer *models.Manufacturer) error {
  3124. err := XTWriteDB().Create(manufacturer).Error
  3125. return err
  3126. }
  3127. func UpdatedManufactur(manufacturer *models.Manufacturer, orgid int64) error {
  3128. err := XTWriteDB().Model(&manufacturer).Where("org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"manufacturer_name": manufacturer.ManufacturerName}).Error
  3129. return err
  3130. }
  3131. func GetGoodsInformaitonIsExist(goodName string, goodType int64, specificationName string, orgid int64) (*models.GoodInfo, error) {
  3132. var good models.GoodInfo
  3133. var err error
  3134. err = XTReadDB().Model(&good).Where("good_name = ? and good_type_id = ? and specification_name = ? and org_id = ? and status = 1", goodName, goodType, specificationName, orgid).Find(&good).Error
  3135. if err == gorm.ErrRecordNotFound {
  3136. return nil, err
  3137. }
  3138. if err != nil {
  3139. return nil, err
  3140. }
  3141. return &good, nil
  3142. }
  3143. func CreateGoodsInfomation(info *models.GoodInfo) error {
  3144. err := XTWriteDB().Create(&info).Error
  3145. return err
  3146. }
  3147. func UpdateGoodsInformation(info *models.GoodInfo, goodName string, goodType int64, specificationName string, orgid int64) error {
  3148. err := XTWriteDB().Model(&info).Where("good_name = ? and good_type_id = ? and specification_name = ? and org_id = ? and status = 1", goodName, goodType, specificationName, orgid).Updates(map[string]interface{}{"good_name": info.GoodName, "good_type_id": info.GoodTypeId, "medical_insurance_level": info.MedicalInsuranceLevel, "good_kind": info.GoodKind, "specification_name": info.SpecificationName, "manufacturer": info.Manufacturer, "good_unit": info.GoodUnit, "retail_price": info.RetailPrice, "stock_warn_count": info.StockWarnCount, "dealer": info.Dealer, "pinyin": info.Pinyin, "wubi": info.Wubi, "buy_price": info.BuyPrice, "medical_insurance_number": info.MedicalInsuranceNumber, "is_special_diseases": info.IsSpecialDiseases, "is_record": info.IsRecord, "statistics_category": info.StatisticsCategory, "good_status": info.GoodStatus, "social_security_directory_code": info.SocialSecurityDirectoryCode, "production_type": info.ProductionType, "special_medical": info.SpecialMedical, "remark": info.Remark}).Error
  3149. return err
  3150. }
  3151. func GetCoutWareseOutInfo(startime int64, endtime int64, orgid int64) (houseOutInfo []*models.VmWarehouseOutInfo, err error) {
  3152. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  3153. err = db.Select("x.good_id,Sum(x.count) as count,x.remark,x.price,x.org_id").Where("x.ctime >=? and x.ctime<=? and x.org_id = ?", startime, endtime, orgid).Group("x.good_id").Scan(&houseOutInfo).Error
  3154. return houseOutInfo, err
  3155. }
  3156. func GetGoodInfomationList(orgid int64, keyword string) (goodInfo []*models.GoodInfo, err error) {
  3157. key := "%" + keyword + "%"
  3158. if len(keyword) == 0 {
  3159. err = XTReadDB().Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Find(&goodInfo).Error
  3160. } else {
  3161. err = XTReadDB().Where("org_id = ? and status = 1 AND good_name like ? AND find_in_set('停用',good_status) = 0", orgid, key).Find(&goodInfo).Error
  3162. }
  3163. return goodInfo, err
  3164. }
  3165. func IsExistDrugByName(drugname string, drugdesc string, orgid int64) (*models.BaseDrugLib, error) {
  3166. var drug models.BaseDrugLib
  3167. var err error
  3168. err = XTReadDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and status =1 and org_id = ?", drugname, drugdesc, orgid).Find(&drug).Error
  3169. if err == gorm.ErrRecordNotFound {
  3170. return nil, err
  3171. }
  3172. if err != nil {
  3173. return nil, err
  3174. }
  3175. return &drug, nil
  3176. }
  3177. func IsExistDrugByNameOne(drugname string, dosage string, doseunit string, minnumber int64, minunit string, packtunit string, orgid int64) (*models.BaseDrugLib, error) {
  3178. var drug models.BaseDrugLib
  3179. var err error
  3180. err = XTReadDB().Model(&drug).Where("drug_name = ? and dose = ? and dose_unit = ? and min_number = ? and min_unit=? and max_unit = ? and status =1 and org_id = ?", drugname, dosage, doseunit, minnumber, minunit, packtunit, orgid).Find(&drug).Error
  3181. if err == gorm.ErrRecordNotFound {
  3182. return nil, err
  3183. }
  3184. if err != nil {
  3185. return nil, err
  3186. }
  3187. return &drug, nil
  3188. }
  3189. func CreateDrugsInfomation(info *models.BaseDrugLib) error {
  3190. err := XTWriteDB().Create(&info).Error
  3191. return err
  3192. }
  3193. func UpdateDrugsInformation(drug *models.BaseDrugLib, drugName string, drugSpec string, orgid int64) error {
  3194. err := XTWriteDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and org_id = ? and status = 1", drugName, drugSpec, orgid).Updates(map[string]interface{}{"drug_name": drug.DrugName, "drug_alias": drug.DrugAlias, "drug_spec": drug.DrugSpec, "drug_type": drug.DrugType, "drug_stock_limit": drug.DrugStockLimit, "drug_origin_place": drug.DrugOriginPlace, "drug_dosage_form": drug.DrugDosageForm, "medical_insurance_level": drug.MedicalInsuranceLevel, "max_unit": drug.MaxUnit, "min_unit": drug.MinUnit, "unit_matrixing": drug.UnitMatrixing, "retail_price": drug.RetailPrice, "last_price": drug.LastPrice, "drug_classify": drug.DrugClassify, "manufacturer": drug.Manufacturer, "dealer": drug.Dealer, "pinyin": drug.Pinyin, "wubi": drug.Wubi, "drug_alias_pinyin": drug.DrugAliasPinyin, "drug_alias_wubi": drug.DrugAliasWubi, "drug_category": drug.DrugCategory, "drug_control": drug.DrugControl, "number": drug.Number, "hosp_appr_flag": drug.HospApprFlag, "medical_insurance_number": drug.MedicalInsuranceNumber, "pharmacology_category": drug.PharmacologyCategory, "statistics_category": drug.StatisticsCategory, "code": drug.Code, "is_special_diseases": drug.IsSpecialDiseases, "is_record": drug.IsRecord, "prescription_mark": drug.PrescriptionMark, "social_security_directory_code": drug.SocialSecurityDirectoryCode, "record_date": drug.RecordDate, "drug_remark": drug.DrugRemark, "drug_status": drug.DrugStatus, "limit_remark": drug.LimitRemark, "drug_dose": drug.DrugDose, "drug_dose_unit": drug.DrugDoseUnit, "lmt_used_flag": drug.LmtUsedFlag, "delivery_way": drug.DeliveryWay, "execution_frequency": drug.ExecutionFrequency, "drug_day": drug.DrugDay}).Error
  3195. return err
  3196. }
  3197. func GetProjectByNameIsExsit(projectName string, orgid int64) (*models.HisProject, error) {
  3198. var projoect models.HisProject
  3199. var err error
  3200. err = XTReadDB().Model(&projoect).Where("project_name = ? and user_org_id = ? and status =1", projectName, orgid).Find(&projoect).Error
  3201. if err == gorm.ErrRecordNotFound {
  3202. return nil, err
  3203. }
  3204. if err != nil {
  3205. return nil, err
  3206. }
  3207. return &projoect, nil
  3208. }
  3209. func CreateProjectInfomation(info *models.HisProject) error {
  3210. err := XTWriteDB().Create(&info).Error
  3211. return err
  3212. }
  3213. func UpdateProjectByName(project *models.HisProject, projectName string, orgid int64) error {
  3214. err := XTWriteDB().Model(&project).Where("project_name = ? and user_org_id = ? and status = 1", projectName, orgid).Updates(map[string]interface{}{"project_name": project.ProjectName, "price": project.Price, "unit": project.Unit, "cost_classify": project.CostClassify, "executive_section": project.ExecutiveSection, "medical_coverage": project.MedicalCoverage, "pinyin": project.Pinyin, "wubi": project.Wubi, "statistical_classification": project.StatisticalClassification, "disease_directory": project.DiseaseDirectory, "is_record": project.IsRecord, "medical_code": project.MedicalCode, "tube_color": project.TubeColor, "single_dose": project.SingleDose, "delivery_way": project.DeliveryWay, "execution_frequency": project.ExecutionFrequency, "number_days": project.NumberDays, "total": project.Total, "medical_status": project.MedicalStatus, "category": project.Category, "specail_project": project.SpecailProject, "social_security_directory_code": project.SocialSecurityDirectoryCode, "record_date": project.RecordDate, "remark": project.Remark}).Error
  3215. return err
  3216. }
  3217. func GetWareOutInfoById(warehouseoutid int64, orgid int64) (warehouseinfo []*models.WarehouseOutInfo, err error) {
  3218. err = XTReadDB().Model(&warehouseinfo).Where("warehouse_out_id = ? and org_id = ? and status= 1 and is_sys = 0", warehouseoutid, orgid).Find(&warehouseinfo).Error
  3219. return warehouseinfo, err
  3220. }
  3221. func EditDrugWarehousingOne(warehouse models.DrugWarehouse, id int64) error {
  3222. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("warehousing_order = ? AND status = 1 and id = ?", warehouse.WarehousingOrder, id).Update(map[string]interface{}{"mtime": time.Now().Unix(), "warehousing_time": warehouse.WarehousingTime, "modifier": warehouse.Modifier, "dealer": warehouse.Dealer, "manufacturer": warehouse.Manufacturer, "storehouse_id": warehouse.StorehouseId}).Error
  3223. return err
  3224. }
  3225. func GetAllManufacturerList(orgid int64) (list []*models.Manufacturer, err error) {
  3226. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3227. return list, err
  3228. }
  3229. func GetNewAllManufacturerList(orgid int64) (list []*models.BloodManufacturer, err error) {
  3230. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3231. return list, err
  3232. }
  3233. func GetAllDealerList(orgid int64) (list []*models.Dealer, err error) {
  3234. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3235. return list, err
  3236. }
  3237. // func GetAllDealerListTX(orgid int64,tx *gorm.DB) (list []*models.Dealer, err error) {
  3238. // err = tx.Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3239. // return list, err
  3240. // }
  3241. func GetDealerByName(orgid int64, name string) (*models.Dealer, error) {
  3242. var dealer models.Dealer
  3243. var err error
  3244. err = XTReadDB().Model(&dealer).Where("org_id = ? and dealer_name = ? and status = 1", orgid, name).Find(&dealer).Error
  3245. if err == gorm.ErrRecordNotFound {
  3246. return nil, err
  3247. }
  3248. if err != nil {
  3249. return nil, err
  3250. }
  3251. return &dealer, nil
  3252. }
  3253. func CreateDealer(dealer *models.Dealer) error {
  3254. err := XTWriteDB().Create(&dealer).Error
  3255. return err
  3256. }
  3257. func UpdateDealer(dealer *models.Dealer, orgid int64) error {
  3258. err := XTWriteDB().Model(&dealer).Where("org_id = ? and status =1", orgid).Updates(map[string]interface{}{"dealer_name": dealer.DealerName}).Error
  3259. return err
  3260. }
  3261. func GetDrugDataConfig(orgid int64, name string) (models.DictDataconfig, error) {
  3262. dataconfig := models.DictDataconfig{}
  3263. err := XTReadDB().Where("org_id = ? and name = ?", orgid, name).Find(&dataconfig).Error
  3264. return dataconfig, err
  3265. }
  3266. func GetParentDataConfig(parentid int64, orgid int64) (dataconfig []*models.DictDataconfig, err error) {
  3267. err = XTReadDB().Where("parent_id = ? and status =1 and (org_id = ? or org_id = 0)", parentid, orgid).Find(&dataconfig).Error
  3268. return dataconfig, err
  3269. }
  3270. func IsExistDicConfig(parentid int64, goodKind string, orgid int64) (*models.DictDataconfig, error) {
  3271. var dataconfig models.DictDataconfig
  3272. var err error
  3273. err = XTReadDB().Where("parent_id = ? and status = 1 and name = ? and (org_id = ? or org_id = 0)", parentid, goodKind, orgid).Find(&dataconfig).Error
  3274. if err == gorm.ErrRecordNotFound {
  3275. return nil, err
  3276. }
  3277. if err != nil {
  3278. return nil, err
  3279. }
  3280. return &dataconfig, nil
  3281. }
  3282. func GetLastDicConfig(parentid int64, orgid int64) (models.DictDataconfig, error) {
  3283. dataconfig := models.DictDataconfig{}
  3284. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status = 1", parentid, orgid).Last(&dataconfig).Error
  3285. return dataconfig, err
  3286. }
  3287. func CreatedDicConfig(dataconfig *models.DictDataconfig) error {
  3288. err := XTWriteDB().Create(&dataconfig).Error
  3289. return err
  3290. }
  3291. func UpdatedDicConfig(parentid int64, dataconfig *models.DictDataconfig, orgid int64, goodKind string) error {
  3292. err := XTWriteDB().Model(&dataconfig).Where("parent_id = ? and org_id = ? and name = ?", parentid, orgid, goodKind).Updates(map[string]interface{}{"name": dataconfig.Name}).Error
  3293. return err
  3294. }
  3295. func GetGoodType(goodName string, orgid int64) (*models.GoodsType, error) {
  3296. var goodType models.GoodsType
  3297. var err error
  3298. err = XTReadDB().Where("type_name =? and org_id = ? and status = 1", goodName, orgid).Find(&goodType).Error
  3299. if err == gorm.ErrRecordNotFound {
  3300. return nil, err
  3301. }
  3302. if err != nil {
  3303. return nil, err
  3304. }
  3305. return &goodType, nil
  3306. }
  3307. func CreatedGoodType(goodsType *models.GoodsType) error {
  3308. err := XTWriteDB().Create(&goodsType).Error
  3309. return err
  3310. }
  3311. func GetAllGoodType(orgid int64) (goodsType []*models.GoodsType, err error) {
  3312. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3313. return goodsType, err
  3314. }
  3315. func GetAllNewGoodType(orgid int64) (goodsType []*models.NewGoodsType, err error) {
  3316. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3317. return goodsType, err
  3318. }
  3319. func GetDepartMentByName(name string, orgid int64) (*models.XtHisDepartment, error) {
  3320. var department models.XtHisDepartment
  3321. var err error
  3322. err = XTReadDB().Where("name= ? and user_org_id = ? and status = 1", name, orgid).Find(&department).Error
  3323. if err == gorm.ErrRecordNotFound {
  3324. return nil, err
  3325. }
  3326. if err != nil {
  3327. return nil, err
  3328. }
  3329. return &department, nil
  3330. }
  3331. func GetDataConfigIsExist(orgid int64, name string) (models.Dataconfig, error) {
  3332. dataconfig := models.Dataconfig{}
  3333. err := XTReadDB().Model(&dataconfig).Where("org_id = ? and name = ? and status = 1", orgid, name).Find(&dataconfig).Error
  3334. return dataconfig, err
  3335. }
  3336. func GetChildeConfigIsExist(parentid int64, name string, orgid int64) (*models.Dataconfig, error) {
  3337. var dataconfig models.Dataconfig
  3338. var err error
  3339. err = XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and name = ? and status = 1", parentid, orgid, name).Find(&dataconfig).Error
  3340. if err == gorm.ErrRecordNotFound {
  3341. return nil, err
  3342. }
  3343. if err != nil {
  3344. return nil, err
  3345. }
  3346. return &dataconfig, nil
  3347. }
  3348. func GetLastChildeConfig(parentid int64, orgid int64) (models.Dataconfig, error) {
  3349. dataconfig := models.Dataconfig{}
  3350. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status =1", parentid, orgid).Last(&dataconfig).Error
  3351. return dataconfig, err
  3352. }
  3353. func CreateDataConfig(dataconfig *models.Dataconfig) error {
  3354. err := XTWriteDB().Create(&dataconfig).Error
  3355. return err
  3356. }
  3357. func FindAllDataConfigList(orgid int64, parentId int64) (dataconfig []*models.Dataconfig, err error) {
  3358. err = XTReadDB().Model(&dataconfig).Where("(org_id = ? or org_id = 0) and parent_id = ? and status = 1", orgid, parentId).Find(&dataconfig).Error
  3359. return dataconfig, err
  3360. }
  3361. func GetWarehouseOrderInfoList(orgid int64) (list []*models.WarehousingInfo, err error) {
  3362. err = XTReadDB().Model(&list).Where("org_id = ? and status = 1", orgid).Order("id asc").Group("good_id").Find(&list).Error
  3363. return list, err
  3364. }
  3365. func GetSearchDrugList(keywords string, orgid int64) (drug []*models.BaseDrugLib, err error) {
  3366. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3367. if len(keywords) > 0 {
  3368. likekey := "%" + keywords + "%"
  3369. err = db.Where("(drug_name LIKE ?)", likekey).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3370. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3371. }).Find(&drug).Error
  3372. } else {
  3373. err = db.Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3374. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3375. }).Find(&drug).Error
  3376. }
  3377. return
  3378. }
  3379. func GetSearchDrugListSix(keywords string, orgid int64, storehouse_id int64) (drug []*models.BaseDrugLib, err error) {
  3380. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3381. if len(keywords) > 0 {
  3382. likekey := "%" + keywords + "%"
  3383. err = db.Where("(drug_name LIKE ?)", likekey).Preload("LastDrugWarehouseInfo", "org_id=? and status = 1 and (stock_max_number > 0 or stock_min_number>0) and storehouse_id = ?", orgid, storehouse_id).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3384. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3385. }).Find(&drug).Error
  3386. } else {
  3387. err = db.Preload("LastDrugWarehouseInfo", "org_id=? and status = 1 and (stock_max_number > 0 or stock_min_number>0) and storehouse_id = ?", orgid, storehouse_id).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3388. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3389. }).Find(&drug).Error
  3390. }
  3391. return
  3392. }
  3393. func GetSearchGoodList(keywords string, orgid int64) (good []*models.GoodInfo, err error) {
  3394. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 and good_status <> '停用'", orgid)
  3395. if len(keywords) > 0 {
  3396. likekey := "%" + keywords + "%"
  3397. err = db.Where("(good_name Like ?)", likekey).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3398. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3399. }).Find(&good).Error
  3400. } else {
  3401. err = db.Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3402. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3403. }).Find(&good).Error
  3404. }
  3405. return
  3406. }
  3407. func GetSearchGoodListSix(keywords string, orgid int64, storehouse_id int64) (good []*models.GoodInfoSevenThirty, err error) {
  3408. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 AND find_in_set('停用',good_status) = 0", orgid)
  3409. if len(keywords) > 0 {
  3410. likekey := "%" + keywords + "%"
  3411. err = db.Where("(good_name Like ?)", likekey).Preload("LaStWarehousingInfo", "org_id=? and status = 1 and stock_count <> 0 and storehouse_id =?", orgid, storehouse_id).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3412. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id =?", orgid, storehouse_id)
  3413. }).Find(&good).Error
  3414. } else {
  3415. err = db.Preload("LaStWarehousingInfo", "org_id=? and status = 1 and stock_count <> 0 and storehouse_id =?", orgid, storehouse_id).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3416. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id)
  3417. }).Find(&good).Error
  3418. }
  3419. return
  3420. }
  3421. func CreateAutoReduceRecord(detail *models.BloodAutomaticReduceDetail) error {
  3422. db := writeDb.Begin()
  3423. err := db.Create(&detail).Error
  3424. if err != nil {
  3425. db.Rollback()
  3426. return err
  3427. }
  3428. db.Commit()
  3429. return err
  3430. }
  3431. func GetAutoMaticReduceDetail(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (*models.BloodAutomaticReduceDetail, error) {
  3432. var autoreduece models.BloodAutomaticReduceDetail
  3433. var err error
  3434. err = XTReadDB().Model(&autoreduece).Where("org_id = ? and patient_id = ? and status = 1 and record_time = ? and good_id = ? and good_type_id = ? and count<> 0", orgid, patient_id, recordtime, goodid, goodtypeid).Find(&autoreduece).Error
  3435. if err == gorm.ErrRecordNotFound {
  3436. return nil, err
  3437. }
  3438. if err != nil {
  3439. return nil, err
  3440. }
  3441. return &autoreduece, nil
  3442. }
  3443. func GetAutoMaticReduceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (models.BloodAutomaticReduceDetail, error) {
  3444. auto := models.BloodAutomaticReduceDetail{}
  3445. err = XTReadDB().Model(&auto).Where("org_id = ? and patient_id = ? and status = 1 and record_time = ? and good_id = ? and good_type_id = ? and count>0", orgid, patient_id, recordtime, goodid, goodtypeid).Find(&auto).Error
  3446. return auto, err
  3447. }
  3448. func DeleteAutoRedeceDetailTwo(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) error {
  3449. detail := models.BloodAutomaticReduceDetail{}
  3450. err := XTWriteDB().Model(&detail).Where("org_id = ? and patient_id = ? and record_time = ? and status =1 and good_id = ? and good_type_id = ?", orgid, patient_id, recordtime, goodid, goodtypeid).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3451. return err
  3452. }
  3453. func GetWarehosueOutByStockFlow(patient_id int64, recrod_date int64, goodid int64) (stockflow []*models.VmStockFlow, err error) {
  3454. err = XTReadDB().Where("patient_id = ? and system_time = ? and good_id = ? and consumable_type = 3 and status = 1", patient_id, recrod_date, goodid).Find(&stockflow).Error
  3455. return stockflow, err
  3456. }
  3457. func UpdateAutoGood(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64, count int64) (err error) {
  3458. err = XTWriteDB().Model(models.BloodAutomaticReduceDetail{}).Where("org_id = ? and patient_id = ? and record_time = ? and status =1 and good_id = ?", orgid, patient_id, recordtime, goodid).UpdateColumn("count", gorm.Expr("count - ?", count)).Error
  3459. err = XTWriteDB().Model(models.WarehouseOutInfo{}).Where("org_id = ? and patient_id = ? and sys_record_time = ? and good_id = ? and status = 1", orgid, patient_id, recordtime, goodid).UpdateColumn("count", gorm.Expr("count - ?", count)).Error
  3460. err = XTWriteDB().Model(&models.DialysisBeforePrepare{}).Where("user_org_id = ? and patient_id = ? and record_date = ? and good_id = ? and status = 1", orgid, patient_id, recordtime, goodid).UpdateColumn("count", gorm.Expr("count - ?", count)).Error
  3461. return err
  3462. }
  3463. func DeleteAutoRedeceDetailTen(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3464. detail := models.BloodAutomaticReduceDetail{}
  3465. err := XTWriteDB().Model(&detail).Where("org_id = ? and patient_id = ? and record_time = ? and status =1 and good_id = ? ", orgid, patient_id, recordtime, goodid).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3466. info := models.WarehouseOutInfo{}
  3467. err = XTWriteDB().Model(&info).Where("org_id = ? and patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 and project_id = ?", orgid, patient_id, recordtime, goodid, project_id).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3468. prepare := models.DialysisBeforePrepare{}
  3469. err = XTWriteDB().Model(&prepare).Where("user_org_id = ? and patient_id = ? and record_date = ? and good_id = ? and status = 1", orgid, patient_id, recordtime, goodid).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3470. flow := models.VmStockFlow{}
  3471. err = XTWriteDB().Model(&flow).Where("user_org_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1 and project_id = ?", orgid, patient_id, recordtime, goodid, project_id).Updates(map[string]interface{}{"is_read": 1}).Error
  3472. return err
  3473. }
  3474. func DeleteAutoRedeceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3475. detail := models.BloodAutomaticReduceDetail{}
  3476. err := XTWriteDB().Model(&detail).Where("org_id = ? and patient_id = ? and record_time = ? and status =1 and good_id = ? and project_id = ?", orgid, patient_id, recordtime, goodid, project_id).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3477. info := models.WarehouseOutInfo{}
  3478. err = XTWriteDB().Model(&info).Where("org_id = ? and patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 and project_id = ?", orgid, patient_id, recordtime, goodid, project_id).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3479. prepare := models.DialysisBeforePrepare{}
  3480. err = XTWriteDB().Model(&prepare).Where("user_org_id = ? and patient_id = ? and record_date = ? and good_id = ? and status = 1", orgid, patient_id, recordtime, goodid).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3481. flow := models.VmStockFlow{}
  3482. err = XTWriteDB().Model(&flow).Where("user_org_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1 and project_id = ?", orgid, patient_id, recordtime, goodid, project_id).Updates(map[string]interface{}{"is_read": 1}).Error
  3483. return err
  3484. }
  3485. func DeleteAutoRedeceDetailTenOne(orgid int64, patient_id int64, recordtime int64, goodid int64, order_id int64) error {
  3486. detail := models.BloodAutomaticReduceDetail{}
  3487. err := XTWriteDB().Model(&detail).Where("org_id = ? and patient_id = ? and record_time = ? and status =1 and good_id = ? ", orgid, patient_id, recordtime, goodid).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3488. info := models.WarehouseOutInfo{}
  3489. err = XTWriteDB().Model(&info).Where("org_id = ? and patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 and order_id = ?", orgid, patient_id, recordtime, goodid, order_id).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3490. prepare := models.DialysisBeforePrepare{}
  3491. err = XTWriteDB().Model(&prepare).Where("user_org_id = ? and patient_id = ? and record_date = ? and good_id = ? and status = 1", orgid, patient_id, recordtime, goodid).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3492. flow := models.VmStockFlow{}
  3493. err = XTWriteDB().Model(&flow).Where("user_org_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1 and order_id = ?", orgid, patient_id, recordtime, goodid, order_id).Updates(map[string]interface{}{"is_read": 1}).Error
  3494. return err
  3495. }
  3496. func UpDateWarehouseInfoByStockDelete(id int64, count int64, patient_id int64, record_time int64, goodid int64) (err error) {
  3497. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  3498. //err = writeDb.Model(&models.VmStockFlow{}).Where("warehousing_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1", id, patient_id, record_time, goodid).UpdateColumn("over_count", gorm.Expr("over_count + ?", count)).Error
  3499. return err
  3500. }
  3501. func GetCancelStockInfoByPatientId(patientid int64, goodid int64, record_date int64) (info []*models.CancelStockInfo, err error) {
  3502. err = XTReadDB().Where("patient_id = ? and good_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&info).Error
  3503. return info, err
  3504. }
  3505. func GetHisPrepscritionByPatientId(patientid int64, goodid int64, record_date int64) (list []*models.HisPrescriptionProject, err error) {
  3506. err = XTReadDB().Where("patient_id = ? and project_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&list).Error
  3507. return list, err
  3508. }
  3509. func UpDateWarehouStockFlowByStockDelete(id int64, recordtime int64, good_id int64, count int64, patient_id int64) (err error) {
  3510. err = writeDb.Model(&models.VmStockFlow{}).Where("warehousing_id = ? and system_time = ? and good_id = ? and patient_id = ?", id, recordtime, good_id, patient_id).UpdateColumn("count", gorm.Expr("count - ?", count)).Error
  3511. return err
  3512. }
  3513. func UpDateWarehouseInfoByStockDeleteOne(id int64, count int64) (err error) {
  3514. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  3515. return err
  3516. }
  3517. func GetAutoDialysisBefor(goodid int64, goodtypeid int64, patientid int64, orgid int64, recorddate int64) (*models.BloodAutomaticReduceDetail, error) {
  3518. var autoreduece models.BloodAutomaticReduceDetail
  3519. var err error
  3520. err = XTReadDB().Model(&autoreduece).Where("good_id = ? and good_type_id = ? and patient_id = ? and org_id = ? and status = 1 and record_time = ?", goodid, goodtypeid, patientid, orgid, recorddate).Find(&autoreduece).Error
  3521. if err == gorm.ErrRecordNotFound {
  3522. return nil, err
  3523. }
  3524. if err != nil {
  3525. return nil, err
  3526. }
  3527. return &autoreduece, nil
  3528. }
  3529. func GetAutoRecordByGoodIdSeven(goodid int64, patientid int64, recorddate int64) (models.WarehouseOutInfoSeven, error) {
  3530. detail := models.WarehouseOutInfoSeven{}
  3531. err := XTReadDB().Select("id,count").Where(" good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  3532. return detail, err
  3533. }
  3534. func GetAutoRecordByGoodIdSevenEight(goodid int64, patientid int64, recorddate int64) (detail []*models.WarehouseOutInfoSeven, err error) {
  3535. err = XTReadDB().Select("id,count").Where("good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  3536. return detail, err
  3537. }
  3538. func GetHisProjectPrescriptionByPatientId(patientid int64, recorddate int64) (project []*models.HisPrescriptionProject, err error) {
  3539. err = XTReadDB().Model(&project).Where("patient_id = ? and record_date = ? and status =1", patientid, recorddate).Find(&project).Error
  3540. return project, err
  3541. }
  3542. func GetHisProjectPrescriptionByPatientIdOne(id int64) (models.HisPrescriptionProject, error) {
  3543. project := models.HisPrescriptionProject{}
  3544. err = XTReadDB().Model(&project).Where("id = ? and status =1", id).Find(&project).Error
  3545. return project, err
  3546. }
  3547. func GetAllAutoRecordByPatient(patientid int64, recorddate int64) (auto []*models.WarehouseOutInfoNight, err error) {
  3548. err = XTReadDB().Select("count,warehouse_info_id,good_id,org_id,sys_record_time,patient_id,project_id,warehouse_info_id,warehouse_out_order_number,storehouse_id,price,dealer,manufacturer,good_type_id,product_date,expiry_date,number,license_number").Where(" patient_id = ? and sys_record_time = ? and status = 1 and is_sys = 1 and count>0", patientid, recorddate).Find(&auto).Error
  3549. return auto, err
  3550. }
  3551. func ModefyWarehouseInfo(count int64, id int64) error {
  3552. err := XTWriteDB().Model(models.WarehousingInfo{}).Where(" id =? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  3553. return err
  3554. }
  3555. func DeleteAutoWarehouse(patient_id int64, record_time int64) error {
  3556. err := XTWriteDB().Model(models.AutomaticReduceDetail{}).Where("patient_id = ? and record_time = ? and status = 1", patient_id, record_time).Updates(map[string]interface{}{"status": 0}).Error
  3557. err = XTWriteDB().Model(models.DialysisBeforePrepare{}).Where("patient_id = ? and record_date = ? and status = 1", patient_id, record_time).Updates(map[string]interface{}{"status": 0}).Error
  3558. err = XTWriteDB().Model(models.VmStockFlow{}).Where("patient_id = ? and system_time = ? and status = 1", patient_id, record_time).Updates(map[string]interface{}{"is_read": 1}).Error
  3559. //err = XTWriteDB().Model(models.WarehouseOutInfoNight{}).Where("patient_id = ? and sys_record_time = ? and status = 1", patient_id, record_time).Updates(map[string]interface{}{"status": 0}).Error
  3560. return err
  3561. }
  3562. func FindGoodInfoByIdTwo(id int64) (goodInfo models.GoodInfo, err error) {
  3563. err = readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  3564. return
  3565. }
  3566. func FindGoodInfoByIdSeven(id int64) (goodInfo models.GoodInfoSeven, err error) {
  3567. err = readDb.Select("id,good_name").Where("id = ? AND status = 1", id).First(&goodInfo).Error
  3568. return
  3569. }
  3570. func GetGoodWarehouseInfo(good_id int64) (info []*models.WarehousingInfo, err error) {
  3571. err = readDb.Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  3572. return info, err
  3573. }
  3574. func GetGoodWarehouseInfoSeven(good_id int64) (info []*models.WarehousingInfoSeven, err error) {
  3575. err = readDb.Select("stock_count").Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  3576. return info, err
  3577. }
  3578. func GetGoodWarehouseInfoSevenTen(good_id int64, storehouse_id int64) (info []*models.WarehousingInfoSeven, err error) {
  3579. err = readDb.Select("stock_count").Where("good_id = ? and status = 1 and stock_count<>0 and storehouse_id = ?", good_id, storehouse_id).Find(&info).Error
  3580. return info, err
  3581. }
  3582. func GetAllStockList(page int64, limit int64, startime int64, endtime int64, good_type int64, keyword string, orgid int64) (info []*models.VmWarehousingInfo, total int64, err error) {
  3583. offset := (page - 1) * limit
  3584. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1")
  3585. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3586. fmt.Println(table)
  3587. if startime > 0 {
  3588. db = db.Where("x.ctime>=?", startime)
  3589. }
  3590. if endtime > 0 {
  3591. db = db.Where("x.ctime<=?", endtime)
  3592. }
  3593. likeKey := "%" + keyword + "%"
  3594. if len(likeKey) > 0 {
  3595. db = db.Where("t.good_name like ?", likeKey)
  3596. }
  3597. if orgid > 0 {
  3598. db = db.Where("x.org_id = ?", orgid)
  3599. }
  3600. if good_type > 0 {
  3601. db = db.Where("t.good_type_id = ?", good_type)
  3602. }
  3603. err = db.Select("x.id,x.warehousing_id,x.good_id,x.good_type_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.warehousing_unit,x.stock_count,x.price,x.total_price,x.dealer,x.remark,x.ctime,x.is_return,x.warehousing_order,x.type,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.packing_unit,t.manufacturer").Joins("left join xt_good_information as t on t.id = x.good_id").Group("x.good_id").Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Scan(&info).Error
  3604. return info, total, err
  3605. }
  3606. func GetAllGoodInfoStockList(page int64, limit int64, startime int64, endtime int64, good_type int64, keyword string, orgid int64, good_id int64, storehouse_id int64) (info []*models.GoodInfo, total int64, err error) {
  3607. offset := (page - 1) * limit
  3608. db := XTReadDB().Table("xt_good_information as x").Where("x.status= 1")
  3609. likeKey := "%" + keyword + "%"
  3610. if len(likeKey) > 0 {
  3611. db = db.Where("x.good_name like ?", likeKey)
  3612. }
  3613. if orgid > 0 {
  3614. db = db.Where("x.org_id = ?", orgid)
  3615. }
  3616. if good_type > 0 {
  3617. db = db.Where("x.good_type_id = ?", good_type)
  3618. }
  3619. if good_id > 0 {
  3620. db = db.Where("x.id = ?", good_id)
  3621. }
  3622. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3623. if startime > 0 {
  3624. db = db.Where("ctime>=?", startime)
  3625. }
  3626. if endtime > 0 {
  3627. db = db.Where("ctime<=?", endtime)
  3628. }
  3629. if good_id > 0 {
  3630. db = db.Where("good_id = ?", good_id)
  3631. }
  3632. if storehouse_id > 0 {
  3633. db = db.Where("storehouse_id = ?", storehouse_id)
  3634. }
  3635. return db.Where("status = 1 and is_check = 1")
  3636. }).Preload("WarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3637. if startime > 0 {
  3638. db = db.Where("ctime>=?", startime)
  3639. }
  3640. if endtime > 0 {
  3641. db = db.Where("ctime<=?", endtime)
  3642. }
  3643. if good_id > 0 {
  3644. db = db.Where("good_id = ?", good_id)
  3645. }
  3646. if storehouse_id > 0 {
  3647. db = db.Where("storehouse_id = ?", storehouse_id)
  3648. }
  3649. return db.Where("status = 1").Group("good_id,storehouse_id")
  3650. }).Preload("CancelStockInfo", func(db *gorm.DB) *gorm.DB {
  3651. if startime > 0 {
  3652. db = db.Where("ctime>=?", startime)
  3653. }
  3654. if endtime > 0 {
  3655. db = db.Where("ctime<=?", endtime)
  3656. }
  3657. return db.Where("status = 1")
  3658. }).Find(&info).Error
  3659. return info, total, err
  3660. }
  3661. func GetStockListById(good_id int64, orgid int64, limit int64, page int64, startime int64, endtime int64, startfirsttime int64, endfirsttime int64) (info []*models.VmWarehousingInfo, total int64, err error) {
  3662. offset := (page - 1) * limit
  3663. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1 and x.is_check = 1")
  3664. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3665. tables := XTReadDB().Table("xt_stock_flow as f").Where("f.status = 1 and f.consumable_type = 1 and f.user_org_id = ?", orgid)
  3666. fmt.Println(table, tables)
  3667. if startime > 0 {
  3668. db = db.Where("x.ctime>=?", startime)
  3669. }
  3670. if endtime > 0 {
  3671. db = db.Where("x.ctime<=?", endtime)
  3672. }
  3673. if startfirsttime > 0 {
  3674. db = db.Where("x.expiry_date >=?", startfirsttime)
  3675. }
  3676. if endfirsttime > 0 {
  3677. db = db.Where("x.expiry_date <=?", endfirsttime)
  3678. }
  3679. if orgid > 0 {
  3680. db = db.Where("x.org_id = ?", orgid)
  3681. }
  3682. if good_id > 0 {
  3683. db = db.Where("x.good_id = ?", good_id)
  3684. }
  3685. err = db.Select("x.id,x.warehousing_id,x.good_id,x.good_type_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.warehousing_unit,x.stock_count,x.price,x.total_price,x.dealer,x.packing_price,t.manufacturer,x.remark,x.ctime,x.is_return,x.warehousing_order,x.type,x.storehouse_id,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.min_unit,t.buy_price,f.batch_number_count").Joins("left join xt_good_information as t on t.id = x.good_id").Joins("left join xt_stock_flow as f on f.warehousing_detail_id = x.id").Count(&total).Offset(offset).Limit(limit).Group("x.id").Order("x.id desc").Scan(&info).Error
  3686. return info, total, err
  3687. }
  3688. func GetStockOutList(good_id int64, orgid int64, limit int64, page int64, startime int64, endtime int64, is_sys int64) (info []*models.WarehouseOutInfo, total int64, err error) {
  3689. offset := (page - 1) * limit
  3690. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status= 1")
  3691. if startime > 0 {
  3692. db = db.Where("x.ctime>=?", startime)
  3693. }
  3694. if endtime > 0 {
  3695. db = db.Where("x.ctime<=?", endtime)
  3696. }
  3697. if orgid > 0 {
  3698. db = db.Where("x.org_id = ?", orgid)
  3699. }
  3700. if good_id > 0 {
  3701. db = db.Where("x.good_id = ?", good_id)
  3702. }
  3703. if is_sys == 2 {
  3704. db = db.Where("x.is_sys = 1")
  3705. }
  3706. if is_sys == 3 {
  3707. db = db.Where("x.is_sys = 0")
  3708. }
  3709. err = db.Select("x.id,x.warehouse_out_id,x.warehouse_info_id,x.good_id,x.good_type_id,x.warehousing_out_target,x.count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.warehouse_out_order_number,x.is_sys,x.number,x.dealer,x.manufacturer,x.consumable_type").Offset(offset).Count(&total).Scan(&info).Error
  3710. return info, total, err
  3711. }
  3712. func GetStockDrugCount(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  3713. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3714. if startime > 0 {
  3715. db = db.Where("x.ctime >=?", startime)
  3716. }
  3717. if endtime > 0 {
  3718. db = db.Where("x.ctime<=?", endtime)
  3719. }
  3720. if orgid > 0 {
  3721. db = db.Where("x.org_id = ?", orgid)
  3722. }
  3723. err = db.Select("sum(x.warehousing_count) as count,sum(x.stock_count) as stock_count,x.good_id").Group("x.good_id").Scan(&info).Error
  3724. return info, err
  3725. }
  3726. func GetAutoDiallysisBefor(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  3727. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3728. if startime > 0 {
  3729. db = db.Where("x.ctime >=?", startime)
  3730. }
  3731. if endtime > 0 {
  3732. db = db.Where("x.ctime<=?", endtime)
  3733. }
  3734. if orgid > 0 {
  3735. db = db.Where("x.org_id = ?", orgid)
  3736. }
  3737. err = db.Select("sum(x.count) as count,x.good_id").Where("is_sys = 0").Group("x.good_id").Scan(&info).Error
  3738. return info, err
  3739. }
  3740. func GetOutStockTotalCountFour(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3741. if endtime > 0 {
  3742. err = XTReadDB().Raw("SELECT good_id,SUM(b.count) as count FROM (SELECT DISTINCT x.patient_id,x.good_id,x.count,x.record_time FROM xt_automatic_reduce_detail as x WHERE x.org_id = ? and x.record_time >= ? and x.record_time <=? and `status` = 1) as b GROUP BY good_id", orgid, startime, endtime).Scan(&autoMatic).Error
  3743. }
  3744. if endtime == 0 {
  3745. err = XTReadDB().Raw("SELECT good_id,SUM(b.count) as count FROM (SELECT DISTINCT x.patient_id,x.good_id,x.count,x.record_time FROM xt_automatic_reduce_detail as x WHERE x.org_id = ? and `status` = 1) as b GROUP BY good_id", orgid).Scan(&autoMatic).Error
  3746. }
  3747. return autoMatic, err
  3748. }
  3749. func GetGoodWarehouseOutById(id int64, orgid int64) (models.WarehouseOut, error) {
  3750. out := models.WarehouseOut{}
  3751. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", id, orgid).Find(&out).Error
  3752. return out, err
  3753. }
  3754. func GetOrderDetialByOrderId(id int64, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  3755. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3756. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3757. fmt.Println(table)
  3758. if id > 0 {
  3759. db = db.Where("x.warehouse_out_id = ?", id)
  3760. }
  3761. if orgid > 0 {
  3762. db = db.Where("x.org_id = ?", orgid)
  3763. }
  3764. err = db.Select("x.id,x.warehouse_out_id,x.warehouse_info_id,x.good_id,sum(x.count) as count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.warehouse_out_order_number,x.type,x.dealer,x.patient_id,t.manufacturer,x.is_sys,x.sys_record_time,x.number,x.remark,x.license_number,x.supply_cancel_out_id,x.supply_warehouse_id,x.storehouse_id,x.admin_user_id,x.buy_price,x.stock_count,x.register_number,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.packing_unit,t.min_unit").Joins("left join xt_good_information as t on t.id=x.good_id").Group("x.good_id,x.warehouse_info_id").Order("x.ctime desc").Scan(&out).Error
  3765. return out, err
  3766. }
  3767. func GetWarehouseOutOrder(id []string, orgid int64) (out []*models.WarehouseOut, err error) {
  3768. db := XTReadDB().Table("xt_warehouse_out as x").Where("x.status = 1")
  3769. if len(id) > 0 {
  3770. db = db.Where("x.id in(?)", id)
  3771. }
  3772. if orgid > 0 {
  3773. db = db.Where("x.org_id= ?", orgid)
  3774. }
  3775. err = db.Select("x.id,x.warehouse_out_order_number,x.operation_time,x.creater,x.org_id,x.modifier,x.remark,x.warehouse_out_time,x.dealer,x.manufacturer,x.type").Find(&out).Error
  3776. return out, err
  3777. }
  3778. func GetOrderDetialByOrderIdOne(id []string, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  3779. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3780. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3781. tableOne := XTReadDB().Table("xt_warehouse_info as n").Where("n.status = 1")
  3782. fmt.Println(table, tableOne)
  3783. if len(id) > 0 {
  3784. db = db.Where("x.warehouse_out_id in (?)", id)
  3785. }
  3786. if orgid > 0 {
  3787. db = db.Where("x.org_id = ?", orgid)
  3788. }
  3789. err = db.Select("x.id,x.warehouse_out_id,x.good_id,sum(x.count) as count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.warehouse_out_order_number,x.type,x.dealer,x.manufacturer,t.social_security_directory_code,x.is_sys,x.sys_record_time,n.number,x.remark,x.license_number,x.storehouse_id,x.admin_user_id,x.buy_price,x.stock_count,x.warehouse_info_id,x.remark,x.patient_id,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.packing_unit,t.min_unit,t.packing_price,x.register_number").Joins("left join xt_warehouse_info as n on n.id=x.warehouse_info_id").Joins("left join xt_good_information as t on t.id=x.good_id").Group("x.warehouse_out_id,x.good_id").Order("x.ctime desc").Scan(&out).Error
  3790. return out, err
  3791. }
  3792. func GetOrderDetailStockFlowByStorehouseById(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  3793. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  3794. if len(id) > 0 {
  3795. db = db.Where("warehouse_out_id in (?)", id)
  3796. }
  3797. if orgid > 0 {
  3798. db = db.Where("user_org_id = ?", orgid)
  3799. }
  3800. err = db.Group("warehousing_id").Find(&list).Error
  3801. return list, err
  3802. }
  3803. func GetOrderDetailStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  3804. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  3805. if len(id) > 0 {
  3806. db = db.Where("warehouse_out_id in (?)", id)
  3807. }
  3808. if orgid > 0 {
  3809. db = db.Where("user_org_id = ?", orgid)
  3810. }
  3811. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  3812. return list, err
  3813. }
  3814. func GetOrderStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  3815. db := XTReadDB().Model(&list).Where("status = 1 and consumable_type = 7 and patient_id > 0 and count>0")
  3816. if len(id) > 0 {
  3817. db = db.Where("warehouse_out_id in (?)", id)
  3818. }
  3819. if orgid > 0 {
  3820. db = db.Where("user_org_id = ?", orgid)
  3821. }
  3822. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  3823. return list, err
  3824. }
  3825. func GetLastWarehouseOutInfo(ids []string, orgId int64) (info []*models.WarehouseOutInfoSix, err error) {
  3826. db := XTReadDB().Model(&info)
  3827. db = db.Preload("GoodInfo", "status = 1")
  3828. err = db.Raw("select * from(SELECT * FROM xt_warehouse_out_info WHERE warehouse_out_id IN(?) AND org_id = ? and status = 1 ORDER BY id DESC LIMIT 1000) a group by good_id", ids, orgId).Find(&info).Error
  3829. return info, err
  3830. }
  3831. func FindUserDetailByIdOne(good_id int64, record_time int64, org_id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  3832. db := readDb.Model(&models.AutomaticReduceDetail{})
  3833. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  3834. db = db.Preload("GoodsType", "status = 1")
  3835. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3836. db = db.Preload("WarehouseOutInfo", "org_id = ? and status = 1 and good_id = ? and sys_record_time = ?", org_id, good_id, record_time)
  3837. db = db.Where("status = 1 AND org_id = ? AND good_id = ? AND record_time = ? and count<> 0 ", org_id, good_id, record_time)
  3838. db = db.Count(&total)
  3839. err = db.Find(&user).Error
  3840. return
  3841. }
  3842. func GetGoodInformationByGoodId(good_id int64) (models.GoodInfo, error) {
  3843. info := models.GoodInfo{}
  3844. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  3845. return info, err
  3846. }
  3847. func GetGoodInformationByGoodIdThirty(good_id int64) (models.GoodInformationThirty, error) {
  3848. info := models.GoodInformationThirty{}
  3849. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  3850. return info, err
  3851. }
  3852. func GetNewGoodInformationByGoodIdThirty(good_id int64, tx *gorm.DB) (models.GoodInformationThirty, error) {
  3853. info := models.GoodInformationThirty{}
  3854. err := tx.Where("id = ? and status = 1", good_id).Find(&info).Error
  3855. if err != gorm.ErrRecordNotFound {
  3856. if err != nil {
  3857. tx.Rollback()
  3858. return info, err
  3859. }
  3860. }
  3861. return info, err
  3862. }
  3863. func UpdateGoodInfoList(info models.GoodInfo, id int64) error {
  3864. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"total": info.Total}).Error
  3865. return err
  3866. }
  3867. func GetExprotStockList(orgid int64, orderid []string, startime int64, endtime int64) (info []*models.StWarehousingInfo, err error) {
  3868. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3869. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3870. fmt.Println(table)
  3871. if orgid > 0 {
  3872. db = db.Where("x.org_id = ?", orgid)
  3873. }
  3874. if len(orderid) > 0 {
  3875. db = db.Where("x.warehousing_id in(?)", orderid)
  3876. }
  3877. if startime > 0 {
  3878. db = db.Where("x.ctime>=?", startime)
  3879. }
  3880. if endtime > 0 {
  3881. db = db.Where("x.ctime<=?", endtime)
  3882. }
  3883. err = db.Select("x.id,x.good_id,x.good_type_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.price,x.dealer,x.manufacturer,x.remark,x.license_number,t.good_name,t.packing_unit,t.specification_name").Joins("left join xt_good_information as t on t.id = x.good_id").Scan(&info).Error
  3884. return info, err
  3885. }
  3886. func GetExprotStockListTwenty(orgid int64, orderid []string, startime int64, endtime int64, storehouse_id int64) (info []*models.StWarehousingInfo, err error) {
  3887. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3888. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3889. fmt.Println(table)
  3890. if orgid > 0 {
  3891. db = db.Where("x.org_id = ?", orgid)
  3892. }
  3893. if len(orderid) > 0 {
  3894. db = db.Where("x.warehousing_id in(?)", orderid)
  3895. }
  3896. if startime > 0 {
  3897. db = db.Where("x.ctime>=?", startime)
  3898. }
  3899. if endtime > 0 {
  3900. db = db.Where("x.ctime<=?", endtime)
  3901. }
  3902. if storehouse_id > 0 {
  3903. db = db.Where("x.storehouse_id = ?", storehouse_id)
  3904. }
  3905. err = db.Select("x.id,x.good_id,x.good_type_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.price,x.dealer,x.manufacturer,x.remark,x.license_number,t.good_name,t.packing_unit,t.specification_name,p.warehousing_time").Joins("left join xt_warehouse as p on p.id =x.warehousing_id").Joins("left join xt_good_information as t on t.id = x.good_id").Scan(&info).Error
  3906. return info, err
  3907. }
  3908. func GetDrugWarehouOrderInfo(startime int64, endtime int64, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  3909. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  3910. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  3911. fmt.Println(table)
  3912. if startime > 0 {
  3913. db = db.Where("x.ctime>=?", startime)
  3914. }
  3915. if endtime > 0 {
  3916. db = db.Where("x.ctime<=?", endtime)
  3917. }
  3918. if orgid > 0 {
  3919. db = db.Where("x.org_id = ?", orgid)
  3920. }
  3921. err = db.Select("x.drug_id,x.count,x.count_unit,t.max_unit,t.min_unit,t.min_number").Joins("left join xt_base_drug as t on t.id = x.drug_id").Scan(&info).Error
  3922. return info, err
  3923. }
  3924. func GetOutExprotList(orgid int64, ids []string, startime int64, endtime int64) (out []*models.WarehouseOutInfoOne, err error) {
  3925. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1 ")
  3926. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3927. tableOne := XTReadDB().Table("xt_warehouse_out as p").Where("p.status=1")
  3928. fmt.Println(table, tableOne)
  3929. if orgid > 0 {
  3930. db = db.Where("x.org_id = ?", orgid)
  3931. }
  3932. if len(ids) > 0 {
  3933. db = db.Where("x.warehouse_out_id in (?)", ids)
  3934. }
  3935. if startime > 0 {
  3936. db = db.Where("x.ctime>=?", startime)
  3937. }
  3938. if endtime > 0 {
  3939. db = db.Where("x.ctime<=?", endtime)
  3940. }
  3941. err = db.Select("x.id,x.warehouse_out_id,x.good_id,x.good_type_id,x.warehousing_out_target,x.count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.remark,x.number,x.license_number,t.good_name,t.specification_name,t.min_number,t.packing_price,t.min_unit,t.packing_unit,x.manufacturer,x.dealer,p.warehouse_out_time").Joins("left join xt_warehouse_out as p on p.id =x.warehouse_out_id").Joins("left join xt_good_information as t on t.id = x.good_id").Scan(&out).Error
  3942. return out, err
  3943. }
  3944. func FindeDrugWarehouseOutOrder(ids []string, orgid int64) (info []*models.DrugWarehouseOut, err error) {
  3945. db := XTReadDB().Table("xt_drug_warehouse_out as x").Where("x.status = 1")
  3946. if len(ids) > 0 {
  3947. db = db.Where("x.id in(?)", ids)
  3948. }
  3949. if orgid > 0 {
  3950. db = db.Where("x.org_id = ?", orgid)
  3951. }
  3952. err = db.Select("x.id,x.warehouse_out_order_number,x.operation_time,x.creater,x.org_id,x.modifier,x.remark,x.warehouse_out_time,x.is_sys").Scan(&info).Error
  3953. return info, err
  3954. }
  3955. func FindeDrugWarehouseOutDetail(ids []string, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  3956. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status =1 ")
  3957. table := XTReadDB().Table("x.base_drug as t").Where("t.status = 1")
  3958. fmt.Println(table)
  3959. if len(ids) > 0 {
  3960. db = db.Where("x.warehouse_out_id in(?)", ids)
  3961. }
  3962. if orgid > 0 {
  3963. db = db.Where("x.org_id = ?", orgid)
  3964. }
  3965. err = db.Select("x.id,x.warehouse_out_id,x.drug_id,x.warehousing_out_target,x.count,x.count_unit,x.price,x.product_date,x.expiry_date,x.number,x.batch_number,t.drug_type,t.max_unit,t.min_unit,t.min_number,t.dose,t.dose_unit,t.last_price,t.drug_name").Joins("left join xt_base_drug as t on t.id =x.drug_id").Scan(&info).Error
  3966. return info, err
  3967. }
  3968. func GetDrugNameByKeyword(keyword string, orgid int64) (basedrug []*models.XtBaseDrug, err error) {
  3969. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1")
  3970. likeKey := "%" + keyword + "%"
  3971. if len(keyword) > 0 {
  3972. db = db.Where("x.drug_name like ?", likeKey)
  3973. }
  3974. if orgid > 0 {
  3975. db = db.Where("x.org_id = ?", orgid)
  3976. }
  3977. err = db.Find(&basedrug).Error
  3978. return basedrug, err
  3979. }
  3980. func GetDrugWarehouseOrderDetail(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  3981. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  3982. if len(ids) > 0 {
  3983. db = db.Where("x.drug_id in(?)", ids)
  3984. }
  3985. if orgid > 0 {
  3986. db = db.Where("x.org_id = ?", orgid)
  3987. }
  3988. if startime > 0 {
  3989. db = db.Where("x.ctime>=?", startime)
  3990. }
  3991. if endtime > 0 {
  3992. db = db.Where("x.ctime<=?", endtime)
  3993. }
  3994. if storehouse_id > 0 {
  3995. db = db.Where("x.storehouse_id = ?", storehouse_id)
  3996. }
  3997. err = db.Find(&info).Error
  3998. return info, err
  3999. }
  4000. func GetDrugWarehouseOrderDetailTwo(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  4001. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4002. if len(ids) > 0 {
  4003. db = db.Where("x.drug_id in(?)", ids)
  4004. }
  4005. if orgid > 0 {
  4006. db = db.Where("x.org_id = ?", orgid)
  4007. }
  4008. if startime > 0 {
  4009. db = db.Where("x.ctime>=?", startime)
  4010. }
  4011. if endtime > 0 {
  4012. db = db.Where("x.ctime<=?", endtime)
  4013. }
  4014. if storehouse_id > 0 {
  4015. db = db.Where("storehouse_id = ?", storehouse_id)
  4016. }
  4017. err = db.Find(&info).Error
  4018. return info, err
  4019. }
  4020. func GetDrugTotalCount(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4021. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  4022. if drugid > 0 {
  4023. db = db.Where("x.drug_id = ?", drugid)
  4024. }
  4025. if orgid > 0 {
  4026. db = db.Where("x.org_id = ?", orgid)
  4027. }
  4028. if storehouse_id > 0 {
  4029. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4030. }
  4031. err = db.Select("x.drug_id,sum(x.stock_max_number) as count,sum(x.stock_min_number) as stock_min_number,x.max_unit,x.min_unit").Find(&info).Error
  4032. return info, err
  4033. }
  4034. func GetDrugTotalCountTwenty(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4035. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1 ")
  4036. if drugid > 0 {
  4037. db = db.Where("x.drug_id = ?", drugid)
  4038. }
  4039. if orgid > 0 {
  4040. db = db.Where("x.org_id = ?", orgid)
  4041. }
  4042. if storehouse_id > 0 {
  4043. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4044. }
  4045. err = db.Select("x.drug_id,sum(x.stock_max_number) as count,sum(x.stock_min_number) as stock_min_number,x.max_unit,x.min_unit").Find(&info).Error
  4046. return info, err
  4047. }
  4048. func GetDrugTotalCountSwx(id int64, orgid int64) (info models.VmDrugWarehouseInfo, err error) {
  4049. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4050. if id > 0 {
  4051. db = db.Where("x.id = ?", id)
  4052. }
  4053. if orgid > 0 {
  4054. db = db.Where("x.org_id = ?", orgid)
  4055. }
  4056. err = db.Select("x.drug_id,sum(x.stock_max_number) as count,sum(x.stock_min_number) as stock_min_number,x.max_unit,x.min_unit").Find(&info).Error
  4057. return info, err
  4058. }
  4059. func GetDrugTotalCountOne(drugid int64, orgid int64, id int64) (models.VmDrugWarehouseInfo, error) {
  4060. info := models.VmDrugWarehouseInfo{}
  4061. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4062. if id == 0 {
  4063. db = db.Where("id = ?", id)
  4064. }
  4065. if drugid > 0 {
  4066. db = db.Where("x.drug_id = ?", drugid)
  4067. }
  4068. if orgid > 0 {
  4069. db = db.Where("x.org_id = ?", orgid)
  4070. }
  4071. err = db.Select("x.drug_id,x.stock_max_number,x.stock_min_number,x.max_unit,x.min_unit").Find(&info).Error
  4072. return info, err
  4073. }
  4074. func GetDrugTotalCountTwo(drugid int64, storehouse_id int64) (list []*models.VmDrugWarehouseInfo, err error) {
  4075. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4076. table := XTReadDB().Table("xt_base_drug as d").Where("d.status = 1")
  4077. fmt.Println(table)
  4078. if drugid > 0 {
  4079. db = db.Where("x.drug_id = ?", drugid)
  4080. }
  4081. if storehouse_id > 0 {
  4082. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4083. }
  4084. err = db.Select("x.drug_id,x.stock_max_number,x.stock_min_number,d.min_number").Joins("left join xt_base_drug as d on d.id = x.drug_id").Scan(&list).Error
  4085. return list, err
  4086. }
  4087. func GetDrugByGoodId(drugid int64) (models.BaseDrugLib, error) {
  4088. lib := models.BaseDrugLib{}
  4089. err := XTReadDB().Model(&lib).Where("id = ? and status = 1", drugid).Find(&lib).Error
  4090. return lib, err
  4091. }
  4092. func GetWarehoureOrderInfoList(goodid int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4093. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4094. if goodid > 0 {
  4095. db = db.Where("x.good_id = ?", goodid)
  4096. }
  4097. if orgid > 0 {
  4098. db = db.Where("x.org_id = ?", orgid)
  4099. }
  4100. err = db.Select("x.id,x.warehousing_id,x.good_id,x.good_type_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.price,x.total_price,x.dealer,x.manufacturer,x.remark,x.is_return,x.warehousing_order,x.warehousing_unit,x.stock_count,x.license_number,x.org_id").Find(&info).Error
  4101. return info, err
  4102. }
  4103. func UpdateWarehousingCount(info *models.WarehousingInfo, goodid int64, orgid int64, warehousingId int64) error {
  4104. err := XTWriteDB().Model(models.WarehousingInfo{}).Where("good_id = ? and org_id = ? and status = 1 and warehousing_id = ?", goodid, orgid, warehousingId).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  4105. return err
  4106. }
  4107. func GetSingleCancelOrder(id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  4108. err = XTReadDB().Where("cancel_stock_id = ? and status = 1 and org_id = ?", id, orgid).Preload("GoodInfo", "status = 1").Find(&info).Error
  4109. return info, err
  4110. }
  4111. func UpdateCancelOut(list []*models.WarehousingInfo, count int64) error {
  4112. //遍历循环入库数据
  4113. for i, item := range list {
  4114. //计算每个批次剩余库存与入库数据的差 然后和退库数量比较
  4115. if (item.WarehousingCount - item.StockCount) >= count {
  4116. //如果差距大于退库数量,则直接把退库数量增加到该批次中
  4117. info := models.WarehousingInfo{
  4118. GoodId: item.GoodId,
  4119. OrgId: item.OrgId,
  4120. WarehousingId: item.WarehousingId,
  4121. StockCount: item.StockCount + count,
  4122. }
  4123. err2 := UpdateWarehousingCount(&info, item.GoodId, item.OrgId, item.WarehousingId)
  4124. fmt.Println("2323233232323232323232323232323", err2)
  4125. return err
  4126. }
  4127. if (item.WarehousingCount - item.StockCount) < count {
  4128. //如果该批次小于退库数量,则先出库该相差数量
  4129. var countOne = item.WarehousingCount - item.StockCount
  4130. infoOne := models.WarehousingInfo{
  4131. GoodId: item.GoodId,
  4132. OrgId: item.OrgId,
  4133. WarehousingId: item.WarehousingId,
  4134. StockCount: countOne + item.StockCount,
  4135. }
  4136. err := UpdateWarehousingCount(&infoOne, item.GoodId, item.OrgId, item.WarehousingId)
  4137. if err == nil {
  4138. //剩余的出库到第二个批次
  4139. var countTwo = count - countOne
  4140. infos := append(list, list[i+1])
  4141. var idArray []*models.WarehousingInfo
  4142. for _, it := range infos {
  4143. if it.WarehousingId != item.WarehousingId {
  4144. idArray = append(idArray, it)
  4145. }
  4146. }
  4147. //查询更新后的库存
  4148. UpdateCancelOut(idArray, countTwo)
  4149. return err
  4150. } else {
  4151. return err
  4152. }
  4153. }
  4154. }
  4155. return err
  4156. }
  4157. func GetCancelStockOrderPrint(idstr []string) (info []*models.CancelStockInfo, err error) {
  4158. if len(idstr) > 0 {
  4159. err = XTReadDB().Model(&info).Where("cancel_stock_id in(?)", idstr).Preload("GoodInfo", "status = 1").Find(&info).Error
  4160. } else {
  4161. err = XTReadDB().Model(&info).Preload("GoodInfo", "status = 1").Find(&info).Error
  4162. }
  4163. return info, err
  4164. }
  4165. func GetCancelStockOrderPrintOne(idstr []string, orgid int64) (info []*models.CancelStock, err error) {
  4166. if len(idstr) > 0 {
  4167. err = XTReadDB().Model(&info).Where("id in(?)", idstr).
  4168. Preload("XtCancelStockInfo", func(db *gorm.DB) *gorm.DB {
  4169. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodInfo", "status = 1 and org_id = ?", orgid)
  4170. }).Find(&info).Error
  4171. }
  4172. return info, err
  4173. }
  4174. func GetCancelOutTotalCount(startime int64, endtime int64, orgid int64) (info []*models.VmCancelStockInfo, err error) {
  4175. db := XTReadDB().Table("xt_cancel_stock_info as x").Where("x.status = 1")
  4176. if startime > 0 {
  4177. db = db.Where("x.ctime >=?", startime)
  4178. }
  4179. if endtime > 0 {
  4180. db = db.Where("x.ctime <= ?", endtime)
  4181. }
  4182. if orgid > 0 {
  4183. db = db.Where("x.org_id = ?", orgid)
  4184. }
  4185. err = db.Select("x.good_id,sum(x.count) as count").Group("x.good_id").Scan(&info).Error
  4186. return info, err
  4187. }
  4188. func GetWarehoureOutOrderInfo(goodid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  4189. info := models.WarehouseOutInfo{}
  4190. var err error
  4191. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1", goodid, orgid).Find(&info).Error
  4192. if err == gorm.ErrRecordNotFound {
  4193. return nil, err
  4194. }
  4195. if err != nil {
  4196. return nil, err
  4197. }
  4198. return &info, nil
  4199. }
  4200. func GetWarehoureOrderInfoCount(goodid int64, orgid int64, id int64) (info models.WarehouseOutInfo, err error) {
  4201. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  4202. if goodid > 0 {
  4203. db = db.Where("x.good_id = ?", goodid)
  4204. }
  4205. if orgid > 0 {
  4206. db = db.Where("x.org_id = ?", orgid)
  4207. }
  4208. if id > 0 {
  4209. db = db.Where("x.warehouse_info_id = ?", id)
  4210. }
  4211. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4212. return info, err
  4213. }
  4214. func GetWarehoureOrderStockOutFlow(goodid int64, orgid int64, id int64, storehouse_id int64) (info models.VmStockFlow, err error) {
  4215. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status =1 and x.consumable_type = 2 or x.consumable_type = 3")
  4216. if goodid > 0 {
  4217. db = db.Where("x.good_id = ?", goodid)
  4218. }
  4219. if orgid > 0 {
  4220. db = db.Where("x.user_org_id = ?", orgid)
  4221. }
  4222. if id > 0 {
  4223. db = db.Where("x.warehousing_id = ?", id)
  4224. }
  4225. if storehouse_id > 0 {
  4226. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4227. }
  4228. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4229. return info, err
  4230. }
  4231. func UpdateCancelInfo(id int64) (models.CancelStock, error) {
  4232. stock := models.CancelStock{}
  4233. err := XTWriteDB().Model(&stock).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4234. return stock, err
  4235. }
  4236. func GetStockBatchNumber(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4237. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1 and number <> ''", id, orgid).Find(&info).Error
  4238. return info, err
  4239. }
  4240. func GetWarehouseOutInfoById(id int64, goodid int64) (info []*models.WarehouseOutInfo, err error) {
  4241. err = XTReadDB().Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  4242. return info, err
  4243. }
  4244. func GetStockFlowBatchNumber(id int64, goodid int64) (stock []*models.VmStockFlow, err error) {
  4245. err = XTReadDB().Model(&stock).Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Group("patient_id,number").Find(&stock).Error
  4246. return stock, err
  4247. }
  4248. func GetStockFlowBatchNumberOne(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4249. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status = 1 and x.count > 0 and (x.consumable_type = 3 or x.consumable_type = 2 or x.consumable_type = 15)")
  4250. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4251. fmt.Println(table)
  4252. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4253. fmt.Println(tableone)
  4254. if id > 0 {
  4255. db = db.Where("x.warehouse_out_id = ?", id)
  4256. }
  4257. if goodid > 0 {
  4258. db = db.Where("x.good_id = ?", goodid)
  4259. }
  4260. err = db.Select("x.id,x.warehousing_id,x.good_id,x.number,x.count,x.patient_id,x.system_time,x.is_sys,x.warehousing_order,x.warehouse_out_id,x.warehouse_out_order_number,x.price,x.product_date,x.expire_date,x.ctime,o.packing_unit,t.name").Joins("left join xt_patients as t on t.id = x.patient_id").Joins("left join xt_good_information as o on o.id = x.good_id").Scan(&stock).Error
  4261. return stock, err
  4262. }
  4263. func GetStockFlowCancelInfo(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4264. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status = 1 and x.count > 0 and x.consumable_type = 7")
  4265. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4266. fmt.Println(table)
  4267. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4268. fmt.Println(tableone)
  4269. if id > 0 {
  4270. db = db.Where("x.warehouse_out_id = ?", id)
  4271. }
  4272. if goodid > 0 {
  4273. db = db.Where("x.good_id = ?", goodid)
  4274. }
  4275. err = db.Select("x.id,x.warehousing_id,x.good_id,x.number,x.count,x.patient_id,x.system_time,x.is_sys,x.warehousing_order,x.warehouse_out_id,x.warehouse_out_order_number,x.price,x.product_date,x.expire_date,x.ctime,o.packing_unit,t.name").Joins("left join xt_patients as t on t.id = x.patient_id").Joins("left join xt_good_information as o on o.id = x.good_id").Scan(&stock).Error
  4276. return stock, err
  4277. }
  4278. func GetDrugBatchNumber(drug_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  4279. err = XTReadDB().Model(&info).Where("drug_id = ? and org_id = ? and status = 1 and batch_number <>''", drug_id, orgid).Find(&info).Error
  4280. return info, err
  4281. }
  4282. func ExportDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseInfo, err error) {
  4283. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status =1")
  4284. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  4285. tableOne := XTReadDB().Table("xt_drug_warehouse as p")
  4286. fmt.Println("table", table)
  4287. fmt.Println("tableOne", tableOne)
  4288. if len(warehouse_out_id) > 0 {
  4289. db = db.Where("x.warehousing_id in(?)", warehouse_out_id)
  4290. }
  4291. err = db.Select("x.id,x.warehousing_id,x.drug_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.warehouseing_unit,x.max_unit,x.min_unit,x.stock_max_number,x.stock_min_number,x.price,x.dealer,x.manufacturer,x.warehousing_order,x.batch_number,b.dose,b.dose_unit,b.min_number,b.min_unit,b.max_unit,b.drug_name,b.drug_type,p.warehousing_time").Joins("left join xt_drug_warehouse as p on p.id =x.warehousing_id").Joins("left join xt_base_drug as b on b.id = x.drug_id").Scan(&out).Error
  4292. return out, err
  4293. }
  4294. func GetExportOutOrderDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4295. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4296. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  4297. fmt.Println("table", table)
  4298. if len(warehouse_out_id) > 0 {
  4299. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4300. }
  4301. err = db.Select("x.id,x.warehouse_out_id,x.drug_id,x.count,x.count_unit,x.price,x.product_date,x.expiry_date,x.warehouse_out_order_number,x.dealer,x.manufacturer,x.is_sys,x.sys_record_time,x.warehouse_info_id,x.number,x.batch_number,x.patient_id,b.dose,b.dose_unit,b.min_number,b.min_unit,b.max_unit,b.drug_name,b.drug_type,b.min_price").Joins("left join xt_base_drug as b on b.id = x.drug_id").Scan(&out).Error
  4302. return out, err
  4303. }
  4304. func GetExportOutOrderDrugListOne(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4305. db := XTReadDB().Table("xt_drug_flow as x").Where("x.status = 1")
  4306. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  4307. fmt.Println("table", table)
  4308. if len(warehouse_out_id) > 0 {
  4309. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4310. }
  4311. err = db.Select("x.id,x.warehouse_out_id,x.drug_id,x.count,x.max_unit as count_unit,x.price,x.retail_price,x.product_date,x.expire_date,x.warehouse_out_order_number,x.dealer,x.manufacturer,x.is_sys,x.number,x.batch_number,x.patient_id,x.ctime,x.consumable_type,b.dose,b.dose_unit,b.min_number,b.min_unit,b.max_unit,b.drug_name,b.drug_type,b.min_price").Joins("left join xt_base_drug as b on b.id = x.drug_id").Scan(&out).Error
  4312. return out, err
  4313. }
  4314. func GetDrugOutListFlow(warehouse_out_id []string, orgid int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  4315. db := XTReadDB().Model(&info).Where("status = 1")
  4316. if len(warehouse_out_id) > 0 {
  4317. db = db.Where("warehouse_out_id in(?)", warehouse_out_id)
  4318. }
  4319. if orgid > 0 {
  4320. db = db.Where("org_id = ?", orgid)
  4321. }
  4322. err = db.Find(&info).Error
  4323. return info, err
  4324. }
  4325. func GetDrugWarehouseOut(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4326. err = XTReadDB().Where("warehouse_out_id in(?)", warehouse_out_id).Find(&out).Error
  4327. return out, err
  4328. }
  4329. func GetDrugWarehouseOutTenty(warehouse_out_id []string, orgid int64) (out []*models.DrugWarehouseOutTwenty, err error) {
  4330. err = XTReadDB().Where("id in(?)", warehouse_out_id).Preload("DrugWarehouseOutInfoTenty", func(db *gorm.DB) *gorm.DB {
  4331. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("BaseDrugLib", "status = 1 and org_id = ?", orgid).Preload("DrugWarehouseInfo", "status = 1 and org_id = ?", orgid)
  4332. }).Find(&out).Error
  4333. return out, err
  4334. }
  4335. func GetLastGoodInformationByGoodId(goodid int64, warehouse_info_id int64, orgid_id int64) (models.WarehouseOutInfo, error) {
  4336. info := models.WarehouseOutInfo{}
  4337. err := XTReadDB().Where("good_id = ? and warehouse_info_id= ? and status =1", goodid, warehouse_info_id).Last(&info).Error
  4338. return info, err
  4339. }
  4340. func GetTotalCountByGoodId(goodid int64, storehouse_id int64) (models.WarehousingInfo, error) {
  4341. info := models.WarehousingInfo{}
  4342. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4343. if goodid > 0 {
  4344. db = db.Where("x.good_id = ?", goodid)
  4345. }
  4346. if storehouse_id > 0 {
  4347. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4348. }
  4349. err := db.Select("x.good_id,sum(x.stock_count) as stock_count").Scan(&info).Error
  4350. return info, err
  4351. }
  4352. func GetLastGoodCountById(goodid int64, warehouse_info_id int64) (models.WarehousingInfo, error) {
  4353. info := models.WarehousingInfo{}
  4354. err := XTReadDB().Where("good_id = ? and id= ? and status = 1", goodid, warehouse_info_id).First(&info).Error
  4355. return info, err
  4356. }
  4357. func UpdateWarehousingInfo(info models.WarehousingInfo, id int64) error {
  4358. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  4359. return err
  4360. }
  4361. func FindFirstWarehousingInfoByGoodId(good_id int64, id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  4362. if storehouse_id == 0 {
  4363. err = readDb.Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime asc").First(&info).Error
  4364. }
  4365. if storehouse_id > 0 {
  4366. err = readDb.Where("good_id = ? AND status = 1 and stock_count > 0 and storehouse_id=?", good_id, storehouse_id).Order("ctime asc").First(&info).Error
  4367. }
  4368. return info, err
  4369. }
  4370. func CreateStockFlow(warehousingInfo []*models.VmStockFlow) (err error) {
  4371. if len(warehousingInfo) > 0 {
  4372. utx := writeDb.Begin()
  4373. if len(warehousingInfo) > 0 {
  4374. thisSQL := "INSERT INTO xt_stock_flow (warehousing_id, good_id, number, license_number, count,user_org_id,patient_id,system_time,consumable_type,is_sys,warehousing_order,warehouse_out_id,warehouse_out_order_number,is_edit,cancel_stock_id,cancel_order_number,manufacturer,dealer,creator,update_creator,status,ctime,mtime,price,warehousing_detail_id,warehouse_out_detail_id,cancel_out_detail_id,expire_date,product_date) VALUES "
  4375. insertParams := make([]string, 0)
  4376. insertData := make([]interface{}, 0)
  4377. for _, info := range warehousingInfo {
  4378. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  4379. insertData = append(insertData, info.WarehousingId)
  4380. insertData = append(insertData, info.GoodId)
  4381. insertData = append(insertData, info.Number)
  4382. insertData = append(insertData, info.LicenseNumber)
  4383. insertData = append(insertData, info.Count)
  4384. insertData = append(insertData, info.UserOrgId)
  4385. insertData = append(insertData, info.PatientId)
  4386. insertData = append(insertData, info.SystemTime)
  4387. insertData = append(insertData, info.ConsumableType)
  4388. insertData = append(insertData, info.IsSys)
  4389. insertData = append(insertData, info.WarehousingOrder)
  4390. insertData = append(insertData, info.WarehouseOutId)
  4391. insertData = append(insertData, info.WarehouseOutOrderNumber)
  4392. insertData = append(insertData, info.IsEdit)
  4393. insertData = append(insertData, info.CancelStockId)
  4394. insertData = append(insertData, info.CancelOrderNumber)
  4395. insertData = append(insertData, info.Manufacturer)
  4396. insertData = append(insertData, info.Dealer)
  4397. insertData = append(insertData, info.Creator)
  4398. insertData = append(insertData, info.UpdateCreator)
  4399. insertData = append(insertData, info.Status)
  4400. insertData = append(insertData, info.Ctime)
  4401. insertData = append(insertData, info.Mtime)
  4402. insertData = append(insertData, info.Price)
  4403. insertData = append(insertData, info.WarehousingDetailId)
  4404. insertData = append(insertData, info.WarehouseOutDetailId)
  4405. insertData = append(insertData, info.CancelOutDetailId)
  4406. insertData = append(insertData, info.ExpireDate)
  4407. insertData = append(insertData, info.ProductDate)
  4408. }
  4409. thisSQL += strings.Join(insertParams, ", ")
  4410. err = utx.Exec(thisSQL, insertData...).Error
  4411. if err != nil {
  4412. utx.Rollback()
  4413. return
  4414. }
  4415. }
  4416. utx.Commit()
  4417. }
  4418. return
  4419. }
  4420. func CreateStockFlowOne(flow models.VmStockFlow) error {
  4421. utx := XTWriteDB().Begin()
  4422. err := utx.Create(&flow).Error
  4423. if err != nil {
  4424. utx.Rollback()
  4425. return err
  4426. }
  4427. utx.Commit()
  4428. return err
  4429. }
  4430. func CreateNewStockFlowOne(flow models.VmStockFlow, tx *gorm.DB) error {
  4431. err := tx.Create(&flow).Error
  4432. if err != nil {
  4433. tx.Rollback()
  4434. return err
  4435. }
  4436. return err
  4437. }
  4438. func CreateStockFlowSix(flow *models.VmStockFlow) error {
  4439. err := XTWriteDB().Create(&flow).Error
  4440. return err
  4441. }
  4442. func UpdatedStockFlow(flow models.VmStockFlow) error {
  4443. err := XTWriteDB().Save(&flow).Error
  4444. return err
  4445. }
  4446. func UpdatedStockFlowOne(flow models.VmStockFlow, warehousing_id int64, patient_id int64, record_time int64, good_id int64) error {
  4447. err = XTWriteDB().Model(&flow).Where("warehousing_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1", warehousing_id, patient_id, record_time, good_id).Updates(map[string]interface{}{"number": flow.Number, "license_number": flow.LicenseNumber, "count": flow.Count, "expire_date": flow.ExpireDate, "product_date": flow.ProductDate, "price": flow.Price, "manufacturer": flow.Manufacturer, "dealer": flow.Dealer, "over_count": flow.OverCount}).Error
  4448. return err
  4449. }
  4450. func UpdatedStockFlowFive(flow models.VmStockFlow, warehouse_out_id int64, patient_id int64, record_time int64, good_id int64) error {
  4451. err = XTWriteDB().Model(&flow).Where("warehouse_out_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1", warehouse_out_id, patient_id, record_time, good_id).Updates(map[string]interface{}{"number": flow.Number, "license_number": flow.LicenseNumber, "count": flow.Count, "expire_date": flow.ExpireDate, "product_date": flow.ProductDate, "price": flow.Price, "manufacturer": flow.Manufacturer, "dealer": flow.Dealer, "over_count": flow.OverCount}).Error
  4452. return err
  4453. }
  4454. func GetStockFlowIsExsit(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (*models.VmStockFlow, error) {
  4455. stock := models.VmStockFlow{}
  4456. var err error
  4457. err = XTReadDB().Model(&stock).Where("warehousing_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1 and is_read = 0", warehousing_id, patient_id, record_time, good_id).Find(&stock).Error
  4458. if err == gorm.ErrRecordNotFound {
  4459. return nil, err
  4460. }
  4461. if err != nil {
  4462. return nil, err
  4463. }
  4464. return &stock, nil
  4465. }
  4466. func GetStockFlowIsBatchNumber(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  4467. err = XTReadDB().Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type = 3", patient_id, record_time, good_id).Find(&flow).Error
  4468. return flow, nil
  4469. }
  4470. func GetStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  4471. err = XTReadDB().Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type = 3 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4472. return flow, nil
  4473. }
  4474. func GetNewStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  4475. err = tx.Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type = 3 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4476. if err != gorm.ErrRecordNotFound {
  4477. if err != nil {
  4478. tx.Rollback()
  4479. return flow, nil
  4480. }
  4481. }
  4482. return flow, nil
  4483. }
  4484. func GetStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.WarehouseOutInfo, err error) {
  4485. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4486. return flow, nil
  4487. }
  4488. func GetNewStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.WarehouseOutInfo, err error) {
  4489. err = tx.Where("patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4490. if err != gorm.ErrRecordNotFound {
  4491. if err != nil {
  4492. tx.Rollback()
  4493. return flow, nil
  4494. }
  4495. }
  4496. return flow, nil
  4497. }
  4498. func GetStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.AutomaticReduceDetail, err error) {
  4499. err = XTReadDB().Where("patient_id = ? and record_time = ? and good_id = ? and status = 1 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4500. return flow, nil
  4501. }
  4502. func GetNewStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.AutomaticReduceDetail, err error) {
  4503. err = tx.Where("patient_id = ? and record_time = ? and good_id = ? and status = 1 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4504. if err != gorm.ErrRecordNotFound {
  4505. if err != nil {
  4506. tx.Rollback()
  4507. return flow, err
  4508. }
  4509. }
  4510. return flow, err
  4511. }
  4512. func GetStockFlowIsBatchNumberTwo(patient_id int64, record_time int64, good_id int64) (flow []*models.WarehouseOutInfo, err error) {
  4513. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 ", patient_id, record_time, good_id).Find(&flow).Error
  4514. return flow, nil
  4515. }
  4516. func GetStockFlowIsBatchNumberThree(patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  4517. err = XTReadDB().Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type =7", patient_id, record_time, good_id).Find(&flow).Error
  4518. return flow, nil
  4519. }
  4520. func GetStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  4521. err = XTReadDB().Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type =7 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4522. return flow, nil
  4523. }
  4524. func GetNewStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  4525. err = tx.Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type =7 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4526. if err != gorm.ErrRecordNotFound {
  4527. if err != nil {
  4528. tx.Rollback()
  4529. return flow, nil
  4530. }
  4531. }
  4532. return flow, nil
  4533. }
  4534. func GetStockFlowIsBatchNumberFourty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  4535. err = XTReadDB().Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type =7 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4536. return flow, nil
  4537. }
  4538. func GetWarehouseOutInfoIsExist(id int64) (*models.WarehouseOutInfo, error) {
  4539. info := models.WarehouseOutInfo{}
  4540. var err error
  4541. err = XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  4542. if err == gorm.ErrRecordNotFound {
  4543. return nil, err
  4544. }
  4545. if err != nil {
  4546. return nil, err
  4547. }
  4548. return &info, nil
  4549. }
  4550. func GetWarehouseOutInfoIsExistOne(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  4551. info := models.WarehouseOutInfo{}
  4552. var err error
  4553. err = XTReadDB().Model(&info).Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and is_sys=1", good_id, record_time, patient_id).Find(&info).Error
  4554. if err == gorm.ErrRecordNotFound {
  4555. return nil, err
  4556. }
  4557. if err != nil {
  4558. return nil, err
  4559. }
  4560. return &info, nil
  4561. }
  4562. func GetWarehouseOutInfoIsExistTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  4563. info := models.WarehouseOutInfo{}
  4564. err := XTReadDB().Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and is_sys=1", good_id, record_time, patient_id).Find(&info).Error
  4565. return info, err
  4566. }
  4567. func GetStockFlowList(limit int64, page int64, consumable_type int64, orgId int64, startTime int64, endTime int64, good_id int64) (flow []*models.VmStockFlow, total int64, err error) {
  4568. offset := (page - 1) * limit
  4569. db := XTReadDB().Model(&flow).Where("status = 1 and count > 0")
  4570. if consumable_type > 0 {
  4571. //入库
  4572. if consumable_type == 1 {
  4573. db = db.Where("consumable_type = ?", consumable_type)
  4574. }
  4575. //2 手动出库
  4576. if consumable_type == 2 {
  4577. db = db.Where(" consumable_type = ? and is_sys = 0", consumable_type)
  4578. }
  4579. //3.自动出库
  4580. if consumable_type == 3 {
  4581. db = db.Where(" consumable_type = ? and is_sys = 1", consumable_type)
  4582. }
  4583. //4.手动退库
  4584. if consumable_type == 4 {
  4585. db = db.Where(" consumable_type = ?", consumable_type)
  4586. }
  4587. //自动退库
  4588. if consumable_type == 7 {
  4589. db = db.Where(" consumable_type = ?", consumable_type)
  4590. }
  4591. if consumable_type == 10 {
  4592. db = db.Where(" consumable_type = ?", consumable_type)
  4593. }
  4594. if consumable_type == 11 {
  4595. db = db.Where(" consumable_type = ?", consumable_type)
  4596. }
  4597. if consumable_type == 12 {
  4598. db = db.Where(" consumable_type = ?", consumable_type)
  4599. }
  4600. if consumable_type == 13 {
  4601. db = db.Where(" consumable_type = ?", consumable_type)
  4602. }
  4603. if consumable_type == 15 {
  4604. db = db.Where(" consumable_type = ?", consumable_type)
  4605. }
  4606. }
  4607. if orgId > 0 {
  4608. db = db.Where("user_org_id = ?", orgId)
  4609. }
  4610. if startTime > 0 {
  4611. db = db.Where("ctime >= ?", startTime)
  4612. }
  4613. if endTime > 0 {
  4614. db = db.Where("ctime<=?", endTime)
  4615. }
  4616. if good_id > 0 {
  4617. db = db.Where("good_id = ?", good_id)
  4618. }
  4619. err = db.Count(&total).Offset(offset).Limit(limit).Order("id desc").Find(&flow).Error
  4620. return flow, total, err
  4621. }
  4622. func GetLastGoodListByPatientId(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.AutomaticReduceDetail, error) {
  4623. detail := models.AutomaticReduceDetail{}
  4624. err := XTReadDB().Model(&detail).Where("record_time = ? and patient_id = ? and good_id = ? and good_type_id = ? and status = 1", recordtime, patientid, goodid, goodtypeid).Last(&detail).Error
  4625. return detail, err
  4626. }
  4627. func GetLastGoodListByPatientIdOne(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.WarehouseOutInfo, error) {
  4628. detail := models.WarehouseOutInfo{}
  4629. err := XTReadDB().Model(&detail).Where("sys_record_time = ? and patient_id = ? and good_id = ? and good_type_id = ? and status = 1", recordtime, patientid, goodid, goodtypeid).Last(&detail).Error
  4630. return detail, err
  4631. }
  4632. func UpdatedWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64) error {
  4633. outInfo := models.WarehouseOutInfo{}
  4634. err := XTWriteDB().Model(&outInfo).Where("good_id = ? and patient_id = ? and sys_record_time =? and status = 1", good_id, patient_id, record_time).Updates(map[string]interface{}{"warehouse_out_id": info.WarehouseOutId, "WarehouseOutOrderNumber": info.WarehouseOutOrderNumber, "sys_record_time": info.SysRecordTime, "good_type_id": info.GoodTypeId, "patient_id": info.PatientId, "consumable_type": info.ConsumableType, "count": info.Count, "price": info.Price, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "number": info.Number, "license_number": info.LicenseNumber, "over_count": info.OverCount}).Error
  4635. return err
  4636. }
  4637. func UpdatedNewWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) error {
  4638. outInfo := models.WarehouseOutInfo{}
  4639. err := tx.Model(&outInfo).Where("good_id = ? and patient_id = ? and sys_record_time =? and status = 1", good_id, patient_id, record_time).Updates(map[string]interface{}{"warehouse_out_id": info.WarehouseOutId, "WarehouseOutOrderNumber": info.WarehouseOutOrderNumber, "sys_record_time": info.SysRecordTime, "good_type_id": info.GoodTypeId, "patient_id": info.PatientId, "consumable_type": info.ConsumableType, "count": info.Count, "price": info.Price, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "number": info.Number, "license_number": info.LicenseNumber, "over_count": info.OverCount}).Error
  4640. if err != nil {
  4641. tx.Rollback()
  4642. return err
  4643. }
  4644. return err
  4645. }
  4646. func UpdatedWarehouseOutInfoSix(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) error {
  4647. outInfo := models.WarehouseOutInfo{}
  4648. err := XTWriteDB().Model(&outInfo).Where("good_id = ? and patient_id = ? and sys_record_time =? and status = 1 and project_id = ? and warehouse_info_id =? ", good_id, patient_id, record_time, project_id, warehouse_info_id).Updates(map[string]interface{}{"warehouse_out_id": info.WarehouseOutId, "WarehouseOutOrderNumber": info.WarehouseOutOrderNumber, "sys_record_time": info.SysRecordTime, "good_type_id": info.GoodTypeId, "patient_id": info.PatientId, "consumable_type": info.ConsumableType, "count": info.Count, "price": info.Price, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "number": info.Number, "license_number": info.LicenseNumber, "over_count": info.OverCount}).Error
  4649. return err
  4650. }
  4651. func UpdatedNewWarehouseOutInfoSix(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) error {
  4652. outInfo := models.WarehouseOutInfo{}
  4653. err := tx.Model(&outInfo).Where("good_id = ? and patient_id = ? and sys_record_time =? and status = 1 and project_id = ? and warehouse_info_id =? ", good_id, patient_id, record_time, project_id, warehouse_info_id).Updates(map[string]interface{}{"warehouse_out_id": info.WarehouseOutId, "WarehouseOutOrderNumber": info.WarehouseOutOrderNumber, "sys_record_time": info.SysRecordTime, "good_type_id": info.GoodTypeId, "patient_id": info.PatientId, "consumable_type": info.ConsumableType, "count": info.Count, "price": info.Price, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "number": info.Number, "license_number": info.LicenseNumber, "over_count": info.OverCount}).Error
  4654. if err != nil {
  4655. tx.Rollback()
  4656. return err
  4657. }
  4658. return err
  4659. }
  4660. func GetLastWarehouOutInfoByPatientId(orgid int64, patient_id int64, record_time int64, goodid int64, good_type_id int64) (models.WarehouseOutInfo, error) {
  4661. info := models.WarehouseOutInfo{}
  4662. err = XTReadDB().Model(&info).Where("org_id = ? and patient_id = ? and sys_record_time = ? and good_id = ? and good_type_id = ? and status = 1", orgid, patient_id, record_time, goodid, good_type_id).Last(&info).Error
  4663. return info, err
  4664. }
  4665. func GetLastStockOut(id int64) (models.WarehousingInfo, error) {
  4666. info := models.WarehousingInfo{}
  4667. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  4668. return info, err
  4669. }
  4670. func GetStockGoodCount(good_id int64) (models.WarehousingInfo, error) {
  4671. info := models.WarehousingInfo{}
  4672. db := XTReadDB().Model(&info)
  4673. if good_id > 0 {
  4674. db = db.Where("good_id = ? and status = 1", good_id)
  4675. }
  4676. err := db.Select("sum(stock_count) as stock_count").Find(&info).Error
  4677. return info, err
  4678. }
  4679. func GetDrugFlow(drugid int64, orgid int64, limit int64, page int64, startTime int64, endTime int64, stock_type int64) (drug_flow []*models.DrugFlow, total int64, err error) {
  4680. offset := (page - 1) * limit
  4681. db := XTReadDB().Model(drug_flow).Where("status = 1")
  4682. if drugid > 0 {
  4683. db = db.Where("drug_id = ?", drugid)
  4684. }
  4685. if orgid > 0 {
  4686. db = db.Where("user_org_id = ?", orgid)
  4687. }
  4688. if startTime > 0 {
  4689. db = db.Where("ctime >=? ", startTime)
  4690. }
  4691. if endTime > 0 {
  4692. db = db.Where("ctime<=?", endTime)
  4693. }
  4694. if stock_type > 0 {
  4695. //手动入库
  4696. if stock_type == 1 {
  4697. db = db.Where("consumable_type = 1")
  4698. }
  4699. //手动出库
  4700. if stock_type == 2 {
  4701. db = db.Where("consumable_type = 2 and is_sys = 0")
  4702. }
  4703. //自动出库
  4704. if stock_type == 3 {
  4705. db = db.Where("consumable_type = 3 and is_sys = 1")
  4706. }
  4707. if stock_type == 4 {
  4708. db = db.Where("consumable_type = 4")
  4709. }
  4710. if stock_type == 10 {
  4711. db = db.Where("consumable_type = 10")
  4712. }
  4713. if stock_type == 11 {
  4714. db = db.Where("consumable_type = 11")
  4715. }
  4716. if stock_type == 12 {
  4717. db = db.Where("consumable_type = 12")
  4718. }
  4719. if stock_type == 13 {
  4720. db = db.Where("consumable_type = 13")
  4721. }
  4722. if stock_type == 15 {
  4723. db = db.Where("consumable_type = 15")
  4724. }
  4725. }
  4726. err = db.Count(&total).Offset(offset).Limit(limit).Order("id desc").Preload("BaseDrugLib", "status = 1").Preload("DrugWarehouseInfo", "status = 1").Preload("XtDrugWarehouseOutInfo", "status = 1").Find(&drug_flow).Error
  4727. return drug_flow, total, err
  4728. }
  4729. func GetDrugWarehouseOutInfoIsExit(drug_id int64, patient_id int64, recordtime int64) (*models.DrugWarehouseOutInfo, error) {
  4730. outInfo := models.DrugWarehouseOutInfo{}
  4731. var err error
  4732. err = XTReadDB().Model(&outInfo).Where("drug_id = ? and patient_id = ? and status = 1 and sys_record_time = ?", drug_id, patient_id, recordtime).Find(&outInfo).Error
  4733. if err == gorm.ErrRecordNotFound {
  4734. return nil, err
  4735. }
  4736. if err != nil {
  4737. return nil, err
  4738. }
  4739. return &outInfo, nil
  4740. }
  4741. func GetDrugWarehouseOutInfoIsExitOne(drug_id int64, patient_id int64, recordtime int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  4742. err = XTReadDB().Model(&outInfo).Where("drug_id = ? and patient_id = ? and status = 1 and sys_record_time = ?", drug_id, patient_id, recordtime).Find(&outInfo).Error
  4743. return outInfo, err
  4744. }
  4745. func UpdatedDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, drug_id int64, patient_id int64, record_date int64, orgid int64) error {
  4746. outInfo := models.DrugWarehouseOutInfo{}
  4747. err := XTWriteDB().Model(&outInfo).Where("drug_id = ? and patient_id = ? and sys_record_time = ? and org_id = ?", drug_id, patient_id, record_date, orgid).Updates(map[string]interface{}{"warehouse_out_id": info.WarehouseOutId, "drug_id": info.DrugId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "mtime": info.Mtime, "ctime": info.Ctime, "org_id": info.OrgId, "warehouse_out_order_number": info.WarehouseOutOrderNumber, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "is_sys": info.IsSys, "sys_record_time": info.SysRecordTime, "warehouse_info_id": info.WarehouseInfoId, "number": info.Number, "batch_number": info.BatchNumber, "patient_id": info.PatientId}).Error
  4748. return err
  4749. }
  4750. func GetCancelExportList(startime int64, endtime int64, ids []string, orgid int64) (cancle []*models.CancelStockInfo, err error) {
  4751. db := XTReadDB().Model(&cancle).Where("status = 1")
  4752. if startime > 0 {
  4753. db = db.Where("ctime >=?", startime)
  4754. }
  4755. if endtime > 0 {
  4756. db = db.Where("ctime <=?", endtime)
  4757. }
  4758. if len(ids) > 0 {
  4759. db = db.Where("cancel_stock_id in(?)", ids)
  4760. }
  4761. if orgid > 0 {
  4762. db = db.Where("org_id = ?", orgid)
  4763. }
  4764. db = db.Preload("GoodInfo", "status =1 and org_id = ?", orgid)
  4765. err = db.Find(&cancle).Error
  4766. return cancle, err
  4767. }
  4768. func GetCancelStockDetail(id int64) (info []*models.DrugCancelStockInfo, err error) {
  4769. db := XTReadDB().Model(&info)
  4770. if id > 0 {
  4771. db = db.Where("cancel_stock_id = ? and status = 1", id)
  4772. }
  4773. db = db.Preload("BaseDrugLib")
  4774. err = db.Find(&info).Error
  4775. return info, err
  4776. }
  4777. func GetDrugWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  4778. info := models.XtDrugWarehouseInfo{}
  4779. db := XTReadDB().Model(&info).Where("status = 1")
  4780. if id > 0 {
  4781. db = db.Where("id = ?", id)
  4782. }
  4783. err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  4784. return info, err
  4785. }
  4786. //func GetDrugWarehouseInfoTX(id int64,tx *gorm.DB) (models.XtDrugWarehouseInfo, error) {
  4787. // info := models.XtDrugWarehouseInfo{}
  4788. // db := tx.Model(&info).Where("status = 1")
  4789. // if id > 0 {
  4790. // db = db.Where("id = ?", id)
  4791. // }
  4792. // err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  4793. // return info, err
  4794. //}
  4795. func GetDrugWarehouseInfoPrescription(drugid int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  4796. err = XTReadDB().Where("drug_id = ? and org_id =? and status = 1", drugid, orgid).Find(&info).Error
  4797. return info, err
  4798. }
  4799. func GetDrugWarehouseInfoPrescriptionSeven(drugid int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfoSeven, err error) {
  4800. err = XTReadDB().Select("id,stock_max_number,stock_min_number").Where("drug_id = ? and org_id =? and status = 1 and storehouse_id = ? and is_check = 1", drugid, orgid, storehouse_id).Find(&info).Error
  4801. return info, err
  4802. }
  4803. func GetHisAdviceListByDrugId(drugid int64, patient_id int64, advice_date int64) (models.HisDoctorAdviceInfo, error) {
  4804. info := models.HisDoctorAdviceInfo{}
  4805. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1", drugid, patient_id, advice_date).Find(&info).Error
  4806. return info, err
  4807. }
  4808. func GetHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64) (info []*models.HisDoctorAdviceInfo, err error) {
  4809. err = XTReadDB().Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  4810. return info, err
  4811. }
  4812. func GetNewHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64, tx *gorm.DB) (info []*models.HisDoctorAdviceInfo, err error) {
  4813. err = tx.Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  4814. if err != gorm.ErrRecordNotFound {
  4815. if err != nil {
  4816. tx.Rollback()
  4817. return info, err
  4818. }
  4819. }
  4820. return info, err
  4821. }
  4822. func GetLastHisAdvicesList(patient_id int64, advice_date int64, user_org_id int64, tx *gorm.DB) (models.HisDoctorAdviceInfo, error) {
  4823. adviceInfo := models.HisDoctorAdviceInfo{}
  4824. err := tx.Where("patient_id = ? and advice_date= ? and user_org_id = ? and status=1", patient_id, advice_date, user_org_id).Last(&adviceInfo).Error
  4825. if err != gorm.ErrRecordNotFound {
  4826. if err != nil {
  4827. tx.Rollback()
  4828. return adviceInfo, err
  4829. }
  4830. }
  4831. return adviceInfo, err
  4832. }
  4833. func GetHisAdviceListByDrugIdEight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  4834. info := models.HisDoctorAdviceInfo{}
  4835. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1 and id =?", drugid, patient_id, advice_date, advice_id).First(&info).Error
  4836. return info, err
  4837. }
  4838. func GetLastDrugWarehouseOutByDrugId(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.DrugAutomaticReduceDetail, error) {
  4839. detail := models.DrugAutomaticReduceDetail{}
  4840. err := XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1 and advice_id = ?", drugid, patient_id, advice_date, advice_id).Last(&detail).Error
  4841. return detail, err
  4842. }
  4843. func GetLastDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64) (out []*models.DrugWarehouseOutInfo, err error) {
  4844. err = XTReadDB().Where("advice_id = ? and drug_id = ? and patient_id = ? and sys_record_time = ? and status = 1 and is_check = 1", advice_id, drugid, patient_id, advice_date).Find(&out).Error
  4845. return out, err
  4846. }
  4847. func GetLastNewDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64, tx *gorm.DB) (out []*models.DrugWarehouseOutInfo, err error) {
  4848. err = tx.Where("advice_id = ? and drug_id = ? and patient_id = ? and sys_record_time = ? and status = 1 and is_check = 1", advice_id, drugid, patient_id, advice_date).Find(&out).Error
  4849. if err != gorm.ErrRecordNotFound {
  4850. if err != nil {
  4851. tx.Rollback()
  4852. return out, err
  4853. }
  4854. }
  4855. return out, err
  4856. }
  4857. func ModefyDrugByWarehouseInfo(id int64, stock_max_number int64) error {
  4858. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  4859. return err
  4860. }
  4861. func ModefyDrugByWarehouseInfoOne(id int64, stock_min_number int64) error {
  4862. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  4863. return err
  4864. }
  4865. func DeleteDrugAutoWarehouse(drugid int64, patient_id int64, record_time int64) error {
  4866. err := XTWriteDB().Model(models.DrugAutomaticReduceDetail{}).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patient_id, record_time).Updates(map[string]interface{}{"status": 0}).Error
  4867. err = XTWriteDB().Model(models.DrugWarehouseOutInfo{}).Where("drug_id = ? and patient_id = ? and sys_record_time = ? and status = 1", drugid, patient_id, record_time).Updates(map[string]interface{}{"status": 0}).Error
  4868. err = XTWriteDB().Model(models.DrugFlow{}).Where("drug_id = ? and patient_id = ? and system_time = ? and status = 1", drugid, patient_id, record_time).Updates(map[string]interface{}{"status": 0}).Error
  4869. return err
  4870. }
  4871. func DeleteDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  4872. err := XTWriteDB().Model(models.DrugAutomaticReduceDetail{}).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1 and advice_id = ?", drugid, patient_id, record_time, advice_id).Updates(map[string]interface{}{"status": 0}).Error
  4873. err = XTWriteDB().Model(models.DrugFlow{}).Where("drug_id = ? and patient_id = ? and system_time = ? and status = 1 and advice_id = ?", drugid, patient_id, record_time, advice_id).Updates(map[string]interface{}{"status": 0}).Error
  4874. return err
  4875. }
  4876. func DeleteNewDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64, tx *gorm.DB) error {
  4877. err := tx.Model(models.DrugAutomaticReduceDetail{}).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1 and advice_id = ?", drugid, patient_id, record_time, advice_id).Updates(map[string]interface{}{"status": 0}).Error
  4878. err = tx.Model(models.DrugFlow{}).Where("drug_id = ? and patient_id = ? and system_time = ? and status = 1 and advice_id = ?", drugid, patient_id, record_time, advice_id).Updates(map[string]interface{}{"status": 0}).Error
  4879. if err != nil {
  4880. tx.Rollback()
  4881. return err
  4882. }
  4883. return err
  4884. }
  4885. func GetDrugWarehouseInfoById(id int64) (models.XtDrugWarehouseInfo, error) {
  4886. info := models.XtDrugWarehouseInfo{}
  4887. err := XTReadDB().Model(&info).Where("id=? and status =1", id).Find(&info).Error
  4888. return info, err
  4889. }
  4890. func GetDrugWarehouseInfoByIdTwenty(id int64) (models.XtDrugWarehouseInfo, error) {
  4891. info := models.XtDrugWarehouseInfo{}
  4892. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  4893. return info, err
  4894. }
  4895. func UpdateDrugWarehouseInfo(info *models.XtDrugWarehouseInfo, id int64) error {
  4896. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  4897. return err
  4898. }
  4899. //func UpdateDrugWarehouseInfoTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  4900. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  4901. // return err
  4902. //}
  4903. func UpdateDrugWarehouseInfoOne(info *models.XtDrugWarehouseInfo, id int64) error {
  4904. //err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_min_number": info.StockMaxNumber}).Error
  4905. //return err
  4906. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  4907. return err
  4908. }
  4909. //func UpdateDrugWarehouseInfoOneTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  4910. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  4911. // return err
  4912. //}
  4913. func GetLastCancelStockInfo(id int64) (models.DrugCancelStockInfo, error) {
  4914. info := models.DrugCancelStockInfo{}
  4915. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  4916. return info, err
  4917. }
  4918. func GetLastCancelStockInfoOne(id int64, drug_id int64) (models.DrugCancelStockInfo, error) {
  4919. info := models.DrugCancelStockInfo{}
  4920. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ? and status = 1", id, drug_id).Find(&info).Error
  4921. return info, err
  4922. }
  4923. func UpdatedDrugInfo(id int64, count int64) (err error) {
  4924. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  4925. return err
  4926. }
  4927. func UpdatedDrugFlow(cancelstockid int64, drug_id int64, flow models.DrugFlow) error {
  4928. err := XTWriteDB().Model(&models.DrugFlow{}).Where("cancel_stock_id = ? and drug_id = ? and status = 1", cancelstockid, drug_id).Updates(map[string]interface{}{"count": flow.Count}).Error
  4929. return err
  4930. }
  4931. func UpdatedDrugInfoOne(id int64, count int64) (err error) {
  4932. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  4933. return err
  4934. }
  4935. func UpdateWarehouseInfo(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  4936. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? and org_id = ? and status = 1 and id = ?", drugid, orgid, warehouse_info_id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  4937. return err
  4938. }
  4939. func UpdateWarehouseInfoTwo(count int64, drugid int64, orgid int64) error {
  4940. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? and org_id = ? and status = 1", drugid, orgid).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  4941. return err
  4942. }
  4943. func UpdateWarehouseInfoOne(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  4944. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? and org_id = ? and status = 1 and id = ?", drugid, orgid, warehouse_info_id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  4945. return err
  4946. }
  4947. func UpdateWarehouseInfoFour(count int64, drugid int64, orgid int64) error {
  4948. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? and org_id = ? and status = 1", drugid, orgid).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  4949. return err
  4950. }
  4951. func GetWarehouseInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  4952. info := models.WarehousingInfo{}
  4953. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  4954. return info, err
  4955. }
  4956. func GetLastWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  4957. info := models.WarehousingInfo{}
  4958. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  4959. return info, err
  4960. }
  4961. func GetFirstWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  4962. info := models.WarehousingInfo{}
  4963. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).First(&info).Error
  4964. return info, err
  4965. }
  4966. func CreateWarehouseOut(out models.WarehouseOut) error {
  4967. err := XTWriteDB().Create(&out).Error
  4968. return err
  4969. }
  4970. func GetLastGoodWarehouseOut(orgid int64) (models.WarehouseOut, error) {
  4971. out := models.WarehouseOut{}
  4972. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  4973. return out, err
  4974. }
  4975. func CreateWarehouseInfo(info models.WarehousingInfo) error {
  4976. err := XTWriteDB().Create(&info).Error
  4977. return err
  4978. }
  4979. func UpdateGoodWarehouseInfo(id int64, info models.WarehousingInfo) error {
  4980. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  4981. return err
  4982. }
  4983. func UpdateWareInfoById(id int64, count int64) error {
  4984. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  4985. return err
  4986. }
  4987. func UpdateWareInfoByIdSix(id int64, count int64) error {
  4988. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  4989. return err
  4990. }
  4991. func GetLastCancelInfo(id int64, goodid int64) (models.CancelStockInfo, error) {
  4992. info := models.CancelStockInfo{}
  4993. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  4994. return info, err
  4995. }
  4996. func GetLastCancelInfoSix(id int64, goodid int64, storehouse_id int64) (models.CancelStockInfo, error) {
  4997. info := models.CancelStockInfo{}
  4998. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1 and storehouse_id = ?", id, goodid, storehouse_id).Find(&info).Error
  4999. return info, err
  5000. }
  5001. func GetAllCancelInfoById(id int64, goodid int64) (info []*models.CancelStockInfo, err error) {
  5002. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5003. return info, err
  5004. }
  5005. func GetAllCancelInfoByIdSix(id int64, goodid int64, storehouse_id int64) (info []*models.CancelStockInfo, err error) {
  5006. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5007. return info, err
  5008. }
  5009. func UpdateStockFlow(flow models.VmStockFlow, goodid int64, warehousing_id int64) error {
  5010. err := XTWriteDB().Model(&flow).Where("good_id = ? and warehousing_id= ? and status = 1", goodid, warehousing_id).Updates(map[string]interface{}{"count": flow.Count}).Error
  5011. return err
  5012. }
  5013. func UpdateDrugFlow(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5014. drugFlow := models.DrugFlow{}
  5015. err := XTWriteDB().Model(&drugFlow).Where("drug_id = ? and warehousing_id= ? and status = 1", drugid, warehousing_id).Updates(map[string]interface{}{"count": flow.Count, "max_unit": flow.MaxUnit, "min_unit": flow.MinUnit, "price": flow.Price, "expire_date": flow.ExpireDate}).Error
  5016. return err
  5017. }
  5018. func UpdateDrugFlowNight(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5019. drugFlow := models.DrugFlow{}
  5020. err := XTWriteDB().Model(&drugFlow).Where("drug_id = ? and warehousing_detail_id= ? and status = 1", drugid, warehousing_id).Updates(map[string]interface{}{"count": flow.Count, "max_unit": flow.MaxUnit, "min_unit": flow.MinUnit, "price": flow.Price, "expire_date": flow.ExpireDate}).Error
  5021. return err
  5022. }
  5023. func UpdateDrugCancel(id int64) (models.DrugCancelStock, error) {
  5024. stock := models.DrugCancelStock{}
  5025. err := XTWriteDB().Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5026. return stock, err
  5027. }
  5028. //func UpdateDrugCancelTX(id int64,tx *gorm.DB) (models.DrugCancelStock, error) {
  5029. // stock := models.DrugCancelStock{}
  5030. // err := tx.Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5031. // return stock, err
  5032. //}
  5033. func GetDrugWarehouseOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugWarehouseOutInfo, err error) {
  5034. db := XTReadDB().Model(&info)
  5035. if warehouse_info_id > 0 {
  5036. db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5037. }
  5038. if drug_id > 0 {
  5039. db = db.Where("drug_id = ? and status =1", drug_id)
  5040. }
  5041. err = db.Find(&info).Error
  5042. return info, err
  5043. }
  5044. //func GetDrugWarehouseOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  5045. // db := tx.Model(&info)
  5046. // if warehouse_info_id > 0 {
  5047. // db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5048. // }
  5049. // if drug_id > 0 {
  5050. // db = db.Where("drug_id = ? and status =1", drug_id)
  5051. // }
  5052. // err = db.Find(&info).Error
  5053. // return info, err
  5054. //}
  5055. func GetCancelDrugStockOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugCancelStockInfo, err error) {
  5056. err = XTReadDB().Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5057. return info, err
  5058. }
  5059. //func GetCancelDrugStockOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugCancelStockInfo, err error) {
  5060. // err = tx.Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5061. // return info, err
  5062. //}
  5063. func GetTemplateSummary(orgid int64) (config []*models.Dataconfig, err error) {
  5064. err = XTReadDB().Model(&config).Where("module='template_summary' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5065. return config, err
  5066. }
  5067. func GetTemplatePlan(orgid int64) (config []*models.Dataconfig, err error) {
  5068. err = XTReadDB().Model(&config).Where("module='template_plan' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5069. return config, err
  5070. }
  5071. func GetSearchGoodWarehouseList(keyword string, orgid int64, storehouse_id int64) (info []*models.BloodWarehouseInfo, err error) {
  5072. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  5073. table := XTReadDB().Table("xt_goood_information as t").Where("t.status")
  5074. fmt.Println(table)
  5075. likeKey := "%" + keyword + "%"
  5076. if orgid > 0 {
  5077. db = db.Where("x.org_id = ?", orgid)
  5078. }
  5079. if storehouse_id > 0 {
  5080. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5081. }
  5082. err = db.Group("x.good_id").Select("x.id,x.warehousing_id,x.good_id,x.good_type_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.price,x.total_price,t.dealer,x.remark,x.warehousing_order,x.warehousing_unit,x.stock_count,x.license_number,x.warehousing_unit,x.storehouse_id,t.good_name,t.specification_name,t.manufacturer,t.packing_price,t.id as good_id,t.packing_unit,t.buy_price").Joins("left join xt_good_information as t on t.id = x.good_id").Where("t.good_name like ? and t.org_id = ?", likeKey, orgid).Order("x.ctime desc").Scan(&info).Error
  5083. return info, err
  5084. }
  5085. func CreateAdjustPrice(adjustPrice *models.XtStockAdjustPrice) error {
  5086. err := XTWriteDB().Create(&adjustPrice).Error
  5087. return err
  5088. }
  5089. func GetAllStockPrice(orgid int64, startime int64, endtime int64, keyword string, limit int64, page int64) (list []*models.VmStockAdjustPrice, total int64, err error) {
  5090. likeKey := "%" + keyword + "%"
  5091. offset := (page - 1) * limit
  5092. db := XTReadDB().Table("xt_stock_adjust_price as x").Where("x.status = 1")
  5093. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5094. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5095. fmt.Println(table, tab)
  5096. if startime > 0 {
  5097. db = db.Where("x.start_time>=?", startime)
  5098. }
  5099. if endtime > 0 {
  5100. db = db.Where("x.start_time<=?", endtime)
  5101. }
  5102. if len(keyword) > 0 {
  5103. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  5104. }
  5105. if orgid > 0 {
  5106. db = db.Where("x.user_org_id = ?", orgid)
  5107. }
  5108. err = db.Group("x.id").Select("x.id,x.good_name,x.specification_name,x.warehousing_unit,x.count,x.buy_price,x.packing_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.good_id,x.warehousing_order,x.license_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,t.packing_unit").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.creater").Joins("left join xt_good_information as t on t.id =x.good_id").Count(&total).Offset(offset).Limit(limit).Scan(&list).Error
  5109. return list, total, err
  5110. }
  5111. func UpdateAdjustPrice(ids []string, info models.XtStockAdjustPrice) error {
  5112. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5113. return err
  5114. }
  5115. func UpdateAdjustCheckPrice(info *models.GoodInfo, id int64) error {
  5116. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"packing_price": info.PackingPrice, "buy_price": info.BuyPrice}).Error
  5117. return err
  5118. }
  5119. func GetAdjustCheckPriceList(ids []string) (info []*models.XtStockAdjustPrice, err error) {
  5120. err = XTReadDB().Where("id in(?)", ids).Find(&info).Error
  5121. return info, err
  5122. }
  5123. func GetAdjustPricebyIdOne(ids []string) (adjust []*models.XtStockAdjustPrice, err error) {
  5124. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5125. return adjust, err
  5126. }
  5127. func CreateReportPrice(price *models.XtStockReportPrice) error {
  5128. err := XTWriteDB().Create(&price).Error
  5129. return err
  5130. }
  5131. func GetReportStockList(startime int64, endtime int64, orgid int64, keyword string, page int64, limit int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5132. likeKey := "%" + keyword + "%"
  5133. offset := (page - 1) * limit
  5134. db := XTReadDB().Table("xt_stock_report_price as x").Where("x.status = 1")
  5135. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5136. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5137. fmt.Println(table, tab)
  5138. if startime > 0 {
  5139. db = db.Where("x.ctime>=?", startime)
  5140. }
  5141. if endtime > 0 {
  5142. db = db.Where("x.ctime<=?", endtime)
  5143. }
  5144. if len(keyword) > 0 {
  5145. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5146. }
  5147. if orgid > 0 {
  5148. db = db.Where("x.user_org_id = ?", orgid)
  5149. }
  5150. err = db.Group("x.id").Select("x.id,x.good_name,x.specification_name,x.warehousing_unit,x.count,x.buy_price,x.packing_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.good_id,x.warehousing_order,x.license_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,t.packing_unit,r.user_name").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.creater").Joins("left join xt_good_information as t on t.id =x.good_id").Count(&total).Offset(offset).Limit(limit).Scan(&adjust).Error
  5151. return adjust, total, err
  5152. }
  5153. func GetStockDamagedList(orgid int64, keyword string, page int64, limit int64, startime int64, endtime int64, storehouse_id int64) (adjust []*models.VmStockInventory, total int64, err error) {
  5154. likeKey := "%" + keyword + "%"
  5155. offset := (page - 1) * limit
  5156. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.inventory_type = 4")
  5157. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5158. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5159. fmt.Println(table, tab)
  5160. if len(keyword) > 0 {
  5161. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5162. }
  5163. if orgid > 0 {
  5164. db = db.Where("x.user_org_id = ?", orgid)
  5165. }
  5166. if startime > 0 {
  5167. db = db.Where("x.ctime >=?", startime)
  5168. }
  5169. if endtime > 0 {
  5170. db = db.Where("x.ctime <=?", endtime)
  5171. }
  5172. if storehouse_id > 0 {
  5173. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5174. }
  5175. err = db.Group("x.id").Select("x.ctime,x.id,x.good_name,x.specification_name,x.warehousing_unit,x.count,x.buy_price,x.packing_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.good_id,x.warehousing_order,x.license_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.total,x.number,x.storehouse_id,t.packing_unit,r.user_name").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.creater").Joins("left join xt_good_information as t on t.id =x.good_id").Count(&total).Offset(offset).Limit(limit).Scan(&adjust).Error
  5176. return adjust, total, err
  5177. }
  5178. func GetStockDamagedCountOne(orgid int64, keyword string, inventory_type int64) (adjust []*models.VmStockInventory, err error) {
  5179. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5180. if inventory_type == 0 {
  5181. db = db.Where("x.inventory_type = 4")
  5182. }
  5183. if inventory_type > 0 {
  5184. db = db.Where("x.inventory_type = ?", inventory_type)
  5185. }
  5186. if len(keyword) > 0 {
  5187. likeKey := "%" + keyword + "%"
  5188. db = db.Where("x.warehousing_order like ?", likeKey)
  5189. }
  5190. if orgid > 0 {
  5191. db = db.Where("x.user_org_id = ?", orgid)
  5192. }
  5193. err = db.Group("x.good_id").Select("x.id,x.good_name,x.specification_name,x.warehousing_unit,sum(x.count) as count,x.buy_price,x.packing_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.good_id,x.warehousing_order,x.license_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.total,x.number").Scan(&adjust).Error
  5194. return adjust, err
  5195. }
  5196. func GetStockDamagedCount(orgid int64) (adjust []*models.VmStockInventory, err error) {
  5197. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and inventory_type = 4 ")
  5198. if orgid > 0 {
  5199. db = db.Where("x.user_org_id = ?", orgid)
  5200. }
  5201. err = db.Group("x.good_id").Select("x.id,x.good_name,x.specification_name,x.warehousing_unit,sum(x.count) as count,x.buy_price,x.packing_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.good_id,x.warehousing_order,x.license_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.total,x.number").Scan(&adjust).Error
  5202. return adjust, err
  5203. }
  5204. func UpdateCheckDamage(ids []string, reportPrice models.XtStockReportPrice) error {
  5205. err := XTWriteDB().Model(&reportPrice).Where("id in(?) and status = 1", ids).Updates(map[string]interface{}{"checker": reportPrice.Checker, "checker_time": reportPrice.CheckerTime, "checker_status": reportPrice.CheckerStatus}).Error
  5206. return err
  5207. }
  5208. func GetCheckDamageList(ids []string) (report []*models.XtStockReportPrice, err error) {
  5209. err = XTReadDB().Model(&report).Where("id in(?) and status= 1", ids).Find(&report).Error
  5210. return report, err
  5211. }
  5212. func CreateInentory(inventory models.XtStockInventory) error {
  5213. err = XTWriteDB().Create(&inventory).Error
  5214. return err
  5215. }
  5216. func GetInventorylist(startime int64, endtime int64, page int64, limit int64, inventory_status int64, keyword string, orgid int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5217. likeKey := "%" + keyword + "%"
  5218. offset := (page - 1) * limit
  5219. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5220. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5221. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5222. fmt.Println(table, tab)
  5223. if startime > 0 {
  5224. db = db.Where("x.ctime>=?", startime)
  5225. }
  5226. if endtime > 0 {
  5227. db = db.Where("x.ctime<=?", endtime)
  5228. }
  5229. if len(keyword) > 0 {
  5230. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5231. }
  5232. if orgid > 0 {
  5233. db = db.Where("x.user_org_id = ?", orgid)
  5234. }
  5235. if inventory_status > 0 {
  5236. db = db.Where("checker_status = ?", inventory_status)
  5237. }
  5238. err = db.Group("x.id").Select("x.id,x.good_name,x.specification_name,x.warehousing_unit,x.count,x.buy_price,x.packing_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.good_id,x.warehousing_order,x.license_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.total,x.number,t.packing_unit,r.user_name").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.creater").Joins("left join xt_good_information as t on t.id =x.good_id").Count(&total).Offset(offset).Limit(limit).Scan(&adjust).Error
  5239. return adjust, total, err
  5240. }
  5241. func UpdateCheckInventory(ids []string, info models.XtStockInventory) error {
  5242. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5243. return err
  5244. }
  5245. func GetModifyPriceDetail(id int64) (models.XtStockAdjustPrice, error) {
  5246. info := models.XtStockAdjustPrice{}
  5247. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5248. return info, err
  5249. }
  5250. func GetInventoryDetailById(id []string) (list []*models.XtStockInventory, err error) {
  5251. err = XTReadDB().Model(&list).Where("id in(?) and status = 1", id).Find(&list).Error
  5252. return list, err
  5253. }
  5254. func GetWarehouseInfoByIdNight(id int64) (models.WarehousingInfo, error) {
  5255. info := models.WarehousingInfo{}
  5256. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5257. return info, err
  5258. }
  5259. func GetWarehouseInfoById(id int64) (models.WarehousingInfo, error) {
  5260. info := models.WarehousingInfo{}
  5261. //err :=XTReadDB().Model(&info).Where("id = ? or warehouse_info_id and status = 1", id).Find(&info).Error
  5262. //return info, err
  5263. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5264. if id > 0 {
  5265. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5266. }
  5267. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5268. return info, err
  5269. }
  5270. func GetWarehouseInfoByIdTen(id int64, storehouse_id int64) (models.WarehousingInfo, error) {
  5271. info := models.WarehousingInfo{}
  5272. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5273. if id > 0 {
  5274. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5275. }
  5276. if storehouse_id > 0 {
  5277. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5278. }
  5279. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5280. return info, err
  5281. }
  5282. func UpdateStockPrice(info models.XtStockAdjustPrice, id int64) error {
  5283. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"new_price": info.NewPrice, "good_name": info.GoodName, "packing_price": info.PackingPrice, "good_id": info.GoodId, "remark": info.Remark, "manufacturer": info.Manufacturer, "dealer": info.Dealer}).Error
  5284. return err
  5285. }
  5286. func DeleteStockPrice(id int64) error {
  5287. info := models.XtStockAdjustPrice{}
  5288. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5289. return err
  5290. }
  5291. func GetStockDamageDetail(id int64) (models.XtStockReportPrice, error) {
  5292. report := models.XtStockReportPrice{}
  5293. err := XTReadDB().Model(&report).Where("id=? and status = 1", id).Find(&report).Error
  5294. return report, err
  5295. }
  5296. func ModifyPrice(id int64, info models.XtStockReportPrice) error {
  5297. err := XTWriteDB().Model(&info).Where("id = ?", id).Updates(map[string]interface{}{"count": info.Count, "specification_name": info.SpecificationName, "good_name": info.GoodName, "number": info.Number, "warehousing_unit": info.WarehousingUnit, "buy_price": info.BuyPrice, "packing_price": info.PackingPrice, "good_origin_place": info.GoodOriginPlace, "manufacturer": info.Manufacturer, "dealer": info.Dealer, "total": info.Total, "remark": info.Remark, "warehousing_info_id": info.WarehousingInfoId, "good_id": info.GoodId}).Error
  5298. return err
  5299. }
  5300. func DeleteDamage(id int64) error {
  5301. adjust := models.XtStockReportPrice{}
  5302. err := XTWriteDB().Model(&adjust).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5303. return err
  5304. }
  5305. func GetStockDamageById(ids []string) (adjust []*models.XtStockReportPrice, err error) {
  5306. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5307. return adjust, err
  5308. }
  5309. func GetInventoryDetail(id int64) (models.XtStockInventory, error) {
  5310. inventory := models.XtStockInventory{}
  5311. err := XTReadDB().Model(&inventory).Where("id=? and status = 1", id).Find(&inventory).Error
  5312. return inventory, err
  5313. }
  5314. func ModifyInventory(id int64, inventory models.XtStockInventory) error {
  5315. err := XTWriteDB().Model(&inventory).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"good_id": inventory.GoodId, "good_name": inventory.GoodName, "specification_name": inventory.SpecificationName, "number": inventory.Number, "warehousing_unit": inventory.WarehousingUnit, "buy_price": inventory.BuyPrice, "packing_price": inventory.PackingPrice, "total": inventory.Total, "good_origin_place": inventory.GoodOriginPlace, "license_number": inventory.LicenseNumber, "dealer": inventory.Dealer, "manufacturer": inventory.Dealer, "count": inventory.Count, "remark": inventory.Remark}).Error
  5316. return err
  5317. }
  5318. func DeleteInventory(id int64) error {
  5319. inventory := models.XtStockInventory{}
  5320. err := XTWriteDB().Model(&inventory).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5321. return err
  5322. }
  5323. func GetInventoryDetailList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64) (list []*models.VmStockInventory, total int64, err error) {
  5324. likeKey := "%" + keyword + "%"
  5325. offset := (page - 1) * limit
  5326. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5327. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5328. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5329. fmt.Println(table, tab)
  5330. if len(keyword) > 0 {
  5331. db = db.Where("x.good_name like ?", likeKey)
  5332. }
  5333. if orgid > 0 {
  5334. db = db.Where("x.user_org_id = ?", orgid)
  5335. }
  5336. if storehouse_id > 0 {
  5337. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5338. }
  5339. err = db.Select("x.id,x.good_name,x.specification_name,x.warehousing_unit,x.count,x.buy_price,x.packing_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.good_id,x.warehousing_order,x.license_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.total,x.number,x.expire_date,x.product_date,t.packing_unit,x.last_stock_count,x.type,x.inventory_type,x.stock_count,x.storehouse_id,x.ctime").Joins("left join sgj_users.sgj_user_admin_role as r on r.id = x.creater").Joins("left join xt_good_information as t on t.id =x.good_id").Count(&total).Offset(offset).Limit(limit).Order("x.id desc").Scan(&list).Error
  5340. return list, total, err
  5341. }
  5342. func GetInventoryDetailPrintMapList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64, ids []string) (list []*models.VmStockInventory, total int64, err error) {
  5343. likeKey := "%" + keyword + "%"
  5344. offset := (page - 1) * limit
  5345. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5346. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5347. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5348. fmt.Println(table, tab)
  5349. if len(keyword) > 0 {
  5350. db = db.Where("x.good_name like ?", likeKey)
  5351. }
  5352. if orgid > 0 {
  5353. db = db.Where("x.user_org_id = ?", orgid)
  5354. }
  5355. if storehouse_id > 0 {
  5356. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5357. }
  5358. if len(ids) > 0 {
  5359. db = db.Where("x.id in(?)", ids)
  5360. }
  5361. err = db.Select("x.id,x.good_name,x.specification_name,x.warehousing_unit,x.count,x.buy_price,x.packing_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.good_id,x.warehousing_order,x.license_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.total,x.number,x.expire_date,x.product_date,t.packing_unit,x.last_stock_count,x.type,x.inventory_type,x.stock_count,x.storehouse_id").Joins("left join sgj_users.sgj_user_admin_role as r on r.id = x.creater").Joins("left join xt_good_information as t on t.id =x.good_id").Count(&total).Offset(offset).Limit(limit).Scan(&list).Error
  5362. return list, total, err
  5363. }
  5364. func GetInventoryPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5365. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5366. return list, err
  5367. }
  5368. func GetInventoryDetailPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5369. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5370. return list, err
  5371. }
  5372. func DeleteDrugPrice(id int64) error {
  5373. info := models.XtDrugAdjustPrice{}
  5374. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5375. return err
  5376. }
  5377. func GetGoodWarehouseList(goodid int64) (models.WarehousingInfo, error) {
  5378. info := models.WarehousingInfo{}
  5379. db := XTReadDB().Model(&info).Where("status = 1")
  5380. if goodid > 0 {
  5381. db = db.Where("good_id = ?", goodid)
  5382. }
  5383. err := db.Select("good_id,sum(stock_count) as stock_count").Find(&info).Error
  5384. return info, err
  5385. }
  5386. func UpdateProofInventory(id int64, info models.WarehousingInfo) error {
  5387. err := XTWriteDB().Model(&info).Where("id=? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5388. return err
  5389. }
  5390. func UpdateStockInventory(inventory models.XtStockInventory, id int64) error {
  5391. err := XTWriteDB().Model(&inventory).Where("id= ? and status = 1", id).Updates(map[string]interface{}{"total": inventory.Total}).Error
  5392. return err
  5393. }
  5394. func CeateStockCorrectRecord(record models.XtStockCorrectRecord) error {
  5395. err := XTWriteDB().Create(&record).Error
  5396. return err
  5397. }
  5398. func GetWarehouseTotal(id int64) (models.WarehousingInfo, error) {
  5399. info := models.WarehousingInfo{}
  5400. //err :=XTReadDB()()()()().Where("id=? and status = 1", id).Find(&info).Error
  5401. //return info, err
  5402. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5403. if id > 0 {
  5404. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5405. }
  5406. err := db.Select("sum(x.stock_count) as stock_count").Scan(&info).Error
  5407. return info, err
  5408. }
  5409. func GetCurrentWarehosueInfo(id int64) (info []*models.DrugWarehouseInfo, err error) {
  5410. err = XTReadDB().Model(&info).Where("id=? or warehousing_info_id = ? ", id, id).Find(&info).Error
  5411. return info, err
  5412. }
  5413. func GetCurrentWarehosueInfoOne(id int64) (models.DrugWarehouseInfo, error) {
  5414. info := models.DrugWarehouseInfo{}
  5415. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  5416. return info, err
  5417. }
  5418. func ModifyDrugWarehouseInfo(info *models.DrugWarehouseInfo, id int64) error {
  5419. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_max_number": info.StockMaxNumber, "stock_min_number": info.StockMinNumber, "max_unit": info.MaxUnit, "min_unit": info.MinUnit}).Error
  5420. return err
  5421. }
  5422. func ModifyDrugWarehouseInfoSix(info *models.DrugWarehouseInfo, id int64) error {
  5423. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_max_number": info.StockMaxNumber, "stock_min_number": info.StockMinNumber, "max_unit": info.MaxUnit, "min_unit": info.MinUnit}).Error
  5424. return err
  5425. }
  5426. func GetGoodInventoryWarehouseList(id int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  5427. err = XTReadDB().Model(&info).Where("good_id = ? and status = 1 and storehouse_id=? and is_check= 1 and stock_count>0", id, storehouse_id).Preload("GoodInfo", "status = 1").Find(&info).Error
  5428. return info, err
  5429. }
  5430. func UpdatedWarehousingInfo(info models.WarehousingInfo, id int64) error {
  5431. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5432. return err
  5433. }
  5434. func UpdatedWarehousingInfoSix(info models.WarehousingInfo, id int64) error {
  5435. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount, "warehousing_count": info.WarehousingCount}).Error
  5436. return err
  5437. }
  5438. func GetDamageDetailByGoodId(good_id int64, keyword string, inventory_type int64) (list []*models.XtStockInventory, err error) {
  5439. db := XTReadDB().Model(&list).Where(" status = 1")
  5440. if good_id > 0 {
  5441. db = db.Where("good_id = ?", good_id)
  5442. }
  5443. if len(keyword) > 0 {
  5444. likeKey := "%" + keyword + "%"
  5445. db = db.Where("warehousing_order like ?", likeKey)
  5446. }
  5447. if inventory_type > 0 {
  5448. db = db.Where("inventory_type = ?", inventory_type)
  5449. }
  5450. if inventory_type == 0 {
  5451. db = db.Where("inventory_type = 4")
  5452. }
  5453. err = db.Find(&list).Error
  5454. return list, err
  5455. }
  5456. func GetCancelStockId(cancelstock_id int64, drug_id int64) (*models.DrugFlow, error) {
  5457. flow := models.DrugFlow{}
  5458. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ?", cancelstock_id, drug_id).Find(&flow).Error
  5459. if err == gorm.ErrRecordNotFound {
  5460. return nil, err
  5461. }
  5462. if err != nil {
  5463. return nil, err
  5464. }
  5465. return &flow, nil
  5466. }
  5467. func GetTotalDrugCancelInfo(batchNumber_id int64, drug_id int64, id int64) (info []*models.DrugCancelStockInfo, err error) {
  5468. err = XTReadDB().Where("batch_number_id = ? and drug_id = ? and status = 1 and id<>?", batchNumber_id, drug_id, id).Find(&info).Error
  5469. return info, err
  5470. }
  5471. func GetLastDrugWarehouseInfo(drugid int64) (models.XtDrugWarehouseInfo, error) {
  5472. info := models.XtDrugWarehouseInfo{}
  5473. err := XTReadDB().Where("drug_id = ? and status =1", drugid).Last(&info).Error
  5474. return info, err
  5475. }
  5476. func GetLastGoodWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5477. info := models.WarehousingInfo{}
  5478. err := XTReadDB().Where("good_id =? and status = 1", goodid).Find(&info).Error
  5479. return info, err
  5480. }
  5481. func GetGoodStockFlowInfo(warehouseid int64, goodid int64) (*models.VmStockFlow, error) {
  5482. flow := models.VmStockFlow{}
  5483. err := XTReadDB().Where("warehousing_detail_id = ? and good_id = ? and status = 1", warehouseid, goodid).Find(&flow).Error
  5484. if err == gorm.ErrRecordNotFound {
  5485. return nil, err
  5486. }
  5487. if err != nil {
  5488. return nil, err
  5489. }
  5490. return &flow, nil
  5491. }
  5492. func GetLastInfoMationById(id int64) (models.GoodInfo, error) {
  5493. info := models.GoodInfo{}
  5494. err := XTReadDB().Where("id =? and status = 1", id).Find(&info).Error
  5495. return info, err
  5496. }
  5497. func UpdateGoodInfoMation(goodid int64, info models.GoodInfo) error {
  5498. err := XTWriteDB().Model(&info).Where("id =? and status = 1", goodid).Updates(map[string]interface{}{"total": info.Total}).Error
  5499. return err
  5500. }
  5501. func UpdateWarehouseInfoByGoodId(info models.WarehousingInfo, id int64) error {
  5502. err := XTWriteDB().Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount}).Error
  5503. return err
  5504. }
  5505. func UpdateWarehouseInfoByGoodIdOne(info *models.WarehousingInfo, id int64) error {
  5506. err := XTWriteDB().Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount, "price": info.Price, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "license_number": info.LicenseNumber, "manufacturer": info.Manufacturer, "dealer": info.Dealer, "remark": info.Remark, "packing_price": info.PackingPrice, "storehouse_id": info.StorehouseId, "register_number": info.RegisterNumber, "good_id": info.GoodId}).Error
  5507. return err
  5508. }
  5509. func UpdatedStockFlowByGoodId(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  5510. err := XTWriteDB().Model(&flow).Where("warehousing_id = ? and good_id = ? and status = 1", warehousing_id, good_id).Updates(map[string]interface{}{"count": flow.Count}).Error
  5511. return err
  5512. }
  5513. func UpdatedStockFlowByGoodIdOne(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  5514. err := XTWriteDB().Model(&flow).Where("warehousing_id = ? and good_id = ? and status = 1", warehousing_id, good_id).Updates(map[string]interface{}{"count": flow.Count, "number": flow.Number, "product_date": flow.ProductDate, "expire_date": flow.ExpireDate, "Price": flow.Price, "manufacturer": flow.Manufacturer, "dealer": flow.Dealer, "license_number": flow.LicenseNumber, "storehouse_id": flow.StorehouseId, "register_number": flow.RegisterNumber}).Error
  5515. return err
  5516. }
  5517. func UpdateStockFlowByDetailId(flow models.VmStockFlow, wareoutdetailid int64) error {
  5518. err := XTWriteDB().Model(&flow).Where("warehouse_out_detail_id = ? and status = 1", wareoutdetailid).Updates(map[string]interface{}{"count": flow.Count, "number": flow.Number, "product_date": flow.ProductDate, "expire_date": flow.ExpireDate, "Price": flow.Price, "manufacturer": flow.Manufacturer, "dealer": flow.Dealer, "license_number": flow.LicenseNumber, "price": flow.Price, "storehouse_id": flow.StorehouseId, "admin_user_id": flow.AdminUserId, "stock_count": flow.StockCount, "register_number": flow.RegisterNumber}).Error
  5519. return err
  5520. }
  5521. func UpdatedGoodInfo(info models.GoodInfo, goodid int64) error {
  5522. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", goodid).Updates(map[string]interface{}{"manufacturer": info.Manufacturer, "dealer": info.Dealer}).Error
  5523. return err
  5524. }
  5525. func GetInventoryExportList(orgid int64) (info []*models.PatientWarehouseInfo, err error) {
  5526. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5527. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5528. fmt.Println(table)
  5529. if orgid > 0 {
  5530. db = db.Where("x.org_id = ?", orgid)
  5531. }
  5532. err = db.Select("x.id,x.good_id,x.number,x.number,x.product_date,x.expiry_date,x.warehousing_unit,x.dealer,x.manufacturer,t.good_name,t.specification_name,t.packing_unit,t.remark").Joins("left join xt_good_information as t on t.id = x.good_id").Group("x.good_id,x.number").Scan(&info).Error
  5533. return info, err
  5534. }
  5535. func UpdateDrugWarehouseingInfoSix(id int64, info models.XtDrugWarehouseInfo) error {
  5536. warehouseInfo := models.XtDrugWarehouseInfo{}
  5537. err := XTWriteDB().Model(&warehouseInfo).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "batch_number": info.BatchNumber, "max_unit": info.MaxUnit, "storehouse_id": info.StorehouseId}).Error
  5538. return err
  5539. }
  5540. func CreatedCancelStock(info models.DrugCancelStockInfo) error {
  5541. err := XTWriteDB().Create(&info).Error
  5542. return err
  5543. }
  5544. func CreatedNewCancelStock(info models.DrugCancelStockInfo, tx *gorm.DB) error {
  5545. err := tx.Create(&info).Error
  5546. if err != nil {
  5547. tx.Rollback()
  5548. return err
  5549. }
  5550. return err
  5551. }
  5552. func IsExsitStockFlow(goodid int64, cancel_order_number string) (*models.VmStockFlow, error) {
  5553. flow := models.VmStockFlow{}
  5554. err := XTReadDB().Where("good_id = ? and cancel_order_number = ? and status = 1", goodid, cancel_order_number).Find(&flow).Error
  5555. if err == gorm.ErrRecordNotFound {
  5556. return nil, err
  5557. }
  5558. if err != nil {
  5559. return nil, err
  5560. }
  5561. return &flow, nil
  5562. }
  5563. func UpdateStockFlowTwo(id int64, flow models.VmStockFlow) error {
  5564. err := XTWriteDB().Model(&flow).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"count": flow.Count}).Error
  5565. return err
  5566. }
  5567. func UpdateStockFlowThree(id int64, flow models.VmStockFlow) error {
  5568. err := XTWriteDB().Model(&flow).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"count": flow.Count, "manufacturer": flow.Count, "dealer": flow.Dealer, "number": flow.Number, "license_number": flow.LicenseNumber, "price": flow.Price, "product_date": flow.ProductDate, "expire_date": flow.ExpireDate, "storehouse_id": flow.StorehouseId}).Error
  5569. return err
  5570. }
  5571. func GetLastWarehouseOutInfoById(drugid int64) (models.DrugWarehouseOutInfo, error) {
  5572. flow := models.DrugWarehouseOutInfo{}
  5573. err := XTReadDB().Where("drug_id = ? and status = 1", drugid).Last(&flow).Error
  5574. return flow, err
  5575. }
  5576. func GetLastWarehoseByGoodId(goodid int64, patient_id int64, record_date int64) (models.WarehouseOutInfo, error) {
  5577. info := models.WarehouseOutInfo{}
  5578. err := XTReadDB().Model(&info).Where("good_id = ? and patient_id = ? and sys_record_time = ? and status = 1", goodid, patient_id, record_date).First(&info).Error
  5579. return info, err
  5580. }
  5581. func ModifyDrugWarehouseOutInfo(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64) error {
  5582. err := XTWriteDB().Model(&info).Where("drug_id = ? and patient_id = ? and sys_record_time = ? and status = 1", drugid, patientid, sys_record_time).Updates(map[string]interface{}{"count": info.Count, "count_unit": info.CountUnit}).Error
  5583. return err
  5584. }
  5585. func ModifyDrugWarehouseOutInfoOne(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64, warehouse_info_id int64) error {
  5586. err := XTWriteDB().Model(&info).Where("drug_id = ? and patient_id = ? and sys_record_time = ? and status = 1 and warehouse_info_id = ?", drugid, patientid, sys_record_time, warehouse_info_id).Updates(map[string]interface{}{"count": info.Count, "count_unit": info.CountUnit}).Error
  5587. return err
  5588. }
  5589. func GetDrugWarehouseOutInfoByWarehosueId(drugid int64, patientid int64, recorddate int64, warehosueinfoid int64) (*models.DrugWarehouseOutInfo, error) {
  5590. info := models.DrugWarehouseOutInfo{}
  5591. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and sys_record_time = ? and warehouse_info_id = ? and status = 1", drugid, patientid, recorddate, warehosueinfoid).Find(&info).Error
  5592. if err == gorm.ErrRecordNotFound {
  5593. return nil, err
  5594. }
  5595. if err != nil {
  5596. return nil, err
  5597. }
  5598. return &info, nil
  5599. }
  5600. func GetDrugFlowRecord(warehouse_out_order_number string, drugid int64, patientid int64) (*models.DrugFlow, error) {
  5601. drugFlow := models.DrugFlow{}
  5602. err := XTReadDB().Where("warehouse_out_order_number = ? and drug_id =? and patient_id = ? and status = 1", warehouse_out_order_number, drugid, patientid).Find(&drugFlow).Error
  5603. if err == gorm.ErrRecordNotFound {
  5604. return nil, err
  5605. }
  5606. if err != nil {
  5607. return nil, err
  5608. }
  5609. return &drugFlow, nil
  5610. }
  5611. func ModifyDrugFlow(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64) error {
  5612. err := XTWriteDB().Model(&flow).Where("warehouse_out_order_number = ? and drug_id = ? and patient_id = ? and status = 1", warehouse_out_order_number, drugid, patient_id).Updates(map[string]interface{}{"count": flow.Count, "max_unit": flow.MaxUnit}).Error
  5613. return err
  5614. }
  5615. func ModifyDrugFlowOne(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64, warehousing_detail_id int64) error {
  5616. err := XTWriteDB().Model(&flow).Where("warehouse_out_order_number = ? and drug_id = ? and patient_id = ? and status = 1 and warehousing_detail_id = ?", warehouse_out_order_number, drugid, patient_id, warehousing_detail_id).Updates(map[string]interface{}{"count": flow.Count, "max_unit": flow.MaxUnit}).Error
  5617. return err
  5618. }
  5619. func GetDrugAutoReduceRecordOne(drugid int64, patientid int64, recorddate int64) (detail []*models.DrugAutomaticReduceDetail, err error) {
  5620. err = XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  5621. return detail, err
  5622. }
  5623. func GetDrugAutoReduceRecord(drugid int64, patientid int64, recorddate int64) (*models.DrugAutomaticReduceDetail, error) {
  5624. detail := models.DrugAutomaticReduceDetail{}
  5625. err := XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  5626. if err == gorm.ErrRecordNotFound {
  5627. return nil, err
  5628. }
  5629. if err != nil {
  5630. return nil, err
  5631. }
  5632. return &detail, nil
  5633. }
  5634. func ModifyAutoMaticReduce(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64) error {
  5635. err := XTWriteDB().Model(&auto).Where("patient_id = ? and record_time = ? and drug_id = ? and status = 1", patientid, recordate, drugid).Updates(map[string]interface{}{"count": auto.Count, "count_unit": auto.CountUnit}).Error
  5636. return err
  5637. }
  5638. func ModifyAutoMaticReduceOne(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64, warehouse_info_id int64) error {
  5639. err := XTWriteDB().Model(&auto).Where("patient_id = ? and record_time = ? and drug_id = ? and status = 1 and warehouse_info_id = ?", patientid, recordate, drugid, warehouse_info_id).Updates(map[string]interface{}{"count": auto.Count, "count_unit": auto.CountUnit}).Error
  5640. return err
  5641. }
  5642. func ModifyDrugWarehouse(id int64, stock_max_number int64, stock_min_number int64) (models.DrugWarehouseInfo, error) {
  5643. info := models.DrugWarehouseInfo{}
  5644. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  5645. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  5646. return info, err
  5647. }
  5648. func ModeifyDrugWarehosueInfo(id int64, stock_max_number int64) error {
  5649. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  5650. return err
  5651. }
  5652. func ModeFyStockMinById(id int64, stock_min_number int64) error {
  5653. info := models.DrugWarehouseInfo{}
  5654. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  5655. return err
  5656. }
  5657. func GetDrugCancelById(id int64) (models.DrugCancelStockInfo, error) {
  5658. info := models.DrugCancelStockInfo{}
  5659. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5660. return info, err
  5661. }
  5662. func ModeFyDrugWareseInfo(id int64, stock_max_number int64) error {
  5663. info := models.DrugWarehouseInfo{}
  5664. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", stock_max_number)).Error
  5665. return err
  5666. }
  5667. func ModeFyDrugWarehouseMin(id int64, stock_min_number int64) error {
  5668. info := models.DrugWarehouseInfo{}
  5669. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", stock_min_number)).Error
  5670. return err
  5671. }
  5672. func GetAllHisDoctorAdviceById(drugid int64, patient_id int64, advicedate int64, orgid int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  5673. err = XTReadDB().Where("drug_id = ? and patient_id = ? and advice_date = ? and execution_state = 1 and status = 1 and user_org_id = ?", drugid, patient_id, advicedate, orgid).Find(&advice).Error
  5674. return advice, err
  5675. }
  5676. func GetAllDrugFlowById(drugid int64, patient_id int64, advicedate int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  5677. err = XTReadDB().Where("drug_id = ? and patient_id = ? and sys_record_time = ? and org_id = ? and status = 1", drugid, patient_id, advicedate, orgid).Find(&info).Error
  5678. return info, err
  5679. }
  5680. // 耗材加库存
  5681. func ModifyGoodAddInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5682. info := models.GoodInfo{}
  5683. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  5684. return info, err
  5685. }
  5686. func ModifyGoodAddInformationSix(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5687. info := models.GoodInfo{}
  5688. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  5689. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count + ?", sum_count)).Error
  5690. return info, err
  5691. }
  5692. func ModifyGoodReduceInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5693. info := models.GoodInfo{}
  5694. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count - ?", sum_count)).Error
  5695. return info, err
  5696. }
  5697. func ModifyGoodReduceInformationTwo(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5698. info := models.GoodInfo{}
  5699. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count - ?", sum_count)).Error
  5700. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count - ?", sum_count)).Error
  5701. return info, err
  5702. }
  5703. func ModifyGoodReduceInformationSix(good_id int64, sum_count int64, orgid int64, retail_price float64) (models.GoodInfo, error) {
  5704. info := models.GoodInfo{}
  5705. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Update(map[string]interface{}{"batch_retai_price": retail_price}).Error
  5706. return info, err
  5707. }
  5708. func GetWarehousingByIdList(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  5709. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  5710. return info, err
  5711. }
  5712. func GetWarehousingByIdListTwenty(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  5713. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  5714. return info, err
  5715. }
  5716. func GetWarehouseInfoByIdSix(id int64, orgid int64) (models.WarehousingInfo, error) {
  5717. info := models.WarehousingInfo{}
  5718. err := XTReadDB().Model(&info).Where("id = ? and status= 1 and org_id = ?", id, orgid).Find(&info).Error
  5719. return info, err
  5720. }
  5721. func GetWarehouseInfoByIdTwenty(id int64, orgid int64) (models.WarehousingInfo, error) {
  5722. info := models.WarehousingInfo{}
  5723. err := XTReadDB().Model(&info).Where("id = ? and org_id = ?", id, orgid).Find(&info).Error
  5724. return info, err
  5725. }
  5726. // 药品加库存
  5727. func ModifyDrugAddInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5728. info := models.BaseDrugLib{}
  5729. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  5730. return info, err
  5731. }
  5732. func ModifyDrugAddInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5733. info := models.BaseDrugLib{}
  5734. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  5735. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count + ?", sum_count)).Error
  5736. return info, err
  5737. }
  5738. func ModifyDrugReduceInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5739. info := models.BaseDrugLib{}
  5740. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count - ?", sum_count)).Error
  5741. return info, err
  5742. }
  5743. func ModifyDrugReduceInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5744. info := models.BaseDrugLib{}
  5745. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count - ?", sum_count)).Error
  5746. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count - ?", sum_count)).Error
  5747. return info, err
  5748. }
  5749. func ModifyDrugReduceInformationSix(drug_id int64, sum_count int64, orgid int64, batch_retai_price float64) (models.BaseDrugLib, error) {
  5750. info := models.BaseDrugLib{}
  5751. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count - ?", sum_count)).Error
  5752. err = XTWriteDB().Model(&info).Where("id=? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"batch_retai_price": batch_retai_price}).Error
  5753. return info, err
  5754. }
  5755. func GetLastDrugwarehouseInfoById(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5756. err = XTReadDB().Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  5757. return info, err
  5758. }
  5759. func GetLastDrugwarehouseInfoByIdTwenty(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5760. err = XTReadDB().Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  5761. return info, err
  5762. }
  5763. func GetWarhouseOutById(id int64) (models.WarehouseOutInfo, error) {
  5764. info := models.WarehouseOutInfo{}
  5765. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5766. return info, err
  5767. }
  5768. func FindeGoodWareHouseOutInfo(id int64, storehouse_id int64, orgid int64) (out []*models.WarehouseOutInfo, err error) {
  5769. err = XTReadDB().Where("warehouse_out_id = ? and storehouse_id =? and org_id = ?", id, storehouse_id, orgid).Find(&out).Error
  5770. return out, err
  5771. }
  5772. func ModifyWarehouseInfo(id int64, count int64) (models.WarehousingInfo, error) {
  5773. info := models.WarehousingInfo{}
  5774. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  5775. return info, err
  5776. }
  5777. func GetOrgIdList() (template []*models.VMGobalTemplate, err error) {
  5778. err = XTReadDB().Where("status = 1").Find(&template).Error
  5779. return template, err
  5780. }
  5781. func GetAllGoodList(orgid int64) (good []*models.GoodInfo, err error) {
  5782. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&good).Error
  5783. return good, err
  5784. }
  5785. func ModifyGoodAddInformationSeven(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5786. info := models.GoodInfo{}
  5787. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  5788. return info, err
  5789. }
  5790. func GetStockCountWarehouseInfoById(good_id int64) (info []*models.WarehousingInfo, err error) {
  5791. db := XTReadDB().Where("status = 1")
  5792. if good_id > 0 {
  5793. db = db.Where("good_id = ?", good_id)
  5794. }
  5795. err = db.Find(&info).Error
  5796. return info, err
  5797. }
  5798. func GetDrugStockCountWarehoseInfoById(drug_id int64) (info []*models.DrugWarehouseInfo, err error) {
  5799. db := XTReadDB().Model(&info).Where("status = 1")
  5800. if drug_id > 0 {
  5801. db = db.Where("drug_id = ?", drug_id)
  5802. }
  5803. err = db.Find(&info).Error
  5804. return info, err
  5805. }
  5806. func UpdateModifyDrugInfoById(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5807. lib := models.BaseDrugLib{}
  5808. err := XTWriteDB().Model(&lib).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  5809. return lib, err
  5810. }
  5811. func GetWarehouseBySecondWarehouseId(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (*models.Warehousing, error) {
  5812. info := models.Warehousing{}
  5813. err := XTReadDB().Where("is_sys= ? and second_warehouse_id=? and status = 1 and org_id = ? and warehousing_time = ?", is_sys, sencond_wahouse_id, orgid, record_date).Find(&info).Error
  5814. if err == gorm.ErrRecordNotFound {
  5815. return nil, err
  5816. }
  5817. if err != nil {
  5818. return nil, err
  5819. }
  5820. return &info, nil
  5821. }
  5822. func GetLastWarehouseById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.Warehousing, error) {
  5823. info := models.Warehousing{}
  5824. err := XTReadDB().Where("is_sys = ? and second_warehouse_id=? and status = 1 and org_id = ? and warehousing_time = ?", is_sys, sencond_wahouse_id, orgid, record_date).Last(&info).Error
  5825. return info, err
  5826. }
  5827. func GetDrugWarehouseBySyS(orgid int64, is_sys int64, sencond_wahouse_id int64, record_date int64) (*models.DrugWarehouseOut, error) {
  5828. out := models.DrugWarehouseOut{}
  5829. err := XTReadDB().Where("org_id = ? and is_sys = ? and second_warehouse_id = ? and warehouse_out_time = ? and status = 1", orgid, is_sys, sencond_wahouse_id, record_date).Find(&out).Error
  5830. if err == gorm.ErrRecordNotFound {
  5831. return nil, err
  5832. }
  5833. if err != nil {
  5834. return nil, err
  5835. }
  5836. return &out, nil
  5837. }
  5838. func GetLastDrugWarehouseOutById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.DrugWarehouseOut, error) {
  5839. info := models.DrugWarehouseOut{}
  5840. err := XTReadDB().Where("is_sys = ? and second_warehouse_id = ? and status = 1 and org_id =? and warehouse_out_time = ?", is_sys, sencond_wahouse_id, orgid, record_date).Last(&info).Error
  5841. return info, err
  5842. }
  5843. func GetDrugWarehouseingBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (*models.DrugWarehouse, error) {
  5844. info := models.DrugWarehouse{}
  5845. err := XTReadDB().Where("is_sys = ? and second_warehouse_id = ? and status = 1 and org_id = ? and warehousing_time = ?", is_sys, second_warehouse_id, orgid, record_date).Find(&info).Error
  5846. if err == gorm.ErrRecordNotFound {
  5847. return nil, err
  5848. }
  5849. if err != nil {
  5850. return nil, err
  5851. }
  5852. return &info, nil
  5853. }
  5854. func CreateDrugWaresing(warehouse models.DrugWarehouse) error {
  5855. err := XTWriteDB().Create(&warehouse).Error
  5856. return err
  5857. }
  5858. func GetLastDrugWarehouseBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (models.DrugWarehouse, error) {
  5859. info := models.DrugWarehouse{}
  5860. err := XTReadDB().Where("is_sys = ? and second_warehouse_id = ? and status = 1 and org_id = ? and warehousing_time = ?", is_sys, second_warehouse_id, orgid, record_date).Last(&info).Error
  5861. return info, err
  5862. }
  5863. func ModifyGoodReduceInformationTwenty(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5864. info := models.GoodInfo{}
  5865. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count - ?", sum_count)).Error
  5866. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count - ?", waresing_count)).Error
  5867. return info, err
  5868. }
  5869. func ModifyGoodReduceInformationTwentyOne(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5870. info := models.GoodInfo{}
  5871. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  5872. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count + ?", waresing_count)).Error
  5873. return info, err
  5874. }
  5875. func UpdateMedicalSumCount(drug_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  5876. tx := XTWriteDB().Begin()
  5877. err := tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count, "sum_in_count": sum_in_count}).Error
  5878. if err != nil {
  5879. tx.Rollback()
  5880. return err
  5881. }
  5882. tx.Commit()
  5883. return err
  5884. }
  5885. //func UpdateMedicalSumCountTX(drug_id int64, sum_count int64, sum_in_count int64, orgid int64,tx *gorm.DB) error {
  5886. // err := tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count, "sum_in_count": sum_in_count}).Error
  5887. // return err
  5888. //}
  5889. func UpdateGoodByGoodId(good_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  5890. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Update(map[string]interface{}{"sum_count": sum_count, "sum_in_count": sum_in_count}).Error
  5891. return err
  5892. }
  5893. func GetDrugDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  5894. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  5895. table := XTReadDB().Table("xt_base_drug as x d")
  5896. fmt.Println(table)
  5897. if startime > 0 {
  5898. db = db.Where("x.advice_date >= ?", startime)
  5899. }
  5900. if endtime > 0 {
  5901. db = db.Where("x.advice_date <= ?", endtime)
  5902. }
  5903. if orgid > 0 {
  5904. db = db.Where("x.user_org_id = ?", orgid)
  5905. }
  5906. if len(keyword) > 0 {
  5907. likeKey := "%" + keyword + "%"
  5908. db = db.Select("x.patient_id,x.user_org_id,x.id,x.advice_date,x.advice_date,x.prescribing_number_unit,x.drug_id,x.prescribing_number").Joins("left join sgj_xt.xt_base_drug as d on d.id = x.drug_id").Where("d.drug_name like ?", likeKey)
  5909. }
  5910. offset := (page - 1) * limit
  5911. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.created_time desc").Group("x.drug_id").Scan(&adviceinfo).Error
  5912. return adviceinfo, total, err
  5913. }
  5914. func GetDrugDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  5915. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  5916. table := XTReadDB().Table("xt_base_drug as x d")
  5917. fmt.Println(table)
  5918. if startime > 0 {
  5919. db = db.Where("x.advice_date >= ?", startime)
  5920. }
  5921. if endtime > 0 {
  5922. db = db.Where("x.advice_date <= ?", endtime)
  5923. }
  5924. if orgid > 0 {
  5925. db = db.Where("x.user_org_id = ?", orgid)
  5926. }
  5927. if len(keyword) > 0 {
  5928. likeKey := "%" + keyword + "%"
  5929. db = db.Select("x.patient_id,x.user_org_id,x.id,x.advice_date,x.advice_date,x.prescribing_number_unit,x.drug_id,x.prescribing_number").Joins("left join sgj_xt.xt_base_drug as d on d.id = x.drug_id").Where("d.drug_name like ?", likeKey)
  5930. }
  5931. err = db.Order("x.created_time desc").Scan(&adviceinfo).Error
  5932. return adviceinfo, total, err
  5933. }
  5934. func GetAllDrugSummary(orgid int64) (drug []*models.BaseDrugLib, err error) {
  5935. err = XTReadDB().Where("org_id = ? and status = 1 and is_user = 1", orgid).Find(&drug).Error
  5936. return drug, err
  5937. }
  5938. func GetGoodDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  5939. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  5940. table := XTReadDB().Table("xt_good_information as d")
  5941. fmt.Println(table)
  5942. if startime > 0 {
  5943. db = db.Where("x.record_date >= ?", startime)
  5944. }
  5945. if endtime > 0 {
  5946. db = db.Where("x.record_date <= ?", endtime)
  5947. }
  5948. if orgid > 0 {
  5949. db = db.Where("x.user_org_id = ?", orgid)
  5950. }
  5951. if len(keyword) > 0 {
  5952. likeKey := "%" + keyword + "%"
  5953. db = db.Select("x.id,x.project_id,x.user_org_id,x.patient_id,x.record_date,x.count").Joins("left join xt_good_information as d on d.id = x.project_id").Where("d.good_name like ?", likeKey).Group("d.id")
  5954. }
  5955. offset := (page - 1) * limit
  5956. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Group("x.project_id").Scan(&project).Error
  5957. return project, total, err
  5958. }
  5959. func GetGoodDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  5960. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  5961. table := XTReadDB().Table("xt_good_information as d")
  5962. fmt.Println(table)
  5963. if startime > 0 {
  5964. db = db.Where("x.record_date >= ?", startime)
  5965. }
  5966. if endtime > 0 {
  5967. db = db.Where("x.record_date <= ?", endtime)
  5968. }
  5969. if orgid > 0 {
  5970. db = db.Where("x.user_org_id = ?", orgid)
  5971. }
  5972. if len(keyword) > 0 {
  5973. likeKey := "%" + keyword + "%"
  5974. db = db.Select("x.id,x.project_id,x.user_org_id,x.patient_id,x.record_date,x.count").Joins("left join xt_good_information as d on d.id = x.project_id").Where("d.good_name like ? ", likeKey).Group("d.id")
  5975. }
  5976. err = db.Order("x.ctime desc").Group("x.id").Scan(&project).Error
  5977. return project, total, err
  5978. }
  5979. func GeAllGoodInformation(orgid int64) (good []*models.GoodInfo, err error) {
  5980. err = XTReadDB().Where("org_id =? and status = 1 and is_user = 1", orgid).Find(&good).Error
  5981. return good, err
  5982. }
  5983. func GetStockSettingIsExsit(orgid int64) (*models.XtStockSetting, error) {
  5984. setting := models.XtStockSetting{}
  5985. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  5986. if err == gorm.ErrRecordNotFound {
  5987. return nil, err
  5988. }
  5989. if err != nil {
  5990. return nil, err
  5991. }
  5992. return &setting, nil
  5993. }
  5994. func CreateStockSetting(setting models.XtStockSetting) error {
  5995. err := XTWriteDB().Create(&setting).Error
  5996. return err
  5997. }
  5998. func UpdateStockSetting(orgid int64, setting models.XtStockSetting) error {
  5999. err := XTWriteDB().Model(&models.XtStockSetting{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_type": setting.IsType}).Error
  6000. return err
  6001. }
  6002. func FindStockSettingById(orgid int64) (models.XtStockSetting, error) {
  6003. setting := models.XtStockSetting{}
  6004. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6005. return setting, err
  6006. }
  6007. func GetAutoStockOutById(good_id int64, record_date int64, orgid int64, patient_id int64) (models.BloodAutomaticReduceDetail, error) {
  6008. detail := models.BloodAutomaticReduceDetail{}
  6009. err := XTReadDB().Where("good_id = ? and record_time = ? and org_id = ? and status = 1 and patient_id = ?", good_id, record_date, orgid, patient_id).Find(&detail).Error
  6010. return detail, err
  6011. }
  6012. func GetAutoWarehouseOutList(good_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfoNight, error) {
  6013. detail := models.WarehouseOutInfoNight{}
  6014. err := XTReadDB().Where("good_id = ? and sys_record_time = ? and org_id = ? and status = 1 and patient_id = ?", good_id, record_date, orgid, patient_id).Find(&detail).Error
  6015. return detail, err
  6016. }
  6017. func GetAdviceConfig(orgid int64) (*models.XtAdviceSetting, error) {
  6018. setting := models.XtAdviceSetting{}
  6019. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6020. if err == gorm.ErrRecordNotFound {
  6021. return nil, err
  6022. }
  6023. if err != nil {
  6024. return nil, err
  6025. }
  6026. return &setting, nil
  6027. }
  6028. func CreateAdviceSetting(setting models.XtAdviceSetting) error {
  6029. err := XTWriteDB().Create(&setting).Error
  6030. return err
  6031. }
  6032. func UpdateAdviceSetting(orgid int64, setting models.XtAdviceSetting) error {
  6033. err := XTWriteDB().Model(&setting).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_advice_open": setting.IsAdviceOpen, "mtime": time.Now().Unix()}).Error
  6034. return err
  6035. }
  6036. func FindAdviceSettingById(orgid int64) (models.XtAdviceSetting, error) {
  6037. setting := models.XtAdviceSetting{}
  6038. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6039. return setting, err
  6040. }
  6041. func GetPrescriptionConfig(orgid int64) (*models.XtPrescriptionConfig, error) {
  6042. config := models.XtPrescriptionConfig{}
  6043. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6044. if err == gorm.ErrRecordNotFound {
  6045. return nil, err
  6046. }
  6047. if err != nil {
  6048. return nil, err
  6049. }
  6050. return &config, nil
  6051. }
  6052. func CreatePrescriptionConfig(prescriptionConfig models.XtPrescriptionConfig) error {
  6053. err := XTWriteDB().Create(&prescriptionConfig).Error
  6054. return err
  6055. }
  6056. func UpdatePrescriptionConfig(orgid int64, config models.XtPrescriptionConfig) error {
  6057. err := XTWriteDB().Model(&config).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": config.IsOpen, "mtime": time.Now().Unix()}).Error
  6058. return err
  6059. }
  6060. func FindPrescriptionConfigById(orgid int64) (models.XtPrescriptionConfig, error) {
  6061. config := models.XtPrescriptionConfig{}
  6062. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6063. return config, err
  6064. }
  6065. func UpdateWarehouseInfoTwentyFive(maxNumber int64, drugid int64, orgID int64, storehouse_id int64, drup *models.XtBaseDrug) (err error) {
  6066. var deliver_number int64 = 0
  6067. var stock_number int64 = 0
  6068. deliver_number = maxNumber
  6069. // 根据先进先出原则,查询最先入库的批次,进行出库
  6070. // 如果没有对应的库存,则报错
  6071. //开启事物
  6072. warehouse, err := FindLastDrugWarehousingInfoByID(drugid, storehouse_id)
  6073. if err != nil {
  6074. return err
  6075. }
  6076. // 将该批次的剩余库存数量转换为拆零数量
  6077. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  6078. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  6079. if stock_number >= deliver_number {
  6080. var maxNumber int64 = 0
  6081. var minNumber int64 = 0
  6082. var stock_max_number int64 = 0
  6083. stock_max_number = warehouse.StockMaxNumber
  6084. maxNumber = deliver_number / drup.MinNumber
  6085. minNumber = deliver_number % drup.MinNumber
  6086. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  6087. minNumber = maxNumber
  6088. }
  6089. if drup.MaxUnit != drup.MinUnit {
  6090. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  6091. return errors.New("库存数量不足")
  6092. }
  6093. }
  6094. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  6095. if warehouse.StockMaxNumber < 0 {
  6096. warehouse.StockMaxNumber = 0
  6097. }
  6098. if warehouse.StockMinNumber < 0 {
  6099. warehouse.StockMinNumber = 0
  6100. }
  6101. warehouse.Mtime = time.Now().Unix()
  6102. if warehouse.StockMinNumber < minNumber {
  6103. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  6104. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  6105. } else {
  6106. if minNumber > 0 {
  6107. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  6108. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6109. } else {
  6110. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  6111. }
  6112. }
  6113. if minNumber == 0 && maxNumber != 1 {
  6114. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6115. }
  6116. }
  6117. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  6118. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6119. if warehouse.StockMinNumber == 0 {
  6120. warehouse.StockMaxNumber = stock_max_number
  6121. }
  6122. }
  6123. if drup.MaxUnit != drup.MinUnit {
  6124. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  6125. return errors.New("库存数量不足")
  6126. }
  6127. }
  6128. if warehouse.StockMinNumber <= 0 {
  6129. warehouse.StockMinNumber = 0
  6130. }
  6131. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6132. fmt.Println(errThree)
  6133. //查询默认仓库
  6134. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6135. //查询默认仓库剩余多少库存
  6136. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6137. var sum_count int64
  6138. var sum_in_count int64
  6139. for _, it := range list {
  6140. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6141. if it.MaxUnit == baseDrug.MaxUnit {
  6142. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6143. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6144. sum_count += it.StockMaxNumber + it.StockMinNumber
  6145. sum_in_count += it.WarehousingCount
  6146. }
  6147. }
  6148. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6149. return nil
  6150. } else {
  6151. warehouse.StockMaxNumber = 0
  6152. warehouse.StockMinNumber = 0
  6153. warehouse.Mtime = time.Now().Unix()
  6154. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6155. fmt.Println(errThree)
  6156. //查询默认仓库
  6157. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6158. //查询默认仓库剩余多少库存
  6159. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6160. var sum_count int64
  6161. var sum_in_count int64
  6162. for _, it := range list {
  6163. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6164. if it.MaxUnit == baseDrug.MaxUnit {
  6165. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6166. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6167. sum_count += it.StockMaxNumber + it.StockMinNumber
  6168. sum_in_count += it.WarehousingCount
  6169. }
  6170. }
  6171. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6172. if errThree != nil {
  6173. return errThree
  6174. }
  6175. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  6176. prescribingNumber_two_temp := deliver_number - stock_number
  6177. UpdateWarehouseInfoTwentyFive(prescribingNumber_two_temp, drugid, orgID, storehouse_id, drup)
  6178. }
  6179. return
  6180. }
  6181. func UpdateWarehouseInfoTentyFour(count int64, id int64) error {
  6182. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6183. return err
  6184. }
  6185. func UpdateWarehouseInfoTentyThree(count int64, id int64) error {
  6186. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6187. return err
  6188. }
  6189. func GetDrugOpenConfig(orgid int64) (*models.DrugOutConfig, error) {
  6190. config := models.DrugOutConfig{}
  6191. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6192. if err == gorm.ErrRecordNotFound {
  6193. return nil, err
  6194. }
  6195. if err != nil {
  6196. return nil, err
  6197. }
  6198. return &config, nil
  6199. }
  6200. func GetDrugOpenConfigOne(orgid int64) (models.DrugOutConfig, error) {
  6201. config := models.DrugOutConfig{}
  6202. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).First(&config).Error
  6203. return config, err
  6204. }
  6205. func FindXTHisRecordByOrgIdSix(org_id int64) (models.XtHisConfig, error) {
  6206. config := models.XtHisConfig{}
  6207. err = readDb.Model(&models.XtHisConfig{}).Where("status = 1 AND user_org_id = ?", org_id).Find(&config).Error
  6208. return config, err
  6209. }
  6210. func CreateDrugOutConfig(config models.DrugOutConfig) error {
  6211. err := XTWriteDB().Create(&config).Error
  6212. return err
  6213. }
  6214. func UpdateDrugOutConfig(orgid int64, drug_out_open int64) error {
  6215. err := XTWriteDB().Model(&models.DrugOutConfig{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": drug_out_open}).Error
  6216. return err
  6217. }
  6218. func FindDrugOutConfigById(orgid int64) (models.DrugOutConfig, error) {
  6219. config := models.DrugOutConfig{}
  6220. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6221. return config, err
  6222. }
  6223. func FindeDrugSettleConfigById(orgid int64) (models.HisSettleStockConfig, error) {
  6224. config := models.HisSettleStockConfig{}
  6225. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6226. return config, err
  6227. }
  6228. func UpdatedWarehouseOut(id int64) error {
  6229. err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6230. err = XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6231. return err
  6232. }
  6233. func UpdatedSigleWarehouseOutInfo(id int64, info *models.WarehouseOutInfo) error {
  6234. tx := XTWriteDB().Begin()
  6235. err := tx.Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_info_id": info.WarehouseInfotId}).Error
  6236. if err != nil {
  6237. tx.Rollback()
  6238. return err
  6239. }
  6240. tx.Commit()
  6241. return err
  6242. }
  6243. func GetLastGoodWarehouseOutInfo(orgid int64, id int64) (info []*models.WarehouseOutInfo, err error) {
  6244. err = XTReadDB().Where("org_id = ? and status = 1 and warehouse_out_id = ?", orgid, id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  6245. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  6246. }).Find(&info).Error
  6247. return info, err
  6248. }
  6249. func GetLastGoodWarehouseOutInfoById(orgid int64) (models.WarehouseOutInfo, error) {
  6250. info := models.WarehouseOutInfo{}
  6251. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&info).Error
  6252. return info, err
  6253. }
  6254. func ModifyWarehouseOutInfo(id int64, info *models.WarehouseOutInfo) error {
  6255. err := XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"good_id": info.GoodId, "good_type_id": info.GoodTypeId, "count": info.Count, "price": info.Price, "total_price": info.TotalPrice, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "number": info.Number, "license_number": info.LicenseNumber, "storehouse_id": info.StorehouseId, "admin_user_id": info.AdminUserId, "buy_price": info.BuyPrice, "stock_count": info.StockCount, "register_number": info.RegisterNumber}).Error
  6256. return err
  6257. }
  6258. func UpdateWarehouseOutFlow(info models.VmStockFlow, id int64) error {
  6259. err := XTWriteDB().Model(&models.VmStockFlow{}).Where("warehouse_out_detail_id = ? and status = 1", id).Update(map[string]interface{}{"good_id": info.GoodId, "number": info.Number, "license_number": info.LicenseNumber, "count": info.Count, "manufacturer": info.Manufacturer, "dealer": info.Dealer, "price": info.Price, "expire_date": info.ExpireDate, "product_date": info.ProductDate, "storehouse_id": info.StorehouseId, "admin_user_id": info.AdminUserId, "buy_price": info.BuyPrice, "stock_count": info.StockCount}).Error
  6260. return err
  6261. }
  6262. func GetLastGoodWarehouseOutTwenty(orgid int64) (models.WarehouseOut, error) {
  6263. out := models.WarehouseOut{}
  6264. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  6265. return out, err
  6266. }
  6267. func UpdateaGoodWarehouseInfo(count int64, good_id int64, orgid int64, warehouse_info_id int64) error {
  6268. ut := writeDb.Begin()
  6269. err := ut.Model(&models.WarehousingInfo{}).Where("good_id = ? and org_id = ? and status = 1 and id = ?", good_id, orgid, warehouse_info_id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  6270. if err != nil {
  6271. ut.Rollback()
  6272. return err
  6273. }
  6274. ut.Commit()
  6275. return err
  6276. }
  6277. func ModifyGoodWarehouseOut(id int64) error {
  6278. ut := writeDb.Begin()
  6279. err := ut.Model(&models.WarehouseOut{}).Where("id = ? and status =1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6280. if err != nil {
  6281. ut.Rollback()
  6282. return err
  6283. }
  6284. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6285. if err != nil {
  6286. ut.Rollback()
  6287. return err
  6288. }
  6289. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  6290. if err != nil {
  6291. ut.Rollback()
  6292. return err
  6293. }
  6294. err = ut.Model(&models.XtWarehouseFlushInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  6295. if err != nil {
  6296. ut.Rollback()
  6297. return err
  6298. }
  6299. ut.Commit()
  6300. return err
  6301. }
  6302. func GetGoodWarehouseInfoById(waresing_id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  6303. err = XTReadDB().Where("org_id = ? and status = 1 and warehousing_id = ?", orgid, waresing_id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  6304. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  6305. }).Find(&info).Error
  6306. return info, err
  6307. }
  6308. func FindLastWarehousing(order string) (info models.Warehousing, err error) {
  6309. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  6310. return info, err
  6311. }
  6312. func UpdateWarehouseDetail(info *models.WarehousingInfo, id int64) error {
  6313. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"good_id": info.GoodId, "good_type_id": info.GoodTypeId, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "warehousing_count": info.WarehousingCount, "warehousing_unit": info.WarehousingUnit, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "license_number": info.LicenseNumber, "packing_price": info.PackingPrice, "register_number": info.RegisterNumber, "storehouse_id": info.StorehouseId}).Error
  6314. return err
  6315. }
  6316. func UpdateCheckWarehouseInfo(id int64, checker int64) error {
  6317. tx := XTWriteDB().Begin()
  6318. err := tx.Model(&models.Warehousing{}).Where("id =? and status = 1", id).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  6319. if err != nil {
  6320. tx.Rollback()
  6321. return err
  6322. }
  6323. tx.Commit()
  6324. return err
  6325. }
  6326. func GetWarehouseInfoList(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  6327. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  6328. return info, err
  6329. }
  6330. func UpdateWarehouseInfoByIdListThree(count int64, id int64) error {
  6331. tx := XTWriteDB().Begin()
  6332. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"stock_count": count}).Error
  6333. if err != nil {
  6334. tx.Rollback()
  6335. return err
  6336. }
  6337. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6338. if err != nil {
  6339. tx.Rollback()
  6340. return err
  6341. }
  6342. tx.Commit()
  6343. return err
  6344. }
  6345. func FindLastWarehousingSix(org_id int64) (info models.Warehousing, err error) {
  6346. err = readDb.Model(&models.Warehousing{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  6347. return info, err
  6348. }
  6349. func FindLastWarehousingSeven(org_id int64) (info models.DrugWarehouse, err error) {
  6350. err = readDb.Model(&models.DrugWarehouse{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  6351. return info, err
  6352. }
  6353. func FindLastDrugWarehousingInfoTwenty(org_id int64) (info models.DrugWarehouseInfo, err error) {
  6354. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  6355. return info, err
  6356. }
  6357. func FindDrugWarehouseInfoListById(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  6358. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "status = 1 and org_id =?", orgid).Find(&info).Error
  6359. return info, err
  6360. }
  6361. func CreatedWarehouseing(info *models.DrugWarehouseInfo) error {
  6362. err := XTWriteDB().Create(&info).Error
  6363. return err
  6364. }
  6365. func UpdateWarehouseing(info *models.DrugWarehouseInfo) error {
  6366. err := XTWriteDB().Save(&info).Error
  6367. return err
  6368. }
  6369. func CheckWarehousingInfo(id int64, orgid int64, checker int64) error {
  6370. tx := XTWriteDB().Begin()
  6371. err := tx.Model(&models.DrugWarehouse{}).Where("id = ? and org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  6372. if err != nil {
  6373. tx.Rollback()
  6374. return err
  6375. }
  6376. tx.Commit()
  6377. return err
  6378. }
  6379. func GetWarehousingInfoByList(id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  6380. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  6381. return info, err
  6382. }
  6383. func AddDrugWarehouseStockMaxNumber(count int64, id int64) error {
  6384. tx := XTWriteDB().Begin()
  6385. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6386. if err != nil {
  6387. tx.Rollback()
  6388. return err
  6389. }
  6390. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6391. if err != nil {
  6392. tx.Rollback()
  6393. return err
  6394. }
  6395. tx.Commit()
  6396. return err
  6397. }
  6398. func UpdateWarehousingInfoFlow(id int64, orgid int64) error {
  6399. tx := XTWriteDB().Begin()
  6400. err := tx.Model(&models.DrugFlow{}).Where("warehousing_detail_id = ? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"status": 0}).Error
  6401. if err != nil {
  6402. tx.Rollback()
  6403. return err
  6404. }
  6405. tx.Commit()
  6406. return err
  6407. }
  6408. func AddDrugWarehouseStockMinNumber(count int64, id int64) error {
  6409. tx := XTWriteDB().Begin()
  6410. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6411. if err != nil {
  6412. tx.Rollback()
  6413. return err
  6414. }
  6415. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6416. tx.Commit()
  6417. return err
  6418. }
  6419. func ReturnCheckWarehouseingInfo(id int64, orgid int64) error {
  6420. tx := XTWriteDB().Begin()
  6421. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  6422. if err != nil {
  6423. tx.Rollback()
  6424. return err
  6425. }
  6426. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("warehousing_id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  6427. if err != nil {
  6428. tx.Rollback()
  6429. return err
  6430. }
  6431. tx.Commit()
  6432. return err
  6433. }
  6434. func AddDrugReturnWarehouseStockMaxNumber(count int64, id int64) error {
  6435. tx := XTWriteDB().Begin()
  6436. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6437. if err != nil {
  6438. tx.Rollback()
  6439. return err
  6440. }
  6441. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6442. if err != nil {
  6443. tx.Rollback()
  6444. return err
  6445. }
  6446. tx.Commit()
  6447. return err
  6448. }
  6449. func AddDrugReturnWarehouseStockMinNumber(count int64, id int64) error {
  6450. tx := XTWriteDB().Begin()
  6451. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  6452. if err != nil {
  6453. tx.Rollback()
  6454. return err
  6455. }
  6456. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6457. if err != nil {
  6458. tx.Rollback()
  6459. return err
  6460. }
  6461. tx.Commit()
  6462. return err
  6463. }
  6464. func UpdateDrugWarehouseInfoTwenty(id int64, info *models.DrugWarehouseInfo) error {
  6465. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "warehousing_count": info.WarehousingCount, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "retail_price": info.RetailPrice, "max_unit": info.MaxUnit, "min_unit": info.MinUnit}).Error
  6466. return err
  6467. }
  6468. func UpdatedDrugWarehouseInfo(info *models.DrugWarehouseOutInfo, id int64) error {
  6469. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "number": info.Number, "batch_number": info.BatchNumber, "admin_user_id": info.AdminUserId, "last_price": info.LastPrice, "stock_count": info.StockCount, "storehouse_id": info.StorehouseId, "patient_id": info.PatientId}).Error
  6470. return err
  6471. }
  6472. func GetDrugLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  6473. out := models.DrugWarehouseOut{}
  6474. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  6475. return out, err
  6476. }
  6477. func GetDrugWarehouseOutListById(warehouse_out_id int64, orgId int64) (info []*models.DrugWarehouseOutInfo, err error) {
  6478. err = XTReadDB().Where("warehouse_out_id = ? and org_id =? and status = 1", warehouse_out_id, orgId).Preload("BaseDrugLib", "status=1").Find(&info).Error
  6479. return info, err
  6480. }
  6481. func UpdateCheckDrugOut(out models.DrugWarehouseOut, id int64) error {
  6482. tx := XTWriteDB().Begin()
  6483. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": out.IsCheck}).Error
  6484. if err != nil {
  6485. tx.Rollback()
  6486. return err
  6487. }
  6488. err = XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6489. if err != nil {
  6490. tx.Rollback()
  6491. return err
  6492. }
  6493. tx.Commit()
  6494. return err
  6495. }
  6496. func UpdateCheckDrugOutInfoList(info models.DrugWarehouseOutInfo, warehouse_out_id int64) error {
  6497. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", warehouse_out_id).Update(map[string]interface{}{"is_check": info.IsCheck}).Error
  6498. return err
  6499. }
  6500. func GetDrugWarehouseOutByLastId(id int64) (models.DrugWarehouseOut, error) {
  6501. out := models.DrugWarehouseOut{}
  6502. err := XTReadDB().Where("id = ? and status = 1", id).Find(&out).Error
  6503. return out, err
  6504. }
  6505. func UpdateDrugMaxNumber(count int64, id int64) error {
  6506. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6507. return err
  6508. }
  6509. func UpdateDrugMinNumber(count int64, id int64) error {
  6510. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  6511. return err
  6512. }
  6513. func DeleteDrugOutFlow(id int64, orgid int64) error {
  6514. err := XTWriteDB().Model(models.DrugFlow{}).Where("warehouse_out_detail_id = ? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"status": 0}).Error
  6515. return err
  6516. }
  6517. func GetWarehouseOutByWarehouseInfoId(warehouse_info_id int64, good_id int64, orgid int64) (outInfo []*models.WarehouseOutInfo, err error) {
  6518. err = XTReadDB().Where("warehouse_info_id = ? and good_id = ? and org_id = ? and is_check = 1 and status = 1", warehouse_info_id, good_id, orgid).Find(&outInfo).Error
  6519. return outInfo, err
  6520. }
  6521. func GetDrugWarehouseOutByWarehouseInfoId(warehouse_info_id int64, drug_id int64, orgid int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  6522. err = XTReadDB().Where("warehouse_info_id = ? and drug_id = ? and org_id = ? and is_check = 1 and status = 1", warehouse_info_id, drug_id, orgid).Find(&outInfo).Error
  6523. return outInfo, err
  6524. }
  6525. func GetAllStockInList(id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  6526. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("GoodInfo", "status = 1 and org_id =?", orgid).Find(&info).Error
  6527. return info, err
  6528. }
  6529. func GetDrugWarehouseInfoList(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  6530. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "org_id = ? and status = 1", orgid).Find(&info).Error
  6531. return info, err
  6532. }
  6533. func GetStockCountByGoodId(goodid int64, storehouse_id int64, orgId int64) (info []*models.WarehousingInfo, err error) {
  6534. err = XTReadDB().Where("good_id = ? and storehouse_id = ? and org_id = ? and status = 1 and is_check = 1", goodid, storehouse_id, orgId).Find(&info).Error
  6535. return info, err
  6536. }
  6537. func UpdateGoodInfoSumCount(goodid int64, sum_count int64, orgid int64) error {
  6538. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  6539. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count + ?", sum_count)).Error
  6540. return err
  6541. }
  6542. func UpdateGoodInfoSumCountSix(goodid int64, sum_count int64, orgid int64) error {
  6543. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6544. return err
  6545. }
  6546. func UpdateGoodInfoReduceSumCount(goodid int64, sum_count int64, orgid int64) error {
  6547. tx := XTWriteDB().Begin()
  6548. err = tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6549. if err != nil {
  6550. tx.Rollback()
  6551. return err
  6552. }
  6553. tx.Commit()
  6554. return err
  6555. }
  6556. func UpdateGoodInfoAddSumCount(goodid int64, sum_count int64, orgid int64, sum_in_count int64) error {
  6557. tx := XTWriteDB().Begin()
  6558. err := tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6559. if err != nil {
  6560. tx.Rollback()
  6561. return err
  6562. }
  6563. err = tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  6564. if err != nil {
  6565. tx.Rollback()
  6566. return err
  6567. }
  6568. tx.Commit()
  6569. return err
  6570. }
  6571. func GetCancelStockInfo(id int64, orgid int64) (stock []*models.CancelStockInfo, err error) {
  6572. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", id, orgid).Preload("GoodInfo", "status = 1 and org_id = ?", orgid).Preload("WarehousingInfo", "status = 1 and org_id = ?", orgid).Find(&stock).Error
  6573. return stock, err
  6574. }
  6575. func ModifyCancelStockInfo(id int64, info *models.CancelStockInfo) error {
  6576. err := XTWriteDB().Model(&models.CancelStockInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"good_id": info.GoodId, "good_type_id": info.GoodTypeId, "count": info.Count, "price": info.Price, "total": info.Total, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "number": info.Number, "register_account": info.RegisterAccount, "remark": info.Remark, "storehouse_id": info.StorehouseId}).Error
  6577. return err
  6578. }
  6579. func GetCancelStockInfoListById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  6580. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  6581. return info, err
  6582. }
  6583. func GetWarehouseOutList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.WarehouseOutInfo, err error) {
  6584. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  6585. return info, err
  6586. }
  6587. func GetCancelWarehouseList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.CancelStockInfo, err error) {
  6588. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  6589. return info, err
  6590. }
  6591. func CheckCancelStock(stock models.CancelStock, cancel_stock_id int64, orgid int64) error {
  6592. ut := writeDb.Begin()
  6593. err := ut.Model(&models.CancelStock{}).Where("id = ? and status = 1 and org_id = ?", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 1}).Error
  6594. if err != nil {
  6595. ut.Rollback()
  6596. return err
  6597. }
  6598. err = ut.Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? and status = 1 and org_id = ?", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 1}).Error
  6599. if err != nil {
  6600. ut.Rollback()
  6601. return err
  6602. }
  6603. ut.Commit()
  6604. return err
  6605. }
  6606. func GetCancelStockInfoById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  6607. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&info).Error
  6608. return info, err
  6609. }
  6610. func UpdateCancelStockNumber(stock_count int64, warehouse_info_id int64, orgid int64) error {
  6611. ut := writeDb.Begin()
  6612. err := ut.Model(&models.WarehousingInfo{}).Where("id = ? and org_id = ? and status = 1", warehouse_info_id, orgid).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", stock_count)).Error
  6613. if err != nil {
  6614. ut.Rollback()
  6615. return err
  6616. }
  6617. ut.Commit()
  6618. return err
  6619. }
  6620. func UpdateGoodSumCount(sum_count int64, good_id int64, orgid int64) error {
  6621. ut := writeDb.Begin()
  6622. err := ut.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  6623. if err != nil {
  6624. ut.Rollback()
  6625. return err
  6626. }
  6627. ut.Commit()
  6628. return err
  6629. }
  6630. func UpdateStockWarehouseInfo(cancel_stock_id int64, orgid int64, stock models.CancelStock) error {
  6631. ut := writeDb.Begin()
  6632. err := ut.Model(&models.CancelStock{}).Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  6633. if err != nil {
  6634. ut.Rollback()
  6635. return err
  6636. }
  6637. err = ut.Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  6638. if err != nil {
  6639. ut.Rollback()
  6640. return err
  6641. }
  6642. ut.Commit()
  6643. return err
  6644. }
  6645. func UpdateStockWarehouseInfoStockFlow(id int64, orgid int64, goodid int64) error {
  6646. ut := writeDb.Begin()
  6647. err = ut.Model(&models.VmStockFlow{}).Where("cancel_out_detail_id = ? and user_org_id = ? and status = 1 and good_id =?", id, orgid, goodid).Update(map[string]interface{}{"status": 0}).Error
  6648. if err != nil {
  6649. ut.Rollback()
  6650. return err
  6651. }
  6652. ut.Commit()
  6653. return err
  6654. }
  6655. func UpdateStockNumberWarehouseInfo(warehouse_info_id int64, good_id int64, org_id int64, stock_count int64) error {
  6656. ut := writeDb.Begin()
  6657. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ? and good_id = ? and org_id = ? and status = 1", warehouse_info_id, good_id, org_id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", stock_count)).Error
  6658. if err != nil {
  6659. ut.Rollback()
  6660. return err
  6661. }
  6662. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and status = 1 and org_id = ?", good_id, org_id).UpdateColumn("sum_count", gorm.Expr("sum_count - ?", stock_count)).Error
  6663. if err != nil {
  6664. ut.Rollback()
  6665. return err
  6666. }
  6667. ut.Commit()
  6668. return err
  6669. }
  6670. func UpdateDrugSumCount(drugid int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6671. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ?", drugid, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  6672. err = XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ?", drugid, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count + ?", sum_in_count)).Error
  6673. return err
  6674. }
  6675. func GetDrugStockWarehouseInfo(id int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfo, err error) {
  6676. err = XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1 and storehouse_id = ?", id, orgid, storehouse_id).Find(&info).Error
  6677. return info, err
  6678. }
  6679. func UpdateBaseDrugSum(drugid int64, orgid int64, sum_count int64, sum_in_count int64) error {
  6680. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  6681. err = XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count + ?", sum_in_count)).Error
  6682. return err
  6683. }
  6684. func GetDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64) (info []*models.DrugWarehouseInfo, err error) {
  6685. err = XTReadDB().Where("storehouse_id = ? and org_id = ? and status = 1 and drug_id = ? and is_check = 1", storehouse_id, orgid, drugid).Find(&info).Error
  6686. return info, err
  6687. }
  6688. func GetNewDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
  6689. err = tx.Where("storehouse_id = ? and org_id = ? and status = 1 and drug_id = ? and is_check = 1", storehouse_id, orgid, drugid).Find(&info).Error
  6690. if err != gorm.ErrRecordNotFound {
  6691. if err != nil {
  6692. tx.Rollback()
  6693. return info, err
  6694. }
  6695. }
  6696. return info, err
  6697. }
  6698. func UpdateBaseDrugSumInfo(sum_count int64, drugid int64, orgid int64, sum_in_count int64) error {
  6699. ut := XTWriteDB().Begin()
  6700. err := ut.Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6701. if err != nil {
  6702. ut.Rollback()
  6703. return err
  6704. }
  6705. err = ut.Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  6706. if err != nil {
  6707. ut.Rollback()
  6708. return err
  6709. }
  6710. ut.Commit()
  6711. return err
  6712. }
  6713. func UpdateBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64) error {
  6714. ut := writeDb.Begin()
  6715. err = ut.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6716. if err != nil {
  6717. ut.Rollback()
  6718. return err
  6719. }
  6720. ut.Commit()
  6721. return err
  6722. }
  6723. func UpdateNewBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64, tx *gorm.DB) error {
  6724. err = tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6725. if err != nil {
  6726. tx.Rollback()
  6727. return err
  6728. }
  6729. return err
  6730. }
  6731. func GetLastCancelStockByList(id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  6732. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", id, orgid).Preload("DrugWarehouseInfo", "status = 1 and org_id = ?", orgid).Preload("BaseDrugLib", "status = 1 and org_id = ?", orgid).Find(&info).Error
  6733. return info, err
  6734. }
  6735. func GetCancelDrugStockList(cancel_stock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  6736. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  6737. return info, err
  6738. }
  6739. func ModifyCancelStock(cancel_stock_id int64, stock models.DrugCancelStock, orgid int64) error {
  6740. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 1}).Error
  6741. err = XTWriteDB().Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 1}).Error
  6742. return err
  6743. }
  6744. func GetCancelDrugStock(cancel_stock_id int64, orgid int64) (models.DrugCancelStock, error) {
  6745. stock := models.DrugCancelStock{}
  6746. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&stock).Error
  6747. return stock, err
  6748. }
  6749. func GetDrugCancelCheckById(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  6750. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&stock).Error
  6751. return stock, err
  6752. }
  6753. func GetDrugCancelCheckByIdTwenty(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  6754. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 ", cancel_stock_id, orgid).Find(&stock).Error
  6755. return stock, err
  6756. }
  6757. func GetDrugWarehouseOutList(batch_number_id int64, drugId int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  6758. err = XTReadDB().Where("warehouse_info_id = ? and drug_id = ? and org_id = ? and status = 1", batch_number_id, drugId, orgid).Find(&info).Error
  6759. return info, err
  6760. }
  6761. func ModifyDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64) error {
  6762. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and drug_id = ? and status = 1", id, orgid, drugid).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6763. return err
  6764. }
  6765. func ModifyNewDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  6766. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and drug_id = ? and status = 1", id, orgid, drugid).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6767. if err != nil {
  6768. tx.Rollback()
  6769. return err
  6770. }
  6771. return err
  6772. }
  6773. func ModifyDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64) error {
  6774. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and drug_id = ? and status = 1", id, orgid, drugid).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  6775. return err
  6776. }
  6777. func ModifyNewDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  6778. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and drug_id = ? and status = 1", id, orgid, drugid).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  6779. if err != nil {
  6780. tx.Rollback()
  6781. return err
  6782. }
  6783. return err
  6784. }
  6785. func ModifyDrugCancelStockInfo(id int64, info *models.DrugCancelStockInfo) error {
  6786. err := XTWriteDB().Model(&models.DrugCancelStockInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "count": info.Count, "price": info.Price, "total": info.Total, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "number": info.Number, "register_account": info.RegisterAccount, "remark": info.Remark, "batch_number": info.BatchNumber, "max_unit": info.MaxUnit, "batch_number_id": info.BatchNumberId, "storehouse_id": info.StorehouseId}).Error
  6787. return err
  6788. }
  6789. func ReturnDrugCancelStock(cancelstock_id int64, stock models.DrugCancelStock) error {
  6790. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  6791. err = XTWriteDB().Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  6792. return err
  6793. }
  6794. func GetDrugCancelStockInfo(cancelstock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  6795. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancelstock_id, orgid).Find(&info).Error
  6796. return info, err
  6797. }
  6798. func ModifyDrugFlowByCancelId(cancelstock_id int64, drugId int64, orgid int64) error {
  6799. err := XTWriteDB().Model(&models.DrugFlow{}).Where("cancel_out_detail_id = ? and drug_id = ? and user_org_id = ? and status = 1", cancelstock_id, drugId, orgid).Update(map[string]interface{}{"status": 0}).Error
  6800. return err
  6801. }
  6802. func ModifyDrugMaxNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  6803. ut := XTWriteDB().Begin()
  6804. err := ut.Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and status = 1", id, orgid).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6805. if err != nil {
  6806. ut.Rollback()
  6807. return err
  6808. }
  6809. ut.Commit()
  6810. return err
  6811. }
  6812. func ModifyDrugMinNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  6813. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and status = 1", id, orgid).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  6814. return err
  6815. }
  6816. func GetDrugInfoSix(id int64, orgid int64) (models.DrugWarehouseInfo, error) {
  6817. info := models.DrugWarehouseInfo{}
  6818. err := XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1", id, orgid).Find(&info).Error
  6819. return info, err
  6820. }
  6821. func GetPrintList(storehouse_id int64, good_name string, orgid int64, limit int64, page int64) (storeinventory []*models.XtStockInventory, err error) {
  6822. db := XTReadDB().Model(&storeinventory).Where("status = 1")
  6823. if storehouse_id > 0 {
  6824. db = db.Where("storehouse_id = ?", storehouse_id)
  6825. }
  6826. if orgid > 0 {
  6827. db = db.Where("user_org_id = ?", orgid)
  6828. }
  6829. err = XTReadDB().Preload("GoodInfo", "status = 1 and org_id = ?", orgid).Preload("WarehousingInfo", "status = 1 and org_id=?", orgid).Find(&storeinventory).Error
  6830. return storeinventory, err
  6831. }
  6832. func GetGoodDialysisOutInfo(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.AutomaticReduceDetail, error) {
  6833. detail := models.AutomaticReduceDetail{}
  6834. err := XTReadDB().Where("org_id =? and patient_id = ? and record_time = ? and good_id = ? and status= 1", orgid, patient_id, sys_record_time, good_id).Find(&detail).Error
  6835. return detail, err
  6836. }
  6837. func GetGoodDialysisOutInfoSix(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.WarehouseOutInfo, error) {
  6838. detail := models.WarehouseOutInfo{}
  6839. err := XTReadDB().Where("org_id =? and patient_id = ? and sys_record_time = ? and good_id = ? and status= 1", orgid, patient_id, sys_record_time, good_id).Find(&detail).Error
  6840. return detail, err
  6841. }
  6842. func GetGoodSumCount(storehouse_id int64, good_id int64, orgid int64) (models.XtGoodStockCount, error) {
  6843. stock := models.XtGoodStockCount{}
  6844. err := XTReadDB().Where("storehouse_id = ? and good_id = ? and user_org_id = ? and status = 1", storehouse_id, good_id, orgid).Find(&stock).Error
  6845. return stock, err
  6846. }
  6847. func CreateGoodStockCount(stock models.XtGoodStockCount) error {
  6848. err := XTWriteDB().Create(&stock).Error
  6849. return err
  6850. }
  6851. func AddGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  6852. ut := XTWriteDB().Begin()
  6853. err := ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count + ?", count)).Error
  6854. if err != nil {
  6855. ut.Rollback()
  6856. return err
  6857. }
  6858. ut.Commit()
  6859. return err
  6860. }
  6861. func ReduceGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  6862. ut := XTWriteDB().Begin()
  6863. err := ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count - ?", count)).Error
  6864. if err != nil {
  6865. ut.Rollback()
  6866. return err
  6867. }
  6868. ut.Commit()
  6869. return err
  6870. }
  6871. func UpdateGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64) error {
  6872. ut := XTWriteDB().Begin()
  6873. err := ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  6874. if err != nil {
  6875. ut.Rollback()
  6876. return err
  6877. }
  6878. ut.Commit()
  6879. return err
  6880. }
  6881. func UpdateNewGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64, tx *gorm.DB) error {
  6882. err := tx.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  6883. if err != nil {
  6884. tx.Rollback()
  6885. return err
  6886. }
  6887. return err
  6888. }
  6889. func CreateDrugStock(stock models.XtDrugStockCount) error {
  6890. err := XTWriteDB().Create(&stock).Error
  6891. return err
  6892. }
  6893. func UpdateDrugCount(storehouse_id int64, user_org_id int64, drug_id int64, sum_in_count int64, flush_count int64) error {
  6894. ut := XTWriteDB().Begin()
  6895. err := ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  6896. if err != nil {
  6897. ut.Rollback()
  6898. return err
  6899. }
  6900. err = ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  6901. if err != nil {
  6902. ut.Rollback()
  6903. return err
  6904. }
  6905. ut.Commit()
  6906. return err
  6907. }
  6908. func UpdateDrugCountOne(storehouse_id int64, user_org_id int64, drug_id int64, flush_count int64) error {
  6909. ut := XTWriteDB().Begin()
  6910. err = ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  6911. if err != nil {
  6912. ut.Rollback()
  6913. return err
  6914. }
  6915. ut.Commit()
  6916. return err
  6917. }
  6918. func AddDrugSumOutCount(storehouse_id int64, sum_out_count int64, user_org_id int64, drug_id int64) error {
  6919. ut := XTWriteDB().Begin()
  6920. err := ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count + ?", sum_out_count)).Error
  6921. if err != nil {
  6922. ut.Rollback()
  6923. return err
  6924. }
  6925. err = ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count + ?", sum_out_count)).Error
  6926. if err != nil {
  6927. ut.Rollback()
  6928. return err
  6929. }
  6930. ut.Commit()
  6931. return err
  6932. }
  6933. func ReduceDrugSumOut(storehouse_id int64, user_org_id int64, drug_id int64, sum_out_count int64) error {
  6934. ut := XTWriteDB().Begin()
  6935. err := ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count + ?", sum_out_count)).Error
  6936. if err != nil {
  6937. ut.Rollback()
  6938. return err
  6939. }
  6940. ut.Commit()
  6941. return err
  6942. }
  6943. func AddGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  6944. ut := XTWriteDB().Begin()
  6945. err := ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_out_count", gorm.Expr("stock_out_count + ?", count)).Error
  6946. if err != nil {
  6947. ut.Rollback()
  6948. return err
  6949. }
  6950. err = ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count + ?", count)).Error
  6951. if err != nil {
  6952. ut.Rollback()
  6953. return err
  6954. }
  6955. ut.Commit()
  6956. return err
  6957. }
  6958. func ReduceGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  6959. ut := XTWriteDB().Begin()
  6960. err := ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_out_count", gorm.Expr("stock_out_count - ?", count)).Error
  6961. if err != nil {
  6962. ut.Rollback()
  6963. return err
  6964. }
  6965. err = ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count - ?", count)).Error
  6966. if err != nil {
  6967. ut.Rollback()
  6968. return err
  6969. }
  6970. ut.Commit()
  6971. return err
  6972. }
  6973. func GetInvetoryWarehouseInfoList(orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  6974. if orgid == 10191 {
  6975. err = XTReadDB().Model(&info).Where("status = 1 and storehouse_id=? and is_check= 1 and org_id = ?", storehouse_id, orgid).Preload("GoodInfo", "status = 1").Order("good_id").Find(&info).Error
  6976. return info, err
  6977. } else {
  6978. err = XTReadDB().Model(&info).Where("status = 1 and storehouse_id=? and is_check= 1 and org_id = ? and stock_count >0", storehouse_id, orgid).Preload("GoodInfo", "status = 1").Order("good_id").Find(&info).Error
  6979. return info, err
  6980. }
  6981. }
  6982. func GetSettleOpenConfig(user_org_id int64) (*models.HisSettleStockConfig, error) {
  6983. config := models.HisSettleStockConfig{}
  6984. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  6985. if err == gorm.ErrRecordNotFound {
  6986. return nil, err
  6987. }
  6988. if err != nil {
  6989. return nil, err
  6990. }
  6991. return &config, nil
  6992. }
  6993. func CreateSettleOpenConfig(config models.HisSettleStockConfig) error {
  6994. err := XTWriteDB().Create(&config).Error
  6995. return err
  6996. }
  6997. func UpdateSettleOpenConfig(id int64, config models.HisSettleStockConfig) error {
  6998. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_open": config.IsOpen}).Error
  6999. return err
  7000. }
  7001. func UpdateDrugStockAutomaticReduceRecordOne(orgid int64, is_open int64) (models.DrugStockConfig, error) {
  7002. config := models.DrugStockConfig{}
  7003. err := XTWriteDB().Model(&config).Where("org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  7004. return config, err
  7005. }
  7006. func UpdatePharyConfig(orgid int64, is_open int64) (models.PharmacyConfig, error) {
  7007. stockConfig := models.PharmacyConfig{}
  7008. err := XTWriteDB().Model(&stockConfig).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  7009. return stockConfig, err
  7010. }
  7011. func UpdateSettleOpenConfigOne(id int64, is_open int64) error {
  7012. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  7013. return err
  7014. }
  7015. func UpdateSettleOpenConfigTwo(id int64, is_open int64) error {
  7016. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  7017. return err
  7018. }
  7019. func GetSettleOpenConfigByOrgId(user_org_id int64) (models.HisSettleStockConfig, error) {
  7020. config := models.HisSettleStockConfig{}
  7021. err = XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7022. return config, nil
  7023. }
  7024. func GetGoodInformationByGoodIdOne(good_id int64) (models.GoodInformationFourty, error) {
  7025. info := models.GoodInformationFourty{}
  7026. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  7027. return info, err
  7028. }
  7029. func GetHisDocById(id int64, orgid int64) (models.HisDoctorAdvice, error) {
  7030. advice := models.HisDoctorAdvice{}
  7031. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  7032. return advice, err
  7033. }
  7034. func GetBloodDocById(id int64, orgid int64) (models.DoctorAdvice, error) {
  7035. advice := models.DoctorAdvice{}
  7036. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  7037. return advice, err
  7038. }
  7039. func FindAllSettleWarehouseOut(org_id int64, warehouse_out_time int64, is_sys int64) (models.WarehouseOut, error) {
  7040. out := models.WarehouseOut{}
  7041. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ? and status = 1 and warehouse_out_time = ? and is_sys = ?", org_id, warehouse_out_time, is_sys).Find(&out).Error
  7042. return out, err
  7043. }
  7044. func GetGoodOutOpenConfig(user_org_id int64) (*models.XtGoodOutConfig, error) {
  7045. config := models.XtGoodOutConfig{}
  7046. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7047. if err == gorm.ErrRecordNotFound {
  7048. return nil, err
  7049. }
  7050. if err != nil {
  7051. return nil, err
  7052. }
  7053. return &config, nil
  7054. }
  7055. func CreateGoodOutOpenConfig(good models.XtGoodOutConfig) error {
  7056. err := XTWriteDB().Create(&good).Error
  7057. return err
  7058. }
  7059. func UpdateGoodOutOpenConfig(id int64, good models.XtGoodOutConfig) error {
  7060. err := XTWriteDB().Model(&good).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"is_open": good.IsOpen}).Error
  7061. return err
  7062. }
  7063. func FindGoodOutConfigById(user_org_id int64) (models.XtGoodOutConfig, error) {
  7064. config := models.XtGoodOutConfig{}
  7065. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7066. return config, err
  7067. }
  7068. func UpdateGoodAutoConfig(user_org_id int64) error {
  7069. gobalConfig := models.GobalConfig{}
  7070. err := XTWriteDB().Model(&gobalConfig).Where("org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  7071. return err
  7072. }
  7073. func UpdateGoodOutConfig(user_org_id int64) error {
  7074. config := models.XtGoodOutConfig{}
  7075. err := XTWriteDB().Model(&config).Where("user_org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  7076. return err
  7077. }
  7078. func GetHisGoodList(patient_id int64, record_date int64, orgid int64) (info []*models.GoodHisPrescriptionProject, err error) {
  7079. err = XTReadDB().Where("patient_id = ? and record_date = ? and status = 1 and user_org_id = ? AND type = 3", patient_id, record_date, orgid).Find(&info).Error
  7080. return info, err
  7081. }
  7082. func GetLastGoodWarehouseOutInfoByProjectId(good_id int64, patient_id int64, sys_record_time int64, project_id int64) (good []*models.WarehouseOutInfo, err error) {
  7083. err = XTReadDB().Where("good_id = ? and patient_id = ? and sys_record_time = ? and project_id = ? and status = 1", good_id, patient_id, sys_record_time, project_id).Find(&good).Error
  7084. return good, err
  7085. }
  7086. func ModifyGoodWarehouseInfo(good_id int64, warehouse_info_id int64, orgid int64, count int64) error {
  7087. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ? and org_id = ? and good_id = ? and status = 1", warehouse_info_id, orgid, good_id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  7088. return err
  7089. }
  7090. func DeleteGoodWarehouseOutInfo(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  7091. err := XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("good_id = ? and sys_record_time = ? and org_id = ? and project_id = ? and status=1", good_id, sys_record_time, org_id, project_id).Updates(map[string]interface{}{"status": 0}).Error
  7092. return err
  7093. }
  7094. func DeleteGoodFlow(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  7095. err := XTWriteDB().Model(&models.VmStockFlow{}).Where("good_id = ? and system_time = ? and user_org_id = ? and project_id = ? and status=1", good_id, sys_record_time, org_id, project_id).Updates(map[string]interface{}{"status": 0, "is_read": 1}).Error
  7096. return err
  7097. }
  7098. func FindPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  7099. out := models.WarehouseOut{}
  7100. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ? and status = 1 and warehouse_out_time = ? and is_sys=1", org_id, warehouse_out_time).Find(&out).Error
  7101. return out, err
  7102. }
  7103. func FindNewPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  7104. out := models.WarehouseOut{}
  7105. err = tx.Model(&models.WarehouseOut{}).Where("org_id = ? and status = 1 and warehouse_out_time = ? and is_sys=1", org_id, warehouse_out_time).Find(&out).Error
  7106. if err != gorm.ErrRecordNotFound {
  7107. if err != nil {
  7108. tx.Rollback()
  7109. return out, err
  7110. }
  7111. }
  7112. return out, err
  7113. }
  7114. func FindLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  7115. out := models.WarehouseOut{}
  7116. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ? and status = 1 and warehouse_out_time = ? and is_sys=1", org_id, warehouse_out_time).Last(&out).Error
  7117. return out, err
  7118. }
  7119. func FindNewLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  7120. out := models.WarehouseOut{}
  7121. err = tx.Model(&models.WarehouseOut{}).Where("org_id = ? and status = 1 and warehouse_out_time = ? and is_sys=1", org_id, warehouse_out_time).Last(&out).Error
  7122. if err != gorm.ErrRecordNotFound {
  7123. if err != nil {
  7124. tx.Rollback()
  7125. return out, err
  7126. }
  7127. }
  7128. return out, err
  7129. }
  7130. func GetGoodTypeObj(goodid int64, org_id int64) (models.GoodsType, error) {
  7131. goodsType := models.GoodsType{}
  7132. err := XTReadDB().Where("good_id = ? and org_id = ? and status = 1", goodid, org_id).Find(&goodsType).Error
  7133. return goodsType, err
  7134. }
  7135. func GetSencondeGoodWarehouseInfo(good_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  7136. err = XTReadDB().Where("good_id = ? and org_id= ? and status= 1 and is_check = 1", good_id, org_id).Find(&info).Error
  7137. return info, err
  7138. }
  7139. func UpdateGoodSumCountSeven(sum_count int64, good_id int64, orgid int64) error {
  7140. ut := writeDb.Begin()
  7141. err := ut.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Updates(map[string]interface{}{"sum_count": sum_count}).Error
  7142. if err != nil {
  7143. ut.Rollback()
  7144. return err
  7145. }
  7146. ut.Commit()
  7147. return err
  7148. }
  7149. func UpdateNewGoodSumCountSeven(sum_count int64, good_id int64, orgid int64, tx *gorm.DB) error {
  7150. err := tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Updates(map[string]interface{}{"sum_count": sum_count}).Error
  7151. if err != nil {
  7152. tx.Rollback()
  7153. return err
  7154. }
  7155. return err
  7156. }
  7157. func GetWarehouseOutInfoIsExistOTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  7158. info := models.WarehouseOutInfo{}
  7159. var err error
  7160. err = XTReadDB().Model(&info).Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ?", good_id, record_time, patient_id, project_id).Find(&info).Error
  7161. if err == gorm.ErrRecordNotFound {
  7162. return nil, err
  7163. }
  7164. if err != nil {
  7165. return nil, err
  7166. }
  7167. return &info, nil
  7168. }
  7169. func GetWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  7170. info := models.WarehouseOutInfo{}
  7171. err := XTReadDB().Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ?", good_id, record_time, patient_id, project_id).Find(&info).Error
  7172. return info, err
  7173. }
  7174. func GetNewWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  7175. info := models.WarehouseOutInfo{}
  7176. err := tx.Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ?", good_id, record_time, patient_id, project_id).Find(&info).Error
  7177. if err != gorm.ErrRecordNotFound {
  7178. if err != nil {
  7179. tx.Rollback()
  7180. return info, err
  7181. }
  7182. }
  7183. return info, err
  7184. }
  7185. func GetWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (models.WarehouseOutInfo, error) {
  7186. info := models.WarehouseOutInfo{}
  7187. err := XTReadDB().Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ? and warehouse_info_id = ?", good_id, record_time, patient_id, project_id, warehouse_info_id).Find(&info).Error
  7188. return info, err
  7189. }
  7190. func GetNewWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  7191. info := models.WarehouseOutInfo{}
  7192. err := tx.Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ? and warehouse_info_id = ?", good_id, record_time, patient_id, project_id, warehouse_info_id).Find(&info).Error
  7193. if err != gorm.ErrRecordNotFound {
  7194. if err != nil {
  7195. return info, err
  7196. }
  7197. }
  7198. return info, err
  7199. }
  7200. func GetNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (*models.WarehouseOutInfo, error) {
  7201. info := models.WarehouseOutInfo{}
  7202. var err error
  7203. err = XTReadDB().Model(&info).Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ? and warehouse_info_id = ?", good_id, record_time, patient_id, project_id, warehouse_info_id).Find(&info).Error
  7204. if err == gorm.ErrRecordNotFound {
  7205. return nil, err
  7206. }
  7207. if err != nil {
  7208. return nil, err
  7209. }
  7210. return &info, nil
  7211. }
  7212. func GetNewNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (*models.WarehouseOutInfo, error) {
  7213. info := models.WarehouseOutInfo{}
  7214. var err error
  7215. err = tx.Model(&info).Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ? and warehouse_info_id = ?", good_id, record_time, patient_id, project_id, warehouse_info_id).Find(&info).Error
  7216. if err != gorm.ErrRecordNotFound {
  7217. if err != nil {
  7218. tx.Rollback()
  7219. return &info, err
  7220. }
  7221. }
  7222. return &info, err
  7223. }
  7224. func GetNewGoodWarehouseInfo(startime int64, endtime int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.GoodWarehousingInfo, err error) {
  7225. db := XTReadDB().Model(&info).Where("status = 1")
  7226. if startime > 0 {
  7227. db = db.Where("ctime>=?", startime)
  7228. }
  7229. if endtime > 0 {
  7230. db = db.Where("ctime<=?", endtime)
  7231. }
  7232. if orgid > 0 {
  7233. db = db.Where("org_id = ?", orgid)
  7234. }
  7235. if len(good_id) > 0 {
  7236. db = db.Where("good_id in(?)", good_id)
  7237. }
  7238. if storehouse_id > 0 {
  7239. db = db.Where("storehouse_id = ?", storehouse_id)
  7240. }
  7241. err = db.Preload("GoodInfo", "org_id = ? and status= 1", orgid).Find(&info).Error
  7242. return info, err
  7243. }
  7244. func GetNewWarehouseOutInfoList(start_time int64, end_time int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.WarehouseOutInfoNight, err error) {
  7245. db := XTReadDB().Model(&info).Where("status= 1")
  7246. if start_time > 0 {
  7247. db = db.Where("ctime>=?", start_time)
  7248. }
  7249. if end_time > 0 {
  7250. db = db.Where("ctime <=?", end_time)
  7251. }
  7252. if orgid > 0 {
  7253. db = db.Where("org_id = ?", orgid)
  7254. }
  7255. if len(good_id) > 0 {
  7256. db = db.Where("good_id in(?)", good_id)
  7257. }
  7258. if storehouse_id > 0 {
  7259. db = db.Where("storehouse_id = ?", storehouse_id)
  7260. }
  7261. err = db.Preload("GoodInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  7262. return info, err
  7263. }
  7264. func GetGoodOutOpenConfigOne(user_org_id int64) (models.XtGoodOutConfig, error) {
  7265. config := models.XtGoodOutConfig{}
  7266. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7267. return config, err
  7268. }
  7269. func UpdateDeleteAutoGood(good_id int64, record_date int64, patient_id int64) error {
  7270. err := XTWriteDB().Model(&models.AutomaticReduceDetail{}).Where("good_id = ? and record_time = ? and patient_id = ?", good_id, record_date, patient_id).Updates(map[string]interface{}{"status": 1}).Error
  7271. err = XTWriteDB().Model(&models.DialysisBeforePrepare{}).Where("good_id = ? and record_date = ? and patient_id = ?", good_id, record_date, patient_id).Updates(map[string]interface{}{"status": 1}).Error
  7272. return err
  7273. }
  7274. func UpdateWarehouseInfoMap(list []*models.WarehousingInfo, warehousing_info_id int64, orgId int64) error {
  7275. tx := XTWriteDB().Begin()
  7276. for _, item := range list {
  7277. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", item.WarehousingCount)).Error
  7278. if err != nil {
  7279. tx.Rollback()
  7280. return err
  7281. }
  7282. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).Update(map[string]interface{}{"is_check": 2}).Error
  7283. if err != nil {
  7284. tx.Rollback()
  7285. return err
  7286. }
  7287. err = tx.Model(models.VmStockFlow{}).Where("warehousing_detail_id = ?", item.ID).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  7288. if err != nil {
  7289. tx.Rollback()
  7290. return err
  7291. }
  7292. var goodList []*models.WarehousingInfo
  7293. err = tx.Where("org_id = ? and storehouse_id = ? and good_id = ? and status =1 and is_check = 1", orgId, item.StorehouseId, item.GoodId).Find(&goodList).Error
  7294. var flush_count int64
  7295. for _, it := range goodList {
  7296. flush_count += it.StockCount
  7297. }
  7298. err = tx.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and good_id = ? and storehouse_id = ? and status = 1", item.OrgId, item.GoodId, item.StorehouseId).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count - ?", item.WarehousingCount)).Error
  7299. if err != nil {
  7300. tx.Rollback()
  7301. return err
  7302. }
  7303. err = tx.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and good_id = ? and storehouse_id = ? and status = 1", item.OrgId, item.GoodId, item.StorehouseId).Update(map[string]interface{}{"flush_count": flush_count}).Error
  7304. if err != nil {
  7305. tx.Rollback()
  7306. return err
  7307. }
  7308. storeHouseConfig := models.XtStorehouseConfig{}
  7309. err = tx.Where("user_org_id = ? and status = 1", orgId).First(&storeHouseConfig).Error
  7310. var stockList []*models.WarehousingInfo
  7311. //查询该耗材剩余多少库存
  7312. err = tx.Where("good_id = ? and storehouse_id = ? and org_id = ? and status = 1 and is_check = 1", item.GoodId, storeHouseConfig.StorehouseOutInfo, orgId).Find(&stockList).Error
  7313. var sum_count int64
  7314. var sum_in_count int64
  7315. for _, it := range stockList {
  7316. sum_count += it.StockCount
  7317. sum_in_count += it.WarehousingCount
  7318. }
  7319. err = tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", item.GoodId, orgId).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7320. if err != nil {
  7321. tx.Rollback()
  7322. return err
  7323. }
  7324. err = tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", item.GoodId, orgId).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  7325. if err != nil {
  7326. tx.Rollback()
  7327. return err
  7328. }
  7329. }
  7330. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  7331. if err != nil {
  7332. tx.Rollback()
  7333. return err
  7334. }
  7335. err = tx.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  7336. tx.Commit()
  7337. return err
  7338. }
  7339. func FindFirstWarehousingInfoByStockThree(good_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  7340. if storehouse_id == 0 {
  7341. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime").First(&info).Error
  7342. }
  7343. if storehouse_id > 0 {
  7344. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and stock_count > 0 and storehouse_id =? ", good_id, storehouse_id).Order("ctime").First(&info).Error
  7345. }
  7346. return info, err
  7347. }
  7348. func GetHisPrescriptionNumberList(project_id int64, record_date int64, orgid int64, patient_id int64) (list []*models.HisPrescriptionProject, err error) {
  7349. err = readDb.Where("project_id = ? and record_date =? and user_org_id =? and status =1 and patient_id = ?", project_id, record_date, orgid, patient_id).Find(&list).Error
  7350. return list, err
  7351. }
  7352. func GetWarehouseOutnfoByProjectId(project_id int64, record_date int64, orgid int64, patient_id int64) (infor []*models.WarehouseOutInfo, err error) {
  7353. err = readDb.Where("good_id =? and sys_record_time = ? and org_id = ? and patient_id = ? and status=1", project_id, record_date, orgid, patient_id).Find(&infor).Error
  7354. return infor, err
  7355. }
  7356. func UpdateWarehouseInfoByPatientId(project_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfo, error) {
  7357. info := models.WarehouseOutInfo{}
  7358. err := XTWriteDB().Model(&info).Where("good_id = ? and sys_record_time = ? and org_id = ? and status =1 and patient_id = ?", project_id, record_date, orgid, patient_id).Updates(map[string]interface{}{"status": 0}).Error
  7359. return info, err
  7360. }
  7361. func UpdateWarehouseInfoByProject(good_id int64, id int64, user_org_id int64, count int64) (models.WarehousingInfo, error) {
  7362. info := models.WarehousingInfo{}
  7363. err := XTWriteDB().Where("id = ? and good_id = ? and org_id = ? and status= 1", id, good_id, user_org_id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  7364. return info, err
  7365. }