stock_service.go 463KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "math"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "XT_New/models"
  10. "github.com/jinzhu/gorm"
  11. )
  12. func AddSigleDealer(dealer *models.Dealer) (error, *models.Dealer) {
  13. err := writeDb.Create(&dealer).Error
  14. return err, dealer
  15. }
  16. func ModifyDealer(dealer *models.Dealer) error {
  17. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", dealer.ID).Updates(map[string]interface{}{
  18. "dealer_name": dealer.DealerName,
  19. "contact": dealer.Contact,
  20. "contact_phone": dealer.ContactPhone,
  21. "platform_number": dealer.PlatformNumber,
  22. "email": dealer.Email,
  23. "contact_address": dealer.ContactAddress,
  24. "modifier": dealer.Modifier,
  25. "mtime": time.Now().Unix(),
  26. "remark": dealer.Remark,
  27. "wubi": dealer.WuBi,
  28. "pinyin": dealer.PinYin,
  29. }).Error
  30. return err
  31. }
  32. func FindAllDealerTotal(orgId int64) (total int64) {
  33. db := readDb.Model(&models.Dealer{})
  34. db = db.Where("org_id = ? ", orgId).Count(&total)
  35. return
  36. }
  37. // 获取经销商的名称
  38. func FindAllDealerName(orgId int64) (tmp []*models.Dealer, err error) {
  39. err = readDb.Model(&models.Dealer{}).Where("org_id = ? and status = 1", orgId).Find(&tmp).Error
  40. return
  41. }
  42. func FindAllDealerList(orgId int64, page int64, limit, key int64) (list []*models.Dealer, total int64, err error) {
  43. offset := (page - 1) * limit
  44. db := readDb.Model(&models.Dealer{})
  45. db = db.Where("org_id = ? AND status = 1", orgId)
  46. if key != 0 {
  47. db = db.Where("id = ? ", key)
  48. offset = 0
  49. limit = 10
  50. }
  51. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  52. return
  53. }
  54. func DeleteDealerById(id int64) error {
  55. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  56. return err
  57. }
  58. func FindDealerById(id int64) (*models.Dealer, error) {
  59. dealer := &models.Dealer{}
  60. err := readDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  61. return dealer, err
  62. }
  63. func AddSigleManufacturer(manufacturer *models.Manufacturer) (error, *models.Manufacturer) {
  64. err := writeDb.Create(&manufacturer).Error
  65. return err, manufacturer
  66. }
  67. func ModifyManufacturer(manufacturer *models.Manufacturer) error {
  68. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", manufacturer.ID).Updates(map[string]interface{}{
  69. "manufacturer_name": manufacturer.ManufacturerName,
  70. "contact": manufacturer.Contact,
  71. "contact_phone": manufacturer.ContactPhone,
  72. "platform_number": manufacturer.PlatformNumber,
  73. "email": manufacturer.Email,
  74. "contact_address": manufacturer.ContactAddress,
  75. "modifier": manufacturer.Modifier,
  76. "mtime": time.Now().Unix(),
  77. "remark": manufacturer.Remark,
  78. "wubi": manufacturer.WuBi,
  79. "pinyin": manufacturer.PinYin,
  80. }).Error
  81. return err
  82. }
  83. // 获取生产厂商名称
  84. func FindAllManufacturerName(orgid int64) (tmp []*models.Manufacturer, err error) {
  85. err = readDb.Model(&models.Manufacturer{}).Where("org_id = ? and status = 1", orgid).Find(&tmp).Error
  86. return
  87. }
  88. func FindAllManufacturerList(orgId int64, page int64, limit, key int64) (list []*models.Manufacturer, total int64, err error) {
  89. offset := (page - 1) * limit
  90. db := readDb.Model(&models.Manufacturer{})
  91. db = db.Where("org_id = ? AND status = 1", orgId)
  92. if key != 0 {
  93. db = db.Where("id = ?", key)
  94. offset = 0
  95. limit = 10
  96. }
  97. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  98. return
  99. }
  100. func DeleteManufacturerById(id int64) error {
  101. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  102. return err
  103. }
  104. func FindManufacturerById(id int64) (*models.Manufacturer, error) {
  105. dealer := &models.Manufacturer{}
  106. err := readDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  107. return dealer, err
  108. }
  109. func FindManufacturerTotal(orgId int64) (total int64) {
  110. db := readDb.Model(&models.Manufacturer{})
  111. db = db.Where("org_id = ?", orgId).Count(&total)
  112. return
  113. }
  114. func FindAllDealer(orgId int64) (dealers []*models.Dealer, err error) {
  115. err = readDb.Model(&models.Dealer{}).Where("org_id = ? AND status = 1", orgId).Find(&dealers).Error
  116. return dealers, err
  117. }
  118. func FindAllManufacturer(orgId int64) (manufacturer []*models.Manufacturer, err error) {
  119. err = readDb.Model(&models.Manufacturer{}).Where("org_id = ? AND status = 1", orgId).Find(&manufacturer).Error
  120. return manufacturer, err
  121. }
  122. func FindAllGoodTypeTotal(orgId int64) (total int64) {
  123. db := readDb.Model(&models.GoodsType{})
  124. db = db.Where("org_id = ?", orgId).Count(&total)
  125. return
  126. }
  127. func GetGoodsTypeIsExist(orgid int64) (*models.GoodsType, error) {
  128. goodsType := models.GoodsType{}
  129. err := XTReadDB().Where("org_id = ? and stock_attribute = 2 and status =1", orgid).Find(&goodsType).Error
  130. if err == gorm.ErrRecordNotFound {
  131. return nil, err
  132. }
  133. if err != nil {
  134. return nil, err
  135. }
  136. return &goodsType, nil
  137. }
  138. func GetGoodsTypeIsExistOne(orgid int64) (*models.GoodsType, error) {
  139. goodsType := models.GoodsType{}
  140. err := XTReadDB().Where("org_id = ? and stock_attribute = 3 and status = 1", orgid).Find(&goodsType).Error
  141. if err == gorm.ErrRecordNotFound {
  142. return nil, err
  143. }
  144. if err != nil {
  145. return nil, err
  146. }
  147. return &goodsType, nil
  148. }
  149. func AddSigleGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  150. err := writeDb.Create(&goodType).Error
  151. return err, goodType
  152. }
  153. func ModifyGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  154. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", goodType.ID).Updates(map[string]interface{}{
  155. "modifier": goodType.Modifier,
  156. "mtime": time.Now().Unix(),
  157. "remark": goodType.Remark,
  158. "type_name": goodType.TypeName,
  159. "out_stock": goodType.OutStock,
  160. "stock_attribute": goodType.StockAttribute,
  161. }).Error
  162. return err, goodType
  163. }
  164. func FindAllGoodTypeList(orgId int64, page int64, limit int64, keyword string) (list []*models.GoodsType, total int64, err error) {
  165. offset := (page - 1) * limit
  166. db := readDb.Model(&models.GoodsType{})
  167. db = db.Where("org_id=? AND status = 1", orgId)
  168. if len(keyword) > 0 {
  169. likeKey := "%" + keyword + "%"
  170. db = db.Where("type_code LIKE ? OR type_name LIKE ?", likeKey, likeKey)
  171. }
  172. err = db.Count(&total).Offset(offset).Limit(limit).Order("type desc, number desc,ctime desc").Find(&list).Error
  173. return
  174. }
  175. func DeleteGoodTypeById(id int64, operateId int64) error {
  176. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  177. writeDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId})
  178. return err
  179. }
  180. func FindGoodTypeById(id int64) (*models.GoodsType, error) {
  181. goodType := &models.GoodsType{}
  182. err := readDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).First(&goodType).Error
  183. return goodType, err
  184. }
  185. func FindAllGoodInfoTotal(orgId int64) (total int64) {
  186. db := readDb.Model(&models.GoodInfo{})
  187. db = db.Where("org_id = ?", orgId).Count(&total)
  188. return
  189. }
  190. func AddSigleGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  191. err := writeDb.Create(&goodInfo).Error
  192. return err, goodInfo
  193. }
  194. func ModifyGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  195. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", goodInfo.ID).Updates(map[string]interface{}{
  196. "good_type_id": goodInfo.GoodTypeId,
  197. "modifier": goodInfo.Modifier,
  198. "mtime": time.Now().Unix(),
  199. "remark": goodInfo.Remark,
  200. "specification_name": goodInfo.SpecificationName,
  201. "good_unit": goodInfo.GoodUnit,
  202. "buy_price": goodInfo.BuyPrice,
  203. "sell_price": goodInfo.SellPrice,
  204. "manufacturer": goodInfo.Manufacturer,
  205. "dealer": goodInfo.Dealer,
  206. "expiry_date_warn_day_count": goodInfo.ExpiryDateWarnDayCount,
  207. "stock_warn_count": goodInfo.StockWarnCount,
  208. "is_reuse": goodInfo.IsReuse,
  209. "good_name": goodInfo.GoodName,
  210. "pinyin": goodInfo.Pinyin,
  211. "wubi": goodInfo.Wubi,
  212. "good_kind": goodInfo.GoodKind,
  213. "medical_insurance_level": goodInfo.MedicalInsuranceLevel,
  214. "retail_price": goodInfo.PackingPrice,
  215. "medical_insurance_number": goodInfo.MedicalInsuranceNumber,
  216. "is_special_diseases": goodInfo.IsSpecialDiseases,
  217. "is_record": goodInfo.IsRecord,
  218. "statistics_category": goodInfo.StatisticsCategory,
  219. "good_status": goodInfo.GoodStatus,
  220. "default_count": goodInfo.DefaultCount,
  221. "sign": goodInfo.Sign,
  222. "is_default": goodInfo.IsDefault,
  223. "is_charge_use": goodInfo.IsChargeUse,
  224. "is_charge_predict": goodInfo.IsChargePredict,
  225. "is_statistics_work": goodInfo.IsStatisticsWork,
  226. "sort": goodInfo.Sort,
  227. "is_doctor_use": goodInfo.IsDoctorUse,
  228. "agent": goodInfo.Agent,
  229. "good_number": goodInfo.GoodNumber,
  230. "social_security_directory_code": goodInfo.SocialSecurityDirectoryCode,
  231. "special_medical": goodInfo.SpecialMedical,
  232. "production_type": goodInfo.ProductionType,
  233. "min_number": goodInfo.MinNumber,
  234. "packing_unit": goodInfo.PackingUnit,
  235. "packing_price": goodInfo.PackingPrice,
  236. "default_count_unit": goodInfo.DefaultCountUnit,
  237. "min_unit": goodInfo.MinUnit,
  238. "register_number": goodInfo.RegisterNumber,
  239. "provinces_code": goodInfo.ProvincesCode,
  240. "is_user": goodInfo.IsUser,
  241. "number": goodInfo.Number,
  242. "is_warehouse": goodInfo.IsWarehouse,
  243. "bbx01": goodInfo.Bbx01,
  244. "bby01": goodInfo.Bby01,
  245. "zuobiao_id": goodInfo.Bbx01,
  246. "first_letter": goodInfo.FirstLetter,
  247. "is_show": goodInfo.IsShow,
  248. "is_zero_flag": goodInfo.IsZeroFlag,
  249. "split_of_number": goodInfo.SplitOfNumber,
  250. "register_number_name": goodInfo.RegisterNumberName,
  251. }).Error
  252. writeDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  253. writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  254. writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  255. writeDb.Model(&models.DialysisBeforePrepare{}).Where("good_id = ? AND user_org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  256. writeDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  257. writeDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  258. return err, goodInfo
  259. }
  260. 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) {
  261. offset := (page - 1) * limit
  262. db := readDb.Model(&models.GoodInfo{})
  263. db = db.Where("org_id = ? AND status = 1", orgId)
  264. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  265. if len(keyword) > 0 {
  266. likeKey := "%" + keyword + "%"
  267. db = db.Where("good_code LIKE ? OR good_name LIKE ?", likeKey, likeKey)
  268. if is_use > 0 {
  269. if is_use == 2 {
  270. db = db.Where("find_in_set('停用',good_status) = 0")
  271. } else {
  272. db = db.Where("find_in_set('停用',good_status) > 0")
  273. }
  274. }
  275. if is_charge > 0 {
  276. if is_charge == 1 {
  277. db = db.Where("find_in_set('收费',good_status) > 0")
  278. } else {
  279. db = db.Where("find_in_set('收费',good_status) = 0")
  280. }
  281. }
  282. if good_kind > 0 {
  283. db = db.Where(" good_kind = ?", good_kind)
  284. }
  285. if is_mark > 0 {
  286. db = db.Where("is_mark = ?", is_mark)
  287. }
  288. if manufacturer > 0 {
  289. db = db.Where("manufacturer = ?", manufacturer)
  290. }
  291. if is_type > 0 {
  292. db = db.Where("good_type_id = ?", is_type)
  293. }
  294. } else {
  295. if is_use > 0 {
  296. if is_use == 2 {
  297. db = db.Where("find_in_set('停用',good_status) = 0")
  298. } else {
  299. db = db.Where("find_in_set('停用',good_status) > 0")
  300. }
  301. }
  302. if is_charge > 0 {
  303. if is_charge == 1 {
  304. db = db.Where("find_in_set('收费',good_status) > 0")
  305. } else {
  306. db = db.Where("find_in_set('收费',good_status) = 0")
  307. }
  308. }
  309. if good_kind > 0 {
  310. db = db.Where(" good_kind = ?", good_kind)
  311. }
  312. if is_mark > 0 {
  313. db = db.Where("is_mark = ?", is_mark)
  314. }
  315. if manufacturer > 0 {
  316. db = db.Where("manufacturer = ?", manufacturer)
  317. }
  318. if is_type > 0 {
  319. db = db.Where("good_type_id = ?", is_type)
  320. }
  321. }
  322. err = db.Count(&total).Offset(offset).Limit(limit).Order("id desc").Find(&list).Error
  323. return
  324. }
  325. func DeleteGoodInfoById(id int64, operateId int64) error {
  326. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  327. return err
  328. }
  329. func FindGoodInfoByGoodId(id int64) (list []*models.GoodInfo, err error) {
  330. err = readDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  331. return list, err
  332. }
  333. func FindStockOutDetailGoodInfoByGoodId(id int64) (list []*models.AutomaticReduceDetail, err error) {
  334. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  335. return list, err
  336. }
  337. func FindGoodInfoById(id int64) (*models.GoodInfo, error) {
  338. goodInfo := &models.GoodInfo{}
  339. err := readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  340. return goodInfo, err
  341. }
  342. func FindAllGoodType(org_id int64) (goodType []*models.GoodsType, err error) {
  343. err = XTReadDB().Model(&models.GoodsType{}).Where("org_id = ? AND status = 1", org_id).Find(&goodType).Error
  344. return goodType, err
  345. }
  346. func FindAllGoodTypeOne(org_id int64) (goodType []*models.GoodsType, err error) {
  347. err = readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 and out_stock = 1", org_id).Find(&goodType).Error
  348. return goodType, err
  349. }
  350. func FindAllGoodInfo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  351. 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
  352. return goodInfo, err
  353. }
  354. func FindAllGoodInfoTwo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  355. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id).Find(&goodInfo).Error
  356. return goodInfo, err
  357. }
  358. func FindAllGoodInfoThree(org_id int64) (goodInfo []*models.XtGoodInformationFourty, err error) {
  359. err = XTReadDB().Model(&models.XtGoodInformationFourty{}).Where("org_id = ? AND status = 1 and find_in_set('停用',good_status) = 0", org_id).Find(&goodInfo).Error
  360. return goodInfo, err
  361. }
  362. func FindAllWarehouseTotal(org_id int64) (total int64, err error) {
  363. err = readDb.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  364. return total, err
  365. }
  366. func FindNewAllWarehouseTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  367. err = tx.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  368. if err != gorm.ErrRecordNotFound {
  369. if err != nil {
  370. tx.Rollback()
  371. return total, err
  372. }
  373. }
  374. return total, err
  375. }
  376. func FindAllWarehouseTotalOne(org_id int64) (total int64, err error) {
  377. err = readDb.Model(&models.XtDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  378. return total, err
  379. }
  380. func FindNewAllWarehouseTotalOne(org_id int64, tx *gorm.DB) (total int64, err error) {
  381. err = tx.Model(&models.XtDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  382. if err != gorm.ErrRecordNotFound {
  383. if err != nil {
  384. tx.Rollback()
  385. return total, err
  386. }
  387. }
  388. return total, err
  389. }
  390. func AddSigleWarehouse(warehouse *models.Warehousing) error {
  391. err := writeDb.Create(&warehouse).Error
  392. return err
  393. }
  394. func AddNewSigleWarehouse(warehouse *models.Warehousing, tx *gorm.DB) error {
  395. err := writeDb.Create(&warehouse).Error
  396. if err != nil {
  397. tx.Rollback()
  398. return err
  399. }
  400. return err
  401. }
  402. func UpdateWarehouse(id int64, storehouse_id int64) error {
  403. err := XTWriteDB().Model(&models.Warehousing{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  404. return err
  405. }
  406. func UpdateNewWarehouse(id int64, storehouse_id int64, tx *gorm.DB) error {
  407. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  408. if err != nil {
  409. tx.Rollback()
  410. return err
  411. }
  412. return err
  413. }
  414. func AddSigleDrugWarehouse(warehouse *models.DrugWarehouse) error {
  415. err := writeDb.Create(&warehouse).Error
  416. return err
  417. }
  418. func AddNewSigleDrugWarehouse(warehouse *models.DrugWarehouse, tx *gorm.DB) error {
  419. err := tx.Create(&warehouse).Error
  420. if err != nil {
  421. tx.Rollback()
  422. return err
  423. }
  424. return err
  425. }
  426. func UpdateSigleDrugWarehouse(id int64, storehosue_id int64) error {
  427. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehosue_id}).Error
  428. return err
  429. }
  430. func UpdateNewSigleDrugWarehouse(id int64, storehosue_id int64, tx *gorm.DB) error {
  431. err := tx.Model(&models.DrugWarehouse{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehosue_id}).Error
  432. if err != nil {
  433. tx.Rollback()
  434. return err
  435. }
  436. return err
  437. }
  438. func AddSigleSalesReturn(salesReturn *models.SalesReturn) error {
  439. err := writeDb.Create(&salesReturn).Error
  440. return err
  441. }
  442. func AddSigleDrugSalesReturn(salesReturn *models.DrugSalesReturn) error {
  443. err := writeDb.Create(&salesReturn).Error
  444. return err
  445. }
  446. func UpDateGoodReturnStatus(ids []string) {
  447. writeDb.Model(&models.WarehousingInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_return": 1})
  448. }
  449. func UpDateCanCelStockStatus(ids []string) {
  450. writeDb.Model(&models.WarehouseOutInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_cancel": 1})
  451. }
  452. func CreateWarehousingInfo(warehousingInfo []*models.WarehousingInfo) (err error) {
  453. if len(warehousingInfo) > 0 {
  454. utx := writeDb.Begin()
  455. if len(warehousingInfo) > 0 {
  456. 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 "
  457. insertParams := make([]string, 0)
  458. insertData := make([]interface{}, 0)
  459. for _, info := range warehousingInfo {
  460. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  461. insertData = append(insertData, info.WarehousingId)
  462. insertData = append(insertData, info.GoodId)
  463. insertData = append(insertData, info.GoodTypeId)
  464. insertData = append(insertData, info.Number)
  465. insertData = append(insertData, info.ProductDate)
  466. insertData = append(insertData, info.ExpiryDate)
  467. insertData = append(insertData, info.WarehousingCount)
  468. insertData = append(insertData, info.Price)
  469. insertData = append(insertData, info.TotalPrice)
  470. insertData = append(insertData, info.Dealer)
  471. insertData = append(insertData, info.Manufacturer)
  472. insertData = append(insertData, info.Remark)
  473. insertData = append(insertData, info.Ctime)
  474. insertData = append(insertData, info.Mtime)
  475. insertData = append(insertData, info.Status)
  476. insertData = append(insertData, info.OrgId)
  477. insertData = append(insertData, info.WarehousingOrder)
  478. insertData = append(insertData, info.Type)
  479. insertData = append(insertData, info.WarehousingCount)
  480. insertData = append(insertData, info.LicenseNumber)
  481. insertData = append(insertData, info.PackingPrice)
  482. insertData = append(insertData, info.StorehouseId)
  483. insertData = append(insertData, info.GoodCode)
  484. }
  485. thisSQL += strings.Join(insertParams, ", ")
  486. err = utx.Exec(thisSQL, insertData...).Error
  487. if err != nil {
  488. utx.Rollback()
  489. return
  490. }
  491. }
  492. utx.Commit()
  493. }
  494. return
  495. }
  496. func CreateNewWarehousingInfo(warehousingInfo []*models.WarehousingInfo, tx *gorm.DB) (err error) {
  497. if len(warehousingInfo) > 0 {
  498. if len(warehousingInfo) > 0 {
  499. 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,split_of_number) VALUES "
  500. insertParams := make([]string, 0)
  501. insertData := make([]interface{}, 0)
  502. for _, info := range warehousingInfo {
  503. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  504. insertData = append(insertData, info.WarehousingId)
  505. insertData = append(insertData, info.GoodId)
  506. insertData = append(insertData, info.GoodTypeId)
  507. insertData = append(insertData, info.Number)
  508. insertData = append(insertData, info.ProductDate)
  509. insertData = append(insertData, info.ExpiryDate)
  510. insertData = append(insertData, info.WarehousingCount)
  511. insertData = append(insertData, info.Price)
  512. insertData = append(insertData, info.TotalPrice)
  513. insertData = append(insertData, info.Dealer)
  514. insertData = append(insertData, info.Manufacturer)
  515. insertData = append(insertData, info.Remark)
  516. insertData = append(insertData, info.Ctime)
  517. insertData = append(insertData, info.Mtime)
  518. insertData = append(insertData, info.Status)
  519. insertData = append(insertData, info.OrgId)
  520. insertData = append(insertData, info.WarehousingOrder)
  521. insertData = append(insertData, info.Type)
  522. insertData = append(insertData, info.WarehousingCount)
  523. insertData = append(insertData, info.LicenseNumber)
  524. insertData = append(insertData, info.PackingPrice)
  525. insertData = append(insertData, info.StorehouseId)
  526. insertData = append(insertData, info.GoodCode)
  527. insertData = append(insertData, info.SplitOfNumber)
  528. }
  529. thisSQL += strings.Join(insertParams, ", ")
  530. err = tx.Exec(thisSQL, insertData...).Error
  531. if err != nil {
  532. tx.Rollback()
  533. return
  534. }
  535. }
  536. }
  537. return
  538. }
  539. func CreateDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  540. if len(warehousingInfo) > 0 {
  541. utx := writeDb.Begin()
  542. if len(warehousingInfo) > 0 {
  543. 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 "
  544. insertParams := make([]string, 0)
  545. insertData := make([]interface{}, 0)
  546. for _, info := range warehousingInfo {
  547. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  548. insertData = append(insertData, info.WarehousingId)
  549. insertData = append(insertData, info.DrugId)
  550. insertData = append(insertData, info.Number)
  551. insertData = append(insertData, info.ProductDate)
  552. insertData = append(insertData, info.ExpiryDate)
  553. insertData = append(insertData, info.WarehousingCount)
  554. insertData = append(insertData, info.Price)
  555. insertData = append(insertData, info.TotalPrice)
  556. insertData = append(insertData, info.Dealer)
  557. insertData = append(insertData, info.Manufacturer)
  558. insertData = append(insertData, info.Remark)
  559. insertData = append(insertData, info.Ctime)
  560. insertData = append(insertData, info.Mtime)
  561. insertData = append(insertData, info.Status)
  562. insertData = append(insertData, info.OrgId)
  563. insertData = append(insertData, info.WarehousingOrder)
  564. insertData = append(insertData, info.Type)
  565. insertData = append(insertData, info.RetailPrice)
  566. insertData = append(insertData, info.RetailTotalPrice)
  567. insertData = append(insertData, info.StockMaxNumber)
  568. insertData = append(insertData, info.MaxUnit)
  569. insertData = append(insertData, info.MinUnit)
  570. insertData = append(insertData, info.BatchNumber)
  571. insertData = append(insertData, info.StockMinNumber)
  572. insertData = append(insertData, info.StorehouseId)
  573. }
  574. thisSQL += strings.Join(insertParams, ", ")
  575. err = utx.Exec(thisSQL, insertData...).Error
  576. if err != nil {
  577. utx.Rollback()
  578. return
  579. }
  580. }
  581. utx.Commit()
  582. }
  583. return
  584. }
  585. func CreateNewDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo, tx *gorm.DB) (err error) {
  586. if len(warehousingInfo) > 0 {
  587. if len(warehousingInfo) > 0 {
  588. 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 "
  589. insertParams := make([]string, 0)
  590. insertData := make([]interface{}, 0)
  591. for _, info := range warehousingInfo {
  592. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  593. insertData = append(insertData, info.WarehousingId)
  594. insertData = append(insertData, info.DrugId)
  595. insertData = append(insertData, info.Number)
  596. insertData = append(insertData, info.ProductDate)
  597. insertData = append(insertData, info.ExpiryDate)
  598. insertData = append(insertData, info.WarehousingCount)
  599. insertData = append(insertData, info.Price)
  600. insertData = append(insertData, info.TotalPrice)
  601. insertData = append(insertData, info.Dealer)
  602. insertData = append(insertData, info.Manufacturer)
  603. insertData = append(insertData, info.Remark)
  604. insertData = append(insertData, info.Ctime)
  605. insertData = append(insertData, info.Mtime)
  606. insertData = append(insertData, info.Status)
  607. insertData = append(insertData, info.OrgId)
  608. insertData = append(insertData, info.WarehousingOrder)
  609. insertData = append(insertData, info.Type)
  610. insertData = append(insertData, info.RetailPrice)
  611. insertData = append(insertData, info.RetailTotalPrice)
  612. insertData = append(insertData, info.StockMaxNumber)
  613. insertData = append(insertData, info.MaxUnit)
  614. insertData = append(insertData, info.MinUnit)
  615. insertData = append(insertData, info.BatchNumber)
  616. insertData = append(insertData, info.StockMinNumber)
  617. insertData = append(insertData, info.StorehouseId)
  618. insertData = append(insertData, info.DrugCode)
  619. }
  620. thisSQL += strings.Join(insertParams, ", ")
  621. err = tx.Exec(thisSQL, insertData...).Error
  622. if err != nil {
  623. tx.Rollback()
  624. return
  625. }
  626. }
  627. }
  628. return
  629. }
  630. func CreateDrugWarehousingInfoSix(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  631. if len(warehousingInfo) > 0 {
  632. utx := writeDb.Begin()
  633. if len(warehousingInfo) > 0 {
  634. 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 "
  635. insertParams := make([]string, 0)
  636. insertData := make([]interface{}, 0)
  637. for _, info := range warehousingInfo {
  638. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  639. insertData = append(insertData, info.WarehousingId)
  640. insertData = append(insertData, info.DrugId)
  641. insertData = append(insertData, info.Number)
  642. insertData = append(insertData, info.ProductDate)
  643. insertData = append(insertData, info.ExpiryDate)
  644. insertData = append(insertData, info.WarehousingCount)
  645. insertData = append(insertData, info.Price)
  646. insertData = append(insertData, info.TotalPrice)
  647. insertData = append(insertData, info.Dealer)
  648. insertData = append(insertData, info.Manufacturer)
  649. insertData = append(insertData, info.Remark)
  650. insertData = append(insertData, info.Ctime)
  651. insertData = append(insertData, info.Mtime)
  652. insertData = append(insertData, info.Status)
  653. insertData = append(insertData, info.OrgId)
  654. insertData = append(insertData, info.WarehousingOrder)
  655. insertData = append(insertData, info.Type)
  656. insertData = append(insertData, info.RetailPrice)
  657. insertData = append(insertData, info.RetailTotalPrice)
  658. insertData = append(insertData, info.StockMaxNumber)
  659. insertData = append(insertData, info.MaxUnit)
  660. insertData = append(insertData, info.MinUnit)
  661. insertData = append(insertData, info.BatchNumber)
  662. insertData = append(insertData, info.StockMinNumber)
  663. insertData = append(insertData, info.SupplyWarehouseId)
  664. insertData = append(insertData, info.SupplyWarehouseDetailInfo)
  665. insertData = append(insertData, info.StorehouseId)
  666. insertData = append(insertData, info.IsCheck)
  667. }
  668. thisSQL += strings.Join(insertParams, ", ")
  669. err = utx.Exec(thisSQL, insertData...).Error
  670. if err != nil {
  671. utx.Rollback()
  672. return
  673. }
  674. }
  675. utx.Commit()
  676. }
  677. return
  678. }
  679. func UpdateDrugInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  680. var total float64
  681. var min_number int64
  682. for _, item := range warehousingInfo {
  683. //查询刚入库耗材的库存
  684. count, _ := FindDrugWaresingCount(item.DrugId, item.OrgId)
  685. //更新药品库的总量
  686. info, _ := FindeDrugInfo(item.DrugId)
  687. min_number = count.StockMaxNumber * info.MinNumber
  688. str := strconv.FormatInt(min_number, 10)
  689. minFloat, _ := strconv.ParseFloat(str, 64)
  690. total = info.Total + minFloat
  691. lib := models.BaseDrugLib{
  692. Total: total,
  693. }
  694. UpdateBaseDrug(&lib, item.ID)
  695. }
  696. return err
  697. }
  698. func FindDrugWaresingCount(drug_id int64, orgid int64) (models.XtDrugWarehouseInfo, error) {
  699. info := models.XtDrugWarehouseInfo{}
  700. db := XTReadDB().Table("xt_drug_warehouse_info").Where("status = 1")
  701. err = db.Select("stock_max_number,drug_id").Where("drug_id = ? and org_id = ? and status =1", drug_id, orgid).Last(&info).Error
  702. return info, err
  703. }
  704. func FindeDrugInfo(drug_id int64) (models.BaseDrugLib, error) {
  705. lib := models.BaseDrugLib{}
  706. err := XTReadDB().Model(&lib).Where("id = ?", drug_id).Find(&lib).Error
  707. return lib, err
  708. }
  709. func UpdateBaseDrug(lib *models.BaseDrugLib, id int64) error {
  710. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"total": lib.Total}).Error
  711. return err
  712. }
  713. func UpdateBaseDrugOne(lib models.BaseDrugLib, id int64) error {
  714. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"retail_price": lib.RetailPrice, "last_price": lib.LastPrice}).Error
  715. return err
  716. }
  717. func FindeLastWarehouseInfo(id int64) (models.DrugWarehouseInfo, error) {
  718. info := models.DrugWarehouseInfo{}
  719. err := XTReadDB().Model(&info).Where("id=? and status = 1", id).Find(&info).Error
  720. return info, err
  721. }
  722. func GetLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  723. out := models.DrugWarehouseOut{}
  724. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  725. return out, err
  726. }
  727. func CreateDrugFlow(drugflow []*models.DrugFlow) (err error) {
  728. if len(drugflow) > 0 {
  729. utx := writeDb.Begin()
  730. if len(drugflow) > 0 {
  731. 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 "
  732. insertParams := make([]string, 0)
  733. insertData := make([]interface{}, 0)
  734. for _, info := range drugflow {
  735. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  736. insertData = append(insertData, info.WarehousingId)
  737. insertData = append(insertData, info.DrugId)
  738. insertData = append(insertData, info.Number)
  739. insertData = append(insertData, info.BatchNumber)
  740. insertData = append(insertData, info.Count)
  741. insertData = append(insertData, info.UserOrgId)
  742. insertData = append(insertData, info.PatientId)
  743. insertData = append(insertData, info.SystemTime)
  744. insertData = append(insertData, info.ConsumableType)
  745. insertData = append(insertData, info.IsSys)
  746. insertData = append(insertData, info.WarehousingOrder)
  747. insertData = append(insertData, info.WarehouseOutId)
  748. insertData = append(insertData, info.WarehouseOutOrderNumber)
  749. insertData = append(insertData, info.IsEdit)
  750. insertData = append(insertData, info.CancelStockId)
  751. insertData = append(insertData, info.CancelOrderNumber)
  752. insertData = append(insertData, info.Manufacturer)
  753. insertData = append(insertData, info.Dealer)
  754. insertData = append(insertData, info.Creator)
  755. insertData = append(insertData, info.UpdateCreator)
  756. insertData = append(insertData, info.Status)
  757. insertData = append(insertData, info.Ctime)
  758. insertData = append(insertData, info.Mtime)
  759. insertData = append(insertData, info.Price)
  760. insertData = append(insertData, info.WarehousingDetailId)
  761. insertData = append(insertData, info.WarehouseOutDetailId)
  762. insertData = append(insertData, info.CancelOutDetailId)
  763. insertData = append(insertData, info.ExpireDate)
  764. insertData = append(insertData, info.ProductDate)
  765. insertData = append(insertData, info.MaxUnit)
  766. insertData = append(insertData, info.MinUnit)
  767. insertData = append(insertData, info.StorehouseId)
  768. }
  769. thisSQL += strings.Join(insertParams, ", ")
  770. err = utx.Exec(thisSQL, insertData...).Error
  771. if err != nil {
  772. utx.Rollback()
  773. return
  774. }
  775. }
  776. utx.Commit()
  777. }
  778. return
  779. }
  780. func GetDrugFlowIsExist(warhouseing int64, drug_id int64) (*models.DrugFlow, error) {
  781. info := models.DrugFlow{}
  782. err = XTReadDB().Where("warehousing_id = ? and drug_id = ? and status = 1", warhouseing, drug_id).Find(&info).Error
  783. if err == gorm.ErrRecordNotFound {
  784. return nil, err
  785. }
  786. if err != nil {
  787. return nil, err
  788. }
  789. return &info, nil
  790. }
  791. func GetDrugByWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  792. info := models.XtDrugWarehouseInfo{}
  793. err := XTReadDB().Where("id=? and status = 1", id).Find(&info).Error
  794. return info, err
  795. }
  796. func UpdateDrugWarehouseingInfo(id int64, info models.XtDrugWarehouseInfo) error {
  797. warehouseInfo := models.XtDrugWarehouseInfo{}
  798. err := XTWriteDB().Model(&warehouseInfo).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_max_number": info.StockMaxNumber}).Error
  799. return err
  800. }
  801. func CreateDrugFlowOne(flow models.DrugFlow) error {
  802. ut := writeDb.Begin()
  803. err := ut.Create(&flow).Error
  804. if err != nil {
  805. ut.Rollback()
  806. return err
  807. }
  808. ut.Commit()
  809. return err
  810. }
  811. func CreateSelfDrugFlowTwo(flow models.XtSelfDrugFlow) error {
  812. ut := writeDb.Begin()
  813. err := ut.Create(&flow).Error
  814. if err != nil {
  815. ut.Rollback()
  816. return err
  817. }
  818. ut.Commit()
  819. return err
  820. }
  821. func CreateNewDrugFlowOne(flow models.DrugFlow, tx *gorm.DB) error {
  822. err := tx.Create(&flow).Error
  823. if err != nil {
  824. tx.Rollback()
  825. return err
  826. }
  827. return err
  828. }
  829. func CreateDrugOutInfoLog(log models.XtDrugWarehouseOutInfoLog, tx *gorm.DB) error {
  830. err := tx.Create(&log).Error
  831. if err != nil {
  832. tx.Rollback()
  833. return err
  834. }
  835. return err
  836. }
  837. func UpdateDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64, id int64) (models.DrugAutomaticReduceDetail, error) {
  838. detail := models.DrugAutomaticReduceDetail{}
  839. 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
  840. 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
  841. return detail, err
  842. }
  843. func UpdateSelfDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64, id int64) (models.XtSelfDrugWarehouseOutInfo, error) {
  844. detail := models.XtSelfDrugWarehouseOutInfo{}
  845. 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
  846. return detail, err
  847. }
  848. func UpdateDrugFlowTens(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.DrugFlow, error) {
  849. flow := models.DrugFlow{}
  850. 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
  851. return flow, err
  852. }
  853. func UpdateDrugFlowTensOne(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.XtSelfDrugFlow, error) {
  854. flow := models.XtSelfDrugFlow{}
  855. 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
  856. return flow, err
  857. }
  858. func CreateDrugFlowTwo(flow *models.DrugFlow) error {
  859. tx := XTWriteDB().Begin()
  860. err := tx.Create(&flow).Error
  861. if err != nil {
  862. tx.Rollback()
  863. return err
  864. }
  865. tx.Commit()
  866. return err
  867. }
  868. func CreateNewDrugFlowTwo(flow *models.DrugFlow, tx *gorm.DB) error {
  869. err := tx.Create(&flow).Error
  870. if err != nil {
  871. tx.Rollback()
  872. return err
  873. }
  874. return err
  875. }
  876. func CreateInfoLog(log models.XtDrugWarehouseInfoLog, tx *gorm.DB) error {
  877. err := tx.Create(&log).Error
  878. if err != nil {
  879. tx.Rollback()
  880. return err
  881. }
  882. return err
  883. }
  884. func FindLastWarehousingInfo(order string) (info models.WarehousingInfo, err error) {
  885. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  886. return info, err
  887. }
  888. func GetLastWarehousingInfo(goodid int64) (models.WarehousingInfo, error) {
  889. info := models.WarehousingInfo{}
  890. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  891. return info, err
  892. }
  893. func CreatedWarehouseingDetail(info *models.WarehousingInfo) error {
  894. err := XTWriteDB().Create(&info).Error
  895. return err
  896. }
  897. func CreatedNewWarehouseingDetail(info *models.WarehousingInfo, tx *gorm.DB) error {
  898. err := tx.Create(&info).Error
  899. if err != nil {
  900. tx.Rollback()
  901. return err
  902. }
  903. return err
  904. }
  905. func FindFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  906. if storehouse_id == 0 {
  907. 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
  908. }
  909. if storehouse_id > 0 {
  910. 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
  911. }
  912. return info, err
  913. }
  914. func FindNewFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64, tx *gorm.DB) (info models.WarehousingInfo, err error) {
  915. if storehouse_id == 0 {
  916. 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
  917. if err != gorm.ErrRecordNotFound {
  918. if err != nil {
  919. tx.Rollback()
  920. return info, err
  921. }
  922. }
  923. }
  924. if storehouse_id > 0 {
  925. 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
  926. if err != gorm.ErrRecordNotFound {
  927. if err != nil {
  928. tx.Rollback()
  929. return info, err
  930. }
  931. }
  932. }
  933. return info, err
  934. }
  935. func FindFirstWarehousingInfoByStockTwo(good_id int64, good_type_id int64, storehouse_id int64) (info models.VmWarehousingInfo, err error) {
  936. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  937. if good_id > 0 {
  938. db = db.Where("x.good_id = ?", good_id)
  939. }
  940. if good_type_id > 0 {
  941. db = db.Where("x.good_type_id = ?", good_type_id)
  942. }
  943. if storehouse_id > 0 {
  944. db = db.Where("x.storehouse_id = ?", storehouse_id)
  945. }
  946. err = db.Select("sum(x.stock_count) as count").Scan(&info).Error
  947. return info, err
  948. }
  949. func FindWarehousingInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  950. info := models.WarehousingInfo{}
  951. err := XTReadDB().Model(&info).Where("id= ? and status = 1", id).Find(&info).Error
  952. return info, err
  953. }
  954. func FindWarehousingInfoTen(goodId int64, storehouse_id int64) (models.WarehousingInfo, error) {
  955. info := models.WarehousingInfo{}
  956. var err error
  957. if storehouse_id == 0 {
  958. 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
  959. }
  960. if storehouse_id > 0 {
  961. 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
  962. }
  963. return info, err
  964. }
  965. func FindNewWarehousingInfoTen(goodId int64, storehouse_id int64, tx *gorm.DB) (models.WarehousingInfo, error) {
  966. info := models.WarehousingInfo{}
  967. var err error
  968. if storehouse_id == 0 {
  969. 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
  970. if err != gorm.ErrRecordNotFound {
  971. if err != nil {
  972. tx.Rollback()
  973. return info, err
  974. }
  975. }
  976. }
  977. if storehouse_id > 0 {
  978. 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
  979. if err != gorm.ErrRecordNotFound {
  980. if err != nil {
  981. tx.Rollback()
  982. return info, err
  983. }
  984. }
  985. }
  986. return info, err
  987. }
  988. //药品先进先出,找出最先入库的批次
  989. func FindLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  990. 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
  991. return info, err
  992. }
  993. func FindLastDrugWarehousingInfoByIDTwo(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  994. 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
  995. return info, err
  996. }
  997. func FindNewLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64, tx *gorm.DB) (info models.XtDrugWarehouseInfo, err error) {
  998. 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
  999. if err != gorm.ErrRecordNotFound {
  1000. if err != nil {
  1001. tx.Rollback()
  1002. return info, err
  1003. }
  1004. }
  1005. return info, err
  1006. }
  1007. func FindLastDrugWarehousingInfoByIDSix(drug_id int64, warehouse_out_id int64, is_source int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  1008. if is_source == 1 {
  1009. 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
  1010. }
  1011. if is_source == 2 || is_source == 0 {
  1012. 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
  1013. }
  1014. return info, err
  1015. }
  1016. // 出库时先进先出,找出最先入库的批次
  1017. func FindLastWarehousingInfoByID(drug_id int64) (info models.WarehousingInfo, err error) {
  1018. 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
  1019. return info, err
  1020. }
  1021. //
  1022. //// 出库时先进先出,找出最先入库的批次
  1023. //func (drug_id int64) (info models.WarehousingInfo, err error) {
  1024. // 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
  1025. // return info, err
  1026. //}
  1027. func FindLastDrugWarehousingInfo(order string) (info models.DrugWarehouseInfo, err error) {
  1028. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  1029. return info, err
  1030. }
  1031. 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) {
  1032. offset := (page - 1) * limit
  1033. db := readDb.Model(&models.Warehousing{})
  1034. db = db.Where("xt_warehouse.org_id = ? AND xt_warehouse.status = 1 AND xt_warehouse.type = ?", orgId, types)
  1035. if len(keywords) > 0 {
  1036. likeKey := "%" + keywords + "%"
  1037. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  1038. 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")
  1039. }
  1040. if startTime > 0 {
  1041. db = db.Where("xt_warehouse.operation_time >=?", startTime)
  1042. }
  1043. if endTime > 0 {
  1044. db = db.Where("xt_warehouse.operation_time<= ?", endTime)
  1045. }
  1046. if storehouse_id > 0 {
  1047. db = db.Where("xt_warehouse.storehouse_id = ?", storehouse_id)
  1048. }
  1049. if check_type == 0 {
  1050. db = db.Where("xt_warehouse.is_check = 0 or xt_warehouse.is_check = 1 or xt_warehouse.is_check = 2")
  1051. }
  1052. if check_type > 0 {
  1053. db = db.Where("xt_warehouse.is_check = ?", check_type)
  1054. }
  1055. err = db.Count(&total).Offset(offset).Limit(limit).Order("xt_warehouse.warehousing_time desc,xt_warehouse.id desc").Find(&list).Error
  1056. return
  1057. }
  1058. func GetGoodInforByGoodName(keyword string, orgid int64) (goodInfo []*models.GoodInfo, err error) {
  1059. db := XTReadDB().Model(&goodInfo).Where("status = 1")
  1060. if orgid > 0 {
  1061. db = db.Where("org_id = ?", orgid)
  1062. }
  1063. if len(keyword) > 0 {
  1064. likeKey := "%" + keyword + "%"
  1065. db = db.Where("good_name like ?", likeKey)
  1066. }
  1067. err = db.Find(&goodInfo).Error
  1068. return goodInfo, err
  1069. }
  1070. func GetWarehoureOrderInfoByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, 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. func GetWarehoureOrderOutByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehouseOutInfo, err error) {
  1091. db := XTReadDB().Model(&info).Where("status =1")
  1092. if len(good_id) > 0 {
  1093. db = db.Where("good_id in (?)", good_id)
  1094. }
  1095. if startime > 0 {
  1096. db = db.Where("ctime >=?", startime)
  1097. }
  1098. if endtime > 0 {
  1099. db = db.Where("ctime<=?", endtime)
  1100. }
  1101. if orgid > 0 {
  1102. db = db.Where("org_id = ?", orgid)
  1103. }
  1104. if storehouse_id > 0 {
  1105. db = db.Where("storehouse_id = ?", storehouse_id)
  1106. }
  1107. err = db.Find(&info).Error
  1108. return info, err
  1109. }
  1110. type VMDrugWarehouse struct {
  1111. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1112. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  1113. OperationTime int64 `gorm:"column:operation_time" json:"operation_time" form:"operation_time"`
  1114. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1115. Creater int64 `gorm:"column:creater" json:"creater" form:"creater"`
  1116. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1117. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  1118. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1119. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1120. WarehousingTime int64 `gorm:"column:warehousing_time" json:"warehousing_time" form:"warehousing_time"`
  1121. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1122. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1123. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1124. Manufacturers *models.Manufacturer `gorm:"ForeignKey:Manufacturer;AssociationForeignKey:ID" json:"manufacturers"`
  1125. Dealers *models.Dealer `gorm:"ForeignKey:Dealer;AssociationForeignKey:ID" json:"dealers"`
  1126. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1127. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1128. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  1129. IsCheck int64 `gorm:"column:is_check" json:"is_check" form:"is_check"`
  1130. Checker int64 `gorm:"column:checker" json:"checker" form:"checker"`
  1131. }
  1132. func (VMDrugWarehouse) TableName() string {
  1133. return "xt_drug_warehouse"
  1134. }
  1135. type VMDrugWarehouseInfo struct {
  1136. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1137. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id" form:"warehousing_id"`
  1138. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1139. Number string `gorm:"column:number" json:"number" form:"number"`
  1140. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1141. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1142. WarehousingCount int64 `gorm:"column:warehousing_count" json:"warehousing_count" form:"warehousing_count"`
  1143. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1144. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  1145. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1146. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1147. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1148. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1149. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1150. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1151. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1152. IsReturn int64 `gorm:"column:is_return" json:"is_return" form:"is_return"`
  1153. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  1154. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1155. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1156. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1157. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1158. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1159. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  1160. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1161. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1162. SupplyWarehouseDetailInfo int64 `gorm:"column:supply_warehouse_detail_info" json:"supply_warehouse_detail_info" form:"supply_warehouse_detail_info"`
  1163. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1164. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  1165. }
  1166. func (VMDrugWarehouseInfo) TableName() string {
  1167. return "xt_drug_warehouse_info"
  1168. }
  1169. type Drug struct {
  1170. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1171. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  1172. Pinyin string `gorm:"column:pinyin" json:"pinyin" form:"pinyin"`
  1173. Wubi string `gorm:"column:wubi" json:"wubi" form:"wubi"`
  1174. DrugAlias string `gorm:"column:drug_alias" json:"drug_alias" form:"drug_alias"`
  1175. DrugAliasPinyin string `gorm:"column:drug_alias_pinyin" json:"drug_alias_pinyin" form:"drug_alias_pinyin"`
  1176. DrugAliasWubi string `gorm:"column:drug_alias_wubi" json:"drug_alias_wubi" form:"drug_alias_wubi"`
  1177. DrugCategory int64 `gorm:"column:drug_category" json:"drug_category" form:"drug_category"`
  1178. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  1179. DrugType int64 `gorm:"column:drug_type" json:"drug_type" form:"drug_type"`
  1180. DrugStockLimit string `gorm:"column:drug_stock_limit" json:"drug_stock_limit" form:"drug_stock_limit"`
  1181. DrugOriginPlace string `gorm:"column:drug_origin_place" json:"drug_origin_place" form:"drug_origin_place"`
  1182. DrugDosageForm int64 `gorm:"column:drug_dosage_form" json:"drug_dosage_form" form:"drug_dosage_form"`
  1183. MedicalInsuranceLevel int64 `gorm:"column:medical_insurance_level" json:"medical_insurance_level" form:"medical_insurance_level"`
  1184. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1185. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  1186. UnitMatrixing string `gorm:"column:unit_matrixing" json:"unit_matrixing" form:"unit_matrixing"`
  1187. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1188. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  1189. DrugControl int64 `gorm:"column:drug_control" json:"drug_control" form:"drug_control"`
  1190. Number string `gorm:"column:number" json:"number" form:"number"`
  1191. DrugClassify string `gorm:"column:drug_classify" json:"drug_classify" form:"drug_classify"`
  1192. DrugDose float64 `gorm:"column:drug_dose" json:"drug_dose" form:"drug_dose"`
  1193. DrugDoseUnit int64 `gorm:"column:drug_dose_unit" json:"drug_dose_unit" form:"drug_dose_unit"`
  1194. MedicalInsuranceNumber string `gorm:"column:medical_insurance_number" json:"medical_insurance_number" form:"medical_insurance_number"`
  1195. ProvincesCode string `gorm:"column:provinces_code" json:"provinces_code" form:"provinces_code"`
  1196. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1197. PharmacologyCategory int64 `gorm:"column:pharmacology_category" json:"pharmacology_category" form:"pharmacology_category"`
  1198. StatisticsCategory int64 `gorm:"column:statistics_category" json:"statistics_category" form:"statistics_category"`
  1199. Code string `gorm:"column:code" json:"code" form:"code"`
  1200. IsSpecialDiseases int64 `gorm:"column:is_special_diseases" json:"is_special_diseases" form:"is_special_diseases"`
  1201. IsRecord int64 `gorm:"column:is_record" json:"is_record" form:"is_record"`
  1202. Agent string `gorm:"column:agent" json:"agent" form:"agent"`
  1203. DrugStatus string `gorm:"column:drug_status" json:"drug_status" form:"drug_status"`
  1204. LimitRemark string `gorm:"column:limit_remark" json:"limit_remark" form:"limit_remark"`
  1205. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  1206. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  1207. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  1208. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  1209. Label int64 `gorm:"column:label" json:"label" form:"label"`
  1210. Sort int64 `gorm:"column:sort" json:"sort" form:"sort"`
  1211. IsUseDoctorAdvice int64 `gorm:"column:is_use_doctor_advice" json:"is_use_doctor_advice" form:"is_use_doctor_advice"`
  1212. IsDefault int64 `gorm:"column:is_default" json:"is_default" form:"is_default"`
  1213. IsChargePredict int64 `gorm:"column:is_charge_predict" json:"is_charge_predict" form:"is_charge_predict"`
  1214. IsStatisticsWork int64 `gorm:"column:is_statistics_work" json:"is_statistics_work" form:"is_statistics_work"`
  1215. IsChargeUse int64 `gorm:"column:is_charge_use" json:"is_charge_use" form:"is_charge_use"`
  1216. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1217. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1218. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1219. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1220. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  1221. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1222. DoseCode string `gorm:"column:dose_code" json:"dose_code" form:"dose_code"`
  1223. RecordDate int64 `gorm:"column:record_date" json:"record_date" form:"record_date"`
  1224. DrugRemark string `gorm:"column:drug_remark" json:"drug_remark" form:"drug_remark"`
  1225. SocialSecurityDirectoryCode string `gorm:"column:social_security_directory_code" json:"social_security_directory_code" form:"social_security_directory_code"`
  1226. IsMark int64 `gorm:"column:is_mark" json:"is_mark" form:"is_mark"`
  1227. PrescriptionMark int64 `gorm:"column:prescription_mark" json:"prescription_mark" form:"prescription_mark"`
  1228. HospApprFlag int64 `gorm:"column:hosp_appr_flag" json:"hosp_appr_flag" form:"hosp_appr_flag"`
  1229. LmtUsedFlag int64 `gorm:"column:lmt_used_flag" json:"lmt_used_flag" form:"lmt_used_flag"`
  1230. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1231. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  1232. MinNumber int64 `gorm:"column:min_number" json:"min_number" form:"min_number"`
  1233. Dose string `gorm:"column:dose" json:"dose" form:"dose"`
  1234. DoseUnit string `gorm:"column:dose_unit" json:"dose_unit" form:"dose_unit"`
  1235. DrugDay string `gorm:"column:drug_day" json:"drug_day" form:"drug_day"`
  1236. MinPrice float64 `gorm:"column:min_price" json:"min_price" form:"min_price"`
  1237. IsZeroFlag int64 `gorm:"column:is_zero_flag" json:"is_zero_flag" form:"is_zero_flag"`
  1238. }
  1239. func (Drug) TableName() string {
  1240. return "xt_base_drug"
  1241. }
  1242. func FindAllDrugWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*VMDrugWarehouse, total int64, err error) {
  1243. db := readDb.Model(&VMDrugWarehouse{})
  1244. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1245. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1246. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1247. if len(keywords) > 0 {
  1248. likeKey := "%" + keywords + "%"
  1249. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1250. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse.manufacturer")
  1251. 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")
  1252. }
  1253. if startTime > 0 {
  1254. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1255. }
  1256. if endTime > 0 {
  1257. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1258. }
  1259. db = db.Count(&total)
  1260. offset := (page - 1) * limit
  1261. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1262. return
  1263. }
  1264. 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) {
  1265. db := readDb.Model(&VMDrugWarehouse{})
  1266. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1267. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1268. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1269. if len(keywords) > 0 {
  1270. likeKey := "%" + keywords + "%"
  1271. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1272. 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")
  1273. }
  1274. if startTime > 0 {
  1275. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1276. }
  1277. if endTime > 0 {
  1278. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1279. }
  1280. if storehouse_id > 0 {
  1281. db = db.Where("xt_drug_warehouse.storehouse_id = ?", storehouse_id)
  1282. }
  1283. if check_type > 0 {
  1284. db = db.Where("xt_drug_warehouse.is_check = ?", check_type)
  1285. }
  1286. db = db.Count(&total)
  1287. offset := (page - 1) * limit
  1288. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.warehousing_time desc,xt_drug_warehouse.id desc").Find(&list).Error
  1289. return
  1290. }
  1291. func FindWarehousingInfoById(id int64) (list []*models.WarehousingInfo, err error) {
  1292. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1293. return list, err
  1294. }
  1295. func FindWarehousingInfoByIdOne(id []int64) (list []*models.WarehousingInfo, err error) {
  1296. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id in(?) AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1297. return list, err
  1298. }
  1299. func FindDrugWarehousingInfoById(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1300. 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
  1301. return list, err
  1302. }
  1303. func FindDrugWarehousingInfoByIdThree(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1304. err = readDb.Model(&VMDrugWarehouseInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehousing_id = ? AND status = 1", id).Find(&list).Error
  1305. return list, err
  1306. }
  1307. func FindDrugWarehousingInfoByIdOne(id []string, org_id int64) (list []*models.StDrugWarehouseInfo, err error) {
  1308. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  1309. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  1310. fmt.Println(table)
  1311. if len(id) > 0 {
  1312. db = db.Where("x.warehousing_id in(?)", id)
  1313. }
  1314. if org_id > 0 {
  1315. db = db.Where("x.org_id = ?", org_id)
  1316. }
  1317. 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
  1318. return list, err
  1319. }
  1320. func FindWarehousingById(id int64) (warehousing models.Warehousing, err error) {
  1321. err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1322. return warehousing, err
  1323. }
  1324. func FindNewWarehousingById(id int64, tx *gorm.DB) (warehousing models.Warehousing, err error) {
  1325. err = tx.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1326. if err != gorm.ErrRecordNotFound {
  1327. if err != nil {
  1328. tx.Rollback()
  1329. return warehousing, err
  1330. }
  1331. }
  1332. return warehousing, err
  1333. }
  1334. func FindWarehousingByIdOne(id []string) (warehousing []*models.Warehousing, err error) {
  1335. err = readDb.Model(&models.Warehousing{}).Where("id in(?) and status = 1", id).Find(&warehousing).Error
  1336. //err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1337. return warehousing, err
  1338. }
  1339. func FindDrugWarehousingById(id int64, org_id int64) (warehousing VMDrugWarehouse, err error) {
  1340. 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
  1341. return warehousing, err
  1342. }
  1343. func FindNewDrugWarehousingById(id int64, org_id int64, tx *gorm.DB) (warehousing VMDrugWarehouse, err error) {
  1344. 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
  1345. if err != gorm.ErrRecordNotFound {
  1346. if err != nil {
  1347. tx.Rollback()
  1348. return warehousing, err
  1349. }
  1350. }
  1351. return warehousing, err
  1352. }
  1353. func FindDrugWarehousingByIdOne(id []string, org_id int64) (warehousing []*models.DrugWarehouse, err error) {
  1354. db := XTReadDB().Table("xt_drug_warehouse as x").Where("x.status = 1")
  1355. if len(id) > 0 {
  1356. db = db.Where("x.id in(?)", id)
  1357. }
  1358. if org_id > 0 {
  1359. db = db.Where("x.org_id = ?", org_id)
  1360. }
  1361. 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
  1362. return warehousing, err
  1363. }
  1364. //func FindDrugWarehousingById(id int64) (warehousing models.DrugWarehouse, err error) {
  1365. // err = readDb.Model(&models.DrugWarehouse{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1366. // return warehousing, err
  1367. //
  1368. //}
  1369. func GetInfoByIds(ids []string) (info []*models.WarehousingInfo, err error) {
  1370. err = readDb.Model(&models.WarehousingInfo{}).Preload("Warehousing", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1371. return
  1372. }
  1373. func GetWarehousOutInfoByIds(ids []string) (info []*models.WarehouseOutInfo, err error) {
  1374. err = readDb.Model(&models.WarehouseOutInfo{}).Preload("WarehouseOut", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1375. return
  1376. }
  1377. func CreateSalesReturnInfo(salesReturnInfo []*models.SalesReturnInfo) (err error) {
  1378. if len(salesReturnInfo) > 0 {
  1379. utx := writeDb.Begin()
  1380. if len(salesReturnInfo) > 0 {
  1381. 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 "
  1382. insertParams := make([]string, 0)
  1383. insertData := make([]interface{}, 0)
  1384. for _, info := range salesReturnInfo {
  1385. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1386. insertData = append(insertData, info.GoodId)
  1387. insertData = append(insertData, info.SalesReturnId)
  1388. insertData = append(insertData, info.GoodTypeId)
  1389. insertData = append(insertData, info.Count)
  1390. insertData = append(insertData, info.Price)
  1391. insertData = append(insertData, info.Total)
  1392. insertData = append(insertData, info.Ctime)
  1393. insertData = append(insertData, info.Status)
  1394. insertData = append(insertData, info.OrgId)
  1395. insertData = append(insertData, info.OrderNumber)
  1396. insertData = append(insertData, info.Type)
  1397. insertData = append(insertData, info.Dealer)
  1398. insertData = append(insertData, info.Manufacturer)
  1399. }
  1400. thisSQL += strings.Join(insertParams, ", ")
  1401. err = utx.Exec(thisSQL, insertData...).Error
  1402. if err != nil {
  1403. utx.Rollback()
  1404. return
  1405. }
  1406. }
  1407. utx.Commit()
  1408. }
  1409. return
  1410. }
  1411. func CreateDrugSalesReturnInfo(salesReturnInfo []*models.DrugSalesReturnInfo) (err error) {
  1412. if len(salesReturnInfo) > 0 {
  1413. utx := writeDb.Begin()
  1414. if len(salesReturnInfo) > 0 {
  1415. 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 "
  1416. insertParams := make([]string, 0)
  1417. insertData := make([]interface{}, 0)
  1418. for _, info := range salesReturnInfo {
  1419. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1420. insertData = append(insertData, info.DrugId)
  1421. insertData = append(insertData, info.SalesReturnId)
  1422. insertData = append(insertData, info.Count)
  1423. insertData = append(insertData, info.Price)
  1424. insertData = append(insertData, info.Total)
  1425. insertData = append(insertData, info.Ctime)
  1426. insertData = append(insertData, info.Status)
  1427. insertData = append(insertData, info.OrgId)
  1428. insertData = append(insertData, info.OrderNumber)
  1429. insertData = append(insertData, info.Type)
  1430. insertData = append(insertData, info.Dealer)
  1431. insertData = append(insertData, info.Manufacturer)
  1432. insertData = append(insertData, info.RetailPrice)
  1433. insertData = append(insertData, info.RetailTotalPrice)
  1434. }
  1435. thisSQL += strings.Join(insertParams, ", ")
  1436. err = utx.Exec(thisSQL, insertData...).Error
  1437. if err != nil {
  1438. utx.Rollback()
  1439. return
  1440. }
  1441. }
  1442. utx.Commit()
  1443. }
  1444. return
  1445. }
  1446. func FindAllSalesReturnTotal(org_id int64) (total int64, err error) {
  1447. err = readDb.Model(&models.SalesReturn{}).Where("org_id = ?", org_id).Count(&total).Error
  1448. return total, err
  1449. }
  1450. func FindAllCancelStockTotal(org_id int64) (total int64, err error) {
  1451. err = readDb.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1452. return total, err
  1453. }
  1454. func FindNewAllCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1455. err = tx.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1456. if err != gorm.ErrRecordNotFound {
  1457. if err != nil {
  1458. tx.Rollback()
  1459. return total, err
  1460. }
  1461. }
  1462. return total, err
  1463. }
  1464. func FindAllDrugCancelStockTotal(org_id int64) (total int64, err error) {
  1465. err = readDb.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1466. return total, err
  1467. }
  1468. func FindNewAllDrugCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1469. err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1470. if err != gorm.ErrRecordNotFound {
  1471. if err != nil {
  1472. tx.Rollback()
  1473. return total, err
  1474. }
  1475. }
  1476. return total, err
  1477. }
  1478. //func FindAllDrugCancelStockTotalTX(org_id int64,tx *gorm.DB) (total int64, err error) {
  1479. // err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1480. // return total, err
  1481. //}
  1482. func FindAllReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.SalesReturn, total int64, err error) {
  1483. db := readDb.Model(&models.SalesReturn{})
  1484. db = db.Where("xt_sales_return.org_id = ? AND xt_sales_return.status = 1 AND xt_sales_return.type = ?", orgId, types)
  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_sales_return.creater")
  1488. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_sales_return.manufacturer")
  1489. 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")
  1490. }
  1491. if startTime > 0 {
  1492. db = db.Where("xt_sales_return.opera_time >=?", startTime)
  1493. }
  1494. if endTime > 0 {
  1495. db = db.Where("xt_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_sales_return.ctime desc").Find(&list).Error
  1500. return
  1501. }
  1502. func FindAllDrugReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugSalesReturn, total int64, err error) {
  1503. db := readDb.Model(&models.DrugSalesReturn{})
  1504. db = db.Where("xt_drug_sales_return.org_id = ? AND xt_drug_sales_return.status = 1 ", orgId)
  1505. if len(keywords) > 0 {
  1506. likeKey := "%" + keywords + "%"
  1507. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_sales_return.creater")
  1508. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_sales_return.manufacturer")
  1509. 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")
  1510. }
  1511. if startTime > 0 {
  1512. db = db.Where("xt_drug_sales_return.opera_time >=?", startTime)
  1513. }
  1514. if endTime > 0 {
  1515. db = db.Where("xt_drug_sales_return.opera_time<= ?", endTime)
  1516. }
  1517. db = db.Count(&total)
  1518. offset := (page - 1) * limit
  1519. err = db.Offset(offset).Limit(limit).Order("xt_drug_sales_return.ctime desc").Find(&list).Error
  1520. return
  1521. }
  1522. func FindReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  1523. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  1524. return list, err
  1525. }
  1526. func FindCancelStockInfoById(id int64) (list []*models.CancelStockInfo, err error) {
  1527. err = readDb.Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? AND status = 1", id).Preload("WarehousingInfo", "status = 1").Preload("GoodInfo", "status = 1").Find(&list).Error
  1528. return list, err
  1529. }
  1530. type DrugCancelStockInfo struct {
  1531. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1532. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1533. CancelStockId int64 `gorm:"column:cancel_stock_id" json:"cancel_stock_id" form:"cancel_stock_id"`
  1534. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1535. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1536. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1537. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1538. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1539. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1540. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1541. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1542. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1543. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  1544. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1545. Dealer string `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1546. Manufacturer string `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1547. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1548. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1549. Number string `gorm:"column:number" json:"number" form:"number"`
  1550. RegisterAccount string `gorm:"column:register_account" json:"register_account" form:"register_account"`
  1551. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1552. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1553. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1554. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1555. BatchNumberId int64 `gorm:"column:batch_number_id" json:"batch_number_id" form:"batch_number_id"`
  1556. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1557. DrugWarehouseInfo []*models.XtDrugWarehouseInfo `gorm:"ForeignKey:DrugId;AssociationForeignKey:DrugId" json:"drug_warehouse_info"`
  1558. }
  1559. func (DrugCancelStockInfo) TableName() string {
  1560. return "xt_drug_cancel_stock_info"
  1561. }
  1562. func FindDrugCancelStockInfoById(id int64, org_id int64) (list []*DrugCancelStockInfo, err error) {
  1563. 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
  1564. return list, err
  1565. }
  1566. func FindAllWarehousingInfo(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehousingInfo, total int64, err error) {
  1567. db := readDb.Model(&models.WarehousingInfo{})
  1568. db = db.Where("org_id = ? AND status = 1", orgId)
  1569. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  1570. if startTime > 0 {
  1571. db = db.Where("ctime >=?", startTime)
  1572. }
  1573. if endTime > 0 {
  1574. db = db.Where("ctime<= ?", endTime)
  1575. }
  1576. db = db.Count(&total)
  1577. offset := (page - 1) * limit
  1578. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1579. return list, total, err
  1580. }
  1581. func FindAllWarehouseOut(org_id int64) (total int64, err error) {
  1582. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1583. return total, err
  1584. }
  1585. func FindAllDrugWarehouseOut(org_id int64) (total int64, err error) {
  1586. err = readDb.Model(&models.DrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1587. return total, err
  1588. }
  1589. func FindAllSelfDrugWarehouseOut(org_id int64, tx *gorm.DB) (total int64, err error) {
  1590. err = tx.Model(&models.XtDrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1591. if err != gorm.ErrRecordNotFound {
  1592. if err != nil {
  1593. tx.Rollback()
  1594. return
  1595. }
  1596. }
  1597. return total, err
  1598. }
  1599. func AddSigleWarehouseOutOne(warehouseOut *models.WarehouseOut) error {
  1600. err := XTWriteDB().Create(&warehouseOut).Error
  1601. return err
  1602. }
  1603. func AddSigleWarehouseOut(warehouseOut *models.WarehouseOut, tx *gorm.DB) error {
  1604. err := tx.Create(&warehouseOut).Error
  1605. if err != nil {
  1606. tx.Rollback()
  1607. return err
  1608. }
  1609. return err
  1610. }
  1611. func AddNewSigleWarehouseOut(warehouseOut *models.WarehouseOut, tx *gorm.DB) error {
  1612. err := tx.Create(&warehouseOut).Error
  1613. if err != nil {
  1614. tx.Rollback()
  1615. return err
  1616. }
  1617. return err
  1618. }
  1619. func UpdateSingleWarehoseOut(id int64, storehouse_id int64, tx *gorm.DB) error {
  1620. err := tx.Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1621. if err != nil {
  1622. tx.Rollback()
  1623. return err
  1624. }
  1625. return err
  1626. }
  1627. func GetDrugWarehouseOutIsExit(org_id int64, is_sys int, record_time int64, warehousing_out_order string) (models.DrugWarehouseOut, error) {
  1628. drugWarehouseOut := models.DrugWarehouseOut{}
  1629. 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
  1630. return drugWarehouseOut, err
  1631. }
  1632. func GetNewDrugWarehouseOutIsExit(org_id int64, is_sys int, record_time int64, warehousing_out_order string, tx *gorm.DB) (models.DrugWarehouseOut, error) {
  1633. drugWarehouseOut := models.DrugWarehouseOut{}
  1634. 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
  1635. if err != gorm.ErrRecordNotFound {
  1636. if err != nil {
  1637. tx.Rollback()
  1638. return drugWarehouseOut, err
  1639. }
  1640. }
  1641. return drugWarehouseOut, err
  1642. }
  1643. func GetNewDrugWarehouseOutIsExitOne(org_id int64, is_sys int, record_time int64, warehousing_out_order string, tx *gorm.DB, id int64) (models.DrugWarehouseOut, error) {
  1644. drugWarehouseOut := models.DrugWarehouseOut{}
  1645. err := tx.Where("is_sys = ? AND status = 1 AND org_id = ? AND id = ?", is_sys, org_id, id).Find(&drugWarehouseOut).Error
  1646. if err != gorm.ErrRecordNotFound {
  1647. if err != nil {
  1648. tx.Rollback()
  1649. return drugWarehouseOut, err
  1650. }
  1651. }
  1652. return drugWarehouseOut, err
  1653. }
  1654. func AddSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut) error {
  1655. err := writeDb.Create(&warehouseOut).Error
  1656. return err
  1657. }
  1658. func GetSigleDrugWarehouseOutByPatientId(warehousing_time int64, patient_id int64, tx *gorm.DB) (models.XtSelfDrugWarehouse, error) {
  1659. selfDrugWarehouse := models.XtSelfDrugWarehouse{}
  1660. err := tx.Where("warehousing_time = ? and patient_id = ? and is_sys =1 and status=1", warehousing_time, patient_id).Find(&selfDrugWarehouse).Error
  1661. if err != gorm.ErrRecordNotFound {
  1662. if err != nil {
  1663. tx.Rollback()
  1664. return selfDrugWarehouse, err
  1665. }
  1666. }
  1667. return selfDrugWarehouse, err
  1668. }
  1669. func AddSelfSigleDrugWarehouseOut(warehouseOut *models.XtSelfDrugWarehouseOut, tx *gorm.DB) error {
  1670. err := tx.Create(&warehouseOut).Error
  1671. if err != nil {
  1672. tx.Rollback()
  1673. return err
  1674. }
  1675. return err
  1676. }
  1677. func AddNewSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut, tx *gorm.DB) error {
  1678. err := tx.Create(&warehouseOut).Error
  1679. if err != nil {
  1680. tx.Rollback()
  1681. return err
  1682. }
  1683. return err
  1684. }
  1685. func UpdateSingleDrugWarehouseOut(id int64, storehouse_id int64) error {
  1686. err := XTWriteDB().Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1687. return err
  1688. }
  1689. func UpdateNewSingleDrugWarehouseOut(id int64, storehouse_id int64, tx *gorm.DB) error {
  1690. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1691. if err != nil {
  1692. tx.Rollback()
  1693. return err
  1694. }
  1695. return err
  1696. }
  1697. func UpdateSelfSingleDrugWarehouseOut(id int64, patient_id int64, tx *gorm.DB) error {
  1698. err := tx.Model(&models.XtSelfDrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"patient_id": patient_id}).Error
  1699. return err
  1700. }
  1701. func CreateWarehousingOutInfo(warehouseOutInfo []*models.WarehouseOutInfo) (err error) {
  1702. if len(warehouseOutInfo) > 0 {
  1703. utx := writeDb.Begin()
  1704. if len(warehouseOutInfo) > 0 {
  1705. 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 "
  1706. insertParams := make([]string, 0)
  1707. insertData := make([]interface{}, 0)
  1708. for _, info := range warehouseOutInfo {
  1709. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1710. insertData = append(insertData, info.WarehouseOutId)
  1711. insertData = append(insertData, info.GoodId)
  1712. insertData = append(insertData, info.GoodTypeId)
  1713. insertData = append(insertData, info.ProductDate)
  1714. insertData = append(insertData, info.ExpiryDate)
  1715. insertData = append(insertData, info.Count)
  1716. insertData = append(insertData, info.Price)
  1717. insertData = append(insertData, info.TotalPrice)
  1718. insertData = append(insertData, info.Remark)
  1719. insertData = append(insertData, info.Ctime)
  1720. insertData = append(insertData, info.Status)
  1721. insertData = append(insertData, info.OrgId)
  1722. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1723. insertData = append(insertData, info.Type)
  1724. insertData = append(insertData, info.Dealer)
  1725. insertData = append(insertData, info.Manufacturer)
  1726. insertData = append(insertData, info.Number)
  1727. insertData = append(insertData, info.LicenseNumber)
  1728. }
  1729. thisSQL += strings.Join(insertParams, ", ")
  1730. err = utx.Exec(thisSQL, insertData...).Error
  1731. if err != nil {
  1732. utx.Rollback()
  1733. return
  1734. }
  1735. }
  1736. utx.Commit()
  1737. }
  1738. return
  1739. }
  1740. func CreateDrugWarehousingOutInfo(warehouseOutInfo []*models.DrugWarehouseOutInfo) (err error) {
  1741. if len(warehouseOutInfo) > 0 {
  1742. utx := writeDb.Begin()
  1743. if len(warehouseOutInfo) > 0 {
  1744. 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 "
  1745. insertParams := make([]string, 0)
  1746. insertData := make([]interface{}, 0)
  1747. for _, info := range warehouseOutInfo {
  1748. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1749. insertData = append(insertData, info.WarehouseOutId)
  1750. insertData = append(insertData, info.DrugId)
  1751. insertData = append(insertData, info.ProductDate)
  1752. insertData = append(insertData, info.ExpiryDate)
  1753. insertData = append(insertData, info.Count)
  1754. insertData = append(insertData, info.Price)
  1755. insertData = append(insertData, info.TotalPrice)
  1756. insertData = append(insertData, info.Remark)
  1757. insertData = append(insertData, info.Ctime)
  1758. insertData = append(insertData, info.Status)
  1759. insertData = append(insertData, info.OrgId)
  1760. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1761. insertData = append(insertData, info.Type)
  1762. insertData = append(insertData, info.Dealer)
  1763. insertData = append(insertData, info.Manufacturer)
  1764. insertData = append(insertData, info.RetailPrice)
  1765. insertData = append(insertData, info.RetailTotalPrice)
  1766. insertData = append(insertData, info.Number)
  1767. insertData = append(insertData, info.BatchNumber)
  1768. insertData = append(insertData, info.CountUnit)
  1769. }
  1770. thisSQL += strings.Join(insertParams, ", ")
  1771. err = utx.Exec(thisSQL, insertData...).Error
  1772. if err != nil {
  1773. utx.Rollback()
  1774. return
  1775. }
  1776. }
  1777. utx.Commit()
  1778. }
  1779. return
  1780. }
  1781. func FindLastWarehousingOutInfo(order string) (info models.WarehouseOutInfo, err error) {
  1782. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1783. return info, err
  1784. }
  1785. func FindLastDrugWarehousingOutInfo(order string) (info models.DrugWarehouseOutInfo, err error) {
  1786. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1787. return info, err
  1788. }
  1789. func FindAllWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (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.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse_out.manufacturer")
  1796. 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")
  1797. }
  1798. if startTime > 0 {
  1799. db = db.Where("xt_warehouse_out.ctime >=?", startTime)
  1800. }
  1801. if endTime > 0 {
  1802. db = db.Where("xt_warehouse_out.ctime<= ?", endTime)
  1803. }
  1804. db = db.Count(&total)
  1805. offset := (page - 1) * limit
  1806. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1807. return list, total, err
  1808. }
  1809. 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) {
  1810. db := readDb.Model(&models.WarehouseOut{})
  1811. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1", orgId)
  1812. if len(keywords) > 0 {
  1813. likeKey := "%" + keywords + "%"
  1814. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1815. 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")
  1816. }
  1817. if startTime > 0 {
  1818. db = db.Where("xt_warehouse_out.warehouse_out_time >=?", startTime)
  1819. }
  1820. if endTime > 0 {
  1821. db = db.Where("xt_warehouse_out.warehouse_out_time<= ?", endTime)
  1822. }
  1823. if storehouse_id > 0 {
  1824. db = db.Where("xt_warehouse_out.storehouse_id = ?", storehouse_id)
  1825. }
  1826. if way_type == 0 {
  1827. 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")
  1828. }
  1829. if way_type == 1 {
  1830. db = db.Where("xt_warehouse_out.is_sys = 0")
  1831. }
  1832. if way_type == 2 {
  1833. db = db.Where("xt_warehouse_out.is_sys = 1")
  1834. }
  1835. if way_type == 3 {
  1836. db = db.Where("xt_warehouse_out.is_sys = 12")
  1837. }
  1838. if way_type == 4 {
  1839. db = db.Where("xt_warehouse_out.is_sys = 5")
  1840. }
  1841. if check_type > 0 {
  1842. db = db.Where("xt_warehouse_out.is_check = ?", check_type)
  1843. }
  1844. if check_type == 0 {
  1845. db = db.Where("xt_warehouse_out.is_check = 1 or xt_warehouse_out.is_check = 2")
  1846. }
  1847. db = db.Count(&total)
  1848. offset := (page - 1) * limit
  1849. if orgId == 10191 {
  1850. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1851. } else {
  1852. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1853. }
  1854. return list, total, err
  1855. }
  1856. func FindAllDrugWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (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.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse_out.manufacturer")
  1863. 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")
  1864. }
  1865. if startTime > 0 {
  1866. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1867. }
  1868. if endTime > 0 {
  1869. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1870. }
  1871. db = db.Count(&total)
  1872. offset := (page - 1) * limit
  1873. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1874. return list, total, err
  1875. }
  1876. 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) {
  1877. db := readDb.Model(&models.DrugWarehouseOut{})
  1878. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1879. if len(keywords) > 0 {
  1880. likeKey := "%" + keywords + "%"
  1881. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1882. 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")
  1883. }
  1884. if startTime > 0 {
  1885. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1886. }
  1887. if endTime > 0 {
  1888. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1889. }
  1890. if storehouse_id > 0 {
  1891. db = db.Where("xt_drug_warehouse_out.storehouse_id = ?", storehouse_id)
  1892. }
  1893. if check_type > 0 {
  1894. db = db.Where("xt_drug_warehouse_out.is_check = ?", check_type)
  1895. }
  1896. if way_type == 0 {
  1897. 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")
  1898. }
  1899. if way_type == 1 {
  1900. db = db.Where("xt_drug_warehouse_out.is_sys = 0")
  1901. }
  1902. if way_type == 2 {
  1903. db = db.Where("xt_drug_warehouse_out.is_sys = 1")
  1904. }
  1905. if way_type == 3 {
  1906. db = db.Where("xt_drug_warehouse_out.is_sys = 12")
  1907. }
  1908. if way_type == 4 {
  1909. db = db.Where("xt_drug_warehouse_out.is_sys = 15")
  1910. }
  1911. db = db.Count(&total)
  1912. offset := (page - 1) * limit
  1913. if orgId == 10191 {
  1914. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1915. } else {
  1916. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1917. }
  1918. return list, total, err
  1919. }
  1920. func FindWarehouseOutInfoById(id int64) (list []*models.WarehouseOutInfo, err error) {
  1921. 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
  1922. return list, err
  1923. }
  1924. type DrugWarehouseOutInfo struct {
  1925. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1926. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  1927. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1928. WarehousingOutTarget int64 `gorm:"column:warehousing_out_target" json:"warehousing_out_target" form:"warehousing_out_target"`
  1929. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1930. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  1931. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1932. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  1933. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1934. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1935. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1936. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1937. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1938. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1939. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1940. IsCancel int64 `gorm:"column:is_cancel" json:"is_cancel" form:"is_cancel"`
  1941. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  1942. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1943. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1944. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1945. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  1946. SysRecordTime int64 `gorm:"column:sys_record_time" json:"sys_record_time" form:"sys_record_time"`
  1947. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1948. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1949. Number string `gorm:"column:number" json:"number" form:"number"`
  1950. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1951. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1952. WarehouseInfoId int64 `gorm:"column:warehouse_info_id" json:"warehouse_info_id" form:"warehouse_info_id"`
  1953. SupplyCancelOutId int64 `gorm:"column:supply_cancel_out_id" json:"supply_cancel_out_id" form:"supply_cancel_out_id"`
  1954. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1955. AdminUserId int64 `gorm:"column:admin_user_id" json:"admin_user_id" form:"admin_user_id"`
  1956. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  1957. StockCount string `gorm:"column:stock_count" json:"stock_count" form:"stock_count"`
  1958. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  1959. ClassType string `gorm:"column:class_type" json:"class_type" form:"class_type"`
  1960. }
  1961. func (DrugWarehouseOutInfo) TableName() string {
  1962. return "xt_drug_warehouse_out_info"
  1963. }
  1964. func FindDrugWarehouseOutInfoById(id int64, org_id int64) (list []*DrugWarehouseOutInfo, err error) {
  1965. err = readDb.Model(&DrugWarehouseOutInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehouse_out_id = ? AND status = 1 ", id).Find(&list).Error
  1966. return list, err
  1967. }
  1968. func FindWarehouseInfoByGoodId(good_id int64) (int64, *models.WarehousingInfo) {
  1969. info := &models.WarehousingInfo{}
  1970. type WarehouseInfoCountAmount struct {
  1971. Total int64
  1972. }
  1973. var result WarehouseInfoCountAmount
  1974. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(warehousing_count) as total").Scan(&result)
  1975. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).First(info)
  1976. return result.Total, info
  1977. }
  1978. func FindAllSalesReturnCountByGoodId(good_id int64) (int64, error) {
  1979. type SalesReturnCountAmount struct {
  1980. Total int64
  1981. }
  1982. var result SalesReturnCountAmount
  1983. err = readDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1984. return result.Total, err
  1985. }
  1986. func FindAllWarehouseOutInfoByGoodId(good_id int64) (int64, error) {
  1987. type WarehouseOutInfoCountAmount struct {
  1988. Total int64
  1989. }
  1990. var result WarehouseOutInfoCountAmount
  1991. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1992. return result.Total, err
  1993. }
  1994. func FindAllCancleStockInfoByGoodId(good_id int64) (int64, error) {
  1995. type CancleStockoCountAmount struct {
  1996. Total int64
  1997. }
  1998. var result CancleStockoCountAmount
  1999. err = readDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  2000. return result.Total, err
  2001. }
  2002. func FindWarehouseInfoByGoodTypeId(good_type_id int64) (models.WarehousingInfo, error) {
  2003. info := models.WarehousingInfo{}
  2004. err := readDb.Model(&models.WarehousingInfo{}).Where("good_type_id = ? AND status = 1", good_type_id).First(&info).Error
  2005. return info, err
  2006. }
  2007. func GetCancelStockDetailByOrderNumber(ordernumber string, orgid int64) (*models.CancelStock, error) {
  2008. stock := models.CancelStock{}
  2009. err := XTReadDB().Where("order_number = ? and org_id = ? and status = 1", ordernumber, orgid).Find(&stock).Error
  2010. if err == gorm.ErrRecordNotFound {
  2011. return nil, err
  2012. }
  2013. if err != nil {
  2014. return nil, err
  2015. }
  2016. return &stock, nil
  2017. }
  2018. func GetCancelStockDetailByOrderNumberOne(return_time int64, orgid int64) (*models.CancelStock, error) {
  2019. stock := models.CancelStock{}
  2020. err := XTReadDB().Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  2021. if err == gorm.ErrRecordNotFound {
  2022. return nil, err
  2023. }
  2024. if err != nil {
  2025. return nil, err
  2026. }
  2027. return &stock, nil
  2028. }
  2029. func GetNewCancelStockDetailByOrderNumberOne(return_time int64, orgid int64, tx *gorm.DB) (*models.CancelStock, error) {
  2030. stock := models.CancelStock{}
  2031. err := tx.Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  2032. if err != gorm.ErrRecordNotFound {
  2033. if err != nil {
  2034. tx.Rollback()
  2035. return &stock, err
  2036. }
  2037. }
  2038. return &stock, err
  2039. }
  2040. func AddSigleCancelStock(cancelStock *models.CancelStock) error {
  2041. err := writeDb.Create(&cancelStock).Error
  2042. return err
  2043. }
  2044. func AddNewSigleCancelStock(cancelStock *models.CancelStock, tx *gorm.DB) error {
  2045. err := tx.Create(&cancelStock).Error
  2046. if err != nil {
  2047. tx.Rollback()
  2048. return err
  2049. }
  2050. return err
  2051. }
  2052. func UpdateSigleCancelStock(id int64, storehouse_id int64) error {
  2053. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  2054. return err
  2055. }
  2056. func GetLastCancelStockById(orgid int64) (models.CancelStock, error) {
  2057. stock := models.CancelStock{}
  2058. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2059. return stock, err
  2060. }
  2061. func GetNewLastCancelStockById(orgid int64, tx *gorm.DB) (models.CancelStock, error) {
  2062. stock := models.CancelStock{}
  2063. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2064. if err != gorm.ErrRecordNotFound {
  2065. if err != nil {
  2066. tx.Rollback()
  2067. return stock, err
  2068. }
  2069. }
  2070. return stock, err
  2071. }
  2072. func GetLastDrugCancelStockById(orgid int64) (models.DrugCancelStock, error) {
  2073. stock := models.DrugCancelStock{}
  2074. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2075. return stock, err
  2076. }
  2077. func GetNewLastDrugCancelStockById(orgid int64, tx *gorm.DB) (models.DrugCancelStock, error) {
  2078. stock := models.DrugCancelStock{}
  2079. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2080. if err != gorm.ErrRecordNotFound {
  2081. if err != nil {
  2082. tx.Rollback()
  2083. return stock, err
  2084. }
  2085. }
  2086. return stock, err
  2087. }
  2088. func GetManufactureById(id int64) (models.Manufacturer, error) {
  2089. manufacturer := models.Manufacturer{}
  2090. err := XTReadDB().Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  2091. return manufacturer, err
  2092. }
  2093. func GetNewManufactureById(id int64, tx *gorm.DB) (models.Manufacturer, error) {
  2094. manufacturer := models.Manufacturer{}
  2095. err := tx.Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  2096. if err != gorm.ErrRecordNotFound {
  2097. if err != nil {
  2098. tx.Rollback()
  2099. return manufacturer, err
  2100. }
  2101. }
  2102. return manufacturer, err
  2103. }
  2104. func GetDealerById(id int64) (models.Dealer, error) {
  2105. dealer := models.Dealer{}
  2106. err := XTReadDB().Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  2107. return dealer, err
  2108. }
  2109. func GetNewDealerById(id int64, tx *gorm.DB) (models.Dealer, error) {
  2110. dealer := models.Dealer{}
  2111. err := tx.Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  2112. if err != gorm.ErrRecordNotFound {
  2113. if err != nil {
  2114. tx.Rollback()
  2115. return dealer, err
  2116. }
  2117. }
  2118. return dealer, err
  2119. }
  2120. func AddSigleDrugCancelStock(cancelStock *models.DrugCancelStock) error {
  2121. err := writeDb.Create(&cancelStock).Error
  2122. return err
  2123. }
  2124. func AddNewSigleDrugCancelStock(cancelStock *models.DrugCancelStock, tx *gorm.DB) error {
  2125. err := tx.Create(&cancelStock).Error
  2126. if err != nil {
  2127. tx.Rollback()
  2128. return err
  2129. }
  2130. return err
  2131. }
  2132. func UpdateSingleDrugCancelStock(id int64, storehouse_id int64) error {
  2133. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  2134. return err
  2135. }
  2136. //func AddSigleDrugCancelStockTX(cancelStock *models.DrugCancelStock,tx *gorm.DB) error {
  2137. // err := tx.Create(&cancelStock).Error
  2138. // return err
  2139. //}
  2140. func CreateCancelStockInfo(cancelStockInfo []*models.CancelStockInfo) (err error) {
  2141. if len(cancelStockInfo) > 0 {
  2142. utx := writeDb.Begin()
  2143. if len(cancelStockInfo) > 0 {
  2144. 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 "
  2145. insertParams := make([]string, 0)
  2146. insertData := make([]interface{}, 0)
  2147. for _, info := range cancelStockInfo {
  2148. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  2149. insertData = append(insertData, info.GoodId)
  2150. insertData = append(insertData, info.CancelStockId)
  2151. insertData = append(insertData, info.GoodTypeId)
  2152. insertData = append(insertData, info.Count)
  2153. insertData = append(insertData, info.Price)
  2154. insertData = append(insertData, info.Total)
  2155. insertData = append(insertData, info.Ctime)
  2156. insertData = append(insertData, info.Status)
  2157. insertData = append(insertData, info.OrgId)
  2158. insertData = append(insertData, info.OrderNumber)
  2159. insertData = append(insertData, info.Type)
  2160. insertData = append(insertData, info.Dealer)
  2161. insertData = append(insertData, info.Manufacturer)
  2162. insertData = append(insertData, info.Number)
  2163. insertData = append(insertData, info.RegisterAccount)
  2164. insertData = append(insertData, info.Remark)
  2165. insertData = append(insertData, info.ProductDate)
  2166. insertData = append(insertData, info.ExpiryDate)
  2167. insertData = append(insertData, info.WarehouseInfoId)
  2168. insertData = append(insertData, info.WarehouseInfoId)
  2169. }
  2170. thisSQL += strings.Join(insertParams, ", ")
  2171. err = utx.Exec(thisSQL, insertData...).Error
  2172. if err != nil {
  2173. utx.Rollback()
  2174. return
  2175. }
  2176. }
  2177. utx.Commit()
  2178. }
  2179. return
  2180. }
  2181. func CreateCancelStockInfoTwo(info *models.DrugCancelStockInfo) error {
  2182. err := XTWriteDB().Create(&info).Error
  2183. return err
  2184. }
  2185. //func CreateCancelStockInfoTwoTX(info *models.DrugCancelStockInfo,tx *gorm.DB) error {
  2186. // err := tx.Create(&info).Error
  2187. // return err
  2188. //}
  2189. func GetLastStockInfoThree(drugid int64) (models.DrugCancelStockInfo, error) {
  2190. info := models.DrugCancelStockInfo{}
  2191. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  2192. return info, err
  2193. }
  2194. //func GetLastStockInfoThreeTX(drugid int64,tx *gorm.DB) (models.DrugCancelStockInfo, error) {
  2195. // info := models.DrugCancelStockInfo{}
  2196. // err := tx.Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  2197. // return info, err
  2198. //}
  2199. func CreateCancelStockInfoOne(info *models.CancelStockInfo) error {
  2200. err := XTWriteDB().Create(&info).Error
  2201. return err
  2202. }
  2203. func CreateNewCancelStockInfoOne(info *models.CancelStockInfo, tx *gorm.DB) error {
  2204. err := XTWriteDB().Create(&info).Error
  2205. if err != nil {
  2206. tx.Rollback()
  2207. return err
  2208. }
  2209. return err
  2210. }
  2211. func GetLastCancelStockInfoByGoodId(goodId int64) (models.CancelStockInfo, error) {
  2212. info := models.CancelStockInfo{}
  2213. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  2214. return info, err
  2215. }
  2216. func GetLastNewCancelStockInfoByGoodId(goodId int64, tx *gorm.DB) (models.CancelStockInfo, error) {
  2217. info := models.CancelStockInfo{}
  2218. err := tx.Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  2219. if err != gorm.ErrRecordNotFound {
  2220. if err != nil {
  2221. tx.Rollback()
  2222. return info, err
  2223. }
  2224. }
  2225. return info, err
  2226. }
  2227. func GetLastDrugCancelStockInfoById(drugid int64) (models.DrugCancelStockInfo, error) {
  2228. info := models.DrugCancelStockInfo{}
  2229. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Last(&info).Error
  2230. return info, err
  2231. }
  2232. func CreateDrugCancelStockInfo(cancelStockInfo []*models.DrugCancelStockInfo) (err error) {
  2233. if len(cancelStockInfo) > 0 {
  2234. utx := writeDb.Begin()
  2235. if len(cancelStockInfo) > 0 {
  2236. 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 "
  2237. insertParams := make([]string, 0)
  2238. insertData := make([]interface{}, 0)
  2239. for _, info := range cancelStockInfo {
  2240. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  2241. insertData = append(insertData, info.DrugId)
  2242. insertData = append(insertData, info.CancelStockId)
  2243. insertData = append(insertData, info.Count)
  2244. insertData = append(insertData, info.Price)
  2245. insertData = append(insertData, info.Total)
  2246. insertData = append(insertData, info.Ctime)
  2247. insertData = append(insertData, info.Status)
  2248. insertData = append(insertData, info.OrgId)
  2249. insertData = append(insertData, info.OrderNumber)
  2250. insertData = append(insertData, info.Type)
  2251. insertData = append(insertData, info.Dealer)
  2252. insertData = append(insertData, info.Manufacturer)
  2253. insertData = append(insertData, info.RetailPrice)
  2254. insertData = append(insertData, info.RetailTotalPrice)
  2255. insertData = append(insertData, info.RegisterAccount)
  2256. insertData = append(insertData, info.BatchNumber)
  2257. insertData = append(insertData, info.Remark)
  2258. insertData = append(insertData, info.MaxUnit)
  2259. insertData = append(insertData, info.ProductDate)
  2260. insertData = append(insertData, info.ExpiryDate)
  2261. insertData = append(insertData, info.BatchNumberId)
  2262. }
  2263. thisSQL += strings.Join(insertParams, ", ")
  2264. err = utx.Exec(thisSQL, insertData...).Error
  2265. if err != nil {
  2266. utx.Rollback()
  2267. return
  2268. }
  2269. }
  2270. utx.Commit()
  2271. }
  2272. return
  2273. }
  2274. func FindAllCancelStockList(orgId int64) (list []*models.CancelStock, err error) {
  2275. db := readDb.Model(&models.CancelStock{})
  2276. db = db.Where("org_id = ? AND status = 1", orgId)
  2277. err = db.Order("ctime desc").Find(&list).Error
  2278. return
  2279. }
  2280. func FindAllStockOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  2281. db := readDb.Model(&models.WarehousingInfo{})
  2282. db = db.Where("org_id = ? AND status = 1", orgId)
  2283. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2284. if startTime > 0 {
  2285. db = db.Where("ctime >=?", startTime)
  2286. }
  2287. if endTime > 0 {
  2288. db = db.Where("ctime<= ?", endTime)
  2289. }
  2290. db = db.Count(&total)
  2291. offset := (page - 1) * limit
  2292. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2293. return
  2294. }
  2295. func FindAllStockInfo(orgId int64, page int64, limit int64, keyword string, startime int64, endtime int64, type_name int64) (list []*models.StockInfo, total int64, err error) {
  2296. db := XTReadDB().Model(&models.StockInfo{})
  2297. if startime > 0 {
  2298. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2299. return db.Where("org_id = ? AND status = 1 and sys_record_time>=?", orgId, startime)
  2300. })
  2301. }
  2302. if startime == 0 || endtime == 0 {
  2303. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2304. return db.Where("org_id = ? AND status = 1", orgId)
  2305. })
  2306. }
  2307. if endtime > 0 {
  2308. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2309. return db.Where("org_id = ? AND status = 1 and sys_record_time >=? and sys_record_time<=?", orgId, startime, endtime)
  2310. })
  2311. }
  2312. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgId)
  2313. db = db.Group("xt_good_information.id")
  2314. if startime == 0 || endtime == 0 {
  2315. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2316. return db.Where("org_id = ? AND status = 1", orgId)
  2317. })
  2318. }
  2319. if startime > 0 {
  2320. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2321. return db.Where("org_id = ? AND status = 1 and ctime >= ?", orgId, startime)
  2322. })
  2323. }
  2324. if endtime > 0 {
  2325. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2326. return db.Where("org_id = ? AND status = 1 and ctime>=? and ctime <= ?", orgId, startime, endtime)
  2327. })
  2328. }
  2329. db = db.Preload("QuerySalesReturnInfo", "org_id = ? AND status = 1", orgId)
  2330. if startime == 0 || endtime == 0 {
  2331. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1", orgId)
  2332. }
  2333. if startime > 0 {
  2334. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=?", orgId, startime)
  2335. }
  2336. if endtime > 0 {
  2337. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=? and ctime<=?", orgId, startime, endtime)
  2338. }
  2339. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  2340. if len(keyword) > 0 {
  2341. likeKey := "%" + keyword + "%"
  2342. 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)
  2343. 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")
  2344. }
  2345. if type_name > 0 {
  2346. 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)
  2347. }
  2348. db = db.Count(&total)
  2349. offset := (page - 1) * limit
  2350. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2351. return
  2352. }
  2353. func FindWarehouseInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2354. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2355. return
  2356. }
  2357. func FindWarehouseOutInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2358. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2359. return
  2360. }
  2361. func UpDateWarehouse(warehouse *models.Warehousing) (err error) {
  2362. 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
  2363. return
  2364. }
  2365. func UpDateWarehouseOut(warehouseOut *models.WarehouseOut) (err error) {
  2366. 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
  2367. return
  2368. }
  2369. func UpDateWarehouseInfo(warehouseInfo *models.WarehousingInfo) (err error) {
  2370. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", warehouseInfo.WarehousingOrder).Update(map[string]interface{}{"good_type_id": warehouseInfo.GoodTypeId,
  2371. "good_id": warehouseInfo.GoodId,
  2372. "number": warehouseInfo.Number,
  2373. "product_date": warehouseInfo.ProductDate,
  2374. "expiry_date": warehouseInfo.ExpiryDate,
  2375. "warehousing_count": warehouseInfo.WarehousingCount,
  2376. "price": warehouseInfo.Price,
  2377. "total_price": warehouseInfo.TotalPrice,
  2378. "mtime": warehouseInfo.Mtime,
  2379. "remark": warehouseInfo.Remark,
  2380. }).Error
  2381. return
  2382. }
  2383. func UpDateWarehouseStatus(id int64) (err error) {
  2384. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2385. return
  2386. }
  2387. func UpdatedStockFlowById(id int64) (err error) {
  2388. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("warehousing_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2389. return
  2390. }
  2391. func UpDateDrugWarehouseStatus(id int64) (err error) {
  2392. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2393. return
  2394. }
  2395. func UpdateDrugFlowById(id int64) (err error) {
  2396. err = XTWriteDB().Model(&models.DrugFlow{}).Where("warehousing_detail_id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2397. return
  2398. }
  2399. func DeleteWarehouse(ids []string) (err error) {
  2400. ut := writeDb.Begin()
  2401. err = ut.Model(&models.Warehousing{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2402. if err != nil {
  2403. ut.Rollback()
  2404. return
  2405. }
  2406. err = ut.Model(&models.WarehousingInfo{}).Where("warehousing_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. ut.Commit()
  2412. return
  2413. }
  2414. func DeleteStockFlow(ids []string) (err error) {
  2415. ut := writeDb.Begin()
  2416. err = ut.Model(&models.VmStockFlow{}).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 DeleteDrugWarehouse(ids []string) (err error) {
  2425. ut := writeDb.Begin()
  2426. err = ut.Model(&models.DrugWarehouse{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2427. if err != nil {
  2428. ut.Rollback()
  2429. return
  2430. }
  2431. err = ut.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2432. if err != nil {
  2433. ut.Rollback()
  2434. return
  2435. }
  2436. err = ut.Model(&models.DrugFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2437. if err != nil {
  2438. ut.Rollback()
  2439. return
  2440. }
  2441. ut.Commit()
  2442. return
  2443. }
  2444. func FindWareHouseById(orderNumber string) (*models.Warehousing, error) {
  2445. warehouse := &models.Warehousing{}
  2446. err := readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).First(&warehouse).Error
  2447. return warehouse, err
  2448. }
  2449. func FindWareHouseOutByIdOne(id int64) (*models.WarehouseOut, error) {
  2450. warehouseOut := &models.WarehouseOut{}
  2451. err := XTReadDB().Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  2452. return warehouseOut, err
  2453. }
  2454. func FindWareHouseOutById(id int64, tx *gorm.DB) (*models.WarehouseOut, error) {
  2455. warehouseOut := &models.WarehouseOut{}
  2456. err := tx.Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  2457. if err != gorm.ErrRecordNotFound {
  2458. if err != nil {
  2459. tx.Rollback()
  2460. return warehouseOut, err
  2461. }
  2462. }
  2463. return warehouseOut, err
  2464. }
  2465. func FindDrugWareHouseOutById(id int64, org_id int64) (*models.DrugWarehouseOut, error) {
  2466. warehouseOut := &models.DrugWarehouseOut{}
  2467. 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
  2468. return warehouseOut, err
  2469. }
  2470. func FindNewDrugWareHouseOutById(id int64, org_id int64, tx *gorm.DB) (*models.DrugWarehouseOut, error) {
  2471. warehouseOut := &models.DrugWarehouseOut{}
  2472. 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
  2473. if err != nil {
  2474. tx.Rollback()
  2475. return warehouseOut, err
  2476. }
  2477. return warehouseOut, err
  2478. }
  2479. func FindWarehouseInfoByOrderNumber(orderNumber string) (info []*models.WarehousingInfo, err error) {
  2480. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", orderNumber).Find(&info).Error
  2481. return
  2482. }
  2483. func FindWarehouseOutInfoByOrderNumber(orderNumber string) (info []*models.WarehouseOutInfo, err error) {
  2484. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", orderNumber).Find(&info).Error
  2485. return
  2486. }
  2487. func DeleteStockInRecordByOrderNumber(orderNumber string, id int64) (err error) {
  2488. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2489. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2490. return
  2491. }
  2492. func DeleteStockInRecordByReocordId(id int64) (err error) {
  2493. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2494. return
  2495. }
  2496. func FindAllGoodByManufactureId(manufacturer_id int64, dealer_id int64, org_id int64) (goodInfo []*models.GoodInfo, err error) {
  2497. db := readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id)
  2498. if manufacturer_id > 0 {
  2499. db = db.Where("manufacturer = ?", manufacturer_id)
  2500. }
  2501. if dealer_id > 0 {
  2502. db = db.Where("dealer = ?", dealer_id)
  2503. }
  2504. err = db.Preload("GoodsType", "status = 1 AND org_id = ?", org_id).Find(&goodInfo).Error
  2505. return goodInfo, err
  2506. }
  2507. func FindAllWarehouseByKeyword(orgId int64, page int64, limit int64, keywords string) (list []*models.Warehousing, total int64, err error) {
  2508. db := readDb.Model(&models.Warehousing{})
  2509. likekey := "%" + keywords + "%"
  2510. db = db.Where("org_id = ? AND status = 1 ", orgId)
  2511. db = db.Where("warehousing_order LIKE ? ", likekey)
  2512. db = db.Count(&total)
  2513. offset := (page - 1) * limit
  2514. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2515. return
  2516. }
  2517. func GetLastWarehousingById(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2518. info := models.WarehousingInfo{}
  2519. err = XTReadDB().Model(&info).Where("warehousing_id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2520. return info, err
  2521. }
  2522. func GetLastWarehousingByIdOne(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2523. info := models.WarehousingInfo{}
  2524. err = XTReadDB().Model(&info).Where("id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2525. return info, err
  2526. }
  2527. func UpDateWarehousingInfo(info *models.WarehousingInfo) (err error) {
  2528. err = writeDb.Save(&info).Error
  2529. return err
  2530. }
  2531. func UpDateDrugWarehousingInfo(info *models.DrugWarehouseInfo) (err error) {
  2532. err = writeDb.Save(&info).Error
  2533. return err
  2534. }
  2535. func UpDateDrugWarehousingInfoTwo(id int64, info *models.DrugWarehouseInfo) (err error) {
  2536. 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
  2537. return err
  2538. }
  2539. func UpDateDrugWarehousingInfoSix(id int64, info *models.DrugWarehouseInfo) (err error) {
  2540. 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
  2541. return err
  2542. }
  2543. func GetDrugWarehouseOrderOne(id int64, drug_id int64) (models.DrugWarehouseInfo, error) {
  2544. info := models.DrugWarehouseInfo{}
  2545. err = XTReadDB().Model(&info).Where("warehousing_id = ? and drug_id = ?", id, drug_id).Find(&info).Error
  2546. return info, err
  2547. }
  2548. func EditWarehousing(warehouse models.Warehousing) {
  2549. 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
  2550. }
  2551. func EditNewWarehousing(warehouse models.Warehousing, tx *gorm.DB) {
  2552. 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
  2553. if err != nil {
  2554. tx.Rollback()
  2555. return
  2556. }
  2557. return
  2558. }
  2559. func FindAllWarehouseInfo(org_id int64, types int64) (list []*models.WarehousingInfoConfig, err error) {
  2560. err = readDb.Model(&models.WarehousingInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2561. 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)
  2562. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2563. return list, err
  2564. }
  2565. func DeleteSalesReturn(ids []string) (err error) {
  2566. ut := writeDb.Begin()
  2567. err = ut.Model(&models.SalesReturn{}).Where("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. err = ut.Model(&models.SalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2573. if err != nil {
  2574. ut.Rollback()
  2575. return
  2576. }
  2577. ut.Commit()
  2578. return
  2579. }
  2580. func DeleteDrugSalesReturn(ids []string) (err error) {
  2581. ut := writeDb.Begin()
  2582. err = ut.Model(&models.DrugSalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2583. if err != nil {
  2584. ut.Rollback()
  2585. return
  2586. }
  2587. err = ut.Model(&models.DrugSalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2588. if err != nil {
  2589. ut.Rollback()
  2590. return
  2591. }
  2592. ut.Commit()
  2593. return
  2594. }
  2595. func FindAllSalesReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  2596. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  2597. return
  2598. }
  2599. type DrugSalesReturnInfo struct {
  2600. ID int64 `gorm:"column:id" json:"id" form:"id"`
  2601. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  2602. SalesReturnId int64 `gorm:"column:sales_return_id" json:"sales_return_id" form:"sales_return_id"`
  2603. Count int64 `gorm:"column:count" json:"count" form:"count"`
  2604. Price float64 `gorm:"column:price" json:"price" form:"price"`
  2605. Total float64 `gorm:"column:total" json:"total" form:"total"`
  2606. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  2607. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  2608. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  2609. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  2610. Status int64 `gorm:"column:status" json:"status" form:"status"`
  2611. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  2612. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  2613. Type int64 `gorm:"column:type" json:"type" form:"type"`
  2614. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  2615. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  2616. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  2617. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  2618. Number string `gorm:"column:number" json:"number" form:"number"`
  2619. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  2620. }
  2621. func (DrugSalesReturnInfo) TableName() string {
  2622. return "xt_drug_sales_return_info"
  2623. }
  2624. func FindAllDrugSalesReturnInfoById(id int64, org_id int64) (list []*DrugSalesReturnInfo, err error) {
  2625. 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
  2626. return
  2627. }
  2628. func FindAllSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2629. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2630. return
  2631. }
  2632. func FindAllDrugSalesReturnById(id int64, org_id int64) (salesReturn models.DrugSalesReturn, err error) {
  2633. 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
  2634. return
  2635. }
  2636. func UpDateSaleReturnStatus(id int64) (err error) {
  2637. err = readDb.Model(&models.SalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2638. return
  2639. }
  2640. func UpDateDrugSaleReturnStatus(id int64) (err error) {
  2641. err = readDb.Model(&models.DrugSalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2642. return
  2643. }
  2644. func FindSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2645. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2646. return salesReturn, err
  2647. }
  2648. func FindDrugSalesReturnById(id int64) (salesReturn models.DrugSalesReturn, err error) {
  2649. err = readDb.Model(&models.DrugSalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2650. return salesReturn, err
  2651. }
  2652. func EditSaleReturn(sales models.SalesReturn) {
  2653. 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
  2654. }
  2655. func EditDrugSaleReturn(sales models.DrugSalesReturn) {
  2656. 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
  2657. }
  2658. func UpDateSalesReturnInfo(info *models.SalesReturnInfo) (err error) {
  2659. err = writeDb.Save(&info).Error
  2660. return err
  2661. }
  2662. func UpDateDrugSalesReturnInfo(info *models.DrugSalesReturnInfo) (err error) {
  2663. err = writeDb.Save(&info).Error
  2664. return err
  2665. }
  2666. func DeleteWarehouseOut(ids []string) (err error) {
  2667. ut := writeDb.Begin()
  2668. err = ut.Model(&models.WarehouseOut{}).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.WarehouseOutInfo{}).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.VmStockFlow{}).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 DeleteDrugWarehouseOut(ids []string) (err error) {
  2687. ut := writeDb.Begin()
  2688. err = ut.Model(&models.DrugWarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2689. if err != nil {
  2690. ut.Rollback()
  2691. return
  2692. }
  2693. err = ut.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2694. if err != nil {
  2695. ut.Rollback()
  2696. return
  2697. }
  2698. err = ut.Model(&models.DrugFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2699. if err != nil {
  2700. ut.Rollback()
  2701. return
  2702. }
  2703. ut.Commit()
  2704. return
  2705. }
  2706. func UpDateWarehouseOutStatus(id int64) (err error) {
  2707. err = readDb.Model(&models.WarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2708. return
  2709. }
  2710. func UpDateDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo) (err error) {
  2711. ut := writeDb.Begin()
  2712. err = ut.Save(&info).Error
  2713. if err != nil {
  2714. ut.Rollback()
  2715. return err
  2716. }
  2717. ut.Commit()
  2718. return err
  2719. }
  2720. func UpdateDrugWarehouseInfoSumCount(id int64, deliver_number int64, tx *gorm.DB) error {
  2721. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status=1 and is_check=1", id).UpdateColumn("sum_count", gorm.Expr("sum_count - ?", deliver_number)).Error
  2722. if err != nil {
  2723. tx.Rollback()
  2724. return err
  2725. }
  2726. return err
  2727. }
  2728. func UpdateDrugWarehouseInfoSumCountOne(id int64, deliver_number int64) error {
  2729. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status=1 and is_check=1", id).UpdateColumn("sum_count", gorm.Expr("sum_count - ?", deliver_number)).Error
  2730. return err
  2731. }
  2732. func UpDateNewDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo, tx *gorm.DB) (err error) {
  2733. err = tx.Save(&info).Error
  2734. if err != nil {
  2735. tx.Rollback()
  2736. return err
  2737. }
  2738. return err
  2739. }
  2740. func UpDateWarehouseInfoByStock(info *models.WarehousingInfo) (err error) {
  2741. ut := writeDb.Begin()
  2742. err = ut.Save(&info).Error
  2743. if err != nil {
  2744. ut.Rollback()
  2745. return err
  2746. }
  2747. ut.Commit()
  2748. return err
  2749. }
  2750. func UpDateNewWarehouseInfoByStock(info *models.WarehousingInfo, tx *gorm.DB) (err error) {
  2751. err = tx.Save(&info).Error
  2752. if err != nil {
  2753. tx.Rollback()
  2754. return err
  2755. }
  2756. return err
  2757. }
  2758. func UpDateDrugWarehouseOutStatus(id int64) (err error) {
  2759. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2760. return
  2761. }
  2762. func EditWarehouseOut(warehouseOut models.WarehouseOut, tx *gorm.DB) {
  2763. 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
  2764. if err != nil {
  2765. tx.Rollback()
  2766. return
  2767. }
  2768. return
  2769. }
  2770. func EditDrugWarehouseOut(warehouseOut models.DrugWarehouseOut) {
  2771. 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
  2772. }
  2773. func EditNewDrugWarehouseOut(warehouseOut models.DrugWarehouseOut, tx *gorm.DB) {
  2774. 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
  2775. if err != nil {
  2776. tx.Rollback()
  2777. return
  2778. }
  2779. return
  2780. }
  2781. func UpDateWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) (err error) {
  2782. err = tx.Save(&info).Error
  2783. if err != nil {
  2784. tx.Rollback()
  2785. return err
  2786. }
  2787. return err
  2788. }
  2789. 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) {
  2790. db := readDb.Model(&models.CancelStock{})
  2791. db = db.Where("xt_cancel_stock.org_id = ? AND xt_cancel_stock.status = 1 AND xt_cancel_stock.type = ?", orgId, types)
  2792. if len(keywords) > 0 {
  2793. likeKey := "%" + keywords + "%"
  2794. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2795. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_cancel_stock.manufacturer")
  2796. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_cancel_stock.order_number LIKE ?", likeKey, likeKey).Group("xt_cancel_stock.id")
  2797. }
  2798. if startTime > 0 {
  2799. db = db.Where("xt_cancel_stock.opera_time >=?", startTime)
  2800. }
  2801. if endTime > 0 {
  2802. db = db.Where("xt_cancel_stock.opera_time<= ?", endTime)
  2803. }
  2804. if storehouse_id > 0 {
  2805. db = db.Where("xt_cancel_stock.storehouse_id = ?", storehouse_id)
  2806. }
  2807. if check_type > 0 {
  2808. db = db.Where("xt_cancel_stock.is_check = ?", check_type)
  2809. }
  2810. db = db.Count(&total)
  2811. offset := (page - 1) * limit
  2812. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock.ctime desc").Find(&list).Error
  2813. return
  2814. }
  2815. 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) {
  2816. db := readDb.Model(&models.DrugCancelStock{})
  2817. db = db.Where("xt_drug_cancel_stock.org_id = ? AND xt_drug_cancel_stock.status = 1", orgId)
  2818. if len(keywords) > 0 {
  2819. likeKey := "%" + keywords + "%"
  2820. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_cancel_stock.creater")
  2821. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_cancel_stock.manufacturer")
  2822. 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")
  2823. }
  2824. if startTime > 0 {
  2825. db = db.Where("xt_drug_cancel_stock.opera_time >=?", startTime)
  2826. }
  2827. if endTime > 0 {
  2828. db = db.Where("xt_drug_cancel_stock.opera_time<= ?", endTime)
  2829. }
  2830. if storehoue_id > 0 {
  2831. db = db.Where("xt_drug_cancel_stock.storehouse_id = ?", storehoue_id)
  2832. }
  2833. if check_type > 0 {
  2834. db = db.Where("xt_drug_cancel_stock.is_check = ?", check_type)
  2835. }
  2836. db = db.Count(&total)
  2837. offset := (page - 1) * limit
  2838. err = db.Offset(offset).Limit(limit).Order("xt_drug_cancel_stock.ctime desc").Find(&list).Error
  2839. return
  2840. }
  2841. func GetCancelWarehosueInfo(ids []string) (info []*models.CancelStockInfo, err error) {
  2842. err = XTReadDB().Where("id IN(?) and status = 1", ids).Find(&info).Error
  2843. return info, err
  2844. }
  2845. func DeleteCancelStock(ids []string) (err error) {
  2846. ut := writeDb.Begin()
  2847. err = ut.Model(&models.CancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2848. if err != nil {
  2849. ut.Rollback()
  2850. return
  2851. }
  2852. err = ut.Model(&models.CancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2853. if err != nil {
  2854. ut.Rollback()
  2855. return
  2856. }
  2857. err = ut.Model(&models.VmStockFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2858. if err != nil {
  2859. ut.Rollback()
  2860. return
  2861. }
  2862. ut.Commit()
  2863. return
  2864. }
  2865. func GetDrugWareInfoByCancelId(ids []string) (info []*models.DrugCancelStockInfo, err error) {
  2866. err = XTReadDB().Where("cancel_stock_id IN(?) and status = 1", ids).Find(&info).Error
  2867. return info, err
  2868. }
  2869. func DeleteDrugCancelStock(ids []string) (err error) {
  2870. ut := writeDb.Begin()
  2871. err = ut.Model(&models.DrugCancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2872. if err != nil {
  2873. ut.Rollback()
  2874. return
  2875. }
  2876. err = ut.Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2877. if err != nil {
  2878. ut.Rollback()
  2879. return
  2880. }
  2881. err = ut.Model(&models.DrugFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2882. if err != nil {
  2883. ut.Rollback()
  2884. return
  2885. }
  2886. ut.Commit()
  2887. return
  2888. }
  2889. func UpDateCancleStockStatus(id int64) (err error) {
  2890. err = readDb.Model(&models.CancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2891. return
  2892. }
  2893. func GetCancelStockById(id int64) (models.CancelStockInfo, error) {
  2894. info := models.CancelStockInfo{}
  2895. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  2896. return info, err
  2897. }
  2898. func ModefyWarehouseInfoNight(info models.WarehousingInfo, id int64) error {
  2899. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", info.StockCount)).Error
  2900. return err
  2901. }
  2902. func UpdatedStockFlowByCancelId(id int64, good_id int64) (err error) {
  2903. 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
  2904. return
  2905. }
  2906. func UpDateDrugCancleStockStatus(id int64) (err error) {
  2907. err = readDb.Model(&models.DrugCancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2908. return
  2909. }
  2910. func UpDateDrugCancelStockById(id int64) (err error) {
  2911. err = XTWriteDB().Model(&models.DrugFlow{}).Where("cancel_out_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2912. return
  2913. }
  2914. func EditCancelStock(sales models.CancelStock) {
  2915. 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
  2916. }
  2917. func EditDrugCancelStock(sales models.DrugCancelStock) {
  2918. 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
  2919. }
  2920. func FindCancelStockById(id int64, org_id int64) (cancelStock models.CancelStock, err error) {
  2921. err = readDb.Model(&models.CancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2922. return cancelStock, err
  2923. }
  2924. func FindCancelDrugById(id int64, org_id int64) (cancelStock models.DrugCancelStock, err error) {
  2925. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2926. return cancelStock, err
  2927. }
  2928. func FindDrugCancelStockById(id int64) (cancelStock models.DrugCancelStock, err error) {
  2929. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2930. return cancelStock, err
  2931. }
  2932. func UpDateCancelStockInfo(info *models.CancelStockInfo) (err error) {
  2933. err = writeDb.Save(&info).Error
  2934. return err
  2935. }
  2936. func UpDateDrugCancelStockInfo(info *models.DrugCancelStockInfo) (err error) {
  2937. err = writeDb.Save(&info).Error
  2938. return err
  2939. }
  2940. func FindWarehouseOutList(org_id int64, types int64) (list []*models.WarehouseOut, err error) {
  2941. 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
  2942. return list, err
  2943. }
  2944. func FindAllWarehouseOutInfo(org_id int64, types int64) (list []*models.WarehousingOutInfoConfig, err error) {
  2945. err = readDb.Model(&models.WarehousingOutInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2946. 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)
  2947. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2948. return list, err
  2949. }
  2950. type StockDetailModel struct {
  2951. GoodId int64 `gorm:"good_id" json:"good_id"`
  2952. GoodTypeId int64 `gorm:"good_type_id" json:"good_type_id"`
  2953. WarehousingOrder string `gorm:"warehousing_order" json:"warehousing_order"`
  2954. Price float64 `gorm:"price" json:"price"`
  2955. WarehousingCount int64 `gorm:"warehousing_count" json:"warehousing_count"`
  2956. Type int `gorm:"type" json:"type"`
  2957. Creater int64 `gorm:"creater" json:"creater"`
  2958. WarehousingTime int64 `gorm:"warehousing_time" json:"warehousing_time"`
  2959. Types int64 `gorm:"types" json:"types"`
  2960. }
  2961. func FindStockInDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64, good_id int64) (list []*models.WarehousingInfo, total int64, err error) {
  2962. db := readDb.Model(&models.WarehousingInfo{})
  2963. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1 AND xt_warehouse_info.is_check = 1", orgId)
  2964. if good_id > 0 {
  2965. db = db.Where("xt_warehouse_info.good_id = ?", good_id)
  2966. }
  2967. if len(keywords) > 0 {
  2968. likeKey := "%" + keywords + "%"
  2969. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2970. 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)
  2971. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2972. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2973. 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")
  2974. } else {
  2975. 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)
  2976. }
  2977. if manufacturer > 0 {
  2978. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2979. }
  2980. if storehouse_id > 0 {
  2981. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  2982. }
  2983. if order_type > 0 {
  2984. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2985. }
  2986. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  2987. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2988. db = db.Count(&total)
  2989. offset := (page - 1) * limit
  2990. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_info.id desc").Find(&list).Error
  2991. return
  2992. }
  2993. func GetStockInDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64, good_id int64) (err error, totalPrice float64) {
  2994. db := readDb.Model(&models.WarehousingInfo{})
  2995. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId)
  2996. if good_id > 0 {
  2997. db = db.Where("good_id = ?", good_id)
  2998. }
  2999. if len(keywords) > 0 {
  3000. likeKey := "%" + keywords + "%"
  3001. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  3002. 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)
  3003. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  3004. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  3005. 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")
  3006. } else {
  3007. 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)
  3008. }
  3009. if manufacturer > 0 {
  3010. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  3011. }
  3012. if order_type > 0 {
  3013. db = db.Where("xt_warehouse_info.type = ?", order_type)
  3014. }
  3015. if storehouse_id > 0 {
  3016. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  3017. }
  3018. type PriceAmount struct {
  3019. Total float64
  3020. }
  3021. var result []PriceAmount
  3022. err = db.Select("total_price as total").Scan(&result).Error
  3023. var total float64
  3024. for _, value := range result {
  3025. total = total + value.Total
  3026. }
  3027. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  3028. return err, total
  3029. }
  3030. func FloatRound(f float64, n int) float64 {
  3031. format := "%." + strconv.Itoa(n) + "f"
  3032. res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
  3033. return res
  3034. }
  3035. func FindStockOutDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64, good_id int64, order_way int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  3036. if orgId == 9671 {
  3037. db := readDb.Model(&models.WarehouseOutInfo{})
  3038. 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)
  3039. if len(keywords) > 0 {
  3040. likeKey := "%" + keywords + "%"
  3041. 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)
  3042. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  3043. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  3044. 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")
  3045. } else {
  3046. 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)
  3047. }
  3048. if manufacturer > 0 {
  3049. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  3050. }
  3051. if order_type > 0 {
  3052. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  3053. }
  3054. if storehouse_id > 0 {
  3055. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  3056. }
  3057. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  3058. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ?", orgId)
  3059. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3060. db = db.Count(&total)
  3061. offset := (page - 1) * limit
  3062. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  3063. return
  3064. } else {
  3065. db := readDb.Model(&models.WarehouseOutInfo{})
  3066. 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)
  3067. if good_id > 0 {
  3068. db = db.Where("good_id = ?", good_id)
  3069. }
  3070. if order_way == 1 {
  3071. db = db.Where("xt_warehouse_out_info.is_sys =0")
  3072. }
  3073. if order_way == 2 {
  3074. db = db.Where("xt_warehouse_out_info.is_sys !=0")
  3075. }
  3076. if len(keywords) > 0 {
  3077. likeKey := "%" + keywords + "%"
  3078. 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)
  3079. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  3080. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  3081. 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")
  3082. } else {
  3083. 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)
  3084. }
  3085. if manufacturer > 0 {
  3086. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  3087. }
  3088. if order_type > 0 {
  3089. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  3090. }
  3091. if storehouse_id > 0 {
  3092. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  3093. }
  3094. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ? and is_check =1", orgId)
  3095. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ? and is_check =1", orgId)
  3096. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3097. db = db.Count(&total)
  3098. offset := (page - 1) * limit
  3099. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  3100. return
  3101. }
  3102. }
  3103. func GetStockOutDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64, good_id int64, order_way int64) (err error, totalPrice float64) {
  3104. db := readDb.Model(&models.WarehouseOutInfo{})
  3105. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.count > 0", orgId)
  3106. if len(keywords) > 0 {
  3107. likeKey := "%" + keywords + "%"
  3108. 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)
  3109. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  3110. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  3111. 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")
  3112. } else {
  3113. 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)
  3114. }
  3115. if good_id > 0 {
  3116. db = db.Where("xt_warehouse_out_info.good_id = ?", good_id)
  3117. }
  3118. if order_way == 1 {
  3119. db = db.Where("xt_warehouse_out_info.is_sys = ?", 0)
  3120. }
  3121. if order_way == 2 {
  3122. db = db.Where("xt_warehouse_out_info.is_sys != ?", 0)
  3123. }
  3124. if manufacturer > 0 {
  3125. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  3126. }
  3127. if order_type > 0 {
  3128. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  3129. }
  3130. if storehouse_id > 0 {
  3131. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  3132. }
  3133. type PriceAmount struct {
  3134. Total float64
  3135. }
  3136. var result []PriceAmount
  3137. err = db.Select("(count * price) as total").Scan(&result).Error
  3138. var total float64
  3139. for _, value := range result {
  3140. total = total + value.Total
  3141. }
  3142. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  3143. fmt.Println(total)
  3144. return err, total
  3145. }
  3146. 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) {
  3147. db := readDb.Model(&models.SalesReturnInfo{})
  3148. db = db.Where("xt_sales_return_info.org_id = ? AND xt_sales_return_info.status = 1", orgId)
  3149. if len(keywords) > 0 {
  3150. likeKey := "%" + keywords + "%"
  3151. 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)
  3152. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  3153. db = db.Joins("join xt_good_information on xt_good_information.id = xt_sales_return_info.good_id")
  3154. 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")
  3155. } else {
  3156. 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)
  3157. }
  3158. if manufacturer > 0 {
  3159. db = db.Joins("join xt_sales_return as sr on sr.id = xt_sales_return_info.sales_return_id AND sr.manufacturer =?", manufacturer)
  3160. }
  3161. if order_type > 0 {
  3162. db = db.Where("xt_sales_return_info.type = ?", order_type)
  3163. }
  3164. db = db.Preload("SalesReturn", "status = 1 AND org_id = ?", orgId)
  3165. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3166. offset := (page - 1) * limit
  3167. db = db.Count(&total)
  3168. err = db.Offset(offset).Limit(limit).Order("xt_sales_return_info.ctime desc").Find(&list).Error
  3169. return
  3170. }
  3171. func FindeCancelGroup(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64, good_id int64) (list []*models.CancelStockInfo, err error) {
  3172. db := readDb.Model(&models.CancelStockInfo{})
  3173. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  3174. if len(keywords) > 0 {
  3175. likeKey := "%" + keywords + "%"
  3176. 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)
  3177. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  3178. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  3179. 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")
  3180. } else {
  3181. 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)
  3182. }
  3183. if manufacturer > 0 {
  3184. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  3185. }
  3186. if order_type > 0 {
  3187. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  3188. }
  3189. if storehouse_id > 0 {
  3190. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", order_type)
  3191. }
  3192. if good_id > 0 {
  3193. db = db.Where("xt_cancel_stock_info.good_id = ?", good_id)
  3194. }
  3195. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  3196. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3197. err = db.Group("xt_cancel_stock_info.good_id").Order("xt_cancel_stock_info.ctime desc").Find(&list).Error
  3198. return
  3199. }
  3200. func FindCancelDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64, good_id int64) (list []*models.CancelStockInfo, total int64, err error) {
  3201. db := readDb.Model(&models.CancelStockInfo{})
  3202. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  3203. if len(keywords) > 0 {
  3204. likeKey := "%" + keywords + "%"
  3205. 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)
  3206. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  3207. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  3208. 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")
  3209. } else {
  3210. 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)
  3211. }
  3212. if manufacturer > 0 {
  3213. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  3214. }
  3215. if order_type > 0 {
  3216. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  3217. }
  3218. if storehouse_id > 0 {
  3219. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", storehouse_id)
  3220. }
  3221. if good_id > 0 {
  3222. db = db.Where("xt_cancel_stock_info.good_id =?", good_id)
  3223. }
  3224. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  3225. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3226. db = db.Count(&total)
  3227. offset := (page - 1) * limit
  3228. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock_info.id desc").Find(&list).Error
  3229. return
  3230. }
  3231. func FindStockInByDealerId(id int64) (total int64, err error) {
  3232. err = readDb.Model(&models.Warehousing{}).Where("dealer = ? AND status = 1", id).Count(&total).Error
  3233. return
  3234. }
  3235. func FindStockInByManufacturerId(id int64) (total int64, err error) {
  3236. err = readDb.Model(&models.Warehousing{}).Where("manufacturer = ? AND status = 1", id).Count(&total).Error
  3237. return
  3238. }
  3239. func FindGoodTypeByName(name string, org_id int64) (total int64) {
  3240. readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 AND type_name = ? ", org_id, name).Count(&total)
  3241. return
  3242. }
  3243. func FindGoodInfoByName(name string, org_id int64, good_type_id int64) (total int64) {
  3244. 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)
  3245. return
  3246. }
  3247. func FindGoodInfoByNameTwo(name string, org_id int64, good_type_id int64, good_name string) (total int64) {
  3248. 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)
  3249. return
  3250. }
  3251. func FindGoodInfoByNameOne(name string, org_id int64, good_type_id int64, id int64, goodname string) (total int64) {
  3252. 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)
  3253. return
  3254. }
  3255. func FindAllGoodTypeByType(types int64) (goodType []*models.GoodsType, err error) {
  3256. err = readDb.Model(&models.GoodsType{}).Where("type = ? AND status = 1", types).Find(&goodType).Error
  3257. return goodType, err
  3258. }
  3259. func FindWarehouseInfoByGoodType(good_type_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  3260. 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
  3261. return info, err
  3262. }
  3263. func FindStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.WarehouseOut, err error) {
  3264. 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
  3265. return
  3266. }
  3267. func FindStockOutByIsSysOne(org_id int64, is_sys int, record_time int64, warehousing_out_order string, tx *gorm.DB) (out models.WarehouseOut, err error) {
  3268. 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
  3269. if err != gorm.ErrRecordNotFound {
  3270. if err != nil {
  3271. tx.Rollback()
  3272. return
  3273. }
  3274. }
  3275. return
  3276. }
  3277. func FindStockOutByIsSysSix(org_id int64, is_sys int, record_time int64, second_warehouse_id int64) (out models.WarehouseOut, err error) {
  3278. 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
  3279. return
  3280. }
  3281. func FindDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  3282. 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
  3283. return
  3284. }
  3285. func FindNewDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64, tx *gorm.DB) (out models.DrugWarehouseOut, err error) {
  3286. 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
  3287. if err != gorm.ErrRecordNotFound {
  3288. if err != nil {
  3289. tx.Rollback()
  3290. return
  3291. }
  3292. }
  3293. return
  3294. }
  3295. func FindDrugSettleStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  3296. 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
  3297. return
  3298. }
  3299. func FindLastStockInInfoRecord(good_id int64, org_id int64) (in models.WarehousingInfo, err error) {
  3300. err = readDb.Model(&models.WarehousingInfo{}).Where("status = 1 AND org_id = ? AND good_id = ?", org_id, good_id).First(&in).Error
  3301. return
  3302. }
  3303. func FindLastDrugStockInInfoRecord(drug_id int64, org_id int64) (in models.DrugWarehouseOutInfo, err error) {
  3304. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("status = 1 AND org_id = ? AND drug_id = ?", org_id, drug_id).Last(&in).Error
  3305. return
  3306. }
  3307. func GetStockInRecoredByGoodId(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  3308. info := models.WarehouseOutInfo{}
  3309. 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
  3310. if err == gorm.ErrRecordNotFound {
  3311. return nil, err
  3312. }
  3313. if err != nil {
  3314. return nil, err
  3315. }
  3316. return &info, nil
  3317. }
  3318. func GetStockInRecoredByGoodIdOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64) (*models.WarehouseOutInfo, error) {
  3319. info := models.WarehouseOutInfo{}
  3320. 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
  3321. if err == gorm.ErrRecordNotFound {
  3322. return nil, err
  3323. }
  3324. if err != nil {
  3325. return nil, err
  3326. }
  3327. return &info, nil
  3328. }
  3329. func UpdateAutoMaticReduceDetail(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  3330. 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
  3331. return err
  3332. }
  3333. func UpdateAutoMaticReduceDetailOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  3334. 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
  3335. return err
  3336. }
  3337. func GetGoodsIsExsit(good_id int64, sys_record_time int64, org_id int64, warehouse_out_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  3338. outInfo := models.WarehouseOutInfo{}
  3339. 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
  3340. if err != gorm.ErrRecordNotFound {
  3341. if err != nil {
  3342. tx.Rollback()
  3343. return outInfo, err
  3344. }
  3345. }
  3346. return outInfo, err
  3347. }
  3348. func GetDrugsIsExsit(drug_id int64, sys_record_time int64, org_id int64, warehouse_out_id int64) (models.XtDrugWarehouseOutInfo, error) {
  3349. outInfo := models.XtDrugWarehouseOutInfo{}
  3350. 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
  3351. return outInfo, err
  3352. }
  3353. func GetNewDrugsIsExsit(drug_id int64, sys_record_time int64, org_id int64, warehouse_out_id int64, tx *gorm.DB) (models.XtDrugWarehouseOutInfo, error) {
  3354. outInfo := models.XtDrugWarehouseOutInfo{}
  3355. 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
  3356. if err != gorm.ErrRecordNotFound {
  3357. if err != nil {
  3358. tx.Rollback()
  3359. return outInfo, err
  3360. }
  3361. }
  3362. return outInfo, err
  3363. }
  3364. func AddSigleWarehouseOutInfoOne(info *models.WarehouseOutInfo) error {
  3365. err := XTWriteDB().Create(&info).Error
  3366. return err
  3367. }
  3368. func AddSigleWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) error {
  3369. err := tx.Create(&info).Error
  3370. if err != nil {
  3371. tx.Rollback()
  3372. return err
  3373. }
  3374. return err
  3375. }
  3376. func AddNewSigleWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) error {
  3377. err := tx.Create(&info).Error
  3378. if err != nil {
  3379. tx.Rollback()
  3380. return err
  3381. }
  3382. return err
  3383. }
  3384. func UpdateStockOut(warehouseinfoid int64, info models.WarehousingInfo) error {
  3385. err := XTWriteDB().Model(&info).Where("id=? and status = 1", warehouseinfoid).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  3386. return err
  3387. }
  3388. func AddSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo) error {
  3389. err := writeDb.Create(&info).Error
  3390. return err
  3391. }
  3392. func AddSelfSigleDrugWarehouseOutInfo(info *models.XtSelfDrugWarehouseOutInfo, tx *gorm.DB) error {
  3393. err := tx.Create(&info).Error
  3394. if err != nil {
  3395. tx.Rollback()
  3396. return err
  3397. }
  3398. return err
  3399. }
  3400. func AddNewSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, tx *gorm.DB) error {
  3401. err := tx.Create(&info).Error
  3402. if err != nil {
  3403. tx.Rollback()
  3404. return err
  3405. }
  3406. return err
  3407. }
  3408. func UpdateSigleDrugWarehouseOutInfoDetail(id int64, count int64, tx *gorm.DB) error {
  3409. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
  3410. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).UpdateColumn("over_count", gorm.Expr("over_count - ?", count)).Error
  3411. if err != nil {
  3412. tx.Rollback()
  3413. return err
  3414. }
  3415. return err
  3416. }
  3417. func UpdateSigleDrugWarehouseOutInfoDetailOne(id int64, count int64, tx *gorm.DB) error {
  3418. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"count": count}).Error
  3419. return err
  3420. }
  3421. func AddSigleAutoReduceRecordInfo(detail *models.AutomaticReduceDetail) error {
  3422. ut := writeDb.Begin()
  3423. err := ut.Create(&detail).Error
  3424. if err != nil {
  3425. ut.Rollback()
  3426. return err
  3427. }
  3428. ut.Commit()
  3429. return err
  3430. }
  3431. func AddSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail) error {
  3432. err := writeDb.Create(&detail).Error
  3433. return err
  3434. }
  3435. 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) {
  3436. detail := models.DrugAutomaticReduceDetail{}
  3437. 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
  3438. if err != gorm.ErrRecordNotFound {
  3439. if err != nil {
  3440. tx.Rollback()
  3441. return detail, err
  3442. }
  3443. }
  3444. return detail, err
  3445. }
  3446. func UpdateNewSingleDrugAutoReduceRecordInfo(id int64, count int64, tx *gorm.DB) error {
  3447. err := tx.Model(models.DrugAutomaticReduceDetail{}).Where("id = ? and status =1", id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
  3448. if err != nil {
  3449. tx.Rollback()
  3450. return err
  3451. }
  3452. return err
  3453. }
  3454. func ModifyDrugPriceList(drug_id int64, retail_price float64, min_number int64, tx *gorm.DB) error {
  3455. result := float64(min_number) * retail_price
  3456. //保留两位小数的方法1:使用math.Round
  3457. rounded := math.Round(result*100) / 100
  3458. err = tx.Model(models.Drug{}).Where("id = ? and status=1", drug_id).Updates(map[string]interface{}{"min_price": retail_price, "retail_price": rounded}).Error
  3459. if err != nil {
  3460. tx.Rollback()
  3461. return err
  3462. }
  3463. return err
  3464. }
  3465. func AddNewSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail, tx *gorm.DB) error {
  3466. err := tx.Create(&detail).Error
  3467. if err != nil {
  3468. tx.Rollback()
  3469. return err
  3470. }
  3471. return err
  3472. }
  3473. func FindStockOutInfoByTypeId(org_id int64, good_type_id int64, good_id int64, out_id int64, number string) (out models.WarehouseOutInfo, err error) {
  3474. 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
  3475. return
  3476. }
  3477. func FindStockOutInfoByStock(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (out []*models.WarehouseOutInfo, err error) {
  3478. 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
  3479. return
  3480. }
  3481. func FindStockOutInfoByStockOne(org_id int64, good_type_id int64, good_id int64, record_date int64) (out []*models.WarehouseOutInfo, err error) {
  3482. 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
  3483. return
  3484. }
  3485. func FindStockOutInfoByStockTwo(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (models.WarehouseOutInfo, error) {
  3486. info := models.WarehouseOutInfo{}
  3487. 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
  3488. return info, err
  3489. }
  3490. func FindDrugStockOutInfoByTypeId(org_id int64, drug_id int64, out_id int64, number string) (out models.DrugWarehouseOutInfo, err error) {
  3491. 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
  3492. return
  3493. }
  3494. func UpdateStockOutInfoCount(org_id int64, id int64) {
  3495. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", 1))
  3496. }
  3497. func FindUserDetailById(org_id int64, id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  3498. db := readDb.Model(&models.AutomaticReduceDetail{})
  3499. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  3500. db = db.Preload("GoodsType", "status = 1")
  3501. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3502. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND good_id != 0 AND count <> 0 ", org_id, id)
  3503. db = db.Count(&total)
  3504. err = db.Find(&user).Error
  3505. return
  3506. }
  3507. type DrugAutomaticReduceDetail struct {
  3508. ID int64 `gorm:"column:id" json:"id" form:"id"`
  3509. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  3510. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  3511. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  3512. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  3513. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  3514. Status int64 `gorm:"column:status" json:"status" form:"status"`
  3515. RecordTime int64 `gorm:"column:record_time" json:"record_time" form:"record_time"`
  3516. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  3517. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  3518. Count int64 `gorm:"column:count" json:"count" form:"count"`
  3519. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  3520. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  3521. Patients *models.Patients `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"user"`
  3522. DrugWarehouseOutInfo []*models.DrugWarehouseOutInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"drugwarehouseoutinfo"`
  3523. }
  3524. func (DrugAutomaticReduceDetail) TableName() string {
  3525. return "xt_drug_automatic_reduce_detail"
  3526. }
  3527. func FindDrugStockUserDetailById(org_id int64, id int64) (user []*DrugAutomaticReduceDetail, err error, total int64) {
  3528. db := readDb.Model(&DrugAutomaticReduceDetail{})
  3529. db = db.Preload("Drug", "org_id = ? AND status = 1", org_id)
  3530. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3531. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND drug_id != 0", org_id, id)
  3532. db = db.Count(&total)
  3533. err = db.Find(&user).Error
  3534. return
  3535. }
  3536. func UpdateStockOutInfoCount2(org_id int64, id int64, count int64) {
  3537. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3538. }
  3539. func UpdateDrugStockOutInfoCount2(org_id int64, id int64, count int64) {
  3540. writeDb.Model(&models.DrugWarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3541. }
  3542. func FindPatientAutomaticReduceRecord(org_id int64, record_time int64, good_id int64, good_type_id int64, patient_id int64) (count int64, err error) {
  3543. 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
  3544. return
  3545. }
  3546. func FindPatientDrugAutomaticReduceRecord(org_id int64, record_time int64, drug_id int64, patient_id int64) (count int64, err error) {
  3547. 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
  3548. return
  3549. }
  3550. type VMWarehousingInfo struct {
  3551. ID int64 `gorm:"column:id" json:"id"`
  3552. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id"`
  3553. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  3554. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  3555. Status int64 `gorm:"column:status" json:"status"`
  3556. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3557. GoodInfo GoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"good_info"`
  3558. }
  3559. func (VMWarehousingInfo) TableName() string {
  3560. return "xt_warehouse_info"
  3561. }
  3562. type GoodInfo struct {
  3563. ID int64 `gorm:"column:id" json:"id"`
  3564. SpecificationName string `gorm:"column:specification_name" json:"specification_name"`
  3565. Status int64 `gorm:"column:status" json:"status"`
  3566. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3567. }
  3568. func (GoodInfo) TableName() string {
  3569. return "xt_good_information"
  3570. }
  3571. func GetStockInGoodInfo(orgId int64) (stockIn []*VMWarehousingInfo, err error) {
  3572. err = readDb.Model(&VMWarehousingInfo{}).Preload("GoodInfo", "status = 1 AND org_id = ?", orgId).Where("org_id = ? AND status = 1", orgId).Find(&stockIn).Error
  3573. return
  3574. }
  3575. func GetAllStockOutUserDetail(patient_id int64, orgId int64, record_time int64) (goodUser []*models.DialysisBeforePrepare, err error) {
  3576. 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
  3577. return
  3578. }
  3579. func PostSearchStock(keyword string, orgid int64) (goods []*models.GoodsType, err error) {
  3580. likeKey := "%" + keyword + "%"
  3581. db := XTReadDB().Model(&goods)
  3582. if len(keyword) > 0 {
  3583. db = db.Where("type_name like ?", likeKey)
  3584. }
  3585. err = db.Where("org_id = ? and status =1", orgid).Find(&goods).Error
  3586. return goods, err
  3587. }
  3588. func GetStockType(orgid int64) (goodstype []*models.GoodsTypeOne, err error) {
  3589. err = XTReadDB().Where("org_id = ? and status = 1 and (stock_attribute = 2 or stock_attribute = 3 or stock_attribute = 4)", orgid).
  3590. Preload("GoodInfo", "org_id = ? and status = 1 and find_in_set('停用',good_status) = 0", orgid).Find(&goodstype).Error
  3591. return goodstype, err
  3592. }
  3593. func GetDialyStockOut(orgid int64, recordtime int64, patientId int64) (prepare []*models.XtDialysisBeforePrepare, err error) {
  3594. db := XTReadDB().Table("dialysis_before_prepare as x").Where("x.status = 1")
  3595. if orgid > 0 {
  3596. db = db.Where("x.user_org_id = ?", orgid)
  3597. }
  3598. if recordtime > 0 {
  3599. db = db.Where("x.record_date = ?", recordtime)
  3600. }
  3601. if patientId > 0 {
  3602. db = db.Where("x.patient_id = ?", patientId)
  3603. }
  3604. 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
  3605. return prepare, err
  3606. }
  3607. func GetAutoReduceRecordInfoById(orgid int64, recordtime int64, goodid int64, goodtypeid int64) (*models.XtAutomaticReduceDetail, error) {
  3608. detail := models.XtAutomaticReduceDetail{}
  3609. 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
  3610. if err == gorm.ErrRecordNotFound {
  3611. return nil, err
  3612. }
  3613. if err != nil {
  3614. return nil, err
  3615. }
  3616. return &detail, nil
  3617. }
  3618. func GetOutStockTotalCount(wareouttime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3619. 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
  3620. return autoMatic, err
  3621. }
  3622. func GetOutStockTotalCountOne(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3623. 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
  3624. return autoMatic, err
  3625. }
  3626. func GetManufacturerName(orgid int64, name string) (*models.Manufacturer, error) {
  3627. var manufacturer models.Manufacturer
  3628. var err error
  3629. err = XTReadDB().Model(&manufacturer).Where("org_id = ? and manufacturer_name = ? and status = 1", orgid, name).Find(&manufacturer).Error
  3630. if err == gorm.ErrRecordNotFound {
  3631. return nil, err
  3632. }
  3633. if err != nil {
  3634. return nil, err
  3635. }
  3636. return &manufacturer, nil
  3637. }
  3638. func CreateManufacturer(manufacturer *models.Manufacturer) error {
  3639. err := XTWriteDB().Create(manufacturer).Error
  3640. return err
  3641. }
  3642. func UpdatedManufactur(manufacturer *models.Manufacturer, orgid int64) error {
  3643. err := XTWriteDB().Model(&manufacturer).Where("org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"manufacturer_name": manufacturer.ManufacturerName}).Error
  3644. return err
  3645. }
  3646. func GetGoodsInformaitonIsExist(goodName string, goodType int64, specificationName string, orgid int64) (*models.GoodInfo, error) {
  3647. var good models.GoodInfo
  3648. var err error
  3649. 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
  3650. if err == gorm.ErrRecordNotFound {
  3651. return nil, err
  3652. }
  3653. if err != nil {
  3654. return nil, err
  3655. }
  3656. return &good, nil
  3657. }
  3658. func CreateGoodsInfomation(info *models.GoodInfo) error {
  3659. err := XTWriteDB().Create(&info).Error
  3660. return err
  3661. }
  3662. func UpdateGoodsInformation(info *models.GoodInfo, goodName string, goodType int64, specificationName string, orgid int64) error {
  3663. 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
  3664. return err
  3665. }
  3666. func GetCoutWareseOutInfo(startime int64, endtime int64, orgid int64) (houseOutInfo []*models.VmWarehouseOutInfo, err error) {
  3667. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  3668. 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
  3669. return houseOutInfo, err
  3670. }
  3671. func GetGoodInfomationList(orgid int64, keyword string) (goodInfo []*models.GoodInfo, err error) {
  3672. key := "%" + keyword + "%"
  3673. if len(keyword) == 0 {
  3674. err = XTReadDB().Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Find(&goodInfo).Error
  3675. } else {
  3676. err = XTReadDB().Where("org_id = ? and status = 1 AND good_name like ? AND find_in_set('停用',good_status) = 0", orgid, key).Find(&goodInfo).Error
  3677. }
  3678. return goodInfo, err
  3679. }
  3680. func IsExistDrugByName(drugname string, drugdesc string, orgid int64) (*models.BaseDrugLib, error) {
  3681. var drug models.BaseDrugLib
  3682. var err error
  3683. err = XTReadDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and status =1 and org_id = ?", drugname, drugdesc, orgid).Find(&drug).Error
  3684. if err == gorm.ErrRecordNotFound {
  3685. return nil, err
  3686. }
  3687. if err != nil {
  3688. return nil, err
  3689. }
  3690. return &drug, nil
  3691. }
  3692. func IsExistDrugByNameOne(drugname string, dosage string, doseunit string, minnumber int64, minunit string, packtunit string, orgid int64) (*models.BaseDrugLib, error) {
  3693. var drug models.BaseDrugLib
  3694. var err error
  3695. 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
  3696. if err == gorm.ErrRecordNotFound {
  3697. return nil, err
  3698. }
  3699. if err != nil {
  3700. return nil, err
  3701. }
  3702. return &drug, nil
  3703. }
  3704. func CreateDrugsInfomation(info *models.BaseDrugLib) error {
  3705. err := XTWriteDB().Create(&info).Error
  3706. return err
  3707. }
  3708. func UpdateDrugsInformation(drug *models.BaseDrugLib, drugName string, drugSpec string, orgid int64) error {
  3709. 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
  3710. return err
  3711. }
  3712. func GetProjectByNameIsExsit(projectName string, orgid int64) (*models.HisProject, error) {
  3713. var projoect models.HisProject
  3714. var err error
  3715. err = XTReadDB().Model(&projoect).Where("project_name = ? and user_org_id = ? and status =1", projectName, orgid).Find(&projoect).Error
  3716. if err == gorm.ErrRecordNotFound {
  3717. return nil, err
  3718. }
  3719. if err != nil {
  3720. return nil, err
  3721. }
  3722. return &projoect, nil
  3723. }
  3724. func CreateProjectInfomation(info *models.HisProject) error {
  3725. err := XTWriteDB().Create(&info).Error
  3726. return err
  3727. }
  3728. func UpdateProjectByName(project *models.HisProject, projectName string, orgid int64) error {
  3729. 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
  3730. return err
  3731. }
  3732. func GetWareOutInfoById(warehouseoutid int64, orgid int64) (warehouseinfo []*models.WarehouseOutInfo, err error) {
  3733. err = XTReadDB().Model(&warehouseinfo).Where("warehouse_out_id = ? and org_id = ? and status= 1 and is_sys = 0", warehouseoutid, orgid).Find(&warehouseinfo).Error
  3734. return warehouseinfo, err
  3735. }
  3736. func EditDrugWarehousingOne(warehouse models.DrugWarehouse, id int64) error {
  3737. 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
  3738. return err
  3739. }
  3740. func EditNewDrugWarehousingOne(warehouse models.DrugWarehouse, id int64, tx *gorm.DB) error {
  3741. 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
  3742. if err != nil {
  3743. tx.Rollback()
  3744. return err
  3745. }
  3746. return err
  3747. }
  3748. func GetAllManufacturerList(orgid int64) (list []*models.Manufacturer, err error) {
  3749. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3750. return list, err
  3751. }
  3752. func GetNewAllManufacturerList(orgid int64) (list []*models.BloodManufacturer, err error) {
  3753. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3754. return list, err
  3755. }
  3756. func GetAllDealerList(orgid int64) (list []*models.Dealer, err error) {
  3757. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3758. return list, err
  3759. }
  3760. // func GetAllDealerListTX(orgid int64,tx *gorm.DB) (list []*models.Dealer, err error) {
  3761. // err = tx.Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3762. // return list, err
  3763. // }
  3764. func GetDealerByName(orgid int64, name string) (*models.Dealer, error) {
  3765. var dealer models.Dealer
  3766. var err error
  3767. err = XTReadDB().Model(&dealer).Where("org_id = ? and dealer_name = ? and status = 1", orgid, name).Find(&dealer).Error
  3768. if err == gorm.ErrRecordNotFound {
  3769. return nil, err
  3770. }
  3771. if err != nil {
  3772. return nil, err
  3773. }
  3774. return &dealer, nil
  3775. }
  3776. func CreateDealer(dealer *models.Dealer) error {
  3777. err := XTWriteDB().Create(&dealer).Error
  3778. return err
  3779. }
  3780. func UpdateDealer(dealer *models.Dealer, orgid int64) error {
  3781. err := XTWriteDB().Model(&dealer).Where("org_id = ? and status =1", orgid).Updates(map[string]interface{}{"dealer_name": dealer.DealerName}).Error
  3782. return err
  3783. }
  3784. func GetDrugDataConfig(orgid int64, name string) (models.DictDataconfig, error) {
  3785. dataconfig := models.DictDataconfig{}
  3786. err := XTReadDB().Where("org_id = ? and name = ?", orgid, name).Find(&dataconfig).Error
  3787. return dataconfig, err
  3788. }
  3789. func GetParentDataConfig(parentid int64, orgid int64) (dataconfig []*models.DictDataconfig, err error) {
  3790. err = XTReadDB().Where("parent_id = ? and status =1 and (org_id = ? or org_id = 0)", parentid, orgid).Find(&dataconfig).Error
  3791. return dataconfig, err
  3792. }
  3793. func IsExistDicConfig(parentid int64, goodKind string, orgid int64) (*models.DictDataconfig, error) {
  3794. var dataconfig models.DictDataconfig
  3795. var err error
  3796. err = XTReadDB().Where("parent_id = ? and status = 1 and name = ? and (org_id = ? or org_id = 0)", parentid, goodKind, orgid).Find(&dataconfig).Error
  3797. if err == gorm.ErrRecordNotFound {
  3798. return nil, err
  3799. }
  3800. if err != nil {
  3801. return nil, err
  3802. }
  3803. return &dataconfig, nil
  3804. }
  3805. func GetLastDicConfig(parentid int64, orgid int64) (models.DictDataconfig, error) {
  3806. dataconfig := models.DictDataconfig{}
  3807. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status = 1", parentid, orgid).Last(&dataconfig).Error
  3808. return dataconfig, err
  3809. }
  3810. func CreatedDicConfig(dataconfig *models.DictDataconfig) error {
  3811. err := XTWriteDB().Create(&dataconfig).Error
  3812. return err
  3813. }
  3814. func UpdatedDicConfig(parentid int64, dataconfig *models.DictDataconfig, orgid int64, goodKind string) error {
  3815. err := XTWriteDB().Model(&dataconfig).Where("parent_id = ? and org_id = ? and name = ?", parentid, orgid, goodKind).Updates(map[string]interface{}{"name": dataconfig.Name}).Error
  3816. return err
  3817. }
  3818. func GetGoodType(goodName string, orgid int64) (*models.GoodsType, error) {
  3819. var goodType models.GoodsType
  3820. var err error
  3821. err = XTReadDB().Where("type_name =? and org_id = ? and status = 1", goodName, orgid).Find(&goodType).Error
  3822. if err == gorm.ErrRecordNotFound {
  3823. return nil, err
  3824. }
  3825. if err != nil {
  3826. return nil, err
  3827. }
  3828. return &goodType, nil
  3829. }
  3830. func CreatedGoodType(goodsType *models.GoodsType) error {
  3831. err := XTWriteDB().Create(&goodsType).Error
  3832. return err
  3833. }
  3834. func GetAllGoodType(orgid int64) (goodsType []*models.GoodsType, err error) {
  3835. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3836. return goodsType, err
  3837. }
  3838. func GetAllNewGoodType(orgid int64) (goodsType []*models.NewGoodsType, err error) {
  3839. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3840. return goodsType, err
  3841. }
  3842. func GetDepartMentByName(name string, orgid int64) (*models.XtHisDepartment, error) {
  3843. var department models.XtHisDepartment
  3844. var err error
  3845. err = XTReadDB().Where("name= ? and user_org_id = ? and status = 1", name, orgid).Find(&department).Error
  3846. if err == gorm.ErrRecordNotFound {
  3847. return nil, err
  3848. }
  3849. if err != nil {
  3850. return nil, err
  3851. }
  3852. return &department, nil
  3853. }
  3854. func GetDataConfigIsExist(orgid int64, name string) (models.Dataconfig, error) {
  3855. dataconfig := models.Dataconfig{}
  3856. err := XTReadDB().Model(&dataconfig).Where("org_id = ? and name = ? and status = 1", orgid, name).Find(&dataconfig).Error
  3857. return dataconfig, err
  3858. }
  3859. func GetChildeConfigIsExist(parentid int64, name string, orgid int64) (*models.Dataconfig, error) {
  3860. var dataconfig models.Dataconfig
  3861. var err error
  3862. 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
  3863. if err == gorm.ErrRecordNotFound {
  3864. return nil, err
  3865. }
  3866. if err != nil {
  3867. return nil, err
  3868. }
  3869. return &dataconfig, nil
  3870. }
  3871. func GetLastChildeConfig(parentid int64, orgid int64) (models.Dataconfig, error) {
  3872. dataconfig := models.Dataconfig{}
  3873. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status =1", parentid, orgid).Last(&dataconfig).Error
  3874. return dataconfig, err
  3875. }
  3876. func CreateDataConfig(dataconfig *models.Dataconfig) error {
  3877. err := XTWriteDB().Create(&dataconfig).Error
  3878. return err
  3879. }
  3880. func FindAllDataConfigList(orgid int64, parentId int64) (dataconfig []*models.Dataconfig, err error) {
  3881. err = XTReadDB().Model(&dataconfig).Where("(org_id = ? or org_id = 0) and parent_id = ? and status = 1", orgid, parentId).Find(&dataconfig).Error
  3882. return dataconfig, err
  3883. }
  3884. func GetWarehouseOrderInfoList(orgid int64) (list []*models.WarehousingInfo, err error) {
  3885. err = XTReadDB().Model(&list).Where("org_id = ? and status = 1", orgid).Order("id asc").Group("good_id").Find(&list).Error
  3886. return list, err
  3887. }
  3888. func GetSearchDrugList(keywords string, orgid int64) (drug []*models.BaseDrugLib, err error) {
  3889. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3890. if len(keywords) > 0 {
  3891. likekey := "%" + keywords + "%"
  3892. err = db.Where("(drug_name LIKE ?)", likekey).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3893. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3894. }).Find(&drug).Error
  3895. } else {
  3896. err = db.Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3897. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3898. }).Find(&drug).Error
  3899. }
  3900. return
  3901. }
  3902. func GetSearchDrugListSix(keywords string, orgid int64, storehouse_id int64) (drug []*models.BaseDrugLib, err error) {
  3903. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3904. if len(keywords) > 0 {
  3905. likekey := "%" + keywords + "%"
  3906. // 转义特殊字符(如 % _ \)
  3907. //escapedKeywords := strings.ReplaceAll(keywords, "%", "\\%")
  3908. //escapedKeywords = strings.ReplaceAll(escapedKeywords, "_", "\\_")
  3909. //escapedKeywords = strings.ReplaceAll(escapedKeywords, "\\", "\\\\")
  3910. //likekey := "%" + escapedKeywords + "%"
  3911. err = db.Where("(drug_name LIKE ? ESCAPE '\\\\')", 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 {
  3912. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3913. }).Find(&drug).Error
  3914. } else {
  3915. 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 {
  3916. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3917. }).Find(&drug).Error
  3918. }
  3919. return
  3920. }
  3921. func GetSearchGoodList(keywords string, orgid int64) (good []*models.GoodInfo, err error) {
  3922. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 and good_status <> '停用'", orgid)
  3923. if len(keywords) > 0 {
  3924. likekey := "%" + keywords + "%"
  3925. err = db.Where("(good_name Like ?)", likekey).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3926. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3927. }).Find(&good).Error
  3928. } else {
  3929. err = db.Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3930. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3931. }).Find(&good).Error
  3932. }
  3933. return
  3934. }
  3935. func GetSearchGoodListSix(keywords string, orgid int64, storehouse_id int64) (good []*models.GoodInfoSevenThirty, err error) {
  3936. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 AND find_in_set('停用',good_status) = 0", orgid)
  3937. if len(keywords) > 0 {
  3938. likekey := "%" + keywords + "%"
  3939. // 转义特殊字符(如 % _ \)
  3940. //escapedKeywords := strings.ReplaceAll(keywords, "%", "\\%")
  3941. //escapedKeywords = strings.ReplaceAll(escapedKeywords, "_", "\\_")
  3942. //escapedKeywords = strings.ReplaceAll(escapedKeywords, "\\", "\\\\")
  3943. //likekey := "%" + escapedKeywords + "%"
  3944. 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 {
  3945. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id =?", orgid, storehouse_id)
  3946. }).Find(&good).Error
  3947. } else {
  3948. 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 {
  3949. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id)
  3950. }).Find(&good).Error
  3951. }
  3952. return
  3953. }
  3954. func CreateAutoReduceRecord(detail *models.BloodAutomaticReduceDetail) error {
  3955. db := writeDb.Begin()
  3956. err := db.Create(&detail).Error
  3957. if err != nil {
  3958. db.Rollback()
  3959. return err
  3960. }
  3961. db.Commit()
  3962. return err
  3963. }
  3964. func GetAutoMaticReduceDetail(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (*models.BloodAutomaticReduceDetail, error) {
  3965. var autoreduece models.BloodAutomaticReduceDetail
  3966. var err error
  3967. 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
  3968. if err == gorm.ErrRecordNotFound {
  3969. return nil, err
  3970. }
  3971. if err != nil {
  3972. return nil, err
  3973. }
  3974. return &autoreduece, nil
  3975. }
  3976. func GetAutoMaticReduceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (models.BloodAutomaticReduceDetail, error) {
  3977. auto := models.BloodAutomaticReduceDetail{}
  3978. 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
  3979. return auto, err
  3980. }
  3981. func DeleteAutoRedeceDetailTwo(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) error {
  3982. detail := models.BloodAutomaticReduceDetail{}
  3983. 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
  3984. return err
  3985. }
  3986. func GetWarehosueOutByStockFlow(patient_id int64, recrod_date int64, goodid int64) (stockflow []*models.VmStockFlow, err error) {
  3987. 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
  3988. return stockflow, err
  3989. }
  3990. func UpdateAutoGood(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64, count int64) (err error) {
  3991. 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
  3992. 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
  3993. 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
  3994. return err
  3995. }
  3996. func DeleteAutoRedeceDetailTen(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3997. detail := models.BloodAutomaticReduceDetail{}
  3998. 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
  3999. info := models.WarehouseOutInfo{}
  4000. 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
  4001. prepare := models.DialysisBeforePrepare{}
  4002. 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
  4003. flow := models.VmStockFlow{}
  4004. 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
  4005. return err
  4006. }
  4007. func DeleteAutoRedeceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  4008. detail := models.BloodAutomaticReduceDetail{}
  4009. 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
  4010. info := models.WarehouseOutInfo{}
  4011. 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
  4012. prepare := models.DialysisBeforePrepare{}
  4013. 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
  4014. flow := models.VmStockFlow{}
  4015. 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
  4016. return err
  4017. }
  4018. func DeleteAutoRedeceDetailTenOne(orgid int64, patient_id int64, recordtime int64, goodid int64, order_id int64) error {
  4019. detail := models.BloodAutomaticReduceDetail{}
  4020. 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
  4021. info := models.WarehouseOutInfo{}
  4022. 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
  4023. prepare := models.DialysisBeforePrepare{}
  4024. 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
  4025. flow := models.VmStockFlow{}
  4026. 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
  4027. return err
  4028. }
  4029. func UpDateWarehouseInfoByStockDelete(id int64, count int64, patient_id int64, record_time int64, goodid int64) (err error) {
  4030. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  4031. //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
  4032. return err
  4033. }
  4034. func GetCancelStockInfoByPatientId(patientid int64, goodid int64, record_date int64) (info []*models.CancelStockInfo, err error) {
  4035. err = XTReadDB().Where("patient_id = ? and good_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&info).Error
  4036. return info, err
  4037. }
  4038. func GetHisPrepscritionByPatientId(patientid int64, goodid int64, record_date int64) (list []*models.HisPrescriptionProject, err error) {
  4039. err = XTReadDB().Where("patient_id = ? and project_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&list).Error
  4040. return list, err
  4041. }
  4042. func UpDateWarehouStockFlowByStockDelete(id int64, recordtime int64, good_id int64, count int64, patient_id int64) (err error) {
  4043. 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
  4044. return err
  4045. }
  4046. func UpDateWarehouseInfoByStockDeleteOne(id int64, count int64) (err error) {
  4047. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  4048. return err
  4049. }
  4050. func GetAutoDialysisBefor(goodid int64, goodtypeid int64, patientid int64, orgid int64, recorddate int64) (*models.BloodAutomaticReduceDetail, error) {
  4051. var autoreduece models.BloodAutomaticReduceDetail
  4052. var err error
  4053. 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
  4054. if err == gorm.ErrRecordNotFound {
  4055. return nil, err
  4056. }
  4057. if err != nil {
  4058. return nil, err
  4059. }
  4060. return &autoreduece, nil
  4061. }
  4062. func GetAutoRecordByGoodIdSeven(goodid int64, patientid int64, recorddate int64) (models.WarehouseOutInfoSeven, error) {
  4063. detail := models.WarehouseOutInfoSeven{}
  4064. err := XTReadDB().Select("id,count").Where(" good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  4065. return detail, err
  4066. }
  4067. func GetAutoRecordByGoodIdSevenEight(goodid int64, patientid int64, recorddate int64) (detail []*models.WarehouseOutInfoSeven, err error) {
  4068. err = XTReadDB().Select("id,count").Where("good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  4069. return detail, err
  4070. }
  4071. func GetHisProjectPrescriptionByPatientId(patientid int64, recorddate int64) (project []*models.HisPrescriptionProject, err error) {
  4072. err = XTReadDB().Model(&project).Where("patient_id = ? and record_date = ? and status =1", patientid, recorddate).Find(&project).Error
  4073. return project, err
  4074. }
  4075. func GetHisProjectPrescriptionByPatientIdOne(id int64) (models.HisPrescriptionProject, error) {
  4076. project := models.HisPrescriptionProject{}
  4077. err = XTReadDB().Model(&project).Where("id = ? and status =1", id).Find(&project).Error
  4078. return project, err
  4079. }
  4080. func GetAllAutoRecordByPatient(patientid int64, recorddate int64) (auto []*models.WarehouseOutInfoNight, err error) {
  4081. 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
  4082. return auto, err
  4083. }
  4084. func ModefyWarehouseInfo(count int64, id int64) error {
  4085. err := XTWriteDB().Model(models.WarehousingInfo{}).Where(" id =? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  4086. return err
  4087. }
  4088. func DeleteAutoWarehouse(patient_id int64, record_time int64) error {
  4089. 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
  4090. 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
  4091. 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
  4092. //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
  4093. return err
  4094. }
  4095. func FindGoodInfoByIdTwo(id int64) (goodInfo models.GoodInfo, err error) {
  4096. err = readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  4097. return
  4098. }
  4099. func FindGoodInfoByIdSeven(id int64) (goodInfo models.GoodInfoSeven, err error) {
  4100. err = readDb.Select("id,good_name").Where("id = ? AND status = 1", id).First(&goodInfo).Error
  4101. return
  4102. }
  4103. func GetGoodWarehouseInfo(good_id int64) (info []*models.WarehousingInfo, err error) {
  4104. err = readDb.Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  4105. return info, err
  4106. }
  4107. func GetGoodWarehouseInfoSeven(good_id int64) (info []*models.WarehousingInfoSeven, err error) {
  4108. err = readDb.Select("stock_count").Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  4109. return info, err
  4110. }
  4111. func GetGoodWarehouseInfoSevenTen(good_id int64, storehouse_id int64) (info []*models.WarehousingInfoSeven, err error) {
  4112. 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
  4113. return info, err
  4114. }
  4115. func GetAllStockList(page int64, limit int64, startime int64, endtime int64, good_type int64, keyword string, orgid int64) (info []*models.VmWarehousingInfo, total int64, err error) {
  4116. offset := (page - 1) * limit
  4117. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1")
  4118. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4119. fmt.Println(table)
  4120. if startime > 0 {
  4121. db = db.Where("x.ctime>=?", startime)
  4122. }
  4123. if endtime > 0 {
  4124. db = db.Where("x.ctime<=?", endtime)
  4125. }
  4126. likeKey := "%" + keyword + "%"
  4127. if len(likeKey) > 0 {
  4128. db = db.Where("t.good_name like ?", likeKey)
  4129. }
  4130. if orgid > 0 {
  4131. db = db.Where("x.org_id = ?", orgid)
  4132. }
  4133. if good_type > 0 {
  4134. db = db.Where("t.good_type_id = ?", good_type)
  4135. }
  4136. 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
  4137. return info, total, err
  4138. }
  4139. 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) {
  4140. offset := (page - 1) * limit
  4141. db := XTReadDB().Table("xt_good_information as x").Where("x.status= 1")
  4142. likeKey := "%" + keyword + "%"
  4143. if len(likeKey) > 0 {
  4144. db = db.Where("x.good_name like ?", likeKey)
  4145. }
  4146. if orgid > 0 {
  4147. db = db.Where("x.org_id = ?", orgid)
  4148. }
  4149. if good_type > 0 {
  4150. db = db.Where("x.good_type_id = ?", good_type)
  4151. }
  4152. if good_id > 0 {
  4153. db = db.Where("x.id = ?", good_id)
  4154. }
  4155. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  4156. if startime > 0 {
  4157. db = db.Where("ctime>=?", startime)
  4158. }
  4159. if endtime > 0 {
  4160. db = db.Where("ctime<=?", endtime)
  4161. }
  4162. if good_id > 0 {
  4163. db = db.Where("good_id = ?", good_id)
  4164. }
  4165. if storehouse_id > 0 {
  4166. db = db.Where("storehouse_id = ?", storehouse_id)
  4167. }
  4168. return db.Where("status = 1 and is_check = 1")
  4169. }).Preload("WarehousingInfo", func(db *gorm.DB) *gorm.DB {
  4170. if startime > 0 {
  4171. db = db.Where("ctime>=?", startime)
  4172. }
  4173. if endtime > 0 {
  4174. db = db.Where("ctime<=?", endtime)
  4175. }
  4176. if good_id > 0 {
  4177. db = db.Where("good_id = ?", good_id)
  4178. }
  4179. if storehouse_id > 0 {
  4180. db = db.Where("storehouse_id = ?", storehouse_id)
  4181. }
  4182. return db.Where("status = 1").Group("good_id,storehouse_id")
  4183. }).Preload("CancelStockInfo", func(db *gorm.DB) *gorm.DB {
  4184. if startime > 0 {
  4185. db = db.Where("ctime>=?", startime)
  4186. }
  4187. if endtime > 0 {
  4188. db = db.Where("ctime<=?", endtime)
  4189. }
  4190. return db.Where("status = 1")
  4191. }).Find(&info).Error
  4192. return info, total, err
  4193. }
  4194. 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) {
  4195. offset := (page - 1) * limit
  4196. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1 and x.is_check = 1")
  4197. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4198. tables := XTReadDB().Table("xt_stock_flow as f").Where("f.status = 1 and f.consumable_type = 1 and f.user_org_id = ?", orgid)
  4199. fmt.Println(table, tables)
  4200. if startime > 0 {
  4201. db = db.Where("x.ctime>=?", startime)
  4202. }
  4203. if endtime > 0 {
  4204. db = db.Where("x.ctime<=?", endtime)
  4205. }
  4206. if startfirsttime > 0 {
  4207. db = db.Where("x.expiry_date >=?", startfirsttime)
  4208. }
  4209. if endfirsttime > 0 {
  4210. db = db.Where("x.expiry_date <=?", endfirsttime)
  4211. }
  4212. if orgid > 0 {
  4213. db = db.Where("x.org_id = ?", orgid)
  4214. }
  4215. if good_id > 0 {
  4216. db = db.Where("x.good_id = ?", good_id)
  4217. }
  4218. 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
  4219. return info, total, err
  4220. }
  4221. 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) {
  4222. offset := (page - 1) * limit
  4223. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status= 1")
  4224. if startime > 0 {
  4225. db = db.Where("x.ctime>=?", startime)
  4226. }
  4227. if endtime > 0 {
  4228. db = db.Where("x.ctime<=?", endtime)
  4229. }
  4230. if orgid > 0 {
  4231. db = db.Where("x.org_id = ?", orgid)
  4232. }
  4233. if good_id > 0 {
  4234. db = db.Where("x.good_id = ?", good_id)
  4235. }
  4236. if is_sys == 2 {
  4237. db = db.Where("x.is_sys = 1")
  4238. }
  4239. if is_sys == 3 {
  4240. db = db.Where("x.is_sys = 0")
  4241. }
  4242. 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
  4243. return info, total, err
  4244. }
  4245. func GetStockDrugCount(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  4246. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4247. if startime > 0 {
  4248. db = db.Where("x.ctime >=?", startime)
  4249. }
  4250. if endtime > 0 {
  4251. db = db.Where("x.ctime<=?", endtime)
  4252. }
  4253. if orgid > 0 {
  4254. db = db.Where("x.org_id = ?", orgid)
  4255. }
  4256. 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
  4257. return info, err
  4258. }
  4259. func GetAutoDiallysisBefor(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  4260. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  4261. if startime > 0 {
  4262. db = db.Where("x.ctime >=?", startime)
  4263. }
  4264. if endtime > 0 {
  4265. db = db.Where("x.ctime<=?", endtime)
  4266. }
  4267. if orgid > 0 {
  4268. db = db.Where("x.org_id = ?", orgid)
  4269. }
  4270. err = db.Select("sum(x.count) as count,x.good_id").Where("is_sys = 0").Group("x.good_id").Scan(&info).Error
  4271. return info, err
  4272. }
  4273. func GetOutStockTotalCountFour(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  4274. if endtime > 0 {
  4275. 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
  4276. }
  4277. if endtime == 0 {
  4278. 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
  4279. }
  4280. return autoMatic, err
  4281. }
  4282. func GetGoodWarehouseOutById(id int64, orgid int64) (models.WarehouseOut, error) {
  4283. out := models.WarehouseOut{}
  4284. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", id, orgid).Find(&out).Error
  4285. return out, err
  4286. }
  4287. func GetOrderDetialByOrderId(id int64, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  4288. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  4289. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4290. fmt.Println(table)
  4291. if id > 0 {
  4292. db = db.Where("x.warehouse_out_id = ?", id)
  4293. }
  4294. if orgid > 0 {
  4295. db = db.Where("x.org_id = ?", orgid)
  4296. }
  4297. 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
  4298. return out, err
  4299. }
  4300. func GetWarehouseOutOrder(id []string, orgid int64) (out []*models.WarehouseOut, err error) {
  4301. db := XTReadDB().Table("xt_warehouse_out as x").Where("x.status = 1")
  4302. if len(id) > 0 {
  4303. db = db.Where("x.id in(?)", id)
  4304. }
  4305. if orgid > 0 {
  4306. db = db.Where("x.org_id= ?", orgid)
  4307. }
  4308. 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
  4309. return out, err
  4310. }
  4311. func GetOrderDetialByOrderIdOne(id []string, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  4312. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  4313. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4314. tableOne := XTReadDB().Table("xt_warehouse_info as n").Where("n.status = 1")
  4315. fmt.Println(table, tableOne)
  4316. if len(id) > 0 {
  4317. db = db.Where("x.warehouse_out_id in (?)", id)
  4318. }
  4319. if orgid > 0 {
  4320. db = db.Where("x.org_id = ?", orgid)
  4321. }
  4322. 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
  4323. return out, err
  4324. }
  4325. func GetOrderDetailStockFlowByStorehouseById(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  4326. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  4327. if len(id) > 0 {
  4328. db = db.Where("warehouse_out_id in (?)", id)
  4329. }
  4330. if orgid > 0 {
  4331. db = db.Where("user_org_id = ?", orgid)
  4332. }
  4333. err = db.Group("warehousing_id").Find(&list).Error
  4334. return list, err
  4335. }
  4336. func GetOrderDetailStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  4337. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  4338. if len(id) > 0 {
  4339. db = db.Where("warehouse_out_id in (?)", id)
  4340. }
  4341. if orgid > 0 {
  4342. db = db.Where("user_org_id = ?", orgid)
  4343. }
  4344. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  4345. return list, err
  4346. }
  4347. func GetOrderStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  4348. db := XTReadDB().Model(&list).Where("status = 1 and consumable_type = 7 and patient_id > 0 and count>0")
  4349. if len(id) > 0 {
  4350. db = db.Where("warehouse_out_id in (?)", id)
  4351. }
  4352. if orgid > 0 {
  4353. db = db.Where("user_org_id = ?", orgid)
  4354. }
  4355. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  4356. return list, err
  4357. }
  4358. func GetLastWarehouseOutInfo(ids []string, orgId int64) (info []*models.WarehouseOutInfoSix, err error) {
  4359. db := XTReadDB().Model(&info)
  4360. db = db.Preload("GoodInfo", "status = 1")
  4361. 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
  4362. return info, err
  4363. }
  4364. func FindUserDetailByIdOne(good_id int64, record_time int64, org_id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  4365. db := readDb.Model(&models.AutomaticReduceDetail{})
  4366. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  4367. db = db.Preload("GoodsType", "status = 1")
  4368. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  4369. db = db.Preload("WarehouseOutInfo", "org_id = ? and status = 1 and good_id = ? and sys_record_time = ?", org_id, good_id, record_time)
  4370. db = db.Where("status = 1 AND org_id = ? AND good_id = ? AND record_time = ? and count<> 0 ", org_id, good_id, record_time)
  4371. db = db.Count(&total)
  4372. err = db.Find(&user).Error
  4373. return
  4374. }
  4375. func GetGoodInformationByGoodId(good_id int64) (models.GoodInfo, error) {
  4376. info := models.GoodInfo{}
  4377. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  4378. return info, err
  4379. }
  4380. func GetNewGoodInformationByGoodId(good_id int64, tx *gorm.DB) (models.GoodInfo, error) {
  4381. info := models.GoodInfo{}
  4382. err := tx.Where("id = ? and status = 1", good_id).Find(&info).Error
  4383. if err != gorm.ErrRecordNotFound {
  4384. if err != nil {
  4385. tx.Rollback()
  4386. return info, err
  4387. }
  4388. }
  4389. return info, err
  4390. }
  4391. func GetGoodInformationByGoodIdThirty(good_id int64) (models.GoodInformationThirty, error) {
  4392. info := models.GoodInformationThirty{}
  4393. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  4394. return info, err
  4395. }
  4396. func GetNewGoodInformationByGoodIdThirty(good_id int64, tx *gorm.DB) (models.GoodInformationThirty, error) {
  4397. info := models.GoodInformationThirty{}
  4398. err := tx.Where("id = ? and status = 1", good_id).Find(&info).Error
  4399. if err != gorm.ErrRecordNotFound {
  4400. if err != nil {
  4401. tx.Rollback()
  4402. return info, err
  4403. }
  4404. }
  4405. return info, err
  4406. }
  4407. func UpdateGoodInfoList(info models.GoodInfo, id int64) error {
  4408. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"total": info.Total}).Error
  4409. return err
  4410. }
  4411. func GetExprotStockList(orgid int64, orderid []string, startime int64, endtime int64) (info []*models.StWarehousingInfo, err error) {
  4412. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4413. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4414. fmt.Println(table)
  4415. if orgid > 0 {
  4416. db = db.Where("x.org_id = ?", orgid)
  4417. }
  4418. if len(orderid) > 0 {
  4419. db = db.Where("x.warehousing_id in(?)", orderid)
  4420. }
  4421. if startime > 0 {
  4422. db = db.Where("x.ctime>=?", startime)
  4423. }
  4424. if endtime > 0 {
  4425. db = db.Where("x.ctime<=?", endtime)
  4426. }
  4427. 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
  4428. return info, err
  4429. }
  4430. func GetExprotStockListTwenty(orgid int64, orderid []string, startime int64, endtime int64, storehouse_id int64) (info []*models.StWarehousingInfo, err error) {
  4431. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4432. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4433. fmt.Println(table)
  4434. if orgid > 0 {
  4435. db = db.Where("x.org_id = ?", orgid)
  4436. }
  4437. if len(orderid) > 0 {
  4438. db = db.Where("x.warehousing_id in(?)", orderid)
  4439. }
  4440. if startime > 0 {
  4441. db = db.Where("x.ctime>=?", startime)
  4442. }
  4443. if endtime > 0 {
  4444. db = db.Where("x.ctime<=?", endtime)
  4445. }
  4446. if storehouse_id > 0 {
  4447. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4448. }
  4449. 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
  4450. return info, err
  4451. }
  4452. func GetDrugWarehouOrderInfo(startime int64, endtime int64, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  4453. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4454. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  4455. fmt.Println(table)
  4456. if startime > 0 {
  4457. db = db.Where("x.ctime>=?", startime)
  4458. }
  4459. if endtime > 0 {
  4460. db = db.Where("x.ctime<=?", endtime)
  4461. }
  4462. if orgid > 0 {
  4463. db = db.Where("x.org_id = ?", orgid)
  4464. }
  4465. 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
  4466. return info, err
  4467. }
  4468. func GetOutExprotList(orgid int64, ids []string, startime int64, endtime int64) (out []*models.WarehouseOutInfoOne, err error) {
  4469. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1 ")
  4470. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4471. tableOne := XTReadDB().Table("xt_warehouse_out as p").Where("p.status=1")
  4472. fmt.Println(table, tableOne)
  4473. if orgid > 0 {
  4474. db = db.Where("x.org_id = ?", orgid)
  4475. }
  4476. if orgid > 0 {
  4477. db = db.Where("t.org_id = ?", orgid)
  4478. }
  4479. if orgid > 0 {
  4480. db = db.Where("p.org_id = ?", orgid)
  4481. }
  4482. if len(ids) > 0 {
  4483. db = db.Where("x.warehouse_out_id in (?)", ids)
  4484. }
  4485. if startime > 0 {
  4486. db = db.Where("x.ctime>=?", startime)
  4487. }
  4488. if endtime > 0 {
  4489. db = db.Where("x.ctime<=?", endtime)
  4490. }
  4491. 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
  4492. return out, err
  4493. }
  4494. func FindeDrugWarehouseOutOrder(ids []string, orgid int64) (info []*models.DrugWarehouseOut, err error) {
  4495. db := XTReadDB().Table("xt_drug_warehouse_out as x").Where("x.status = 1")
  4496. if len(ids) > 0 {
  4497. db = db.Where("x.id in(?)", ids)
  4498. }
  4499. if orgid > 0 {
  4500. db = db.Where("x.org_id = ?", orgid)
  4501. }
  4502. 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
  4503. return info, err
  4504. }
  4505. func FindeDrugWarehouseOutDetail(ids []string, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  4506. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status =1 ")
  4507. table := XTReadDB().Table("x.base_drug as t").Where("t.status = 1")
  4508. fmt.Println(table)
  4509. if len(ids) > 0 {
  4510. db = db.Where("x.warehouse_out_id in(?)", ids)
  4511. }
  4512. if orgid > 0 {
  4513. db = db.Where("x.org_id = ?", orgid)
  4514. }
  4515. 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
  4516. return info, err
  4517. }
  4518. func GetDrugNameByKeyword(keyword string, orgid int64) (basedrug []*models.XtBaseDrug, err error) {
  4519. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1")
  4520. likeKey := "%" + keyword + "%"
  4521. if len(keyword) > 0 {
  4522. db = db.Where("x.drug_name like ?", likeKey)
  4523. }
  4524. if orgid > 0 {
  4525. db = db.Where("x.org_id = ?", orgid)
  4526. }
  4527. err = db.Find(&basedrug).Error
  4528. return basedrug, err
  4529. }
  4530. func GetDrugWarehouseOrderDetail(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  4531. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4532. if len(ids) > 0 {
  4533. db = db.Where("x.drug_id in(?)", ids)
  4534. }
  4535. if orgid > 0 {
  4536. db = db.Where("x.org_id = ?", orgid)
  4537. }
  4538. if startime > 0 {
  4539. db = db.Where("x.ctime>=?", startime)
  4540. }
  4541. if endtime > 0 {
  4542. db = db.Where("x.ctime<=?", endtime)
  4543. }
  4544. if storehouse_id > 0 {
  4545. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4546. }
  4547. err = db.Find(&info).Error
  4548. return info, err
  4549. }
  4550. func GetDrugWarehouseOrderDetailTwo(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  4551. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4552. if len(ids) > 0 {
  4553. db = db.Where("x.drug_id in(?)", ids)
  4554. }
  4555. if orgid > 0 {
  4556. db = db.Where("x.org_id = ?", orgid)
  4557. }
  4558. if startime > 0 {
  4559. db = db.Where("x.ctime>=?", startime)
  4560. }
  4561. if endtime > 0 {
  4562. db = db.Where("x.ctime<=?", endtime)
  4563. }
  4564. if storehouse_id > 0 {
  4565. db = db.Where("storehouse_id = ?", storehouse_id)
  4566. }
  4567. err = db.Find(&info).Error
  4568. return info, err
  4569. }
  4570. func GetDrugTotalCount(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4571. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  4572. if drugid > 0 {
  4573. db = db.Where("x.drug_id = ?", drugid)
  4574. }
  4575. if orgid > 0 {
  4576. db = db.Where("x.org_id = ?", orgid)
  4577. }
  4578. if storehouse_id > 0 {
  4579. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4580. }
  4581. 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
  4582. return info, err
  4583. }
  4584. func GetDrugTotalCountTwoTwety(drugid int64, orgid int64, storehouse_id int64, tx *gorm.DB) (info models.VmDrugWarehouseInfo, err error) {
  4585. db := tx.Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  4586. if drugid > 0 {
  4587. db = db.Where("x.drug_id = ?", drugid)
  4588. }
  4589. if orgid > 0 {
  4590. db = db.Where("x.org_id = ?", orgid)
  4591. }
  4592. if storehouse_id > 0 {
  4593. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4594. }
  4595. 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
  4596. if err != gorm.ErrRecordNotFound {
  4597. if err != nil {
  4598. tx.Rollback()
  4599. return info, err
  4600. }
  4601. }
  4602. return info, err
  4603. }
  4604. func GetDrugTotalCountTwenty(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4605. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1 ")
  4606. if drugid > 0 {
  4607. db = db.Where("x.drug_id = ?", drugid)
  4608. }
  4609. if orgid > 0 {
  4610. db = db.Where("x.org_id = ?", orgid)
  4611. }
  4612. if storehouse_id > 0 {
  4613. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4614. }
  4615. 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
  4616. return info, err
  4617. }
  4618. func GetDrugTotalCountSwx(id int64, orgid int64) (info models.VmDrugWarehouseInfo, err error) {
  4619. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4620. if id > 0 {
  4621. db = db.Where("x.id = ?", id)
  4622. }
  4623. if orgid > 0 {
  4624. db = db.Where("x.org_id = ?", orgid)
  4625. }
  4626. 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
  4627. return info, err
  4628. }
  4629. func GetDrugTotalCountOne(drugid int64, orgid int64, id int64) (models.VmDrugWarehouseInfo, error) {
  4630. info := models.VmDrugWarehouseInfo{}
  4631. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4632. if id == 0 {
  4633. db = db.Where("id = ?", id)
  4634. }
  4635. if drugid > 0 {
  4636. db = db.Where("x.drug_id = ?", drugid)
  4637. }
  4638. if orgid > 0 {
  4639. db = db.Where("x.org_id = ?", orgid)
  4640. }
  4641. err = db.Select("x.drug_id,x.stock_max_number,x.stock_min_number,x.max_unit,x.min_unit").Find(&info).Error
  4642. return info, err
  4643. }
  4644. func GetDrugTotalCountTwo(drugid int64, storehouse_id int64) (list []*models.VmDrugWarehouseInfo, err error) {
  4645. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4646. table := XTReadDB().Table("xt_base_drug as d").Where("d.status = 1")
  4647. fmt.Println(table)
  4648. if drugid > 0 {
  4649. db = db.Where("x.drug_id = ?", drugid)
  4650. }
  4651. if storehouse_id > 0 {
  4652. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4653. }
  4654. 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
  4655. return list, err
  4656. }
  4657. func GetDrugByGoodId(drugid int64) (models.BaseDrugLib, error) {
  4658. lib := models.BaseDrugLib{}
  4659. err := XTReadDB().Model(&lib).Where("id = ? and status = 1", drugid).Find(&lib).Error
  4660. return lib, err
  4661. }
  4662. func GetWarehoureOrderInfoList(goodid int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4663. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4664. if goodid > 0 {
  4665. db = db.Where("x.good_id = ?", goodid)
  4666. }
  4667. if orgid > 0 {
  4668. db = db.Where("x.org_id = ?", orgid)
  4669. }
  4670. 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
  4671. return info, err
  4672. }
  4673. func UpdateWarehousingCount(info *models.WarehousingInfo, goodid int64, orgid int64, warehousingId int64) error {
  4674. 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
  4675. return err
  4676. }
  4677. func GetSingleCancelOrder(id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  4678. err = XTReadDB().Where("cancel_stock_id = ? and status = 1 and org_id = ?", id, orgid).Preload("GoodInfo", "status = 1").Find(&info).Error
  4679. return info, err
  4680. }
  4681. func UpdateCancelOut(list []*models.WarehousingInfo, count int64) error {
  4682. //遍历循环入库数据
  4683. for i, item := range list {
  4684. //计算每个批次剩余库存与入库数据的差 然后和退库数量比较
  4685. if (item.WarehousingCount - item.StockCount) >= count {
  4686. //如果差距大于退库数量,则直接把退库数量增加到该批次中
  4687. info := models.WarehousingInfo{
  4688. GoodId: item.GoodId,
  4689. OrgId: item.OrgId,
  4690. WarehousingId: item.WarehousingId,
  4691. StockCount: item.StockCount + count,
  4692. }
  4693. err2 := UpdateWarehousingCount(&info, item.GoodId, item.OrgId, item.WarehousingId)
  4694. fmt.Println("2323233232323232323232323232323", err2)
  4695. return err
  4696. }
  4697. if (item.WarehousingCount - item.StockCount) < count {
  4698. //如果该批次小于退库数量,则先出库该相差数量
  4699. var countOne = item.WarehousingCount - item.StockCount
  4700. infoOne := models.WarehousingInfo{
  4701. GoodId: item.GoodId,
  4702. OrgId: item.OrgId,
  4703. WarehousingId: item.WarehousingId,
  4704. StockCount: countOne + item.StockCount,
  4705. }
  4706. err := UpdateWarehousingCount(&infoOne, item.GoodId, item.OrgId, item.WarehousingId)
  4707. if err == nil {
  4708. //剩余的出库到第二个批次
  4709. var countTwo = count - countOne
  4710. infos := append(list, list[i+1])
  4711. var idArray []*models.WarehousingInfo
  4712. for _, it := range infos {
  4713. if it.WarehousingId != item.WarehousingId {
  4714. idArray = append(idArray, it)
  4715. }
  4716. }
  4717. //查询更新后的库存
  4718. UpdateCancelOut(idArray, countTwo)
  4719. return err
  4720. } else {
  4721. return err
  4722. }
  4723. }
  4724. }
  4725. return err
  4726. }
  4727. func GetCancelStockOrderPrint(idstr []string) (info []*models.CancelStockInfo, err error) {
  4728. if len(idstr) > 0 {
  4729. err = XTReadDB().Model(&info).Where("cancel_stock_id in(?)", idstr).Preload("GoodInfo", "status = 1").Find(&info).Error
  4730. } else {
  4731. err = XTReadDB().Model(&info).Preload("GoodInfo", "status = 1").Find(&info).Error
  4732. }
  4733. return info, err
  4734. }
  4735. func GetCancelStockOrderPrintOne(idstr []string, orgid int64) (info []*models.CancelStock, err error) {
  4736. if len(idstr) > 0 {
  4737. err = XTReadDB().Model(&info).Where("id in(?)", idstr).
  4738. Preload("XtCancelStockInfo", func(db *gorm.DB) *gorm.DB {
  4739. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodInfo", "status = 1 and org_id = ?", orgid)
  4740. }).Find(&info).Error
  4741. }
  4742. return info, err
  4743. }
  4744. func GetCancelOutTotalCount(startime int64, endtime int64, orgid int64) (info []*models.VmCancelStockInfo, err error) {
  4745. db := XTReadDB().Table("xt_cancel_stock_info as x").Where("x.status = 1")
  4746. if startime > 0 {
  4747. db = db.Where("x.ctime >=?", startime)
  4748. }
  4749. if endtime > 0 {
  4750. db = db.Where("x.ctime <= ?", endtime)
  4751. }
  4752. if orgid > 0 {
  4753. db = db.Where("x.org_id = ?", orgid)
  4754. }
  4755. err = db.Select("x.good_id,sum(x.count) as count").Group("x.good_id").Scan(&info).Error
  4756. return info, err
  4757. }
  4758. func GetWarehoureOutOrderInfo(goodid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  4759. info := models.WarehouseOutInfo{}
  4760. var err error
  4761. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1", goodid, orgid).Find(&info).Error
  4762. if err == gorm.ErrRecordNotFound {
  4763. return nil, err
  4764. }
  4765. if err != nil {
  4766. return nil, err
  4767. }
  4768. return &info, nil
  4769. }
  4770. func GetWarehoureOrderInfoCount(goodid int64, orgid int64, id int64) (info models.WarehouseOutInfo, err error) {
  4771. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  4772. if goodid > 0 {
  4773. db = db.Where("x.good_id = ?", goodid)
  4774. }
  4775. if orgid > 0 {
  4776. db = db.Where("x.org_id = ?", orgid)
  4777. }
  4778. if id > 0 {
  4779. db = db.Where("x.warehouse_info_id = ?", id)
  4780. }
  4781. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4782. return info, err
  4783. }
  4784. func GetWarehoureOrderStockOutFlow(goodid int64, orgid int64, id int64, storehouse_id int64) (info models.VmStockFlow, err error) {
  4785. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status =1 and x.consumable_type = 2 or x.consumable_type = 3")
  4786. if goodid > 0 {
  4787. db = db.Where("x.good_id = ?", goodid)
  4788. }
  4789. if orgid > 0 {
  4790. db = db.Where("x.user_org_id = ?", orgid)
  4791. }
  4792. if id > 0 {
  4793. db = db.Where("x.warehousing_id = ?", id)
  4794. }
  4795. if storehouse_id > 0 {
  4796. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4797. }
  4798. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4799. return info, err
  4800. }
  4801. func UpdateCancelInfo(id int64) (models.CancelStock, error) {
  4802. stock := models.CancelStock{}
  4803. err := XTWriteDB().Model(&stock).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4804. return stock, err
  4805. }
  4806. func GetStockBatchNumber(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4807. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1 and number <> ''", id, orgid).Find(&info).Error
  4808. return info, err
  4809. }
  4810. func GetWarehouseOutInfoById(id int64, goodid int64) (info []*models.WarehouseOutInfo, err error) {
  4811. err = XTReadDB().Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  4812. return info, err
  4813. }
  4814. func GetStockFlowBatchNumber(id int64, goodid int64) (stock []*models.VmStockFlow, err error) {
  4815. err = XTReadDB().Model(&stock).Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Group("patient_id,number").Find(&stock).Error
  4816. return stock, err
  4817. }
  4818. func GetStockFlowBatchNumberOne(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4819. 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)")
  4820. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4821. fmt.Println(table)
  4822. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4823. fmt.Println(tableone)
  4824. if id > 0 {
  4825. db = db.Where("x.warehouse_out_id = ?", id)
  4826. }
  4827. if goodid > 0 {
  4828. db = db.Where("x.good_id = ?", goodid)
  4829. }
  4830. 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
  4831. return stock, err
  4832. }
  4833. func GetStockFlowCancelInfo(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4834. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status = 1 and x.count > 0 and x.consumable_type = 7")
  4835. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4836. fmt.Println(table)
  4837. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4838. fmt.Println(tableone)
  4839. if id > 0 {
  4840. db = db.Where("x.warehouse_out_id = ?", id)
  4841. }
  4842. if goodid > 0 {
  4843. db = db.Where("x.good_id = ?", goodid)
  4844. }
  4845. 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
  4846. return stock, err
  4847. }
  4848. func GetDrugBatchNumber(drug_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  4849. err = XTReadDB().Model(&info).Where("drug_id = ? and org_id = ? and status = 1 and batch_number <>''", drug_id, orgid).Find(&info).Error
  4850. return info, err
  4851. }
  4852. func ExportDrugList(warehouse_out_id []string, org_id int64) (out []*models.StDrugWarehouseInfo, err error) {
  4853. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status =1 AND x.org_id=?", org_id)
  4854. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1 and b.org_id = ?", org_id)
  4855. tableOne := XTReadDB().Table("xt_drug_warehouse as p").Where("p.status=1 and p.org_id=?", org_id)
  4856. fmt.Println("table", table)
  4857. fmt.Println("tableOne", tableOne)
  4858. if len(warehouse_out_id) > 0 {
  4859. db = db.Where("x.warehousing_id in(?)", warehouse_out_id)
  4860. }
  4861. 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
  4862. return out, err
  4863. }
  4864. func GetExportOutOrderDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4865. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4866. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  4867. fmt.Println("table", table)
  4868. if len(warehouse_out_id) > 0 {
  4869. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4870. }
  4871. 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
  4872. return out, err
  4873. }
  4874. func GetExportOutOrderDrugListOne(warehouse_out_id []string, org_id int64) (out []*models.StDrugWarehouseOutInfo, err error) {
  4875. db := XTReadDB().Table("xt_drug_flow as x").Where("x.status = 1 and x.user_org_id =?", org_id)
  4876. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1 and b.org_id = ?", org_id)
  4877. fmt.Println("table", table)
  4878. if len(warehouse_out_id) > 0 {
  4879. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4880. }
  4881. 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
  4882. return out, err
  4883. }
  4884. func GetDrugOutListFlow(warehouse_out_id []string, orgid int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  4885. db := XTReadDB().Model(&info).Where("status = 1")
  4886. if len(warehouse_out_id) > 0 {
  4887. db = db.Where("warehouse_out_id in(?)", warehouse_out_id)
  4888. }
  4889. if orgid > 0 {
  4890. db = db.Where("org_id = ?", orgid)
  4891. }
  4892. err = db.Find(&info).Error
  4893. return info, err
  4894. }
  4895. func GetDrugWarehouseOut(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4896. err = XTReadDB().Where("warehouse_out_id in(?)", warehouse_out_id).Find(&out).Error
  4897. return out, err
  4898. }
  4899. func GetDrugWarehouseOutTenty(warehouse_out_id []string, orgid int64) (out []*models.DrugWarehouseOutTwenty, err error) {
  4900. err = XTReadDB().Where("id in(?)", warehouse_out_id).Preload("DrugWarehouseOutInfoTenty", func(db *gorm.DB) *gorm.DB {
  4901. 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)
  4902. }).Find(&out).Error
  4903. return out, err
  4904. }
  4905. func GetLastGoodInformationByGoodId(goodid int64, warehouse_info_id int64, orgid_id int64) (models.WarehouseOutInfo, error) {
  4906. info := models.WarehouseOutInfo{}
  4907. err := XTReadDB().Where("good_id = ? and warehouse_info_id= ? and status =1", goodid, warehouse_info_id).Last(&info).Error
  4908. return info, err
  4909. }
  4910. func GetTotalCountByGoodId(goodid int64, storehouse_id int64) (models.WarehousingInfo, error) {
  4911. info := models.WarehousingInfo{}
  4912. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4913. if goodid > 0 {
  4914. db = db.Where("x.good_id = ?", goodid)
  4915. }
  4916. if storehouse_id > 0 {
  4917. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4918. }
  4919. err := db.Select("x.good_id,sum(x.stock_count) as stock_count").Scan(&info).Error
  4920. return info, err
  4921. }
  4922. func GetLastGoodCountById(goodid int64, warehouse_info_id int64) (models.WarehousingInfo, error) {
  4923. info := models.WarehousingInfo{}
  4924. err := XTReadDB().Where("good_id = ? and id= ? and status = 1", goodid, warehouse_info_id).First(&info).Error
  4925. return info, err
  4926. }
  4927. func UpdateWarehousingInfo(info models.WarehousingInfo, id int64) error {
  4928. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  4929. return err
  4930. }
  4931. func FindFirstWarehousingInfoByGoodId(good_id int64, id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  4932. if storehouse_id == 0 {
  4933. err = readDb.Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime asc").First(&info).Error
  4934. }
  4935. if storehouse_id > 0 {
  4936. 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
  4937. }
  4938. return info, err
  4939. }
  4940. func CreateStockFlow(warehousingInfo []*models.VmStockFlow) (err error) {
  4941. if len(warehousingInfo) > 0 {
  4942. utx := writeDb.Begin()
  4943. if len(warehousingInfo) > 0 {
  4944. 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 "
  4945. insertParams := make([]string, 0)
  4946. insertData := make([]interface{}, 0)
  4947. for _, info := range warehousingInfo {
  4948. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  4949. insertData = append(insertData, info.WarehousingId)
  4950. insertData = append(insertData, info.GoodId)
  4951. insertData = append(insertData, info.Number)
  4952. insertData = append(insertData, info.LicenseNumber)
  4953. insertData = append(insertData, info.Count)
  4954. insertData = append(insertData, info.UserOrgId)
  4955. insertData = append(insertData, info.PatientId)
  4956. insertData = append(insertData, info.SystemTime)
  4957. insertData = append(insertData, info.ConsumableType)
  4958. insertData = append(insertData, info.IsSys)
  4959. insertData = append(insertData, info.WarehousingOrder)
  4960. insertData = append(insertData, info.WarehouseOutId)
  4961. insertData = append(insertData, info.WarehouseOutOrderNumber)
  4962. insertData = append(insertData, info.IsEdit)
  4963. insertData = append(insertData, info.CancelStockId)
  4964. insertData = append(insertData, info.CancelOrderNumber)
  4965. insertData = append(insertData, info.Manufacturer)
  4966. insertData = append(insertData, info.Dealer)
  4967. insertData = append(insertData, info.Creator)
  4968. insertData = append(insertData, info.UpdateCreator)
  4969. insertData = append(insertData, info.Status)
  4970. insertData = append(insertData, info.Ctime)
  4971. insertData = append(insertData, info.Mtime)
  4972. insertData = append(insertData, info.Price)
  4973. insertData = append(insertData, info.WarehousingDetailId)
  4974. insertData = append(insertData, info.WarehouseOutDetailId)
  4975. insertData = append(insertData, info.CancelOutDetailId)
  4976. insertData = append(insertData, info.ExpireDate)
  4977. insertData = append(insertData, info.ProductDate)
  4978. }
  4979. thisSQL += strings.Join(insertParams, ", ")
  4980. err = utx.Exec(thisSQL, insertData...).Error
  4981. if err != nil {
  4982. utx.Rollback()
  4983. return
  4984. }
  4985. }
  4986. utx.Commit()
  4987. }
  4988. return
  4989. }
  4990. func CreateStockFlowOne(flow models.VmStockFlow) error {
  4991. utx := XTWriteDB().Begin()
  4992. err := utx.Create(&flow).Error
  4993. if err != nil {
  4994. utx.Rollback()
  4995. return err
  4996. }
  4997. utx.Commit()
  4998. return err
  4999. }
  5000. func CreateNewStockFlowOne(flow models.VmStockFlow, tx *gorm.DB) error {
  5001. err := tx.Create(&flow).Error
  5002. if err != nil {
  5003. tx.Rollback()
  5004. return err
  5005. }
  5006. return err
  5007. }
  5008. func CreateOutInfoLog(log models.XtGoodWarehouseOutInfoLog, tx *gorm.DB) error {
  5009. err := tx.Create(&log).Error
  5010. if err != nil {
  5011. tx.Rollback()
  5012. return err
  5013. }
  5014. return err
  5015. }
  5016. func CreateGoodWarehouseInfoLog(log models.XtGoodWarehouseInfoLog, tx *gorm.DB) error {
  5017. err := tx.Create(&log).Error
  5018. if err != nil {
  5019. tx.Rollback()
  5020. return err
  5021. }
  5022. return err
  5023. }
  5024. func CreateStockFlowSix(flow *models.VmStockFlow) error {
  5025. err := XTWriteDB().Create(&flow).Error
  5026. return err
  5027. }
  5028. func UpdatedStockFlow(flow models.VmStockFlow) error {
  5029. err := XTWriteDB().Save(&flow).Error
  5030. return err
  5031. }
  5032. func UpdatedStockFlowOne(flow models.VmStockFlow, warehousing_id int64, patient_id int64, record_time int64, good_id int64) error {
  5033. 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
  5034. return err
  5035. }
  5036. func UpdatedStockFlowFive(flow models.VmStockFlow, warehouse_out_id int64, patient_id int64, record_time int64, good_id int64) error {
  5037. 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
  5038. return err
  5039. }
  5040. func GetStockFlowIsExsit(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (*models.VmStockFlow, error) {
  5041. stock := models.VmStockFlow{}
  5042. var err error
  5043. 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
  5044. if err == gorm.ErrRecordNotFound {
  5045. return nil, err
  5046. }
  5047. if err != nil {
  5048. return nil, err
  5049. }
  5050. return &stock, nil
  5051. }
  5052. func GetStockFlowIsBatchNumber(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  5053. 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
  5054. return flow, nil
  5055. }
  5056. func GetStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  5057. 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
  5058. return flow, nil
  5059. }
  5060. func GetNewStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  5061. 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
  5062. if err != gorm.ErrRecordNotFound {
  5063. if err != nil {
  5064. tx.Rollback()
  5065. return flow, nil
  5066. }
  5067. }
  5068. return flow, nil
  5069. }
  5070. func GetStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.WarehouseOutInfo, err error) {
  5071. 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
  5072. return flow, nil
  5073. }
  5074. func GetNewStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.WarehouseOutInfo, err error) {
  5075. 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
  5076. if err != gorm.ErrRecordNotFound {
  5077. if err != nil {
  5078. tx.Rollback()
  5079. return flow, nil
  5080. }
  5081. }
  5082. return flow, nil
  5083. }
  5084. func GetStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.AutomaticReduceDetail, err error) {
  5085. 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
  5086. return flow, nil
  5087. }
  5088. func GetNewStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.AutomaticReduceDetail, err error) {
  5089. 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
  5090. if err != gorm.ErrRecordNotFound {
  5091. if err != nil {
  5092. tx.Rollback()
  5093. return flow, err
  5094. }
  5095. }
  5096. return flow, err
  5097. }
  5098. func GetStockFlowIsBatchNumberTwo(patient_id int64, record_time int64, good_id int64) (flow []*models.WarehouseOutInfo, err error) {
  5099. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 ", patient_id, record_time, good_id).Find(&flow).Error
  5100. return flow, nil
  5101. }
  5102. func GetStockFlowIsBatchNumberThree(patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  5103. 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
  5104. return flow, nil
  5105. }
  5106. func GetStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  5107. 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
  5108. return flow, nil
  5109. }
  5110. func GetNewStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  5111. 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
  5112. if err != gorm.ErrRecordNotFound {
  5113. if err != nil {
  5114. tx.Rollback()
  5115. return flow, nil
  5116. }
  5117. }
  5118. return flow, nil
  5119. }
  5120. func GetStockFlowIsBatchNumberFourty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  5121. 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
  5122. return flow, nil
  5123. }
  5124. func GetWarehouseOutInfoIsExist(id int64) (*models.WarehouseOutInfo, error) {
  5125. info := models.WarehouseOutInfo{}
  5126. var err error
  5127. err = XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5128. if err == gorm.ErrRecordNotFound {
  5129. return nil, err
  5130. }
  5131. if err != nil {
  5132. return nil, err
  5133. }
  5134. return &info, nil
  5135. }
  5136. func GetWarehouseOutInfoIsExistOne(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  5137. info := models.WarehouseOutInfo{}
  5138. var err error
  5139. 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
  5140. if err == gorm.ErrRecordNotFound {
  5141. return nil, err
  5142. }
  5143. if err != nil {
  5144. return nil, err
  5145. }
  5146. return &info, nil
  5147. }
  5148. func GetWarehouseOutInfoIsExistTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  5149. info := models.WarehouseOutInfo{}
  5150. 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
  5151. return info, err
  5152. }
  5153. 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) {
  5154. offset := (page - 1) * limit
  5155. db := XTReadDB().Model(&flow).Where("status = 1 and count > 0")
  5156. if consumable_type > 0 {
  5157. //入库
  5158. if consumable_type == 1 {
  5159. db = db.Where("consumable_type = ?", consumable_type)
  5160. }
  5161. //2 手动出库
  5162. if consumable_type == 2 {
  5163. db = db.Where(" consumable_type = ? and is_sys = 0", consumable_type)
  5164. }
  5165. //3.自动出库
  5166. if consumable_type == 3 {
  5167. db = db.Where(" consumable_type = ? and is_sys = 1", consumable_type)
  5168. }
  5169. //4.手动退库
  5170. if consumable_type == 4 {
  5171. db = db.Where(" consumable_type = ?", consumable_type)
  5172. }
  5173. //自动退库
  5174. if consumable_type == 7 {
  5175. db = db.Where(" consumable_type = ?", consumable_type)
  5176. }
  5177. if consumable_type == 10 {
  5178. db = db.Where(" consumable_type = ?", consumable_type)
  5179. }
  5180. if consumable_type == 11 {
  5181. db = db.Where(" consumable_type = ?", consumable_type)
  5182. }
  5183. if consumable_type == 12 {
  5184. db = db.Where(" consumable_type = ?", consumable_type)
  5185. }
  5186. if consumable_type == 13 {
  5187. db = db.Where(" consumable_type = ?", consumable_type)
  5188. }
  5189. if consumable_type == 15 {
  5190. db = db.Where(" consumable_type = ?", consumable_type)
  5191. }
  5192. }
  5193. if orgId > 0 {
  5194. db = db.Where("user_org_id = ?", orgId)
  5195. }
  5196. if startTime > 0 {
  5197. db = db.Where("ctime >= ?", startTime)
  5198. }
  5199. if endTime > 0 {
  5200. db = db.Where("ctime<=?", endTime)
  5201. }
  5202. if good_id > 0 {
  5203. db = db.Where("good_id = ?", good_id)
  5204. }
  5205. err = db.Count(&total).Offset(offset).Limit(limit).Order("id desc").Find(&flow).Error
  5206. return flow, total, err
  5207. }
  5208. func GetHidsDoctorAdvice(good_id int64, user_org_id int64, patient_id int64, record_date int64) (models.HisPrescriptionProject, error) {
  5209. project := models.HisPrescriptionProject{}
  5210. err = XTReadDB().Where("project_id = ? and type =3 and user_org_id = ? and record_date =? and patient_id = ?", good_id, user_org_id, record_date, patient_id).Find(&project).Error
  5211. return project, err
  5212. }
  5213. func GetLastGoodListByPatientId(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.AutomaticReduceDetail, error) {
  5214. detail := models.AutomaticReduceDetail{}
  5215. 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
  5216. return detail, err
  5217. }
  5218. func GetLastGoodListByPatientIdOne(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.WarehouseOutInfo, error) {
  5219. detail := models.WarehouseOutInfo{}
  5220. 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
  5221. return detail, err
  5222. }
  5223. func UpdatedWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64) error {
  5224. outInfo := models.WarehouseOutInfo{}
  5225. 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
  5226. return err
  5227. }
  5228. func UpdatedNewWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) error {
  5229. outInfo := models.WarehouseOutInfo{}
  5230. 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
  5231. if err != nil {
  5232. tx.Rollback()
  5233. return err
  5234. }
  5235. return err
  5236. }
  5237. func UpdatedWarehouseOutInfoSix(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) error {
  5238. outInfo := models.WarehouseOutInfo{}
  5239. 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
  5240. return err
  5241. }
  5242. 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 {
  5243. outInfo := models.WarehouseOutInfo{}
  5244. 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
  5245. if err != nil {
  5246. tx.Rollback()
  5247. return err
  5248. }
  5249. return err
  5250. }
  5251. func GetLastWarehouOutInfoByPatientId(orgid int64, patient_id int64, record_time int64, goodid int64, good_type_id int64) (models.WarehouseOutInfo, error) {
  5252. info := models.WarehouseOutInfo{}
  5253. 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
  5254. return info, err
  5255. }
  5256. func GetLastStockOut(id int64) (models.WarehousingInfo, error) {
  5257. info := models.WarehousingInfo{}
  5258. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5259. return info, err
  5260. }
  5261. func GetStockGoodCount(good_id int64) (models.WarehousingInfo, error) {
  5262. info := models.WarehousingInfo{}
  5263. db := XTReadDB().Model(&info)
  5264. if good_id > 0 {
  5265. db = db.Where("good_id = ? and status = 1", good_id)
  5266. }
  5267. err := db.Select("sum(stock_count) as stock_count").Find(&info).Error
  5268. return info, err
  5269. }
  5270. 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) {
  5271. offset := (page - 1) * limit
  5272. db := XTReadDB().Model(drug_flow).Where("status = 1")
  5273. if drugid > 0 {
  5274. db = db.Where("drug_id = ?", drugid)
  5275. }
  5276. if orgid > 0 {
  5277. db = db.Where("user_org_id = ?", orgid)
  5278. }
  5279. if startTime > 0 {
  5280. db = db.Where("ctime >=? ", startTime)
  5281. }
  5282. if endTime > 0 {
  5283. db = db.Where("ctime<=?", endTime)
  5284. }
  5285. if stock_type > 0 {
  5286. //手动入库
  5287. if stock_type == 1 {
  5288. db = db.Where("consumable_type = 1")
  5289. }
  5290. //手动出库
  5291. if stock_type == 2 {
  5292. db = db.Where("consumable_type = 2 and is_sys = 0")
  5293. }
  5294. //自动出库
  5295. if stock_type == 3 {
  5296. db = db.Where("consumable_type = 3 and is_sys = 1")
  5297. }
  5298. if stock_type == 4 {
  5299. db = db.Where("consumable_type = 4")
  5300. }
  5301. if stock_type == 10 {
  5302. db = db.Where("consumable_type = 10")
  5303. }
  5304. if stock_type == 11 {
  5305. db = db.Where("consumable_type = 11")
  5306. }
  5307. if stock_type == 12 {
  5308. db = db.Where("consumable_type = 12")
  5309. }
  5310. if stock_type == 13 {
  5311. db = db.Where("consumable_type = 13")
  5312. }
  5313. if stock_type == 15 {
  5314. db = db.Where("consumable_type = 15")
  5315. }
  5316. }
  5317. 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
  5318. return drug_flow, total, err
  5319. }
  5320. func HisDoctorAdviceMap(patient_id int64, advice_date int64, drug_id int64, user_org_id int64) (models.HisDoctorAdviceInfo, error) {
  5321. advicelist := models.HisDoctorAdviceInfo{}
  5322. err = XTReadDB().Where("patient_id = ? and drug_id = ? and user_org_id = ? and advice_date = ? and status=1", patient_id, drug_id, user_org_id, advice_date).Find(&advicelist).Error
  5323. return advicelist, err
  5324. }
  5325. func GetDrugWarehouseOutInfoIsExit(drug_id int64, patient_id int64, recordtime int64) (*models.DrugWarehouseOutInfo, error) {
  5326. outInfo := models.DrugWarehouseOutInfo{}
  5327. var err error
  5328. 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
  5329. if err == gorm.ErrRecordNotFound {
  5330. return nil, err
  5331. }
  5332. if err != nil {
  5333. return nil, err
  5334. }
  5335. return &outInfo, nil
  5336. }
  5337. func GetDrugWarehouseOutInfoIsExitOne(drug_id int64, patient_id int64, recordtime int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  5338. 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
  5339. return outInfo, err
  5340. }
  5341. func UpdatedDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, drug_id int64, patient_id int64, record_date int64, orgid int64) error {
  5342. outInfo := models.DrugWarehouseOutInfo{}
  5343. 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
  5344. return err
  5345. }
  5346. func GetCancelExportList(startime int64, endtime int64, ids []string, orgid int64) (cancle []*models.CancelStockInfo, err error) {
  5347. db := XTReadDB().Model(&cancle).Where("status = 1")
  5348. if startime > 0 {
  5349. db = db.Where("ctime >=?", startime)
  5350. }
  5351. if endtime > 0 {
  5352. db = db.Where("ctime <=?", endtime)
  5353. }
  5354. if len(ids) > 0 {
  5355. db = db.Where("cancel_stock_id in(?)", ids)
  5356. }
  5357. if orgid > 0 {
  5358. db = db.Where("org_id = ?", orgid)
  5359. }
  5360. db = db.Preload("GoodInfo", "status =1 and org_id = ?", orgid)
  5361. err = db.Find(&cancle).Error
  5362. return cancle, err
  5363. }
  5364. func GetCancelStockDetail(id int64) (info []*models.DrugCancelStockInfo, err error) {
  5365. db := XTReadDB().Model(&info)
  5366. if id > 0 {
  5367. db = db.Where("cancel_stock_id = ? and status = 1", id)
  5368. }
  5369. db = db.Preload("BaseDrugLib")
  5370. err = db.Find(&info).Error
  5371. return info, err
  5372. }
  5373. func GetDrugWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  5374. info := models.XtDrugWarehouseInfo{}
  5375. db := XTReadDB().Model(&info).Where("status = 1")
  5376. if id > 0 {
  5377. db = db.Where("id = ?", id)
  5378. }
  5379. err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  5380. return info, err
  5381. }
  5382. func GetDrugWarehouseInfoOne(id int64) (models.XtDrugWarehouseInfo, error) {
  5383. info := models.XtDrugWarehouseInfo{}
  5384. err := XTReadDB().Where("status = 1 and id=?", id).Find(&info).Error
  5385. return info, err
  5386. }
  5387. //func GetDrugWarehouseInfoTX(id int64,tx *gorm.DB) (models.XtDrugWarehouseInfo, error) {
  5388. // info := models.XtDrugWarehouseInfo{}
  5389. // db := tx.Model(&info).Where("status = 1")
  5390. // if id > 0 {
  5391. // db = db.Where("id = ?", id)
  5392. // }
  5393. // err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  5394. // return info, err
  5395. //}
  5396. func GetDrugWarehouseInfoPrescription(drugid int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5397. err = XTReadDB().Where("drug_id = ? and org_id =? and status = 1", drugid, orgid).Find(&info).Error
  5398. return info, err
  5399. }
  5400. func GetDrugWarehouseInfoPrescriptionSeven(drugid int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfoSeven, err error) {
  5401. 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
  5402. return info, err
  5403. }
  5404. func GetHisAdviceListByDrugId(drugid int64, patient_id int64, advice_date int64) (models.HisDoctorAdviceInfo, error) {
  5405. info := models.HisDoctorAdviceInfo{}
  5406. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1", drugid, patient_id, advice_date).Find(&info).Error
  5407. return info, err
  5408. }
  5409. func GetHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64) (info []*models.HisDoctorAdviceInfo, err error) {
  5410. err = XTReadDB().Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  5411. return info, err
  5412. }
  5413. func GetNewHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64, tx *gorm.DB) (info []*models.HisDoctorAdviceInfo, err error) {
  5414. err = tx.Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  5415. if err != gorm.ErrRecordNotFound {
  5416. if err != nil {
  5417. tx.Rollback()
  5418. return info, err
  5419. }
  5420. }
  5421. return info, err
  5422. }
  5423. func GetLastHisAdvicesList(patient_id int64, advice_date int64, user_org_id int64, tx *gorm.DB) (models.HisDoctorAdviceInfo, error) {
  5424. adviceInfo := models.HisDoctorAdviceInfo{}
  5425. 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
  5426. if err != gorm.ErrRecordNotFound {
  5427. if err != nil {
  5428. tx.Rollback()
  5429. return adviceInfo, err
  5430. }
  5431. }
  5432. return adviceInfo, err
  5433. }
  5434. func GetHisAdviceListByDrugIdEight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  5435. info := models.HisDoctorAdviceInfo{}
  5436. 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
  5437. return info, err
  5438. }
  5439. func GetHisAdviceListByDrugIdNight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  5440. info := models.HisDoctorAdviceInfo{}
  5441. 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
  5442. return info, err
  5443. }
  5444. func GetLastDrugWarehouseOutByDrugId(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.DrugAutomaticReduceDetail, error) {
  5445. detail := models.DrugAutomaticReduceDetail{}
  5446. 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
  5447. return detail, err
  5448. }
  5449. func GetWarehouseOutListExport(drug_id int64, patient_id int64, record_date int64, advice_id int64) (out []*models.XtDrugWarehouseOutInfo, err error) {
  5450. 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
  5451. return out, err
  5452. }
  5453. func GetLastDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64) (out []*models.DrugWarehouseOutInfo, err error) {
  5454. 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
  5455. return out, err
  5456. }
  5457. func GetLastNewDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64, tx *gorm.DB) (out []*models.DrugWarehouseOutInfo, err error) {
  5458. 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
  5459. if err != gorm.ErrRecordNotFound {
  5460. if err != nil {
  5461. tx.Rollback()
  5462. return out, err
  5463. }
  5464. }
  5465. return out, err
  5466. }
  5467. func ModefyDrugByWarehouseInfo(id int64, stock_max_number int64) error {
  5468. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  5469. return err
  5470. }
  5471. func ModefyDrugByWarehouseInfoOne(id int64, stock_min_number int64) error {
  5472. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  5473. return err
  5474. }
  5475. func DeleteDrugAutoWarehouse(drugid int64, patient_id int64, record_time int64) error {
  5476. 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
  5477. 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
  5478. 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
  5479. return err
  5480. }
  5481. func DeleteDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  5482. 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
  5483. 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
  5484. return err
  5485. }
  5486. func DeleteNewDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  5487. 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
  5488. 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
  5489. return err
  5490. }
  5491. func GetDrugWarehouseInfoById(id int64) (models.XtDrugWarehouseInfo, error) {
  5492. info := models.XtDrugWarehouseInfo{}
  5493. err := XTReadDB().Model(&info).Where("id=? and status =1", id).Find(&info).Error
  5494. return info, err
  5495. }
  5496. func GetDrugWarehouseInfoByIdTwenty(id int64) (models.XtDrugWarehouseInfo, error) {
  5497. info := models.XtDrugWarehouseInfo{}
  5498. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  5499. return info, err
  5500. }
  5501. func UpdateDrugWarehouseInfo(info *models.XtDrugWarehouseInfo, id int64) error {
  5502. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  5503. return err
  5504. }
  5505. //func UpdateDrugWarehouseInfoTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  5506. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  5507. // return err
  5508. //}
  5509. func UpdateDrugWarehouseInfoOne(info *models.XtDrugWarehouseInfo, id int64) error {
  5510. //err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_min_number": info.StockMaxNumber}).Error
  5511. //return err
  5512. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  5513. return err
  5514. }
  5515. //func UpdateDrugWarehouseInfoOneTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  5516. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  5517. // return err
  5518. //}
  5519. func GetLastCancelStockInfo(id int64) (models.DrugCancelStockInfo, error) {
  5520. info := models.DrugCancelStockInfo{}
  5521. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5522. return info, err
  5523. }
  5524. func GetLastCancelStockInfoOne(id int64, drug_id int64) (models.DrugCancelStockInfo, error) {
  5525. info := models.DrugCancelStockInfo{}
  5526. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ? and status = 1", id, drug_id).Find(&info).Error
  5527. return info, err
  5528. }
  5529. func UpdatedDrugInfo(id int64, count int64) (err error) {
  5530. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  5531. return err
  5532. }
  5533. func UpdatedDrugFlow(cancelstockid int64, drug_id int64, flow models.DrugFlow) error {
  5534. 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
  5535. return err
  5536. }
  5537. func UpdatedDrugInfoOne(id int64, count int64) (err error) {
  5538. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  5539. return err
  5540. }
  5541. func UpdateWarehouseInfo(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  5542. 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
  5543. return err
  5544. }
  5545. func UpdateWarehouseInfoTwo(count int64, drugid int64, orgid int64) error {
  5546. 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
  5547. return err
  5548. }
  5549. func UpdateWarehouseInfoOne(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  5550. 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
  5551. return err
  5552. }
  5553. func UpdateWarehouseInfoFour(count int64, drugid int64, orgid int64) error {
  5554. 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
  5555. return err
  5556. }
  5557. func GetWarehouseInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  5558. info := models.WarehousingInfo{}
  5559. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5560. return info, err
  5561. }
  5562. func GetLastWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5563. info := models.WarehousingInfo{}
  5564. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  5565. return info, err
  5566. }
  5567. func GetLastWarehouseInfoTwo(goodid int64, storehouse_id int64, user_org_id int64) (info []*models.WarehousingInfo, err error) {
  5568. 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
  5569. return info, err
  5570. }
  5571. func GetFirstWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5572. info := models.WarehousingInfo{}
  5573. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).First(&info).Error
  5574. return info, err
  5575. }
  5576. func CreateWarehouseOut(out models.WarehouseOut) error {
  5577. err := XTWriteDB().Create(&out).Error
  5578. return err
  5579. }
  5580. func GetLastGoodWarehouseOut(orgid int64) (models.WarehouseOut, error) {
  5581. out := models.WarehouseOut{}
  5582. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  5583. return out, err
  5584. }
  5585. func CreateWarehouseInfo(info models.WarehousingInfo) error {
  5586. err := XTWriteDB().Create(&info).Error
  5587. return err
  5588. }
  5589. func UpdateGoodWarehouseInfo(id int64, info models.WarehousingInfo) error {
  5590. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5591. return err
  5592. }
  5593. func UpdateWareInfoById(id int64, count int64) error {
  5594. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  5595. return err
  5596. }
  5597. func UpdateWareInfoByIdSix(id int64, count int64) error {
  5598. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  5599. return err
  5600. }
  5601. func GetLastCancelInfo(id int64, goodid int64) (models.CancelStockInfo, error) {
  5602. info := models.CancelStockInfo{}
  5603. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5604. return info, err
  5605. }
  5606. func GetLastCancelInfoSix(id int64, goodid int64, storehouse_id int64) (models.CancelStockInfo, error) {
  5607. info := models.CancelStockInfo{}
  5608. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1 and storehouse_id = ?", id, goodid, storehouse_id).Find(&info).Error
  5609. return info, err
  5610. }
  5611. func GetAllCancelInfoById(id int64, goodid int64) (info []*models.CancelStockInfo, err error) {
  5612. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5613. return info, err
  5614. }
  5615. func GetAllCancelInfoByIdSix(id int64, goodid int64, storehouse_id int64) (info []*models.CancelStockInfo, err error) {
  5616. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5617. return info, err
  5618. }
  5619. func UpdateStockFlow(flow models.VmStockFlow, goodid int64, warehousing_id int64) error {
  5620. err := XTWriteDB().Model(&flow).Where("good_id = ? and warehousing_id= ? and status = 1", goodid, warehousing_id).Updates(map[string]interface{}{"count": flow.Count}).Error
  5621. return err
  5622. }
  5623. func UpdateDrugFlow(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5624. drugFlow := models.DrugFlow{}
  5625. 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
  5626. return err
  5627. }
  5628. func UpdateDrugFlowNight(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5629. drugFlow := models.DrugFlow{}
  5630. 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
  5631. return err
  5632. }
  5633. func UpdateDrugCancel(id int64) (models.DrugCancelStock, error) {
  5634. stock := models.DrugCancelStock{}
  5635. err := XTWriteDB().Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5636. return stock, err
  5637. }
  5638. //func UpdateDrugCancelTX(id int64,tx *gorm.DB) (models.DrugCancelStock, error) {
  5639. // stock := models.DrugCancelStock{}
  5640. // err := tx.Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5641. // return stock, err
  5642. //}
  5643. func GetDrugWarehouseOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugWarehouseOutInfo, err error) {
  5644. db := XTReadDB().Model(&info)
  5645. if warehouse_info_id > 0 {
  5646. db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5647. }
  5648. if drug_id > 0 {
  5649. db = db.Where("drug_id = ? and status =1", drug_id)
  5650. }
  5651. err = db.Find(&info).Error
  5652. return info, err
  5653. }
  5654. //func GetDrugWarehouseOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  5655. // db := tx.Model(&info)
  5656. // if warehouse_info_id > 0 {
  5657. // db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5658. // }
  5659. // if drug_id > 0 {
  5660. // db = db.Where("drug_id = ? and status =1", drug_id)
  5661. // }
  5662. // err = db.Find(&info).Error
  5663. // return info, err
  5664. //}
  5665. func GetCancelDrugStockOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugCancelStockInfo, err error) {
  5666. err = XTReadDB().Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5667. return info, err
  5668. }
  5669. //func GetCancelDrugStockOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugCancelStockInfo, err error) {
  5670. // err = tx.Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5671. // return info, err
  5672. //}
  5673. func GetTemplateSummary(orgid int64) (config []*models.Dataconfig, err error) {
  5674. err = XTReadDB().Model(&config).Where("module='template_summary' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5675. return config, err
  5676. }
  5677. func GetTemplatePlan(orgid int64) (config []*models.Dataconfig, err error) {
  5678. err = XTReadDB().Model(&config).Where("module='template_plan' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5679. return config, err
  5680. }
  5681. func GetSearchGoodWarehouseList(keyword string, orgid int64, storehouse_id int64) (info []*models.BloodWarehouseInfo, err error) {
  5682. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  5683. table := XTReadDB().Table("xt_goood_information as t").Where("t.status")
  5684. fmt.Println(table)
  5685. likeKey := "%" + keyword + "%"
  5686. if orgid > 0 {
  5687. db = db.Where("x.org_id = ?", orgid)
  5688. }
  5689. if storehouse_id > 0 {
  5690. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5691. }
  5692. 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
  5693. return info, err
  5694. }
  5695. func CreateAdjustPrice(adjustPrice *models.XtStockAdjustPrice) error {
  5696. err := XTWriteDB().Create(&adjustPrice).Error
  5697. return err
  5698. }
  5699. func GetAllStockPrice(orgid int64, startime int64, endtime int64, keyword string, limit int64, page int64) (list []*models.VmStockAdjustPrice, total int64, err error) {
  5700. likeKey := "%" + keyword + "%"
  5701. offset := (page - 1) * limit
  5702. db := XTReadDB().Table("xt_stock_adjust_price as x").Where("x.status = 1")
  5703. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5704. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5705. fmt.Println(table, tab)
  5706. if startime > 0 {
  5707. db = db.Where("x.start_time>=?", startime)
  5708. }
  5709. if endtime > 0 {
  5710. db = db.Where("x.start_time<=?", endtime)
  5711. }
  5712. if len(keyword) > 0 {
  5713. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  5714. }
  5715. if orgid > 0 {
  5716. db = db.Where("x.user_org_id = ?", orgid)
  5717. }
  5718. 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
  5719. return list, total, err
  5720. }
  5721. func UpdateAdjustPrice(ids []string, info models.XtStockAdjustPrice) error {
  5722. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5723. return err
  5724. }
  5725. func UpdateAdjustCheckPrice(info *models.GoodInfo, id int64) error {
  5726. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"packing_price": info.PackingPrice, "buy_price": info.BuyPrice}).Error
  5727. return err
  5728. }
  5729. func GetAdjustCheckPriceList(ids []string) (info []*models.XtStockAdjustPrice, err error) {
  5730. err = XTReadDB().Where("id in(?)", ids).Find(&info).Error
  5731. return info, err
  5732. }
  5733. func GetAdjustPricebyIdOne(ids []string) (adjust []*models.XtStockAdjustPrice, err error) {
  5734. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5735. return adjust, err
  5736. }
  5737. func CreateReportPrice(price *models.XtStockReportPrice) error {
  5738. err := XTWriteDB().Create(&price).Error
  5739. return err
  5740. }
  5741. func GetReportStockList(startime int64, endtime int64, orgid int64, keyword string, page int64, limit int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5742. likeKey := "%" + keyword + "%"
  5743. offset := (page - 1) * limit
  5744. db := XTReadDB().Table("xt_stock_report_price as x").Where("x.status = 1")
  5745. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5746. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5747. fmt.Println(table, tab)
  5748. if startime > 0 {
  5749. db = db.Where("x.ctime>=?", startime)
  5750. }
  5751. if endtime > 0 {
  5752. db = db.Where("x.ctime<=?", endtime)
  5753. }
  5754. if len(keyword) > 0 {
  5755. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5756. }
  5757. if orgid > 0 {
  5758. db = db.Where("x.user_org_id = ?", orgid)
  5759. }
  5760. 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
  5761. return adjust, total, err
  5762. }
  5763. func GetStockDamagedList(orgid int64, keyword string, page int64, limit int64, startime int64, endtime int64, storehouse_id int64) (adjust []*models.VmStockInventory, total int64, err error) {
  5764. likeKey := "%" + keyword + "%"
  5765. offset := (page - 1) * limit
  5766. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.inventory_type = 4")
  5767. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5768. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5769. fmt.Println(table, tab)
  5770. if len(keyword) > 0 {
  5771. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5772. }
  5773. if orgid > 0 {
  5774. db = db.Where("x.user_org_id = ?", orgid)
  5775. }
  5776. if startime > 0 {
  5777. db = db.Where("x.ctime >=?", startime)
  5778. }
  5779. if endtime > 0 {
  5780. db = db.Where("x.ctime <=?", endtime)
  5781. }
  5782. if storehouse_id > 0 {
  5783. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5784. }
  5785. 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
  5786. return adjust, total, err
  5787. }
  5788. func GetStockDamagedCountOne(orgid int64, keyword string, inventory_type int64) (adjust []*models.VmStockInventory, err error) {
  5789. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5790. if inventory_type == 0 {
  5791. db = db.Where("x.inventory_type = 4")
  5792. }
  5793. if inventory_type > 0 {
  5794. db = db.Where("x.inventory_type = ?", inventory_type)
  5795. }
  5796. if len(keyword) > 0 {
  5797. likeKey := "%" + keyword + "%"
  5798. db = db.Where("x.warehousing_order like ?", likeKey)
  5799. }
  5800. if orgid > 0 {
  5801. db = db.Where("x.user_org_id = ?", orgid)
  5802. }
  5803. 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
  5804. return adjust, err
  5805. }
  5806. func GetStockDamagedCount(orgid int64) (adjust []*models.VmStockInventory, err error) {
  5807. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and inventory_type = 4 ")
  5808. if orgid > 0 {
  5809. db = db.Where("x.user_org_id = ?", orgid)
  5810. }
  5811. 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
  5812. return adjust, err
  5813. }
  5814. func UpdateCheckDamage(ids []string, reportPrice models.XtStockReportPrice) error {
  5815. 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
  5816. return err
  5817. }
  5818. func GetCheckDamageList(ids []string) (report []*models.XtStockReportPrice, err error) {
  5819. err = XTReadDB().Model(&report).Where("id in(?) and status= 1", ids).Find(&report).Error
  5820. return report, err
  5821. }
  5822. func CreateInentory(inventory models.XtStockInventory) error {
  5823. err = XTWriteDB().Create(&inventory).Error
  5824. return err
  5825. }
  5826. func GetInventorylist(startime int64, endtime int64, page int64, limit int64, inventory_status int64, keyword string, orgid int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5827. likeKey := "%" + keyword + "%"
  5828. offset := (page - 1) * limit
  5829. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5830. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5831. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5832. fmt.Println(table, tab)
  5833. if startime > 0 {
  5834. db = db.Where("x.ctime>=?", startime)
  5835. }
  5836. if endtime > 0 {
  5837. db = db.Where("x.ctime<=?", endtime)
  5838. }
  5839. if len(keyword) > 0 {
  5840. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5841. }
  5842. if orgid > 0 {
  5843. db = db.Where("x.user_org_id = ?", orgid)
  5844. }
  5845. if inventory_status > 0 {
  5846. db = db.Where("checker_status = ?", inventory_status)
  5847. }
  5848. 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
  5849. return adjust, total, err
  5850. }
  5851. func UpdateCheckInventory(ids []string, info models.XtStockInventory) error {
  5852. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5853. return err
  5854. }
  5855. func GetModifyPriceDetail(id int64) (models.XtStockAdjustPrice, error) {
  5856. info := models.XtStockAdjustPrice{}
  5857. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5858. return info, err
  5859. }
  5860. func GetInventoryDetailById(id []string) (list []*models.XtStockInventory, err error) {
  5861. err = XTReadDB().Model(&list).Where("id in(?) and status = 1", id).Find(&list).Error
  5862. return list, err
  5863. }
  5864. func GetWarehouseInfoByIdNight(id int64) (models.WarehousingInfo, error) {
  5865. info := models.WarehousingInfo{}
  5866. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5867. return info, err
  5868. }
  5869. func GetWarehouseInfoById(id int64) (models.WarehousingInfo, error) {
  5870. info := models.WarehousingInfo{}
  5871. //err :=XTReadDB().Model(&info).Where("id = ? or warehouse_info_id and status = 1", id).Find(&info).Error
  5872. //return info, err
  5873. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5874. if id > 0 {
  5875. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5876. }
  5877. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5878. return info, err
  5879. }
  5880. func GetWarehouseInfoByIdTen(id int64, storehouse_id int64) (models.WarehousingInfo, error) {
  5881. info := models.WarehousingInfo{}
  5882. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5883. if id > 0 {
  5884. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5885. }
  5886. if storehouse_id > 0 {
  5887. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5888. }
  5889. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5890. return info, err
  5891. }
  5892. func UpdateStockPrice(info models.XtStockAdjustPrice, id int64) error {
  5893. 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
  5894. return err
  5895. }
  5896. func DeleteStockPrice(id int64) error {
  5897. info := models.XtStockAdjustPrice{}
  5898. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5899. return err
  5900. }
  5901. func GetStockDamageDetail(id int64) (models.XtStockReportPrice, error) {
  5902. report := models.XtStockReportPrice{}
  5903. err := XTReadDB().Model(&report).Where("id=? and status = 1", id).Find(&report).Error
  5904. return report, err
  5905. }
  5906. func ModifyPrice(id int64, info models.XtStockReportPrice) error {
  5907. 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
  5908. return err
  5909. }
  5910. func DeleteDamage(id int64) error {
  5911. adjust := models.XtStockReportPrice{}
  5912. err := XTWriteDB().Model(&adjust).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5913. return err
  5914. }
  5915. func GetStockDamageById(ids []string) (adjust []*models.XtStockReportPrice, err error) {
  5916. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5917. return adjust, err
  5918. }
  5919. func GetInventoryDetail(id int64) (models.XtStockInventory, error) {
  5920. inventory := models.XtStockInventory{}
  5921. err := XTReadDB().Model(&inventory).Where("id=? and status = 1", id).Find(&inventory).Error
  5922. return inventory, err
  5923. }
  5924. func ModifyInventory(id int64, inventory models.XtStockInventory) error {
  5925. 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
  5926. return err
  5927. }
  5928. func DeleteInventory(id int64) error {
  5929. inventory := models.XtStockInventory{}
  5930. err := XTWriteDB().Model(&inventory).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5931. return err
  5932. }
  5933. func GetInventoryDetailList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64) (list []*models.VmStockInventory, total int64, err error) {
  5934. likeKey := "%" + keyword + "%"
  5935. offset := (page - 1) * limit
  5936. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5937. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5938. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5939. fmt.Println(table, tab)
  5940. if len(keyword) > 0 {
  5941. db = db.Where("x.good_name like ?", likeKey)
  5942. }
  5943. if orgid > 0 {
  5944. db = db.Where("x.user_org_id = ?", orgid)
  5945. }
  5946. if storehouse_id > 0 {
  5947. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5948. }
  5949. 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
  5950. return list, total, err
  5951. }
  5952. func GetInventoryDetailPrintMapList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64, ids []string) (list []*models.VmStockInventory, total int64, err error) {
  5953. likeKey := "%" + keyword + "%"
  5954. offset := (page - 1) * limit
  5955. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5956. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5957. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5958. fmt.Println(table, tab)
  5959. if len(keyword) > 0 {
  5960. db = db.Where("x.good_name like ?", likeKey)
  5961. }
  5962. if orgid > 0 {
  5963. db = db.Where("x.user_org_id = ?", orgid)
  5964. }
  5965. if storehouse_id > 0 {
  5966. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5967. }
  5968. if len(ids) > 0 {
  5969. db = db.Where("x.id in(?)", ids)
  5970. }
  5971. 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
  5972. return list, total, err
  5973. }
  5974. func GetInventoryPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5975. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5976. return list, err
  5977. }
  5978. func GetInventoryDetailPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5979. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5980. return list, err
  5981. }
  5982. func DeleteDrugPrice(id int64) error {
  5983. info := models.XtDrugAdjustPrice{}
  5984. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5985. return err
  5986. }
  5987. func GetGoodWarehouseList(goodid int64) (models.WarehousingInfo, error) {
  5988. info := models.WarehousingInfo{}
  5989. db := XTReadDB().Model(&info).Where("status = 1")
  5990. if goodid > 0 {
  5991. db = db.Where("good_id = ?", goodid)
  5992. }
  5993. err := db.Select("good_id,sum(stock_count) as stock_count").Find(&info).Error
  5994. return info, err
  5995. }
  5996. func UpdateProofInventory(id int64, info models.WarehousingInfo) error {
  5997. err := XTWriteDB().Model(&info).Where("id=? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5998. return err
  5999. }
  6000. func UpdateStockInventory(inventory models.XtStockInventory, id int64) error {
  6001. err := XTWriteDB().Model(&inventory).Where("id= ? and status = 1", id).Updates(map[string]interface{}{"total": inventory.Total}).Error
  6002. return err
  6003. }
  6004. func CeateStockCorrectRecord(record models.XtStockCorrectRecord) error {
  6005. err := XTWriteDB().Create(&record).Error
  6006. return err
  6007. }
  6008. func GetWarehouseTotal(id int64) (models.WarehousingInfo, error) {
  6009. info := models.WarehousingInfo{}
  6010. //err :=XTReadDB()()()()().Where("id=? and status = 1", id).Find(&info).Error
  6011. //return info, err
  6012. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  6013. if id > 0 {
  6014. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  6015. }
  6016. err := db.Select("sum(x.stock_count) as stock_count").Scan(&info).Error
  6017. return info, err
  6018. }
  6019. func GetCurrentWarehosueInfo(id int64) (info []*models.DrugWarehouseInfo, err error) {
  6020. err = XTReadDB().Model(&info).Where("id=? or warehousing_info_id = ? ", id, id).Find(&info).Error
  6021. return info, err
  6022. }
  6023. func GetCurrentWarehosueInfoOne(id int64) (models.DrugWarehouseInfo, error) {
  6024. info := models.DrugWarehouseInfo{}
  6025. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  6026. return info, err
  6027. }
  6028. func ModifyDrugWarehouseInfo(info *models.DrugWarehouseInfo, id int64) error {
  6029. 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
  6030. return err
  6031. }
  6032. func ModifyDrugWarehouseInfoSix(info *models.DrugWarehouseInfo, id int64) error {
  6033. 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
  6034. return err
  6035. }
  6036. func GetGoodInventoryWarehouseList(id int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  6037. 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
  6038. return info, err
  6039. }
  6040. func UpdatedWarehousingInfo(info models.WarehousingInfo, id int64) error {
  6041. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  6042. return err
  6043. }
  6044. func UpdatedWarehousingInfoSix(info models.WarehousingInfo, id int64) error {
  6045. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount, "warehousing_count": info.WarehousingCount}).Error
  6046. return err
  6047. }
  6048. func GetDamageDetailByGoodId(good_id int64, keyword string, inventory_type int64) (list []*models.XtStockInventory, err error) {
  6049. db := XTReadDB().Model(&list).Where(" status = 1")
  6050. if good_id > 0 {
  6051. db = db.Where("good_id = ?", good_id)
  6052. }
  6053. if len(keyword) > 0 {
  6054. likeKey := "%" + keyword + "%"
  6055. db = db.Where("warehousing_order like ?", likeKey)
  6056. }
  6057. if inventory_type > 0 {
  6058. db = db.Where("inventory_type = ?", inventory_type)
  6059. }
  6060. if inventory_type == 0 {
  6061. db = db.Where("inventory_type = 4")
  6062. }
  6063. err = db.Find(&list).Error
  6064. return list, err
  6065. }
  6066. func GetCancelStockId(cancelstock_id int64, drug_id int64) (*models.DrugFlow, error) {
  6067. flow := models.DrugFlow{}
  6068. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ?", cancelstock_id, drug_id).Find(&flow).Error
  6069. if err == gorm.ErrRecordNotFound {
  6070. return nil, err
  6071. }
  6072. if err != nil {
  6073. return nil, err
  6074. }
  6075. return &flow, nil
  6076. }
  6077. func GetTotalDrugCancelInfo(batchNumber_id int64, drug_id int64, id int64) (info []*models.DrugCancelStockInfo, err error) {
  6078. err = XTReadDB().Where("batch_number_id = ? and drug_id = ? and status = 1 and id<>?", batchNumber_id, drug_id, id).Find(&info).Error
  6079. return info, err
  6080. }
  6081. func GetLastDrugWarehouseInfo(drugid int64) (models.XtDrugWarehouseInfo, error) {
  6082. info := models.XtDrugWarehouseInfo{}
  6083. err := XTReadDB().Where("drug_id = ? and status =1", drugid).Last(&info).Error
  6084. return info, err
  6085. }
  6086. func GetLastGoodWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  6087. info := models.WarehousingInfo{}
  6088. err := XTReadDB().Where("good_id =? and status = 1", goodid).Find(&info).Error
  6089. return info, err
  6090. }
  6091. func GetGoodStockFlowInfo(warehouseid int64, goodid int64) (*models.VmStockFlow, error) {
  6092. flow := models.VmStockFlow{}
  6093. err := XTReadDB().Where("warehousing_detail_id = ? and good_id = ? and status = 1", warehouseid, goodid).Find(&flow).Error
  6094. if err == gorm.ErrRecordNotFound {
  6095. return nil, err
  6096. }
  6097. if err != nil {
  6098. return nil, err
  6099. }
  6100. return &flow, nil
  6101. }
  6102. func GetLastInfoMationById(id int64) (models.GoodInfo, error) {
  6103. info := models.GoodInfo{}
  6104. err := XTReadDB().Where("id =? and status = 1", id).Find(&info).Error
  6105. return info, err
  6106. }
  6107. func UpdateGoodInfoMation(goodid int64, info models.GoodInfo) error {
  6108. err := XTWriteDB().Model(&info).Where("id =? and status = 1", goodid).Updates(map[string]interface{}{"total": info.Total}).Error
  6109. return err
  6110. }
  6111. func UpdateWarehouseInfoByGoodId(info models.WarehousingInfo, id int64) error {
  6112. err := XTWriteDB().Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount}).Error
  6113. return err
  6114. }
  6115. func UpdateWarehouseInfoByGoodIdOne(info *models.WarehousingInfo, id int64) error {
  6116. 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
  6117. return err
  6118. }
  6119. func UpdateNewWarehouseInfoByGoodIdOne(info *models.WarehousingInfo, id int64, tx *gorm.DB) error {
  6120. 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, "split_of_number": info.SplitOfNumber}).Error
  6121. if err != nil {
  6122. tx.Rollback()
  6123. return err
  6124. }
  6125. return err
  6126. }
  6127. func UpdatedStockFlowByGoodId(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  6128. 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
  6129. return err
  6130. }
  6131. func UpdatedStockFlowByGoodIdOne(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  6132. 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
  6133. return err
  6134. }
  6135. func UpdateStockFlowByDetailId(flow models.VmStockFlow, wareoutdetailid int64) error {
  6136. 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
  6137. return err
  6138. }
  6139. func UpdatedGoodInfo(info models.GoodInfo, goodid int64) error {
  6140. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", goodid).Updates(map[string]interface{}{"manufacturer": info.Manufacturer, "dealer": info.Dealer}).Error
  6141. return err
  6142. }
  6143. func GetInventoryExportList(orgid int64) (info []*models.PatientWarehouseInfo, err error) {
  6144. if orgid == 10653 {
  6145. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.warehousing_count>0 and x.stock_count>0")
  6146. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  6147. fmt.Println(table)
  6148. if orgid > 0 {
  6149. db = db.Where("x.org_id = ?", orgid)
  6150. }
  6151. 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,x.stock_count,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
  6152. return info, err
  6153. } else {
  6154. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.warehousing_count>0")
  6155. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  6156. fmt.Println(table)
  6157. if orgid > 0 {
  6158. db = db.Where("x.org_id = ?", orgid)
  6159. }
  6160. 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,x.stock_count,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
  6161. return info, err
  6162. }
  6163. }
  6164. func UpdateDrugWarehouseingInfoSix(id int64, info models.XtDrugWarehouseInfo) error {
  6165. warehouseInfo := models.XtDrugWarehouseInfo{}
  6166. 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
  6167. return err
  6168. }
  6169. func CreatedCancelStock(info models.DrugCancelStockInfo) error {
  6170. err := XTWriteDB().Create(&info).Error
  6171. return err
  6172. }
  6173. func CreatedNewCancelStock(info models.DrugCancelStockInfo, tx *gorm.DB) error {
  6174. err := tx.Create(&info).Error
  6175. if err != nil {
  6176. tx.Rollback()
  6177. return err
  6178. }
  6179. return err
  6180. }
  6181. func IsExsitStockFlow(goodid int64, cancel_order_number string) (*models.VmStockFlow, error) {
  6182. flow := models.VmStockFlow{}
  6183. err := XTReadDB().Where("good_id = ? and cancel_order_number = ? and status = 1", goodid, cancel_order_number).Find(&flow).Error
  6184. if err == gorm.ErrRecordNotFound {
  6185. return nil, err
  6186. }
  6187. if err != nil {
  6188. return nil, err
  6189. }
  6190. return &flow, nil
  6191. }
  6192. func UpdateStockFlowTwo(id int64, flow models.VmStockFlow) error {
  6193. err := XTWriteDB().Model(&flow).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"count": flow.Count}).Error
  6194. return err
  6195. }
  6196. func UpdateStockFlowThree(id int64, flow models.VmStockFlow) error {
  6197. 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
  6198. return err
  6199. }
  6200. func GetLastWarehouseOutInfoById(drugid int64) (models.DrugWarehouseOutInfo, error) {
  6201. flow := models.DrugWarehouseOutInfo{}
  6202. err := XTReadDB().Where("drug_id = ? and status = 1", drugid).Last(&flow).Error
  6203. return flow, err
  6204. }
  6205. func GetLastNewWarehouseOutInfoById(drugid int64, tx *gorm.DB) (models.DrugWarehouseOutInfo, error) {
  6206. flow := models.DrugWarehouseOutInfo{}
  6207. err := tx.Where("drug_id = ? and status = 1", drugid).Last(&flow).Error
  6208. if err != gorm.ErrRecordNotFound {
  6209. if err != nil {
  6210. tx.Rollback()
  6211. return flow, err
  6212. }
  6213. }
  6214. return flow, err
  6215. }
  6216. func GetLastWarehoseByGoodId(goodid int64, patient_id int64, record_date int64) (models.WarehouseOutInfo, error) {
  6217. info := models.WarehouseOutInfo{}
  6218. 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
  6219. return info, err
  6220. }
  6221. func ModifyDrugWarehouseOutInfo(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64) error {
  6222. 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
  6223. return err
  6224. }
  6225. func ModifyDrugWarehouseOutInfoOne(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64, warehouse_info_id int64) error {
  6226. 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
  6227. return err
  6228. }
  6229. func GetDrugWarehouseOutInfoByWarehosueId(drugid int64, patientid int64, recorddate int64, warehosueinfoid int64) (*models.DrugWarehouseOutInfo, error) {
  6230. info := models.DrugWarehouseOutInfo{}
  6231. 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
  6232. if err == gorm.ErrRecordNotFound {
  6233. return nil, err
  6234. }
  6235. if err != nil {
  6236. return nil, err
  6237. }
  6238. return &info, nil
  6239. }
  6240. func GetDrugFlowRecord(warehouse_out_order_number string, drugid int64, patientid int64) (*models.DrugFlow, error) {
  6241. drugFlow := models.DrugFlow{}
  6242. 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
  6243. if err == gorm.ErrRecordNotFound {
  6244. return nil, err
  6245. }
  6246. if err != nil {
  6247. return nil, err
  6248. }
  6249. return &drugFlow, nil
  6250. }
  6251. func ModifyDrugFlow(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64) error {
  6252. 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
  6253. return err
  6254. }
  6255. func ModifyDrugFlowOne(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64, warehousing_detail_id int64) error {
  6256. 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
  6257. return err
  6258. }
  6259. func GetDrugAutoReduceRecordOne(drugid int64, patientid int64, recorddate int64) (detail []*models.DrugAutomaticReduceDetail, err error) {
  6260. err = XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  6261. return detail, err
  6262. }
  6263. func GetDrugAutoReduceRecord(drugid int64, patientid int64, recorddate int64) (*models.DrugAutomaticReduceDetail, error) {
  6264. detail := models.DrugAutomaticReduceDetail{}
  6265. err := XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  6266. if err == gorm.ErrRecordNotFound {
  6267. return nil, err
  6268. }
  6269. if err != nil {
  6270. return nil, err
  6271. }
  6272. return &detail, nil
  6273. }
  6274. func ModifyAutoMaticReduce(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64) error {
  6275. 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
  6276. return err
  6277. }
  6278. func ModifyAutoMaticReduceOne(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64, warehouse_info_id int64) error {
  6279. 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
  6280. return err
  6281. }
  6282. func ModifyDrugWarehouse(id int64, stock_max_number int64, stock_min_number int64) (models.DrugWarehouseInfo, error) {
  6283. info := models.DrugWarehouseInfo{}
  6284. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  6285. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  6286. return info, err
  6287. }
  6288. func ModeifyDrugWarehosueInfo(id int64, stock_max_number int64) error {
  6289. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  6290. return err
  6291. }
  6292. func ModeFyStockMinById(id int64, stock_min_number int64) error {
  6293. info := models.DrugWarehouseInfo{}
  6294. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  6295. return err
  6296. }
  6297. func GetDrugCancelById(id int64) (models.DrugCancelStockInfo, error) {
  6298. info := models.DrugCancelStockInfo{}
  6299. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  6300. return info, err
  6301. }
  6302. func ModeFyDrugWareseInfo(id int64, stock_max_number int64) error {
  6303. info := models.DrugWarehouseInfo{}
  6304. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", stock_max_number)).Error
  6305. return err
  6306. }
  6307. func ModeFyDrugWarehouseMin(id int64, stock_min_number int64) error {
  6308. info := models.DrugWarehouseInfo{}
  6309. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", stock_min_number)).Error
  6310. return err
  6311. }
  6312. func GetAllHisDoctorAdviceById(drugid int64, patient_id int64, advicedate int64, orgid int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  6313. 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
  6314. return advice, err
  6315. }
  6316. func GetAllHisDoctorAdviceByIdOne(drugid int64, patient_id int64, advicedate int64, orgid int64, tx *gorm.DB) (advice []*models.HisDoctorAdviceInfo, err error) {
  6317. 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
  6318. if err != gorm.ErrRecordNotFound {
  6319. if err != nil {
  6320. tx.Rollback()
  6321. return advice, err
  6322. }
  6323. }
  6324. return advice, err
  6325. }
  6326. func GetAllDrugFlowById(drugid int64, patient_id int64, advicedate int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  6327. 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
  6328. return info, err
  6329. }
  6330. func GetAllDrugFlowByIdOne(drugid int64, patient_id int64, advicedate int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  6331. 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
  6332. if err != gorm.ErrRecordNotFound {
  6333. if err != nil {
  6334. tx.Rollback()
  6335. return info, err
  6336. }
  6337. }
  6338. return info, err
  6339. }
  6340. // 耗材加库存
  6341. func ModifyGoodAddInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6342. info := models.GoodInfo{}
  6343. 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
  6344. return info, err
  6345. }
  6346. func ModifyGoodAddInformationSix(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6347. info := models.GoodInfo{}
  6348. 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
  6349. 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
  6350. return info, err
  6351. }
  6352. func ModifyGoodReduceInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6353. info := models.GoodInfo{}
  6354. 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
  6355. return info, err
  6356. }
  6357. func ModifyGoodReduceInformationTwo(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6358. info := models.GoodInfo{}
  6359. 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
  6360. 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
  6361. return info, err
  6362. }
  6363. func ModifyGoodReduceInformationSix(good_id int64, sum_count int64, orgid int64, retail_price float64) (models.GoodInfo, error) {
  6364. info := models.GoodInfo{}
  6365. 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
  6366. return info, err
  6367. }
  6368. func GetWarehousingByIdList(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  6369. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  6370. return info, err
  6371. }
  6372. func GetWarehousingByIdListTwenty(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  6373. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  6374. return info, err
  6375. }
  6376. func GetWarehouseInfoByIdSix(id int64, orgid int64) (models.WarehousingInfo, error) {
  6377. info := models.WarehousingInfo{}
  6378. err := XTReadDB().Model(&info).Where("id = ? and status= 1 and org_id = ?", id, orgid).Find(&info).Error
  6379. return info, err
  6380. }
  6381. func GetWarehouseInfoByIdTwenty(id int64, orgid int64) (models.WarehousingInfo, error) {
  6382. info := models.WarehousingInfo{}
  6383. err := XTReadDB().Model(&info).Where("id = ? and org_id = ?", id, orgid).Find(&info).Error
  6384. return info, err
  6385. }
  6386. // 药品加库存
  6387. func ModifyDrugAddInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6388. info := models.BaseDrugLib{}
  6389. 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
  6390. return info, err
  6391. }
  6392. func ModifyDrugAddInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6393. info := models.BaseDrugLib{}
  6394. 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
  6395. 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
  6396. return info, err
  6397. }
  6398. func ModifyDrugReduceInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6399. info := models.BaseDrugLib{}
  6400. 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
  6401. return info, err
  6402. }
  6403. func ModifyDrugReduceInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6404. info := models.BaseDrugLib{}
  6405. 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
  6406. 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
  6407. return info, err
  6408. }
  6409. func ModifyDrugReduceInformationSix(drug_id int64, sum_count int64, orgid int64, batch_retai_price float64) (models.BaseDrugLib, error) {
  6410. info := models.BaseDrugLib{}
  6411. 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
  6412. 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
  6413. return info, err
  6414. }
  6415. func GetLastDrugwarehouseInfoById(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  6416. err = XTReadDB().Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  6417. return info, err
  6418. }
  6419. func GetLastDrugwarehouseInfoByIdTwenty(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  6420. err = XTReadDB().Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  6421. return info, err
  6422. }
  6423. func GetWarhouseOutById(id int64) (models.WarehouseOutInfo, error) {
  6424. info := models.WarehouseOutInfo{}
  6425. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  6426. return info, err
  6427. }
  6428. func FindeGoodWareHouseOutInfo(id int64, storehouse_id int64, orgid int64) (out []*models.WarehouseOutInfo, err error) {
  6429. err = XTReadDB().Where("warehouse_out_id = ? and storehouse_id =? and org_id = ?", id, storehouse_id, orgid).Find(&out).Error
  6430. return out, err
  6431. }
  6432. func ModifyWarehouseInfo(id int64, count int64) (models.WarehousingInfo, error) {
  6433. info := models.WarehousingInfo{}
  6434. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  6435. return info, err
  6436. }
  6437. func GetOrgIdList() (template []*models.VMGobalTemplate, err error) {
  6438. err = XTReadDB().Where("status = 1").Find(&template).Error
  6439. return template, err
  6440. }
  6441. func GetAllGoodList(orgid int64) (good []*models.GoodInfo, err error) {
  6442. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&good).Error
  6443. return good, err
  6444. }
  6445. func GetMobileGoodList(org_id int64) (list []*models.GoodInformationThirty, err error) {
  6446. err = XTReadDB().Where("org_id = ? and status =1", org_id).Find(&list).Error
  6447. return list, err
  6448. }
  6449. func GetAllMobileProjectList(orgid int64) (project []*models.HisProject, err error) {
  6450. err = XTReadDB().Where("user_org_id =? and status =1", orgid).Find(&project).Error
  6451. return project, err
  6452. }
  6453. func ModifyGoodAddInformationSeven(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6454. info := models.GoodInfo{}
  6455. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6456. return info, err
  6457. }
  6458. func GetStockCountWarehouseInfoById(good_id int64) (info []*models.WarehousingInfo, err error) {
  6459. db := XTReadDB().Where("status = 1")
  6460. if good_id > 0 {
  6461. db = db.Where("good_id = ?", good_id)
  6462. }
  6463. err = db.Find(&info).Error
  6464. return info, err
  6465. }
  6466. func GetDrugStockCountWarehoseInfoById(drug_id int64) (info []*models.DrugWarehouseInfo, err error) {
  6467. db := XTReadDB().Model(&info).Where("status = 1")
  6468. if drug_id > 0 {
  6469. db = db.Where("drug_id = ?", drug_id)
  6470. }
  6471. err = db.Find(&info).Error
  6472. return info, err
  6473. }
  6474. func UpdateModifyDrugInfoById(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6475. lib := models.BaseDrugLib{}
  6476. err := XTWriteDB().Model(&lib).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6477. return lib, err
  6478. }
  6479. func GetWarehouseBySecondWarehouseId(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (*models.Warehousing, error) {
  6480. info := models.Warehousing{}
  6481. 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
  6482. if err == gorm.ErrRecordNotFound {
  6483. return nil, err
  6484. }
  6485. if err != nil {
  6486. return nil, err
  6487. }
  6488. return &info, nil
  6489. }
  6490. func GetLastWarehouseById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.Warehousing, error) {
  6491. info := models.Warehousing{}
  6492. 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
  6493. return info, err
  6494. }
  6495. func GetDrugWarehouseBySyS(orgid int64, is_sys int64, sencond_wahouse_id int64, record_date int64) (*models.DrugWarehouseOut, error) {
  6496. out := models.DrugWarehouseOut{}
  6497. 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
  6498. if err == gorm.ErrRecordNotFound {
  6499. return nil, err
  6500. }
  6501. if err != nil {
  6502. return nil, err
  6503. }
  6504. return &out, nil
  6505. }
  6506. func GetLastDrugWarehouseOutById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.DrugWarehouseOut, error) {
  6507. info := models.DrugWarehouseOut{}
  6508. 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
  6509. return info, err
  6510. }
  6511. func GetDrugWarehouseingBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (*models.DrugWarehouse, error) {
  6512. info := models.DrugWarehouse{}
  6513. 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
  6514. if err == gorm.ErrRecordNotFound {
  6515. return nil, err
  6516. }
  6517. if err != nil {
  6518. return nil, err
  6519. }
  6520. return &info, nil
  6521. }
  6522. func CreateDrugWaresing(warehouse models.DrugWarehouse) error {
  6523. err := XTWriteDB().Create(&warehouse).Error
  6524. return err
  6525. }
  6526. func GetLastDrugWarehouseBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (models.DrugWarehouse, error) {
  6527. info := models.DrugWarehouse{}
  6528. 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
  6529. return info, err
  6530. }
  6531. func ModifyGoodReduceInformationTwenty(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6532. info := models.GoodInfo{}
  6533. 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
  6534. 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
  6535. return info, err
  6536. }
  6537. func ModifyGoodReduceInformationTwentyOne(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6538. info := models.GoodInfo{}
  6539. 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
  6540. 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
  6541. return info, err
  6542. }
  6543. func UpdateMedicalSumCount(drug_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6544. tx := XTWriteDB().Begin()
  6545. 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
  6546. if err != nil {
  6547. tx.Rollback()
  6548. return err
  6549. }
  6550. tx.Commit()
  6551. return err
  6552. }
  6553. func UpdateNewMedicalSumCount(drug_id int64, sum_count int64, sum_in_count int64, orgid int64, tx *gorm.DB) error {
  6554. 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
  6555. if err != nil {
  6556. tx.Rollback()
  6557. return err
  6558. }
  6559. return err
  6560. }
  6561. func UpdateMedicalSumCountOne(drug_id int64, sum_count int64, orgid int64) error {
  6562. tx := XTWriteDB().Begin()
  6563. 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
  6564. if err != nil {
  6565. tx.Rollback()
  6566. return err
  6567. }
  6568. tx.Commit()
  6569. return err
  6570. }
  6571. //func UpdateMedicalSumCountTX(drug_id int64, sum_count int64, sum_in_count int64, orgid int64,tx *gorm.DB) error {
  6572. // 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
  6573. // return err
  6574. //}
  6575. func UpdateGoodByGoodId(good_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6576. 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
  6577. return err
  6578. }
  6579. func GetDrugDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  6580. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  6581. table := XTReadDB().Table("xt_base_drug as x d")
  6582. fmt.Println(table)
  6583. if startime > 0 {
  6584. db = db.Where("x.advice_date >= ?", startime)
  6585. }
  6586. if endtime > 0 {
  6587. db = db.Where("x.advice_date <= ?", endtime)
  6588. }
  6589. if orgid > 0 {
  6590. db = db.Where("x.user_org_id = ?", orgid)
  6591. }
  6592. if len(keyword) > 0 {
  6593. likeKey := "%" + keyword + "%"
  6594. 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)
  6595. }
  6596. offset := (page - 1) * limit
  6597. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.created_time desc").Group("x.drug_id").Scan(&adviceinfo).Error
  6598. return adviceinfo, total, err
  6599. }
  6600. func GetDrugDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  6601. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  6602. table := XTReadDB().Table("xt_base_drug as x d")
  6603. fmt.Println(table)
  6604. if startime > 0 {
  6605. db = db.Where("x.advice_date >= ?", startime)
  6606. }
  6607. if endtime > 0 {
  6608. db = db.Where("x.advice_date <= ?", endtime)
  6609. }
  6610. if orgid > 0 {
  6611. db = db.Where("x.user_org_id = ?", orgid)
  6612. }
  6613. if len(keyword) > 0 {
  6614. likeKey := "%" + keyword + "%"
  6615. 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)
  6616. }
  6617. err = db.Order("x.created_time desc").Scan(&adviceinfo).Error
  6618. return adviceinfo, total, err
  6619. }
  6620. func GetAllDrugSummary(orgid int64) (drug []*models.BaseDrugLib, err error) {
  6621. err = XTReadDB().Where("org_id = ? and status = 1 and is_user = 1", orgid).Find(&drug).Error
  6622. return drug, err
  6623. }
  6624. func GetGoodDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  6625. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  6626. table := XTReadDB().Table("xt_good_information as d")
  6627. fmt.Println(table)
  6628. if startime > 0 {
  6629. db = db.Where("x.record_date >= ?", startime)
  6630. }
  6631. if endtime > 0 {
  6632. db = db.Where("x.record_date <= ?", endtime)
  6633. }
  6634. if orgid > 0 {
  6635. db = db.Where("x.user_org_id = ?", orgid)
  6636. }
  6637. if len(keyword) > 0 {
  6638. likeKey := "%" + keyword + "%"
  6639. 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")
  6640. }
  6641. offset := (page - 1) * limit
  6642. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Group("x.project_id").Scan(&project).Error
  6643. return project, total, err
  6644. }
  6645. func GetGoodDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  6646. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  6647. table := XTReadDB().Table("xt_good_information as d")
  6648. fmt.Println(table)
  6649. if startime > 0 {
  6650. db = db.Where("x.record_date >= ?", startime)
  6651. }
  6652. if endtime > 0 {
  6653. db = db.Where("x.record_date <= ?", endtime)
  6654. }
  6655. if orgid > 0 {
  6656. db = db.Where("x.user_org_id = ?", orgid)
  6657. }
  6658. if len(keyword) > 0 {
  6659. likeKey := "%" + keyword + "%"
  6660. 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")
  6661. }
  6662. err = db.Order("x.ctime desc").Group("x.id").Scan(&project).Error
  6663. return project, total, err
  6664. }
  6665. func GeAllGoodInformation(orgid int64) (good []*models.GoodInfo, err error) {
  6666. err = XTReadDB().Where("org_id =? and status = 1 and is_user = 1", orgid).Find(&good).Error
  6667. return good, err
  6668. }
  6669. func GetStockSettingIsExsit(orgid int64) (*models.XtStockSetting, error) {
  6670. setting := models.XtStockSetting{}
  6671. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6672. if err == gorm.ErrRecordNotFound {
  6673. return nil, err
  6674. }
  6675. if err != nil {
  6676. return nil, err
  6677. }
  6678. return &setting, nil
  6679. }
  6680. func CreateStockSetting(setting models.XtStockSetting) error {
  6681. err := XTWriteDB().Create(&setting).Error
  6682. return err
  6683. }
  6684. func UpdateStockSetting(orgid int64, setting models.XtStockSetting) error {
  6685. err := XTWriteDB().Model(&models.XtStockSetting{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_type": setting.IsType}).Error
  6686. return err
  6687. }
  6688. func FindStockSettingById(orgid int64) (models.XtStockSetting, error) {
  6689. setting := models.XtStockSetting{}
  6690. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6691. return setting, err
  6692. }
  6693. func GetAutoStockOutById(good_id int64, record_date int64, orgid int64, patient_id int64) (models.BloodAutomaticReduceDetail, error) {
  6694. detail := models.BloodAutomaticReduceDetail{}
  6695. 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
  6696. return detail, err
  6697. }
  6698. func GetAutoWarehouseOutList(good_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfoNight, error) {
  6699. detail := models.WarehouseOutInfoNight{}
  6700. 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
  6701. return detail, err
  6702. }
  6703. func GetAdviceConfig(orgid int64) (*models.XtAdviceSetting, error) {
  6704. setting := models.XtAdviceSetting{}
  6705. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6706. if err == gorm.ErrRecordNotFound {
  6707. return nil, err
  6708. }
  6709. if err != nil {
  6710. return nil, err
  6711. }
  6712. return &setting, nil
  6713. }
  6714. func CreateAdviceSetting(setting models.XtAdviceSetting) error {
  6715. err := XTWriteDB().Create(&setting).Error
  6716. return err
  6717. }
  6718. func UpdateAdviceSetting(orgid int64, setting models.XtAdviceSetting) error {
  6719. 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
  6720. return err
  6721. }
  6722. func FindAdviceSettingById(orgid int64) (models.XtAdviceSetting, error) {
  6723. setting := models.XtAdviceSetting{}
  6724. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6725. return setting, err
  6726. }
  6727. func GetPrescriptionConfig(orgid int64) (*models.XtPrescriptionConfig, error) {
  6728. config := models.XtPrescriptionConfig{}
  6729. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6730. if err == gorm.ErrRecordNotFound {
  6731. return nil, err
  6732. }
  6733. if err != nil {
  6734. return nil, err
  6735. }
  6736. return &config, nil
  6737. }
  6738. func CreatePrescriptionConfig(prescriptionConfig models.XtPrescriptionConfig) error {
  6739. err := XTWriteDB().Create(&prescriptionConfig).Error
  6740. return err
  6741. }
  6742. func UpdatePrescriptionConfig(orgid int64, config models.XtPrescriptionConfig) error {
  6743. 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
  6744. return err
  6745. }
  6746. func FindPrescriptionConfigById(orgid int64) (models.XtPrescriptionConfig, error) {
  6747. config := models.XtPrescriptionConfig{}
  6748. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6749. return config, err
  6750. }
  6751. func UpdateWarehouseInfoTwentyFive(maxNumber int64, drugid int64, orgID int64, storehouse_id int64, drup *models.XtBaseDrug) (err error) {
  6752. var deliver_number int64 = 0
  6753. var stock_number int64 = 0
  6754. deliver_number = maxNumber
  6755. // 根据先进先出原则,查询最先入库的批次,进行出库
  6756. // 如果没有对应的库存,则报错
  6757. //开启事物
  6758. warehouse, err := FindLastDrugWarehousingInfoByID(drugid, storehouse_id)
  6759. if err != nil {
  6760. return err
  6761. }
  6762. // 将该批次的剩余库存数量转换为拆零数量
  6763. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  6764. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  6765. if stock_number >= deliver_number {
  6766. var maxNumber int64 = 0
  6767. var minNumber int64 = 0
  6768. var stock_max_number int64 = 0
  6769. stock_max_number = warehouse.StockMaxNumber
  6770. maxNumber = deliver_number / drup.MinNumber
  6771. minNumber = deliver_number % drup.MinNumber
  6772. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  6773. minNumber = maxNumber
  6774. }
  6775. if drup.MaxUnit != drup.MinUnit {
  6776. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  6777. return errors.New("库存数量不足")
  6778. }
  6779. }
  6780. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  6781. if warehouse.StockMaxNumber < 0 {
  6782. warehouse.StockMaxNumber = 0
  6783. }
  6784. if warehouse.StockMinNumber < 0 {
  6785. warehouse.StockMinNumber = 0
  6786. }
  6787. warehouse.Mtime = time.Now().Unix()
  6788. if warehouse.StockMinNumber < minNumber {
  6789. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  6790. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  6791. } else {
  6792. if minNumber > 0 {
  6793. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  6794. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6795. } else {
  6796. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  6797. }
  6798. }
  6799. if minNumber == 0 && maxNumber != 1 {
  6800. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6801. }
  6802. }
  6803. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  6804. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6805. if warehouse.StockMinNumber == 0 {
  6806. warehouse.StockMaxNumber = stock_max_number
  6807. }
  6808. }
  6809. if drup.MaxUnit != drup.MinUnit {
  6810. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  6811. return errors.New("库存数量不足")
  6812. }
  6813. }
  6814. if warehouse.StockMinNumber <= 0 {
  6815. warehouse.StockMinNumber = 0
  6816. }
  6817. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6818. fmt.Println(errThree)
  6819. //查询默认仓库
  6820. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6821. //查询默认仓库剩余多少库存
  6822. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6823. var sum_count int64
  6824. var sum_in_count int64
  6825. for _, it := range list {
  6826. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6827. if it.MaxUnit == baseDrug.MaxUnit {
  6828. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6829. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6830. sum_count += it.StockMaxNumber + it.StockMinNumber
  6831. sum_in_count += it.WarehousingCount
  6832. }
  6833. }
  6834. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6835. return nil
  6836. } else {
  6837. warehouse.StockMaxNumber = 0
  6838. warehouse.StockMinNumber = 0
  6839. warehouse.Mtime = time.Now().Unix()
  6840. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6841. fmt.Println(errThree)
  6842. //查询默认仓库
  6843. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6844. //查询默认仓库剩余多少库存
  6845. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6846. var sum_count int64
  6847. var sum_in_count int64
  6848. for _, it := range list {
  6849. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6850. if it.MaxUnit == baseDrug.MaxUnit {
  6851. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6852. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6853. sum_count += it.StockMaxNumber + it.StockMinNumber
  6854. sum_in_count += it.WarehousingCount
  6855. }
  6856. }
  6857. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6858. if errThree != nil {
  6859. return errThree
  6860. }
  6861. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  6862. prescribingNumber_two_temp := deliver_number - stock_number
  6863. UpdateWarehouseInfoTwentyFive(prescribingNumber_two_temp, drugid, orgID, storehouse_id, drup)
  6864. }
  6865. return
  6866. }
  6867. func UpdateWarehouseInfoTentyFour(count int64, id int64) error {
  6868. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6869. return err
  6870. }
  6871. func UpdateWarehouseInfoTentyThree(count int64, id int64) error {
  6872. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6873. return err
  6874. }
  6875. func GetDrugOpenConfig(orgid int64) (*models.DrugOutConfig, error) {
  6876. config := models.DrugOutConfig{}
  6877. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6878. if err == gorm.ErrRecordNotFound {
  6879. return nil, err
  6880. }
  6881. if err != nil {
  6882. return nil, err
  6883. }
  6884. return &config, nil
  6885. }
  6886. func GetDrugOpenConfigOne(orgid int64) (models.DrugOutConfig, error) {
  6887. config := models.DrugOutConfig{}
  6888. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).First(&config).Error
  6889. return config, err
  6890. }
  6891. func FindXTHisRecordByOrgIdSix(org_id int64) (models.XtHisConfig, error) {
  6892. config := models.XtHisConfig{}
  6893. err = readDb.Model(&models.XtHisConfig{}).Where("status = 1 AND user_org_id = ?", org_id).Find(&config).Error
  6894. return config, err
  6895. }
  6896. func CreateDrugOutConfig(config models.DrugOutConfig) error {
  6897. err := XTWriteDB().Create(&config).Error
  6898. return err
  6899. }
  6900. func UpdateDrugOutConfig(orgid int64, drug_out_open int64) error {
  6901. err := XTWriteDB().Model(&models.DrugOutConfig{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": drug_out_open}).Error
  6902. return err
  6903. }
  6904. func FindDrugOutConfigById(orgid int64) (models.DrugOutConfig, error) {
  6905. config := models.DrugOutConfig{}
  6906. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6907. return config, err
  6908. }
  6909. func FindeDrugSettleConfigById(orgid int64) (models.HisSettleStockConfig, error) {
  6910. config := models.HisSettleStockConfig{}
  6911. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6912. return config, err
  6913. }
  6914. func UpdatedWarehouseOut(id int64) error {
  6915. err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6916. err = XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6917. return err
  6918. }
  6919. func UpdatedNewWarehouseOut(id int64, tx *gorm.DB) error {
  6920. err := tx.Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6921. if err != nil {
  6922. tx.Rollback()
  6923. return err
  6924. }
  6925. err = tx.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6926. if err != nil {
  6927. tx.Rollback()
  6928. return err
  6929. }
  6930. return err
  6931. }
  6932. func UpdatedSigleWarehouseOutInfo(id int64, info *models.WarehouseOutInfo) error {
  6933. tx := XTWriteDB().Begin()
  6934. err := tx.Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_info_id": info.WarehouseInfotId}).Error
  6935. if err != nil {
  6936. tx.Rollback()
  6937. return err
  6938. }
  6939. tx.Commit()
  6940. return err
  6941. }
  6942. func UpdatedNewSigleWarehouseOutInfo(id int64, info *models.WarehouseOutInfo, tx *gorm.DB) error {
  6943. err := tx.Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_info_id": info.WarehouseInfotId}).Error
  6944. if err != nil {
  6945. tx.Rollback()
  6946. return err
  6947. }
  6948. return err
  6949. }
  6950. func GetLastGoodWarehouseOutInfo(orgid int64, id int64, tx *gorm.DB) (info []*models.WarehouseOutInfo, err error) {
  6951. err = tx.Where("org_id = ? and status = 1 and warehouse_out_id = ?", orgid, id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  6952. return tx.Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  6953. }).Find(&info).Error
  6954. if err != nil {
  6955. tx.Rollback()
  6956. return info, err
  6957. }
  6958. return info, err
  6959. }
  6960. func GetLastGoodWarehouseOutInfoById(orgid int64) (models.WarehouseOutInfo, error) {
  6961. info := models.WarehouseOutInfo{}
  6962. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&info).Error
  6963. return info, err
  6964. }
  6965. func ModifyWarehouseOutInfo(id int64, info *models.WarehouseOutInfo, tx *gorm.DB) error {
  6966. 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
  6967. if err != nil {
  6968. tx.Rollback()
  6969. return err
  6970. }
  6971. return err
  6972. }
  6973. func UpdateWarehouseOutFlow(info models.VmStockFlow, id int64) error {
  6974. 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
  6975. return err
  6976. }
  6977. func GetLastGoodWarehouseOutTwenty(orgid int64) (models.WarehouseOut, error) {
  6978. out := models.WarehouseOut{}
  6979. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  6980. return out, err
  6981. }
  6982. func GetLastGoodWarehouseOutTwentyOne(orgid int64, tx *gorm.DB) (models.WarehouseOut, error) {
  6983. out := models.WarehouseOut{}
  6984. err := tx.Where("org_id = ? and status = 1", orgid).Last(&out).Error
  6985. if err != gorm.ErrRecordNotFound {
  6986. if err != nil {
  6987. tx.Rollback()
  6988. return out, err
  6989. }
  6990. }
  6991. return out, err
  6992. }
  6993. func UpdateaGoodWarehouseInfo(count int64, good_id int64, orgid int64, warehouse_info_id int64) error {
  6994. ut := writeDb.Begin()
  6995. 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
  6996. if err != nil {
  6997. ut.Rollback()
  6998. return err
  6999. }
  7000. ut.Commit()
  7001. return err
  7002. }
  7003. func ModifyGoodOutInfoLog(good_id int64, id int64, record_date int64, user_org_id int64, tx *gorm.DB) error {
  7004. outInfoLog := models.XtGoodWarehouseOutInfoLog{}
  7005. err := tx.Model(&outInfoLog).Where("good_id = ? and warehouse_out_info_id = ? and status=1 and record_date = ? and user_org_id = ?", good_id, id, record_date, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  7006. if err != nil {
  7007. tx.Rollback()
  7008. return err
  7009. }
  7010. return err
  7011. }
  7012. func UpdateaNewGoodWarehouseInfo(count int64, good_id int64, orgid int64, warehouse_info_id int64, tx *gorm.DB) error {
  7013. 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
  7014. if err != nil {
  7015. tx.Rollback()
  7016. return err
  7017. }
  7018. return err
  7019. }
  7020. func ModifyGoodWarehouseOut(id int64) error {
  7021. ut := writeDb.Begin()
  7022. err := ut.Model(&models.WarehouseOut{}).Where("id = ? and status =1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7023. if err != nil {
  7024. ut.Rollback()
  7025. return err
  7026. }
  7027. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7028. if err != nil {
  7029. ut.Rollback()
  7030. return err
  7031. }
  7032. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  7033. if err != nil {
  7034. ut.Rollback()
  7035. return err
  7036. }
  7037. err = ut.Model(&models.XtWarehouseFlushInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  7038. if err != nil {
  7039. ut.Rollback()
  7040. return err
  7041. }
  7042. ut.Commit()
  7043. return err
  7044. }
  7045. func ModifyNewGoodWarehouseOut(id int64, tx *gorm.DB) error {
  7046. err := tx.Model(&models.WarehouseOut{}).Where("id = ? and status =1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7047. if err != nil {
  7048. tx.Rollback()
  7049. return err
  7050. }
  7051. err = tx.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7052. if err != nil {
  7053. tx.Rollback()
  7054. return err
  7055. }
  7056. err = tx.Model(&models.VmStockFlow{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  7057. if err != nil {
  7058. tx.Rollback()
  7059. return err
  7060. }
  7061. err = tx.Model(&models.XtWarehouseFlushInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  7062. if err != nil {
  7063. tx.Rollback()
  7064. return err
  7065. }
  7066. return err
  7067. }
  7068. func GetGoodWarehouseInfoById(waresing_id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  7069. err = XTReadDB().Where("org_id = ? and status = 1 and warehousing_id = ?", orgid, waresing_id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  7070. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  7071. }).Find(&info).Error
  7072. return info, err
  7073. }
  7074. func GetNewGoodWarehouseInfoById(waresing_id int64, orgid int64, tx *gorm.DB) (info []*models.VmWarehousingInfoSix, err error) {
  7075. err = tx.Where("org_id = ? and status = 1 and warehousing_id = ?", orgid, waresing_id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  7076. return tx.Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  7077. }).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 FindLastWarehousing(order string) (info models.Warehousing, err error) {
  7087. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  7088. return info, err
  7089. }
  7090. func UpdateWarehouseDetail(info *models.WarehousingInfo, id int64) error {
  7091. 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
  7092. return err
  7093. }
  7094. func UpdateNewWarehouseDetail(info *models.WarehousingInfo, id int64, tx *gorm.DB) error {
  7095. 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, "split_of_number": info.SplitOfNumber}).Error
  7096. if err != nil {
  7097. tx.Rollback()
  7098. return err
  7099. }
  7100. return err
  7101. }
  7102. func UpdateCheckWarehouseInfo(id int64, checker int64) error {
  7103. tx := XTWriteDB().Begin()
  7104. err := tx.Model(&models.Warehousing{}).Where("id =? and status = 1", id).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  7105. if err != nil {
  7106. tx.Rollback()
  7107. return err
  7108. }
  7109. tx.Commit()
  7110. return err
  7111. }
  7112. func UpdateNewCheckWarehouseInfo(id int64, checker int64, tx *gorm.DB) error {
  7113. err := tx.Model(&models.Warehousing{}).Where("id =? and status = 1", id).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  7114. if err != nil {
  7115. tx.Rollback()
  7116. return err
  7117. }
  7118. return err
  7119. }
  7120. func GetWarehouseInfoList(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  7121. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7122. return info, err
  7123. }
  7124. func GetNewWarehouseInfoList(id int64, orgid int64, tx *gorm.DB) (info []*models.WarehousingInfo, err error) {
  7125. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7126. if err != gorm.ErrRecordNotFound {
  7127. if err != nil {
  7128. tx.Rollback()
  7129. return info, err
  7130. }
  7131. }
  7132. return info, err
  7133. }
  7134. func UpdateWarehouseInfoByIdListThree(count int64, id int64) error {
  7135. tx := XTWriteDB().Begin()
  7136. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"stock_count": count}).Error
  7137. if err != nil {
  7138. tx.Rollback()
  7139. return err
  7140. }
  7141. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7142. if err != nil {
  7143. tx.Rollback()
  7144. return err
  7145. }
  7146. tx.Commit()
  7147. return err
  7148. }
  7149. func UpdateNewWarehouseInfoByIdListThree(count int64, id int64, tx *gorm.DB) error {
  7150. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"stock_count": count}).Error
  7151. if err != nil {
  7152. tx.Rollback()
  7153. return err
  7154. }
  7155. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7156. if err != nil {
  7157. tx.Rollback()
  7158. return err
  7159. }
  7160. return err
  7161. }
  7162. func FindLastWarehousingSix(org_id int64) (info models.Warehousing, err error) {
  7163. err = readDb.Model(&models.Warehousing{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7164. return info, err
  7165. }
  7166. func FindNewLastWarehousingSix(org_id int64, tx *gorm.DB) (info models.Warehousing, err error) {
  7167. err = tx.Model(&models.Warehousing{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7168. if err != gorm.ErrRecordNotFound {
  7169. if err != nil {
  7170. tx.Rollback()
  7171. return info, err
  7172. }
  7173. }
  7174. return info, err
  7175. }
  7176. func FindLastWarehousingSeven(org_id int64) (info models.DrugWarehouse, err error) {
  7177. err = readDb.Model(&models.DrugWarehouse{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7178. return info, err
  7179. }
  7180. func FindNewLastWarehousingSeven(org_id int64, tx *gorm.DB) (info models.DrugWarehouse, err error) {
  7181. err = tx.Model(&models.DrugWarehouse{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7182. if err != gorm.ErrRecordNotFound {
  7183. if err != nil {
  7184. tx.Rollback()
  7185. return info, err
  7186. }
  7187. }
  7188. return info, err
  7189. }
  7190. func FindDrugWarehouseInfoListById(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7191. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "status = 1 and org_id =?", orgid).Find(&info).Error
  7192. return info, err
  7193. }
  7194. func FindNewDrugWarehouseInfoListById(id int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7195. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "status = 1 and org_id =?", orgid).Find(&info).Error
  7196. if err != gorm.ErrRecordNotFound {
  7197. if err != nil {
  7198. tx.Rollback()
  7199. return info, err
  7200. }
  7201. }
  7202. return info, err
  7203. }
  7204. func CreatedWarehouseing(info *models.DrugWarehouseInfo) error {
  7205. err := XTWriteDB().Create(&info).Error
  7206. return err
  7207. }
  7208. func CreatedNewWarehouseing(info *models.DrugWarehouseInfo, tx *gorm.DB) error {
  7209. err := tx.Create(&info).Error
  7210. if err != nil {
  7211. tx.Rollback()
  7212. return err
  7213. }
  7214. return err
  7215. }
  7216. func UpdateWarehouseing(info *models.DrugWarehouseInfo) error {
  7217. err := XTWriteDB().Save(&info).Error
  7218. return err
  7219. }
  7220. func UpdateNewWarehouseing(info *models.DrugWarehouseInfo, tx *gorm.DB) error {
  7221. err := tx.Save(&info).Error
  7222. if err != nil {
  7223. tx.Rollback()
  7224. return err
  7225. }
  7226. return err
  7227. }
  7228. func CheckWarehousingInfo(id int64, orgid int64, checker int64) error {
  7229. tx := XTWriteDB().Begin()
  7230. 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
  7231. if err != nil {
  7232. tx.Rollback()
  7233. return err
  7234. }
  7235. tx.Commit()
  7236. return err
  7237. }
  7238. func CheckNewWarehousingInfo(id int64, orgid int64, checker int64, tx *gorm.DB) error {
  7239. 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
  7240. if err != nil {
  7241. tx.Rollback()
  7242. return err
  7243. }
  7244. return err
  7245. }
  7246. func GetWarehousingInfoByList(id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  7247. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7248. return info, err
  7249. }
  7250. func GetNewWarehousingInfoByList(id int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
  7251. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7252. if err != gorm.ErrRecordNotFound {
  7253. if err != nil {
  7254. tx.Rollback()
  7255. return info, err
  7256. }
  7257. }
  7258. return info, err
  7259. }
  7260. func AddDrugWarehouseStockMaxNumber(count int64, id int64) error {
  7261. tx := XTWriteDB().Begin()
  7262. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7263. if err != nil {
  7264. tx.Rollback()
  7265. return err
  7266. }
  7267. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7268. if err != nil {
  7269. tx.Rollback()
  7270. return err
  7271. }
  7272. tx.Commit()
  7273. return err
  7274. }
  7275. func AddNewDrugWarehouseStockMaxNumber(count int64, id int64, tx *gorm.DB) error {
  7276. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7277. if err != nil {
  7278. tx.Rollback()
  7279. return err
  7280. }
  7281. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7282. if err != nil {
  7283. tx.Rollback()
  7284. return err
  7285. }
  7286. return err
  7287. }
  7288. func UpdateWarehousingInfoFlow(id int64, orgid int64) error {
  7289. tx := XTWriteDB().Begin()
  7290. 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
  7291. if err != nil {
  7292. tx.Rollback()
  7293. return err
  7294. }
  7295. tx.Commit()
  7296. return err
  7297. }
  7298. func DeleteModiftyDrugWarehouseInfo(id int64, tx *gorm.DB) error {
  7299. info := models.DrugWarehouseInfo{}
  7300. err := tx.Model(&info).Where("id= ? and status=1", id).Updates(map[string]interface{}{"sum_count": 0}).Error
  7301. if err != nil {
  7302. tx.Rollback()
  7303. return err
  7304. }
  7305. return err
  7306. }
  7307. func UpdateNewWarehousingInfoFlow(id int64, orgid int64, tx *gorm.DB) error {
  7308. 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
  7309. if err != nil {
  7310. tx.Rollback()
  7311. return err
  7312. }
  7313. return err
  7314. }
  7315. func AddDrugWarehouseStockMinNumber(count int64, id int64) error {
  7316. tx := XTWriteDB().Begin()
  7317. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7318. if err != nil {
  7319. tx.Rollback()
  7320. return err
  7321. }
  7322. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7323. tx.Commit()
  7324. return err
  7325. }
  7326. func AddNewDrugWarehouseStockMinNumber(count int64, id int64, tx *gorm.DB) error {
  7327. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7328. if err != nil {
  7329. tx.Rollback()
  7330. return err
  7331. }
  7332. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7333. if err != nil {
  7334. tx.Rollback()
  7335. return err
  7336. }
  7337. return err
  7338. }
  7339. func ModifyDrugWarehouseInfoById(id int64, sum_count int64, tx *gorm.DB) error {
  7340. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"sum_count": sum_count}).Error
  7341. if err != nil {
  7342. tx.Rollback()
  7343. return err
  7344. }
  7345. return err
  7346. }
  7347. func ReturnCheckWarehouseingInfo(id int64, orgid int64) error {
  7348. tx := XTWriteDB().Begin()
  7349. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7350. if err != nil {
  7351. tx.Rollback()
  7352. return err
  7353. }
  7354. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("warehousing_id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7355. if err != nil {
  7356. tx.Rollback()
  7357. return err
  7358. }
  7359. tx.Commit()
  7360. return err
  7361. }
  7362. func ReturnNewCheckWarehouseingInfo(id int64, orgid int64, tx *gorm.DB) error {
  7363. err := tx.Model(&models.DrugWarehouse{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7364. if err != nil {
  7365. tx.Rollback()
  7366. return err
  7367. }
  7368. err = tx.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7369. if err != nil {
  7370. tx.Rollback()
  7371. return err
  7372. }
  7373. err = tx.Model(models.XtDrugWarehouseInfoLog{}).Where("warehouse_info_id = ? and status=1", id).Update(map[string]interface{}{"status": 0}).Error
  7374. if err != nil {
  7375. tx.Rollback()
  7376. return err
  7377. }
  7378. return err
  7379. }
  7380. func AddDrugReturnWarehouseStockMaxNumber(count int64, id int64) error {
  7381. tx := XTWriteDB().Begin()
  7382. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  7383. if err != nil {
  7384. tx.Rollback()
  7385. return err
  7386. }
  7387. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7388. if err != nil {
  7389. tx.Rollback()
  7390. return err
  7391. }
  7392. tx.Commit()
  7393. return err
  7394. }
  7395. func AddNewDrugReturnWarehouseStockMaxNumber(count int64, id int64, tx *gorm.DB) error {
  7396. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  7397. if err != nil {
  7398. tx.Rollback()
  7399. return err
  7400. }
  7401. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7402. if err != nil {
  7403. tx.Rollback()
  7404. return err
  7405. }
  7406. return err
  7407. }
  7408. func AddDrugReturnWarehouseStockMinNumber(count int64, id int64) error {
  7409. tx := XTWriteDB().Begin()
  7410. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  7411. if err != nil {
  7412. tx.Rollback()
  7413. return err
  7414. }
  7415. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7416. if err != nil {
  7417. tx.Rollback()
  7418. return err
  7419. }
  7420. tx.Commit()
  7421. return err
  7422. }
  7423. func AddNewDrugReturnWarehouseStockMinNumber(count int64, id int64, tx *gorm.DB) error {
  7424. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  7425. if err != nil {
  7426. tx.Rollback()
  7427. return err
  7428. }
  7429. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7430. if err != nil {
  7431. tx.Rollback()
  7432. return err
  7433. }
  7434. return err
  7435. }
  7436. func UpdateDrugWarehouseInfoTwenty(id int64, info *models.DrugWarehouseInfo) error {
  7437. 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
  7438. return err
  7439. }
  7440. func UpdateNewDrugWarehouseInfoTwenty(id int64, info *models.DrugWarehouseInfo, tx *gorm.DB) error {
  7441. 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
  7442. if err != nil {
  7443. tx.Rollback()
  7444. return err
  7445. }
  7446. return err
  7447. }
  7448. func UpdatedDrugWarehouseInfo(info *models.DrugWarehouseOutInfo, id int64) error {
  7449. 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
  7450. return err
  7451. }
  7452. func UpdatedNewDrugWarehouseInfo(info *models.DrugWarehouseOutInfo, id int64, tx *gorm.DB) error {
  7453. 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
  7454. if err != nil {
  7455. tx.Rollback()
  7456. return err
  7457. }
  7458. return err
  7459. }
  7460. func UpdatedSelfDrugWarehouseInfo(info *models.XtSelfDrugWarehouseOutInfo, id int64, tx *gorm.DB) error {
  7461. 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
  7462. if err != gorm.ErrRecordNotFound {
  7463. return err
  7464. }
  7465. return err
  7466. }
  7467. func GetDrugLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  7468. out := models.DrugWarehouseOut{}
  7469. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  7470. return out, err
  7471. }
  7472. func GetNewDrugLastWarehouseOut(orgid int64, tx *gorm.DB) (models.DrugWarehouseOut, error) {
  7473. out := models.DrugWarehouseOut{}
  7474. err := tx.Where("org_id = ? and status = 1", orgid).Find(&out).Error
  7475. if err != gorm.ErrRecordNotFound {
  7476. if err != nil {
  7477. tx.Rollback()
  7478. return out, err
  7479. }
  7480. }
  7481. return out, err
  7482. }
  7483. func GetSelfDrugLastWarehouseOut(orgid int64, tx *gorm.DB) (models.XtSelfDrugWarehouseOut, error) {
  7484. out := models.XtSelfDrugWarehouseOut{}
  7485. err := tx.Where("org_id = ? and status = 1", orgid).Find(&out).Error
  7486. if err != gorm.ErrRecordNotFound {
  7487. if err != nil {
  7488. tx.Rollback()
  7489. return out, err
  7490. }
  7491. }
  7492. return out, err
  7493. }
  7494. func GetSelfDrugLastWarehouseOutOne(id int64, tx *gorm.DB) (models.XtSelfDrugWarehouseOut, error) {
  7495. out := models.XtSelfDrugWarehouseOut{}
  7496. err := tx.Where("id = ? and status = 1", id).Find(&out).Error
  7497. if err != gorm.ErrRecordNotFound {
  7498. if err != nil {
  7499. tx.Rollback()
  7500. return out, err
  7501. }
  7502. }
  7503. return out, err
  7504. }
  7505. func GetDrugWarehouseOutListById(warehouse_out_id int64, orgId int64) (info []*models.DrugWarehouseOutInfo, err error) {
  7506. err = XTReadDB().Where("warehouse_out_id = ? and org_id =? and status = 1", warehouse_out_id, orgId).Preload("BaseDrugLib", "status=1").Find(&info).Error
  7507. return info, err
  7508. }
  7509. func GetNewDrugWarehouseOutListById(warehouse_out_id int64, orgId int64, tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  7510. err = tx.Where("warehouse_out_id = ? and org_id =? and status = 1", warehouse_out_id, orgId).Preload("BaseDrugLib", "status=1").Find(&info).Error
  7511. if err != gorm.ErrRecordNotFound {
  7512. if err != nil {
  7513. tx.Rollback()
  7514. return info, err
  7515. }
  7516. }
  7517. return info, err
  7518. }
  7519. func UpdateCheckDrugOut(out models.DrugWarehouseOut, id int64) error {
  7520. tx := XTWriteDB().Begin()
  7521. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": out.IsCheck}).Error
  7522. if err != nil {
  7523. tx.Rollback()
  7524. return err
  7525. }
  7526. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7527. if err != nil {
  7528. tx.Rollback()
  7529. return err
  7530. }
  7531. tx.Commit()
  7532. return err
  7533. }
  7534. func UpdateNewCheckDrugOut(out models.DrugWarehouseOut, id int64, tx *gorm.DB) error {
  7535. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": out.IsCheck}).Error
  7536. if err != nil {
  7537. tx.Rollback()
  7538. return err
  7539. }
  7540. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7541. if err != nil {
  7542. tx.Rollback()
  7543. return err
  7544. }
  7545. return err
  7546. }
  7547. func UpdateCheckDrugOutInfoList(info models.DrugWarehouseOutInfo, warehouse_out_id int64) error {
  7548. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", warehouse_out_id).Update(map[string]interface{}{"is_check": info.IsCheck}).Error
  7549. return err
  7550. }
  7551. func UpdateNewCheckDrugOutInfoList(info models.DrugWarehouseOutInfo, warehouse_out_id int64, tx *gorm.DB) error {
  7552. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", warehouse_out_id).Update(map[string]interface{}{"is_check": info.IsCheck}).Error
  7553. if err != nil {
  7554. tx.Rollback()
  7555. return err
  7556. }
  7557. return err
  7558. }
  7559. func GetDrugWarehouseOutByLastId(id int64) (models.DrugWarehouseOut, error) {
  7560. out := models.DrugWarehouseOut{}
  7561. err := XTReadDB().Where("id = ? and status = 1", id).Find(&out).Error
  7562. return out, err
  7563. }
  7564. func GetNewDrugWarehouseOutByLastId(id int64, tx *gorm.DB) (models.DrugWarehouseOut, error) {
  7565. out := models.DrugWarehouseOut{}
  7566. err := tx.Where("id = ? and status = 1", id).Find(&out).Error
  7567. if err != gorm.ErrRecordNotFound {
  7568. if err != nil {
  7569. tx.Rollback()
  7570. return out, err
  7571. }
  7572. }
  7573. return out, err
  7574. }
  7575. func UpdateDrugMaxNumber(count int64, id int64) error {
  7576. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7577. return err
  7578. }
  7579. func UpdateNewDrugMaxNumber(count int64, id int64, tx *gorm.DB) error {
  7580. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7581. if err != gorm.ErrRecordNotFound {
  7582. if err != nil {
  7583. tx.Rollback()
  7584. return err
  7585. }
  7586. }
  7587. return err
  7588. }
  7589. func UpdateNewDrugMaxNumberOne(count int64, id int64, tx *gorm.DB) error {
  7590. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  7591. if err != gorm.ErrRecordNotFound {
  7592. if err != nil {
  7593. tx.Rollback()
  7594. return err
  7595. }
  7596. }
  7597. return err
  7598. }
  7599. func UpdateDrugMinNumber(count int64, id int64) error {
  7600. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  7601. return err
  7602. }
  7603. func UpdateNewDrugMinNumber(count int64, id int64, tx *gorm.DB) error {
  7604. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  7605. if err != nil {
  7606. tx.Rollback()
  7607. return err
  7608. }
  7609. return err
  7610. }
  7611. func DeleteDrugOutFlow(id int64, orgid int64) error {
  7612. 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
  7613. return err
  7614. }
  7615. func DeleteNewDrugOutFlow(id int64, orgid int64, tx *gorm.DB) error {
  7616. 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
  7617. if err != nil {
  7618. tx.Rollback()
  7619. return err
  7620. }
  7621. return err
  7622. }
  7623. func GetWarehouseOutByWarehouseInfoId(warehouse_info_id int64, good_id int64, orgid int64) (outInfo []*models.WarehouseOutInfo, err error) {
  7624. 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
  7625. return outInfo, err
  7626. }
  7627. func GetNewWarehouseOutByWarehouseInfoId(warehouse_info_id int64, good_id int64, orgid int64, tx *gorm.DB) (outInfo []*models.WarehouseOutInfo, err error) {
  7628. 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
  7629. if err != gorm.ErrRecordNotFound {
  7630. if err != nil {
  7631. tx.Rollback()
  7632. return outInfo, err
  7633. }
  7634. }
  7635. return outInfo, err
  7636. }
  7637. func GetDrugWarehouseOutByWarehouseInfoId(warehouse_info_id int64, drug_id int64, orgid int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  7638. 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
  7639. return outInfo, err
  7640. }
  7641. func GetNewDrugWarehouseOutByWarehouseInfoId(warehouse_info_id int64, drug_id int64, orgid int64, tx *gorm.DB) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  7642. 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
  7643. if err != gorm.ErrRecordNotFound {
  7644. if err != nil {
  7645. tx.Rollback()
  7646. return outInfo, err
  7647. }
  7648. }
  7649. return outInfo, err
  7650. }
  7651. func GetAllStockInList(id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  7652. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("GoodInfo", "status = 1 and org_id =?", orgid).Find(&info).Error
  7653. return info, err
  7654. }
  7655. func GetNewAllStockInList(id int64, orgid int64, tx *gorm.DB) (info []*models.VmWarehousingInfoSix, err error) {
  7656. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("GoodInfo", "status = 1 and org_id =?", orgid).Find(&info).Error
  7657. if err != gorm.ErrRecordNotFound {
  7658. if err != nil {
  7659. tx.Rollback()
  7660. return info, err
  7661. }
  7662. }
  7663. return info, err
  7664. }
  7665. func GetDrugWarehouseInfoList(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7666. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "org_id = ? and status = 1", orgid).Find(&info).Error
  7667. return info, err
  7668. }
  7669. func GetNewDrugWarehouseInfoList(id int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7670. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "org_id = ? and status = 1", orgid).Find(&info).Error
  7671. if err != gorm.ErrRecordNotFound {
  7672. if err != nil {
  7673. tx.Rollback()
  7674. return info, err
  7675. }
  7676. }
  7677. return info, err
  7678. }
  7679. func GetStockCountByGoodId(goodid int64, storehouse_id int64, orgId int64) (info []*models.WarehousingInfo, err error) {
  7680. 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
  7681. return info, err
  7682. }
  7683. func GetNewStockCountByGoodId(goodid int64, storehouse_id int64, orgId int64, tx *gorm.DB) (info []*models.WarehousingInfo, err error) {
  7684. 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
  7685. if err != gorm.ErrRecordNotFound {
  7686. if err != nil {
  7687. tx.Rollback()
  7688. return info, err
  7689. }
  7690. }
  7691. return info, err
  7692. }
  7693. func UpdateGoodInfoSumCount(goodid int64, sum_count int64, orgid int64) error {
  7694. 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
  7695. 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
  7696. return err
  7697. }
  7698. func UpdateGoodInfoSumCountSix(goodid int64, sum_count int64, orgid int64) error {
  7699. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7700. return err
  7701. }
  7702. func UpdateGoodInfoReduceSumCount(goodid int64, sum_count int64, orgid int64) error {
  7703. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7704. return err
  7705. }
  7706. func UpdateNewGoodInfoReduceSumCount(goodid int64, sum_count int64, orgid int64, tx *gorm.DB) error {
  7707. err = tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7708. if err != gorm.ErrRecordNotFound {
  7709. if err != nil {
  7710. tx.Rollback()
  7711. return err
  7712. }
  7713. }
  7714. return err
  7715. }
  7716. func GetGoodFlowByWarehouseOutId(good_id int64, user_org_id int64, id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  7717. 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
  7718. if err != gorm.ErrRecordNotFound {
  7719. if err != nil {
  7720. tx.Rollback()
  7721. return flow, err
  7722. }
  7723. }
  7724. return
  7725. }
  7726. func UpdateGoodInfoAddSumCount(goodid int64, sum_count int64, orgid int64, sum_in_count int64) error {
  7727. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7728. 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
  7729. return err
  7730. }
  7731. func UpdateNewGoodInfoAddSumCount(goodid int64, sum_count int64, orgid int64, sum_in_count int64, tx *gorm.DB) error {
  7732. err := tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7733. if err != nil {
  7734. tx.Rollback()
  7735. return err
  7736. }
  7737. 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
  7738. if err != nil {
  7739. tx.Rollback()
  7740. return err
  7741. }
  7742. return err
  7743. }
  7744. func GetCancelStockInfo(id int64, orgid int64) (stock []*models.CancelStockInfo, err error) {
  7745. 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
  7746. return stock, err
  7747. }
  7748. func ModifyCancelStockInfo(id int64, info *models.CancelStockInfo) error {
  7749. 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
  7750. return err
  7751. }
  7752. func GetCancelStockInfoListById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  7753. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  7754. return info, err
  7755. }
  7756. func GetWarehouseOutList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.WarehouseOutInfo, err error) {
  7757. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  7758. return info, err
  7759. }
  7760. func GetCancelWarehouseList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.CancelStockInfo, err error) {
  7761. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  7762. return info, err
  7763. }
  7764. func CheckCancelStock(stock models.CancelStock, cancel_stock_id int64, orgid int64) error {
  7765. 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
  7766. 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
  7767. return err
  7768. }
  7769. func GetCancelStockInfoById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  7770. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&info).Error
  7771. return info, err
  7772. }
  7773. func UpdateCancelStockNumber(stock_count int64, warehouse_info_id int64, orgid int64) error {
  7774. 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
  7775. return err
  7776. }
  7777. func UpdateGoodSumCount(sum_count int64, good_id int64, orgid int64) error {
  7778. 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
  7779. return err
  7780. }
  7781. func UpdateStockWarehouseInfo(cancel_stock_id int64, orgid int64, stock models.CancelStock) error {
  7782. 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
  7783. 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
  7784. return err
  7785. }
  7786. func UpdateStockWarehouseInfoStockFlow(id int64, orgid int64, goodid int64) error {
  7787. 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
  7788. return err
  7789. }
  7790. func UpdateStockNumberWarehouseInfo(warehouse_info_id int64, good_id int64, org_id int64, stock_count int64) error {
  7791. 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
  7792. 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
  7793. return err
  7794. }
  7795. func UpdateDrugSumCount(drugid int64, sum_count int64, sum_in_count int64, orgid int64) error {
  7796. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ?", drugid, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  7797. 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
  7798. return err
  7799. }
  7800. func GetDrugStockWarehouseInfo(id int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfo, err error) {
  7801. err = XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1 and storehouse_id = ?", id, orgid, storehouse_id).Find(&info).Error
  7802. return info, err
  7803. }
  7804. func UpdateBaseDrugSum(drugid int64, orgid int64, sum_count int64, sum_in_count int64) error {
  7805. 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
  7806. 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
  7807. return err
  7808. }
  7809. func GetDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64) (info []*models.DrugWarehouseInfo, err error) {
  7810. 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
  7811. return info, err
  7812. }
  7813. func GetSelfDrugAllStockInfoOne(patient_id int64, orgid int64, drugid int64) (info []*models.XtSelfDrugWarehouseInfo, err error) {
  7814. 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
  7815. return info, err
  7816. }
  7817. func GetNewDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
  7818. 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
  7819. if err != gorm.ErrRecordNotFound {
  7820. if err != nil {
  7821. tx.Rollback()
  7822. return info, err
  7823. }
  7824. }
  7825. return info, err
  7826. }
  7827. func UpdateBaseDrugSumInfo(sum_count int64, drugid int64, orgid int64, sum_in_count int64) error {
  7828. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7829. 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
  7830. return err
  7831. }
  7832. func UpdateNewBaseDrugSumInfo(sum_count int64, drugid int64, orgid int64, sum_in_count int64, tx *gorm.DB) error {
  7833. err := tx.Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7834. if err != gorm.ErrRecordNotFound {
  7835. if err != nil {
  7836. tx.Rollback()
  7837. return err
  7838. }
  7839. }
  7840. 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
  7841. if err != gorm.ErrRecordNotFound {
  7842. if err != nil {
  7843. tx.Rollback()
  7844. return err
  7845. }
  7846. }
  7847. return err
  7848. }
  7849. func UpdateBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64) error {
  7850. err = writeDb.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7851. return err
  7852. }
  7853. func UpdateNewBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64, tx *gorm.DB) error {
  7854. err = tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7855. if err != nil {
  7856. tx.Rollback()
  7857. return err
  7858. }
  7859. return err
  7860. }
  7861. func GetLastCancelStockByList(id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  7862. 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
  7863. return info, err
  7864. }
  7865. func GetCancelDrugStockList(cancel_stock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  7866. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  7867. return info, err
  7868. }
  7869. func ModifyCancelStock(cancel_stock_id int64, stock models.DrugCancelStock, orgid int64) error {
  7870. 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
  7871. 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
  7872. return err
  7873. }
  7874. func GetCancelDrugStock(cancel_stock_id int64, orgid int64) (models.DrugCancelStock, error) {
  7875. stock := models.DrugCancelStock{}
  7876. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&stock).Error
  7877. return stock, err
  7878. }
  7879. func GetDrugCancelCheckById(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  7880. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&stock).Error
  7881. return stock, err
  7882. }
  7883. func GetDrugCancelCheckByIdTwenty(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  7884. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 ", cancel_stock_id, orgid).Find(&stock).Error
  7885. return stock, err
  7886. }
  7887. func GetDrugWarehouseOutList(batch_number_id int64, drugId int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  7888. err = XTReadDB().Where("warehouse_info_id = ? and drug_id = ? and org_id = ? and status = 1", batch_number_id, drugId, orgid).Find(&info).Error
  7889. return info, err
  7890. }
  7891. func ModifyDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64) error {
  7892. 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
  7893. return err
  7894. }
  7895. func ModifyNewDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  7896. 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
  7897. if err != nil {
  7898. tx.Rollback()
  7899. return err
  7900. }
  7901. return err
  7902. }
  7903. func ModifyDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64) error {
  7904. 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
  7905. return err
  7906. }
  7907. func ModifyNewDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  7908. 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
  7909. if err != nil {
  7910. tx.Rollback()
  7911. return err
  7912. }
  7913. return err
  7914. }
  7915. func ModifyDrugWareshoueInfoId(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  7916. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and drug_id = ? and status = 1", id, orgid, drugid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", count)).Error
  7917. if err != nil {
  7918. tx.Rollback()
  7919. return err
  7920. }
  7921. return err
  7922. }
  7923. func ModifyDrugCancelStockInfo(id int64, info *models.DrugCancelStockInfo) error {
  7924. 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
  7925. return err
  7926. }
  7927. func ReturnDrugCancelStock(cancelstock_id int64, stock models.DrugCancelStock) error {
  7928. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  7929. err = XTWriteDB().Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  7930. return err
  7931. }
  7932. func GetDrugCancelStockInfo(cancelstock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  7933. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancelstock_id, orgid).Find(&info).Error
  7934. return info, err
  7935. }
  7936. func ModifyDrugFlowByCancelId(cancelstock_id int64, drugId int64, orgid int64) error {
  7937. 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
  7938. return err
  7939. }
  7940. func ModifyDrugMaxNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  7941. 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
  7942. return err
  7943. }
  7944. func ModifyDrugMinNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  7945. 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
  7946. return err
  7947. }
  7948. func GetDrugInfoSix(id int64, orgid int64) (models.DrugWarehouseInfo, error) {
  7949. info := models.DrugWarehouseInfo{}
  7950. err := XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1", id, orgid).Find(&info).Error
  7951. return info, err
  7952. }
  7953. func GetSelfDrugInfoSix(id int64, orgid int64) (models.XtSelfDrugWarehouseInfo, error) {
  7954. info := models.XtSelfDrugWarehouseInfo{}
  7955. err := XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1", id, orgid).Find(&info).Error
  7956. return info, err
  7957. }
  7958. func GetPrintList(storehouse_id int64, good_name string, orgid int64, limit int64, page int64) (storeinventory []*models.XtStockInventory, err error) {
  7959. db := XTReadDB().Model(&storeinventory).Where("status = 1")
  7960. if storehouse_id > 0 {
  7961. db = db.Where("storehouse_id = ?", storehouse_id)
  7962. }
  7963. if orgid > 0 {
  7964. db = db.Where("user_org_id = ?", orgid)
  7965. }
  7966. err = XTReadDB().Preload("GoodInfo", "status = 1 and org_id = ?", orgid).Preload("WarehousingInfo", "status = 1 and org_id=?", orgid).Find(&storeinventory).Error
  7967. return storeinventory, err
  7968. }
  7969. func GetGoodDialysisOutInfo(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.AutomaticReduceDetail, error) {
  7970. detail := models.AutomaticReduceDetail{}
  7971. 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
  7972. return detail, err
  7973. }
  7974. func GetGoodDialysisOutInfoSix(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.WarehouseOutInfo, error) {
  7975. detail := models.WarehouseOutInfo{}
  7976. 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
  7977. return detail, err
  7978. }
  7979. func GetGoodSumCount(storehouse_id int64, good_id int64, orgid int64) (models.XtGoodStockCount, error) {
  7980. stock := models.XtGoodStockCount{}
  7981. err := XTReadDB().Where("storehouse_id = ? and good_id = ? and user_org_id = ? and status = 1", storehouse_id, good_id, orgid).Find(&stock).Error
  7982. return stock, err
  7983. }
  7984. func CreateGoodStockCount(stock models.XtGoodStockCount) error {
  7985. err := XTWriteDB().Create(&stock).Error
  7986. return err
  7987. }
  7988. func AddGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7989. 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
  7990. return err
  7991. }
  7992. func ReduceGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7993. ut := XTWriteDB().Begin()
  7994. 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
  7995. if err != nil {
  7996. ut.Rollback()
  7997. return err
  7998. }
  7999. ut.Commit()
  8000. return err
  8001. }
  8002. func UpdateGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64) error {
  8003. 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
  8004. return err
  8005. }
  8006. func UpdateNewGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64, tx *gorm.DB) error {
  8007. 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
  8008. if err != nil {
  8009. tx.Rollback()
  8010. return err
  8011. }
  8012. return err
  8013. }
  8014. func CreateDrugStock(stock models.XtDrugStockCount) error {
  8015. err := XTWriteDB().Create(&stock).Error
  8016. return err
  8017. }
  8018. func UpdateDrugCount(storehouse_id int64, user_org_id int64, drug_id int64, sum_in_count int64, flush_count int64) error {
  8019. ut := XTWriteDB().Begin()
  8020. 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
  8021. if err != nil {
  8022. ut.Rollback()
  8023. return err
  8024. }
  8025. 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
  8026. if err != nil {
  8027. ut.Rollback()
  8028. return err
  8029. }
  8030. ut.Commit()
  8031. return err
  8032. }
  8033. func UpdateDrugCountOne(storehouse_id int64, user_org_id int64, drug_id int64, flush_count int64) error {
  8034. ut := XTWriteDB().Begin()
  8035. 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
  8036. if err != nil {
  8037. ut.Rollback()
  8038. return err
  8039. }
  8040. ut.Commit()
  8041. return err
  8042. }
  8043. func AddDrugSumOutCount(storehouse_id int64, sum_out_count int64, user_org_id int64, drug_id int64) error {
  8044. ut := XTWriteDB().Begin()
  8045. 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
  8046. if err != nil {
  8047. ut.Rollback()
  8048. return err
  8049. }
  8050. 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
  8051. if err != nil {
  8052. ut.Rollback()
  8053. return err
  8054. }
  8055. ut.Commit()
  8056. return err
  8057. }
  8058. func ReduceDrugSumOut(storehouse_id int64, user_org_id int64, drug_id int64, sum_out_count int64) error {
  8059. ut := XTWriteDB().Begin()
  8060. 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
  8061. if err != nil {
  8062. ut.Rollback()
  8063. return err
  8064. }
  8065. ut.Commit()
  8066. return err
  8067. }
  8068. func AddGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  8069. ut := XTWriteDB().Begin()
  8070. 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
  8071. if err != nil {
  8072. ut.Rollback()
  8073. return err
  8074. }
  8075. 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
  8076. if err != nil {
  8077. ut.Rollback()
  8078. return err
  8079. }
  8080. ut.Commit()
  8081. return err
  8082. }
  8083. func ReduceGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  8084. ut := XTWriteDB().Begin()
  8085. 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
  8086. if err != nil {
  8087. ut.Rollback()
  8088. return err
  8089. }
  8090. 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
  8091. if err != nil {
  8092. ut.Rollback()
  8093. return err
  8094. }
  8095. ut.Commit()
  8096. return err
  8097. }
  8098. func GetInvetoryWarehouseInfoList(orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  8099. if orgid == 10191 || orgid == 10138 || orgid == 10278 {
  8100. 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
  8101. return info, err
  8102. } else {
  8103. 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
  8104. return info, err
  8105. }
  8106. }
  8107. func GetSettleOpenConfig(user_org_id int64) (*models.HisSettleStockConfig, error) {
  8108. config := models.HisSettleStockConfig{}
  8109. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8110. if err == gorm.ErrRecordNotFound {
  8111. return nil, err
  8112. }
  8113. if err != nil {
  8114. return nil, err
  8115. }
  8116. return &config, nil
  8117. }
  8118. func CreateSettleOpenConfig(config models.HisSettleStockConfig) error {
  8119. err := XTWriteDB().Create(&config).Error
  8120. return err
  8121. }
  8122. func UpdateSettleOpenConfig(id int64, config models.HisSettleStockConfig) error {
  8123. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_open": config.IsOpen}).Error
  8124. return err
  8125. }
  8126. func UpdateDrugStockAutomaticReduceRecordOne(orgid int64, is_open int64) (models.DrugStockConfig, error) {
  8127. config := models.DrugStockConfig{}
  8128. err := XTWriteDB().Model(&config).Where("org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  8129. return config, err
  8130. }
  8131. func UpdatePharyConfig(orgid int64, is_open int64) (models.PharmacyConfig, error) {
  8132. stockConfig := models.PharmacyConfig{}
  8133. err := XTWriteDB().Model(&stockConfig).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  8134. return stockConfig, err
  8135. }
  8136. func UpdateSettleOpenConfigOne(id int64, is_open int64) error {
  8137. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  8138. return err
  8139. }
  8140. func UpdateSettleOpenConfigTwo(id int64, is_open int64) error {
  8141. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  8142. return err
  8143. }
  8144. func GetSettleOpenConfigByOrgId(user_org_id int64) (models.HisSettleStockConfig, error) {
  8145. config := models.HisSettleStockConfig{}
  8146. err = XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8147. return config, nil
  8148. }
  8149. func GetGoodWareHouseOutInfoByGood(good_id int64, sys_record_time int64, user_org_id int64) (info []*models.WarehouseOutInfo, err error) {
  8150. err = XTReadDB().Where("good_id= ? and sys_record_time = ? and org_id = ? and status=1", good_id, sys_record_time, user_org_id).Find(&info).Error
  8151. return info, err
  8152. }
  8153. func GetGoodInformationByGoodIdOne(good_id int64) (models.GoodInformationFourty, error) {
  8154. info := models.GoodInformationFourty{}
  8155. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  8156. return info, err
  8157. }
  8158. func GetGoodWarehouseInfoByGoodList(good_id int64) (info []*models.WarehousingInfo, err error) {
  8159. err = XTReadDB().Where("good_id = ? and status =1 and stock_count>0 and is_check = 1", good_id).Find(&info).Error
  8160. return info, err
  8161. }
  8162. func GetAllWarehouseInfo(good_id int64, user_org_id int64) (info []*models.WarehousingInfo, err error) {
  8163. err = XTReadDB().Where("good_id = ? and user_org_id =? and status=1 and is_check =1", good_id, user_org_id).Find(&info).Error
  8164. return info, err
  8165. }
  8166. func GetHisPrescriptionByGoodId(good_id int64, user_org_id int64) (project []*models.HisPrescriptionProject, err error) {
  8167. err = XTReadDB().Where("project_id = ? and user_org_id =? and status=1 and type =3", good_id, user_org_id).Find(&project).Error
  8168. return project, err
  8169. }
  8170. func GetHisDocById(id int64, orgid int64) (models.HisDoctorAdvice, error) {
  8171. advice := models.HisDoctorAdvice{}
  8172. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  8173. return advice, err
  8174. }
  8175. func GetBloodDocById(id int64, orgid int64) (models.DoctorAdvice, error) {
  8176. advice := models.DoctorAdvice{}
  8177. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  8178. return advice, err
  8179. }
  8180. func FindAllSettleWarehouseOut(org_id int64, warehouse_out_time int64, is_sys int64) (models.WarehouseOut, error) {
  8181. out := models.WarehouseOut{}
  8182. 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
  8183. return out, err
  8184. }
  8185. func GetGoodOutOpenConfig(user_org_id int64) (*models.XtGoodOutConfig, error) {
  8186. config := models.XtGoodOutConfig{}
  8187. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8188. if err == gorm.ErrRecordNotFound {
  8189. return nil, err
  8190. }
  8191. if err != nil {
  8192. return nil, err
  8193. }
  8194. return &config, nil
  8195. }
  8196. func CreateGoodOutOpenConfig(good models.XtGoodOutConfig) error {
  8197. err := XTWriteDB().Create(&good).Error
  8198. return err
  8199. }
  8200. func UpdateGoodOutOpenConfig(id int64, good models.XtGoodOutConfig) error {
  8201. err := XTWriteDB().Model(&good).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"is_open": good.IsOpen}).Error
  8202. return err
  8203. }
  8204. func FindGoodOutConfigById(user_org_id int64) (models.XtGoodOutConfig, error) {
  8205. config := models.XtGoodOutConfig{}
  8206. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8207. return config, err
  8208. }
  8209. func UpdateGoodAutoConfig(user_org_id int64) error {
  8210. gobalConfig := models.GobalConfig{}
  8211. err := XTWriteDB().Model(&gobalConfig).Where("org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  8212. return err
  8213. }
  8214. func UpdateGoodOutConfig(user_org_id int64) error {
  8215. config := models.XtGoodOutConfig{}
  8216. err := XTWriteDB().Model(&config).Where("user_org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  8217. return err
  8218. }
  8219. func GetHisGoodList(patient_id int64, record_date int64, orgid int64) (info []*models.GoodHisPrescriptionProject, err error) {
  8220. 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
  8221. return info, err
  8222. }
  8223. func GetLastGoodWarehouseOutInfoByProjectId(good_id int64, patient_id int64, sys_record_time int64, project_id int64) (good []*models.WarehouseOutInfo, err error) {
  8224. 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
  8225. return good, err
  8226. }
  8227. func ModifyGoodWarehouseInfo(good_id int64, warehouse_info_id int64, orgid int64, count int64) error {
  8228. 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
  8229. return err
  8230. }
  8231. func DeleteGoodWarehouseOutInfo(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  8232. 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
  8233. return err
  8234. }
  8235. func DeleteGoodFlow(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  8236. 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
  8237. return err
  8238. }
  8239. func FindPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  8240. out := models.WarehouseOut{}
  8241. 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
  8242. return out, err
  8243. }
  8244. func FindNewPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  8245. out := models.WarehouseOut{}
  8246. 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
  8247. if err != gorm.ErrRecordNotFound {
  8248. if err != nil {
  8249. tx.Rollback()
  8250. return out, err
  8251. }
  8252. }
  8253. return out, err
  8254. }
  8255. func FindLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  8256. out := models.WarehouseOut{}
  8257. 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
  8258. return out, err
  8259. }
  8260. func FindNewLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  8261. out := models.WarehouseOut{}
  8262. 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
  8263. if err != gorm.ErrRecordNotFound {
  8264. if err != nil {
  8265. tx.Rollback()
  8266. return out, err
  8267. }
  8268. }
  8269. return out, err
  8270. }
  8271. func GetGoodTypeObj(goodid int64, org_id int64) (models.GoodsType, error) {
  8272. goodsType := models.GoodsType{}
  8273. err := XTReadDB().Where("good_id = ? and org_id = ? and status = 1", goodid, org_id).Find(&goodsType).Error
  8274. return goodsType, err
  8275. }
  8276. func GetAllGoodTypeName(good_type_id int64, org_id int64) (good_type_name string) {
  8277. goodsTypeList, _ := GetAllGoodType(org_id)
  8278. for _, item := range goodsTypeList {
  8279. if good_type_id == item.ID {
  8280. good_type_name = item.TypeName
  8281. }
  8282. }
  8283. return good_type_name
  8284. }
  8285. func UpdateGoodTypeName(good_id int64, good_type_name string) (models.GoodInformation, error) {
  8286. information := models.GoodInformation{}
  8287. err := XTWriteDB().Model(&information).Where("id = ? and status =1", good_id).Updates(map[string]interface{}{"good_type_name": good_type_name}).Error
  8288. return information, err
  8289. }
  8290. func GetSencondeGoodWarehouseInfo(good_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  8291. err = XTReadDB().Where("good_id = ? and org_id= ? and status= 1 and is_check = 1", good_id, org_id).Find(&info).Error
  8292. return info, err
  8293. }
  8294. func UpdateGoodSumCountSeven(sum_count int64, good_id int64, orgid int64) error {
  8295. ut := writeDb.Begin()
  8296. 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
  8297. if err != nil {
  8298. ut.Rollback()
  8299. return err
  8300. }
  8301. ut.Commit()
  8302. return err
  8303. }
  8304. func UpdateNewGoodSumCountSeven(sum_count int64, good_id int64, orgid int64, tx *gorm.DB) error {
  8305. 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
  8306. if err != nil {
  8307. tx.Rollback()
  8308. return err
  8309. }
  8310. return err
  8311. }
  8312. func GetWarehouseOutInfoIsExistOTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  8313. info := models.WarehouseOutInfo{}
  8314. var err error
  8315. 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
  8316. if err == gorm.ErrRecordNotFound {
  8317. return nil, err
  8318. }
  8319. if err != nil {
  8320. return nil, err
  8321. }
  8322. return &info, nil
  8323. }
  8324. func GetWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  8325. info := models.WarehouseOutInfo{}
  8326. 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
  8327. return info, err
  8328. }
  8329. func GetNewWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  8330. info := models.WarehouseOutInfo{}
  8331. 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
  8332. if err != gorm.ErrRecordNotFound {
  8333. if err != nil {
  8334. tx.Rollback()
  8335. return info, err
  8336. }
  8337. }
  8338. return info, err
  8339. }
  8340. func GetWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (models.WarehouseOutInfo, error) {
  8341. info := models.WarehouseOutInfo{}
  8342. 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
  8343. return info, err
  8344. }
  8345. func GetNewWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  8346. info := models.WarehouseOutInfo{}
  8347. 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
  8348. if err != gorm.ErrRecordNotFound {
  8349. if err != nil {
  8350. tx.Rollback()
  8351. return info, err
  8352. }
  8353. }
  8354. return info, err
  8355. }
  8356. func GetNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (*models.WarehouseOutInfo, error) {
  8357. info := models.WarehouseOutInfo{}
  8358. var err error
  8359. 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
  8360. if err == gorm.ErrRecordNotFound {
  8361. return nil, err
  8362. }
  8363. if err != nil {
  8364. return nil, err
  8365. }
  8366. return &info, nil
  8367. }
  8368. func GetNewNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (*models.WarehouseOutInfo, error) {
  8369. info := models.WarehouseOutInfo{}
  8370. var err error
  8371. 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
  8372. if err != gorm.ErrRecordNotFound {
  8373. if err != nil {
  8374. tx.Rollback()
  8375. return &info, err
  8376. }
  8377. }
  8378. return &info, err
  8379. }
  8380. func GetNewGoodWarehouseInfo(startime int64, endtime int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.GoodWarehousingInfo, err error) {
  8381. db := XTReadDB().Model(&info).Where("status = 1")
  8382. if startime > 0 {
  8383. db = db.Where("ctime>=?", startime)
  8384. }
  8385. if endtime > 0 {
  8386. db = db.Where("ctime<=?", endtime)
  8387. }
  8388. if orgid > 0 {
  8389. db = db.Where("org_id = ?", orgid)
  8390. }
  8391. if len(good_id) > 0 {
  8392. db = db.Where("good_id in(?)", good_id)
  8393. }
  8394. if storehouse_id > 0 {
  8395. db = db.Where("storehouse_id = ?", storehouse_id)
  8396. }
  8397. err = db.Preload("GoodInfo", "org_id = ? and status= 1", orgid).Find(&info).Error
  8398. return info, err
  8399. }
  8400. func GetNewWarehouseOutInfoList(start_time int64, end_time int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.WarehouseOutInfoNight, err error) {
  8401. db := XTReadDB().Model(&info).Where("status= 1")
  8402. if start_time > 0 {
  8403. db = db.Where("ctime>=?", start_time)
  8404. }
  8405. if end_time > 0 {
  8406. db = db.Where("ctime <=?", end_time)
  8407. }
  8408. if orgid > 0 {
  8409. db = db.Where("org_id = ?", orgid)
  8410. }
  8411. if len(good_id) > 0 {
  8412. db = db.Where("good_id in(?)", good_id)
  8413. }
  8414. if storehouse_id > 0 {
  8415. db = db.Where("storehouse_id = ?", storehouse_id)
  8416. }
  8417. err = db.Preload("GoodInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  8418. return info, err
  8419. }
  8420. func GetGoodOutOpenConfigOne(user_org_id int64) (models.XtGoodOutConfig, error) {
  8421. config := models.XtGoodOutConfig{}
  8422. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8423. return config, err
  8424. }
  8425. func UpdateDeleteAutoGood(good_id int64, record_date int64, patient_id int64) error {
  8426. 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
  8427. 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
  8428. return err
  8429. }
  8430. func UpdateWarehouseInfoMap(list []*models.WarehousingInfo, warehousing_info_id int64, orgId int64) error {
  8431. tx := XTWriteDB().Begin()
  8432. for _, item := range list {
  8433. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", item.WarehousingCount)).Error
  8434. if err != nil {
  8435. tx.Rollback()
  8436. return err
  8437. }
  8438. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).Update(map[string]interface{}{"is_check": 2}).Error
  8439. if err != nil {
  8440. tx.Rollback()
  8441. return err
  8442. }
  8443. err = tx.Model(models.VmStockFlow{}).Where("warehousing_detail_id = ?", item.ID).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  8444. if err != nil {
  8445. tx.Rollback()
  8446. return err
  8447. }
  8448. var goodList []*models.WarehousingInfo
  8449. 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
  8450. var flush_count int64
  8451. for _, it := range goodList {
  8452. flush_count += it.StockCount
  8453. }
  8454. 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
  8455. if err != nil {
  8456. tx.Rollback()
  8457. return err
  8458. }
  8459. 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
  8460. if err != nil {
  8461. tx.Rollback()
  8462. return err
  8463. }
  8464. storeHouseConfig := models.XtStorehouseConfig{}
  8465. err = tx.Where("user_org_id = ? and status = 1", orgId).First(&storeHouseConfig).Error
  8466. var stockList []*models.WarehousingInfo
  8467. //查询该耗材剩余多少库存
  8468. 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
  8469. var sum_count int64
  8470. var sum_in_count int64
  8471. for _, it := range stockList {
  8472. sum_count += it.StockCount
  8473. sum_in_count += it.WarehousingCount
  8474. }
  8475. 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
  8476. if err != nil {
  8477. tx.Rollback()
  8478. return err
  8479. }
  8480. 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
  8481. if err != nil {
  8482. tx.Rollback()
  8483. return err
  8484. }
  8485. }
  8486. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8487. if err != nil {
  8488. tx.Rollback()
  8489. return err
  8490. }
  8491. err = tx.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8492. tx.Commit()
  8493. return err
  8494. }
  8495. func UpdateNewWarehouseInfoMap(list []*models.WarehousingInfo, warehousing_info_id int64, orgId int64, tx *gorm.DB) error {
  8496. for _, item := range list {
  8497. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", item.WarehousingCount)).Error
  8498. if err != nil {
  8499. tx.Rollback()
  8500. return err
  8501. }
  8502. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).Update(map[string]interface{}{"is_check": 2}).Error
  8503. if err != nil {
  8504. tx.Rollback()
  8505. return err
  8506. }
  8507. err = tx.Model(models.VmStockFlow{}).Where("warehousing_detail_id = ?", item.ID).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  8508. if err != nil {
  8509. tx.Rollback()
  8510. return err
  8511. }
  8512. var goodList []*models.WarehousingInfo
  8513. 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
  8514. var flush_count int64
  8515. for _, it := range goodList {
  8516. flush_count += it.StockCount
  8517. }
  8518. 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
  8519. if err != nil {
  8520. tx.Rollback()
  8521. return err
  8522. }
  8523. 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
  8524. if err != nil {
  8525. tx.Rollback()
  8526. return err
  8527. }
  8528. storeHouseConfig := models.XtStorehouseConfig{}
  8529. err = tx.Where("user_org_id = ? and status = 1", orgId).First(&storeHouseConfig).Error
  8530. var stockList []*models.WarehousingInfo
  8531. //查询该耗材剩余多少库存
  8532. 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
  8533. var sum_count int64
  8534. var sum_in_count int64
  8535. for _, it := range stockList {
  8536. sum_count += it.StockCount
  8537. sum_in_count += it.WarehousingCount
  8538. }
  8539. 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
  8540. if err != nil {
  8541. tx.Rollback()
  8542. return err
  8543. }
  8544. 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
  8545. if err != nil {
  8546. tx.Rollback()
  8547. return err
  8548. }
  8549. err = tx.Model(models.XtGoodWarehouseInfoLog{}).Where("warehouse_info_id = ? and status=1", item.ID).Update(map[string]interface{}{"status": 0}).Error
  8550. if err != nil {
  8551. tx.Rollback()
  8552. return err
  8553. }
  8554. }
  8555. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8556. if err != nil {
  8557. tx.Rollback()
  8558. return err
  8559. }
  8560. err = tx.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8561. return err
  8562. }
  8563. func FindFirstWarehousingInfoByStockThree(good_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  8564. if storehouse_id == 0 {
  8565. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime").First(&info).Error
  8566. }
  8567. if storehouse_id > 0 {
  8568. 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
  8569. }
  8570. return info, err
  8571. }
  8572. func FindFirstWarehousingInfoByStockFour(good_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  8573. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and storehouse_id =? ", good_id, storehouse_id).Order("ctime").Last(&info).Error
  8574. return info, err
  8575. }
  8576. func GetHisPrescriptionNumberList(project_id int64, record_date int64, orgid int64, patient_id int64) (list []*models.HisPrescriptionProject, err error) {
  8577. 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
  8578. return list, err
  8579. }
  8580. func GetWarehouseOutnfoByProjectId(project_id int64, record_date int64, orgid int64, patient_id int64) (infor []*models.WarehouseOutInfo, err error) {
  8581. 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
  8582. return infor, err
  8583. }
  8584. func UpdateWarehouseInfoByPatientId(project_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfo, error) {
  8585. info := models.WarehouseOutInfo{}
  8586. 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
  8587. return info, err
  8588. }
  8589. func UpdateWarehouseInfoByProject(good_id int64, id int64, user_org_id int64, count int64) (models.WarehousingInfo, error) {
  8590. info := models.WarehousingInfo{}
  8591. 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
  8592. return info, err
  8593. }
  8594. func GetHisPrescripionProjectList(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  8595. err = XTReadDB().Where("project_id=? and status=1 and type =3 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  8596. return list, err
  8597. }
  8598. func GetHisPrescripionProjectListOne(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  8599. err = XTReadDB().Where("project_id=? and status=1 and type =2 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  8600. return list, err
  8601. }
  8602. func UpdateHisPrescriptionProjectList(id int64, price float64) (models.HisPrescriptionProjectTemplateL, error) {
  8603. templateL := models.HisPrescriptionProjectTemplateL{}
  8604. err := XTWriteDB().Model(&templateL).Where("id =? and status=1", id).Updates(map[string]interface{}{"price": price}).Error
  8605. return templateL, err
  8606. }
  8607. func ModifyNewGoodWarehouseInfoStockMinNumber(count int64, goodid int64, orgid int64, id int64, tx *gorm.DB) error {
  8608. 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
  8609. if err != nil {
  8610. tx.Rollback()
  8611. return err
  8612. }
  8613. return err
  8614. }
  8615. func GetDrugInvetoryByDrugId(user_org_id int64, drug_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  8616. 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
  8617. return info, err
  8618. }
  8619. func UpdateNewDrugWarehouseInfoById(id int64, last_stock_max_number int64, last_stock_min_number int64) error {
  8620. 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
  8621. return err
  8622. }
  8623. func GeteSelfPrescriptionOpen(user_org_id int64) (models.XtSelfDrugInfoConfig, error) {
  8624. drugInfoConfig := models.XtSelfDrugInfoConfig{}
  8625. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugInfoConfig).Error
  8626. return drugInfoConfig, err
  8627. }
  8628. func CreaterSelfConfig(infoConfig models.XtSelfDrugInfoConfig) error {
  8629. err := XTWriteDB().Create(&infoConfig).Error
  8630. return err
  8631. }
  8632. func SaveSelfConfig(infoConfig models.XtSelfDrugInfoConfig) error {
  8633. err := XTWriteDB().Save(&infoConfig).Error
  8634. return err
  8635. }
  8636. func GetSelfPrescriptionOutOpen(user_org_id int64) (models.XtSelfDrugOutConfig, error) {
  8637. drugOutConfig := models.XtSelfDrugOutConfig{}
  8638. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugOutConfig).Error
  8639. return drugOutConfig, err
  8640. }
  8641. func CreateSelfDrugOutConfig(config models.XtSelfDrugOutConfig) error {
  8642. err := XTWriteDB().Create(&config).Error
  8643. return err
  8644. }
  8645. func UpdateSelfDrugOutConfig(config models.XtSelfDrugOutConfig) error {
  8646. err := XTWriteDB().Save(&config).Error
  8647. return err
  8648. }
  8649. func CreateSelfDrugFlowOne(flow models.XtSelfDrugFlow, tx *gorm.DB) error {
  8650. err := tx.Create(&flow).Error
  8651. if err != nil {
  8652. tx.Rollback()
  8653. return err
  8654. }
  8655. return err
  8656. }
  8657. func UpdateWarehouseOutInfoByPrice(good_id int64, packing_price float64) error {
  8658. 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
  8659. return err
  8660. }
  8661. func GetWarehosueOutInfoByPrice(good_id int64, org_id int64) (out []*models.WarehouseOutInfo, err error) {
  8662. err = XTReadDB().Where("good_id = ? and org_id = ? and status=1 and price =0", good_id, org_id).Find(&out).Error
  8663. return out, err
  8664. }
  8665. func UpdateWarehouseOut(good_id int64, packing_price float64) error {
  8666. err := XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("id = ? and status=1", good_id).Updates(map[string]interface{}{"price": packing_price}).Error
  8667. return err
  8668. }
  8669. func UpdateStockFlowByList(good_id int64, packing_price float64) error {
  8670. 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
  8671. return err
  8672. }
  8673. func GetGoodStockInventoryByRecordate(good_id int64, record_date int64, user_org_id int64) (models.XtGoodStockInventoryList, error) {
  8674. goodStockInventoryList := models.XtGoodStockInventoryList{}
  8675. 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
  8676. return goodStockInventoryList, err
  8677. }
  8678. func CreateGoodStockInventoryList(list models.XtGoodStockInventoryList) error {
  8679. err := XTWriteDB().Create(&list).Error
  8680. return err
  8681. }
  8682. func SaveGoodStockInventoryList(list models.XtGoodStockInventoryList) error {
  8683. err := XTWriteDB().Save(&list).Error
  8684. return err
  8685. }
  8686. func GetGoodStockInventoryList(keyword string, limit int64, page int64, storehouse_id int64, startTime int64, endTime int64, orgId int64) (list []*models.XtGoodStockInventoryList, total int64, err error) {
  8687. likeKey := "%" + keyword + "%"
  8688. offset := (page - 1) * limit
  8689. db := XTReadDB().Model(&models.XtGoodStockInventoryList{}).Where("status=1")
  8690. if storehouse_id > 0 {
  8691. db = db.Where("storehouse_id = ?", storehouse_id)
  8692. }
  8693. if startTime > 0 {
  8694. db = db.Where("record_date >=?", startTime)
  8695. }
  8696. if endTime > 0 {
  8697. db = db.Where("record_date<=?", endTime)
  8698. }
  8699. if len(keyword) > 0 {
  8700. db = db.Where("good_name like ? or specification_name like ?", likeKey, likeKey)
  8701. }
  8702. if orgId > 0 {
  8703. db = db.Where("user_org_id = ?", orgId)
  8704. }
  8705. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  8706. return list, total, err
  8707. }
  8708. func GetDrugStockInventoryList(drug_id int64, record_date int64, user_org_id int64) (models.XtDrugStockInventoryList, error) {
  8709. drugStockInventoryList := models.XtDrugStockInventoryList{}
  8710. 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
  8711. return drugStockInventoryList, err
  8712. }
  8713. func CreatedDrugStockInventory(list models.XtDrugStockInventoryList) error {
  8714. err := XTWriteDB().Create(&list).Error
  8715. return err
  8716. }
  8717. func SaveDrugStockInventory(list models.XtDrugStockInventoryList) error {
  8718. err := XTWriteDB().Save(&list).Error
  8719. return err
  8720. }
  8721. func GetStockInventoryPrintList(storehouse_id int64, keyword string, limit int64, page int64, ids []string, orgId int64) (list []*models.XtGoodStockInventoryList, total int64, err error) {
  8722. db := XTReadDB().Model(&list).Where("status=1")
  8723. likeKey := "%" + keyword + "%"
  8724. offset := (page - 1) * limit
  8725. if storehouse_id > 0 {
  8726. db = db.Where("storehouse_id = ?", storehouse_id)
  8727. }
  8728. if len(keyword) > 0 {
  8729. db = db.Where("good_name like ?", likeKey)
  8730. }
  8731. if orgId > 0 {
  8732. db = db.Where("user_org_id = ?", orgId)
  8733. }
  8734. if len(ids) > 0 {
  8735. db = db.Where("id in(?)", ids)
  8736. }
  8737. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  8738. return list, total, err
  8739. }
  8740. func GetGoodWarehouseInfoList(startTime int64, endTime int64, orgId int64) (info []*models.WarehousingInfo, err error) {
  8741. db := XTReadDB().Model(&info).Where("status=1 and is_check=1")
  8742. if startTime > 0 {
  8743. db = db.Where("ctime>=?", startTime)
  8744. }
  8745. if endTime > 0 {
  8746. db = db.Where("ctime<=?", endTime)
  8747. }
  8748. if orgId > 0 {
  8749. db = db.Where("org_id = ?", orgId)
  8750. }
  8751. err = db.Find(&info).Error
  8752. return info, err
  8753. }
  8754. func GetGoodWarhouseOutList(startTime int64, endTime int64, orgId int64) (outinfo []*models.WarehouseOutInfo, err error) {
  8755. db := XTReadDB().Model(&outinfo).Where("status=1 and is_check =1")
  8756. if startTime > 0 {
  8757. db = db.Where("ctime>=?", startTime)
  8758. }
  8759. if endTime > 0 {
  8760. db = db.Where("ctime<=?", endTime)
  8761. }
  8762. if orgId > 0 {
  8763. db = db.Where("org_id = ?", orgId)
  8764. }
  8765. err = db.Find(&outinfo).Error
  8766. return outinfo, err
  8767. }
  8768. func GetGoodStockFlowProList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8769. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 10")
  8770. if startTime > 0 {
  8771. db = db.Where("ctime>=?", startTime)
  8772. }
  8773. if endTime > 0 {
  8774. db = db.Where("ctime<=?", endTime)
  8775. }
  8776. if orgId > 0 {
  8777. db = db.Where("user_org_id = ?", orgId)
  8778. }
  8779. err = db.Find(&flow).Error
  8780. return flow, err
  8781. }
  8782. func GetGoodStockFlowNoProList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8783. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 11")
  8784. if startTime > 0 {
  8785. db = db.Where("ctime>=?", startTime)
  8786. }
  8787. if endTime > 0 {
  8788. db = db.Where("ctime<=?", endTime)
  8789. }
  8790. if orgId > 0 {
  8791. db = db.Where("user_org_id = ?", orgId)
  8792. }
  8793. err = db.Find(&flow).Error
  8794. return flow, err
  8795. }
  8796. func GetCancelStockFlowList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8797. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 7")
  8798. if startTime > 0 {
  8799. db = db.Where("ctime>=?", startTime)
  8800. }
  8801. if endTime > 0 {
  8802. db = db.Where("ctime<=?", endTime)
  8803. }
  8804. if orgId > 0 {
  8805. db = db.Where("user_org_id = ?", orgId)
  8806. }
  8807. err = db.Find(&flow).Error
  8808. return flow, err
  8809. }
  8810. func GetLastMonthCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8811. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 1 or consumable_type = 10)")
  8812. if startTime > 0 {
  8813. db = db.Where("ctime <?", startTime)
  8814. }
  8815. if orgId > 0 {
  8816. db = db.Where("user_org_id = ?", orgId)
  8817. }
  8818. err = db.Find(&flow).Error
  8819. return flow, err
  8820. }
  8821. func GetLastMonthOverCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8822. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 2 or consumable_type=3 or consumable_type=11)")
  8823. if startTime > 0 {
  8824. db = db.Where("ctime <?", startTime)
  8825. }
  8826. if orgId > 0 {
  8827. db = db.Where("user_org_id = ?", orgId)
  8828. }
  8829. err = db.Find(&flow).Error
  8830. return flow, err
  8831. }
  8832. func GetLastCancelMonthCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8833. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 7)")
  8834. if startTime > 0 {
  8835. db = db.Where("ctime <?", startTime)
  8836. }
  8837. if orgId > 0 {
  8838. db = db.Where("user_org_id = ?", orgId)
  8839. }
  8840. err = db.Find(&flow).Error
  8841. return flow, err
  8842. }
  8843. func GetDrugCodeOpen(user_org_id int64) (models.XtDrugCodeOpen, error) {
  8844. drugCodeOpen := models.XtDrugCodeOpen{}
  8845. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugCodeOpen).Error
  8846. return drugCodeOpen, err
  8847. }
  8848. func GetGoodCodeOpen(user_org_id int64) (models.XtGoodCodeOpen, error) {
  8849. drugCodeOpen := models.XtGoodCodeOpen{}
  8850. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugCodeOpen).Error
  8851. return drugCodeOpen, err
  8852. }
  8853. func CreateDrugOpen(open models.XtDrugCodeOpen) error {
  8854. err := XTWriteDB().Create(&open).Error
  8855. return err
  8856. }
  8857. func SaveDrugOpen(open models.XtDrugCodeOpen) error {
  8858. err := XTWriteDB().Save(&open).Error
  8859. return err
  8860. }
  8861. func GetHisDrugCodeQuery(orgId int64, start_time int64, end_time int64, limit int64, page int64, is_sale int64, is_settle int64, keywords string, is_code int64) (list []*models.NewHisDoctorAdviceInfo, total int64, err error) {
  8862. db := XTReadDB().Model(&models.NewHisDoctorAdviceInfo{}).Preload("NewHisPrescription", "status = 1").Where("his_doctor_advice_info.status=1")
  8863. if is_settle == 2 {
  8864. db = db.Joins("join his_prescription on his_prescription.id = his_doctor_advice_info.prescription_id and his_prescription.order_status = 2 and his_prescription.status = 1")
  8865. } else if is_settle == 1 {
  8866. db = db.Joins("join his_prescription on his_prescription.id = his_doctor_advice_info.prescription_id and his_prescription.order_status <> 2 and his_prescription.status = 1")
  8867. }
  8868. if is_code == 2 {
  8869. //db = db.Joins("join his_prescription on his_prescription.id = his_doctor_advice_info.prescription_id and his_prescription.order_status = 2 and his_prescription.status = 1")
  8870. db = db.Where("his_doctor_advice_info.drug_code <> ''")
  8871. } else if is_code == 1 {
  8872. db = db.Where("length(his_doctor_advice_info.drug_code) = 0")
  8873. }
  8874. if len(keywords) > 0 {
  8875. keywords = "%" + keywords + "%"
  8876. db = db.Joins("join xt_patients on xt_patients.id = his_doctor_advice_info.patient_id and xt_patients.user_org_id = ? and xt_patients.name like ?", orgId, keywords)
  8877. }
  8878. if orgId > 0 {
  8879. db = db.Where("his_doctor_advice_info.user_org_id=?", orgId)
  8880. }
  8881. if start_time > 0 {
  8882. db = db.Where("his_doctor_advice_info.advice_date >= ?", start_time)
  8883. }
  8884. if end_time > 0 {
  8885. db = db.Where("his_doctor_advice_info.advice_date<=?", end_time)
  8886. }
  8887. offset := (page - 1) * limit
  8888. if is_sale == 1 {
  8889. db = db.Where("his_doctor_advice_info.is_upload = 1")
  8890. }
  8891. if is_sale == 2 {
  8892. db = db.Where("his_doctor_advice_info.is_upload = 2 or his_doctor_advice_info.is_upload =0")
  8893. }
  8894. err = db.Count(&total).Offset(offset).Limit(limit).Order("his_doctor_advice_info.record_date desc").Find(&list).Error
  8895. return list, total, err
  8896. }
  8897. func GetHisGoodCodeQuery(orgId int64, start_time int64, end_time int64, limit int64, page int64, is_sale int64, is_settle int64, keywords string, is_code int64) (list []*models.NewHisPrintPrescriptionProject, total int64, err error) {
  8898. db := XTReadDB().Model(&models.NewHisPrintPrescriptionProject{}).Preload("NewHisPrescription", "status = 1").Preload("GoodInfo", "status = 1").Where("his_prescription_project.status=1")
  8899. if is_settle == 2 {
  8900. db = db.Joins("join his_prescription on his_prescription.id = his_prescription_project.prescription_id and his_prescription.order_status = 2 and his_prescription.status = 1")
  8901. } else if is_settle == 1 {
  8902. db = db.Joins("join his_prescription on his_prescription.id = his_prescription_project.prescription_id and his_prescription.order_status <> 2 and his_prescription.status = 1")
  8903. }
  8904. if is_code == 2 {
  8905. //db = db.Joins("join his_prescription on his_prescription.id = his_doctor_advice_info.prescription_id and his_prescription.order_status = 2 and his_prescription.status = 1")
  8906. db = db.Where("his_prescription_project.drug_code <> ''")
  8907. } else if is_code == 1 {
  8908. db = db.Where("length(his_prescription_project.drug_code) = 0")
  8909. }
  8910. if len(keywords) > 0 {
  8911. keywords = "%" + keywords + "%"
  8912. db = db.Joins("join xt_patients on xt_patients.id = his_doctor_advice_info.patient_id and xt_patients.user_org_id = ? and xt_patients.name like ?", orgId, keywords)
  8913. }
  8914. if orgId > 0 {
  8915. db = db.Where("his_prescription_project.user_org_id=?", orgId)
  8916. }
  8917. if start_time > 0 {
  8918. db = db.Where("his_prescription_project.record_date >= ?", start_time)
  8919. }
  8920. if end_time > 0 {
  8921. db = db.Where("his_prescription_project.record_date<=?", end_time)
  8922. }
  8923. offset := (page - 1) * limit
  8924. if is_sale == 1 {
  8925. db = db.Where("his_prescription_project.is_upload = 1")
  8926. }
  8927. if is_sale == 2 {
  8928. db = db.Where("his_prescription_project.is_upload = 2 or his_prescription_project.is_upload =0")
  8929. }
  8930. db = db.Where("his_prescription_project.type = 3")
  8931. err = db.Count(&total).Offset(offset).Limit(limit).Order("his_prescription_project.record_date desc").Find(&list).Error
  8932. return list, total, err
  8933. }
  8934. func GetBloodDrugCode(orgId int64, start_time int64, end_time int64, limit int64, page int64, is_sale int64) (advice []*models.DoctorAdvice, total int64, err error) {
  8935. db := XTReadDB().Model(&advice).Where("status=1 and advice_type !=1")
  8936. if orgId > 0 {
  8937. db = db.Where("user_org_id = ?", orgId)
  8938. }
  8939. if start_time > 0 {
  8940. db = db.Where("advice_date>=?", start_time)
  8941. }
  8942. if end_time > 0 {
  8943. db = db.Where("advice_date<=?", end_time)
  8944. }
  8945. if is_sale > 0 {
  8946. db = db.Where("is_upload = ?", is_sale)
  8947. }
  8948. offset := (page - 1) * limit
  8949. err = db.Count(&total).Offset(offset).Limit(limit).Order("advice_date desc").Find(&advice).Error
  8950. return advice, total, err
  8951. }
  8952. func GetDoctorAdviceInfoList(page int64, limit int64, orgId int64, start_time int64, end_time int64, is_sale int64) ([]models.HisDoctorAdviceInfo, int64, error) {
  8953. var list []models.HisDoctorAdviceInfo
  8954. var total int64
  8955. // 计算偏移量
  8956. offset := (page - 1) * limit
  8957. // 初始化数据库查询
  8958. db := XTReadDB().Model(&models.HisDoctorAdviceInfo{})
  8959. // 构建 WHERE 子句
  8960. query := db.Where("status=1")
  8961. if orgId > 0 {
  8962. query = query.Where("user_org_id=?", orgId)
  8963. }
  8964. if start_time > 0 {
  8965. query = query.Where("advice_date >= ?", start_time)
  8966. }
  8967. if end_time > 0 {
  8968. query = query.Where("advice_date <= ?", end_time)
  8969. }
  8970. if is_sale == 1 {
  8971. query = query.Where("is_upload = 1")
  8972. }
  8973. if is_sale == 2 {
  8974. query = query.Where("is_upload IN (?, ?)", 2, 0) // 使用 IN 替代 OR,通常性能更好
  8975. }
  8976. // 获取总记录数
  8977. err := query.Count(&total).Error
  8978. if err != nil {
  8979. return nil, 0, err
  8980. }
  8981. // 获取分页后的数据
  8982. err = query.Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  8983. if err != nil {
  8984. return nil, 0, err
  8985. }
  8986. return list, total, nil
  8987. }
  8988. func GetDrugWarehouseOutInfoByDrugCode(drug_id int64, patient_id int64, advice_date int64, user_org_id int64) (outinfo []*models.XtDrugWarehouseOutInfo, err error) {
  8989. 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
  8990. return outinfo, err
  8991. }
  8992. func GetNewGoodWarehouseOutInfo(good_id int64, user_org_id int64, warehouse_out_id int64, tx *gorm.DB) (info []*models.WarehouseOutInfo, err error) {
  8993. 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
  8994. if err != gorm.ErrRecordNotFound {
  8995. if err != nil {
  8996. tx.Rollback()
  8997. return info, err
  8998. }
  8999. }
  9000. return info, err
  9001. }
  9002. func GetInitGoodFlow(user_org_id int64, good_id int64, warehousing_detail_id int64, tx *gorm.DB) (list []*models.VmStockFlow, err error) {
  9003. 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
  9004. if err != gorm.ErrRecordNotFound {
  9005. if err != nil {
  9006. tx.Rollback()
  9007. return list, err
  9008. }
  9009. }
  9010. return list, err
  9011. }
  9012. func GetNewDrugFlowList(drug_id int64, user_org_id int64, warehousing_detail_id int64, tx *gorm.DB) (flow []*models.DrugFlow, err error) {
  9013. 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
  9014. if err != gorm.ErrRecordNotFound {
  9015. if err != nil {
  9016. tx.Rollback()
  9017. return flow, err
  9018. }
  9019. }
  9020. return flow, err
  9021. }
  9022. func GetNewPatientAdviceList(patient_id int64, start_time int64, end_time int64, user_org_id int64) (advice []*models.DoctorAdvice, err error) {
  9023. db := XTReadDB().Model(&advice).Where("status=1 and advice_type =2")
  9024. if patient_id > 0 {
  9025. db = db.Where("patient_id = ?", patient_id)
  9026. }
  9027. if start_time > 0 {
  9028. db = db.Where("advice_date>=?", start_time)
  9029. }
  9030. if end_time > 0 {
  9031. db = db.Where("advice_date<=?", end_time)
  9032. }
  9033. if user_org_id > 0 {
  9034. db = db.Where("user_org_id = ?", user_org_id)
  9035. }
  9036. err = db.Order("drug_id desc").Find(&advice).Error
  9037. return advice, err
  9038. }
  9039. func GetSelfDrugWarehouseInfoByPatientId(patient_id int64, user_org_id int64) (drugInfo []*models.XtSelfDrugWarehouseInfo, err error) {
  9040. err = XTReadDB().Where("patient_id = ? and org_id = ? and status =1 and is_check =1", patient_id, user_org_id).Find(&drugInfo).Error
  9041. return drugInfo, err
  9042. }
  9043. func GetPatientAdviceListDetail(patient_id int64, start_time int64, end_time int64, user_org_id int64, advice_name string) (advice []*models.DoctorAdvice, err error) {
  9044. db := XTReadDB().Model(&advice).Where("status = 1")
  9045. if patient_id > 0 {
  9046. db = db.Where("patient_id = ?", patient_id)
  9047. }
  9048. if start_time > 0 {
  9049. db = db.Where("advice_date>=?", start_time)
  9050. }
  9051. if end_time > 0 {
  9052. db = db.Where("advice_date<=?", end_time)
  9053. }
  9054. if user_org_id > 0 {
  9055. db = db.Where("user_org_id = ?", user_org_id)
  9056. }
  9057. if len(advice_name) > 0 {
  9058. db = db.Where("advice_name =?", advice_name)
  9059. }
  9060. err = db.Order("advice_date asc").Find(&advice).Error
  9061. return advice, err
  9062. }
  9063. func GetGoodWarehosueInfoPrintList(good_id int64, start_time int64, end_time int64, user_org_id int64) (info []*models.PrintWarehousingInfo, err error) {
  9064. db := XTReadDB().Model(&info).Where("status=1")
  9065. if good_id > 0 {
  9066. db = db.Where("good_id = ?", good_id)
  9067. }
  9068. if start_time > 0 {
  9069. db = db.Where("ctime>=?", start_time)
  9070. }
  9071. if end_time > 0 {
  9072. db = db.Where("ctime<=?", end_time)
  9073. }
  9074. if user_org_id > 0 {
  9075. db = db.Where("org_id = ?", user_org_id)
  9076. }
  9077. err = db.Find(&info).Error
  9078. return info, err
  9079. }
  9080. func GetDrugWarehouseInfoPrintList(drug_id int64, user_org_id int64, start_time int64, end_time int64) (drugInfo []*models.PrintDrugWarehouseInfo, err error) {
  9081. db := XTReadDB().Model(&drug_id).Where("status=1")
  9082. if drug_id > 0 {
  9083. db = db.Where("drug_id = ?", drug_id)
  9084. }
  9085. if start_time > 0 {
  9086. db = db.Where("ctime>=?", start_time)
  9087. }
  9088. if end_time > 0 {
  9089. db = db.Where("ctime<=?", end_time)
  9090. }
  9091. if user_org_id > 0 {
  9092. db = db.Where("org_id = ?", user_org_id)
  9093. }
  9094. err = db.Find(&drugInfo).Error
  9095. return drugInfo, err
  9096. }