stock_service.go 413KB

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