stock_service.go 450KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "XT_New/models"
  9. "github.com/jinzhu/gorm"
  10. )
  11. func AddSigleDealer(dealer *models.Dealer) (error, *models.Dealer) {
  12. err := writeDb.Create(&dealer).Error
  13. return err, dealer
  14. }
  15. func ModifyDealer(dealer *models.Dealer) error {
  16. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", dealer.ID).Updates(map[string]interface{}{
  17. "dealer_name": dealer.DealerName,
  18. "contact": dealer.Contact,
  19. "contact_phone": dealer.ContactPhone,
  20. "platform_number": dealer.PlatformNumber,
  21. "email": dealer.Email,
  22. "contact_address": dealer.ContactAddress,
  23. "modifier": dealer.Modifier,
  24. "mtime": time.Now().Unix(),
  25. "remark": dealer.Remark,
  26. "wubi": dealer.WuBi,
  27. "pinyin": dealer.PinYin,
  28. }).Error
  29. return err
  30. }
  31. func FindAllDealerTotal(orgId int64) (total int64) {
  32. db := readDb.Model(&models.Dealer{})
  33. db = db.Where("org_id = ? ", orgId).Count(&total)
  34. return
  35. }
  36. // 获取经销商的名称
  37. func FindAllDealerName(orgId int64) (tmp []*models.Dealer, err error) {
  38. err = readDb.Model(&models.Dealer{}).Where("org_id = ? and status = 1", orgId).Find(&tmp).Error
  39. return
  40. }
  41. func FindAllDealerList(orgId int64, page int64, limit, key int64) (list []*models.Dealer, total int64, err error) {
  42. offset := (page - 1) * limit
  43. db := readDb.Model(&models.Dealer{})
  44. db = db.Where("org_id = ? AND status = 1", orgId)
  45. if key != 0 {
  46. db = db.Where("id = ? ", key)
  47. offset = 0
  48. limit = 10
  49. }
  50. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  51. return
  52. }
  53. func DeleteDealerById(id int64) error {
  54. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  55. return err
  56. }
  57. func FindDealerById(id int64) (*models.Dealer, error) {
  58. dealer := &models.Dealer{}
  59. err := readDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  60. return dealer, err
  61. }
  62. func AddSigleManufacturer(manufacturer *models.Manufacturer) (error, *models.Manufacturer) {
  63. err := writeDb.Create(&manufacturer).Error
  64. return err, manufacturer
  65. }
  66. func ModifyManufacturer(manufacturer *models.Manufacturer) error {
  67. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", manufacturer.ID).Updates(map[string]interface{}{
  68. "manufacturer_name": manufacturer.ManufacturerName,
  69. "contact": manufacturer.Contact,
  70. "contact_phone": manufacturer.ContactPhone,
  71. "platform_number": manufacturer.PlatformNumber,
  72. "email": manufacturer.Email,
  73. "contact_address": manufacturer.ContactAddress,
  74. "modifier": manufacturer.Modifier,
  75. "mtime": time.Now().Unix(),
  76. "remark": manufacturer.Remark,
  77. "wubi": manufacturer.WuBi,
  78. "pinyin": manufacturer.PinYin,
  79. }).Error
  80. return err
  81. }
  82. // 获取生产厂商名称
  83. func FindAllManufacturerName(orgid int64) (tmp []*models.Manufacturer, err error) {
  84. err = readDb.Model(&models.Manufacturer{}).Where("org_id = ? and status = 1", orgid).Find(&tmp).Error
  85. return
  86. }
  87. func FindAllManufacturerList(orgId int64, page int64, limit, key int64) (list []*models.Manufacturer, total int64, err error) {
  88. offset := (page - 1) * limit
  89. db := readDb.Model(&models.Manufacturer{})
  90. db = db.Where("org_id = ? AND status = 1", orgId)
  91. if key != 0 {
  92. db = db.Where("id = ?", key)
  93. offset = 0
  94. limit = 10
  95. }
  96. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  97. return
  98. }
  99. func DeleteManufacturerById(id int64) error {
  100. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  101. return err
  102. }
  103. func FindManufacturerById(id int64) (*models.Manufacturer, error) {
  104. dealer := &models.Manufacturer{}
  105. err := readDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  106. return dealer, err
  107. }
  108. func FindManufacturerTotal(orgId int64) (total int64) {
  109. db := readDb.Model(&models.Manufacturer{})
  110. db = db.Where("org_id = ?", orgId).Count(&total)
  111. return
  112. }
  113. func FindAllDealer(orgId int64) (dealers []*models.Dealer, err error) {
  114. err = readDb.Model(&models.Dealer{}).Where("org_id = ? AND status = 1", orgId).Find(&dealers).Error
  115. return dealers, err
  116. }
  117. func FindAllManufacturer(orgId int64) (manufacturer []*models.Manufacturer, err error) {
  118. err = readDb.Model(&models.Manufacturer{}).Where("org_id = ? AND status = 1", orgId).Find(&manufacturer).Error
  119. return manufacturer, err
  120. }
  121. func FindAllGoodTypeTotal(orgId int64) (total int64) {
  122. db := readDb.Model(&models.GoodsType{})
  123. db = db.Where("org_id = ?", orgId).Count(&total)
  124. return
  125. }
  126. func GetGoodsTypeIsExist(orgid int64) (*models.GoodsType, error) {
  127. goodsType := models.GoodsType{}
  128. err := XTReadDB().Where("org_id = ? and stock_attribute = 2 and status =1", orgid).Find(&goodsType).Error
  129. if err == gorm.ErrRecordNotFound {
  130. return nil, err
  131. }
  132. if err != nil {
  133. return nil, err
  134. }
  135. return &goodsType, nil
  136. }
  137. func GetGoodsTypeIsExistOne(orgid int64) (*models.GoodsType, error) {
  138. goodsType := models.GoodsType{}
  139. err := XTReadDB().Where("org_id = ? and stock_attribute = 3 and status = 1", orgid).Find(&goodsType).Error
  140. if err == gorm.ErrRecordNotFound {
  141. return nil, err
  142. }
  143. if err != nil {
  144. return nil, err
  145. }
  146. return &goodsType, nil
  147. }
  148. func AddSigleGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  149. err := writeDb.Create(&goodType).Error
  150. return err, goodType
  151. }
  152. func ModifyGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  153. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", goodType.ID).Updates(map[string]interface{}{
  154. "modifier": goodType.Modifier,
  155. "mtime": time.Now().Unix(),
  156. "remark": goodType.Remark,
  157. "type_name": goodType.TypeName,
  158. "out_stock": goodType.OutStock,
  159. "stock_attribute": goodType.StockAttribute,
  160. }).Error
  161. return err, goodType
  162. }
  163. func FindAllGoodTypeList(orgId int64, page int64, limit int64, keyword string) (list []*models.GoodsType, total int64, err error) {
  164. offset := (page - 1) * limit
  165. db := readDb.Model(&models.GoodsType{})
  166. db = db.Where("org_id=? AND status = 1", orgId)
  167. if len(keyword) > 0 {
  168. likeKey := "%" + keyword + "%"
  169. db = db.Where("type_code LIKE ? OR type_name LIKE ?", likeKey, likeKey)
  170. }
  171. err = db.Count(&total).Offset(offset).Limit(limit).Order("type desc, number desc,ctime desc").Find(&list).Error
  172. return
  173. }
  174. func DeleteGoodTypeById(id int64, operateId int64) error {
  175. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  176. writeDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId})
  177. return err
  178. }
  179. func FindGoodTypeById(id int64) (*models.GoodsType, error) {
  180. goodType := &models.GoodsType{}
  181. err := readDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).First(&goodType).Error
  182. return goodType, err
  183. }
  184. func FindAllGoodInfoTotal(orgId int64) (total int64) {
  185. db := readDb.Model(&models.GoodInfo{})
  186. db = db.Where("org_id = ?", orgId).Count(&total)
  187. return
  188. }
  189. func AddSigleGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  190. err := writeDb.Create(&goodInfo).Error
  191. return err, goodInfo
  192. }
  193. func ModifyGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  194. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", goodInfo.ID).Updates(map[string]interface{}{
  195. "good_type_id": goodInfo.GoodTypeId,
  196. "modifier": goodInfo.Modifier,
  197. "mtime": time.Now().Unix(),
  198. "remark": goodInfo.Remark,
  199. "specification_name": goodInfo.SpecificationName,
  200. "good_unit": goodInfo.GoodUnit,
  201. "buy_price": goodInfo.BuyPrice,
  202. "sell_price": goodInfo.SellPrice,
  203. "manufacturer": goodInfo.Manufacturer,
  204. "dealer": goodInfo.Dealer,
  205. "expiry_date_warn_day_count": goodInfo.ExpiryDateWarnDayCount,
  206. "stock_warn_count": goodInfo.StockWarnCount,
  207. "is_reuse": goodInfo.IsReuse,
  208. "good_name": goodInfo.GoodName,
  209. "pinyin": goodInfo.Pinyin,
  210. "wubi": goodInfo.Wubi,
  211. "good_kind": goodInfo.GoodKind,
  212. "medical_insurance_level": goodInfo.MedicalInsuranceLevel,
  213. "retail_price": goodInfo.PackingPrice,
  214. "medical_insurance_number": goodInfo.MedicalInsuranceNumber,
  215. "is_special_diseases": goodInfo.IsSpecialDiseases,
  216. "is_record": goodInfo.IsRecord,
  217. "statistics_category": goodInfo.StatisticsCategory,
  218. "good_status": goodInfo.GoodStatus,
  219. "default_count": goodInfo.DefaultCount,
  220. "sign": goodInfo.Sign,
  221. "is_default": goodInfo.IsDefault,
  222. "is_charge_use": goodInfo.IsChargeUse,
  223. "is_charge_predict": goodInfo.IsChargePredict,
  224. "is_statistics_work": goodInfo.IsStatisticsWork,
  225. "sort": goodInfo.Sort,
  226. "is_doctor_use": goodInfo.IsDoctorUse,
  227. "agent": goodInfo.Agent,
  228. "good_number": goodInfo.GoodNumber,
  229. "social_security_directory_code": goodInfo.SocialSecurityDirectoryCode,
  230. "special_medical": goodInfo.SpecialMedical,
  231. "production_type": goodInfo.ProductionType,
  232. "min_number": goodInfo.MinNumber,
  233. "packing_unit": goodInfo.PackingUnit,
  234. "packing_price": goodInfo.PackingPrice,
  235. "default_count_unit": goodInfo.DefaultCountUnit,
  236. "min_unit": goodInfo.MinUnit,
  237. "register_number": goodInfo.RegisterNumber,
  238. "provinces_code": goodInfo.ProvincesCode,
  239. "is_user": goodInfo.IsUser,
  240. "number": goodInfo.Number,
  241. "is_warehouse": goodInfo.IsWarehouse,
  242. "bbx01": goodInfo.Bbx01,
  243. "bby01": goodInfo.Bby01,
  244. "zuobiao_id": goodInfo.Bbx01,
  245. "first_letter": goodInfo.FirstLetter,
  246. "is_show": goodInfo.IsShow,
  247. "is_zero_flag": goodInfo.IsZeroFlag,
  248. }).Error
  249. writeDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  250. writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  251. writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  252. writeDb.Model(&models.DialysisBeforePrepare{}).Where("good_id = ? AND user_org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  253. writeDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  254. writeDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  255. return err, goodInfo
  256. }
  257. func FindGoodInfoList(orgId int64, page int64, limit int64, keyword string, is_use int64, is_charge int64, good_kind int64, is_mark int64, manufacturer int64, is_type int64) (list []*models.GoodInfo, total int64, err error) {
  258. offset := (page - 1) * limit
  259. db := readDb.Model(&models.GoodInfo{})
  260. db = db.Where("org_id = ? AND status = 1", orgId)
  261. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  262. if len(keyword) > 0 {
  263. likeKey := "%" + keyword + "%"
  264. db = db.Where("good_code LIKE ? OR good_name LIKE ?", likeKey, likeKey)
  265. if is_use > 0 {
  266. if is_use == 2 {
  267. db = db.Where("find_in_set('停用',good_status) = 0")
  268. } else {
  269. db = db.Where("find_in_set('停用',good_status) > 0")
  270. }
  271. }
  272. if is_charge > 0 {
  273. if is_charge == 1 {
  274. db = db.Where("find_in_set('收费',good_status) > 0")
  275. } else {
  276. db = db.Where("find_in_set('收费',good_status) = 0")
  277. }
  278. }
  279. if good_kind > 0 {
  280. db = db.Where(" good_kind = ?", good_kind)
  281. }
  282. if is_mark > 0 {
  283. db = db.Where("is_mark = ?", is_mark)
  284. }
  285. if manufacturer > 0 {
  286. db = db.Where("manufacturer = ?", manufacturer)
  287. }
  288. if is_type > 0 {
  289. db = db.Where("good_type_id = ?", is_type)
  290. }
  291. } else {
  292. if is_use > 0 {
  293. if is_use == 2 {
  294. db = db.Where("find_in_set('停用',good_status) = 0")
  295. } else {
  296. db = db.Where("find_in_set('停用',good_status) > 0")
  297. }
  298. }
  299. if is_charge > 0 {
  300. if is_charge == 1 {
  301. db = db.Where("find_in_set('收费',good_status) > 0")
  302. } else {
  303. db = db.Where("find_in_set('收费',good_status) = 0")
  304. }
  305. }
  306. if good_kind > 0 {
  307. db = db.Where(" good_kind = ?", good_kind)
  308. }
  309. if is_mark > 0 {
  310. db = db.Where("is_mark = ?", is_mark)
  311. }
  312. if manufacturer > 0 {
  313. db = db.Where("manufacturer = ?", manufacturer)
  314. }
  315. if is_type > 0 {
  316. db = db.Where("good_type_id = ?", is_type)
  317. }
  318. }
  319. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  320. return
  321. }
  322. func DeleteGoodInfoById(id int64, operateId int64) error {
  323. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  324. return err
  325. }
  326. func FindGoodInfoByGoodId(id int64) (list []*models.GoodInfo, err error) {
  327. err = readDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  328. return list, err
  329. }
  330. func FindStockOutDetailGoodInfoByGoodId(id int64) (list []*models.AutomaticReduceDetail, err error) {
  331. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  332. return list, err
  333. }
  334. func FindGoodInfoById(id int64) (*models.GoodInfo, error) {
  335. goodInfo := &models.GoodInfo{}
  336. err := readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  337. return goodInfo, err
  338. }
  339. func FindAllGoodType(org_id int64) (goodType []*models.GoodsType, err error) {
  340. err = XTReadDB().Model(&models.GoodsType{}).Where("org_id = ? AND status = 1", org_id).Find(&goodType).Error
  341. return goodType, err
  342. }
  343. func FindAllGoodTypeOne(org_id int64) (goodType []*models.GoodsType, err error) {
  344. err = readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 and out_stock = 1", org_id).Find(&goodType).Error
  345. return goodType, err
  346. }
  347. func FindAllGoodInfo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  348. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1 and is_warehouse = 1 and find_in_set('停用',good_status) = 0", org_id).Find(&goodInfo).Error
  349. return goodInfo, err
  350. }
  351. func FindAllGoodInfoTwo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  352. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id).Find(&goodInfo).Error
  353. return goodInfo, err
  354. }
  355. func FindAllGoodInfoThree(org_id int64) (goodInfo []*models.XtGoodInformationFourty, err error) {
  356. err = XTReadDB().Model(&models.XtGoodInformationFourty{}).Where("org_id = ? AND status = 1 and find_in_set('停用',good_status) = 0", org_id).Find(&goodInfo).Error
  357. return goodInfo, err
  358. }
  359. func FindAllWarehouseTotal(org_id int64) (total int64, err error) {
  360. err = readDb.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  361. return total, err
  362. }
  363. func FindNewAllWarehouseTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  364. err = tx.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  365. if err != gorm.ErrRecordNotFound {
  366. if err != nil {
  367. tx.Rollback()
  368. return total, err
  369. }
  370. }
  371. return total, err
  372. }
  373. func FindAllWarehouseTotalOne(org_id int64) (total int64, err error) {
  374. err = readDb.Model(&models.XtDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  375. return total, err
  376. }
  377. func FindNewAllWarehouseTotalOne(org_id int64, tx *gorm.DB) (total int64, err error) {
  378. err = tx.Model(&models.XtDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  379. if err != gorm.ErrRecordNotFound {
  380. if err != nil {
  381. tx.Rollback()
  382. return total, err
  383. }
  384. }
  385. return total, err
  386. }
  387. func AddSigleWarehouse(warehouse *models.Warehousing) error {
  388. err := writeDb.Create(&warehouse).Error
  389. return err
  390. }
  391. func AddNewSigleWarehouse(warehouse *models.Warehousing, tx *gorm.DB) error {
  392. err := writeDb.Create(&warehouse).Error
  393. if err != nil {
  394. tx.Rollback()
  395. return err
  396. }
  397. return err
  398. }
  399. func UpdateWarehouse(id int64, storehouse_id int64) error {
  400. err := XTWriteDB().Model(&models.Warehousing{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  401. return err
  402. }
  403. func UpdateNewWarehouse(id int64, storehouse_id int64, tx *gorm.DB) error {
  404. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  405. if err != nil {
  406. tx.Rollback()
  407. return err
  408. }
  409. return err
  410. }
  411. func AddSigleDrugWarehouse(warehouse *models.DrugWarehouse) error {
  412. err := writeDb.Create(&warehouse).Error
  413. return err
  414. }
  415. func AddNewSigleDrugWarehouse(warehouse *models.DrugWarehouse, tx *gorm.DB) error {
  416. err := tx.Create(&warehouse).Error
  417. if err != nil {
  418. tx.Rollback()
  419. return err
  420. }
  421. return err
  422. }
  423. func UpdateSigleDrugWarehouse(id int64, storehosue_id int64) error {
  424. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehosue_id}).Error
  425. return err
  426. }
  427. func UpdateNewSigleDrugWarehouse(id int64, storehosue_id int64, tx *gorm.DB) error {
  428. err := tx.Model(&models.DrugWarehouse{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehosue_id}).Error
  429. if err != nil {
  430. tx.Rollback()
  431. return err
  432. }
  433. return err
  434. }
  435. func AddSigleSalesReturn(salesReturn *models.SalesReturn) error {
  436. err := writeDb.Create(&salesReturn).Error
  437. return err
  438. }
  439. func AddSigleDrugSalesReturn(salesReturn *models.DrugSalesReturn) error {
  440. err := writeDb.Create(&salesReturn).Error
  441. return err
  442. }
  443. func UpDateGoodReturnStatus(ids []string) {
  444. writeDb.Model(&models.WarehousingInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_return": 1})
  445. }
  446. func UpDateCanCelStockStatus(ids []string) {
  447. writeDb.Model(&models.WarehouseOutInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_cancel": 1})
  448. }
  449. func CreateWarehousingInfo(warehousingInfo []*models.WarehousingInfo) (err error) {
  450. if len(warehousingInfo) > 0 {
  451. utx := writeDb.Begin()
  452. if len(warehousingInfo) > 0 {
  453. thisSQL := "INSERT INTO xt_warehouse_info (warehousing_id, good_id, good_type_id, number, product_date,expiry_date,warehousing_count,price,total_price,dealer,manufacturer,remark,ctime,mtime,status,org_id,warehousing_order,type,stock_count,license_number,packing_price,storehouse_id,good_code) VALUES "
  454. insertParams := make([]string, 0)
  455. insertData := make([]interface{}, 0)
  456. for _, info := range warehousingInfo {
  457. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  458. insertData = append(insertData, info.WarehousingId)
  459. insertData = append(insertData, info.GoodId)
  460. insertData = append(insertData, info.GoodTypeId)
  461. insertData = append(insertData, info.Number)
  462. insertData = append(insertData, info.ProductDate)
  463. insertData = append(insertData, info.ExpiryDate)
  464. insertData = append(insertData, info.WarehousingCount)
  465. insertData = append(insertData, info.Price)
  466. insertData = append(insertData, info.TotalPrice)
  467. insertData = append(insertData, info.Dealer)
  468. insertData = append(insertData, info.Manufacturer)
  469. insertData = append(insertData, info.Remark)
  470. insertData = append(insertData, info.Ctime)
  471. insertData = append(insertData, info.Mtime)
  472. insertData = append(insertData, info.Status)
  473. insertData = append(insertData, info.OrgId)
  474. insertData = append(insertData, info.WarehousingOrder)
  475. insertData = append(insertData, info.Type)
  476. insertData = append(insertData, info.WarehousingCount)
  477. insertData = append(insertData, info.LicenseNumber)
  478. insertData = append(insertData, info.PackingPrice)
  479. insertData = append(insertData, info.StorehouseId)
  480. insertData = append(insertData, info.GoodCode)
  481. }
  482. thisSQL += strings.Join(insertParams, ", ")
  483. err = utx.Exec(thisSQL, insertData...).Error
  484. if err != nil {
  485. utx.Rollback()
  486. return
  487. }
  488. }
  489. utx.Commit()
  490. }
  491. return
  492. }
  493. func CreateNewWarehousingInfo(warehousingInfo []*models.WarehousingInfo, tx *gorm.DB) (err error) {
  494. if len(warehousingInfo) > 0 {
  495. if len(warehousingInfo) > 0 {
  496. thisSQL := "INSERT INTO xt_warehouse_info (warehousing_id, good_id, good_type_id, number, product_date,expiry_date,warehousing_count,price,total_price,dealer,manufacturer,remark,ctime,mtime,status,org_id,warehousing_order,type,stock_count,license_number,packing_price,storehouse_id,good_code) VALUES "
  497. insertParams := make([]string, 0)
  498. insertData := make([]interface{}, 0)
  499. for _, info := range warehousingInfo {
  500. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  501. insertData = append(insertData, info.WarehousingId)
  502. insertData = append(insertData, info.GoodId)
  503. insertData = append(insertData, info.GoodTypeId)
  504. insertData = append(insertData, info.Number)
  505. insertData = append(insertData, info.ProductDate)
  506. insertData = append(insertData, info.ExpiryDate)
  507. insertData = append(insertData, info.WarehousingCount)
  508. insertData = append(insertData, info.Price)
  509. insertData = append(insertData, info.TotalPrice)
  510. insertData = append(insertData, info.Dealer)
  511. insertData = append(insertData, info.Manufacturer)
  512. insertData = append(insertData, info.Remark)
  513. insertData = append(insertData, info.Ctime)
  514. insertData = append(insertData, info.Mtime)
  515. insertData = append(insertData, info.Status)
  516. insertData = append(insertData, info.OrgId)
  517. insertData = append(insertData, info.WarehousingOrder)
  518. insertData = append(insertData, info.Type)
  519. insertData = append(insertData, info.WarehousingCount)
  520. insertData = append(insertData, info.LicenseNumber)
  521. insertData = append(insertData, info.PackingPrice)
  522. insertData = append(insertData, info.StorehouseId)
  523. insertData = append(insertData, info.GoodCode)
  524. }
  525. thisSQL += strings.Join(insertParams, ", ")
  526. err = tx.Exec(thisSQL, insertData...).Error
  527. if err != nil {
  528. tx.Rollback()
  529. return
  530. }
  531. }
  532. }
  533. return
  534. }
  535. func CreateDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  536. if len(warehousingInfo) > 0 {
  537. utx := writeDb.Begin()
  538. if len(warehousingInfo) > 0 {
  539. 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 "
  540. insertParams := make([]string, 0)
  541. insertData := make([]interface{}, 0)
  542. for _, info := range warehousingInfo {
  543. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  544. insertData = append(insertData, info.WarehousingId)
  545. insertData = append(insertData, info.DrugId)
  546. insertData = append(insertData, info.Number)
  547. insertData = append(insertData, info.ProductDate)
  548. insertData = append(insertData, info.ExpiryDate)
  549. insertData = append(insertData, info.WarehousingCount)
  550. insertData = append(insertData, info.Price)
  551. insertData = append(insertData, info.TotalPrice)
  552. insertData = append(insertData, info.Dealer)
  553. insertData = append(insertData, info.Manufacturer)
  554. insertData = append(insertData, info.Remark)
  555. insertData = append(insertData, info.Ctime)
  556. insertData = append(insertData, info.Mtime)
  557. insertData = append(insertData, info.Status)
  558. insertData = append(insertData, info.OrgId)
  559. insertData = append(insertData, info.WarehousingOrder)
  560. insertData = append(insertData, info.Type)
  561. insertData = append(insertData, info.RetailPrice)
  562. insertData = append(insertData, info.RetailTotalPrice)
  563. insertData = append(insertData, info.StockMaxNumber)
  564. insertData = append(insertData, info.MaxUnit)
  565. insertData = append(insertData, info.MinUnit)
  566. insertData = append(insertData, info.BatchNumber)
  567. insertData = append(insertData, info.StockMinNumber)
  568. insertData = append(insertData, info.StorehouseId)
  569. }
  570. thisSQL += strings.Join(insertParams, ", ")
  571. err = utx.Exec(thisSQL, insertData...).Error
  572. if err != nil {
  573. utx.Rollback()
  574. return
  575. }
  576. }
  577. utx.Commit()
  578. }
  579. return
  580. }
  581. func CreateNewDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo, tx *gorm.DB) (err error) {
  582. if len(warehousingInfo) > 0 {
  583. if len(warehousingInfo) > 0 {
  584. 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,drug_code) VALUES "
  585. insertParams := make([]string, 0)
  586. insertData := make([]interface{}, 0)
  587. for _, info := range warehousingInfo {
  588. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  589. insertData = append(insertData, info.WarehousingId)
  590. insertData = append(insertData, info.DrugId)
  591. insertData = append(insertData, info.Number)
  592. insertData = append(insertData, info.ProductDate)
  593. insertData = append(insertData, info.ExpiryDate)
  594. insertData = append(insertData, info.WarehousingCount)
  595. insertData = append(insertData, info.Price)
  596. insertData = append(insertData, info.TotalPrice)
  597. insertData = append(insertData, info.Dealer)
  598. insertData = append(insertData, info.Manufacturer)
  599. insertData = append(insertData, info.Remark)
  600. insertData = append(insertData, info.Ctime)
  601. insertData = append(insertData, info.Mtime)
  602. insertData = append(insertData, info.Status)
  603. insertData = append(insertData, info.OrgId)
  604. insertData = append(insertData, info.WarehousingOrder)
  605. insertData = append(insertData, info.Type)
  606. insertData = append(insertData, info.RetailPrice)
  607. insertData = append(insertData, info.RetailTotalPrice)
  608. insertData = append(insertData, info.StockMaxNumber)
  609. insertData = append(insertData, info.MaxUnit)
  610. insertData = append(insertData, info.MinUnit)
  611. insertData = append(insertData, info.BatchNumber)
  612. insertData = append(insertData, info.StockMinNumber)
  613. insertData = append(insertData, info.StorehouseId)
  614. insertData = append(insertData, info.DrugCode)
  615. }
  616. thisSQL += strings.Join(insertParams, ", ")
  617. err = tx.Exec(thisSQL, insertData...).Error
  618. if err != nil {
  619. tx.Rollback()
  620. return
  621. }
  622. }
  623. }
  624. return
  625. }
  626. func CreateDrugWarehousingInfoSix(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  627. if len(warehousingInfo) > 0 {
  628. utx := writeDb.Begin()
  629. if len(warehousingInfo) > 0 {
  630. 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 "
  631. insertParams := make([]string, 0)
  632. insertData := make([]interface{}, 0)
  633. for _, info := range warehousingInfo {
  634. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  635. insertData = append(insertData, info.WarehousingId)
  636. insertData = append(insertData, info.DrugId)
  637. insertData = append(insertData, info.Number)
  638. insertData = append(insertData, info.ProductDate)
  639. insertData = append(insertData, info.ExpiryDate)
  640. insertData = append(insertData, info.WarehousingCount)
  641. insertData = append(insertData, info.Price)
  642. insertData = append(insertData, info.TotalPrice)
  643. insertData = append(insertData, info.Dealer)
  644. insertData = append(insertData, info.Manufacturer)
  645. insertData = append(insertData, info.Remark)
  646. insertData = append(insertData, info.Ctime)
  647. insertData = append(insertData, info.Mtime)
  648. insertData = append(insertData, info.Status)
  649. insertData = append(insertData, info.OrgId)
  650. insertData = append(insertData, info.WarehousingOrder)
  651. insertData = append(insertData, info.Type)
  652. insertData = append(insertData, info.RetailPrice)
  653. insertData = append(insertData, info.RetailTotalPrice)
  654. insertData = append(insertData, info.StockMaxNumber)
  655. insertData = append(insertData, info.MaxUnit)
  656. insertData = append(insertData, info.MinUnit)
  657. insertData = append(insertData, info.BatchNumber)
  658. insertData = append(insertData, info.StockMinNumber)
  659. insertData = append(insertData, info.SupplyWarehouseId)
  660. insertData = append(insertData, info.SupplyWarehouseDetailInfo)
  661. insertData = append(insertData, info.StorehouseId)
  662. insertData = append(insertData, info.IsCheck)
  663. }
  664. thisSQL += strings.Join(insertParams, ", ")
  665. err = utx.Exec(thisSQL, insertData...).Error
  666. if err != nil {
  667. utx.Rollback()
  668. return
  669. }
  670. }
  671. utx.Commit()
  672. }
  673. return
  674. }
  675. func UpdateDrugInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  676. var total float64
  677. var min_number int64
  678. for _, item := range warehousingInfo {
  679. //查询刚入库耗材的库存
  680. count, _ := FindDrugWaresingCount(item.DrugId, item.OrgId)
  681. //更新药品库的总量
  682. info, _ := FindeDrugInfo(item.DrugId)
  683. min_number = count.StockMaxNumber * info.MinNumber
  684. str := strconv.FormatInt(min_number, 10)
  685. minFloat, _ := strconv.ParseFloat(str, 64)
  686. total = info.Total + minFloat
  687. lib := models.BaseDrugLib{
  688. Total: total,
  689. }
  690. UpdateBaseDrug(&lib, item.ID)
  691. }
  692. return err
  693. }
  694. func FindDrugWaresingCount(drug_id int64, orgid int64) (models.XtDrugWarehouseInfo, error) {
  695. info := models.XtDrugWarehouseInfo{}
  696. db := XTReadDB().Table("xt_drug_warehouse_info").Where("status = 1")
  697. err = db.Select("stock_max_number,drug_id").Where("drug_id = ? and org_id = ? and status =1", drug_id, orgid).Last(&info).Error
  698. return info, err
  699. }
  700. func FindeDrugInfo(drug_id int64) (models.BaseDrugLib, error) {
  701. lib := models.BaseDrugLib{}
  702. err := XTReadDB().Model(&lib).Where("id = ?", drug_id).Find(&lib).Error
  703. return lib, err
  704. }
  705. func UpdateBaseDrug(lib *models.BaseDrugLib, id int64) error {
  706. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"total": lib.Total}).Error
  707. return err
  708. }
  709. func UpdateBaseDrugOne(lib models.BaseDrugLib, id int64) error {
  710. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"retail_price": lib.RetailPrice, "last_price": lib.LastPrice}).Error
  711. return err
  712. }
  713. func FindeLastWarehouseInfo(id int64) (models.DrugWarehouseInfo, error) {
  714. info := models.DrugWarehouseInfo{}
  715. err := XTReadDB().Model(&info).Where("id=? and status = 1", id).Find(&info).Error
  716. return info, err
  717. }
  718. func GetLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  719. out := models.DrugWarehouseOut{}
  720. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  721. return out, err
  722. }
  723. func CreateDrugFlow(drugflow []*models.DrugFlow) (err error) {
  724. if len(drugflow) > 0 {
  725. utx := writeDb.Begin()
  726. if len(drugflow) > 0 {
  727. 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 "
  728. insertParams := make([]string, 0)
  729. insertData := make([]interface{}, 0)
  730. for _, info := range drugflow {
  731. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  732. insertData = append(insertData, info.WarehousingId)
  733. insertData = append(insertData, info.DrugId)
  734. insertData = append(insertData, info.Number)
  735. insertData = append(insertData, info.BatchNumber)
  736. insertData = append(insertData, info.Count)
  737. insertData = append(insertData, info.UserOrgId)
  738. insertData = append(insertData, info.PatientId)
  739. insertData = append(insertData, info.SystemTime)
  740. insertData = append(insertData, info.ConsumableType)
  741. insertData = append(insertData, info.IsSys)
  742. insertData = append(insertData, info.WarehousingOrder)
  743. insertData = append(insertData, info.WarehouseOutId)
  744. insertData = append(insertData, info.WarehouseOutOrderNumber)
  745. insertData = append(insertData, info.IsEdit)
  746. insertData = append(insertData, info.CancelStockId)
  747. insertData = append(insertData, info.CancelOrderNumber)
  748. insertData = append(insertData, info.Manufacturer)
  749. insertData = append(insertData, info.Dealer)
  750. insertData = append(insertData, info.Creator)
  751. insertData = append(insertData, info.UpdateCreator)
  752. insertData = append(insertData, info.Status)
  753. insertData = append(insertData, info.Ctime)
  754. insertData = append(insertData, info.Mtime)
  755. insertData = append(insertData, info.Price)
  756. insertData = append(insertData, info.WarehousingDetailId)
  757. insertData = append(insertData, info.WarehouseOutDetailId)
  758. insertData = append(insertData, info.CancelOutDetailId)
  759. insertData = append(insertData, info.ExpireDate)
  760. insertData = append(insertData, info.ProductDate)
  761. insertData = append(insertData, info.MaxUnit)
  762. insertData = append(insertData, info.MinUnit)
  763. insertData = append(insertData, info.StorehouseId)
  764. }
  765. thisSQL += strings.Join(insertParams, ", ")
  766. err = utx.Exec(thisSQL, insertData...).Error
  767. if err != nil {
  768. utx.Rollback()
  769. return
  770. }
  771. }
  772. utx.Commit()
  773. }
  774. return
  775. }
  776. func GetDrugFlowIsExist(warhouseing int64, drug_id int64) (*models.DrugFlow, error) {
  777. info := models.DrugFlow{}
  778. err = XTReadDB().Where("warehousing_id = ? and drug_id = ? and status = 1", warhouseing, drug_id).Find(&info).Error
  779. if err == gorm.ErrRecordNotFound {
  780. return nil, err
  781. }
  782. if err != nil {
  783. return nil, err
  784. }
  785. return &info, nil
  786. }
  787. func GetDrugByWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  788. info := models.XtDrugWarehouseInfo{}
  789. err := XTReadDB().Where("id=? and status = 1", id).Find(&info).Error
  790. return info, err
  791. }
  792. func UpdateDrugWarehouseingInfo(id int64, info models.XtDrugWarehouseInfo) error {
  793. warehouseInfo := models.XtDrugWarehouseInfo{}
  794. err := XTWriteDB().Model(&warehouseInfo).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_max_number": info.StockMaxNumber}).Error
  795. return err
  796. }
  797. func CreateDrugFlowOne(flow models.DrugFlow) error {
  798. ut := writeDb.Begin()
  799. err := ut.Create(&flow).Error
  800. if err != nil {
  801. ut.Rollback()
  802. return err
  803. }
  804. ut.Commit()
  805. return err
  806. }
  807. func CreateSelfDrugFlowTwo(flow models.XtSelfDrugFlow) error {
  808. ut := writeDb.Begin()
  809. err := ut.Create(&flow).Error
  810. if err != nil {
  811. ut.Rollback()
  812. return err
  813. }
  814. ut.Commit()
  815. return err
  816. }
  817. func CreateNewDrugFlowOne(flow models.DrugFlow, tx *gorm.DB) error {
  818. err := tx.Create(&flow).Error
  819. if err != nil {
  820. tx.Rollback()
  821. return err
  822. }
  823. return err
  824. }
  825. func UpdateDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64, id int64) (models.DrugAutomaticReduceDetail, error) {
  826. detail := models.DrugAutomaticReduceDetail{}
  827. 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
  828. 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
  829. return detail, err
  830. }
  831. func UpdateSelfDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64, id int64) (models.XtSelfDrugWarehouseOutInfo, error) {
  832. detail := models.XtSelfDrugWarehouseOutInfo{}
  833. err = XTWriteDB().Model(&models.XtSelfDrugWarehouseOutInfo{}).Where("patient_id =? and sys_record_time =? and drug_id = ? and status = 1 and advice_id =?", patientid, recorddate, drugid, advice_id).Updates(map[string]interface{}{"status": 0}).Error
  834. return detail, err
  835. }
  836. func UpdateDrugFlowTens(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.DrugFlow, error) {
  837. flow := models.DrugFlow{}
  838. 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
  839. return flow, err
  840. }
  841. func UpdateDrugFlowTensOne(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.XtSelfDrugFlow, error) {
  842. flow := models.XtSelfDrugFlow{}
  843. 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
  844. return flow, err
  845. }
  846. func CreateDrugFlowTwo(flow *models.DrugFlow) error {
  847. tx := XTWriteDB().Begin()
  848. err := tx.Create(&flow).Error
  849. if err != nil {
  850. tx.Rollback()
  851. return err
  852. }
  853. tx.Commit()
  854. return err
  855. }
  856. func CreateNewDrugFlowTwo(flow *models.DrugFlow, tx *gorm.DB) error {
  857. err := tx.Create(&flow).Error
  858. if err != nil {
  859. tx.Rollback()
  860. return err
  861. }
  862. return err
  863. }
  864. func FindLastWarehousingInfo(order string) (info models.WarehousingInfo, err error) {
  865. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  866. return info, err
  867. }
  868. func GetLastWarehousingInfo(goodid int64) (models.WarehousingInfo, error) {
  869. info := models.WarehousingInfo{}
  870. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  871. return info, err
  872. }
  873. func CreatedWarehouseingDetail(info *models.WarehousingInfo) error {
  874. err := XTWriteDB().Create(&info).Error
  875. return err
  876. }
  877. func CreatedNewWarehouseingDetail(info *models.WarehousingInfo, tx *gorm.DB) error {
  878. err := tx.Create(&info).Error
  879. if err != nil {
  880. tx.Rollback()
  881. return err
  882. }
  883. return err
  884. }
  885. func FindFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  886. if storehouse_id == 0 {
  887. 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
  888. }
  889. if storehouse_id > 0 {
  890. 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
  891. }
  892. return info, err
  893. }
  894. func FindNewFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64, tx *gorm.DB) (info models.WarehousingInfo, err error) {
  895. if storehouse_id == 0 {
  896. err = tx.Model(&models.WarehousingInfo{}).Where("good_id = ? AND good_type_id = ? AND status = 1 and stock_count > 0", good_id, good_type_id).Order("ctime").First(&info).Error
  897. if err != gorm.ErrRecordNotFound {
  898. if err != nil {
  899. tx.Rollback()
  900. return info, err
  901. }
  902. }
  903. }
  904. if storehouse_id > 0 {
  905. err = tx.Model(&models.WarehousingInfo{}).Where("good_id = ? AND good_type_id = ? AND status = 1 and stock_count > 0 and storehouse_id =? ", good_id, good_type_id, storehouse_id).Order("ctime").First(&info).Error
  906. if err != gorm.ErrRecordNotFound {
  907. if err != nil {
  908. tx.Rollback()
  909. return info, err
  910. }
  911. }
  912. }
  913. return info, err
  914. }
  915. func FindFirstWarehousingInfoByStockTwo(good_id int64, good_type_id int64, storehouse_id int64) (info models.VmWarehousingInfo, err error) {
  916. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  917. if good_id > 0 {
  918. db = db.Where("x.good_id = ?", good_id)
  919. }
  920. if good_type_id > 0 {
  921. db = db.Where("x.good_type_id = ?", good_type_id)
  922. }
  923. if storehouse_id > 0 {
  924. db = db.Where("x.storehouse_id = ?", storehouse_id)
  925. }
  926. err = db.Select("sum(x.stock_count) as count").Scan(&info).Error
  927. return info, err
  928. }
  929. func FindWarehousingInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  930. info := models.WarehousingInfo{}
  931. err := XTReadDB().Model(&info).Where("id= ? and status = 1", id).Find(&info).Error
  932. return info, err
  933. }
  934. func FindWarehousingInfoTen(goodId int64, storehouse_id int64) (models.WarehousingInfo, error) {
  935. info := models.WarehousingInfo{}
  936. var err error
  937. if storehouse_id == 0 {
  938. 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
  939. }
  940. if storehouse_id > 0 {
  941. 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
  942. }
  943. return info, err
  944. }
  945. func FindNewWarehousingInfoTen(goodId int64, storehouse_id int64, tx *gorm.DB) (models.WarehousingInfo, error) {
  946. info := models.WarehousingInfo{}
  947. var err error
  948. if storehouse_id == 0 {
  949. err = tx.Select(" good_id,sum(stock_count) as stock_count").Where("good_id = ? and status = 1 and is_check = 1", goodId).Find(&info).Error
  950. if err != gorm.ErrRecordNotFound {
  951. if err != nil {
  952. tx.Rollback()
  953. return info, err
  954. }
  955. }
  956. }
  957. if storehouse_id > 0 {
  958. err := tx.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
  959. if err != gorm.ErrRecordNotFound {
  960. if err != nil {
  961. tx.Rollback()
  962. return info, err
  963. }
  964. }
  965. }
  966. return info, err
  967. }
  968. //药品先进先出,找出最先入库的批次
  969. func FindLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  970. 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
  971. return info, err
  972. }
  973. func FindLastDrugWarehousingInfoByIDTwo(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  974. err = readDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 and storehouse_id = ? and is_check = 1", drug_id, storehouse_id).Order("ctime").Last(&info).Error
  975. return info, err
  976. }
  977. func FindNewLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64, tx *gorm.DB) (info models.XtDrugWarehouseInfo, err error) {
  978. err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) and storehouse_id = ? and is_check = 1", drug_id, storehouse_id).Order("ctime").First(&info).Error
  979. if err != gorm.ErrRecordNotFound {
  980. if err != nil {
  981. tx.Rollback()
  982. return info, err
  983. }
  984. }
  985. return info, err
  986. }
  987. func FindLastDrugWarehousingInfoByIDSix(drug_id int64, warehouse_out_id int64, is_source int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  988. if is_source == 1 {
  989. 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
  990. }
  991. if is_source == 2 || is_source == 0 {
  992. 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
  993. }
  994. return info, err
  995. }
  996. // 出库时先进先出,找出最先入库的批次
  997. func FindLastWarehousingInfoByID(drug_id int64) (info models.WarehousingInfo, err error) {
  998. 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
  999. return info, err
  1000. }
  1001. //
  1002. //// 出库时先进先出,找出最先入库的批次
  1003. //func (drug_id int64) (info models.WarehousingInfo, err error) {
  1004. // 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
  1005. // return info, err
  1006. //}
  1007. func FindLastDrugWarehousingInfo(order string) (info models.DrugWarehouseInfo, err error) {
  1008. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  1009. return info, err
  1010. }
  1011. 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) {
  1012. offset := (page - 1) * limit
  1013. db := readDb.Model(&models.Warehousing{})
  1014. db = db.Where("xt_warehouse.org_id = ? AND xt_warehouse.status = 1 AND xt_warehouse.type = ?", orgId, types)
  1015. if len(keywords) > 0 {
  1016. likeKey := "%" + keywords + "%"
  1017. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  1018. 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")
  1019. }
  1020. if startTime > 0 {
  1021. db = db.Where("xt_warehouse.operation_time >=?", startTime)
  1022. }
  1023. if endTime > 0 {
  1024. db = db.Where("xt_warehouse.operation_time<= ?", endTime)
  1025. }
  1026. if storehouse_id > 0 {
  1027. db = db.Where("xt_warehouse.storehouse_id = ?", storehouse_id)
  1028. }
  1029. if check_type == 0 {
  1030. db = db.Where("xt_warehouse.is_check = 0 or xt_warehouse.is_check = 1 or xt_warehouse.is_check = 2")
  1031. }
  1032. if check_type > 0 {
  1033. db = db.Where("xt_warehouse.is_check = ?", check_type)
  1034. }
  1035. err = db.Count(&total).Offset(offset).Limit(limit).Order("xt_warehouse.ctime desc").Find(&list).Error
  1036. return
  1037. }
  1038. func GetGoodInforByGoodName(keyword string, orgid int64) (goodInfo []*models.GoodInfo, err error) {
  1039. db := XTReadDB().Model(&goodInfo).Where("status = 1")
  1040. if orgid > 0 {
  1041. db = db.Where("org_id = ?", orgid)
  1042. }
  1043. if len(keyword) > 0 {
  1044. likeKey := "%" + keyword + "%"
  1045. db = db.Where("good_name like ?", likeKey)
  1046. }
  1047. err = db.Find(&goodInfo).Error
  1048. return goodInfo, err
  1049. }
  1050. func GetWarehoureOrderInfoByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  1051. db := XTReadDB().Model(&info).Where("status =1")
  1052. if len(good_id) > 0 {
  1053. db = db.Where("good_id in (?)", good_id)
  1054. }
  1055. if startime > 0 {
  1056. db = db.Where("ctime >=?", startime)
  1057. }
  1058. if endtime > 0 {
  1059. db = db.Where("ctime<=?", endtime)
  1060. }
  1061. if orgid > 0 {
  1062. db = db.Where("org_id = ?", orgid)
  1063. }
  1064. if storehouse_id > 0 {
  1065. db = db.Where("storehouse_id = ?", storehouse_id)
  1066. }
  1067. err = db.Find(&info).Error
  1068. return info, err
  1069. }
  1070. func GetWarehoureOrderOutByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehouseOutInfo, err error) {
  1071. db := XTReadDB().Model(&info).Where("status =1")
  1072. if len(good_id) > 0 {
  1073. db = db.Where("good_id in (?)", good_id)
  1074. }
  1075. if startime > 0 {
  1076. db = db.Where("ctime >=?", startime)
  1077. }
  1078. if endtime > 0 {
  1079. db = db.Where("ctime<=?", endtime)
  1080. }
  1081. if orgid > 0 {
  1082. db = db.Where("org_id = ?", orgid)
  1083. }
  1084. if storehouse_id > 0 {
  1085. db = db.Where("storehouse_id = ?", storehouse_id)
  1086. }
  1087. err = db.Find(&info).Error
  1088. return info, err
  1089. }
  1090. type VMDrugWarehouse struct {
  1091. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1092. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  1093. OperationTime int64 `gorm:"column:operation_time" json:"operation_time" form:"operation_time"`
  1094. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1095. Creater int64 `gorm:"column:creater" json:"creater" form:"creater"`
  1096. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1097. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  1098. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1099. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1100. WarehousingTime int64 `gorm:"column:warehousing_time" json:"warehousing_time" form:"warehousing_time"`
  1101. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1102. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1103. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1104. Manufacturers *models.Manufacturer `gorm:"ForeignKey:Manufacturer;AssociationForeignKey:ID" json:"manufacturers"`
  1105. Dealers *models.Dealer `gorm:"ForeignKey:Dealer;AssociationForeignKey:ID" json:"dealers"`
  1106. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1107. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1108. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  1109. IsCheck int64 `gorm:"column:is_check" json:"is_check" form:"is_check"`
  1110. Checker int64 `gorm:"column:checker" json:"checker" form:"checker"`
  1111. }
  1112. func (VMDrugWarehouse) TableName() string {
  1113. return "xt_drug_warehouse"
  1114. }
  1115. type VMDrugWarehouseInfo struct {
  1116. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1117. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id" form:"warehousing_id"`
  1118. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1119. Number string `gorm:"column:number" json:"number" form:"number"`
  1120. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1121. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1122. WarehousingCount int64 `gorm:"column:warehousing_count" json:"warehousing_count" form:"warehousing_count"`
  1123. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1124. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  1125. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1126. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1127. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1128. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1129. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1130. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1131. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1132. IsReturn int64 `gorm:"column:is_return" json:"is_return" form:"is_return"`
  1133. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  1134. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1135. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1136. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1137. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1138. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1139. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  1140. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1141. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1142. SupplyWarehouseDetailInfo int64 `gorm:"column:supply_warehouse_detail_info" json:"supply_warehouse_detail_info" form:"supply_warehouse_detail_info"`
  1143. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1144. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  1145. }
  1146. func (VMDrugWarehouseInfo) TableName() string {
  1147. return "xt_drug_warehouse_info"
  1148. }
  1149. type Drug struct {
  1150. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1151. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  1152. Pinyin string `gorm:"column:pinyin" json:"pinyin" form:"pinyin"`
  1153. Wubi string `gorm:"column:wubi" json:"wubi" form:"wubi"`
  1154. DrugAlias string `gorm:"column:drug_alias" json:"drug_alias" form:"drug_alias"`
  1155. DrugAliasPinyin string `gorm:"column:drug_alias_pinyin" json:"drug_alias_pinyin" form:"drug_alias_pinyin"`
  1156. DrugAliasWubi string `gorm:"column:drug_alias_wubi" json:"drug_alias_wubi" form:"drug_alias_wubi"`
  1157. DrugCategory int64 `gorm:"column:drug_category" json:"drug_category" form:"drug_category"`
  1158. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  1159. DrugType int64 `gorm:"column:drug_type" json:"drug_type" form:"drug_type"`
  1160. DrugStockLimit string `gorm:"column:drug_stock_limit" json:"drug_stock_limit" form:"drug_stock_limit"`
  1161. DrugOriginPlace string `gorm:"column:drug_origin_place" json:"drug_origin_place" form:"drug_origin_place"`
  1162. DrugDosageForm int64 `gorm:"column:drug_dosage_form" json:"drug_dosage_form" form:"drug_dosage_form"`
  1163. MedicalInsuranceLevel int64 `gorm:"column:medical_insurance_level" json:"medical_insurance_level" form:"medical_insurance_level"`
  1164. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1165. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  1166. UnitMatrixing string `gorm:"column:unit_matrixing" json:"unit_matrixing" form:"unit_matrixing"`
  1167. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1168. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  1169. DrugControl int64 `gorm:"column:drug_control" json:"drug_control" form:"drug_control"`
  1170. Number string `gorm:"column:number" json:"number" form:"number"`
  1171. DrugClassify string `gorm:"column:drug_classify" json:"drug_classify" form:"drug_classify"`
  1172. DrugDose float64 `gorm:"column:drug_dose" json:"drug_dose" form:"drug_dose"`
  1173. DrugDoseUnit int64 `gorm:"column:drug_dose_unit" json:"drug_dose_unit" form:"drug_dose_unit"`
  1174. MedicalInsuranceNumber string `gorm:"column:medical_insurance_number" json:"medical_insurance_number" form:"medical_insurance_number"`
  1175. ProvincesCode string `gorm:"column:provinces_code" json:"provinces_code" form:"provinces_code"`
  1176. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1177. PharmacologyCategory int64 `gorm:"column:pharmacology_category" json:"pharmacology_category" form:"pharmacology_category"`
  1178. StatisticsCategory int64 `gorm:"column:statistics_category" json:"statistics_category" form:"statistics_category"`
  1179. Code string `gorm:"column:code" json:"code" form:"code"`
  1180. IsSpecialDiseases int64 `gorm:"column:is_special_diseases" json:"is_special_diseases" form:"is_special_diseases"`
  1181. IsRecord int64 `gorm:"column:is_record" json:"is_record" form:"is_record"`
  1182. Agent string `gorm:"column:agent" json:"agent" form:"agent"`
  1183. DrugStatus string `gorm:"column:drug_status" json:"drug_status" form:"drug_status"`
  1184. LimitRemark string `gorm:"column:limit_remark" json:"limit_remark" form:"limit_remark"`
  1185. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  1186. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  1187. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  1188. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  1189. Label int64 `gorm:"column:label" json:"label" form:"label"`
  1190. Sort int64 `gorm:"column:sort" json:"sort" form:"sort"`
  1191. IsUseDoctorAdvice int64 `gorm:"column:is_use_doctor_advice" json:"is_use_doctor_advice" form:"is_use_doctor_advice"`
  1192. IsDefault int64 `gorm:"column:is_default" json:"is_default" form:"is_default"`
  1193. IsChargePredict int64 `gorm:"column:is_charge_predict" json:"is_charge_predict" form:"is_charge_predict"`
  1194. IsStatisticsWork int64 `gorm:"column:is_statistics_work" json:"is_statistics_work" form:"is_statistics_work"`
  1195. IsChargeUse int64 `gorm:"column:is_charge_use" json:"is_charge_use" form:"is_charge_use"`
  1196. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1197. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1198. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1199. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1200. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  1201. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1202. DoseCode string `gorm:"column:dose_code" json:"dose_code" form:"dose_code"`
  1203. RecordDate int64 `gorm:"column:record_date" json:"record_date" form:"record_date"`
  1204. DrugRemark string `gorm:"column:drug_remark" json:"drug_remark" form:"drug_remark"`
  1205. SocialSecurityDirectoryCode string `gorm:"column:social_security_directory_code" json:"social_security_directory_code" form:"social_security_directory_code"`
  1206. IsMark int64 `gorm:"column:is_mark" json:"is_mark" form:"is_mark"`
  1207. PrescriptionMark int64 `gorm:"column:prescription_mark" json:"prescription_mark" form:"prescription_mark"`
  1208. HospApprFlag int64 `gorm:"column:hosp_appr_flag" json:"hosp_appr_flag" form:"hosp_appr_flag"`
  1209. LmtUsedFlag int64 `gorm:"column:lmt_used_flag" json:"lmt_used_flag" form:"lmt_used_flag"`
  1210. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1211. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  1212. MinNumber int64 `gorm:"column:min_number" json:"min_number" form:"min_number"`
  1213. Dose string `gorm:"column:dose" json:"dose" form:"dose"`
  1214. DoseUnit string `gorm:"column:dose_unit" json:"dose_unit" form:"dose_unit"`
  1215. DrugDay string `gorm:"column:drug_day" json:"drug_day" form:"drug_day"`
  1216. MinPrice float64 `gorm:"column:min_price" json:"min_price" form:"min_price"`
  1217. IsZeroFlag int64 `gorm:"column:is_zero_flag" json:"is_zero_flag" form:"is_zero_flag"`
  1218. }
  1219. func (Drug) TableName() string {
  1220. return "xt_base_drug"
  1221. }
  1222. func FindAllDrugWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*VMDrugWarehouse, total int64, err error) {
  1223. db := readDb.Model(&VMDrugWarehouse{})
  1224. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1225. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1226. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1227. if len(keywords) > 0 {
  1228. likeKey := "%" + keywords + "%"
  1229. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1230. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse.manufacturer")
  1231. 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")
  1232. }
  1233. if startTime > 0 {
  1234. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1235. }
  1236. if endTime > 0 {
  1237. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1238. }
  1239. db = db.Count(&total)
  1240. offset := (page - 1) * limit
  1241. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1242. return
  1243. }
  1244. 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) {
  1245. db := readDb.Model(&VMDrugWarehouse{})
  1246. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1247. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1248. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1249. if len(keywords) > 0 {
  1250. likeKey := "%" + keywords + "%"
  1251. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1252. 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")
  1253. }
  1254. if startTime > 0 {
  1255. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1256. }
  1257. if endTime > 0 {
  1258. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1259. }
  1260. if storehouse_id > 0 {
  1261. db = db.Where("xt_drug_warehouse.storehouse_id = ?", storehouse_id)
  1262. }
  1263. if check_type > 0 {
  1264. db = db.Where("xt_drug_warehouse.is_check = ?", check_type)
  1265. }
  1266. db = db.Count(&total)
  1267. offset := (page - 1) * limit
  1268. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1269. return
  1270. }
  1271. func FindWarehousingInfoById(id int64) (list []*models.WarehousingInfo, err error) {
  1272. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1273. return list, err
  1274. }
  1275. func FindWarehousingInfoByIdOne(id []int64) (list []*models.WarehousingInfo, err error) {
  1276. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id in(?) AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1277. return list, err
  1278. }
  1279. func FindDrugWarehousingInfoById(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1280. 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
  1281. return list, err
  1282. }
  1283. func FindDrugWarehousingInfoByIdThree(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1284. err = readDb.Model(&VMDrugWarehouseInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehousing_id = ? AND status = 1", id).Find(&list).Error
  1285. return list, err
  1286. }
  1287. func FindDrugWarehousingInfoByIdOne(id []string, org_id int64) (list []*models.StDrugWarehouseInfo, err error) {
  1288. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  1289. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  1290. fmt.Println(table)
  1291. if len(id) > 0 {
  1292. db = db.Where("x.warehousing_id in(?)", id)
  1293. }
  1294. if org_id > 0 {
  1295. db = db.Where("x.org_id = ?", org_id)
  1296. }
  1297. 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
  1298. return list, err
  1299. }
  1300. func FindWarehousingById(id int64) (warehousing models.Warehousing, err error) {
  1301. err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1302. return warehousing, err
  1303. }
  1304. func FindNewWarehousingById(id int64, tx *gorm.DB) (warehousing models.Warehousing, err error) {
  1305. err = tx.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1306. if err != gorm.ErrRecordNotFound {
  1307. if err != nil {
  1308. tx.Rollback()
  1309. return warehousing, err
  1310. }
  1311. }
  1312. return warehousing, err
  1313. }
  1314. func FindWarehousingByIdOne(id []string) (warehousing []*models.Warehousing, err error) {
  1315. err = readDb.Model(&models.Warehousing{}).Where("id in(?) and status = 1", id).Find(&warehousing).Error
  1316. //err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1317. return warehousing, err
  1318. }
  1319. func FindDrugWarehousingById(id int64, org_id int64) (warehousing VMDrugWarehouse, err error) {
  1320. 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
  1321. return warehousing, err
  1322. }
  1323. func FindNewDrugWarehousingById(id int64, org_id int64, tx *gorm.DB) (warehousing VMDrugWarehouse, err error) {
  1324. err = tx.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
  1325. if err != gorm.ErrRecordNotFound {
  1326. if err != nil {
  1327. tx.Rollback()
  1328. return warehousing, err
  1329. }
  1330. }
  1331. return warehousing, err
  1332. }
  1333. func FindDrugWarehousingByIdOne(id []string, org_id int64) (warehousing []*models.DrugWarehouse, err error) {
  1334. db := XTReadDB().Table("xt_drug_warehouse as x").Where("x.status = 1")
  1335. if len(id) > 0 {
  1336. db = db.Where("x.id in(?)", id)
  1337. }
  1338. if org_id > 0 {
  1339. db = db.Where("x.org_id = ?", org_id)
  1340. }
  1341. 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
  1342. return warehousing, err
  1343. }
  1344. //func FindDrugWarehousingById(id int64) (warehousing models.DrugWarehouse, err error) {
  1345. // err = readDb.Model(&models.DrugWarehouse{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1346. // return warehousing, err
  1347. //
  1348. //}
  1349. func GetInfoByIds(ids []string) (info []*models.WarehousingInfo, err error) {
  1350. err = readDb.Model(&models.WarehousingInfo{}).Preload("Warehousing", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1351. return
  1352. }
  1353. func GetWarehousOutInfoByIds(ids []string) (info []*models.WarehouseOutInfo, err error) {
  1354. err = readDb.Model(&models.WarehouseOutInfo{}).Preload("WarehouseOut", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1355. return
  1356. }
  1357. func CreateSalesReturnInfo(salesReturnInfo []*models.SalesReturnInfo) (err error) {
  1358. if len(salesReturnInfo) > 0 {
  1359. utx := writeDb.Begin()
  1360. if len(salesReturnInfo) > 0 {
  1361. 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 "
  1362. insertParams := make([]string, 0)
  1363. insertData := make([]interface{}, 0)
  1364. for _, info := range salesReturnInfo {
  1365. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1366. insertData = append(insertData, info.GoodId)
  1367. insertData = append(insertData, info.SalesReturnId)
  1368. insertData = append(insertData, info.GoodTypeId)
  1369. insertData = append(insertData, info.Count)
  1370. insertData = append(insertData, info.Price)
  1371. insertData = append(insertData, info.Total)
  1372. insertData = append(insertData, info.Ctime)
  1373. insertData = append(insertData, info.Status)
  1374. insertData = append(insertData, info.OrgId)
  1375. insertData = append(insertData, info.OrderNumber)
  1376. insertData = append(insertData, info.Type)
  1377. insertData = append(insertData, info.Dealer)
  1378. insertData = append(insertData, info.Manufacturer)
  1379. }
  1380. thisSQL += strings.Join(insertParams, ", ")
  1381. err = utx.Exec(thisSQL, insertData...).Error
  1382. if err != nil {
  1383. utx.Rollback()
  1384. return
  1385. }
  1386. }
  1387. utx.Commit()
  1388. }
  1389. return
  1390. }
  1391. func CreateDrugSalesReturnInfo(salesReturnInfo []*models.DrugSalesReturnInfo) (err error) {
  1392. if len(salesReturnInfo) > 0 {
  1393. utx := writeDb.Begin()
  1394. if len(salesReturnInfo) > 0 {
  1395. 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 "
  1396. insertParams := make([]string, 0)
  1397. insertData := make([]interface{}, 0)
  1398. for _, info := range salesReturnInfo {
  1399. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1400. insertData = append(insertData, info.DrugId)
  1401. insertData = append(insertData, info.SalesReturnId)
  1402. insertData = append(insertData, info.Count)
  1403. insertData = append(insertData, info.Price)
  1404. insertData = append(insertData, info.Total)
  1405. insertData = append(insertData, info.Ctime)
  1406. insertData = append(insertData, info.Status)
  1407. insertData = append(insertData, info.OrgId)
  1408. insertData = append(insertData, info.OrderNumber)
  1409. insertData = append(insertData, info.Type)
  1410. insertData = append(insertData, info.Dealer)
  1411. insertData = append(insertData, info.Manufacturer)
  1412. insertData = append(insertData, info.RetailPrice)
  1413. insertData = append(insertData, info.RetailTotalPrice)
  1414. }
  1415. thisSQL += strings.Join(insertParams, ", ")
  1416. err = utx.Exec(thisSQL, insertData...).Error
  1417. if err != nil {
  1418. utx.Rollback()
  1419. return
  1420. }
  1421. }
  1422. utx.Commit()
  1423. }
  1424. return
  1425. }
  1426. func FindAllSalesReturnTotal(org_id int64) (total int64, err error) {
  1427. err = readDb.Model(&models.SalesReturn{}).Where("org_id = ?", org_id).Count(&total).Error
  1428. return total, err
  1429. }
  1430. func FindAllCancelStockTotal(org_id int64) (total int64, err error) {
  1431. err = readDb.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1432. return total, err
  1433. }
  1434. func FindNewAllCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1435. err = tx.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1436. if err != gorm.ErrRecordNotFound {
  1437. if err != nil {
  1438. tx.Rollback()
  1439. return total, err
  1440. }
  1441. }
  1442. return total, err
  1443. }
  1444. func FindAllDrugCancelStockTotal(org_id int64) (total int64, err error) {
  1445. err = readDb.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1446. return total, err
  1447. }
  1448. func FindNewAllDrugCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1449. err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1450. if err != gorm.ErrRecordNotFound {
  1451. if err != nil {
  1452. tx.Rollback()
  1453. return total, err
  1454. }
  1455. }
  1456. return total, err
  1457. }
  1458. //func FindAllDrugCancelStockTotalTX(org_id int64,tx *gorm.DB) (total int64, err error) {
  1459. // err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1460. // return total, err
  1461. //}
  1462. func FindAllReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.SalesReturn, total int64, err error) {
  1463. db := readDb.Model(&models.SalesReturn{})
  1464. db = db.Where("xt_sales_return.org_id = ? AND xt_sales_return.status = 1 AND xt_sales_return.type = ?", orgId, types)
  1465. if len(keywords) > 0 {
  1466. likeKey := "%" + keywords + "%"
  1467. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  1468. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_sales_return.manufacturer")
  1469. 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")
  1470. }
  1471. if startTime > 0 {
  1472. db = db.Where("xt_sales_return.opera_time >=?", startTime)
  1473. }
  1474. if endTime > 0 {
  1475. db = db.Where("xt_sales_return.opera_time<= ?", endTime)
  1476. }
  1477. db = db.Count(&total)
  1478. offset := (page - 1) * limit
  1479. err = db.Offset(offset).Limit(limit).Order("xt_sales_return.ctime desc").Find(&list).Error
  1480. return
  1481. }
  1482. func FindAllDrugReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugSalesReturn, total int64, err error) {
  1483. db := readDb.Model(&models.DrugSalesReturn{})
  1484. db = db.Where("xt_drug_sales_return.org_id = ? AND xt_drug_sales_return.status = 1 ", orgId)
  1485. if len(keywords) > 0 {
  1486. likeKey := "%" + keywords + "%"
  1487. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_sales_return.creater")
  1488. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_sales_return.manufacturer")
  1489. 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")
  1490. }
  1491. if startTime > 0 {
  1492. db = db.Where("xt_drug_sales_return.opera_time >=?", startTime)
  1493. }
  1494. if endTime > 0 {
  1495. db = db.Where("xt_drug_sales_return.opera_time<= ?", endTime)
  1496. }
  1497. db = db.Count(&total)
  1498. offset := (page - 1) * limit
  1499. err = db.Offset(offset).Limit(limit).Order("xt_drug_sales_return.ctime desc").Find(&list).Error
  1500. return
  1501. }
  1502. func FindReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  1503. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  1504. return list, err
  1505. }
  1506. func FindCancelStockInfoById(id int64) (list []*models.CancelStockInfo, err error) {
  1507. err = readDb.Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? AND status = 1", id).Preload("WarehousingInfo", "status = 1").Preload("GoodInfo", "status = 1").Find(&list).Error
  1508. return list, err
  1509. }
  1510. type DrugCancelStockInfo struct {
  1511. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1512. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1513. CancelStockId int64 `gorm:"column:cancel_stock_id" json:"cancel_stock_id" form:"cancel_stock_id"`
  1514. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1515. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1516. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1517. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1518. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1519. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1520. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1521. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1522. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1523. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  1524. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1525. Dealer string `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1526. Manufacturer string `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1527. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1528. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1529. Number string `gorm:"column:number" json:"number" form:"number"`
  1530. RegisterAccount string `gorm:"column:register_account" json:"register_account" form:"register_account"`
  1531. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1532. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1533. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1534. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1535. BatchNumberId int64 `gorm:"column:batch_number_id" json:"batch_number_id" form:"batch_number_id"`
  1536. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1537. DrugWarehouseInfo []*models.XtDrugWarehouseInfo `gorm:"ForeignKey:DrugId;AssociationForeignKey:DrugId" json:"drug_warehouse_info"`
  1538. }
  1539. func (DrugCancelStockInfo) TableName() string {
  1540. return "xt_drug_cancel_stock_info"
  1541. }
  1542. func FindDrugCancelStockInfoById(id int64, org_id int64) (list []*DrugCancelStockInfo, err error) {
  1543. 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
  1544. return list, err
  1545. }
  1546. func FindAllWarehousingInfo(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehousingInfo, total int64, err error) {
  1547. db := readDb.Model(&models.WarehousingInfo{})
  1548. db = db.Where("org_id = ? AND status = 1", orgId)
  1549. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  1550. if startTime > 0 {
  1551. db = db.Where("ctime >=?", startTime)
  1552. }
  1553. if endTime > 0 {
  1554. db = db.Where("ctime<= ?", endTime)
  1555. }
  1556. db = db.Count(&total)
  1557. offset := (page - 1) * limit
  1558. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1559. return list, total, err
  1560. }
  1561. func FindAllWarehouseOut(org_id int64) (total int64, err error) {
  1562. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1563. return total, err
  1564. }
  1565. func FindAllDrugWarehouseOut(org_id int64) (total int64, err error) {
  1566. err = readDb.Model(&models.DrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1567. return total, err
  1568. }
  1569. func FindAllSelfDrugWarehouseOut(org_id int64, tx *gorm.DB) (total int64, err error) {
  1570. err = tx.Model(&models.XtSelfDrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1571. if err != gorm.ErrRecordNotFound {
  1572. if err != nil {
  1573. tx.Rollback()
  1574. return
  1575. }
  1576. }
  1577. return total, err
  1578. }
  1579. func AddSigleWarehouseOutOne(warehouseOut *models.WarehouseOut) error {
  1580. err := XTWriteDB().Create(&warehouseOut).Error
  1581. return err
  1582. }
  1583. func AddSigleWarehouseOut(warehouseOut *models.WarehouseOut, tx *gorm.DB) error {
  1584. err := tx.Create(&warehouseOut).Error
  1585. if err != nil {
  1586. tx.Rollback()
  1587. return err
  1588. }
  1589. return err
  1590. }
  1591. func AddNewSigleWarehouseOut(warehouseOut *models.WarehouseOut, tx *gorm.DB) error {
  1592. err := tx.Create(&warehouseOut).Error
  1593. if err != nil {
  1594. tx.Rollback()
  1595. return err
  1596. }
  1597. return err
  1598. }
  1599. func UpdateSingleWarehoseOut(id int64, storehouse_id int64, tx *gorm.DB) error {
  1600. err := tx.Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1601. if err != nil {
  1602. tx.Rollback()
  1603. return err
  1604. }
  1605. return err
  1606. }
  1607. func GetDrugWarehouseOutIsExit(org_id int64, is_sys int, record_time int64, warehousing_out_order string) (models.DrugWarehouseOut, error) {
  1608. drugWarehouseOut := models.DrugWarehouseOut{}
  1609. err := XTReadDB().Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ? and warehouse_out_order_number = ?", is_sys, org_id, record_time, warehousing_out_order).Find(&drugWarehouseOut).Error
  1610. return drugWarehouseOut, err
  1611. }
  1612. func GetNewDrugWarehouseOutIsExit(org_id int64, is_sys int, record_time int64, warehousing_out_order string, tx *gorm.DB) (models.DrugWarehouseOut, error) {
  1613. drugWarehouseOut := models.DrugWarehouseOut{}
  1614. err := tx.Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ? and warehouse_out_order_number = ?", is_sys, org_id, record_time, warehousing_out_order).Find(&drugWarehouseOut).Error
  1615. if err != gorm.ErrRecordNotFound {
  1616. if err != nil {
  1617. tx.Rollback()
  1618. return drugWarehouseOut, err
  1619. }
  1620. }
  1621. return drugWarehouseOut, err
  1622. }
  1623. func GetNewDrugWarehouseOutIsExitOne(org_id int64, is_sys int, record_time int64, warehousing_out_order string, tx *gorm.DB, id int64) (models.DrugWarehouseOut, error) {
  1624. drugWarehouseOut := models.DrugWarehouseOut{}
  1625. err := tx.Where("is_sys = ? AND status = 1 AND org_id = ? AND id = ?", is_sys, org_id, id).Find(&drugWarehouseOut).Error
  1626. if err != gorm.ErrRecordNotFound {
  1627. if err != nil {
  1628. tx.Rollback()
  1629. return drugWarehouseOut, err
  1630. }
  1631. }
  1632. return drugWarehouseOut, err
  1633. }
  1634. func AddSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut) error {
  1635. err := writeDb.Create(&warehouseOut).Error
  1636. return err
  1637. }
  1638. func GetSigleDrugWarehouseOutByPatientId(warehousing_time int64, patient_id int64, tx *gorm.DB) (models.XtSelfDrugWarehouse, error) {
  1639. selfDrugWarehouse := models.XtSelfDrugWarehouse{}
  1640. err := tx.Where("warehousing_time = ? and patient_id = ? and is_sys =1 and status=1", warehousing_time, patient_id).Find(&selfDrugWarehouse).Error
  1641. if err != gorm.ErrRecordNotFound {
  1642. if err != nil {
  1643. tx.Rollback()
  1644. return selfDrugWarehouse, err
  1645. }
  1646. }
  1647. return selfDrugWarehouse, err
  1648. }
  1649. func AddSelfSigleDrugWarehouseOut(warehouseOut *models.XtSelfDrugWarehouseOut, tx *gorm.DB) error {
  1650. err := tx.Create(&warehouseOut).Error
  1651. if err != nil {
  1652. tx.Rollback()
  1653. return err
  1654. }
  1655. return err
  1656. }
  1657. func AddNewSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut, tx *gorm.DB) error {
  1658. err := tx.Create(&warehouseOut).Error
  1659. if err != nil {
  1660. tx.Rollback()
  1661. return err
  1662. }
  1663. return err
  1664. }
  1665. func UpdateSingleDrugWarehouseOut(id int64, storehouse_id int64) error {
  1666. err := XTWriteDB().Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1667. return err
  1668. }
  1669. func UpdateNewSingleDrugWarehouseOut(id int64, storehouse_id int64, tx *gorm.DB) error {
  1670. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1671. if err != nil {
  1672. tx.Rollback()
  1673. return err
  1674. }
  1675. return err
  1676. }
  1677. func UpdateSelfSingleDrugWarehouseOut(id int64, patient_id int64, tx *gorm.DB) error {
  1678. err := tx.Model(&models.XtSelfDrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"patient_id": patient_id}).Error
  1679. return err
  1680. }
  1681. func CreateWarehousingOutInfo(warehouseOutInfo []*models.WarehouseOutInfo) (err error) {
  1682. if len(warehouseOutInfo) > 0 {
  1683. utx := writeDb.Begin()
  1684. if len(warehouseOutInfo) > 0 {
  1685. 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 "
  1686. insertParams := make([]string, 0)
  1687. insertData := make([]interface{}, 0)
  1688. for _, info := range warehouseOutInfo {
  1689. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1690. insertData = append(insertData, info.WarehouseOutId)
  1691. insertData = append(insertData, info.GoodId)
  1692. insertData = append(insertData, info.GoodTypeId)
  1693. insertData = append(insertData, info.ProductDate)
  1694. insertData = append(insertData, info.ExpiryDate)
  1695. insertData = append(insertData, info.Count)
  1696. insertData = append(insertData, info.Price)
  1697. insertData = append(insertData, info.TotalPrice)
  1698. insertData = append(insertData, info.Remark)
  1699. insertData = append(insertData, info.Ctime)
  1700. insertData = append(insertData, info.Status)
  1701. insertData = append(insertData, info.OrgId)
  1702. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1703. insertData = append(insertData, info.Type)
  1704. insertData = append(insertData, info.Dealer)
  1705. insertData = append(insertData, info.Manufacturer)
  1706. insertData = append(insertData, info.Number)
  1707. insertData = append(insertData, info.LicenseNumber)
  1708. }
  1709. thisSQL += strings.Join(insertParams, ", ")
  1710. err = utx.Exec(thisSQL, insertData...).Error
  1711. if err != nil {
  1712. utx.Rollback()
  1713. return
  1714. }
  1715. }
  1716. utx.Commit()
  1717. }
  1718. return
  1719. }
  1720. func CreateDrugWarehousingOutInfo(warehouseOutInfo []*models.DrugWarehouseOutInfo) (err error) {
  1721. if len(warehouseOutInfo) > 0 {
  1722. utx := writeDb.Begin()
  1723. if len(warehouseOutInfo) > 0 {
  1724. 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 "
  1725. insertParams := make([]string, 0)
  1726. insertData := make([]interface{}, 0)
  1727. for _, info := range warehouseOutInfo {
  1728. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1729. insertData = append(insertData, info.WarehouseOutId)
  1730. insertData = append(insertData, info.DrugId)
  1731. insertData = append(insertData, info.ProductDate)
  1732. insertData = append(insertData, info.ExpiryDate)
  1733. insertData = append(insertData, info.Count)
  1734. insertData = append(insertData, info.Price)
  1735. insertData = append(insertData, info.TotalPrice)
  1736. insertData = append(insertData, info.Remark)
  1737. insertData = append(insertData, info.Ctime)
  1738. insertData = append(insertData, info.Status)
  1739. insertData = append(insertData, info.OrgId)
  1740. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1741. insertData = append(insertData, info.Type)
  1742. insertData = append(insertData, info.Dealer)
  1743. insertData = append(insertData, info.Manufacturer)
  1744. insertData = append(insertData, info.RetailPrice)
  1745. insertData = append(insertData, info.RetailTotalPrice)
  1746. insertData = append(insertData, info.Number)
  1747. insertData = append(insertData, info.BatchNumber)
  1748. insertData = append(insertData, info.CountUnit)
  1749. }
  1750. thisSQL += strings.Join(insertParams, ", ")
  1751. err = utx.Exec(thisSQL, insertData...).Error
  1752. if err != nil {
  1753. utx.Rollback()
  1754. return
  1755. }
  1756. }
  1757. utx.Commit()
  1758. }
  1759. return
  1760. }
  1761. func FindLastWarehousingOutInfo(order string) (info models.WarehouseOutInfo, err error) {
  1762. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1763. return info, err
  1764. }
  1765. func FindLastDrugWarehousingOutInfo(order string) (info models.DrugWarehouseOutInfo, err error) {
  1766. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1767. return info, err
  1768. }
  1769. func FindAllWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.WarehouseOut, total int64, err error) {
  1770. db := readDb.Model(&models.WarehouseOut{})
  1771. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1772. if len(keywords) > 0 {
  1773. likeKey := "%" + keywords + "%"
  1774. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1775. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse_out.manufacturer")
  1776. 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")
  1777. }
  1778. if startTime > 0 {
  1779. db = db.Where("xt_warehouse_out.ctime >=?", startTime)
  1780. }
  1781. if endTime > 0 {
  1782. db = db.Where("xt_warehouse_out.ctime<= ?", endTime)
  1783. }
  1784. db = db.Count(&total)
  1785. offset := (page - 1) * limit
  1786. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1787. return list, total, err
  1788. }
  1789. 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) {
  1790. db := readDb.Model(&models.WarehouseOut{})
  1791. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1792. if len(keywords) > 0 {
  1793. likeKey := "%" + keywords + "%"
  1794. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1795. 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")
  1796. }
  1797. if startTime > 0 {
  1798. db = db.Where("xt_warehouse_out.warehouse_out_time >=?", startTime)
  1799. }
  1800. if endTime > 0 {
  1801. db = db.Where("xt_warehouse_out.warehouse_out_time<= ?", endTime)
  1802. }
  1803. if storehouse_id > 0 {
  1804. db = db.Where("xt_warehouse_out.storehouse_id = ?", storehouse_id)
  1805. }
  1806. if way_type == 0 {
  1807. 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")
  1808. }
  1809. if way_type == 1 {
  1810. db = db.Where("xt_warehouse_out.is_sys = 0")
  1811. }
  1812. if way_type == 2 {
  1813. db = db.Where("xt_warehouse_out.is_sys = 1")
  1814. }
  1815. if way_type == 3 {
  1816. db = db.Where("xt_warehouse_out.is_sys = 12")
  1817. }
  1818. if way_type == 4 {
  1819. db = db.Where("xt_warehouse_out.is_sys = 5")
  1820. }
  1821. if check_type > 0 {
  1822. db = db.Where("xt_warehouse_out.is_check = ?", check_type)
  1823. }
  1824. if check_type == 0 {
  1825. db = db.Where("xt_warehouse_out.is_check = 1 or xt_warehouse_out.is_check = 2")
  1826. }
  1827. db = db.Count(&total)
  1828. offset := (page - 1) * limit
  1829. if orgId == 10191 {
  1830. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1831. } else {
  1832. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1833. }
  1834. return list, total, err
  1835. }
  1836. func FindAllDrugWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugWarehouseOut, total int64, err error) {
  1837. db := readDb.Model(&models.DrugWarehouseOut{})
  1838. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1839. if len(keywords) > 0 {
  1840. likeKey := "%" + keywords + "%"
  1841. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1842. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse_out.manufacturer")
  1843. 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")
  1844. }
  1845. if startTime > 0 {
  1846. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1847. }
  1848. if endTime > 0 {
  1849. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1850. }
  1851. db = db.Count(&total)
  1852. offset := (page - 1) * limit
  1853. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1854. return list, total, err
  1855. }
  1856. 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) {
  1857. db := readDb.Model(&models.DrugWarehouseOut{})
  1858. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1859. if len(keywords) > 0 {
  1860. likeKey := "%" + keywords + "%"
  1861. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1862. 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")
  1863. }
  1864. if startTime > 0 {
  1865. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1866. }
  1867. if endTime > 0 {
  1868. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1869. }
  1870. if storehouse_id > 0 {
  1871. db = db.Where("xt_drug_warehouse_out.storehouse_id = ?", storehouse_id)
  1872. }
  1873. if check_type > 0 {
  1874. db = db.Where("xt_drug_warehouse_out.is_check = ?", check_type)
  1875. }
  1876. if way_type == 0 {
  1877. 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")
  1878. }
  1879. if way_type == 1 {
  1880. db = db.Where("xt_drug_warehouse_out.is_sys = 0")
  1881. }
  1882. if way_type == 2 {
  1883. db = db.Where("xt_drug_warehouse_out.is_sys = 1")
  1884. }
  1885. if way_type == 3 {
  1886. db = db.Where("xt_drug_warehouse_out.is_sys = 12")
  1887. }
  1888. if way_type == 4 {
  1889. db = db.Where("xt_drug_warehouse_out.is_sys = 15")
  1890. }
  1891. db = db.Count(&total)
  1892. offset := (page - 1) * limit
  1893. if orgId == 10191 {
  1894. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1895. } else {
  1896. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1897. }
  1898. return list, total, err
  1899. }
  1900. func FindWarehouseOutInfoById(id int64) (list []*models.WarehouseOutInfo, err error) {
  1901. 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
  1902. return list, err
  1903. }
  1904. type DrugWarehouseOutInfo struct {
  1905. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1906. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  1907. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1908. WarehousingOutTarget int64 `gorm:"column:warehousing_out_target" json:"warehousing_out_target" form:"warehousing_out_target"`
  1909. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1910. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  1911. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1912. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  1913. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1914. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1915. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1916. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1917. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1918. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1919. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1920. IsCancel int64 `gorm:"column:is_cancel" json:"is_cancel" form:"is_cancel"`
  1921. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  1922. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1923. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1924. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1925. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  1926. SysRecordTime int64 `gorm:"column:sys_record_time" json:"sys_record_time" form:"sys_record_time"`
  1927. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1928. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1929. Number string `gorm:"column:number" json:"number" form:"number"`
  1930. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1931. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1932. WarehouseInfoId int64 `gorm:"column:warehouse_info_id" json:"warehouse_info_id" form:"warehouse_info_id"`
  1933. SupplyCancelOutId int64 `gorm:"column:supply_cancel_out_id" json:"supply_cancel_out_id" form:"supply_cancel_out_id"`
  1934. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1935. AdminUserId int64 `gorm:"column:admin_user_id" json:"admin_user_id" form:"admin_user_id"`
  1936. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  1937. StockCount string `gorm:"column:stock_count" json:"stock_count" form:"stock_count"`
  1938. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  1939. ClassType string `gorm:"column:class_type" json:"class_type" form:"class_type"`
  1940. }
  1941. func (DrugWarehouseOutInfo) TableName() string {
  1942. return "xt_drug_warehouse_out_info"
  1943. }
  1944. func FindDrugWarehouseOutInfoById(id int64, org_id int64) (list []*DrugWarehouseOutInfo, err error) {
  1945. err = readDb.Model(&DrugWarehouseOutInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehouse_out_id = ? AND status = 1 ", id).Find(&list).Error
  1946. return list, err
  1947. }
  1948. func FindWarehouseInfoByGoodId(good_id int64) (int64, *models.WarehousingInfo) {
  1949. info := &models.WarehousingInfo{}
  1950. type WarehouseInfoCountAmount struct {
  1951. Total int64
  1952. }
  1953. var result WarehouseInfoCountAmount
  1954. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(warehousing_count) as total").Scan(&result)
  1955. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).First(info)
  1956. return result.Total, info
  1957. }
  1958. func FindAllSalesReturnCountByGoodId(good_id int64) (int64, error) {
  1959. type SalesReturnCountAmount struct {
  1960. Total int64
  1961. }
  1962. var result SalesReturnCountAmount
  1963. err = readDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1964. return result.Total, err
  1965. }
  1966. func FindAllWarehouseOutInfoByGoodId(good_id int64) (int64, error) {
  1967. type WarehouseOutInfoCountAmount struct {
  1968. Total int64
  1969. }
  1970. var result WarehouseOutInfoCountAmount
  1971. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1972. return result.Total, err
  1973. }
  1974. func FindAllCancleStockInfoByGoodId(good_id int64) (int64, error) {
  1975. type CancleStockoCountAmount struct {
  1976. Total int64
  1977. }
  1978. var result CancleStockoCountAmount
  1979. err = readDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1980. return result.Total, err
  1981. }
  1982. func FindWarehouseInfoByGoodTypeId(good_type_id int64) (models.WarehousingInfo, error) {
  1983. info := models.WarehousingInfo{}
  1984. err := readDb.Model(&models.WarehousingInfo{}).Where("good_type_id = ? AND status = 1", good_type_id).First(&info).Error
  1985. return info, err
  1986. }
  1987. func GetCancelStockDetailByOrderNumber(ordernumber string, orgid int64) (*models.CancelStock, error) {
  1988. stock := models.CancelStock{}
  1989. err := XTReadDB().Where("order_number = ? and org_id = ? and status = 1", ordernumber, orgid).Find(&stock).Error
  1990. if err == gorm.ErrRecordNotFound {
  1991. return nil, err
  1992. }
  1993. if err != nil {
  1994. return nil, err
  1995. }
  1996. return &stock, nil
  1997. }
  1998. func GetCancelStockDetailByOrderNumberOne(return_time int64, orgid int64) (*models.CancelStock, error) {
  1999. stock := models.CancelStock{}
  2000. err := XTReadDB().Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  2001. if err == gorm.ErrRecordNotFound {
  2002. return nil, err
  2003. }
  2004. if err != nil {
  2005. return nil, err
  2006. }
  2007. return &stock, nil
  2008. }
  2009. func GetNewCancelStockDetailByOrderNumberOne(return_time int64, orgid int64, tx *gorm.DB) (*models.CancelStock, error) {
  2010. stock := models.CancelStock{}
  2011. err := tx.Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  2012. if err != gorm.ErrRecordNotFound {
  2013. if err != nil {
  2014. tx.Rollback()
  2015. return &stock, err
  2016. }
  2017. }
  2018. return &stock, err
  2019. }
  2020. func AddSigleCancelStock(cancelStock *models.CancelStock) error {
  2021. err := writeDb.Create(&cancelStock).Error
  2022. return err
  2023. }
  2024. func AddNewSigleCancelStock(cancelStock *models.CancelStock, tx *gorm.DB) error {
  2025. err := tx.Create(&cancelStock).Error
  2026. if err != nil {
  2027. tx.Rollback()
  2028. return err
  2029. }
  2030. return err
  2031. }
  2032. func UpdateSigleCancelStock(id int64, storehouse_id int64) error {
  2033. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  2034. return err
  2035. }
  2036. func GetLastCancelStockById(orgid int64) (models.CancelStock, error) {
  2037. stock := models.CancelStock{}
  2038. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2039. return stock, err
  2040. }
  2041. func GetNewLastCancelStockById(orgid int64, tx *gorm.DB) (models.CancelStock, error) {
  2042. stock := models.CancelStock{}
  2043. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2044. if err != gorm.ErrRecordNotFound {
  2045. if err != nil {
  2046. tx.Rollback()
  2047. return stock, err
  2048. }
  2049. }
  2050. return stock, err
  2051. }
  2052. func GetLastDrugCancelStockById(orgid int64) (models.DrugCancelStock, error) {
  2053. stock := models.DrugCancelStock{}
  2054. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2055. return stock, err
  2056. }
  2057. func GetNewLastDrugCancelStockById(orgid int64, tx *gorm.DB) (models.DrugCancelStock, error) {
  2058. stock := models.DrugCancelStock{}
  2059. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2060. if err != gorm.ErrRecordNotFound {
  2061. if err != nil {
  2062. tx.Rollback()
  2063. return stock, err
  2064. }
  2065. }
  2066. return stock, err
  2067. }
  2068. func GetManufactureById(id int64) (models.Manufacturer, error) {
  2069. manufacturer := models.Manufacturer{}
  2070. err := XTReadDB().Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  2071. return manufacturer, err
  2072. }
  2073. func GetNewManufactureById(id int64, tx *gorm.DB) (models.Manufacturer, error) {
  2074. manufacturer := models.Manufacturer{}
  2075. err := tx.Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  2076. if err != gorm.ErrRecordNotFound {
  2077. if err != nil {
  2078. tx.Rollback()
  2079. return manufacturer, err
  2080. }
  2081. }
  2082. return manufacturer, err
  2083. }
  2084. func GetDealerById(id int64) (models.Dealer, error) {
  2085. dealer := models.Dealer{}
  2086. err := XTReadDB().Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  2087. return dealer, err
  2088. }
  2089. func GetNewDealerById(id int64, tx *gorm.DB) (models.Dealer, error) {
  2090. dealer := models.Dealer{}
  2091. err := tx.Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  2092. if err != gorm.ErrRecordNotFound {
  2093. if err != nil {
  2094. tx.Rollback()
  2095. return dealer, err
  2096. }
  2097. }
  2098. return dealer, err
  2099. }
  2100. func AddSigleDrugCancelStock(cancelStock *models.DrugCancelStock) error {
  2101. err := writeDb.Create(&cancelStock).Error
  2102. return err
  2103. }
  2104. func AddNewSigleDrugCancelStock(cancelStock *models.DrugCancelStock, tx *gorm.DB) error {
  2105. err := tx.Create(&cancelStock).Error
  2106. if err != nil {
  2107. tx.Rollback()
  2108. return err
  2109. }
  2110. return err
  2111. }
  2112. func UpdateSingleDrugCancelStock(id int64, storehouse_id int64) error {
  2113. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  2114. return err
  2115. }
  2116. //func AddSigleDrugCancelStockTX(cancelStock *models.DrugCancelStock,tx *gorm.DB) error {
  2117. // err := tx.Create(&cancelStock).Error
  2118. // return err
  2119. //}
  2120. func CreateCancelStockInfo(cancelStockInfo []*models.CancelStockInfo) (err error) {
  2121. if len(cancelStockInfo) > 0 {
  2122. utx := writeDb.Begin()
  2123. if len(cancelStockInfo) > 0 {
  2124. 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 "
  2125. insertParams := make([]string, 0)
  2126. insertData := make([]interface{}, 0)
  2127. for _, info := range cancelStockInfo {
  2128. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  2129. insertData = append(insertData, info.GoodId)
  2130. insertData = append(insertData, info.CancelStockId)
  2131. insertData = append(insertData, info.GoodTypeId)
  2132. insertData = append(insertData, info.Count)
  2133. insertData = append(insertData, info.Price)
  2134. insertData = append(insertData, info.Total)
  2135. insertData = append(insertData, info.Ctime)
  2136. insertData = append(insertData, info.Status)
  2137. insertData = append(insertData, info.OrgId)
  2138. insertData = append(insertData, info.OrderNumber)
  2139. insertData = append(insertData, info.Type)
  2140. insertData = append(insertData, info.Dealer)
  2141. insertData = append(insertData, info.Manufacturer)
  2142. insertData = append(insertData, info.Number)
  2143. insertData = append(insertData, info.RegisterAccount)
  2144. insertData = append(insertData, info.Remark)
  2145. insertData = append(insertData, info.ProductDate)
  2146. insertData = append(insertData, info.ExpiryDate)
  2147. insertData = append(insertData, info.WarehouseInfoId)
  2148. insertData = append(insertData, info.WarehouseInfoId)
  2149. }
  2150. thisSQL += strings.Join(insertParams, ", ")
  2151. err = utx.Exec(thisSQL, insertData...).Error
  2152. if err != nil {
  2153. utx.Rollback()
  2154. return
  2155. }
  2156. }
  2157. utx.Commit()
  2158. }
  2159. return
  2160. }
  2161. func CreateCancelStockInfoTwo(info *models.DrugCancelStockInfo) error {
  2162. err := XTWriteDB().Create(&info).Error
  2163. return err
  2164. }
  2165. //func CreateCancelStockInfoTwoTX(info *models.DrugCancelStockInfo,tx *gorm.DB) error {
  2166. // err := tx.Create(&info).Error
  2167. // return err
  2168. //}
  2169. func GetLastStockInfoThree(drugid int64) (models.DrugCancelStockInfo, error) {
  2170. info := models.DrugCancelStockInfo{}
  2171. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  2172. return info, err
  2173. }
  2174. //func GetLastStockInfoThreeTX(drugid int64,tx *gorm.DB) (models.DrugCancelStockInfo, error) {
  2175. // info := models.DrugCancelStockInfo{}
  2176. // err := tx.Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  2177. // return info, err
  2178. //}
  2179. func CreateCancelStockInfoOne(info *models.CancelStockInfo) error {
  2180. err := XTWriteDB().Create(&info).Error
  2181. return err
  2182. }
  2183. func CreateNewCancelStockInfoOne(info *models.CancelStockInfo, tx *gorm.DB) error {
  2184. err := XTWriteDB().Create(&info).Error
  2185. if err != nil {
  2186. tx.Rollback()
  2187. return err
  2188. }
  2189. return err
  2190. }
  2191. func GetLastCancelStockInfoByGoodId(goodId int64) (models.CancelStockInfo, error) {
  2192. info := models.CancelStockInfo{}
  2193. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  2194. return info, err
  2195. }
  2196. func GetLastNewCancelStockInfoByGoodId(goodId int64, tx *gorm.DB) (models.CancelStockInfo, error) {
  2197. info := models.CancelStockInfo{}
  2198. err := tx.Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  2199. if err != gorm.ErrRecordNotFound {
  2200. if err != nil {
  2201. tx.Rollback()
  2202. return info, err
  2203. }
  2204. }
  2205. return info, err
  2206. }
  2207. func GetLastDrugCancelStockInfoById(drugid int64) (models.DrugCancelStockInfo, error) {
  2208. info := models.DrugCancelStockInfo{}
  2209. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Last(&info).Error
  2210. return info, err
  2211. }
  2212. func CreateDrugCancelStockInfo(cancelStockInfo []*models.DrugCancelStockInfo) (err error) {
  2213. if len(cancelStockInfo) > 0 {
  2214. utx := writeDb.Begin()
  2215. if len(cancelStockInfo) > 0 {
  2216. 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 "
  2217. insertParams := make([]string, 0)
  2218. insertData := make([]interface{}, 0)
  2219. for _, info := range cancelStockInfo {
  2220. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  2221. insertData = append(insertData, info.DrugId)
  2222. insertData = append(insertData, info.CancelStockId)
  2223. insertData = append(insertData, info.Count)
  2224. insertData = append(insertData, info.Price)
  2225. insertData = append(insertData, info.Total)
  2226. insertData = append(insertData, info.Ctime)
  2227. insertData = append(insertData, info.Status)
  2228. insertData = append(insertData, info.OrgId)
  2229. insertData = append(insertData, info.OrderNumber)
  2230. insertData = append(insertData, info.Type)
  2231. insertData = append(insertData, info.Dealer)
  2232. insertData = append(insertData, info.Manufacturer)
  2233. insertData = append(insertData, info.RetailPrice)
  2234. insertData = append(insertData, info.RetailTotalPrice)
  2235. insertData = append(insertData, info.RegisterAccount)
  2236. insertData = append(insertData, info.BatchNumber)
  2237. insertData = append(insertData, info.Remark)
  2238. insertData = append(insertData, info.MaxUnit)
  2239. insertData = append(insertData, info.ProductDate)
  2240. insertData = append(insertData, info.ExpiryDate)
  2241. insertData = append(insertData, info.BatchNumberId)
  2242. }
  2243. thisSQL += strings.Join(insertParams, ", ")
  2244. err = utx.Exec(thisSQL, insertData...).Error
  2245. if err != nil {
  2246. utx.Rollback()
  2247. return
  2248. }
  2249. }
  2250. utx.Commit()
  2251. }
  2252. return
  2253. }
  2254. func FindAllCancelStockList(orgId int64) (list []*models.CancelStock, err error) {
  2255. db := readDb.Model(&models.CancelStock{})
  2256. db = db.Where("org_id = ? AND status = 1", orgId)
  2257. err = db.Order("ctime desc").Find(&list).Error
  2258. return
  2259. }
  2260. func FindAllStockOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  2261. db := readDb.Model(&models.WarehousingInfo{})
  2262. db = db.Where("org_id = ? AND status = 1", orgId)
  2263. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2264. if startTime > 0 {
  2265. db = db.Where("ctime >=?", startTime)
  2266. }
  2267. if endTime > 0 {
  2268. db = db.Where("ctime<= ?", endTime)
  2269. }
  2270. db = db.Count(&total)
  2271. offset := (page - 1) * limit
  2272. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2273. return
  2274. }
  2275. func FindAllStockInfo(orgId int64, page int64, limit int64, keyword string, startime int64, endtime int64, type_name int64) (list []*models.StockInfo, total int64, err error) {
  2276. db := XTReadDB().Model(&models.StockInfo{})
  2277. if startime > 0 {
  2278. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2279. return db.Where("org_id = ? AND status = 1 and sys_record_time>=?", orgId, startime)
  2280. })
  2281. }
  2282. if startime == 0 || endtime == 0 {
  2283. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2284. return db.Where("org_id = ? AND status = 1", orgId)
  2285. })
  2286. }
  2287. if endtime > 0 {
  2288. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2289. return db.Where("org_id = ? AND status = 1 and sys_record_time >=? and sys_record_time<=?", orgId, startime, endtime)
  2290. })
  2291. }
  2292. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgId)
  2293. db = db.Group("xt_good_information.id")
  2294. if startime == 0 || endtime == 0 {
  2295. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2296. return db.Where("org_id = ? AND status = 1", orgId)
  2297. })
  2298. }
  2299. if startime > 0 {
  2300. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2301. return db.Where("org_id = ? AND status = 1 and ctime >= ?", orgId, startime)
  2302. })
  2303. }
  2304. if endtime > 0 {
  2305. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2306. return db.Where("org_id = ? AND status = 1 and ctime>=? and ctime <= ?", orgId, startime, endtime)
  2307. })
  2308. }
  2309. db = db.Preload("QuerySalesReturnInfo", "org_id = ? AND status = 1", orgId)
  2310. if startime == 0 || endtime == 0 {
  2311. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1", orgId)
  2312. }
  2313. if startime > 0 {
  2314. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=?", orgId, startime)
  2315. }
  2316. if endtime > 0 {
  2317. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=? and ctime<=?", orgId, startime, endtime)
  2318. }
  2319. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  2320. if len(keyword) > 0 {
  2321. likeKey := "%" + keyword + "%"
  2322. 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)
  2323. 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")
  2324. }
  2325. if type_name > 0 {
  2326. 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)
  2327. }
  2328. db = db.Count(&total)
  2329. offset := (page - 1) * limit
  2330. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2331. return
  2332. }
  2333. func FindWarehouseInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2334. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2335. return
  2336. }
  2337. func FindWarehouseOutInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2338. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2339. return
  2340. }
  2341. func UpDateWarehouse(warehouse *models.Warehousing) (err error) {
  2342. 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
  2343. return
  2344. }
  2345. func UpDateWarehouseOut(warehouseOut *models.WarehouseOut) (err error) {
  2346. 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
  2347. return
  2348. }
  2349. func UpDateWarehouseInfo(warehouseInfo *models.WarehousingInfo) (err error) {
  2350. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", warehouseInfo.WarehousingOrder).Update(map[string]interface{}{"good_type_id": warehouseInfo.GoodTypeId,
  2351. "good_id": warehouseInfo.GoodId,
  2352. "number": warehouseInfo.Number,
  2353. "product_date": warehouseInfo.ProductDate,
  2354. "expiry_date": warehouseInfo.ExpiryDate,
  2355. "warehousing_count": warehouseInfo.WarehousingCount,
  2356. "price": warehouseInfo.Price,
  2357. "total_price": warehouseInfo.TotalPrice,
  2358. "mtime": warehouseInfo.Mtime,
  2359. "remark": warehouseInfo.Remark,
  2360. }).Error
  2361. return
  2362. }
  2363. func UpDateWarehouseStatus(id int64) (err error) {
  2364. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2365. return
  2366. }
  2367. func UpdatedStockFlowById(id int64) (err error) {
  2368. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("warehousing_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2369. return
  2370. }
  2371. func UpDateDrugWarehouseStatus(id int64) (err error) {
  2372. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2373. return
  2374. }
  2375. func UpdateDrugFlowById(id int64) (err error) {
  2376. err = XTWriteDB().Model(&models.DrugFlow{}).Where("warehousing_detail_id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2377. return
  2378. }
  2379. func DeleteWarehouse(ids []string) (err error) {
  2380. ut := writeDb.Begin()
  2381. err = ut.Model(&models.Warehousing{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2382. if err != nil {
  2383. ut.Rollback()
  2384. return
  2385. }
  2386. err = ut.Model(&models.WarehousingInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2387. if err != nil {
  2388. ut.Rollback()
  2389. return
  2390. }
  2391. ut.Commit()
  2392. return
  2393. }
  2394. func DeleteStockFlow(ids []string) (err error) {
  2395. ut := writeDb.Begin()
  2396. err = ut.Model(&models.VmStockFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2397. if err != nil {
  2398. ut.Rollback()
  2399. return
  2400. }
  2401. ut.Commit()
  2402. return
  2403. }
  2404. func DeleteDrugWarehouse(ids []string) (err error) {
  2405. ut := writeDb.Begin()
  2406. err = ut.Model(&models.DrugWarehouse{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2407. if err != nil {
  2408. ut.Rollback()
  2409. return
  2410. }
  2411. err = ut.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2412. if err != nil {
  2413. ut.Rollback()
  2414. return
  2415. }
  2416. err = ut.Model(&models.DrugFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2417. if err != nil {
  2418. ut.Rollback()
  2419. return
  2420. }
  2421. ut.Commit()
  2422. return
  2423. }
  2424. func FindWareHouseById(orderNumber string) (*models.Warehousing, error) {
  2425. warehouse := &models.Warehousing{}
  2426. err := readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).First(&warehouse).Error
  2427. return warehouse, err
  2428. }
  2429. func FindWareHouseOutByIdOne(id int64) (*models.WarehouseOut, error) {
  2430. warehouseOut := &models.WarehouseOut{}
  2431. err := XTReadDB().Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  2432. return warehouseOut, err
  2433. }
  2434. func FindWareHouseOutById(id int64, tx *gorm.DB) (*models.WarehouseOut, error) {
  2435. warehouseOut := &models.WarehouseOut{}
  2436. err := tx.Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  2437. if err != gorm.ErrRecordNotFound {
  2438. if err != nil {
  2439. tx.Rollback()
  2440. return warehouseOut, err
  2441. }
  2442. }
  2443. return warehouseOut, err
  2444. }
  2445. func FindDrugWareHouseOutById(id int64, org_id int64) (*models.DrugWarehouseOut, error) {
  2446. warehouseOut := &models.DrugWarehouseOut{}
  2447. 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
  2448. return warehouseOut, err
  2449. }
  2450. func FindNewDrugWareHouseOutById(id int64, org_id int64, tx *gorm.DB) (*models.DrugWarehouseOut, error) {
  2451. warehouseOut := &models.DrugWarehouseOut{}
  2452. err := tx.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
  2453. if err != nil {
  2454. tx.Rollback()
  2455. return warehouseOut, err
  2456. }
  2457. return warehouseOut, err
  2458. }
  2459. func FindWarehouseInfoByOrderNumber(orderNumber string) (info []*models.WarehousingInfo, err error) {
  2460. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", orderNumber).Find(&info).Error
  2461. return
  2462. }
  2463. func FindWarehouseOutInfoByOrderNumber(orderNumber string) (info []*models.WarehouseOutInfo, err error) {
  2464. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", orderNumber).Find(&info).Error
  2465. return
  2466. }
  2467. func DeleteStockInRecordByOrderNumber(orderNumber string, id int64) (err error) {
  2468. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2469. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2470. return
  2471. }
  2472. func DeleteStockInRecordByReocordId(id int64) (err error) {
  2473. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2474. return
  2475. }
  2476. func FindAllGoodByManufactureId(manufacturer_id int64, dealer_id int64, org_id int64) (goodInfo []*models.GoodInfo, err error) {
  2477. db := readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id)
  2478. if manufacturer_id > 0 {
  2479. db = db.Where("manufacturer = ?", manufacturer_id)
  2480. }
  2481. if dealer_id > 0 {
  2482. db = db.Where("dealer = ?", dealer_id)
  2483. }
  2484. err = db.Preload("GoodsType", "status = 1 AND org_id = ?", org_id).Find(&goodInfo).Error
  2485. return goodInfo, err
  2486. }
  2487. func FindAllWarehouseByKeyword(orgId int64, page int64, limit int64, keywords string) (list []*models.Warehousing, total int64, err error) {
  2488. db := readDb.Model(&models.Warehousing{})
  2489. likekey := "%" + keywords + "%"
  2490. db = db.Where("org_id = ? AND status = 1 ", orgId)
  2491. db = db.Where("warehousing_order LIKE ? ", likekey)
  2492. db = db.Count(&total)
  2493. offset := (page - 1) * limit
  2494. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2495. return
  2496. }
  2497. func GetLastWarehousingById(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2498. info := models.WarehousingInfo{}
  2499. err = XTReadDB().Model(&info).Where("warehousing_id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2500. return info, err
  2501. }
  2502. func GetLastWarehousingByIdOne(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2503. info := models.WarehousingInfo{}
  2504. err = XTReadDB().Model(&info).Where("id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2505. return info, err
  2506. }
  2507. func UpDateWarehousingInfo(info *models.WarehousingInfo) (err error) {
  2508. err = writeDb.Save(&info).Error
  2509. return err
  2510. }
  2511. func UpDateDrugWarehousingInfo(info *models.DrugWarehouseInfo) (err error) {
  2512. err = writeDb.Save(&info).Error
  2513. return err
  2514. }
  2515. func UpDateDrugWarehousingInfoTwo(id int64, info *models.DrugWarehouseInfo) (err error) {
  2516. 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
  2517. return err
  2518. }
  2519. func UpDateDrugWarehousingInfoSix(id int64, info *models.DrugWarehouseInfo) (err error) {
  2520. 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
  2521. return err
  2522. }
  2523. func GetDrugWarehouseOrderOne(id int64, drug_id int64) (models.DrugWarehouseInfo, error) {
  2524. info := models.DrugWarehouseInfo{}
  2525. err = XTReadDB().Model(&info).Where("warehousing_id = ? and drug_id = ?", id, drug_id).Find(&info).Error
  2526. return info, err
  2527. }
  2528. func EditWarehousing(warehouse models.Warehousing) {
  2529. 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
  2530. }
  2531. func EditNewWarehousing(warehouse models.Warehousing, tx *gorm.DB) {
  2532. err = tx.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
  2533. if err != nil {
  2534. tx.Rollback()
  2535. return
  2536. }
  2537. return
  2538. }
  2539. func FindAllWarehouseInfo(org_id int64, types int64) (list []*models.WarehousingInfoConfig, err error) {
  2540. err = readDb.Model(&models.WarehousingInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2541. 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)
  2542. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2543. return list, err
  2544. }
  2545. func DeleteSalesReturn(ids []string) (err error) {
  2546. ut := writeDb.Begin()
  2547. err = ut.Model(&models.SalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2548. if err != nil {
  2549. ut.Rollback()
  2550. return
  2551. }
  2552. err = ut.Model(&models.SalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2553. if err != nil {
  2554. ut.Rollback()
  2555. return
  2556. }
  2557. ut.Commit()
  2558. return
  2559. }
  2560. func DeleteDrugSalesReturn(ids []string) (err error) {
  2561. ut := writeDb.Begin()
  2562. err = ut.Model(&models.DrugSalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2563. if err != nil {
  2564. ut.Rollback()
  2565. return
  2566. }
  2567. err = ut.Model(&models.DrugSalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2568. if err != nil {
  2569. ut.Rollback()
  2570. return
  2571. }
  2572. ut.Commit()
  2573. return
  2574. }
  2575. func FindAllSalesReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  2576. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  2577. return
  2578. }
  2579. type DrugSalesReturnInfo struct {
  2580. ID int64 `gorm:"column:id" json:"id" form:"id"`
  2581. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  2582. SalesReturnId int64 `gorm:"column:sales_return_id" json:"sales_return_id" form:"sales_return_id"`
  2583. Count int64 `gorm:"column:count" json:"count" form:"count"`
  2584. Price float64 `gorm:"column:price" json:"price" form:"price"`
  2585. Total float64 `gorm:"column:total" json:"total" form:"total"`
  2586. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  2587. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  2588. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  2589. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  2590. Status int64 `gorm:"column:status" json:"status" form:"status"`
  2591. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  2592. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  2593. Type int64 `gorm:"column:type" json:"type" form:"type"`
  2594. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  2595. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  2596. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  2597. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  2598. Number string `gorm:"column:number" json:"number" form:"number"`
  2599. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  2600. }
  2601. func (DrugSalesReturnInfo) TableName() string {
  2602. return "xt_drug_sales_return_info"
  2603. }
  2604. func FindAllDrugSalesReturnInfoById(id int64, org_id int64) (list []*DrugSalesReturnInfo, err error) {
  2605. 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
  2606. return
  2607. }
  2608. func FindAllSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2609. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2610. return
  2611. }
  2612. func FindAllDrugSalesReturnById(id int64, org_id int64) (salesReturn models.DrugSalesReturn, err error) {
  2613. 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
  2614. return
  2615. }
  2616. func UpDateSaleReturnStatus(id int64) (err error) {
  2617. err = readDb.Model(&models.SalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2618. return
  2619. }
  2620. func UpDateDrugSaleReturnStatus(id int64) (err error) {
  2621. err = readDb.Model(&models.DrugSalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2622. return
  2623. }
  2624. func FindSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2625. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2626. return salesReturn, err
  2627. }
  2628. func FindDrugSalesReturnById(id int64) (salesReturn models.DrugSalesReturn, err error) {
  2629. err = readDb.Model(&models.DrugSalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2630. return salesReturn, err
  2631. }
  2632. func EditSaleReturn(sales models.SalesReturn) {
  2633. 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
  2634. }
  2635. func EditDrugSaleReturn(sales models.DrugSalesReturn) {
  2636. 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
  2637. }
  2638. func UpDateSalesReturnInfo(info *models.SalesReturnInfo) (err error) {
  2639. err = writeDb.Save(&info).Error
  2640. return err
  2641. }
  2642. func UpDateDrugSalesReturnInfo(info *models.DrugSalesReturnInfo) (err error) {
  2643. err = writeDb.Save(&info).Error
  2644. return err
  2645. }
  2646. func DeleteWarehouseOut(ids []string) (err error) {
  2647. ut := writeDb.Begin()
  2648. err = ut.Model(&models.WarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2649. if err != nil {
  2650. ut.Rollback()
  2651. return
  2652. }
  2653. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2654. if err != nil {
  2655. ut.Rollback()
  2656. return
  2657. }
  2658. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2659. if err != nil {
  2660. ut.Rollback()
  2661. return
  2662. }
  2663. ut.Commit()
  2664. return
  2665. }
  2666. func DeleteDrugWarehouseOut(ids []string) (err error) {
  2667. ut := writeDb.Begin()
  2668. err = ut.Model(&models.DrugWarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2669. if err != nil {
  2670. ut.Rollback()
  2671. return
  2672. }
  2673. err = ut.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2674. if err != nil {
  2675. ut.Rollback()
  2676. return
  2677. }
  2678. err = ut.Model(&models.DrugFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2679. if err != nil {
  2680. ut.Rollback()
  2681. return
  2682. }
  2683. ut.Commit()
  2684. return
  2685. }
  2686. func UpDateWarehouseOutStatus(id int64) (err error) {
  2687. err = readDb.Model(&models.WarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2688. return
  2689. }
  2690. func UpDateDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo) (err error) {
  2691. ut := writeDb.Begin()
  2692. err = ut.Save(&info).Error
  2693. if err != nil {
  2694. ut.Rollback()
  2695. return err
  2696. }
  2697. ut.Commit()
  2698. return err
  2699. }
  2700. func UpDateNewDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo, tx *gorm.DB) (err error) {
  2701. err = tx.Save(&info).Error
  2702. if err != nil {
  2703. tx.Rollback()
  2704. return err
  2705. }
  2706. return err
  2707. }
  2708. func UpDateWarehouseInfoByStock(info *models.WarehousingInfo) (err error) {
  2709. ut := writeDb.Begin()
  2710. err = ut.Save(&info).Error
  2711. if err != nil {
  2712. ut.Rollback()
  2713. return err
  2714. }
  2715. ut.Commit()
  2716. return err
  2717. }
  2718. func UpDateNewWarehouseInfoByStock(info *models.WarehousingInfo, tx *gorm.DB) (err error) {
  2719. err = tx.Save(&info).Error
  2720. if err != nil {
  2721. tx.Rollback()
  2722. return err
  2723. }
  2724. return err
  2725. }
  2726. func UpDateDrugWarehouseOutStatus(id int64) (err error) {
  2727. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2728. return
  2729. }
  2730. func EditWarehouseOut(warehouseOut models.WarehouseOut, tx *gorm.DB) {
  2731. err = tx.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
  2732. if err != nil {
  2733. tx.Rollback()
  2734. return
  2735. }
  2736. return
  2737. }
  2738. func EditDrugWarehouseOut(warehouseOut models.DrugWarehouseOut) {
  2739. 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
  2740. }
  2741. func EditNewDrugWarehouseOut(warehouseOut models.DrugWarehouseOut, tx *gorm.DB) {
  2742. err = tx.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
  2743. if err != nil {
  2744. tx.Rollback()
  2745. return
  2746. }
  2747. return
  2748. }
  2749. func UpDateWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) (err error) {
  2750. err = tx.Save(&info).Error
  2751. if err != nil {
  2752. tx.Rollback()
  2753. return err
  2754. }
  2755. return err
  2756. }
  2757. 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) {
  2758. db := readDb.Model(&models.CancelStock{})
  2759. db = db.Where("xt_cancel_stock.org_id = ? AND xt_cancel_stock.status = 1 AND xt_cancel_stock.type = ?", orgId, types)
  2760. if len(keywords) > 0 {
  2761. likeKey := "%" + keywords + "%"
  2762. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2763. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_cancel_stock.manufacturer")
  2764. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_cancel_stock.order_number LIKE ?", likeKey, likeKey).Group("xt_cancel_stock.id")
  2765. }
  2766. if startTime > 0 {
  2767. db = db.Where("xt_cancel_stock.opera_time >=?", startTime)
  2768. }
  2769. if endTime > 0 {
  2770. db = db.Where("xt_cancel_stock.opera_time<= ?", endTime)
  2771. }
  2772. if storehouse_id > 0 {
  2773. db = db.Where("xt_cancel_stock.storehouse_id = ?", storehouse_id)
  2774. }
  2775. if check_type > 0 {
  2776. db = db.Where("xt_cancel_stock.is_check = ?", check_type)
  2777. }
  2778. db = db.Count(&total)
  2779. offset := (page - 1) * limit
  2780. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock.ctime desc").Find(&list).Error
  2781. return
  2782. }
  2783. 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) {
  2784. db := readDb.Model(&models.DrugCancelStock{})
  2785. db = db.Where("xt_drug_cancel_stock.org_id = ? AND xt_drug_cancel_stock.status = 1", orgId)
  2786. if len(keywords) > 0 {
  2787. likeKey := "%" + keywords + "%"
  2788. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_cancel_stock.creater")
  2789. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_cancel_stock.manufacturer")
  2790. 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")
  2791. }
  2792. if startTime > 0 {
  2793. db = db.Where("xt_drug_cancel_stock.opera_time >=?", startTime)
  2794. }
  2795. if endTime > 0 {
  2796. db = db.Where("xt_drug_cancel_stock.opera_time<= ?", endTime)
  2797. }
  2798. if storehoue_id > 0 {
  2799. db = db.Where("xt_drug_cancel_stock.storehouse_id = ?", storehoue_id)
  2800. }
  2801. if check_type > 0 {
  2802. db = db.Where("xt_drug_cancel_stock.is_check = ?", check_type)
  2803. }
  2804. db = db.Count(&total)
  2805. offset := (page - 1) * limit
  2806. err = db.Offset(offset).Limit(limit).Order("xt_drug_cancel_stock.ctime desc").Find(&list).Error
  2807. return
  2808. }
  2809. func GetCancelWarehosueInfo(ids []string) (info []*models.CancelStockInfo, err error) {
  2810. err = XTReadDB().Where("id IN(?) and status = 1", ids).Find(&info).Error
  2811. return info, err
  2812. }
  2813. func DeleteCancelStock(ids []string) (err error) {
  2814. ut := writeDb.Begin()
  2815. err = ut.Model(&models.CancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2816. if err != nil {
  2817. ut.Rollback()
  2818. return
  2819. }
  2820. err = ut.Model(&models.CancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2821. if err != nil {
  2822. ut.Rollback()
  2823. return
  2824. }
  2825. err = ut.Model(&models.VmStockFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2826. if err != nil {
  2827. ut.Rollback()
  2828. return
  2829. }
  2830. ut.Commit()
  2831. return
  2832. }
  2833. func GetDrugWareInfoByCancelId(ids []string) (info []*models.DrugCancelStockInfo, err error) {
  2834. err = XTReadDB().Where("cancel_stock_id IN(?) and status = 1", ids).Find(&info).Error
  2835. return info, err
  2836. }
  2837. func DeleteDrugCancelStock(ids []string) (err error) {
  2838. ut := writeDb.Begin()
  2839. err = ut.Model(&models.DrugCancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2840. if err != nil {
  2841. ut.Rollback()
  2842. return
  2843. }
  2844. err = ut.Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2845. if err != nil {
  2846. ut.Rollback()
  2847. return
  2848. }
  2849. err = ut.Model(&models.DrugFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2850. if err != nil {
  2851. ut.Rollback()
  2852. return
  2853. }
  2854. ut.Commit()
  2855. return
  2856. }
  2857. func UpDateCancleStockStatus(id int64) (err error) {
  2858. err = readDb.Model(&models.CancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2859. return
  2860. }
  2861. func GetCancelStockById(id int64) (models.CancelStockInfo, error) {
  2862. info := models.CancelStockInfo{}
  2863. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  2864. return info, err
  2865. }
  2866. func ModefyWarehouseInfoNight(info models.WarehousingInfo, id int64) error {
  2867. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", info.StockCount)).Error
  2868. return err
  2869. }
  2870. func UpdatedStockFlowByCancelId(id int64, good_id int64) (err error) {
  2871. 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
  2872. return
  2873. }
  2874. func UpDateDrugCancleStockStatus(id int64) (err error) {
  2875. err = readDb.Model(&models.DrugCancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2876. return
  2877. }
  2878. func UpDateDrugCancelStockById(id int64) (err error) {
  2879. err = XTWriteDB().Model(&models.DrugFlow{}).Where("cancel_out_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2880. return
  2881. }
  2882. func EditCancelStock(sales models.CancelStock) {
  2883. 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
  2884. }
  2885. func EditDrugCancelStock(sales models.DrugCancelStock) {
  2886. 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
  2887. }
  2888. func FindCancelStockById(id int64, org_id int64) (cancelStock models.CancelStock, err error) {
  2889. err = readDb.Model(&models.CancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2890. return cancelStock, err
  2891. }
  2892. func FindCancelDrugById(id int64, org_id int64) (cancelStock models.DrugCancelStock, err error) {
  2893. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2894. return cancelStock, err
  2895. }
  2896. func FindDrugCancelStockById(id int64) (cancelStock models.DrugCancelStock, err error) {
  2897. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2898. return cancelStock, err
  2899. }
  2900. func UpDateCancelStockInfo(info *models.CancelStockInfo) (err error) {
  2901. err = writeDb.Save(&info).Error
  2902. return err
  2903. }
  2904. func UpDateDrugCancelStockInfo(info *models.DrugCancelStockInfo) (err error) {
  2905. err = writeDb.Save(&info).Error
  2906. return err
  2907. }
  2908. func FindWarehouseOutList(org_id int64, types int64) (list []*models.WarehouseOut, err error) {
  2909. 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
  2910. return list, err
  2911. }
  2912. func FindAllWarehouseOutInfo(org_id int64, types int64) (list []*models.WarehousingOutInfoConfig, err error) {
  2913. err = readDb.Model(&models.WarehousingOutInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2914. 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)
  2915. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2916. return list, err
  2917. }
  2918. type StockDetailModel struct {
  2919. GoodId int64 `gorm:"good_id" json:"good_id"`
  2920. GoodTypeId int64 `gorm:"good_type_id" json:"good_type_id"`
  2921. WarehousingOrder string `gorm:"warehousing_order" json:"warehousing_order"`
  2922. Price float64 `gorm:"price" json:"price"`
  2923. WarehousingCount int64 `gorm:"warehousing_count" json:"warehousing_count"`
  2924. Type int `gorm:"type" json:"type"`
  2925. Creater int64 `gorm:"creater" json:"creater"`
  2926. WarehousingTime int64 `gorm:"warehousing_time" json:"warehousing_time"`
  2927. Types int64 `gorm:"types" json:"types"`
  2928. }
  2929. 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) {
  2930. db := readDb.Model(&models.WarehousingInfo{})
  2931. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1 AND xt_warehouse_info.is_check = 1", orgId)
  2932. if len(keywords) > 0 {
  2933. likeKey := "%" + keywords + "%"
  2934. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2935. 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)
  2936. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2937. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2938. 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")
  2939. } else {
  2940. 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)
  2941. }
  2942. if manufacturer > 0 {
  2943. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2944. }
  2945. if storehouse_id > 0 {
  2946. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  2947. }
  2948. if order_type > 0 {
  2949. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2950. }
  2951. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  2952. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2953. db = db.Count(&total)
  2954. offset := (page - 1) * limit
  2955. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_info.id desc").Find(&list).Error
  2956. return
  2957. }
  2958. func GetStockInDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (err error, totalPrice float64) {
  2959. db := readDb.Model(&models.WarehousingInfo{})
  2960. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId)
  2961. if len(keywords) > 0 {
  2962. likeKey := "%" + keywords + "%"
  2963. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2964. 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)
  2965. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2966. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2967. 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")
  2968. } else {
  2969. 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)
  2970. }
  2971. if manufacturer > 0 {
  2972. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2973. }
  2974. if order_type > 0 {
  2975. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2976. }
  2977. if storehouse_id > 0 {
  2978. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  2979. }
  2980. type PriceAmount struct {
  2981. Total float64
  2982. }
  2983. var result []PriceAmount
  2984. err = db.Select("total_price as total").Scan(&result).Error
  2985. var total float64
  2986. for _, value := range result {
  2987. total = total + value.Total
  2988. }
  2989. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  2990. return err, total
  2991. }
  2992. func FloatRound(f float64, n int) float64 {
  2993. format := "%." + strconv.Itoa(n) + "f"
  2994. res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
  2995. return res
  2996. }
  2997. 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) {
  2998. if orgId == 9671 {
  2999. db := readDb.Model(&models.WarehouseOutInfo{})
  3000. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.is_check = 1 AND xt_warehouse_out_info.good_id <> 0 AND xt_warehouse_out_info.count <> 0 AND xt_warehouse_out_info.sys_record_time>=? AND xt_warehouse_out_info.sys_record_time<=?", orgId, startTime, endTime)
  3001. if len(keywords) > 0 {
  3002. likeKey := "%" + keywords + "%"
  3003. 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)
  3004. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  3005. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  3006. 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")
  3007. } else {
  3008. 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)
  3009. }
  3010. if manufacturer > 0 {
  3011. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  3012. }
  3013. if order_type > 0 {
  3014. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  3015. }
  3016. if storehouse_id > 0 {
  3017. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  3018. }
  3019. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  3020. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ?", orgId)
  3021. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3022. db = db.Count(&total)
  3023. offset := (page - 1) * limit
  3024. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  3025. return
  3026. } else {
  3027. db := readDb.Model(&models.WarehouseOutInfo{})
  3028. 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.count <> 0 ", orgId)
  3029. if len(keywords) > 0 {
  3030. likeKey := "%" + keywords + "%"
  3031. 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)
  3032. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  3033. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  3034. 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")
  3035. } else {
  3036. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id and xt_warehouse_out.org_id = xt_warehouse_out_info.org_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)
  3037. }
  3038. if manufacturer > 0 {
  3039. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  3040. }
  3041. if order_type > 0 {
  3042. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  3043. }
  3044. if storehouse_id > 0 {
  3045. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  3046. }
  3047. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ? and is_check =1", orgId)
  3048. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ? and is_check =1", orgId)
  3049. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3050. db = db.Count(&total)
  3051. offset := (page - 1) * limit
  3052. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  3053. return
  3054. }
  3055. }
  3056. func GetStockOutDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (err error, totalPrice float64) {
  3057. db := readDb.Model(&models.WarehouseOutInfo{})
  3058. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.count > 0", orgId)
  3059. if len(keywords) > 0 {
  3060. likeKey := "%" + keywords + "%"
  3061. 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)
  3062. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  3063. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  3064. 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")
  3065. } else {
  3066. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id and xt_warehouse_out.org_id = xt_warehouse_out_info.org_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)
  3067. }
  3068. if manufacturer > 0 {
  3069. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  3070. }
  3071. if order_type > 0 {
  3072. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  3073. }
  3074. if storehouse_id > 0 {
  3075. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  3076. }
  3077. type PriceAmount struct {
  3078. Total float64
  3079. }
  3080. var result []PriceAmount
  3081. err = db.Select("(count * price) as total").Scan(&result).Error
  3082. var total float64
  3083. for _, value := range result {
  3084. total = total + value.Total
  3085. }
  3086. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  3087. fmt.Println(total)
  3088. return err, total
  3089. }
  3090. 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) {
  3091. db := readDb.Model(&models.SalesReturnInfo{})
  3092. db = db.Where("xt_sales_return_info.org_id = ? AND xt_sales_return_info.status = 1", orgId)
  3093. if len(keywords) > 0 {
  3094. likeKey := "%" + keywords + "%"
  3095. 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)
  3096. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  3097. db = db.Joins("join xt_good_information on xt_good_information.id = xt_sales_return_info.good_id")
  3098. 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")
  3099. } else {
  3100. 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)
  3101. }
  3102. if manufacturer > 0 {
  3103. db = db.Joins("join xt_sales_return as sr on sr.id = xt_sales_return_info.sales_return_id AND sr.manufacturer =?", manufacturer)
  3104. }
  3105. if order_type > 0 {
  3106. db = db.Where("xt_sales_return_info.type = ?", order_type)
  3107. }
  3108. db = db.Preload("SalesReturn", "status = 1 AND org_id = ?", orgId)
  3109. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3110. offset := (page - 1) * limit
  3111. db = db.Count(&total)
  3112. err = db.Offset(offset).Limit(limit).Order("xt_sales_return_info.ctime desc").Find(&list).Error
  3113. return
  3114. }
  3115. 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) {
  3116. db := readDb.Model(&models.CancelStockInfo{})
  3117. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  3118. if len(keywords) > 0 {
  3119. likeKey := "%" + keywords + "%"
  3120. 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)
  3121. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  3122. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  3123. 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")
  3124. } else {
  3125. 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)
  3126. }
  3127. if manufacturer > 0 {
  3128. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  3129. }
  3130. if order_type > 0 {
  3131. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  3132. }
  3133. if storehouse_id > 0 {
  3134. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", order_type)
  3135. }
  3136. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  3137. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3138. err = db.Group("xt_cancel_stock_info.good_id").Order("xt_cancel_stock_info.ctime desc").Find(&list).Error
  3139. return
  3140. }
  3141. 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) {
  3142. db := readDb.Model(&models.CancelStockInfo{})
  3143. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  3144. if len(keywords) > 0 {
  3145. likeKey := "%" + keywords + "%"
  3146. 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)
  3147. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  3148. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  3149. 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")
  3150. } else {
  3151. 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)
  3152. }
  3153. if manufacturer > 0 {
  3154. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  3155. }
  3156. if order_type > 0 {
  3157. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  3158. }
  3159. if storehouse_id > 0 {
  3160. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", storehouse_id)
  3161. }
  3162. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  3163. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3164. db = db.Count(&total)
  3165. offset := (page - 1) * limit
  3166. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock_info.id desc").Find(&list).Error
  3167. return
  3168. }
  3169. func FindStockInByDealerId(id int64) (total int64, err error) {
  3170. err = readDb.Model(&models.Warehousing{}).Where("dealer = ? AND status = 1", id).Count(&total).Error
  3171. return
  3172. }
  3173. func FindStockInByManufacturerId(id int64) (total int64, err error) {
  3174. err = readDb.Model(&models.Warehousing{}).Where("manufacturer = ? AND status = 1", id).Count(&total).Error
  3175. return
  3176. }
  3177. func FindGoodTypeByName(name string, org_id int64) (total int64) {
  3178. readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 AND type_name = ? ", org_id, name).Count(&total)
  3179. return
  3180. }
  3181. func FindGoodInfoByName(name string, org_id int64, good_type_id int64) (total int64) {
  3182. 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)
  3183. return
  3184. }
  3185. func FindGoodInfoByNameTwo(name string, org_id int64, good_type_id int64, good_name string) (total int64) {
  3186. 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)
  3187. return
  3188. }
  3189. func FindGoodInfoByNameOne(name string, org_id int64, good_type_id int64, id int64, goodname string) (total int64) {
  3190. 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)
  3191. return
  3192. }
  3193. func FindAllGoodTypeByType(types int64) (goodType []*models.GoodsType, err error) {
  3194. err = readDb.Model(&models.GoodsType{}).Where("type = ? AND status = 1", types).Find(&goodType).Error
  3195. return goodType, err
  3196. }
  3197. func FindWarehouseInfoByGoodType(good_type_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  3198. 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
  3199. return info, err
  3200. }
  3201. func FindStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.WarehouseOut, err error) {
  3202. 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
  3203. return
  3204. }
  3205. func FindStockOutByIsSysOne(org_id int64, is_sys int, record_time int64, warehousing_out_order string, tx *gorm.DB) (out models.WarehouseOut, err error) {
  3206. err = tx.Model(&models.WarehouseOut{}).Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ? and warehouse_out_order_number = ?", is_sys, org_id, record_time, warehousing_out_order).First(&out).Error
  3207. if err != gorm.ErrRecordNotFound {
  3208. if err != nil {
  3209. tx.Rollback()
  3210. return
  3211. }
  3212. }
  3213. return
  3214. }
  3215. func FindStockOutByIsSysSix(org_id int64, is_sys int, record_time int64, second_warehouse_id int64) (out models.WarehouseOut, err error) {
  3216. 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
  3217. return
  3218. }
  3219. func FindDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  3220. 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
  3221. return
  3222. }
  3223. func FindNewDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64, tx *gorm.DB) (out models.DrugWarehouseOut, err error) {
  3224. err = tx.Model(&models.DrugWarehouseOut{}).Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ?", is_sys, org_id, record_time).First(&out).Error
  3225. if err != gorm.ErrRecordNotFound {
  3226. if err != nil {
  3227. tx.Rollback()
  3228. return
  3229. }
  3230. }
  3231. return
  3232. }
  3233. func FindDrugSettleStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  3234. 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
  3235. return
  3236. }
  3237. func FindLastStockInInfoRecord(good_id int64, org_id int64) (in models.WarehousingInfo, err error) {
  3238. err = readDb.Model(&models.WarehousingInfo{}).Where("status = 1 AND org_id = ? AND good_id = ?", org_id, good_id).First(&in).Error
  3239. return
  3240. }
  3241. func FindLastDrugStockInInfoRecord(drug_id int64, org_id int64) (in models.DrugWarehouseOutInfo, err error) {
  3242. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("status = 1 AND org_id = ? AND drug_id = ?", org_id, drug_id).Last(&in).Error
  3243. return
  3244. }
  3245. func GetStockInRecoredByGoodId(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  3246. info := models.WarehouseOutInfo{}
  3247. 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
  3248. if err == gorm.ErrRecordNotFound {
  3249. return nil, err
  3250. }
  3251. if err != nil {
  3252. return nil, err
  3253. }
  3254. return &info, nil
  3255. }
  3256. func GetStockInRecoredByGoodIdOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64) (*models.WarehouseOutInfo, error) {
  3257. info := models.WarehouseOutInfo{}
  3258. 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
  3259. if err == gorm.ErrRecordNotFound {
  3260. return nil, err
  3261. }
  3262. if err != nil {
  3263. return nil, err
  3264. }
  3265. return &info, nil
  3266. }
  3267. func UpdateAutoMaticReduceDetail(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  3268. 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
  3269. return err
  3270. }
  3271. func UpdateAutoMaticReduceDetailOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  3272. 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
  3273. return err
  3274. }
  3275. func GetGoodsIsExsit(good_id int64, sys_record_time int64, org_id int64, warehouse_out_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  3276. outInfo := models.WarehouseOutInfo{}
  3277. err := tx.Where("good_id = ? and sys_record_time = ? and org_id = ? and is_sys =0 and status = 1 and warehouse_out_id = ?", good_id, sys_record_time, org_id, warehouse_out_id).Find(&outInfo).Error
  3278. if err != gorm.ErrRecordNotFound {
  3279. if err != nil {
  3280. tx.Rollback()
  3281. return outInfo, err
  3282. }
  3283. }
  3284. return outInfo, err
  3285. }
  3286. func GetDrugsIsExsit(drug_id int64, sys_record_time int64, org_id int64, warehouse_out_id int64) (models.XtDrugWarehouseOutInfo, error) {
  3287. outInfo := models.XtDrugWarehouseOutInfo{}
  3288. err := XTReadDB().Where("drug_id = ? and sys_record_time = ? and org_id = ? and is_sys =0 and status = 1 and warehouse_out_id = ?", drug_id, sys_record_time, org_id, warehouse_out_id).Find(&outInfo).Error
  3289. return outInfo, err
  3290. }
  3291. func GetNewDrugsIsExsit(drug_id int64, sys_record_time int64, org_id int64, warehouse_out_id int64, tx *gorm.DB) (models.XtDrugWarehouseOutInfo, error) {
  3292. outInfo := models.XtDrugWarehouseOutInfo{}
  3293. err := tx.Where("drug_id = ? and sys_record_time = ? and org_id = ? and is_sys =0 and status = 1 and warehouse_out_id = ?", drug_id, sys_record_time, org_id, warehouse_out_id).Find(&outInfo).Error
  3294. if err != gorm.ErrRecordNotFound {
  3295. if err != nil {
  3296. tx.Rollback()
  3297. return outInfo, err
  3298. }
  3299. }
  3300. return outInfo, err
  3301. }
  3302. func AddSigleWarehouseOutInfoOne(info *models.WarehouseOutInfo) error {
  3303. err := XTWriteDB().Create(&info).Error
  3304. return err
  3305. }
  3306. func AddSigleWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) error {
  3307. err := tx.Create(&info).Error
  3308. if err != nil {
  3309. tx.Rollback()
  3310. return err
  3311. }
  3312. return err
  3313. }
  3314. func AddNewSigleWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) error {
  3315. err := tx.Create(&info).Error
  3316. if err != nil {
  3317. tx.Rollback()
  3318. return err
  3319. }
  3320. return err
  3321. }
  3322. func UpdateStockOut(warehouseinfoid int64, info models.WarehousingInfo) error {
  3323. err := XTWriteDB().Model(&info).Where("id=? and status = 1", warehouseinfoid).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  3324. return err
  3325. }
  3326. func AddSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo) error {
  3327. err := writeDb.Create(&info).Error
  3328. return err
  3329. }
  3330. func AddSelfSigleDrugWarehouseOutInfo(info *models.XtSelfDrugWarehouseOutInfo, tx *gorm.DB) error {
  3331. err := tx.Create(&info).Error
  3332. if err != nil {
  3333. tx.Rollback()
  3334. return err
  3335. }
  3336. return err
  3337. }
  3338. func AddNewSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, tx *gorm.DB) error {
  3339. err := tx.Create(&info).Error
  3340. if err != nil {
  3341. tx.Rollback()
  3342. return err
  3343. }
  3344. return err
  3345. }
  3346. func UpdateSigleDrugWarehouseOutInfoDetail(id int64, count int64, tx *gorm.DB) error {
  3347. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
  3348. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).UpdateColumn("over_count", gorm.Expr("over_count - ?", count)).Error
  3349. if err != nil {
  3350. tx.Rollback()
  3351. return err
  3352. }
  3353. return err
  3354. }
  3355. func UpdateSigleDrugWarehouseOutInfoDetailOne(id int64, count int64, tx *gorm.DB) error {
  3356. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"count": count}).Error
  3357. return err
  3358. }
  3359. func AddSigleAutoReduceRecordInfo(detail *models.AutomaticReduceDetail) error {
  3360. ut := writeDb.Begin()
  3361. err := ut.Create(&detail).Error
  3362. if err != nil {
  3363. ut.Rollback()
  3364. return err
  3365. }
  3366. ut.Commit()
  3367. return err
  3368. }
  3369. func AddSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail) error {
  3370. err := writeDb.Create(&detail).Error
  3371. return err
  3372. }
  3373. func GetDrugAutoMaticReduceDetail(drug_id int64, patient_id int64, record_time int64, org_id int64, adivce_id int64, warehouse_info_id int64, tx *gorm.DB) (models.DrugAutomaticReduceDetail, error) {
  3374. detail := models.DrugAutomaticReduceDetail{}
  3375. err := tx.Where("drug_id = ? and patient_id =? and record_time =? and org_id = ? and advice_id = ? and warehouse_info_id =?", drug_id, patient_id, record_time, org_id, adivce_id, warehouse_info_id).Find(&detail).Error
  3376. if err != gorm.ErrRecordNotFound {
  3377. if err != nil {
  3378. tx.Rollback()
  3379. return detail, err
  3380. }
  3381. }
  3382. return detail, err
  3383. }
  3384. func UpdateNewSingleDrugAutoReduceRecordInfo(id int64, count int64, tx *gorm.DB) error {
  3385. err := tx.Model(models.DrugAutomaticReduceDetail{}).Where("id = ? and status =1", id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
  3386. if err != nil {
  3387. tx.Rollback()
  3388. return err
  3389. }
  3390. return err
  3391. }
  3392. func AddNewSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail, tx *gorm.DB) error {
  3393. err := tx.Create(&detail).Error
  3394. if err != nil {
  3395. tx.Rollback()
  3396. return err
  3397. }
  3398. return err
  3399. }
  3400. func FindStockOutInfoByTypeId(org_id int64, good_type_id int64, good_id int64, out_id int64, number string) (out models.WarehouseOutInfo, err error) {
  3401. 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
  3402. return
  3403. }
  3404. func FindStockOutInfoByStock(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (out []*models.WarehouseOutInfo, err error) {
  3405. 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
  3406. return
  3407. }
  3408. func FindStockOutInfoByStockOne(org_id int64, good_type_id int64, good_id int64, record_date int64) (out []*models.WarehouseOutInfo, err error) {
  3409. 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
  3410. return
  3411. }
  3412. func FindStockOutInfoByStockTwo(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (models.WarehouseOutInfo, error) {
  3413. info := models.WarehouseOutInfo{}
  3414. 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
  3415. return info, err
  3416. }
  3417. func FindDrugStockOutInfoByTypeId(org_id int64, drug_id int64, out_id int64, number string) (out models.DrugWarehouseOutInfo, err error) {
  3418. 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
  3419. return
  3420. }
  3421. func UpdateStockOutInfoCount(org_id int64, id int64) {
  3422. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", 1))
  3423. }
  3424. func FindUserDetailById(org_id int64, id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  3425. db := readDb.Model(&models.AutomaticReduceDetail{})
  3426. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  3427. db = db.Preload("GoodsType", "status = 1")
  3428. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3429. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND good_id != 0 AND count <> 0 ", org_id, id)
  3430. db = db.Count(&total)
  3431. err = db.Find(&user).Error
  3432. return
  3433. }
  3434. type DrugAutomaticReduceDetail struct {
  3435. ID int64 `gorm:"column:id" json:"id" form:"id"`
  3436. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  3437. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  3438. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  3439. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  3440. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  3441. Status int64 `gorm:"column:status" json:"status" form:"status"`
  3442. RecordTime int64 `gorm:"column:record_time" json:"record_time" form:"record_time"`
  3443. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  3444. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  3445. Count int64 `gorm:"column:count" json:"count" form:"count"`
  3446. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  3447. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  3448. Patients *models.Patients `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"user"`
  3449. DrugWarehouseOutInfo []*models.DrugWarehouseOutInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"drugwarehouseoutinfo"`
  3450. }
  3451. func (DrugAutomaticReduceDetail) TableName() string {
  3452. return "xt_drug_automatic_reduce_detail"
  3453. }
  3454. func FindDrugStockUserDetailById(org_id int64, id int64) (user []*DrugAutomaticReduceDetail, err error, total int64) {
  3455. db := readDb.Model(&DrugAutomaticReduceDetail{})
  3456. db = db.Preload("Drug", "org_id = ? AND status = 1", org_id)
  3457. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3458. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND drug_id != 0", org_id, id)
  3459. db = db.Count(&total)
  3460. err = db.Find(&user).Error
  3461. return
  3462. }
  3463. func UpdateStockOutInfoCount2(org_id int64, id int64, count int64) {
  3464. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3465. }
  3466. func UpdateDrugStockOutInfoCount2(org_id int64, id int64, count int64) {
  3467. writeDb.Model(&models.DrugWarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3468. }
  3469. func FindPatientAutomaticReduceRecord(org_id int64, record_time int64, good_id int64, good_type_id int64, patient_id int64) (count int64, err error) {
  3470. 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
  3471. return
  3472. }
  3473. func FindPatientDrugAutomaticReduceRecord(org_id int64, record_time int64, drug_id int64, patient_id int64) (count int64, err error) {
  3474. 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
  3475. return
  3476. }
  3477. type VMWarehousingInfo struct {
  3478. ID int64 `gorm:"column:id" json:"id"`
  3479. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id"`
  3480. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  3481. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  3482. Status int64 `gorm:"column:status" json:"status"`
  3483. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3484. GoodInfo GoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"good_info"`
  3485. }
  3486. func (VMWarehousingInfo) TableName() string {
  3487. return "xt_warehouse_info"
  3488. }
  3489. type GoodInfo struct {
  3490. ID int64 `gorm:"column:id" json:"id"`
  3491. SpecificationName string `gorm:"column:specification_name" json:"specification_name"`
  3492. Status int64 `gorm:"column:status" json:"status"`
  3493. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3494. }
  3495. func (GoodInfo) TableName() string {
  3496. return "xt_good_information"
  3497. }
  3498. func GetStockInGoodInfo(orgId int64) (stockIn []*VMWarehousingInfo, err error) {
  3499. err = readDb.Model(&VMWarehousingInfo{}).Preload("GoodInfo", "status = 1 AND org_id = ?", orgId).Where("org_id = ? AND status = 1", orgId).Find(&stockIn).Error
  3500. return
  3501. }
  3502. func GetAllStockOutUserDetail(patient_id int64, orgId int64, record_time int64) (goodUser []*models.DialysisBeforePrepare, err error) {
  3503. 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
  3504. return
  3505. }
  3506. func PostSearchStock(keyword string, orgid int64) (goods []*models.GoodsType, err error) {
  3507. likeKey := "%" + keyword + "%"
  3508. db := XTReadDB().Model(&goods)
  3509. if len(keyword) > 0 {
  3510. db = db.Where("type_name like ?", likeKey)
  3511. }
  3512. err = db.Where("org_id = ? and status =1", orgid).Find(&goods).Error
  3513. return goods, err
  3514. }
  3515. func GetStockType(orgid int64) (goodstype []*models.GoodsTypeOne, err error) {
  3516. err = XTReadDB().Where("org_id = ? and status = 1 and (stock_attribute = 2 or stock_attribute = 3 or stock_attribute = 4)", orgid).
  3517. Preload("GoodInfo", "org_id = ? and status = 1 and find_in_set('停用',good_status) = 0", orgid).Find(&goodstype).Error
  3518. return goodstype, err
  3519. }
  3520. func GetDialyStockOut(orgid int64, recordtime int64, patientId int64) (prepare []*models.XtDialysisBeforePrepare, err error) {
  3521. db := XTReadDB().Table("dialysis_before_prepare as x").Where("x.status = 1")
  3522. if orgid > 0 {
  3523. db = db.Where("x.user_org_id = ?", orgid)
  3524. }
  3525. if recordtime > 0 {
  3526. db = db.Where("x.record_date = ?", recordtime)
  3527. }
  3528. if patientId > 0 {
  3529. db = db.Where("x.patient_id = ?", patientId)
  3530. }
  3531. 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
  3532. return prepare, err
  3533. }
  3534. func GetAutoReduceRecordInfoById(orgid int64, recordtime int64, goodid int64, goodtypeid int64) (*models.XtAutomaticReduceDetail, error) {
  3535. detail := models.XtAutomaticReduceDetail{}
  3536. 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
  3537. if err == gorm.ErrRecordNotFound {
  3538. return nil, err
  3539. }
  3540. if err != nil {
  3541. return nil, err
  3542. }
  3543. return &detail, nil
  3544. }
  3545. func GetOutStockTotalCount(wareouttime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3546. 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
  3547. return autoMatic, err
  3548. }
  3549. func GetOutStockTotalCountOne(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3550. 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
  3551. return autoMatic, err
  3552. }
  3553. func GetManufacturerName(orgid int64, name string) (*models.Manufacturer, error) {
  3554. var manufacturer models.Manufacturer
  3555. var err error
  3556. err = XTReadDB().Model(&manufacturer).Where("org_id = ? and manufacturer_name = ? and status = 1", orgid, name).Find(&manufacturer).Error
  3557. if err == gorm.ErrRecordNotFound {
  3558. return nil, err
  3559. }
  3560. if err != nil {
  3561. return nil, err
  3562. }
  3563. return &manufacturer, nil
  3564. }
  3565. func CreateManufacturer(manufacturer *models.Manufacturer) error {
  3566. err := XTWriteDB().Create(manufacturer).Error
  3567. return err
  3568. }
  3569. func UpdatedManufactur(manufacturer *models.Manufacturer, orgid int64) error {
  3570. err := XTWriteDB().Model(&manufacturer).Where("org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"manufacturer_name": manufacturer.ManufacturerName}).Error
  3571. return err
  3572. }
  3573. func GetGoodsInformaitonIsExist(goodName string, goodType int64, specificationName string, orgid int64) (*models.GoodInfo, error) {
  3574. var good models.GoodInfo
  3575. var err error
  3576. 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
  3577. if err == gorm.ErrRecordNotFound {
  3578. return nil, err
  3579. }
  3580. if err != nil {
  3581. return nil, err
  3582. }
  3583. return &good, nil
  3584. }
  3585. func CreateGoodsInfomation(info *models.GoodInfo) error {
  3586. err := XTWriteDB().Create(&info).Error
  3587. return err
  3588. }
  3589. func UpdateGoodsInformation(info *models.GoodInfo, goodName string, goodType int64, specificationName string, orgid int64) error {
  3590. err := XTWriteDB().Model(&info).Where("good_name = ? and good_type_id = ? and specification_name = ? and org_id = ? and status = 1", goodName, goodType, specificationName, orgid).Updates(map[string]interface{}{"good_name": info.GoodName, "good_type_id": info.GoodTypeId, "medical_insurance_level": info.MedicalInsuranceLevel, "good_kind": info.GoodKind, "specification_name": info.SpecificationName, "manufacturer": info.Manufacturer, "good_unit": info.GoodUnit, "retail_price": info.RetailPrice, "stock_warn_count": info.StockWarnCount, "dealer": info.Dealer, "pinyin": info.Pinyin, "wubi": info.Wubi, "buy_price": info.BuyPrice, "medical_insurance_number": info.MedicalInsuranceNumber, "is_special_diseases": info.IsSpecialDiseases, "is_record": info.IsRecord, "statistics_category": info.StatisticsCategory, "good_status": info.GoodStatus, "social_security_directory_code": info.SocialSecurityDirectoryCode, "production_type": info.ProductionType, "special_medical": info.SpecialMedical, "remark": info.Remark, "number": info.Number, "register_number": info.RegisterNumber}).Error
  3591. return err
  3592. }
  3593. func GetCoutWareseOutInfo(startime int64, endtime int64, orgid int64) (houseOutInfo []*models.VmWarehouseOutInfo, err error) {
  3594. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  3595. 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
  3596. return houseOutInfo, err
  3597. }
  3598. func GetGoodInfomationList(orgid int64, keyword string) (goodInfo []*models.GoodInfo, err error) {
  3599. key := "%" + keyword + "%"
  3600. if len(keyword) == 0 {
  3601. err = XTReadDB().Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Find(&goodInfo).Error
  3602. } else {
  3603. err = XTReadDB().Where("org_id = ? and status = 1 AND good_name like ? AND find_in_set('停用',good_status) = 0", orgid, key).Find(&goodInfo).Error
  3604. }
  3605. return goodInfo, err
  3606. }
  3607. func IsExistDrugByName(drugname string, drugdesc string, orgid int64) (*models.BaseDrugLib, error) {
  3608. var drug models.BaseDrugLib
  3609. var err error
  3610. err = XTReadDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and status =1 and org_id = ?", drugname, drugdesc, orgid).Find(&drug).Error
  3611. if err == gorm.ErrRecordNotFound {
  3612. return nil, err
  3613. }
  3614. if err != nil {
  3615. return nil, err
  3616. }
  3617. return &drug, nil
  3618. }
  3619. func IsExistDrugByNameOne(drugname string, dosage string, doseunit string, minnumber int64, minunit string, packtunit string, orgid int64) (*models.BaseDrugLib, error) {
  3620. var drug models.BaseDrugLib
  3621. var err error
  3622. 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
  3623. if err == gorm.ErrRecordNotFound {
  3624. return nil, err
  3625. }
  3626. if err != nil {
  3627. return nil, err
  3628. }
  3629. return &drug, nil
  3630. }
  3631. func CreateDrugsInfomation(info *models.BaseDrugLib) error {
  3632. err := XTWriteDB().Create(&info).Error
  3633. return err
  3634. }
  3635. func UpdateDrugsInformation(drug *models.BaseDrugLib, drugName string, drugSpec string, orgid int64) error {
  3636. err := XTWriteDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and org_id = ? and status = 1", drugName, drugSpec, orgid).Updates(map[string]interface{}{"drug_name": drug.DrugName, "drug_alias": drug.DrugAlias, "drug_spec": drug.DrugSpec, "drug_type": drug.DrugType, "drug_stock_limit": drug.DrugStockLimit, "drug_origin_place": drug.DrugOriginPlace, "drug_dosage_form": drug.DrugDosageForm, "medical_insurance_level": drug.MedicalInsuranceLevel, "max_unit": drug.MaxUnit, "min_unit": drug.MinUnit, "unit_matrixing": drug.UnitMatrixing, "retail_price": drug.RetailPrice, "last_price": drug.LastPrice, "drug_classify": drug.DrugClassify, "manufacturer": drug.Manufacturer, "dealer": drug.Dealer, "pinyin": drug.Pinyin, "wubi": drug.Wubi, "drug_alias_pinyin": drug.DrugAliasPinyin, "drug_alias_wubi": drug.DrugAliasWubi, "drug_category": drug.DrugCategory, "drug_control": drug.DrugControl, "number": drug.Number, "hosp_appr_flag": drug.HospApprFlag, "medical_insurance_number": drug.MedicalInsuranceNumber, "pharmacology_category": drug.PharmacologyCategory, "statistics_category": drug.StatisticsCategory, "code": drug.Code, "is_special_diseases": drug.IsSpecialDiseases, "is_record": drug.IsRecord, "prescription_mark": drug.PrescriptionMark, "social_security_directory_code": drug.SocialSecurityDirectoryCode, "record_date": drug.RecordDate, "drug_remark": drug.DrugRemark, "drug_status": drug.DrugStatus, "limit_remark": drug.LimitRemark, "drug_dose": drug.DrugDose, "single_dose": drug.DrugDose, "drug_dose_unit": drug.DrugDoseUnit, "lmt_used_flag": drug.LmtUsedFlag, "delivery_way": drug.DeliveryWay, "execution_frequency": drug.ExecutionFrequency, "drug_day": drug.DrugDay}).Error
  3637. return err
  3638. }
  3639. func GetProjectByNameIsExsit(projectName string, orgid int64) (*models.HisProject, error) {
  3640. var projoect models.HisProject
  3641. var err error
  3642. err = XTReadDB().Model(&projoect).Where("project_name = ? and user_org_id = ? and status =1", projectName, orgid).Find(&projoect).Error
  3643. if err == gorm.ErrRecordNotFound {
  3644. return nil, err
  3645. }
  3646. if err != nil {
  3647. return nil, err
  3648. }
  3649. return &projoect, nil
  3650. }
  3651. func CreateProjectInfomation(info *models.HisProject) error {
  3652. err := XTWriteDB().Create(&info).Error
  3653. return err
  3654. }
  3655. func UpdateProjectByName(project *models.HisProject, projectName string, orgid int64) error {
  3656. 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
  3657. return err
  3658. }
  3659. func GetWareOutInfoById(warehouseoutid int64, orgid int64) (warehouseinfo []*models.WarehouseOutInfo, err error) {
  3660. err = XTReadDB().Model(&warehouseinfo).Where("warehouse_out_id = ? and org_id = ? and status= 1 and is_sys = 0", warehouseoutid, orgid).Find(&warehouseinfo).Error
  3661. return warehouseinfo, err
  3662. }
  3663. func EditDrugWarehousingOne(warehouse models.DrugWarehouse, id int64) error {
  3664. 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
  3665. return err
  3666. }
  3667. func EditNewDrugWarehousingOne(warehouse models.DrugWarehouse, id int64, tx *gorm.DB) error {
  3668. err := tx.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
  3669. if err != nil {
  3670. tx.Rollback()
  3671. return err
  3672. }
  3673. return err
  3674. }
  3675. func GetAllManufacturerList(orgid int64) (list []*models.Manufacturer, err error) {
  3676. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3677. return list, err
  3678. }
  3679. func GetNewAllManufacturerList(orgid int64) (list []*models.BloodManufacturer, err error) {
  3680. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3681. return list, err
  3682. }
  3683. func GetAllDealerList(orgid int64) (list []*models.Dealer, err error) {
  3684. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3685. return list, err
  3686. }
  3687. // func GetAllDealerListTX(orgid int64,tx *gorm.DB) (list []*models.Dealer, err error) {
  3688. // err = tx.Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3689. // return list, err
  3690. // }
  3691. func GetDealerByName(orgid int64, name string) (*models.Dealer, error) {
  3692. var dealer models.Dealer
  3693. var err error
  3694. err = XTReadDB().Model(&dealer).Where("org_id = ? and dealer_name = ? and status = 1", orgid, name).Find(&dealer).Error
  3695. if err == gorm.ErrRecordNotFound {
  3696. return nil, err
  3697. }
  3698. if err != nil {
  3699. return nil, err
  3700. }
  3701. return &dealer, nil
  3702. }
  3703. func CreateDealer(dealer *models.Dealer) error {
  3704. err := XTWriteDB().Create(&dealer).Error
  3705. return err
  3706. }
  3707. func UpdateDealer(dealer *models.Dealer, orgid int64) error {
  3708. err := XTWriteDB().Model(&dealer).Where("org_id = ? and status =1", orgid).Updates(map[string]interface{}{"dealer_name": dealer.DealerName}).Error
  3709. return err
  3710. }
  3711. func GetDrugDataConfig(orgid int64, name string) (models.DictDataconfig, error) {
  3712. dataconfig := models.DictDataconfig{}
  3713. err := XTReadDB().Where("org_id = ? and name = ?", orgid, name).Find(&dataconfig).Error
  3714. return dataconfig, err
  3715. }
  3716. func GetParentDataConfig(parentid int64, orgid int64) (dataconfig []*models.DictDataconfig, err error) {
  3717. err = XTReadDB().Where("parent_id = ? and status =1 and (org_id = ? or org_id = 0)", parentid, orgid).Find(&dataconfig).Error
  3718. return dataconfig, err
  3719. }
  3720. func IsExistDicConfig(parentid int64, goodKind string, orgid int64) (*models.DictDataconfig, error) {
  3721. var dataconfig models.DictDataconfig
  3722. var err error
  3723. err = XTReadDB().Where("parent_id = ? and status = 1 and name = ? and (org_id = ? or org_id = 0)", parentid, goodKind, orgid).Find(&dataconfig).Error
  3724. if err == gorm.ErrRecordNotFound {
  3725. return nil, err
  3726. }
  3727. if err != nil {
  3728. return nil, err
  3729. }
  3730. return &dataconfig, nil
  3731. }
  3732. func GetLastDicConfig(parentid int64, orgid int64) (models.DictDataconfig, error) {
  3733. dataconfig := models.DictDataconfig{}
  3734. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status = 1", parentid, orgid).Last(&dataconfig).Error
  3735. return dataconfig, err
  3736. }
  3737. func CreatedDicConfig(dataconfig *models.DictDataconfig) error {
  3738. err := XTWriteDB().Create(&dataconfig).Error
  3739. return err
  3740. }
  3741. func UpdatedDicConfig(parentid int64, dataconfig *models.DictDataconfig, orgid int64, goodKind string) error {
  3742. err := XTWriteDB().Model(&dataconfig).Where("parent_id = ? and org_id = ? and name = ?", parentid, orgid, goodKind).Updates(map[string]interface{}{"name": dataconfig.Name}).Error
  3743. return err
  3744. }
  3745. func GetGoodType(goodName string, orgid int64) (*models.GoodsType, error) {
  3746. var goodType models.GoodsType
  3747. var err error
  3748. err = XTReadDB().Where("type_name =? and org_id = ? and status = 1", goodName, orgid).Find(&goodType).Error
  3749. if err == gorm.ErrRecordNotFound {
  3750. return nil, err
  3751. }
  3752. if err != nil {
  3753. return nil, err
  3754. }
  3755. return &goodType, nil
  3756. }
  3757. func CreatedGoodType(goodsType *models.GoodsType) error {
  3758. err := XTWriteDB().Create(&goodsType).Error
  3759. return err
  3760. }
  3761. func GetAllGoodType(orgid int64) (goodsType []*models.GoodsType, err error) {
  3762. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3763. return goodsType, err
  3764. }
  3765. func GetAllNewGoodType(orgid int64) (goodsType []*models.NewGoodsType, err error) {
  3766. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3767. return goodsType, err
  3768. }
  3769. func GetDepartMentByName(name string, orgid int64) (*models.XtHisDepartment, error) {
  3770. var department models.XtHisDepartment
  3771. var err error
  3772. err = XTReadDB().Where("name= ? and user_org_id = ? and status = 1", name, orgid).Find(&department).Error
  3773. if err == gorm.ErrRecordNotFound {
  3774. return nil, err
  3775. }
  3776. if err != nil {
  3777. return nil, err
  3778. }
  3779. return &department, nil
  3780. }
  3781. func GetDataConfigIsExist(orgid int64, name string) (models.Dataconfig, error) {
  3782. dataconfig := models.Dataconfig{}
  3783. err := XTReadDB().Model(&dataconfig).Where("org_id = ? and name = ? and status = 1", orgid, name).Find(&dataconfig).Error
  3784. return dataconfig, err
  3785. }
  3786. func GetChildeConfigIsExist(parentid int64, name string, orgid int64) (*models.Dataconfig, error) {
  3787. var dataconfig models.Dataconfig
  3788. var err error
  3789. 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
  3790. if err == gorm.ErrRecordNotFound {
  3791. return nil, err
  3792. }
  3793. if err != nil {
  3794. return nil, err
  3795. }
  3796. return &dataconfig, nil
  3797. }
  3798. func GetLastChildeConfig(parentid int64, orgid int64) (models.Dataconfig, error) {
  3799. dataconfig := models.Dataconfig{}
  3800. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status =1", parentid, orgid).Last(&dataconfig).Error
  3801. return dataconfig, err
  3802. }
  3803. func CreateDataConfig(dataconfig *models.Dataconfig) error {
  3804. err := XTWriteDB().Create(&dataconfig).Error
  3805. return err
  3806. }
  3807. func FindAllDataConfigList(orgid int64, parentId int64) (dataconfig []*models.Dataconfig, err error) {
  3808. err = XTReadDB().Model(&dataconfig).Where("(org_id = ? or org_id = 0) and parent_id = ? and status = 1", orgid, parentId).Find(&dataconfig).Error
  3809. return dataconfig, err
  3810. }
  3811. func GetWarehouseOrderInfoList(orgid int64) (list []*models.WarehousingInfo, err error) {
  3812. err = XTReadDB().Model(&list).Where("org_id = ? and status = 1", orgid).Order("id asc").Group("good_id").Find(&list).Error
  3813. return list, err
  3814. }
  3815. func GetSearchDrugList(keywords string, orgid int64) (drug []*models.BaseDrugLib, err error) {
  3816. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3817. if len(keywords) > 0 {
  3818. likekey := "%" + keywords + "%"
  3819. err = db.Where("(drug_name LIKE ?)", likekey).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3820. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3821. }).Find(&drug).Error
  3822. } else {
  3823. err = db.Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3824. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3825. }).Find(&drug).Error
  3826. }
  3827. return
  3828. }
  3829. func GetSearchDrugListSix(keywords string, orgid int64, storehouse_id int64) (drug []*models.BaseDrugLib, err error) {
  3830. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3831. if len(keywords) > 0 {
  3832. likekey := "%" + keywords + "%"
  3833. err = db.Where("(drug_name LIKE ?)", likekey).Preload("LastDrugWarehouseInfo", "org_id=? and status = 1 and (stock_max_number > 0 or stock_min_number>0) and storehouse_id = ?", orgid, storehouse_id).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3834. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3835. }).Find(&drug).Error
  3836. } else {
  3837. err = db.Preload("LastDrugWarehouseInfo", "org_id=? and status = 1 and (stock_max_number > 0 or stock_min_number>0) and storehouse_id = ?", orgid, storehouse_id).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3838. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3839. }).Find(&drug).Error
  3840. }
  3841. return
  3842. }
  3843. func GetSearchGoodList(keywords string, orgid int64) (good []*models.GoodInfo, err error) {
  3844. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 and good_status <> '停用'", orgid)
  3845. if len(keywords) > 0 {
  3846. likekey := "%" + keywords + "%"
  3847. err = db.Where("(good_name Like ?)", likekey).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3848. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3849. }).Find(&good).Error
  3850. } else {
  3851. err = db.Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3852. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3853. }).Find(&good).Error
  3854. }
  3855. return
  3856. }
  3857. func GetSearchGoodListSix(keywords string, orgid int64, storehouse_id int64) (good []*models.GoodInfoSevenThirty, err error) {
  3858. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 AND find_in_set('停用',good_status) = 0", orgid)
  3859. if len(keywords) > 0 {
  3860. likekey := "%" + keywords + "%"
  3861. err = db.Where("(good_name Like ?)", likekey).Preload("LaStWarehousingInfo", "org_id=? and status = 1 and stock_count <> 0 and storehouse_id =?", orgid, storehouse_id).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3862. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id =?", orgid, storehouse_id)
  3863. }).Find(&good).Error
  3864. } else {
  3865. err = db.Preload("LaStWarehousingInfo", "org_id=? and status = 1 and stock_count <> 0 and storehouse_id =?", orgid, storehouse_id).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3866. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id)
  3867. }).Find(&good).Error
  3868. }
  3869. return
  3870. }
  3871. func CreateAutoReduceRecord(detail *models.BloodAutomaticReduceDetail) error {
  3872. db := writeDb.Begin()
  3873. err := db.Create(&detail).Error
  3874. if err != nil {
  3875. db.Rollback()
  3876. return err
  3877. }
  3878. db.Commit()
  3879. return err
  3880. }
  3881. func GetAutoMaticReduceDetail(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (*models.BloodAutomaticReduceDetail, error) {
  3882. var autoreduece models.BloodAutomaticReduceDetail
  3883. var err error
  3884. 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
  3885. if err == gorm.ErrRecordNotFound {
  3886. return nil, err
  3887. }
  3888. if err != nil {
  3889. return nil, err
  3890. }
  3891. return &autoreduece, nil
  3892. }
  3893. func GetAutoMaticReduceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (models.BloodAutomaticReduceDetail, error) {
  3894. auto := models.BloodAutomaticReduceDetail{}
  3895. 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
  3896. return auto, err
  3897. }
  3898. func DeleteAutoRedeceDetailTwo(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) error {
  3899. detail := models.BloodAutomaticReduceDetail{}
  3900. 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
  3901. return err
  3902. }
  3903. func GetWarehosueOutByStockFlow(patient_id int64, recrod_date int64, goodid int64) (stockflow []*models.VmStockFlow, err error) {
  3904. 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
  3905. return stockflow, err
  3906. }
  3907. func UpdateAutoGood(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64, count int64) (err error) {
  3908. 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
  3909. 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
  3910. 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
  3911. return err
  3912. }
  3913. func DeleteAutoRedeceDetailTen(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3914. detail := models.BloodAutomaticReduceDetail{}
  3915. 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
  3916. info := models.WarehouseOutInfo{}
  3917. 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
  3918. prepare := models.DialysisBeforePrepare{}
  3919. 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
  3920. flow := models.VmStockFlow{}
  3921. 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
  3922. return err
  3923. }
  3924. func DeleteAutoRedeceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3925. detail := models.BloodAutomaticReduceDetail{}
  3926. 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
  3927. info := models.WarehouseOutInfo{}
  3928. 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
  3929. prepare := models.DialysisBeforePrepare{}
  3930. 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
  3931. flow := models.VmStockFlow{}
  3932. 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
  3933. return err
  3934. }
  3935. func DeleteAutoRedeceDetailTenOne(orgid int64, patient_id int64, recordtime int64, goodid int64, order_id int64) error {
  3936. detail := models.BloodAutomaticReduceDetail{}
  3937. 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
  3938. info := models.WarehouseOutInfo{}
  3939. 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
  3940. prepare := models.DialysisBeforePrepare{}
  3941. 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
  3942. flow := models.VmStockFlow{}
  3943. 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
  3944. return err
  3945. }
  3946. func UpDateWarehouseInfoByStockDelete(id int64, count int64, patient_id int64, record_time int64, goodid int64) (err error) {
  3947. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  3948. //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
  3949. return err
  3950. }
  3951. func GetCancelStockInfoByPatientId(patientid int64, goodid int64, record_date int64) (info []*models.CancelStockInfo, err error) {
  3952. err = XTReadDB().Where("patient_id = ? and good_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&info).Error
  3953. return info, err
  3954. }
  3955. func GetHisPrepscritionByPatientId(patientid int64, goodid int64, record_date int64) (list []*models.HisPrescriptionProject, err error) {
  3956. err = XTReadDB().Where("patient_id = ? and project_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&list).Error
  3957. return list, err
  3958. }
  3959. func UpDateWarehouStockFlowByStockDelete(id int64, recordtime int64, good_id int64, count int64, patient_id int64) (err error) {
  3960. 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
  3961. return err
  3962. }
  3963. func UpDateWarehouseInfoByStockDeleteOne(id int64, count int64) (err error) {
  3964. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  3965. return err
  3966. }
  3967. func GetAutoDialysisBefor(goodid int64, goodtypeid int64, patientid int64, orgid int64, recorddate int64) (*models.BloodAutomaticReduceDetail, error) {
  3968. var autoreduece models.BloodAutomaticReduceDetail
  3969. var err error
  3970. 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
  3971. if err == gorm.ErrRecordNotFound {
  3972. return nil, err
  3973. }
  3974. if err != nil {
  3975. return nil, err
  3976. }
  3977. return &autoreduece, nil
  3978. }
  3979. func GetAutoRecordByGoodIdSeven(goodid int64, patientid int64, recorddate int64) (models.WarehouseOutInfoSeven, error) {
  3980. detail := models.WarehouseOutInfoSeven{}
  3981. err := XTReadDB().Select("id,count").Where(" good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  3982. return detail, err
  3983. }
  3984. func GetAutoRecordByGoodIdSevenEight(goodid int64, patientid int64, recorddate int64) (detail []*models.WarehouseOutInfoSeven, err error) {
  3985. err = XTReadDB().Select("id,count").Where("good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  3986. return detail, err
  3987. }
  3988. func GetHisProjectPrescriptionByPatientId(patientid int64, recorddate int64) (project []*models.HisPrescriptionProject, err error) {
  3989. err = XTReadDB().Model(&project).Where("patient_id = ? and record_date = ? and status =1", patientid, recorddate).Find(&project).Error
  3990. return project, err
  3991. }
  3992. func GetHisProjectPrescriptionByPatientIdOne(id int64) (models.HisPrescriptionProject, error) {
  3993. project := models.HisPrescriptionProject{}
  3994. err = XTReadDB().Model(&project).Where("id = ? and status =1", id).Find(&project).Error
  3995. return project, err
  3996. }
  3997. func GetAllAutoRecordByPatient(patientid int64, recorddate int64) (auto []*models.WarehouseOutInfoNight, err error) {
  3998. 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
  3999. return auto, err
  4000. }
  4001. func ModefyWarehouseInfo(count int64, id int64) error {
  4002. err := XTWriteDB().Model(models.WarehousingInfo{}).Where(" id =? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  4003. return err
  4004. }
  4005. func DeleteAutoWarehouse(patient_id int64, record_time int64) error {
  4006. 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
  4007. 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
  4008. 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
  4009. //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
  4010. return err
  4011. }
  4012. func FindGoodInfoByIdTwo(id int64) (goodInfo models.GoodInfo, err error) {
  4013. err = readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  4014. return
  4015. }
  4016. func FindGoodInfoByIdSeven(id int64) (goodInfo models.GoodInfoSeven, err error) {
  4017. err = readDb.Select("id,good_name").Where("id = ? AND status = 1", id).First(&goodInfo).Error
  4018. return
  4019. }
  4020. func GetGoodWarehouseInfo(good_id int64) (info []*models.WarehousingInfo, err error) {
  4021. err = readDb.Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  4022. return info, err
  4023. }
  4024. func GetGoodWarehouseInfoSeven(good_id int64) (info []*models.WarehousingInfoSeven, err error) {
  4025. err = readDb.Select("stock_count").Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  4026. return info, err
  4027. }
  4028. func GetGoodWarehouseInfoSevenTen(good_id int64, storehouse_id int64) (info []*models.WarehousingInfoSeven, err error) {
  4029. 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
  4030. return info, err
  4031. }
  4032. func GetAllStockList(page int64, limit int64, startime int64, endtime int64, good_type int64, keyword string, orgid int64) (info []*models.VmWarehousingInfo, total int64, err error) {
  4033. offset := (page - 1) * limit
  4034. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1")
  4035. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4036. fmt.Println(table)
  4037. if startime > 0 {
  4038. db = db.Where("x.ctime>=?", startime)
  4039. }
  4040. if endtime > 0 {
  4041. db = db.Where("x.ctime<=?", endtime)
  4042. }
  4043. likeKey := "%" + keyword + "%"
  4044. if len(likeKey) > 0 {
  4045. db = db.Where("t.good_name like ?", likeKey)
  4046. }
  4047. if orgid > 0 {
  4048. db = db.Where("x.org_id = ?", orgid)
  4049. }
  4050. if good_type > 0 {
  4051. db = db.Where("t.good_type_id = ?", good_type)
  4052. }
  4053. 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
  4054. return info, total, err
  4055. }
  4056. 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) {
  4057. offset := (page - 1) * limit
  4058. db := XTReadDB().Table("xt_good_information as x").Where("x.status= 1")
  4059. likeKey := "%" + keyword + "%"
  4060. if len(likeKey) > 0 {
  4061. db = db.Where("x.good_name like ?", likeKey)
  4062. }
  4063. if orgid > 0 {
  4064. db = db.Where("x.org_id = ?", orgid)
  4065. }
  4066. if good_type > 0 {
  4067. db = db.Where("x.good_type_id = ?", good_type)
  4068. }
  4069. if good_id > 0 {
  4070. db = db.Where("x.id = ?", good_id)
  4071. }
  4072. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  4073. if startime > 0 {
  4074. db = db.Where("ctime>=?", startime)
  4075. }
  4076. if endtime > 0 {
  4077. db = db.Where("ctime<=?", endtime)
  4078. }
  4079. if good_id > 0 {
  4080. db = db.Where("good_id = ?", good_id)
  4081. }
  4082. if storehouse_id > 0 {
  4083. db = db.Where("storehouse_id = ?", storehouse_id)
  4084. }
  4085. return db.Where("status = 1 and is_check = 1")
  4086. }).Preload("WarehousingInfo", func(db *gorm.DB) *gorm.DB {
  4087. if startime > 0 {
  4088. db = db.Where("ctime>=?", startime)
  4089. }
  4090. if endtime > 0 {
  4091. db = db.Where("ctime<=?", endtime)
  4092. }
  4093. if good_id > 0 {
  4094. db = db.Where("good_id = ?", good_id)
  4095. }
  4096. if storehouse_id > 0 {
  4097. db = db.Where("storehouse_id = ?", storehouse_id)
  4098. }
  4099. return db.Where("status = 1").Group("good_id,storehouse_id")
  4100. }).Preload("CancelStockInfo", func(db *gorm.DB) *gorm.DB {
  4101. if startime > 0 {
  4102. db = db.Where("ctime>=?", startime)
  4103. }
  4104. if endtime > 0 {
  4105. db = db.Where("ctime<=?", endtime)
  4106. }
  4107. return db.Where("status = 1")
  4108. }).Find(&info).Error
  4109. return info, total, err
  4110. }
  4111. 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) {
  4112. offset := (page - 1) * limit
  4113. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1 and x.is_check = 1")
  4114. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4115. tables := XTReadDB().Table("xt_stock_flow as f").Where("f.status = 1 and f.consumable_type = 1 and f.user_org_id = ?", orgid)
  4116. fmt.Println(table, tables)
  4117. if startime > 0 {
  4118. db = db.Where("x.ctime>=?", startime)
  4119. }
  4120. if endtime > 0 {
  4121. db = db.Where("x.ctime<=?", endtime)
  4122. }
  4123. if startfirsttime > 0 {
  4124. db = db.Where("x.expiry_date >=?", startfirsttime)
  4125. }
  4126. if endfirsttime > 0 {
  4127. db = db.Where("x.expiry_date <=?", endfirsttime)
  4128. }
  4129. if orgid > 0 {
  4130. db = db.Where("x.org_id = ?", orgid)
  4131. }
  4132. if good_id > 0 {
  4133. db = db.Where("x.good_id = ?", good_id)
  4134. }
  4135. 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
  4136. return info, total, err
  4137. }
  4138. 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) {
  4139. offset := (page - 1) * limit
  4140. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status= 1")
  4141. if startime > 0 {
  4142. db = db.Where("x.ctime>=?", startime)
  4143. }
  4144. if endtime > 0 {
  4145. db = db.Where("x.ctime<=?", endtime)
  4146. }
  4147. if orgid > 0 {
  4148. db = db.Where("x.org_id = ?", orgid)
  4149. }
  4150. if good_id > 0 {
  4151. db = db.Where("x.good_id = ?", good_id)
  4152. }
  4153. if is_sys == 2 {
  4154. db = db.Where("x.is_sys = 1")
  4155. }
  4156. if is_sys == 3 {
  4157. db = db.Where("x.is_sys = 0")
  4158. }
  4159. 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
  4160. return info, total, err
  4161. }
  4162. func GetStockDrugCount(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  4163. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4164. if startime > 0 {
  4165. db = db.Where("x.ctime >=?", startime)
  4166. }
  4167. if endtime > 0 {
  4168. db = db.Where("x.ctime<=?", endtime)
  4169. }
  4170. if orgid > 0 {
  4171. db = db.Where("x.org_id = ?", orgid)
  4172. }
  4173. 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
  4174. return info, err
  4175. }
  4176. func GetAutoDiallysisBefor(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  4177. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  4178. if startime > 0 {
  4179. db = db.Where("x.ctime >=?", startime)
  4180. }
  4181. if endtime > 0 {
  4182. db = db.Where("x.ctime<=?", endtime)
  4183. }
  4184. if orgid > 0 {
  4185. db = db.Where("x.org_id = ?", orgid)
  4186. }
  4187. err = db.Select("sum(x.count) as count,x.good_id").Where("is_sys = 0").Group("x.good_id").Scan(&info).Error
  4188. return info, err
  4189. }
  4190. func GetOutStockTotalCountFour(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  4191. if endtime > 0 {
  4192. 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
  4193. }
  4194. if endtime == 0 {
  4195. 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
  4196. }
  4197. return autoMatic, err
  4198. }
  4199. func GetGoodWarehouseOutById(id int64, orgid int64) (models.WarehouseOut, error) {
  4200. out := models.WarehouseOut{}
  4201. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", id, orgid).Find(&out).Error
  4202. return out, err
  4203. }
  4204. func GetOrderDetialByOrderId(id int64, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  4205. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  4206. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4207. fmt.Println(table)
  4208. if id > 0 {
  4209. db = db.Where("x.warehouse_out_id = ?", id)
  4210. }
  4211. if orgid > 0 {
  4212. db = db.Where("x.org_id = ?", orgid)
  4213. }
  4214. err = db.Select("x.id,x.warehouse_out_id,x.warehouse_info_id,x.good_id,sum(x.count) as count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.warehouse_out_order_number,x.type,x.dealer,x.patient_id,t.manufacturer,x.is_sys,x.sys_record_time,x.number,x.remark,x.license_number,x.supply_cancel_out_id,x.supply_warehouse_id,x.storehouse_id,x.admin_user_id,x.buy_price,x.stock_count,x.register_number,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.packing_unit,t.min_unit").Joins("left join xt_good_information as t on t.id=x.good_id").Group("x.good_id,x.warehouse_info_id").Order("x.ctime desc").Scan(&out).Error
  4215. return out, err
  4216. }
  4217. func GetWarehouseOutOrder(id []string, orgid int64) (out []*models.WarehouseOut, err error) {
  4218. db := XTReadDB().Table("xt_warehouse_out as x").Where("x.status = 1")
  4219. if len(id) > 0 {
  4220. db = db.Where("x.id in(?)", id)
  4221. }
  4222. if orgid > 0 {
  4223. db = db.Where("x.org_id= ?", orgid)
  4224. }
  4225. 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
  4226. return out, err
  4227. }
  4228. func GetOrderDetialByOrderIdOne(id []string, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  4229. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  4230. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4231. tableOne := XTReadDB().Table("xt_warehouse_info as n").Where("n.status = 1")
  4232. fmt.Println(table, tableOne)
  4233. if len(id) > 0 {
  4234. db = db.Where("x.warehouse_out_id in (?)", id)
  4235. }
  4236. if orgid > 0 {
  4237. db = db.Where("x.org_id = ?", orgid)
  4238. }
  4239. err = db.Select("x.id,x.warehouse_out_id,x.good_id,sum(x.count) as count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.warehouse_out_order_number,x.type,x.dealer,x.manufacturer,t.social_security_directory_code,x.is_sys,x.sys_record_time,n.number,x.remark,x.license_number,x.storehouse_id,x.admin_user_id,x.buy_price,x.stock_count,x.warehouse_info_id,x.remark,x.patient_id,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.packing_unit,t.min_unit,t.packing_price,x.register_number,x.class_type").Joins("left join xt_warehouse_info as n on n.id=x.warehouse_info_id").Joins("left join xt_good_information as t on t.id=x.good_id").Group("x.warehouse_out_id,x.good_id").Order("x.ctime desc").Scan(&out).Error
  4240. return out, err
  4241. }
  4242. func GetOrderDetailStockFlowByStorehouseById(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  4243. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  4244. if len(id) > 0 {
  4245. db = db.Where("warehouse_out_id in (?)", id)
  4246. }
  4247. if orgid > 0 {
  4248. db = db.Where("user_org_id = ?", orgid)
  4249. }
  4250. err = db.Group("warehousing_id").Find(&list).Error
  4251. return list, err
  4252. }
  4253. func GetOrderDetailStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  4254. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  4255. if len(id) > 0 {
  4256. db = db.Where("warehouse_out_id in (?)", id)
  4257. }
  4258. if orgid > 0 {
  4259. db = db.Where("user_org_id = ?", orgid)
  4260. }
  4261. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  4262. return list, err
  4263. }
  4264. func GetOrderStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  4265. db := XTReadDB().Model(&list).Where("status = 1 and consumable_type = 7 and patient_id > 0 and count>0")
  4266. if len(id) > 0 {
  4267. db = db.Where("warehouse_out_id in (?)", id)
  4268. }
  4269. if orgid > 0 {
  4270. db = db.Where("user_org_id = ?", orgid)
  4271. }
  4272. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  4273. return list, err
  4274. }
  4275. func GetLastWarehouseOutInfo(ids []string, orgId int64) (info []*models.WarehouseOutInfoSix, err error) {
  4276. db := XTReadDB().Model(&info)
  4277. db = db.Preload("GoodInfo", "status = 1")
  4278. 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
  4279. return info, err
  4280. }
  4281. func FindUserDetailByIdOne(good_id int64, record_time int64, org_id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  4282. db := readDb.Model(&models.AutomaticReduceDetail{})
  4283. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  4284. db = db.Preload("GoodsType", "status = 1")
  4285. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  4286. db = db.Preload("WarehouseOutInfo", "org_id = ? and status = 1 and good_id = ? and sys_record_time = ?", org_id, good_id, record_time)
  4287. db = db.Where("status = 1 AND org_id = ? AND good_id = ? AND record_time = ? and count<> 0 ", org_id, good_id, record_time)
  4288. db = db.Count(&total)
  4289. err = db.Find(&user).Error
  4290. return
  4291. }
  4292. func GetGoodInformationByGoodId(good_id int64) (models.GoodInfo, error) {
  4293. info := models.GoodInfo{}
  4294. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  4295. return info, err
  4296. }
  4297. func GetNewGoodInformationByGoodId(good_id int64, tx *gorm.DB) (models.GoodInfo, error) {
  4298. info := models.GoodInfo{}
  4299. err := tx.Where("id = ? and status = 1", good_id).Find(&info).Error
  4300. if err != gorm.ErrRecordNotFound {
  4301. if err != nil {
  4302. tx.Rollback()
  4303. return info, err
  4304. }
  4305. }
  4306. return info, err
  4307. }
  4308. func GetGoodInformationByGoodIdThirty(good_id int64) (models.GoodInformationThirty, error) {
  4309. info := models.GoodInformationThirty{}
  4310. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  4311. return info, err
  4312. }
  4313. func GetNewGoodInformationByGoodIdThirty(good_id int64, tx *gorm.DB) (models.GoodInformationThirty, error) {
  4314. info := models.GoodInformationThirty{}
  4315. err := tx.Where("id = ? and status = 1", good_id).Find(&info).Error
  4316. if err != gorm.ErrRecordNotFound {
  4317. if err != nil {
  4318. tx.Rollback()
  4319. return info, err
  4320. }
  4321. }
  4322. return info, err
  4323. }
  4324. func UpdateGoodInfoList(info models.GoodInfo, id int64) error {
  4325. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"total": info.Total}).Error
  4326. return err
  4327. }
  4328. func GetExprotStockList(orgid int64, orderid []string, startime int64, endtime int64) (info []*models.StWarehousingInfo, err error) {
  4329. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4330. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4331. fmt.Println(table)
  4332. if orgid > 0 {
  4333. db = db.Where("x.org_id = ?", orgid)
  4334. }
  4335. if len(orderid) > 0 {
  4336. db = db.Where("x.warehousing_id in(?)", orderid)
  4337. }
  4338. if startime > 0 {
  4339. db = db.Where("x.ctime>=?", startime)
  4340. }
  4341. if endtime > 0 {
  4342. db = db.Where("x.ctime<=?", endtime)
  4343. }
  4344. 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
  4345. return info, err
  4346. }
  4347. func GetExprotStockListTwenty(orgid int64, orderid []string, startime int64, endtime int64, storehouse_id int64) (info []*models.StWarehousingInfo, err error) {
  4348. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4349. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4350. fmt.Println(table)
  4351. if orgid > 0 {
  4352. db = db.Where("x.org_id = ?", orgid)
  4353. }
  4354. if len(orderid) > 0 {
  4355. db = db.Where("x.warehousing_id in(?)", orderid)
  4356. }
  4357. if startime > 0 {
  4358. db = db.Where("x.ctime>=?", startime)
  4359. }
  4360. if endtime > 0 {
  4361. db = db.Where("x.ctime<=?", endtime)
  4362. }
  4363. if storehouse_id > 0 {
  4364. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4365. }
  4366. err = db.Select("x.id,x.good_id,x.good_type_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.price,x.dealer,x.manufacturer,x.remark,x.license_number,t.good_name,t.packing_unit,t.specification_name,p.warehousing_time").Joins("left join xt_warehouse as p on p.id =x.warehousing_id").Joins("left join xt_good_information as t on t.id = x.good_id").Scan(&info).Error
  4367. return info, err
  4368. }
  4369. func GetDrugWarehouOrderInfo(startime int64, endtime int64, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  4370. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4371. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  4372. fmt.Println(table)
  4373. if startime > 0 {
  4374. db = db.Where("x.ctime>=?", startime)
  4375. }
  4376. if endtime > 0 {
  4377. db = db.Where("x.ctime<=?", endtime)
  4378. }
  4379. if orgid > 0 {
  4380. db = db.Where("x.org_id = ?", orgid)
  4381. }
  4382. 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
  4383. return info, err
  4384. }
  4385. func GetOutExprotList(orgid int64, ids []string, startime int64, endtime int64) (out []*models.WarehouseOutInfoOne, err error) {
  4386. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1 ")
  4387. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4388. tableOne := XTReadDB().Table("xt_warehouse_out as p").Where("p.status=1")
  4389. fmt.Println(table, tableOne)
  4390. if orgid > 0 {
  4391. db = db.Where("x.org_id = ?", orgid)
  4392. }
  4393. if orgid > 0 {
  4394. db = db.Where("t.org_id = ?", orgid)
  4395. }
  4396. if orgid > 0 {
  4397. db = db.Where("p.org_id = ?", orgid)
  4398. }
  4399. if len(ids) > 0 {
  4400. db = db.Where("x.warehouse_out_id in (?)", ids)
  4401. }
  4402. if startime > 0 {
  4403. db = db.Where("x.ctime>=?", startime)
  4404. }
  4405. if endtime > 0 {
  4406. db = db.Where("x.ctime<=?", endtime)
  4407. }
  4408. err = db.Select("x.id,x.warehouse_out_id,x.good_id,x.good_type_id,x.warehousing_out_target,x.count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.remark,x.number,x.license_number,x.warehouse_out_order_number,t.good_name,t.specification_name,t.min_number,t.packing_price,t.min_unit,t.packing_unit,x.manufacturer,x.dealer,p.warehouse_out_time").Joins("left join xt_warehouse_out as p on p.id =x.warehouse_out_id").Joins("left join xt_good_information as t on t.id = x.good_id").Scan(&out).Error
  4409. return out, err
  4410. }
  4411. func FindeDrugWarehouseOutOrder(ids []string, orgid int64) (info []*models.DrugWarehouseOut, err error) {
  4412. db := XTReadDB().Table("xt_drug_warehouse_out as x").Where("x.status = 1")
  4413. if len(ids) > 0 {
  4414. db = db.Where("x.id in(?)", ids)
  4415. }
  4416. if orgid > 0 {
  4417. db = db.Where("x.org_id = ?", orgid)
  4418. }
  4419. 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
  4420. return info, err
  4421. }
  4422. func FindeDrugWarehouseOutDetail(ids []string, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  4423. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status =1 ")
  4424. table := XTReadDB().Table("x.base_drug as t").Where("t.status = 1")
  4425. fmt.Println(table)
  4426. if len(ids) > 0 {
  4427. db = db.Where("x.warehouse_out_id in(?)", ids)
  4428. }
  4429. if orgid > 0 {
  4430. db = db.Where("x.org_id = ?", orgid)
  4431. }
  4432. 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
  4433. return info, err
  4434. }
  4435. func GetDrugNameByKeyword(keyword string, orgid int64) (basedrug []*models.XtBaseDrug, err error) {
  4436. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1")
  4437. likeKey := "%" + keyword + "%"
  4438. if len(keyword) > 0 {
  4439. db = db.Where("x.drug_name like ?", likeKey)
  4440. }
  4441. if orgid > 0 {
  4442. db = db.Where("x.org_id = ?", orgid)
  4443. }
  4444. err = db.Find(&basedrug).Error
  4445. return basedrug, err
  4446. }
  4447. func GetDrugWarehouseOrderDetail(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  4448. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4449. if len(ids) > 0 {
  4450. db = db.Where("x.drug_id in(?)", ids)
  4451. }
  4452. if orgid > 0 {
  4453. db = db.Where("x.org_id = ?", orgid)
  4454. }
  4455. if startime > 0 {
  4456. db = db.Where("x.ctime>=?", startime)
  4457. }
  4458. if endtime > 0 {
  4459. db = db.Where("x.ctime<=?", endtime)
  4460. }
  4461. if storehouse_id > 0 {
  4462. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4463. }
  4464. err = db.Find(&info).Error
  4465. return info, err
  4466. }
  4467. func GetDrugWarehouseOrderDetailTwo(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  4468. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4469. if len(ids) > 0 {
  4470. db = db.Where("x.drug_id in(?)", ids)
  4471. }
  4472. if orgid > 0 {
  4473. db = db.Where("x.org_id = ?", orgid)
  4474. }
  4475. if startime > 0 {
  4476. db = db.Where("x.ctime>=?", startime)
  4477. }
  4478. if endtime > 0 {
  4479. db = db.Where("x.ctime<=?", endtime)
  4480. }
  4481. if storehouse_id > 0 {
  4482. db = db.Where("storehouse_id = ?", storehouse_id)
  4483. }
  4484. err = db.Find(&info).Error
  4485. return info, err
  4486. }
  4487. func GetDrugTotalCount(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4488. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  4489. if drugid > 0 {
  4490. db = db.Where("x.drug_id = ?", drugid)
  4491. }
  4492. if orgid > 0 {
  4493. db = db.Where("x.org_id = ?", orgid)
  4494. }
  4495. if storehouse_id > 0 {
  4496. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4497. }
  4498. 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
  4499. return info, err
  4500. }
  4501. func GetDrugTotalCountTwoTwety(drugid int64, orgid int64, storehouse_id int64, tx *gorm.DB) (info models.VmDrugWarehouseInfo, err error) {
  4502. db := tx.Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  4503. if drugid > 0 {
  4504. db = db.Where("x.drug_id = ?", drugid)
  4505. }
  4506. if orgid > 0 {
  4507. db = db.Where("x.org_id = ?", orgid)
  4508. }
  4509. if storehouse_id > 0 {
  4510. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4511. }
  4512. 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
  4513. if err != gorm.ErrRecordNotFound {
  4514. if err != nil {
  4515. tx.Rollback()
  4516. return info, err
  4517. }
  4518. }
  4519. return info, err
  4520. }
  4521. func GetDrugTotalCountTwenty(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4522. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1 ")
  4523. if drugid > 0 {
  4524. db = db.Where("x.drug_id = ?", drugid)
  4525. }
  4526. if orgid > 0 {
  4527. db = db.Where("x.org_id = ?", orgid)
  4528. }
  4529. if storehouse_id > 0 {
  4530. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4531. }
  4532. 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
  4533. return info, err
  4534. }
  4535. func GetDrugTotalCountSwx(id int64, orgid int64) (info models.VmDrugWarehouseInfo, err error) {
  4536. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4537. if id > 0 {
  4538. db = db.Where("x.id = ?", id)
  4539. }
  4540. if orgid > 0 {
  4541. db = db.Where("x.org_id = ?", orgid)
  4542. }
  4543. 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
  4544. return info, err
  4545. }
  4546. func GetDrugTotalCountOne(drugid int64, orgid int64, id int64) (models.VmDrugWarehouseInfo, error) {
  4547. info := models.VmDrugWarehouseInfo{}
  4548. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4549. if id == 0 {
  4550. db = db.Where("id = ?", id)
  4551. }
  4552. if drugid > 0 {
  4553. db = db.Where("x.drug_id = ?", drugid)
  4554. }
  4555. if orgid > 0 {
  4556. db = db.Where("x.org_id = ?", orgid)
  4557. }
  4558. err = db.Select("x.drug_id,x.stock_max_number,x.stock_min_number,x.max_unit,x.min_unit").Find(&info).Error
  4559. return info, err
  4560. }
  4561. func GetDrugTotalCountTwo(drugid int64, storehouse_id int64) (list []*models.VmDrugWarehouseInfo, err error) {
  4562. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4563. table := XTReadDB().Table("xt_base_drug as d").Where("d.status = 1")
  4564. fmt.Println(table)
  4565. if drugid > 0 {
  4566. db = db.Where("x.drug_id = ?", drugid)
  4567. }
  4568. if storehouse_id > 0 {
  4569. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4570. }
  4571. 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
  4572. return list, err
  4573. }
  4574. func GetDrugByGoodId(drugid int64) (models.BaseDrugLib, error) {
  4575. lib := models.BaseDrugLib{}
  4576. err := XTReadDB().Model(&lib).Where("id = ? and status = 1", drugid).Find(&lib).Error
  4577. return lib, err
  4578. }
  4579. func GetWarehoureOrderInfoList(goodid int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4580. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4581. if goodid > 0 {
  4582. db = db.Where("x.good_id = ?", goodid)
  4583. }
  4584. if orgid > 0 {
  4585. db = db.Where("x.org_id = ?", orgid)
  4586. }
  4587. 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
  4588. return info, err
  4589. }
  4590. func UpdateWarehousingCount(info *models.WarehousingInfo, goodid int64, orgid int64, warehousingId int64) error {
  4591. 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
  4592. return err
  4593. }
  4594. func GetSingleCancelOrder(id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  4595. err = XTReadDB().Where("cancel_stock_id = ? and status = 1 and org_id = ?", id, orgid).Preload("GoodInfo", "status = 1").Find(&info).Error
  4596. return info, err
  4597. }
  4598. func UpdateCancelOut(list []*models.WarehousingInfo, count int64) error {
  4599. //遍历循环入库数据
  4600. for i, item := range list {
  4601. //计算每个批次剩余库存与入库数据的差 然后和退库数量比较
  4602. if (item.WarehousingCount - item.StockCount) >= count {
  4603. //如果差距大于退库数量,则直接把退库数量增加到该批次中
  4604. info := models.WarehousingInfo{
  4605. GoodId: item.GoodId,
  4606. OrgId: item.OrgId,
  4607. WarehousingId: item.WarehousingId,
  4608. StockCount: item.StockCount + count,
  4609. }
  4610. err2 := UpdateWarehousingCount(&info, item.GoodId, item.OrgId, item.WarehousingId)
  4611. fmt.Println("2323233232323232323232323232323", err2)
  4612. return err
  4613. }
  4614. if (item.WarehousingCount - item.StockCount) < count {
  4615. //如果该批次小于退库数量,则先出库该相差数量
  4616. var countOne = item.WarehousingCount - item.StockCount
  4617. infoOne := models.WarehousingInfo{
  4618. GoodId: item.GoodId,
  4619. OrgId: item.OrgId,
  4620. WarehousingId: item.WarehousingId,
  4621. StockCount: countOne + item.StockCount,
  4622. }
  4623. err := UpdateWarehousingCount(&infoOne, item.GoodId, item.OrgId, item.WarehousingId)
  4624. if err == nil {
  4625. //剩余的出库到第二个批次
  4626. var countTwo = count - countOne
  4627. infos := append(list, list[i+1])
  4628. var idArray []*models.WarehousingInfo
  4629. for _, it := range infos {
  4630. if it.WarehousingId != item.WarehousingId {
  4631. idArray = append(idArray, it)
  4632. }
  4633. }
  4634. //查询更新后的库存
  4635. UpdateCancelOut(idArray, countTwo)
  4636. return err
  4637. } else {
  4638. return err
  4639. }
  4640. }
  4641. }
  4642. return err
  4643. }
  4644. func GetCancelStockOrderPrint(idstr []string) (info []*models.CancelStockInfo, err error) {
  4645. if len(idstr) > 0 {
  4646. err = XTReadDB().Model(&info).Where("cancel_stock_id in(?)", idstr).Preload("GoodInfo", "status = 1").Find(&info).Error
  4647. } else {
  4648. err = XTReadDB().Model(&info).Preload("GoodInfo", "status = 1").Find(&info).Error
  4649. }
  4650. return info, err
  4651. }
  4652. func GetCancelStockOrderPrintOne(idstr []string, orgid int64) (info []*models.CancelStock, err error) {
  4653. if len(idstr) > 0 {
  4654. err = XTReadDB().Model(&info).Where("id in(?)", idstr).
  4655. Preload("XtCancelStockInfo", func(db *gorm.DB) *gorm.DB {
  4656. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodInfo", "status = 1 and org_id = ?", orgid)
  4657. }).Find(&info).Error
  4658. }
  4659. return info, err
  4660. }
  4661. func GetCancelOutTotalCount(startime int64, endtime int64, orgid int64) (info []*models.VmCancelStockInfo, err error) {
  4662. db := XTReadDB().Table("xt_cancel_stock_info as x").Where("x.status = 1")
  4663. if startime > 0 {
  4664. db = db.Where("x.ctime >=?", startime)
  4665. }
  4666. if endtime > 0 {
  4667. db = db.Where("x.ctime <= ?", endtime)
  4668. }
  4669. if orgid > 0 {
  4670. db = db.Where("x.org_id = ?", orgid)
  4671. }
  4672. err = db.Select("x.good_id,sum(x.count) as count").Group("x.good_id").Scan(&info).Error
  4673. return info, err
  4674. }
  4675. func GetWarehoureOutOrderInfo(goodid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  4676. info := models.WarehouseOutInfo{}
  4677. var err error
  4678. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1", goodid, orgid).Find(&info).Error
  4679. if err == gorm.ErrRecordNotFound {
  4680. return nil, err
  4681. }
  4682. if err != nil {
  4683. return nil, err
  4684. }
  4685. return &info, nil
  4686. }
  4687. func GetWarehoureOrderInfoCount(goodid int64, orgid int64, id int64) (info models.WarehouseOutInfo, err error) {
  4688. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  4689. if goodid > 0 {
  4690. db = db.Where("x.good_id = ?", goodid)
  4691. }
  4692. if orgid > 0 {
  4693. db = db.Where("x.org_id = ?", orgid)
  4694. }
  4695. if id > 0 {
  4696. db = db.Where("x.warehouse_info_id = ?", id)
  4697. }
  4698. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4699. return info, err
  4700. }
  4701. func GetWarehoureOrderStockOutFlow(goodid int64, orgid int64, id int64, storehouse_id int64) (info models.VmStockFlow, err error) {
  4702. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status =1 and x.consumable_type = 2 or x.consumable_type = 3")
  4703. if goodid > 0 {
  4704. db = db.Where("x.good_id = ?", goodid)
  4705. }
  4706. if orgid > 0 {
  4707. db = db.Where("x.user_org_id = ?", orgid)
  4708. }
  4709. if id > 0 {
  4710. db = db.Where("x.warehousing_id = ?", id)
  4711. }
  4712. if storehouse_id > 0 {
  4713. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4714. }
  4715. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4716. return info, err
  4717. }
  4718. func UpdateCancelInfo(id int64) (models.CancelStock, error) {
  4719. stock := models.CancelStock{}
  4720. err := XTWriteDB().Model(&stock).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4721. return stock, err
  4722. }
  4723. func GetStockBatchNumber(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4724. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1 and number <> ''", id, orgid).Find(&info).Error
  4725. return info, err
  4726. }
  4727. func GetWarehouseOutInfoById(id int64, goodid int64) (info []*models.WarehouseOutInfo, err error) {
  4728. err = XTReadDB().Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  4729. return info, err
  4730. }
  4731. func GetStockFlowBatchNumber(id int64, goodid int64) (stock []*models.VmStockFlow, err error) {
  4732. err = XTReadDB().Model(&stock).Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Group("patient_id,number").Find(&stock).Error
  4733. return stock, err
  4734. }
  4735. func GetStockFlowBatchNumberOne(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4736. 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)")
  4737. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4738. fmt.Println(table)
  4739. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4740. fmt.Println(tableone)
  4741. if id > 0 {
  4742. db = db.Where("x.warehouse_out_id = ?", id)
  4743. }
  4744. if goodid > 0 {
  4745. db = db.Where("x.good_id = ?", goodid)
  4746. }
  4747. 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
  4748. return stock, err
  4749. }
  4750. func GetStockFlowCancelInfo(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4751. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status = 1 and x.count > 0 and x.consumable_type = 7")
  4752. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4753. fmt.Println(table)
  4754. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4755. fmt.Println(tableone)
  4756. if id > 0 {
  4757. db = db.Where("x.warehouse_out_id = ?", id)
  4758. }
  4759. if goodid > 0 {
  4760. db = db.Where("x.good_id = ?", goodid)
  4761. }
  4762. 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
  4763. return stock, err
  4764. }
  4765. func GetDrugBatchNumber(drug_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  4766. err = XTReadDB().Model(&info).Where("drug_id = ? and org_id = ? and status = 1 and batch_number <>''", drug_id, orgid).Find(&info).Error
  4767. return info, err
  4768. }
  4769. func ExportDrugList(warehouse_out_id []string, org_id int64) (out []*models.StDrugWarehouseInfo, err error) {
  4770. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status =1 AND x.org_id=?", org_id)
  4771. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1 and b.org_id = ?", org_id)
  4772. tableOne := XTReadDB().Table("xt_drug_warehouse as p").Where("p.status=1 and p.org_id=?", org_id)
  4773. fmt.Println("table", table)
  4774. fmt.Println("tableOne", tableOne)
  4775. if len(warehouse_out_id) > 0 {
  4776. db = db.Where("x.warehousing_id in(?)", warehouse_out_id)
  4777. }
  4778. err = db.Select("x.id,x.warehousing_id,x.drug_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.warehouseing_unit,x.max_unit,x.min_unit,x.stock_max_number,x.stock_min_number,x.price,x.dealer,x.manufacturer,x.warehousing_order,x.batch_number,b.dose,b.dose_unit,b.min_number,b.min_unit,b.max_unit,b.drug_name,b.drug_type,p.warehousing_time").Joins("left join xt_drug_warehouse as p on p.id =x.warehousing_id").Joins("left join xt_base_drug as b on b.id = x.drug_id").Scan(&out).Error
  4779. return out, err
  4780. }
  4781. func GetExportOutOrderDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4782. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4783. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  4784. fmt.Println("table", table)
  4785. if len(warehouse_out_id) > 0 {
  4786. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4787. }
  4788. 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
  4789. return out, err
  4790. }
  4791. func GetExportOutOrderDrugListOne(warehouse_out_id []string, org_id int64) (out []*models.StDrugWarehouseOutInfo, err error) {
  4792. db := XTReadDB().Table("xt_drug_flow as x").Where("x.status = 1 and x.user_org_id =?", org_id)
  4793. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1 and b.org_id = ?", org_id)
  4794. fmt.Println("table", table)
  4795. if len(warehouse_out_id) > 0 {
  4796. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4797. }
  4798. 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
  4799. return out, err
  4800. }
  4801. func GetDrugOutListFlow(warehouse_out_id []string, orgid int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  4802. db := XTReadDB().Model(&info).Where("status = 1")
  4803. if len(warehouse_out_id) > 0 {
  4804. db = db.Where("warehouse_out_id in(?)", warehouse_out_id)
  4805. }
  4806. if orgid > 0 {
  4807. db = db.Where("org_id = ?", orgid)
  4808. }
  4809. err = db.Find(&info).Error
  4810. return info, err
  4811. }
  4812. func GetDrugWarehouseOut(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4813. err = XTReadDB().Where("warehouse_out_id in(?)", warehouse_out_id).Find(&out).Error
  4814. return out, err
  4815. }
  4816. func GetDrugWarehouseOutTenty(warehouse_out_id []string, orgid int64) (out []*models.DrugWarehouseOutTwenty, err error) {
  4817. err = XTReadDB().Where("id in(?)", warehouse_out_id).Preload("DrugWarehouseOutInfoTenty", func(db *gorm.DB) *gorm.DB {
  4818. 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)
  4819. }).Find(&out).Error
  4820. return out, err
  4821. }
  4822. func GetLastGoodInformationByGoodId(goodid int64, warehouse_info_id int64, orgid_id int64) (models.WarehouseOutInfo, error) {
  4823. info := models.WarehouseOutInfo{}
  4824. err := XTReadDB().Where("good_id = ? and warehouse_info_id= ? and status =1", goodid, warehouse_info_id).Last(&info).Error
  4825. return info, err
  4826. }
  4827. func GetTotalCountByGoodId(goodid int64, storehouse_id int64) (models.WarehousingInfo, error) {
  4828. info := models.WarehousingInfo{}
  4829. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4830. if goodid > 0 {
  4831. db = db.Where("x.good_id = ?", goodid)
  4832. }
  4833. if storehouse_id > 0 {
  4834. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4835. }
  4836. err := db.Select("x.good_id,sum(x.stock_count) as stock_count").Scan(&info).Error
  4837. return info, err
  4838. }
  4839. func GetLastGoodCountById(goodid int64, warehouse_info_id int64) (models.WarehousingInfo, error) {
  4840. info := models.WarehousingInfo{}
  4841. err := XTReadDB().Where("good_id = ? and id= ? and status = 1", goodid, warehouse_info_id).First(&info).Error
  4842. return info, err
  4843. }
  4844. func UpdateWarehousingInfo(info models.WarehousingInfo, id int64) error {
  4845. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  4846. return err
  4847. }
  4848. func FindFirstWarehousingInfoByGoodId(good_id int64, id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  4849. if storehouse_id == 0 {
  4850. err = readDb.Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime asc").First(&info).Error
  4851. }
  4852. if storehouse_id > 0 {
  4853. 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
  4854. }
  4855. return info, err
  4856. }
  4857. func CreateStockFlow(warehousingInfo []*models.VmStockFlow) (err error) {
  4858. if len(warehousingInfo) > 0 {
  4859. utx := writeDb.Begin()
  4860. if len(warehousingInfo) > 0 {
  4861. 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 "
  4862. insertParams := make([]string, 0)
  4863. insertData := make([]interface{}, 0)
  4864. for _, info := range warehousingInfo {
  4865. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  4866. insertData = append(insertData, info.WarehousingId)
  4867. insertData = append(insertData, info.GoodId)
  4868. insertData = append(insertData, info.Number)
  4869. insertData = append(insertData, info.LicenseNumber)
  4870. insertData = append(insertData, info.Count)
  4871. insertData = append(insertData, info.UserOrgId)
  4872. insertData = append(insertData, info.PatientId)
  4873. insertData = append(insertData, info.SystemTime)
  4874. insertData = append(insertData, info.ConsumableType)
  4875. insertData = append(insertData, info.IsSys)
  4876. insertData = append(insertData, info.WarehousingOrder)
  4877. insertData = append(insertData, info.WarehouseOutId)
  4878. insertData = append(insertData, info.WarehouseOutOrderNumber)
  4879. insertData = append(insertData, info.IsEdit)
  4880. insertData = append(insertData, info.CancelStockId)
  4881. insertData = append(insertData, info.CancelOrderNumber)
  4882. insertData = append(insertData, info.Manufacturer)
  4883. insertData = append(insertData, info.Dealer)
  4884. insertData = append(insertData, info.Creator)
  4885. insertData = append(insertData, info.UpdateCreator)
  4886. insertData = append(insertData, info.Status)
  4887. insertData = append(insertData, info.Ctime)
  4888. insertData = append(insertData, info.Mtime)
  4889. insertData = append(insertData, info.Price)
  4890. insertData = append(insertData, info.WarehousingDetailId)
  4891. insertData = append(insertData, info.WarehouseOutDetailId)
  4892. insertData = append(insertData, info.CancelOutDetailId)
  4893. insertData = append(insertData, info.ExpireDate)
  4894. insertData = append(insertData, info.ProductDate)
  4895. }
  4896. thisSQL += strings.Join(insertParams, ", ")
  4897. err = utx.Exec(thisSQL, insertData...).Error
  4898. if err != nil {
  4899. utx.Rollback()
  4900. return
  4901. }
  4902. }
  4903. utx.Commit()
  4904. }
  4905. return
  4906. }
  4907. func CreateStockFlowOne(flow models.VmStockFlow) error {
  4908. utx := XTWriteDB().Begin()
  4909. err := utx.Create(&flow).Error
  4910. if err != nil {
  4911. utx.Rollback()
  4912. return err
  4913. }
  4914. utx.Commit()
  4915. return err
  4916. }
  4917. func CreateNewStockFlowOne(flow models.VmStockFlow, tx *gorm.DB) error {
  4918. err := tx.Create(&flow).Error
  4919. if err != nil {
  4920. tx.Rollback()
  4921. return err
  4922. }
  4923. return err
  4924. }
  4925. func CreateStockFlowSix(flow *models.VmStockFlow) error {
  4926. err := XTWriteDB().Create(&flow).Error
  4927. return err
  4928. }
  4929. func UpdatedStockFlow(flow models.VmStockFlow) error {
  4930. err := XTWriteDB().Save(&flow).Error
  4931. return err
  4932. }
  4933. func UpdatedStockFlowOne(flow models.VmStockFlow, warehousing_id int64, patient_id int64, record_time int64, good_id int64) error {
  4934. 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
  4935. return err
  4936. }
  4937. func UpdatedStockFlowFive(flow models.VmStockFlow, warehouse_out_id int64, patient_id int64, record_time int64, good_id int64) error {
  4938. 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
  4939. return err
  4940. }
  4941. func GetStockFlowIsExsit(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (*models.VmStockFlow, error) {
  4942. stock := models.VmStockFlow{}
  4943. var err error
  4944. 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
  4945. if err == gorm.ErrRecordNotFound {
  4946. return nil, err
  4947. }
  4948. if err != nil {
  4949. return nil, err
  4950. }
  4951. return &stock, nil
  4952. }
  4953. func GetStockFlowIsBatchNumber(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  4954. 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
  4955. return flow, nil
  4956. }
  4957. func GetStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  4958. 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
  4959. return flow, nil
  4960. }
  4961. func GetNewStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  4962. err = tx.Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type = 3 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4963. if err != gorm.ErrRecordNotFound {
  4964. if err != nil {
  4965. tx.Rollback()
  4966. return flow, nil
  4967. }
  4968. }
  4969. return flow, nil
  4970. }
  4971. func GetStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.WarehouseOutInfo, err error) {
  4972. 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
  4973. return flow, nil
  4974. }
  4975. func GetNewStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.WarehouseOutInfo, err error) {
  4976. err = tx.Where("patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4977. if err != gorm.ErrRecordNotFound {
  4978. if err != nil {
  4979. tx.Rollback()
  4980. return flow, nil
  4981. }
  4982. }
  4983. return flow, nil
  4984. }
  4985. func GetStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.AutomaticReduceDetail, err error) {
  4986. 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
  4987. return flow, nil
  4988. }
  4989. func GetNewStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.AutomaticReduceDetail, err error) {
  4990. err = tx.Where("patient_id = ? and record_time = ? and good_id = ? and status = 1 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4991. if err != gorm.ErrRecordNotFound {
  4992. if err != nil {
  4993. tx.Rollback()
  4994. return flow, err
  4995. }
  4996. }
  4997. return flow, err
  4998. }
  4999. func GetStockFlowIsBatchNumberTwo(patient_id int64, record_time int64, good_id int64) (flow []*models.WarehouseOutInfo, err error) {
  5000. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 ", patient_id, record_time, good_id).Find(&flow).Error
  5001. return flow, nil
  5002. }
  5003. func GetStockFlowIsBatchNumberThree(patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  5004. 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
  5005. return flow, nil
  5006. }
  5007. func GetStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  5008. 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
  5009. return flow, nil
  5010. }
  5011. func GetNewStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  5012. err = tx.Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type =7 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  5013. if err != gorm.ErrRecordNotFound {
  5014. if err != nil {
  5015. tx.Rollback()
  5016. return flow, nil
  5017. }
  5018. }
  5019. return flow, nil
  5020. }
  5021. func GetStockFlowIsBatchNumberFourty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  5022. 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
  5023. return flow, nil
  5024. }
  5025. func GetWarehouseOutInfoIsExist(id int64) (*models.WarehouseOutInfo, error) {
  5026. info := models.WarehouseOutInfo{}
  5027. var err error
  5028. err = XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5029. if err == gorm.ErrRecordNotFound {
  5030. return nil, err
  5031. }
  5032. if err != nil {
  5033. return nil, err
  5034. }
  5035. return &info, nil
  5036. }
  5037. func GetWarehouseOutInfoIsExistOne(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  5038. info := models.WarehouseOutInfo{}
  5039. var err error
  5040. err = XTReadDB().Model(&info).Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and is_sys=1", good_id, record_time, patient_id).Find(&info).Error
  5041. if err == gorm.ErrRecordNotFound {
  5042. return nil, err
  5043. }
  5044. if err != nil {
  5045. return nil, err
  5046. }
  5047. return &info, nil
  5048. }
  5049. func GetWarehouseOutInfoIsExistTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  5050. info := models.WarehouseOutInfo{}
  5051. err := XTReadDB().Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and is_sys=1", good_id, record_time, patient_id).Find(&info).Error
  5052. return info, err
  5053. }
  5054. 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) {
  5055. offset := (page - 1) * limit
  5056. db := XTReadDB().Model(&flow).Where("status = 1 and count > 0")
  5057. if consumable_type > 0 {
  5058. //入库
  5059. if consumable_type == 1 {
  5060. db = db.Where("consumable_type = ?", consumable_type)
  5061. }
  5062. //2 手动出库
  5063. if consumable_type == 2 {
  5064. db = db.Where(" consumable_type = ? and is_sys = 0", consumable_type)
  5065. }
  5066. //3.自动出库
  5067. if consumable_type == 3 {
  5068. db = db.Where(" consumable_type = ? and is_sys = 1", consumable_type)
  5069. }
  5070. //4.手动退库
  5071. if consumable_type == 4 {
  5072. db = db.Where(" consumable_type = ?", consumable_type)
  5073. }
  5074. //自动退库
  5075. if consumable_type == 7 {
  5076. db = db.Where(" consumable_type = ?", consumable_type)
  5077. }
  5078. if consumable_type == 10 {
  5079. db = db.Where(" consumable_type = ?", consumable_type)
  5080. }
  5081. if consumable_type == 11 {
  5082. db = db.Where(" consumable_type = ?", consumable_type)
  5083. }
  5084. if consumable_type == 12 {
  5085. db = db.Where(" consumable_type = ?", consumable_type)
  5086. }
  5087. if consumable_type == 13 {
  5088. db = db.Where(" consumable_type = ?", consumable_type)
  5089. }
  5090. if consumable_type == 15 {
  5091. db = db.Where(" consumable_type = ?", consumable_type)
  5092. }
  5093. }
  5094. if orgId > 0 {
  5095. db = db.Where("user_org_id = ?", orgId)
  5096. }
  5097. if startTime > 0 {
  5098. db = db.Where("ctime >= ?", startTime)
  5099. }
  5100. if endTime > 0 {
  5101. db = db.Where("ctime<=?", endTime)
  5102. }
  5103. if good_id > 0 {
  5104. db = db.Where("good_id = ?", good_id)
  5105. }
  5106. err = db.Count(&total).Offset(offset).Limit(limit).Order("id desc").Find(&flow).Error
  5107. return flow, total, err
  5108. }
  5109. func GetLastGoodListByPatientId(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.AutomaticReduceDetail, error) {
  5110. detail := models.AutomaticReduceDetail{}
  5111. 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
  5112. return detail, err
  5113. }
  5114. func GetLastGoodListByPatientIdOne(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.WarehouseOutInfo, error) {
  5115. detail := models.WarehouseOutInfo{}
  5116. 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
  5117. return detail, err
  5118. }
  5119. func UpdatedWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64) error {
  5120. outInfo := models.WarehouseOutInfo{}
  5121. 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
  5122. return err
  5123. }
  5124. func UpdatedNewWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) error {
  5125. outInfo := models.WarehouseOutInfo{}
  5126. err := tx.Model(&outInfo).Where("good_id = ? and patient_id = ? and sys_record_time =? and status = 1", good_id, patient_id, record_time).Updates(map[string]interface{}{"warehouse_out_id": info.WarehouseOutId, "WarehouseOutOrderNumber": info.WarehouseOutOrderNumber, "sys_record_time": info.SysRecordTime, "good_type_id": info.GoodTypeId, "patient_id": info.PatientId, "consumable_type": info.ConsumableType, "count": info.Count, "price": info.Price, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "number": info.Number, "license_number": info.LicenseNumber, "over_count": info.OverCount}).Error
  5127. if err != nil {
  5128. tx.Rollback()
  5129. return err
  5130. }
  5131. return err
  5132. }
  5133. func UpdatedWarehouseOutInfoSix(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) error {
  5134. outInfo := models.WarehouseOutInfo{}
  5135. 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
  5136. return err
  5137. }
  5138. func UpdatedNewWarehouseOutInfoSix(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) error {
  5139. outInfo := models.WarehouseOutInfo{}
  5140. err := tx.Model(&outInfo).Where("good_id = ? and patient_id = ? and sys_record_time =? and status = 1 and project_id = ? and warehouse_info_id =? ", good_id, patient_id, record_time, project_id, warehouse_info_id).Updates(map[string]interface{}{"warehouse_out_id": info.WarehouseOutId, "WarehouseOutOrderNumber": info.WarehouseOutOrderNumber, "sys_record_time": info.SysRecordTime, "good_type_id": info.GoodTypeId, "patient_id": info.PatientId, "consumable_type": info.ConsumableType, "count": info.Count, "price": info.Price, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "number": info.Number, "license_number": info.LicenseNumber, "over_count": info.OverCount}).Error
  5141. if err != nil {
  5142. tx.Rollback()
  5143. return err
  5144. }
  5145. return err
  5146. }
  5147. func GetLastWarehouOutInfoByPatientId(orgid int64, patient_id int64, record_time int64, goodid int64, good_type_id int64) (models.WarehouseOutInfo, error) {
  5148. info := models.WarehouseOutInfo{}
  5149. 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
  5150. return info, err
  5151. }
  5152. func GetLastStockOut(id int64) (models.WarehousingInfo, error) {
  5153. info := models.WarehousingInfo{}
  5154. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5155. return info, err
  5156. }
  5157. func GetStockGoodCount(good_id int64) (models.WarehousingInfo, error) {
  5158. info := models.WarehousingInfo{}
  5159. db := XTReadDB().Model(&info)
  5160. if good_id > 0 {
  5161. db = db.Where("good_id = ? and status = 1", good_id)
  5162. }
  5163. err := db.Select("sum(stock_count) as stock_count").Find(&info).Error
  5164. return info, err
  5165. }
  5166. 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) {
  5167. offset := (page - 1) * limit
  5168. db := XTReadDB().Model(drug_flow).Where("status = 1")
  5169. if drugid > 0 {
  5170. db = db.Where("drug_id = ?", drugid)
  5171. }
  5172. if orgid > 0 {
  5173. db = db.Where("user_org_id = ?", orgid)
  5174. }
  5175. if startTime > 0 {
  5176. db = db.Where("ctime >=? ", startTime)
  5177. }
  5178. if endTime > 0 {
  5179. db = db.Where("ctime<=?", endTime)
  5180. }
  5181. if stock_type > 0 {
  5182. //手动入库
  5183. if stock_type == 1 {
  5184. db = db.Where("consumable_type = 1")
  5185. }
  5186. //手动出库
  5187. if stock_type == 2 {
  5188. db = db.Where("consumable_type = 2 and is_sys = 0")
  5189. }
  5190. //自动出库
  5191. if stock_type == 3 {
  5192. db = db.Where("consumable_type = 3 and is_sys = 1")
  5193. }
  5194. if stock_type == 4 {
  5195. db = db.Where("consumable_type = 4")
  5196. }
  5197. if stock_type == 10 {
  5198. db = db.Where("consumable_type = 10")
  5199. }
  5200. if stock_type == 11 {
  5201. db = db.Where("consumable_type = 11")
  5202. }
  5203. if stock_type == 12 {
  5204. db = db.Where("consumable_type = 12")
  5205. }
  5206. if stock_type == 13 {
  5207. db = db.Where("consumable_type = 13")
  5208. }
  5209. if stock_type == 15 {
  5210. db = db.Where("consumable_type = 15")
  5211. }
  5212. }
  5213. 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
  5214. return drug_flow, total, err
  5215. }
  5216. func GetDrugWarehouseOutInfoIsExit(drug_id int64, patient_id int64, recordtime int64) (*models.DrugWarehouseOutInfo, error) {
  5217. outInfo := models.DrugWarehouseOutInfo{}
  5218. var err error
  5219. 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
  5220. if err == gorm.ErrRecordNotFound {
  5221. return nil, err
  5222. }
  5223. if err != nil {
  5224. return nil, err
  5225. }
  5226. return &outInfo, nil
  5227. }
  5228. func GetDrugWarehouseOutInfoIsExitOne(drug_id int64, patient_id int64, recordtime int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  5229. 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
  5230. return outInfo, err
  5231. }
  5232. func UpdatedDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, drug_id int64, patient_id int64, record_date int64, orgid int64) error {
  5233. outInfo := models.DrugWarehouseOutInfo{}
  5234. 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
  5235. return err
  5236. }
  5237. func GetCancelExportList(startime int64, endtime int64, ids []string, orgid int64) (cancle []*models.CancelStockInfo, err error) {
  5238. db := XTReadDB().Model(&cancle).Where("status = 1")
  5239. if startime > 0 {
  5240. db = db.Where("ctime >=?", startime)
  5241. }
  5242. if endtime > 0 {
  5243. db = db.Where("ctime <=?", endtime)
  5244. }
  5245. if len(ids) > 0 {
  5246. db = db.Where("cancel_stock_id in(?)", ids)
  5247. }
  5248. if orgid > 0 {
  5249. db = db.Where("org_id = ?", orgid)
  5250. }
  5251. db = db.Preload("GoodInfo", "status =1 and org_id = ?", orgid)
  5252. err = db.Find(&cancle).Error
  5253. return cancle, err
  5254. }
  5255. func GetCancelStockDetail(id int64) (info []*models.DrugCancelStockInfo, err error) {
  5256. db := XTReadDB().Model(&info)
  5257. if id > 0 {
  5258. db = db.Where("cancel_stock_id = ? and status = 1", id)
  5259. }
  5260. db = db.Preload("BaseDrugLib")
  5261. err = db.Find(&info).Error
  5262. return info, err
  5263. }
  5264. func GetDrugWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  5265. info := models.XtDrugWarehouseInfo{}
  5266. db := XTReadDB().Model(&info).Where("status = 1")
  5267. if id > 0 {
  5268. db = db.Where("id = ?", id)
  5269. }
  5270. err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  5271. return info, err
  5272. }
  5273. func GetDrugWarehouseInfoOne(id int64) (models.XtDrugWarehouseInfo, error) {
  5274. info := models.XtDrugWarehouseInfo{}
  5275. err := XTReadDB().Where("status = 1 and id=?", id).Find(&info).Error
  5276. return info, err
  5277. }
  5278. //func GetDrugWarehouseInfoTX(id int64,tx *gorm.DB) (models.XtDrugWarehouseInfo, error) {
  5279. // info := models.XtDrugWarehouseInfo{}
  5280. // db := tx.Model(&info).Where("status = 1")
  5281. // if id > 0 {
  5282. // db = db.Where("id = ?", id)
  5283. // }
  5284. // err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  5285. // return info, err
  5286. //}
  5287. func GetDrugWarehouseInfoPrescription(drugid int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5288. err = XTReadDB().Where("drug_id = ? and org_id =? and status = 1", drugid, orgid).Find(&info).Error
  5289. return info, err
  5290. }
  5291. func GetDrugWarehouseInfoPrescriptionSeven(drugid int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfoSeven, err error) {
  5292. 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
  5293. return info, err
  5294. }
  5295. func GetHisAdviceListByDrugId(drugid int64, patient_id int64, advice_date int64) (models.HisDoctorAdviceInfo, error) {
  5296. info := models.HisDoctorAdviceInfo{}
  5297. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1", drugid, patient_id, advice_date).Find(&info).Error
  5298. return info, err
  5299. }
  5300. func GetHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64) (info []*models.HisDoctorAdviceInfo, err error) {
  5301. err = XTReadDB().Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  5302. return info, err
  5303. }
  5304. func GetNewHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64, tx *gorm.DB) (info []*models.HisDoctorAdviceInfo, err error) {
  5305. err = tx.Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  5306. if err != gorm.ErrRecordNotFound {
  5307. if err != nil {
  5308. tx.Rollback()
  5309. return info, err
  5310. }
  5311. }
  5312. return info, err
  5313. }
  5314. func GetLastHisAdvicesList(patient_id int64, advice_date int64, user_org_id int64, tx *gorm.DB) (models.HisDoctorAdviceInfo, error) {
  5315. adviceInfo := models.HisDoctorAdviceInfo{}
  5316. err := tx.Where("patient_id = ? and advice_date= ? and user_org_id = ? and status=1", patient_id, advice_date, user_org_id).Last(&adviceInfo).Error
  5317. if err != gorm.ErrRecordNotFound {
  5318. if err != nil {
  5319. tx.Rollback()
  5320. return adviceInfo, err
  5321. }
  5322. }
  5323. return adviceInfo, err
  5324. }
  5325. func GetHisAdviceListByDrugIdEight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  5326. info := models.HisDoctorAdviceInfo{}
  5327. 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
  5328. return info, err
  5329. }
  5330. func GetHisAdviceListByDrugIdNight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  5331. info := models.HisDoctorAdviceInfo{}
  5332. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1 and id =?", drugid, patient_id, advice_date, advice_id).Find(&info).Error
  5333. return info, err
  5334. }
  5335. func GetLastDrugWarehouseOutByDrugId(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.DrugAutomaticReduceDetail, error) {
  5336. detail := models.DrugAutomaticReduceDetail{}
  5337. 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
  5338. return detail, err
  5339. }
  5340. func GetWarehouseOutListExport(drug_id int64, patient_id int64, record_date int64, advice_id int64) (out []*models.XtDrugWarehouseOutInfo, err error) {
  5341. err = XTReadDB().Where("drug_id = ? and patient_id = ? and sys_record_time = ? and advice_id =? and status =1", drug_id, patient_id, record_date, advice_id).Find(&out).Error
  5342. return out, err
  5343. }
  5344. func GetLastDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64) (out []*models.DrugWarehouseOutInfo, err error) {
  5345. 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
  5346. return out, err
  5347. }
  5348. func GetLastNewDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64, tx *gorm.DB) (out []*models.DrugWarehouseOutInfo, err error) {
  5349. err = tx.Where("advice_id = ? and drug_id = ? and patient_id = ? and sys_record_time = ? and status = 1 and is_check = 1", advice_id, drugid, patient_id, advice_date).Find(&out).Error
  5350. if err != gorm.ErrRecordNotFound {
  5351. if err != nil {
  5352. tx.Rollback()
  5353. return out, err
  5354. }
  5355. }
  5356. return out, err
  5357. }
  5358. func ModefyDrugByWarehouseInfo(id int64, stock_max_number int64) error {
  5359. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  5360. return err
  5361. }
  5362. func ModefyDrugByWarehouseInfoOne(id int64, stock_min_number int64) error {
  5363. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  5364. return err
  5365. }
  5366. func DeleteDrugAutoWarehouse(drugid int64, patient_id int64, record_time int64) error {
  5367. 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
  5368. 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
  5369. 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
  5370. return err
  5371. }
  5372. func DeleteDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  5373. 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
  5374. 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
  5375. return err
  5376. }
  5377. func DeleteNewDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  5378. 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
  5379. 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
  5380. return err
  5381. }
  5382. func GetDrugWarehouseInfoById(id int64) (models.XtDrugWarehouseInfo, error) {
  5383. info := models.XtDrugWarehouseInfo{}
  5384. err := XTReadDB().Model(&info).Where("id=? and status =1", id).Find(&info).Error
  5385. return info, err
  5386. }
  5387. func GetDrugWarehouseInfoByIdTwenty(id int64) (models.XtDrugWarehouseInfo, error) {
  5388. info := models.XtDrugWarehouseInfo{}
  5389. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  5390. return info, err
  5391. }
  5392. func UpdateDrugWarehouseInfo(info *models.XtDrugWarehouseInfo, id int64) error {
  5393. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  5394. return err
  5395. }
  5396. //func UpdateDrugWarehouseInfoTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  5397. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  5398. // return err
  5399. //}
  5400. func UpdateDrugWarehouseInfoOne(info *models.XtDrugWarehouseInfo, id int64) error {
  5401. //err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_min_number": info.StockMaxNumber}).Error
  5402. //return err
  5403. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  5404. return err
  5405. }
  5406. //func UpdateDrugWarehouseInfoOneTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  5407. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  5408. // return err
  5409. //}
  5410. func GetLastCancelStockInfo(id int64) (models.DrugCancelStockInfo, error) {
  5411. info := models.DrugCancelStockInfo{}
  5412. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5413. return info, err
  5414. }
  5415. func GetLastCancelStockInfoOne(id int64, drug_id int64) (models.DrugCancelStockInfo, error) {
  5416. info := models.DrugCancelStockInfo{}
  5417. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ? and status = 1", id, drug_id).Find(&info).Error
  5418. return info, err
  5419. }
  5420. func UpdatedDrugInfo(id int64, count int64) (err error) {
  5421. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  5422. return err
  5423. }
  5424. func UpdatedDrugFlow(cancelstockid int64, drug_id int64, flow models.DrugFlow) error {
  5425. 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
  5426. return err
  5427. }
  5428. func UpdatedDrugInfoOne(id int64, count int64) (err error) {
  5429. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  5430. return err
  5431. }
  5432. func UpdateWarehouseInfo(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  5433. 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
  5434. return err
  5435. }
  5436. func UpdateWarehouseInfoTwo(count int64, drugid int64, orgid int64) error {
  5437. 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
  5438. return err
  5439. }
  5440. func UpdateWarehouseInfoOne(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  5441. 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
  5442. return err
  5443. }
  5444. func UpdateWarehouseInfoFour(count int64, drugid int64, orgid int64) error {
  5445. 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
  5446. return err
  5447. }
  5448. func GetWarehouseInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  5449. info := models.WarehousingInfo{}
  5450. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5451. return info, err
  5452. }
  5453. func GetLastWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5454. info := models.WarehousingInfo{}
  5455. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  5456. return info, err
  5457. }
  5458. func GetLastWarehouseInfoTwo(goodid int64, storehouse_id int64, user_org_id int64) (info []*models.WarehousingInfo, err error) {
  5459. err = XTReadDB().Model(&info).Where("good_id = ? and status = 1 and storehouse_id=? and org_id = ? and stock_count > 0", goodid, storehouse_id, user_org_id).Order("ctime").First(&info).Error
  5460. return info, err
  5461. }
  5462. func GetFirstWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5463. info := models.WarehousingInfo{}
  5464. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).First(&info).Error
  5465. return info, err
  5466. }
  5467. func CreateWarehouseOut(out models.WarehouseOut) error {
  5468. err := XTWriteDB().Create(&out).Error
  5469. return err
  5470. }
  5471. func GetLastGoodWarehouseOut(orgid int64) (models.WarehouseOut, error) {
  5472. out := models.WarehouseOut{}
  5473. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  5474. return out, err
  5475. }
  5476. func CreateWarehouseInfo(info models.WarehousingInfo) error {
  5477. err := XTWriteDB().Create(&info).Error
  5478. return err
  5479. }
  5480. func UpdateGoodWarehouseInfo(id int64, info models.WarehousingInfo) error {
  5481. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5482. return err
  5483. }
  5484. func UpdateWareInfoById(id int64, count int64) error {
  5485. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  5486. return err
  5487. }
  5488. func UpdateWareInfoByIdSix(id int64, count int64) error {
  5489. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  5490. return err
  5491. }
  5492. func GetLastCancelInfo(id int64, goodid int64) (models.CancelStockInfo, error) {
  5493. info := models.CancelStockInfo{}
  5494. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5495. return info, err
  5496. }
  5497. func GetLastCancelInfoSix(id int64, goodid int64, storehouse_id int64) (models.CancelStockInfo, error) {
  5498. info := models.CancelStockInfo{}
  5499. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1 and storehouse_id = ?", id, goodid, storehouse_id).Find(&info).Error
  5500. return info, err
  5501. }
  5502. func GetAllCancelInfoById(id int64, goodid int64) (info []*models.CancelStockInfo, err error) {
  5503. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5504. return info, err
  5505. }
  5506. func GetAllCancelInfoByIdSix(id int64, goodid int64, storehouse_id int64) (info []*models.CancelStockInfo, err error) {
  5507. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5508. return info, err
  5509. }
  5510. func UpdateStockFlow(flow models.VmStockFlow, goodid int64, warehousing_id int64) error {
  5511. err := XTWriteDB().Model(&flow).Where("good_id = ? and warehousing_id= ? and status = 1", goodid, warehousing_id).Updates(map[string]interface{}{"count": flow.Count}).Error
  5512. return err
  5513. }
  5514. func UpdateDrugFlow(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5515. drugFlow := models.DrugFlow{}
  5516. 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
  5517. return err
  5518. }
  5519. func UpdateDrugFlowNight(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5520. drugFlow := models.DrugFlow{}
  5521. 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
  5522. return err
  5523. }
  5524. func UpdateDrugCancel(id int64) (models.DrugCancelStock, error) {
  5525. stock := models.DrugCancelStock{}
  5526. err := XTWriteDB().Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5527. return stock, err
  5528. }
  5529. //func UpdateDrugCancelTX(id int64,tx *gorm.DB) (models.DrugCancelStock, error) {
  5530. // stock := models.DrugCancelStock{}
  5531. // err := tx.Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5532. // return stock, err
  5533. //}
  5534. func GetDrugWarehouseOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugWarehouseOutInfo, err error) {
  5535. db := XTReadDB().Model(&info)
  5536. if warehouse_info_id > 0 {
  5537. db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5538. }
  5539. if drug_id > 0 {
  5540. db = db.Where("drug_id = ? and status =1", drug_id)
  5541. }
  5542. err = db.Find(&info).Error
  5543. return info, err
  5544. }
  5545. //func GetDrugWarehouseOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  5546. // db := tx.Model(&info)
  5547. // if warehouse_info_id > 0 {
  5548. // db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5549. // }
  5550. // if drug_id > 0 {
  5551. // db = db.Where("drug_id = ? and status =1", drug_id)
  5552. // }
  5553. // err = db.Find(&info).Error
  5554. // return info, err
  5555. //}
  5556. func GetCancelDrugStockOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugCancelStockInfo, err error) {
  5557. err = XTReadDB().Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5558. return info, err
  5559. }
  5560. //func GetCancelDrugStockOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugCancelStockInfo, err error) {
  5561. // err = tx.Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5562. // return info, err
  5563. //}
  5564. func GetTemplateSummary(orgid int64) (config []*models.Dataconfig, err error) {
  5565. err = XTReadDB().Model(&config).Where("module='template_summary' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5566. return config, err
  5567. }
  5568. func GetTemplatePlan(orgid int64) (config []*models.Dataconfig, err error) {
  5569. err = XTReadDB().Model(&config).Where("module='template_plan' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5570. return config, err
  5571. }
  5572. func GetSearchGoodWarehouseList(keyword string, orgid int64, storehouse_id int64) (info []*models.BloodWarehouseInfo, err error) {
  5573. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  5574. table := XTReadDB().Table("xt_goood_information as t").Where("t.status")
  5575. fmt.Println(table)
  5576. likeKey := "%" + keyword + "%"
  5577. if orgid > 0 {
  5578. db = db.Where("x.org_id = ?", orgid)
  5579. }
  5580. if storehouse_id > 0 {
  5581. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5582. }
  5583. 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
  5584. return info, err
  5585. }
  5586. func CreateAdjustPrice(adjustPrice *models.XtStockAdjustPrice) error {
  5587. err := XTWriteDB().Create(&adjustPrice).Error
  5588. return err
  5589. }
  5590. func GetAllStockPrice(orgid int64, startime int64, endtime int64, keyword string, limit int64, page int64) (list []*models.VmStockAdjustPrice, total int64, err error) {
  5591. likeKey := "%" + keyword + "%"
  5592. offset := (page - 1) * limit
  5593. db := XTReadDB().Table("xt_stock_adjust_price as x").Where("x.status = 1")
  5594. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5595. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5596. fmt.Println(table, tab)
  5597. if startime > 0 {
  5598. db = db.Where("x.start_time>=?", startime)
  5599. }
  5600. if endtime > 0 {
  5601. db = db.Where("x.start_time<=?", endtime)
  5602. }
  5603. if len(keyword) > 0 {
  5604. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  5605. }
  5606. if orgid > 0 {
  5607. db = db.Where("x.user_org_id = ?", orgid)
  5608. }
  5609. 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
  5610. return list, total, err
  5611. }
  5612. func UpdateAdjustPrice(ids []string, info models.XtStockAdjustPrice) error {
  5613. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5614. return err
  5615. }
  5616. func UpdateAdjustCheckPrice(info *models.GoodInfo, id int64) error {
  5617. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"packing_price": info.PackingPrice, "buy_price": info.BuyPrice}).Error
  5618. return err
  5619. }
  5620. func GetAdjustCheckPriceList(ids []string) (info []*models.XtStockAdjustPrice, err error) {
  5621. err = XTReadDB().Where("id in(?)", ids).Find(&info).Error
  5622. return info, err
  5623. }
  5624. func GetAdjustPricebyIdOne(ids []string) (adjust []*models.XtStockAdjustPrice, err error) {
  5625. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5626. return adjust, err
  5627. }
  5628. func CreateReportPrice(price *models.XtStockReportPrice) error {
  5629. err := XTWriteDB().Create(&price).Error
  5630. return err
  5631. }
  5632. func GetReportStockList(startime int64, endtime int64, orgid int64, keyword string, page int64, limit int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5633. likeKey := "%" + keyword + "%"
  5634. offset := (page - 1) * limit
  5635. db := XTReadDB().Table("xt_stock_report_price as x").Where("x.status = 1")
  5636. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5637. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5638. fmt.Println(table, tab)
  5639. if startime > 0 {
  5640. db = db.Where("x.ctime>=?", startime)
  5641. }
  5642. if endtime > 0 {
  5643. db = db.Where("x.ctime<=?", endtime)
  5644. }
  5645. if len(keyword) > 0 {
  5646. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5647. }
  5648. if orgid > 0 {
  5649. db = db.Where("x.user_org_id = ?", orgid)
  5650. }
  5651. 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
  5652. return adjust, total, err
  5653. }
  5654. func GetStockDamagedList(orgid int64, keyword string, page int64, limit int64, startime int64, endtime int64, storehouse_id int64) (adjust []*models.VmStockInventory, total int64, err error) {
  5655. likeKey := "%" + keyword + "%"
  5656. offset := (page - 1) * limit
  5657. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.inventory_type = 4")
  5658. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5659. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5660. fmt.Println(table, tab)
  5661. if len(keyword) > 0 {
  5662. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5663. }
  5664. if orgid > 0 {
  5665. db = db.Where("x.user_org_id = ?", orgid)
  5666. }
  5667. if startime > 0 {
  5668. db = db.Where("x.ctime >=?", startime)
  5669. }
  5670. if endtime > 0 {
  5671. db = db.Where("x.ctime <=?", endtime)
  5672. }
  5673. if storehouse_id > 0 {
  5674. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5675. }
  5676. err = db.Group("x.good_id").Select("x.ctime,x.id,x.good_name,x.specification_name,x.warehousing_unit,x.count,x.buy_price,x.packing_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.good_id,x.warehousing_order,x.license_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.total,x.number,x.storehouse_id,t.packing_unit,r.user_name").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.creater").Joins("left join xt_good_information as t on t.id =x.good_id").Count(&total).Offset(offset).Limit(limit).Scan(&adjust).Error
  5677. return adjust, total, err
  5678. }
  5679. func GetStockDamagedCountOne(orgid int64, keyword string, inventory_type int64) (adjust []*models.VmStockInventory, err error) {
  5680. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5681. if inventory_type == 0 {
  5682. db = db.Where("x.inventory_type = 4")
  5683. }
  5684. if inventory_type > 0 {
  5685. db = db.Where("x.inventory_type = ?", inventory_type)
  5686. }
  5687. if len(keyword) > 0 {
  5688. likeKey := "%" + keyword + "%"
  5689. db = db.Where("x.warehousing_order like ?", likeKey)
  5690. }
  5691. if orgid > 0 {
  5692. db = db.Where("x.user_org_id = ?", orgid)
  5693. }
  5694. 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
  5695. return adjust, err
  5696. }
  5697. func GetStockDamagedCount(orgid int64) (adjust []*models.VmStockInventory, err error) {
  5698. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and inventory_type = 4 ")
  5699. if orgid > 0 {
  5700. db = db.Where("x.user_org_id = ?", orgid)
  5701. }
  5702. 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
  5703. return adjust, err
  5704. }
  5705. func UpdateCheckDamage(ids []string, reportPrice models.XtStockReportPrice) error {
  5706. 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
  5707. return err
  5708. }
  5709. func GetCheckDamageList(ids []string) (report []*models.XtStockReportPrice, err error) {
  5710. err = XTReadDB().Model(&report).Where("id in(?) and status= 1", ids).Find(&report).Error
  5711. return report, err
  5712. }
  5713. func CreateInentory(inventory models.XtStockInventory) error {
  5714. err = XTWriteDB().Create(&inventory).Error
  5715. return err
  5716. }
  5717. func GetInventorylist(startime int64, endtime int64, page int64, limit int64, inventory_status int64, keyword string, orgid int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5718. likeKey := "%" + keyword + "%"
  5719. offset := (page - 1) * limit
  5720. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5721. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5722. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5723. fmt.Println(table, tab)
  5724. if startime > 0 {
  5725. db = db.Where("x.ctime>=?", startime)
  5726. }
  5727. if endtime > 0 {
  5728. db = db.Where("x.ctime<=?", endtime)
  5729. }
  5730. if len(keyword) > 0 {
  5731. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5732. }
  5733. if orgid > 0 {
  5734. db = db.Where("x.user_org_id = ?", orgid)
  5735. }
  5736. if inventory_status > 0 {
  5737. db = db.Where("checker_status = ?", inventory_status)
  5738. }
  5739. 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
  5740. return adjust, total, err
  5741. }
  5742. func UpdateCheckInventory(ids []string, info models.XtStockInventory) error {
  5743. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5744. return err
  5745. }
  5746. func GetModifyPriceDetail(id int64) (models.XtStockAdjustPrice, error) {
  5747. info := models.XtStockAdjustPrice{}
  5748. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5749. return info, err
  5750. }
  5751. func GetInventoryDetailById(id []string) (list []*models.XtStockInventory, err error) {
  5752. err = XTReadDB().Model(&list).Where("id in(?) and status = 1", id).Find(&list).Error
  5753. return list, err
  5754. }
  5755. func GetWarehouseInfoByIdNight(id int64) (models.WarehousingInfo, error) {
  5756. info := models.WarehousingInfo{}
  5757. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5758. return info, err
  5759. }
  5760. func GetWarehouseInfoById(id int64) (models.WarehousingInfo, error) {
  5761. info := models.WarehousingInfo{}
  5762. //err :=XTReadDB().Model(&info).Where("id = ? or warehouse_info_id and status = 1", id).Find(&info).Error
  5763. //return info, err
  5764. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5765. if id > 0 {
  5766. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5767. }
  5768. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5769. return info, err
  5770. }
  5771. func GetWarehouseInfoByIdTen(id int64, storehouse_id int64) (models.WarehousingInfo, error) {
  5772. info := models.WarehousingInfo{}
  5773. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5774. if id > 0 {
  5775. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5776. }
  5777. if storehouse_id > 0 {
  5778. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5779. }
  5780. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5781. return info, err
  5782. }
  5783. func UpdateStockPrice(info models.XtStockAdjustPrice, id int64) error {
  5784. 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
  5785. return err
  5786. }
  5787. func DeleteStockPrice(id int64) error {
  5788. info := models.XtStockAdjustPrice{}
  5789. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5790. return err
  5791. }
  5792. func GetStockDamageDetail(id int64) (models.XtStockReportPrice, error) {
  5793. report := models.XtStockReportPrice{}
  5794. err := XTReadDB().Model(&report).Where("id=? and status = 1", id).Find(&report).Error
  5795. return report, err
  5796. }
  5797. func ModifyPrice(id int64, info models.XtStockReportPrice) error {
  5798. 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
  5799. return err
  5800. }
  5801. func DeleteDamage(id int64) error {
  5802. adjust := models.XtStockReportPrice{}
  5803. err := XTWriteDB().Model(&adjust).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5804. return err
  5805. }
  5806. func GetStockDamageById(ids []string) (adjust []*models.XtStockReportPrice, err error) {
  5807. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5808. return adjust, err
  5809. }
  5810. func GetInventoryDetail(id int64) (models.XtStockInventory, error) {
  5811. inventory := models.XtStockInventory{}
  5812. err := XTReadDB().Model(&inventory).Where("id=? and status = 1", id).Find(&inventory).Error
  5813. return inventory, err
  5814. }
  5815. func ModifyInventory(id int64, inventory models.XtStockInventory) error {
  5816. 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
  5817. return err
  5818. }
  5819. func DeleteInventory(id int64) error {
  5820. inventory := models.XtStockInventory{}
  5821. err := XTWriteDB().Model(&inventory).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5822. return err
  5823. }
  5824. func GetInventoryDetailList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64) (list []*models.VmStockInventory, total int64, err error) {
  5825. likeKey := "%" + keyword + "%"
  5826. offset := (page - 1) * limit
  5827. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5828. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5829. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5830. fmt.Println(table, tab)
  5831. if len(keyword) > 0 {
  5832. db = db.Where("x.good_name like ?", likeKey)
  5833. }
  5834. if orgid > 0 {
  5835. db = db.Where("x.user_org_id = ?", orgid)
  5836. }
  5837. if storehouse_id > 0 {
  5838. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5839. }
  5840. err = db.Select("x.id,x.good_name,x.specification_name,x.warehousing_unit,x.count,x.buy_price,x.packing_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.good_id,x.warehousing_order,x.license_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.total,x.number,x.expire_date,x.product_date,t.packing_unit,x.last_stock_count,x.type,x.inventory_type,x.stock_count,x.storehouse_id,x.ctime").Joins("left join sgj_users.sgj_user_admin_role as r on r.id = x.creater").Joins("left join xt_good_information as t on t.id =x.good_id").Count(&total).Offset(offset).Limit(limit).Order("x.id desc").Scan(&list).Error
  5841. return list, total, err
  5842. }
  5843. func GetInventoryDetailPrintMapList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64, ids []string) (list []*models.VmStockInventory, total int64, err error) {
  5844. likeKey := "%" + keyword + "%"
  5845. offset := (page - 1) * limit
  5846. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5847. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5848. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5849. fmt.Println(table, tab)
  5850. if len(keyword) > 0 {
  5851. db = db.Where("x.good_name like ?", likeKey)
  5852. }
  5853. if orgid > 0 {
  5854. db = db.Where("x.user_org_id = ?", orgid)
  5855. }
  5856. if storehouse_id > 0 {
  5857. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5858. }
  5859. if len(ids) > 0 {
  5860. db = db.Where("x.id in(?)", ids)
  5861. }
  5862. 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
  5863. return list, total, err
  5864. }
  5865. func GetInventoryPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5866. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5867. return list, err
  5868. }
  5869. func GetInventoryDetailPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5870. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5871. return list, err
  5872. }
  5873. func DeleteDrugPrice(id int64) error {
  5874. info := models.XtDrugAdjustPrice{}
  5875. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5876. return err
  5877. }
  5878. func GetGoodWarehouseList(goodid int64) (models.WarehousingInfo, error) {
  5879. info := models.WarehousingInfo{}
  5880. db := XTReadDB().Model(&info).Where("status = 1")
  5881. if goodid > 0 {
  5882. db = db.Where("good_id = ?", goodid)
  5883. }
  5884. err := db.Select("good_id,sum(stock_count) as stock_count").Find(&info).Error
  5885. return info, err
  5886. }
  5887. func UpdateProofInventory(id int64, info models.WarehousingInfo) error {
  5888. err := XTWriteDB().Model(&info).Where("id=? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5889. return err
  5890. }
  5891. func UpdateStockInventory(inventory models.XtStockInventory, id int64) error {
  5892. err := XTWriteDB().Model(&inventory).Where("id= ? and status = 1", id).Updates(map[string]interface{}{"total": inventory.Total}).Error
  5893. return err
  5894. }
  5895. func CeateStockCorrectRecord(record models.XtStockCorrectRecord) error {
  5896. err := XTWriteDB().Create(&record).Error
  5897. return err
  5898. }
  5899. func GetWarehouseTotal(id int64) (models.WarehousingInfo, error) {
  5900. info := models.WarehousingInfo{}
  5901. //err :=XTReadDB()()()()().Where("id=? and status = 1", id).Find(&info).Error
  5902. //return info, err
  5903. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5904. if id > 0 {
  5905. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5906. }
  5907. err := db.Select("sum(x.stock_count) as stock_count").Scan(&info).Error
  5908. return info, err
  5909. }
  5910. func GetCurrentWarehosueInfo(id int64) (info []*models.DrugWarehouseInfo, err error) {
  5911. err = XTReadDB().Model(&info).Where("id=? or warehousing_info_id = ? ", id, id).Find(&info).Error
  5912. return info, err
  5913. }
  5914. func GetCurrentWarehosueInfoOne(id int64) (models.DrugWarehouseInfo, error) {
  5915. info := models.DrugWarehouseInfo{}
  5916. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  5917. return info, err
  5918. }
  5919. func ModifyDrugWarehouseInfo(info *models.DrugWarehouseInfo, id int64) error {
  5920. 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
  5921. return err
  5922. }
  5923. func ModifyDrugWarehouseInfoSix(info *models.DrugWarehouseInfo, id int64) error {
  5924. 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
  5925. return err
  5926. }
  5927. func GetGoodInventoryWarehouseList(id int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  5928. err = XTReadDB().Model(&info).Where("good_id = ? and status = 1 and storehouse_id=? and is_check= 1 and stock_count>0", id, storehouse_id).Preload("GoodInfo", "status = 1").Find(&info).Error
  5929. return info, err
  5930. }
  5931. func UpdatedWarehousingInfo(info models.WarehousingInfo, id int64) error {
  5932. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5933. return err
  5934. }
  5935. func UpdatedWarehousingInfoSix(info models.WarehousingInfo, id int64) error {
  5936. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount, "warehousing_count": info.WarehousingCount}).Error
  5937. return err
  5938. }
  5939. func GetDamageDetailByGoodId(good_id int64, keyword string, inventory_type int64) (list []*models.XtStockInventory, err error) {
  5940. db := XTReadDB().Model(&list).Where(" status = 1")
  5941. if good_id > 0 {
  5942. db = db.Where("good_id = ?", good_id)
  5943. }
  5944. if len(keyword) > 0 {
  5945. likeKey := "%" + keyword + "%"
  5946. db = db.Where("warehousing_order like ?", likeKey)
  5947. }
  5948. if inventory_type > 0 {
  5949. db = db.Where("inventory_type = ?", inventory_type)
  5950. }
  5951. if inventory_type == 0 {
  5952. db = db.Where("inventory_type = 4")
  5953. }
  5954. err = db.Find(&list).Error
  5955. return list, err
  5956. }
  5957. func GetCancelStockId(cancelstock_id int64, drug_id int64) (*models.DrugFlow, error) {
  5958. flow := models.DrugFlow{}
  5959. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ?", cancelstock_id, drug_id).Find(&flow).Error
  5960. if err == gorm.ErrRecordNotFound {
  5961. return nil, err
  5962. }
  5963. if err != nil {
  5964. return nil, err
  5965. }
  5966. return &flow, nil
  5967. }
  5968. func GetTotalDrugCancelInfo(batchNumber_id int64, drug_id int64, id int64) (info []*models.DrugCancelStockInfo, err error) {
  5969. err = XTReadDB().Where("batch_number_id = ? and drug_id = ? and status = 1 and id<>?", batchNumber_id, drug_id, id).Find(&info).Error
  5970. return info, err
  5971. }
  5972. func GetLastDrugWarehouseInfo(drugid int64) (models.XtDrugWarehouseInfo, error) {
  5973. info := models.XtDrugWarehouseInfo{}
  5974. err := XTReadDB().Where("drug_id = ? and status =1", drugid).Last(&info).Error
  5975. return info, err
  5976. }
  5977. func GetLastGoodWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5978. info := models.WarehousingInfo{}
  5979. err := XTReadDB().Where("good_id =? and status = 1", goodid).Find(&info).Error
  5980. return info, err
  5981. }
  5982. func GetGoodStockFlowInfo(warehouseid int64, goodid int64) (*models.VmStockFlow, error) {
  5983. flow := models.VmStockFlow{}
  5984. err := XTReadDB().Where("warehousing_detail_id = ? and good_id = ? and status = 1", warehouseid, goodid).Find(&flow).Error
  5985. if err == gorm.ErrRecordNotFound {
  5986. return nil, err
  5987. }
  5988. if err != nil {
  5989. return nil, err
  5990. }
  5991. return &flow, nil
  5992. }
  5993. func GetLastInfoMationById(id int64) (models.GoodInfo, error) {
  5994. info := models.GoodInfo{}
  5995. err := XTReadDB().Where("id =? and status = 1", id).Find(&info).Error
  5996. return info, err
  5997. }
  5998. func UpdateGoodInfoMation(goodid int64, info models.GoodInfo) error {
  5999. err := XTWriteDB().Model(&info).Where("id =? and status = 1", goodid).Updates(map[string]interface{}{"total": info.Total}).Error
  6000. return err
  6001. }
  6002. func UpdateWarehouseInfoByGoodId(info models.WarehousingInfo, id int64) error {
  6003. err := XTWriteDB().Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount}).Error
  6004. return err
  6005. }
  6006. func UpdateWarehouseInfoByGoodIdOne(info *models.WarehousingInfo, id int64) error {
  6007. err := XTWriteDB().Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount, "price": info.Price, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "license_number": info.LicenseNumber, "manufacturer": info.Manufacturer, "dealer": info.Dealer, "remark": info.Remark, "packing_price": info.PackingPrice, "storehouse_id": info.StorehouseId, "register_number": info.RegisterNumber, "good_id": info.GoodId, "good_code": info.GoodCode}).Error
  6008. return err
  6009. }
  6010. func UpdateNewWarehouseInfoByGoodIdOne(info *models.WarehousingInfo, id int64, tx *gorm.DB) error {
  6011. err := tx.Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount, "price": info.Price, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "license_number": info.LicenseNumber, "manufacturer": info.Manufacturer, "dealer": info.Dealer, "remark": info.Remark, "packing_price": info.PackingPrice, "storehouse_id": info.StorehouseId, "register_number": info.RegisterNumber, "good_id": info.GoodId, "good_code": info.GoodCode}).Error
  6012. if err != nil {
  6013. tx.Rollback()
  6014. return err
  6015. }
  6016. return err
  6017. }
  6018. func UpdatedStockFlowByGoodId(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  6019. 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
  6020. return err
  6021. }
  6022. func UpdatedStockFlowByGoodIdOne(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  6023. 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
  6024. return err
  6025. }
  6026. func UpdateStockFlowByDetailId(flow models.VmStockFlow, wareoutdetailid int64) error {
  6027. 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
  6028. return err
  6029. }
  6030. func UpdatedGoodInfo(info models.GoodInfo, goodid int64) error {
  6031. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", goodid).Updates(map[string]interface{}{"manufacturer": info.Manufacturer, "dealer": info.Dealer}).Error
  6032. return err
  6033. }
  6034. func GetInventoryExportList(orgid int64) (info []*models.PatientWarehouseInfo, err error) {
  6035. if orgid == 10653 {
  6036. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.warehousing_count>0 and x.stock_count>0")
  6037. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  6038. fmt.Println(table)
  6039. if orgid > 0 {
  6040. db = db.Where("x.org_id = ?", orgid)
  6041. }
  6042. 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
  6043. return info, err
  6044. } else {
  6045. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.warehousing_count>0")
  6046. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  6047. fmt.Println(table)
  6048. if orgid > 0 {
  6049. db = db.Where("x.org_id = ?", orgid)
  6050. }
  6051. 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
  6052. return info, err
  6053. }
  6054. }
  6055. func UpdateDrugWarehouseingInfoSix(id int64, info models.XtDrugWarehouseInfo) error {
  6056. warehouseInfo := models.XtDrugWarehouseInfo{}
  6057. 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
  6058. return err
  6059. }
  6060. func CreatedCancelStock(info models.DrugCancelStockInfo) error {
  6061. err := XTWriteDB().Create(&info).Error
  6062. return err
  6063. }
  6064. func CreatedNewCancelStock(info models.DrugCancelStockInfo, tx *gorm.DB) error {
  6065. err := tx.Create(&info).Error
  6066. if err != nil {
  6067. tx.Rollback()
  6068. return err
  6069. }
  6070. return err
  6071. }
  6072. func IsExsitStockFlow(goodid int64, cancel_order_number string) (*models.VmStockFlow, error) {
  6073. flow := models.VmStockFlow{}
  6074. err := XTReadDB().Where("good_id = ? and cancel_order_number = ? and status = 1", goodid, cancel_order_number).Find(&flow).Error
  6075. if err == gorm.ErrRecordNotFound {
  6076. return nil, err
  6077. }
  6078. if err != nil {
  6079. return nil, err
  6080. }
  6081. return &flow, nil
  6082. }
  6083. func UpdateStockFlowTwo(id int64, flow models.VmStockFlow) error {
  6084. err := XTWriteDB().Model(&flow).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"count": flow.Count}).Error
  6085. return err
  6086. }
  6087. func UpdateStockFlowThree(id int64, flow models.VmStockFlow) error {
  6088. 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
  6089. return err
  6090. }
  6091. func GetLastWarehouseOutInfoById(drugid int64) (models.DrugWarehouseOutInfo, error) {
  6092. flow := models.DrugWarehouseOutInfo{}
  6093. err := XTReadDB().Where("drug_id = ? and status = 1", drugid).Last(&flow).Error
  6094. return flow, err
  6095. }
  6096. func GetLastNewWarehouseOutInfoById(drugid int64, tx *gorm.DB) (models.DrugWarehouseOutInfo, error) {
  6097. flow := models.DrugWarehouseOutInfo{}
  6098. err := tx.Where("drug_id = ? and status = 1", drugid).Last(&flow).Error
  6099. if err != gorm.ErrRecordNotFound {
  6100. if err != nil {
  6101. tx.Rollback()
  6102. return flow, err
  6103. }
  6104. }
  6105. return flow, err
  6106. }
  6107. func GetLastWarehoseByGoodId(goodid int64, patient_id int64, record_date int64) (models.WarehouseOutInfo, error) {
  6108. info := models.WarehouseOutInfo{}
  6109. 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
  6110. return info, err
  6111. }
  6112. func ModifyDrugWarehouseOutInfo(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64) error {
  6113. 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
  6114. return err
  6115. }
  6116. func ModifyDrugWarehouseOutInfoOne(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64, warehouse_info_id int64) error {
  6117. 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
  6118. return err
  6119. }
  6120. func GetDrugWarehouseOutInfoByWarehosueId(drugid int64, patientid int64, recorddate int64, warehosueinfoid int64) (*models.DrugWarehouseOutInfo, error) {
  6121. info := models.DrugWarehouseOutInfo{}
  6122. 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
  6123. if err == gorm.ErrRecordNotFound {
  6124. return nil, err
  6125. }
  6126. if err != nil {
  6127. return nil, err
  6128. }
  6129. return &info, nil
  6130. }
  6131. func GetDrugFlowRecord(warehouse_out_order_number string, drugid int64, patientid int64) (*models.DrugFlow, error) {
  6132. drugFlow := models.DrugFlow{}
  6133. 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
  6134. if err == gorm.ErrRecordNotFound {
  6135. return nil, err
  6136. }
  6137. if err != nil {
  6138. return nil, err
  6139. }
  6140. return &drugFlow, nil
  6141. }
  6142. func ModifyDrugFlow(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64) error {
  6143. 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
  6144. return err
  6145. }
  6146. func ModifyDrugFlowOne(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64, warehousing_detail_id int64) error {
  6147. 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
  6148. return err
  6149. }
  6150. func GetDrugAutoReduceRecordOne(drugid int64, patientid int64, recorddate int64) (detail []*models.DrugAutomaticReduceDetail, err error) {
  6151. err = XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  6152. return detail, err
  6153. }
  6154. func GetDrugAutoReduceRecord(drugid int64, patientid int64, recorddate int64) (*models.DrugAutomaticReduceDetail, error) {
  6155. detail := models.DrugAutomaticReduceDetail{}
  6156. err := XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  6157. if err == gorm.ErrRecordNotFound {
  6158. return nil, err
  6159. }
  6160. if err != nil {
  6161. return nil, err
  6162. }
  6163. return &detail, nil
  6164. }
  6165. func ModifyAutoMaticReduce(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64) error {
  6166. 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
  6167. return err
  6168. }
  6169. func ModifyAutoMaticReduceOne(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64, warehouse_info_id int64) error {
  6170. 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
  6171. return err
  6172. }
  6173. func ModifyDrugWarehouse(id int64, stock_max_number int64, stock_min_number int64) (models.DrugWarehouseInfo, error) {
  6174. info := models.DrugWarehouseInfo{}
  6175. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  6176. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  6177. return info, err
  6178. }
  6179. func ModeifyDrugWarehosueInfo(id int64, stock_max_number int64) error {
  6180. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  6181. return err
  6182. }
  6183. func ModeFyStockMinById(id int64, stock_min_number int64) error {
  6184. info := models.DrugWarehouseInfo{}
  6185. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  6186. return err
  6187. }
  6188. func GetDrugCancelById(id int64) (models.DrugCancelStockInfo, error) {
  6189. info := models.DrugCancelStockInfo{}
  6190. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  6191. return info, err
  6192. }
  6193. func ModeFyDrugWareseInfo(id int64, stock_max_number int64) error {
  6194. info := models.DrugWarehouseInfo{}
  6195. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", stock_max_number)).Error
  6196. return err
  6197. }
  6198. func ModeFyDrugWarehouseMin(id int64, stock_min_number int64) error {
  6199. info := models.DrugWarehouseInfo{}
  6200. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", stock_min_number)).Error
  6201. return err
  6202. }
  6203. func GetAllHisDoctorAdviceById(drugid int64, patient_id int64, advicedate int64, orgid int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  6204. 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
  6205. return advice, err
  6206. }
  6207. func GetAllHisDoctorAdviceByIdOne(drugid int64, patient_id int64, advicedate int64, orgid int64, tx *gorm.DB) (advice []*models.HisDoctorAdviceInfo, err error) {
  6208. err = tx.Where("drug_id = ? and patient_id = ? and advice_date = ? and execution_state = 1 and status = 1 and user_org_id = ?", drugid, patient_id, advicedate, orgid).Find(&advice).Error
  6209. if err != gorm.ErrRecordNotFound {
  6210. if err != nil {
  6211. tx.Rollback()
  6212. return advice, err
  6213. }
  6214. }
  6215. return advice, err
  6216. }
  6217. func GetAllDrugFlowById(drugid int64, patient_id int64, advicedate int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  6218. 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
  6219. return info, err
  6220. }
  6221. func GetAllDrugFlowByIdOne(drugid int64, patient_id int64, advicedate int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  6222. err = tx.Where("drug_id = ? and patient_id = ? and sys_record_time = ? and org_id = ? and status = 1", drugid, patient_id, advicedate, orgid).Find(&info).Error
  6223. if err != gorm.ErrRecordNotFound {
  6224. if err != nil {
  6225. tx.Rollback()
  6226. return info, err
  6227. }
  6228. }
  6229. return info, err
  6230. }
  6231. // 耗材加库存
  6232. func ModifyGoodAddInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6233. info := models.GoodInfo{}
  6234. 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
  6235. return info, err
  6236. }
  6237. func ModifyGoodAddInformationSix(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6238. info := models.GoodInfo{}
  6239. 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
  6240. 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
  6241. return info, err
  6242. }
  6243. func ModifyGoodReduceInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6244. info := models.GoodInfo{}
  6245. 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
  6246. return info, err
  6247. }
  6248. func ModifyGoodReduceInformationTwo(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6249. info := models.GoodInfo{}
  6250. 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
  6251. 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
  6252. return info, err
  6253. }
  6254. func ModifyGoodReduceInformationSix(good_id int64, sum_count int64, orgid int64, retail_price float64) (models.GoodInfo, error) {
  6255. info := models.GoodInfo{}
  6256. 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
  6257. return info, err
  6258. }
  6259. func GetWarehousingByIdList(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  6260. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  6261. return info, err
  6262. }
  6263. func GetWarehousingByIdListTwenty(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  6264. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  6265. return info, err
  6266. }
  6267. func GetWarehouseInfoByIdSix(id int64, orgid int64) (models.WarehousingInfo, error) {
  6268. info := models.WarehousingInfo{}
  6269. err := XTReadDB().Model(&info).Where("id = ? and status= 1 and org_id = ?", id, orgid).Find(&info).Error
  6270. return info, err
  6271. }
  6272. func GetWarehouseInfoByIdTwenty(id int64, orgid int64) (models.WarehousingInfo, error) {
  6273. info := models.WarehousingInfo{}
  6274. err := XTReadDB().Model(&info).Where("id = ? and org_id = ?", id, orgid).Find(&info).Error
  6275. return info, err
  6276. }
  6277. // 药品加库存
  6278. func ModifyDrugAddInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6279. info := models.BaseDrugLib{}
  6280. 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
  6281. return info, err
  6282. }
  6283. func ModifyDrugAddInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6284. info := models.BaseDrugLib{}
  6285. 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
  6286. 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
  6287. return info, err
  6288. }
  6289. func ModifyDrugReduceInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6290. info := models.BaseDrugLib{}
  6291. 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
  6292. return info, err
  6293. }
  6294. func ModifyDrugReduceInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6295. info := models.BaseDrugLib{}
  6296. 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
  6297. 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
  6298. return info, err
  6299. }
  6300. func ModifyDrugReduceInformationSix(drug_id int64, sum_count int64, orgid int64, batch_retai_price float64) (models.BaseDrugLib, error) {
  6301. info := models.BaseDrugLib{}
  6302. 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
  6303. 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
  6304. return info, err
  6305. }
  6306. func GetLastDrugwarehouseInfoById(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  6307. err = XTReadDB().Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  6308. return info, err
  6309. }
  6310. func GetLastDrugwarehouseInfoByIdTwenty(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  6311. err = XTReadDB().Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  6312. return info, err
  6313. }
  6314. func GetWarhouseOutById(id int64) (models.WarehouseOutInfo, error) {
  6315. info := models.WarehouseOutInfo{}
  6316. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  6317. return info, err
  6318. }
  6319. func FindeGoodWareHouseOutInfo(id int64, storehouse_id int64, orgid int64) (out []*models.WarehouseOutInfo, err error) {
  6320. err = XTReadDB().Where("warehouse_out_id = ? and storehouse_id =? and org_id = ?", id, storehouse_id, orgid).Find(&out).Error
  6321. return out, err
  6322. }
  6323. func ModifyWarehouseInfo(id int64, count int64) (models.WarehousingInfo, error) {
  6324. info := models.WarehousingInfo{}
  6325. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  6326. return info, err
  6327. }
  6328. func GetOrgIdList() (template []*models.VMGobalTemplate, err error) {
  6329. err = XTReadDB().Where("status = 1").Find(&template).Error
  6330. return template, err
  6331. }
  6332. func GetAllGoodList(orgid int64) (good []*models.GoodInfo, err error) {
  6333. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&good).Error
  6334. return good, err
  6335. }
  6336. func GetMobileGoodList(org_id int64) (list []*models.GoodInformationThirty, err error) {
  6337. err = XTReadDB().Where("org_id = ? and status =1", org_id).Find(&list).Error
  6338. return list, err
  6339. }
  6340. func GetAllMobileProjectList(orgid int64) (project []*models.HisProject, err error) {
  6341. err = XTReadDB().Where("user_org_id =? and status =1", orgid).Find(&project).Error
  6342. return project, err
  6343. }
  6344. func ModifyGoodAddInformationSeven(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6345. info := models.GoodInfo{}
  6346. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6347. return info, err
  6348. }
  6349. func GetStockCountWarehouseInfoById(good_id int64) (info []*models.WarehousingInfo, err error) {
  6350. db := XTReadDB().Where("status = 1")
  6351. if good_id > 0 {
  6352. db = db.Where("good_id = ?", good_id)
  6353. }
  6354. err = db.Find(&info).Error
  6355. return info, err
  6356. }
  6357. func GetDrugStockCountWarehoseInfoById(drug_id int64) (info []*models.DrugWarehouseInfo, err error) {
  6358. db := XTReadDB().Model(&info).Where("status = 1")
  6359. if drug_id > 0 {
  6360. db = db.Where("drug_id = ?", drug_id)
  6361. }
  6362. err = db.Find(&info).Error
  6363. return info, err
  6364. }
  6365. func UpdateModifyDrugInfoById(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6366. lib := models.BaseDrugLib{}
  6367. err := XTWriteDB().Model(&lib).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6368. return lib, err
  6369. }
  6370. func GetWarehouseBySecondWarehouseId(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (*models.Warehousing, error) {
  6371. info := models.Warehousing{}
  6372. 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
  6373. if err == gorm.ErrRecordNotFound {
  6374. return nil, err
  6375. }
  6376. if err != nil {
  6377. return nil, err
  6378. }
  6379. return &info, nil
  6380. }
  6381. func GetLastWarehouseById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.Warehousing, error) {
  6382. info := models.Warehousing{}
  6383. 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
  6384. return info, err
  6385. }
  6386. func GetDrugWarehouseBySyS(orgid int64, is_sys int64, sencond_wahouse_id int64, record_date int64) (*models.DrugWarehouseOut, error) {
  6387. out := models.DrugWarehouseOut{}
  6388. 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
  6389. if err == gorm.ErrRecordNotFound {
  6390. return nil, err
  6391. }
  6392. if err != nil {
  6393. return nil, err
  6394. }
  6395. return &out, nil
  6396. }
  6397. func GetLastDrugWarehouseOutById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.DrugWarehouseOut, error) {
  6398. info := models.DrugWarehouseOut{}
  6399. 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
  6400. return info, err
  6401. }
  6402. func GetDrugWarehouseingBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (*models.DrugWarehouse, error) {
  6403. info := models.DrugWarehouse{}
  6404. 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
  6405. if err == gorm.ErrRecordNotFound {
  6406. return nil, err
  6407. }
  6408. if err != nil {
  6409. return nil, err
  6410. }
  6411. return &info, nil
  6412. }
  6413. func CreateDrugWaresing(warehouse models.DrugWarehouse) error {
  6414. err := XTWriteDB().Create(&warehouse).Error
  6415. return err
  6416. }
  6417. func GetLastDrugWarehouseBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (models.DrugWarehouse, error) {
  6418. info := models.DrugWarehouse{}
  6419. 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
  6420. return info, err
  6421. }
  6422. func ModifyGoodReduceInformationTwenty(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6423. info := models.GoodInfo{}
  6424. 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
  6425. 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
  6426. return info, err
  6427. }
  6428. func ModifyGoodReduceInformationTwentyOne(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6429. info := models.GoodInfo{}
  6430. 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
  6431. 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
  6432. return info, err
  6433. }
  6434. func UpdateMedicalSumCount(drug_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6435. tx := XTWriteDB().Begin()
  6436. 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
  6437. if err != nil {
  6438. tx.Rollback()
  6439. return err
  6440. }
  6441. tx.Commit()
  6442. return err
  6443. }
  6444. func UpdateNewMedicalSumCount(drug_id int64, sum_count int64, sum_in_count int64, orgid int64, tx *gorm.DB) error {
  6445. 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
  6446. if err != nil {
  6447. tx.Rollback()
  6448. return err
  6449. }
  6450. return err
  6451. }
  6452. func UpdateMedicalSumCountOne(drug_id int64, sum_count int64, orgid int64) error {
  6453. tx := XTWriteDB().Begin()
  6454. err := tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6455. if err != nil {
  6456. tx.Rollback()
  6457. return err
  6458. }
  6459. tx.Commit()
  6460. return err
  6461. }
  6462. //func UpdateMedicalSumCountTX(drug_id int64, sum_count int64, sum_in_count int64, orgid int64,tx *gorm.DB) error {
  6463. // 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
  6464. // return err
  6465. //}
  6466. func UpdateGoodByGoodId(good_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6467. 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
  6468. return err
  6469. }
  6470. func GetDrugDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  6471. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  6472. table := XTReadDB().Table("xt_base_drug as x d")
  6473. fmt.Println(table)
  6474. if startime > 0 {
  6475. db = db.Where("x.advice_date >= ?", startime)
  6476. }
  6477. if endtime > 0 {
  6478. db = db.Where("x.advice_date <= ?", endtime)
  6479. }
  6480. if orgid > 0 {
  6481. db = db.Where("x.user_org_id = ?", orgid)
  6482. }
  6483. if len(keyword) > 0 {
  6484. likeKey := "%" + keyword + "%"
  6485. 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)
  6486. }
  6487. offset := (page - 1) * limit
  6488. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.created_time desc").Group("x.drug_id").Scan(&adviceinfo).Error
  6489. return adviceinfo, total, err
  6490. }
  6491. func GetDrugDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  6492. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  6493. table := XTReadDB().Table("xt_base_drug as x d")
  6494. fmt.Println(table)
  6495. if startime > 0 {
  6496. db = db.Where("x.advice_date >= ?", startime)
  6497. }
  6498. if endtime > 0 {
  6499. db = db.Where("x.advice_date <= ?", endtime)
  6500. }
  6501. if orgid > 0 {
  6502. db = db.Where("x.user_org_id = ?", orgid)
  6503. }
  6504. if len(keyword) > 0 {
  6505. likeKey := "%" + keyword + "%"
  6506. 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)
  6507. }
  6508. err = db.Order("x.created_time desc").Scan(&adviceinfo).Error
  6509. return adviceinfo, total, err
  6510. }
  6511. func GetAllDrugSummary(orgid int64) (drug []*models.BaseDrugLib, err error) {
  6512. err = XTReadDB().Where("org_id = ? and status = 1 and is_user = 1", orgid).Find(&drug).Error
  6513. return drug, err
  6514. }
  6515. func GetGoodDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  6516. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  6517. table := XTReadDB().Table("xt_good_information as d")
  6518. fmt.Println(table)
  6519. if startime > 0 {
  6520. db = db.Where("x.record_date >= ?", startime)
  6521. }
  6522. if endtime > 0 {
  6523. db = db.Where("x.record_date <= ?", endtime)
  6524. }
  6525. if orgid > 0 {
  6526. db = db.Where("x.user_org_id = ?", orgid)
  6527. }
  6528. if len(keyword) > 0 {
  6529. likeKey := "%" + keyword + "%"
  6530. 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")
  6531. }
  6532. offset := (page - 1) * limit
  6533. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Group("x.project_id").Scan(&project).Error
  6534. return project, total, err
  6535. }
  6536. func GetGoodDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  6537. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  6538. table := XTReadDB().Table("xt_good_information as d")
  6539. fmt.Println(table)
  6540. if startime > 0 {
  6541. db = db.Where("x.record_date >= ?", startime)
  6542. }
  6543. if endtime > 0 {
  6544. db = db.Where("x.record_date <= ?", endtime)
  6545. }
  6546. if orgid > 0 {
  6547. db = db.Where("x.user_org_id = ?", orgid)
  6548. }
  6549. if len(keyword) > 0 {
  6550. likeKey := "%" + keyword + "%"
  6551. 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")
  6552. }
  6553. err = db.Order("x.ctime desc").Group("x.id").Scan(&project).Error
  6554. return project, total, err
  6555. }
  6556. func GeAllGoodInformation(orgid int64) (good []*models.GoodInfo, err error) {
  6557. err = XTReadDB().Where("org_id =? and status = 1 and is_user = 1", orgid).Find(&good).Error
  6558. return good, err
  6559. }
  6560. func GetStockSettingIsExsit(orgid int64) (*models.XtStockSetting, error) {
  6561. setting := models.XtStockSetting{}
  6562. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6563. if err == gorm.ErrRecordNotFound {
  6564. return nil, err
  6565. }
  6566. if err != nil {
  6567. return nil, err
  6568. }
  6569. return &setting, nil
  6570. }
  6571. func CreateStockSetting(setting models.XtStockSetting) error {
  6572. err := XTWriteDB().Create(&setting).Error
  6573. return err
  6574. }
  6575. func UpdateStockSetting(orgid int64, setting models.XtStockSetting) error {
  6576. err := XTWriteDB().Model(&models.XtStockSetting{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_type": setting.IsType}).Error
  6577. return err
  6578. }
  6579. func FindStockSettingById(orgid int64) (models.XtStockSetting, error) {
  6580. setting := models.XtStockSetting{}
  6581. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6582. return setting, err
  6583. }
  6584. func GetAutoStockOutById(good_id int64, record_date int64, orgid int64, patient_id int64) (models.BloodAutomaticReduceDetail, error) {
  6585. detail := models.BloodAutomaticReduceDetail{}
  6586. 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
  6587. return detail, err
  6588. }
  6589. func GetAutoWarehouseOutList(good_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfoNight, error) {
  6590. detail := models.WarehouseOutInfoNight{}
  6591. 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
  6592. return detail, err
  6593. }
  6594. func GetAdviceConfig(orgid int64) (*models.XtAdviceSetting, error) {
  6595. setting := models.XtAdviceSetting{}
  6596. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6597. if err == gorm.ErrRecordNotFound {
  6598. return nil, err
  6599. }
  6600. if err != nil {
  6601. return nil, err
  6602. }
  6603. return &setting, nil
  6604. }
  6605. func CreateAdviceSetting(setting models.XtAdviceSetting) error {
  6606. err := XTWriteDB().Create(&setting).Error
  6607. return err
  6608. }
  6609. func UpdateAdviceSetting(orgid int64, setting models.XtAdviceSetting) error {
  6610. 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
  6611. return err
  6612. }
  6613. func FindAdviceSettingById(orgid int64) (models.XtAdviceSetting, error) {
  6614. setting := models.XtAdviceSetting{}
  6615. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6616. return setting, err
  6617. }
  6618. func GetPrescriptionConfig(orgid int64) (*models.XtPrescriptionConfig, error) {
  6619. config := models.XtPrescriptionConfig{}
  6620. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6621. if err == gorm.ErrRecordNotFound {
  6622. return nil, err
  6623. }
  6624. if err != nil {
  6625. return nil, err
  6626. }
  6627. return &config, nil
  6628. }
  6629. func CreatePrescriptionConfig(prescriptionConfig models.XtPrescriptionConfig) error {
  6630. err := XTWriteDB().Create(&prescriptionConfig).Error
  6631. return err
  6632. }
  6633. func UpdatePrescriptionConfig(orgid int64, config models.XtPrescriptionConfig) error {
  6634. 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
  6635. return err
  6636. }
  6637. func FindPrescriptionConfigById(orgid int64) (models.XtPrescriptionConfig, error) {
  6638. config := models.XtPrescriptionConfig{}
  6639. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6640. return config, err
  6641. }
  6642. func UpdateWarehouseInfoTwentyFive(maxNumber int64, drugid int64, orgID int64, storehouse_id int64, drup *models.XtBaseDrug) (err error) {
  6643. var deliver_number int64 = 0
  6644. var stock_number int64 = 0
  6645. deliver_number = maxNumber
  6646. // 根据先进先出原则,查询最先入库的批次,进行出库
  6647. // 如果没有对应的库存,则报错
  6648. //开启事物
  6649. warehouse, err := FindLastDrugWarehousingInfoByID(drugid, storehouse_id)
  6650. if err != nil {
  6651. return err
  6652. }
  6653. // 将该批次的剩余库存数量转换为拆零数量
  6654. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  6655. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  6656. if stock_number >= deliver_number {
  6657. var maxNumber int64 = 0
  6658. var minNumber int64 = 0
  6659. var stock_max_number int64 = 0
  6660. stock_max_number = warehouse.StockMaxNumber
  6661. maxNumber = deliver_number / drup.MinNumber
  6662. minNumber = deliver_number % drup.MinNumber
  6663. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  6664. minNumber = maxNumber
  6665. }
  6666. if drup.MaxUnit != drup.MinUnit {
  6667. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  6668. return errors.New("库存数量不足")
  6669. }
  6670. }
  6671. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  6672. if warehouse.StockMaxNumber < 0 {
  6673. warehouse.StockMaxNumber = 0
  6674. }
  6675. if warehouse.StockMinNumber < 0 {
  6676. warehouse.StockMinNumber = 0
  6677. }
  6678. warehouse.Mtime = time.Now().Unix()
  6679. if warehouse.StockMinNumber < minNumber {
  6680. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  6681. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  6682. } else {
  6683. if minNumber > 0 {
  6684. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  6685. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6686. } else {
  6687. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  6688. }
  6689. }
  6690. if minNumber == 0 && maxNumber != 1 {
  6691. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6692. }
  6693. }
  6694. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  6695. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6696. if warehouse.StockMinNumber == 0 {
  6697. warehouse.StockMaxNumber = stock_max_number
  6698. }
  6699. }
  6700. if drup.MaxUnit != drup.MinUnit {
  6701. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  6702. return errors.New("库存数量不足")
  6703. }
  6704. }
  6705. if warehouse.StockMinNumber <= 0 {
  6706. warehouse.StockMinNumber = 0
  6707. }
  6708. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6709. fmt.Println(errThree)
  6710. //查询默认仓库
  6711. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6712. //查询默认仓库剩余多少库存
  6713. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6714. var sum_count int64
  6715. var sum_in_count int64
  6716. for _, it := range list {
  6717. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6718. if it.MaxUnit == baseDrug.MaxUnit {
  6719. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6720. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6721. sum_count += it.StockMaxNumber + it.StockMinNumber
  6722. sum_in_count += it.WarehousingCount
  6723. }
  6724. }
  6725. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6726. return nil
  6727. } else {
  6728. warehouse.StockMaxNumber = 0
  6729. warehouse.StockMinNumber = 0
  6730. warehouse.Mtime = time.Now().Unix()
  6731. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6732. fmt.Println(errThree)
  6733. //查询默认仓库
  6734. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6735. //查询默认仓库剩余多少库存
  6736. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6737. var sum_count int64
  6738. var sum_in_count int64
  6739. for _, it := range list {
  6740. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6741. if it.MaxUnit == baseDrug.MaxUnit {
  6742. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6743. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6744. sum_count += it.StockMaxNumber + it.StockMinNumber
  6745. sum_in_count += it.WarehousingCount
  6746. }
  6747. }
  6748. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6749. if errThree != nil {
  6750. return errThree
  6751. }
  6752. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  6753. prescribingNumber_two_temp := deliver_number - stock_number
  6754. UpdateWarehouseInfoTwentyFive(prescribingNumber_two_temp, drugid, orgID, storehouse_id, drup)
  6755. }
  6756. return
  6757. }
  6758. func UpdateWarehouseInfoTentyFour(count int64, id int64) error {
  6759. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6760. return err
  6761. }
  6762. func UpdateWarehouseInfoTentyThree(count int64, id int64) error {
  6763. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6764. return err
  6765. }
  6766. func GetDrugOpenConfig(orgid int64) (*models.DrugOutConfig, error) {
  6767. config := models.DrugOutConfig{}
  6768. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6769. if err == gorm.ErrRecordNotFound {
  6770. return nil, err
  6771. }
  6772. if err != nil {
  6773. return nil, err
  6774. }
  6775. return &config, nil
  6776. }
  6777. func GetDrugOpenConfigOne(orgid int64) (models.DrugOutConfig, error) {
  6778. config := models.DrugOutConfig{}
  6779. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).First(&config).Error
  6780. return config, err
  6781. }
  6782. func FindXTHisRecordByOrgIdSix(org_id int64) (models.XtHisConfig, error) {
  6783. config := models.XtHisConfig{}
  6784. err = readDb.Model(&models.XtHisConfig{}).Where("status = 1 AND user_org_id = ?", org_id).Find(&config).Error
  6785. return config, err
  6786. }
  6787. func CreateDrugOutConfig(config models.DrugOutConfig) error {
  6788. err := XTWriteDB().Create(&config).Error
  6789. return err
  6790. }
  6791. func UpdateDrugOutConfig(orgid int64, drug_out_open int64) error {
  6792. err := XTWriteDB().Model(&models.DrugOutConfig{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": drug_out_open}).Error
  6793. return err
  6794. }
  6795. func FindDrugOutConfigById(orgid int64) (models.DrugOutConfig, error) {
  6796. config := models.DrugOutConfig{}
  6797. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6798. return config, err
  6799. }
  6800. func FindeDrugSettleConfigById(orgid int64) (models.HisSettleStockConfig, error) {
  6801. config := models.HisSettleStockConfig{}
  6802. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6803. return config, err
  6804. }
  6805. func UpdatedWarehouseOut(id int64) error {
  6806. err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6807. err = XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6808. return err
  6809. }
  6810. func UpdatedNewWarehouseOut(id int64, tx *gorm.DB) error {
  6811. err := tx.Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6812. if err != nil {
  6813. tx.Rollback()
  6814. return err
  6815. }
  6816. err = tx.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6817. if err != nil {
  6818. tx.Rollback()
  6819. return err
  6820. }
  6821. return err
  6822. }
  6823. func UpdatedSigleWarehouseOutInfo(id int64, info *models.WarehouseOutInfo) error {
  6824. tx := XTWriteDB().Begin()
  6825. err := tx.Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_info_id": info.WarehouseInfotId}).Error
  6826. if err != nil {
  6827. tx.Rollback()
  6828. return err
  6829. }
  6830. tx.Commit()
  6831. return err
  6832. }
  6833. func UpdatedNewSigleWarehouseOutInfo(id int64, info *models.WarehouseOutInfo, tx *gorm.DB) error {
  6834. err := tx.Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_info_id": info.WarehouseInfotId}).Error
  6835. if err != nil {
  6836. tx.Rollback()
  6837. return err
  6838. }
  6839. return err
  6840. }
  6841. func GetLastGoodWarehouseOutInfo(orgid int64, id int64, tx *gorm.DB) (info []*models.WarehouseOutInfo, err error) {
  6842. err = tx.Where("org_id = ? and status = 1 and warehouse_out_id = ?", orgid, id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  6843. return tx.Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  6844. }).Find(&info).Error
  6845. if err != nil {
  6846. tx.Rollback()
  6847. return info, err
  6848. }
  6849. return info, err
  6850. }
  6851. func GetLastGoodWarehouseOutInfoById(orgid int64) (models.WarehouseOutInfo, error) {
  6852. info := models.WarehouseOutInfo{}
  6853. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&info).Error
  6854. return info, err
  6855. }
  6856. func ModifyWarehouseOutInfo(id int64, info *models.WarehouseOutInfo, tx *gorm.DB) error {
  6857. err := tx.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
  6858. if err != nil {
  6859. tx.Rollback()
  6860. return err
  6861. }
  6862. return err
  6863. }
  6864. func UpdateWarehouseOutFlow(info models.VmStockFlow, id int64) error {
  6865. 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
  6866. return err
  6867. }
  6868. func GetLastGoodWarehouseOutTwenty(orgid int64) (models.WarehouseOut, error) {
  6869. out := models.WarehouseOut{}
  6870. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  6871. return out, err
  6872. }
  6873. func GetLastGoodWarehouseOutTwentyOne(orgid int64, tx *gorm.DB) (models.WarehouseOut, error) {
  6874. out := models.WarehouseOut{}
  6875. err := tx.Where("org_id = ? and status = 1", orgid).Last(&out).Error
  6876. if err != gorm.ErrRecordNotFound {
  6877. if err != nil {
  6878. tx.Rollback()
  6879. return out, err
  6880. }
  6881. }
  6882. return out, err
  6883. }
  6884. func UpdateaGoodWarehouseInfo(count int64, good_id int64, orgid int64, warehouse_info_id int64) error {
  6885. ut := writeDb.Begin()
  6886. 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
  6887. if err != nil {
  6888. ut.Rollback()
  6889. return err
  6890. }
  6891. ut.Commit()
  6892. return err
  6893. }
  6894. func UpdateaNewGoodWarehouseInfo(count int64, good_id int64, orgid int64, warehouse_info_id int64, tx *gorm.DB) error {
  6895. err := tx.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
  6896. if err != nil {
  6897. tx.Rollback()
  6898. return err
  6899. }
  6900. return err
  6901. }
  6902. func ModifyGoodWarehouseOut(id int64) error {
  6903. ut := writeDb.Begin()
  6904. err := ut.Model(&models.WarehouseOut{}).Where("id = ? and status =1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6905. if err != nil {
  6906. ut.Rollback()
  6907. return err
  6908. }
  6909. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6910. if err != nil {
  6911. ut.Rollback()
  6912. return err
  6913. }
  6914. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  6915. if err != nil {
  6916. ut.Rollback()
  6917. return err
  6918. }
  6919. err = ut.Model(&models.XtWarehouseFlushInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  6920. if err != nil {
  6921. ut.Rollback()
  6922. return err
  6923. }
  6924. ut.Commit()
  6925. return err
  6926. }
  6927. func ModifyNewGoodWarehouseOut(id int64, tx *gorm.DB) error {
  6928. err := tx.Model(&models.WarehouseOut{}).Where("id = ? and status =1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6929. if err != nil {
  6930. tx.Rollback()
  6931. return err
  6932. }
  6933. err = tx.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6934. if err != nil {
  6935. tx.Rollback()
  6936. return err
  6937. }
  6938. err = tx.Model(&models.VmStockFlow{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  6939. if err != nil {
  6940. tx.Rollback()
  6941. return err
  6942. }
  6943. err = tx.Model(&models.XtWarehouseFlushInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  6944. if err != nil {
  6945. tx.Rollback()
  6946. return err
  6947. }
  6948. return err
  6949. }
  6950. func GetGoodWarehouseInfoById(waresing_id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  6951. err = XTReadDB().Where("org_id = ? and status = 1 and warehousing_id = ?", orgid, waresing_id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  6952. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  6953. }).Find(&info).Error
  6954. return info, err
  6955. }
  6956. func GetNewGoodWarehouseInfoById(waresing_id int64, orgid int64, tx *gorm.DB) (info []*models.VmWarehousingInfoSix, err error) {
  6957. err = tx.Where("org_id = ? and status = 1 and warehousing_id = ?", orgid, waresing_id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  6958. return tx.Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  6959. }).Find(&info).Error
  6960. if err != gorm.ErrRecordNotFound {
  6961. if err != nil {
  6962. tx.Rollback()
  6963. return info, err
  6964. }
  6965. }
  6966. return info, err
  6967. }
  6968. func FindLastWarehousing(order string) (info models.Warehousing, err error) {
  6969. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  6970. return info, err
  6971. }
  6972. func UpdateWarehouseDetail(info *models.WarehousingInfo, id int64) error {
  6973. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"good_id": info.GoodId, "good_type_id": info.GoodTypeId, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "warehousing_count": info.WarehousingCount, "warehousing_unit": info.WarehousingUnit, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "license_number": info.LicenseNumber, "packing_price": info.PackingPrice, "register_number": info.RegisterNumber, "storehouse_id": info.StorehouseId, "good_code": info.GoodCode}).Error
  6974. return err
  6975. }
  6976. func UpdateNewWarehouseDetail(info *models.WarehousingInfo, id int64, tx *gorm.DB) error {
  6977. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"good_id": info.GoodId, "good_type_id": info.GoodTypeId, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "warehousing_count": info.WarehousingCount, "warehousing_unit": info.WarehousingUnit, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "license_number": info.LicenseNumber, "packing_price": info.PackingPrice, "register_number": info.RegisterNumber, "storehouse_id": info.StorehouseId, "good_code": info.GoodCode}).Error
  6978. if err != nil {
  6979. tx.Rollback()
  6980. return err
  6981. }
  6982. return err
  6983. }
  6984. func UpdateCheckWarehouseInfo(id int64, checker int64) error {
  6985. tx := XTWriteDB().Begin()
  6986. err := tx.Model(&models.Warehousing{}).Where("id =? and status = 1", id).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  6987. if err != nil {
  6988. tx.Rollback()
  6989. return err
  6990. }
  6991. tx.Commit()
  6992. return err
  6993. }
  6994. func UpdateNewCheckWarehouseInfo(id int64, checker int64, tx *gorm.DB) error {
  6995. err := tx.Model(&models.Warehousing{}).Where("id =? and status = 1", id).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  6996. if err != nil {
  6997. tx.Rollback()
  6998. return err
  6999. }
  7000. return err
  7001. }
  7002. func GetWarehouseInfoList(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  7003. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7004. return info, err
  7005. }
  7006. func GetNewWarehouseInfoList(id int64, orgid int64, tx *gorm.DB) (info []*models.WarehousingInfo, err error) {
  7007. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7008. if err != gorm.ErrRecordNotFound {
  7009. if err != nil {
  7010. tx.Rollback()
  7011. return info, err
  7012. }
  7013. }
  7014. return info, err
  7015. }
  7016. func UpdateWarehouseInfoByIdListThree(count int64, id int64) error {
  7017. tx := XTWriteDB().Begin()
  7018. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"stock_count": count}).Error
  7019. if err != nil {
  7020. tx.Rollback()
  7021. return err
  7022. }
  7023. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7024. if err != nil {
  7025. tx.Rollback()
  7026. return err
  7027. }
  7028. tx.Commit()
  7029. return err
  7030. }
  7031. func UpdateNewWarehouseInfoByIdListThree(count int64, id int64, tx *gorm.DB) error {
  7032. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"stock_count": count}).Error
  7033. if err != nil {
  7034. tx.Rollback()
  7035. return err
  7036. }
  7037. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7038. if err != nil {
  7039. tx.Rollback()
  7040. return err
  7041. }
  7042. return err
  7043. }
  7044. func FindLastWarehousingSix(org_id int64) (info models.Warehousing, err error) {
  7045. err = readDb.Model(&models.Warehousing{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7046. return info, err
  7047. }
  7048. func FindNewLastWarehousingSix(org_id int64, tx *gorm.DB) (info models.Warehousing, err error) {
  7049. err = tx.Model(&models.Warehousing{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7050. if err != gorm.ErrRecordNotFound {
  7051. if err != nil {
  7052. tx.Rollback()
  7053. return info, err
  7054. }
  7055. }
  7056. return info, err
  7057. }
  7058. func FindLastWarehousingSeven(org_id int64) (info models.DrugWarehouse, err error) {
  7059. err = readDb.Model(&models.DrugWarehouse{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7060. return info, err
  7061. }
  7062. func FindNewLastWarehousingSeven(org_id int64, tx *gorm.DB) (info models.DrugWarehouse, err error) {
  7063. err = tx.Model(&models.DrugWarehouse{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7064. if err != gorm.ErrRecordNotFound {
  7065. if err != nil {
  7066. tx.Rollback()
  7067. return info, err
  7068. }
  7069. }
  7070. return info, err
  7071. }
  7072. func FindDrugWarehouseInfoListById(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7073. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "status = 1 and org_id =?", orgid).Find(&info).Error
  7074. return info, err
  7075. }
  7076. func FindNewDrugWarehouseInfoListById(id int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7077. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "status = 1 and org_id =?", orgid).Find(&info).Error
  7078. if err != gorm.ErrRecordNotFound {
  7079. if err != nil {
  7080. tx.Rollback()
  7081. return info, err
  7082. }
  7083. }
  7084. return info, err
  7085. }
  7086. func CreatedWarehouseing(info *models.DrugWarehouseInfo) error {
  7087. err := XTWriteDB().Create(&info).Error
  7088. return err
  7089. }
  7090. func CreatedNewWarehouseing(info *models.DrugWarehouseInfo, tx *gorm.DB) error {
  7091. err := tx.Create(&info).Error
  7092. if err != nil {
  7093. tx.Rollback()
  7094. return err
  7095. }
  7096. return err
  7097. }
  7098. func UpdateWarehouseing(info *models.DrugWarehouseInfo) error {
  7099. err := XTWriteDB().Save(&info).Error
  7100. return err
  7101. }
  7102. func UpdateNewWarehouseing(info *models.DrugWarehouseInfo, tx *gorm.DB) error {
  7103. err := tx.Save(&info).Error
  7104. if err != nil {
  7105. tx.Rollback()
  7106. return err
  7107. }
  7108. return err
  7109. }
  7110. func CheckWarehousingInfo(id int64, orgid int64, checker int64) error {
  7111. tx := XTWriteDB().Begin()
  7112. err := tx.Model(&models.DrugWarehouse{}).Where("id = ? and org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  7113. if err != nil {
  7114. tx.Rollback()
  7115. return err
  7116. }
  7117. tx.Commit()
  7118. return err
  7119. }
  7120. func CheckNewWarehousingInfo(id int64, orgid int64, checker int64, tx *gorm.DB) error {
  7121. err := tx.Model(&models.DrugWarehouse{}).Where("id = ? and org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  7122. if err != nil {
  7123. tx.Rollback()
  7124. return err
  7125. }
  7126. return err
  7127. }
  7128. func GetWarehousingInfoByList(id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  7129. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7130. return info, err
  7131. }
  7132. func GetNewWarehousingInfoByList(id int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
  7133. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7134. if err != gorm.ErrRecordNotFound {
  7135. if err != nil {
  7136. tx.Rollback()
  7137. return info, err
  7138. }
  7139. }
  7140. return info, err
  7141. }
  7142. func AddDrugWarehouseStockMaxNumber(count int64, id int64) error {
  7143. tx := XTWriteDB().Begin()
  7144. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7145. if err != nil {
  7146. tx.Rollback()
  7147. return err
  7148. }
  7149. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7150. if err != nil {
  7151. tx.Rollback()
  7152. return err
  7153. }
  7154. tx.Commit()
  7155. return err
  7156. }
  7157. func AddNewDrugWarehouseStockMaxNumber(count int64, id int64, tx *gorm.DB) error {
  7158. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7159. if err != nil {
  7160. tx.Rollback()
  7161. return err
  7162. }
  7163. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7164. if err != nil {
  7165. tx.Rollback()
  7166. return err
  7167. }
  7168. return err
  7169. }
  7170. func UpdateWarehousingInfoFlow(id int64, orgid int64) error {
  7171. tx := XTWriteDB().Begin()
  7172. 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
  7173. if err != nil {
  7174. tx.Rollback()
  7175. return err
  7176. }
  7177. tx.Commit()
  7178. return err
  7179. }
  7180. func UpdateNewWarehousingInfoFlow(id int64, orgid int64, tx *gorm.DB) error {
  7181. 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
  7182. if err != nil {
  7183. tx.Rollback()
  7184. return err
  7185. }
  7186. return err
  7187. }
  7188. func AddDrugWarehouseStockMinNumber(count int64, id int64) error {
  7189. tx := XTWriteDB().Begin()
  7190. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7191. if err != nil {
  7192. tx.Rollback()
  7193. return err
  7194. }
  7195. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7196. tx.Commit()
  7197. return err
  7198. }
  7199. func AddNewDrugWarehouseStockMinNumber(count int64, id int64, tx *gorm.DB) error {
  7200. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7201. if err != nil {
  7202. tx.Rollback()
  7203. return err
  7204. }
  7205. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7206. if err != nil {
  7207. tx.Rollback()
  7208. return err
  7209. }
  7210. return err
  7211. }
  7212. func ReturnCheckWarehouseingInfo(id int64, orgid int64) error {
  7213. tx := XTWriteDB().Begin()
  7214. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7215. if err != nil {
  7216. tx.Rollback()
  7217. return err
  7218. }
  7219. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("warehousing_id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7220. if err != nil {
  7221. tx.Rollback()
  7222. return err
  7223. }
  7224. tx.Commit()
  7225. return err
  7226. }
  7227. func ReturnNewCheckWarehouseingInfo(id int64, orgid int64, tx *gorm.DB) error {
  7228. err := tx.Model(&models.DrugWarehouse{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7229. if err != nil {
  7230. tx.Rollback()
  7231. return err
  7232. }
  7233. err = tx.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7234. if err != nil {
  7235. tx.Rollback()
  7236. return err
  7237. }
  7238. return err
  7239. }
  7240. func AddDrugReturnWarehouseStockMaxNumber(count int64, id int64) error {
  7241. tx := XTWriteDB().Begin()
  7242. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  7243. if err != nil {
  7244. tx.Rollback()
  7245. return err
  7246. }
  7247. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7248. if err != nil {
  7249. tx.Rollback()
  7250. return err
  7251. }
  7252. tx.Commit()
  7253. return err
  7254. }
  7255. func AddNewDrugReturnWarehouseStockMaxNumber(count int64, id int64, tx *gorm.DB) error {
  7256. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  7257. if err != nil {
  7258. tx.Rollback()
  7259. return err
  7260. }
  7261. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7262. if err != nil {
  7263. tx.Rollback()
  7264. return err
  7265. }
  7266. return err
  7267. }
  7268. func AddDrugReturnWarehouseStockMinNumber(count int64, id int64) error {
  7269. tx := XTWriteDB().Begin()
  7270. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  7271. if err != nil {
  7272. tx.Rollback()
  7273. return err
  7274. }
  7275. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7276. if err != nil {
  7277. tx.Rollback()
  7278. return err
  7279. }
  7280. tx.Commit()
  7281. return err
  7282. }
  7283. func AddNewDrugReturnWarehouseStockMinNumber(count int64, id int64, tx *gorm.DB) error {
  7284. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  7285. if err != nil {
  7286. tx.Rollback()
  7287. return err
  7288. }
  7289. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7290. if err != nil {
  7291. tx.Rollback()
  7292. return err
  7293. }
  7294. return err
  7295. }
  7296. func UpdateDrugWarehouseInfoTwenty(id int64, info *models.DrugWarehouseInfo) error {
  7297. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "warehousing_count": info.WarehousingCount, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "retail_price": info.RetailPrice, "max_unit": info.MaxUnit, "min_unit": info.MinUnit, "drug_code": info.DrugCode, "batch_number": info.BatchNumber}).Error
  7298. return err
  7299. }
  7300. func UpdateNewDrugWarehouseInfoTwenty(id int64, info *models.DrugWarehouseInfo, tx *gorm.DB) error {
  7301. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "warehousing_count": info.WarehousingCount, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "retail_price": info.RetailPrice, "max_unit": info.MaxUnit, "min_unit": info.MinUnit, "drug_code": info.DrugCode, "batch_number": info.BatchNumber}).Error
  7302. if err != nil {
  7303. tx.Rollback()
  7304. return err
  7305. }
  7306. return err
  7307. }
  7308. func UpdatedDrugWarehouseInfo(info *models.DrugWarehouseOutInfo, id int64) error {
  7309. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "number": info.Number, "batch_number": info.BatchNumber, "admin_user_id": info.AdminUserId, "last_price": info.LastPrice, "stock_count": info.StockCount, "storehouse_id": info.StorehouseId, "patient_id": info.PatientId, "class_type": info.ClassType}).Error
  7310. return err
  7311. }
  7312. func UpdatedNewDrugWarehouseInfo(info *models.DrugWarehouseOutInfo, id int64, tx *gorm.DB) error {
  7313. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "number": info.Number, "batch_number": info.BatchNumber, "admin_user_id": info.AdminUserId, "last_price": info.LastPrice, "stock_count": info.StockCount, "storehouse_id": info.StorehouseId, "patient_id": info.PatientId, "class_type": info.ClassType}).Error
  7314. if err != nil {
  7315. tx.Rollback()
  7316. return err
  7317. }
  7318. return err
  7319. }
  7320. func UpdatedSelfDrugWarehouseInfo(info *models.XtSelfDrugWarehouseOutInfo, id int64, tx *gorm.DB) error {
  7321. err := tx.Model(&models.XtSelfDrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "number": info.Number, "batch_number": info.BatchNumber, "admin_user_id": info.AdminUserId, "last_price": info.LastPrice, "stock_count": info.StockCount, "storehouse_id": info.StorehouseId, "patient_id": info.PatientId, "class_type": info.ClassType}).Error
  7322. if err != gorm.ErrRecordNotFound {
  7323. return err
  7324. }
  7325. return err
  7326. }
  7327. func GetDrugLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  7328. out := models.DrugWarehouseOut{}
  7329. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  7330. return out, err
  7331. }
  7332. func GetNewDrugLastWarehouseOut(orgid int64, tx *gorm.DB) (models.DrugWarehouseOut, error) {
  7333. out := models.DrugWarehouseOut{}
  7334. err := tx.Where("org_id = ? and status = 1", orgid).Find(&out).Error
  7335. if err != gorm.ErrRecordNotFound {
  7336. if err != nil {
  7337. tx.Rollback()
  7338. return out, err
  7339. }
  7340. }
  7341. return out, err
  7342. }
  7343. func GetSelfDrugLastWarehouseOut(orgid int64, tx *gorm.DB) (models.XtSelfDrugWarehouseOut, error) {
  7344. out := models.XtSelfDrugWarehouseOut{}
  7345. err := tx.Where("org_id = ? and status = 1", orgid).Find(&out).Error
  7346. if err != gorm.ErrRecordNotFound {
  7347. if err != nil {
  7348. tx.Rollback()
  7349. return out, err
  7350. }
  7351. }
  7352. return out, err
  7353. }
  7354. func GetSelfDrugLastWarehouseOutOne(id int64, tx *gorm.DB) (models.XtSelfDrugWarehouseOut, error) {
  7355. out := models.XtSelfDrugWarehouseOut{}
  7356. err := tx.Where("id = ? and status = 1", id).Find(&out).Error
  7357. if err != gorm.ErrRecordNotFound {
  7358. if err != nil {
  7359. tx.Rollback()
  7360. return out, err
  7361. }
  7362. }
  7363. return out, err
  7364. }
  7365. func GetDrugWarehouseOutListById(warehouse_out_id int64, orgId int64) (info []*models.DrugWarehouseOutInfo, err error) {
  7366. err = XTReadDB().Where("warehouse_out_id = ? and org_id =? and status = 1", warehouse_out_id, orgId).Preload("BaseDrugLib", "status=1").Find(&info).Error
  7367. return info, err
  7368. }
  7369. func GetNewDrugWarehouseOutListById(warehouse_out_id int64, orgId int64, tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  7370. err = tx.Where("warehouse_out_id = ? and org_id =? and status = 1", warehouse_out_id, orgId).Preload("BaseDrugLib", "status=1").Find(&info).Error
  7371. if err != gorm.ErrRecordNotFound {
  7372. if err != nil {
  7373. tx.Rollback()
  7374. return info, err
  7375. }
  7376. }
  7377. return info, err
  7378. }
  7379. func UpdateCheckDrugOut(out models.DrugWarehouseOut, id int64) error {
  7380. tx := XTWriteDB().Begin()
  7381. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": out.IsCheck}).Error
  7382. if err != nil {
  7383. tx.Rollback()
  7384. return err
  7385. }
  7386. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7387. if err != nil {
  7388. tx.Rollback()
  7389. return err
  7390. }
  7391. tx.Commit()
  7392. return err
  7393. }
  7394. func UpdateNewCheckDrugOut(out models.DrugWarehouseOut, id int64, tx *gorm.DB) error {
  7395. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": out.IsCheck}).Error
  7396. if err != nil {
  7397. tx.Rollback()
  7398. return err
  7399. }
  7400. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7401. if err != nil {
  7402. tx.Rollback()
  7403. return err
  7404. }
  7405. return err
  7406. }
  7407. func UpdateCheckDrugOutInfoList(info models.DrugWarehouseOutInfo, warehouse_out_id int64) error {
  7408. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", warehouse_out_id).Update(map[string]interface{}{"is_check": info.IsCheck}).Error
  7409. return err
  7410. }
  7411. func UpdateNewCheckDrugOutInfoList(info models.DrugWarehouseOutInfo, warehouse_out_id int64, tx *gorm.DB) error {
  7412. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", warehouse_out_id).Update(map[string]interface{}{"is_check": info.IsCheck}).Error
  7413. if err != nil {
  7414. tx.Rollback()
  7415. return err
  7416. }
  7417. return err
  7418. }
  7419. func GetDrugWarehouseOutByLastId(id int64) (models.DrugWarehouseOut, error) {
  7420. out := models.DrugWarehouseOut{}
  7421. err := XTReadDB().Where("id = ? and status = 1", id).Find(&out).Error
  7422. return out, err
  7423. }
  7424. func GetNewDrugWarehouseOutByLastId(id int64, tx *gorm.DB) (models.DrugWarehouseOut, error) {
  7425. out := models.DrugWarehouseOut{}
  7426. err := tx.Where("id = ? and status = 1", id).Find(&out).Error
  7427. if err != gorm.ErrRecordNotFound {
  7428. if err != nil {
  7429. tx.Rollback()
  7430. return out, err
  7431. }
  7432. }
  7433. return out, err
  7434. }
  7435. func UpdateDrugMaxNumber(count int64, id int64) error {
  7436. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7437. return err
  7438. }
  7439. func UpdateNewDrugMaxNumber(count int64, id int64, tx *gorm.DB) error {
  7440. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7441. if err != gorm.ErrRecordNotFound {
  7442. if err != nil {
  7443. tx.Rollback()
  7444. return err
  7445. }
  7446. }
  7447. return err
  7448. }
  7449. func UpdateNewDrugMaxNumberOne(count int64, id int64, tx *gorm.DB) error {
  7450. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  7451. if err != gorm.ErrRecordNotFound {
  7452. if err != nil {
  7453. tx.Rollback()
  7454. return err
  7455. }
  7456. }
  7457. return err
  7458. }
  7459. func UpdateDrugMinNumber(count int64, id int64) error {
  7460. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  7461. return err
  7462. }
  7463. func UpdateNewDrugMinNumber(count int64, id int64, tx *gorm.DB) error {
  7464. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  7465. if err != nil {
  7466. tx.Rollback()
  7467. return err
  7468. }
  7469. return err
  7470. }
  7471. func DeleteDrugOutFlow(id int64, orgid int64) error {
  7472. 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
  7473. return err
  7474. }
  7475. func DeleteNewDrugOutFlow(id int64, orgid int64, tx *gorm.DB) error {
  7476. err := tx.Model(models.DrugFlow{}).Where("warehouse_out_detail_id = ? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"status": 0}).Error
  7477. if err != nil {
  7478. tx.Rollback()
  7479. return err
  7480. }
  7481. return err
  7482. }
  7483. func GetWarehouseOutByWarehouseInfoId(warehouse_info_id int64, good_id int64, orgid int64) (outInfo []*models.WarehouseOutInfo, err error) {
  7484. 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
  7485. return outInfo, err
  7486. }
  7487. func GetNewWarehouseOutByWarehouseInfoId(warehouse_info_id int64, good_id int64, orgid int64, tx *gorm.DB) (outInfo []*models.WarehouseOutInfo, err error) {
  7488. err = tx.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
  7489. if err != gorm.ErrRecordNotFound {
  7490. if err != nil {
  7491. tx.Rollback()
  7492. return outInfo, err
  7493. }
  7494. }
  7495. return outInfo, err
  7496. }
  7497. func GetDrugWarehouseOutByWarehouseInfoId(warehouse_info_id int64, drug_id int64, orgid int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  7498. 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
  7499. return outInfo, err
  7500. }
  7501. func GetNewDrugWarehouseOutByWarehouseInfoId(warehouse_info_id int64, drug_id int64, orgid int64, tx *gorm.DB) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  7502. err = tx.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
  7503. if err != gorm.ErrRecordNotFound {
  7504. if err != nil {
  7505. tx.Rollback()
  7506. return outInfo, err
  7507. }
  7508. }
  7509. return outInfo, err
  7510. }
  7511. func GetAllStockInList(id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  7512. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("GoodInfo", "status = 1 and org_id =?", orgid).Find(&info).Error
  7513. return info, err
  7514. }
  7515. func GetNewAllStockInList(id int64, orgid int64, tx *gorm.DB) (info []*models.VmWarehousingInfoSix, err error) {
  7516. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("GoodInfo", "status = 1 and org_id =?", orgid).Find(&info).Error
  7517. if err != gorm.ErrRecordNotFound {
  7518. if err != nil {
  7519. tx.Rollback()
  7520. return info, err
  7521. }
  7522. }
  7523. return info, err
  7524. }
  7525. func GetDrugWarehouseInfoList(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7526. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "org_id = ? and status = 1", orgid).Find(&info).Error
  7527. return info, err
  7528. }
  7529. func GetNewDrugWarehouseInfoList(id int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7530. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "org_id = ? and status = 1", orgid).Find(&info).Error
  7531. if err != gorm.ErrRecordNotFound {
  7532. if err != nil {
  7533. tx.Rollback()
  7534. return info, err
  7535. }
  7536. }
  7537. return info, err
  7538. }
  7539. func GetStockCountByGoodId(goodid int64, storehouse_id int64, orgId int64) (info []*models.WarehousingInfo, err error) {
  7540. 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
  7541. return info, err
  7542. }
  7543. func GetNewStockCountByGoodId(goodid int64, storehouse_id int64, orgId int64, tx *gorm.DB) (info []*models.WarehousingInfo, err error) {
  7544. err = tx.Where("good_id = ? and storehouse_id = ? and org_id = ? and status = 1 and is_check = 1", goodid, storehouse_id, orgId).Find(&info).Error
  7545. if err != gorm.ErrRecordNotFound {
  7546. if err != nil {
  7547. tx.Rollback()
  7548. return info, err
  7549. }
  7550. }
  7551. return info, err
  7552. }
  7553. func UpdateGoodInfoSumCount(goodid int64, sum_count int64, orgid int64) error {
  7554. 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
  7555. 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
  7556. return err
  7557. }
  7558. func UpdateGoodInfoSumCountSix(goodid int64, sum_count int64, orgid int64) error {
  7559. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7560. return err
  7561. }
  7562. func UpdateGoodInfoReduceSumCount(goodid int64, sum_count int64, orgid int64) error {
  7563. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7564. return err
  7565. }
  7566. func UpdateNewGoodInfoReduceSumCount(goodid int64, sum_count int64, orgid int64, tx *gorm.DB) error {
  7567. err = tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7568. if err != gorm.ErrRecordNotFound {
  7569. if err != nil {
  7570. tx.Rollback()
  7571. return err
  7572. }
  7573. }
  7574. return err
  7575. }
  7576. func GetGoodFlowByWarehouseOutId(good_id int64, user_org_id int64, id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  7577. err = tx.Where("good_id = ? and user_org_id = ? and warehouse_out_detail_id = ? and status=1", good_id, user_org_id, id).Find(&flow).Error
  7578. if err != gorm.ErrRecordNotFound {
  7579. if err != nil {
  7580. tx.Rollback()
  7581. return flow, err
  7582. }
  7583. }
  7584. return
  7585. }
  7586. func UpdateGoodInfoAddSumCount(goodid int64, sum_count int64, orgid int64, sum_in_count int64) error {
  7587. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7588. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  7589. return err
  7590. }
  7591. func UpdateNewGoodInfoAddSumCount(goodid int64, sum_count int64, orgid int64, sum_in_count int64, tx *gorm.DB) error {
  7592. err := tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7593. if err != nil {
  7594. tx.Rollback()
  7595. return err
  7596. }
  7597. 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
  7598. if err != nil {
  7599. tx.Rollback()
  7600. return err
  7601. }
  7602. return err
  7603. }
  7604. func GetCancelStockInfo(id int64, orgid int64) (stock []*models.CancelStockInfo, err error) {
  7605. 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
  7606. return stock, err
  7607. }
  7608. func ModifyCancelStockInfo(id int64, info *models.CancelStockInfo) error {
  7609. 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
  7610. return err
  7611. }
  7612. func GetCancelStockInfoListById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  7613. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  7614. return info, err
  7615. }
  7616. func GetWarehouseOutList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.WarehouseOutInfo, err error) {
  7617. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  7618. return info, err
  7619. }
  7620. func GetCancelWarehouseList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.CancelStockInfo, err error) {
  7621. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  7622. return info, err
  7623. }
  7624. func CheckCancelStock(stock models.CancelStock, cancel_stock_id int64, orgid int64) error {
  7625. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and status = 1 and org_id = ?", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 1}).Error
  7626. err = XTWriteDB().Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? and status = 1 and org_id = ?", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 1}).Error
  7627. return err
  7628. }
  7629. func GetCancelStockInfoById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  7630. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&info).Error
  7631. return info, err
  7632. }
  7633. func UpdateCancelStockNumber(stock_count int64, warehouse_info_id int64, orgid int64) error {
  7634. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ? and org_id = ? and status = 1", warehouse_info_id, orgid).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", stock_count)).Error
  7635. return err
  7636. }
  7637. func UpdateGoodSumCount(sum_count int64, good_id int64, orgid int64) error {
  7638. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  7639. return err
  7640. }
  7641. func UpdateStockWarehouseInfo(cancel_stock_id int64, orgid int64, stock models.CancelStock) error {
  7642. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7643. err = XTWriteDB().Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7644. return err
  7645. }
  7646. func UpdateStockWarehouseInfoStockFlow(id int64, orgid int64, goodid int64) error {
  7647. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("cancel_out_detail_id = ? and user_org_id = ? and status = 1 and good_id =?", id, orgid, goodid).Update(map[string]interface{}{"status": 0}).Error
  7648. return err
  7649. }
  7650. func UpdateStockNumberWarehouseInfo(warehouse_info_id int64, good_id int64, org_id int64, stock_count int64) error {
  7651. 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
  7652. 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
  7653. return err
  7654. }
  7655. func UpdateDrugSumCount(drugid int64, sum_count int64, sum_in_count int64, orgid int64) error {
  7656. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ?", drugid, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  7657. 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
  7658. return err
  7659. }
  7660. func GetDrugStockWarehouseInfo(id int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfo, err error) {
  7661. err = XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1 and storehouse_id = ?", id, orgid, storehouse_id).Find(&info).Error
  7662. return info, err
  7663. }
  7664. func UpdateBaseDrugSum(drugid int64, orgid int64, sum_count int64, sum_in_count int64) error {
  7665. 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
  7666. 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
  7667. return err
  7668. }
  7669. func GetDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64) (info []*models.DrugWarehouseInfo, err error) {
  7670. 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
  7671. return info, err
  7672. }
  7673. func GetSelfDrugAllStockInfoOne(patient_id int64, orgid int64, drugid int64) (info []*models.XtSelfDrugWarehouseInfo, err error) {
  7674. err = XTReadDB().Where("patient_id = ? and org_id = ? and status = 1 and drug_id = ? and is_check = 1", patient_id, orgid, drugid).Find(&info).Error
  7675. return info, err
  7676. }
  7677. func GetNewDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
  7678. err = tx.Where("storehouse_id = ? and org_id = ? and status = 1 and drug_id = ? and is_check = 1", storehouse_id, orgid, drugid).Find(&info).Error
  7679. if err != gorm.ErrRecordNotFound {
  7680. if err != nil {
  7681. tx.Rollback()
  7682. return info, err
  7683. }
  7684. }
  7685. return info, err
  7686. }
  7687. func UpdateBaseDrugSumInfo(sum_count int64, drugid int64, orgid int64, sum_in_count int64) error {
  7688. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7689. err = XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  7690. return err
  7691. }
  7692. func UpdateNewBaseDrugSumInfo(sum_count int64, drugid int64, orgid int64, sum_in_count int64, tx *gorm.DB) error {
  7693. err := tx.Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7694. if err != gorm.ErrRecordNotFound {
  7695. if err != nil {
  7696. tx.Rollback()
  7697. return err
  7698. }
  7699. }
  7700. err = tx.Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  7701. if err != gorm.ErrRecordNotFound {
  7702. if err != nil {
  7703. tx.Rollback()
  7704. return err
  7705. }
  7706. }
  7707. return err
  7708. }
  7709. func UpdateBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64) error {
  7710. err = writeDb.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7711. return err
  7712. }
  7713. func UpdateNewBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64, tx *gorm.DB) error {
  7714. err = tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7715. if err != nil {
  7716. tx.Rollback()
  7717. return err
  7718. }
  7719. return err
  7720. }
  7721. func GetLastCancelStockByList(id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  7722. 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
  7723. return info, err
  7724. }
  7725. func GetCancelDrugStockList(cancel_stock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  7726. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  7727. return info, err
  7728. }
  7729. func ModifyCancelStock(cancel_stock_id int64, stock models.DrugCancelStock, orgid int64) error {
  7730. 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
  7731. 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
  7732. return err
  7733. }
  7734. func GetCancelDrugStock(cancel_stock_id int64, orgid int64) (models.DrugCancelStock, error) {
  7735. stock := models.DrugCancelStock{}
  7736. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&stock).Error
  7737. return stock, err
  7738. }
  7739. func GetDrugCancelCheckById(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  7740. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&stock).Error
  7741. return stock, err
  7742. }
  7743. func GetDrugCancelCheckByIdTwenty(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  7744. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 ", cancel_stock_id, orgid).Find(&stock).Error
  7745. return stock, err
  7746. }
  7747. func GetDrugWarehouseOutList(batch_number_id int64, drugId int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  7748. err = XTReadDB().Where("warehouse_info_id = ? and drug_id = ? and org_id = ? and status = 1", batch_number_id, drugId, orgid).Find(&info).Error
  7749. return info, err
  7750. }
  7751. func ModifyDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64) error {
  7752. 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
  7753. return err
  7754. }
  7755. func ModifyNewDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  7756. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and drug_id = ? and status = 1", id, orgid, drugid).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7757. if err != nil {
  7758. tx.Rollback()
  7759. return err
  7760. }
  7761. return err
  7762. }
  7763. func ModifyDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64) error {
  7764. 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
  7765. return err
  7766. }
  7767. func ModifyNewDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  7768. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and drug_id = ? and status = 1", id, orgid, drugid).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  7769. if err != nil {
  7770. tx.Rollback()
  7771. return err
  7772. }
  7773. return err
  7774. }
  7775. func ModifyDrugCancelStockInfo(id int64, info *models.DrugCancelStockInfo) error {
  7776. 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
  7777. return err
  7778. }
  7779. func ReturnDrugCancelStock(cancelstock_id int64, stock models.DrugCancelStock) error {
  7780. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  7781. err = XTWriteDB().Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  7782. return err
  7783. }
  7784. func GetDrugCancelStockInfo(cancelstock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  7785. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancelstock_id, orgid).Find(&info).Error
  7786. return info, err
  7787. }
  7788. func ModifyDrugFlowByCancelId(cancelstock_id int64, drugId int64, orgid int64) error {
  7789. 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
  7790. return err
  7791. }
  7792. func ModifyDrugMaxNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  7793. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and status = 1", id, orgid).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  7794. return err
  7795. }
  7796. func ModifyDrugMinNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  7797. 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
  7798. return err
  7799. }
  7800. func GetDrugInfoSix(id int64, orgid int64) (models.DrugWarehouseInfo, error) {
  7801. info := models.DrugWarehouseInfo{}
  7802. err := XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1", id, orgid).Find(&info).Error
  7803. return info, err
  7804. }
  7805. func GetSelfDrugInfoSix(id int64, orgid int64) (models.XtSelfDrugWarehouseInfo, error) {
  7806. info := models.XtSelfDrugWarehouseInfo{}
  7807. err := XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1", id, orgid).Find(&info).Error
  7808. return info, err
  7809. }
  7810. func GetPrintList(storehouse_id int64, good_name string, orgid int64, limit int64, page int64) (storeinventory []*models.XtStockInventory, err error) {
  7811. db := XTReadDB().Model(&storeinventory).Where("status = 1")
  7812. if storehouse_id > 0 {
  7813. db = db.Where("storehouse_id = ?", storehouse_id)
  7814. }
  7815. if orgid > 0 {
  7816. db = db.Where("user_org_id = ?", orgid)
  7817. }
  7818. err = XTReadDB().Preload("GoodInfo", "status = 1 and org_id = ?", orgid).Preload("WarehousingInfo", "status = 1 and org_id=?", orgid).Find(&storeinventory).Error
  7819. return storeinventory, err
  7820. }
  7821. func GetGoodDialysisOutInfo(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.AutomaticReduceDetail, error) {
  7822. detail := models.AutomaticReduceDetail{}
  7823. 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
  7824. return detail, err
  7825. }
  7826. func GetGoodDialysisOutInfoSix(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.WarehouseOutInfo, error) {
  7827. detail := models.WarehouseOutInfo{}
  7828. 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
  7829. return detail, err
  7830. }
  7831. func GetGoodSumCount(storehouse_id int64, good_id int64, orgid int64) (models.XtGoodStockCount, error) {
  7832. stock := models.XtGoodStockCount{}
  7833. err := XTReadDB().Where("storehouse_id = ? and good_id = ? and user_org_id = ? and status = 1", storehouse_id, good_id, orgid).Find(&stock).Error
  7834. return stock, err
  7835. }
  7836. func CreateGoodStockCount(stock models.XtGoodStockCount) error {
  7837. err := XTWriteDB().Create(&stock).Error
  7838. return err
  7839. }
  7840. func AddGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7841. err := XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count + ?", count)).Error
  7842. return err
  7843. }
  7844. func ReduceGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7845. ut := XTWriteDB().Begin()
  7846. 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
  7847. if err != nil {
  7848. ut.Rollback()
  7849. return err
  7850. }
  7851. ut.Commit()
  7852. return err
  7853. }
  7854. func UpdateGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64) error {
  7855. err := XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  7856. return err
  7857. }
  7858. func UpdateNewGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64, tx *gorm.DB) error {
  7859. err := tx.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  7860. if err != nil {
  7861. tx.Rollback()
  7862. return err
  7863. }
  7864. return err
  7865. }
  7866. func CreateDrugStock(stock models.XtDrugStockCount) error {
  7867. err := XTWriteDB().Create(&stock).Error
  7868. return err
  7869. }
  7870. func UpdateDrugCount(storehouse_id int64, user_org_id int64, drug_id int64, sum_in_count int64, flush_count int64) error {
  7871. ut := XTWriteDB().Begin()
  7872. 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
  7873. if err != nil {
  7874. ut.Rollback()
  7875. return err
  7876. }
  7877. 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
  7878. if err != nil {
  7879. ut.Rollback()
  7880. return err
  7881. }
  7882. ut.Commit()
  7883. return err
  7884. }
  7885. func UpdateDrugCountOne(storehouse_id int64, user_org_id int64, drug_id int64, flush_count int64) error {
  7886. ut := XTWriteDB().Begin()
  7887. 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
  7888. if err != nil {
  7889. ut.Rollback()
  7890. return err
  7891. }
  7892. ut.Commit()
  7893. return err
  7894. }
  7895. func AddDrugSumOutCount(storehouse_id int64, sum_out_count int64, user_org_id int64, drug_id int64) error {
  7896. ut := XTWriteDB().Begin()
  7897. 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
  7898. if err != nil {
  7899. ut.Rollback()
  7900. return err
  7901. }
  7902. 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
  7903. if err != nil {
  7904. ut.Rollback()
  7905. return err
  7906. }
  7907. ut.Commit()
  7908. return err
  7909. }
  7910. func ReduceDrugSumOut(storehouse_id int64, user_org_id int64, drug_id int64, sum_out_count int64) error {
  7911. ut := XTWriteDB().Begin()
  7912. 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
  7913. if err != nil {
  7914. ut.Rollback()
  7915. return err
  7916. }
  7917. ut.Commit()
  7918. return err
  7919. }
  7920. func AddGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7921. ut := XTWriteDB().Begin()
  7922. 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
  7923. if err != nil {
  7924. ut.Rollback()
  7925. return err
  7926. }
  7927. 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
  7928. if err != nil {
  7929. ut.Rollback()
  7930. return err
  7931. }
  7932. ut.Commit()
  7933. return err
  7934. }
  7935. func ReduceGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7936. ut := XTWriteDB().Begin()
  7937. 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
  7938. if err != nil {
  7939. ut.Rollback()
  7940. return err
  7941. }
  7942. 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
  7943. if err != nil {
  7944. ut.Rollback()
  7945. return err
  7946. }
  7947. ut.Commit()
  7948. return err
  7949. }
  7950. func GetInvetoryWarehouseInfoList(orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  7951. if orgid == 10191 || orgid == 10138 || orgid == 10278 {
  7952. err = XTReadDB().Model(&info).Where("status = 1 and storehouse_id=? and is_check= 1 and org_id = ?", storehouse_id, orgid).Preload("GoodInfo", "status = 1").Order("good_id").Find(&info).Error
  7953. return info, err
  7954. } else {
  7955. err = XTReadDB().Model(&info).Where("status = 1 and storehouse_id=? and is_check= 1 and org_id = ? and stock_count >0", storehouse_id, orgid).Preload("GoodInfo", "status = 1").Order("good_id").Find(&info).Error
  7956. return info, err
  7957. }
  7958. }
  7959. func GetSettleOpenConfig(user_org_id int64) (*models.HisSettleStockConfig, error) {
  7960. config := models.HisSettleStockConfig{}
  7961. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7962. if err == gorm.ErrRecordNotFound {
  7963. return nil, err
  7964. }
  7965. if err != nil {
  7966. return nil, err
  7967. }
  7968. return &config, nil
  7969. }
  7970. func CreateSettleOpenConfig(config models.HisSettleStockConfig) error {
  7971. err := XTWriteDB().Create(&config).Error
  7972. return err
  7973. }
  7974. func UpdateSettleOpenConfig(id int64, config models.HisSettleStockConfig) error {
  7975. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_open": config.IsOpen}).Error
  7976. return err
  7977. }
  7978. func UpdateDrugStockAutomaticReduceRecordOne(orgid int64, is_open int64) (models.DrugStockConfig, error) {
  7979. config := models.DrugStockConfig{}
  7980. err := XTWriteDB().Model(&config).Where("org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  7981. return config, err
  7982. }
  7983. func UpdatePharyConfig(orgid int64, is_open int64) (models.PharmacyConfig, error) {
  7984. stockConfig := models.PharmacyConfig{}
  7985. err := XTWriteDB().Model(&stockConfig).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  7986. return stockConfig, err
  7987. }
  7988. func UpdateSettleOpenConfigOne(id int64, is_open int64) error {
  7989. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  7990. return err
  7991. }
  7992. func UpdateSettleOpenConfigTwo(id int64, is_open int64) error {
  7993. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  7994. return err
  7995. }
  7996. func GetSettleOpenConfigByOrgId(user_org_id int64) (models.HisSettleStockConfig, error) {
  7997. config := models.HisSettleStockConfig{}
  7998. err = XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7999. return config, nil
  8000. }
  8001. func GetGoodInformationByGoodIdOne(good_id int64) (models.GoodInformationFourty, error) {
  8002. info := models.GoodInformationFourty{}
  8003. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  8004. return info, err
  8005. }
  8006. func GetAllWarehouseInfo(good_id int64, user_org_id int64) (info []*models.WarehousingInfo, err error) {
  8007. err = XTReadDB().Where("good_id = ? and user_org_id =? and status=1 and is_check =1", good_id, user_org_id).Find(&info).Error
  8008. return info, err
  8009. }
  8010. func GetHisPrescriptionByGoodId(good_id int64, user_org_id int64) (project []*models.HisPrescriptionProject, err error) {
  8011. err = XTReadDB().Where("project_id = ? and user_org_id =? and status=1 and type =3", good_id, user_org_id).Find(&project).Error
  8012. return project, err
  8013. }
  8014. func GetHisDocById(id int64, orgid int64) (models.HisDoctorAdvice, error) {
  8015. advice := models.HisDoctorAdvice{}
  8016. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  8017. return advice, err
  8018. }
  8019. func GetBloodDocById(id int64, orgid int64) (models.DoctorAdvice, error) {
  8020. advice := models.DoctorAdvice{}
  8021. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  8022. return advice, err
  8023. }
  8024. func FindAllSettleWarehouseOut(org_id int64, warehouse_out_time int64, is_sys int64) (models.WarehouseOut, error) {
  8025. out := models.WarehouseOut{}
  8026. 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
  8027. return out, err
  8028. }
  8029. func GetGoodOutOpenConfig(user_org_id int64) (*models.XtGoodOutConfig, error) {
  8030. config := models.XtGoodOutConfig{}
  8031. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8032. if err == gorm.ErrRecordNotFound {
  8033. return nil, err
  8034. }
  8035. if err != nil {
  8036. return nil, err
  8037. }
  8038. return &config, nil
  8039. }
  8040. func CreateGoodOutOpenConfig(good models.XtGoodOutConfig) error {
  8041. err := XTWriteDB().Create(&good).Error
  8042. return err
  8043. }
  8044. func UpdateGoodOutOpenConfig(id int64, good models.XtGoodOutConfig) error {
  8045. err := XTWriteDB().Model(&good).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"is_open": good.IsOpen}).Error
  8046. return err
  8047. }
  8048. func FindGoodOutConfigById(user_org_id int64) (models.XtGoodOutConfig, error) {
  8049. config := models.XtGoodOutConfig{}
  8050. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8051. return config, err
  8052. }
  8053. func UpdateGoodAutoConfig(user_org_id int64) error {
  8054. gobalConfig := models.GobalConfig{}
  8055. err := XTWriteDB().Model(&gobalConfig).Where("org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  8056. return err
  8057. }
  8058. func UpdateGoodOutConfig(user_org_id int64) error {
  8059. config := models.XtGoodOutConfig{}
  8060. err := XTWriteDB().Model(&config).Where("user_org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  8061. return err
  8062. }
  8063. func GetHisGoodList(patient_id int64, record_date int64, orgid int64) (info []*models.GoodHisPrescriptionProject, err error) {
  8064. 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
  8065. return info, err
  8066. }
  8067. func GetLastGoodWarehouseOutInfoByProjectId(good_id int64, patient_id int64, sys_record_time int64, project_id int64) (good []*models.WarehouseOutInfo, err error) {
  8068. 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
  8069. return good, err
  8070. }
  8071. func ModifyGoodWarehouseInfo(good_id int64, warehouse_info_id int64, orgid int64, count int64) error {
  8072. 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
  8073. return err
  8074. }
  8075. func DeleteGoodWarehouseOutInfo(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  8076. 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
  8077. return err
  8078. }
  8079. func DeleteGoodFlow(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  8080. 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
  8081. return err
  8082. }
  8083. func FindPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  8084. out := models.WarehouseOut{}
  8085. 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
  8086. return out, err
  8087. }
  8088. func FindNewPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  8089. out := models.WarehouseOut{}
  8090. err = tx.Model(&models.WarehouseOut{}).Where("org_id = ? and status = 1 and warehouse_out_time = ? and is_sys=1", org_id, warehouse_out_time).Find(&out).Error
  8091. if err != gorm.ErrRecordNotFound {
  8092. if err != nil {
  8093. tx.Rollback()
  8094. return out, err
  8095. }
  8096. }
  8097. return out, err
  8098. }
  8099. func FindLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  8100. out := models.WarehouseOut{}
  8101. 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
  8102. return out, err
  8103. }
  8104. func FindNewLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  8105. out := models.WarehouseOut{}
  8106. err = tx.Model(&models.WarehouseOut{}).Where("org_id = ? and status = 1 and warehouse_out_time = ? and is_sys=1", org_id, warehouse_out_time).Last(&out).Error
  8107. if err != gorm.ErrRecordNotFound {
  8108. if err != nil {
  8109. tx.Rollback()
  8110. return out, err
  8111. }
  8112. }
  8113. return out, err
  8114. }
  8115. func GetGoodTypeObj(goodid int64, org_id int64) (models.GoodsType, error) {
  8116. goodsType := models.GoodsType{}
  8117. err := XTReadDB().Where("good_id = ? and org_id = ? and status = 1", goodid, org_id).Find(&goodsType).Error
  8118. return goodsType, err
  8119. }
  8120. func GetAllGoodTypeName(good_type_id int64, org_id int64) (good_type_name string) {
  8121. goodsTypeList, _ := GetAllGoodType(org_id)
  8122. for _, item := range goodsTypeList {
  8123. if good_type_id == item.ID {
  8124. good_type_name = item.TypeName
  8125. }
  8126. }
  8127. return good_type_name
  8128. }
  8129. func UpdateGoodTypeName(good_id int64, good_type_name string) (models.GoodInformation, error) {
  8130. information := models.GoodInformation{}
  8131. err := XTWriteDB().Model(&information).Where("id = ? and status =1", good_id).Updates(map[string]interface{}{"good_type_name": good_type_name}).Error
  8132. return information, err
  8133. }
  8134. func GetSencondeGoodWarehouseInfo(good_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  8135. err = XTReadDB().Where("good_id = ? and org_id= ? and status= 1 and is_check = 1", good_id, org_id).Find(&info).Error
  8136. return info, err
  8137. }
  8138. func UpdateGoodSumCountSeven(sum_count int64, good_id int64, orgid int64) error {
  8139. ut := writeDb.Begin()
  8140. 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
  8141. if err != nil {
  8142. ut.Rollback()
  8143. return err
  8144. }
  8145. ut.Commit()
  8146. return err
  8147. }
  8148. func UpdateNewGoodSumCountSeven(sum_count int64, good_id int64, orgid int64, tx *gorm.DB) error {
  8149. err := tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Updates(map[string]interface{}{"sum_count": sum_count}).Error
  8150. if err != nil {
  8151. tx.Rollback()
  8152. return err
  8153. }
  8154. return err
  8155. }
  8156. func GetWarehouseOutInfoIsExistOTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  8157. info := models.WarehouseOutInfo{}
  8158. var err error
  8159. 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
  8160. if err == gorm.ErrRecordNotFound {
  8161. return nil, err
  8162. }
  8163. if err != nil {
  8164. return nil, err
  8165. }
  8166. return &info, nil
  8167. }
  8168. func GetWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  8169. info := models.WarehouseOutInfo{}
  8170. 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
  8171. return info, err
  8172. }
  8173. func GetNewWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  8174. info := models.WarehouseOutInfo{}
  8175. err := tx.Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ?", good_id, record_time, patient_id, project_id).Find(&info).Error
  8176. if err != gorm.ErrRecordNotFound {
  8177. if err != nil {
  8178. tx.Rollback()
  8179. return info, err
  8180. }
  8181. }
  8182. return info, err
  8183. }
  8184. func GetWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (models.WarehouseOutInfo, error) {
  8185. info := models.WarehouseOutInfo{}
  8186. 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
  8187. return info, err
  8188. }
  8189. func GetNewWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  8190. info := models.WarehouseOutInfo{}
  8191. err := tx.Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ? and warehouse_info_id = ?", good_id, record_time, patient_id, project_id, warehouse_info_id).Find(&info).Error
  8192. if err != gorm.ErrRecordNotFound {
  8193. if err != nil {
  8194. tx.Rollback()
  8195. return info, err
  8196. }
  8197. }
  8198. return info, err
  8199. }
  8200. func GetNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (*models.WarehouseOutInfo, error) {
  8201. info := models.WarehouseOutInfo{}
  8202. var err error
  8203. 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
  8204. if err == gorm.ErrRecordNotFound {
  8205. return nil, err
  8206. }
  8207. if err != nil {
  8208. return nil, err
  8209. }
  8210. return &info, nil
  8211. }
  8212. func GetNewNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (*models.WarehouseOutInfo, error) {
  8213. info := models.WarehouseOutInfo{}
  8214. var err error
  8215. err = tx.Model(&info).Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ? and warehouse_info_id = ?", good_id, record_time, patient_id, project_id, warehouse_info_id).Find(&info).Error
  8216. if err != gorm.ErrRecordNotFound {
  8217. if err != nil {
  8218. tx.Rollback()
  8219. return &info, err
  8220. }
  8221. }
  8222. return &info, err
  8223. }
  8224. func GetNewGoodWarehouseInfo(startime int64, endtime int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.GoodWarehousingInfo, err error) {
  8225. db := XTReadDB().Model(&info).Where("status = 1")
  8226. if startime > 0 {
  8227. db = db.Where("ctime>=?", startime)
  8228. }
  8229. if endtime > 0 {
  8230. db = db.Where("ctime<=?", endtime)
  8231. }
  8232. if orgid > 0 {
  8233. db = db.Where("org_id = ?", orgid)
  8234. }
  8235. if len(good_id) > 0 {
  8236. db = db.Where("good_id in(?)", good_id)
  8237. }
  8238. if storehouse_id > 0 {
  8239. db = db.Where("storehouse_id = ?", storehouse_id)
  8240. }
  8241. err = db.Preload("GoodInfo", "org_id = ? and status= 1", orgid).Find(&info).Error
  8242. return info, err
  8243. }
  8244. func GetNewWarehouseOutInfoList(start_time int64, end_time int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.WarehouseOutInfoNight, err error) {
  8245. db := XTReadDB().Model(&info).Where("status= 1")
  8246. if start_time > 0 {
  8247. db = db.Where("ctime>=?", start_time)
  8248. }
  8249. if end_time > 0 {
  8250. db = db.Where("ctime <=?", end_time)
  8251. }
  8252. if orgid > 0 {
  8253. db = db.Where("org_id = ?", orgid)
  8254. }
  8255. if len(good_id) > 0 {
  8256. db = db.Where("good_id in(?)", good_id)
  8257. }
  8258. if storehouse_id > 0 {
  8259. db = db.Where("storehouse_id = ?", storehouse_id)
  8260. }
  8261. err = db.Preload("GoodInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  8262. return info, err
  8263. }
  8264. func GetGoodOutOpenConfigOne(user_org_id int64) (models.XtGoodOutConfig, error) {
  8265. config := models.XtGoodOutConfig{}
  8266. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8267. return config, err
  8268. }
  8269. func UpdateDeleteAutoGood(good_id int64, record_date int64, patient_id int64) error {
  8270. 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
  8271. 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
  8272. return err
  8273. }
  8274. func UpdateWarehouseInfoMap(list []*models.WarehousingInfo, warehousing_info_id int64, orgId int64) error {
  8275. tx := XTWriteDB().Begin()
  8276. for _, item := range list {
  8277. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", item.WarehousingCount)).Error
  8278. if err != nil {
  8279. tx.Rollback()
  8280. return err
  8281. }
  8282. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).Update(map[string]interface{}{"is_check": 2}).Error
  8283. if err != nil {
  8284. tx.Rollback()
  8285. return err
  8286. }
  8287. err = tx.Model(models.VmStockFlow{}).Where("warehousing_detail_id = ?", item.ID).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  8288. if err != nil {
  8289. tx.Rollback()
  8290. return err
  8291. }
  8292. var goodList []*models.WarehousingInfo
  8293. 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
  8294. var flush_count int64
  8295. for _, it := range goodList {
  8296. flush_count += it.StockCount
  8297. }
  8298. 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
  8299. if err != nil {
  8300. tx.Rollback()
  8301. return err
  8302. }
  8303. 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
  8304. if err != nil {
  8305. tx.Rollback()
  8306. return err
  8307. }
  8308. storeHouseConfig := models.XtStorehouseConfig{}
  8309. err = tx.Where("user_org_id = ? and status = 1", orgId).First(&storeHouseConfig).Error
  8310. var stockList []*models.WarehousingInfo
  8311. //查询该耗材剩余多少库存
  8312. 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
  8313. var sum_count int64
  8314. var sum_in_count int64
  8315. for _, it := range stockList {
  8316. sum_count += it.StockCount
  8317. sum_in_count += it.WarehousingCount
  8318. }
  8319. 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
  8320. if err != nil {
  8321. tx.Rollback()
  8322. return err
  8323. }
  8324. 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
  8325. if err != nil {
  8326. tx.Rollback()
  8327. return err
  8328. }
  8329. }
  8330. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8331. if err != nil {
  8332. tx.Rollback()
  8333. return err
  8334. }
  8335. err = tx.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8336. tx.Commit()
  8337. return err
  8338. }
  8339. func UpdateNewWarehouseInfoMap(list []*models.WarehousingInfo, warehousing_info_id int64, orgId int64, tx *gorm.DB) error {
  8340. for _, item := range list {
  8341. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", item.WarehousingCount)).Error
  8342. if err != nil {
  8343. tx.Rollback()
  8344. return err
  8345. }
  8346. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).Update(map[string]interface{}{"is_check": 2}).Error
  8347. if err != nil {
  8348. tx.Rollback()
  8349. return err
  8350. }
  8351. err = tx.Model(models.VmStockFlow{}).Where("warehousing_detail_id = ?", item.ID).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  8352. if err != nil {
  8353. tx.Rollback()
  8354. return err
  8355. }
  8356. var goodList []*models.WarehousingInfo
  8357. 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
  8358. var flush_count int64
  8359. for _, it := range goodList {
  8360. flush_count += it.StockCount
  8361. }
  8362. 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
  8363. if err != nil {
  8364. tx.Rollback()
  8365. return err
  8366. }
  8367. 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
  8368. if err != nil {
  8369. tx.Rollback()
  8370. return err
  8371. }
  8372. storeHouseConfig := models.XtStorehouseConfig{}
  8373. err = tx.Where("user_org_id = ? and status = 1", orgId).First(&storeHouseConfig).Error
  8374. var stockList []*models.WarehousingInfo
  8375. //查询该耗材剩余多少库存
  8376. 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
  8377. var sum_count int64
  8378. var sum_in_count int64
  8379. for _, it := range stockList {
  8380. sum_count += it.StockCount
  8381. sum_in_count += it.WarehousingCount
  8382. }
  8383. 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
  8384. if err != nil {
  8385. tx.Rollback()
  8386. return err
  8387. }
  8388. 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
  8389. if err != nil {
  8390. tx.Rollback()
  8391. return err
  8392. }
  8393. }
  8394. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8395. if err != nil {
  8396. tx.Rollback()
  8397. return err
  8398. }
  8399. err = tx.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8400. return err
  8401. }
  8402. func FindFirstWarehousingInfoByStockThree(good_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  8403. if storehouse_id == 0 {
  8404. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime").First(&info).Error
  8405. }
  8406. if storehouse_id > 0 {
  8407. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and stock_count > 0 and storehouse_id =? ", good_id, storehouse_id).Order("ctime").First(&info).Error
  8408. }
  8409. return info, err
  8410. }
  8411. func FindFirstWarehousingInfoByStockFour(good_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  8412. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and storehouse_id =? ", good_id, storehouse_id).Order("ctime").Last(&info).Error
  8413. return info, err
  8414. }
  8415. func GetHisPrescriptionNumberList(project_id int64, record_date int64, orgid int64, patient_id int64) (list []*models.HisPrescriptionProject, err error) {
  8416. err = readDb.Where("project_id = ? and record_date =? and user_org_id =? and status =1 and patient_id = ?", project_id, record_date, orgid, patient_id).Find(&list).Error
  8417. return list, err
  8418. }
  8419. func GetWarehouseOutnfoByProjectId(project_id int64, record_date int64, orgid int64, patient_id int64) (infor []*models.WarehouseOutInfo, err error) {
  8420. err = readDb.Where("good_id =? and sys_record_time = ? and org_id = ? and patient_id = ? and status=1", project_id, record_date, orgid, patient_id).Find(&infor).Error
  8421. return infor, err
  8422. }
  8423. func UpdateWarehouseInfoByPatientId(project_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfo, error) {
  8424. info := models.WarehouseOutInfo{}
  8425. err := XTWriteDB().Model(&info).Where("good_id = ? and sys_record_time = ? and org_id = ? and status =1 and patient_id = ?", project_id, record_date, orgid, patient_id).Updates(map[string]interface{}{"status": 0}).Error
  8426. return info, err
  8427. }
  8428. func UpdateWarehouseInfoByProject(good_id int64, id int64, user_org_id int64, count int64) (models.WarehousingInfo, error) {
  8429. info := models.WarehousingInfo{}
  8430. err := XTWriteDB().Where("id = ? and good_id = ? and org_id = ? and status= 1", id, good_id, user_org_id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  8431. return info, err
  8432. }
  8433. func GetHisPrescripionProjectList(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  8434. err = XTReadDB().Where("project_id=? and status=1 and type =3 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  8435. return list, err
  8436. }
  8437. func GetHisPrescripionProjectListOne(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  8438. err = XTReadDB().Where("project_id=? and status=1 and type =2 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  8439. return list, err
  8440. }
  8441. func UpdateHisPrescriptionProjectList(id int64, price float64) (models.HisPrescriptionProjectTemplateL, error) {
  8442. templateL := models.HisPrescriptionProjectTemplateL{}
  8443. err := XTWriteDB().Model(&templateL).Where("id =? and status=1", id).Updates(map[string]interface{}{"price": price}).Error
  8444. return templateL, err
  8445. }
  8446. func ModifyNewGoodWarehouseInfoStockMinNumber(count int64, goodid int64, orgid int64, id int64, tx *gorm.DB) error {
  8447. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and org_id = ? and good_id = ? and status = 1", id, orgid, goodid).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  8448. if err != nil {
  8449. tx.Rollback()
  8450. return err
  8451. }
  8452. return err
  8453. }
  8454. func GetDrugInvetoryByDrugId(user_org_id int64, drug_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  8455. err = XTReadDB().Where("org_id = ? and drug_id = ? and status=1", user_org_id, drug_id).Preload("XtBaseDrug", "org_id = ? and status=1", user_org_id).Order("id desc").Find(&info).Error
  8456. return info, err
  8457. }
  8458. func UpdateNewDrugWarehouseInfoById(id int64, last_stock_max_number int64, last_stock_min_number int64) error {
  8459. err := XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"stock_max_number": last_stock_max_number, "stock_min_number": last_stock_min_number}).Error
  8460. return err
  8461. }
  8462. func GeteSelfPrescriptionOpen(user_org_id int64) (models.XtSelfDrugInfoConfig, error) {
  8463. drugInfoConfig := models.XtSelfDrugInfoConfig{}
  8464. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugInfoConfig).Error
  8465. return drugInfoConfig, err
  8466. }
  8467. func CreaterSelfConfig(infoConfig models.XtSelfDrugInfoConfig) error {
  8468. err := XTWriteDB().Create(&infoConfig).Error
  8469. return err
  8470. }
  8471. func SaveSelfConfig(infoConfig models.XtSelfDrugInfoConfig) error {
  8472. err := XTWriteDB().Save(&infoConfig).Error
  8473. return err
  8474. }
  8475. func GetSelfPrescriptionOutOpen(user_org_id int64) (models.XtSelfDrugOutConfig, error) {
  8476. drugOutConfig := models.XtSelfDrugOutConfig{}
  8477. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugOutConfig).Error
  8478. return drugOutConfig, err
  8479. }
  8480. func CreateSelfDrugOutConfig(config models.XtSelfDrugOutConfig) error {
  8481. err := XTWriteDB().Create(&config).Error
  8482. return err
  8483. }
  8484. func UpdateSelfDrugOutConfig(config models.XtSelfDrugOutConfig) error {
  8485. err := XTWriteDB().Save(&config).Error
  8486. return err
  8487. }
  8488. func CreateSelfDrugFlowOne(flow models.XtSelfDrugFlow, tx *gorm.DB) error {
  8489. err := tx.Create(&flow).Error
  8490. if err != nil {
  8491. tx.Rollback()
  8492. return err
  8493. }
  8494. return err
  8495. }
  8496. func UpdateWarehouseOutInfoByPrice(good_id int64, packing_price float64) error {
  8497. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("good_id =? and status =1 and is_check=1", good_id).Updates(map[string]interface{}{"packing_price": packing_price}).Error
  8498. return err
  8499. }
  8500. func GetWarehosueOutInfoByPrice(good_id int64, org_id int64) (out []*models.WarehouseOutInfo, err error) {
  8501. err = XTReadDB().Where("good_id = ? and org_id = ? and status=1 and price =0", good_id, org_id).Find(&out).Error
  8502. return out, err
  8503. }
  8504. func UpdateWarehouseOut(good_id int64, packing_price float64) error {
  8505. err := XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("id = ? and status=1", good_id).Updates(map[string]interface{}{"price": packing_price}).Error
  8506. return err
  8507. }
  8508. func UpdateStockFlowByList(good_id int64, packing_price float64) error {
  8509. err := XTWriteDB().Model(models.VmStockFlow{}).Where("good_id = ? and user_org_id = 10633 and status=1", good_id).Updates(map[string]interface{}{"price": packing_price}).Error
  8510. return err
  8511. }
  8512. func GetGoodStockInventoryByRecordate(good_id int64, record_date int64, user_org_id int64) (models.XtGoodStockInventoryList, error) {
  8513. goodStockInventoryList := models.XtGoodStockInventoryList{}
  8514. err := XTReadDB().Where("good_id = ? and record_date = ? and user_org_id = ? and status=1", good_id, record_date, user_org_id).Find(&goodStockInventoryList).Error
  8515. return goodStockInventoryList, err
  8516. }
  8517. func CreateGoodStockInventoryList(list models.XtGoodStockInventoryList) error {
  8518. err := XTWriteDB().Create(&list).Error
  8519. return err
  8520. }
  8521. func SaveGoodStockInventoryList(list models.XtGoodStockInventoryList) error {
  8522. err := XTWriteDB().Save(&list).Error
  8523. return err
  8524. }
  8525. func GetGoodStockInventoryList(keyword string, limit int64, page int64, storehouse_id int64, startTime int64, endTime int64, orgId int64) (list []*models.XtGoodStockInventoryList, total int64, err error) {
  8526. likeKey := "%" + keyword + "%"
  8527. offset := (page - 1) * limit
  8528. db := XTReadDB().Model(&models.XtGoodStockInventoryList{}).Where("status=1")
  8529. if storehouse_id > 0 {
  8530. db = db.Where("storehouse_id = ?", storehouse_id)
  8531. }
  8532. if startTime > 0 {
  8533. db = db.Where("record_date >=?", startTime)
  8534. }
  8535. if endTime > 0 {
  8536. db = db.Where("record_date<=?", endTime)
  8537. }
  8538. if len(keyword) > 0 {
  8539. db = db.Where("good_name like ? or specification_name like ?", likeKey, likeKey)
  8540. }
  8541. if orgId > 0 {
  8542. db = db.Where("user_org_id = ?", orgId)
  8543. }
  8544. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  8545. return list, total, err
  8546. }
  8547. func GetDrugStockInventoryList(drug_id int64, record_date int64, user_org_id int64) (models.XtDrugStockInventoryList, error) {
  8548. drugStockInventoryList := models.XtDrugStockInventoryList{}
  8549. err := XTReadDB().Where("drug_id = ? and record_date = ? and user_org_id = ? and status=1", drug_id, record_date, user_org_id).Find(&drugStockInventoryList).Error
  8550. return drugStockInventoryList, err
  8551. }
  8552. func CreatedDrugStockInventory(list models.XtDrugStockInventoryList) error {
  8553. err := XTWriteDB().Create(&list).Error
  8554. return err
  8555. }
  8556. func SaveDrugStockInventory(list models.XtDrugStockInventoryList) error {
  8557. err := XTWriteDB().Save(&list).Error
  8558. return err
  8559. }
  8560. func GetStockInventoryPrintList(storehouse_id int64, keyword string, limit int64, page int64, ids []string, orgId int64) (list []*models.XtGoodStockInventoryList, total int64, err error) {
  8561. db := XTReadDB().Model(&list).Where("status=1")
  8562. likeKey := "%" + keyword + "%"
  8563. offset := (page - 1) * limit
  8564. if storehouse_id > 0 {
  8565. db = db.Where("storehouse_id = ?", storehouse_id)
  8566. }
  8567. if len(keyword) > 0 {
  8568. db = db.Where("good_name like ?", likeKey)
  8569. }
  8570. if orgId > 0 {
  8571. db = db.Where("user_org_id = ?", orgId)
  8572. }
  8573. if len(ids) > 0 {
  8574. db = db.Where("id in(?)", ids)
  8575. }
  8576. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  8577. return list, total, err
  8578. }
  8579. func GetGoodWarehouseInfoList(startTime int64, endTime int64, orgId int64) (info []*models.WarehousingInfo, err error) {
  8580. db := XTReadDB().Model(&info).Where("status=1 and is_check=1")
  8581. if startTime > 0 {
  8582. db = db.Where("ctime>=?", startTime)
  8583. }
  8584. if endTime > 0 {
  8585. db = db.Where("ctime<=?", endTime)
  8586. }
  8587. if orgId > 0 {
  8588. db = db.Where("org_id = ?", orgId)
  8589. }
  8590. err = db.Find(&info).Error
  8591. return info, err
  8592. }
  8593. func GetGoodWarhouseOutList(startTime int64, endTime int64, orgId int64) (outinfo []*models.WarehouseOutInfo, err error) {
  8594. db := XTReadDB().Model(&outinfo).Where("status=1 and is_check =1")
  8595. if startTime > 0 {
  8596. db = db.Where("ctime>=?", startTime)
  8597. }
  8598. if endTime > 0 {
  8599. db = db.Where("ctime<=?", endTime)
  8600. }
  8601. if orgId > 0 {
  8602. db = db.Where("org_id = ?", orgId)
  8603. }
  8604. err = db.Find(&outinfo).Error
  8605. return outinfo, err
  8606. }
  8607. func GetGoodStockFlowProList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8608. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 10")
  8609. if startTime > 0 {
  8610. db = db.Where("ctime>=?", startTime)
  8611. }
  8612. if endTime > 0 {
  8613. db = db.Where("ctime<=?", endTime)
  8614. }
  8615. if orgId > 0 {
  8616. db = db.Where("user_org_id = ?", orgId)
  8617. }
  8618. err = db.Find(&flow).Error
  8619. return flow, err
  8620. }
  8621. func GetGoodStockFlowNoProList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8622. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 11")
  8623. if startTime > 0 {
  8624. db = db.Where("ctime>=?", startTime)
  8625. }
  8626. if endTime > 0 {
  8627. db = db.Where("ctime<=?", endTime)
  8628. }
  8629. if orgId > 0 {
  8630. db = db.Where("user_org_id = ?", orgId)
  8631. }
  8632. err = db.Find(&flow).Error
  8633. return flow, err
  8634. }
  8635. func GetCancelStockFlowList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8636. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 7")
  8637. if startTime > 0 {
  8638. db = db.Where("ctime>=?", startTime)
  8639. }
  8640. if endTime > 0 {
  8641. db = db.Where("ctime<=?", endTime)
  8642. }
  8643. if orgId > 0 {
  8644. db = db.Where("user_org_id = ?", orgId)
  8645. }
  8646. err = db.Find(&flow).Error
  8647. return flow, err
  8648. }
  8649. func GetLastMonthCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8650. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 1 or consumable_type = 10)")
  8651. if startTime > 0 {
  8652. db = db.Where("ctime <?", startTime)
  8653. }
  8654. if orgId > 0 {
  8655. db = db.Where("user_org_id = ?", orgId)
  8656. }
  8657. err = db.Find(&flow).Error
  8658. return flow, err
  8659. }
  8660. func GetLastMonthOverCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8661. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 2 or consumable_type=3 or consumable_type=11)")
  8662. if startTime > 0 {
  8663. db = db.Where("ctime <?", startTime)
  8664. }
  8665. if orgId > 0 {
  8666. db = db.Where("user_org_id = ?", orgId)
  8667. }
  8668. err = db.Find(&flow).Error
  8669. return flow, err
  8670. }
  8671. func GetLastCancelMonthCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8672. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 7)")
  8673. if startTime > 0 {
  8674. db = db.Where("ctime <?", startTime)
  8675. }
  8676. if orgId > 0 {
  8677. db = db.Where("user_org_id = ?", orgId)
  8678. }
  8679. err = db.Find(&flow).Error
  8680. return flow, err
  8681. }
  8682. func GetDrugCodeOpen(user_org_id int64) (models.XtDrugCodeOpen, error) {
  8683. drugCodeOpen := models.XtDrugCodeOpen{}
  8684. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugCodeOpen).Error
  8685. return drugCodeOpen, err
  8686. }
  8687. func CreateDrugOpen(open models.XtDrugCodeOpen) error {
  8688. err := XTWriteDB().Create(&open).Error
  8689. return err
  8690. }
  8691. func SaveDrugOpen(open models.XtDrugCodeOpen) error {
  8692. err := XTWriteDB().Save(&open).Error
  8693. return err
  8694. }
  8695. func GetHisDrugCodeQuery(orgId int64, start_time int64, end_time int64, limit int64, page int64, is_sale int64) (list []*models.HisDoctorAdviceInfo, total int64, err error) {
  8696. db := XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("status=1")
  8697. if orgId > 0 {
  8698. db = db.Where("user_org_id=?", orgId)
  8699. }
  8700. if start_time > 0 {
  8701. db = db.Where("advice_date >= ?", start_time)
  8702. }
  8703. if end_time > 0 {
  8704. db = db.Where("advice_date<=?", end_time)
  8705. }
  8706. offset := (page - 1) * limit
  8707. if is_sale == 1 {
  8708. db = db.Where("is_upload = 1")
  8709. }
  8710. if is_sale == 2 {
  8711. db = db.Where("is_upload = 2 or is_upload =0")
  8712. }
  8713. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  8714. return list, total, err
  8715. }
  8716. func GetDrugWarehouseOutInfoByDrugCode(drug_id int64, patient_id int64, advice_date int64, user_org_id int64) (outinfo []*models.XtDrugWarehouseOutInfo, err error) {
  8717. err = XTReadDB().Where("drug_id = ? and patient_id = ? and sys_record_time =? and org_id = ? and status =1", drug_id, patient_id, advice_date, user_org_id).Find(&outinfo).Error
  8718. return outinfo, err
  8719. }
  8720. func GetNewGoodWarehouseOutInfo(good_id int64, user_org_id int64, warehouse_out_id int64, tx *gorm.DB) (info []*models.WarehouseOutInfo, err error) {
  8721. err = tx.Where("good_id = ? and org_id = ? and warehouse_out_id = ? and status= 1", good_id, user_org_id, warehouse_out_id).Find(&info).Error
  8722. if err != gorm.ErrRecordNotFound {
  8723. if err != nil {
  8724. tx.Rollback()
  8725. return info, err
  8726. }
  8727. }
  8728. return info, err
  8729. }
  8730. func GetInitGoodFlow(user_org_id int64, good_id int64, warehousing_detail_id int64, tx *gorm.DB) (list []*models.VmStockFlow, err error) {
  8731. err = tx.Where("user_org_id = ? and good_id = ? and warehousing_detail_id = ? and status=1", user_org_id, good_id, warehousing_detail_id).Find(&list).Error
  8732. if err != gorm.ErrRecordNotFound {
  8733. if err != nil {
  8734. tx.Rollback()
  8735. return list, err
  8736. }
  8737. }
  8738. return list, err
  8739. }
  8740. func GetNewDrugFlowList(drug_id int64, user_org_id int64, warehousing_detail_id int64, tx *gorm.DB) (flow []*models.DrugFlow, err error) {
  8741. err = tx.Where("drug_id = ? and user_org_id =? and warehousing_detail_id = ? and status=1", drug_id, user_org_id, warehousing_detail_id).Find(&flow).Error
  8742. if err != gorm.ErrRecordNotFound {
  8743. if err != nil {
  8744. tx.Rollback()
  8745. return flow, err
  8746. }
  8747. }
  8748. return flow, err
  8749. }
  8750. func GetNewPatientAdviceList(patient_id int64, start_time int64, end_time int64, user_org_id int64) (advice []*models.DoctorAdvice, err error) {
  8751. db := XTReadDB().Model(&advice).Where("status=1 and advice_type =2")
  8752. if patient_id > 0 {
  8753. db = db.Where("patient_id = ?", patient_id)
  8754. }
  8755. if start_time > 0 {
  8756. db = db.Where("advice_date>=?", start_time)
  8757. }
  8758. if end_time > 0 {
  8759. db = db.Where("advice_date<=?", end_time)
  8760. }
  8761. if user_org_id > 0 {
  8762. db = db.Where("user_org_id = ?", user_org_id)
  8763. }
  8764. err = db.Find(&advice).Error
  8765. return advice, err
  8766. }
  8767. func GetPatientAdviceListDetail(patient_id int64, start_time int64, end_time int64, user_org_id int64, advice_name string) (advice []*models.DoctorAdvice, err error) {
  8768. db := XTReadDB().Model(&advice).Where("status = 1")
  8769. if patient_id > 0 {
  8770. db = db.Where("patient_id = ?", patient_id)
  8771. }
  8772. if start_time > 0 {
  8773. db = db.Where("advice_date>=?", start_time)
  8774. }
  8775. if end_time > 0 {
  8776. db = db.Where("advice_date<=?", end_time)
  8777. }
  8778. if user_org_id > 0 {
  8779. db = db.Where("user_org_id = ?", user_org_id)
  8780. }
  8781. if len(advice_name) > 0 {
  8782. db = db.Where("advice_name =?", advice_name)
  8783. }
  8784. err = db.Order("advice_date asc").Find(&advice).Error
  8785. return advice, err
  8786. }