stock_service.go 365KB

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