stock_service.go 412KB

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