stock_service.go 392KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "XT_New/models"
  9. "github.com/jinzhu/gorm"
  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. "first_letter": goodInfo.FirstLetter,
  246. }).Error
  247. writeDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  248. writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  249. writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  250. writeDb.Model(&models.DialysisBeforePrepare{}).Where("good_id = ? AND user_org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  251. writeDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  252. writeDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  253. return err, goodInfo
  254. }
  255. 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) {
  256. offset := (page - 1) * limit
  257. db := readDb.Model(&models.GoodInfo{})
  258. db = db.Where("org_id = ? AND status = 1", orgId)
  259. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  260. if len(keyword) > 0 {
  261. likeKey := "%" + keyword + "%"
  262. db = db.Where("good_code LIKE ? OR good_name LIKE ?", likeKey, likeKey)
  263. if is_use > 0 {
  264. if is_use == 2 {
  265. db = db.Where("find_in_set('停用',good_status) = 0")
  266. } else {
  267. db = db.Where("find_in_set('停用',good_status) > 0")
  268. }
  269. }
  270. if is_charge > 0 {
  271. if is_charge == 1 {
  272. db = db.Where("find_in_set('收费',good_status) > 0")
  273. } else {
  274. db = db.Where("find_in_set('收费',good_status) = 0")
  275. }
  276. }
  277. if good_kind > 0 {
  278. db = db.Where(" good_kind = ?", good_kind)
  279. }
  280. if is_mark > 0 {
  281. db = db.Where("is_mark = ?", is_mark)
  282. }
  283. if manufacturer > 0 {
  284. db = db.Where("manufacturer = ?", manufacturer)
  285. }
  286. if is_type > 0 {
  287. db = db.Where("good_type_id = ?", is_type)
  288. }
  289. } else {
  290. if is_use > 0 {
  291. if is_use == 2 {
  292. db = db.Where("find_in_set('停用',good_status) = 0")
  293. } else {
  294. db = db.Where("find_in_set('停用',good_status) > 0")
  295. }
  296. }
  297. if is_charge > 0 {
  298. if is_charge == 1 {
  299. db = db.Where("find_in_set('收费',good_status) > 0")
  300. } else {
  301. db = db.Where("find_in_set('收费',good_status) = 0")
  302. }
  303. }
  304. if good_kind > 0 {
  305. db = db.Where(" good_kind = ?", good_kind)
  306. }
  307. if is_mark > 0 {
  308. db = db.Where("is_mark = ?", is_mark)
  309. }
  310. if manufacturer > 0 {
  311. db = db.Where("manufacturer = ?", manufacturer)
  312. }
  313. if is_type > 0 {
  314. db = db.Where("good_type_id = ?", is_type)
  315. }
  316. }
  317. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  318. return
  319. }
  320. func DeleteGoodInfoById(id int64, operateId int64) error {
  321. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  322. return err
  323. }
  324. func FindGoodInfoByGoodId(id int64) (list []*models.GoodInfo, err error) {
  325. err = readDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  326. return list, err
  327. }
  328. func FindStockOutDetailGoodInfoByGoodId(id int64) (list []*models.AutomaticReduceDetail, err error) {
  329. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  330. return list, err
  331. }
  332. func FindGoodInfoById(id int64) (*models.GoodInfo, error) {
  333. goodInfo := &models.GoodInfo{}
  334. err := readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  335. return goodInfo, err
  336. }
  337. func FindAllGoodType(org_id int64) (goodType []*models.GoodsType, err error) {
  338. err = XTReadDB().Model(&models.GoodsType{}).Where("org_id = ? AND status = 1", org_id).Find(&goodType).Error
  339. return goodType, err
  340. }
  341. func FindAllGoodTypeOne(org_id int64) (goodType []*models.GoodsType, err error) {
  342. err = readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 and out_stock = 1", org_id).Find(&goodType).Error
  343. return goodType, err
  344. }
  345. func FindAllGoodInfo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  346. 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
  347. return goodInfo, err
  348. }
  349. func FindAllGoodInfoTwo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  350. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id).Find(&goodInfo).Error
  351. return goodInfo, err
  352. }
  353. func FindAllWarehouseTotal(org_id int64) (total int64, err error) {
  354. err = readDb.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  355. return total, err
  356. }
  357. func FindAllWarehouseTotalOne(org_id int64) (total int64, err error) {
  358. err = readDb.Model(&models.XtDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  359. return total, err
  360. }
  361. func AddSigleWarehouse(warehouse *models.Warehousing) error {
  362. err := writeDb.Create(&warehouse).Error
  363. return err
  364. }
  365. func UpdateWarehouse(id int64, storehouse_id int64) error {
  366. err := XTWriteDB().Model(&models.Warehousing{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  367. return err
  368. }
  369. func AddSigleDrugWarehouse(warehouse *models.DrugWarehouse) error {
  370. err := writeDb.Create(&warehouse).Error
  371. return err
  372. }
  373. func UpdateSigleDrugWarehouse(id int64, storehosue_id int64) error {
  374. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehosue_id}).Error
  375. return err
  376. }
  377. func AddSigleSalesReturn(salesReturn *models.SalesReturn) error {
  378. err := writeDb.Create(&salesReturn).Error
  379. return err
  380. }
  381. func AddSigleDrugSalesReturn(salesReturn *models.DrugSalesReturn) error {
  382. err := writeDb.Create(&salesReturn).Error
  383. return err
  384. }
  385. func UpDateGoodReturnStatus(ids []string) {
  386. writeDb.Model(&models.WarehousingInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_return": 1})
  387. }
  388. func UpDateCanCelStockStatus(ids []string) {
  389. writeDb.Model(&models.WarehouseOutInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_cancel": 1})
  390. }
  391. func CreateWarehousingInfo(warehousingInfo []*models.WarehousingInfo) (err error) {
  392. if len(warehousingInfo) > 0 {
  393. utx := writeDb.Begin()
  394. if len(warehousingInfo) > 0 {
  395. 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 "
  396. insertParams := make([]string, 0)
  397. insertData := make([]interface{}, 0)
  398. for _, info := range warehousingInfo {
  399. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  400. insertData = append(insertData, info.WarehousingId)
  401. insertData = append(insertData, info.GoodId)
  402. insertData = append(insertData, info.GoodTypeId)
  403. insertData = append(insertData, info.Number)
  404. insertData = append(insertData, info.ProductDate)
  405. insertData = append(insertData, info.ExpiryDate)
  406. insertData = append(insertData, info.WarehousingCount)
  407. insertData = append(insertData, info.Price)
  408. insertData = append(insertData, info.TotalPrice)
  409. insertData = append(insertData, info.Dealer)
  410. insertData = append(insertData, info.Manufacturer)
  411. insertData = append(insertData, info.Remark)
  412. insertData = append(insertData, info.Ctime)
  413. insertData = append(insertData, info.Mtime)
  414. insertData = append(insertData, info.Status)
  415. insertData = append(insertData, info.OrgId)
  416. insertData = append(insertData, info.WarehousingOrder)
  417. insertData = append(insertData, info.Type)
  418. insertData = append(insertData, info.WarehousingCount)
  419. insertData = append(insertData, info.LicenseNumber)
  420. insertData = append(insertData, info.PackingPrice)
  421. insertData = append(insertData, info.StorehouseId)
  422. }
  423. thisSQL += strings.Join(insertParams, ", ")
  424. err = utx.Exec(thisSQL, insertData...).Error
  425. if err != nil {
  426. utx.Rollback()
  427. return
  428. }
  429. }
  430. utx.Commit()
  431. }
  432. return
  433. }
  434. func CreateDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  435. if len(warehousingInfo) > 0 {
  436. utx := writeDb.Begin()
  437. if len(warehousingInfo) > 0 {
  438. 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 "
  439. insertParams := make([]string, 0)
  440. insertData := make([]interface{}, 0)
  441. for _, info := range warehousingInfo {
  442. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  443. insertData = append(insertData, info.WarehousingId)
  444. insertData = append(insertData, info.DrugId)
  445. insertData = append(insertData, info.Number)
  446. insertData = append(insertData, info.ProductDate)
  447. insertData = append(insertData, info.ExpiryDate)
  448. insertData = append(insertData, info.WarehousingCount)
  449. insertData = append(insertData, info.Price)
  450. insertData = append(insertData, info.TotalPrice)
  451. insertData = append(insertData, info.Dealer)
  452. insertData = append(insertData, info.Manufacturer)
  453. insertData = append(insertData, info.Remark)
  454. insertData = append(insertData, info.Ctime)
  455. insertData = append(insertData, info.Mtime)
  456. insertData = append(insertData, info.Status)
  457. insertData = append(insertData, info.OrgId)
  458. insertData = append(insertData, info.WarehousingOrder)
  459. insertData = append(insertData, info.Type)
  460. insertData = append(insertData, info.RetailPrice)
  461. insertData = append(insertData, info.RetailTotalPrice)
  462. insertData = append(insertData, info.StockMaxNumber)
  463. insertData = append(insertData, info.MaxUnit)
  464. insertData = append(insertData, info.MinUnit)
  465. insertData = append(insertData, info.BatchNumber)
  466. insertData = append(insertData, info.StockMinNumber)
  467. insertData = append(insertData, info.StorehouseId)
  468. }
  469. thisSQL += strings.Join(insertParams, ", ")
  470. err = utx.Exec(thisSQL, insertData...).Error
  471. if err != nil {
  472. utx.Rollback()
  473. return
  474. }
  475. }
  476. utx.Commit()
  477. }
  478. return
  479. }
  480. func CreateDrugWarehousingInfoSix(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  481. if len(warehousingInfo) > 0 {
  482. utx := writeDb.Begin()
  483. if len(warehousingInfo) > 0 {
  484. 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 "
  485. insertParams := make([]string, 0)
  486. insertData := make([]interface{}, 0)
  487. for _, info := range warehousingInfo {
  488. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  489. insertData = append(insertData, info.WarehousingId)
  490. insertData = append(insertData, info.DrugId)
  491. insertData = append(insertData, info.Number)
  492. insertData = append(insertData, info.ProductDate)
  493. insertData = append(insertData, info.ExpiryDate)
  494. insertData = append(insertData, info.WarehousingCount)
  495. insertData = append(insertData, info.Price)
  496. insertData = append(insertData, info.TotalPrice)
  497. insertData = append(insertData, info.Dealer)
  498. insertData = append(insertData, info.Manufacturer)
  499. insertData = append(insertData, info.Remark)
  500. insertData = append(insertData, info.Ctime)
  501. insertData = append(insertData, info.Mtime)
  502. insertData = append(insertData, info.Status)
  503. insertData = append(insertData, info.OrgId)
  504. insertData = append(insertData, info.WarehousingOrder)
  505. insertData = append(insertData, info.Type)
  506. insertData = append(insertData, info.RetailPrice)
  507. insertData = append(insertData, info.RetailTotalPrice)
  508. insertData = append(insertData, info.StockMaxNumber)
  509. insertData = append(insertData, info.MaxUnit)
  510. insertData = append(insertData, info.MinUnit)
  511. insertData = append(insertData, info.BatchNumber)
  512. insertData = append(insertData, info.StockMinNumber)
  513. insertData = append(insertData, info.SupplyWarehouseId)
  514. insertData = append(insertData, info.SupplyWarehouseDetailInfo)
  515. insertData = append(insertData, info.StorehouseId)
  516. insertData = append(insertData, info.IsCheck)
  517. }
  518. thisSQL += strings.Join(insertParams, ", ")
  519. err = utx.Exec(thisSQL, insertData...).Error
  520. if err != nil {
  521. utx.Rollback()
  522. return
  523. }
  524. }
  525. utx.Commit()
  526. }
  527. return
  528. }
  529. func UpdateDrugInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  530. var total float64
  531. var min_number int64
  532. for _, item := range warehousingInfo {
  533. //查询刚入库耗材的库存
  534. count, _ := FindDrugWaresingCount(item.DrugId, item.OrgId)
  535. //更新药品库的总量
  536. info, _ := FindeDrugInfo(item.DrugId)
  537. min_number = count.StockMaxNumber * info.MinNumber
  538. str := strconv.FormatInt(min_number, 10)
  539. minFloat, _ := strconv.ParseFloat(str, 64)
  540. total = info.Total + minFloat
  541. lib := models.BaseDrugLib{
  542. Total: total,
  543. }
  544. UpdateBaseDrug(&lib, item.ID)
  545. }
  546. return err
  547. }
  548. func FindDrugWaresingCount(drug_id int64, orgid int64) (models.XtDrugWarehouseInfo, error) {
  549. info := models.XtDrugWarehouseInfo{}
  550. db := XTReadDB().Table("xt_drug_warehouse_info").Where("status = 1")
  551. err = db.Select("stock_max_number,drug_id").Where("drug_id = ? and org_id = ? and status =1", drug_id, orgid).Last(&info).Error
  552. return info, err
  553. }
  554. func FindeDrugInfo(drug_id int64) (models.BaseDrugLib, error) {
  555. lib := models.BaseDrugLib{}
  556. err := XTReadDB().Model(&lib).Where("id = ?", drug_id).Find(&lib).Error
  557. return lib, err
  558. }
  559. func UpdateBaseDrug(lib *models.BaseDrugLib, id int64) error {
  560. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"total": lib.Total}).Error
  561. return err
  562. }
  563. func UpdateBaseDrugOne(lib models.BaseDrugLib, id int64) error {
  564. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"retail_price": lib.RetailPrice, "last_price": lib.LastPrice}).Error
  565. return err
  566. }
  567. func FindeLastWarehouseInfo(id int64) (models.DrugWarehouseInfo, error) {
  568. info := models.DrugWarehouseInfo{}
  569. err := XTReadDB().Model(&info).Where("id=? and status = 1", id).Find(&info).Error
  570. return info, err
  571. }
  572. func GetLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  573. out := models.DrugWarehouseOut{}
  574. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  575. return out, err
  576. }
  577. func CreateDrugFlow(drugflow []*models.DrugFlow) (err error) {
  578. if len(drugflow) > 0 {
  579. utx := writeDb.Begin()
  580. if len(drugflow) > 0 {
  581. 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 "
  582. insertParams := make([]string, 0)
  583. insertData := make([]interface{}, 0)
  584. for _, info := range drugflow {
  585. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  586. insertData = append(insertData, info.WarehousingId)
  587. insertData = append(insertData, info.DrugId)
  588. insertData = append(insertData, info.Number)
  589. insertData = append(insertData, info.BatchNumber)
  590. insertData = append(insertData, info.Count)
  591. insertData = append(insertData, info.UserOrgId)
  592. insertData = append(insertData, info.PatientId)
  593. insertData = append(insertData, info.SystemTime)
  594. insertData = append(insertData, info.ConsumableType)
  595. insertData = append(insertData, info.IsSys)
  596. insertData = append(insertData, info.WarehousingOrder)
  597. insertData = append(insertData, info.WarehouseOutId)
  598. insertData = append(insertData, info.WarehouseOutOrderNumber)
  599. insertData = append(insertData, info.IsEdit)
  600. insertData = append(insertData, info.CancelStockId)
  601. insertData = append(insertData, info.CancelOrderNumber)
  602. insertData = append(insertData, info.Manufacturer)
  603. insertData = append(insertData, info.Dealer)
  604. insertData = append(insertData, info.Creator)
  605. insertData = append(insertData, info.UpdateCreator)
  606. insertData = append(insertData, info.Status)
  607. insertData = append(insertData, info.Ctime)
  608. insertData = append(insertData, info.Mtime)
  609. insertData = append(insertData, info.Price)
  610. insertData = append(insertData, info.WarehousingDetailId)
  611. insertData = append(insertData, info.WarehouseOutDetailId)
  612. insertData = append(insertData, info.CancelOutDetailId)
  613. insertData = append(insertData, info.ExpireDate)
  614. insertData = append(insertData, info.ProductDate)
  615. insertData = append(insertData, info.MaxUnit)
  616. insertData = append(insertData, info.MinUnit)
  617. insertData = append(insertData, info.StorehouseId)
  618. }
  619. thisSQL += strings.Join(insertParams, ", ")
  620. err = utx.Exec(thisSQL, insertData...).Error
  621. if err != nil {
  622. utx.Rollback()
  623. return
  624. }
  625. }
  626. utx.Commit()
  627. }
  628. return
  629. }
  630. func GetDrugFlowIsExist(warhouseing int64, drug_id int64) (*models.DrugFlow, error) {
  631. info := models.DrugFlow{}
  632. err = XTReadDB().Where("warehousing_id = ? and drug_id = ? and status = 1", warhouseing, drug_id).Find(&info).Error
  633. if err == gorm.ErrRecordNotFound {
  634. return nil, err
  635. }
  636. if err != nil {
  637. return nil, err
  638. }
  639. return &info, nil
  640. }
  641. func GetDrugByWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  642. info := models.XtDrugWarehouseInfo{}
  643. err := XTReadDB().Where("id=? and status = 1", id).Find(&info).Error
  644. return info, err
  645. }
  646. func UpdateDrugWarehouseingInfo(id int64, info models.XtDrugWarehouseInfo) error {
  647. warehouseInfo := models.XtDrugWarehouseInfo{}
  648. err := XTWriteDB().Model(&warehouseInfo).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_max_number": info.StockMaxNumber}).Error
  649. return err
  650. }
  651. func CreateDrugFlowOne(flow models.DrugFlow) error {
  652. ut := writeDb.Begin()
  653. err := ut.Create(&flow).Error
  654. if err != nil {
  655. ut.Rollback()
  656. return err
  657. }
  658. ut.Commit()
  659. return err
  660. }
  661. func CreateNewDrugFlowOne(flow models.DrugFlow, tx *gorm.DB) error {
  662. err := tx.Create(&flow).Error
  663. if err != nil {
  664. tx.Rollback()
  665. return err
  666. }
  667. return err
  668. }
  669. func UpdateDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64, id int64) (models.DrugAutomaticReduceDetail, error) {
  670. detail := models.DrugAutomaticReduceDetail{}
  671. 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
  672. 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
  673. return detail, err
  674. }
  675. func UpdateDrugFlowTens(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.DrugFlow, error) {
  676. flow := models.DrugFlow{}
  677. 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
  678. return flow, err
  679. }
  680. func CreateDrugFlowTwo(flow *models.DrugFlow) error {
  681. tx := XTWriteDB().Begin()
  682. err := tx.Create(&flow).Error
  683. if err != nil {
  684. tx.Rollback()
  685. return err
  686. }
  687. tx.Commit()
  688. return err
  689. }
  690. func FindLastWarehousingInfo(order string) (info models.WarehousingInfo, err error) {
  691. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  692. return info, err
  693. }
  694. func GetLastWarehousingInfo(goodid int64) (models.WarehousingInfo, error) {
  695. info := models.WarehousingInfo{}
  696. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  697. return info, err
  698. }
  699. func CreatedWarehouseingDetail(info *models.WarehousingInfo) error {
  700. err := XTWriteDB().Create(&info).Error
  701. return err
  702. }
  703. func FindFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  704. if storehouse_id == 0 {
  705. 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
  706. }
  707. if storehouse_id > 0 {
  708. 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
  709. }
  710. return info, err
  711. }
  712. func FindNewFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64, tx *gorm.DB) (info models.WarehousingInfo, err error) {
  713. if storehouse_id == 0 {
  714. 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
  715. if err != gorm.ErrRecordNotFound {
  716. if err != nil {
  717. tx.Rollback()
  718. return info, err
  719. }
  720. }
  721. }
  722. if storehouse_id > 0 {
  723. 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
  724. if err != gorm.ErrRecordNotFound {
  725. if err != nil {
  726. tx.Rollback()
  727. return info, err
  728. }
  729. }
  730. }
  731. return info, err
  732. }
  733. func FindFirstWarehousingInfoByStockTwo(good_id int64, good_type_id int64, storehouse_id int64) (info models.VmWarehousingInfo, err error) {
  734. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  735. if good_id > 0 {
  736. db = db.Where("x.good_id = ?", good_id)
  737. }
  738. if good_type_id > 0 {
  739. db = db.Where("x.good_type_id = ?", good_type_id)
  740. }
  741. if storehouse_id > 0 {
  742. db = db.Where("x.storehouse_id = ?", storehouse_id)
  743. }
  744. err = db.Select("sum(x.stock_count) as count").Scan(&info).Error
  745. return info, err
  746. }
  747. func FindWarehousingInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  748. info := models.WarehousingInfo{}
  749. err := XTReadDB().Model(&info).Where("id= ? and status = 1", id).Find(&info).Error
  750. return info, err
  751. }
  752. func FindWarehousingInfoTen(goodId int64, storehouse_id int64) (models.WarehousingInfo, error) {
  753. info := models.WarehousingInfo{}
  754. var err error
  755. if storehouse_id == 0 {
  756. 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
  757. }
  758. if storehouse_id > 0 {
  759. 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
  760. }
  761. return info, err
  762. }
  763. //药品先进先出,找出最先入库的批次
  764. func FindLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  765. 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
  766. return info, err
  767. }
  768. func FindLastDrugWarehousingInfoByIDTwo(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  769. err = readDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 and storehouse_id = ? and is_check = 1", drug_id, storehouse_id).Order("ctime").Last(&info).Error
  770. return info, err
  771. }
  772. func FindNewLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64, tx *gorm.DB) (info models.XtDrugWarehouseInfo, err error) {
  773. 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
  774. if err != gorm.ErrRecordNotFound {
  775. if err != nil {
  776. tx.Rollback()
  777. return info, err
  778. }
  779. }
  780. return info, err
  781. }
  782. func FindLastDrugWarehousingInfoByIDSix(drug_id int64, warehouse_out_id int64, is_source int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  783. if is_source == 1 {
  784. 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
  785. }
  786. if is_source == 2 || is_source == 0 {
  787. 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
  788. }
  789. return info, err
  790. }
  791. // 出库时先进先出,找出最先入库的批次
  792. func FindLastWarehousingInfoByID(drug_id int64) (info models.WarehousingInfo, err error) {
  793. 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
  794. return info, err
  795. }
  796. //
  797. //// 出库时先进先出,找出最先入库的批次
  798. //func (drug_id int64) (info models.WarehousingInfo, err error) {
  799. // 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
  800. // return info, err
  801. //}
  802. func FindLastDrugWarehousingInfo(order string) (info models.DrugWarehouseInfo, err error) {
  803. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  804. return info, err
  805. }
  806. 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) {
  807. offset := (page - 1) * limit
  808. db := readDb.Model(&models.Warehousing{})
  809. db = db.Where("xt_warehouse.org_id = ? AND xt_warehouse.status = 1 AND xt_warehouse.type = ?", orgId, types)
  810. if len(keywords) > 0 {
  811. likeKey := "%" + keywords + "%"
  812. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  813. 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")
  814. }
  815. if startTime > 0 {
  816. db = db.Where("xt_warehouse.operation_time >=?", startTime)
  817. }
  818. if endTime > 0 {
  819. db = db.Where("xt_warehouse.operation_time<= ?", endTime)
  820. }
  821. if storehouse_id > 0 {
  822. db = db.Where("xt_warehouse.storehouse_id = ?", storehouse_id)
  823. }
  824. if check_type == 0 {
  825. db = db.Where("xt_warehouse.is_check = 0 or xt_warehouse.is_check = 1 or xt_warehouse.is_check = 2")
  826. }
  827. if check_type > 0 {
  828. db = db.Where("xt_warehouse.is_check = ?", check_type)
  829. }
  830. err = db.Count(&total).Offset(offset).Limit(limit).Order("xt_warehouse.ctime desc").Find(&list).Error
  831. return
  832. }
  833. func GetGoodInforByGoodName(keyword string, orgid int64) (goodInfo []*models.GoodInfo, err error) {
  834. db := XTReadDB().Model(&goodInfo).Where("status = 1")
  835. if orgid > 0 {
  836. db = db.Where("org_id = ?", orgid)
  837. }
  838. if len(keyword) > 0 {
  839. likeKey := "%" + keyword + "%"
  840. db = db.Where("good_name like ?", likeKey)
  841. }
  842. err = db.Find(&goodInfo).Error
  843. return goodInfo, err
  844. }
  845. func GetWarehoureOrderInfoByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  846. db := XTReadDB().Model(&info).Where("status =1")
  847. if len(good_id) > 0 {
  848. db = db.Where("good_id in (?)", good_id)
  849. }
  850. if startime > 0 {
  851. db = db.Where("ctime >=?", startime)
  852. }
  853. if endtime > 0 {
  854. db = db.Where("ctime<=?", endtime)
  855. }
  856. if orgid > 0 {
  857. db = db.Where("org_id = ?", orgid)
  858. }
  859. if storehouse_id > 0 {
  860. db = db.Where("storehouse_id = ?", storehouse_id)
  861. }
  862. err = db.Find(&info).Error
  863. return info, err
  864. }
  865. func GetWarehoureOrderOutByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehouseOutInfo, err error) {
  866. db := XTReadDB().Model(&info).Where("status =1")
  867. if len(good_id) > 0 {
  868. db = db.Where("good_id in (?)", good_id)
  869. }
  870. if startime > 0 {
  871. db = db.Where("ctime >=?", startime)
  872. }
  873. if endtime > 0 {
  874. db = db.Where("ctime<=?", endtime)
  875. }
  876. if orgid > 0 {
  877. db = db.Where("org_id = ?", orgid)
  878. }
  879. if storehouse_id > 0 {
  880. db = db.Where("storehouse_id = ?", storehouse_id)
  881. }
  882. err = db.Find(&info).Error
  883. return info, err
  884. }
  885. type VMDrugWarehouse struct {
  886. ID int64 `gorm:"column:id" json:"id" form:"id"`
  887. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  888. OperationTime int64 `gorm:"column:operation_time" json:"operation_time" form:"operation_time"`
  889. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  890. Creater int64 `gorm:"column:creater" json:"creater" form:"creater"`
  891. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  892. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  893. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  894. Status int64 `gorm:"column:status" json:"status" form:"status"`
  895. WarehousingTime int64 `gorm:"column:warehousing_time" json:"warehousing_time" form:"warehousing_time"`
  896. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  897. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  898. Type int64 `gorm:"column:type" json:"type" form:"type"`
  899. Manufacturers *models.Manufacturer `gorm:"ForeignKey:Manufacturer;AssociationForeignKey:ID" json:"manufacturers"`
  900. Dealers *models.Dealer `gorm:"ForeignKey:Dealer;AssociationForeignKey:ID" json:"dealers"`
  901. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  902. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  903. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  904. IsCheck int64 `gorm:"column:is_check" json:"is_check" form:"is_check"`
  905. Checker int64 `gorm:"column:checker" json:"checker" form:"checker"`
  906. }
  907. func (VMDrugWarehouse) TableName() string {
  908. return "xt_drug_warehouse"
  909. }
  910. type VMDrugWarehouseInfo struct {
  911. ID int64 `gorm:"column:id" json:"id" form:"id"`
  912. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id" form:"warehousing_id"`
  913. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  914. Number string `gorm:"column:number" json:"number" form:"number"`
  915. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  916. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  917. WarehousingCount int64 `gorm:"column:warehousing_count" json:"warehousing_count" form:"warehousing_count"`
  918. Price float64 `gorm:"column:price" json:"price" form:"price"`
  919. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  920. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  921. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  922. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  923. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  924. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  925. Status int64 `gorm:"column:status" json:"status" form:"status"`
  926. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  927. IsReturn int64 `gorm:"column:is_return" json:"is_return" form:"is_return"`
  928. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  929. Type int64 `gorm:"column:type" json:"type" form:"type"`
  930. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  931. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  932. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  933. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  934. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  935. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  936. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  937. SupplyWarehouseDetailInfo int64 `gorm:"column:supply_warehouse_detail_info" json:"supply_warehouse_detail_info" form:"supply_warehouse_detail_info"`
  938. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  939. }
  940. func (VMDrugWarehouseInfo) TableName() string {
  941. return "xt_drug_warehouse_info"
  942. }
  943. type Drug struct {
  944. ID int64 `gorm:"column:id" json:"id" form:"id"`
  945. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  946. Pinyin string `gorm:"column:pinyin" json:"pinyin" form:"pinyin"`
  947. Wubi string `gorm:"column:wubi" json:"wubi" form:"wubi"`
  948. DrugAlias string `gorm:"column:drug_alias" json:"drug_alias" form:"drug_alias"`
  949. DrugAliasPinyin string `gorm:"column:drug_alias_pinyin" json:"drug_alias_pinyin" form:"drug_alias_pinyin"`
  950. DrugAliasWubi string `gorm:"column:drug_alias_wubi" json:"drug_alias_wubi" form:"drug_alias_wubi"`
  951. DrugCategory int64 `gorm:"column:drug_category" json:"drug_category" form:"drug_category"`
  952. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  953. DrugType int64 `gorm:"column:drug_type" json:"drug_type" form:"drug_type"`
  954. DrugStockLimit string `gorm:"column:drug_stock_limit" json:"drug_stock_limit" form:"drug_stock_limit"`
  955. DrugOriginPlace string `gorm:"column:drug_origin_place" json:"drug_origin_place" form:"drug_origin_place"`
  956. DrugDosageForm int64 `gorm:"column:drug_dosage_form" json:"drug_dosage_form" form:"drug_dosage_form"`
  957. MedicalInsuranceLevel int64 `gorm:"column:medical_insurance_level" json:"medical_insurance_level" form:"medical_insurance_level"`
  958. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  959. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  960. UnitMatrixing string `gorm:"column:unit_matrixing" json:"unit_matrixing" form:"unit_matrixing"`
  961. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  962. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  963. DrugControl int64 `gorm:"column:drug_control" json:"drug_control" form:"drug_control"`
  964. Number string `gorm:"column:number" json:"number" form:"number"`
  965. DrugClassify string `gorm:"column:drug_classify" json:"drug_classify" form:"drug_classify"`
  966. DrugDose float64 `gorm:"column:drug_dose" json:"drug_dose" form:"drug_dose"`
  967. DrugDoseUnit int64 `gorm:"column:drug_dose_unit" json:"drug_dose_unit" form:"drug_dose_unit"`
  968. MedicalInsuranceNumber string `gorm:"column:medical_insurance_number" json:"medical_insurance_number" form:"medical_insurance_number"`
  969. ProvincesCode string `gorm:"column:provinces_code" json:"provinces_code" form:"provinces_code"`
  970. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  971. PharmacologyCategory int64 `gorm:"column:pharmacology_category" json:"pharmacology_category" form:"pharmacology_category"`
  972. StatisticsCategory int64 `gorm:"column:statistics_category" json:"statistics_category" form:"statistics_category"`
  973. Code string `gorm:"column:code" json:"code" form:"code"`
  974. IsSpecialDiseases int64 `gorm:"column:is_special_diseases" json:"is_special_diseases" form:"is_special_diseases"`
  975. IsRecord int64 `gorm:"column:is_record" json:"is_record" form:"is_record"`
  976. Agent string `gorm:"column:agent" json:"agent" form:"agent"`
  977. DrugStatus string `gorm:"column:drug_status" json:"drug_status" form:"drug_status"`
  978. LimitRemark string `gorm:"column:limit_remark" json:"limit_remark" form:"limit_remark"`
  979. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  980. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  981. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  982. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  983. Label int64 `gorm:"column:label" json:"label" form:"label"`
  984. Sort int64 `gorm:"column:sort" json:"sort" form:"sort"`
  985. IsUseDoctorAdvice int64 `gorm:"column:is_use_doctor_advice" json:"is_use_doctor_advice" form:"is_use_doctor_advice"`
  986. IsDefault int64 `gorm:"column:is_default" json:"is_default" form:"is_default"`
  987. IsChargePredict int64 `gorm:"column:is_charge_predict" json:"is_charge_predict" form:"is_charge_predict"`
  988. IsStatisticsWork int64 `gorm:"column:is_statistics_work" json:"is_statistics_work" form:"is_statistics_work"`
  989. IsChargeUse int64 `gorm:"column:is_charge_use" json:"is_charge_use" form:"is_charge_use"`
  990. Status int64 `gorm:"column:status" json:"status" form:"status"`
  991. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  992. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  993. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  994. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  995. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  996. DoseCode string `gorm:"column:dose_code" json:"dose_code" form:"dose_code"`
  997. RecordDate int64 `gorm:"column:record_date" json:"record_date" form:"record_date"`
  998. DrugRemark string `gorm:"column:drug_remark" json:"drug_remark" form:"drug_remark"`
  999. SocialSecurityDirectoryCode string `gorm:"column:social_security_directory_code" json:"social_security_directory_code" form:"social_security_directory_code"`
  1000. IsMark int64 `gorm:"column:is_mark" json:"is_mark" form:"is_mark"`
  1001. PrescriptionMark int64 `gorm:"column:prescription_mark" json:"prescription_mark" form:"prescription_mark"`
  1002. HospApprFlag int64 `gorm:"column:hosp_appr_flag" json:"hosp_appr_flag" form:"hosp_appr_flag"`
  1003. LmtUsedFlag int64 `gorm:"column:lmt_used_flag" json:"lmt_used_flag" form:"lmt_used_flag"`
  1004. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1005. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  1006. MinNumber int64 `gorm:"column:min_number" json:"min_number" form:"min_number"`
  1007. Dose string `gorm:"column:dose" json:"dose" form:"dose"`
  1008. DoseUnit string `gorm:"column:dose_unit" json:"dose_unit" form:"dose_unit"`
  1009. DrugDay string `gorm:"column:drug_day" json:"drug_day" form:"drug_day"`
  1010. MinPrice float64 `gorm:"column:min_price" json:"min_price" form:"min_price"`
  1011. }
  1012. func (Drug) TableName() string {
  1013. return "xt_base_drug"
  1014. }
  1015. func FindAllDrugWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*VMDrugWarehouse, total int64, err error) {
  1016. db := readDb.Model(&VMDrugWarehouse{})
  1017. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1018. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1019. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1020. if len(keywords) > 0 {
  1021. likeKey := "%" + keywords + "%"
  1022. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1023. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse.manufacturer")
  1024. 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")
  1025. }
  1026. if startTime > 0 {
  1027. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1028. }
  1029. if endTime > 0 {
  1030. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1031. }
  1032. db = db.Count(&total)
  1033. offset := (page - 1) * limit
  1034. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1035. return
  1036. }
  1037. 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) {
  1038. db := readDb.Model(&VMDrugWarehouse{})
  1039. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1040. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1041. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1042. if len(keywords) > 0 {
  1043. likeKey := "%" + keywords + "%"
  1044. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1045. 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")
  1046. }
  1047. if startTime > 0 {
  1048. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1049. }
  1050. if endTime > 0 {
  1051. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1052. }
  1053. if storehouse_id > 0 {
  1054. db = db.Where("xt_drug_warehouse.storehouse_id = ?", storehouse_id)
  1055. }
  1056. if check_type > 0 {
  1057. db = db.Where("xt_drug_warehouse.is_check = ?", check_type)
  1058. }
  1059. db = db.Count(&total)
  1060. offset := (page - 1) * limit
  1061. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1062. return
  1063. }
  1064. func FindWarehousingInfoById(id int64) (list []*models.WarehousingInfo, err error) {
  1065. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1066. return list, err
  1067. }
  1068. func FindWarehousingInfoByIdOne(id []int64) (list []*models.WarehousingInfo, err error) {
  1069. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id in(?) AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1070. return list, err
  1071. }
  1072. func FindDrugWarehousingInfoById(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1073. 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
  1074. return list, err
  1075. }
  1076. func FindDrugWarehousingInfoByIdThree(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1077. err = readDb.Model(&VMDrugWarehouseInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehousing_id = ? AND status = 1", id).Find(&list).Error
  1078. return list, err
  1079. }
  1080. func FindDrugWarehousingInfoByIdOne(id []string, org_id int64) (list []*models.StDrugWarehouseInfo, err error) {
  1081. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  1082. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  1083. fmt.Println(table)
  1084. if len(id) > 0 {
  1085. db = db.Where("x.warehousing_id in(?)", id)
  1086. }
  1087. if org_id > 0 {
  1088. db = db.Where("x.org_id = ?", org_id)
  1089. }
  1090. 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
  1091. return list, err
  1092. }
  1093. func FindWarehousingById(id int64) (warehousing models.Warehousing, err error) {
  1094. err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1095. return warehousing, err
  1096. }
  1097. func FindWarehousingByIdOne(id []string) (warehousing []*models.Warehousing, err error) {
  1098. err = readDb.Model(&models.Warehousing{}).Where("id in(?) and status = 1", id).Find(&warehousing).Error
  1099. //err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1100. return warehousing, err
  1101. }
  1102. func FindDrugWarehousingById(id int64, org_id int64) (warehousing VMDrugWarehouse, err error) {
  1103. 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
  1104. return warehousing, err
  1105. }
  1106. func FindDrugWarehousingByIdOne(id []string, org_id int64) (warehousing []*models.DrugWarehouse, err error) {
  1107. db := XTReadDB().Table("xt_drug_warehouse as x").Where("x.status = 1")
  1108. if len(id) > 0 {
  1109. db = db.Where("x.id in(?)", id)
  1110. }
  1111. if org_id > 0 {
  1112. db = db.Where("x.org_id = ?", org_id)
  1113. }
  1114. 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
  1115. return warehousing, err
  1116. }
  1117. //func FindDrugWarehousingById(id int64) (warehousing models.DrugWarehouse, err error) {
  1118. // err = readDb.Model(&models.DrugWarehouse{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1119. // return warehousing, err
  1120. //
  1121. //}
  1122. func GetInfoByIds(ids []string) (info []*models.WarehousingInfo, err error) {
  1123. err = readDb.Model(&models.WarehousingInfo{}).Preload("Warehousing", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1124. return
  1125. }
  1126. func GetWarehousOutInfoByIds(ids []string) (info []*models.WarehouseOutInfo, err error) {
  1127. err = readDb.Model(&models.WarehouseOutInfo{}).Preload("WarehouseOut", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1128. return
  1129. }
  1130. func CreateSalesReturnInfo(salesReturnInfo []*models.SalesReturnInfo) (err error) {
  1131. if len(salesReturnInfo) > 0 {
  1132. utx := writeDb.Begin()
  1133. if len(salesReturnInfo) > 0 {
  1134. 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 "
  1135. insertParams := make([]string, 0)
  1136. insertData := make([]interface{}, 0)
  1137. for _, info := range salesReturnInfo {
  1138. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1139. insertData = append(insertData, info.GoodId)
  1140. insertData = append(insertData, info.SalesReturnId)
  1141. insertData = append(insertData, info.GoodTypeId)
  1142. insertData = append(insertData, info.Count)
  1143. insertData = append(insertData, info.Price)
  1144. insertData = append(insertData, info.Total)
  1145. insertData = append(insertData, info.Ctime)
  1146. insertData = append(insertData, info.Status)
  1147. insertData = append(insertData, info.OrgId)
  1148. insertData = append(insertData, info.OrderNumber)
  1149. insertData = append(insertData, info.Type)
  1150. insertData = append(insertData, info.Dealer)
  1151. insertData = append(insertData, info.Manufacturer)
  1152. }
  1153. thisSQL += strings.Join(insertParams, ", ")
  1154. err = utx.Exec(thisSQL, insertData...).Error
  1155. if err != nil {
  1156. utx.Rollback()
  1157. return
  1158. }
  1159. }
  1160. utx.Commit()
  1161. }
  1162. return
  1163. }
  1164. func CreateDrugSalesReturnInfo(salesReturnInfo []*models.DrugSalesReturnInfo) (err error) {
  1165. if len(salesReturnInfo) > 0 {
  1166. utx := writeDb.Begin()
  1167. if len(salesReturnInfo) > 0 {
  1168. 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 "
  1169. insertParams := make([]string, 0)
  1170. insertData := make([]interface{}, 0)
  1171. for _, info := range salesReturnInfo {
  1172. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1173. insertData = append(insertData, info.DrugId)
  1174. insertData = append(insertData, info.SalesReturnId)
  1175. insertData = append(insertData, info.Count)
  1176. insertData = append(insertData, info.Price)
  1177. insertData = append(insertData, info.Total)
  1178. insertData = append(insertData, info.Ctime)
  1179. insertData = append(insertData, info.Status)
  1180. insertData = append(insertData, info.OrgId)
  1181. insertData = append(insertData, info.OrderNumber)
  1182. insertData = append(insertData, info.Type)
  1183. insertData = append(insertData, info.Dealer)
  1184. insertData = append(insertData, info.Manufacturer)
  1185. insertData = append(insertData, info.RetailPrice)
  1186. insertData = append(insertData, info.RetailTotalPrice)
  1187. }
  1188. thisSQL += strings.Join(insertParams, ", ")
  1189. err = utx.Exec(thisSQL, insertData...).Error
  1190. if err != nil {
  1191. utx.Rollback()
  1192. return
  1193. }
  1194. }
  1195. utx.Commit()
  1196. }
  1197. return
  1198. }
  1199. func FindAllSalesReturnTotal(org_id int64) (total int64, err error) {
  1200. err = readDb.Model(&models.SalesReturn{}).Where("org_id = ?", org_id).Count(&total).Error
  1201. return total, err
  1202. }
  1203. func FindAllCancelStockTotal(org_id int64) (total int64, err error) {
  1204. err = readDb.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1205. return total, err
  1206. }
  1207. func FindNewAllCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1208. err = tx.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1209. if err != gorm.ErrRecordNotFound {
  1210. if err != nil {
  1211. tx.Rollback()
  1212. return total, err
  1213. }
  1214. }
  1215. return total, err
  1216. }
  1217. func FindAllDrugCancelStockTotal(org_id int64) (total int64, err error) {
  1218. err = readDb.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1219. return total, err
  1220. }
  1221. func FindNewAllDrugCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1222. err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1223. if err != gorm.ErrRecordNotFound {
  1224. if err != nil {
  1225. tx.Rollback()
  1226. return total, err
  1227. }
  1228. }
  1229. return total, err
  1230. }
  1231. //func FindAllDrugCancelStockTotalTX(org_id int64,tx *gorm.DB) (total int64, err error) {
  1232. // err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1233. // return total, err
  1234. //}
  1235. func FindAllReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.SalesReturn, total int64, err error) {
  1236. db := readDb.Model(&models.SalesReturn{})
  1237. db = db.Where("xt_sales_return.org_id = ? AND xt_sales_return.status = 1 AND xt_sales_return.type = ?", orgId, types)
  1238. if len(keywords) > 0 {
  1239. likeKey := "%" + keywords + "%"
  1240. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  1241. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_sales_return.manufacturer")
  1242. 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")
  1243. }
  1244. if startTime > 0 {
  1245. db = db.Where("xt_sales_return.opera_time >=?", startTime)
  1246. }
  1247. if endTime > 0 {
  1248. db = db.Where("xt_sales_return.opera_time<= ?", endTime)
  1249. }
  1250. db = db.Count(&total)
  1251. offset := (page - 1) * limit
  1252. err = db.Offset(offset).Limit(limit).Order("xt_sales_return.ctime desc").Find(&list).Error
  1253. return
  1254. }
  1255. func FindAllDrugReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugSalesReturn, total int64, err error) {
  1256. db := readDb.Model(&models.DrugSalesReturn{})
  1257. db = db.Where("xt_drug_sales_return.org_id = ? AND xt_drug_sales_return.status = 1 ", orgId)
  1258. if len(keywords) > 0 {
  1259. likeKey := "%" + keywords + "%"
  1260. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_sales_return.creater")
  1261. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_sales_return.manufacturer")
  1262. 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")
  1263. }
  1264. if startTime > 0 {
  1265. db = db.Where("xt_drug_sales_return.opera_time >=?", startTime)
  1266. }
  1267. if endTime > 0 {
  1268. db = db.Where("xt_drug_sales_return.opera_time<= ?", endTime)
  1269. }
  1270. db = db.Count(&total)
  1271. offset := (page - 1) * limit
  1272. err = db.Offset(offset).Limit(limit).Order("xt_drug_sales_return.ctime desc").Find(&list).Error
  1273. return
  1274. }
  1275. func FindReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  1276. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  1277. return list, err
  1278. }
  1279. func FindCancelStockInfoById(id int64) (list []*models.CancelStockInfo, err error) {
  1280. err = readDb.Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? AND status = 1", id).Preload("WarehousingInfo", "status = 1").Preload("GoodInfo", "status = 1").Find(&list).Error
  1281. return list, err
  1282. }
  1283. type DrugCancelStockInfo struct {
  1284. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1285. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1286. CancelStockId int64 `gorm:"column:cancel_stock_id" json:"cancel_stock_id" form:"cancel_stock_id"`
  1287. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1288. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1289. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1290. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1291. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1292. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1293. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1294. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1295. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1296. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  1297. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1298. Dealer string `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1299. Manufacturer string `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1300. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1301. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1302. Number string `gorm:"column:number" json:"number" form:"number"`
  1303. RegisterAccount string `gorm:"column:register_account" json:"register_account" form:"register_account"`
  1304. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1305. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1306. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1307. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1308. BatchNumberId int64 `gorm:"column:batch_number_id" json:"batch_number_id" form:"batch_number_id"`
  1309. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1310. DrugWarehouseInfo []*models.XtDrugWarehouseInfo `gorm:"ForeignKey:DrugId;AssociationForeignKey:DrugId" json:"drug_warehouse_info"`
  1311. }
  1312. func (DrugCancelStockInfo) TableName() string {
  1313. return "xt_drug_cancel_stock_info"
  1314. }
  1315. func FindDrugCancelStockInfoById(id int64, org_id int64) (list []*DrugCancelStockInfo, err error) {
  1316. 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
  1317. return list, err
  1318. }
  1319. func FindAllWarehousingInfo(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehousingInfo, total int64, err error) {
  1320. db := readDb.Model(&models.WarehousingInfo{})
  1321. db = db.Where("org_id = ? AND status = 1", orgId)
  1322. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  1323. if startTime > 0 {
  1324. db = db.Where("ctime >=?", startTime)
  1325. }
  1326. if endTime > 0 {
  1327. db = db.Where("ctime<= ?", endTime)
  1328. }
  1329. db = db.Count(&total)
  1330. offset := (page - 1) * limit
  1331. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1332. return list, total, err
  1333. }
  1334. func FindAllWarehouseOut(org_id int64) (total int64, err error) {
  1335. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1336. return total, err
  1337. }
  1338. func FindAllDrugWarehouseOut(org_id int64) (total int64, err error) {
  1339. err = readDb.Model(&models.DrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1340. return total, err
  1341. }
  1342. func AddSigleWarehouseOut(warehouseOut *models.WarehouseOut) error {
  1343. err := writeDb.Create(&warehouseOut).Error
  1344. return err
  1345. }
  1346. func AddNewSigleWarehouseOut(warehouseOut *models.WarehouseOut, tx *gorm.DB) error {
  1347. err := tx.Create(&warehouseOut).Error
  1348. if err != nil {
  1349. tx.Rollback()
  1350. return err
  1351. }
  1352. return err
  1353. }
  1354. func UpdateSingleWarehoseOut(id int64, storehouse_id int64) error {
  1355. err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1356. return err
  1357. }
  1358. func AddSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut) error {
  1359. err := writeDb.Create(&warehouseOut).Error
  1360. return err
  1361. }
  1362. func AddNewSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut, tx *gorm.DB) error {
  1363. err := tx.Create(&warehouseOut).Error
  1364. if err != nil {
  1365. tx.Rollback()
  1366. return err
  1367. }
  1368. return err
  1369. }
  1370. func UpdateSingleDrugWarehouseOut(id int64, storehouse_id int64) error {
  1371. err := XTWriteDB().Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1372. return err
  1373. }
  1374. func CreateWarehousingOutInfo(warehouseOutInfo []*models.WarehouseOutInfo) (err error) {
  1375. if len(warehouseOutInfo) > 0 {
  1376. utx := writeDb.Begin()
  1377. if len(warehouseOutInfo) > 0 {
  1378. 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 "
  1379. insertParams := make([]string, 0)
  1380. insertData := make([]interface{}, 0)
  1381. for _, info := range warehouseOutInfo {
  1382. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1383. insertData = append(insertData, info.WarehouseOutId)
  1384. insertData = append(insertData, info.GoodId)
  1385. insertData = append(insertData, info.GoodTypeId)
  1386. insertData = append(insertData, info.ProductDate)
  1387. insertData = append(insertData, info.ExpiryDate)
  1388. insertData = append(insertData, info.Count)
  1389. insertData = append(insertData, info.Price)
  1390. insertData = append(insertData, info.TotalPrice)
  1391. insertData = append(insertData, info.Remark)
  1392. insertData = append(insertData, info.Ctime)
  1393. insertData = append(insertData, info.Status)
  1394. insertData = append(insertData, info.OrgId)
  1395. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1396. insertData = append(insertData, info.Type)
  1397. insertData = append(insertData, info.Dealer)
  1398. insertData = append(insertData, info.Manufacturer)
  1399. insertData = append(insertData, info.Number)
  1400. insertData = append(insertData, info.LicenseNumber)
  1401. }
  1402. thisSQL += strings.Join(insertParams, ", ")
  1403. err = utx.Exec(thisSQL, insertData...).Error
  1404. if err != nil {
  1405. utx.Rollback()
  1406. return
  1407. }
  1408. }
  1409. utx.Commit()
  1410. }
  1411. return
  1412. }
  1413. func CreateDrugWarehousingOutInfo(warehouseOutInfo []*models.DrugWarehouseOutInfo) (err error) {
  1414. if len(warehouseOutInfo) > 0 {
  1415. utx := writeDb.Begin()
  1416. if len(warehouseOutInfo) > 0 {
  1417. 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 "
  1418. insertParams := make([]string, 0)
  1419. insertData := make([]interface{}, 0)
  1420. for _, info := range warehouseOutInfo {
  1421. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1422. insertData = append(insertData, info.WarehouseOutId)
  1423. insertData = append(insertData, info.DrugId)
  1424. insertData = append(insertData, info.ProductDate)
  1425. insertData = append(insertData, info.ExpiryDate)
  1426. insertData = append(insertData, info.Count)
  1427. insertData = append(insertData, info.Price)
  1428. insertData = append(insertData, info.TotalPrice)
  1429. insertData = append(insertData, info.Remark)
  1430. insertData = append(insertData, info.Ctime)
  1431. insertData = append(insertData, info.Status)
  1432. insertData = append(insertData, info.OrgId)
  1433. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1434. insertData = append(insertData, info.Type)
  1435. insertData = append(insertData, info.Dealer)
  1436. insertData = append(insertData, info.Manufacturer)
  1437. insertData = append(insertData, info.RetailPrice)
  1438. insertData = append(insertData, info.RetailTotalPrice)
  1439. insertData = append(insertData, info.Number)
  1440. insertData = append(insertData, info.BatchNumber)
  1441. insertData = append(insertData, info.CountUnit)
  1442. }
  1443. thisSQL += strings.Join(insertParams, ", ")
  1444. err = utx.Exec(thisSQL, insertData...).Error
  1445. if err != nil {
  1446. utx.Rollback()
  1447. return
  1448. }
  1449. }
  1450. utx.Commit()
  1451. }
  1452. return
  1453. }
  1454. func FindLastWarehousingOutInfo(order string) (info models.WarehouseOutInfo, err error) {
  1455. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1456. return info, err
  1457. }
  1458. func FindLastDrugWarehousingOutInfo(order string) (info models.DrugWarehouseOutInfo, err error) {
  1459. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1460. return info, err
  1461. }
  1462. func FindAllWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.WarehouseOut, total int64, err error) {
  1463. db := readDb.Model(&models.WarehouseOut{})
  1464. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1465. if len(keywords) > 0 {
  1466. likeKey := "%" + keywords + "%"
  1467. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1468. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse_out.manufacturer")
  1469. 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")
  1470. }
  1471. if startTime > 0 {
  1472. db = db.Where("xt_warehouse_out.ctime >=?", startTime)
  1473. }
  1474. if endTime > 0 {
  1475. db = db.Where("xt_warehouse_out.ctime<= ?", endTime)
  1476. }
  1477. db = db.Count(&total)
  1478. offset := (page - 1) * limit
  1479. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1480. return list, total, err
  1481. }
  1482. 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) {
  1483. db := readDb.Model(&models.WarehouseOut{})
  1484. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1485. if len(keywords) > 0 {
  1486. likeKey := "%" + keywords + "%"
  1487. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1488. 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")
  1489. }
  1490. if startTime > 0 {
  1491. db = db.Where("xt_warehouse_out.warehouse_out_time >=?", startTime)
  1492. }
  1493. if endTime > 0 {
  1494. db = db.Where("xt_warehouse_out.warehouse_out_time<= ?", endTime)
  1495. }
  1496. if storehouse_id > 0 {
  1497. db = db.Where("xt_warehouse_out.storehouse_id = ?", storehouse_id)
  1498. }
  1499. if way_type == 0 {
  1500. 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")
  1501. }
  1502. if way_type == 1 {
  1503. db = db.Where("xt_warehouse_out.is_sys = 0")
  1504. }
  1505. if way_type == 2 {
  1506. db = db.Where("xt_warehouse_out.is_sys = 1")
  1507. }
  1508. if way_type == 3 {
  1509. db = db.Where("xt_warehouse_out.is_sys = 12")
  1510. }
  1511. if way_type == 4 {
  1512. db = db.Where("xt_warehouse_out.is_sys = 5")
  1513. }
  1514. if check_type > 0 {
  1515. db = db.Where("xt_warehouse_out.is_check = ?", check_type)
  1516. }
  1517. if check_type == 0 {
  1518. db = db.Where("xt_warehouse_out.is_check = 1 or xt_warehouse_out.is_check = 2")
  1519. }
  1520. db = db.Count(&total)
  1521. offset := (page - 1) * limit
  1522. if orgId == 10191 {
  1523. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1524. } else {
  1525. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1526. }
  1527. return list, total, err
  1528. }
  1529. func FindAllDrugWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugWarehouseOut, total int64, err error) {
  1530. db := readDb.Model(&models.DrugWarehouseOut{})
  1531. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1532. if len(keywords) > 0 {
  1533. likeKey := "%" + keywords + "%"
  1534. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1535. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse_out.manufacturer")
  1536. 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")
  1537. }
  1538. if startTime > 0 {
  1539. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1540. }
  1541. if endTime > 0 {
  1542. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1543. }
  1544. db = db.Count(&total)
  1545. offset := (page - 1) * limit
  1546. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1547. return list, total, err
  1548. }
  1549. 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) {
  1550. db := readDb.Model(&models.DrugWarehouseOut{})
  1551. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1552. if len(keywords) > 0 {
  1553. likeKey := "%" + keywords + "%"
  1554. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1555. 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")
  1556. }
  1557. if startTime > 0 {
  1558. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1559. }
  1560. if endTime > 0 {
  1561. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1562. }
  1563. if storehouse_id > 0 {
  1564. db = db.Where("xt_drug_warehouse_out.storehouse_id = ?", storehouse_id)
  1565. }
  1566. if check_type > 0 {
  1567. db = db.Where("xt_drug_warehouse_out.is_check = ?", check_type)
  1568. }
  1569. if way_type == 0 {
  1570. 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")
  1571. }
  1572. if way_type == 1 {
  1573. db = db.Where("xt_drug_warehouse_out.is_sys = 0")
  1574. }
  1575. if way_type == 2 {
  1576. db = db.Where("xt_drug_warehouse_out.is_sys = 1")
  1577. }
  1578. if way_type == 3 {
  1579. db = db.Where("xt_drug_warehouse_out.is_sys = 12")
  1580. }
  1581. if way_type == 4 {
  1582. db = db.Where("xt_drug_warehouse_out.is_sys = 15")
  1583. }
  1584. db = db.Count(&total)
  1585. offset := (page - 1) * limit
  1586. if orgId == 10191 {
  1587. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1588. } else {
  1589. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1590. }
  1591. return list, total, err
  1592. }
  1593. func FindWarehouseOutInfoById(id int64) (list []*models.WarehouseOutInfo, err error) {
  1594. 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
  1595. return list, err
  1596. }
  1597. type DrugWarehouseOutInfo struct {
  1598. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1599. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  1600. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1601. WarehousingOutTarget int64 `gorm:"column:warehousing_out_target" json:"warehousing_out_target" form:"warehousing_out_target"`
  1602. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1603. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  1604. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1605. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  1606. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1607. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1608. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1609. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1610. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1611. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1612. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1613. IsCancel int64 `gorm:"column:is_cancel" json:"is_cancel" form:"is_cancel"`
  1614. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  1615. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1616. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1617. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1618. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  1619. SysRecordTime int64 `gorm:"column:sys_record_time" json:"sys_record_time" form:"sys_record_time"`
  1620. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1621. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1622. Number string `gorm:"column:number" json:"number" form:"number"`
  1623. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1624. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1625. WarehouseInfoId int64 `gorm:"column:warehouse_info_id" json:"warehouse_info_id" form:"warehouse_info_id"`
  1626. SupplyCancelOutId int64 `gorm:"column:supply_cancel_out_id" json:"supply_cancel_out_id" form:"supply_cancel_out_id"`
  1627. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1628. AdminUserId int64 `gorm:"column:admin_user_id" json:"admin_user_id" form:"admin_user_id"`
  1629. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  1630. StockCount string `gorm:"column:stock_count" json:"stock_count" form:"stock_count"`
  1631. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  1632. }
  1633. func (DrugWarehouseOutInfo) TableName() string {
  1634. return "xt_drug_warehouse_out_info"
  1635. }
  1636. func FindDrugWarehouseOutInfoById(id int64, org_id int64) (list []*DrugWarehouseOutInfo, err error) {
  1637. err = readDb.Model(&DrugWarehouseOutInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehouse_out_id = ? AND status = 1 ", id).Find(&list).Error
  1638. return list, err
  1639. }
  1640. func FindWarehouseInfoByGoodId(good_id int64) (int64, *models.WarehousingInfo) {
  1641. info := &models.WarehousingInfo{}
  1642. type WarehouseInfoCountAmount struct {
  1643. Total int64
  1644. }
  1645. var result WarehouseInfoCountAmount
  1646. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(warehousing_count) as total").Scan(&result)
  1647. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).First(info)
  1648. return result.Total, info
  1649. }
  1650. func FindAllSalesReturnCountByGoodId(good_id int64) (int64, error) {
  1651. type SalesReturnCountAmount struct {
  1652. Total int64
  1653. }
  1654. var result SalesReturnCountAmount
  1655. err = readDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1656. return result.Total, err
  1657. }
  1658. func FindAllWarehouseOutInfoByGoodId(good_id int64) (int64, error) {
  1659. type WarehouseOutInfoCountAmount struct {
  1660. Total int64
  1661. }
  1662. var result WarehouseOutInfoCountAmount
  1663. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1664. return result.Total, err
  1665. }
  1666. func FindAllCancleStockInfoByGoodId(good_id int64) (int64, error) {
  1667. type CancleStockoCountAmount struct {
  1668. Total int64
  1669. }
  1670. var result CancleStockoCountAmount
  1671. err = readDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1672. return result.Total, err
  1673. }
  1674. func FindWarehouseInfoByGoodTypeId(good_type_id int64) (models.WarehousingInfo, error) {
  1675. info := models.WarehousingInfo{}
  1676. err := readDb.Model(&models.WarehousingInfo{}).Where("good_type_id = ? AND status = 1", good_type_id).First(&info).Error
  1677. return info, err
  1678. }
  1679. func GetCancelStockDetailByOrderNumber(ordernumber string, orgid int64) (*models.CancelStock, error) {
  1680. stock := models.CancelStock{}
  1681. err := XTReadDB().Where("order_number = ? and org_id = ? and status = 1", ordernumber, orgid).Find(&stock).Error
  1682. if err == gorm.ErrRecordNotFound {
  1683. return nil, err
  1684. }
  1685. if err != nil {
  1686. return nil, err
  1687. }
  1688. return &stock, nil
  1689. }
  1690. func GetCancelStockDetailByOrderNumberOne(return_time int64, orgid int64) (*models.CancelStock, error) {
  1691. stock := models.CancelStock{}
  1692. err := XTReadDB().Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  1693. if err == gorm.ErrRecordNotFound {
  1694. return nil, err
  1695. }
  1696. if err != nil {
  1697. return nil, err
  1698. }
  1699. return &stock, nil
  1700. }
  1701. func GetNewCancelStockDetailByOrderNumberOne(return_time int64, orgid int64, tx *gorm.DB) (*models.CancelStock, error) {
  1702. stock := models.CancelStock{}
  1703. err := tx.Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  1704. if err != gorm.ErrRecordNotFound {
  1705. if err != nil {
  1706. tx.Rollback()
  1707. return &stock, err
  1708. }
  1709. }
  1710. return &stock, err
  1711. }
  1712. func AddSigleCancelStock(cancelStock *models.CancelStock) error {
  1713. err := writeDb.Create(&cancelStock).Error
  1714. return err
  1715. }
  1716. func AddNewSigleCancelStock(cancelStock *models.CancelStock, tx *gorm.DB) error {
  1717. err := tx.Create(&cancelStock).Error
  1718. if err != nil {
  1719. tx.Rollback()
  1720. return err
  1721. }
  1722. return err
  1723. }
  1724. func UpdateSigleCancelStock(id int64, storehouse_id int64) error {
  1725. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1726. return err
  1727. }
  1728. func GetLastCancelStockById(orgid int64) (models.CancelStock, error) {
  1729. stock := models.CancelStock{}
  1730. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1731. return stock, err
  1732. }
  1733. func GetNewLastCancelStockById(orgid int64, tx *gorm.DB) (models.CancelStock, error) {
  1734. stock := models.CancelStock{}
  1735. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1736. if err != gorm.ErrRecordNotFound {
  1737. if err != nil {
  1738. tx.Rollback()
  1739. return stock, err
  1740. }
  1741. }
  1742. return stock, err
  1743. }
  1744. func GetLastDrugCancelStockById(orgid int64) (models.DrugCancelStock, error) {
  1745. stock := models.DrugCancelStock{}
  1746. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1747. return stock, err
  1748. }
  1749. func GetNewLastDrugCancelStockById(orgid int64, tx *gorm.DB) (models.DrugCancelStock, error) {
  1750. stock := models.DrugCancelStock{}
  1751. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1752. if err != gorm.ErrRecordNotFound {
  1753. if err != nil {
  1754. tx.Rollback()
  1755. return stock, err
  1756. }
  1757. }
  1758. return stock, err
  1759. }
  1760. func GetManufactureById(id int64) (models.Manufacturer, error) {
  1761. manufacturer := models.Manufacturer{}
  1762. err := XTReadDB().Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  1763. return manufacturer, err
  1764. }
  1765. func GetNewManufactureById(id int64, tx *gorm.DB) (models.Manufacturer, error) {
  1766. manufacturer := models.Manufacturer{}
  1767. err := tx.Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  1768. if err != gorm.ErrRecordNotFound {
  1769. if err != nil {
  1770. tx.Rollback()
  1771. return manufacturer, err
  1772. }
  1773. }
  1774. return manufacturer, err
  1775. }
  1776. func GetDealerById(id int64) (models.Dealer, error) {
  1777. dealer := models.Dealer{}
  1778. err := XTReadDB().Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  1779. return dealer, err
  1780. }
  1781. func GetNewDealerById(id int64, tx *gorm.DB) (models.Dealer, error) {
  1782. dealer := models.Dealer{}
  1783. err := tx.Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  1784. if err != gorm.ErrRecordNotFound {
  1785. if err != nil {
  1786. tx.Rollback()
  1787. return dealer, err
  1788. }
  1789. }
  1790. return dealer, err
  1791. }
  1792. func AddSigleDrugCancelStock(cancelStock *models.DrugCancelStock) error {
  1793. err := writeDb.Create(&cancelStock).Error
  1794. return err
  1795. }
  1796. func AddNewSigleDrugCancelStock(cancelStock *models.DrugCancelStock, tx *gorm.DB) error {
  1797. err := tx.Create(&cancelStock).Error
  1798. if err != nil {
  1799. tx.Rollback()
  1800. return err
  1801. }
  1802. return err
  1803. }
  1804. func UpdateSingleDrugCancelStock(id int64, storehouse_id int64) error {
  1805. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1806. return err
  1807. }
  1808. //func AddSigleDrugCancelStockTX(cancelStock *models.DrugCancelStock,tx *gorm.DB) error {
  1809. // err := tx.Create(&cancelStock).Error
  1810. // return err
  1811. //}
  1812. func CreateCancelStockInfo(cancelStockInfo []*models.CancelStockInfo) (err error) {
  1813. if len(cancelStockInfo) > 0 {
  1814. utx := writeDb.Begin()
  1815. if len(cancelStockInfo) > 0 {
  1816. 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 "
  1817. insertParams := make([]string, 0)
  1818. insertData := make([]interface{}, 0)
  1819. for _, info := range cancelStockInfo {
  1820. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1821. insertData = append(insertData, info.GoodId)
  1822. insertData = append(insertData, info.CancelStockId)
  1823. insertData = append(insertData, info.GoodTypeId)
  1824. insertData = append(insertData, info.Count)
  1825. insertData = append(insertData, info.Price)
  1826. insertData = append(insertData, info.Total)
  1827. insertData = append(insertData, info.Ctime)
  1828. insertData = append(insertData, info.Status)
  1829. insertData = append(insertData, info.OrgId)
  1830. insertData = append(insertData, info.OrderNumber)
  1831. insertData = append(insertData, info.Type)
  1832. insertData = append(insertData, info.Dealer)
  1833. insertData = append(insertData, info.Manufacturer)
  1834. insertData = append(insertData, info.Number)
  1835. insertData = append(insertData, info.RegisterAccount)
  1836. insertData = append(insertData, info.Remark)
  1837. insertData = append(insertData, info.ProductDate)
  1838. insertData = append(insertData, info.ExpiryDate)
  1839. insertData = append(insertData, info.WarehouseInfoId)
  1840. insertData = append(insertData, info.WarehouseInfoId)
  1841. }
  1842. thisSQL += strings.Join(insertParams, ", ")
  1843. err = utx.Exec(thisSQL, insertData...).Error
  1844. if err != nil {
  1845. utx.Rollback()
  1846. return
  1847. }
  1848. }
  1849. utx.Commit()
  1850. }
  1851. return
  1852. }
  1853. func CreateCancelStockInfoTwo(info *models.DrugCancelStockInfo) error {
  1854. err := XTWriteDB().Create(&info).Error
  1855. return err
  1856. }
  1857. //func CreateCancelStockInfoTwoTX(info *models.DrugCancelStockInfo,tx *gorm.DB) error {
  1858. // err := tx.Create(&info).Error
  1859. // return err
  1860. //}
  1861. func GetLastStockInfoThree(drugid int64) (models.DrugCancelStockInfo, error) {
  1862. info := models.DrugCancelStockInfo{}
  1863. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  1864. return info, err
  1865. }
  1866. //func GetLastStockInfoThreeTX(drugid int64,tx *gorm.DB) (models.DrugCancelStockInfo, error) {
  1867. // info := models.DrugCancelStockInfo{}
  1868. // err := tx.Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  1869. // return info, err
  1870. //}
  1871. func CreateCancelStockInfoOne(info *models.CancelStockInfo) error {
  1872. err := XTWriteDB().Create(&info).Error
  1873. return err
  1874. }
  1875. func CreateNewCancelStockInfoOne(info *models.CancelStockInfo, tx *gorm.DB) error {
  1876. err := XTWriteDB().Create(&info).Error
  1877. if err != nil {
  1878. tx.Rollback()
  1879. return err
  1880. }
  1881. return err
  1882. }
  1883. func GetLastCancelStockInfoByGoodId(goodId int64) (models.CancelStockInfo, error) {
  1884. info := models.CancelStockInfo{}
  1885. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  1886. return info, err
  1887. }
  1888. func GetLastNewCancelStockInfoByGoodId(goodId int64, tx *gorm.DB) (models.CancelStockInfo, error) {
  1889. info := models.CancelStockInfo{}
  1890. err := tx.Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  1891. if err != gorm.ErrRecordNotFound {
  1892. if err != nil {
  1893. tx.Rollback()
  1894. return info, err
  1895. }
  1896. }
  1897. return info, err
  1898. }
  1899. func GetLastDrugCancelStockInfoById(drugid int64) (models.DrugCancelStockInfo, error) {
  1900. info := models.DrugCancelStockInfo{}
  1901. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Last(&info).Error
  1902. return info, err
  1903. }
  1904. func CreateDrugCancelStockInfo(cancelStockInfo []*models.DrugCancelStockInfo) (err error) {
  1905. if len(cancelStockInfo) > 0 {
  1906. utx := writeDb.Begin()
  1907. if len(cancelStockInfo) > 0 {
  1908. 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 "
  1909. insertParams := make([]string, 0)
  1910. insertData := make([]interface{}, 0)
  1911. for _, info := range cancelStockInfo {
  1912. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1913. insertData = append(insertData, info.DrugId)
  1914. insertData = append(insertData, info.CancelStockId)
  1915. insertData = append(insertData, info.Count)
  1916. insertData = append(insertData, info.Price)
  1917. insertData = append(insertData, info.Total)
  1918. insertData = append(insertData, info.Ctime)
  1919. insertData = append(insertData, info.Status)
  1920. insertData = append(insertData, info.OrgId)
  1921. insertData = append(insertData, info.OrderNumber)
  1922. insertData = append(insertData, info.Type)
  1923. insertData = append(insertData, info.Dealer)
  1924. insertData = append(insertData, info.Manufacturer)
  1925. insertData = append(insertData, info.RetailPrice)
  1926. insertData = append(insertData, info.RetailTotalPrice)
  1927. insertData = append(insertData, info.RegisterAccount)
  1928. insertData = append(insertData, info.BatchNumber)
  1929. insertData = append(insertData, info.Remark)
  1930. insertData = append(insertData, info.MaxUnit)
  1931. insertData = append(insertData, info.ProductDate)
  1932. insertData = append(insertData, info.ExpiryDate)
  1933. insertData = append(insertData, info.BatchNumberId)
  1934. }
  1935. thisSQL += strings.Join(insertParams, ", ")
  1936. err = utx.Exec(thisSQL, insertData...).Error
  1937. if err != nil {
  1938. utx.Rollback()
  1939. return
  1940. }
  1941. }
  1942. utx.Commit()
  1943. }
  1944. return
  1945. }
  1946. func FindAllCancelStockList(orgId int64) (list []*models.CancelStock, err error) {
  1947. db := readDb.Model(&models.CancelStock{})
  1948. db = db.Where("org_id = ? AND status = 1", orgId)
  1949. err = db.Order("ctime desc").Find(&list).Error
  1950. return
  1951. }
  1952. func FindAllStockOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  1953. db := readDb.Model(&models.WarehousingInfo{})
  1954. db = db.Where("org_id = ? AND status = 1", orgId)
  1955. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  1956. if startTime > 0 {
  1957. db = db.Where("ctime >=?", startTime)
  1958. }
  1959. if endTime > 0 {
  1960. db = db.Where("ctime<= ?", endTime)
  1961. }
  1962. db = db.Count(&total)
  1963. offset := (page - 1) * limit
  1964. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1965. return
  1966. }
  1967. func FindAllStockInfo(orgId int64, page int64, limit int64, keyword string, startime int64, endtime int64, type_name int64) (list []*models.StockInfo, total int64, err error) {
  1968. db := XTReadDB().Model(&models.StockInfo{})
  1969. if startime > 0 {
  1970. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1971. return db.Where("org_id = ? AND status = 1 and sys_record_time>=?", orgId, startime)
  1972. })
  1973. }
  1974. if startime == 0 || endtime == 0 {
  1975. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1976. return db.Where("org_id = ? AND status = 1", orgId)
  1977. })
  1978. }
  1979. if endtime > 0 {
  1980. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1981. return db.Where("org_id = ? AND status = 1 and sys_record_time >=? and sys_record_time<=?", orgId, startime, endtime)
  1982. })
  1983. }
  1984. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgId)
  1985. db = db.Group("xt_good_information.id")
  1986. if startime == 0 || endtime == 0 {
  1987. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  1988. return db.Where("org_id = ? AND status = 1", orgId)
  1989. })
  1990. }
  1991. if startime > 0 {
  1992. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  1993. return db.Where("org_id = ? AND status = 1 and ctime >= ?", orgId, startime)
  1994. })
  1995. }
  1996. if endtime > 0 {
  1997. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  1998. return db.Where("org_id = ? AND status = 1 and ctime>=? and ctime <= ?", orgId, startime, endtime)
  1999. })
  2000. }
  2001. db = db.Preload("QuerySalesReturnInfo", "org_id = ? AND status = 1", orgId)
  2002. if startime == 0 || endtime == 0 {
  2003. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1", orgId)
  2004. }
  2005. if startime > 0 {
  2006. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=?", orgId, startime)
  2007. }
  2008. if endtime > 0 {
  2009. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=? and ctime<=?", orgId, startime, endtime)
  2010. }
  2011. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  2012. if len(keyword) > 0 {
  2013. likeKey := "%" + keyword + "%"
  2014. 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)
  2015. 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")
  2016. }
  2017. if type_name > 0 {
  2018. 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)
  2019. }
  2020. db = db.Count(&total)
  2021. offset := (page - 1) * limit
  2022. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2023. return
  2024. }
  2025. func FindWarehouseInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2026. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2027. return
  2028. }
  2029. func FindWarehouseOutInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2030. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2031. return
  2032. }
  2033. func UpDateWarehouse(warehouse *models.Warehousing) (err error) {
  2034. 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
  2035. return
  2036. }
  2037. func UpDateWarehouseOut(warehouseOut *models.WarehouseOut) (err error) {
  2038. 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
  2039. return
  2040. }
  2041. func UpDateWarehouseInfo(warehouseInfo *models.WarehousingInfo) (err error) {
  2042. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", warehouseInfo.WarehousingOrder).Update(map[string]interface{}{"good_type_id": warehouseInfo.GoodTypeId,
  2043. "good_id": warehouseInfo.GoodId,
  2044. "number": warehouseInfo.Number,
  2045. "product_date": warehouseInfo.ProductDate,
  2046. "expiry_date": warehouseInfo.ExpiryDate,
  2047. "warehousing_count": warehouseInfo.WarehousingCount,
  2048. "price": warehouseInfo.Price,
  2049. "total_price": warehouseInfo.TotalPrice,
  2050. "mtime": warehouseInfo.Mtime,
  2051. "remark": warehouseInfo.Remark,
  2052. }).Error
  2053. return
  2054. }
  2055. func UpDateWarehouseStatus(id int64) (err error) {
  2056. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2057. return
  2058. }
  2059. func UpdatedStockFlowById(id int64) (err error) {
  2060. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("warehousing_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2061. return
  2062. }
  2063. func UpDateDrugWarehouseStatus(id int64) (err error) {
  2064. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2065. return
  2066. }
  2067. func UpdateDrugFlowById(id int64) (err error) {
  2068. err = XTWriteDB().Model(&models.DrugFlow{}).Where("warehousing_detail_id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2069. return
  2070. }
  2071. func DeleteWarehouse(ids []string) (err error) {
  2072. ut := writeDb.Begin()
  2073. err = ut.Model(&models.Warehousing{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2074. if err != nil {
  2075. ut.Rollback()
  2076. return
  2077. }
  2078. err = ut.Model(&models.WarehousingInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2079. if err != nil {
  2080. ut.Rollback()
  2081. return
  2082. }
  2083. ut.Commit()
  2084. return
  2085. }
  2086. func DeleteStockFlow(ids []string) (err error) {
  2087. ut := writeDb.Begin()
  2088. err = ut.Model(&models.VmStockFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2089. if err != nil {
  2090. ut.Rollback()
  2091. return
  2092. }
  2093. ut.Commit()
  2094. return
  2095. }
  2096. func DeleteDrugWarehouse(ids []string) (err error) {
  2097. ut := writeDb.Begin()
  2098. err = ut.Model(&models.DrugWarehouse{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2099. if err != nil {
  2100. ut.Rollback()
  2101. return
  2102. }
  2103. err = ut.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2104. if err != nil {
  2105. ut.Rollback()
  2106. return
  2107. }
  2108. err = ut.Model(&models.DrugFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2109. if err != nil {
  2110. ut.Rollback()
  2111. return
  2112. }
  2113. ut.Commit()
  2114. return
  2115. }
  2116. func FindWareHouseById(orderNumber string) (*models.Warehousing, error) {
  2117. warehouse := &models.Warehousing{}
  2118. err := readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).First(&warehouse).Error
  2119. return warehouse, err
  2120. }
  2121. func FindWareHouseOutById(id int64) (*models.WarehouseOut, error) {
  2122. warehouseOut := &models.WarehouseOut{}
  2123. err := readDb.Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  2124. return warehouseOut, err
  2125. }
  2126. func FindDrugWareHouseOutById(id int64, org_id int64) (*models.DrugWarehouseOut, error) {
  2127. warehouseOut := &models.DrugWarehouseOut{}
  2128. 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
  2129. return warehouseOut, err
  2130. }
  2131. func FindWarehouseInfoByOrderNumber(orderNumber string) (info []*models.WarehousingInfo, err error) {
  2132. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", orderNumber).Find(&info).Error
  2133. return
  2134. }
  2135. func FindWarehouseOutInfoByOrderNumber(orderNumber string) (info []*models.WarehouseOutInfo, err error) {
  2136. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", orderNumber).Find(&info).Error
  2137. return
  2138. }
  2139. func DeleteStockInRecordByOrderNumber(orderNumber string, id int64) (err error) {
  2140. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2141. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2142. return
  2143. }
  2144. func DeleteStockInRecordByReocordId(id int64) (err error) {
  2145. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2146. return
  2147. }
  2148. func FindAllGoodByManufactureId(manufacturer_id int64, dealer_id int64, org_id int64) (goodInfo []*models.GoodInfo, err error) {
  2149. db := readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id)
  2150. if manufacturer_id > 0 {
  2151. db = db.Where("manufacturer = ?", manufacturer_id)
  2152. }
  2153. if dealer_id > 0 {
  2154. db = db.Where("dealer = ?", dealer_id)
  2155. }
  2156. err = db.Preload("GoodsType", "status = 1 AND org_id = ?", org_id).Find(&goodInfo).Error
  2157. return goodInfo, err
  2158. }
  2159. func FindAllWarehouseByKeyword(orgId int64, page int64, limit int64, keywords string) (list []*models.Warehousing, total int64, err error) {
  2160. db := readDb.Model(&models.Warehousing{})
  2161. likekey := "%" + keywords + "%"
  2162. db = db.Where("org_id = ? AND status = 1 ", orgId)
  2163. db = db.Where("warehousing_order LIKE ? ", likekey)
  2164. db = db.Count(&total)
  2165. offset := (page - 1) * limit
  2166. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2167. return
  2168. }
  2169. func GetLastWarehousingById(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2170. info := models.WarehousingInfo{}
  2171. err = XTReadDB().Model(&info).Where("warehousing_id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2172. return info, err
  2173. }
  2174. func GetLastWarehousingByIdOne(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2175. info := models.WarehousingInfo{}
  2176. err = XTReadDB().Model(&info).Where("id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2177. return info, err
  2178. }
  2179. func UpDateWarehousingInfo(info *models.WarehousingInfo) (err error) {
  2180. err = writeDb.Save(&info).Error
  2181. return err
  2182. }
  2183. func UpDateDrugWarehousingInfo(info *models.DrugWarehouseInfo) (err error) {
  2184. err = writeDb.Save(&info).Error
  2185. return err
  2186. }
  2187. func UpDateDrugWarehousingInfoTwo(id int64, info *models.DrugWarehouseInfo) (err error) {
  2188. 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
  2189. return err
  2190. }
  2191. func UpDateDrugWarehousingInfoSix(id int64, info *models.DrugWarehouseInfo) (err error) {
  2192. 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
  2193. return err
  2194. }
  2195. func GetDrugWarehouseOrderOne(id int64, drug_id int64) (models.DrugWarehouseInfo, error) {
  2196. info := models.DrugWarehouseInfo{}
  2197. err = XTReadDB().Model(&info).Where("warehousing_id = ? and drug_id = ?", id, drug_id).Find(&info).Error
  2198. return info, err
  2199. }
  2200. func EditWarehousing(warehouse models.Warehousing) {
  2201. 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
  2202. }
  2203. func EditDrugWarehousing(warehouse models.DrugWarehouse) {
  2204. 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
  2205. }
  2206. func FindAllWarehouse(org_id int64, types int64) (list []*models.Warehousing, err error) {
  2207. 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
  2208. return list, err
  2209. }
  2210. func FindAllWarehouseInfo(org_id int64, types int64) (list []*models.WarehousingInfoConfig, err error) {
  2211. err = readDb.Model(&models.WarehousingInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2212. 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)
  2213. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2214. return list, err
  2215. }
  2216. func DeleteSalesReturn(ids []string) (err error) {
  2217. ut := writeDb.Begin()
  2218. err = ut.Model(&models.SalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2219. if err != nil {
  2220. ut.Rollback()
  2221. return
  2222. }
  2223. err = ut.Model(&models.SalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2224. if err != nil {
  2225. ut.Rollback()
  2226. return
  2227. }
  2228. ut.Commit()
  2229. return
  2230. }
  2231. func DeleteDrugSalesReturn(ids []string) (err error) {
  2232. ut := writeDb.Begin()
  2233. err = ut.Model(&models.DrugSalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2234. if err != nil {
  2235. ut.Rollback()
  2236. return
  2237. }
  2238. err = ut.Model(&models.DrugSalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2239. if err != nil {
  2240. ut.Rollback()
  2241. return
  2242. }
  2243. ut.Commit()
  2244. return
  2245. }
  2246. func FindAllSalesReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  2247. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  2248. return
  2249. }
  2250. type DrugSalesReturnInfo struct {
  2251. ID int64 `gorm:"column:id" json:"id" form:"id"`
  2252. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  2253. SalesReturnId int64 `gorm:"column:sales_return_id" json:"sales_return_id" form:"sales_return_id"`
  2254. Count int64 `gorm:"column:count" json:"count" form:"count"`
  2255. Price float64 `gorm:"column:price" json:"price" form:"price"`
  2256. Total float64 `gorm:"column:total" json:"total" form:"total"`
  2257. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  2258. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  2259. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  2260. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  2261. Status int64 `gorm:"column:status" json:"status" form:"status"`
  2262. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  2263. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  2264. Type int64 `gorm:"column:type" json:"type" form:"type"`
  2265. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  2266. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  2267. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  2268. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  2269. Number string `gorm:"column:number" json:"number" form:"number"`
  2270. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  2271. }
  2272. func (DrugSalesReturnInfo) TableName() string {
  2273. return "xt_drug_sales_return_info"
  2274. }
  2275. func FindAllDrugSalesReturnInfoById(id int64, org_id int64) (list []*DrugSalesReturnInfo, err error) {
  2276. 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
  2277. return
  2278. }
  2279. func FindAllSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2280. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2281. return
  2282. }
  2283. func FindAllDrugSalesReturnById(id int64, org_id int64) (salesReturn models.DrugSalesReturn, err error) {
  2284. 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
  2285. return
  2286. }
  2287. func UpDateSaleReturnStatus(id int64) (err error) {
  2288. err = readDb.Model(&models.SalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2289. return
  2290. }
  2291. func UpDateDrugSaleReturnStatus(id int64) (err error) {
  2292. err = readDb.Model(&models.DrugSalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2293. return
  2294. }
  2295. func FindSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2296. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2297. return salesReturn, err
  2298. }
  2299. func FindDrugSalesReturnById(id int64) (salesReturn models.DrugSalesReturn, err error) {
  2300. err = readDb.Model(&models.DrugSalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2301. return salesReturn, err
  2302. }
  2303. func EditSaleReturn(sales models.SalesReturn) {
  2304. 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
  2305. }
  2306. func EditDrugSaleReturn(sales models.DrugSalesReturn) {
  2307. 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
  2308. }
  2309. func UpDateSalesReturnInfo(info *models.SalesReturnInfo) (err error) {
  2310. err = writeDb.Save(&info).Error
  2311. return err
  2312. }
  2313. func UpDateDrugSalesReturnInfo(info *models.DrugSalesReturnInfo) (err error) {
  2314. err = writeDb.Save(&info).Error
  2315. return err
  2316. }
  2317. func DeleteWarehouseOut(ids []string) (err error) {
  2318. ut := writeDb.Begin()
  2319. err = ut.Model(&models.WarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2320. if err != nil {
  2321. ut.Rollback()
  2322. return
  2323. }
  2324. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2325. if err != nil {
  2326. ut.Rollback()
  2327. return
  2328. }
  2329. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2330. if err != nil {
  2331. ut.Rollback()
  2332. return
  2333. }
  2334. ut.Commit()
  2335. return
  2336. }
  2337. func DeleteDrugWarehouseOut(ids []string) (err error) {
  2338. ut := writeDb.Begin()
  2339. err = ut.Model(&models.DrugWarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2340. if err != nil {
  2341. ut.Rollback()
  2342. return
  2343. }
  2344. err = ut.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2345. if err != nil {
  2346. ut.Rollback()
  2347. return
  2348. }
  2349. err = ut.Model(&models.DrugFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2350. if err != nil {
  2351. ut.Rollback()
  2352. return
  2353. }
  2354. ut.Commit()
  2355. return
  2356. }
  2357. func UpDateWarehouseOutStatus(id int64) (err error) {
  2358. err = readDb.Model(&models.WarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2359. return
  2360. }
  2361. func UpDateDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo) (err error) {
  2362. ut := writeDb.Begin()
  2363. err = ut.Save(&info).Error
  2364. if err != nil {
  2365. ut.Rollback()
  2366. return err
  2367. }
  2368. ut.Commit()
  2369. return err
  2370. }
  2371. func UpDateNewDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo, tx *gorm.DB) (err error) {
  2372. err = tx.Save(&info).Error
  2373. if err != nil {
  2374. tx.Rollback()
  2375. return err
  2376. }
  2377. return err
  2378. }
  2379. func UpDateWarehouseInfoByStock(info *models.WarehousingInfo) (err error) {
  2380. ut := writeDb.Begin()
  2381. err = ut.Save(&info).Error
  2382. if err != nil {
  2383. ut.Rollback()
  2384. return err
  2385. }
  2386. ut.Commit()
  2387. return err
  2388. }
  2389. func UpDateNewWarehouseInfoByStock(info *models.WarehousingInfo, tx *gorm.DB) (err error) {
  2390. err = tx.Save(&info).Error
  2391. if err != nil {
  2392. tx.Rollback()
  2393. return err
  2394. }
  2395. return err
  2396. }
  2397. func UpDateDrugWarehouseOutStatus(id int64) (err error) {
  2398. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2399. return
  2400. }
  2401. func EditWarehouseOut(warehouseOut models.WarehouseOut) {
  2402. 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
  2403. }
  2404. func EditDrugWarehouseOut(warehouseOut models.DrugWarehouseOut) {
  2405. 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
  2406. }
  2407. func UpDateWarehouseOutInfo(info *models.WarehouseOutInfo) (err error) {
  2408. err = writeDb.Save(&info).Error
  2409. return err
  2410. }
  2411. 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) {
  2412. db := readDb.Model(&models.CancelStock{})
  2413. db = db.Where("xt_cancel_stock.org_id = ? AND xt_cancel_stock.status = 1 AND xt_cancel_stock.type = ?", orgId, types)
  2414. if len(keywords) > 0 {
  2415. likeKey := "%" + keywords + "%"
  2416. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2417. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_cancel_stock.manufacturer")
  2418. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_cancel_stock.order_number LIKE ?", likeKey, likeKey).Group("xt_cancel_stock.id")
  2419. }
  2420. if startTime > 0 {
  2421. db = db.Where("xt_cancel_stock.opera_time >=?", startTime)
  2422. }
  2423. if endTime > 0 {
  2424. db = db.Where("xt_cancel_stock.opera_time<= ?", endTime)
  2425. }
  2426. if storehouse_id > 0 {
  2427. db = db.Where("xt_cancel_stock.storehouse_id = ?", storehouse_id)
  2428. }
  2429. if check_type > 0 {
  2430. db = db.Where("xt_cancel_stock.is_check = ?", check_type)
  2431. }
  2432. db = db.Count(&total)
  2433. offset := (page - 1) * limit
  2434. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock.ctime desc").Find(&list).Error
  2435. return
  2436. }
  2437. 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) {
  2438. db := readDb.Model(&models.DrugCancelStock{})
  2439. db = db.Where("xt_drug_cancel_stock.org_id = ? AND xt_drug_cancel_stock.status = 1", orgId)
  2440. if len(keywords) > 0 {
  2441. likeKey := "%" + keywords + "%"
  2442. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_cancel_stock.creater")
  2443. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_cancel_stock.manufacturer")
  2444. 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")
  2445. }
  2446. if startTime > 0 {
  2447. db = db.Where("xt_drug_cancel_stock.opera_time >=?", startTime)
  2448. }
  2449. if endTime > 0 {
  2450. db = db.Where("xt_drug_cancel_stock.opera_time<= ?", endTime)
  2451. }
  2452. if storehoue_id > 0 {
  2453. db = db.Where("xt_drug_cancel_stock.storehouse_id = ?", storehoue_id)
  2454. }
  2455. if check_type > 0 {
  2456. db = db.Where("xt_drug_cancel_stock.is_check = ?", check_type)
  2457. }
  2458. db = db.Count(&total)
  2459. offset := (page - 1) * limit
  2460. err = db.Offset(offset).Limit(limit).Order("xt_drug_cancel_stock.ctime desc").Find(&list).Error
  2461. return
  2462. }
  2463. func GetCancelWarehosueInfo(ids []string) (info []*models.CancelStockInfo, err error) {
  2464. err = XTReadDB().Where("id IN(?) and status = 1", ids).Find(&info).Error
  2465. return info, err
  2466. }
  2467. func DeleteCancelStock(ids []string) (err error) {
  2468. ut := writeDb.Begin()
  2469. err = ut.Model(&models.CancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2470. if err != nil {
  2471. ut.Rollback()
  2472. return
  2473. }
  2474. err = ut.Model(&models.CancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2475. if err != nil {
  2476. ut.Rollback()
  2477. return
  2478. }
  2479. err = ut.Model(&models.VmStockFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2480. if err != nil {
  2481. ut.Rollback()
  2482. return
  2483. }
  2484. ut.Commit()
  2485. return
  2486. }
  2487. func GetDrugWareInfoByCancelId(ids []string) (info []*models.DrugCancelStockInfo, err error) {
  2488. err = XTReadDB().Where("cancel_stock_id IN(?) and status = 1", ids).Find(&info).Error
  2489. return info, err
  2490. }
  2491. func DeleteDrugCancelStock(ids []string) (err error) {
  2492. ut := writeDb.Begin()
  2493. err = ut.Model(&models.DrugCancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2494. if err != nil {
  2495. ut.Rollback()
  2496. return
  2497. }
  2498. err = ut.Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2499. if err != nil {
  2500. ut.Rollback()
  2501. return
  2502. }
  2503. err = ut.Model(&models.DrugFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2504. if err != nil {
  2505. ut.Rollback()
  2506. return
  2507. }
  2508. ut.Commit()
  2509. return
  2510. }
  2511. func UpDateCancleStockStatus(id int64) (err error) {
  2512. err = readDb.Model(&models.CancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2513. return
  2514. }
  2515. func GetCancelStockById(id int64) (models.CancelStockInfo, error) {
  2516. info := models.CancelStockInfo{}
  2517. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  2518. return info, err
  2519. }
  2520. func ModefyWarehouseInfoNight(info models.WarehousingInfo, id int64) error {
  2521. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", info.StockCount)).Error
  2522. return err
  2523. }
  2524. func UpdatedStockFlowByCancelId(id int64, good_id int64) (err error) {
  2525. 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
  2526. return
  2527. }
  2528. func UpDateDrugCancleStockStatus(id int64) (err error) {
  2529. err = readDb.Model(&models.DrugCancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2530. return
  2531. }
  2532. func UpDateDrugCancelStockById(id int64) (err error) {
  2533. err = XTWriteDB().Model(&models.DrugFlow{}).Where("cancel_out_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2534. return
  2535. }
  2536. func EditCancelStock(sales models.CancelStock) {
  2537. 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
  2538. }
  2539. func EditDrugCancelStock(sales models.DrugCancelStock) {
  2540. 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
  2541. }
  2542. func FindCancelStockById(id int64, org_id int64) (cancelStock models.CancelStock, err error) {
  2543. err = readDb.Model(&models.CancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2544. return cancelStock, err
  2545. }
  2546. func FindCancelDrugById(id int64, org_id int64) (cancelStock models.DrugCancelStock, err error) {
  2547. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2548. return cancelStock, err
  2549. }
  2550. func FindDrugCancelStockById(id int64) (cancelStock models.DrugCancelStock, err error) {
  2551. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2552. return cancelStock, err
  2553. }
  2554. func UpDateCancelStockInfo(info *models.CancelStockInfo) (err error) {
  2555. err = writeDb.Save(&info).Error
  2556. return err
  2557. }
  2558. func UpDateDrugCancelStockInfo(info *models.DrugCancelStockInfo) (err error) {
  2559. err = writeDb.Save(&info).Error
  2560. return err
  2561. }
  2562. func FindWarehouseOutList(org_id int64, types int64) (list []*models.WarehouseOut, err error) {
  2563. 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
  2564. return list, err
  2565. }
  2566. func FindAllWarehouseOutInfo(org_id int64, types int64) (list []*models.WarehousingOutInfoConfig, err error) {
  2567. err = readDb.Model(&models.WarehousingOutInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2568. 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)
  2569. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2570. return list, err
  2571. }
  2572. type StockDetailModel struct {
  2573. GoodId int64 `gorm:"good_id" json:"good_id"`
  2574. GoodTypeId int64 `gorm:"good_type_id" json:"good_type_id"`
  2575. WarehousingOrder string `gorm:"warehousing_order" json:"warehousing_order"`
  2576. Price float64 `gorm:"price" json:"price"`
  2577. WarehousingCount int64 `gorm:"warehousing_count" json:"warehousing_count"`
  2578. Type int `gorm:"type" json:"type"`
  2579. Creater int64 `gorm:"creater" json:"creater"`
  2580. WarehousingTime int64 `gorm:"warehousing_time" json:"warehousing_time"`
  2581. Types int64 `gorm:"types" json:"types"`
  2582. }
  2583. 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) {
  2584. db := readDb.Model(&models.WarehousingInfo{})
  2585. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1 AND xt_warehouse_info.is_check = 1", orgId)
  2586. if len(keywords) > 0 {
  2587. likeKey := "%" + keywords + "%"
  2588. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2589. 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)
  2590. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2591. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2592. 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")
  2593. } else {
  2594. 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)
  2595. }
  2596. if manufacturer > 0 {
  2597. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2598. }
  2599. if storehouse_id > 0 {
  2600. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  2601. }
  2602. if order_type > 0 {
  2603. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2604. }
  2605. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  2606. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2607. db = db.Count(&total)
  2608. offset := (page - 1) * limit
  2609. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_info.id desc").Find(&list).Error
  2610. return
  2611. }
  2612. func GetStockInDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (err error, totalPrice float64) {
  2613. db := readDb.Model(&models.WarehousingInfo{})
  2614. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId)
  2615. if len(keywords) > 0 {
  2616. likeKey := "%" + keywords + "%"
  2617. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2618. 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)
  2619. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2620. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2621. 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")
  2622. } else {
  2623. 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)
  2624. }
  2625. if manufacturer > 0 {
  2626. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2627. }
  2628. if order_type > 0 {
  2629. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2630. }
  2631. if storehouse_id > 0 {
  2632. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  2633. }
  2634. type PriceAmount struct {
  2635. Total float64
  2636. }
  2637. var result []PriceAmount
  2638. err = db.Select("total_price as total").Scan(&result).Error
  2639. var total float64
  2640. for _, value := range result {
  2641. total = total + value.Total
  2642. }
  2643. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  2644. return err, total
  2645. }
  2646. func FloatRound(f float64, n int) float64 {
  2647. format := "%." + strconv.Itoa(n) + "f"
  2648. res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
  2649. return res
  2650. }
  2651. 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) {
  2652. if orgId == 9671 {
  2653. db := readDb.Model(&models.WarehouseOutInfo{})
  2654. 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)
  2655. if len(keywords) > 0 {
  2656. likeKey := "%" + keywords + "%"
  2657. 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)
  2658. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2659. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2660. 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")
  2661. } else {
  2662. 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)
  2663. }
  2664. if manufacturer > 0 {
  2665. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2666. }
  2667. if order_type > 0 {
  2668. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2669. }
  2670. if storehouse_id > 0 {
  2671. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  2672. }
  2673. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2674. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ?", orgId)
  2675. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2676. db = db.Count(&total)
  2677. offset := (page - 1) * limit
  2678. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  2679. return
  2680. } else {
  2681. db := readDb.Model(&models.WarehouseOutInfo{})
  2682. 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)
  2683. if len(keywords) > 0 {
  2684. likeKey := "%" + keywords + "%"
  2685. 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)
  2686. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2687. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2688. 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")
  2689. } else {
  2690. 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)
  2691. }
  2692. if manufacturer > 0 {
  2693. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2694. }
  2695. if order_type > 0 {
  2696. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2697. }
  2698. if storehouse_id > 0 {
  2699. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  2700. }
  2701. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2702. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ?", orgId)
  2703. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2704. db = db.Count(&total)
  2705. offset := (page - 1) * limit
  2706. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  2707. return
  2708. }
  2709. }
  2710. func GetStockOutDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (err error, totalPrice float64) {
  2711. db := readDb.Model(&models.WarehouseOutInfo{})
  2712. 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)
  2713. if len(keywords) > 0 {
  2714. likeKey := "%" + keywords + "%"
  2715. 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)
  2716. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2717. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2718. 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")
  2719. } else {
  2720. 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)
  2721. }
  2722. if manufacturer > 0 {
  2723. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2724. }
  2725. if order_type > 0 {
  2726. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2727. }
  2728. if storehouse_id > 0 {
  2729. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  2730. }
  2731. type PriceAmount struct {
  2732. Total float64
  2733. }
  2734. var result []PriceAmount
  2735. err = db.Select("(count * price) as total").Scan(&result).Error
  2736. var total float64
  2737. for _, value := range result {
  2738. total = total + value.Total
  2739. }
  2740. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  2741. fmt.Println(total)
  2742. return err, total
  2743. }
  2744. 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) {
  2745. db := readDb.Model(&models.SalesReturnInfo{})
  2746. db = db.Where("xt_sales_return_info.org_id = ? AND xt_sales_return_info.status = 1", orgId)
  2747. if len(keywords) > 0 {
  2748. likeKey := "%" + keywords + "%"
  2749. 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)
  2750. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  2751. db = db.Joins("join xt_good_information on xt_good_information.id = xt_sales_return_info.good_id")
  2752. 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")
  2753. } else {
  2754. 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)
  2755. }
  2756. if manufacturer > 0 {
  2757. db = db.Joins("join xt_sales_return as sr on sr.id = xt_sales_return_info.sales_return_id AND sr.manufacturer =?", manufacturer)
  2758. }
  2759. if order_type > 0 {
  2760. db = db.Where("xt_sales_return_info.type = ?", order_type)
  2761. }
  2762. db = db.Preload("SalesReturn", "status = 1 AND org_id = ?", orgId)
  2763. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2764. offset := (page - 1) * limit
  2765. db = db.Count(&total)
  2766. err = db.Offset(offset).Limit(limit).Order("xt_sales_return_info.ctime desc").Find(&list).Error
  2767. return
  2768. }
  2769. 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) {
  2770. db := readDb.Model(&models.CancelStockInfo{})
  2771. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  2772. if len(keywords) > 0 {
  2773. likeKey := "%" + keywords + "%"
  2774. 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)
  2775. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2776. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  2777. 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")
  2778. } else {
  2779. 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)
  2780. }
  2781. if manufacturer > 0 {
  2782. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  2783. }
  2784. if order_type > 0 {
  2785. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  2786. }
  2787. if storehouse_id > 0 {
  2788. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", order_type)
  2789. }
  2790. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  2791. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2792. err = db.Group("xt_cancel_stock_info.good_id").Order("xt_cancel_stock_info.ctime desc").Find(&list).Error
  2793. return
  2794. }
  2795. 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) {
  2796. db := readDb.Model(&models.CancelStockInfo{})
  2797. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  2798. if len(keywords) > 0 {
  2799. likeKey := "%" + keywords + "%"
  2800. 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)
  2801. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2802. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  2803. 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")
  2804. } else {
  2805. 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)
  2806. }
  2807. if manufacturer > 0 {
  2808. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  2809. }
  2810. if order_type > 0 {
  2811. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  2812. }
  2813. if storehouse_id > 0 {
  2814. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", storehouse_id)
  2815. }
  2816. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  2817. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2818. db = db.Count(&total)
  2819. offset := (page - 1) * limit
  2820. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock_info.id desc").Find(&list).Error
  2821. return
  2822. }
  2823. func FindStockInByDealerId(id int64) (total int64, err error) {
  2824. err = readDb.Model(&models.Warehousing{}).Where("dealer = ? AND status = 1", id).Count(&total).Error
  2825. return
  2826. }
  2827. func FindStockInByManufacturerId(id int64) (total int64, err error) {
  2828. err = readDb.Model(&models.Warehousing{}).Where("manufacturer = ? AND status = 1", id).Count(&total).Error
  2829. return
  2830. }
  2831. func FindGoodTypeByName(name string, org_id int64) (total int64) {
  2832. readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 AND type_name = ? ", org_id, name).Count(&total)
  2833. return
  2834. }
  2835. func FindGoodInfoByName(name string, org_id int64, good_type_id int64) (total int64) {
  2836. 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)
  2837. return
  2838. }
  2839. func FindGoodInfoByNameTwo(name string, org_id int64, good_type_id int64, good_name string) (total int64) {
  2840. 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)
  2841. return
  2842. }
  2843. func FindGoodInfoByNameOne(name string, org_id int64, good_type_id int64, id int64, goodname string) (total int64) {
  2844. 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)
  2845. return
  2846. }
  2847. func FindAllGoodTypeByType(types int64) (goodType []*models.GoodsType, err error) {
  2848. err = readDb.Model(&models.GoodsType{}).Where("type = ? AND status = 1", types).Find(&goodType).Error
  2849. return goodType, err
  2850. }
  2851. func FindWarehouseInfoByGoodType(good_type_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  2852. 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
  2853. return info, err
  2854. }
  2855. func FindStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.WarehouseOut, err error) {
  2856. 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
  2857. return
  2858. }
  2859. func FindStockOutByIsSysSix(org_id int64, is_sys int, record_time int64, second_warehouse_id int64) (out models.WarehouseOut, err error) {
  2860. 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
  2861. return
  2862. }
  2863. func FindDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  2864. 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
  2865. return
  2866. }
  2867. func FindNewDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64, tx *gorm.DB) (out models.DrugWarehouseOut, err error) {
  2868. 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
  2869. if err != gorm.ErrRecordNotFound {
  2870. if err != nil {
  2871. tx.Rollback()
  2872. return
  2873. }
  2874. }
  2875. return
  2876. }
  2877. func FindDrugSettleStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  2878. 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
  2879. return
  2880. }
  2881. func FindLastStockInInfoRecord(good_id int64, org_id int64) (in models.WarehousingInfo, err error) {
  2882. err = readDb.Model(&models.WarehousingInfo{}).Where("status = 1 AND org_id = ? AND good_id = ?", org_id, good_id).First(&in).Error
  2883. return
  2884. }
  2885. func FindLastDrugStockInInfoRecord(drug_id int64, org_id int64) (in models.DrugWarehouseOutInfo, err error) {
  2886. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("status = 1 AND org_id = ? AND drug_id = ?", org_id, drug_id).Last(&in).Error
  2887. return
  2888. }
  2889. func GetStockInRecoredByGoodId(goodid int64, goodtypeid int64, recordtime int64, patientid 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 patient_id = ? and org_id = ? and status = 1", goodid, goodtypeid, recordtime, patientid, 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 GetStockInRecoredByGoodIdOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64) (*models.WarehouseOutInfo, error) {
  2901. info := models.WarehouseOutInfo{}
  2902. 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
  2903. if err == gorm.ErrRecordNotFound {
  2904. return nil, err
  2905. }
  2906. if err != nil {
  2907. return nil, err
  2908. }
  2909. return &info, nil
  2910. }
  2911. func UpdateAutoMaticReduceDetail(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  2912. 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
  2913. return err
  2914. }
  2915. func UpdateAutoMaticReduceDetailOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  2916. 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
  2917. return err
  2918. }
  2919. func AddSigleWarehouseOutInfo(info *models.WarehouseOutInfo) error {
  2920. err := writeDb.Create(&info).Error
  2921. return err
  2922. }
  2923. func AddNewSigleWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) error {
  2924. err := tx.Create(&info).Error
  2925. if err != nil {
  2926. tx.Rollback()
  2927. return err
  2928. }
  2929. return err
  2930. }
  2931. func UpdateStockOut(warehouseinfoid int64, info models.WarehousingInfo) error {
  2932. err := XTWriteDB().Model(&info).Where("id=? and status = 1", warehouseinfoid).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  2933. return err
  2934. }
  2935. func AddSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo) error {
  2936. err := writeDb.Create(&info).Error
  2937. return err
  2938. }
  2939. func AddNewSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, tx *gorm.DB) error {
  2940. err := tx.Create(&info).Error
  2941. if err != nil {
  2942. tx.Rollback()
  2943. return err
  2944. }
  2945. return err
  2946. }
  2947. func UpdateSigleDrugWarehouseOutInfoDetail(id int64, count int64, tx *gorm.DB) error {
  2948. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
  2949. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).UpdateColumn("over_count", gorm.Expr("over_count - ?", count)).Error
  2950. if err != nil {
  2951. tx.Rollback()
  2952. return err
  2953. }
  2954. return err
  2955. }
  2956. func AddSigleAutoReduceRecordInfo(detail *models.AutomaticReduceDetail) error {
  2957. ut := writeDb.Begin()
  2958. err := ut.Create(&detail).Error
  2959. if err != nil {
  2960. ut.Rollback()
  2961. return err
  2962. }
  2963. ut.Commit()
  2964. return err
  2965. }
  2966. func AddSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail) error {
  2967. err := writeDb.Create(&detail).Error
  2968. return err
  2969. }
  2970. func GetDrugAutoMaticReduceDetail(drug_id int64, patient_id int64, record_time int64, org_id int64, adivce_id int64, warehouse_info_id int64, tx *gorm.DB) (models.DrugAutomaticReduceDetail, error) {
  2971. detail := models.DrugAutomaticReduceDetail{}
  2972. err := tx.Where("drug_id = ? and patient_id =? and record_time =? and org_id = ? and advice_id = ? and warehouse_info_id =?", drug_id, patient_id, record_time, org_id, adivce_id, warehouse_info_id).Find(&detail).Error
  2973. if err != gorm.ErrRecordNotFound {
  2974. if err != nil {
  2975. tx.Rollback()
  2976. return detail, err
  2977. }
  2978. }
  2979. return detail, err
  2980. }
  2981. func UpdateNewSingleDrugAutoReduceRecordInfo(id int64, count int64, tx *gorm.DB) error {
  2982. err := tx.Model(models.DrugAutomaticReduceDetail{}).Where("id = ? and status =1", id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
  2983. if err != nil {
  2984. tx.Rollback()
  2985. return err
  2986. }
  2987. return err
  2988. }
  2989. func AddNewSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail, tx *gorm.DB) error {
  2990. err := tx.Create(&detail).Error
  2991. if err != nil {
  2992. tx.Rollback()
  2993. return err
  2994. }
  2995. return err
  2996. }
  2997. func FindStockOutInfoByTypeId(org_id int64, good_type_id int64, good_id int64, out_id int64, number string) (out models.WarehouseOutInfo, err error) {
  2998. 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
  2999. return
  3000. }
  3001. func FindStockOutInfoByStock(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (out []*models.WarehouseOutInfo, err error) {
  3002. 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
  3003. return
  3004. }
  3005. func FindStockOutInfoByStockOne(org_id int64, good_type_id int64, good_id int64, record_date int64) (out []*models.WarehouseOutInfo, err error) {
  3006. 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
  3007. return
  3008. }
  3009. func FindStockOutInfoByStockTwo(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (models.WarehouseOutInfo, error) {
  3010. info := models.WarehouseOutInfo{}
  3011. 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
  3012. return info, err
  3013. }
  3014. func FindDrugStockOutInfoByTypeId(org_id int64, drug_id int64, out_id int64, number string) (out models.DrugWarehouseOutInfo, err error) {
  3015. 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
  3016. return
  3017. }
  3018. func UpdateStockOutInfoCount(org_id int64, id int64) {
  3019. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", 1))
  3020. }
  3021. func FindUserDetailById(org_id int64, id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  3022. db := readDb.Model(&models.AutomaticReduceDetail{})
  3023. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  3024. db = db.Preload("GoodsType", "status = 1")
  3025. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3026. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND good_id != 0 AND count <> 0 ", org_id, id)
  3027. db = db.Count(&total)
  3028. err = db.Find(&user).Error
  3029. return
  3030. }
  3031. type DrugAutomaticReduceDetail struct {
  3032. ID int64 `gorm:"column:id" json:"id" form:"id"`
  3033. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  3034. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  3035. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  3036. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  3037. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  3038. Status int64 `gorm:"column:status" json:"status" form:"status"`
  3039. RecordTime int64 `gorm:"column:record_time" json:"record_time" form:"record_time"`
  3040. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  3041. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  3042. Count int64 `gorm:"column:count" json:"count" form:"count"`
  3043. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  3044. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  3045. Patients *models.Patients `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"user"`
  3046. DrugWarehouseOutInfo []*models.DrugWarehouseOutInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"drugwarehouseoutinfo"`
  3047. }
  3048. func (DrugAutomaticReduceDetail) TableName() string {
  3049. return "xt_drug_automatic_reduce_detail"
  3050. }
  3051. func FindDrugStockUserDetailById(org_id int64, id int64) (user []*DrugAutomaticReduceDetail, err error, total int64) {
  3052. db := readDb.Model(&DrugAutomaticReduceDetail{})
  3053. db = db.Preload("Drug", "org_id = ? AND status = 1", org_id)
  3054. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3055. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND drug_id != 0", org_id, id)
  3056. db = db.Count(&total)
  3057. err = db.Find(&user).Error
  3058. return
  3059. }
  3060. func UpdateStockOutInfoCount2(org_id int64, id int64, count int64) {
  3061. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3062. }
  3063. func UpdateDrugStockOutInfoCount2(org_id int64, id int64, count int64) {
  3064. writeDb.Model(&models.DrugWarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3065. }
  3066. func FindPatientAutomaticReduceRecord(org_id int64, record_time int64, good_id int64, good_type_id int64, patient_id int64) (count int64, err error) {
  3067. 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
  3068. return
  3069. }
  3070. func FindPatientDrugAutomaticReduceRecord(org_id int64, record_time int64, drug_id int64, patient_id int64) (count int64, err error) {
  3071. 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
  3072. return
  3073. }
  3074. type VMWarehousingInfo struct {
  3075. ID int64 `gorm:"column:id" json:"id"`
  3076. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id"`
  3077. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  3078. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  3079. Status int64 `gorm:"column:status" json:"status"`
  3080. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3081. GoodInfo GoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"good_info"`
  3082. }
  3083. func (VMWarehousingInfo) TableName() string {
  3084. return "xt_warehouse_info"
  3085. }
  3086. type GoodInfo struct {
  3087. ID int64 `gorm:"column:id" json:"id"`
  3088. SpecificationName string `gorm:"column:specification_name" json:"specification_name"`
  3089. Status int64 `gorm:"column:status" json:"status"`
  3090. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3091. }
  3092. func (GoodInfo) TableName() string {
  3093. return "xt_good_information"
  3094. }
  3095. func GetStockInGoodInfo(orgId int64) (stockIn []*VMWarehousingInfo, err error) {
  3096. err = readDb.Model(&VMWarehousingInfo{}).Preload("GoodInfo", "status = 1 AND org_id = ?", orgId).Where("org_id = ? AND status = 1", orgId).Find(&stockIn).Error
  3097. return
  3098. }
  3099. func GetAllStockOutUserDetail(patient_id int64, orgId int64, record_time int64) (goodUser []*models.DialysisBeforePrepare, err error) {
  3100. 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
  3101. return
  3102. }
  3103. func PostSearchStock(keyword string, orgid int64) (goods []*models.GoodsType, err error) {
  3104. likeKey := "%" + keyword + "%"
  3105. db := XTReadDB().Model(&goods)
  3106. if len(keyword) > 0 {
  3107. db = db.Where("type_name like ?", likeKey)
  3108. }
  3109. err = db.Where("org_id = ? and status =1", orgid).Find(&goods).Error
  3110. return goods, err
  3111. }
  3112. func GetStockType(orgid int64) (goodstype []*models.GoodsTypeOne, err error) {
  3113. err = XTReadDB().Where("org_id = ? and status = 1 and (stock_attribute = 2 or stock_attribute = 3 or stock_attribute = 4)", orgid).
  3114. Preload("GoodInfo", "org_id = ? and status = 1 and find_in_set('停用',good_status) = 0", orgid).Find(&goodstype).Error
  3115. return goodstype, err
  3116. }
  3117. func GetDialyStockOut(orgid int64, recordtime int64, patientId int64) (prepare []*models.XtDialysisBeforePrepare, err error) {
  3118. db := XTReadDB().Table("dialysis_before_prepare as x").Where("x.status = 1")
  3119. if orgid > 0 {
  3120. db = db.Where("x.user_org_id = ?", orgid)
  3121. }
  3122. if recordtime > 0 {
  3123. db = db.Where("x.record_date = ?", recordtime)
  3124. }
  3125. if patientId > 0 {
  3126. db = db.Where("x.patient_id = ?", patientId)
  3127. }
  3128. 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
  3129. return prepare, err
  3130. }
  3131. func GetAutoReduceRecordInfoById(orgid int64, recordtime int64, goodid int64, goodtypeid int64) (*models.XtAutomaticReduceDetail, error) {
  3132. detail := models.XtAutomaticReduceDetail{}
  3133. 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
  3134. if err == gorm.ErrRecordNotFound {
  3135. return nil, err
  3136. }
  3137. if err != nil {
  3138. return nil, err
  3139. }
  3140. return &detail, nil
  3141. }
  3142. func GetOutStockTotalCount(wareouttime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3143. 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
  3144. return autoMatic, err
  3145. }
  3146. func GetOutStockTotalCountOne(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3147. 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
  3148. return autoMatic, err
  3149. }
  3150. func GetManufacturerName(orgid int64, name string) (*models.Manufacturer, error) {
  3151. var manufacturer models.Manufacturer
  3152. var err error
  3153. err = XTReadDB().Model(&manufacturer).Where("org_id = ? and manufacturer_name = ? and status = 1", orgid, name).Find(&manufacturer).Error
  3154. if err == gorm.ErrRecordNotFound {
  3155. return nil, err
  3156. }
  3157. if err != nil {
  3158. return nil, err
  3159. }
  3160. return &manufacturer, nil
  3161. }
  3162. func CreateManufacturer(manufacturer *models.Manufacturer) error {
  3163. err := XTWriteDB().Create(manufacturer).Error
  3164. return err
  3165. }
  3166. func UpdatedManufactur(manufacturer *models.Manufacturer, orgid int64) error {
  3167. err := XTWriteDB().Model(&manufacturer).Where("org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"manufacturer_name": manufacturer.ManufacturerName}).Error
  3168. return err
  3169. }
  3170. func GetGoodsInformaitonIsExist(goodName string, goodType int64, specificationName string, orgid int64) (*models.GoodInfo, error) {
  3171. var good models.GoodInfo
  3172. var err error
  3173. 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
  3174. if err == gorm.ErrRecordNotFound {
  3175. return nil, err
  3176. }
  3177. if err != nil {
  3178. return nil, err
  3179. }
  3180. return &good, nil
  3181. }
  3182. func CreateGoodsInfomation(info *models.GoodInfo) error {
  3183. err := XTWriteDB().Create(&info).Error
  3184. return err
  3185. }
  3186. func UpdateGoodsInformation(info *models.GoodInfo, goodName string, goodType int64, specificationName string, orgid int64) error {
  3187. 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, "number": info.Number, "register_number": info.RegisterNumber}).Error
  3188. return err
  3189. }
  3190. func GetCoutWareseOutInfo(startime int64, endtime int64, orgid int64) (houseOutInfo []*models.VmWarehouseOutInfo, err error) {
  3191. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  3192. 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
  3193. return houseOutInfo, err
  3194. }
  3195. func GetGoodInfomationList(orgid int64, keyword string) (goodInfo []*models.GoodInfo, err error) {
  3196. key := "%" + keyword + "%"
  3197. if len(keyword) == 0 {
  3198. err = XTReadDB().Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Find(&goodInfo).Error
  3199. } else {
  3200. err = XTReadDB().Where("org_id = ? and status = 1 AND good_name like ? AND find_in_set('停用',good_status) = 0", orgid, key).Find(&goodInfo).Error
  3201. }
  3202. return goodInfo, err
  3203. }
  3204. func IsExistDrugByName(drugname string, drugdesc string, orgid int64) (*models.BaseDrugLib, error) {
  3205. var drug models.BaseDrugLib
  3206. var err error
  3207. err = XTReadDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and status =1 and org_id = ?", drugname, drugdesc, orgid).Find(&drug).Error
  3208. if err == gorm.ErrRecordNotFound {
  3209. return nil, err
  3210. }
  3211. if err != nil {
  3212. return nil, err
  3213. }
  3214. return &drug, nil
  3215. }
  3216. func IsExistDrugByNameOne(drugname string, dosage string, doseunit string, minnumber int64, minunit string, packtunit string, orgid int64) (*models.BaseDrugLib, error) {
  3217. var drug models.BaseDrugLib
  3218. var err error
  3219. 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
  3220. if err == gorm.ErrRecordNotFound {
  3221. return nil, err
  3222. }
  3223. if err != nil {
  3224. return nil, err
  3225. }
  3226. return &drug, nil
  3227. }
  3228. func CreateDrugsInfomation(info *models.BaseDrugLib) error {
  3229. err := XTWriteDB().Create(&info).Error
  3230. return err
  3231. }
  3232. func UpdateDrugsInformation(drug *models.BaseDrugLib, drugName string, drugSpec string, orgid int64) error {
  3233. 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
  3234. return err
  3235. }
  3236. func GetProjectByNameIsExsit(projectName string, orgid int64) (*models.HisProject, error) {
  3237. var projoect models.HisProject
  3238. var err error
  3239. err = XTReadDB().Model(&projoect).Where("project_name = ? and user_org_id = ? and status =1", projectName, orgid).Find(&projoect).Error
  3240. if err == gorm.ErrRecordNotFound {
  3241. return nil, err
  3242. }
  3243. if err != nil {
  3244. return nil, err
  3245. }
  3246. return &projoect, nil
  3247. }
  3248. func CreateProjectInfomation(info *models.HisProject) error {
  3249. err := XTWriteDB().Create(&info).Error
  3250. return err
  3251. }
  3252. func UpdateProjectByName(project *models.HisProject, projectName string, orgid int64) error {
  3253. 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
  3254. return err
  3255. }
  3256. func GetWareOutInfoById(warehouseoutid int64, orgid int64) (warehouseinfo []*models.WarehouseOutInfo, err error) {
  3257. err = XTReadDB().Model(&warehouseinfo).Where("warehouse_out_id = ? and org_id = ? and status= 1 and is_sys = 0", warehouseoutid, orgid).Find(&warehouseinfo).Error
  3258. return warehouseinfo, err
  3259. }
  3260. func EditDrugWarehousingOne(warehouse models.DrugWarehouse, id int64) error {
  3261. 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
  3262. return err
  3263. }
  3264. func GetAllManufacturerList(orgid int64) (list []*models.Manufacturer, err error) {
  3265. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3266. return list, err
  3267. }
  3268. func GetNewAllManufacturerList(orgid int64) (list []*models.BloodManufacturer, err error) {
  3269. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3270. return list, err
  3271. }
  3272. func GetAllDealerList(orgid int64) (list []*models.Dealer, err error) {
  3273. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3274. return list, err
  3275. }
  3276. // func GetAllDealerListTX(orgid int64,tx *gorm.DB) (list []*models.Dealer, err error) {
  3277. // err = tx.Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3278. // return list, err
  3279. // }
  3280. func GetDealerByName(orgid int64, name string) (*models.Dealer, error) {
  3281. var dealer models.Dealer
  3282. var err error
  3283. err = XTReadDB().Model(&dealer).Where("org_id = ? and dealer_name = ? and status = 1", orgid, name).Find(&dealer).Error
  3284. if err == gorm.ErrRecordNotFound {
  3285. return nil, err
  3286. }
  3287. if err != nil {
  3288. return nil, err
  3289. }
  3290. return &dealer, nil
  3291. }
  3292. func CreateDealer(dealer *models.Dealer) error {
  3293. err := XTWriteDB().Create(&dealer).Error
  3294. return err
  3295. }
  3296. func UpdateDealer(dealer *models.Dealer, orgid int64) error {
  3297. err := XTWriteDB().Model(&dealer).Where("org_id = ? and status =1", orgid).Updates(map[string]interface{}{"dealer_name": dealer.DealerName}).Error
  3298. return err
  3299. }
  3300. func GetDrugDataConfig(orgid int64, name string) (models.DictDataconfig, error) {
  3301. dataconfig := models.DictDataconfig{}
  3302. err := XTReadDB().Where("org_id = ? and name = ?", orgid, name).Find(&dataconfig).Error
  3303. return dataconfig, err
  3304. }
  3305. func GetParentDataConfig(parentid int64, orgid int64) (dataconfig []*models.DictDataconfig, err error) {
  3306. err = XTReadDB().Where("parent_id = ? and status =1 and (org_id = ? or org_id = 0)", parentid, orgid).Find(&dataconfig).Error
  3307. return dataconfig, err
  3308. }
  3309. func IsExistDicConfig(parentid int64, goodKind string, orgid int64) (*models.DictDataconfig, error) {
  3310. var dataconfig models.DictDataconfig
  3311. var err error
  3312. err = XTReadDB().Where("parent_id = ? and status = 1 and name = ? and (org_id = ? or org_id = 0)", parentid, goodKind, orgid).Find(&dataconfig).Error
  3313. if err == gorm.ErrRecordNotFound {
  3314. return nil, err
  3315. }
  3316. if err != nil {
  3317. return nil, err
  3318. }
  3319. return &dataconfig, nil
  3320. }
  3321. func GetLastDicConfig(parentid int64, orgid int64) (models.DictDataconfig, error) {
  3322. dataconfig := models.DictDataconfig{}
  3323. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status = 1", parentid, orgid).Last(&dataconfig).Error
  3324. return dataconfig, err
  3325. }
  3326. func CreatedDicConfig(dataconfig *models.DictDataconfig) error {
  3327. err := XTWriteDB().Create(&dataconfig).Error
  3328. return err
  3329. }
  3330. func UpdatedDicConfig(parentid int64, dataconfig *models.DictDataconfig, orgid int64, goodKind string) error {
  3331. err := XTWriteDB().Model(&dataconfig).Where("parent_id = ? and org_id = ? and name = ?", parentid, orgid, goodKind).Updates(map[string]interface{}{"name": dataconfig.Name}).Error
  3332. return err
  3333. }
  3334. func GetGoodType(goodName string, orgid int64) (*models.GoodsType, error) {
  3335. var goodType models.GoodsType
  3336. var err error
  3337. err = XTReadDB().Where("type_name =? and org_id = ? and status = 1", goodName, orgid).Find(&goodType).Error
  3338. if err == gorm.ErrRecordNotFound {
  3339. return nil, err
  3340. }
  3341. if err != nil {
  3342. return nil, err
  3343. }
  3344. return &goodType, nil
  3345. }
  3346. func CreatedGoodType(goodsType *models.GoodsType) error {
  3347. err := XTWriteDB().Create(&goodsType).Error
  3348. return err
  3349. }
  3350. func GetAllGoodType(orgid int64) (goodsType []*models.GoodsType, err error) {
  3351. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3352. return goodsType, err
  3353. }
  3354. func GetAllNewGoodType(orgid int64) (goodsType []*models.NewGoodsType, err error) {
  3355. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3356. return goodsType, err
  3357. }
  3358. func GetDepartMentByName(name string, orgid int64) (*models.XtHisDepartment, error) {
  3359. var department models.XtHisDepartment
  3360. var err error
  3361. err = XTReadDB().Where("name= ? and user_org_id = ? and status = 1", name, orgid).Find(&department).Error
  3362. if err == gorm.ErrRecordNotFound {
  3363. return nil, err
  3364. }
  3365. if err != nil {
  3366. return nil, err
  3367. }
  3368. return &department, nil
  3369. }
  3370. func GetDataConfigIsExist(orgid int64, name string) (models.Dataconfig, error) {
  3371. dataconfig := models.Dataconfig{}
  3372. err := XTReadDB().Model(&dataconfig).Where("org_id = ? and name = ? and status = 1", orgid, name).Find(&dataconfig).Error
  3373. return dataconfig, err
  3374. }
  3375. func GetChildeConfigIsExist(parentid int64, name string, orgid int64) (*models.Dataconfig, error) {
  3376. var dataconfig models.Dataconfig
  3377. var err error
  3378. 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
  3379. if err == gorm.ErrRecordNotFound {
  3380. return nil, err
  3381. }
  3382. if err != nil {
  3383. return nil, err
  3384. }
  3385. return &dataconfig, nil
  3386. }
  3387. func GetLastChildeConfig(parentid int64, orgid int64) (models.Dataconfig, error) {
  3388. dataconfig := models.Dataconfig{}
  3389. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status =1", parentid, orgid).Last(&dataconfig).Error
  3390. return dataconfig, err
  3391. }
  3392. func CreateDataConfig(dataconfig *models.Dataconfig) error {
  3393. err := XTWriteDB().Create(&dataconfig).Error
  3394. return err
  3395. }
  3396. func FindAllDataConfigList(orgid int64, parentId int64) (dataconfig []*models.Dataconfig, err error) {
  3397. err = XTReadDB().Model(&dataconfig).Where("(org_id = ? or org_id = 0) and parent_id = ? and status = 1", orgid, parentId).Find(&dataconfig).Error
  3398. return dataconfig, err
  3399. }
  3400. func GetWarehouseOrderInfoList(orgid int64) (list []*models.WarehousingInfo, err error) {
  3401. err = XTReadDB().Model(&list).Where("org_id = ? and status = 1", orgid).Order("id asc").Group("good_id").Find(&list).Error
  3402. return list, err
  3403. }
  3404. func GetSearchDrugList(keywords string, orgid int64) (drug []*models.BaseDrugLib, err error) {
  3405. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3406. if len(keywords) > 0 {
  3407. likekey := "%" + keywords + "%"
  3408. err = db.Where("(drug_name LIKE ?)", likekey).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3409. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3410. }).Find(&drug).Error
  3411. } else {
  3412. err = db.Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3413. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3414. }).Find(&drug).Error
  3415. }
  3416. return
  3417. }
  3418. func GetSearchDrugListSix(keywords string, orgid int64, storehouse_id int64) (drug []*models.BaseDrugLib, err error) {
  3419. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3420. if len(keywords) > 0 {
  3421. likekey := "%" + keywords + "%"
  3422. 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 {
  3423. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3424. }).Find(&drug).Error
  3425. } else {
  3426. 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 {
  3427. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3428. }).Find(&drug).Error
  3429. }
  3430. return
  3431. }
  3432. func GetSearchGoodList(keywords string, orgid int64) (good []*models.GoodInfo, err error) {
  3433. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 and good_status <> '停用'", orgid)
  3434. if len(keywords) > 0 {
  3435. likekey := "%" + keywords + "%"
  3436. err = db.Where("(good_name Like ?)", likekey).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3437. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3438. }).Find(&good).Error
  3439. } else {
  3440. err = db.Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3441. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3442. }).Find(&good).Error
  3443. }
  3444. return
  3445. }
  3446. func GetSearchGoodListSix(keywords string, orgid int64, storehouse_id int64) (good []*models.GoodInfoSevenThirty, err error) {
  3447. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 AND find_in_set('停用',good_status) = 0", orgid)
  3448. if len(keywords) > 0 {
  3449. likekey := "%" + keywords + "%"
  3450. 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 {
  3451. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id =?", orgid, storehouse_id)
  3452. }).Find(&good).Error
  3453. } else {
  3454. 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 {
  3455. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id)
  3456. }).Find(&good).Error
  3457. }
  3458. return
  3459. }
  3460. func CreateAutoReduceRecord(detail *models.BloodAutomaticReduceDetail) error {
  3461. db := writeDb.Begin()
  3462. err := db.Create(&detail).Error
  3463. if err != nil {
  3464. db.Rollback()
  3465. return err
  3466. }
  3467. db.Commit()
  3468. return err
  3469. }
  3470. func GetAutoMaticReduceDetail(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (*models.BloodAutomaticReduceDetail, error) {
  3471. var autoreduece models.BloodAutomaticReduceDetail
  3472. var err error
  3473. 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
  3474. if err == gorm.ErrRecordNotFound {
  3475. return nil, err
  3476. }
  3477. if err != nil {
  3478. return nil, err
  3479. }
  3480. return &autoreduece, nil
  3481. }
  3482. func GetAutoMaticReduceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (models.BloodAutomaticReduceDetail, error) {
  3483. auto := models.BloodAutomaticReduceDetail{}
  3484. 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
  3485. return auto, err
  3486. }
  3487. func DeleteAutoRedeceDetailTwo(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) error {
  3488. detail := models.BloodAutomaticReduceDetail{}
  3489. 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
  3490. return err
  3491. }
  3492. func GetWarehosueOutByStockFlow(patient_id int64, recrod_date int64, goodid int64) (stockflow []*models.VmStockFlow, err error) {
  3493. 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
  3494. return stockflow, err
  3495. }
  3496. func UpdateAutoGood(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64, count int64) (err error) {
  3497. 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
  3498. 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
  3499. 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
  3500. return err
  3501. }
  3502. func DeleteAutoRedeceDetailTen(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3503. detail := models.BloodAutomaticReduceDetail{}
  3504. 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
  3505. info := models.WarehouseOutInfo{}
  3506. 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
  3507. prepare := models.DialysisBeforePrepare{}
  3508. 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
  3509. flow := models.VmStockFlow{}
  3510. 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
  3511. return err
  3512. }
  3513. func DeleteAutoRedeceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3514. detail := models.BloodAutomaticReduceDetail{}
  3515. 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
  3516. info := models.WarehouseOutInfo{}
  3517. 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
  3518. prepare := models.DialysisBeforePrepare{}
  3519. 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
  3520. flow := models.VmStockFlow{}
  3521. 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
  3522. return err
  3523. }
  3524. func DeleteAutoRedeceDetailTenOne(orgid int64, patient_id int64, recordtime int64, goodid int64, order_id int64) error {
  3525. detail := models.BloodAutomaticReduceDetail{}
  3526. 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
  3527. info := models.WarehouseOutInfo{}
  3528. 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
  3529. prepare := models.DialysisBeforePrepare{}
  3530. 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
  3531. flow := models.VmStockFlow{}
  3532. 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
  3533. return err
  3534. }
  3535. func UpDateWarehouseInfoByStockDelete(id int64, count int64, patient_id int64, record_time int64, goodid int64) (err error) {
  3536. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  3537. //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
  3538. return err
  3539. }
  3540. func GetCancelStockInfoByPatientId(patientid int64, goodid int64, record_date int64) (info []*models.CancelStockInfo, err error) {
  3541. err = XTReadDB().Where("patient_id = ? and good_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&info).Error
  3542. return info, err
  3543. }
  3544. func GetHisPrepscritionByPatientId(patientid int64, goodid int64, record_date int64) (list []*models.HisPrescriptionProject, err error) {
  3545. err = XTReadDB().Where("patient_id = ? and project_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&list).Error
  3546. return list, err
  3547. }
  3548. func UpDateWarehouStockFlowByStockDelete(id int64, recordtime int64, good_id int64, count int64, patient_id int64) (err error) {
  3549. 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
  3550. return err
  3551. }
  3552. func UpDateWarehouseInfoByStockDeleteOne(id int64, count int64) (err error) {
  3553. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  3554. return err
  3555. }
  3556. func GetAutoDialysisBefor(goodid int64, goodtypeid int64, patientid int64, orgid int64, recorddate int64) (*models.BloodAutomaticReduceDetail, error) {
  3557. var autoreduece models.BloodAutomaticReduceDetail
  3558. var err error
  3559. 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
  3560. if err == gorm.ErrRecordNotFound {
  3561. return nil, err
  3562. }
  3563. if err != nil {
  3564. return nil, err
  3565. }
  3566. return &autoreduece, nil
  3567. }
  3568. func GetAutoRecordByGoodIdSeven(goodid int64, patientid int64, recorddate int64) (models.WarehouseOutInfoSeven, error) {
  3569. detail := models.WarehouseOutInfoSeven{}
  3570. err := XTReadDB().Select("id,count").Where(" good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  3571. return detail, err
  3572. }
  3573. func GetAutoRecordByGoodIdSevenEight(goodid int64, patientid int64, recorddate int64) (detail []*models.WarehouseOutInfoSeven, err error) {
  3574. err = XTReadDB().Select("id,count").Where("good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  3575. return detail, err
  3576. }
  3577. func GetHisProjectPrescriptionByPatientId(patientid int64, recorddate int64) (project []*models.HisPrescriptionProject, err error) {
  3578. err = XTReadDB().Model(&project).Where("patient_id = ? and record_date = ? and status =1", patientid, recorddate).Find(&project).Error
  3579. return project, err
  3580. }
  3581. func GetHisProjectPrescriptionByPatientIdOne(id int64) (models.HisPrescriptionProject, error) {
  3582. project := models.HisPrescriptionProject{}
  3583. err = XTReadDB().Model(&project).Where("id = ? and status =1", id).Find(&project).Error
  3584. return project, err
  3585. }
  3586. func GetAllAutoRecordByPatient(patientid int64, recorddate int64) (auto []*models.WarehouseOutInfoNight, err error) {
  3587. 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
  3588. return auto, err
  3589. }
  3590. func ModefyWarehouseInfo(count int64, id int64) error {
  3591. err := XTWriteDB().Model(models.WarehousingInfo{}).Where(" id =? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  3592. return err
  3593. }
  3594. func DeleteAutoWarehouse(patient_id int64, record_time int64) error {
  3595. 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
  3596. 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
  3597. 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
  3598. //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
  3599. return err
  3600. }
  3601. func FindGoodInfoByIdTwo(id int64) (goodInfo models.GoodInfo, err error) {
  3602. err = readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  3603. return
  3604. }
  3605. func FindGoodInfoByIdSeven(id int64) (goodInfo models.GoodInfoSeven, err error) {
  3606. err = readDb.Select("id,good_name").Where("id = ? AND status = 1", id).First(&goodInfo).Error
  3607. return
  3608. }
  3609. func GetGoodWarehouseInfo(good_id int64) (info []*models.WarehousingInfo, err error) {
  3610. err = readDb.Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  3611. return info, err
  3612. }
  3613. func GetGoodWarehouseInfoSeven(good_id int64) (info []*models.WarehousingInfoSeven, err error) {
  3614. err = readDb.Select("stock_count").Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  3615. return info, err
  3616. }
  3617. func GetGoodWarehouseInfoSevenTen(good_id int64, storehouse_id int64) (info []*models.WarehousingInfoSeven, err error) {
  3618. 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
  3619. return info, err
  3620. }
  3621. func GetAllStockList(page int64, limit int64, startime int64, endtime int64, good_type int64, keyword string, orgid int64) (info []*models.VmWarehousingInfo, total int64, err error) {
  3622. offset := (page - 1) * limit
  3623. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1")
  3624. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3625. fmt.Println(table)
  3626. if startime > 0 {
  3627. db = db.Where("x.ctime>=?", startime)
  3628. }
  3629. if endtime > 0 {
  3630. db = db.Where("x.ctime<=?", endtime)
  3631. }
  3632. likeKey := "%" + keyword + "%"
  3633. if len(likeKey) > 0 {
  3634. db = db.Where("t.good_name like ?", likeKey)
  3635. }
  3636. if orgid > 0 {
  3637. db = db.Where("x.org_id = ?", orgid)
  3638. }
  3639. if good_type > 0 {
  3640. db = db.Where("t.good_type_id = ?", good_type)
  3641. }
  3642. 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
  3643. return info, total, err
  3644. }
  3645. 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) {
  3646. offset := (page - 1) * limit
  3647. db := XTReadDB().Table("xt_good_information as x").Where("x.status= 1")
  3648. likeKey := "%" + keyword + "%"
  3649. if len(likeKey) > 0 {
  3650. db = db.Where("x.good_name like ?", likeKey)
  3651. }
  3652. if orgid > 0 {
  3653. db = db.Where("x.org_id = ?", orgid)
  3654. }
  3655. if good_type > 0 {
  3656. db = db.Where("x.good_type_id = ?", good_type)
  3657. }
  3658. if good_id > 0 {
  3659. db = db.Where("x.id = ?", good_id)
  3660. }
  3661. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3662. if startime > 0 {
  3663. db = db.Where("ctime>=?", startime)
  3664. }
  3665. if endtime > 0 {
  3666. db = db.Where("ctime<=?", endtime)
  3667. }
  3668. if good_id > 0 {
  3669. db = db.Where("good_id = ?", good_id)
  3670. }
  3671. if storehouse_id > 0 {
  3672. db = db.Where("storehouse_id = ?", storehouse_id)
  3673. }
  3674. return db.Where("status = 1 and is_check = 1")
  3675. }).Preload("WarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3676. if startime > 0 {
  3677. db = db.Where("ctime>=?", startime)
  3678. }
  3679. if endtime > 0 {
  3680. db = db.Where("ctime<=?", endtime)
  3681. }
  3682. if good_id > 0 {
  3683. db = db.Where("good_id = ?", good_id)
  3684. }
  3685. if storehouse_id > 0 {
  3686. db = db.Where("storehouse_id = ?", storehouse_id)
  3687. }
  3688. return db.Where("status = 1").Group("good_id,storehouse_id")
  3689. }).Preload("CancelStockInfo", func(db *gorm.DB) *gorm.DB {
  3690. if startime > 0 {
  3691. db = db.Where("ctime>=?", startime)
  3692. }
  3693. if endtime > 0 {
  3694. db = db.Where("ctime<=?", endtime)
  3695. }
  3696. return db.Where("status = 1")
  3697. }).Find(&info).Error
  3698. return info, total, err
  3699. }
  3700. 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) {
  3701. offset := (page - 1) * limit
  3702. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1 and x.is_check = 1")
  3703. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3704. tables := XTReadDB().Table("xt_stock_flow as f").Where("f.status = 1 and f.consumable_type = 1 and f.user_org_id = ?", orgid)
  3705. fmt.Println(table, tables)
  3706. if startime > 0 {
  3707. db = db.Where("x.ctime>=?", startime)
  3708. }
  3709. if endtime > 0 {
  3710. db = db.Where("x.ctime<=?", endtime)
  3711. }
  3712. if startfirsttime > 0 {
  3713. db = db.Where("x.expiry_date >=?", startfirsttime)
  3714. }
  3715. if endfirsttime > 0 {
  3716. db = db.Where("x.expiry_date <=?", endfirsttime)
  3717. }
  3718. if orgid > 0 {
  3719. db = db.Where("x.org_id = ?", orgid)
  3720. }
  3721. if good_id > 0 {
  3722. db = db.Where("x.good_id = ?", good_id)
  3723. }
  3724. 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
  3725. return info, total, err
  3726. }
  3727. 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) {
  3728. offset := (page - 1) * limit
  3729. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status= 1")
  3730. if startime > 0 {
  3731. db = db.Where("x.ctime>=?", startime)
  3732. }
  3733. if endtime > 0 {
  3734. db = db.Where("x.ctime<=?", endtime)
  3735. }
  3736. if orgid > 0 {
  3737. db = db.Where("x.org_id = ?", orgid)
  3738. }
  3739. if good_id > 0 {
  3740. db = db.Where("x.good_id = ?", good_id)
  3741. }
  3742. if is_sys == 2 {
  3743. db = db.Where("x.is_sys = 1")
  3744. }
  3745. if is_sys == 3 {
  3746. db = db.Where("x.is_sys = 0")
  3747. }
  3748. 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
  3749. return info, total, err
  3750. }
  3751. func GetStockDrugCount(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  3752. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3753. if startime > 0 {
  3754. db = db.Where("x.ctime >=?", startime)
  3755. }
  3756. if endtime > 0 {
  3757. db = db.Where("x.ctime<=?", endtime)
  3758. }
  3759. if orgid > 0 {
  3760. db = db.Where("x.org_id = ?", orgid)
  3761. }
  3762. 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
  3763. return info, err
  3764. }
  3765. func GetAutoDiallysisBefor(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  3766. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3767. if startime > 0 {
  3768. db = db.Where("x.ctime >=?", startime)
  3769. }
  3770. if endtime > 0 {
  3771. db = db.Where("x.ctime<=?", endtime)
  3772. }
  3773. if orgid > 0 {
  3774. db = db.Where("x.org_id = ?", orgid)
  3775. }
  3776. err = db.Select("sum(x.count) as count,x.good_id").Where("is_sys = 0").Group("x.good_id").Scan(&info).Error
  3777. return info, err
  3778. }
  3779. func GetOutStockTotalCountFour(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3780. if endtime > 0 {
  3781. 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
  3782. }
  3783. if endtime == 0 {
  3784. 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
  3785. }
  3786. return autoMatic, err
  3787. }
  3788. func GetGoodWarehouseOutById(id int64, orgid int64) (models.WarehouseOut, error) {
  3789. out := models.WarehouseOut{}
  3790. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", id, orgid).Find(&out).Error
  3791. return out, err
  3792. }
  3793. func GetOrderDetialByOrderId(id int64, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  3794. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3795. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3796. fmt.Println(table)
  3797. if id > 0 {
  3798. db = db.Where("x.warehouse_out_id = ?", id)
  3799. }
  3800. if orgid > 0 {
  3801. db = db.Where("x.org_id = ?", orgid)
  3802. }
  3803. 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
  3804. return out, err
  3805. }
  3806. func GetWarehouseOutOrder(id []string, orgid int64) (out []*models.WarehouseOut, err error) {
  3807. db := XTReadDB().Table("xt_warehouse_out as x").Where("x.status = 1")
  3808. if len(id) > 0 {
  3809. db = db.Where("x.id in(?)", id)
  3810. }
  3811. if orgid > 0 {
  3812. db = db.Where("x.org_id= ?", orgid)
  3813. }
  3814. 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
  3815. return out, err
  3816. }
  3817. func GetOrderDetialByOrderIdOne(id []string, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  3818. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3819. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3820. tableOne := XTReadDB().Table("xt_warehouse_info as n").Where("n.status = 1")
  3821. fmt.Println(table, tableOne)
  3822. if len(id) > 0 {
  3823. db = db.Where("x.warehouse_out_id in (?)", id)
  3824. }
  3825. if orgid > 0 {
  3826. db = db.Where("x.org_id = ?", orgid)
  3827. }
  3828. 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
  3829. return out, err
  3830. }
  3831. func GetOrderDetailStockFlowByStorehouseById(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  3832. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  3833. if len(id) > 0 {
  3834. db = db.Where("warehouse_out_id in (?)", id)
  3835. }
  3836. if orgid > 0 {
  3837. db = db.Where("user_org_id = ?", orgid)
  3838. }
  3839. err = db.Group("warehousing_id").Find(&list).Error
  3840. return list, err
  3841. }
  3842. func GetOrderDetailStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  3843. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  3844. if len(id) > 0 {
  3845. db = db.Where("warehouse_out_id in (?)", id)
  3846. }
  3847. if orgid > 0 {
  3848. db = db.Where("user_org_id = ?", orgid)
  3849. }
  3850. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  3851. return list, err
  3852. }
  3853. func GetOrderStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  3854. db := XTReadDB().Model(&list).Where("status = 1 and consumable_type = 7 and patient_id > 0 and count>0")
  3855. if len(id) > 0 {
  3856. db = db.Where("warehouse_out_id in (?)", id)
  3857. }
  3858. if orgid > 0 {
  3859. db = db.Where("user_org_id = ?", orgid)
  3860. }
  3861. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  3862. return list, err
  3863. }
  3864. func GetLastWarehouseOutInfo(ids []string, orgId int64) (info []*models.WarehouseOutInfoSix, err error) {
  3865. db := XTReadDB().Model(&info)
  3866. db = db.Preload("GoodInfo", "status = 1")
  3867. 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
  3868. return info, err
  3869. }
  3870. func FindUserDetailByIdOne(good_id int64, record_time int64, org_id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  3871. db := readDb.Model(&models.AutomaticReduceDetail{})
  3872. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  3873. db = db.Preload("GoodsType", "status = 1")
  3874. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3875. db = db.Preload("WarehouseOutInfo", "org_id = ? and status = 1 and good_id = ? and sys_record_time = ?", org_id, good_id, record_time)
  3876. db = db.Where("status = 1 AND org_id = ? AND good_id = ? AND record_time = ? and count<> 0 ", org_id, good_id, record_time)
  3877. db = db.Count(&total)
  3878. err = db.Find(&user).Error
  3879. return
  3880. }
  3881. func GetGoodInformationByGoodId(good_id int64) (models.GoodInfo, error) {
  3882. info := models.GoodInfo{}
  3883. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  3884. return info, err
  3885. }
  3886. func GetGoodInformationByGoodIdThirty(good_id int64) (models.GoodInformationThirty, error) {
  3887. info := models.GoodInformationThirty{}
  3888. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  3889. return info, err
  3890. }
  3891. func GetNewGoodInformationByGoodIdThirty(good_id int64, tx *gorm.DB) (models.GoodInformationThirty, error) {
  3892. info := models.GoodInformationThirty{}
  3893. err := tx.Where("id = ? and status = 1", good_id).Find(&info).Error
  3894. if err != gorm.ErrRecordNotFound {
  3895. if err != nil {
  3896. tx.Rollback()
  3897. return info, err
  3898. }
  3899. }
  3900. return info, err
  3901. }
  3902. func UpdateGoodInfoList(info models.GoodInfo, id int64) error {
  3903. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"total": info.Total}).Error
  3904. return err
  3905. }
  3906. func GetExprotStockList(orgid int64, orderid []string, startime int64, endtime int64) (info []*models.StWarehousingInfo, err error) {
  3907. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3908. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3909. fmt.Println(table)
  3910. if orgid > 0 {
  3911. db = db.Where("x.org_id = ?", orgid)
  3912. }
  3913. if len(orderid) > 0 {
  3914. db = db.Where("x.warehousing_id in(?)", orderid)
  3915. }
  3916. if startime > 0 {
  3917. db = db.Where("x.ctime>=?", startime)
  3918. }
  3919. if endtime > 0 {
  3920. db = db.Where("x.ctime<=?", endtime)
  3921. }
  3922. 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
  3923. return info, err
  3924. }
  3925. func GetExprotStockListTwenty(orgid int64, orderid []string, startime int64, endtime int64, storehouse_id int64) (info []*models.StWarehousingInfo, err error) {
  3926. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3927. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3928. fmt.Println(table)
  3929. if orgid > 0 {
  3930. db = db.Where("x.org_id = ?", orgid)
  3931. }
  3932. if len(orderid) > 0 {
  3933. db = db.Where("x.warehousing_id in(?)", orderid)
  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. if storehouse_id > 0 {
  3942. db = db.Where("x.storehouse_id = ?", storehouse_id)
  3943. }
  3944. 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
  3945. return info, err
  3946. }
  3947. func GetDrugWarehouOrderInfo(startime int64, endtime int64, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  3948. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  3949. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  3950. fmt.Println(table)
  3951. if startime > 0 {
  3952. db = db.Where("x.ctime>=?", startime)
  3953. }
  3954. if endtime > 0 {
  3955. db = db.Where("x.ctime<=?", endtime)
  3956. }
  3957. if orgid > 0 {
  3958. db = db.Where("x.org_id = ?", orgid)
  3959. }
  3960. 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
  3961. return info, err
  3962. }
  3963. func GetOutExprotList(orgid int64, ids []string, startime int64, endtime int64) (out []*models.WarehouseOutInfoOne, err error) {
  3964. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1 ")
  3965. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3966. tableOne := XTReadDB().Table("xt_warehouse_out as p").Where("p.status=1")
  3967. fmt.Println(table, tableOne)
  3968. if orgid > 0 {
  3969. db = db.Where("x.org_id = ?", orgid)
  3970. }
  3971. if len(ids) > 0 {
  3972. db = db.Where("x.warehouse_out_id in (?)", ids)
  3973. }
  3974. if startime > 0 {
  3975. db = db.Where("x.ctime>=?", startime)
  3976. }
  3977. if endtime > 0 {
  3978. db = db.Where("x.ctime<=?", endtime)
  3979. }
  3980. 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
  3981. return out, err
  3982. }
  3983. func FindeDrugWarehouseOutOrder(ids []string, orgid int64) (info []*models.DrugWarehouseOut, err error) {
  3984. db := XTReadDB().Table("xt_drug_warehouse_out as x").Where("x.status = 1")
  3985. if len(ids) > 0 {
  3986. db = db.Where("x.id in(?)", ids)
  3987. }
  3988. if orgid > 0 {
  3989. db = db.Where("x.org_id = ?", orgid)
  3990. }
  3991. 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
  3992. return info, err
  3993. }
  3994. func FindeDrugWarehouseOutDetail(ids []string, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  3995. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status =1 ")
  3996. table := XTReadDB().Table("x.base_drug as t").Where("t.status = 1")
  3997. fmt.Println(table)
  3998. if len(ids) > 0 {
  3999. db = db.Where("x.warehouse_out_id in(?)", ids)
  4000. }
  4001. if orgid > 0 {
  4002. db = db.Where("x.org_id = ?", orgid)
  4003. }
  4004. 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
  4005. return info, err
  4006. }
  4007. func GetDrugNameByKeyword(keyword string, orgid int64) (basedrug []*models.XtBaseDrug, err error) {
  4008. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1")
  4009. likeKey := "%" + keyword + "%"
  4010. if len(keyword) > 0 {
  4011. db = db.Where("x.drug_name like ?", likeKey)
  4012. }
  4013. if orgid > 0 {
  4014. db = db.Where("x.org_id = ?", orgid)
  4015. }
  4016. err = db.Find(&basedrug).Error
  4017. return basedrug, err
  4018. }
  4019. func GetDrugWarehouseOrderDetail(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  4020. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4021. if len(ids) > 0 {
  4022. db = db.Where("x.drug_id in(?)", ids)
  4023. }
  4024. if orgid > 0 {
  4025. db = db.Where("x.org_id = ?", orgid)
  4026. }
  4027. if startime > 0 {
  4028. db = db.Where("x.ctime>=?", startime)
  4029. }
  4030. if endtime > 0 {
  4031. db = db.Where("x.ctime<=?", endtime)
  4032. }
  4033. if storehouse_id > 0 {
  4034. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4035. }
  4036. err = db.Find(&info).Error
  4037. return info, err
  4038. }
  4039. func GetDrugWarehouseOrderDetailTwo(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  4040. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4041. if len(ids) > 0 {
  4042. db = db.Where("x.drug_id in(?)", ids)
  4043. }
  4044. if orgid > 0 {
  4045. db = db.Where("x.org_id = ?", orgid)
  4046. }
  4047. if startime > 0 {
  4048. db = db.Where("x.ctime>=?", startime)
  4049. }
  4050. if endtime > 0 {
  4051. db = db.Where("x.ctime<=?", endtime)
  4052. }
  4053. if storehouse_id > 0 {
  4054. db = db.Where("storehouse_id = ?", storehouse_id)
  4055. }
  4056. err = db.Find(&info).Error
  4057. return info, err
  4058. }
  4059. func GetDrugTotalCount(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4060. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  4061. if drugid > 0 {
  4062. db = db.Where("x.drug_id = ?", drugid)
  4063. }
  4064. if orgid > 0 {
  4065. db = db.Where("x.org_id = ?", orgid)
  4066. }
  4067. if storehouse_id > 0 {
  4068. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4069. }
  4070. 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
  4071. return info, err
  4072. }
  4073. func GetDrugTotalCountTwoTwety(drugid int64, orgid int64, storehouse_id int64, tx *gorm.DB) (info models.VmDrugWarehouseInfo, err error) {
  4074. db := tx.Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  4075. if drugid > 0 {
  4076. db = db.Where("x.drug_id = ?", drugid)
  4077. }
  4078. if orgid > 0 {
  4079. db = db.Where("x.org_id = ?", orgid)
  4080. }
  4081. if storehouse_id > 0 {
  4082. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4083. }
  4084. 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
  4085. if err != gorm.ErrRecordNotFound {
  4086. if err != nil {
  4087. tx.Rollback()
  4088. return info, err
  4089. }
  4090. }
  4091. return info, err
  4092. }
  4093. func GetDrugTotalCountTwenty(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4094. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1 ")
  4095. if drugid > 0 {
  4096. db = db.Where("x.drug_id = ?", drugid)
  4097. }
  4098. if orgid > 0 {
  4099. db = db.Where("x.org_id = ?", orgid)
  4100. }
  4101. if storehouse_id > 0 {
  4102. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4103. }
  4104. 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
  4105. return info, err
  4106. }
  4107. func GetDrugTotalCountSwx(id int64, orgid int64) (info models.VmDrugWarehouseInfo, err error) {
  4108. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4109. if id > 0 {
  4110. db = db.Where("x.id = ?", id)
  4111. }
  4112. if orgid > 0 {
  4113. db = db.Where("x.org_id = ?", orgid)
  4114. }
  4115. 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
  4116. return info, err
  4117. }
  4118. func GetDrugTotalCountOne(drugid int64, orgid int64, id int64) (models.VmDrugWarehouseInfo, error) {
  4119. info := models.VmDrugWarehouseInfo{}
  4120. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4121. if id == 0 {
  4122. db = db.Where("id = ?", id)
  4123. }
  4124. if drugid > 0 {
  4125. db = db.Where("x.drug_id = ?", drugid)
  4126. }
  4127. if orgid > 0 {
  4128. db = db.Where("x.org_id = ?", orgid)
  4129. }
  4130. err = db.Select("x.drug_id,x.stock_max_number,x.stock_min_number,x.max_unit,x.min_unit").Find(&info).Error
  4131. return info, err
  4132. }
  4133. func GetDrugTotalCountTwo(drugid int64, storehouse_id int64) (list []*models.VmDrugWarehouseInfo, err error) {
  4134. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4135. table := XTReadDB().Table("xt_base_drug as d").Where("d.status = 1")
  4136. fmt.Println(table)
  4137. if drugid > 0 {
  4138. db = db.Where("x.drug_id = ?", drugid)
  4139. }
  4140. if storehouse_id > 0 {
  4141. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4142. }
  4143. 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
  4144. return list, err
  4145. }
  4146. func GetDrugByGoodId(drugid int64) (models.BaseDrugLib, error) {
  4147. lib := models.BaseDrugLib{}
  4148. err := XTReadDB().Model(&lib).Where("id = ? and status = 1", drugid).Find(&lib).Error
  4149. return lib, err
  4150. }
  4151. func GetWarehoureOrderInfoList(goodid int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4152. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4153. if goodid > 0 {
  4154. db = db.Where("x.good_id = ?", goodid)
  4155. }
  4156. if orgid > 0 {
  4157. db = db.Where("x.org_id = ?", orgid)
  4158. }
  4159. 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
  4160. return info, err
  4161. }
  4162. func UpdateWarehousingCount(info *models.WarehousingInfo, goodid int64, orgid int64, warehousingId int64) error {
  4163. 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
  4164. return err
  4165. }
  4166. func GetSingleCancelOrder(id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  4167. err = XTReadDB().Where("cancel_stock_id = ? and status = 1 and org_id = ?", id, orgid).Preload("GoodInfo", "status = 1").Find(&info).Error
  4168. return info, err
  4169. }
  4170. func UpdateCancelOut(list []*models.WarehousingInfo, count int64) error {
  4171. //遍历循环入库数据
  4172. for i, item := range list {
  4173. //计算每个批次剩余库存与入库数据的差 然后和退库数量比较
  4174. if (item.WarehousingCount - item.StockCount) >= count {
  4175. //如果差距大于退库数量,则直接把退库数量增加到该批次中
  4176. info := models.WarehousingInfo{
  4177. GoodId: item.GoodId,
  4178. OrgId: item.OrgId,
  4179. WarehousingId: item.WarehousingId,
  4180. StockCount: item.StockCount + count,
  4181. }
  4182. err2 := UpdateWarehousingCount(&info, item.GoodId, item.OrgId, item.WarehousingId)
  4183. fmt.Println("2323233232323232323232323232323", err2)
  4184. return err
  4185. }
  4186. if (item.WarehousingCount - item.StockCount) < count {
  4187. //如果该批次小于退库数量,则先出库该相差数量
  4188. var countOne = item.WarehousingCount - item.StockCount
  4189. infoOne := models.WarehousingInfo{
  4190. GoodId: item.GoodId,
  4191. OrgId: item.OrgId,
  4192. WarehousingId: item.WarehousingId,
  4193. StockCount: countOne + item.StockCount,
  4194. }
  4195. err := UpdateWarehousingCount(&infoOne, item.GoodId, item.OrgId, item.WarehousingId)
  4196. if err == nil {
  4197. //剩余的出库到第二个批次
  4198. var countTwo = count - countOne
  4199. infos := append(list, list[i+1])
  4200. var idArray []*models.WarehousingInfo
  4201. for _, it := range infos {
  4202. if it.WarehousingId != item.WarehousingId {
  4203. idArray = append(idArray, it)
  4204. }
  4205. }
  4206. //查询更新后的库存
  4207. UpdateCancelOut(idArray, countTwo)
  4208. return err
  4209. } else {
  4210. return err
  4211. }
  4212. }
  4213. }
  4214. return err
  4215. }
  4216. func GetCancelStockOrderPrint(idstr []string) (info []*models.CancelStockInfo, err error) {
  4217. if len(idstr) > 0 {
  4218. err = XTReadDB().Model(&info).Where("cancel_stock_id in(?)", idstr).Preload("GoodInfo", "status = 1").Find(&info).Error
  4219. } else {
  4220. err = XTReadDB().Model(&info).Preload("GoodInfo", "status = 1").Find(&info).Error
  4221. }
  4222. return info, err
  4223. }
  4224. func GetCancelStockOrderPrintOne(idstr []string, orgid int64) (info []*models.CancelStock, err error) {
  4225. if len(idstr) > 0 {
  4226. err = XTReadDB().Model(&info).Where("id in(?)", idstr).
  4227. Preload("XtCancelStockInfo", func(db *gorm.DB) *gorm.DB {
  4228. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodInfo", "status = 1 and org_id = ?", orgid)
  4229. }).Find(&info).Error
  4230. }
  4231. return info, err
  4232. }
  4233. func GetCancelOutTotalCount(startime int64, endtime int64, orgid int64) (info []*models.VmCancelStockInfo, err error) {
  4234. db := XTReadDB().Table("xt_cancel_stock_info as x").Where("x.status = 1")
  4235. if startime > 0 {
  4236. db = db.Where("x.ctime >=?", startime)
  4237. }
  4238. if endtime > 0 {
  4239. db = db.Where("x.ctime <= ?", endtime)
  4240. }
  4241. if orgid > 0 {
  4242. db = db.Where("x.org_id = ?", orgid)
  4243. }
  4244. err = db.Select("x.good_id,sum(x.count) as count").Group("x.good_id").Scan(&info).Error
  4245. return info, err
  4246. }
  4247. func GetWarehoureOutOrderInfo(goodid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  4248. info := models.WarehouseOutInfo{}
  4249. var err error
  4250. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1", goodid, orgid).Find(&info).Error
  4251. if err == gorm.ErrRecordNotFound {
  4252. return nil, err
  4253. }
  4254. if err != nil {
  4255. return nil, err
  4256. }
  4257. return &info, nil
  4258. }
  4259. func GetWarehoureOrderInfoCount(goodid int64, orgid int64, id int64) (info models.WarehouseOutInfo, err error) {
  4260. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  4261. if goodid > 0 {
  4262. db = db.Where("x.good_id = ?", goodid)
  4263. }
  4264. if orgid > 0 {
  4265. db = db.Where("x.org_id = ?", orgid)
  4266. }
  4267. if id > 0 {
  4268. db = db.Where("x.warehouse_info_id = ?", id)
  4269. }
  4270. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4271. return info, err
  4272. }
  4273. func GetWarehoureOrderStockOutFlow(goodid int64, orgid int64, id int64, storehouse_id int64) (info models.VmStockFlow, err error) {
  4274. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status =1 and x.consumable_type = 2 or x.consumable_type = 3")
  4275. if goodid > 0 {
  4276. db = db.Where("x.good_id = ?", goodid)
  4277. }
  4278. if orgid > 0 {
  4279. db = db.Where("x.user_org_id = ?", orgid)
  4280. }
  4281. if id > 0 {
  4282. db = db.Where("x.warehousing_id = ?", id)
  4283. }
  4284. if storehouse_id > 0 {
  4285. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4286. }
  4287. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4288. return info, err
  4289. }
  4290. func UpdateCancelInfo(id int64) (models.CancelStock, error) {
  4291. stock := models.CancelStock{}
  4292. err := XTWriteDB().Model(&stock).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4293. return stock, err
  4294. }
  4295. func GetStockBatchNumber(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4296. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1 and number <> ''", id, orgid).Find(&info).Error
  4297. return info, err
  4298. }
  4299. func GetWarehouseOutInfoById(id int64, goodid int64) (info []*models.WarehouseOutInfo, err error) {
  4300. err = XTReadDB().Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  4301. return info, err
  4302. }
  4303. func GetStockFlowBatchNumber(id int64, goodid int64) (stock []*models.VmStockFlow, err error) {
  4304. err = XTReadDB().Model(&stock).Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Group("patient_id,number").Find(&stock).Error
  4305. return stock, err
  4306. }
  4307. func GetStockFlowBatchNumberOne(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4308. 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)")
  4309. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4310. fmt.Println(table)
  4311. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4312. fmt.Println(tableone)
  4313. if id > 0 {
  4314. db = db.Where("x.warehouse_out_id = ?", id)
  4315. }
  4316. if goodid > 0 {
  4317. db = db.Where("x.good_id = ?", goodid)
  4318. }
  4319. 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
  4320. return stock, err
  4321. }
  4322. func GetStockFlowCancelInfo(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4323. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status = 1 and x.count > 0 and x.consumable_type = 7")
  4324. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4325. fmt.Println(table)
  4326. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4327. fmt.Println(tableone)
  4328. if id > 0 {
  4329. db = db.Where("x.warehouse_out_id = ?", id)
  4330. }
  4331. if goodid > 0 {
  4332. db = db.Where("x.good_id = ?", goodid)
  4333. }
  4334. 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
  4335. return stock, err
  4336. }
  4337. func GetDrugBatchNumber(drug_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  4338. err = XTReadDB().Model(&info).Where("drug_id = ? and org_id = ? and status = 1 and batch_number <>''", drug_id, orgid).Find(&info).Error
  4339. return info, err
  4340. }
  4341. func ExportDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseInfo, err error) {
  4342. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status =1")
  4343. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  4344. tableOne := XTReadDB().Table("xt_drug_warehouse as p")
  4345. fmt.Println("table", table)
  4346. fmt.Println("tableOne", tableOne)
  4347. if len(warehouse_out_id) > 0 {
  4348. db = db.Where("x.warehousing_id in(?)", warehouse_out_id)
  4349. }
  4350. 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
  4351. return out, err
  4352. }
  4353. func GetExportOutOrderDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4354. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4355. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  4356. fmt.Println("table", table)
  4357. if len(warehouse_out_id) > 0 {
  4358. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4359. }
  4360. 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
  4361. return out, err
  4362. }
  4363. func GetExportOutOrderDrugListOne(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4364. db := XTReadDB().Table("xt_drug_flow as x").Where("x.status = 1")
  4365. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  4366. fmt.Println("table", table)
  4367. if len(warehouse_out_id) > 0 {
  4368. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4369. }
  4370. 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
  4371. return out, err
  4372. }
  4373. func GetDrugOutListFlow(warehouse_out_id []string, orgid int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  4374. db := XTReadDB().Model(&info).Where("status = 1")
  4375. if len(warehouse_out_id) > 0 {
  4376. db = db.Where("warehouse_out_id in(?)", warehouse_out_id)
  4377. }
  4378. if orgid > 0 {
  4379. db = db.Where("org_id = ?", orgid)
  4380. }
  4381. err = db.Find(&info).Error
  4382. return info, err
  4383. }
  4384. func GetDrugWarehouseOut(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4385. err = XTReadDB().Where("warehouse_out_id in(?)", warehouse_out_id).Find(&out).Error
  4386. return out, err
  4387. }
  4388. func GetDrugWarehouseOutTenty(warehouse_out_id []string, orgid int64) (out []*models.DrugWarehouseOutTwenty, err error) {
  4389. err = XTReadDB().Where("id in(?)", warehouse_out_id).Preload("DrugWarehouseOutInfoTenty", func(db *gorm.DB) *gorm.DB {
  4390. 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)
  4391. }).Find(&out).Error
  4392. return out, err
  4393. }
  4394. func GetLastGoodInformationByGoodId(goodid int64, warehouse_info_id int64, orgid_id int64) (models.WarehouseOutInfo, error) {
  4395. info := models.WarehouseOutInfo{}
  4396. err := XTReadDB().Where("good_id = ? and warehouse_info_id= ? and status =1", goodid, warehouse_info_id).Last(&info).Error
  4397. return info, err
  4398. }
  4399. func GetTotalCountByGoodId(goodid int64, storehouse_id int64) (models.WarehousingInfo, error) {
  4400. info := models.WarehousingInfo{}
  4401. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4402. if goodid > 0 {
  4403. db = db.Where("x.good_id = ?", goodid)
  4404. }
  4405. if storehouse_id > 0 {
  4406. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4407. }
  4408. err := db.Select("x.good_id,sum(x.stock_count) as stock_count").Scan(&info).Error
  4409. return info, err
  4410. }
  4411. func GetLastGoodCountById(goodid int64, warehouse_info_id int64) (models.WarehousingInfo, error) {
  4412. info := models.WarehousingInfo{}
  4413. err := XTReadDB().Where("good_id = ? and id= ? and status = 1", goodid, warehouse_info_id).First(&info).Error
  4414. return info, err
  4415. }
  4416. func UpdateWarehousingInfo(info models.WarehousingInfo, id int64) error {
  4417. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  4418. return err
  4419. }
  4420. func FindFirstWarehousingInfoByGoodId(good_id int64, id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  4421. if storehouse_id == 0 {
  4422. err = readDb.Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime asc").First(&info).Error
  4423. }
  4424. if storehouse_id > 0 {
  4425. 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
  4426. }
  4427. return info, err
  4428. }
  4429. func CreateStockFlow(warehousingInfo []*models.VmStockFlow) (err error) {
  4430. if len(warehousingInfo) > 0 {
  4431. utx := writeDb.Begin()
  4432. if len(warehousingInfo) > 0 {
  4433. 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 "
  4434. insertParams := make([]string, 0)
  4435. insertData := make([]interface{}, 0)
  4436. for _, info := range warehousingInfo {
  4437. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  4438. insertData = append(insertData, info.WarehousingId)
  4439. insertData = append(insertData, info.GoodId)
  4440. insertData = append(insertData, info.Number)
  4441. insertData = append(insertData, info.LicenseNumber)
  4442. insertData = append(insertData, info.Count)
  4443. insertData = append(insertData, info.UserOrgId)
  4444. insertData = append(insertData, info.PatientId)
  4445. insertData = append(insertData, info.SystemTime)
  4446. insertData = append(insertData, info.ConsumableType)
  4447. insertData = append(insertData, info.IsSys)
  4448. insertData = append(insertData, info.WarehousingOrder)
  4449. insertData = append(insertData, info.WarehouseOutId)
  4450. insertData = append(insertData, info.WarehouseOutOrderNumber)
  4451. insertData = append(insertData, info.IsEdit)
  4452. insertData = append(insertData, info.CancelStockId)
  4453. insertData = append(insertData, info.CancelOrderNumber)
  4454. insertData = append(insertData, info.Manufacturer)
  4455. insertData = append(insertData, info.Dealer)
  4456. insertData = append(insertData, info.Creator)
  4457. insertData = append(insertData, info.UpdateCreator)
  4458. insertData = append(insertData, info.Status)
  4459. insertData = append(insertData, info.Ctime)
  4460. insertData = append(insertData, info.Mtime)
  4461. insertData = append(insertData, info.Price)
  4462. insertData = append(insertData, info.WarehousingDetailId)
  4463. insertData = append(insertData, info.WarehouseOutDetailId)
  4464. insertData = append(insertData, info.CancelOutDetailId)
  4465. insertData = append(insertData, info.ExpireDate)
  4466. insertData = append(insertData, info.ProductDate)
  4467. }
  4468. thisSQL += strings.Join(insertParams, ", ")
  4469. err = utx.Exec(thisSQL, insertData...).Error
  4470. if err != nil {
  4471. utx.Rollback()
  4472. return
  4473. }
  4474. }
  4475. utx.Commit()
  4476. }
  4477. return
  4478. }
  4479. func CreateStockFlowOne(flow models.VmStockFlow) error {
  4480. utx := XTWriteDB().Begin()
  4481. err := utx.Create(&flow).Error
  4482. if err != nil {
  4483. utx.Rollback()
  4484. return err
  4485. }
  4486. utx.Commit()
  4487. return err
  4488. }
  4489. func CreateNewStockFlowOne(flow models.VmStockFlow, tx *gorm.DB) error {
  4490. err := tx.Create(&flow).Error
  4491. if err != nil {
  4492. tx.Rollback()
  4493. return err
  4494. }
  4495. return err
  4496. }
  4497. func CreateStockFlowSix(flow *models.VmStockFlow) error {
  4498. err := XTWriteDB().Create(&flow).Error
  4499. return err
  4500. }
  4501. func UpdatedStockFlow(flow models.VmStockFlow) error {
  4502. err := XTWriteDB().Save(&flow).Error
  4503. return err
  4504. }
  4505. func UpdatedStockFlowOne(flow models.VmStockFlow, warehousing_id int64, patient_id int64, record_time int64, good_id int64) error {
  4506. 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
  4507. return err
  4508. }
  4509. func UpdatedStockFlowFive(flow models.VmStockFlow, warehouse_out_id int64, patient_id int64, record_time int64, good_id int64) error {
  4510. 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
  4511. return err
  4512. }
  4513. func GetStockFlowIsExsit(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (*models.VmStockFlow, error) {
  4514. stock := models.VmStockFlow{}
  4515. var err error
  4516. 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
  4517. if err == gorm.ErrRecordNotFound {
  4518. return nil, err
  4519. }
  4520. if err != nil {
  4521. return nil, err
  4522. }
  4523. return &stock, nil
  4524. }
  4525. func GetStockFlowIsBatchNumber(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  4526. 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
  4527. return flow, nil
  4528. }
  4529. func GetStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  4530. 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
  4531. return flow, nil
  4532. }
  4533. func GetNewStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  4534. 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
  4535. if err != gorm.ErrRecordNotFound {
  4536. if err != nil {
  4537. tx.Rollback()
  4538. return flow, nil
  4539. }
  4540. }
  4541. return flow, nil
  4542. }
  4543. func GetStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.WarehouseOutInfo, err error) {
  4544. 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
  4545. return flow, nil
  4546. }
  4547. func GetNewStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.WarehouseOutInfo, err error) {
  4548. 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
  4549. if err != gorm.ErrRecordNotFound {
  4550. if err != nil {
  4551. tx.Rollback()
  4552. return flow, nil
  4553. }
  4554. }
  4555. return flow, nil
  4556. }
  4557. func GetStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.AutomaticReduceDetail, err error) {
  4558. 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
  4559. return flow, nil
  4560. }
  4561. func GetNewStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.AutomaticReduceDetail, err error) {
  4562. 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
  4563. if err != gorm.ErrRecordNotFound {
  4564. if err != nil {
  4565. tx.Rollback()
  4566. return flow, err
  4567. }
  4568. }
  4569. return flow, err
  4570. }
  4571. func GetStockFlowIsBatchNumberTwo(patient_id int64, record_time int64, good_id int64) (flow []*models.WarehouseOutInfo, err error) {
  4572. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 ", patient_id, record_time, good_id).Find(&flow).Error
  4573. return flow, nil
  4574. }
  4575. func GetStockFlowIsBatchNumberThree(patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  4576. 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
  4577. return flow, nil
  4578. }
  4579. func GetStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  4580. 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
  4581. return flow, nil
  4582. }
  4583. func GetNewStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  4584. 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
  4585. if err != gorm.ErrRecordNotFound {
  4586. if err != nil {
  4587. tx.Rollback()
  4588. return flow, nil
  4589. }
  4590. }
  4591. return flow, nil
  4592. }
  4593. func GetStockFlowIsBatchNumberFourty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  4594. 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
  4595. return flow, nil
  4596. }
  4597. func GetWarehouseOutInfoIsExist(id int64) (*models.WarehouseOutInfo, error) {
  4598. info := models.WarehouseOutInfo{}
  4599. var err error
  4600. err = XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  4601. if err == gorm.ErrRecordNotFound {
  4602. return nil, err
  4603. }
  4604. if err != nil {
  4605. return nil, err
  4606. }
  4607. return &info, nil
  4608. }
  4609. func GetWarehouseOutInfoIsExistOne(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  4610. info := models.WarehouseOutInfo{}
  4611. var err error
  4612. 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
  4613. if err == gorm.ErrRecordNotFound {
  4614. return nil, err
  4615. }
  4616. if err != nil {
  4617. return nil, err
  4618. }
  4619. return &info, nil
  4620. }
  4621. func GetWarehouseOutInfoIsExistTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  4622. info := models.WarehouseOutInfo{}
  4623. 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
  4624. return info, err
  4625. }
  4626. 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) {
  4627. offset := (page - 1) * limit
  4628. db := XTReadDB().Model(&flow).Where("status = 1 and count > 0")
  4629. if consumable_type > 0 {
  4630. //入库
  4631. if consumable_type == 1 {
  4632. db = db.Where("consumable_type = ?", consumable_type)
  4633. }
  4634. //2 手动出库
  4635. if consumable_type == 2 {
  4636. db = db.Where(" consumable_type = ? and is_sys = 0", consumable_type)
  4637. }
  4638. //3.自动出库
  4639. if consumable_type == 3 {
  4640. db = db.Where(" consumable_type = ? and is_sys = 1", consumable_type)
  4641. }
  4642. //4.手动退库
  4643. if consumable_type == 4 {
  4644. db = db.Where(" consumable_type = ?", consumable_type)
  4645. }
  4646. //自动退库
  4647. if consumable_type == 7 {
  4648. db = db.Where(" consumable_type = ?", consumable_type)
  4649. }
  4650. if consumable_type == 10 {
  4651. db = db.Where(" consumable_type = ?", consumable_type)
  4652. }
  4653. if consumable_type == 11 {
  4654. db = db.Where(" consumable_type = ?", consumable_type)
  4655. }
  4656. if consumable_type == 12 {
  4657. db = db.Where(" consumable_type = ?", consumable_type)
  4658. }
  4659. if consumable_type == 13 {
  4660. db = db.Where(" consumable_type = ?", consumable_type)
  4661. }
  4662. if consumable_type == 15 {
  4663. db = db.Where(" consumable_type = ?", consumable_type)
  4664. }
  4665. }
  4666. if orgId > 0 {
  4667. db = db.Where("user_org_id = ?", orgId)
  4668. }
  4669. if startTime > 0 {
  4670. db = db.Where("ctime >= ?", startTime)
  4671. }
  4672. if endTime > 0 {
  4673. db = db.Where("ctime<=?", endTime)
  4674. }
  4675. if good_id > 0 {
  4676. db = db.Where("good_id = ?", good_id)
  4677. }
  4678. err = db.Count(&total).Offset(offset).Limit(limit).Order("id desc").Find(&flow).Error
  4679. return flow, total, err
  4680. }
  4681. func GetLastGoodListByPatientId(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.AutomaticReduceDetail, error) {
  4682. detail := models.AutomaticReduceDetail{}
  4683. 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
  4684. return detail, err
  4685. }
  4686. func GetLastGoodListByPatientIdOne(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.WarehouseOutInfo, error) {
  4687. detail := models.WarehouseOutInfo{}
  4688. 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
  4689. return detail, err
  4690. }
  4691. func UpdatedWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64) error {
  4692. outInfo := models.WarehouseOutInfo{}
  4693. 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
  4694. return err
  4695. }
  4696. func UpdatedNewWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) error {
  4697. outInfo := models.WarehouseOutInfo{}
  4698. 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
  4699. if err != nil {
  4700. tx.Rollback()
  4701. return err
  4702. }
  4703. return err
  4704. }
  4705. func UpdatedWarehouseOutInfoSix(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) error {
  4706. outInfo := models.WarehouseOutInfo{}
  4707. 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
  4708. return err
  4709. }
  4710. 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 {
  4711. outInfo := models.WarehouseOutInfo{}
  4712. 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
  4713. if err != nil {
  4714. tx.Rollback()
  4715. return err
  4716. }
  4717. return err
  4718. }
  4719. func GetLastWarehouOutInfoByPatientId(orgid int64, patient_id int64, record_time int64, goodid int64, good_type_id int64) (models.WarehouseOutInfo, error) {
  4720. info := models.WarehouseOutInfo{}
  4721. 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
  4722. return info, err
  4723. }
  4724. func GetLastStockOut(id int64) (models.WarehousingInfo, error) {
  4725. info := models.WarehousingInfo{}
  4726. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  4727. return info, err
  4728. }
  4729. func GetStockGoodCount(good_id int64) (models.WarehousingInfo, error) {
  4730. info := models.WarehousingInfo{}
  4731. db := XTReadDB().Model(&info)
  4732. if good_id > 0 {
  4733. db = db.Where("good_id = ? and status = 1", good_id)
  4734. }
  4735. err := db.Select("sum(stock_count) as stock_count").Find(&info).Error
  4736. return info, err
  4737. }
  4738. 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) {
  4739. offset := (page - 1) * limit
  4740. db := XTReadDB().Model(drug_flow).Where("status = 1")
  4741. if drugid > 0 {
  4742. db = db.Where("drug_id = ?", drugid)
  4743. }
  4744. if orgid > 0 {
  4745. db = db.Where("user_org_id = ?", orgid)
  4746. }
  4747. if startTime > 0 {
  4748. db = db.Where("ctime >=? ", startTime)
  4749. }
  4750. if endTime > 0 {
  4751. db = db.Where("ctime<=?", endTime)
  4752. }
  4753. if stock_type > 0 {
  4754. //手动入库
  4755. if stock_type == 1 {
  4756. db = db.Where("consumable_type = 1")
  4757. }
  4758. //手动出库
  4759. if stock_type == 2 {
  4760. db = db.Where("consumable_type = 2 and is_sys = 0")
  4761. }
  4762. //自动出库
  4763. if stock_type == 3 {
  4764. db = db.Where("consumable_type = 3 and is_sys = 1")
  4765. }
  4766. if stock_type == 4 {
  4767. db = db.Where("consumable_type = 4")
  4768. }
  4769. if stock_type == 10 {
  4770. db = db.Where("consumable_type = 10")
  4771. }
  4772. if stock_type == 11 {
  4773. db = db.Where("consumable_type = 11")
  4774. }
  4775. if stock_type == 12 {
  4776. db = db.Where("consumable_type = 12")
  4777. }
  4778. if stock_type == 13 {
  4779. db = db.Where("consumable_type = 13")
  4780. }
  4781. if stock_type == 15 {
  4782. db = db.Where("consumable_type = 15")
  4783. }
  4784. }
  4785. 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
  4786. return drug_flow, total, err
  4787. }
  4788. func GetDrugWarehouseOutInfoIsExit(drug_id int64, patient_id int64, recordtime int64) (*models.DrugWarehouseOutInfo, error) {
  4789. outInfo := models.DrugWarehouseOutInfo{}
  4790. var err error
  4791. 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
  4792. if err == gorm.ErrRecordNotFound {
  4793. return nil, err
  4794. }
  4795. if err != nil {
  4796. return nil, err
  4797. }
  4798. return &outInfo, nil
  4799. }
  4800. func GetDrugWarehouseOutInfoIsExitOne(drug_id int64, patient_id int64, recordtime int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  4801. 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
  4802. return outInfo, err
  4803. }
  4804. func UpdatedDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, drug_id int64, patient_id int64, record_date int64, orgid int64) error {
  4805. outInfo := models.DrugWarehouseOutInfo{}
  4806. 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
  4807. return err
  4808. }
  4809. func GetCancelExportList(startime int64, endtime int64, ids []string, orgid int64) (cancle []*models.CancelStockInfo, err error) {
  4810. db := XTReadDB().Model(&cancle).Where("status = 1")
  4811. if startime > 0 {
  4812. db = db.Where("ctime >=?", startime)
  4813. }
  4814. if endtime > 0 {
  4815. db = db.Where("ctime <=?", endtime)
  4816. }
  4817. if len(ids) > 0 {
  4818. db = db.Where("cancel_stock_id in(?)", ids)
  4819. }
  4820. if orgid > 0 {
  4821. db = db.Where("org_id = ?", orgid)
  4822. }
  4823. db = db.Preload("GoodInfo", "status =1 and org_id = ?", orgid)
  4824. err = db.Find(&cancle).Error
  4825. return cancle, err
  4826. }
  4827. func GetCancelStockDetail(id int64) (info []*models.DrugCancelStockInfo, err error) {
  4828. db := XTReadDB().Model(&info)
  4829. if id > 0 {
  4830. db = db.Where("cancel_stock_id = ? and status = 1", id)
  4831. }
  4832. db = db.Preload("BaseDrugLib")
  4833. err = db.Find(&info).Error
  4834. return info, err
  4835. }
  4836. func GetDrugWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  4837. info := models.XtDrugWarehouseInfo{}
  4838. db := XTReadDB().Model(&info).Where("status = 1")
  4839. if id > 0 {
  4840. db = db.Where("id = ?", id)
  4841. }
  4842. err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  4843. return info, err
  4844. }
  4845. //func GetDrugWarehouseInfoTX(id int64,tx *gorm.DB) (models.XtDrugWarehouseInfo, error) {
  4846. // info := models.XtDrugWarehouseInfo{}
  4847. // db := tx.Model(&info).Where("status = 1")
  4848. // if id > 0 {
  4849. // db = db.Where("id = ?", id)
  4850. // }
  4851. // err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  4852. // return info, err
  4853. //}
  4854. func GetDrugWarehouseInfoPrescription(drugid int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  4855. err = XTReadDB().Where("drug_id = ? and org_id =? and status = 1", drugid, orgid).Find(&info).Error
  4856. return info, err
  4857. }
  4858. func GetDrugWarehouseInfoPrescriptionSeven(drugid int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfoSeven, err error) {
  4859. 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
  4860. return info, err
  4861. }
  4862. func GetHisAdviceListByDrugId(drugid int64, patient_id int64, advice_date int64) (models.HisDoctorAdviceInfo, error) {
  4863. info := models.HisDoctorAdviceInfo{}
  4864. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1", drugid, patient_id, advice_date).Find(&info).Error
  4865. return info, err
  4866. }
  4867. func GetHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64) (info []*models.HisDoctorAdviceInfo, err error) {
  4868. err = XTReadDB().Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  4869. return info, err
  4870. }
  4871. func GetNewHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64, tx *gorm.DB) (info []*models.HisDoctorAdviceInfo, err error) {
  4872. err = tx.Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  4873. if err != gorm.ErrRecordNotFound {
  4874. if err != nil {
  4875. tx.Rollback()
  4876. return info, err
  4877. }
  4878. }
  4879. return info, err
  4880. }
  4881. func GetLastHisAdvicesList(patient_id int64, advice_date int64, user_org_id int64, tx *gorm.DB) (models.HisDoctorAdviceInfo, error) {
  4882. adviceInfo := models.HisDoctorAdviceInfo{}
  4883. 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
  4884. if err != gorm.ErrRecordNotFound {
  4885. if err != nil {
  4886. tx.Rollback()
  4887. return adviceInfo, err
  4888. }
  4889. }
  4890. return adviceInfo, err
  4891. }
  4892. func GetHisAdviceListByDrugIdEight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  4893. info := models.HisDoctorAdviceInfo{}
  4894. 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
  4895. return info, err
  4896. }
  4897. func GetLastDrugWarehouseOutByDrugId(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.DrugAutomaticReduceDetail, error) {
  4898. detail := models.DrugAutomaticReduceDetail{}
  4899. 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
  4900. return detail, err
  4901. }
  4902. func GetLastDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64) (out []*models.DrugWarehouseOutInfo, err error) {
  4903. 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
  4904. return out, err
  4905. }
  4906. func GetLastNewDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64, tx *gorm.DB) (out []*models.DrugWarehouseOutInfo, err error) {
  4907. 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
  4908. if err != gorm.ErrRecordNotFound {
  4909. if err != nil {
  4910. tx.Rollback()
  4911. return out, err
  4912. }
  4913. }
  4914. return out, err
  4915. }
  4916. func ModefyDrugByWarehouseInfo(id int64, stock_max_number int64) error {
  4917. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  4918. return err
  4919. }
  4920. func ModefyDrugByWarehouseInfoOne(id int64, stock_min_number int64) error {
  4921. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  4922. return err
  4923. }
  4924. func DeleteDrugAutoWarehouse(drugid int64, patient_id int64, record_time int64) error {
  4925. 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
  4926. 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
  4927. 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
  4928. return err
  4929. }
  4930. func DeleteDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  4931. 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
  4932. 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
  4933. return err
  4934. }
  4935. func DeleteNewDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  4936. 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
  4937. 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
  4938. return err
  4939. }
  4940. func GetDrugWarehouseInfoById(id int64) (models.XtDrugWarehouseInfo, error) {
  4941. info := models.XtDrugWarehouseInfo{}
  4942. err := XTReadDB().Model(&info).Where("id=? and status =1", id).Find(&info).Error
  4943. return info, err
  4944. }
  4945. func GetDrugWarehouseInfoByIdTwenty(id int64) (models.XtDrugWarehouseInfo, error) {
  4946. info := models.XtDrugWarehouseInfo{}
  4947. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  4948. return info, err
  4949. }
  4950. func UpdateDrugWarehouseInfo(info *models.XtDrugWarehouseInfo, id int64) error {
  4951. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  4952. return err
  4953. }
  4954. //func UpdateDrugWarehouseInfoTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  4955. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  4956. // return err
  4957. //}
  4958. func UpdateDrugWarehouseInfoOne(info *models.XtDrugWarehouseInfo, id int64) error {
  4959. //err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_min_number": info.StockMaxNumber}).Error
  4960. //return err
  4961. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  4962. return err
  4963. }
  4964. //func UpdateDrugWarehouseInfoOneTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  4965. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  4966. // return err
  4967. //}
  4968. func GetLastCancelStockInfo(id int64) (models.DrugCancelStockInfo, error) {
  4969. info := models.DrugCancelStockInfo{}
  4970. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  4971. return info, err
  4972. }
  4973. func GetLastCancelStockInfoOne(id int64, drug_id int64) (models.DrugCancelStockInfo, error) {
  4974. info := models.DrugCancelStockInfo{}
  4975. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ? and status = 1", id, drug_id).Find(&info).Error
  4976. return info, err
  4977. }
  4978. func UpdatedDrugInfo(id int64, count int64) (err error) {
  4979. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  4980. return err
  4981. }
  4982. func UpdatedDrugFlow(cancelstockid int64, drug_id int64, flow models.DrugFlow) error {
  4983. 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
  4984. return err
  4985. }
  4986. func UpdatedDrugInfoOne(id int64, count int64) (err error) {
  4987. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  4988. return err
  4989. }
  4990. func UpdateWarehouseInfo(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  4991. 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
  4992. return err
  4993. }
  4994. func UpdateWarehouseInfoTwo(count int64, drugid int64, orgid int64) error {
  4995. 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
  4996. return err
  4997. }
  4998. func UpdateWarehouseInfoOne(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  4999. 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
  5000. return err
  5001. }
  5002. func UpdateWarehouseInfoFour(count int64, drugid int64, orgid int64) error {
  5003. 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
  5004. return err
  5005. }
  5006. func GetWarehouseInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  5007. info := models.WarehousingInfo{}
  5008. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5009. return info, err
  5010. }
  5011. func GetLastWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5012. info := models.WarehousingInfo{}
  5013. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  5014. return info, err
  5015. }
  5016. func GetFirstWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5017. info := models.WarehousingInfo{}
  5018. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).First(&info).Error
  5019. return info, err
  5020. }
  5021. func CreateWarehouseOut(out models.WarehouseOut) error {
  5022. err := XTWriteDB().Create(&out).Error
  5023. return err
  5024. }
  5025. func GetLastGoodWarehouseOut(orgid int64) (models.WarehouseOut, error) {
  5026. out := models.WarehouseOut{}
  5027. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  5028. return out, err
  5029. }
  5030. func CreateWarehouseInfo(info models.WarehousingInfo) error {
  5031. err := XTWriteDB().Create(&info).Error
  5032. return err
  5033. }
  5034. func UpdateGoodWarehouseInfo(id int64, info models.WarehousingInfo) error {
  5035. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5036. return err
  5037. }
  5038. func UpdateWareInfoById(id int64, count int64) error {
  5039. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  5040. return err
  5041. }
  5042. func UpdateWareInfoByIdSix(id int64, count int64) error {
  5043. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  5044. return err
  5045. }
  5046. func GetLastCancelInfo(id int64, goodid int64) (models.CancelStockInfo, error) {
  5047. info := models.CancelStockInfo{}
  5048. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5049. return info, err
  5050. }
  5051. func GetLastCancelInfoSix(id int64, goodid int64, storehouse_id int64) (models.CancelStockInfo, error) {
  5052. info := models.CancelStockInfo{}
  5053. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1 and storehouse_id = ?", id, goodid, storehouse_id).Find(&info).Error
  5054. return info, err
  5055. }
  5056. func GetAllCancelInfoById(id int64, goodid int64) (info []*models.CancelStockInfo, err error) {
  5057. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5058. return info, err
  5059. }
  5060. func GetAllCancelInfoByIdSix(id int64, goodid int64, storehouse_id int64) (info []*models.CancelStockInfo, err error) {
  5061. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5062. return info, err
  5063. }
  5064. func UpdateStockFlow(flow models.VmStockFlow, goodid int64, warehousing_id int64) error {
  5065. err := XTWriteDB().Model(&flow).Where("good_id = ? and warehousing_id= ? and status = 1", goodid, warehousing_id).Updates(map[string]interface{}{"count": flow.Count}).Error
  5066. return err
  5067. }
  5068. func UpdateDrugFlow(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5069. drugFlow := models.DrugFlow{}
  5070. 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
  5071. return err
  5072. }
  5073. func UpdateDrugFlowNight(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5074. drugFlow := models.DrugFlow{}
  5075. 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
  5076. return err
  5077. }
  5078. func UpdateDrugCancel(id int64) (models.DrugCancelStock, error) {
  5079. stock := models.DrugCancelStock{}
  5080. err := XTWriteDB().Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5081. return stock, err
  5082. }
  5083. //func UpdateDrugCancelTX(id int64,tx *gorm.DB) (models.DrugCancelStock, error) {
  5084. // stock := models.DrugCancelStock{}
  5085. // err := tx.Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5086. // return stock, err
  5087. //}
  5088. func GetDrugWarehouseOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugWarehouseOutInfo, err error) {
  5089. db := XTReadDB().Model(&info)
  5090. if warehouse_info_id > 0 {
  5091. db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5092. }
  5093. if drug_id > 0 {
  5094. db = db.Where("drug_id = ? and status =1", drug_id)
  5095. }
  5096. err = db.Find(&info).Error
  5097. return info, err
  5098. }
  5099. //func GetDrugWarehouseOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  5100. // db := tx.Model(&info)
  5101. // if warehouse_info_id > 0 {
  5102. // db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5103. // }
  5104. // if drug_id > 0 {
  5105. // db = db.Where("drug_id = ? and status =1", drug_id)
  5106. // }
  5107. // err = db.Find(&info).Error
  5108. // return info, err
  5109. //}
  5110. func GetCancelDrugStockOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugCancelStockInfo, err error) {
  5111. err = XTReadDB().Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5112. return info, err
  5113. }
  5114. //func GetCancelDrugStockOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugCancelStockInfo, err error) {
  5115. // err = tx.Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5116. // return info, err
  5117. //}
  5118. func GetTemplateSummary(orgid int64) (config []*models.Dataconfig, err error) {
  5119. err = XTReadDB().Model(&config).Where("module='template_summary' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5120. return config, err
  5121. }
  5122. func GetTemplatePlan(orgid int64) (config []*models.Dataconfig, err error) {
  5123. err = XTReadDB().Model(&config).Where("module='template_plan' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5124. return config, err
  5125. }
  5126. func GetSearchGoodWarehouseList(keyword string, orgid int64, storehouse_id int64) (info []*models.BloodWarehouseInfo, err error) {
  5127. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  5128. table := XTReadDB().Table("xt_goood_information as t").Where("t.status")
  5129. fmt.Println(table)
  5130. likeKey := "%" + keyword + "%"
  5131. if orgid > 0 {
  5132. db = db.Where("x.org_id = ?", orgid)
  5133. }
  5134. if storehouse_id > 0 {
  5135. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5136. }
  5137. 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
  5138. return info, err
  5139. }
  5140. func CreateAdjustPrice(adjustPrice *models.XtStockAdjustPrice) error {
  5141. err := XTWriteDB().Create(&adjustPrice).Error
  5142. return err
  5143. }
  5144. func GetAllStockPrice(orgid int64, startime int64, endtime int64, keyword string, limit int64, page int64) (list []*models.VmStockAdjustPrice, total int64, err error) {
  5145. likeKey := "%" + keyword + "%"
  5146. offset := (page - 1) * limit
  5147. db := XTReadDB().Table("xt_stock_adjust_price as x").Where("x.status = 1")
  5148. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5149. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5150. fmt.Println(table, tab)
  5151. if startime > 0 {
  5152. db = db.Where("x.start_time>=?", startime)
  5153. }
  5154. if endtime > 0 {
  5155. db = db.Where("x.start_time<=?", endtime)
  5156. }
  5157. if len(keyword) > 0 {
  5158. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  5159. }
  5160. if orgid > 0 {
  5161. db = db.Where("x.user_org_id = ?", orgid)
  5162. }
  5163. 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
  5164. return list, total, err
  5165. }
  5166. func UpdateAdjustPrice(ids []string, info models.XtStockAdjustPrice) error {
  5167. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5168. return err
  5169. }
  5170. func UpdateAdjustCheckPrice(info *models.GoodInfo, id int64) error {
  5171. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"packing_price": info.PackingPrice, "buy_price": info.BuyPrice}).Error
  5172. return err
  5173. }
  5174. func GetAdjustCheckPriceList(ids []string) (info []*models.XtStockAdjustPrice, err error) {
  5175. err = XTReadDB().Where("id in(?)", ids).Find(&info).Error
  5176. return info, err
  5177. }
  5178. func GetAdjustPricebyIdOne(ids []string) (adjust []*models.XtStockAdjustPrice, err error) {
  5179. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5180. return adjust, err
  5181. }
  5182. func CreateReportPrice(price *models.XtStockReportPrice) error {
  5183. err := XTWriteDB().Create(&price).Error
  5184. return err
  5185. }
  5186. func GetReportStockList(startime int64, endtime int64, orgid int64, keyword string, page int64, limit int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5187. likeKey := "%" + keyword + "%"
  5188. offset := (page - 1) * limit
  5189. db := XTReadDB().Table("xt_stock_report_price as x").Where("x.status = 1")
  5190. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5191. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5192. fmt.Println(table, tab)
  5193. if startime > 0 {
  5194. db = db.Where("x.ctime>=?", startime)
  5195. }
  5196. if endtime > 0 {
  5197. db = db.Where("x.ctime<=?", endtime)
  5198. }
  5199. if len(keyword) > 0 {
  5200. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5201. }
  5202. if orgid > 0 {
  5203. db = db.Where("x.user_org_id = ?", orgid)
  5204. }
  5205. 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
  5206. return adjust, total, err
  5207. }
  5208. func GetStockDamagedList(orgid int64, keyword string, page int64, limit int64, startime int64, endtime int64, storehouse_id int64) (adjust []*models.VmStockInventory, total int64, err error) {
  5209. likeKey := "%" + keyword + "%"
  5210. offset := (page - 1) * limit
  5211. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.inventory_type = 4")
  5212. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5213. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5214. fmt.Println(table, tab)
  5215. if len(keyword) > 0 {
  5216. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5217. }
  5218. if orgid > 0 {
  5219. db = db.Where("x.user_org_id = ?", orgid)
  5220. }
  5221. if startime > 0 {
  5222. db = db.Where("x.ctime >=?", startime)
  5223. }
  5224. if endtime > 0 {
  5225. db = db.Where("x.ctime <=?", endtime)
  5226. }
  5227. if storehouse_id > 0 {
  5228. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5229. }
  5230. err = db.Group("x.good_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
  5231. return adjust, total, err
  5232. }
  5233. func GetStockDamagedCountOne(orgid int64, keyword string, inventory_type int64) (adjust []*models.VmStockInventory, err error) {
  5234. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5235. if inventory_type == 0 {
  5236. db = db.Where("x.inventory_type = 4")
  5237. }
  5238. if inventory_type > 0 {
  5239. db = db.Where("x.inventory_type = ?", inventory_type)
  5240. }
  5241. if len(keyword) > 0 {
  5242. likeKey := "%" + keyword + "%"
  5243. db = db.Where("x.warehousing_order like ?", likeKey)
  5244. }
  5245. if orgid > 0 {
  5246. db = db.Where("x.user_org_id = ?", orgid)
  5247. }
  5248. 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
  5249. return adjust, err
  5250. }
  5251. func GetStockDamagedCount(orgid int64) (adjust []*models.VmStockInventory, err error) {
  5252. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and inventory_type = 4 ")
  5253. if orgid > 0 {
  5254. db = db.Where("x.user_org_id = ?", orgid)
  5255. }
  5256. 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
  5257. return adjust, err
  5258. }
  5259. func UpdateCheckDamage(ids []string, reportPrice models.XtStockReportPrice) error {
  5260. 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
  5261. return err
  5262. }
  5263. func GetCheckDamageList(ids []string) (report []*models.XtStockReportPrice, err error) {
  5264. err = XTReadDB().Model(&report).Where("id in(?) and status= 1", ids).Find(&report).Error
  5265. return report, err
  5266. }
  5267. func CreateInentory(inventory models.XtStockInventory) error {
  5268. err = XTWriteDB().Create(&inventory).Error
  5269. return err
  5270. }
  5271. func GetInventorylist(startime int64, endtime int64, page int64, limit int64, inventory_status int64, keyword string, orgid int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5272. likeKey := "%" + keyword + "%"
  5273. offset := (page - 1) * limit
  5274. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5275. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5276. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5277. fmt.Println(table, tab)
  5278. if startime > 0 {
  5279. db = db.Where("x.ctime>=?", startime)
  5280. }
  5281. if endtime > 0 {
  5282. db = db.Where("x.ctime<=?", endtime)
  5283. }
  5284. if len(keyword) > 0 {
  5285. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5286. }
  5287. if orgid > 0 {
  5288. db = db.Where("x.user_org_id = ?", orgid)
  5289. }
  5290. if inventory_status > 0 {
  5291. db = db.Where("checker_status = ?", inventory_status)
  5292. }
  5293. 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
  5294. return adjust, total, err
  5295. }
  5296. func UpdateCheckInventory(ids []string, info models.XtStockInventory) error {
  5297. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5298. return err
  5299. }
  5300. func GetModifyPriceDetail(id int64) (models.XtStockAdjustPrice, error) {
  5301. info := models.XtStockAdjustPrice{}
  5302. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5303. return info, err
  5304. }
  5305. func GetInventoryDetailById(id []string) (list []*models.XtStockInventory, err error) {
  5306. err = XTReadDB().Model(&list).Where("id in(?) and status = 1", id).Find(&list).Error
  5307. return list, err
  5308. }
  5309. func GetWarehouseInfoByIdNight(id int64) (models.WarehousingInfo, error) {
  5310. info := models.WarehousingInfo{}
  5311. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5312. return info, err
  5313. }
  5314. func GetWarehouseInfoById(id int64) (models.WarehousingInfo, error) {
  5315. info := models.WarehousingInfo{}
  5316. //err :=XTReadDB().Model(&info).Where("id = ? or warehouse_info_id and status = 1", id).Find(&info).Error
  5317. //return info, err
  5318. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5319. if id > 0 {
  5320. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5321. }
  5322. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5323. return info, err
  5324. }
  5325. func GetWarehouseInfoByIdTen(id int64, storehouse_id int64) (models.WarehousingInfo, error) {
  5326. info := models.WarehousingInfo{}
  5327. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5328. if id > 0 {
  5329. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5330. }
  5331. if storehouse_id > 0 {
  5332. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5333. }
  5334. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5335. return info, err
  5336. }
  5337. func UpdateStockPrice(info models.XtStockAdjustPrice, id int64) error {
  5338. 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
  5339. return err
  5340. }
  5341. func DeleteStockPrice(id int64) error {
  5342. info := models.XtStockAdjustPrice{}
  5343. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5344. return err
  5345. }
  5346. func GetStockDamageDetail(id int64) (models.XtStockReportPrice, error) {
  5347. report := models.XtStockReportPrice{}
  5348. err := XTReadDB().Model(&report).Where("id=? and status = 1", id).Find(&report).Error
  5349. return report, err
  5350. }
  5351. func ModifyPrice(id int64, info models.XtStockReportPrice) error {
  5352. 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
  5353. return err
  5354. }
  5355. func DeleteDamage(id int64) error {
  5356. adjust := models.XtStockReportPrice{}
  5357. err := XTWriteDB().Model(&adjust).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5358. return err
  5359. }
  5360. func GetStockDamageById(ids []string) (adjust []*models.XtStockReportPrice, err error) {
  5361. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5362. return adjust, err
  5363. }
  5364. func GetInventoryDetail(id int64) (models.XtStockInventory, error) {
  5365. inventory := models.XtStockInventory{}
  5366. err := XTReadDB().Model(&inventory).Where("id=? and status = 1", id).Find(&inventory).Error
  5367. return inventory, err
  5368. }
  5369. func ModifyInventory(id int64, inventory models.XtStockInventory) error {
  5370. 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
  5371. return err
  5372. }
  5373. func DeleteInventory(id int64) error {
  5374. inventory := models.XtStockInventory{}
  5375. err := XTWriteDB().Model(&inventory).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5376. return err
  5377. }
  5378. func GetInventoryDetailList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64) (list []*models.VmStockInventory, total int64, err error) {
  5379. likeKey := "%" + keyword + "%"
  5380. offset := (page - 1) * limit
  5381. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5382. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5383. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5384. fmt.Println(table, tab)
  5385. if len(keyword) > 0 {
  5386. db = db.Where("x.good_name like ?", likeKey)
  5387. }
  5388. if orgid > 0 {
  5389. db = db.Where("x.user_org_id = ?", orgid)
  5390. }
  5391. if storehouse_id > 0 {
  5392. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5393. }
  5394. 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
  5395. return list, total, err
  5396. }
  5397. func GetInventoryDetailPrintMapList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64, ids []string) (list []*models.VmStockInventory, total int64, err error) {
  5398. likeKey := "%" + keyword + "%"
  5399. offset := (page - 1) * limit
  5400. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5401. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5402. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5403. fmt.Println(table, tab)
  5404. if len(keyword) > 0 {
  5405. db = db.Where("x.good_name like ?", likeKey)
  5406. }
  5407. if orgid > 0 {
  5408. db = db.Where("x.user_org_id = ?", orgid)
  5409. }
  5410. if storehouse_id > 0 {
  5411. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5412. }
  5413. if len(ids) > 0 {
  5414. db = db.Where("x.id in(?)", ids)
  5415. }
  5416. 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
  5417. return list, total, err
  5418. }
  5419. func GetInventoryPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5420. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5421. return list, err
  5422. }
  5423. func GetInventoryDetailPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5424. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5425. return list, err
  5426. }
  5427. func DeleteDrugPrice(id int64) error {
  5428. info := models.XtDrugAdjustPrice{}
  5429. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5430. return err
  5431. }
  5432. func GetGoodWarehouseList(goodid int64) (models.WarehousingInfo, error) {
  5433. info := models.WarehousingInfo{}
  5434. db := XTReadDB().Model(&info).Where("status = 1")
  5435. if goodid > 0 {
  5436. db = db.Where("good_id = ?", goodid)
  5437. }
  5438. err := db.Select("good_id,sum(stock_count) as stock_count").Find(&info).Error
  5439. return info, err
  5440. }
  5441. func UpdateProofInventory(id int64, info models.WarehousingInfo) error {
  5442. err := XTWriteDB().Model(&info).Where("id=? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5443. return err
  5444. }
  5445. func UpdateStockInventory(inventory models.XtStockInventory, id int64) error {
  5446. err := XTWriteDB().Model(&inventory).Where("id= ? and status = 1", id).Updates(map[string]interface{}{"total": inventory.Total}).Error
  5447. return err
  5448. }
  5449. func CeateStockCorrectRecord(record models.XtStockCorrectRecord) error {
  5450. err := XTWriteDB().Create(&record).Error
  5451. return err
  5452. }
  5453. func GetWarehouseTotal(id int64) (models.WarehousingInfo, error) {
  5454. info := models.WarehousingInfo{}
  5455. //err :=XTReadDB()()()()().Where("id=? and status = 1", id).Find(&info).Error
  5456. //return info, err
  5457. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5458. if id > 0 {
  5459. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5460. }
  5461. err := db.Select("sum(x.stock_count) as stock_count").Scan(&info).Error
  5462. return info, err
  5463. }
  5464. func GetCurrentWarehosueInfo(id int64) (info []*models.DrugWarehouseInfo, err error) {
  5465. err = XTReadDB().Model(&info).Where("id=? or warehousing_info_id = ? ", id, id).Find(&info).Error
  5466. return info, err
  5467. }
  5468. func GetCurrentWarehosueInfoOne(id int64) (models.DrugWarehouseInfo, error) {
  5469. info := models.DrugWarehouseInfo{}
  5470. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  5471. return info, err
  5472. }
  5473. func ModifyDrugWarehouseInfo(info *models.DrugWarehouseInfo, id int64) error {
  5474. 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
  5475. return err
  5476. }
  5477. func ModifyDrugWarehouseInfoSix(info *models.DrugWarehouseInfo, id int64) error {
  5478. 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
  5479. return err
  5480. }
  5481. func GetGoodInventoryWarehouseList(id int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  5482. 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
  5483. return info, err
  5484. }
  5485. func UpdatedWarehousingInfo(info models.WarehousingInfo, id int64) error {
  5486. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5487. return err
  5488. }
  5489. func UpdatedWarehousingInfoSix(info models.WarehousingInfo, id int64) error {
  5490. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount, "warehousing_count": info.WarehousingCount}).Error
  5491. return err
  5492. }
  5493. func GetDamageDetailByGoodId(good_id int64, keyword string, inventory_type int64) (list []*models.XtStockInventory, err error) {
  5494. db := XTReadDB().Model(&list).Where(" status = 1")
  5495. if good_id > 0 {
  5496. db = db.Where("good_id = ?", good_id)
  5497. }
  5498. if len(keyword) > 0 {
  5499. likeKey := "%" + keyword + "%"
  5500. db = db.Where("warehousing_order like ?", likeKey)
  5501. }
  5502. if inventory_type > 0 {
  5503. db = db.Where("inventory_type = ?", inventory_type)
  5504. }
  5505. if inventory_type == 0 {
  5506. db = db.Where("inventory_type = 4")
  5507. }
  5508. err = db.Find(&list).Error
  5509. return list, err
  5510. }
  5511. func GetCancelStockId(cancelstock_id int64, drug_id int64) (*models.DrugFlow, error) {
  5512. flow := models.DrugFlow{}
  5513. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ?", cancelstock_id, drug_id).Find(&flow).Error
  5514. if err == gorm.ErrRecordNotFound {
  5515. return nil, err
  5516. }
  5517. if err != nil {
  5518. return nil, err
  5519. }
  5520. return &flow, nil
  5521. }
  5522. func GetTotalDrugCancelInfo(batchNumber_id int64, drug_id int64, id int64) (info []*models.DrugCancelStockInfo, err error) {
  5523. err = XTReadDB().Where("batch_number_id = ? and drug_id = ? and status = 1 and id<>?", batchNumber_id, drug_id, id).Find(&info).Error
  5524. return info, err
  5525. }
  5526. func GetLastDrugWarehouseInfo(drugid int64) (models.XtDrugWarehouseInfo, error) {
  5527. info := models.XtDrugWarehouseInfo{}
  5528. err := XTReadDB().Where("drug_id = ? and status =1", drugid).Last(&info).Error
  5529. return info, err
  5530. }
  5531. func GetLastGoodWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5532. info := models.WarehousingInfo{}
  5533. err := XTReadDB().Where("good_id =? and status = 1", goodid).Find(&info).Error
  5534. return info, err
  5535. }
  5536. func GetGoodStockFlowInfo(warehouseid int64, goodid int64) (*models.VmStockFlow, error) {
  5537. flow := models.VmStockFlow{}
  5538. err := XTReadDB().Where("warehousing_detail_id = ? and good_id = ? and status = 1", warehouseid, goodid).Find(&flow).Error
  5539. if err == gorm.ErrRecordNotFound {
  5540. return nil, err
  5541. }
  5542. if err != nil {
  5543. return nil, err
  5544. }
  5545. return &flow, nil
  5546. }
  5547. func GetLastInfoMationById(id int64) (models.GoodInfo, error) {
  5548. info := models.GoodInfo{}
  5549. err := XTReadDB().Where("id =? and status = 1", id).Find(&info).Error
  5550. return info, err
  5551. }
  5552. func UpdateGoodInfoMation(goodid int64, info models.GoodInfo) error {
  5553. err := XTWriteDB().Model(&info).Where("id =? and status = 1", goodid).Updates(map[string]interface{}{"total": info.Total}).Error
  5554. return err
  5555. }
  5556. func UpdateWarehouseInfoByGoodId(info models.WarehousingInfo, id int64) error {
  5557. err := XTWriteDB().Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount}).Error
  5558. return err
  5559. }
  5560. func UpdateWarehouseInfoByGoodIdOne(info *models.WarehousingInfo, id int64) error {
  5561. 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
  5562. return err
  5563. }
  5564. func UpdatedStockFlowByGoodId(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  5565. 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
  5566. return err
  5567. }
  5568. func UpdatedStockFlowByGoodIdOne(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  5569. 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
  5570. return err
  5571. }
  5572. func UpdateStockFlowByDetailId(flow models.VmStockFlow, wareoutdetailid int64) error {
  5573. 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
  5574. return err
  5575. }
  5576. func UpdatedGoodInfo(info models.GoodInfo, goodid int64) error {
  5577. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", goodid).Updates(map[string]interface{}{"manufacturer": info.Manufacturer, "dealer": info.Dealer}).Error
  5578. return err
  5579. }
  5580. func GetInventoryExportList(orgid int64) (info []*models.PatientWarehouseInfo, err error) {
  5581. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5582. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5583. fmt.Println(table)
  5584. if orgid > 0 {
  5585. db = db.Where("x.org_id = ?", orgid)
  5586. }
  5587. 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
  5588. return info, err
  5589. }
  5590. func UpdateDrugWarehouseingInfoSix(id int64, info models.XtDrugWarehouseInfo) error {
  5591. warehouseInfo := models.XtDrugWarehouseInfo{}
  5592. 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
  5593. return err
  5594. }
  5595. func CreatedCancelStock(info models.DrugCancelStockInfo) error {
  5596. err := XTWriteDB().Create(&info).Error
  5597. return err
  5598. }
  5599. func CreatedNewCancelStock(info models.DrugCancelStockInfo, tx *gorm.DB) error {
  5600. err := tx.Create(&info).Error
  5601. if err != nil {
  5602. tx.Rollback()
  5603. return err
  5604. }
  5605. return err
  5606. }
  5607. func IsExsitStockFlow(goodid int64, cancel_order_number string) (*models.VmStockFlow, error) {
  5608. flow := models.VmStockFlow{}
  5609. err := XTReadDB().Where("good_id = ? and cancel_order_number = ? and status = 1", goodid, cancel_order_number).Find(&flow).Error
  5610. if err == gorm.ErrRecordNotFound {
  5611. return nil, err
  5612. }
  5613. if err != nil {
  5614. return nil, err
  5615. }
  5616. return &flow, nil
  5617. }
  5618. func UpdateStockFlowTwo(id int64, flow models.VmStockFlow) error {
  5619. err := XTWriteDB().Model(&flow).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"count": flow.Count}).Error
  5620. return err
  5621. }
  5622. func UpdateStockFlowThree(id int64, flow models.VmStockFlow) error {
  5623. 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
  5624. return err
  5625. }
  5626. func GetLastWarehouseOutInfoById(drugid int64) (models.DrugWarehouseOutInfo, error) {
  5627. flow := models.DrugWarehouseOutInfo{}
  5628. err := XTReadDB().Where("drug_id = ? and status = 1", drugid).Last(&flow).Error
  5629. return flow, err
  5630. }
  5631. func GetLastWarehoseByGoodId(goodid int64, patient_id int64, record_date int64) (models.WarehouseOutInfo, error) {
  5632. info := models.WarehouseOutInfo{}
  5633. 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
  5634. return info, err
  5635. }
  5636. func ModifyDrugWarehouseOutInfo(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64) error {
  5637. 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
  5638. return err
  5639. }
  5640. func ModifyDrugWarehouseOutInfoOne(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64, warehouse_info_id int64) error {
  5641. 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
  5642. return err
  5643. }
  5644. func GetDrugWarehouseOutInfoByWarehosueId(drugid int64, patientid int64, recorddate int64, warehosueinfoid int64) (*models.DrugWarehouseOutInfo, error) {
  5645. info := models.DrugWarehouseOutInfo{}
  5646. 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
  5647. if err == gorm.ErrRecordNotFound {
  5648. return nil, err
  5649. }
  5650. if err != nil {
  5651. return nil, err
  5652. }
  5653. return &info, nil
  5654. }
  5655. func GetDrugFlowRecord(warehouse_out_order_number string, drugid int64, patientid int64) (*models.DrugFlow, error) {
  5656. drugFlow := models.DrugFlow{}
  5657. 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
  5658. if err == gorm.ErrRecordNotFound {
  5659. return nil, err
  5660. }
  5661. if err != nil {
  5662. return nil, err
  5663. }
  5664. return &drugFlow, nil
  5665. }
  5666. func ModifyDrugFlow(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64) error {
  5667. 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
  5668. return err
  5669. }
  5670. func ModifyDrugFlowOne(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64, warehousing_detail_id int64) error {
  5671. 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
  5672. return err
  5673. }
  5674. func GetDrugAutoReduceRecordOne(drugid int64, patientid int64, recorddate int64) (detail []*models.DrugAutomaticReduceDetail, err error) {
  5675. err = XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  5676. return detail, err
  5677. }
  5678. func GetDrugAutoReduceRecord(drugid int64, patientid int64, recorddate int64) (*models.DrugAutomaticReduceDetail, error) {
  5679. detail := models.DrugAutomaticReduceDetail{}
  5680. err := XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  5681. if err == gorm.ErrRecordNotFound {
  5682. return nil, err
  5683. }
  5684. if err != nil {
  5685. return nil, err
  5686. }
  5687. return &detail, nil
  5688. }
  5689. func ModifyAutoMaticReduce(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64) error {
  5690. 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
  5691. return err
  5692. }
  5693. func ModifyAutoMaticReduceOne(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64, warehouse_info_id int64) error {
  5694. 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
  5695. return err
  5696. }
  5697. func ModifyDrugWarehouse(id int64, stock_max_number int64, stock_min_number int64) (models.DrugWarehouseInfo, error) {
  5698. info := models.DrugWarehouseInfo{}
  5699. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  5700. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  5701. return info, err
  5702. }
  5703. func ModeifyDrugWarehosueInfo(id int64, stock_max_number int64) error {
  5704. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  5705. return err
  5706. }
  5707. func ModeFyStockMinById(id int64, stock_min_number int64) error {
  5708. info := models.DrugWarehouseInfo{}
  5709. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  5710. return err
  5711. }
  5712. func GetDrugCancelById(id int64) (models.DrugCancelStockInfo, error) {
  5713. info := models.DrugCancelStockInfo{}
  5714. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5715. return info, err
  5716. }
  5717. func ModeFyDrugWareseInfo(id int64, stock_max_number int64) error {
  5718. info := models.DrugWarehouseInfo{}
  5719. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", stock_max_number)).Error
  5720. return err
  5721. }
  5722. func ModeFyDrugWarehouseMin(id int64, stock_min_number int64) error {
  5723. info := models.DrugWarehouseInfo{}
  5724. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", stock_min_number)).Error
  5725. return err
  5726. }
  5727. func GetAllHisDoctorAdviceById(drugid int64, patient_id int64, advicedate int64, orgid int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  5728. 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
  5729. return advice, err
  5730. }
  5731. func GetAllHisDoctorAdviceByIdOne(drugid int64, patient_id int64, advicedate int64, orgid int64, tx *gorm.DB) (advice []*models.HisDoctorAdviceInfo, err error) {
  5732. err = tx.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
  5733. if err != gorm.ErrRecordNotFound {
  5734. if err != nil {
  5735. tx.Rollback()
  5736. return advice, err
  5737. }
  5738. }
  5739. return advice, err
  5740. }
  5741. func GetAllDrugFlowById(drugid int64, patient_id int64, advicedate int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  5742. 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
  5743. return info, err
  5744. }
  5745. func GetAllDrugFlowByIdOne(drugid int64, patient_id int64, advicedate int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  5746. err = tx.Where("drug_id = ? and patient_id = ? and sys_record_time = ? and org_id = ? and status = 1", drugid, patient_id, advicedate, orgid).Find(&info).Error
  5747. if err != gorm.ErrRecordNotFound {
  5748. if err != nil {
  5749. tx.Rollback()
  5750. return info, err
  5751. }
  5752. }
  5753. return info, err
  5754. }
  5755. // 耗材加库存
  5756. func ModifyGoodAddInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5757. info := models.GoodInfo{}
  5758. 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
  5759. return info, err
  5760. }
  5761. func ModifyGoodAddInformationSix(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5762. info := models.GoodInfo{}
  5763. 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
  5764. 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
  5765. return info, err
  5766. }
  5767. func ModifyGoodReduceInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5768. info := models.GoodInfo{}
  5769. 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
  5770. return info, err
  5771. }
  5772. func ModifyGoodReduceInformationTwo(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5773. info := models.GoodInfo{}
  5774. 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
  5775. 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
  5776. return info, err
  5777. }
  5778. func ModifyGoodReduceInformationSix(good_id int64, sum_count int64, orgid int64, retail_price float64) (models.GoodInfo, error) {
  5779. info := models.GoodInfo{}
  5780. 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
  5781. return info, err
  5782. }
  5783. func GetWarehousingByIdList(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  5784. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  5785. return info, err
  5786. }
  5787. func GetWarehousingByIdListTwenty(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  5788. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  5789. return info, err
  5790. }
  5791. func GetWarehouseInfoByIdSix(id int64, orgid int64) (models.WarehousingInfo, error) {
  5792. info := models.WarehousingInfo{}
  5793. err := XTReadDB().Model(&info).Where("id = ? and status= 1 and org_id = ?", id, orgid).Find(&info).Error
  5794. return info, err
  5795. }
  5796. func GetWarehouseInfoByIdTwenty(id int64, orgid int64) (models.WarehousingInfo, error) {
  5797. info := models.WarehousingInfo{}
  5798. err := XTReadDB().Model(&info).Where("id = ? and org_id = ?", id, orgid).Find(&info).Error
  5799. return info, err
  5800. }
  5801. // 药品加库存
  5802. func ModifyDrugAddInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5803. info := models.BaseDrugLib{}
  5804. 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
  5805. return info, err
  5806. }
  5807. func ModifyDrugAddInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5808. info := models.BaseDrugLib{}
  5809. 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
  5810. 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
  5811. return info, err
  5812. }
  5813. func ModifyDrugReduceInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5814. info := models.BaseDrugLib{}
  5815. 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
  5816. return info, err
  5817. }
  5818. func ModifyDrugReduceInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5819. info := models.BaseDrugLib{}
  5820. 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
  5821. 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
  5822. return info, err
  5823. }
  5824. func ModifyDrugReduceInformationSix(drug_id int64, sum_count int64, orgid int64, batch_retai_price float64) (models.BaseDrugLib, error) {
  5825. info := models.BaseDrugLib{}
  5826. 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
  5827. 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
  5828. return info, err
  5829. }
  5830. func GetLastDrugwarehouseInfoById(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5831. err = XTReadDB().Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  5832. return info, err
  5833. }
  5834. func GetLastDrugwarehouseInfoByIdTwenty(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5835. err = XTReadDB().Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  5836. return info, err
  5837. }
  5838. func GetWarhouseOutById(id int64) (models.WarehouseOutInfo, error) {
  5839. info := models.WarehouseOutInfo{}
  5840. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5841. return info, err
  5842. }
  5843. func FindeGoodWareHouseOutInfo(id int64, storehouse_id int64, orgid int64) (out []*models.WarehouseOutInfo, err error) {
  5844. err = XTReadDB().Where("warehouse_out_id = ? and storehouse_id =? and org_id = ?", id, storehouse_id, orgid).Find(&out).Error
  5845. return out, err
  5846. }
  5847. func ModifyWarehouseInfo(id int64, count int64) (models.WarehousingInfo, error) {
  5848. info := models.WarehousingInfo{}
  5849. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  5850. return info, err
  5851. }
  5852. func GetOrgIdList() (template []*models.VMGobalTemplate, err error) {
  5853. err = XTReadDB().Where("status = 1").Find(&template).Error
  5854. return template, err
  5855. }
  5856. func GetAllGoodList(orgid int64) (good []*models.GoodInfo, err error) {
  5857. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&good).Error
  5858. return good, err
  5859. }
  5860. func ModifyGoodAddInformationSeven(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5861. info := models.GoodInfo{}
  5862. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  5863. return info, err
  5864. }
  5865. func GetStockCountWarehouseInfoById(good_id int64) (info []*models.WarehousingInfo, err error) {
  5866. db := XTReadDB().Where("status = 1")
  5867. if good_id > 0 {
  5868. db = db.Where("good_id = ?", good_id)
  5869. }
  5870. err = db.Find(&info).Error
  5871. return info, err
  5872. }
  5873. func GetDrugStockCountWarehoseInfoById(drug_id int64) (info []*models.DrugWarehouseInfo, err error) {
  5874. db := XTReadDB().Model(&info).Where("status = 1")
  5875. if drug_id > 0 {
  5876. db = db.Where("drug_id = ?", drug_id)
  5877. }
  5878. err = db.Find(&info).Error
  5879. return info, err
  5880. }
  5881. func UpdateModifyDrugInfoById(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5882. lib := models.BaseDrugLib{}
  5883. err := XTWriteDB().Model(&lib).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  5884. return lib, err
  5885. }
  5886. func GetWarehouseBySecondWarehouseId(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (*models.Warehousing, error) {
  5887. info := models.Warehousing{}
  5888. 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
  5889. if err == gorm.ErrRecordNotFound {
  5890. return nil, err
  5891. }
  5892. if err != nil {
  5893. return nil, err
  5894. }
  5895. return &info, nil
  5896. }
  5897. func GetLastWarehouseById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.Warehousing, error) {
  5898. info := models.Warehousing{}
  5899. 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
  5900. return info, err
  5901. }
  5902. func GetDrugWarehouseBySyS(orgid int64, is_sys int64, sencond_wahouse_id int64, record_date int64) (*models.DrugWarehouseOut, error) {
  5903. out := models.DrugWarehouseOut{}
  5904. 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
  5905. if err == gorm.ErrRecordNotFound {
  5906. return nil, err
  5907. }
  5908. if err != nil {
  5909. return nil, err
  5910. }
  5911. return &out, nil
  5912. }
  5913. func GetLastDrugWarehouseOutById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.DrugWarehouseOut, error) {
  5914. info := models.DrugWarehouseOut{}
  5915. 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
  5916. return info, err
  5917. }
  5918. func GetDrugWarehouseingBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (*models.DrugWarehouse, error) {
  5919. info := models.DrugWarehouse{}
  5920. 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
  5921. if err == gorm.ErrRecordNotFound {
  5922. return nil, err
  5923. }
  5924. if err != nil {
  5925. return nil, err
  5926. }
  5927. return &info, nil
  5928. }
  5929. func CreateDrugWaresing(warehouse models.DrugWarehouse) error {
  5930. err := XTWriteDB().Create(&warehouse).Error
  5931. return err
  5932. }
  5933. func GetLastDrugWarehouseBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (models.DrugWarehouse, error) {
  5934. info := models.DrugWarehouse{}
  5935. 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
  5936. return info, err
  5937. }
  5938. func ModifyGoodReduceInformationTwenty(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5939. info := models.GoodInfo{}
  5940. 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
  5941. 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
  5942. return info, err
  5943. }
  5944. func ModifyGoodReduceInformationTwentyOne(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5945. info := models.GoodInfo{}
  5946. 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
  5947. 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
  5948. return info, err
  5949. }
  5950. func UpdateMedicalSumCount(drug_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  5951. tx := XTWriteDB().Begin()
  5952. 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
  5953. if err != nil {
  5954. tx.Rollback()
  5955. return err
  5956. }
  5957. tx.Commit()
  5958. return err
  5959. }
  5960. //func UpdateMedicalSumCountTX(drug_id int64, sum_count int64, sum_in_count int64, orgid int64,tx *gorm.DB) error {
  5961. // 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
  5962. // return err
  5963. //}
  5964. func UpdateGoodByGoodId(good_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  5965. 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
  5966. return err
  5967. }
  5968. func GetDrugDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  5969. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  5970. table := XTReadDB().Table("xt_base_drug as x d")
  5971. fmt.Println(table)
  5972. if startime > 0 {
  5973. db = db.Where("x.advice_date >= ?", startime)
  5974. }
  5975. if endtime > 0 {
  5976. db = db.Where("x.advice_date <= ?", endtime)
  5977. }
  5978. if orgid > 0 {
  5979. db = db.Where("x.user_org_id = ?", orgid)
  5980. }
  5981. if len(keyword) > 0 {
  5982. likeKey := "%" + keyword + "%"
  5983. 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)
  5984. }
  5985. offset := (page - 1) * limit
  5986. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.created_time desc").Group("x.drug_id").Scan(&adviceinfo).Error
  5987. return adviceinfo, total, err
  5988. }
  5989. func GetDrugDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  5990. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  5991. table := XTReadDB().Table("xt_base_drug as x d")
  5992. fmt.Println(table)
  5993. if startime > 0 {
  5994. db = db.Where("x.advice_date >= ?", startime)
  5995. }
  5996. if endtime > 0 {
  5997. db = db.Where("x.advice_date <= ?", endtime)
  5998. }
  5999. if orgid > 0 {
  6000. db = db.Where("x.user_org_id = ?", orgid)
  6001. }
  6002. if len(keyword) > 0 {
  6003. likeKey := "%" + keyword + "%"
  6004. 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)
  6005. }
  6006. err = db.Order("x.created_time desc").Scan(&adviceinfo).Error
  6007. return adviceinfo, total, err
  6008. }
  6009. func GetAllDrugSummary(orgid int64) (drug []*models.BaseDrugLib, err error) {
  6010. err = XTReadDB().Where("org_id = ? and status = 1 and is_user = 1", orgid).Find(&drug).Error
  6011. return drug, err
  6012. }
  6013. func GetGoodDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  6014. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  6015. table := XTReadDB().Table("xt_good_information as d")
  6016. fmt.Println(table)
  6017. if startime > 0 {
  6018. db = db.Where("x.record_date >= ?", startime)
  6019. }
  6020. if endtime > 0 {
  6021. db = db.Where("x.record_date <= ?", endtime)
  6022. }
  6023. if orgid > 0 {
  6024. db = db.Where("x.user_org_id = ?", orgid)
  6025. }
  6026. if len(keyword) > 0 {
  6027. likeKey := "%" + keyword + "%"
  6028. 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")
  6029. }
  6030. offset := (page - 1) * limit
  6031. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Group("x.project_id").Scan(&project).Error
  6032. return project, total, err
  6033. }
  6034. func GetGoodDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  6035. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  6036. table := XTReadDB().Table("xt_good_information as d")
  6037. fmt.Println(table)
  6038. if startime > 0 {
  6039. db = db.Where("x.record_date >= ?", startime)
  6040. }
  6041. if endtime > 0 {
  6042. db = db.Where("x.record_date <= ?", endtime)
  6043. }
  6044. if orgid > 0 {
  6045. db = db.Where("x.user_org_id = ?", orgid)
  6046. }
  6047. if len(keyword) > 0 {
  6048. likeKey := "%" + keyword + "%"
  6049. 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")
  6050. }
  6051. err = db.Order("x.ctime desc").Group("x.id").Scan(&project).Error
  6052. return project, total, err
  6053. }
  6054. func GeAllGoodInformation(orgid int64) (good []*models.GoodInfo, err error) {
  6055. err = XTReadDB().Where("org_id =? and status = 1 and is_user = 1", orgid).Find(&good).Error
  6056. return good, err
  6057. }
  6058. func GetStockSettingIsExsit(orgid int64) (*models.XtStockSetting, error) {
  6059. setting := models.XtStockSetting{}
  6060. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6061. if err == gorm.ErrRecordNotFound {
  6062. return nil, err
  6063. }
  6064. if err != nil {
  6065. return nil, err
  6066. }
  6067. return &setting, nil
  6068. }
  6069. func CreateStockSetting(setting models.XtStockSetting) error {
  6070. err := XTWriteDB().Create(&setting).Error
  6071. return err
  6072. }
  6073. func UpdateStockSetting(orgid int64, setting models.XtStockSetting) error {
  6074. err := XTWriteDB().Model(&models.XtStockSetting{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_type": setting.IsType}).Error
  6075. return err
  6076. }
  6077. func FindStockSettingById(orgid int64) (models.XtStockSetting, error) {
  6078. setting := models.XtStockSetting{}
  6079. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6080. return setting, err
  6081. }
  6082. func GetAutoStockOutById(good_id int64, record_date int64, orgid int64, patient_id int64) (models.BloodAutomaticReduceDetail, error) {
  6083. detail := models.BloodAutomaticReduceDetail{}
  6084. 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
  6085. return detail, err
  6086. }
  6087. func GetAutoWarehouseOutList(good_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfoNight, error) {
  6088. detail := models.WarehouseOutInfoNight{}
  6089. 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
  6090. return detail, err
  6091. }
  6092. func GetAdviceConfig(orgid int64) (*models.XtAdviceSetting, error) {
  6093. setting := models.XtAdviceSetting{}
  6094. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6095. if err == gorm.ErrRecordNotFound {
  6096. return nil, err
  6097. }
  6098. if err != nil {
  6099. return nil, err
  6100. }
  6101. return &setting, nil
  6102. }
  6103. func CreateAdviceSetting(setting models.XtAdviceSetting) error {
  6104. err := XTWriteDB().Create(&setting).Error
  6105. return err
  6106. }
  6107. func UpdateAdviceSetting(orgid int64, setting models.XtAdviceSetting) error {
  6108. 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
  6109. return err
  6110. }
  6111. func FindAdviceSettingById(orgid int64) (models.XtAdviceSetting, error) {
  6112. setting := models.XtAdviceSetting{}
  6113. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6114. return setting, err
  6115. }
  6116. func GetPrescriptionConfig(orgid int64) (*models.XtPrescriptionConfig, error) {
  6117. config := models.XtPrescriptionConfig{}
  6118. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6119. if err == gorm.ErrRecordNotFound {
  6120. return nil, err
  6121. }
  6122. if err != nil {
  6123. return nil, err
  6124. }
  6125. return &config, nil
  6126. }
  6127. func CreatePrescriptionConfig(prescriptionConfig models.XtPrescriptionConfig) error {
  6128. err := XTWriteDB().Create(&prescriptionConfig).Error
  6129. return err
  6130. }
  6131. func UpdatePrescriptionConfig(orgid int64, config models.XtPrescriptionConfig) error {
  6132. 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
  6133. return err
  6134. }
  6135. func FindPrescriptionConfigById(orgid int64) (models.XtPrescriptionConfig, error) {
  6136. config := models.XtPrescriptionConfig{}
  6137. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6138. return config, err
  6139. }
  6140. func UpdateWarehouseInfoTwentyFive(maxNumber int64, drugid int64, orgID int64, storehouse_id int64, drup *models.XtBaseDrug) (err error) {
  6141. var deliver_number int64 = 0
  6142. var stock_number int64 = 0
  6143. deliver_number = maxNumber
  6144. // 根据先进先出原则,查询最先入库的批次,进行出库
  6145. // 如果没有对应的库存,则报错
  6146. //开启事物
  6147. warehouse, err := FindLastDrugWarehousingInfoByID(drugid, storehouse_id)
  6148. if err != nil {
  6149. return err
  6150. }
  6151. // 将该批次的剩余库存数量转换为拆零数量
  6152. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  6153. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  6154. if stock_number >= deliver_number {
  6155. var maxNumber int64 = 0
  6156. var minNumber int64 = 0
  6157. var stock_max_number int64 = 0
  6158. stock_max_number = warehouse.StockMaxNumber
  6159. maxNumber = deliver_number / drup.MinNumber
  6160. minNumber = deliver_number % drup.MinNumber
  6161. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  6162. minNumber = maxNumber
  6163. }
  6164. if drup.MaxUnit != drup.MinUnit {
  6165. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  6166. return errors.New("库存数量不足")
  6167. }
  6168. }
  6169. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  6170. if warehouse.StockMaxNumber < 0 {
  6171. warehouse.StockMaxNumber = 0
  6172. }
  6173. if warehouse.StockMinNumber < 0 {
  6174. warehouse.StockMinNumber = 0
  6175. }
  6176. warehouse.Mtime = time.Now().Unix()
  6177. if warehouse.StockMinNumber < minNumber {
  6178. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  6179. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  6180. } else {
  6181. if minNumber > 0 {
  6182. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  6183. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6184. } else {
  6185. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  6186. }
  6187. }
  6188. if minNumber == 0 && maxNumber != 1 {
  6189. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6190. }
  6191. }
  6192. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  6193. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6194. if warehouse.StockMinNumber == 0 {
  6195. warehouse.StockMaxNumber = stock_max_number
  6196. }
  6197. }
  6198. if drup.MaxUnit != drup.MinUnit {
  6199. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  6200. return errors.New("库存数量不足")
  6201. }
  6202. }
  6203. if warehouse.StockMinNumber <= 0 {
  6204. warehouse.StockMinNumber = 0
  6205. }
  6206. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6207. fmt.Println(errThree)
  6208. //查询默认仓库
  6209. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6210. //查询默认仓库剩余多少库存
  6211. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6212. var sum_count int64
  6213. var sum_in_count int64
  6214. for _, it := range list {
  6215. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6216. if it.MaxUnit == baseDrug.MaxUnit {
  6217. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6218. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6219. sum_count += it.StockMaxNumber + it.StockMinNumber
  6220. sum_in_count += it.WarehousingCount
  6221. }
  6222. }
  6223. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6224. return nil
  6225. } else {
  6226. warehouse.StockMaxNumber = 0
  6227. warehouse.StockMinNumber = 0
  6228. warehouse.Mtime = time.Now().Unix()
  6229. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6230. fmt.Println(errThree)
  6231. //查询默认仓库
  6232. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6233. //查询默认仓库剩余多少库存
  6234. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6235. var sum_count int64
  6236. var sum_in_count int64
  6237. for _, it := range list {
  6238. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6239. if it.MaxUnit == baseDrug.MaxUnit {
  6240. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6241. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6242. sum_count += it.StockMaxNumber + it.StockMinNumber
  6243. sum_in_count += it.WarehousingCount
  6244. }
  6245. }
  6246. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6247. if errThree != nil {
  6248. return errThree
  6249. }
  6250. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  6251. prescribingNumber_two_temp := deliver_number - stock_number
  6252. UpdateWarehouseInfoTwentyFive(prescribingNumber_two_temp, drugid, orgID, storehouse_id, drup)
  6253. }
  6254. return
  6255. }
  6256. func UpdateWarehouseInfoTentyFour(count int64, id int64) error {
  6257. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6258. return err
  6259. }
  6260. func UpdateWarehouseInfoTentyThree(count int64, id int64) error {
  6261. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6262. return err
  6263. }
  6264. func GetDrugOpenConfig(orgid int64) (*models.DrugOutConfig, error) {
  6265. config := models.DrugOutConfig{}
  6266. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6267. if err == gorm.ErrRecordNotFound {
  6268. return nil, err
  6269. }
  6270. if err != nil {
  6271. return nil, err
  6272. }
  6273. return &config, nil
  6274. }
  6275. func GetDrugOpenConfigOne(orgid int64) (models.DrugOutConfig, error) {
  6276. config := models.DrugOutConfig{}
  6277. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).First(&config).Error
  6278. return config, err
  6279. }
  6280. func FindXTHisRecordByOrgIdSix(org_id int64) (models.XtHisConfig, error) {
  6281. config := models.XtHisConfig{}
  6282. err = readDb.Model(&models.XtHisConfig{}).Where("status = 1 AND user_org_id = ?", org_id).Find(&config).Error
  6283. return config, err
  6284. }
  6285. func CreateDrugOutConfig(config models.DrugOutConfig) error {
  6286. err := XTWriteDB().Create(&config).Error
  6287. return err
  6288. }
  6289. func UpdateDrugOutConfig(orgid int64, drug_out_open int64) error {
  6290. err := XTWriteDB().Model(&models.DrugOutConfig{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": drug_out_open}).Error
  6291. return err
  6292. }
  6293. func FindDrugOutConfigById(orgid int64) (models.DrugOutConfig, error) {
  6294. config := models.DrugOutConfig{}
  6295. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6296. return config, err
  6297. }
  6298. func FindeDrugSettleConfigById(orgid int64) (models.HisSettleStockConfig, error) {
  6299. config := models.HisSettleStockConfig{}
  6300. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6301. return config, err
  6302. }
  6303. func UpdatedWarehouseOut(id int64) error {
  6304. err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6305. err = XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6306. return err
  6307. }
  6308. func UpdatedSigleWarehouseOutInfo(id int64, info *models.WarehouseOutInfo) error {
  6309. tx := XTWriteDB().Begin()
  6310. err := tx.Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_info_id": info.WarehouseInfotId}).Error
  6311. if err != nil {
  6312. tx.Rollback()
  6313. return err
  6314. }
  6315. tx.Commit()
  6316. return err
  6317. }
  6318. func GetLastGoodWarehouseOutInfo(orgid int64, id int64) (info []*models.WarehouseOutInfo, err error) {
  6319. err = XTReadDB().Where("org_id = ? and status = 1 and warehouse_out_id = ?", orgid, id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  6320. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  6321. }).Find(&info).Error
  6322. return info, err
  6323. }
  6324. func GetLastGoodWarehouseOutInfoById(orgid int64) (models.WarehouseOutInfo, error) {
  6325. info := models.WarehouseOutInfo{}
  6326. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&info).Error
  6327. return info, err
  6328. }
  6329. func ModifyWarehouseOutInfo(id int64, info *models.WarehouseOutInfo) error {
  6330. 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
  6331. return err
  6332. }
  6333. func UpdateWarehouseOutFlow(info models.VmStockFlow, id int64) error {
  6334. 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
  6335. return err
  6336. }
  6337. func GetLastGoodWarehouseOutTwenty(orgid int64) (models.WarehouseOut, error) {
  6338. out := models.WarehouseOut{}
  6339. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  6340. return out, err
  6341. }
  6342. func UpdateaGoodWarehouseInfo(count int64, good_id int64, orgid int64, warehouse_info_id int64) error {
  6343. ut := writeDb.Begin()
  6344. 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
  6345. if err != nil {
  6346. ut.Rollback()
  6347. return err
  6348. }
  6349. ut.Commit()
  6350. return err
  6351. }
  6352. func ModifyGoodWarehouseOut(id int64) error {
  6353. ut := writeDb.Begin()
  6354. err := ut.Model(&models.WarehouseOut{}).Where("id = ? and status =1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6355. if err != nil {
  6356. ut.Rollback()
  6357. return err
  6358. }
  6359. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6360. if err != nil {
  6361. ut.Rollback()
  6362. return err
  6363. }
  6364. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  6365. if err != nil {
  6366. ut.Rollback()
  6367. return err
  6368. }
  6369. err = ut.Model(&models.XtWarehouseFlushInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  6370. if err != nil {
  6371. ut.Rollback()
  6372. return err
  6373. }
  6374. ut.Commit()
  6375. return err
  6376. }
  6377. func GetGoodWarehouseInfoById(waresing_id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  6378. err = XTReadDB().Where("org_id = ? and status = 1 and warehousing_id = ?", orgid, waresing_id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  6379. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  6380. }).Find(&info).Error
  6381. return info, err
  6382. }
  6383. func FindLastWarehousing(order string) (info models.Warehousing, err error) {
  6384. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  6385. return info, err
  6386. }
  6387. func UpdateWarehouseDetail(info *models.WarehousingInfo, id int64) error {
  6388. 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
  6389. return err
  6390. }
  6391. func UpdateCheckWarehouseInfo(id int64, checker int64) error {
  6392. tx := XTWriteDB().Begin()
  6393. err := tx.Model(&models.Warehousing{}).Where("id =? and status = 1", id).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  6394. if err != nil {
  6395. tx.Rollback()
  6396. return err
  6397. }
  6398. tx.Commit()
  6399. return err
  6400. }
  6401. func GetWarehouseInfoList(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  6402. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  6403. return info, err
  6404. }
  6405. func UpdateWarehouseInfoByIdListThree(count int64, id int64) error {
  6406. tx := XTWriteDB().Begin()
  6407. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"stock_count": count}).Error
  6408. if err != nil {
  6409. tx.Rollback()
  6410. return err
  6411. }
  6412. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6413. if err != nil {
  6414. tx.Rollback()
  6415. return err
  6416. }
  6417. tx.Commit()
  6418. return err
  6419. }
  6420. func FindLastWarehousingSix(org_id int64) (info models.Warehousing, err error) {
  6421. err = readDb.Model(&models.Warehousing{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  6422. return info, err
  6423. }
  6424. func FindLastWarehousingSeven(org_id int64) (info models.DrugWarehouse, err error) {
  6425. err = readDb.Model(&models.DrugWarehouse{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  6426. return info, err
  6427. }
  6428. func FindLastDrugWarehousingInfoTwenty(org_id int64) (info models.DrugWarehouseInfo, err error) {
  6429. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  6430. return info, err
  6431. }
  6432. func FindDrugWarehouseInfoListById(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  6433. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "status = 1 and org_id =?", orgid).Find(&info).Error
  6434. return info, err
  6435. }
  6436. func CreatedWarehouseing(info *models.DrugWarehouseInfo) error {
  6437. err := XTWriteDB().Create(&info).Error
  6438. return err
  6439. }
  6440. func UpdateWarehouseing(info *models.DrugWarehouseInfo) error {
  6441. err := XTWriteDB().Save(&info).Error
  6442. return err
  6443. }
  6444. func CheckWarehousingInfo(id int64, orgid int64, checker int64) error {
  6445. tx := XTWriteDB().Begin()
  6446. 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
  6447. if err != nil {
  6448. tx.Rollback()
  6449. return err
  6450. }
  6451. tx.Commit()
  6452. return err
  6453. }
  6454. func GetWarehousingInfoByList(id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  6455. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  6456. return info, err
  6457. }
  6458. func AddDrugWarehouseStockMaxNumber(count int64, id int64) error {
  6459. tx := XTWriteDB().Begin()
  6460. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6461. if err != nil {
  6462. tx.Rollback()
  6463. return err
  6464. }
  6465. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6466. if err != nil {
  6467. tx.Rollback()
  6468. return err
  6469. }
  6470. tx.Commit()
  6471. return err
  6472. }
  6473. func UpdateWarehousingInfoFlow(id int64, orgid int64) error {
  6474. tx := XTWriteDB().Begin()
  6475. 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
  6476. if err != nil {
  6477. tx.Rollback()
  6478. return err
  6479. }
  6480. tx.Commit()
  6481. return err
  6482. }
  6483. func AddDrugWarehouseStockMinNumber(count int64, id int64) error {
  6484. tx := XTWriteDB().Begin()
  6485. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6486. if err != nil {
  6487. tx.Rollback()
  6488. return err
  6489. }
  6490. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6491. tx.Commit()
  6492. return err
  6493. }
  6494. func ReturnCheckWarehouseingInfo(id int64, orgid int64) error {
  6495. tx := XTWriteDB().Begin()
  6496. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  6497. if err != nil {
  6498. tx.Rollback()
  6499. return err
  6500. }
  6501. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("warehousing_id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  6502. if err != nil {
  6503. tx.Rollback()
  6504. return err
  6505. }
  6506. tx.Commit()
  6507. return err
  6508. }
  6509. func AddDrugReturnWarehouseStockMaxNumber(count int64, id int64) error {
  6510. tx := XTWriteDB().Begin()
  6511. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6512. if err != nil {
  6513. tx.Rollback()
  6514. return err
  6515. }
  6516. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6517. if err != nil {
  6518. tx.Rollback()
  6519. return err
  6520. }
  6521. tx.Commit()
  6522. return err
  6523. }
  6524. func AddDrugReturnWarehouseStockMinNumber(count int64, id int64) error {
  6525. tx := XTWriteDB().Begin()
  6526. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  6527. if err != nil {
  6528. tx.Rollback()
  6529. return err
  6530. }
  6531. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6532. if err != nil {
  6533. tx.Rollback()
  6534. return err
  6535. }
  6536. tx.Commit()
  6537. return err
  6538. }
  6539. func UpdateDrugWarehouseInfoTwenty(id int64, info *models.DrugWarehouseInfo) error {
  6540. 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
  6541. return err
  6542. }
  6543. func UpdatedDrugWarehouseInfo(info *models.DrugWarehouseOutInfo, id int64) error {
  6544. 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
  6545. return err
  6546. }
  6547. func GetDrugLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  6548. out := models.DrugWarehouseOut{}
  6549. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  6550. return out, err
  6551. }
  6552. func GetDrugWarehouseOutListById(warehouse_out_id int64, orgId int64) (info []*models.DrugWarehouseOutInfo, err error) {
  6553. err = XTReadDB().Where("warehouse_out_id = ? and org_id =? and status = 1", warehouse_out_id, orgId).Preload("BaseDrugLib", "status=1").Find(&info).Error
  6554. return info, err
  6555. }
  6556. func UpdateCheckDrugOut(out models.DrugWarehouseOut, id int64) error {
  6557. tx := XTWriteDB().Begin()
  6558. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": out.IsCheck}).Error
  6559. if err != nil {
  6560. tx.Rollback()
  6561. return err
  6562. }
  6563. err = XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6564. if err != nil {
  6565. tx.Rollback()
  6566. return err
  6567. }
  6568. tx.Commit()
  6569. return err
  6570. }
  6571. func UpdateCheckDrugOutInfoList(info models.DrugWarehouseOutInfo, warehouse_out_id int64) error {
  6572. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", warehouse_out_id).Update(map[string]interface{}{"is_check": info.IsCheck}).Error
  6573. return err
  6574. }
  6575. func GetDrugWarehouseOutByLastId(id int64) (models.DrugWarehouseOut, error) {
  6576. out := models.DrugWarehouseOut{}
  6577. err := XTReadDB().Where("id = ? and status = 1", id).Find(&out).Error
  6578. return out, err
  6579. }
  6580. func UpdateDrugMaxNumber(count int64, id int64) error {
  6581. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6582. return err
  6583. }
  6584. func UpdateDrugMinNumber(count int64, id int64) error {
  6585. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  6586. return err
  6587. }
  6588. func DeleteDrugOutFlow(id int64, orgid int64) error {
  6589. 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
  6590. return err
  6591. }
  6592. func GetWarehouseOutByWarehouseInfoId(warehouse_info_id int64, good_id int64, orgid int64) (outInfo []*models.WarehouseOutInfo, err error) {
  6593. 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
  6594. return outInfo, err
  6595. }
  6596. func GetDrugWarehouseOutByWarehouseInfoId(warehouse_info_id int64, drug_id int64, orgid int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  6597. 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
  6598. return outInfo, err
  6599. }
  6600. func GetAllStockInList(id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  6601. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("GoodInfo", "status = 1 and org_id =?", orgid).Find(&info).Error
  6602. return info, err
  6603. }
  6604. func GetDrugWarehouseInfoList(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  6605. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "org_id = ? and status = 1", orgid).Find(&info).Error
  6606. return info, err
  6607. }
  6608. func GetStockCountByGoodId(goodid int64, storehouse_id int64, orgId int64) (info []*models.WarehousingInfo, err error) {
  6609. 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
  6610. return info, err
  6611. }
  6612. func UpdateGoodInfoSumCount(goodid int64, sum_count int64, orgid int64) error {
  6613. 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
  6614. 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
  6615. return err
  6616. }
  6617. func UpdateGoodInfoSumCountSix(goodid int64, sum_count int64, orgid int64) error {
  6618. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6619. return err
  6620. }
  6621. func UpdateGoodInfoReduceSumCount(goodid int64, sum_count int64, orgid int64) error {
  6622. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6623. return err
  6624. }
  6625. func UpdateGoodInfoAddSumCount(goodid int64, sum_count int64, orgid int64, sum_in_count int64) error {
  6626. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6627. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  6628. return err
  6629. }
  6630. func GetCancelStockInfo(id int64, orgid int64) (stock []*models.CancelStockInfo, err error) {
  6631. 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
  6632. return stock, err
  6633. }
  6634. func ModifyCancelStockInfo(id int64, info *models.CancelStockInfo) error {
  6635. 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
  6636. return err
  6637. }
  6638. func GetCancelStockInfoListById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  6639. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  6640. return info, err
  6641. }
  6642. func GetWarehouseOutList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.WarehouseOutInfo, err error) {
  6643. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  6644. return info, err
  6645. }
  6646. func GetCancelWarehouseList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.CancelStockInfo, err error) {
  6647. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  6648. return info, err
  6649. }
  6650. func CheckCancelStock(stock models.CancelStock, cancel_stock_id int64, orgid int64) error {
  6651. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and status = 1 and org_id = ?", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 1}).Error
  6652. err = XTWriteDB().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
  6653. return err
  6654. }
  6655. func GetCancelStockInfoById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  6656. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&info).Error
  6657. return info, err
  6658. }
  6659. func UpdateCancelStockNumber(stock_count int64, warehouse_info_id int64, orgid int64) error {
  6660. err := XTWriteDB().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
  6661. return err
  6662. }
  6663. func UpdateGoodSumCount(sum_count int64, good_id int64, orgid int64) error {
  6664. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  6665. return err
  6666. }
  6667. func UpdateStockWarehouseInfo(cancel_stock_id int64, orgid int64, stock models.CancelStock) error {
  6668. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  6669. err = XTWriteDB().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
  6670. return err
  6671. }
  6672. func UpdateStockWarehouseInfoStockFlow(id int64, orgid int64, goodid int64) error {
  6673. err = XTWriteDB().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
  6674. return err
  6675. }
  6676. func UpdateStockNumberWarehouseInfo(warehouse_info_id int64, good_id int64, org_id int64, stock_count int64) error {
  6677. 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
  6678. 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
  6679. return err
  6680. }
  6681. func UpdateDrugSumCount(drugid int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6682. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ?", drugid, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  6683. 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
  6684. return err
  6685. }
  6686. func GetDrugStockWarehouseInfo(id int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfo, err error) {
  6687. err = XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1 and storehouse_id = ?", id, orgid, storehouse_id).Find(&info).Error
  6688. return info, err
  6689. }
  6690. func UpdateBaseDrugSum(drugid int64, orgid int64, sum_count int64, sum_in_count int64) error {
  6691. 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
  6692. 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
  6693. return err
  6694. }
  6695. func GetDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64) (info []*models.DrugWarehouseInfo, err error) {
  6696. 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
  6697. return info, err
  6698. }
  6699. func GetNewDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
  6700. 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
  6701. if err != gorm.ErrRecordNotFound {
  6702. if err != nil {
  6703. tx.Rollback()
  6704. return info, err
  6705. }
  6706. }
  6707. return info, err
  6708. }
  6709. func UpdateBaseDrugSumInfo(sum_count int64, drugid int64, orgid int64, sum_in_count int64) error {
  6710. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6711. err = XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  6712. return err
  6713. }
  6714. func UpdateBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64) error {
  6715. err = writeDb.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6716. return err
  6717. }
  6718. func UpdateNewBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64, tx *gorm.DB) error {
  6719. err = tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6720. if err != nil {
  6721. tx.Rollback()
  6722. return err
  6723. }
  6724. return err
  6725. }
  6726. func GetLastCancelStockByList(id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  6727. 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
  6728. return info, err
  6729. }
  6730. func GetCancelDrugStockList(cancel_stock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  6731. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  6732. return info, err
  6733. }
  6734. func ModifyCancelStock(cancel_stock_id int64, stock models.DrugCancelStock, orgid int64) error {
  6735. 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
  6736. 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
  6737. return err
  6738. }
  6739. func GetCancelDrugStock(cancel_stock_id int64, orgid int64) (models.DrugCancelStock, error) {
  6740. stock := models.DrugCancelStock{}
  6741. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&stock).Error
  6742. return stock, err
  6743. }
  6744. func GetDrugCancelCheckById(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  6745. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&stock).Error
  6746. return stock, err
  6747. }
  6748. func GetDrugCancelCheckByIdTwenty(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  6749. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 ", cancel_stock_id, orgid).Find(&stock).Error
  6750. return stock, err
  6751. }
  6752. func GetDrugWarehouseOutList(batch_number_id int64, drugId int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  6753. err = XTReadDB().Where("warehouse_info_id = ? and drug_id = ? and org_id = ? and status = 1", batch_number_id, drugId, orgid).Find(&info).Error
  6754. return info, err
  6755. }
  6756. func ModifyDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64) error {
  6757. 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
  6758. return err
  6759. }
  6760. func ModifyNewDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  6761. 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
  6762. if err != nil {
  6763. tx.Rollback()
  6764. return err
  6765. }
  6766. return err
  6767. }
  6768. func ModifyDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64) error {
  6769. 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
  6770. return err
  6771. }
  6772. func ModifyNewDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  6773. 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
  6774. if err != nil {
  6775. tx.Rollback()
  6776. return err
  6777. }
  6778. return err
  6779. }
  6780. func ModifyDrugCancelStockInfo(id int64, info *models.DrugCancelStockInfo) error {
  6781. 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
  6782. return err
  6783. }
  6784. func ReturnDrugCancelStock(cancelstock_id int64, stock models.DrugCancelStock) error {
  6785. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  6786. err = XTWriteDB().Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  6787. return err
  6788. }
  6789. func GetDrugCancelStockInfo(cancelstock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  6790. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancelstock_id, orgid).Find(&info).Error
  6791. return info, err
  6792. }
  6793. func ModifyDrugFlowByCancelId(cancelstock_id int64, drugId int64, orgid int64) error {
  6794. 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
  6795. return err
  6796. }
  6797. func ModifyDrugMaxNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  6798. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and status = 1", id, orgid).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6799. return err
  6800. }
  6801. func ModifyDrugMinNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  6802. 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
  6803. return err
  6804. }
  6805. func GetDrugInfoSix(id int64, orgid int64) (models.DrugWarehouseInfo, error) {
  6806. info := models.DrugWarehouseInfo{}
  6807. err := XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1", id, orgid).Find(&info).Error
  6808. return info, err
  6809. }
  6810. func GetPrintList(storehouse_id int64, good_name string, orgid int64, limit int64, page int64) (storeinventory []*models.XtStockInventory, err error) {
  6811. db := XTReadDB().Model(&storeinventory).Where("status = 1")
  6812. if storehouse_id > 0 {
  6813. db = db.Where("storehouse_id = ?", storehouse_id)
  6814. }
  6815. if orgid > 0 {
  6816. db = db.Where("user_org_id = ?", orgid)
  6817. }
  6818. err = XTReadDB().Preload("GoodInfo", "status = 1 and org_id = ?", orgid).Preload("WarehousingInfo", "status = 1 and org_id=?", orgid).Find(&storeinventory).Error
  6819. return storeinventory, err
  6820. }
  6821. func GetGoodDialysisOutInfo(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.AutomaticReduceDetail, error) {
  6822. detail := models.AutomaticReduceDetail{}
  6823. 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
  6824. return detail, err
  6825. }
  6826. func GetGoodDialysisOutInfoSix(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.WarehouseOutInfo, error) {
  6827. detail := models.WarehouseOutInfo{}
  6828. 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
  6829. return detail, err
  6830. }
  6831. func GetGoodSumCount(storehouse_id int64, good_id int64, orgid int64) (models.XtGoodStockCount, error) {
  6832. stock := models.XtGoodStockCount{}
  6833. err := XTReadDB().Where("storehouse_id = ? and good_id = ? and user_org_id = ? and status = 1", storehouse_id, good_id, orgid).Find(&stock).Error
  6834. return stock, err
  6835. }
  6836. func CreateGoodStockCount(stock models.XtGoodStockCount) error {
  6837. err := XTWriteDB().Create(&stock).Error
  6838. return err
  6839. }
  6840. func AddGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  6841. err := XTWriteDB().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
  6842. return err
  6843. }
  6844. func ReduceGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  6845. ut := XTWriteDB().Begin()
  6846. 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
  6847. if err != nil {
  6848. ut.Rollback()
  6849. return err
  6850. }
  6851. ut.Commit()
  6852. return err
  6853. }
  6854. func UpdateGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64) error {
  6855. err := XTWriteDB().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
  6856. return err
  6857. }
  6858. func UpdateNewGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64, tx *gorm.DB) error {
  6859. 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
  6860. if err != nil {
  6861. tx.Rollback()
  6862. return err
  6863. }
  6864. return err
  6865. }
  6866. func CreateDrugStock(stock models.XtDrugStockCount) error {
  6867. err := XTWriteDB().Create(&stock).Error
  6868. return err
  6869. }
  6870. func UpdateDrugCount(storehouse_id int64, user_org_id int64, drug_id int64, sum_in_count int64, flush_count int64) error {
  6871. ut := XTWriteDB().Begin()
  6872. 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
  6873. if err != nil {
  6874. ut.Rollback()
  6875. return err
  6876. }
  6877. 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
  6878. if err != nil {
  6879. ut.Rollback()
  6880. return err
  6881. }
  6882. ut.Commit()
  6883. return err
  6884. }
  6885. func UpdateDrugCountOne(storehouse_id int64, user_org_id int64, drug_id int64, flush_count int64) error {
  6886. ut := XTWriteDB().Begin()
  6887. 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
  6888. if err != nil {
  6889. ut.Rollback()
  6890. return err
  6891. }
  6892. ut.Commit()
  6893. return err
  6894. }
  6895. func AddDrugSumOutCount(storehouse_id int64, sum_out_count int64, user_org_id int64, drug_id int64) error {
  6896. ut := XTWriteDB().Begin()
  6897. 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
  6898. if err != nil {
  6899. ut.Rollback()
  6900. return err
  6901. }
  6902. 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
  6903. if err != nil {
  6904. ut.Rollback()
  6905. return err
  6906. }
  6907. ut.Commit()
  6908. return err
  6909. }
  6910. func ReduceDrugSumOut(storehouse_id int64, user_org_id int64, drug_id int64, sum_out_count int64) error {
  6911. ut := XTWriteDB().Begin()
  6912. 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
  6913. if err != nil {
  6914. ut.Rollback()
  6915. return err
  6916. }
  6917. ut.Commit()
  6918. return err
  6919. }
  6920. func AddGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  6921. ut := XTWriteDB().Begin()
  6922. 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
  6923. if err != nil {
  6924. ut.Rollback()
  6925. return err
  6926. }
  6927. 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
  6928. if err != nil {
  6929. ut.Rollback()
  6930. return err
  6931. }
  6932. ut.Commit()
  6933. return err
  6934. }
  6935. func ReduceGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  6936. ut := XTWriteDB().Begin()
  6937. 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
  6938. if err != nil {
  6939. ut.Rollback()
  6940. return err
  6941. }
  6942. 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
  6943. if err != nil {
  6944. ut.Rollback()
  6945. return err
  6946. }
  6947. ut.Commit()
  6948. return err
  6949. }
  6950. func GetInvetoryWarehouseInfoList(orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  6951. if orgid == 10191 || orgid == 10138 || orgid == 10278 {
  6952. 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
  6953. return info, err
  6954. } else {
  6955. 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
  6956. return info, err
  6957. }
  6958. }
  6959. func GetSettleOpenConfig(user_org_id int64) (*models.HisSettleStockConfig, error) {
  6960. config := models.HisSettleStockConfig{}
  6961. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  6962. if err == gorm.ErrRecordNotFound {
  6963. return nil, err
  6964. }
  6965. if err != nil {
  6966. return nil, err
  6967. }
  6968. return &config, nil
  6969. }
  6970. func CreateSettleOpenConfig(config models.HisSettleStockConfig) error {
  6971. err := XTWriteDB().Create(&config).Error
  6972. return err
  6973. }
  6974. func UpdateSettleOpenConfig(id int64, config models.HisSettleStockConfig) error {
  6975. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_open": config.IsOpen}).Error
  6976. return err
  6977. }
  6978. func UpdateDrugStockAutomaticReduceRecordOne(orgid int64, is_open int64) (models.DrugStockConfig, error) {
  6979. config := models.DrugStockConfig{}
  6980. err := XTWriteDB().Model(&config).Where("org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  6981. return config, err
  6982. }
  6983. func UpdatePharyConfig(orgid int64, is_open int64) (models.PharmacyConfig, error) {
  6984. stockConfig := models.PharmacyConfig{}
  6985. err := XTWriteDB().Model(&stockConfig).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  6986. return stockConfig, err
  6987. }
  6988. func UpdateSettleOpenConfigOne(id int64, is_open int64) error {
  6989. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  6990. return err
  6991. }
  6992. func UpdateSettleOpenConfigTwo(id int64, is_open int64) error {
  6993. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  6994. return err
  6995. }
  6996. func GetSettleOpenConfigByOrgId(user_org_id int64) (models.HisSettleStockConfig, error) {
  6997. config := models.HisSettleStockConfig{}
  6998. err = XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  6999. return config, nil
  7000. }
  7001. func GetGoodInformationByGoodIdOne(good_id int64) (models.GoodInformationFourty, error) {
  7002. info := models.GoodInformationFourty{}
  7003. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  7004. return info, err
  7005. }
  7006. func GetHisDocById(id int64, orgid int64) (models.HisDoctorAdvice, error) {
  7007. advice := models.HisDoctorAdvice{}
  7008. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  7009. return advice, err
  7010. }
  7011. func GetBloodDocById(id int64, orgid int64) (models.DoctorAdvice, error) {
  7012. advice := models.DoctorAdvice{}
  7013. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  7014. return advice, err
  7015. }
  7016. func FindAllSettleWarehouseOut(org_id int64, warehouse_out_time int64, is_sys int64) (models.WarehouseOut, error) {
  7017. out := models.WarehouseOut{}
  7018. 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
  7019. return out, err
  7020. }
  7021. func GetGoodOutOpenConfig(user_org_id int64) (*models.XtGoodOutConfig, error) {
  7022. config := models.XtGoodOutConfig{}
  7023. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7024. if err == gorm.ErrRecordNotFound {
  7025. return nil, err
  7026. }
  7027. if err != nil {
  7028. return nil, err
  7029. }
  7030. return &config, nil
  7031. }
  7032. func CreateGoodOutOpenConfig(good models.XtGoodOutConfig) error {
  7033. err := XTWriteDB().Create(&good).Error
  7034. return err
  7035. }
  7036. func UpdateGoodOutOpenConfig(id int64, good models.XtGoodOutConfig) error {
  7037. err := XTWriteDB().Model(&good).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"is_open": good.IsOpen}).Error
  7038. return err
  7039. }
  7040. func FindGoodOutConfigById(user_org_id int64) (models.XtGoodOutConfig, error) {
  7041. config := models.XtGoodOutConfig{}
  7042. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7043. return config, err
  7044. }
  7045. func UpdateGoodAutoConfig(user_org_id int64) error {
  7046. gobalConfig := models.GobalConfig{}
  7047. err := XTWriteDB().Model(&gobalConfig).Where("org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  7048. return err
  7049. }
  7050. func UpdateGoodOutConfig(user_org_id int64) error {
  7051. config := models.XtGoodOutConfig{}
  7052. err := XTWriteDB().Model(&config).Where("user_org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  7053. return err
  7054. }
  7055. func GetHisGoodList(patient_id int64, record_date int64, orgid int64) (info []*models.GoodHisPrescriptionProject, err error) {
  7056. 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
  7057. return info, err
  7058. }
  7059. func GetLastGoodWarehouseOutInfoByProjectId(good_id int64, patient_id int64, sys_record_time int64, project_id int64) (good []*models.WarehouseOutInfo, err error) {
  7060. 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
  7061. return good, err
  7062. }
  7063. func ModifyGoodWarehouseInfo(good_id int64, warehouse_info_id int64, orgid int64, count int64) error {
  7064. 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
  7065. return err
  7066. }
  7067. func DeleteGoodWarehouseOutInfo(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  7068. 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
  7069. return err
  7070. }
  7071. func DeleteGoodFlow(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  7072. 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
  7073. return err
  7074. }
  7075. func FindPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  7076. out := models.WarehouseOut{}
  7077. 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
  7078. return out, err
  7079. }
  7080. func FindNewPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  7081. out := models.WarehouseOut{}
  7082. 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
  7083. if err != gorm.ErrRecordNotFound {
  7084. if err != nil {
  7085. tx.Rollback()
  7086. return out, err
  7087. }
  7088. }
  7089. return out, err
  7090. }
  7091. func FindLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  7092. out := models.WarehouseOut{}
  7093. 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
  7094. return out, err
  7095. }
  7096. func FindNewLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  7097. out := models.WarehouseOut{}
  7098. 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
  7099. if err != gorm.ErrRecordNotFound {
  7100. if err != nil {
  7101. tx.Rollback()
  7102. return out, err
  7103. }
  7104. }
  7105. return out, err
  7106. }
  7107. func GetGoodTypeObj(goodid int64, org_id int64) (models.GoodsType, error) {
  7108. goodsType := models.GoodsType{}
  7109. err := XTReadDB().Where("good_id = ? and org_id = ? and status = 1", goodid, org_id).Find(&goodsType).Error
  7110. return goodsType, err
  7111. }
  7112. func GetAllGoodTypeName(good_type_id int64, org_id int64) (good_type_name string) {
  7113. goodsTypeList, _ := GetAllGoodType(org_id)
  7114. for _, item := range goodsTypeList {
  7115. if good_type_id == item.ID {
  7116. good_type_name = item.TypeName
  7117. }
  7118. }
  7119. return good_type_name
  7120. }
  7121. func UpdateGoodTypeName(good_id int64, good_type_name string) (models.GoodInformation, error) {
  7122. information := models.GoodInformation{}
  7123. err := XTWriteDB().Model(&information).Where("id = ? and status =1", good_id).Updates(map[string]interface{}{"good_type_name": good_type_name}).Error
  7124. return information, err
  7125. }
  7126. func GetSencondeGoodWarehouseInfo(good_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  7127. err = XTReadDB().Where("good_id = ? and org_id= ? and status= 1 and is_check = 1", good_id, org_id).Find(&info).Error
  7128. return info, err
  7129. }
  7130. func UpdateGoodSumCountSeven(sum_count int64, good_id int64, orgid int64) error {
  7131. ut := writeDb.Begin()
  7132. 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
  7133. if err != nil {
  7134. ut.Rollback()
  7135. return err
  7136. }
  7137. ut.Commit()
  7138. return err
  7139. }
  7140. func UpdateNewGoodSumCountSeven(sum_count int64, good_id int64, orgid int64, tx *gorm.DB) error {
  7141. 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
  7142. if err != nil {
  7143. tx.Rollback()
  7144. return err
  7145. }
  7146. return err
  7147. }
  7148. func GetWarehouseOutInfoIsExistOTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  7149. info := models.WarehouseOutInfo{}
  7150. var err error
  7151. 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
  7152. if err == gorm.ErrRecordNotFound {
  7153. return nil, err
  7154. }
  7155. if err != nil {
  7156. return nil, err
  7157. }
  7158. return &info, nil
  7159. }
  7160. func GetWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  7161. info := models.WarehouseOutInfo{}
  7162. 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
  7163. return info, err
  7164. }
  7165. func GetNewWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  7166. info := models.WarehouseOutInfo{}
  7167. 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
  7168. if err != gorm.ErrRecordNotFound {
  7169. if err != nil {
  7170. tx.Rollback()
  7171. return info, err
  7172. }
  7173. }
  7174. return info, err
  7175. }
  7176. func GetWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (models.WarehouseOutInfo, error) {
  7177. info := models.WarehouseOutInfo{}
  7178. 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
  7179. return info, err
  7180. }
  7181. func GetNewWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  7182. info := models.WarehouseOutInfo{}
  7183. 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
  7184. if err != gorm.ErrRecordNotFound {
  7185. if err != nil {
  7186. tx.Rollback()
  7187. return info, err
  7188. }
  7189. }
  7190. return info, err
  7191. }
  7192. func GetNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (*models.WarehouseOutInfo, error) {
  7193. info := models.WarehouseOutInfo{}
  7194. var err error
  7195. 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
  7196. if err == gorm.ErrRecordNotFound {
  7197. return nil, err
  7198. }
  7199. if err != nil {
  7200. return nil, err
  7201. }
  7202. return &info, nil
  7203. }
  7204. func GetNewNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (*models.WarehouseOutInfo, error) {
  7205. info := models.WarehouseOutInfo{}
  7206. var err error
  7207. 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
  7208. if err != gorm.ErrRecordNotFound {
  7209. if err != nil {
  7210. tx.Rollback()
  7211. return &info, err
  7212. }
  7213. }
  7214. return &info, err
  7215. }
  7216. func GetNewGoodWarehouseInfo(startime int64, endtime int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.GoodWarehousingInfo, err error) {
  7217. db := XTReadDB().Model(&info).Where("status = 1")
  7218. if startime > 0 {
  7219. db = db.Where("ctime>=?", startime)
  7220. }
  7221. if endtime > 0 {
  7222. db = db.Where("ctime<=?", endtime)
  7223. }
  7224. if orgid > 0 {
  7225. db = db.Where("org_id = ?", orgid)
  7226. }
  7227. if len(good_id) > 0 {
  7228. db = db.Where("good_id in(?)", good_id)
  7229. }
  7230. if storehouse_id > 0 {
  7231. db = db.Where("storehouse_id = ?", storehouse_id)
  7232. }
  7233. err = db.Preload("GoodInfo", "org_id = ? and status= 1", orgid).Find(&info).Error
  7234. return info, err
  7235. }
  7236. func GetNewWarehouseOutInfoList(start_time int64, end_time int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.WarehouseOutInfoNight, err error) {
  7237. db := XTReadDB().Model(&info).Where("status= 1")
  7238. if start_time > 0 {
  7239. db = db.Where("ctime>=?", start_time)
  7240. }
  7241. if end_time > 0 {
  7242. db = db.Where("ctime <=?", end_time)
  7243. }
  7244. if orgid > 0 {
  7245. db = db.Where("org_id = ?", orgid)
  7246. }
  7247. if len(good_id) > 0 {
  7248. db = db.Where("good_id in(?)", good_id)
  7249. }
  7250. if storehouse_id > 0 {
  7251. db = db.Where("storehouse_id = ?", storehouse_id)
  7252. }
  7253. err = db.Preload("GoodInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  7254. return info, err
  7255. }
  7256. func GetGoodOutOpenConfigOne(user_org_id int64) (models.XtGoodOutConfig, error) {
  7257. config := models.XtGoodOutConfig{}
  7258. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7259. return config, err
  7260. }
  7261. func UpdateDeleteAutoGood(good_id int64, record_date int64, patient_id int64) error {
  7262. 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
  7263. 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
  7264. return err
  7265. }
  7266. func UpdateWarehouseInfoMap(list []*models.WarehousingInfo, warehousing_info_id int64, orgId int64) error {
  7267. tx := XTWriteDB().Begin()
  7268. for _, item := range list {
  7269. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", item.WarehousingCount)).Error
  7270. if err != nil {
  7271. tx.Rollback()
  7272. return err
  7273. }
  7274. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).Update(map[string]interface{}{"is_check": 2}).Error
  7275. if err != nil {
  7276. tx.Rollback()
  7277. return err
  7278. }
  7279. err = tx.Model(models.VmStockFlow{}).Where("warehousing_detail_id = ?", item.ID).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  7280. if err != nil {
  7281. tx.Rollback()
  7282. return err
  7283. }
  7284. var goodList []*models.WarehousingInfo
  7285. 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
  7286. var flush_count int64
  7287. for _, it := range goodList {
  7288. flush_count += it.StockCount
  7289. }
  7290. 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
  7291. if err != nil {
  7292. tx.Rollback()
  7293. return err
  7294. }
  7295. 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
  7296. if err != nil {
  7297. tx.Rollback()
  7298. return err
  7299. }
  7300. storeHouseConfig := models.XtStorehouseConfig{}
  7301. err = tx.Where("user_org_id = ? and status = 1", orgId).First(&storeHouseConfig).Error
  7302. var stockList []*models.WarehousingInfo
  7303. //查询该耗材剩余多少库存
  7304. 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
  7305. var sum_count int64
  7306. var sum_in_count int64
  7307. for _, it := range stockList {
  7308. sum_count += it.StockCount
  7309. sum_in_count += it.WarehousingCount
  7310. }
  7311. 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
  7312. if err != nil {
  7313. tx.Rollback()
  7314. return err
  7315. }
  7316. 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
  7317. if err != nil {
  7318. tx.Rollback()
  7319. return err
  7320. }
  7321. }
  7322. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  7323. if err != nil {
  7324. tx.Rollback()
  7325. return err
  7326. }
  7327. err = tx.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  7328. tx.Commit()
  7329. return err
  7330. }
  7331. func FindFirstWarehousingInfoByStockThree(good_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  7332. if storehouse_id == 0 {
  7333. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime").First(&info).Error
  7334. }
  7335. if storehouse_id > 0 {
  7336. 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
  7337. }
  7338. return info, err
  7339. }
  7340. func GetHisPrescriptionNumberList(project_id int64, record_date int64, orgid int64, patient_id int64) (list []*models.HisPrescriptionProject, err error) {
  7341. 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
  7342. return list, err
  7343. }
  7344. func GetWarehouseOutnfoByProjectId(project_id int64, record_date int64, orgid int64, patient_id int64) (infor []*models.WarehouseOutInfo, err error) {
  7345. 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
  7346. return infor, err
  7347. }
  7348. func UpdateWarehouseInfoByPatientId(project_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfo, error) {
  7349. info := models.WarehouseOutInfo{}
  7350. 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
  7351. return info, err
  7352. }
  7353. func UpdateWarehouseInfoByProject(good_id int64, id int64, user_org_id int64, count int64) (models.WarehousingInfo, error) {
  7354. info := models.WarehousingInfo{}
  7355. 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
  7356. return info, err
  7357. }
  7358. func GetHisPrescripionProjectList(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  7359. err = XTReadDB().Where("project_id=? and status=1 and type =3 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  7360. return list, err
  7361. }
  7362. func GetHisPrescripionProjectListOne(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  7363. err = XTReadDB().Where("project_id=? and status=1 and type =2 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  7364. return list, err
  7365. }
  7366. func UpdateHisPrescriptionProjectList(id int64, price float64) (models.HisPrescriptionProjectTemplateL, error) {
  7367. templateL := models.HisPrescriptionProjectTemplateL{}
  7368. err := XTWriteDB().Model(&templateL).Where("id =? and status=1", id).Updates(map[string]interface{}{"price": price}).Error
  7369. return templateL, err
  7370. }