stock_service.go 369KB

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