stock_service.go 457KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "XT_New/models"
  9. "github.com/jinzhu/gorm"
  10. )
  11. func AddSigleDealer(dealer *models.Dealer) (error, *models.Dealer) {
  12. err := writeDb.Create(&dealer).Error
  13. return err, dealer
  14. }
  15. func ModifyDealer(dealer *models.Dealer) error {
  16. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", dealer.ID).Updates(map[string]interface{}{
  17. "dealer_name": dealer.DealerName,
  18. "contact": dealer.Contact,
  19. "contact_phone": dealer.ContactPhone,
  20. "platform_number": dealer.PlatformNumber,
  21. "email": dealer.Email,
  22. "contact_address": dealer.ContactAddress,
  23. "modifier": dealer.Modifier,
  24. "mtime": time.Now().Unix(),
  25. "remark": dealer.Remark,
  26. "wubi": dealer.WuBi,
  27. "pinyin": dealer.PinYin,
  28. }).Error
  29. return err
  30. }
  31. func FindAllDealerTotal(orgId int64) (total int64) {
  32. db := readDb.Model(&models.Dealer{})
  33. db = db.Where("org_id = ? ", orgId).Count(&total)
  34. return
  35. }
  36. // 获取经销商的名称
  37. func FindAllDealerName(orgId int64) (tmp []*models.Dealer, err error) {
  38. err = readDb.Model(&models.Dealer{}).Where("org_id = ? and status = 1", orgId).Find(&tmp).Error
  39. return
  40. }
  41. func FindAllDealerList(orgId int64, page int64, limit, key int64) (list []*models.Dealer, total int64, err error) {
  42. offset := (page - 1) * limit
  43. db := readDb.Model(&models.Dealer{})
  44. db = db.Where("org_id = ? AND status = 1", orgId)
  45. if key != 0 {
  46. db = db.Where("id = ? ", key)
  47. offset = 0
  48. limit = 10
  49. }
  50. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  51. return
  52. }
  53. func DeleteDealerById(id int64) error {
  54. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  55. return err
  56. }
  57. func FindDealerById(id int64) (*models.Dealer, error) {
  58. dealer := &models.Dealer{}
  59. err := readDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  60. return dealer, err
  61. }
  62. func AddSigleManufacturer(manufacturer *models.Manufacturer) (error, *models.Manufacturer) {
  63. err := writeDb.Create(&manufacturer).Error
  64. return err, manufacturer
  65. }
  66. func ModifyManufacturer(manufacturer *models.Manufacturer) error {
  67. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", manufacturer.ID).Updates(map[string]interface{}{
  68. "manufacturer_name": manufacturer.ManufacturerName,
  69. "contact": manufacturer.Contact,
  70. "contact_phone": manufacturer.ContactPhone,
  71. "platform_number": manufacturer.PlatformNumber,
  72. "email": manufacturer.Email,
  73. "contact_address": manufacturer.ContactAddress,
  74. "modifier": manufacturer.Modifier,
  75. "mtime": time.Now().Unix(),
  76. "remark": manufacturer.Remark,
  77. "wubi": manufacturer.WuBi,
  78. "pinyin": manufacturer.PinYin,
  79. }).Error
  80. return err
  81. }
  82. // 获取生产厂商名称
  83. func FindAllManufacturerName(orgid int64) (tmp []*models.Manufacturer, err error) {
  84. err = readDb.Model(&models.Manufacturer{}).Where("org_id = ? and status = 1", orgid).Find(&tmp).Error
  85. return
  86. }
  87. func FindAllManufacturerList(orgId int64, page int64, limit, key int64) (list []*models.Manufacturer, total int64, err error) {
  88. offset := (page - 1) * limit
  89. db := readDb.Model(&models.Manufacturer{})
  90. db = db.Where("org_id = ? AND status = 1", orgId)
  91. if key != 0 {
  92. db = db.Where("id = ?", key)
  93. offset = 0
  94. limit = 10
  95. }
  96. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  97. return
  98. }
  99. func DeleteManufacturerById(id int64) error {
  100. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  101. return err
  102. }
  103. func FindManufacturerById(id int64) (*models.Manufacturer, error) {
  104. dealer := &models.Manufacturer{}
  105. err := readDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  106. return dealer, err
  107. }
  108. func FindManufacturerTotal(orgId int64) (total int64) {
  109. db := readDb.Model(&models.Manufacturer{})
  110. db = db.Where("org_id = ?", orgId).Count(&total)
  111. return
  112. }
  113. func FindAllDealer(orgId int64) (dealers []*models.Dealer, err error) {
  114. err = readDb.Model(&models.Dealer{}).Where("org_id = ? AND status = 1", orgId).Find(&dealers).Error
  115. return dealers, err
  116. }
  117. func FindAllManufacturer(orgId int64) (manufacturer []*models.Manufacturer, err error) {
  118. err = readDb.Model(&models.Manufacturer{}).Where("org_id = ? AND status = 1", orgId).Find(&manufacturer).Error
  119. return manufacturer, err
  120. }
  121. func FindAllGoodTypeTotal(orgId int64) (total int64) {
  122. db := readDb.Model(&models.GoodsType{})
  123. db = db.Where("org_id = ?", orgId).Count(&total)
  124. return
  125. }
  126. func GetGoodsTypeIsExist(orgid int64) (*models.GoodsType, error) {
  127. goodsType := models.GoodsType{}
  128. err := XTReadDB().Where("org_id = ? and stock_attribute = 2 and status =1", orgid).Find(&goodsType).Error
  129. if err == gorm.ErrRecordNotFound {
  130. return nil, err
  131. }
  132. if err != nil {
  133. return nil, err
  134. }
  135. return &goodsType, nil
  136. }
  137. func GetGoodsTypeIsExistOne(orgid int64) (*models.GoodsType, error) {
  138. goodsType := models.GoodsType{}
  139. err := XTReadDB().Where("org_id = ? and stock_attribute = 3 and status = 1", orgid).Find(&goodsType).Error
  140. if err == gorm.ErrRecordNotFound {
  141. return nil, err
  142. }
  143. if err != nil {
  144. return nil, err
  145. }
  146. return &goodsType, nil
  147. }
  148. func AddSigleGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  149. err := writeDb.Create(&goodType).Error
  150. return err, goodType
  151. }
  152. func ModifyGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  153. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", goodType.ID).Updates(map[string]interface{}{
  154. "modifier": goodType.Modifier,
  155. "mtime": time.Now().Unix(),
  156. "remark": goodType.Remark,
  157. "type_name": goodType.TypeName,
  158. "out_stock": goodType.OutStock,
  159. "stock_attribute": goodType.StockAttribute,
  160. }).Error
  161. return err, goodType
  162. }
  163. func FindAllGoodTypeList(orgId int64, page int64, limit int64, keyword string) (list []*models.GoodsType, total int64, err error) {
  164. offset := (page - 1) * limit
  165. db := readDb.Model(&models.GoodsType{})
  166. db = db.Where("org_id=? AND status = 1", orgId)
  167. if len(keyword) > 0 {
  168. likeKey := "%" + keyword + "%"
  169. db = db.Where("type_code LIKE ? OR type_name LIKE ?", likeKey, likeKey)
  170. }
  171. err = db.Count(&total).Offset(offset).Limit(limit).Order("type desc, number desc,ctime desc").Find(&list).Error
  172. return
  173. }
  174. func DeleteGoodTypeById(id int64, operateId int64) error {
  175. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  176. writeDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId})
  177. return err
  178. }
  179. func FindGoodTypeById(id int64) (*models.GoodsType, error) {
  180. goodType := &models.GoodsType{}
  181. err := readDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).First(&goodType).Error
  182. return goodType, err
  183. }
  184. func FindAllGoodInfoTotal(orgId int64) (total int64) {
  185. db := readDb.Model(&models.GoodInfo{})
  186. db = db.Where("org_id = ?", orgId).Count(&total)
  187. return
  188. }
  189. func AddSigleGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  190. err := writeDb.Create(&goodInfo).Error
  191. return err, goodInfo
  192. }
  193. func ModifyGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  194. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", goodInfo.ID).Updates(map[string]interface{}{
  195. "good_type_id": goodInfo.GoodTypeId,
  196. "modifier": goodInfo.Modifier,
  197. "mtime": time.Now().Unix(),
  198. "remark": goodInfo.Remark,
  199. "specification_name": goodInfo.SpecificationName,
  200. "good_unit": goodInfo.GoodUnit,
  201. "buy_price": goodInfo.BuyPrice,
  202. "sell_price": goodInfo.SellPrice,
  203. "manufacturer": goodInfo.Manufacturer,
  204. "dealer": goodInfo.Dealer,
  205. "expiry_date_warn_day_count": goodInfo.ExpiryDateWarnDayCount,
  206. "stock_warn_count": goodInfo.StockWarnCount,
  207. "is_reuse": goodInfo.IsReuse,
  208. "good_name": goodInfo.GoodName,
  209. "pinyin": goodInfo.Pinyin,
  210. "wubi": goodInfo.Wubi,
  211. "good_kind": goodInfo.GoodKind,
  212. "medical_insurance_level": goodInfo.MedicalInsuranceLevel,
  213. "retail_price": goodInfo.PackingPrice,
  214. "medical_insurance_number": goodInfo.MedicalInsuranceNumber,
  215. "is_special_diseases": goodInfo.IsSpecialDiseases,
  216. "is_record": goodInfo.IsRecord,
  217. "statistics_category": goodInfo.StatisticsCategory,
  218. "good_status": goodInfo.GoodStatus,
  219. "default_count": goodInfo.DefaultCount,
  220. "sign": goodInfo.Sign,
  221. "is_default": goodInfo.IsDefault,
  222. "is_charge_use": goodInfo.IsChargeUse,
  223. "is_charge_predict": goodInfo.IsChargePredict,
  224. "is_statistics_work": goodInfo.IsStatisticsWork,
  225. "sort": goodInfo.Sort,
  226. "is_doctor_use": goodInfo.IsDoctorUse,
  227. "agent": goodInfo.Agent,
  228. "good_number": goodInfo.GoodNumber,
  229. "social_security_directory_code": goodInfo.SocialSecurityDirectoryCode,
  230. "special_medical": goodInfo.SpecialMedical,
  231. "production_type": goodInfo.ProductionType,
  232. "min_number": goodInfo.MinNumber,
  233. "packing_unit": goodInfo.PackingUnit,
  234. "packing_price": goodInfo.PackingPrice,
  235. "default_count_unit": goodInfo.DefaultCountUnit,
  236. "min_unit": goodInfo.MinUnit,
  237. "register_number": goodInfo.RegisterNumber,
  238. "provinces_code": goodInfo.ProvincesCode,
  239. "is_user": goodInfo.IsUser,
  240. "number": goodInfo.Number,
  241. "is_warehouse": goodInfo.IsWarehouse,
  242. "bbx01": goodInfo.Bbx01,
  243. "bby01": goodInfo.Bby01,
  244. "zuobiao_id": goodInfo.Bbx01,
  245. "first_letter": goodInfo.FirstLetter,
  246. "is_show": goodInfo.IsShow,
  247. "is_zero_flag": goodInfo.IsZeroFlag,
  248. }).Error
  249. writeDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  250. writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  251. writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  252. writeDb.Model(&models.DialysisBeforePrepare{}).Where("good_id = ? AND user_org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  253. writeDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  254. writeDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  255. return err, goodInfo
  256. }
  257. func FindGoodInfoList(orgId int64, page int64, limit int64, keyword string, is_use int64, is_charge int64, good_kind int64, is_mark int64, manufacturer int64, is_type int64) (list []*models.GoodInfo, total int64, err error) {
  258. offset := (page - 1) * limit
  259. db := readDb.Model(&models.GoodInfo{})
  260. db = db.Where("org_id = ? AND status = 1", orgId)
  261. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  262. if len(keyword) > 0 {
  263. likeKey := "%" + keyword + "%"
  264. db = db.Where("good_code LIKE ? OR good_name LIKE ?", likeKey, likeKey)
  265. if is_use > 0 {
  266. if is_use == 2 {
  267. db = db.Where("find_in_set('停用',good_status) = 0")
  268. } else {
  269. db = db.Where("find_in_set('停用',good_status) > 0")
  270. }
  271. }
  272. if is_charge > 0 {
  273. if is_charge == 1 {
  274. db = db.Where("find_in_set('收费',good_status) > 0")
  275. } else {
  276. db = db.Where("find_in_set('收费',good_status) = 0")
  277. }
  278. }
  279. if good_kind > 0 {
  280. db = db.Where(" good_kind = ?", good_kind)
  281. }
  282. if is_mark > 0 {
  283. db = db.Where("is_mark = ?", is_mark)
  284. }
  285. if manufacturer > 0 {
  286. db = db.Where("manufacturer = ?", manufacturer)
  287. }
  288. if is_type > 0 {
  289. db = db.Where("good_type_id = ?", is_type)
  290. }
  291. } else {
  292. if is_use > 0 {
  293. if is_use == 2 {
  294. db = db.Where("find_in_set('停用',good_status) = 0")
  295. } else {
  296. db = db.Where("find_in_set('停用',good_status) > 0")
  297. }
  298. }
  299. if is_charge > 0 {
  300. if is_charge == 1 {
  301. db = db.Where("find_in_set('收费',good_status) > 0")
  302. } else {
  303. db = db.Where("find_in_set('收费',good_status) = 0")
  304. }
  305. }
  306. if good_kind > 0 {
  307. db = db.Where(" good_kind = ?", good_kind)
  308. }
  309. if is_mark > 0 {
  310. db = db.Where("is_mark = ?", is_mark)
  311. }
  312. if manufacturer > 0 {
  313. db = db.Where("manufacturer = ?", manufacturer)
  314. }
  315. if is_type > 0 {
  316. db = db.Where("good_type_id = ?", is_type)
  317. }
  318. }
  319. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  320. return
  321. }
  322. func DeleteGoodInfoById(id int64, operateId int64) error {
  323. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  324. return err
  325. }
  326. func FindGoodInfoByGoodId(id int64) (list []*models.GoodInfo, err error) {
  327. err = readDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  328. return list, err
  329. }
  330. func FindStockOutDetailGoodInfoByGoodId(id int64) (list []*models.AutomaticReduceDetail, err error) {
  331. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  332. return list, err
  333. }
  334. func FindGoodInfoById(id int64) (*models.GoodInfo, error) {
  335. goodInfo := &models.GoodInfo{}
  336. err := readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  337. return goodInfo, err
  338. }
  339. func FindAllGoodType(org_id int64) (goodType []*models.GoodsType, err error) {
  340. err = XTReadDB().Model(&models.GoodsType{}).Where("org_id = ? AND status = 1", org_id).Find(&goodType).Error
  341. return goodType, err
  342. }
  343. func FindAllGoodTypeOne(org_id int64) (goodType []*models.GoodsType, err error) {
  344. err = readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 and out_stock = 1", org_id).Find(&goodType).Error
  345. return goodType, err
  346. }
  347. func FindAllGoodInfo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  348. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1 and is_warehouse = 1 and find_in_set('停用',good_status) = 0", org_id).Find(&goodInfo).Error
  349. return goodInfo, err
  350. }
  351. func FindAllGoodInfoTwo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  352. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id).Find(&goodInfo).Error
  353. return goodInfo, err
  354. }
  355. func FindAllGoodInfoThree(org_id int64) (goodInfo []*models.XtGoodInformationFourty, err error) {
  356. err = XTReadDB().Model(&models.XtGoodInformationFourty{}).Where("org_id = ? AND status = 1 and find_in_set('停用',good_status) = 0", org_id).Find(&goodInfo).Error
  357. return goodInfo, err
  358. }
  359. func FindAllWarehouseTotal(org_id int64) (total int64, err error) {
  360. err = readDb.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  361. return total, err
  362. }
  363. func FindNewAllWarehouseTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  364. err = tx.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  365. if err != gorm.ErrRecordNotFound {
  366. if err != nil {
  367. tx.Rollback()
  368. return total, err
  369. }
  370. }
  371. return total, err
  372. }
  373. func FindAllWarehouseTotalOne(org_id int64) (total int64, err error) {
  374. err = readDb.Model(&models.XtDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  375. return total, err
  376. }
  377. func FindNewAllWarehouseTotalOne(org_id int64, tx *gorm.DB) (total int64, err error) {
  378. err = tx.Model(&models.XtDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  379. if err != gorm.ErrRecordNotFound {
  380. if err != nil {
  381. tx.Rollback()
  382. return total, err
  383. }
  384. }
  385. return total, err
  386. }
  387. func AddSigleWarehouse(warehouse *models.Warehousing) error {
  388. err := writeDb.Create(&warehouse).Error
  389. return err
  390. }
  391. func AddNewSigleWarehouse(warehouse *models.Warehousing, tx *gorm.DB) error {
  392. err := writeDb.Create(&warehouse).Error
  393. if err != nil {
  394. tx.Rollback()
  395. return err
  396. }
  397. return err
  398. }
  399. func UpdateWarehouse(id int64, storehouse_id int64) error {
  400. err := XTWriteDB().Model(&models.Warehousing{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  401. return err
  402. }
  403. func UpdateNewWarehouse(id int64, storehouse_id int64, tx *gorm.DB) error {
  404. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  405. if err != nil {
  406. tx.Rollback()
  407. return err
  408. }
  409. return err
  410. }
  411. func AddSigleDrugWarehouse(warehouse *models.DrugWarehouse) error {
  412. err := writeDb.Create(&warehouse).Error
  413. return err
  414. }
  415. func AddNewSigleDrugWarehouse(warehouse *models.DrugWarehouse, tx *gorm.DB) error {
  416. err := tx.Create(&warehouse).Error
  417. if err != nil {
  418. tx.Rollback()
  419. return err
  420. }
  421. return err
  422. }
  423. func UpdateSigleDrugWarehouse(id int64, storehosue_id int64) error {
  424. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehosue_id}).Error
  425. return err
  426. }
  427. func UpdateNewSigleDrugWarehouse(id int64, storehosue_id int64, tx *gorm.DB) error {
  428. err := tx.Model(&models.DrugWarehouse{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehosue_id}).Error
  429. if err != nil {
  430. tx.Rollback()
  431. return err
  432. }
  433. return err
  434. }
  435. func AddSigleSalesReturn(salesReturn *models.SalesReturn) error {
  436. err := writeDb.Create(&salesReturn).Error
  437. return err
  438. }
  439. func AddSigleDrugSalesReturn(salesReturn *models.DrugSalesReturn) error {
  440. err := writeDb.Create(&salesReturn).Error
  441. return err
  442. }
  443. func UpDateGoodReturnStatus(ids []string) {
  444. writeDb.Model(&models.WarehousingInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_return": 1})
  445. }
  446. func UpDateCanCelStockStatus(ids []string) {
  447. writeDb.Model(&models.WarehouseOutInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_cancel": 1})
  448. }
  449. func CreateWarehousingInfo(warehousingInfo []*models.WarehousingInfo) (err error) {
  450. if len(warehousingInfo) > 0 {
  451. utx := writeDb.Begin()
  452. if len(warehousingInfo) > 0 {
  453. thisSQL := "INSERT INTO xt_warehouse_info (warehousing_id, good_id, good_type_id, number, product_date,expiry_date,warehousing_count,price,total_price,dealer,manufacturer,remark,ctime,mtime,status,org_id,warehousing_order,type,stock_count,license_number,packing_price,storehouse_id,good_code) VALUES "
  454. insertParams := make([]string, 0)
  455. insertData := make([]interface{}, 0)
  456. for _, info := range warehousingInfo {
  457. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  458. insertData = append(insertData, info.WarehousingId)
  459. insertData = append(insertData, info.GoodId)
  460. insertData = append(insertData, info.GoodTypeId)
  461. insertData = append(insertData, info.Number)
  462. insertData = append(insertData, info.ProductDate)
  463. insertData = append(insertData, info.ExpiryDate)
  464. insertData = append(insertData, info.WarehousingCount)
  465. insertData = append(insertData, info.Price)
  466. insertData = append(insertData, info.TotalPrice)
  467. insertData = append(insertData, info.Dealer)
  468. insertData = append(insertData, info.Manufacturer)
  469. insertData = append(insertData, info.Remark)
  470. insertData = append(insertData, info.Ctime)
  471. insertData = append(insertData, info.Mtime)
  472. insertData = append(insertData, info.Status)
  473. insertData = append(insertData, info.OrgId)
  474. insertData = append(insertData, info.WarehousingOrder)
  475. insertData = append(insertData, info.Type)
  476. insertData = append(insertData, info.WarehousingCount)
  477. insertData = append(insertData, info.LicenseNumber)
  478. insertData = append(insertData, info.PackingPrice)
  479. insertData = append(insertData, info.StorehouseId)
  480. insertData = append(insertData, info.GoodCode)
  481. }
  482. thisSQL += strings.Join(insertParams, ", ")
  483. err = utx.Exec(thisSQL, insertData...).Error
  484. if err != nil {
  485. utx.Rollback()
  486. return
  487. }
  488. }
  489. utx.Commit()
  490. }
  491. return
  492. }
  493. func CreateNewWarehousingInfo(warehousingInfo []*models.WarehousingInfo, tx *gorm.DB) (err error) {
  494. if len(warehousingInfo) > 0 {
  495. if len(warehousingInfo) > 0 {
  496. thisSQL := "INSERT INTO xt_warehouse_info (warehousing_id, good_id, good_type_id, number, product_date,expiry_date,warehousing_count,price,total_price,dealer,manufacturer,remark,ctime,mtime,status,org_id,warehousing_order,type,stock_count,license_number,packing_price,storehouse_id,good_code) VALUES "
  497. insertParams := make([]string, 0)
  498. insertData := make([]interface{}, 0)
  499. for _, info := range warehousingInfo {
  500. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  501. insertData = append(insertData, info.WarehousingId)
  502. insertData = append(insertData, info.GoodId)
  503. insertData = append(insertData, info.GoodTypeId)
  504. insertData = append(insertData, info.Number)
  505. insertData = append(insertData, info.ProductDate)
  506. insertData = append(insertData, info.ExpiryDate)
  507. insertData = append(insertData, info.WarehousingCount)
  508. insertData = append(insertData, info.Price)
  509. insertData = append(insertData, info.TotalPrice)
  510. insertData = append(insertData, info.Dealer)
  511. insertData = append(insertData, info.Manufacturer)
  512. insertData = append(insertData, info.Remark)
  513. insertData = append(insertData, info.Ctime)
  514. insertData = append(insertData, info.Mtime)
  515. insertData = append(insertData, info.Status)
  516. insertData = append(insertData, info.OrgId)
  517. insertData = append(insertData, info.WarehousingOrder)
  518. insertData = append(insertData, info.Type)
  519. insertData = append(insertData, info.WarehousingCount)
  520. insertData = append(insertData, info.LicenseNumber)
  521. insertData = append(insertData, info.PackingPrice)
  522. insertData = append(insertData, info.StorehouseId)
  523. insertData = append(insertData, info.GoodCode)
  524. }
  525. thisSQL += strings.Join(insertParams, ", ")
  526. err = tx.Exec(thisSQL, insertData...).Error
  527. if err != nil {
  528. tx.Rollback()
  529. return
  530. }
  531. }
  532. }
  533. return
  534. }
  535. func CreateDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  536. if len(warehousingInfo) > 0 {
  537. utx := writeDb.Begin()
  538. if len(warehousingInfo) > 0 {
  539. thisSQL := "INSERT INTO xt_drug_warehouse_info (warehousing_id, drug_id, number, product_date,expiry_date,warehousing_count,price,total_price,dealer,manufacturer,remark,ctime,mtime,status,org_id,warehousing_order,type,retail_price,retail_total_price,stock_max_number,max_unit,min_unit,batch_number,stock_min_number,storehouse_id) VALUES "
  540. insertParams := make([]string, 0)
  541. insertData := make([]interface{}, 0)
  542. for _, info := range warehousingInfo {
  543. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  544. insertData = append(insertData, info.WarehousingId)
  545. insertData = append(insertData, info.DrugId)
  546. insertData = append(insertData, info.Number)
  547. insertData = append(insertData, info.ProductDate)
  548. insertData = append(insertData, info.ExpiryDate)
  549. insertData = append(insertData, info.WarehousingCount)
  550. insertData = append(insertData, info.Price)
  551. insertData = append(insertData, info.TotalPrice)
  552. insertData = append(insertData, info.Dealer)
  553. insertData = append(insertData, info.Manufacturer)
  554. insertData = append(insertData, info.Remark)
  555. insertData = append(insertData, info.Ctime)
  556. insertData = append(insertData, info.Mtime)
  557. insertData = append(insertData, info.Status)
  558. insertData = append(insertData, info.OrgId)
  559. insertData = append(insertData, info.WarehousingOrder)
  560. insertData = append(insertData, info.Type)
  561. insertData = append(insertData, info.RetailPrice)
  562. insertData = append(insertData, info.RetailTotalPrice)
  563. insertData = append(insertData, info.StockMaxNumber)
  564. insertData = append(insertData, info.MaxUnit)
  565. insertData = append(insertData, info.MinUnit)
  566. insertData = append(insertData, info.BatchNumber)
  567. insertData = append(insertData, info.StockMinNumber)
  568. insertData = append(insertData, info.StorehouseId)
  569. }
  570. thisSQL += strings.Join(insertParams, ", ")
  571. err = utx.Exec(thisSQL, insertData...).Error
  572. if err != nil {
  573. utx.Rollback()
  574. return
  575. }
  576. }
  577. utx.Commit()
  578. }
  579. return
  580. }
  581. func CreateNewDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo, tx *gorm.DB) (err error) {
  582. if len(warehousingInfo) > 0 {
  583. if len(warehousingInfo) > 0 {
  584. thisSQL := "INSERT INTO xt_drug_warehouse_info (warehousing_id, drug_id, number, product_date,expiry_date,warehousing_count,price,total_price,dealer,manufacturer,remark,ctime,mtime,status,org_id,warehousing_order,type,retail_price,retail_total_price,stock_max_number,max_unit,min_unit,batch_number,stock_min_number,storehouse_id,drug_code) VALUES "
  585. insertParams := make([]string, 0)
  586. insertData := make([]interface{}, 0)
  587. for _, info := range warehousingInfo {
  588. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  589. insertData = append(insertData, info.WarehousingId)
  590. insertData = append(insertData, info.DrugId)
  591. insertData = append(insertData, info.Number)
  592. insertData = append(insertData, info.ProductDate)
  593. insertData = append(insertData, info.ExpiryDate)
  594. insertData = append(insertData, info.WarehousingCount)
  595. insertData = append(insertData, info.Price)
  596. insertData = append(insertData, info.TotalPrice)
  597. insertData = append(insertData, info.Dealer)
  598. insertData = append(insertData, info.Manufacturer)
  599. insertData = append(insertData, info.Remark)
  600. insertData = append(insertData, info.Ctime)
  601. insertData = append(insertData, info.Mtime)
  602. insertData = append(insertData, info.Status)
  603. insertData = append(insertData, info.OrgId)
  604. insertData = append(insertData, info.WarehousingOrder)
  605. insertData = append(insertData, info.Type)
  606. insertData = append(insertData, info.RetailPrice)
  607. insertData = append(insertData, info.RetailTotalPrice)
  608. insertData = append(insertData, info.StockMaxNumber)
  609. insertData = append(insertData, info.MaxUnit)
  610. insertData = append(insertData, info.MinUnit)
  611. insertData = append(insertData, info.BatchNumber)
  612. insertData = append(insertData, info.StockMinNumber)
  613. insertData = append(insertData, info.StorehouseId)
  614. insertData = append(insertData, info.DrugCode)
  615. }
  616. thisSQL += strings.Join(insertParams, ", ")
  617. err = tx.Exec(thisSQL, insertData...).Error
  618. if err != nil {
  619. tx.Rollback()
  620. return
  621. }
  622. }
  623. }
  624. return
  625. }
  626. func CreateDrugWarehousingInfoSix(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  627. if len(warehousingInfo) > 0 {
  628. utx := writeDb.Begin()
  629. if len(warehousingInfo) > 0 {
  630. thisSQL := "INSERT INTO xt_drug_warehouse_info (warehousing_id, drug_id, number, product_date,expiry_date,warehousing_count,price,total_price,dealer,manufacturer,remark,ctime,mtime,status,org_id,warehousing_order,type,retail_price,retail_total_price,stock_max_number,max_unit,min_unit,batch_number,stock_min_number,supply_warehouse_id,supply_warehouse_detail_info,storehouse_id,is_check) VALUES "
  631. insertParams := make([]string, 0)
  632. insertData := make([]interface{}, 0)
  633. for _, info := range warehousingInfo {
  634. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  635. insertData = append(insertData, info.WarehousingId)
  636. insertData = append(insertData, info.DrugId)
  637. insertData = append(insertData, info.Number)
  638. insertData = append(insertData, info.ProductDate)
  639. insertData = append(insertData, info.ExpiryDate)
  640. insertData = append(insertData, info.WarehousingCount)
  641. insertData = append(insertData, info.Price)
  642. insertData = append(insertData, info.TotalPrice)
  643. insertData = append(insertData, info.Dealer)
  644. insertData = append(insertData, info.Manufacturer)
  645. insertData = append(insertData, info.Remark)
  646. insertData = append(insertData, info.Ctime)
  647. insertData = append(insertData, info.Mtime)
  648. insertData = append(insertData, info.Status)
  649. insertData = append(insertData, info.OrgId)
  650. insertData = append(insertData, info.WarehousingOrder)
  651. insertData = append(insertData, info.Type)
  652. insertData = append(insertData, info.RetailPrice)
  653. insertData = append(insertData, info.RetailTotalPrice)
  654. insertData = append(insertData, info.StockMaxNumber)
  655. insertData = append(insertData, info.MaxUnit)
  656. insertData = append(insertData, info.MinUnit)
  657. insertData = append(insertData, info.BatchNumber)
  658. insertData = append(insertData, info.StockMinNumber)
  659. insertData = append(insertData, info.SupplyWarehouseId)
  660. insertData = append(insertData, info.SupplyWarehouseDetailInfo)
  661. insertData = append(insertData, info.StorehouseId)
  662. insertData = append(insertData, info.IsCheck)
  663. }
  664. thisSQL += strings.Join(insertParams, ", ")
  665. err = utx.Exec(thisSQL, insertData...).Error
  666. if err != nil {
  667. utx.Rollback()
  668. return
  669. }
  670. }
  671. utx.Commit()
  672. }
  673. return
  674. }
  675. func UpdateDrugInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  676. var total float64
  677. var min_number int64
  678. for _, item := range warehousingInfo {
  679. //查询刚入库耗材的库存
  680. count, _ := FindDrugWaresingCount(item.DrugId, item.OrgId)
  681. //更新药品库的总量
  682. info, _ := FindeDrugInfo(item.DrugId)
  683. min_number = count.StockMaxNumber * info.MinNumber
  684. str := strconv.FormatInt(min_number, 10)
  685. minFloat, _ := strconv.ParseFloat(str, 64)
  686. total = info.Total + minFloat
  687. lib := models.BaseDrugLib{
  688. Total: total,
  689. }
  690. UpdateBaseDrug(&lib, item.ID)
  691. }
  692. return err
  693. }
  694. func FindDrugWaresingCount(drug_id int64, orgid int64) (models.XtDrugWarehouseInfo, error) {
  695. info := models.XtDrugWarehouseInfo{}
  696. db := XTReadDB().Table("xt_drug_warehouse_info").Where("status = 1")
  697. err = db.Select("stock_max_number,drug_id").Where("drug_id = ? and org_id = ? and status =1", drug_id, orgid).Last(&info).Error
  698. return info, err
  699. }
  700. func FindeDrugInfo(drug_id int64) (models.BaseDrugLib, error) {
  701. lib := models.BaseDrugLib{}
  702. err := XTReadDB().Model(&lib).Where("id = ?", drug_id).Find(&lib).Error
  703. return lib, err
  704. }
  705. func UpdateBaseDrug(lib *models.BaseDrugLib, id int64) error {
  706. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"total": lib.Total}).Error
  707. return err
  708. }
  709. func UpdateBaseDrugOne(lib models.BaseDrugLib, id int64) error {
  710. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"retail_price": lib.RetailPrice, "last_price": lib.LastPrice}).Error
  711. return err
  712. }
  713. func FindeLastWarehouseInfo(id int64) (models.DrugWarehouseInfo, error) {
  714. info := models.DrugWarehouseInfo{}
  715. err := XTReadDB().Model(&info).Where("id=? and status = 1", id).Find(&info).Error
  716. return info, err
  717. }
  718. func GetLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  719. out := models.DrugWarehouseOut{}
  720. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  721. return out, err
  722. }
  723. func CreateDrugFlow(drugflow []*models.DrugFlow) (err error) {
  724. if len(drugflow) > 0 {
  725. utx := writeDb.Begin()
  726. if len(drugflow) > 0 {
  727. thisSQL := "INSERT INTO xt_drug_flow (warehousing_id, drug_id, number,batch_number,count,user_org_id,patient_id,system_time,consumable_type,is_sys,warehousing_order,warehouse_out_id,warehouse_out_order_number,is_edit,cancel_stock_id,cancel_order_number,manufacturer,dealer,creator,update_creator,status,ctime,mtime,price,warehousing_detail_id,warehouse_out_detail_id,cancel_out_detail_id,expire_date,product_date,max_unit,min_unit,storehouse_id) VALUES "
  728. insertParams := make([]string, 0)
  729. insertData := make([]interface{}, 0)
  730. for _, info := range drugflow {
  731. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  732. insertData = append(insertData, info.WarehousingId)
  733. insertData = append(insertData, info.DrugId)
  734. insertData = append(insertData, info.Number)
  735. insertData = append(insertData, info.BatchNumber)
  736. insertData = append(insertData, info.Count)
  737. insertData = append(insertData, info.UserOrgId)
  738. insertData = append(insertData, info.PatientId)
  739. insertData = append(insertData, info.SystemTime)
  740. insertData = append(insertData, info.ConsumableType)
  741. insertData = append(insertData, info.IsSys)
  742. insertData = append(insertData, info.WarehousingOrder)
  743. insertData = append(insertData, info.WarehouseOutId)
  744. insertData = append(insertData, info.WarehouseOutOrderNumber)
  745. insertData = append(insertData, info.IsEdit)
  746. insertData = append(insertData, info.CancelStockId)
  747. insertData = append(insertData, info.CancelOrderNumber)
  748. insertData = append(insertData, info.Manufacturer)
  749. insertData = append(insertData, info.Dealer)
  750. insertData = append(insertData, info.Creator)
  751. insertData = append(insertData, info.UpdateCreator)
  752. insertData = append(insertData, info.Status)
  753. insertData = append(insertData, info.Ctime)
  754. insertData = append(insertData, info.Mtime)
  755. insertData = append(insertData, info.Price)
  756. insertData = append(insertData, info.WarehousingDetailId)
  757. insertData = append(insertData, info.WarehouseOutDetailId)
  758. insertData = append(insertData, info.CancelOutDetailId)
  759. insertData = append(insertData, info.ExpireDate)
  760. insertData = append(insertData, info.ProductDate)
  761. insertData = append(insertData, info.MaxUnit)
  762. insertData = append(insertData, info.MinUnit)
  763. insertData = append(insertData, info.StorehouseId)
  764. }
  765. thisSQL += strings.Join(insertParams, ", ")
  766. err = utx.Exec(thisSQL, insertData...).Error
  767. if err != nil {
  768. utx.Rollback()
  769. return
  770. }
  771. }
  772. utx.Commit()
  773. }
  774. return
  775. }
  776. func GetDrugFlowIsExist(warhouseing int64, drug_id int64) (*models.DrugFlow, error) {
  777. info := models.DrugFlow{}
  778. err = XTReadDB().Where("warehousing_id = ? and drug_id = ? and status = 1", warhouseing, drug_id).Find(&info).Error
  779. if err == gorm.ErrRecordNotFound {
  780. return nil, err
  781. }
  782. if err != nil {
  783. return nil, err
  784. }
  785. return &info, nil
  786. }
  787. func GetDrugByWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  788. info := models.XtDrugWarehouseInfo{}
  789. err := XTReadDB().Where("id=? and status = 1", id).Find(&info).Error
  790. return info, err
  791. }
  792. func UpdateDrugWarehouseingInfo(id int64, info models.XtDrugWarehouseInfo) error {
  793. warehouseInfo := models.XtDrugWarehouseInfo{}
  794. err := XTWriteDB().Model(&warehouseInfo).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_max_number": info.StockMaxNumber}).Error
  795. return err
  796. }
  797. func CreateDrugFlowOne(flow models.DrugFlow) error {
  798. ut := writeDb.Begin()
  799. err := ut.Create(&flow).Error
  800. if err != nil {
  801. ut.Rollback()
  802. return err
  803. }
  804. ut.Commit()
  805. return err
  806. }
  807. func CreateSelfDrugFlowTwo(flow models.XtSelfDrugFlow) error {
  808. ut := writeDb.Begin()
  809. err := ut.Create(&flow).Error
  810. if err != nil {
  811. ut.Rollback()
  812. return err
  813. }
  814. ut.Commit()
  815. return err
  816. }
  817. func CreateNewDrugFlowOne(flow models.DrugFlow, tx *gorm.DB) error {
  818. err := tx.Create(&flow).Error
  819. if err != nil {
  820. tx.Rollback()
  821. return err
  822. }
  823. return err
  824. }
  825. func CreateDrugOutInfoLog(log models.XtDrugWarehouseOutInfoLog, tx *gorm.DB) error {
  826. err := tx.Create(&log).Error
  827. if err != nil {
  828. tx.Rollback()
  829. return err
  830. }
  831. return err
  832. }
  833. func UpdateDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64, id int64) (models.DrugAutomaticReduceDetail, error) {
  834. detail := models.DrugAutomaticReduceDetail{}
  835. 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
  836. 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
  837. return detail, err
  838. }
  839. func UpdateSelfDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64, id int64) (models.XtSelfDrugWarehouseOutInfo, error) {
  840. detail := models.XtSelfDrugWarehouseOutInfo{}
  841. 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
  842. return detail, err
  843. }
  844. func UpdateDrugFlowTens(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.DrugFlow, error) {
  845. flow := models.DrugFlow{}
  846. 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
  847. return flow, err
  848. }
  849. func UpdateDrugFlowTensOne(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.XtSelfDrugFlow, error) {
  850. flow := models.XtSelfDrugFlow{}
  851. 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
  852. return flow, err
  853. }
  854. func CreateDrugFlowTwo(flow *models.DrugFlow) error {
  855. tx := XTWriteDB().Begin()
  856. err := tx.Create(&flow).Error
  857. if err != nil {
  858. tx.Rollback()
  859. return err
  860. }
  861. tx.Commit()
  862. return err
  863. }
  864. func CreateNewDrugFlowTwo(flow *models.DrugFlow, tx *gorm.DB) error {
  865. err := tx.Create(&flow).Error
  866. if err != nil {
  867. tx.Rollback()
  868. return err
  869. }
  870. return err
  871. }
  872. func CreateInfoLog(log models.XtDrugWarehouseInfoLog, tx *gorm.DB) error {
  873. err := tx.Create(&log).Error
  874. if err != nil {
  875. tx.Rollback()
  876. return err
  877. }
  878. return err
  879. }
  880. func FindLastWarehousingInfo(order string) (info models.WarehousingInfo, err error) {
  881. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  882. return info, err
  883. }
  884. func GetLastWarehousingInfo(goodid int64) (models.WarehousingInfo, error) {
  885. info := models.WarehousingInfo{}
  886. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  887. return info, err
  888. }
  889. func CreatedWarehouseingDetail(info *models.WarehousingInfo) error {
  890. err := XTWriteDB().Create(&info).Error
  891. return err
  892. }
  893. func CreatedNewWarehouseingDetail(info *models.WarehousingInfo, tx *gorm.DB) error {
  894. err := tx.Create(&info).Error
  895. if err != nil {
  896. tx.Rollback()
  897. return err
  898. }
  899. return err
  900. }
  901. func FindFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  902. if storehouse_id == 0 {
  903. 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
  904. }
  905. if storehouse_id > 0 {
  906. 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
  907. }
  908. return info, err
  909. }
  910. func FindNewFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64, tx *gorm.DB) (info models.WarehousingInfo, err error) {
  911. if storehouse_id == 0 {
  912. 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
  913. if err != gorm.ErrRecordNotFound {
  914. if err != nil {
  915. tx.Rollback()
  916. return info, err
  917. }
  918. }
  919. }
  920. if storehouse_id > 0 {
  921. 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
  922. if err != gorm.ErrRecordNotFound {
  923. if err != nil {
  924. tx.Rollback()
  925. return info, err
  926. }
  927. }
  928. }
  929. return info, err
  930. }
  931. func FindFirstWarehousingInfoByStockTwo(good_id int64, good_type_id int64, storehouse_id int64) (info models.VmWarehousingInfo, err error) {
  932. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  933. if good_id > 0 {
  934. db = db.Where("x.good_id = ?", good_id)
  935. }
  936. if good_type_id > 0 {
  937. db = db.Where("x.good_type_id = ?", good_type_id)
  938. }
  939. if storehouse_id > 0 {
  940. db = db.Where("x.storehouse_id = ?", storehouse_id)
  941. }
  942. err = db.Select("sum(x.stock_count) as count").Scan(&info).Error
  943. return info, err
  944. }
  945. func FindWarehousingInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  946. info := models.WarehousingInfo{}
  947. err := XTReadDB().Model(&info).Where("id= ? and status = 1", id).Find(&info).Error
  948. return info, err
  949. }
  950. func FindWarehousingInfoTen(goodId int64, storehouse_id int64) (models.WarehousingInfo, error) {
  951. info := models.WarehousingInfo{}
  952. var err error
  953. if storehouse_id == 0 {
  954. 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
  955. }
  956. if storehouse_id > 0 {
  957. 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
  958. }
  959. return info, err
  960. }
  961. func FindNewWarehousingInfoTen(goodId int64, storehouse_id int64, tx *gorm.DB) (models.WarehousingInfo, error) {
  962. info := models.WarehousingInfo{}
  963. var err error
  964. if storehouse_id == 0 {
  965. 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
  966. if err != gorm.ErrRecordNotFound {
  967. if err != nil {
  968. tx.Rollback()
  969. return info, err
  970. }
  971. }
  972. }
  973. if storehouse_id > 0 {
  974. 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
  975. if err != gorm.ErrRecordNotFound {
  976. if err != nil {
  977. tx.Rollback()
  978. return info, err
  979. }
  980. }
  981. }
  982. return info, err
  983. }
  984. //药品先进先出,找出最先入库的批次
  985. func FindLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  986. 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
  987. return info, err
  988. }
  989. func FindLastDrugWarehousingInfoByIDTwo(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  990. 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
  991. return info, err
  992. }
  993. func FindNewLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64, tx *gorm.DB) (info models.XtDrugWarehouseInfo, err error) {
  994. 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
  995. if err != gorm.ErrRecordNotFound {
  996. if err != nil {
  997. tx.Rollback()
  998. return info, err
  999. }
  1000. }
  1001. return info, err
  1002. }
  1003. func FindLastDrugWarehousingInfoByIDSix(drug_id int64, warehouse_out_id int64, is_source int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  1004. if is_source == 1 {
  1005. 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
  1006. }
  1007. if is_source == 2 || is_source == 0 {
  1008. 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
  1009. }
  1010. return info, err
  1011. }
  1012. // 出库时先进先出,找出最先入库的批次
  1013. func FindLastWarehousingInfoByID(drug_id int64) (info models.WarehousingInfo, err error) {
  1014. 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
  1015. return info, err
  1016. }
  1017. //
  1018. //// 出库时先进先出,找出最先入库的批次
  1019. //func (drug_id int64) (info models.WarehousingInfo, err error) {
  1020. // 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
  1021. // return info, err
  1022. //}
  1023. func FindLastDrugWarehousingInfo(order string) (info models.DrugWarehouseInfo, err error) {
  1024. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  1025. return info, err
  1026. }
  1027. 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) {
  1028. offset := (page - 1) * limit
  1029. db := readDb.Model(&models.Warehousing{})
  1030. db = db.Where("xt_warehouse.org_id = ? AND xt_warehouse.status = 1 AND xt_warehouse.type = ?", orgId, types)
  1031. if len(keywords) > 0 {
  1032. likeKey := "%" + keywords + "%"
  1033. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  1034. 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")
  1035. }
  1036. if startTime > 0 {
  1037. db = db.Where("xt_warehouse.operation_time >=?", startTime)
  1038. }
  1039. if endTime > 0 {
  1040. db = db.Where("xt_warehouse.operation_time<= ?", endTime)
  1041. }
  1042. if storehouse_id > 0 {
  1043. db = db.Where("xt_warehouse.storehouse_id = ?", storehouse_id)
  1044. }
  1045. if check_type == 0 {
  1046. db = db.Where("xt_warehouse.is_check = 0 or xt_warehouse.is_check = 1 or xt_warehouse.is_check = 2")
  1047. }
  1048. if check_type > 0 {
  1049. db = db.Where("xt_warehouse.is_check = ?", check_type)
  1050. }
  1051. err = db.Count(&total).Offset(offset).Limit(limit).Order("xt_warehouse.ctime desc").Find(&list).Error
  1052. return
  1053. }
  1054. func GetGoodInforByGoodName(keyword string, orgid int64) (goodInfo []*models.GoodInfo, err error) {
  1055. db := XTReadDB().Model(&goodInfo).Where("status = 1")
  1056. if orgid > 0 {
  1057. db = db.Where("org_id = ?", orgid)
  1058. }
  1059. if len(keyword) > 0 {
  1060. likeKey := "%" + keyword + "%"
  1061. db = db.Where("good_name like ?", likeKey)
  1062. }
  1063. err = db.Find(&goodInfo).Error
  1064. return goodInfo, err
  1065. }
  1066. func GetWarehoureOrderInfoByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  1067. db := XTReadDB().Model(&info).Where("status =1")
  1068. if len(good_id) > 0 {
  1069. db = db.Where("good_id in (?)", good_id)
  1070. }
  1071. if startime > 0 {
  1072. db = db.Where("ctime >=?", startime)
  1073. }
  1074. if endtime > 0 {
  1075. db = db.Where("ctime<=?", endtime)
  1076. }
  1077. if orgid > 0 {
  1078. db = db.Where("org_id = ?", orgid)
  1079. }
  1080. if storehouse_id > 0 {
  1081. db = db.Where("storehouse_id = ?", storehouse_id)
  1082. }
  1083. err = db.Find(&info).Error
  1084. return info, err
  1085. }
  1086. func GetWarehoureOrderOutByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehouseOutInfo, err error) {
  1087. db := XTReadDB().Model(&info).Where("status =1")
  1088. if len(good_id) > 0 {
  1089. db = db.Where("good_id in (?)", good_id)
  1090. }
  1091. if startime > 0 {
  1092. db = db.Where("ctime >=?", startime)
  1093. }
  1094. if endtime > 0 {
  1095. db = db.Where("ctime<=?", endtime)
  1096. }
  1097. if orgid > 0 {
  1098. db = db.Where("org_id = ?", orgid)
  1099. }
  1100. if storehouse_id > 0 {
  1101. db = db.Where("storehouse_id = ?", storehouse_id)
  1102. }
  1103. err = db.Find(&info).Error
  1104. return info, err
  1105. }
  1106. type VMDrugWarehouse struct {
  1107. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1108. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  1109. OperationTime int64 `gorm:"column:operation_time" json:"operation_time" form:"operation_time"`
  1110. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1111. Creater int64 `gorm:"column:creater" json:"creater" form:"creater"`
  1112. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1113. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  1114. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1115. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1116. WarehousingTime int64 `gorm:"column:warehousing_time" json:"warehousing_time" form:"warehousing_time"`
  1117. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1118. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1119. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1120. Manufacturers *models.Manufacturer `gorm:"ForeignKey:Manufacturer;AssociationForeignKey:ID" json:"manufacturers"`
  1121. Dealers *models.Dealer `gorm:"ForeignKey:Dealer;AssociationForeignKey:ID" json:"dealers"`
  1122. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1123. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1124. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  1125. IsCheck int64 `gorm:"column:is_check" json:"is_check" form:"is_check"`
  1126. Checker int64 `gorm:"column:checker" json:"checker" form:"checker"`
  1127. }
  1128. func (VMDrugWarehouse) TableName() string {
  1129. return "xt_drug_warehouse"
  1130. }
  1131. type VMDrugWarehouseInfo struct {
  1132. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1133. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id" form:"warehousing_id"`
  1134. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1135. Number string `gorm:"column:number" json:"number" form:"number"`
  1136. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1137. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1138. WarehousingCount int64 `gorm:"column:warehousing_count" json:"warehousing_count" form:"warehousing_count"`
  1139. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1140. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  1141. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1142. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1143. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1144. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1145. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1146. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1147. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1148. IsReturn int64 `gorm:"column:is_return" json:"is_return" form:"is_return"`
  1149. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  1150. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1151. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1152. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1153. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1154. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1155. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  1156. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1157. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1158. SupplyWarehouseDetailInfo int64 `gorm:"column:supply_warehouse_detail_info" json:"supply_warehouse_detail_info" form:"supply_warehouse_detail_info"`
  1159. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1160. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  1161. }
  1162. func (VMDrugWarehouseInfo) TableName() string {
  1163. return "xt_drug_warehouse_info"
  1164. }
  1165. type Drug struct {
  1166. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1167. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  1168. Pinyin string `gorm:"column:pinyin" json:"pinyin" form:"pinyin"`
  1169. Wubi string `gorm:"column:wubi" json:"wubi" form:"wubi"`
  1170. DrugAlias string `gorm:"column:drug_alias" json:"drug_alias" form:"drug_alias"`
  1171. DrugAliasPinyin string `gorm:"column:drug_alias_pinyin" json:"drug_alias_pinyin" form:"drug_alias_pinyin"`
  1172. DrugAliasWubi string `gorm:"column:drug_alias_wubi" json:"drug_alias_wubi" form:"drug_alias_wubi"`
  1173. DrugCategory int64 `gorm:"column:drug_category" json:"drug_category" form:"drug_category"`
  1174. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  1175. DrugType int64 `gorm:"column:drug_type" json:"drug_type" form:"drug_type"`
  1176. DrugStockLimit string `gorm:"column:drug_stock_limit" json:"drug_stock_limit" form:"drug_stock_limit"`
  1177. DrugOriginPlace string `gorm:"column:drug_origin_place" json:"drug_origin_place" form:"drug_origin_place"`
  1178. DrugDosageForm int64 `gorm:"column:drug_dosage_form" json:"drug_dosage_form" form:"drug_dosage_form"`
  1179. MedicalInsuranceLevel int64 `gorm:"column:medical_insurance_level" json:"medical_insurance_level" form:"medical_insurance_level"`
  1180. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1181. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  1182. UnitMatrixing string `gorm:"column:unit_matrixing" json:"unit_matrixing" form:"unit_matrixing"`
  1183. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1184. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  1185. DrugControl int64 `gorm:"column:drug_control" json:"drug_control" form:"drug_control"`
  1186. Number string `gorm:"column:number" json:"number" form:"number"`
  1187. DrugClassify string `gorm:"column:drug_classify" json:"drug_classify" form:"drug_classify"`
  1188. DrugDose float64 `gorm:"column:drug_dose" json:"drug_dose" form:"drug_dose"`
  1189. DrugDoseUnit int64 `gorm:"column:drug_dose_unit" json:"drug_dose_unit" form:"drug_dose_unit"`
  1190. MedicalInsuranceNumber string `gorm:"column:medical_insurance_number" json:"medical_insurance_number" form:"medical_insurance_number"`
  1191. ProvincesCode string `gorm:"column:provinces_code" json:"provinces_code" form:"provinces_code"`
  1192. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1193. PharmacologyCategory int64 `gorm:"column:pharmacology_category" json:"pharmacology_category" form:"pharmacology_category"`
  1194. StatisticsCategory int64 `gorm:"column:statistics_category" json:"statistics_category" form:"statistics_category"`
  1195. Code string `gorm:"column:code" json:"code" form:"code"`
  1196. IsSpecialDiseases int64 `gorm:"column:is_special_diseases" json:"is_special_diseases" form:"is_special_diseases"`
  1197. IsRecord int64 `gorm:"column:is_record" json:"is_record" form:"is_record"`
  1198. Agent string `gorm:"column:agent" json:"agent" form:"agent"`
  1199. DrugStatus string `gorm:"column:drug_status" json:"drug_status" form:"drug_status"`
  1200. LimitRemark string `gorm:"column:limit_remark" json:"limit_remark" form:"limit_remark"`
  1201. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  1202. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  1203. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  1204. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  1205. Label int64 `gorm:"column:label" json:"label" form:"label"`
  1206. Sort int64 `gorm:"column:sort" json:"sort" form:"sort"`
  1207. IsUseDoctorAdvice int64 `gorm:"column:is_use_doctor_advice" json:"is_use_doctor_advice" form:"is_use_doctor_advice"`
  1208. IsDefault int64 `gorm:"column:is_default" json:"is_default" form:"is_default"`
  1209. IsChargePredict int64 `gorm:"column:is_charge_predict" json:"is_charge_predict" form:"is_charge_predict"`
  1210. IsStatisticsWork int64 `gorm:"column:is_statistics_work" json:"is_statistics_work" form:"is_statistics_work"`
  1211. IsChargeUse int64 `gorm:"column:is_charge_use" json:"is_charge_use" form:"is_charge_use"`
  1212. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1213. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1214. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1215. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1216. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  1217. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1218. DoseCode string `gorm:"column:dose_code" json:"dose_code" form:"dose_code"`
  1219. RecordDate int64 `gorm:"column:record_date" json:"record_date" form:"record_date"`
  1220. DrugRemark string `gorm:"column:drug_remark" json:"drug_remark" form:"drug_remark"`
  1221. SocialSecurityDirectoryCode string `gorm:"column:social_security_directory_code" json:"social_security_directory_code" form:"social_security_directory_code"`
  1222. IsMark int64 `gorm:"column:is_mark" json:"is_mark" form:"is_mark"`
  1223. PrescriptionMark int64 `gorm:"column:prescription_mark" json:"prescription_mark" form:"prescription_mark"`
  1224. HospApprFlag int64 `gorm:"column:hosp_appr_flag" json:"hosp_appr_flag" form:"hosp_appr_flag"`
  1225. LmtUsedFlag int64 `gorm:"column:lmt_used_flag" json:"lmt_used_flag" form:"lmt_used_flag"`
  1226. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1227. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  1228. MinNumber int64 `gorm:"column:min_number" json:"min_number" form:"min_number"`
  1229. Dose string `gorm:"column:dose" json:"dose" form:"dose"`
  1230. DoseUnit string `gorm:"column:dose_unit" json:"dose_unit" form:"dose_unit"`
  1231. DrugDay string `gorm:"column:drug_day" json:"drug_day" form:"drug_day"`
  1232. MinPrice float64 `gorm:"column:min_price" json:"min_price" form:"min_price"`
  1233. IsZeroFlag int64 `gorm:"column:is_zero_flag" json:"is_zero_flag" form:"is_zero_flag"`
  1234. }
  1235. func (Drug) TableName() string {
  1236. return "xt_base_drug"
  1237. }
  1238. func FindAllDrugWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*VMDrugWarehouse, total int64, err error) {
  1239. db := readDb.Model(&VMDrugWarehouse{})
  1240. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1241. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1242. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1243. if len(keywords) > 0 {
  1244. likeKey := "%" + keywords + "%"
  1245. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1246. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse.manufacturer")
  1247. 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")
  1248. }
  1249. if startTime > 0 {
  1250. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1251. }
  1252. if endTime > 0 {
  1253. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1254. }
  1255. db = db.Count(&total)
  1256. offset := (page - 1) * limit
  1257. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1258. return
  1259. }
  1260. 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) {
  1261. db := readDb.Model(&VMDrugWarehouse{})
  1262. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1263. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1264. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1265. if len(keywords) > 0 {
  1266. likeKey := "%" + keywords + "%"
  1267. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1268. 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")
  1269. }
  1270. if startTime > 0 {
  1271. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1272. }
  1273. if endTime > 0 {
  1274. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1275. }
  1276. if storehouse_id > 0 {
  1277. db = db.Where("xt_drug_warehouse.storehouse_id = ?", storehouse_id)
  1278. }
  1279. if check_type > 0 {
  1280. db = db.Where("xt_drug_warehouse.is_check = ?", check_type)
  1281. }
  1282. db = db.Count(&total)
  1283. offset := (page - 1) * limit
  1284. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1285. return
  1286. }
  1287. func FindWarehousingInfoById(id int64) (list []*models.WarehousingInfo, err error) {
  1288. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1289. return list, err
  1290. }
  1291. func FindWarehousingInfoByIdOne(id []int64) (list []*models.WarehousingInfo, err error) {
  1292. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id in(?) AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1293. return list, err
  1294. }
  1295. func FindDrugWarehousingInfoById(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1296. 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
  1297. return list, err
  1298. }
  1299. func FindDrugWarehousingInfoByIdThree(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1300. err = readDb.Model(&VMDrugWarehouseInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehousing_id = ? AND status = 1", id).Find(&list).Error
  1301. return list, err
  1302. }
  1303. func FindDrugWarehousingInfoByIdOne(id []string, org_id int64) (list []*models.StDrugWarehouseInfo, err error) {
  1304. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  1305. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  1306. fmt.Println(table)
  1307. if len(id) > 0 {
  1308. db = db.Where("x.warehousing_id in(?)", id)
  1309. }
  1310. if org_id > 0 {
  1311. db = db.Where("x.org_id = ?", org_id)
  1312. }
  1313. 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
  1314. return list, err
  1315. }
  1316. func FindWarehousingById(id int64) (warehousing models.Warehousing, err error) {
  1317. err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1318. return warehousing, err
  1319. }
  1320. func FindNewWarehousingById(id int64, tx *gorm.DB) (warehousing models.Warehousing, err error) {
  1321. err = tx.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1322. if err != gorm.ErrRecordNotFound {
  1323. if err != nil {
  1324. tx.Rollback()
  1325. return warehousing, err
  1326. }
  1327. }
  1328. return warehousing, err
  1329. }
  1330. func FindWarehousingByIdOne(id []string) (warehousing []*models.Warehousing, err error) {
  1331. err = readDb.Model(&models.Warehousing{}).Where("id in(?) and status = 1", id).Find(&warehousing).Error
  1332. //err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1333. return warehousing, err
  1334. }
  1335. func FindDrugWarehousingById(id int64, org_id int64) (warehousing VMDrugWarehouse, err error) {
  1336. 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
  1337. return warehousing, err
  1338. }
  1339. func FindNewDrugWarehousingById(id int64, org_id int64, tx *gorm.DB) (warehousing VMDrugWarehouse, err error) {
  1340. 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
  1341. if err != gorm.ErrRecordNotFound {
  1342. if err != nil {
  1343. tx.Rollback()
  1344. return warehousing, err
  1345. }
  1346. }
  1347. return warehousing, err
  1348. }
  1349. func FindDrugWarehousingByIdOne(id []string, org_id int64) (warehousing []*models.DrugWarehouse, err error) {
  1350. db := XTReadDB().Table("xt_drug_warehouse as x").Where("x.status = 1")
  1351. if len(id) > 0 {
  1352. db = db.Where("x.id in(?)", id)
  1353. }
  1354. if org_id > 0 {
  1355. db = db.Where("x.org_id = ?", org_id)
  1356. }
  1357. 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
  1358. return warehousing, err
  1359. }
  1360. //func FindDrugWarehousingById(id int64) (warehousing models.DrugWarehouse, err error) {
  1361. // err = readDb.Model(&models.DrugWarehouse{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1362. // return warehousing, err
  1363. //
  1364. //}
  1365. func GetInfoByIds(ids []string) (info []*models.WarehousingInfo, err error) {
  1366. err = readDb.Model(&models.WarehousingInfo{}).Preload("Warehousing", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1367. return
  1368. }
  1369. func GetWarehousOutInfoByIds(ids []string) (info []*models.WarehouseOutInfo, err error) {
  1370. err = readDb.Model(&models.WarehouseOutInfo{}).Preload("WarehouseOut", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1371. return
  1372. }
  1373. func CreateSalesReturnInfo(salesReturnInfo []*models.SalesReturnInfo) (err error) {
  1374. if len(salesReturnInfo) > 0 {
  1375. utx := writeDb.Begin()
  1376. if len(salesReturnInfo) > 0 {
  1377. 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 "
  1378. insertParams := make([]string, 0)
  1379. insertData := make([]interface{}, 0)
  1380. for _, info := range salesReturnInfo {
  1381. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1382. insertData = append(insertData, info.GoodId)
  1383. insertData = append(insertData, info.SalesReturnId)
  1384. insertData = append(insertData, info.GoodTypeId)
  1385. insertData = append(insertData, info.Count)
  1386. insertData = append(insertData, info.Price)
  1387. insertData = append(insertData, info.Total)
  1388. insertData = append(insertData, info.Ctime)
  1389. insertData = append(insertData, info.Status)
  1390. insertData = append(insertData, info.OrgId)
  1391. insertData = append(insertData, info.OrderNumber)
  1392. insertData = append(insertData, info.Type)
  1393. insertData = append(insertData, info.Dealer)
  1394. insertData = append(insertData, info.Manufacturer)
  1395. }
  1396. thisSQL += strings.Join(insertParams, ", ")
  1397. err = utx.Exec(thisSQL, insertData...).Error
  1398. if err != nil {
  1399. utx.Rollback()
  1400. return
  1401. }
  1402. }
  1403. utx.Commit()
  1404. }
  1405. return
  1406. }
  1407. func CreateDrugSalesReturnInfo(salesReturnInfo []*models.DrugSalesReturnInfo) (err error) {
  1408. if len(salesReturnInfo) > 0 {
  1409. utx := writeDb.Begin()
  1410. if len(salesReturnInfo) > 0 {
  1411. 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 "
  1412. insertParams := make([]string, 0)
  1413. insertData := make([]interface{}, 0)
  1414. for _, info := range salesReturnInfo {
  1415. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1416. insertData = append(insertData, info.DrugId)
  1417. insertData = append(insertData, info.SalesReturnId)
  1418. insertData = append(insertData, info.Count)
  1419. insertData = append(insertData, info.Price)
  1420. insertData = append(insertData, info.Total)
  1421. insertData = append(insertData, info.Ctime)
  1422. insertData = append(insertData, info.Status)
  1423. insertData = append(insertData, info.OrgId)
  1424. insertData = append(insertData, info.OrderNumber)
  1425. insertData = append(insertData, info.Type)
  1426. insertData = append(insertData, info.Dealer)
  1427. insertData = append(insertData, info.Manufacturer)
  1428. insertData = append(insertData, info.RetailPrice)
  1429. insertData = append(insertData, info.RetailTotalPrice)
  1430. }
  1431. thisSQL += strings.Join(insertParams, ", ")
  1432. err = utx.Exec(thisSQL, insertData...).Error
  1433. if err != nil {
  1434. utx.Rollback()
  1435. return
  1436. }
  1437. }
  1438. utx.Commit()
  1439. }
  1440. return
  1441. }
  1442. func FindAllSalesReturnTotal(org_id int64) (total int64, err error) {
  1443. err = readDb.Model(&models.SalesReturn{}).Where("org_id = ?", org_id).Count(&total).Error
  1444. return total, err
  1445. }
  1446. func FindAllCancelStockTotal(org_id int64) (total int64, err error) {
  1447. err = readDb.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1448. return total, err
  1449. }
  1450. func FindNewAllCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1451. err = tx.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1452. if err != gorm.ErrRecordNotFound {
  1453. if err != nil {
  1454. tx.Rollback()
  1455. return total, err
  1456. }
  1457. }
  1458. return total, err
  1459. }
  1460. func FindAllDrugCancelStockTotal(org_id int64) (total int64, err error) {
  1461. err = readDb.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1462. return total, err
  1463. }
  1464. func FindNewAllDrugCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1465. err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1466. if err != gorm.ErrRecordNotFound {
  1467. if err != nil {
  1468. tx.Rollback()
  1469. return total, err
  1470. }
  1471. }
  1472. return total, err
  1473. }
  1474. //func FindAllDrugCancelStockTotalTX(org_id int64,tx *gorm.DB) (total int64, err error) {
  1475. // err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1476. // return total, err
  1477. //}
  1478. func FindAllReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.SalesReturn, total int64, err error) {
  1479. db := readDb.Model(&models.SalesReturn{})
  1480. db = db.Where("xt_sales_return.org_id = ? AND xt_sales_return.status = 1 AND xt_sales_return.type = ?", orgId, types)
  1481. if len(keywords) > 0 {
  1482. likeKey := "%" + keywords + "%"
  1483. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  1484. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_sales_return.manufacturer")
  1485. 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")
  1486. }
  1487. if startTime > 0 {
  1488. db = db.Where("xt_sales_return.opera_time >=?", startTime)
  1489. }
  1490. if endTime > 0 {
  1491. db = db.Where("xt_sales_return.opera_time<= ?", endTime)
  1492. }
  1493. db = db.Count(&total)
  1494. offset := (page - 1) * limit
  1495. err = db.Offset(offset).Limit(limit).Order("xt_sales_return.ctime desc").Find(&list).Error
  1496. return
  1497. }
  1498. func FindAllDrugReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugSalesReturn, total int64, err error) {
  1499. db := readDb.Model(&models.DrugSalesReturn{})
  1500. db = db.Where("xt_drug_sales_return.org_id = ? AND xt_drug_sales_return.status = 1 ", orgId)
  1501. if len(keywords) > 0 {
  1502. likeKey := "%" + keywords + "%"
  1503. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_sales_return.creater")
  1504. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_sales_return.manufacturer")
  1505. 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")
  1506. }
  1507. if startTime > 0 {
  1508. db = db.Where("xt_drug_sales_return.opera_time >=?", startTime)
  1509. }
  1510. if endTime > 0 {
  1511. db = db.Where("xt_drug_sales_return.opera_time<= ?", endTime)
  1512. }
  1513. db = db.Count(&total)
  1514. offset := (page - 1) * limit
  1515. err = db.Offset(offset).Limit(limit).Order("xt_drug_sales_return.ctime desc").Find(&list).Error
  1516. return
  1517. }
  1518. func FindReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  1519. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  1520. return list, err
  1521. }
  1522. func FindCancelStockInfoById(id int64) (list []*models.CancelStockInfo, err error) {
  1523. err = readDb.Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? AND status = 1", id).Preload("WarehousingInfo", "status = 1").Preload("GoodInfo", "status = 1").Find(&list).Error
  1524. return list, err
  1525. }
  1526. type DrugCancelStockInfo struct {
  1527. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1528. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1529. CancelStockId int64 `gorm:"column:cancel_stock_id" json:"cancel_stock_id" form:"cancel_stock_id"`
  1530. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1531. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1532. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1533. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1534. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1535. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1536. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1537. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1538. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1539. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  1540. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1541. Dealer string `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1542. Manufacturer string `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1543. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1544. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1545. Number string `gorm:"column:number" json:"number" form:"number"`
  1546. RegisterAccount string `gorm:"column:register_account" json:"register_account" form:"register_account"`
  1547. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1548. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1549. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1550. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1551. BatchNumberId int64 `gorm:"column:batch_number_id" json:"batch_number_id" form:"batch_number_id"`
  1552. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1553. DrugWarehouseInfo []*models.XtDrugWarehouseInfo `gorm:"ForeignKey:DrugId;AssociationForeignKey:DrugId" json:"drug_warehouse_info"`
  1554. }
  1555. func (DrugCancelStockInfo) TableName() string {
  1556. return "xt_drug_cancel_stock_info"
  1557. }
  1558. func FindDrugCancelStockInfoById(id int64, org_id int64) (list []*DrugCancelStockInfo, err error) {
  1559. 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
  1560. return list, err
  1561. }
  1562. func FindAllWarehousingInfo(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehousingInfo, total int64, err error) {
  1563. db := readDb.Model(&models.WarehousingInfo{})
  1564. db = db.Where("org_id = ? AND status = 1", orgId)
  1565. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  1566. if startTime > 0 {
  1567. db = db.Where("ctime >=?", startTime)
  1568. }
  1569. if endTime > 0 {
  1570. db = db.Where("ctime<= ?", endTime)
  1571. }
  1572. db = db.Count(&total)
  1573. offset := (page - 1) * limit
  1574. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1575. return list, total, err
  1576. }
  1577. func FindAllWarehouseOut(org_id int64) (total int64, err error) {
  1578. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1579. return total, err
  1580. }
  1581. func FindAllDrugWarehouseOut(org_id int64) (total int64, err error) {
  1582. err = readDb.Model(&models.DrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1583. return total, err
  1584. }
  1585. func FindAllSelfDrugWarehouseOut(org_id int64, tx *gorm.DB) (total int64, err error) {
  1586. err = tx.Model(&models.XtDrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1587. if err != gorm.ErrRecordNotFound {
  1588. if err != nil {
  1589. tx.Rollback()
  1590. return
  1591. }
  1592. }
  1593. return total, err
  1594. }
  1595. func AddSigleWarehouseOutOne(warehouseOut *models.WarehouseOut) error {
  1596. err := XTWriteDB().Create(&warehouseOut).Error
  1597. return err
  1598. }
  1599. func AddSigleWarehouseOut(warehouseOut *models.WarehouseOut, tx *gorm.DB) error {
  1600. err := tx.Create(&warehouseOut).Error
  1601. if err != nil {
  1602. tx.Rollback()
  1603. return err
  1604. }
  1605. return err
  1606. }
  1607. func AddNewSigleWarehouseOut(warehouseOut *models.WarehouseOut, tx *gorm.DB) error {
  1608. err := tx.Create(&warehouseOut).Error
  1609. if err != nil {
  1610. tx.Rollback()
  1611. return err
  1612. }
  1613. return err
  1614. }
  1615. func UpdateSingleWarehoseOut(id int64, storehouse_id int64, tx *gorm.DB) error {
  1616. err := tx.Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1617. if err != nil {
  1618. tx.Rollback()
  1619. return err
  1620. }
  1621. return err
  1622. }
  1623. func GetDrugWarehouseOutIsExit(org_id int64, is_sys int, record_time int64, warehousing_out_order string) (models.DrugWarehouseOut, error) {
  1624. drugWarehouseOut := models.DrugWarehouseOut{}
  1625. 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
  1626. return drugWarehouseOut, err
  1627. }
  1628. func GetNewDrugWarehouseOutIsExit(org_id int64, is_sys int, record_time int64, warehousing_out_order string, tx *gorm.DB) (models.DrugWarehouseOut, error) {
  1629. drugWarehouseOut := models.DrugWarehouseOut{}
  1630. 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
  1631. if err != gorm.ErrRecordNotFound {
  1632. if err != nil {
  1633. tx.Rollback()
  1634. return drugWarehouseOut, err
  1635. }
  1636. }
  1637. return drugWarehouseOut, err
  1638. }
  1639. func GetNewDrugWarehouseOutIsExitOne(org_id int64, is_sys int, record_time int64, warehousing_out_order string, tx *gorm.DB, id int64) (models.DrugWarehouseOut, error) {
  1640. drugWarehouseOut := models.DrugWarehouseOut{}
  1641. err := tx.Where("is_sys = ? AND status = 1 AND org_id = ? AND id = ?", is_sys, org_id, id).Find(&drugWarehouseOut).Error
  1642. if err != gorm.ErrRecordNotFound {
  1643. if err != nil {
  1644. tx.Rollback()
  1645. return drugWarehouseOut, err
  1646. }
  1647. }
  1648. return drugWarehouseOut, err
  1649. }
  1650. func AddSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut) error {
  1651. err := writeDb.Create(&warehouseOut).Error
  1652. return err
  1653. }
  1654. func GetSigleDrugWarehouseOutByPatientId(warehousing_time int64, patient_id int64, tx *gorm.DB) (models.XtSelfDrugWarehouse, error) {
  1655. selfDrugWarehouse := models.XtSelfDrugWarehouse{}
  1656. err := tx.Where("warehousing_time = ? and patient_id = ? and is_sys =1 and status=1", warehousing_time, patient_id).Find(&selfDrugWarehouse).Error
  1657. if err != gorm.ErrRecordNotFound {
  1658. if err != nil {
  1659. tx.Rollback()
  1660. return selfDrugWarehouse, err
  1661. }
  1662. }
  1663. return selfDrugWarehouse, err
  1664. }
  1665. func AddSelfSigleDrugWarehouseOut(warehouseOut *models.XtSelfDrugWarehouseOut, tx *gorm.DB) error {
  1666. err := tx.Create(&warehouseOut).Error
  1667. if err != nil {
  1668. tx.Rollback()
  1669. return err
  1670. }
  1671. return err
  1672. }
  1673. func AddNewSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut, tx *gorm.DB) error {
  1674. err := tx.Create(&warehouseOut).Error
  1675. if err != nil {
  1676. tx.Rollback()
  1677. return err
  1678. }
  1679. return err
  1680. }
  1681. func UpdateSingleDrugWarehouseOut(id int64, storehouse_id int64) error {
  1682. err := XTWriteDB().Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1683. return err
  1684. }
  1685. func UpdateNewSingleDrugWarehouseOut(id int64, storehouse_id int64, tx *gorm.DB) error {
  1686. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1687. if err != nil {
  1688. tx.Rollback()
  1689. return err
  1690. }
  1691. return err
  1692. }
  1693. func UpdateSelfSingleDrugWarehouseOut(id int64, patient_id int64, tx *gorm.DB) error {
  1694. err := tx.Model(&models.XtSelfDrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"patient_id": patient_id}).Error
  1695. return err
  1696. }
  1697. func CreateWarehousingOutInfo(warehouseOutInfo []*models.WarehouseOutInfo) (err error) {
  1698. if len(warehouseOutInfo) > 0 {
  1699. utx := writeDb.Begin()
  1700. if len(warehouseOutInfo) > 0 {
  1701. 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 "
  1702. insertParams := make([]string, 0)
  1703. insertData := make([]interface{}, 0)
  1704. for _, info := range warehouseOutInfo {
  1705. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1706. insertData = append(insertData, info.WarehouseOutId)
  1707. insertData = append(insertData, info.GoodId)
  1708. insertData = append(insertData, info.GoodTypeId)
  1709. insertData = append(insertData, info.ProductDate)
  1710. insertData = append(insertData, info.ExpiryDate)
  1711. insertData = append(insertData, info.Count)
  1712. insertData = append(insertData, info.Price)
  1713. insertData = append(insertData, info.TotalPrice)
  1714. insertData = append(insertData, info.Remark)
  1715. insertData = append(insertData, info.Ctime)
  1716. insertData = append(insertData, info.Status)
  1717. insertData = append(insertData, info.OrgId)
  1718. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1719. insertData = append(insertData, info.Type)
  1720. insertData = append(insertData, info.Dealer)
  1721. insertData = append(insertData, info.Manufacturer)
  1722. insertData = append(insertData, info.Number)
  1723. insertData = append(insertData, info.LicenseNumber)
  1724. }
  1725. thisSQL += strings.Join(insertParams, ", ")
  1726. err = utx.Exec(thisSQL, insertData...).Error
  1727. if err != nil {
  1728. utx.Rollback()
  1729. return
  1730. }
  1731. }
  1732. utx.Commit()
  1733. }
  1734. return
  1735. }
  1736. func CreateDrugWarehousingOutInfo(warehouseOutInfo []*models.DrugWarehouseOutInfo) (err error) {
  1737. if len(warehouseOutInfo) > 0 {
  1738. utx := writeDb.Begin()
  1739. if len(warehouseOutInfo) > 0 {
  1740. 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 "
  1741. insertParams := make([]string, 0)
  1742. insertData := make([]interface{}, 0)
  1743. for _, info := range warehouseOutInfo {
  1744. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1745. insertData = append(insertData, info.WarehouseOutId)
  1746. insertData = append(insertData, info.DrugId)
  1747. insertData = append(insertData, info.ProductDate)
  1748. insertData = append(insertData, info.ExpiryDate)
  1749. insertData = append(insertData, info.Count)
  1750. insertData = append(insertData, info.Price)
  1751. insertData = append(insertData, info.TotalPrice)
  1752. insertData = append(insertData, info.Remark)
  1753. insertData = append(insertData, info.Ctime)
  1754. insertData = append(insertData, info.Status)
  1755. insertData = append(insertData, info.OrgId)
  1756. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1757. insertData = append(insertData, info.Type)
  1758. insertData = append(insertData, info.Dealer)
  1759. insertData = append(insertData, info.Manufacturer)
  1760. insertData = append(insertData, info.RetailPrice)
  1761. insertData = append(insertData, info.RetailTotalPrice)
  1762. insertData = append(insertData, info.Number)
  1763. insertData = append(insertData, info.BatchNumber)
  1764. insertData = append(insertData, info.CountUnit)
  1765. }
  1766. thisSQL += strings.Join(insertParams, ", ")
  1767. err = utx.Exec(thisSQL, insertData...).Error
  1768. if err != nil {
  1769. utx.Rollback()
  1770. return
  1771. }
  1772. }
  1773. utx.Commit()
  1774. }
  1775. return
  1776. }
  1777. func FindLastWarehousingOutInfo(order string) (info models.WarehouseOutInfo, err error) {
  1778. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1779. return info, err
  1780. }
  1781. func FindLastDrugWarehousingOutInfo(order string) (info models.DrugWarehouseOutInfo, err error) {
  1782. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1783. return info, err
  1784. }
  1785. func FindAllWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.WarehouseOut, total int64, err error) {
  1786. db := readDb.Model(&models.WarehouseOut{})
  1787. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1788. if len(keywords) > 0 {
  1789. likeKey := "%" + keywords + "%"
  1790. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1791. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse_out.manufacturer")
  1792. 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")
  1793. }
  1794. if startTime > 0 {
  1795. db = db.Where("xt_warehouse_out.ctime >=?", startTime)
  1796. }
  1797. if endTime > 0 {
  1798. db = db.Where("xt_warehouse_out.ctime<= ?", endTime)
  1799. }
  1800. db = db.Count(&total)
  1801. offset := (page - 1) * limit
  1802. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1803. return list, total, err
  1804. }
  1805. 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) {
  1806. db := readDb.Model(&models.WarehouseOut{})
  1807. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1", orgId)
  1808. if len(keywords) > 0 {
  1809. likeKey := "%" + keywords + "%"
  1810. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1811. 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")
  1812. }
  1813. if startTime > 0 {
  1814. db = db.Where("xt_warehouse_out.warehouse_out_time >=?", startTime)
  1815. }
  1816. if endTime > 0 {
  1817. db = db.Where("xt_warehouse_out.warehouse_out_time<= ?", endTime)
  1818. }
  1819. if storehouse_id > 0 {
  1820. db = db.Where("xt_warehouse_out.storehouse_id = ?", storehouse_id)
  1821. }
  1822. if way_type == 0 {
  1823. 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")
  1824. }
  1825. if way_type == 1 {
  1826. db = db.Where("xt_warehouse_out.is_sys = 0")
  1827. }
  1828. if way_type == 2 {
  1829. db = db.Where("xt_warehouse_out.is_sys = 1")
  1830. }
  1831. if way_type == 3 {
  1832. db = db.Where("xt_warehouse_out.is_sys = 12")
  1833. }
  1834. if way_type == 4 {
  1835. db = db.Where("xt_warehouse_out.is_sys = 5")
  1836. }
  1837. if check_type > 0 {
  1838. db = db.Where("xt_warehouse_out.is_check = ?", check_type)
  1839. }
  1840. if check_type == 0 {
  1841. db = db.Where("xt_warehouse_out.is_check = 1 or xt_warehouse_out.is_check = 2")
  1842. }
  1843. db = db.Count(&total)
  1844. offset := (page - 1) * limit
  1845. if orgId == 10191 {
  1846. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1847. } else {
  1848. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1849. }
  1850. return list, total, err
  1851. }
  1852. func FindAllDrugWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugWarehouseOut, total int64, err error) {
  1853. db := readDb.Model(&models.DrugWarehouseOut{})
  1854. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1855. if len(keywords) > 0 {
  1856. likeKey := "%" + keywords + "%"
  1857. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1858. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse_out.manufacturer")
  1859. 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")
  1860. }
  1861. if startTime > 0 {
  1862. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1863. }
  1864. if endTime > 0 {
  1865. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1866. }
  1867. db = db.Count(&total)
  1868. offset := (page - 1) * limit
  1869. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1870. return list, total, err
  1871. }
  1872. 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) {
  1873. db := readDb.Model(&models.DrugWarehouseOut{})
  1874. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1875. if len(keywords) > 0 {
  1876. likeKey := "%" + keywords + "%"
  1877. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1878. 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")
  1879. }
  1880. if startTime > 0 {
  1881. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1882. }
  1883. if endTime > 0 {
  1884. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1885. }
  1886. if storehouse_id > 0 {
  1887. db = db.Where("xt_drug_warehouse_out.storehouse_id = ?", storehouse_id)
  1888. }
  1889. if check_type > 0 {
  1890. db = db.Where("xt_drug_warehouse_out.is_check = ?", check_type)
  1891. }
  1892. if way_type == 0 {
  1893. 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")
  1894. }
  1895. if way_type == 1 {
  1896. db = db.Where("xt_drug_warehouse_out.is_sys = 0")
  1897. }
  1898. if way_type == 2 {
  1899. db = db.Where("xt_drug_warehouse_out.is_sys = 1")
  1900. }
  1901. if way_type == 3 {
  1902. db = db.Where("xt_drug_warehouse_out.is_sys = 12")
  1903. }
  1904. if way_type == 4 {
  1905. db = db.Where("xt_drug_warehouse_out.is_sys = 15")
  1906. }
  1907. db = db.Count(&total)
  1908. offset := (page - 1) * limit
  1909. if orgId == 10191 {
  1910. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1911. } else {
  1912. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1913. }
  1914. return list, total, err
  1915. }
  1916. func FindWarehouseOutInfoById(id int64) (list []*models.WarehouseOutInfo, err error) {
  1917. 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
  1918. return list, err
  1919. }
  1920. type DrugWarehouseOutInfo struct {
  1921. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1922. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  1923. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1924. WarehousingOutTarget int64 `gorm:"column:warehousing_out_target" json:"warehousing_out_target" form:"warehousing_out_target"`
  1925. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1926. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  1927. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1928. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  1929. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1930. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1931. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1932. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1933. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1934. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1935. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1936. IsCancel int64 `gorm:"column:is_cancel" json:"is_cancel" form:"is_cancel"`
  1937. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  1938. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1939. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1940. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1941. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  1942. SysRecordTime int64 `gorm:"column:sys_record_time" json:"sys_record_time" form:"sys_record_time"`
  1943. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1944. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1945. Number string `gorm:"column:number" json:"number" form:"number"`
  1946. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1947. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1948. WarehouseInfoId int64 `gorm:"column:warehouse_info_id" json:"warehouse_info_id" form:"warehouse_info_id"`
  1949. SupplyCancelOutId int64 `gorm:"column:supply_cancel_out_id" json:"supply_cancel_out_id" form:"supply_cancel_out_id"`
  1950. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1951. AdminUserId int64 `gorm:"column:admin_user_id" json:"admin_user_id" form:"admin_user_id"`
  1952. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  1953. StockCount string `gorm:"column:stock_count" json:"stock_count" form:"stock_count"`
  1954. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  1955. ClassType string `gorm:"column:class_type" json:"class_type" form:"class_type"`
  1956. }
  1957. func (DrugWarehouseOutInfo) TableName() string {
  1958. return "xt_drug_warehouse_out_info"
  1959. }
  1960. func FindDrugWarehouseOutInfoById(id int64, org_id int64) (list []*DrugWarehouseOutInfo, err error) {
  1961. err = readDb.Model(&DrugWarehouseOutInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehouse_out_id = ? AND status = 1 ", id).Find(&list).Error
  1962. return list, err
  1963. }
  1964. func FindWarehouseInfoByGoodId(good_id int64) (int64, *models.WarehousingInfo) {
  1965. info := &models.WarehousingInfo{}
  1966. type WarehouseInfoCountAmount struct {
  1967. Total int64
  1968. }
  1969. var result WarehouseInfoCountAmount
  1970. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(warehousing_count) as total").Scan(&result)
  1971. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).First(info)
  1972. return result.Total, info
  1973. }
  1974. func FindAllSalesReturnCountByGoodId(good_id int64) (int64, error) {
  1975. type SalesReturnCountAmount struct {
  1976. Total int64
  1977. }
  1978. var result SalesReturnCountAmount
  1979. err = readDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1980. return result.Total, err
  1981. }
  1982. func FindAllWarehouseOutInfoByGoodId(good_id int64) (int64, error) {
  1983. type WarehouseOutInfoCountAmount struct {
  1984. Total int64
  1985. }
  1986. var result WarehouseOutInfoCountAmount
  1987. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1988. return result.Total, err
  1989. }
  1990. func FindAllCancleStockInfoByGoodId(good_id int64) (int64, error) {
  1991. type CancleStockoCountAmount struct {
  1992. Total int64
  1993. }
  1994. var result CancleStockoCountAmount
  1995. err = readDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1996. return result.Total, err
  1997. }
  1998. func FindWarehouseInfoByGoodTypeId(good_type_id int64) (models.WarehousingInfo, error) {
  1999. info := models.WarehousingInfo{}
  2000. err := readDb.Model(&models.WarehousingInfo{}).Where("good_type_id = ? AND status = 1", good_type_id).First(&info).Error
  2001. return info, err
  2002. }
  2003. func GetCancelStockDetailByOrderNumber(ordernumber string, orgid int64) (*models.CancelStock, error) {
  2004. stock := models.CancelStock{}
  2005. err := XTReadDB().Where("order_number = ? and org_id = ? and status = 1", ordernumber, orgid).Find(&stock).Error
  2006. if err == gorm.ErrRecordNotFound {
  2007. return nil, err
  2008. }
  2009. if err != nil {
  2010. return nil, err
  2011. }
  2012. return &stock, nil
  2013. }
  2014. func GetCancelStockDetailByOrderNumberOne(return_time int64, orgid int64) (*models.CancelStock, error) {
  2015. stock := models.CancelStock{}
  2016. err := XTReadDB().Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  2017. if err == gorm.ErrRecordNotFound {
  2018. return nil, err
  2019. }
  2020. if err != nil {
  2021. return nil, err
  2022. }
  2023. return &stock, nil
  2024. }
  2025. func GetNewCancelStockDetailByOrderNumberOne(return_time int64, orgid int64, tx *gorm.DB) (*models.CancelStock, error) {
  2026. stock := models.CancelStock{}
  2027. err := tx.Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  2028. if err != gorm.ErrRecordNotFound {
  2029. if err != nil {
  2030. tx.Rollback()
  2031. return &stock, err
  2032. }
  2033. }
  2034. return &stock, err
  2035. }
  2036. func AddSigleCancelStock(cancelStock *models.CancelStock) error {
  2037. err := writeDb.Create(&cancelStock).Error
  2038. return err
  2039. }
  2040. func AddNewSigleCancelStock(cancelStock *models.CancelStock, tx *gorm.DB) error {
  2041. err := tx.Create(&cancelStock).Error
  2042. if err != nil {
  2043. tx.Rollback()
  2044. return err
  2045. }
  2046. return err
  2047. }
  2048. func UpdateSigleCancelStock(id int64, storehouse_id int64) error {
  2049. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  2050. return err
  2051. }
  2052. func GetLastCancelStockById(orgid int64) (models.CancelStock, error) {
  2053. stock := models.CancelStock{}
  2054. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2055. return stock, err
  2056. }
  2057. func GetNewLastCancelStockById(orgid int64, tx *gorm.DB) (models.CancelStock, error) {
  2058. stock := models.CancelStock{}
  2059. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2060. if err != gorm.ErrRecordNotFound {
  2061. if err != nil {
  2062. tx.Rollback()
  2063. return stock, err
  2064. }
  2065. }
  2066. return stock, err
  2067. }
  2068. func GetLastDrugCancelStockById(orgid int64) (models.DrugCancelStock, error) {
  2069. stock := models.DrugCancelStock{}
  2070. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2071. return stock, err
  2072. }
  2073. func GetNewLastDrugCancelStockById(orgid int64, tx *gorm.DB) (models.DrugCancelStock, error) {
  2074. stock := models.DrugCancelStock{}
  2075. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2076. if err != gorm.ErrRecordNotFound {
  2077. if err != nil {
  2078. tx.Rollback()
  2079. return stock, err
  2080. }
  2081. }
  2082. return stock, err
  2083. }
  2084. func GetManufactureById(id int64) (models.Manufacturer, error) {
  2085. manufacturer := models.Manufacturer{}
  2086. err := XTReadDB().Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  2087. return manufacturer, err
  2088. }
  2089. func GetNewManufactureById(id int64, tx *gorm.DB) (models.Manufacturer, error) {
  2090. manufacturer := models.Manufacturer{}
  2091. err := tx.Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  2092. if err != gorm.ErrRecordNotFound {
  2093. if err != nil {
  2094. tx.Rollback()
  2095. return manufacturer, err
  2096. }
  2097. }
  2098. return manufacturer, err
  2099. }
  2100. func GetDealerById(id int64) (models.Dealer, error) {
  2101. dealer := models.Dealer{}
  2102. err := XTReadDB().Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  2103. return dealer, err
  2104. }
  2105. func GetNewDealerById(id int64, tx *gorm.DB) (models.Dealer, error) {
  2106. dealer := models.Dealer{}
  2107. err := tx.Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  2108. if err != gorm.ErrRecordNotFound {
  2109. if err != nil {
  2110. tx.Rollback()
  2111. return dealer, err
  2112. }
  2113. }
  2114. return dealer, err
  2115. }
  2116. func AddSigleDrugCancelStock(cancelStock *models.DrugCancelStock) error {
  2117. err := writeDb.Create(&cancelStock).Error
  2118. return err
  2119. }
  2120. func AddNewSigleDrugCancelStock(cancelStock *models.DrugCancelStock, tx *gorm.DB) error {
  2121. err := tx.Create(&cancelStock).Error
  2122. if err != nil {
  2123. tx.Rollback()
  2124. return err
  2125. }
  2126. return err
  2127. }
  2128. func UpdateSingleDrugCancelStock(id int64, storehouse_id int64) error {
  2129. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  2130. return err
  2131. }
  2132. //func AddSigleDrugCancelStockTX(cancelStock *models.DrugCancelStock,tx *gorm.DB) error {
  2133. // err := tx.Create(&cancelStock).Error
  2134. // return err
  2135. //}
  2136. func CreateCancelStockInfo(cancelStockInfo []*models.CancelStockInfo) (err error) {
  2137. if len(cancelStockInfo) > 0 {
  2138. utx := writeDb.Begin()
  2139. if len(cancelStockInfo) > 0 {
  2140. 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 "
  2141. insertParams := make([]string, 0)
  2142. insertData := make([]interface{}, 0)
  2143. for _, info := range cancelStockInfo {
  2144. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  2145. insertData = append(insertData, info.GoodId)
  2146. insertData = append(insertData, info.CancelStockId)
  2147. insertData = append(insertData, info.GoodTypeId)
  2148. insertData = append(insertData, info.Count)
  2149. insertData = append(insertData, info.Price)
  2150. insertData = append(insertData, info.Total)
  2151. insertData = append(insertData, info.Ctime)
  2152. insertData = append(insertData, info.Status)
  2153. insertData = append(insertData, info.OrgId)
  2154. insertData = append(insertData, info.OrderNumber)
  2155. insertData = append(insertData, info.Type)
  2156. insertData = append(insertData, info.Dealer)
  2157. insertData = append(insertData, info.Manufacturer)
  2158. insertData = append(insertData, info.Number)
  2159. insertData = append(insertData, info.RegisterAccount)
  2160. insertData = append(insertData, info.Remark)
  2161. insertData = append(insertData, info.ProductDate)
  2162. insertData = append(insertData, info.ExpiryDate)
  2163. insertData = append(insertData, info.WarehouseInfoId)
  2164. insertData = append(insertData, info.WarehouseInfoId)
  2165. }
  2166. thisSQL += strings.Join(insertParams, ", ")
  2167. err = utx.Exec(thisSQL, insertData...).Error
  2168. if err != nil {
  2169. utx.Rollback()
  2170. return
  2171. }
  2172. }
  2173. utx.Commit()
  2174. }
  2175. return
  2176. }
  2177. func CreateCancelStockInfoTwo(info *models.DrugCancelStockInfo) error {
  2178. err := XTWriteDB().Create(&info).Error
  2179. return err
  2180. }
  2181. //func CreateCancelStockInfoTwoTX(info *models.DrugCancelStockInfo,tx *gorm.DB) error {
  2182. // err := tx.Create(&info).Error
  2183. // return err
  2184. //}
  2185. func GetLastStockInfoThree(drugid int64) (models.DrugCancelStockInfo, error) {
  2186. info := models.DrugCancelStockInfo{}
  2187. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  2188. return info, err
  2189. }
  2190. //func GetLastStockInfoThreeTX(drugid int64,tx *gorm.DB) (models.DrugCancelStockInfo, error) {
  2191. // info := models.DrugCancelStockInfo{}
  2192. // err := tx.Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  2193. // return info, err
  2194. //}
  2195. func CreateCancelStockInfoOne(info *models.CancelStockInfo) error {
  2196. err := XTWriteDB().Create(&info).Error
  2197. return err
  2198. }
  2199. func CreateNewCancelStockInfoOne(info *models.CancelStockInfo, tx *gorm.DB) error {
  2200. err := XTWriteDB().Create(&info).Error
  2201. if err != nil {
  2202. tx.Rollback()
  2203. return err
  2204. }
  2205. return err
  2206. }
  2207. func GetLastCancelStockInfoByGoodId(goodId int64) (models.CancelStockInfo, error) {
  2208. info := models.CancelStockInfo{}
  2209. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  2210. return info, err
  2211. }
  2212. func GetLastNewCancelStockInfoByGoodId(goodId int64, tx *gorm.DB) (models.CancelStockInfo, error) {
  2213. info := models.CancelStockInfo{}
  2214. err := tx.Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  2215. if err != gorm.ErrRecordNotFound {
  2216. if err != nil {
  2217. tx.Rollback()
  2218. return info, err
  2219. }
  2220. }
  2221. return info, err
  2222. }
  2223. func GetLastDrugCancelStockInfoById(drugid int64) (models.DrugCancelStockInfo, error) {
  2224. info := models.DrugCancelStockInfo{}
  2225. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Last(&info).Error
  2226. return info, err
  2227. }
  2228. func CreateDrugCancelStockInfo(cancelStockInfo []*models.DrugCancelStockInfo) (err error) {
  2229. if len(cancelStockInfo) > 0 {
  2230. utx := writeDb.Begin()
  2231. if len(cancelStockInfo) > 0 {
  2232. 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 "
  2233. insertParams := make([]string, 0)
  2234. insertData := make([]interface{}, 0)
  2235. for _, info := range cancelStockInfo {
  2236. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  2237. insertData = append(insertData, info.DrugId)
  2238. insertData = append(insertData, info.CancelStockId)
  2239. insertData = append(insertData, info.Count)
  2240. insertData = append(insertData, info.Price)
  2241. insertData = append(insertData, info.Total)
  2242. insertData = append(insertData, info.Ctime)
  2243. insertData = append(insertData, info.Status)
  2244. insertData = append(insertData, info.OrgId)
  2245. insertData = append(insertData, info.OrderNumber)
  2246. insertData = append(insertData, info.Type)
  2247. insertData = append(insertData, info.Dealer)
  2248. insertData = append(insertData, info.Manufacturer)
  2249. insertData = append(insertData, info.RetailPrice)
  2250. insertData = append(insertData, info.RetailTotalPrice)
  2251. insertData = append(insertData, info.RegisterAccount)
  2252. insertData = append(insertData, info.BatchNumber)
  2253. insertData = append(insertData, info.Remark)
  2254. insertData = append(insertData, info.MaxUnit)
  2255. insertData = append(insertData, info.ProductDate)
  2256. insertData = append(insertData, info.ExpiryDate)
  2257. insertData = append(insertData, info.BatchNumberId)
  2258. }
  2259. thisSQL += strings.Join(insertParams, ", ")
  2260. err = utx.Exec(thisSQL, insertData...).Error
  2261. if err != nil {
  2262. utx.Rollback()
  2263. return
  2264. }
  2265. }
  2266. utx.Commit()
  2267. }
  2268. return
  2269. }
  2270. func FindAllCancelStockList(orgId int64) (list []*models.CancelStock, err error) {
  2271. db := readDb.Model(&models.CancelStock{})
  2272. db = db.Where("org_id = ? AND status = 1", orgId)
  2273. err = db.Order("ctime desc").Find(&list).Error
  2274. return
  2275. }
  2276. func FindAllStockOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  2277. db := readDb.Model(&models.WarehousingInfo{})
  2278. db = db.Where("org_id = ? AND status = 1", orgId)
  2279. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2280. if startTime > 0 {
  2281. db = db.Where("ctime >=?", startTime)
  2282. }
  2283. if endTime > 0 {
  2284. db = db.Where("ctime<= ?", endTime)
  2285. }
  2286. db = db.Count(&total)
  2287. offset := (page - 1) * limit
  2288. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2289. return
  2290. }
  2291. func FindAllStockInfo(orgId int64, page int64, limit int64, keyword string, startime int64, endtime int64, type_name int64) (list []*models.StockInfo, total int64, err error) {
  2292. db := XTReadDB().Model(&models.StockInfo{})
  2293. if startime > 0 {
  2294. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2295. return db.Where("org_id = ? AND status = 1 and sys_record_time>=?", orgId, startime)
  2296. })
  2297. }
  2298. if startime == 0 || endtime == 0 {
  2299. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2300. return db.Where("org_id = ? AND status = 1", orgId)
  2301. })
  2302. }
  2303. if endtime > 0 {
  2304. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2305. return db.Where("org_id = ? AND status = 1 and sys_record_time >=? and sys_record_time<=?", orgId, startime, endtime)
  2306. })
  2307. }
  2308. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgId)
  2309. db = db.Group("xt_good_information.id")
  2310. if startime == 0 || endtime == 0 {
  2311. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2312. return db.Where("org_id = ? AND status = 1", orgId)
  2313. })
  2314. }
  2315. if startime > 0 {
  2316. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2317. return db.Where("org_id = ? AND status = 1 and ctime >= ?", orgId, startime)
  2318. })
  2319. }
  2320. if endtime > 0 {
  2321. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2322. return db.Where("org_id = ? AND status = 1 and ctime>=? and ctime <= ?", orgId, startime, endtime)
  2323. })
  2324. }
  2325. db = db.Preload("QuerySalesReturnInfo", "org_id = ? AND status = 1", orgId)
  2326. if startime == 0 || endtime == 0 {
  2327. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1", orgId)
  2328. }
  2329. if startime > 0 {
  2330. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=?", orgId, startime)
  2331. }
  2332. if endtime > 0 {
  2333. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=? and ctime<=?", orgId, startime, endtime)
  2334. }
  2335. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  2336. if len(keyword) > 0 {
  2337. likeKey := "%" + keyword + "%"
  2338. 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)
  2339. 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")
  2340. }
  2341. if type_name > 0 {
  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 and xt_goods_type.id = ?", orgId, type_name)
  2343. }
  2344. db = db.Count(&total)
  2345. offset := (page - 1) * limit
  2346. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2347. return
  2348. }
  2349. func FindWarehouseInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2350. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2351. return
  2352. }
  2353. func FindWarehouseOutInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2354. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2355. return
  2356. }
  2357. func UpDateWarehouse(warehouse *models.Warehousing) (err error) {
  2358. 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
  2359. return
  2360. }
  2361. func UpDateWarehouseOut(warehouseOut *models.WarehouseOut) (err error) {
  2362. 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
  2363. return
  2364. }
  2365. func UpDateWarehouseInfo(warehouseInfo *models.WarehousingInfo) (err error) {
  2366. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", warehouseInfo.WarehousingOrder).Update(map[string]interface{}{"good_type_id": warehouseInfo.GoodTypeId,
  2367. "good_id": warehouseInfo.GoodId,
  2368. "number": warehouseInfo.Number,
  2369. "product_date": warehouseInfo.ProductDate,
  2370. "expiry_date": warehouseInfo.ExpiryDate,
  2371. "warehousing_count": warehouseInfo.WarehousingCount,
  2372. "price": warehouseInfo.Price,
  2373. "total_price": warehouseInfo.TotalPrice,
  2374. "mtime": warehouseInfo.Mtime,
  2375. "remark": warehouseInfo.Remark,
  2376. }).Error
  2377. return
  2378. }
  2379. func UpDateWarehouseStatus(id int64) (err error) {
  2380. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2381. return
  2382. }
  2383. func UpdatedStockFlowById(id int64) (err error) {
  2384. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("warehousing_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2385. return
  2386. }
  2387. func UpDateDrugWarehouseStatus(id int64) (err error) {
  2388. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2389. return
  2390. }
  2391. func UpdateDrugFlowById(id int64) (err error) {
  2392. err = XTWriteDB().Model(&models.DrugFlow{}).Where("warehousing_detail_id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2393. return
  2394. }
  2395. func DeleteWarehouse(ids []string) (err error) {
  2396. ut := writeDb.Begin()
  2397. err = ut.Model(&models.Warehousing{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2398. if err != nil {
  2399. ut.Rollback()
  2400. return
  2401. }
  2402. err = ut.Model(&models.WarehousingInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2403. if err != nil {
  2404. ut.Rollback()
  2405. return
  2406. }
  2407. ut.Commit()
  2408. return
  2409. }
  2410. func DeleteStockFlow(ids []string) (err error) {
  2411. ut := writeDb.Begin()
  2412. err = ut.Model(&models.VmStockFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2413. if err != nil {
  2414. ut.Rollback()
  2415. return
  2416. }
  2417. ut.Commit()
  2418. return
  2419. }
  2420. func DeleteDrugWarehouse(ids []string) (err error) {
  2421. ut := writeDb.Begin()
  2422. err = ut.Model(&models.DrugWarehouse{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2423. if err != nil {
  2424. ut.Rollback()
  2425. return
  2426. }
  2427. err = ut.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2428. if err != nil {
  2429. ut.Rollback()
  2430. return
  2431. }
  2432. err = ut.Model(&models.DrugFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2433. if err != nil {
  2434. ut.Rollback()
  2435. return
  2436. }
  2437. ut.Commit()
  2438. return
  2439. }
  2440. func FindWareHouseById(orderNumber string) (*models.Warehousing, error) {
  2441. warehouse := &models.Warehousing{}
  2442. err := readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).First(&warehouse).Error
  2443. return warehouse, err
  2444. }
  2445. func FindWareHouseOutByIdOne(id int64) (*models.WarehouseOut, error) {
  2446. warehouseOut := &models.WarehouseOut{}
  2447. err := XTReadDB().Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  2448. return warehouseOut, err
  2449. }
  2450. func FindWareHouseOutById(id int64, tx *gorm.DB) (*models.WarehouseOut, error) {
  2451. warehouseOut := &models.WarehouseOut{}
  2452. err := tx.Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  2453. if err != gorm.ErrRecordNotFound {
  2454. if err != nil {
  2455. tx.Rollback()
  2456. return warehouseOut, err
  2457. }
  2458. }
  2459. return warehouseOut, err
  2460. }
  2461. func FindDrugWareHouseOutById(id int64, org_id int64) (*models.DrugWarehouseOut, error) {
  2462. warehouseOut := &models.DrugWarehouseOut{}
  2463. 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
  2464. return warehouseOut, err
  2465. }
  2466. func FindNewDrugWareHouseOutById(id int64, org_id int64, tx *gorm.DB) (*models.DrugWarehouseOut, error) {
  2467. warehouseOut := &models.DrugWarehouseOut{}
  2468. 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
  2469. if err != nil {
  2470. tx.Rollback()
  2471. return warehouseOut, err
  2472. }
  2473. return warehouseOut, err
  2474. }
  2475. func FindWarehouseInfoByOrderNumber(orderNumber string) (info []*models.WarehousingInfo, err error) {
  2476. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", orderNumber).Find(&info).Error
  2477. return
  2478. }
  2479. func FindWarehouseOutInfoByOrderNumber(orderNumber string) (info []*models.WarehouseOutInfo, err error) {
  2480. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", orderNumber).Find(&info).Error
  2481. return
  2482. }
  2483. func DeleteStockInRecordByOrderNumber(orderNumber string, id int64) (err error) {
  2484. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2485. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2486. return
  2487. }
  2488. func DeleteStockInRecordByReocordId(id int64) (err error) {
  2489. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2490. return
  2491. }
  2492. func FindAllGoodByManufactureId(manufacturer_id int64, dealer_id int64, org_id int64) (goodInfo []*models.GoodInfo, err error) {
  2493. db := readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id)
  2494. if manufacturer_id > 0 {
  2495. db = db.Where("manufacturer = ?", manufacturer_id)
  2496. }
  2497. if dealer_id > 0 {
  2498. db = db.Where("dealer = ?", dealer_id)
  2499. }
  2500. err = db.Preload("GoodsType", "status = 1 AND org_id = ?", org_id).Find(&goodInfo).Error
  2501. return goodInfo, err
  2502. }
  2503. func FindAllWarehouseByKeyword(orgId int64, page int64, limit int64, keywords string) (list []*models.Warehousing, total int64, err error) {
  2504. db := readDb.Model(&models.Warehousing{})
  2505. likekey := "%" + keywords + "%"
  2506. db = db.Where("org_id = ? AND status = 1 ", orgId)
  2507. db = db.Where("warehousing_order LIKE ? ", likekey)
  2508. db = db.Count(&total)
  2509. offset := (page - 1) * limit
  2510. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2511. return
  2512. }
  2513. func GetLastWarehousingById(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2514. info := models.WarehousingInfo{}
  2515. err = XTReadDB().Model(&info).Where("warehousing_id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2516. return info, err
  2517. }
  2518. func GetLastWarehousingByIdOne(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2519. info := models.WarehousingInfo{}
  2520. err = XTReadDB().Model(&info).Where("id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2521. return info, err
  2522. }
  2523. func UpDateWarehousingInfo(info *models.WarehousingInfo) (err error) {
  2524. err = writeDb.Save(&info).Error
  2525. return err
  2526. }
  2527. func UpDateDrugWarehousingInfo(info *models.DrugWarehouseInfo) (err error) {
  2528. err = writeDb.Save(&info).Error
  2529. return err
  2530. }
  2531. func UpDateDrugWarehousingInfoTwo(id int64, info *models.DrugWarehouseInfo) (err error) {
  2532. 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
  2533. return err
  2534. }
  2535. func UpDateDrugWarehousingInfoSix(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, "storehouse_id": info.StorehouseId}).Error
  2537. return err
  2538. }
  2539. func GetDrugWarehouseOrderOne(id int64, drug_id int64) (models.DrugWarehouseInfo, error) {
  2540. info := models.DrugWarehouseInfo{}
  2541. err = XTReadDB().Model(&info).Where("warehousing_id = ? and drug_id = ?", id, drug_id).Find(&info).Error
  2542. return info, err
  2543. }
  2544. func EditWarehousing(warehouse models.Warehousing) {
  2545. 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
  2546. }
  2547. func EditNewWarehousing(warehouse models.Warehousing, tx *gorm.DB) {
  2548. 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
  2549. if err != nil {
  2550. tx.Rollback()
  2551. return
  2552. }
  2553. return
  2554. }
  2555. func FindAllWarehouseInfo(org_id int64, types int64) (list []*models.WarehousingInfoConfig, err error) {
  2556. err = readDb.Model(&models.WarehousingInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2557. 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)
  2558. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2559. return list, err
  2560. }
  2561. func DeleteSalesReturn(ids []string) (err error) {
  2562. ut := writeDb.Begin()
  2563. err = ut.Model(&models.SalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2564. if err != nil {
  2565. ut.Rollback()
  2566. return
  2567. }
  2568. err = ut.Model(&models.SalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2569. if err != nil {
  2570. ut.Rollback()
  2571. return
  2572. }
  2573. ut.Commit()
  2574. return
  2575. }
  2576. func DeleteDrugSalesReturn(ids []string) (err error) {
  2577. ut := writeDb.Begin()
  2578. err = ut.Model(&models.DrugSalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2579. if err != nil {
  2580. ut.Rollback()
  2581. return
  2582. }
  2583. err = ut.Model(&models.DrugSalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2584. if err != nil {
  2585. ut.Rollback()
  2586. return
  2587. }
  2588. ut.Commit()
  2589. return
  2590. }
  2591. func FindAllSalesReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  2592. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  2593. return
  2594. }
  2595. type DrugSalesReturnInfo struct {
  2596. ID int64 `gorm:"column:id" json:"id" form:"id"`
  2597. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  2598. SalesReturnId int64 `gorm:"column:sales_return_id" json:"sales_return_id" form:"sales_return_id"`
  2599. Count int64 `gorm:"column:count" json:"count" form:"count"`
  2600. Price float64 `gorm:"column:price" json:"price" form:"price"`
  2601. Total float64 `gorm:"column:total" json:"total" form:"total"`
  2602. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  2603. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  2604. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  2605. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  2606. Status int64 `gorm:"column:status" json:"status" form:"status"`
  2607. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  2608. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  2609. Type int64 `gorm:"column:type" json:"type" form:"type"`
  2610. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  2611. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  2612. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  2613. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  2614. Number string `gorm:"column:number" json:"number" form:"number"`
  2615. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  2616. }
  2617. func (DrugSalesReturnInfo) TableName() string {
  2618. return "xt_drug_sales_return_info"
  2619. }
  2620. func FindAllDrugSalesReturnInfoById(id int64, org_id int64) (list []*DrugSalesReturnInfo, err error) {
  2621. 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
  2622. return
  2623. }
  2624. func FindAllSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2625. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2626. return
  2627. }
  2628. func FindAllDrugSalesReturnById(id int64, org_id int64) (salesReturn models.DrugSalesReturn, err error) {
  2629. 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
  2630. return
  2631. }
  2632. func UpDateSaleReturnStatus(id int64) (err error) {
  2633. err = readDb.Model(&models.SalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2634. return
  2635. }
  2636. func UpDateDrugSaleReturnStatus(id int64) (err error) {
  2637. err = readDb.Model(&models.DrugSalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2638. return
  2639. }
  2640. func FindSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2641. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2642. return salesReturn, err
  2643. }
  2644. func FindDrugSalesReturnById(id int64) (salesReturn models.DrugSalesReturn, err error) {
  2645. err = readDb.Model(&models.DrugSalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2646. return salesReturn, err
  2647. }
  2648. func EditSaleReturn(sales models.SalesReturn) {
  2649. 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
  2650. }
  2651. func EditDrugSaleReturn(sales models.DrugSalesReturn) {
  2652. 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
  2653. }
  2654. func UpDateSalesReturnInfo(info *models.SalesReturnInfo) (err error) {
  2655. err = writeDb.Save(&info).Error
  2656. return err
  2657. }
  2658. func UpDateDrugSalesReturnInfo(info *models.DrugSalesReturnInfo) (err error) {
  2659. err = writeDb.Save(&info).Error
  2660. return err
  2661. }
  2662. func DeleteWarehouseOut(ids []string) (err error) {
  2663. ut := writeDb.Begin()
  2664. err = ut.Model(&models.WarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2665. if err != nil {
  2666. ut.Rollback()
  2667. return
  2668. }
  2669. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2670. if err != nil {
  2671. ut.Rollback()
  2672. return
  2673. }
  2674. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2675. if err != nil {
  2676. ut.Rollback()
  2677. return
  2678. }
  2679. ut.Commit()
  2680. return
  2681. }
  2682. func DeleteDrugWarehouseOut(ids []string) (err error) {
  2683. ut := writeDb.Begin()
  2684. err = ut.Model(&models.DrugWarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2685. if err != nil {
  2686. ut.Rollback()
  2687. return
  2688. }
  2689. err = ut.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2690. if err != nil {
  2691. ut.Rollback()
  2692. return
  2693. }
  2694. err = ut.Model(&models.DrugFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2695. if err != nil {
  2696. ut.Rollback()
  2697. return
  2698. }
  2699. ut.Commit()
  2700. return
  2701. }
  2702. func UpDateWarehouseOutStatus(id int64) (err error) {
  2703. err = readDb.Model(&models.WarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2704. return
  2705. }
  2706. func UpDateDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo) (err error) {
  2707. ut := writeDb.Begin()
  2708. err = ut.Save(&info).Error
  2709. if err != nil {
  2710. ut.Rollback()
  2711. return err
  2712. }
  2713. ut.Commit()
  2714. return err
  2715. }
  2716. func UpDateNewDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo, tx *gorm.DB) (err error) {
  2717. err = tx.Save(&info).Error
  2718. if err != nil {
  2719. tx.Rollback()
  2720. return err
  2721. }
  2722. return err
  2723. }
  2724. func UpDateWarehouseInfoByStock(info *models.WarehousingInfo) (err error) {
  2725. ut := writeDb.Begin()
  2726. err = ut.Save(&info).Error
  2727. if err != nil {
  2728. ut.Rollback()
  2729. return err
  2730. }
  2731. ut.Commit()
  2732. return err
  2733. }
  2734. func UpDateNewWarehouseInfoByStock(info *models.WarehousingInfo, tx *gorm.DB) (err error) {
  2735. err = tx.Save(&info).Error
  2736. if err != nil {
  2737. tx.Rollback()
  2738. return err
  2739. }
  2740. return err
  2741. }
  2742. func UpDateDrugWarehouseOutStatus(id int64) (err error) {
  2743. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2744. return
  2745. }
  2746. func EditWarehouseOut(warehouseOut models.WarehouseOut, tx *gorm.DB) {
  2747. 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
  2748. if err != nil {
  2749. tx.Rollback()
  2750. return
  2751. }
  2752. return
  2753. }
  2754. func EditDrugWarehouseOut(warehouseOut models.DrugWarehouseOut) {
  2755. 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
  2756. }
  2757. func EditNewDrugWarehouseOut(warehouseOut models.DrugWarehouseOut, tx *gorm.DB) {
  2758. 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
  2759. if err != nil {
  2760. tx.Rollback()
  2761. return
  2762. }
  2763. return
  2764. }
  2765. func UpDateWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) (err error) {
  2766. err = tx.Save(&info).Error
  2767. if err != nil {
  2768. tx.Rollback()
  2769. return err
  2770. }
  2771. return err
  2772. }
  2773. 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) {
  2774. db := readDb.Model(&models.CancelStock{})
  2775. db = db.Where("xt_cancel_stock.org_id = ? AND xt_cancel_stock.status = 1 AND xt_cancel_stock.type = ?", orgId, types)
  2776. if len(keywords) > 0 {
  2777. likeKey := "%" + keywords + "%"
  2778. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2779. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_cancel_stock.manufacturer")
  2780. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_cancel_stock.order_number LIKE ?", likeKey, likeKey).Group("xt_cancel_stock.id")
  2781. }
  2782. if startTime > 0 {
  2783. db = db.Where("xt_cancel_stock.opera_time >=?", startTime)
  2784. }
  2785. if endTime > 0 {
  2786. db = db.Where("xt_cancel_stock.opera_time<= ?", endTime)
  2787. }
  2788. if storehouse_id > 0 {
  2789. db = db.Where("xt_cancel_stock.storehouse_id = ?", storehouse_id)
  2790. }
  2791. if check_type > 0 {
  2792. db = db.Where("xt_cancel_stock.is_check = ?", check_type)
  2793. }
  2794. db = db.Count(&total)
  2795. offset := (page - 1) * limit
  2796. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock.ctime desc").Find(&list).Error
  2797. return
  2798. }
  2799. 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) {
  2800. db := readDb.Model(&models.DrugCancelStock{})
  2801. db = db.Where("xt_drug_cancel_stock.org_id = ? AND xt_drug_cancel_stock.status = 1", orgId)
  2802. if len(keywords) > 0 {
  2803. likeKey := "%" + keywords + "%"
  2804. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_cancel_stock.creater")
  2805. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_cancel_stock.manufacturer")
  2806. 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")
  2807. }
  2808. if startTime > 0 {
  2809. db = db.Where("xt_drug_cancel_stock.opera_time >=?", startTime)
  2810. }
  2811. if endTime > 0 {
  2812. db = db.Where("xt_drug_cancel_stock.opera_time<= ?", endTime)
  2813. }
  2814. if storehoue_id > 0 {
  2815. db = db.Where("xt_drug_cancel_stock.storehouse_id = ?", storehoue_id)
  2816. }
  2817. if check_type > 0 {
  2818. db = db.Where("xt_drug_cancel_stock.is_check = ?", check_type)
  2819. }
  2820. db = db.Count(&total)
  2821. offset := (page - 1) * limit
  2822. err = db.Offset(offset).Limit(limit).Order("xt_drug_cancel_stock.ctime desc").Find(&list).Error
  2823. return
  2824. }
  2825. func GetCancelWarehosueInfo(ids []string) (info []*models.CancelStockInfo, err error) {
  2826. err = XTReadDB().Where("id IN(?) and status = 1", ids).Find(&info).Error
  2827. return info, err
  2828. }
  2829. func DeleteCancelStock(ids []string) (err error) {
  2830. ut := writeDb.Begin()
  2831. err = ut.Model(&models.CancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2832. if err != nil {
  2833. ut.Rollback()
  2834. return
  2835. }
  2836. err = ut.Model(&models.CancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2837. if err != nil {
  2838. ut.Rollback()
  2839. return
  2840. }
  2841. err = ut.Model(&models.VmStockFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2842. if err != nil {
  2843. ut.Rollback()
  2844. return
  2845. }
  2846. ut.Commit()
  2847. return
  2848. }
  2849. func GetDrugWareInfoByCancelId(ids []string) (info []*models.DrugCancelStockInfo, err error) {
  2850. err = XTReadDB().Where("cancel_stock_id IN(?) and status = 1", ids).Find(&info).Error
  2851. return info, err
  2852. }
  2853. func DeleteDrugCancelStock(ids []string) (err error) {
  2854. ut := writeDb.Begin()
  2855. err = ut.Model(&models.DrugCancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2856. if err != nil {
  2857. ut.Rollback()
  2858. return
  2859. }
  2860. err = ut.Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2861. if err != nil {
  2862. ut.Rollback()
  2863. return
  2864. }
  2865. err = ut.Model(&models.DrugFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2866. if err != nil {
  2867. ut.Rollback()
  2868. return
  2869. }
  2870. ut.Commit()
  2871. return
  2872. }
  2873. func UpDateCancleStockStatus(id int64) (err error) {
  2874. err = readDb.Model(&models.CancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2875. return
  2876. }
  2877. func GetCancelStockById(id int64) (models.CancelStockInfo, error) {
  2878. info := models.CancelStockInfo{}
  2879. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  2880. return info, err
  2881. }
  2882. func ModefyWarehouseInfoNight(info models.WarehousingInfo, id int64) error {
  2883. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", info.StockCount)).Error
  2884. return err
  2885. }
  2886. func UpdatedStockFlowByCancelId(id int64, good_id int64) (err error) {
  2887. 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
  2888. return
  2889. }
  2890. func UpDateDrugCancleStockStatus(id int64) (err error) {
  2891. err = readDb.Model(&models.DrugCancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2892. return
  2893. }
  2894. func UpDateDrugCancelStockById(id int64) (err error) {
  2895. err = XTWriteDB().Model(&models.DrugFlow{}).Where("cancel_out_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2896. return
  2897. }
  2898. func EditCancelStock(sales models.CancelStock) {
  2899. 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
  2900. }
  2901. func EditDrugCancelStock(sales models.DrugCancelStock) {
  2902. 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
  2903. }
  2904. func FindCancelStockById(id int64, org_id int64) (cancelStock models.CancelStock, err error) {
  2905. err = readDb.Model(&models.CancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2906. return cancelStock, err
  2907. }
  2908. func FindCancelDrugById(id int64, org_id int64) (cancelStock models.DrugCancelStock, err error) {
  2909. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2910. return cancelStock, err
  2911. }
  2912. func FindDrugCancelStockById(id int64) (cancelStock models.DrugCancelStock, err error) {
  2913. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2914. return cancelStock, err
  2915. }
  2916. func UpDateCancelStockInfo(info *models.CancelStockInfo) (err error) {
  2917. err = writeDb.Save(&info).Error
  2918. return err
  2919. }
  2920. func UpDateDrugCancelStockInfo(info *models.DrugCancelStockInfo) (err error) {
  2921. err = writeDb.Save(&info).Error
  2922. return err
  2923. }
  2924. func FindWarehouseOutList(org_id int64, types int64) (list []*models.WarehouseOut, err error) {
  2925. 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
  2926. return list, err
  2927. }
  2928. func FindAllWarehouseOutInfo(org_id int64, types int64) (list []*models.WarehousingOutInfoConfig, err error) {
  2929. err = readDb.Model(&models.WarehousingOutInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2930. 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)
  2931. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2932. return list, err
  2933. }
  2934. type StockDetailModel struct {
  2935. GoodId int64 `gorm:"good_id" json:"good_id"`
  2936. GoodTypeId int64 `gorm:"good_type_id" json:"good_type_id"`
  2937. WarehousingOrder string `gorm:"warehousing_order" json:"warehousing_order"`
  2938. Price float64 `gorm:"price" json:"price"`
  2939. WarehousingCount int64 `gorm:"warehousing_count" json:"warehousing_count"`
  2940. Type int `gorm:"type" json:"type"`
  2941. Creater int64 `gorm:"creater" json:"creater"`
  2942. WarehousingTime int64 `gorm:"warehousing_time" json:"warehousing_time"`
  2943. Types int64 `gorm:"types" json:"types"`
  2944. }
  2945. 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) {
  2946. db := readDb.Model(&models.WarehousingInfo{})
  2947. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1 AND xt_warehouse_info.is_check = 1", orgId)
  2948. if good_id > 0 {
  2949. db = db.Where("xt_warehouse_info.good_id = ?", good_id)
  2950. }
  2951. if len(keywords) > 0 {
  2952. likeKey := "%" + keywords + "%"
  2953. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2954. 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)
  2955. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2956. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2957. 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")
  2958. } else {
  2959. 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)
  2960. }
  2961. if manufacturer > 0 {
  2962. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2963. }
  2964. if storehouse_id > 0 {
  2965. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  2966. }
  2967. if order_type > 0 {
  2968. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2969. }
  2970. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  2971. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2972. db = db.Count(&total)
  2973. offset := (page - 1) * limit
  2974. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_info.id desc").Find(&list).Error
  2975. return
  2976. }
  2977. 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) {
  2978. db := readDb.Model(&models.WarehousingInfo{})
  2979. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId)
  2980. if good_id > 0 {
  2981. db = db.Where("good_id = ?", good_id)
  2982. }
  2983. if len(keywords) > 0 {
  2984. likeKey := "%" + keywords + "%"
  2985. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2986. 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)
  2987. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2988. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2989. 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")
  2990. } else {
  2991. 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)
  2992. }
  2993. if manufacturer > 0 {
  2994. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2995. }
  2996. if order_type > 0 {
  2997. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2998. }
  2999. if storehouse_id > 0 {
  3000. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  3001. }
  3002. type PriceAmount struct {
  3003. Total float64
  3004. }
  3005. var result []PriceAmount
  3006. err = db.Select("total_price as total").Scan(&result).Error
  3007. var total float64
  3008. for _, value := range result {
  3009. total = total + value.Total
  3010. }
  3011. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  3012. return err, total
  3013. }
  3014. func FloatRound(f float64, n int) float64 {
  3015. format := "%." + strconv.Itoa(n) + "f"
  3016. res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
  3017. return res
  3018. }
  3019. 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) {
  3020. if orgId == 9671 {
  3021. db := readDb.Model(&models.WarehouseOutInfo{})
  3022. 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)
  3023. if len(keywords) > 0 {
  3024. likeKey := "%" + keywords + "%"
  3025. 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)
  3026. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  3027. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  3028. 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")
  3029. } else {
  3030. 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)
  3031. }
  3032. if manufacturer > 0 {
  3033. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  3034. }
  3035. if order_type > 0 {
  3036. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  3037. }
  3038. if storehouse_id > 0 {
  3039. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  3040. }
  3041. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  3042. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ?", orgId)
  3043. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3044. db = db.Count(&total)
  3045. offset := (page - 1) * limit
  3046. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  3047. return
  3048. } else {
  3049. db := readDb.Model(&models.WarehouseOutInfo{})
  3050. 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)
  3051. if good_id > 0 {
  3052. db = db.Where("good_id = ?", good_id)
  3053. }
  3054. if order_way == 1 {
  3055. db = db.Where("xt_warehouse_out_info.is_sys =0")
  3056. }
  3057. if order_way == 2 {
  3058. db = db.Where("xt_warehouse_out_info.is_sys !=0")
  3059. }
  3060. if len(keywords) > 0 {
  3061. likeKey := "%" + keywords + "%"
  3062. 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)
  3063. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  3064. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  3065. 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")
  3066. } else {
  3067. 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)
  3068. }
  3069. if manufacturer > 0 {
  3070. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  3071. }
  3072. if order_type > 0 {
  3073. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  3074. }
  3075. if storehouse_id > 0 {
  3076. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  3077. }
  3078. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ? and is_check =1", orgId)
  3079. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ? and is_check =1", orgId)
  3080. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3081. db = db.Count(&total)
  3082. offset := (page - 1) * limit
  3083. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  3084. return
  3085. }
  3086. }
  3087. 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) {
  3088. db := readDb.Model(&models.WarehouseOutInfo{})
  3089. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.count > 0", orgId)
  3090. if len(keywords) > 0 {
  3091. likeKey := "%" + keywords + "%"
  3092. 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)
  3093. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  3094. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  3095. 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")
  3096. } else {
  3097. 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)
  3098. }
  3099. if good_id > 0 {
  3100. db = db.Where("xt_warehouse_out_info.good_id = ?", good_id)
  3101. }
  3102. if order_way == 1 {
  3103. db = db.Where("xt_warehouse_out_info.is_sys = ?", 0)
  3104. }
  3105. if order_way == 2 {
  3106. db = db.Where("xt_warehouse_out_info.is_sys != ?", 0)
  3107. }
  3108. if manufacturer > 0 {
  3109. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  3110. }
  3111. if order_type > 0 {
  3112. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  3113. }
  3114. if storehouse_id > 0 {
  3115. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  3116. }
  3117. type PriceAmount struct {
  3118. Total float64
  3119. }
  3120. var result []PriceAmount
  3121. err = db.Select("(count * price) as total").Scan(&result).Error
  3122. var total float64
  3123. for _, value := range result {
  3124. total = total + value.Total
  3125. }
  3126. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  3127. fmt.Println(total)
  3128. return err, total
  3129. }
  3130. 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) {
  3131. db := readDb.Model(&models.SalesReturnInfo{})
  3132. db = db.Where("xt_sales_return_info.org_id = ? AND xt_sales_return_info.status = 1", orgId)
  3133. if len(keywords) > 0 {
  3134. likeKey := "%" + keywords + "%"
  3135. 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)
  3136. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  3137. db = db.Joins("join xt_good_information on xt_good_information.id = xt_sales_return_info.good_id")
  3138. 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")
  3139. } else {
  3140. 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)
  3141. }
  3142. if manufacturer > 0 {
  3143. db = db.Joins("join xt_sales_return as sr on sr.id = xt_sales_return_info.sales_return_id AND sr.manufacturer =?", manufacturer)
  3144. }
  3145. if order_type > 0 {
  3146. db = db.Where("xt_sales_return_info.type = ?", order_type)
  3147. }
  3148. db = db.Preload("SalesReturn", "status = 1 AND org_id = ?", orgId)
  3149. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3150. offset := (page - 1) * limit
  3151. db = db.Count(&total)
  3152. err = db.Offset(offset).Limit(limit).Order("xt_sales_return_info.ctime desc").Find(&list).Error
  3153. return
  3154. }
  3155. 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) {
  3156. db := readDb.Model(&models.CancelStockInfo{})
  3157. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  3158. if len(keywords) > 0 {
  3159. likeKey := "%" + keywords + "%"
  3160. 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)
  3161. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  3162. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  3163. 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")
  3164. } else {
  3165. 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)
  3166. }
  3167. if manufacturer > 0 {
  3168. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  3169. }
  3170. if order_type > 0 {
  3171. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  3172. }
  3173. if storehouse_id > 0 {
  3174. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", order_type)
  3175. }
  3176. if good_id > 0 {
  3177. db = db.Where("xt_cancel_stock_info.good_id = ?", good_id)
  3178. }
  3179. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  3180. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3181. err = db.Group("xt_cancel_stock_info.good_id").Order("xt_cancel_stock_info.ctime desc").Find(&list).Error
  3182. return
  3183. }
  3184. 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) {
  3185. db := readDb.Model(&models.CancelStockInfo{})
  3186. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  3187. if len(keywords) > 0 {
  3188. likeKey := "%" + keywords + "%"
  3189. 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)
  3190. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  3191. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  3192. 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")
  3193. } else {
  3194. 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)
  3195. }
  3196. if manufacturer > 0 {
  3197. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  3198. }
  3199. if order_type > 0 {
  3200. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  3201. }
  3202. if storehouse_id > 0 {
  3203. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", storehouse_id)
  3204. }
  3205. if good_id > 0 {
  3206. db = db.Where("xt_cancel_stock_info.good_id =?", good_id)
  3207. }
  3208. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  3209. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3210. db = db.Count(&total)
  3211. offset := (page - 1) * limit
  3212. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock_info.id desc").Find(&list).Error
  3213. return
  3214. }
  3215. func FindStockInByDealerId(id int64) (total int64, err error) {
  3216. err = readDb.Model(&models.Warehousing{}).Where("dealer = ? AND status = 1", id).Count(&total).Error
  3217. return
  3218. }
  3219. func FindStockInByManufacturerId(id int64) (total int64, err error) {
  3220. err = readDb.Model(&models.Warehousing{}).Where("manufacturer = ? AND status = 1", id).Count(&total).Error
  3221. return
  3222. }
  3223. func FindGoodTypeByName(name string, org_id int64) (total int64) {
  3224. readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 AND type_name = ? ", org_id, name).Count(&total)
  3225. return
  3226. }
  3227. func FindGoodInfoByName(name string, org_id int64, good_type_id int64) (total int64) {
  3228. 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)
  3229. return
  3230. }
  3231. func FindGoodInfoByNameTwo(name string, org_id int64, good_type_id int64, good_name string) (total int64) {
  3232. 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)
  3233. return
  3234. }
  3235. func FindGoodInfoByNameOne(name string, org_id int64, good_type_id int64, id int64, goodname string) (total int64) {
  3236. 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)
  3237. return
  3238. }
  3239. func FindAllGoodTypeByType(types int64) (goodType []*models.GoodsType, err error) {
  3240. err = readDb.Model(&models.GoodsType{}).Where("type = ? AND status = 1", types).Find(&goodType).Error
  3241. return goodType, err
  3242. }
  3243. func FindWarehouseInfoByGoodType(good_type_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  3244. 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
  3245. return info, err
  3246. }
  3247. func FindStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.WarehouseOut, err error) {
  3248. 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
  3249. return
  3250. }
  3251. func FindStockOutByIsSysOne(org_id int64, is_sys int, record_time int64, warehousing_out_order string, tx *gorm.DB) (out models.WarehouseOut, err error) {
  3252. 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
  3253. if err != gorm.ErrRecordNotFound {
  3254. if err != nil {
  3255. tx.Rollback()
  3256. return
  3257. }
  3258. }
  3259. return
  3260. }
  3261. func FindStockOutByIsSysSix(org_id int64, is_sys int, record_time int64, second_warehouse_id int64) (out models.WarehouseOut, err error) {
  3262. 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
  3263. return
  3264. }
  3265. func FindDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  3266. 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
  3267. return
  3268. }
  3269. func FindNewDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64, tx *gorm.DB) (out models.DrugWarehouseOut, err error) {
  3270. 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
  3271. if err != gorm.ErrRecordNotFound {
  3272. if err != nil {
  3273. tx.Rollback()
  3274. return
  3275. }
  3276. }
  3277. return
  3278. }
  3279. func FindDrugSettleStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  3280. 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
  3281. return
  3282. }
  3283. func FindLastStockInInfoRecord(good_id int64, org_id int64) (in models.WarehousingInfo, err error) {
  3284. err = readDb.Model(&models.WarehousingInfo{}).Where("status = 1 AND org_id = ? AND good_id = ?", org_id, good_id).First(&in).Error
  3285. return
  3286. }
  3287. func FindLastDrugStockInInfoRecord(drug_id int64, org_id int64) (in models.DrugWarehouseOutInfo, err error) {
  3288. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("status = 1 AND org_id = ? AND drug_id = ?", org_id, drug_id).Last(&in).Error
  3289. return
  3290. }
  3291. func GetStockInRecoredByGoodId(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  3292. info := models.WarehouseOutInfo{}
  3293. 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
  3294. if err == gorm.ErrRecordNotFound {
  3295. return nil, err
  3296. }
  3297. if err != nil {
  3298. return nil, err
  3299. }
  3300. return &info, nil
  3301. }
  3302. func GetStockInRecoredByGoodIdOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64) (*models.WarehouseOutInfo, error) {
  3303. info := models.WarehouseOutInfo{}
  3304. 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
  3305. if err == gorm.ErrRecordNotFound {
  3306. return nil, err
  3307. }
  3308. if err != nil {
  3309. return nil, err
  3310. }
  3311. return &info, nil
  3312. }
  3313. func UpdateAutoMaticReduceDetail(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  3314. 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
  3315. return err
  3316. }
  3317. func UpdateAutoMaticReduceDetailOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  3318. 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
  3319. return err
  3320. }
  3321. func GetGoodsIsExsit(good_id int64, sys_record_time int64, org_id int64, warehouse_out_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  3322. outInfo := models.WarehouseOutInfo{}
  3323. 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
  3324. if err != gorm.ErrRecordNotFound {
  3325. if err != nil {
  3326. tx.Rollback()
  3327. return outInfo, err
  3328. }
  3329. }
  3330. return outInfo, err
  3331. }
  3332. func GetDrugsIsExsit(drug_id int64, sys_record_time int64, org_id int64, warehouse_out_id int64) (models.XtDrugWarehouseOutInfo, error) {
  3333. outInfo := models.XtDrugWarehouseOutInfo{}
  3334. 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
  3335. return outInfo, err
  3336. }
  3337. func GetNewDrugsIsExsit(drug_id int64, sys_record_time int64, org_id int64, warehouse_out_id int64, tx *gorm.DB) (models.XtDrugWarehouseOutInfo, error) {
  3338. outInfo := models.XtDrugWarehouseOutInfo{}
  3339. 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
  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 AddSigleWarehouseOutInfoOne(info *models.WarehouseOutInfo) error {
  3349. err := XTWriteDB().Create(&info).Error
  3350. return err
  3351. }
  3352. func AddSigleWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) error {
  3353. err := tx.Create(&info).Error
  3354. if err != nil {
  3355. tx.Rollback()
  3356. return err
  3357. }
  3358. return err
  3359. }
  3360. func AddNewSigleWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) error {
  3361. err := tx.Create(&info).Error
  3362. if err != nil {
  3363. tx.Rollback()
  3364. return err
  3365. }
  3366. return err
  3367. }
  3368. func UpdateStockOut(warehouseinfoid int64, info models.WarehousingInfo) error {
  3369. err := XTWriteDB().Model(&info).Where("id=? and status = 1", warehouseinfoid).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  3370. return err
  3371. }
  3372. func AddSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo) error {
  3373. err := writeDb.Create(&info).Error
  3374. return err
  3375. }
  3376. func AddSelfSigleDrugWarehouseOutInfo(info *models.XtSelfDrugWarehouseOutInfo, 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 AddNewSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, tx *gorm.DB) error {
  3385. err := tx.Create(&info).Error
  3386. if err != nil {
  3387. tx.Rollback()
  3388. return err
  3389. }
  3390. return err
  3391. }
  3392. func UpdateSigleDrugWarehouseOutInfoDetail(id int64, count int64, tx *gorm.DB) error {
  3393. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
  3394. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).UpdateColumn("over_count", gorm.Expr("over_count - ?", count)).Error
  3395. if err != nil {
  3396. tx.Rollback()
  3397. return err
  3398. }
  3399. return err
  3400. }
  3401. func UpdateSigleDrugWarehouseOutInfoDetailOne(id int64, count int64, tx *gorm.DB) error {
  3402. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"count": count}).Error
  3403. return err
  3404. }
  3405. func AddSigleAutoReduceRecordInfo(detail *models.AutomaticReduceDetail) error {
  3406. ut := writeDb.Begin()
  3407. err := ut.Create(&detail).Error
  3408. if err != nil {
  3409. ut.Rollback()
  3410. return err
  3411. }
  3412. ut.Commit()
  3413. return err
  3414. }
  3415. func AddSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail) error {
  3416. err := writeDb.Create(&detail).Error
  3417. return err
  3418. }
  3419. 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) {
  3420. detail := models.DrugAutomaticReduceDetail{}
  3421. 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
  3422. if err != gorm.ErrRecordNotFound {
  3423. if err != nil {
  3424. tx.Rollback()
  3425. return detail, err
  3426. }
  3427. }
  3428. return detail, err
  3429. }
  3430. func UpdateNewSingleDrugAutoReduceRecordInfo(id int64, count int64, tx *gorm.DB) error {
  3431. err := tx.Model(models.DrugAutomaticReduceDetail{}).Where("id = ? and status =1", id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
  3432. if err != nil {
  3433. tx.Rollback()
  3434. return err
  3435. }
  3436. return err
  3437. }
  3438. func AddNewSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail, tx *gorm.DB) error {
  3439. err := tx.Create(&detail).Error
  3440. if err != nil {
  3441. tx.Rollback()
  3442. return err
  3443. }
  3444. return err
  3445. }
  3446. func FindStockOutInfoByTypeId(org_id int64, good_type_id int64, good_id int64, out_id int64, number string) (out models.WarehouseOutInfo, err error) {
  3447. 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
  3448. return
  3449. }
  3450. func FindStockOutInfoByStock(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (out []*models.WarehouseOutInfo, err error) {
  3451. 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
  3452. return
  3453. }
  3454. func FindStockOutInfoByStockOne(org_id int64, good_type_id int64, good_id int64, record_date int64) (out []*models.WarehouseOutInfo, err error) {
  3455. 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
  3456. return
  3457. }
  3458. func FindStockOutInfoByStockTwo(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (models.WarehouseOutInfo, error) {
  3459. info := models.WarehouseOutInfo{}
  3460. 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
  3461. return info, err
  3462. }
  3463. func FindDrugStockOutInfoByTypeId(org_id int64, drug_id int64, out_id int64, number string) (out models.DrugWarehouseOutInfo, err error) {
  3464. 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
  3465. return
  3466. }
  3467. func UpdateStockOutInfoCount(org_id int64, id int64) {
  3468. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", 1))
  3469. }
  3470. func FindUserDetailById(org_id int64, id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  3471. db := readDb.Model(&models.AutomaticReduceDetail{})
  3472. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  3473. db = db.Preload("GoodsType", "status = 1")
  3474. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3475. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND good_id != 0 AND count <> 0 ", org_id, id)
  3476. db = db.Count(&total)
  3477. err = db.Find(&user).Error
  3478. return
  3479. }
  3480. type DrugAutomaticReduceDetail struct {
  3481. ID int64 `gorm:"column:id" json:"id" form:"id"`
  3482. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  3483. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  3484. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  3485. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  3486. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  3487. Status int64 `gorm:"column:status" json:"status" form:"status"`
  3488. RecordTime int64 `gorm:"column:record_time" json:"record_time" form:"record_time"`
  3489. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  3490. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  3491. Count int64 `gorm:"column:count" json:"count" form:"count"`
  3492. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  3493. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  3494. Patients *models.Patients `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"user"`
  3495. DrugWarehouseOutInfo []*models.DrugWarehouseOutInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"drugwarehouseoutinfo"`
  3496. }
  3497. func (DrugAutomaticReduceDetail) TableName() string {
  3498. return "xt_drug_automatic_reduce_detail"
  3499. }
  3500. func FindDrugStockUserDetailById(org_id int64, id int64) (user []*DrugAutomaticReduceDetail, err error, total int64) {
  3501. db := readDb.Model(&DrugAutomaticReduceDetail{})
  3502. db = db.Preload("Drug", "org_id = ? AND status = 1", org_id)
  3503. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3504. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND drug_id != 0", org_id, id)
  3505. db = db.Count(&total)
  3506. err = db.Find(&user).Error
  3507. return
  3508. }
  3509. func UpdateStockOutInfoCount2(org_id int64, id int64, count int64) {
  3510. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3511. }
  3512. func UpdateDrugStockOutInfoCount2(org_id int64, id int64, count int64) {
  3513. writeDb.Model(&models.DrugWarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3514. }
  3515. func FindPatientAutomaticReduceRecord(org_id int64, record_time int64, good_id int64, good_type_id int64, patient_id int64) (count int64, err error) {
  3516. 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
  3517. return
  3518. }
  3519. func FindPatientDrugAutomaticReduceRecord(org_id int64, record_time int64, drug_id int64, patient_id int64) (count int64, err error) {
  3520. 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
  3521. return
  3522. }
  3523. type VMWarehousingInfo struct {
  3524. ID int64 `gorm:"column:id" json:"id"`
  3525. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id"`
  3526. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  3527. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  3528. Status int64 `gorm:"column:status" json:"status"`
  3529. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3530. GoodInfo GoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"good_info"`
  3531. }
  3532. func (VMWarehousingInfo) TableName() string {
  3533. return "xt_warehouse_info"
  3534. }
  3535. type GoodInfo struct {
  3536. ID int64 `gorm:"column:id" json:"id"`
  3537. SpecificationName string `gorm:"column:specification_name" json:"specification_name"`
  3538. Status int64 `gorm:"column:status" json:"status"`
  3539. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3540. }
  3541. func (GoodInfo) TableName() string {
  3542. return "xt_good_information"
  3543. }
  3544. func GetStockInGoodInfo(orgId int64) (stockIn []*VMWarehousingInfo, err error) {
  3545. err = readDb.Model(&VMWarehousingInfo{}).Preload("GoodInfo", "status = 1 AND org_id = ?", orgId).Where("org_id = ? AND status = 1", orgId).Find(&stockIn).Error
  3546. return
  3547. }
  3548. func GetAllStockOutUserDetail(patient_id int64, orgId int64, record_time int64) (goodUser []*models.DialysisBeforePrepare, err error) {
  3549. 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
  3550. return
  3551. }
  3552. func PostSearchStock(keyword string, orgid int64) (goods []*models.GoodsType, err error) {
  3553. likeKey := "%" + keyword + "%"
  3554. db := XTReadDB().Model(&goods)
  3555. if len(keyword) > 0 {
  3556. db = db.Where("type_name like ?", likeKey)
  3557. }
  3558. err = db.Where("org_id = ? and status =1", orgid).Find(&goods).Error
  3559. return goods, err
  3560. }
  3561. func GetStockType(orgid int64) (goodstype []*models.GoodsTypeOne, err error) {
  3562. err = XTReadDB().Where("org_id = ? and status = 1 and (stock_attribute = 2 or stock_attribute = 3 or stock_attribute = 4)", orgid).
  3563. Preload("GoodInfo", "org_id = ? and status = 1 and find_in_set('停用',good_status) = 0", orgid).Find(&goodstype).Error
  3564. return goodstype, err
  3565. }
  3566. func GetDialyStockOut(orgid int64, recordtime int64, patientId int64) (prepare []*models.XtDialysisBeforePrepare, err error) {
  3567. db := XTReadDB().Table("dialysis_before_prepare as x").Where("x.status = 1")
  3568. if orgid > 0 {
  3569. db = db.Where("x.user_org_id = ?", orgid)
  3570. }
  3571. if recordtime > 0 {
  3572. db = db.Where("x.record_date = ?", recordtime)
  3573. }
  3574. if patientId > 0 {
  3575. db = db.Where("x.patient_id = ?", patientId)
  3576. }
  3577. 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
  3578. return prepare, err
  3579. }
  3580. func GetAutoReduceRecordInfoById(orgid int64, recordtime int64, goodid int64, goodtypeid int64) (*models.XtAutomaticReduceDetail, error) {
  3581. detail := models.XtAutomaticReduceDetail{}
  3582. 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
  3583. if err == gorm.ErrRecordNotFound {
  3584. return nil, err
  3585. }
  3586. if err != nil {
  3587. return nil, err
  3588. }
  3589. return &detail, nil
  3590. }
  3591. func GetOutStockTotalCount(wareouttime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3592. 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
  3593. return autoMatic, err
  3594. }
  3595. func GetOutStockTotalCountOne(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3596. 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
  3597. return autoMatic, err
  3598. }
  3599. func GetManufacturerName(orgid int64, name string) (*models.Manufacturer, error) {
  3600. var manufacturer models.Manufacturer
  3601. var err error
  3602. err = XTReadDB().Model(&manufacturer).Where("org_id = ? and manufacturer_name = ? and status = 1", orgid, name).Find(&manufacturer).Error
  3603. if err == gorm.ErrRecordNotFound {
  3604. return nil, err
  3605. }
  3606. if err != nil {
  3607. return nil, err
  3608. }
  3609. return &manufacturer, nil
  3610. }
  3611. func CreateManufacturer(manufacturer *models.Manufacturer) error {
  3612. err := XTWriteDB().Create(manufacturer).Error
  3613. return err
  3614. }
  3615. func UpdatedManufactur(manufacturer *models.Manufacturer, orgid int64) error {
  3616. err := XTWriteDB().Model(&manufacturer).Where("org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"manufacturer_name": manufacturer.ManufacturerName}).Error
  3617. return err
  3618. }
  3619. func GetGoodsInformaitonIsExist(goodName string, goodType int64, specificationName string, orgid int64) (*models.GoodInfo, error) {
  3620. var good models.GoodInfo
  3621. var err error
  3622. 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
  3623. if err == gorm.ErrRecordNotFound {
  3624. return nil, err
  3625. }
  3626. if err != nil {
  3627. return nil, err
  3628. }
  3629. return &good, nil
  3630. }
  3631. func CreateGoodsInfomation(info *models.GoodInfo) error {
  3632. err := XTWriteDB().Create(&info).Error
  3633. return err
  3634. }
  3635. func UpdateGoodsInformation(info *models.GoodInfo, goodName string, goodType int64, specificationName string, orgid int64) error {
  3636. 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
  3637. return err
  3638. }
  3639. func GetCoutWareseOutInfo(startime int64, endtime int64, orgid int64) (houseOutInfo []*models.VmWarehouseOutInfo, err error) {
  3640. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  3641. 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
  3642. return houseOutInfo, err
  3643. }
  3644. func GetGoodInfomationList(orgid int64, keyword string) (goodInfo []*models.GoodInfo, err error) {
  3645. key := "%" + keyword + "%"
  3646. if len(keyword) == 0 {
  3647. err = XTReadDB().Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Find(&goodInfo).Error
  3648. } else {
  3649. err = XTReadDB().Where("org_id = ? and status = 1 AND good_name like ? AND find_in_set('停用',good_status) = 0", orgid, key).Find(&goodInfo).Error
  3650. }
  3651. return goodInfo, err
  3652. }
  3653. func IsExistDrugByName(drugname string, drugdesc string, orgid int64) (*models.BaseDrugLib, error) {
  3654. var drug models.BaseDrugLib
  3655. var err error
  3656. err = XTReadDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and status =1 and org_id = ?", drugname, drugdesc, orgid).Find(&drug).Error
  3657. if err == gorm.ErrRecordNotFound {
  3658. return nil, err
  3659. }
  3660. if err != nil {
  3661. return nil, err
  3662. }
  3663. return &drug, nil
  3664. }
  3665. func IsExistDrugByNameOne(drugname string, dosage string, doseunit string, minnumber int64, minunit string, packtunit string, orgid int64) (*models.BaseDrugLib, error) {
  3666. var drug models.BaseDrugLib
  3667. var err error
  3668. 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
  3669. if err == gorm.ErrRecordNotFound {
  3670. return nil, err
  3671. }
  3672. if err != nil {
  3673. return nil, err
  3674. }
  3675. return &drug, nil
  3676. }
  3677. func CreateDrugsInfomation(info *models.BaseDrugLib) error {
  3678. err := XTWriteDB().Create(&info).Error
  3679. return err
  3680. }
  3681. func UpdateDrugsInformation(drug *models.BaseDrugLib, drugName string, drugSpec string, orgid int64) error {
  3682. 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
  3683. return err
  3684. }
  3685. func GetProjectByNameIsExsit(projectName string, orgid int64) (*models.HisProject, error) {
  3686. var projoect models.HisProject
  3687. var err error
  3688. err = XTReadDB().Model(&projoect).Where("project_name = ? and user_org_id = ? and status =1", projectName, orgid).Find(&projoect).Error
  3689. if err == gorm.ErrRecordNotFound {
  3690. return nil, err
  3691. }
  3692. if err != nil {
  3693. return nil, err
  3694. }
  3695. return &projoect, nil
  3696. }
  3697. func CreateProjectInfomation(info *models.HisProject) error {
  3698. err := XTWriteDB().Create(&info).Error
  3699. return err
  3700. }
  3701. func UpdateProjectByName(project *models.HisProject, projectName string, orgid int64) error {
  3702. 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
  3703. return err
  3704. }
  3705. func GetWareOutInfoById(warehouseoutid int64, orgid int64) (warehouseinfo []*models.WarehouseOutInfo, err error) {
  3706. err = XTReadDB().Model(&warehouseinfo).Where("warehouse_out_id = ? and org_id = ? and status= 1 and is_sys = 0", warehouseoutid, orgid).Find(&warehouseinfo).Error
  3707. return warehouseinfo, err
  3708. }
  3709. func EditDrugWarehousingOne(warehouse models.DrugWarehouse, id int64) error {
  3710. 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
  3711. return err
  3712. }
  3713. func EditNewDrugWarehousingOne(warehouse models.DrugWarehouse, id int64, tx *gorm.DB) error {
  3714. 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
  3715. if err != nil {
  3716. tx.Rollback()
  3717. return err
  3718. }
  3719. return err
  3720. }
  3721. func GetAllManufacturerList(orgid int64) (list []*models.Manufacturer, err error) {
  3722. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3723. return list, err
  3724. }
  3725. func GetNewAllManufacturerList(orgid int64) (list []*models.BloodManufacturer, err error) {
  3726. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3727. return list, err
  3728. }
  3729. func GetAllDealerList(orgid int64) (list []*models.Dealer, err error) {
  3730. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3731. return list, err
  3732. }
  3733. // func GetAllDealerListTX(orgid int64,tx *gorm.DB) (list []*models.Dealer, err error) {
  3734. // err = tx.Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3735. // return list, err
  3736. // }
  3737. func GetDealerByName(orgid int64, name string) (*models.Dealer, error) {
  3738. var dealer models.Dealer
  3739. var err error
  3740. err = XTReadDB().Model(&dealer).Where("org_id = ? and dealer_name = ? and status = 1", orgid, name).Find(&dealer).Error
  3741. if err == gorm.ErrRecordNotFound {
  3742. return nil, err
  3743. }
  3744. if err != nil {
  3745. return nil, err
  3746. }
  3747. return &dealer, nil
  3748. }
  3749. func CreateDealer(dealer *models.Dealer) error {
  3750. err := XTWriteDB().Create(&dealer).Error
  3751. return err
  3752. }
  3753. func UpdateDealer(dealer *models.Dealer, orgid int64) error {
  3754. err := XTWriteDB().Model(&dealer).Where("org_id = ? and status =1", orgid).Updates(map[string]interface{}{"dealer_name": dealer.DealerName}).Error
  3755. return err
  3756. }
  3757. func GetDrugDataConfig(orgid int64, name string) (models.DictDataconfig, error) {
  3758. dataconfig := models.DictDataconfig{}
  3759. err := XTReadDB().Where("org_id = ? and name = ?", orgid, name).Find(&dataconfig).Error
  3760. return dataconfig, err
  3761. }
  3762. func GetParentDataConfig(parentid int64, orgid int64) (dataconfig []*models.DictDataconfig, err error) {
  3763. err = XTReadDB().Where("parent_id = ? and status =1 and (org_id = ? or org_id = 0)", parentid, orgid).Find(&dataconfig).Error
  3764. return dataconfig, err
  3765. }
  3766. func IsExistDicConfig(parentid int64, goodKind string, orgid int64) (*models.DictDataconfig, error) {
  3767. var dataconfig models.DictDataconfig
  3768. var err error
  3769. err = XTReadDB().Where("parent_id = ? and status = 1 and name = ? and (org_id = ? or org_id = 0)", parentid, goodKind, orgid).Find(&dataconfig).Error
  3770. if err == gorm.ErrRecordNotFound {
  3771. return nil, err
  3772. }
  3773. if err != nil {
  3774. return nil, err
  3775. }
  3776. return &dataconfig, nil
  3777. }
  3778. func GetLastDicConfig(parentid int64, orgid int64) (models.DictDataconfig, error) {
  3779. dataconfig := models.DictDataconfig{}
  3780. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status = 1", parentid, orgid).Last(&dataconfig).Error
  3781. return dataconfig, err
  3782. }
  3783. func CreatedDicConfig(dataconfig *models.DictDataconfig) error {
  3784. err := XTWriteDB().Create(&dataconfig).Error
  3785. return err
  3786. }
  3787. func UpdatedDicConfig(parentid int64, dataconfig *models.DictDataconfig, orgid int64, goodKind string) error {
  3788. err := XTWriteDB().Model(&dataconfig).Where("parent_id = ? and org_id = ? and name = ?", parentid, orgid, goodKind).Updates(map[string]interface{}{"name": dataconfig.Name}).Error
  3789. return err
  3790. }
  3791. func GetGoodType(goodName string, orgid int64) (*models.GoodsType, error) {
  3792. var goodType models.GoodsType
  3793. var err error
  3794. err = XTReadDB().Where("type_name =? and org_id = ? and status = 1", goodName, orgid).Find(&goodType).Error
  3795. if err == gorm.ErrRecordNotFound {
  3796. return nil, err
  3797. }
  3798. if err != nil {
  3799. return nil, err
  3800. }
  3801. return &goodType, nil
  3802. }
  3803. func CreatedGoodType(goodsType *models.GoodsType) error {
  3804. err := XTWriteDB().Create(&goodsType).Error
  3805. return err
  3806. }
  3807. func GetAllGoodType(orgid int64) (goodsType []*models.GoodsType, err error) {
  3808. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3809. return goodsType, err
  3810. }
  3811. func GetAllNewGoodType(orgid int64) (goodsType []*models.NewGoodsType, err error) {
  3812. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3813. return goodsType, err
  3814. }
  3815. func GetDepartMentByName(name string, orgid int64) (*models.XtHisDepartment, error) {
  3816. var department models.XtHisDepartment
  3817. var err error
  3818. err = XTReadDB().Where("name= ? and user_org_id = ? and status = 1", name, orgid).Find(&department).Error
  3819. if err == gorm.ErrRecordNotFound {
  3820. return nil, err
  3821. }
  3822. if err != nil {
  3823. return nil, err
  3824. }
  3825. return &department, nil
  3826. }
  3827. func GetDataConfigIsExist(orgid int64, name string) (models.Dataconfig, error) {
  3828. dataconfig := models.Dataconfig{}
  3829. err := XTReadDB().Model(&dataconfig).Where("org_id = ? and name = ? and status = 1", orgid, name).Find(&dataconfig).Error
  3830. return dataconfig, err
  3831. }
  3832. func GetChildeConfigIsExist(parentid int64, name string, orgid int64) (*models.Dataconfig, error) {
  3833. var dataconfig models.Dataconfig
  3834. var err error
  3835. 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
  3836. if err == gorm.ErrRecordNotFound {
  3837. return nil, err
  3838. }
  3839. if err != nil {
  3840. return nil, err
  3841. }
  3842. return &dataconfig, nil
  3843. }
  3844. func GetLastChildeConfig(parentid int64, orgid int64) (models.Dataconfig, error) {
  3845. dataconfig := models.Dataconfig{}
  3846. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status =1", parentid, orgid).Last(&dataconfig).Error
  3847. return dataconfig, err
  3848. }
  3849. func CreateDataConfig(dataconfig *models.Dataconfig) error {
  3850. err := XTWriteDB().Create(&dataconfig).Error
  3851. return err
  3852. }
  3853. func FindAllDataConfigList(orgid int64, parentId int64) (dataconfig []*models.Dataconfig, err error) {
  3854. err = XTReadDB().Model(&dataconfig).Where("(org_id = ? or org_id = 0) and parent_id = ? and status = 1", orgid, parentId).Find(&dataconfig).Error
  3855. return dataconfig, err
  3856. }
  3857. func GetWarehouseOrderInfoList(orgid int64) (list []*models.WarehousingInfo, err error) {
  3858. err = XTReadDB().Model(&list).Where("org_id = ? and status = 1", orgid).Order("id asc").Group("good_id").Find(&list).Error
  3859. return list, err
  3860. }
  3861. func GetSearchDrugList(keywords string, orgid int64) (drug []*models.BaseDrugLib, err error) {
  3862. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3863. if len(keywords) > 0 {
  3864. likekey := "%" + keywords + "%"
  3865. err = db.Where("(drug_name LIKE ?)", likekey).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3866. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3867. }).Find(&drug).Error
  3868. } else {
  3869. err = db.Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3870. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3871. }).Find(&drug).Error
  3872. }
  3873. return
  3874. }
  3875. func GetSearchDrugListSix(keywords string, orgid int64, storehouse_id int64) (drug []*models.BaseDrugLib, err error) {
  3876. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3877. if len(keywords) > 0 {
  3878. likekey := "%" + keywords + "%"
  3879. err = db.Where("(drug_name LIKE ?)", likekey).Preload("LastDrugWarehouseInfo", "org_id=? and status = 1 and (stock_max_number > 0 or stock_min_number>0) and storehouse_id = ?", orgid, storehouse_id).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3880. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3881. }).Find(&drug).Error
  3882. } else {
  3883. 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 {
  3884. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3885. }).Find(&drug).Error
  3886. }
  3887. return
  3888. }
  3889. func GetSearchGoodList(keywords string, orgid int64) (good []*models.GoodInfo, err error) {
  3890. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 and good_status <> '停用'", orgid)
  3891. if len(keywords) > 0 {
  3892. likekey := "%" + keywords + "%"
  3893. err = db.Where("(good_name Like ?)", likekey).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3894. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3895. }).Find(&good).Error
  3896. } else {
  3897. err = db.Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3898. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3899. }).Find(&good).Error
  3900. }
  3901. return
  3902. }
  3903. func GetSearchGoodListSix(keywords string, orgid int64, storehouse_id int64) (good []*models.GoodInfoSevenThirty, err error) {
  3904. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 AND find_in_set('停用',good_status) = 0", orgid)
  3905. if len(keywords) > 0 {
  3906. likekey := "%" + keywords + "%"
  3907. 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 {
  3908. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id =?", orgid, storehouse_id)
  3909. }).Find(&good).Error
  3910. } else {
  3911. 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 {
  3912. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id)
  3913. }).Find(&good).Error
  3914. }
  3915. return
  3916. }
  3917. func CreateAutoReduceRecord(detail *models.BloodAutomaticReduceDetail) error {
  3918. db := writeDb.Begin()
  3919. err := db.Create(&detail).Error
  3920. if err != nil {
  3921. db.Rollback()
  3922. return err
  3923. }
  3924. db.Commit()
  3925. return err
  3926. }
  3927. func GetAutoMaticReduceDetail(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (*models.BloodAutomaticReduceDetail, error) {
  3928. var autoreduece models.BloodAutomaticReduceDetail
  3929. var err error
  3930. 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
  3931. if err == gorm.ErrRecordNotFound {
  3932. return nil, err
  3933. }
  3934. if err != nil {
  3935. return nil, err
  3936. }
  3937. return &autoreduece, nil
  3938. }
  3939. func GetAutoMaticReduceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (models.BloodAutomaticReduceDetail, error) {
  3940. auto := models.BloodAutomaticReduceDetail{}
  3941. 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
  3942. return auto, err
  3943. }
  3944. func DeleteAutoRedeceDetailTwo(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) error {
  3945. detail := models.BloodAutomaticReduceDetail{}
  3946. 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
  3947. return err
  3948. }
  3949. func GetWarehosueOutByStockFlow(patient_id int64, recrod_date int64, goodid int64) (stockflow []*models.VmStockFlow, err error) {
  3950. 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
  3951. return stockflow, err
  3952. }
  3953. func UpdateAutoGood(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64, count int64) (err error) {
  3954. 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
  3955. 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
  3956. 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
  3957. return err
  3958. }
  3959. func DeleteAutoRedeceDetailTen(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3960. detail := models.BloodAutomaticReduceDetail{}
  3961. 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
  3962. info := models.WarehouseOutInfo{}
  3963. 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
  3964. prepare := models.DialysisBeforePrepare{}
  3965. 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
  3966. flow := models.VmStockFlow{}
  3967. 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
  3968. return err
  3969. }
  3970. func DeleteAutoRedeceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3971. detail := models.BloodAutomaticReduceDetail{}
  3972. 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
  3973. info := models.WarehouseOutInfo{}
  3974. 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
  3975. prepare := models.DialysisBeforePrepare{}
  3976. 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
  3977. flow := models.VmStockFlow{}
  3978. 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
  3979. return err
  3980. }
  3981. func DeleteAutoRedeceDetailTenOne(orgid int64, patient_id int64, recordtime int64, goodid int64, order_id 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 = ? ", orgid, patient_id, recordtime, goodid).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3984. info := models.WarehouseOutInfo{}
  3985. 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
  3986. prepare := models.DialysisBeforePrepare{}
  3987. 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
  3988. flow := models.VmStockFlow{}
  3989. 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
  3990. return err
  3991. }
  3992. func UpDateWarehouseInfoByStockDelete(id int64, count int64, patient_id int64, record_time int64, goodid int64) (err error) {
  3993. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  3994. //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
  3995. return err
  3996. }
  3997. func GetCancelStockInfoByPatientId(patientid int64, goodid int64, record_date int64) (info []*models.CancelStockInfo, err error) {
  3998. err = XTReadDB().Where("patient_id = ? and good_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&info).Error
  3999. return info, err
  4000. }
  4001. func GetHisPrepscritionByPatientId(patientid int64, goodid int64, record_date int64) (list []*models.HisPrescriptionProject, err error) {
  4002. err = XTReadDB().Where("patient_id = ? and project_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&list).Error
  4003. return list, err
  4004. }
  4005. func UpDateWarehouStockFlowByStockDelete(id int64, recordtime int64, good_id int64, count int64, patient_id int64) (err error) {
  4006. 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
  4007. return err
  4008. }
  4009. func UpDateWarehouseInfoByStockDeleteOne(id int64, count int64) (err error) {
  4010. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  4011. return err
  4012. }
  4013. func GetAutoDialysisBefor(goodid int64, goodtypeid int64, patientid int64, orgid int64, recorddate int64) (*models.BloodAutomaticReduceDetail, error) {
  4014. var autoreduece models.BloodAutomaticReduceDetail
  4015. var err error
  4016. 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
  4017. if err == gorm.ErrRecordNotFound {
  4018. return nil, err
  4019. }
  4020. if err != nil {
  4021. return nil, err
  4022. }
  4023. return &autoreduece, nil
  4024. }
  4025. func GetAutoRecordByGoodIdSeven(goodid int64, patientid int64, recorddate int64) (models.WarehouseOutInfoSeven, error) {
  4026. detail := models.WarehouseOutInfoSeven{}
  4027. err := XTReadDB().Select("id,count").Where(" good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  4028. return detail, err
  4029. }
  4030. func GetAutoRecordByGoodIdSevenEight(goodid int64, patientid int64, recorddate int64) (detail []*models.WarehouseOutInfoSeven, err error) {
  4031. err = XTReadDB().Select("id,count").Where("good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  4032. return detail, err
  4033. }
  4034. func GetHisProjectPrescriptionByPatientId(patientid int64, recorddate int64) (project []*models.HisPrescriptionProject, err error) {
  4035. err = XTReadDB().Model(&project).Where("patient_id = ? and record_date = ? and status =1", patientid, recorddate).Find(&project).Error
  4036. return project, err
  4037. }
  4038. func GetHisProjectPrescriptionByPatientIdOne(id int64) (models.HisPrescriptionProject, error) {
  4039. project := models.HisPrescriptionProject{}
  4040. err = XTReadDB().Model(&project).Where("id = ? and status =1", id).Find(&project).Error
  4041. return project, err
  4042. }
  4043. func GetAllAutoRecordByPatient(patientid int64, recorddate int64) (auto []*models.WarehouseOutInfoNight, err error) {
  4044. 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
  4045. return auto, err
  4046. }
  4047. func ModefyWarehouseInfo(count int64, id int64) error {
  4048. err := XTWriteDB().Model(models.WarehousingInfo{}).Where(" id =? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  4049. return err
  4050. }
  4051. func DeleteAutoWarehouse(patient_id int64, record_time int64) error {
  4052. 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
  4053. 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
  4054. 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
  4055. //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
  4056. return err
  4057. }
  4058. func FindGoodInfoByIdTwo(id int64) (goodInfo models.GoodInfo, err error) {
  4059. err = readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  4060. return
  4061. }
  4062. func FindGoodInfoByIdSeven(id int64) (goodInfo models.GoodInfoSeven, err error) {
  4063. err = readDb.Select("id,good_name").Where("id = ? AND status = 1", id).First(&goodInfo).Error
  4064. return
  4065. }
  4066. func GetGoodWarehouseInfo(good_id int64) (info []*models.WarehousingInfo, err error) {
  4067. err = readDb.Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  4068. return info, err
  4069. }
  4070. func GetGoodWarehouseInfoSeven(good_id int64) (info []*models.WarehousingInfoSeven, err error) {
  4071. err = readDb.Select("stock_count").Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  4072. return info, err
  4073. }
  4074. func GetGoodWarehouseInfoSevenTen(good_id int64, storehouse_id int64) (info []*models.WarehousingInfoSeven, err error) {
  4075. 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
  4076. return info, err
  4077. }
  4078. func GetAllStockList(page int64, limit int64, startime int64, endtime int64, good_type int64, keyword string, orgid int64) (info []*models.VmWarehousingInfo, total int64, err error) {
  4079. offset := (page - 1) * limit
  4080. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1")
  4081. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4082. fmt.Println(table)
  4083. if startime > 0 {
  4084. db = db.Where("x.ctime>=?", startime)
  4085. }
  4086. if endtime > 0 {
  4087. db = db.Where("x.ctime<=?", endtime)
  4088. }
  4089. likeKey := "%" + keyword + "%"
  4090. if len(likeKey) > 0 {
  4091. db = db.Where("t.good_name like ?", likeKey)
  4092. }
  4093. if orgid > 0 {
  4094. db = db.Where("x.org_id = ?", orgid)
  4095. }
  4096. if good_type > 0 {
  4097. db = db.Where("t.good_type_id = ?", good_type)
  4098. }
  4099. 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
  4100. return info, total, err
  4101. }
  4102. 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) {
  4103. offset := (page - 1) * limit
  4104. db := XTReadDB().Table("xt_good_information as x").Where("x.status= 1")
  4105. likeKey := "%" + keyword + "%"
  4106. if len(likeKey) > 0 {
  4107. db = db.Where("x.good_name like ?", likeKey)
  4108. }
  4109. if orgid > 0 {
  4110. db = db.Where("x.org_id = ?", orgid)
  4111. }
  4112. if good_type > 0 {
  4113. db = db.Where("x.good_type_id = ?", good_type)
  4114. }
  4115. if good_id > 0 {
  4116. db = db.Where("x.id = ?", good_id)
  4117. }
  4118. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  4119. if startime > 0 {
  4120. db = db.Where("ctime>=?", startime)
  4121. }
  4122. if endtime > 0 {
  4123. db = db.Where("ctime<=?", endtime)
  4124. }
  4125. if good_id > 0 {
  4126. db = db.Where("good_id = ?", good_id)
  4127. }
  4128. if storehouse_id > 0 {
  4129. db = db.Where("storehouse_id = ?", storehouse_id)
  4130. }
  4131. return db.Where("status = 1 and is_check = 1")
  4132. }).Preload("WarehousingInfo", func(db *gorm.DB) *gorm.DB {
  4133. if startime > 0 {
  4134. db = db.Where("ctime>=?", startime)
  4135. }
  4136. if endtime > 0 {
  4137. db = db.Where("ctime<=?", endtime)
  4138. }
  4139. if good_id > 0 {
  4140. db = db.Where("good_id = ?", good_id)
  4141. }
  4142. if storehouse_id > 0 {
  4143. db = db.Where("storehouse_id = ?", storehouse_id)
  4144. }
  4145. return db.Where("status = 1").Group("good_id,storehouse_id")
  4146. }).Preload("CancelStockInfo", func(db *gorm.DB) *gorm.DB {
  4147. if startime > 0 {
  4148. db = db.Where("ctime>=?", startime)
  4149. }
  4150. if endtime > 0 {
  4151. db = db.Where("ctime<=?", endtime)
  4152. }
  4153. return db.Where("status = 1")
  4154. }).Find(&info).Error
  4155. return info, total, err
  4156. }
  4157. 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) {
  4158. offset := (page - 1) * limit
  4159. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1 and x.is_check = 1")
  4160. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4161. tables := XTReadDB().Table("xt_stock_flow as f").Where("f.status = 1 and f.consumable_type = 1 and f.user_org_id = ?", orgid)
  4162. fmt.Println(table, tables)
  4163. if startime > 0 {
  4164. db = db.Where("x.ctime>=?", startime)
  4165. }
  4166. if endtime > 0 {
  4167. db = db.Where("x.ctime<=?", endtime)
  4168. }
  4169. if startfirsttime > 0 {
  4170. db = db.Where("x.expiry_date >=?", startfirsttime)
  4171. }
  4172. if endfirsttime > 0 {
  4173. db = db.Where("x.expiry_date <=?", endfirsttime)
  4174. }
  4175. if orgid > 0 {
  4176. db = db.Where("x.org_id = ?", orgid)
  4177. }
  4178. if good_id > 0 {
  4179. db = db.Where("x.good_id = ?", good_id)
  4180. }
  4181. 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
  4182. return info, total, err
  4183. }
  4184. 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) {
  4185. offset := (page - 1) * limit
  4186. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status= 1")
  4187. if startime > 0 {
  4188. db = db.Where("x.ctime>=?", startime)
  4189. }
  4190. if endtime > 0 {
  4191. db = db.Where("x.ctime<=?", endtime)
  4192. }
  4193. if orgid > 0 {
  4194. db = db.Where("x.org_id = ?", orgid)
  4195. }
  4196. if good_id > 0 {
  4197. db = db.Where("x.good_id = ?", good_id)
  4198. }
  4199. if is_sys == 2 {
  4200. db = db.Where("x.is_sys = 1")
  4201. }
  4202. if is_sys == 3 {
  4203. db = db.Where("x.is_sys = 0")
  4204. }
  4205. 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
  4206. return info, total, err
  4207. }
  4208. func GetStockDrugCount(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  4209. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4210. if startime > 0 {
  4211. db = db.Where("x.ctime >=?", startime)
  4212. }
  4213. if endtime > 0 {
  4214. db = db.Where("x.ctime<=?", endtime)
  4215. }
  4216. if orgid > 0 {
  4217. db = db.Where("x.org_id = ?", orgid)
  4218. }
  4219. 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
  4220. return info, err
  4221. }
  4222. func GetAutoDiallysisBefor(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  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. err = db.Select("sum(x.count) as count,x.good_id").Where("is_sys = 0").Group("x.good_id").Scan(&info).Error
  4234. return info, err
  4235. }
  4236. func GetOutStockTotalCountFour(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  4237. if endtime > 0 {
  4238. 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
  4239. }
  4240. if endtime == 0 {
  4241. 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
  4242. }
  4243. return autoMatic, err
  4244. }
  4245. func GetGoodWarehouseOutById(id int64, orgid int64) (models.WarehouseOut, error) {
  4246. out := models.WarehouseOut{}
  4247. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", id, orgid).Find(&out).Error
  4248. return out, err
  4249. }
  4250. func GetOrderDetialByOrderId(id int64, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  4251. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  4252. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4253. fmt.Println(table)
  4254. if id > 0 {
  4255. db = db.Where("x.warehouse_out_id = ?", id)
  4256. }
  4257. if orgid > 0 {
  4258. db = db.Where("x.org_id = ?", orgid)
  4259. }
  4260. 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
  4261. return out, err
  4262. }
  4263. func GetWarehouseOutOrder(id []string, orgid int64) (out []*models.WarehouseOut, err error) {
  4264. db := XTReadDB().Table("xt_warehouse_out as x").Where("x.status = 1")
  4265. if len(id) > 0 {
  4266. db = db.Where("x.id in(?)", id)
  4267. }
  4268. if orgid > 0 {
  4269. db = db.Where("x.org_id= ?", orgid)
  4270. }
  4271. 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
  4272. return out, err
  4273. }
  4274. func GetOrderDetialByOrderIdOne(id []string, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  4275. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  4276. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4277. tableOne := XTReadDB().Table("xt_warehouse_info as n").Where("n.status = 1")
  4278. fmt.Println(table, tableOne)
  4279. if len(id) > 0 {
  4280. db = db.Where("x.warehouse_out_id in (?)", id)
  4281. }
  4282. if orgid > 0 {
  4283. db = db.Where("x.org_id = ?", orgid)
  4284. }
  4285. 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
  4286. return out, err
  4287. }
  4288. func GetOrderDetailStockFlowByStorehouseById(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  4289. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  4290. if len(id) > 0 {
  4291. db = db.Where("warehouse_out_id in (?)", id)
  4292. }
  4293. if orgid > 0 {
  4294. db = db.Where("user_org_id = ?", orgid)
  4295. }
  4296. err = db.Group("warehousing_id").Find(&list).Error
  4297. return list, err
  4298. }
  4299. func GetOrderDetailStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  4300. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  4301. if len(id) > 0 {
  4302. db = db.Where("warehouse_out_id in (?)", id)
  4303. }
  4304. if orgid > 0 {
  4305. db = db.Where("user_org_id = ?", orgid)
  4306. }
  4307. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  4308. return list, err
  4309. }
  4310. func GetOrderStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  4311. db := XTReadDB().Model(&list).Where("status = 1 and consumable_type = 7 and patient_id > 0 and count>0")
  4312. if len(id) > 0 {
  4313. db = db.Where("warehouse_out_id in (?)", id)
  4314. }
  4315. if orgid > 0 {
  4316. db = db.Where("user_org_id = ?", orgid)
  4317. }
  4318. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  4319. return list, err
  4320. }
  4321. func GetLastWarehouseOutInfo(ids []string, orgId int64) (info []*models.WarehouseOutInfoSix, err error) {
  4322. db := XTReadDB().Model(&info)
  4323. db = db.Preload("GoodInfo", "status = 1")
  4324. 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
  4325. return info, err
  4326. }
  4327. func FindUserDetailByIdOne(good_id int64, record_time int64, org_id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  4328. db := readDb.Model(&models.AutomaticReduceDetail{})
  4329. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  4330. db = db.Preload("GoodsType", "status = 1")
  4331. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  4332. db = db.Preload("WarehouseOutInfo", "org_id = ? and status = 1 and good_id = ? and sys_record_time = ?", org_id, good_id, record_time)
  4333. db = db.Where("status = 1 AND org_id = ? AND good_id = ? AND record_time = ? and count<> 0 ", org_id, good_id, record_time)
  4334. db = db.Count(&total)
  4335. err = db.Find(&user).Error
  4336. return
  4337. }
  4338. func GetGoodInformationByGoodId(good_id int64) (models.GoodInfo, error) {
  4339. info := models.GoodInfo{}
  4340. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  4341. return info, err
  4342. }
  4343. func GetNewGoodInformationByGoodId(good_id int64, tx *gorm.DB) (models.GoodInfo, error) {
  4344. info := models.GoodInfo{}
  4345. err := tx.Where("id = ? and status = 1", good_id).Find(&info).Error
  4346. if err != gorm.ErrRecordNotFound {
  4347. if err != nil {
  4348. tx.Rollback()
  4349. return info, err
  4350. }
  4351. }
  4352. return info, err
  4353. }
  4354. func GetGoodInformationByGoodIdThirty(good_id int64) (models.GoodInformationThirty, error) {
  4355. info := models.GoodInformationThirty{}
  4356. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  4357. return info, err
  4358. }
  4359. func GetNewGoodInformationByGoodIdThirty(good_id int64, tx *gorm.DB) (models.GoodInformationThirty, error) {
  4360. info := models.GoodInformationThirty{}
  4361. err := tx.Where("id = ? and status = 1", good_id).Find(&info).Error
  4362. if err != gorm.ErrRecordNotFound {
  4363. if err != nil {
  4364. tx.Rollback()
  4365. return info, err
  4366. }
  4367. }
  4368. return info, err
  4369. }
  4370. func UpdateGoodInfoList(info models.GoodInfo, id int64) error {
  4371. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"total": info.Total}).Error
  4372. return err
  4373. }
  4374. func GetExprotStockList(orgid int64, orderid []string, startime int64, endtime int64) (info []*models.StWarehousingInfo, err error) {
  4375. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4376. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4377. fmt.Println(table)
  4378. if orgid > 0 {
  4379. db = db.Where("x.org_id = ?", orgid)
  4380. }
  4381. if len(orderid) > 0 {
  4382. db = db.Where("x.warehousing_id in(?)", orderid)
  4383. }
  4384. if startime > 0 {
  4385. db = db.Where("x.ctime>=?", startime)
  4386. }
  4387. if endtime > 0 {
  4388. db = db.Where("x.ctime<=?", endtime)
  4389. }
  4390. 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
  4391. return info, err
  4392. }
  4393. func GetExprotStockListTwenty(orgid int64, orderid []string, startime int64, endtime int64, storehouse_id int64) (info []*models.StWarehousingInfo, err error) {
  4394. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4395. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4396. fmt.Println(table)
  4397. if orgid > 0 {
  4398. db = db.Where("x.org_id = ?", orgid)
  4399. }
  4400. if len(orderid) > 0 {
  4401. db = db.Where("x.warehousing_id in(?)", orderid)
  4402. }
  4403. if startime > 0 {
  4404. db = db.Where("x.ctime>=?", startime)
  4405. }
  4406. if endtime > 0 {
  4407. db = db.Where("x.ctime<=?", endtime)
  4408. }
  4409. if storehouse_id > 0 {
  4410. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4411. }
  4412. 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
  4413. return info, err
  4414. }
  4415. func GetDrugWarehouOrderInfo(startime int64, endtime int64, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  4416. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4417. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  4418. fmt.Println(table)
  4419. if startime > 0 {
  4420. db = db.Where("x.ctime>=?", startime)
  4421. }
  4422. if endtime > 0 {
  4423. db = db.Where("x.ctime<=?", endtime)
  4424. }
  4425. if orgid > 0 {
  4426. db = db.Where("x.org_id = ?", orgid)
  4427. }
  4428. 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
  4429. return info, err
  4430. }
  4431. func GetOutExprotList(orgid int64, ids []string, startime int64, endtime int64) (out []*models.WarehouseOutInfoOne, err error) {
  4432. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1 ")
  4433. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4434. tableOne := XTReadDB().Table("xt_warehouse_out as p").Where("p.status=1")
  4435. fmt.Println(table, tableOne)
  4436. if orgid > 0 {
  4437. db = db.Where("x.org_id = ?", orgid)
  4438. }
  4439. if orgid > 0 {
  4440. db = db.Where("t.org_id = ?", orgid)
  4441. }
  4442. if orgid > 0 {
  4443. db = db.Where("p.org_id = ?", orgid)
  4444. }
  4445. if len(ids) > 0 {
  4446. db = db.Where("x.warehouse_out_id in (?)", ids)
  4447. }
  4448. if startime > 0 {
  4449. db = db.Where("x.ctime>=?", startime)
  4450. }
  4451. if endtime > 0 {
  4452. db = db.Where("x.ctime<=?", endtime)
  4453. }
  4454. 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
  4455. return out, err
  4456. }
  4457. func FindeDrugWarehouseOutOrder(ids []string, orgid int64) (info []*models.DrugWarehouseOut, err error) {
  4458. db := XTReadDB().Table("xt_drug_warehouse_out as x").Where("x.status = 1")
  4459. if len(ids) > 0 {
  4460. db = db.Where("x.id in(?)", ids)
  4461. }
  4462. if orgid > 0 {
  4463. db = db.Where("x.org_id = ?", orgid)
  4464. }
  4465. 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
  4466. return info, err
  4467. }
  4468. func FindeDrugWarehouseOutDetail(ids []string, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  4469. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status =1 ")
  4470. table := XTReadDB().Table("x.base_drug as t").Where("t.status = 1")
  4471. fmt.Println(table)
  4472. if len(ids) > 0 {
  4473. db = db.Where("x.warehouse_out_id in(?)", ids)
  4474. }
  4475. if orgid > 0 {
  4476. db = db.Where("x.org_id = ?", orgid)
  4477. }
  4478. 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
  4479. return info, err
  4480. }
  4481. func GetDrugNameByKeyword(keyword string, orgid int64) (basedrug []*models.XtBaseDrug, err error) {
  4482. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1")
  4483. likeKey := "%" + keyword + "%"
  4484. if len(keyword) > 0 {
  4485. db = db.Where("x.drug_name like ?", likeKey)
  4486. }
  4487. if orgid > 0 {
  4488. db = db.Where("x.org_id = ?", orgid)
  4489. }
  4490. err = db.Find(&basedrug).Error
  4491. return basedrug, err
  4492. }
  4493. func GetDrugWarehouseOrderDetail(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  4494. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4495. if len(ids) > 0 {
  4496. db = db.Where("x.drug_id in(?)", ids)
  4497. }
  4498. if orgid > 0 {
  4499. db = db.Where("x.org_id = ?", orgid)
  4500. }
  4501. if startime > 0 {
  4502. db = db.Where("x.ctime>=?", startime)
  4503. }
  4504. if endtime > 0 {
  4505. db = db.Where("x.ctime<=?", endtime)
  4506. }
  4507. if storehouse_id > 0 {
  4508. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4509. }
  4510. err = db.Find(&info).Error
  4511. return info, err
  4512. }
  4513. func GetDrugWarehouseOrderDetailTwo(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  4514. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4515. if len(ids) > 0 {
  4516. db = db.Where("x.drug_id in(?)", ids)
  4517. }
  4518. if orgid > 0 {
  4519. db = db.Where("x.org_id = ?", orgid)
  4520. }
  4521. if startime > 0 {
  4522. db = db.Where("x.ctime>=?", startime)
  4523. }
  4524. if endtime > 0 {
  4525. db = db.Where("x.ctime<=?", endtime)
  4526. }
  4527. if storehouse_id > 0 {
  4528. db = db.Where("storehouse_id = ?", storehouse_id)
  4529. }
  4530. err = db.Find(&info).Error
  4531. return info, err
  4532. }
  4533. func GetDrugTotalCount(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4534. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  4535. if drugid > 0 {
  4536. db = db.Where("x.drug_id = ?", drugid)
  4537. }
  4538. if orgid > 0 {
  4539. db = db.Where("x.org_id = ?", orgid)
  4540. }
  4541. if storehouse_id > 0 {
  4542. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4543. }
  4544. 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
  4545. return info, err
  4546. }
  4547. func GetDrugTotalCountTwoTwety(drugid int64, orgid int64, storehouse_id int64, tx *gorm.DB) (info models.VmDrugWarehouseInfo, err error) {
  4548. db := tx.Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  4549. if drugid > 0 {
  4550. db = db.Where("x.drug_id = ?", drugid)
  4551. }
  4552. if orgid > 0 {
  4553. db = db.Where("x.org_id = ?", orgid)
  4554. }
  4555. if storehouse_id > 0 {
  4556. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4557. }
  4558. 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
  4559. if err != gorm.ErrRecordNotFound {
  4560. if err != nil {
  4561. tx.Rollback()
  4562. return info, err
  4563. }
  4564. }
  4565. return info, err
  4566. }
  4567. func GetDrugTotalCountTwenty(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4568. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1 ")
  4569. if drugid > 0 {
  4570. db = db.Where("x.drug_id = ?", drugid)
  4571. }
  4572. if orgid > 0 {
  4573. db = db.Where("x.org_id = ?", orgid)
  4574. }
  4575. if storehouse_id > 0 {
  4576. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4577. }
  4578. 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
  4579. return info, err
  4580. }
  4581. func GetDrugTotalCountSwx(id int64, orgid int64) (info models.VmDrugWarehouseInfo, err error) {
  4582. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4583. if id > 0 {
  4584. db = db.Where("x.id = ?", id)
  4585. }
  4586. if orgid > 0 {
  4587. db = db.Where("x.org_id = ?", orgid)
  4588. }
  4589. 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
  4590. return info, err
  4591. }
  4592. func GetDrugTotalCountOne(drugid int64, orgid int64, id int64) (models.VmDrugWarehouseInfo, error) {
  4593. info := models.VmDrugWarehouseInfo{}
  4594. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4595. if id == 0 {
  4596. db = db.Where("id = ?", id)
  4597. }
  4598. if drugid > 0 {
  4599. db = db.Where("x.drug_id = ?", drugid)
  4600. }
  4601. if orgid > 0 {
  4602. db = db.Where("x.org_id = ?", orgid)
  4603. }
  4604. err = db.Select("x.drug_id,x.stock_max_number,x.stock_min_number,x.max_unit,x.min_unit").Find(&info).Error
  4605. return info, err
  4606. }
  4607. func GetDrugTotalCountTwo(drugid int64, storehouse_id int64) (list []*models.VmDrugWarehouseInfo, err error) {
  4608. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4609. table := XTReadDB().Table("xt_base_drug as d").Where("d.status = 1")
  4610. fmt.Println(table)
  4611. if drugid > 0 {
  4612. db = db.Where("x.drug_id = ?", drugid)
  4613. }
  4614. if storehouse_id > 0 {
  4615. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4616. }
  4617. 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
  4618. return list, err
  4619. }
  4620. func GetDrugByGoodId(drugid int64) (models.BaseDrugLib, error) {
  4621. lib := models.BaseDrugLib{}
  4622. err := XTReadDB().Model(&lib).Where("id = ? and status = 1", drugid).Find(&lib).Error
  4623. return lib, err
  4624. }
  4625. func GetWarehoureOrderInfoList(goodid int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4626. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4627. if goodid > 0 {
  4628. db = db.Where("x.good_id = ?", goodid)
  4629. }
  4630. if orgid > 0 {
  4631. db = db.Where("x.org_id = ?", orgid)
  4632. }
  4633. 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
  4634. return info, err
  4635. }
  4636. func UpdateWarehousingCount(info *models.WarehousingInfo, goodid int64, orgid int64, warehousingId int64) error {
  4637. 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
  4638. return err
  4639. }
  4640. func GetSingleCancelOrder(id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  4641. err = XTReadDB().Where("cancel_stock_id = ? and status = 1 and org_id = ?", id, orgid).Preload("GoodInfo", "status = 1").Find(&info).Error
  4642. return info, err
  4643. }
  4644. func UpdateCancelOut(list []*models.WarehousingInfo, count int64) error {
  4645. //遍历循环入库数据
  4646. for i, item := range list {
  4647. //计算每个批次剩余库存与入库数据的差 然后和退库数量比较
  4648. if (item.WarehousingCount - item.StockCount) >= count {
  4649. //如果差距大于退库数量,则直接把退库数量增加到该批次中
  4650. info := models.WarehousingInfo{
  4651. GoodId: item.GoodId,
  4652. OrgId: item.OrgId,
  4653. WarehousingId: item.WarehousingId,
  4654. StockCount: item.StockCount + count,
  4655. }
  4656. err2 := UpdateWarehousingCount(&info, item.GoodId, item.OrgId, item.WarehousingId)
  4657. fmt.Println("2323233232323232323232323232323", err2)
  4658. return err
  4659. }
  4660. if (item.WarehousingCount - item.StockCount) < count {
  4661. //如果该批次小于退库数量,则先出库该相差数量
  4662. var countOne = item.WarehousingCount - item.StockCount
  4663. infoOne := models.WarehousingInfo{
  4664. GoodId: item.GoodId,
  4665. OrgId: item.OrgId,
  4666. WarehousingId: item.WarehousingId,
  4667. StockCount: countOne + item.StockCount,
  4668. }
  4669. err := UpdateWarehousingCount(&infoOne, item.GoodId, item.OrgId, item.WarehousingId)
  4670. if err == nil {
  4671. //剩余的出库到第二个批次
  4672. var countTwo = count - countOne
  4673. infos := append(list, list[i+1])
  4674. var idArray []*models.WarehousingInfo
  4675. for _, it := range infos {
  4676. if it.WarehousingId != item.WarehousingId {
  4677. idArray = append(idArray, it)
  4678. }
  4679. }
  4680. //查询更新后的库存
  4681. UpdateCancelOut(idArray, countTwo)
  4682. return err
  4683. } else {
  4684. return err
  4685. }
  4686. }
  4687. }
  4688. return err
  4689. }
  4690. func GetCancelStockOrderPrint(idstr []string) (info []*models.CancelStockInfo, err error) {
  4691. if len(idstr) > 0 {
  4692. err = XTReadDB().Model(&info).Where("cancel_stock_id in(?)", idstr).Preload("GoodInfo", "status = 1").Find(&info).Error
  4693. } else {
  4694. err = XTReadDB().Model(&info).Preload("GoodInfo", "status = 1").Find(&info).Error
  4695. }
  4696. return info, err
  4697. }
  4698. func GetCancelStockOrderPrintOne(idstr []string, orgid int64) (info []*models.CancelStock, err error) {
  4699. if len(idstr) > 0 {
  4700. err = XTReadDB().Model(&info).Where("id in(?)", idstr).
  4701. Preload("XtCancelStockInfo", func(db *gorm.DB) *gorm.DB {
  4702. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodInfo", "status = 1 and org_id = ?", orgid)
  4703. }).Find(&info).Error
  4704. }
  4705. return info, err
  4706. }
  4707. func GetCancelOutTotalCount(startime int64, endtime int64, orgid int64) (info []*models.VmCancelStockInfo, err error) {
  4708. db := XTReadDB().Table("xt_cancel_stock_info as x").Where("x.status = 1")
  4709. if startime > 0 {
  4710. db = db.Where("x.ctime >=?", startime)
  4711. }
  4712. if endtime > 0 {
  4713. db = db.Where("x.ctime <= ?", endtime)
  4714. }
  4715. if orgid > 0 {
  4716. db = db.Where("x.org_id = ?", orgid)
  4717. }
  4718. err = db.Select("x.good_id,sum(x.count) as count").Group("x.good_id").Scan(&info).Error
  4719. return info, err
  4720. }
  4721. func GetWarehoureOutOrderInfo(goodid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  4722. info := models.WarehouseOutInfo{}
  4723. var err error
  4724. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1", goodid, orgid).Find(&info).Error
  4725. if err == gorm.ErrRecordNotFound {
  4726. return nil, err
  4727. }
  4728. if err != nil {
  4729. return nil, err
  4730. }
  4731. return &info, nil
  4732. }
  4733. func GetWarehoureOrderInfoCount(goodid int64, orgid int64, id int64) (info models.WarehouseOutInfo, err error) {
  4734. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  4735. if goodid > 0 {
  4736. db = db.Where("x.good_id = ?", goodid)
  4737. }
  4738. if orgid > 0 {
  4739. db = db.Where("x.org_id = ?", orgid)
  4740. }
  4741. if id > 0 {
  4742. db = db.Where("x.warehouse_info_id = ?", id)
  4743. }
  4744. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4745. return info, err
  4746. }
  4747. func GetWarehoureOrderStockOutFlow(goodid int64, orgid int64, id int64, storehouse_id int64) (info models.VmStockFlow, err error) {
  4748. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status =1 and x.consumable_type = 2 or x.consumable_type = 3")
  4749. if goodid > 0 {
  4750. db = db.Where("x.good_id = ?", goodid)
  4751. }
  4752. if orgid > 0 {
  4753. db = db.Where("x.user_org_id = ?", orgid)
  4754. }
  4755. if id > 0 {
  4756. db = db.Where("x.warehousing_id = ?", id)
  4757. }
  4758. if storehouse_id > 0 {
  4759. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4760. }
  4761. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4762. return info, err
  4763. }
  4764. func UpdateCancelInfo(id int64) (models.CancelStock, error) {
  4765. stock := models.CancelStock{}
  4766. err := XTWriteDB().Model(&stock).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4767. return stock, err
  4768. }
  4769. func GetStockBatchNumber(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4770. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1 and number <> ''", id, orgid).Find(&info).Error
  4771. return info, err
  4772. }
  4773. func GetWarehouseOutInfoById(id int64, goodid int64) (info []*models.WarehouseOutInfo, err error) {
  4774. err = XTReadDB().Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  4775. return info, err
  4776. }
  4777. func GetStockFlowBatchNumber(id int64, goodid int64) (stock []*models.VmStockFlow, err error) {
  4778. err = XTReadDB().Model(&stock).Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Group("patient_id,number").Find(&stock).Error
  4779. return stock, err
  4780. }
  4781. func GetStockFlowBatchNumberOne(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4782. 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)")
  4783. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4784. fmt.Println(table)
  4785. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4786. fmt.Println(tableone)
  4787. if id > 0 {
  4788. db = db.Where("x.warehouse_out_id = ?", id)
  4789. }
  4790. if goodid > 0 {
  4791. db = db.Where("x.good_id = ?", goodid)
  4792. }
  4793. 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
  4794. return stock, err
  4795. }
  4796. func GetStockFlowCancelInfo(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4797. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status = 1 and x.count > 0 and x.consumable_type = 7")
  4798. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4799. fmt.Println(table)
  4800. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4801. fmt.Println(tableone)
  4802. if id > 0 {
  4803. db = db.Where("x.warehouse_out_id = ?", id)
  4804. }
  4805. if goodid > 0 {
  4806. db = db.Where("x.good_id = ?", goodid)
  4807. }
  4808. 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
  4809. return stock, err
  4810. }
  4811. func GetDrugBatchNumber(drug_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  4812. err = XTReadDB().Model(&info).Where("drug_id = ? and org_id = ? and status = 1 and batch_number <>''", drug_id, orgid).Find(&info).Error
  4813. return info, err
  4814. }
  4815. func ExportDrugList(warehouse_out_id []string, org_id int64) (out []*models.StDrugWarehouseInfo, err error) {
  4816. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status =1 AND x.org_id=?", org_id)
  4817. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1 and b.org_id = ?", org_id)
  4818. tableOne := XTReadDB().Table("xt_drug_warehouse as p").Where("p.status=1 and p.org_id=?", org_id)
  4819. fmt.Println("table", table)
  4820. fmt.Println("tableOne", tableOne)
  4821. if len(warehouse_out_id) > 0 {
  4822. db = db.Where("x.warehousing_id in(?)", warehouse_out_id)
  4823. }
  4824. 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
  4825. return out, err
  4826. }
  4827. func GetExportOutOrderDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4828. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4829. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  4830. fmt.Println("table", table)
  4831. if len(warehouse_out_id) > 0 {
  4832. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4833. }
  4834. 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
  4835. return out, err
  4836. }
  4837. func GetExportOutOrderDrugListOne(warehouse_out_id []string, org_id int64) (out []*models.StDrugWarehouseOutInfo, err error) {
  4838. db := XTReadDB().Table("xt_drug_flow as x").Where("x.status = 1 and x.user_org_id =?", org_id)
  4839. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1 and b.org_id = ?", org_id)
  4840. fmt.Println("table", table)
  4841. if len(warehouse_out_id) > 0 {
  4842. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4843. }
  4844. 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
  4845. return out, err
  4846. }
  4847. func GetDrugOutListFlow(warehouse_out_id []string, orgid int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  4848. db := XTReadDB().Model(&info).Where("status = 1")
  4849. if len(warehouse_out_id) > 0 {
  4850. db = db.Where("warehouse_out_id in(?)", warehouse_out_id)
  4851. }
  4852. if orgid > 0 {
  4853. db = db.Where("org_id = ?", orgid)
  4854. }
  4855. err = db.Find(&info).Error
  4856. return info, err
  4857. }
  4858. func GetDrugWarehouseOut(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4859. err = XTReadDB().Where("warehouse_out_id in(?)", warehouse_out_id).Find(&out).Error
  4860. return out, err
  4861. }
  4862. func GetDrugWarehouseOutTenty(warehouse_out_id []string, orgid int64) (out []*models.DrugWarehouseOutTwenty, err error) {
  4863. err = XTReadDB().Where("id in(?)", warehouse_out_id).Preload("DrugWarehouseOutInfoTenty", func(db *gorm.DB) *gorm.DB {
  4864. 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)
  4865. }).Find(&out).Error
  4866. return out, err
  4867. }
  4868. func GetLastGoodInformationByGoodId(goodid int64, warehouse_info_id int64, orgid_id int64) (models.WarehouseOutInfo, error) {
  4869. info := models.WarehouseOutInfo{}
  4870. err := XTReadDB().Where("good_id = ? and warehouse_info_id= ? and status =1", goodid, warehouse_info_id).Last(&info).Error
  4871. return info, err
  4872. }
  4873. func GetTotalCountByGoodId(goodid int64, storehouse_id int64) (models.WarehousingInfo, error) {
  4874. info := models.WarehousingInfo{}
  4875. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4876. if goodid > 0 {
  4877. db = db.Where("x.good_id = ?", goodid)
  4878. }
  4879. if storehouse_id > 0 {
  4880. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4881. }
  4882. err := db.Select("x.good_id,sum(x.stock_count) as stock_count").Scan(&info).Error
  4883. return info, err
  4884. }
  4885. func GetLastGoodCountById(goodid int64, warehouse_info_id int64) (models.WarehousingInfo, error) {
  4886. info := models.WarehousingInfo{}
  4887. err := XTReadDB().Where("good_id = ? and id= ? and status = 1", goodid, warehouse_info_id).First(&info).Error
  4888. return info, err
  4889. }
  4890. func UpdateWarehousingInfo(info models.WarehousingInfo, id int64) error {
  4891. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  4892. return err
  4893. }
  4894. func FindFirstWarehousingInfoByGoodId(good_id int64, id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  4895. if storehouse_id == 0 {
  4896. err = readDb.Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime asc").First(&info).Error
  4897. }
  4898. if storehouse_id > 0 {
  4899. 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
  4900. }
  4901. return info, err
  4902. }
  4903. func CreateStockFlow(warehousingInfo []*models.VmStockFlow) (err error) {
  4904. if len(warehousingInfo) > 0 {
  4905. utx := writeDb.Begin()
  4906. if len(warehousingInfo) > 0 {
  4907. 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 "
  4908. insertParams := make([]string, 0)
  4909. insertData := make([]interface{}, 0)
  4910. for _, info := range warehousingInfo {
  4911. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  4912. insertData = append(insertData, info.WarehousingId)
  4913. insertData = append(insertData, info.GoodId)
  4914. insertData = append(insertData, info.Number)
  4915. insertData = append(insertData, info.LicenseNumber)
  4916. insertData = append(insertData, info.Count)
  4917. insertData = append(insertData, info.UserOrgId)
  4918. insertData = append(insertData, info.PatientId)
  4919. insertData = append(insertData, info.SystemTime)
  4920. insertData = append(insertData, info.ConsumableType)
  4921. insertData = append(insertData, info.IsSys)
  4922. insertData = append(insertData, info.WarehousingOrder)
  4923. insertData = append(insertData, info.WarehouseOutId)
  4924. insertData = append(insertData, info.WarehouseOutOrderNumber)
  4925. insertData = append(insertData, info.IsEdit)
  4926. insertData = append(insertData, info.CancelStockId)
  4927. insertData = append(insertData, info.CancelOrderNumber)
  4928. insertData = append(insertData, info.Manufacturer)
  4929. insertData = append(insertData, info.Dealer)
  4930. insertData = append(insertData, info.Creator)
  4931. insertData = append(insertData, info.UpdateCreator)
  4932. insertData = append(insertData, info.Status)
  4933. insertData = append(insertData, info.Ctime)
  4934. insertData = append(insertData, info.Mtime)
  4935. insertData = append(insertData, info.Price)
  4936. insertData = append(insertData, info.WarehousingDetailId)
  4937. insertData = append(insertData, info.WarehouseOutDetailId)
  4938. insertData = append(insertData, info.CancelOutDetailId)
  4939. insertData = append(insertData, info.ExpireDate)
  4940. insertData = append(insertData, info.ProductDate)
  4941. }
  4942. thisSQL += strings.Join(insertParams, ", ")
  4943. err = utx.Exec(thisSQL, insertData...).Error
  4944. if err != nil {
  4945. utx.Rollback()
  4946. return
  4947. }
  4948. }
  4949. utx.Commit()
  4950. }
  4951. return
  4952. }
  4953. func CreateStockFlowOne(flow models.VmStockFlow) error {
  4954. utx := XTWriteDB().Begin()
  4955. err := utx.Create(&flow).Error
  4956. if err != nil {
  4957. utx.Rollback()
  4958. return err
  4959. }
  4960. utx.Commit()
  4961. return err
  4962. }
  4963. func CreateNewStockFlowOne(flow models.VmStockFlow, tx *gorm.DB) error {
  4964. err := tx.Create(&flow).Error
  4965. if err != nil {
  4966. tx.Rollback()
  4967. return err
  4968. }
  4969. return err
  4970. }
  4971. func CreateOutInfoLog(log models.XtGoodWarehouseOutInfoLog, tx *gorm.DB) error {
  4972. err := tx.Create(&log).Error
  4973. if err != nil {
  4974. tx.Rollback()
  4975. return err
  4976. }
  4977. return err
  4978. }
  4979. func CreateGoodWarehouseInfoLog(log models.XtGoodWarehouseInfoLog, tx *gorm.DB) error {
  4980. err := tx.Create(&log).Error
  4981. if err != nil {
  4982. tx.Rollback()
  4983. return err
  4984. }
  4985. return err
  4986. }
  4987. func CreateStockFlowSix(flow *models.VmStockFlow) error {
  4988. err := XTWriteDB().Create(&flow).Error
  4989. return err
  4990. }
  4991. func UpdatedStockFlow(flow models.VmStockFlow) error {
  4992. err := XTWriteDB().Save(&flow).Error
  4993. return err
  4994. }
  4995. func UpdatedStockFlowOne(flow models.VmStockFlow, warehousing_id int64, patient_id int64, record_time int64, good_id int64) error {
  4996. 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
  4997. return err
  4998. }
  4999. func UpdatedStockFlowFive(flow models.VmStockFlow, warehouse_out_id int64, patient_id int64, record_time int64, good_id int64) error {
  5000. 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
  5001. return err
  5002. }
  5003. func GetStockFlowIsExsit(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (*models.VmStockFlow, error) {
  5004. stock := models.VmStockFlow{}
  5005. var err error
  5006. 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
  5007. if err == gorm.ErrRecordNotFound {
  5008. return nil, err
  5009. }
  5010. if err != nil {
  5011. return nil, err
  5012. }
  5013. return &stock, nil
  5014. }
  5015. func GetStockFlowIsBatchNumber(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  5016. 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
  5017. return flow, nil
  5018. }
  5019. func GetStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  5020. 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
  5021. return flow, nil
  5022. }
  5023. func GetNewStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  5024. 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
  5025. if err != gorm.ErrRecordNotFound {
  5026. if err != nil {
  5027. tx.Rollback()
  5028. return flow, nil
  5029. }
  5030. }
  5031. return flow, nil
  5032. }
  5033. func GetStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.WarehouseOutInfo, err error) {
  5034. 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
  5035. return flow, nil
  5036. }
  5037. func GetNewStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.WarehouseOutInfo, err error) {
  5038. 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
  5039. if err != gorm.ErrRecordNotFound {
  5040. if err != nil {
  5041. tx.Rollback()
  5042. return flow, nil
  5043. }
  5044. }
  5045. return flow, nil
  5046. }
  5047. func GetStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.AutomaticReduceDetail, err error) {
  5048. 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
  5049. return flow, nil
  5050. }
  5051. func GetNewStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.AutomaticReduceDetail, err error) {
  5052. 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
  5053. if err != gorm.ErrRecordNotFound {
  5054. if err != nil {
  5055. tx.Rollback()
  5056. return flow, err
  5057. }
  5058. }
  5059. return flow, err
  5060. }
  5061. func GetStockFlowIsBatchNumberTwo(patient_id int64, record_time int64, good_id int64) (flow []*models.WarehouseOutInfo, err error) {
  5062. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 ", patient_id, record_time, good_id).Find(&flow).Error
  5063. return flow, nil
  5064. }
  5065. func GetStockFlowIsBatchNumberThree(patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  5066. 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
  5067. return flow, nil
  5068. }
  5069. func GetStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  5070. 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
  5071. return flow, nil
  5072. }
  5073. func GetNewStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  5074. 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
  5075. if err != gorm.ErrRecordNotFound {
  5076. if err != nil {
  5077. tx.Rollback()
  5078. return flow, nil
  5079. }
  5080. }
  5081. return flow, nil
  5082. }
  5083. func GetStockFlowIsBatchNumberFourty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  5084. 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
  5085. return flow, nil
  5086. }
  5087. func GetWarehouseOutInfoIsExist(id int64) (*models.WarehouseOutInfo, error) {
  5088. info := models.WarehouseOutInfo{}
  5089. var err error
  5090. err = XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5091. if err == gorm.ErrRecordNotFound {
  5092. return nil, err
  5093. }
  5094. if err != nil {
  5095. return nil, err
  5096. }
  5097. return &info, nil
  5098. }
  5099. func GetWarehouseOutInfoIsExistOne(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  5100. info := models.WarehouseOutInfo{}
  5101. var err error
  5102. 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
  5103. if err == gorm.ErrRecordNotFound {
  5104. return nil, err
  5105. }
  5106. if err != nil {
  5107. return nil, err
  5108. }
  5109. return &info, nil
  5110. }
  5111. func GetWarehouseOutInfoIsExistTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  5112. info := models.WarehouseOutInfo{}
  5113. 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
  5114. return info, err
  5115. }
  5116. 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) {
  5117. offset := (page - 1) * limit
  5118. db := XTReadDB().Model(&flow).Where("status = 1 and count > 0")
  5119. if consumable_type > 0 {
  5120. //入库
  5121. if consumable_type == 1 {
  5122. db = db.Where("consumable_type = ?", consumable_type)
  5123. }
  5124. //2 手动出库
  5125. if consumable_type == 2 {
  5126. db = db.Where(" consumable_type = ? and is_sys = 0", consumable_type)
  5127. }
  5128. //3.自动出库
  5129. if consumable_type == 3 {
  5130. db = db.Where(" consumable_type = ? and is_sys = 1", consumable_type)
  5131. }
  5132. //4.手动退库
  5133. if consumable_type == 4 {
  5134. db = db.Where(" consumable_type = ?", consumable_type)
  5135. }
  5136. //自动退库
  5137. if consumable_type == 7 {
  5138. db = db.Where(" consumable_type = ?", consumable_type)
  5139. }
  5140. if consumable_type == 10 {
  5141. db = db.Where(" consumable_type = ?", consumable_type)
  5142. }
  5143. if consumable_type == 11 {
  5144. db = db.Where(" consumable_type = ?", consumable_type)
  5145. }
  5146. if consumable_type == 12 {
  5147. db = db.Where(" consumable_type = ?", consumable_type)
  5148. }
  5149. if consumable_type == 13 {
  5150. db = db.Where(" consumable_type = ?", consumable_type)
  5151. }
  5152. if consumable_type == 15 {
  5153. db = db.Where(" consumable_type = ?", consumable_type)
  5154. }
  5155. }
  5156. if orgId > 0 {
  5157. db = db.Where("user_org_id = ?", orgId)
  5158. }
  5159. if startTime > 0 {
  5160. db = db.Where("ctime >= ?", startTime)
  5161. }
  5162. if endTime > 0 {
  5163. db = db.Where("ctime<=?", endTime)
  5164. }
  5165. if good_id > 0 {
  5166. db = db.Where("good_id = ?", good_id)
  5167. }
  5168. err = db.Count(&total).Offset(offset).Limit(limit).Order("id desc").Find(&flow).Error
  5169. return flow, total, err
  5170. }
  5171. func GetHidsDoctorAdvice(good_id int64, user_org_id int64, patient_id int64, record_date int64) (models.HisPrescriptionProject, error) {
  5172. project := models.HisPrescriptionProject{}
  5173. 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
  5174. return project, err
  5175. }
  5176. func GetLastGoodListByPatientId(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.AutomaticReduceDetail, error) {
  5177. detail := models.AutomaticReduceDetail{}
  5178. 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
  5179. return detail, err
  5180. }
  5181. func GetLastGoodListByPatientIdOne(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.WarehouseOutInfo, error) {
  5182. detail := models.WarehouseOutInfo{}
  5183. 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
  5184. return detail, err
  5185. }
  5186. func UpdatedWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64) error {
  5187. outInfo := models.WarehouseOutInfo{}
  5188. 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
  5189. return err
  5190. }
  5191. func UpdatedNewWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) error {
  5192. outInfo := models.WarehouseOutInfo{}
  5193. 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
  5194. if err != nil {
  5195. tx.Rollback()
  5196. return err
  5197. }
  5198. return err
  5199. }
  5200. func UpdatedWarehouseOutInfoSix(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) error {
  5201. outInfo := models.WarehouseOutInfo{}
  5202. 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
  5203. return err
  5204. }
  5205. 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 {
  5206. outInfo := models.WarehouseOutInfo{}
  5207. 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
  5208. if err != nil {
  5209. tx.Rollback()
  5210. return err
  5211. }
  5212. return err
  5213. }
  5214. func GetLastWarehouOutInfoByPatientId(orgid int64, patient_id int64, record_time int64, goodid int64, good_type_id int64) (models.WarehouseOutInfo, error) {
  5215. info := models.WarehouseOutInfo{}
  5216. 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
  5217. return info, err
  5218. }
  5219. func GetLastStockOut(id int64) (models.WarehousingInfo, error) {
  5220. info := models.WarehousingInfo{}
  5221. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5222. return info, err
  5223. }
  5224. func GetStockGoodCount(good_id int64) (models.WarehousingInfo, error) {
  5225. info := models.WarehousingInfo{}
  5226. db := XTReadDB().Model(&info)
  5227. if good_id > 0 {
  5228. db = db.Where("good_id = ? and status = 1", good_id)
  5229. }
  5230. err := db.Select("sum(stock_count) as stock_count").Find(&info).Error
  5231. return info, err
  5232. }
  5233. 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) {
  5234. offset := (page - 1) * limit
  5235. db := XTReadDB().Model(drug_flow).Where("status = 1")
  5236. if drugid > 0 {
  5237. db = db.Where("drug_id = ?", drugid)
  5238. }
  5239. if orgid > 0 {
  5240. db = db.Where("user_org_id = ?", orgid)
  5241. }
  5242. if startTime > 0 {
  5243. db = db.Where("ctime >=? ", startTime)
  5244. }
  5245. if endTime > 0 {
  5246. db = db.Where("ctime<=?", endTime)
  5247. }
  5248. if stock_type > 0 {
  5249. //手动入库
  5250. if stock_type == 1 {
  5251. db = db.Where("consumable_type = 1")
  5252. }
  5253. //手动出库
  5254. if stock_type == 2 {
  5255. db = db.Where("consumable_type = 2 and is_sys = 0")
  5256. }
  5257. //自动出库
  5258. if stock_type == 3 {
  5259. db = db.Where("consumable_type = 3 and is_sys = 1")
  5260. }
  5261. if stock_type == 4 {
  5262. db = db.Where("consumable_type = 4")
  5263. }
  5264. if stock_type == 10 {
  5265. db = db.Where("consumable_type = 10")
  5266. }
  5267. if stock_type == 11 {
  5268. db = db.Where("consumable_type = 11")
  5269. }
  5270. if stock_type == 12 {
  5271. db = db.Where("consumable_type = 12")
  5272. }
  5273. if stock_type == 13 {
  5274. db = db.Where("consumable_type = 13")
  5275. }
  5276. if stock_type == 15 {
  5277. db = db.Where("consumable_type = 15")
  5278. }
  5279. }
  5280. 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
  5281. return drug_flow, total, err
  5282. }
  5283. func HisDoctorAdviceMap(patient_id int64, advice_date int64, drug_id int64, user_org_id int64) (models.HisDoctorAdviceInfo, error) {
  5284. advicelist := models.HisDoctorAdviceInfo{}
  5285. 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
  5286. return advicelist, err
  5287. }
  5288. func GetDrugWarehouseOutInfoIsExit(drug_id int64, patient_id int64, recordtime int64) (*models.DrugWarehouseOutInfo, error) {
  5289. outInfo := models.DrugWarehouseOutInfo{}
  5290. var err error
  5291. 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
  5292. if err == gorm.ErrRecordNotFound {
  5293. return nil, err
  5294. }
  5295. if err != nil {
  5296. return nil, err
  5297. }
  5298. return &outInfo, nil
  5299. }
  5300. func GetDrugWarehouseOutInfoIsExitOne(drug_id int64, patient_id int64, recordtime int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  5301. 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
  5302. return outInfo, err
  5303. }
  5304. func UpdatedDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, drug_id int64, patient_id int64, record_date int64, orgid int64) error {
  5305. outInfo := models.DrugWarehouseOutInfo{}
  5306. 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
  5307. return err
  5308. }
  5309. func GetCancelExportList(startime int64, endtime int64, ids []string, orgid int64) (cancle []*models.CancelStockInfo, err error) {
  5310. db := XTReadDB().Model(&cancle).Where("status = 1")
  5311. if startime > 0 {
  5312. db = db.Where("ctime >=?", startime)
  5313. }
  5314. if endtime > 0 {
  5315. db = db.Where("ctime <=?", endtime)
  5316. }
  5317. if len(ids) > 0 {
  5318. db = db.Where("cancel_stock_id in(?)", ids)
  5319. }
  5320. if orgid > 0 {
  5321. db = db.Where("org_id = ?", orgid)
  5322. }
  5323. db = db.Preload("GoodInfo", "status =1 and org_id = ?", orgid)
  5324. err = db.Find(&cancle).Error
  5325. return cancle, err
  5326. }
  5327. func GetCancelStockDetail(id int64) (info []*models.DrugCancelStockInfo, err error) {
  5328. db := XTReadDB().Model(&info)
  5329. if id > 0 {
  5330. db = db.Where("cancel_stock_id = ? and status = 1", id)
  5331. }
  5332. db = db.Preload("BaseDrugLib")
  5333. err = db.Find(&info).Error
  5334. return info, err
  5335. }
  5336. func GetDrugWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  5337. info := models.XtDrugWarehouseInfo{}
  5338. db := XTReadDB().Model(&info).Where("status = 1")
  5339. if id > 0 {
  5340. db = db.Where("id = ?", id)
  5341. }
  5342. err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  5343. return info, err
  5344. }
  5345. func GetDrugWarehouseInfoOne(id int64) (models.XtDrugWarehouseInfo, error) {
  5346. info := models.XtDrugWarehouseInfo{}
  5347. err := XTReadDB().Where("status = 1 and id=?", id).Find(&info).Error
  5348. return info, err
  5349. }
  5350. //func GetDrugWarehouseInfoTX(id int64,tx *gorm.DB) (models.XtDrugWarehouseInfo, error) {
  5351. // info := models.XtDrugWarehouseInfo{}
  5352. // db := tx.Model(&info).Where("status = 1")
  5353. // if id > 0 {
  5354. // db = db.Where("id = ?", id)
  5355. // }
  5356. // err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  5357. // return info, err
  5358. //}
  5359. func GetDrugWarehouseInfoPrescription(drugid int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5360. err = XTReadDB().Where("drug_id = ? and org_id =? and status = 1", drugid, orgid).Find(&info).Error
  5361. return info, err
  5362. }
  5363. func GetDrugWarehouseInfoPrescriptionSeven(drugid int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfoSeven, err error) {
  5364. 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
  5365. return info, err
  5366. }
  5367. func GetHisAdviceListByDrugId(drugid int64, patient_id int64, advice_date int64) (models.HisDoctorAdviceInfo, error) {
  5368. info := models.HisDoctorAdviceInfo{}
  5369. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1", drugid, patient_id, advice_date).Find(&info).Error
  5370. return info, err
  5371. }
  5372. func GetHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64) (info []*models.HisDoctorAdviceInfo, err error) {
  5373. err = XTReadDB().Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  5374. return info, err
  5375. }
  5376. func GetNewHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64, tx *gorm.DB) (info []*models.HisDoctorAdviceInfo, err error) {
  5377. err = tx.Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  5378. if err != gorm.ErrRecordNotFound {
  5379. if err != nil {
  5380. tx.Rollback()
  5381. return info, err
  5382. }
  5383. }
  5384. return info, err
  5385. }
  5386. func GetLastHisAdvicesList(patient_id int64, advice_date int64, user_org_id int64, tx *gorm.DB) (models.HisDoctorAdviceInfo, error) {
  5387. adviceInfo := models.HisDoctorAdviceInfo{}
  5388. 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
  5389. if err != gorm.ErrRecordNotFound {
  5390. if err != nil {
  5391. tx.Rollback()
  5392. return adviceInfo, err
  5393. }
  5394. }
  5395. return adviceInfo, err
  5396. }
  5397. func GetHisAdviceListByDrugIdEight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  5398. info := models.HisDoctorAdviceInfo{}
  5399. 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
  5400. return info, err
  5401. }
  5402. func GetHisAdviceListByDrugIdNight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  5403. info := models.HisDoctorAdviceInfo{}
  5404. 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
  5405. return info, err
  5406. }
  5407. func GetLastDrugWarehouseOutByDrugId(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.DrugAutomaticReduceDetail, error) {
  5408. detail := models.DrugAutomaticReduceDetail{}
  5409. 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
  5410. return detail, err
  5411. }
  5412. func GetWarehouseOutListExport(drug_id int64, patient_id int64, record_date int64, advice_id int64) (out []*models.XtDrugWarehouseOutInfo, err error) {
  5413. 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
  5414. return out, err
  5415. }
  5416. func GetLastDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64) (out []*models.DrugWarehouseOutInfo, err error) {
  5417. 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
  5418. return out, err
  5419. }
  5420. func GetLastNewDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64, tx *gorm.DB) (out []*models.DrugWarehouseOutInfo, err error) {
  5421. 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
  5422. if err != gorm.ErrRecordNotFound {
  5423. if err != nil {
  5424. tx.Rollback()
  5425. return out, err
  5426. }
  5427. }
  5428. return out, err
  5429. }
  5430. func ModefyDrugByWarehouseInfo(id int64, stock_max_number int64) error {
  5431. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  5432. return err
  5433. }
  5434. func ModefyDrugByWarehouseInfoOne(id int64, stock_min_number int64) error {
  5435. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  5436. return err
  5437. }
  5438. func DeleteDrugAutoWarehouse(drugid int64, patient_id int64, record_time int64) error {
  5439. 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
  5440. 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
  5441. 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
  5442. return err
  5443. }
  5444. func DeleteDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  5445. 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
  5446. 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
  5447. return err
  5448. }
  5449. func DeleteNewDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  5450. 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
  5451. 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
  5452. return err
  5453. }
  5454. func GetDrugWarehouseInfoById(id int64) (models.XtDrugWarehouseInfo, error) {
  5455. info := models.XtDrugWarehouseInfo{}
  5456. err := XTReadDB().Model(&info).Where("id=? and status =1", id).Find(&info).Error
  5457. return info, err
  5458. }
  5459. func GetDrugWarehouseInfoByIdTwenty(id int64) (models.XtDrugWarehouseInfo, error) {
  5460. info := models.XtDrugWarehouseInfo{}
  5461. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  5462. return info, err
  5463. }
  5464. func UpdateDrugWarehouseInfo(info *models.XtDrugWarehouseInfo, id int64) error {
  5465. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  5466. return err
  5467. }
  5468. //func UpdateDrugWarehouseInfoTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  5469. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  5470. // return err
  5471. //}
  5472. func UpdateDrugWarehouseInfoOne(info *models.XtDrugWarehouseInfo, id int64) error {
  5473. //err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_min_number": info.StockMaxNumber}).Error
  5474. //return err
  5475. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  5476. return err
  5477. }
  5478. //func UpdateDrugWarehouseInfoOneTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  5479. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  5480. // return err
  5481. //}
  5482. func GetLastCancelStockInfo(id int64) (models.DrugCancelStockInfo, error) {
  5483. info := models.DrugCancelStockInfo{}
  5484. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5485. return info, err
  5486. }
  5487. func GetLastCancelStockInfoOne(id int64, drug_id int64) (models.DrugCancelStockInfo, error) {
  5488. info := models.DrugCancelStockInfo{}
  5489. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ? and status = 1", id, drug_id).Find(&info).Error
  5490. return info, err
  5491. }
  5492. func UpdatedDrugInfo(id int64, count int64) (err error) {
  5493. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  5494. return err
  5495. }
  5496. func UpdatedDrugFlow(cancelstockid int64, drug_id int64, flow models.DrugFlow) error {
  5497. 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
  5498. return err
  5499. }
  5500. func UpdatedDrugInfoOne(id int64, count int64) (err error) {
  5501. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  5502. return err
  5503. }
  5504. func UpdateWarehouseInfo(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  5505. 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
  5506. return err
  5507. }
  5508. func UpdateWarehouseInfoTwo(count int64, drugid int64, orgid int64) error {
  5509. 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
  5510. return err
  5511. }
  5512. func UpdateWarehouseInfoOne(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  5513. 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
  5514. return err
  5515. }
  5516. func UpdateWarehouseInfoFour(count int64, drugid int64, orgid int64) error {
  5517. 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
  5518. return err
  5519. }
  5520. func GetWarehouseInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  5521. info := models.WarehousingInfo{}
  5522. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5523. return info, err
  5524. }
  5525. func GetLastWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5526. info := models.WarehousingInfo{}
  5527. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  5528. return info, err
  5529. }
  5530. func GetLastWarehouseInfoTwo(goodid int64, storehouse_id int64, user_org_id int64) (info []*models.WarehousingInfo, err error) {
  5531. 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
  5532. return info, err
  5533. }
  5534. func GetFirstWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5535. info := models.WarehousingInfo{}
  5536. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).First(&info).Error
  5537. return info, err
  5538. }
  5539. func CreateWarehouseOut(out models.WarehouseOut) error {
  5540. err := XTWriteDB().Create(&out).Error
  5541. return err
  5542. }
  5543. func GetLastGoodWarehouseOut(orgid int64) (models.WarehouseOut, error) {
  5544. out := models.WarehouseOut{}
  5545. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  5546. return out, err
  5547. }
  5548. func CreateWarehouseInfo(info models.WarehousingInfo) error {
  5549. err := XTWriteDB().Create(&info).Error
  5550. return err
  5551. }
  5552. func UpdateGoodWarehouseInfo(id int64, info models.WarehousingInfo) error {
  5553. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5554. return err
  5555. }
  5556. func UpdateWareInfoById(id int64, count int64) error {
  5557. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  5558. return err
  5559. }
  5560. func UpdateWareInfoByIdSix(id int64, count int64) error {
  5561. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  5562. return err
  5563. }
  5564. func GetLastCancelInfo(id int64, goodid int64) (models.CancelStockInfo, error) {
  5565. info := models.CancelStockInfo{}
  5566. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5567. return info, err
  5568. }
  5569. func GetLastCancelInfoSix(id int64, goodid int64, storehouse_id int64) (models.CancelStockInfo, error) {
  5570. info := models.CancelStockInfo{}
  5571. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1 and storehouse_id = ?", id, goodid, storehouse_id).Find(&info).Error
  5572. return info, err
  5573. }
  5574. func GetAllCancelInfoById(id int64, goodid int64) (info []*models.CancelStockInfo, err error) {
  5575. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5576. return info, err
  5577. }
  5578. func GetAllCancelInfoByIdSix(id int64, goodid int64, storehouse_id int64) (info []*models.CancelStockInfo, err error) {
  5579. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5580. return info, err
  5581. }
  5582. func UpdateStockFlow(flow models.VmStockFlow, goodid int64, warehousing_id int64) error {
  5583. err := XTWriteDB().Model(&flow).Where("good_id = ? and warehousing_id= ? and status = 1", goodid, warehousing_id).Updates(map[string]interface{}{"count": flow.Count}).Error
  5584. return err
  5585. }
  5586. func UpdateDrugFlow(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5587. drugFlow := models.DrugFlow{}
  5588. 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
  5589. return err
  5590. }
  5591. func UpdateDrugFlowNight(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5592. drugFlow := models.DrugFlow{}
  5593. 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
  5594. return err
  5595. }
  5596. func UpdateDrugCancel(id int64) (models.DrugCancelStock, error) {
  5597. stock := models.DrugCancelStock{}
  5598. err := XTWriteDB().Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5599. return stock, err
  5600. }
  5601. //func UpdateDrugCancelTX(id int64,tx *gorm.DB) (models.DrugCancelStock, error) {
  5602. // stock := models.DrugCancelStock{}
  5603. // err := tx.Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5604. // return stock, err
  5605. //}
  5606. func GetDrugWarehouseOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugWarehouseOutInfo, err error) {
  5607. db := XTReadDB().Model(&info)
  5608. if warehouse_info_id > 0 {
  5609. db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5610. }
  5611. if drug_id > 0 {
  5612. db = db.Where("drug_id = ? and status =1", drug_id)
  5613. }
  5614. err = db.Find(&info).Error
  5615. return info, err
  5616. }
  5617. //func GetDrugWarehouseOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  5618. // db := tx.Model(&info)
  5619. // if warehouse_info_id > 0 {
  5620. // db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5621. // }
  5622. // if drug_id > 0 {
  5623. // db = db.Where("drug_id = ? and status =1", drug_id)
  5624. // }
  5625. // err = db.Find(&info).Error
  5626. // return info, err
  5627. //}
  5628. func GetCancelDrugStockOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugCancelStockInfo, err error) {
  5629. err = XTReadDB().Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5630. return info, err
  5631. }
  5632. //func GetCancelDrugStockOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugCancelStockInfo, err error) {
  5633. // err = tx.Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5634. // return info, err
  5635. //}
  5636. func GetTemplateSummary(orgid int64) (config []*models.Dataconfig, err error) {
  5637. err = XTReadDB().Model(&config).Where("module='template_summary' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5638. return config, err
  5639. }
  5640. func GetTemplatePlan(orgid int64) (config []*models.Dataconfig, err error) {
  5641. err = XTReadDB().Model(&config).Where("module='template_plan' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5642. return config, err
  5643. }
  5644. func GetSearchGoodWarehouseList(keyword string, orgid int64, storehouse_id int64) (info []*models.BloodWarehouseInfo, err error) {
  5645. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  5646. table := XTReadDB().Table("xt_goood_information as t").Where("t.status")
  5647. fmt.Println(table)
  5648. likeKey := "%" + keyword + "%"
  5649. if orgid > 0 {
  5650. db = db.Where("x.org_id = ?", orgid)
  5651. }
  5652. if storehouse_id > 0 {
  5653. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5654. }
  5655. 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
  5656. return info, err
  5657. }
  5658. func CreateAdjustPrice(adjustPrice *models.XtStockAdjustPrice) error {
  5659. err := XTWriteDB().Create(&adjustPrice).Error
  5660. return err
  5661. }
  5662. func GetAllStockPrice(orgid int64, startime int64, endtime int64, keyword string, limit int64, page int64) (list []*models.VmStockAdjustPrice, total int64, err error) {
  5663. likeKey := "%" + keyword + "%"
  5664. offset := (page - 1) * limit
  5665. db := XTReadDB().Table("xt_stock_adjust_price as x").Where("x.status = 1")
  5666. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5667. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5668. fmt.Println(table, tab)
  5669. if startime > 0 {
  5670. db = db.Where("x.start_time>=?", startime)
  5671. }
  5672. if endtime > 0 {
  5673. db = db.Where("x.start_time<=?", endtime)
  5674. }
  5675. if len(keyword) > 0 {
  5676. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  5677. }
  5678. if orgid > 0 {
  5679. db = db.Where("x.user_org_id = ?", orgid)
  5680. }
  5681. 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
  5682. return list, total, err
  5683. }
  5684. func UpdateAdjustPrice(ids []string, info models.XtStockAdjustPrice) error {
  5685. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5686. return err
  5687. }
  5688. func UpdateAdjustCheckPrice(info *models.GoodInfo, id int64) error {
  5689. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"packing_price": info.PackingPrice, "buy_price": info.BuyPrice}).Error
  5690. return err
  5691. }
  5692. func GetAdjustCheckPriceList(ids []string) (info []*models.XtStockAdjustPrice, err error) {
  5693. err = XTReadDB().Where("id in(?)", ids).Find(&info).Error
  5694. return info, err
  5695. }
  5696. func GetAdjustPricebyIdOne(ids []string) (adjust []*models.XtStockAdjustPrice, err error) {
  5697. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5698. return adjust, err
  5699. }
  5700. func CreateReportPrice(price *models.XtStockReportPrice) error {
  5701. err := XTWriteDB().Create(&price).Error
  5702. return err
  5703. }
  5704. func GetReportStockList(startime int64, endtime int64, orgid int64, keyword string, page int64, limit int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5705. likeKey := "%" + keyword + "%"
  5706. offset := (page - 1) * limit
  5707. db := XTReadDB().Table("xt_stock_report_price as x").Where("x.status = 1")
  5708. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5709. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5710. fmt.Println(table, tab)
  5711. if startime > 0 {
  5712. db = db.Where("x.ctime>=?", startime)
  5713. }
  5714. if endtime > 0 {
  5715. db = db.Where("x.ctime<=?", endtime)
  5716. }
  5717. if len(keyword) > 0 {
  5718. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5719. }
  5720. if orgid > 0 {
  5721. db = db.Where("x.user_org_id = ?", orgid)
  5722. }
  5723. 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
  5724. return adjust, total, err
  5725. }
  5726. func GetStockDamagedList(orgid int64, keyword string, page int64, limit int64, startime int64, endtime int64, storehouse_id int64) (adjust []*models.VmStockInventory, total int64, err error) {
  5727. likeKey := "%" + keyword + "%"
  5728. offset := (page - 1) * limit
  5729. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.inventory_type = 4")
  5730. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5731. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5732. fmt.Println(table, tab)
  5733. if len(keyword) > 0 {
  5734. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5735. }
  5736. if orgid > 0 {
  5737. db = db.Where("x.user_org_id = ?", orgid)
  5738. }
  5739. if startime > 0 {
  5740. db = db.Where("x.ctime >=?", startime)
  5741. }
  5742. if endtime > 0 {
  5743. db = db.Where("x.ctime <=?", endtime)
  5744. }
  5745. if storehouse_id > 0 {
  5746. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5747. }
  5748. 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
  5749. return adjust, total, err
  5750. }
  5751. func GetStockDamagedCountOne(orgid int64, keyword string, inventory_type int64) (adjust []*models.VmStockInventory, err error) {
  5752. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5753. if inventory_type == 0 {
  5754. db = db.Where("x.inventory_type = 4")
  5755. }
  5756. if inventory_type > 0 {
  5757. db = db.Where("x.inventory_type = ?", inventory_type)
  5758. }
  5759. if len(keyword) > 0 {
  5760. likeKey := "%" + keyword + "%"
  5761. db = db.Where("x.warehousing_order like ?", likeKey)
  5762. }
  5763. if orgid > 0 {
  5764. db = db.Where("x.user_org_id = ?", orgid)
  5765. }
  5766. 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
  5767. return adjust, err
  5768. }
  5769. func GetStockDamagedCount(orgid int64) (adjust []*models.VmStockInventory, err error) {
  5770. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and inventory_type = 4 ")
  5771. if orgid > 0 {
  5772. db = db.Where("x.user_org_id = ?", orgid)
  5773. }
  5774. 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
  5775. return adjust, err
  5776. }
  5777. func UpdateCheckDamage(ids []string, reportPrice models.XtStockReportPrice) error {
  5778. 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
  5779. return err
  5780. }
  5781. func GetCheckDamageList(ids []string) (report []*models.XtStockReportPrice, err error) {
  5782. err = XTReadDB().Model(&report).Where("id in(?) and status= 1", ids).Find(&report).Error
  5783. return report, err
  5784. }
  5785. func CreateInentory(inventory models.XtStockInventory) error {
  5786. err = XTWriteDB().Create(&inventory).Error
  5787. return err
  5788. }
  5789. func GetInventorylist(startime int64, endtime int64, page int64, limit int64, inventory_status int64, keyword string, orgid int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5790. likeKey := "%" + keyword + "%"
  5791. offset := (page - 1) * limit
  5792. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5793. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5794. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5795. fmt.Println(table, tab)
  5796. if startime > 0 {
  5797. db = db.Where("x.ctime>=?", startime)
  5798. }
  5799. if endtime > 0 {
  5800. db = db.Where("x.ctime<=?", endtime)
  5801. }
  5802. if len(keyword) > 0 {
  5803. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5804. }
  5805. if orgid > 0 {
  5806. db = db.Where("x.user_org_id = ?", orgid)
  5807. }
  5808. if inventory_status > 0 {
  5809. db = db.Where("checker_status = ?", inventory_status)
  5810. }
  5811. 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
  5812. return adjust, total, err
  5813. }
  5814. func UpdateCheckInventory(ids []string, info models.XtStockInventory) error {
  5815. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5816. return err
  5817. }
  5818. func GetModifyPriceDetail(id int64) (models.XtStockAdjustPrice, error) {
  5819. info := models.XtStockAdjustPrice{}
  5820. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5821. return info, err
  5822. }
  5823. func GetInventoryDetailById(id []string) (list []*models.XtStockInventory, err error) {
  5824. err = XTReadDB().Model(&list).Where("id in(?) and status = 1", id).Find(&list).Error
  5825. return list, err
  5826. }
  5827. func GetWarehouseInfoByIdNight(id int64) (models.WarehousingInfo, error) {
  5828. info := models.WarehousingInfo{}
  5829. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5830. return info, err
  5831. }
  5832. func GetWarehouseInfoById(id int64) (models.WarehousingInfo, error) {
  5833. info := models.WarehousingInfo{}
  5834. //err :=XTReadDB().Model(&info).Where("id = ? or warehouse_info_id and status = 1", id).Find(&info).Error
  5835. //return info, err
  5836. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5837. if id > 0 {
  5838. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5839. }
  5840. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5841. return info, err
  5842. }
  5843. func GetWarehouseInfoByIdTen(id int64, storehouse_id int64) (models.WarehousingInfo, error) {
  5844. info := models.WarehousingInfo{}
  5845. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5846. if id > 0 {
  5847. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5848. }
  5849. if storehouse_id > 0 {
  5850. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5851. }
  5852. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5853. return info, err
  5854. }
  5855. func UpdateStockPrice(info models.XtStockAdjustPrice, id int64) error {
  5856. 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
  5857. return err
  5858. }
  5859. func DeleteStockPrice(id int64) error {
  5860. info := models.XtStockAdjustPrice{}
  5861. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5862. return err
  5863. }
  5864. func GetStockDamageDetail(id int64) (models.XtStockReportPrice, error) {
  5865. report := models.XtStockReportPrice{}
  5866. err := XTReadDB().Model(&report).Where("id=? and status = 1", id).Find(&report).Error
  5867. return report, err
  5868. }
  5869. func ModifyPrice(id int64, info models.XtStockReportPrice) error {
  5870. 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
  5871. return err
  5872. }
  5873. func DeleteDamage(id int64) error {
  5874. adjust := models.XtStockReportPrice{}
  5875. err := XTWriteDB().Model(&adjust).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5876. return err
  5877. }
  5878. func GetStockDamageById(ids []string) (adjust []*models.XtStockReportPrice, err error) {
  5879. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5880. return adjust, err
  5881. }
  5882. func GetInventoryDetail(id int64) (models.XtStockInventory, error) {
  5883. inventory := models.XtStockInventory{}
  5884. err := XTReadDB().Model(&inventory).Where("id=? and status = 1", id).Find(&inventory).Error
  5885. return inventory, err
  5886. }
  5887. func ModifyInventory(id int64, inventory models.XtStockInventory) error {
  5888. 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
  5889. return err
  5890. }
  5891. func DeleteInventory(id int64) error {
  5892. inventory := models.XtStockInventory{}
  5893. err := XTWriteDB().Model(&inventory).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5894. return err
  5895. }
  5896. func GetInventoryDetailList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64) (list []*models.VmStockInventory, total int64, err error) {
  5897. likeKey := "%" + keyword + "%"
  5898. offset := (page - 1) * limit
  5899. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5900. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5901. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5902. fmt.Println(table, tab)
  5903. if len(keyword) > 0 {
  5904. db = db.Where("x.good_name like ?", likeKey)
  5905. }
  5906. if orgid > 0 {
  5907. db = db.Where("x.user_org_id = ?", orgid)
  5908. }
  5909. if storehouse_id > 0 {
  5910. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5911. }
  5912. 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
  5913. return list, total, err
  5914. }
  5915. func GetInventoryDetailPrintMapList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64, ids []string) (list []*models.VmStockInventory, total int64, err error) {
  5916. likeKey := "%" + keyword + "%"
  5917. offset := (page - 1) * limit
  5918. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5919. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5920. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5921. fmt.Println(table, tab)
  5922. if len(keyword) > 0 {
  5923. db = db.Where("x.good_name like ?", likeKey)
  5924. }
  5925. if orgid > 0 {
  5926. db = db.Where("x.user_org_id = ?", orgid)
  5927. }
  5928. if storehouse_id > 0 {
  5929. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5930. }
  5931. if len(ids) > 0 {
  5932. db = db.Where("x.id in(?)", ids)
  5933. }
  5934. 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
  5935. return list, total, err
  5936. }
  5937. func GetInventoryPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5938. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5939. return list, err
  5940. }
  5941. func GetInventoryDetailPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5942. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5943. return list, err
  5944. }
  5945. func DeleteDrugPrice(id int64) error {
  5946. info := models.XtDrugAdjustPrice{}
  5947. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5948. return err
  5949. }
  5950. func GetGoodWarehouseList(goodid int64) (models.WarehousingInfo, error) {
  5951. info := models.WarehousingInfo{}
  5952. db := XTReadDB().Model(&info).Where("status = 1")
  5953. if goodid > 0 {
  5954. db = db.Where("good_id = ?", goodid)
  5955. }
  5956. err := db.Select("good_id,sum(stock_count) as stock_count").Find(&info).Error
  5957. return info, err
  5958. }
  5959. func UpdateProofInventory(id int64, info models.WarehousingInfo) error {
  5960. err := XTWriteDB().Model(&info).Where("id=? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5961. return err
  5962. }
  5963. func UpdateStockInventory(inventory models.XtStockInventory, id int64) error {
  5964. err := XTWriteDB().Model(&inventory).Where("id= ? and status = 1", id).Updates(map[string]interface{}{"total": inventory.Total}).Error
  5965. return err
  5966. }
  5967. func CeateStockCorrectRecord(record models.XtStockCorrectRecord) error {
  5968. err := XTWriteDB().Create(&record).Error
  5969. return err
  5970. }
  5971. func GetWarehouseTotal(id int64) (models.WarehousingInfo, error) {
  5972. info := models.WarehousingInfo{}
  5973. //err :=XTReadDB()()()()().Where("id=? and status = 1", id).Find(&info).Error
  5974. //return info, err
  5975. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5976. if id > 0 {
  5977. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5978. }
  5979. err := db.Select("sum(x.stock_count) as stock_count").Scan(&info).Error
  5980. return info, err
  5981. }
  5982. func GetCurrentWarehosueInfo(id int64) (info []*models.DrugWarehouseInfo, err error) {
  5983. err = XTReadDB().Model(&info).Where("id=? or warehousing_info_id = ? ", id, id).Find(&info).Error
  5984. return info, err
  5985. }
  5986. func GetCurrentWarehosueInfoOne(id int64) (models.DrugWarehouseInfo, error) {
  5987. info := models.DrugWarehouseInfo{}
  5988. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  5989. return info, err
  5990. }
  5991. func ModifyDrugWarehouseInfo(info *models.DrugWarehouseInfo, id int64) error {
  5992. 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
  5993. return err
  5994. }
  5995. func ModifyDrugWarehouseInfoSix(info *models.DrugWarehouseInfo, id int64) error {
  5996. 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
  5997. return err
  5998. }
  5999. func GetGoodInventoryWarehouseList(id int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  6000. 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
  6001. return info, err
  6002. }
  6003. func UpdatedWarehousingInfo(info models.WarehousingInfo, id int64) error {
  6004. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  6005. return err
  6006. }
  6007. func UpdatedWarehousingInfoSix(info models.WarehousingInfo, id int64) error {
  6008. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount, "warehousing_count": info.WarehousingCount}).Error
  6009. return err
  6010. }
  6011. func GetDamageDetailByGoodId(good_id int64, keyword string, inventory_type int64) (list []*models.XtStockInventory, err error) {
  6012. db := XTReadDB().Model(&list).Where(" status = 1")
  6013. if good_id > 0 {
  6014. db = db.Where("good_id = ?", good_id)
  6015. }
  6016. if len(keyword) > 0 {
  6017. likeKey := "%" + keyword + "%"
  6018. db = db.Where("warehousing_order like ?", likeKey)
  6019. }
  6020. if inventory_type > 0 {
  6021. db = db.Where("inventory_type = ?", inventory_type)
  6022. }
  6023. if inventory_type == 0 {
  6024. db = db.Where("inventory_type = 4")
  6025. }
  6026. err = db.Find(&list).Error
  6027. return list, err
  6028. }
  6029. func GetCancelStockId(cancelstock_id int64, drug_id int64) (*models.DrugFlow, error) {
  6030. flow := models.DrugFlow{}
  6031. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ?", cancelstock_id, drug_id).Find(&flow).Error
  6032. if err == gorm.ErrRecordNotFound {
  6033. return nil, err
  6034. }
  6035. if err != nil {
  6036. return nil, err
  6037. }
  6038. return &flow, nil
  6039. }
  6040. func GetTotalDrugCancelInfo(batchNumber_id int64, drug_id int64, id int64) (info []*models.DrugCancelStockInfo, err error) {
  6041. err = XTReadDB().Where("batch_number_id = ? and drug_id = ? and status = 1 and id<>?", batchNumber_id, drug_id, id).Find(&info).Error
  6042. return info, err
  6043. }
  6044. func GetLastDrugWarehouseInfo(drugid int64) (models.XtDrugWarehouseInfo, error) {
  6045. info := models.XtDrugWarehouseInfo{}
  6046. err := XTReadDB().Where("drug_id = ? and status =1", drugid).Last(&info).Error
  6047. return info, err
  6048. }
  6049. func GetLastGoodWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  6050. info := models.WarehousingInfo{}
  6051. err := XTReadDB().Where("good_id =? and status = 1", goodid).Find(&info).Error
  6052. return info, err
  6053. }
  6054. func GetGoodStockFlowInfo(warehouseid int64, goodid int64) (*models.VmStockFlow, error) {
  6055. flow := models.VmStockFlow{}
  6056. err := XTReadDB().Where("warehousing_detail_id = ? and good_id = ? and status = 1", warehouseid, goodid).Find(&flow).Error
  6057. if err == gorm.ErrRecordNotFound {
  6058. return nil, err
  6059. }
  6060. if err != nil {
  6061. return nil, err
  6062. }
  6063. return &flow, nil
  6064. }
  6065. func GetLastInfoMationById(id int64) (models.GoodInfo, error) {
  6066. info := models.GoodInfo{}
  6067. err := XTReadDB().Where("id =? and status = 1", id).Find(&info).Error
  6068. return info, err
  6069. }
  6070. func UpdateGoodInfoMation(goodid int64, info models.GoodInfo) error {
  6071. err := XTWriteDB().Model(&info).Where("id =? and status = 1", goodid).Updates(map[string]interface{}{"total": info.Total}).Error
  6072. return err
  6073. }
  6074. func UpdateWarehouseInfoByGoodId(info models.WarehousingInfo, id int64) error {
  6075. err := XTWriteDB().Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount}).Error
  6076. return err
  6077. }
  6078. func UpdateWarehouseInfoByGoodIdOne(info *models.WarehousingInfo, id int64) error {
  6079. 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
  6080. return err
  6081. }
  6082. func UpdateNewWarehouseInfoByGoodIdOne(info *models.WarehousingInfo, id int64, tx *gorm.DB) error {
  6083. err := tx.Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount, "price": info.Price, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "license_number": info.LicenseNumber, "manufacturer": info.Manufacturer, "dealer": info.Dealer, "remark": info.Remark, "packing_price": info.PackingPrice, "storehouse_id": info.StorehouseId, "register_number": info.RegisterNumber, "good_id": info.GoodId, "good_code": info.GoodCode}).Error
  6084. if err != nil {
  6085. tx.Rollback()
  6086. return err
  6087. }
  6088. return err
  6089. }
  6090. func UpdatedStockFlowByGoodId(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  6091. 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
  6092. return err
  6093. }
  6094. func UpdatedStockFlowByGoodIdOne(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  6095. 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
  6096. return err
  6097. }
  6098. func UpdateStockFlowByDetailId(flow models.VmStockFlow, wareoutdetailid int64) error {
  6099. 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
  6100. return err
  6101. }
  6102. func UpdatedGoodInfo(info models.GoodInfo, goodid int64) error {
  6103. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", goodid).Updates(map[string]interface{}{"manufacturer": info.Manufacturer, "dealer": info.Dealer}).Error
  6104. return err
  6105. }
  6106. func GetInventoryExportList(orgid int64) (info []*models.PatientWarehouseInfo, err error) {
  6107. if orgid == 10653 {
  6108. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.warehousing_count>0 and x.stock_count>0")
  6109. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  6110. fmt.Println(table)
  6111. if orgid > 0 {
  6112. db = db.Where("x.org_id = ?", orgid)
  6113. }
  6114. 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
  6115. return info, err
  6116. } else {
  6117. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.warehousing_count>0")
  6118. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  6119. fmt.Println(table)
  6120. if orgid > 0 {
  6121. db = db.Where("x.org_id = ?", orgid)
  6122. }
  6123. 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
  6124. return info, err
  6125. }
  6126. }
  6127. func UpdateDrugWarehouseingInfoSix(id int64, info models.XtDrugWarehouseInfo) error {
  6128. warehouseInfo := models.XtDrugWarehouseInfo{}
  6129. 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
  6130. return err
  6131. }
  6132. func CreatedCancelStock(info models.DrugCancelStockInfo) error {
  6133. err := XTWriteDB().Create(&info).Error
  6134. return err
  6135. }
  6136. func CreatedNewCancelStock(info models.DrugCancelStockInfo, tx *gorm.DB) error {
  6137. err := tx.Create(&info).Error
  6138. if err != nil {
  6139. tx.Rollback()
  6140. return err
  6141. }
  6142. return err
  6143. }
  6144. func IsExsitStockFlow(goodid int64, cancel_order_number string) (*models.VmStockFlow, error) {
  6145. flow := models.VmStockFlow{}
  6146. err := XTReadDB().Where("good_id = ? and cancel_order_number = ? and status = 1", goodid, cancel_order_number).Find(&flow).Error
  6147. if err == gorm.ErrRecordNotFound {
  6148. return nil, err
  6149. }
  6150. if err != nil {
  6151. return nil, err
  6152. }
  6153. return &flow, nil
  6154. }
  6155. func UpdateStockFlowTwo(id int64, flow models.VmStockFlow) error {
  6156. err := XTWriteDB().Model(&flow).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"count": flow.Count}).Error
  6157. return err
  6158. }
  6159. func UpdateStockFlowThree(id int64, flow models.VmStockFlow) error {
  6160. 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
  6161. return err
  6162. }
  6163. func GetLastWarehouseOutInfoById(drugid int64) (models.DrugWarehouseOutInfo, error) {
  6164. flow := models.DrugWarehouseOutInfo{}
  6165. err := XTReadDB().Where("drug_id = ? and status = 1", drugid).Last(&flow).Error
  6166. return flow, err
  6167. }
  6168. func GetLastNewWarehouseOutInfoById(drugid int64, tx *gorm.DB) (models.DrugWarehouseOutInfo, error) {
  6169. flow := models.DrugWarehouseOutInfo{}
  6170. err := tx.Where("drug_id = ? and status = 1", drugid).Last(&flow).Error
  6171. if err != gorm.ErrRecordNotFound {
  6172. if err != nil {
  6173. tx.Rollback()
  6174. return flow, err
  6175. }
  6176. }
  6177. return flow, err
  6178. }
  6179. func GetLastWarehoseByGoodId(goodid int64, patient_id int64, record_date int64) (models.WarehouseOutInfo, error) {
  6180. info := models.WarehouseOutInfo{}
  6181. 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
  6182. return info, err
  6183. }
  6184. func ModifyDrugWarehouseOutInfo(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64) error {
  6185. 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
  6186. return err
  6187. }
  6188. func ModifyDrugWarehouseOutInfoOne(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64, warehouse_info_id int64) error {
  6189. 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
  6190. return err
  6191. }
  6192. func GetDrugWarehouseOutInfoByWarehosueId(drugid int64, patientid int64, recorddate int64, warehosueinfoid int64) (*models.DrugWarehouseOutInfo, error) {
  6193. info := models.DrugWarehouseOutInfo{}
  6194. 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
  6195. if err == gorm.ErrRecordNotFound {
  6196. return nil, err
  6197. }
  6198. if err != nil {
  6199. return nil, err
  6200. }
  6201. return &info, nil
  6202. }
  6203. func GetDrugFlowRecord(warehouse_out_order_number string, drugid int64, patientid int64) (*models.DrugFlow, error) {
  6204. drugFlow := models.DrugFlow{}
  6205. 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
  6206. if err == gorm.ErrRecordNotFound {
  6207. return nil, err
  6208. }
  6209. if err != nil {
  6210. return nil, err
  6211. }
  6212. return &drugFlow, nil
  6213. }
  6214. func ModifyDrugFlow(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64) error {
  6215. 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
  6216. return err
  6217. }
  6218. func ModifyDrugFlowOne(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64, warehousing_detail_id int64) error {
  6219. 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
  6220. return err
  6221. }
  6222. func GetDrugAutoReduceRecordOne(drugid int64, patientid int64, recorddate int64) (detail []*models.DrugAutomaticReduceDetail, err error) {
  6223. err = XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  6224. return detail, err
  6225. }
  6226. func GetDrugAutoReduceRecord(drugid int64, patientid int64, recorddate int64) (*models.DrugAutomaticReduceDetail, error) {
  6227. detail := models.DrugAutomaticReduceDetail{}
  6228. err := XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  6229. if err == gorm.ErrRecordNotFound {
  6230. return nil, err
  6231. }
  6232. if err != nil {
  6233. return nil, err
  6234. }
  6235. return &detail, nil
  6236. }
  6237. func ModifyAutoMaticReduce(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64) error {
  6238. 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
  6239. return err
  6240. }
  6241. func ModifyAutoMaticReduceOne(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64, warehouse_info_id int64) error {
  6242. 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
  6243. return err
  6244. }
  6245. func ModifyDrugWarehouse(id int64, stock_max_number int64, stock_min_number int64) (models.DrugWarehouseInfo, error) {
  6246. info := models.DrugWarehouseInfo{}
  6247. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  6248. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  6249. return info, err
  6250. }
  6251. func ModeifyDrugWarehosueInfo(id int64, stock_max_number int64) error {
  6252. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  6253. return err
  6254. }
  6255. func ModeFyStockMinById(id int64, stock_min_number int64) error {
  6256. info := models.DrugWarehouseInfo{}
  6257. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  6258. return err
  6259. }
  6260. func GetDrugCancelById(id int64) (models.DrugCancelStockInfo, error) {
  6261. info := models.DrugCancelStockInfo{}
  6262. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  6263. return info, err
  6264. }
  6265. func ModeFyDrugWareseInfo(id int64, stock_max_number int64) error {
  6266. info := models.DrugWarehouseInfo{}
  6267. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", stock_max_number)).Error
  6268. return err
  6269. }
  6270. func ModeFyDrugWarehouseMin(id int64, stock_min_number int64) error {
  6271. info := models.DrugWarehouseInfo{}
  6272. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", stock_min_number)).Error
  6273. return err
  6274. }
  6275. func GetAllHisDoctorAdviceById(drugid int64, patient_id int64, advicedate int64, orgid int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  6276. 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
  6277. return advice, err
  6278. }
  6279. func GetAllHisDoctorAdviceByIdOne(drugid int64, patient_id int64, advicedate int64, orgid int64, tx *gorm.DB) (advice []*models.HisDoctorAdviceInfo, err error) {
  6280. 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
  6281. if err != gorm.ErrRecordNotFound {
  6282. if err != nil {
  6283. tx.Rollback()
  6284. return advice, err
  6285. }
  6286. }
  6287. return advice, err
  6288. }
  6289. func GetAllDrugFlowById(drugid int64, patient_id int64, advicedate int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  6290. 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
  6291. return info, err
  6292. }
  6293. func GetAllDrugFlowByIdOne(drugid int64, patient_id int64, advicedate int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  6294. 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
  6295. if err != gorm.ErrRecordNotFound {
  6296. if err != nil {
  6297. tx.Rollback()
  6298. return info, err
  6299. }
  6300. }
  6301. return info, err
  6302. }
  6303. // 耗材加库存
  6304. func ModifyGoodAddInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6305. info := models.GoodInfo{}
  6306. 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
  6307. return info, err
  6308. }
  6309. func ModifyGoodAddInformationSix(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6310. info := models.GoodInfo{}
  6311. 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
  6312. 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
  6313. return info, err
  6314. }
  6315. func ModifyGoodReduceInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6316. info := models.GoodInfo{}
  6317. 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
  6318. return info, err
  6319. }
  6320. func ModifyGoodReduceInformationTwo(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6321. info := models.GoodInfo{}
  6322. 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
  6323. 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
  6324. return info, err
  6325. }
  6326. func ModifyGoodReduceInformationSix(good_id int64, sum_count int64, orgid int64, retail_price float64) (models.GoodInfo, error) {
  6327. info := models.GoodInfo{}
  6328. 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
  6329. return info, err
  6330. }
  6331. func GetWarehousingByIdList(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  6332. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  6333. return info, err
  6334. }
  6335. func GetWarehousingByIdListTwenty(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  6336. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  6337. return info, err
  6338. }
  6339. func GetWarehouseInfoByIdSix(id int64, orgid int64) (models.WarehousingInfo, error) {
  6340. info := models.WarehousingInfo{}
  6341. err := XTReadDB().Model(&info).Where("id = ? and status= 1 and org_id = ?", id, orgid).Find(&info).Error
  6342. return info, err
  6343. }
  6344. func GetWarehouseInfoByIdTwenty(id int64, orgid int64) (models.WarehousingInfo, error) {
  6345. info := models.WarehousingInfo{}
  6346. err := XTReadDB().Model(&info).Where("id = ? and org_id = ?", id, orgid).Find(&info).Error
  6347. return info, err
  6348. }
  6349. // 药品加库存
  6350. func ModifyDrugAddInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6351. info := models.BaseDrugLib{}
  6352. 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
  6353. return info, err
  6354. }
  6355. func ModifyDrugAddInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6356. info := models.BaseDrugLib{}
  6357. 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
  6358. 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
  6359. return info, err
  6360. }
  6361. func ModifyDrugReduceInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6362. info := models.BaseDrugLib{}
  6363. 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
  6364. return info, err
  6365. }
  6366. func ModifyDrugReduceInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6367. info := models.BaseDrugLib{}
  6368. 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
  6369. 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
  6370. return info, err
  6371. }
  6372. func ModifyDrugReduceInformationSix(drug_id int64, sum_count int64, orgid int64, batch_retai_price float64) (models.BaseDrugLib, error) {
  6373. info := models.BaseDrugLib{}
  6374. 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
  6375. 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
  6376. return info, err
  6377. }
  6378. func GetLastDrugwarehouseInfoById(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  6379. err = XTReadDB().Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  6380. return info, err
  6381. }
  6382. func GetLastDrugwarehouseInfoByIdTwenty(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  6383. err = XTReadDB().Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  6384. return info, err
  6385. }
  6386. func GetWarhouseOutById(id int64) (models.WarehouseOutInfo, error) {
  6387. info := models.WarehouseOutInfo{}
  6388. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  6389. return info, err
  6390. }
  6391. func FindeGoodWareHouseOutInfo(id int64, storehouse_id int64, orgid int64) (out []*models.WarehouseOutInfo, err error) {
  6392. err = XTReadDB().Where("warehouse_out_id = ? and storehouse_id =? and org_id = ?", id, storehouse_id, orgid).Find(&out).Error
  6393. return out, err
  6394. }
  6395. func ModifyWarehouseInfo(id int64, count int64) (models.WarehousingInfo, error) {
  6396. info := models.WarehousingInfo{}
  6397. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  6398. return info, err
  6399. }
  6400. func GetOrgIdList() (template []*models.VMGobalTemplate, err error) {
  6401. err = XTReadDB().Where("status = 1").Find(&template).Error
  6402. return template, err
  6403. }
  6404. func GetAllGoodList(orgid int64) (good []*models.GoodInfo, err error) {
  6405. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&good).Error
  6406. return good, err
  6407. }
  6408. func GetMobileGoodList(org_id int64) (list []*models.GoodInformationThirty, err error) {
  6409. err = XTReadDB().Where("org_id = ? and status =1", org_id).Find(&list).Error
  6410. return list, err
  6411. }
  6412. func GetAllMobileProjectList(orgid int64) (project []*models.HisProject, err error) {
  6413. err = XTReadDB().Where("user_org_id =? and status =1", orgid).Find(&project).Error
  6414. return project, err
  6415. }
  6416. func ModifyGoodAddInformationSeven(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6417. info := models.GoodInfo{}
  6418. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6419. return info, err
  6420. }
  6421. func GetStockCountWarehouseInfoById(good_id int64) (info []*models.WarehousingInfo, err error) {
  6422. db := XTReadDB().Where("status = 1")
  6423. if good_id > 0 {
  6424. db = db.Where("good_id = ?", good_id)
  6425. }
  6426. err = db.Find(&info).Error
  6427. return info, err
  6428. }
  6429. func GetDrugStockCountWarehoseInfoById(drug_id int64) (info []*models.DrugWarehouseInfo, err error) {
  6430. db := XTReadDB().Model(&info).Where("status = 1")
  6431. if drug_id > 0 {
  6432. db = db.Where("drug_id = ?", drug_id)
  6433. }
  6434. err = db.Find(&info).Error
  6435. return info, err
  6436. }
  6437. func UpdateModifyDrugInfoById(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6438. lib := models.BaseDrugLib{}
  6439. err := XTWriteDB().Model(&lib).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6440. return lib, err
  6441. }
  6442. func GetWarehouseBySecondWarehouseId(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (*models.Warehousing, error) {
  6443. info := models.Warehousing{}
  6444. 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
  6445. if err == gorm.ErrRecordNotFound {
  6446. return nil, err
  6447. }
  6448. if err != nil {
  6449. return nil, err
  6450. }
  6451. return &info, nil
  6452. }
  6453. func GetLastWarehouseById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.Warehousing, error) {
  6454. info := models.Warehousing{}
  6455. 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
  6456. return info, err
  6457. }
  6458. func GetDrugWarehouseBySyS(orgid int64, is_sys int64, sencond_wahouse_id int64, record_date int64) (*models.DrugWarehouseOut, error) {
  6459. out := models.DrugWarehouseOut{}
  6460. 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
  6461. if err == gorm.ErrRecordNotFound {
  6462. return nil, err
  6463. }
  6464. if err != nil {
  6465. return nil, err
  6466. }
  6467. return &out, nil
  6468. }
  6469. func GetLastDrugWarehouseOutById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.DrugWarehouseOut, error) {
  6470. info := models.DrugWarehouseOut{}
  6471. 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
  6472. return info, err
  6473. }
  6474. func GetDrugWarehouseingBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (*models.DrugWarehouse, error) {
  6475. info := models.DrugWarehouse{}
  6476. 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
  6477. if err == gorm.ErrRecordNotFound {
  6478. return nil, err
  6479. }
  6480. if err != nil {
  6481. return nil, err
  6482. }
  6483. return &info, nil
  6484. }
  6485. func CreateDrugWaresing(warehouse models.DrugWarehouse) error {
  6486. err := XTWriteDB().Create(&warehouse).Error
  6487. return err
  6488. }
  6489. func GetLastDrugWarehouseBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (models.DrugWarehouse, error) {
  6490. info := models.DrugWarehouse{}
  6491. 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
  6492. return info, err
  6493. }
  6494. func ModifyGoodReduceInformationTwenty(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6495. info := models.GoodInfo{}
  6496. 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
  6497. 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
  6498. return info, err
  6499. }
  6500. func ModifyGoodReduceInformationTwentyOne(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6501. info := models.GoodInfo{}
  6502. 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
  6503. 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
  6504. return info, err
  6505. }
  6506. func UpdateMedicalSumCount(drug_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6507. tx := XTWriteDB().Begin()
  6508. 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
  6509. if err != nil {
  6510. tx.Rollback()
  6511. return err
  6512. }
  6513. tx.Commit()
  6514. return err
  6515. }
  6516. func UpdateNewMedicalSumCount(drug_id int64, sum_count int64, sum_in_count int64, orgid int64, tx *gorm.DB) error {
  6517. 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
  6518. if err != nil {
  6519. tx.Rollback()
  6520. return err
  6521. }
  6522. return err
  6523. }
  6524. func UpdateMedicalSumCountOne(drug_id int64, sum_count int64, orgid int64) error {
  6525. tx := XTWriteDB().Begin()
  6526. 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
  6527. if err != nil {
  6528. tx.Rollback()
  6529. return err
  6530. }
  6531. tx.Commit()
  6532. return err
  6533. }
  6534. //func UpdateMedicalSumCountTX(drug_id int64, sum_count int64, sum_in_count int64, orgid int64,tx *gorm.DB) error {
  6535. // 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
  6536. // return err
  6537. //}
  6538. func UpdateGoodByGoodId(good_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6539. 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
  6540. return err
  6541. }
  6542. func GetDrugDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  6543. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  6544. table := XTReadDB().Table("xt_base_drug as x d")
  6545. fmt.Println(table)
  6546. if startime > 0 {
  6547. db = db.Where("x.advice_date >= ?", startime)
  6548. }
  6549. if endtime > 0 {
  6550. db = db.Where("x.advice_date <= ?", endtime)
  6551. }
  6552. if orgid > 0 {
  6553. db = db.Where("x.user_org_id = ?", orgid)
  6554. }
  6555. if len(keyword) > 0 {
  6556. likeKey := "%" + keyword + "%"
  6557. 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)
  6558. }
  6559. offset := (page - 1) * limit
  6560. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.created_time desc").Group("x.drug_id").Scan(&adviceinfo).Error
  6561. return adviceinfo, total, err
  6562. }
  6563. func GetDrugDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  6564. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  6565. table := XTReadDB().Table("xt_base_drug as x d")
  6566. fmt.Println(table)
  6567. if startime > 0 {
  6568. db = db.Where("x.advice_date >= ?", startime)
  6569. }
  6570. if endtime > 0 {
  6571. db = db.Where("x.advice_date <= ?", endtime)
  6572. }
  6573. if orgid > 0 {
  6574. db = db.Where("x.user_org_id = ?", orgid)
  6575. }
  6576. if len(keyword) > 0 {
  6577. likeKey := "%" + keyword + "%"
  6578. 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)
  6579. }
  6580. err = db.Order("x.created_time desc").Scan(&adviceinfo).Error
  6581. return adviceinfo, total, err
  6582. }
  6583. func GetAllDrugSummary(orgid int64) (drug []*models.BaseDrugLib, err error) {
  6584. err = XTReadDB().Where("org_id = ? and status = 1 and is_user = 1", orgid).Find(&drug).Error
  6585. return drug, err
  6586. }
  6587. func GetGoodDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  6588. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  6589. table := XTReadDB().Table("xt_good_information as d")
  6590. fmt.Println(table)
  6591. if startime > 0 {
  6592. db = db.Where("x.record_date >= ?", startime)
  6593. }
  6594. if endtime > 0 {
  6595. db = db.Where("x.record_date <= ?", endtime)
  6596. }
  6597. if orgid > 0 {
  6598. db = db.Where("x.user_org_id = ?", orgid)
  6599. }
  6600. if len(keyword) > 0 {
  6601. likeKey := "%" + keyword + "%"
  6602. 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")
  6603. }
  6604. offset := (page - 1) * limit
  6605. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Group("x.project_id").Scan(&project).Error
  6606. return project, total, err
  6607. }
  6608. func GetGoodDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  6609. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  6610. table := XTReadDB().Table("xt_good_information as d")
  6611. fmt.Println(table)
  6612. if startime > 0 {
  6613. db = db.Where("x.record_date >= ?", startime)
  6614. }
  6615. if endtime > 0 {
  6616. db = db.Where("x.record_date <= ?", endtime)
  6617. }
  6618. if orgid > 0 {
  6619. db = db.Where("x.user_org_id = ?", orgid)
  6620. }
  6621. if len(keyword) > 0 {
  6622. likeKey := "%" + keyword + "%"
  6623. 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")
  6624. }
  6625. err = db.Order("x.ctime desc").Group("x.id").Scan(&project).Error
  6626. return project, total, err
  6627. }
  6628. func GeAllGoodInformation(orgid int64) (good []*models.GoodInfo, err error) {
  6629. err = XTReadDB().Where("org_id =? and status = 1 and is_user = 1", orgid).Find(&good).Error
  6630. return good, err
  6631. }
  6632. func GetStockSettingIsExsit(orgid int64) (*models.XtStockSetting, error) {
  6633. setting := models.XtStockSetting{}
  6634. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6635. if err == gorm.ErrRecordNotFound {
  6636. return nil, err
  6637. }
  6638. if err != nil {
  6639. return nil, err
  6640. }
  6641. return &setting, nil
  6642. }
  6643. func CreateStockSetting(setting models.XtStockSetting) error {
  6644. err := XTWriteDB().Create(&setting).Error
  6645. return err
  6646. }
  6647. func UpdateStockSetting(orgid int64, setting models.XtStockSetting) error {
  6648. err := XTWriteDB().Model(&models.XtStockSetting{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_type": setting.IsType}).Error
  6649. return err
  6650. }
  6651. func FindStockSettingById(orgid int64) (models.XtStockSetting, error) {
  6652. setting := models.XtStockSetting{}
  6653. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6654. return setting, err
  6655. }
  6656. func GetAutoStockOutById(good_id int64, record_date int64, orgid int64, patient_id int64) (models.BloodAutomaticReduceDetail, error) {
  6657. detail := models.BloodAutomaticReduceDetail{}
  6658. 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
  6659. return detail, err
  6660. }
  6661. func GetAutoWarehouseOutList(good_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfoNight, error) {
  6662. detail := models.WarehouseOutInfoNight{}
  6663. 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
  6664. return detail, err
  6665. }
  6666. func GetAdviceConfig(orgid int64) (*models.XtAdviceSetting, error) {
  6667. setting := models.XtAdviceSetting{}
  6668. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6669. if err == gorm.ErrRecordNotFound {
  6670. return nil, err
  6671. }
  6672. if err != nil {
  6673. return nil, err
  6674. }
  6675. return &setting, nil
  6676. }
  6677. func CreateAdviceSetting(setting models.XtAdviceSetting) error {
  6678. err := XTWriteDB().Create(&setting).Error
  6679. return err
  6680. }
  6681. func UpdateAdviceSetting(orgid int64, setting models.XtAdviceSetting) error {
  6682. 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
  6683. return err
  6684. }
  6685. func FindAdviceSettingById(orgid int64) (models.XtAdviceSetting, error) {
  6686. setting := models.XtAdviceSetting{}
  6687. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6688. return setting, err
  6689. }
  6690. func GetPrescriptionConfig(orgid int64) (*models.XtPrescriptionConfig, error) {
  6691. config := models.XtPrescriptionConfig{}
  6692. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6693. if err == gorm.ErrRecordNotFound {
  6694. return nil, err
  6695. }
  6696. if err != nil {
  6697. return nil, err
  6698. }
  6699. return &config, nil
  6700. }
  6701. func CreatePrescriptionConfig(prescriptionConfig models.XtPrescriptionConfig) error {
  6702. err := XTWriteDB().Create(&prescriptionConfig).Error
  6703. return err
  6704. }
  6705. func UpdatePrescriptionConfig(orgid int64, config models.XtPrescriptionConfig) error {
  6706. 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
  6707. return err
  6708. }
  6709. func FindPrescriptionConfigById(orgid int64) (models.XtPrescriptionConfig, error) {
  6710. config := models.XtPrescriptionConfig{}
  6711. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6712. return config, err
  6713. }
  6714. func UpdateWarehouseInfoTwentyFive(maxNumber int64, drugid int64, orgID int64, storehouse_id int64, drup *models.XtBaseDrug) (err error) {
  6715. var deliver_number int64 = 0
  6716. var stock_number int64 = 0
  6717. deliver_number = maxNumber
  6718. // 根据先进先出原则,查询最先入库的批次,进行出库
  6719. // 如果没有对应的库存,则报错
  6720. //开启事物
  6721. warehouse, err := FindLastDrugWarehousingInfoByID(drugid, storehouse_id)
  6722. if err != nil {
  6723. return err
  6724. }
  6725. // 将该批次的剩余库存数量转换为拆零数量
  6726. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  6727. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  6728. if stock_number >= deliver_number {
  6729. var maxNumber int64 = 0
  6730. var minNumber int64 = 0
  6731. var stock_max_number int64 = 0
  6732. stock_max_number = warehouse.StockMaxNumber
  6733. maxNumber = deliver_number / drup.MinNumber
  6734. minNumber = deliver_number % drup.MinNumber
  6735. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  6736. minNumber = maxNumber
  6737. }
  6738. if drup.MaxUnit != drup.MinUnit {
  6739. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  6740. return errors.New("库存数量不足")
  6741. }
  6742. }
  6743. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  6744. if warehouse.StockMaxNumber < 0 {
  6745. warehouse.StockMaxNumber = 0
  6746. }
  6747. if warehouse.StockMinNumber < 0 {
  6748. warehouse.StockMinNumber = 0
  6749. }
  6750. warehouse.Mtime = time.Now().Unix()
  6751. if warehouse.StockMinNumber < minNumber {
  6752. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  6753. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  6754. } else {
  6755. if minNumber > 0 {
  6756. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  6757. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6758. } else {
  6759. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  6760. }
  6761. }
  6762. if minNumber == 0 && maxNumber != 1 {
  6763. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6764. }
  6765. }
  6766. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  6767. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6768. if warehouse.StockMinNumber == 0 {
  6769. warehouse.StockMaxNumber = stock_max_number
  6770. }
  6771. }
  6772. if drup.MaxUnit != drup.MinUnit {
  6773. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  6774. return errors.New("库存数量不足")
  6775. }
  6776. }
  6777. if warehouse.StockMinNumber <= 0 {
  6778. warehouse.StockMinNumber = 0
  6779. }
  6780. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6781. fmt.Println(errThree)
  6782. //查询默认仓库
  6783. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6784. //查询默认仓库剩余多少库存
  6785. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6786. var sum_count int64
  6787. var sum_in_count int64
  6788. for _, it := range list {
  6789. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6790. if it.MaxUnit == baseDrug.MaxUnit {
  6791. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6792. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6793. sum_count += it.StockMaxNumber + it.StockMinNumber
  6794. sum_in_count += it.WarehousingCount
  6795. }
  6796. }
  6797. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6798. return nil
  6799. } else {
  6800. warehouse.StockMaxNumber = 0
  6801. warehouse.StockMinNumber = 0
  6802. warehouse.Mtime = time.Now().Unix()
  6803. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6804. fmt.Println(errThree)
  6805. //查询默认仓库
  6806. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6807. //查询默认仓库剩余多少库存
  6808. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6809. var sum_count int64
  6810. var sum_in_count int64
  6811. for _, it := range list {
  6812. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6813. if it.MaxUnit == baseDrug.MaxUnit {
  6814. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6815. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6816. sum_count += it.StockMaxNumber + it.StockMinNumber
  6817. sum_in_count += it.WarehousingCount
  6818. }
  6819. }
  6820. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6821. if errThree != nil {
  6822. return errThree
  6823. }
  6824. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  6825. prescribingNumber_two_temp := deliver_number - stock_number
  6826. UpdateWarehouseInfoTwentyFive(prescribingNumber_two_temp, drugid, orgID, storehouse_id, drup)
  6827. }
  6828. return
  6829. }
  6830. func UpdateWarehouseInfoTentyFour(count int64, id int64) error {
  6831. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6832. return err
  6833. }
  6834. func UpdateWarehouseInfoTentyThree(count int64, id int64) error {
  6835. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6836. return err
  6837. }
  6838. func GetDrugOpenConfig(orgid int64) (*models.DrugOutConfig, error) {
  6839. config := models.DrugOutConfig{}
  6840. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6841. if err == gorm.ErrRecordNotFound {
  6842. return nil, err
  6843. }
  6844. if err != nil {
  6845. return nil, err
  6846. }
  6847. return &config, nil
  6848. }
  6849. func GetDrugOpenConfigOne(orgid int64) (models.DrugOutConfig, error) {
  6850. config := models.DrugOutConfig{}
  6851. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).First(&config).Error
  6852. return config, err
  6853. }
  6854. func FindXTHisRecordByOrgIdSix(org_id int64) (models.XtHisConfig, error) {
  6855. config := models.XtHisConfig{}
  6856. err = readDb.Model(&models.XtHisConfig{}).Where("status = 1 AND user_org_id = ?", org_id).Find(&config).Error
  6857. return config, err
  6858. }
  6859. func CreateDrugOutConfig(config models.DrugOutConfig) error {
  6860. err := XTWriteDB().Create(&config).Error
  6861. return err
  6862. }
  6863. func UpdateDrugOutConfig(orgid int64, drug_out_open int64) error {
  6864. err := XTWriteDB().Model(&models.DrugOutConfig{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": drug_out_open}).Error
  6865. return err
  6866. }
  6867. func FindDrugOutConfigById(orgid int64) (models.DrugOutConfig, error) {
  6868. config := models.DrugOutConfig{}
  6869. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6870. return config, err
  6871. }
  6872. func FindeDrugSettleConfigById(orgid int64) (models.HisSettleStockConfig, error) {
  6873. config := models.HisSettleStockConfig{}
  6874. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6875. return config, err
  6876. }
  6877. func UpdatedWarehouseOut(id int64) error {
  6878. err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6879. err = XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6880. return err
  6881. }
  6882. func UpdatedNewWarehouseOut(id int64, tx *gorm.DB) error {
  6883. err := tx.Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6884. if err != nil {
  6885. tx.Rollback()
  6886. return err
  6887. }
  6888. err = tx.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6889. if err != nil {
  6890. tx.Rollback()
  6891. return err
  6892. }
  6893. return err
  6894. }
  6895. func UpdatedSigleWarehouseOutInfo(id int64, info *models.WarehouseOutInfo) error {
  6896. tx := XTWriteDB().Begin()
  6897. err := tx.Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_info_id": info.WarehouseInfotId}).Error
  6898. if err != nil {
  6899. tx.Rollback()
  6900. return err
  6901. }
  6902. tx.Commit()
  6903. return err
  6904. }
  6905. func UpdatedNewSigleWarehouseOutInfo(id int64, info *models.WarehouseOutInfo, tx *gorm.DB) error {
  6906. err := tx.Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_info_id": info.WarehouseInfotId}).Error
  6907. if err != nil {
  6908. tx.Rollback()
  6909. return err
  6910. }
  6911. return err
  6912. }
  6913. func GetLastGoodWarehouseOutInfo(orgid int64, id int64, tx *gorm.DB) (info []*models.WarehouseOutInfo, err error) {
  6914. err = tx.Where("org_id = ? and status = 1 and warehouse_out_id = ?", orgid, id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  6915. return tx.Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  6916. }).Find(&info).Error
  6917. if err != nil {
  6918. tx.Rollback()
  6919. return info, err
  6920. }
  6921. return info, err
  6922. }
  6923. func GetLastGoodWarehouseOutInfoById(orgid int64) (models.WarehouseOutInfo, error) {
  6924. info := models.WarehouseOutInfo{}
  6925. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&info).Error
  6926. return info, err
  6927. }
  6928. func ModifyWarehouseOutInfo(id int64, info *models.WarehouseOutInfo, tx *gorm.DB) error {
  6929. 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
  6930. if err != nil {
  6931. tx.Rollback()
  6932. return err
  6933. }
  6934. return err
  6935. }
  6936. func UpdateWarehouseOutFlow(info models.VmStockFlow, id int64) error {
  6937. 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
  6938. return err
  6939. }
  6940. func GetLastGoodWarehouseOutTwenty(orgid int64) (models.WarehouseOut, error) {
  6941. out := models.WarehouseOut{}
  6942. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  6943. return out, err
  6944. }
  6945. func GetLastGoodWarehouseOutTwentyOne(orgid int64, tx *gorm.DB) (models.WarehouseOut, error) {
  6946. out := models.WarehouseOut{}
  6947. err := tx.Where("org_id = ? and status = 1", orgid).Last(&out).Error
  6948. if err != gorm.ErrRecordNotFound {
  6949. if err != nil {
  6950. tx.Rollback()
  6951. return out, err
  6952. }
  6953. }
  6954. return out, err
  6955. }
  6956. func UpdateaGoodWarehouseInfo(count int64, good_id int64, orgid int64, warehouse_info_id int64) error {
  6957. ut := writeDb.Begin()
  6958. 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
  6959. if err != nil {
  6960. ut.Rollback()
  6961. return err
  6962. }
  6963. ut.Commit()
  6964. return err
  6965. }
  6966. func ModifyGoodOutInfoLog(good_id int64, id int64, tx *gorm.DB) (models.XtGoodWarehouseOutInfoLog, error) {
  6967. outInfoLog := models.XtGoodWarehouseOutInfoLog{}
  6968. err := tx.Model(&outInfoLog).Where("good_id = ? and warehouse_out_info_id = ?", good_id, id).Updates(map[string]interface{}{"status": 0}).Error
  6969. if err != nil {
  6970. tx.Rollback()
  6971. return outInfoLog, err
  6972. }
  6973. return outInfoLog, err
  6974. }
  6975. func UpdateaNewGoodWarehouseInfo(count int64, good_id int64, orgid int64, warehouse_info_id int64, tx *gorm.DB) error {
  6976. 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
  6977. if err != nil {
  6978. tx.Rollback()
  6979. return err
  6980. }
  6981. return err
  6982. }
  6983. func ModifyGoodWarehouseOut(id int64) error {
  6984. ut := writeDb.Begin()
  6985. err := ut.Model(&models.WarehouseOut{}).Where("id = ? and status =1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6986. if err != nil {
  6987. ut.Rollback()
  6988. return err
  6989. }
  6990. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6991. if err != nil {
  6992. ut.Rollback()
  6993. return err
  6994. }
  6995. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  6996. if err != nil {
  6997. ut.Rollback()
  6998. return err
  6999. }
  7000. err = ut.Model(&models.XtWarehouseFlushInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  7001. if err != nil {
  7002. ut.Rollback()
  7003. return err
  7004. }
  7005. ut.Commit()
  7006. return err
  7007. }
  7008. func ModifyNewGoodWarehouseOut(id int64, tx *gorm.DB) error {
  7009. err := tx.Model(&models.WarehouseOut{}).Where("id = ? and status =1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7010. if err != nil {
  7011. tx.Rollback()
  7012. return err
  7013. }
  7014. err = tx.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7015. if err != nil {
  7016. tx.Rollback()
  7017. return err
  7018. }
  7019. err = tx.Model(&models.VmStockFlow{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  7020. if err != nil {
  7021. tx.Rollback()
  7022. return err
  7023. }
  7024. err = tx.Model(&models.XtWarehouseFlushInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  7025. if err != nil {
  7026. tx.Rollback()
  7027. return err
  7028. }
  7029. return err
  7030. }
  7031. func GetGoodWarehouseInfoById(waresing_id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  7032. err = XTReadDB().Where("org_id = ? and status = 1 and warehousing_id = ?", orgid, waresing_id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  7033. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  7034. }).Find(&info).Error
  7035. return info, err
  7036. }
  7037. func GetNewGoodWarehouseInfoById(waresing_id int64, orgid int64, tx *gorm.DB) (info []*models.VmWarehousingInfoSix, err error) {
  7038. err = tx.Where("org_id = ? and status = 1 and warehousing_id = ?", orgid, waresing_id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  7039. return tx.Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  7040. }).Find(&info).Error
  7041. if err != gorm.ErrRecordNotFound {
  7042. if err != nil {
  7043. tx.Rollback()
  7044. return info, err
  7045. }
  7046. }
  7047. return info, err
  7048. }
  7049. func FindLastWarehousing(order string) (info models.Warehousing, err error) {
  7050. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  7051. return info, err
  7052. }
  7053. func UpdateWarehouseDetail(info *models.WarehousingInfo, id int64) error {
  7054. 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
  7055. return err
  7056. }
  7057. func UpdateNewWarehouseDetail(info *models.WarehousingInfo, id int64, tx *gorm.DB) error {
  7058. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"good_id": info.GoodId, "good_type_id": info.GoodTypeId, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "warehousing_count": info.WarehousingCount, "warehousing_unit": info.WarehousingUnit, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "license_number": info.LicenseNumber, "packing_price": info.PackingPrice, "register_number": info.RegisterNumber, "storehouse_id": info.StorehouseId, "good_code": info.GoodCode}).Error
  7059. if err != nil {
  7060. tx.Rollback()
  7061. return err
  7062. }
  7063. return err
  7064. }
  7065. func UpdateCheckWarehouseInfo(id int64, checker int64) error {
  7066. tx := XTWriteDB().Begin()
  7067. err := tx.Model(&models.Warehousing{}).Where("id =? and status = 1", id).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  7068. if err != nil {
  7069. tx.Rollback()
  7070. return err
  7071. }
  7072. tx.Commit()
  7073. return err
  7074. }
  7075. func UpdateNewCheckWarehouseInfo(id int64, checker int64, tx *gorm.DB) error {
  7076. err := tx.Model(&models.Warehousing{}).Where("id =? and status = 1", id).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  7077. if err != nil {
  7078. tx.Rollback()
  7079. return err
  7080. }
  7081. return err
  7082. }
  7083. func GetWarehouseInfoList(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  7084. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7085. return info, err
  7086. }
  7087. func GetNewWarehouseInfoList(id int64, orgid int64, tx *gorm.DB) (info []*models.WarehousingInfo, err error) {
  7088. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7089. if err != gorm.ErrRecordNotFound {
  7090. if err != nil {
  7091. tx.Rollback()
  7092. return info, err
  7093. }
  7094. }
  7095. return info, err
  7096. }
  7097. func UpdateWarehouseInfoByIdListThree(count int64, id int64) error {
  7098. tx := XTWriteDB().Begin()
  7099. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"stock_count": count}).Error
  7100. if err != nil {
  7101. tx.Rollback()
  7102. return err
  7103. }
  7104. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7105. if err != nil {
  7106. tx.Rollback()
  7107. return err
  7108. }
  7109. tx.Commit()
  7110. return err
  7111. }
  7112. func UpdateNewWarehouseInfoByIdListThree(count int64, id int64, tx *gorm.DB) error {
  7113. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"stock_count": count}).Error
  7114. if err != nil {
  7115. tx.Rollback()
  7116. return err
  7117. }
  7118. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7119. if err != nil {
  7120. tx.Rollback()
  7121. return err
  7122. }
  7123. return err
  7124. }
  7125. func FindLastWarehousingSix(org_id int64) (info models.Warehousing, err error) {
  7126. err = readDb.Model(&models.Warehousing{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7127. return info, err
  7128. }
  7129. func FindNewLastWarehousingSix(org_id int64, tx *gorm.DB) (info models.Warehousing, err error) {
  7130. err = tx.Model(&models.Warehousing{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7131. if err != gorm.ErrRecordNotFound {
  7132. if err != nil {
  7133. tx.Rollback()
  7134. return info, err
  7135. }
  7136. }
  7137. return info, err
  7138. }
  7139. func FindLastWarehousingSeven(org_id int64) (info models.DrugWarehouse, err error) {
  7140. err = readDb.Model(&models.DrugWarehouse{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7141. return info, err
  7142. }
  7143. func FindNewLastWarehousingSeven(org_id int64, tx *gorm.DB) (info models.DrugWarehouse, err error) {
  7144. err = tx.Model(&models.DrugWarehouse{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7145. if err != gorm.ErrRecordNotFound {
  7146. if err != nil {
  7147. tx.Rollback()
  7148. return info, err
  7149. }
  7150. }
  7151. return info, err
  7152. }
  7153. func FindDrugWarehouseInfoListById(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7154. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "status = 1 and org_id =?", orgid).Find(&info).Error
  7155. return info, err
  7156. }
  7157. func FindNewDrugWarehouseInfoListById(id int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7158. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "status = 1 and org_id =?", orgid).Find(&info).Error
  7159. if err != gorm.ErrRecordNotFound {
  7160. if err != nil {
  7161. tx.Rollback()
  7162. return info, err
  7163. }
  7164. }
  7165. return info, err
  7166. }
  7167. func CreatedWarehouseing(info *models.DrugWarehouseInfo) error {
  7168. err := XTWriteDB().Create(&info).Error
  7169. return err
  7170. }
  7171. func CreatedNewWarehouseing(info *models.DrugWarehouseInfo, tx *gorm.DB) error {
  7172. err := tx.Create(&info).Error
  7173. if err != nil {
  7174. tx.Rollback()
  7175. return err
  7176. }
  7177. return err
  7178. }
  7179. func UpdateWarehouseing(info *models.DrugWarehouseInfo) error {
  7180. err := XTWriteDB().Save(&info).Error
  7181. return err
  7182. }
  7183. func UpdateNewWarehouseing(info *models.DrugWarehouseInfo, tx *gorm.DB) error {
  7184. err := tx.Save(&info).Error
  7185. if err != nil {
  7186. tx.Rollback()
  7187. return err
  7188. }
  7189. return err
  7190. }
  7191. func CheckWarehousingInfo(id int64, orgid int64, checker int64) error {
  7192. tx := XTWriteDB().Begin()
  7193. 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
  7194. if err != nil {
  7195. tx.Rollback()
  7196. return err
  7197. }
  7198. tx.Commit()
  7199. return err
  7200. }
  7201. func CheckNewWarehousingInfo(id int64, orgid int64, checker int64, tx *gorm.DB) error {
  7202. 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
  7203. if err != nil {
  7204. tx.Rollback()
  7205. return err
  7206. }
  7207. return err
  7208. }
  7209. func GetWarehousingInfoByList(id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  7210. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7211. return info, err
  7212. }
  7213. func GetNewWarehousingInfoByList(id int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
  7214. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7215. if err != gorm.ErrRecordNotFound {
  7216. if err != nil {
  7217. tx.Rollback()
  7218. return info, err
  7219. }
  7220. }
  7221. return info, err
  7222. }
  7223. func AddDrugWarehouseStockMaxNumber(count int64, id int64) error {
  7224. tx := XTWriteDB().Begin()
  7225. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7226. if err != nil {
  7227. tx.Rollback()
  7228. return err
  7229. }
  7230. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7231. if err != nil {
  7232. tx.Rollback()
  7233. return err
  7234. }
  7235. tx.Commit()
  7236. return err
  7237. }
  7238. func AddNewDrugWarehouseStockMaxNumber(count int64, id int64, tx *gorm.DB) error {
  7239. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7240. if err != nil {
  7241. tx.Rollback()
  7242. return err
  7243. }
  7244. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7245. if err != nil {
  7246. tx.Rollback()
  7247. return err
  7248. }
  7249. return err
  7250. }
  7251. func UpdateWarehousingInfoFlow(id int64, orgid int64) error {
  7252. tx := XTWriteDB().Begin()
  7253. 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
  7254. if err != nil {
  7255. tx.Rollback()
  7256. return err
  7257. }
  7258. tx.Commit()
  7259. return err
  7260. }
  7261. func UpdateNewWarehousingInfoFlow(id int64, orgid int64, tx *gorm.DB) error {
  7262. 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
  7263. if err != nil {
  7264. tx.Rollback()
  7265. return err
  7266. }
  7267. return err
  7268. }
  7269. func AddDrugWarehouseStockMinNumber(count int64, id int64) error {
  7270. tx := XTWriteDB().Begin()
  7271. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7272. if err != nil {
  7273. tx.Rollback()
  7274. return err
  7275. }
  7276. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7277. tx.Commit()
  7278. return err
  7279. }
  7280. func AddNewDrugWarehouseStockMinNumber(count int64, id int64, tx *gorm.DB) error {
  7281. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7282. if err != nil {
  7283. tx.Rollback()
  7284. return err
  7285. }
  7286. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7287. if err != nil {
  7288. tx.Rollback()
  7289. return err
  7290. }
  7291. return err
  7292. }
  7293. func ReturnCheckWarehouseingInfo(id int64, orgid int64) error {
  7294. tx := XTWriteDB().Begin()
  7295. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7296. if err != nil {
  7297. tx.Rollback()
  7298. return err
  7299. }
  7300. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("warehousing_id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7301. if err != nil {
  7302. tx.Rollback()
  7303. return err
  7304. }
  7305. tx.Commit()
  7306. return err
  7307. }
  7308. func ReturnNewCheckWarehouseingInfo(id int64, orgid int64, tx *gorm.DB) error {
  7309. err := tx.Model(&models.DrugWarehouse{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7310. if err != nil {
  7311. tx.Rollback()
  7312. return err
  7313. }
  7314. err = tx.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7315. if err != nil {
  7316. tx.Rollback()
  7317. return err
  7318. }
  7319. err = tx.Model(models.XtDrugWarehouseInfoLog{}).Where("warehouse_info_id = ? and status=1", id).Update(map[string]interface{}{"status": 0}).Error
  7320. if err != nil {
  7321. tx.Rollback()
  7322. return err
  7323. }
  7324. return err
  7325. }
  7326. func AddDrugReturnWarehouseStockMaxNumber(count int64, id int64) error {
  7327. tx := XTWriteDB().Begin()
  7328. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  7329. if err != nil {
  7330. tx.Rollback()
  7331. return err
  7332. }
  7333. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7334. if err != nil {
  7335. tx.Rollback()
  7336. return err
  7337. }
  7338. tx.Commit()
  7339. return err
  7340. }
  7341. func AddNewDrugReturnWarehouseStockMaxNumber(count int64, id int64, tx *gorm.DB) error {
  7342. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  7343. if err != nil {
  7344. tx.Rollback()
  7345. return err
  7346. }
  7347. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7348. if err != nil {
  7349. tx.Rollback()
  7350. return err
  7351. }
  7352. return err
  7353. }
  7354. func AddDrugReturnWarehouseStockMinNumber(count int64, id int64) error {
  7355. tx := XTWriteDB().Begin()
  7356. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  7357. if err != nil {
  7358. tx.Rollback()
  7359. return err
  7360. }
  7361. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7362. if err != nil {
  7363. tx.Rollback()
  7364. return err
  7365. }
  7366. tx.Commit()
  7367. return err
  7368. }
  7369. func AddNewDrugReturnWarehouseStockMinNumber(count int64, id int64, tx *gorm.DB) error {
  7370. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  7371. if err != nil {
  7372. tx.Rollback()
  7373. return err
  7374. }
  7375. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7376. if err != nil {
  7377. tx.Rollback()
  7378. return err
  7379. }
  7380. return err
  7381. }
  7382. func UpdateDrugWarehouseInfoTwenty(id int64, info *models.DrugWarehouseInfo) error {
  7383. 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
  7384. return err
  7385. }
  7386. func UpdateNewDrugWarehouseInfoTwenty(id int64, info *models.DrugWarehouseInfo, tx *gorm.DB) error {
  7387. 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
  7388. if err != nil {
  7389. tx.Rollback()
  7390. return err
  7391. }
  7392. return err
  7393. }
  7394. func UpdatedDrugWarehouseInfo(info *models.DrugWarehouseOutInfo, id int64) error {
  7395. 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
  7396. return err
  7397. }
  7398. func UpdatedNewDrugWarehouseInfo(info *models.DrugWarehouseOutInfo, id int64, tx *gorm.DB) error {
  7399. 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
  7400. if err != nil {
  7401. tx.Rollback()
  7402. return err
  7403. }
  7404. return err
  7405. }
  7406. func UpdatedSelfDrugWarehouseInfo(info *models.XtSelfDrugWarehouseOutInfo, id int64, tx *gorm.DB) error {
  7407. 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
  7408. if err != gorm.ErrRecordNotFound {
  7409. return err
  7410. }
  7411. return err
  7412. }
  7413. func GetDrugLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  7414. out := models.DrugWarehouseOut{}
  7415. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  7416. return out, err
  7417. }
  7418. func GetNewDrugLastWarehouseOut(orgid int64, tx *gorm.DB) (models.DrugWarehouseOut, error) {
  7419. out := models.DrugWarehouseOut{}
  7420. err := tx.Where("org_id = ? and status = 1", orgid).Find(&out).Error
  7421. if err != gorm.ErrRecordNotFound {
  7422. if err != nil {
  7423. tx.Rollback()
  7424. return out, err
  7425. }
  7426. }
  7427. return out, err
  7428. }
  7429. func GetSelfDrugLastWarehouseOut(orgid int64, tx *gorm.DB) (models.XtSelfDrugWarehouseOut, error) {
  7430. out := models.XtSelfDrugWarehouseOut{}
  7431. err := tx.Where("org_id = ? and status = 1", orgid).Find(&out).Error
  7432. if err != gorm.ErrRecordNotFound {
  7433. if err != nil {
  7434. tx.Rollback()
  7435. return out, err
  7436. }
  7437. }
  7438. return out, err
  7439. }
  7440. func GetSelfDrugLastWarehouseOutOne(id int64, tx *gorm.DB) (models.XtSelfDrugWarehouseOut, error) {
  7441. out := models.XtSelfDrugWarehouseOut{}
  7442. err := tx.Where("id = ? and status = 1", id).Find(&out).Error
  7443. if err != gorm.ErrRecordNotFound {
  7444. if err != nil {
  7445. tx.Rollback()
  7446. return out, err
  7447. }
  7448. }
  7449. return out, err
  7450. }
  7451. func GetDrugWarehouseOutListById(warehouse_out_id int64, orgId int64) (info []*models.DrugWarehouseOutInfo, err error) {
  7452. err = XTReadDB().Where("warehouse_out_id = ? and org_id =? and status = 1", warehouse_out_id, orgId).Preload("BaseDrugLib", "status=1").Find(&info).Error
  7453. return info, err
  7454. }
  7455. func GetNewDrugWarehouseOutListById(warehouse_out_id int64, orgId int64, tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  7456. err = tx.Where("warehouse_out_id = ? and org_id =? and status = 1", warehouse_out_id, orgId).Preload("BaseDrugLib", "status=1").Find(&info).Error
  7457. if err != gorm.ErrRecordNotFound {
  7458. if err != nil {
  7459. tx.Rollback()
  7460. return info, err
  7461. }
  7462. }
  7463. return info, err
  7464. }
  7465. func UpdateCheckDrugOut(out models.DrugWarehouseOut, id int64) error {
  7466. tx := XTWriteDB().Begin()
  7467. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": out.IsCheck}).Error
  7468. if err != nil {
  7469. tx.Rollback()
  7470. return err
  7471. }
  7472. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7473. if err != nil {
  7474. tx.Rollback()
  7475. return err
  7476. }
  7477. tx.Commit()
  7478. return err
  7479. }
  7480. func UpdateNewCheckDrugOut(out models.DrugWarehouseOut, id int64, tx *gorm.DB) error {
  7481. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": out.IsCheck}).Error
  7482. if err != nil {
  7483. tx.Rollback()
  7484. return err
  7485. }
  7486. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7487. if err != nil {
  7488. tx.Rollback()
  7489. return err
  7490. }
  7491. return err
  7492. }
  7493. func UpdateCheckDrugOutInfoList(info models.DrugWarehouseOutInfo, warehouse_out_id int64) error {
  7494. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", warehouse_out_id).Update(map[string]interface{}{"is_check": info.IsCheck}).Error
  7495. return err
  7496. }
  7497. func UpdateNewCheckDrugOutInfoList(info models.DrugWarehouseOutInfo, warehouse_out_id int64, tx *gorm.DB) error {
  7498. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", warehouse_out_id).Update(map[string]interface{}{"is_check": info.IsCheck}).Error
  7499. if err != nil {
  7500. tx.Rollback()
  7501. return err
  7502. }
  7503. return err
  7504. }
  7505. func GetDrugWarehouseOutByLastId(id int64) (models.DrugWarehouseOut, error) {
  7506. out := models.DrugWarehouseOut{}
  7507. err := XTReadDB().Where("id = ? and status = 1", id).Find(&out).Error
  7508. return out, err
  7509. }
  7510. func GetNewDrugWarehouseOutByLastId(id int64, tx *gorm.DB) (models.DrugWarehouseOut, error) {
  7511. out := models.DrugWarehouseOut{}
  7512. err := tx.Where("id = ? and status = 1", id).Find(&out).Error
  7513. if err != gorm.ErrRecordNotFound {
  7514. if err != nil {
  7515. tx.Rollback()
  7516. return out, err
  7517. }
  7518. }
  7519. return out, err
  7520. }
  7521. func UpdateDrugMaxNumber(count int64, id int64) error {
  7522. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7523. return err
  7524. }
  7525. func UpdateNewDrugMaxNumber(count int64, id int64, tx *gorm.DB) error {
  7526. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7527. if err != gorm.ErrRecordNotFound {
  7528. if err != nil {
  7529. tx.Rollback()
  7530. return err
  7531. }
  7532. }
  7533. return err
  7534. }
  7535. func UpdateNewDrugMaxNumberOne(count int64, id int64, tx *gorm.DB) error {
  7536. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  7537. if err != gorm.ErrRecordNotFound {
  7538. if err != nil {
  7539. tx.Rollback()
  7540. return err
  7541. }
  7542. }
  7543. return err
  7544. }
  7545. func UpdateDrugMinNumber(count int64, id int64) error {
  7546. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  7547. return err
  7548. }
  7549. func UpdateNewDrugMinNumber(count int64, id int64, tx *gorm.DB) error {
  7550. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  7551. if err != nil {
  7552. tx.Rollback()
  7553. return err
  7554. }
  7555. return err
  7556. }
  7557. func DeleteDrugOutFlow(id int64, orgid int64) error {
  7558. 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
  7559. return err
  7560. }
  7561. func DeleteNewDrugOutFlow(id int64, orgid int64, tx *gorm.DB) error {
  7562. 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
  7563. if err != nil {
  7564. tx.Rollback()
  7565. return err
  7566. }
  7567. return err
  7568. }
  7569. func GetWarehouseOutByWarehouseInfoId(warehouse_info_id int64, good_id int64, orgid int64) (outInfo []*models.WarehouseOutInfo, err error) {
  7570. 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
  7571. return outInfo, err
  7572. }
  7573. func GetNewWarehouseOutByWarehouseInfoId(warehouse_info_id int64, good_id int64, orgid int64, tx *gorm.DB) (outInfo []*models.WarehouseOutInfo, err error) {
  7574. 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
  7575. if err != gorm.ErrRecordNotFound {
  7576. if err != nil {
  7577. tx.Rollback()
  7578. return outInfo, err
  7579. }
  7580. }
  7581. return outInfo, err
  7582. }
  7583. func GetDrugWarehouseOutByWarehouseInfoId(warehouse_info_id int64, drug_id int64, orgid int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  7584. 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
  7585. return outInfo, err
  7586. }
  7587. func GetNewDrugWarehouseOutByWarehouseInfoId(warehouse_info_id int64, drug_id int64, orgid int64, tx *gorm.DB) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  7588. 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
  7589. if err != gorm.ErrRecordNotFound {
  7590. if err != nil {
  7591. tx.Rollback()
  7592. return outInfo, err
  7593. }
  7594. }
  7595. return outInfo, err
  7596. }
  7597. func GetAllStockInList(id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  7598. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("GoodInfo", "status = 1 and org_id =?", orgid).Find(&info).Error
  7599. return info, err
  7600. }
  7601. func GetNewAllStockInList(id int64, orgid int64, tx *gorm.DB) (info []*models.VmWarehousingInfoSix, err error) {
  7602. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("GoodInfo", "status = 1 and org_id =?", orgid).Find(&info).Error
  7603. if err != gorm.ErrRecordNotFound {
  7604. if err != nil {
  7605. tx.Rollback()
  7606. return info, err
  7607. }
  7608. }
  7609. return info, err
  7610. }
  7611. func GetDrugWarehouseInfoList(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7612. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "org_id = ? and status = 1", orgid).Find(&info).Error
  7613. return info, err
  7614. }
  7615. func GetNewDrugWarehouseInfoList(id int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7616. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "org_id = ? and status = 1", orgid).Find(&info).Error
  7617. if err != gorm.ErrRecordNotFound {
  7618. if err != nil {
  7619. tx.Rollback()
  7620. return info, err
  7621. }
  7622. }
  7623. return info, err
  7624. }
  7625. func GetStockCountByGoodId(goodid int64, storehouse_id int64, orgId int64) (info []*models.WarehousingInfo, err error) {
  7626. 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
  7627. return info, err
  7628. }
  7629. func GetNewStockCountByGoodId(goodid int64, storehouse_id int64, orgId int64, tx *gorm.DB) (info []*models.WarehousingInfo, err error) {
  7630. 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
  7631. if err != gorm.ErrRecordNotFound {
  7632. if err != nil {
  7633. tx.Rollback()
  7634. return info, err
  7635. }
  7636. }
  7637. return info, err
  7638. }
  7639. func UpdateGoodInfoSumCount(goodid int64, sum_count int64, orgid int64) error {
  7640. 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
  7641. 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
  7642. return err
  7643. }
  7644. func UpdateGoodInfoSumCountSix(goodid int64, sum_count int64, orgid int64) error {
  7645. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7646. return err
  7647. }
  7648. func UpdateGoodInfoReduceSumCount(goodid int64, sum_count int64, orgid int64) error {
  7649. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7650. return err
  7651. }
  7652. func UpdateNewGoodInfoReduceSumCount(goodid int64, sum_count int64, orgid int64, tx *gorm.DB) error {
  7653. err = tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7654. if err != gorm.ErrRecordNotFound {
  7655. if err != nil {
  7656. tx.Rollback()
  7657. return err
  7658. }
  7659. }
  7660. return err
  7661. }
  7662. func GetGoodFlowByWarehouseOutId(good_id int64, user_org_id int64, id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  7663. 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
  7664. if err != gorm.ErrRecordNotFound {
  7665. if err != nil {
  7666. tx.Rollback()
  7667. return flow, err
  7668. }
  7669. }
  7670. return
  7671. }
  7672. func UpdateGoodInfoAddSumCount(goodid int64, sum_count int64, orgid int64, sum_in_count int64) error {
  7673. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7674. 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
  7675. return err
  7676. }
  7677. func UpdateNewGoodInfoAddSumCount(goodid int64, sum_count int64, orgid int64, sum_in_count int64, tx *gorm.DB) error {
  7678. err := tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7679. if err != nil {
  7680. tx.Rollback()
  7681. return err
  7682. }
  7683. 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
  7684. if err != nil {
  7685. tx.Rollback()
  7686. return err
  7687. }
  7688. return err
  7689. }
  7690. func GetCancelStockInfo(id int64, orgid int64) (stock []*models.CancelStockInfo, err error) {
  7691. 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
  7692. return stock, err
  7693. }
  7694. func ModifyCancelStockInfo(id int64, info *models.CancelStockInfo) error {
  7695. 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
  7696. return err
  7697. }
  7698. func GetCancelStockInfoListById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  7699. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  7700. return info, err
  7701. }
  7702. func GetWarehouseOutList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.WarehouseOutInfo, err error) {
  7703. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  7704. return info, err
  7705. }
  7706. func GetCancelWarehouseList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.CancelStockInfo, err error) {
  7707. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  7708. return info, err
  7709. }
  7710. func CheckCancelStock(stock models.CancelStock, cancel_stock_id int64, orgid int64) error {
  7711. 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
  7712. 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
  7713. return err
  7714. }
  7715. func GetCancelStockInfoById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  7716. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&info).Error
  7717. return info, err
  7718. }
  7719. func UpdateCancelStockNumber(stock_count int64, warehouse_info_id int64, orgid int64) error {
  7720. 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
  7721. return err
  7722. }
  7723. func UpdateGoodSumCount(sum_count int64, good_id int64, orgid int64) error {
  7724. 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
  7725. return err
  7726. }
  7727. func UpdateStockWarehouseInfo(cancel_stock_id int64, orgid int64, stock models.CancelStock) error {
  7728. 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
  7729. 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
  7730. return err
  7731. }
  7732. func UpdateStockWarehouseInfoStockFlow(id int64, orgid int64, goodid int64) error {
  7733. 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
  7734. return err
  7735. }
  7736. func UpdateStockNumberWarehouseInfo(warehouse_info_id int64, good_id int64, org_id int64, stock_count int64) error {
  7737. 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
  7738. 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
  7739. return err
  7740. }
  7741. func UpdateDrugSumCount(drugid int64, sum_count int64, sum_in_count int64, orgid int64) error {
  7742. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ?", drugid, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  7743. 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
  7744. return err
  7745. }
  7746. func GetDrugStockWarehouseInfo(id int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfo, err error) {
  7747. err = XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1 and storehouse_id = ?", id, orgid, storehouse_id).Find(&info).Error
  7748. return info, err
  7749. }
  7750. func UpdateBaseDrugSum(drugid int64, orgid int64, sum_count int64, sum_in_count int64) error {
  7751. 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
  7752. 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
  7753. return err
  7754. }
  7755. func GetDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64) (info []*models.DrugWarehouseInfo, err error) {
  7756. 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
  7757. return info, err
  7758. }
  7759. func GetSelfDrugAllStockInfoOne(patient_id int64, orgid int64, drugid int64) (info []*models.XtSelfDrugWarehouseInfo, err error) {
  7760. 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
  7761. return info, err
  7762. }
  7763. func GetNewDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
  7764. 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
  7765. if err != gorm.ErrRecordNotFound {
  7766. if err != nil {
  7767. tx.Rollback()
  7768. return info, err
  7769. }
  7770. }
  7771. return info, err
  7772. }
  7773. func UpdateBaseDrugSumInfo(sum_count int64, drugid int64, orgid int64, sum_in_count int64) error {
  7774. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7775. 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
  7776. return err
  7777. }
  7778. func UpdateNewBaseDrugSumInfo(sum_count int64, drugid int64, orgid int64, sum_in_count int64, tx *gorm.DB) error {
  7779. err := tx.Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7780. if err != gorm.ErrRecordNotFound {
  7781. if err != nil {
  7782. tx.Rollback()
  7783. return err
  7784. }
  7785. }
  7786. 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
  7787. if err != gorm.ErrRecordNotFound {
  7788. if err != nil {
  7789. tx.Rollback()
  7790. return err
  7791. }
  7792. }
  7793. return err
  7794. }
  7795. func UpdateBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64) error {
  7796. err = writeDb.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7797. return err
  7798. }
  7799. func UpdateNewBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64, tx *gorm.DB) error {
  7800. err = tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7801. if err != nil {
  7802. tx.Rollback()
  7803. return err
  7804. }
  7805. return err
  7806. }
  7807. func GetLastCancelStockByList(id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  7808. 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
  7809. return info, err
  7810. }
  7811. func GetCancelDrugStockList(cancel_stock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  7812. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  7813. return info, err
  7814. }
  7815. func ModifyCancelStock(cancel_stock_id int64, stock models.DrugCancelStock, orgid int64) error {
  7816. 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
  7817. 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
  7818. return err
  7819. }
  7820. func GetCancelDrugStock(cancel_stock_id int64, orgid int64) (models.DrugCancelStock, error) {
  7821. stock := models.DrugCancelStock{}
  7822. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&stock).Error
  7823. return stock, err
  7824. }
  7825. func GetDrugCancelCheckById(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  7826. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&stock).Error
  7827. return stock, err
  7828. }
  7829. func GetDrugCancelCheckByIdTwenty(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  7830. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 ", cancel_stock_id, orgid).Find(&stock).Error
  7831. return stock, err
  7832. }
  7833. func GetDrugWarehouseOutList(batch_number_id int64, drugId int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  7834. err = XTReadDB().Where("warehouse_info_id = ? and drug_id = ? and org_id = ? and status = 1", batch_number_id, drugId, orgid).Find(&info).Error
  7835. return info, err
  7836. }
  7837. func ModifyDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64) error {
  7838. 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
  7839. return err
  7840. }
  7841. func ModifyNewDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  7842. 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
  7843. if err != nil {
  7844. tx.Rollback()
  7845. return err
  7846. }
  7847. return err
  7848. }
  7849. func ModifyDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64) error {
  7850. 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
  7851. return err
  7852. }
  7853. func ModifyNewDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  7854. 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
  7855. if err != nil {
  7856. tx.Rollback()
  7857. return err
  7858. }
  7859. return err
  7860. }
  7861. func ModifyDrugCancelStockInfo(id int64, info *models.DrugCancelStockInfo) error {
  7862. 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
  7863. return err
  7864. }
  7865. func ReturnDrugCancelStock(cancelstock_id int64, stock models.DrugCancelStock) error {
  7866. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  7867. err = XTWriteDB().Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  7868. return err
  7869. }
  7870. func GetDrugCancelStockInfo(cancelstock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  7871. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancelstock_id, orgid).Find(&info).Error
  7872. return info, err
  7873. }
  7874. func ModifyDrugFlowByCancelId(cancelstock_id int64, drugId int64, orgid int64) error {
  7875. 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
  7876. return err
  7877. }
  7878. func ModifyDrugMaxNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  7879. 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
  7880. return err
  7881. }
  7882. func ModifyDrugMinNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  7883. 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
  7884. return err
  7885. }
  7886. func GetDrugInfoSix(id int64, orgid int64) (models.DrugWarehouseInfo, error) {
  7887. info := models.DrugWarehouseInfo{}
  7888. err := XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1", id, orgid).Find(&info).Error
  7889. return info, err
  7890. }
  7891. func GetSelfDrugInfoSix(id int64, orgid int64) (models.XtSelfDrugWarehouseInfo, error) {
  7892. info := models.XtSelfDrugWarehouseInfo{}
  7893. err := XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1", id, orgid).Find(&info).Error
  7894. return info, err
  7895. }
  7896. func GetPrintList(storehouse_id int64, good_name string, orgid int64, limit int64, page int64) (storeinventory []*models.XtStockInventory, err error) {
  7897. db := XTReadDB().Model(&storeinventory).Where("status = 1")
  7898. if storehouse_id > 0 {
  7899. db = db.Where("storehouse_id = ?", storehouse_id)
  7900. }
  7901. if orgid > 0 {
  7902. db = db.Where("user_org_id = ?", orgid)
  7903. }
  7904. err = XTReadDB().Preload("GoodInfo", "status = 1 and org_id = ?", orgid).Preload("WarehousingInfo", "status = 1 and org_id=?", orgid).Find(&storeinventory).Error
  7905. return storeinventory, err
  7906. }
  7907. func GetGoodDialysisOutInfo(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.AutomaticReduceDetail, error) {
  7908. detail := models.AutomaticReduceDetail{}
  7909. 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
  7910. return detail, err
  7911. }
  7912. func GetGoodDialysisOutInfoSix(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.WarehouseOutInfo, error) {
  7913. detail := models.WarehouseOutInfo{}
  7914. 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
  7915. return detail, err
  7916. }
  7917. func GetGoodSumCount(storehouse_id int64, good_id int64, orgid int64) (models.XtGoodStockCount, error) {
  7918. stock := models.XtGoodStockCount{}
  7919. err := XTReadDB().Where("storehouse_id = ? and good_id = ? and user_org_id = ? and status = 1", storehouse_id, good_id, orgid).Find(&stock).Error
  7920. return stock, err
  7921. }
  7922. func CreateGoodStockCount(stock models.XtGoodStockCount) error {
  7923. err := XTWriteDB().Create(&stock).Error
  7924. return err
  7925. }
  7926. func AddGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7927. 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
  7928. return err
  7929. }
  7930. func ReduceGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7931. ut := XTWriteDB().Begin()
  7932. 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
  7933. if err != nil {
  7934. ut.Rollback()
  7935. return err
  7936. }
  7937. ut.Commit()
  7938. return err
  7939. }
  7940. func UpdateGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64) error {
  7941. 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
  7942. return err
  7943. }
  7944. func UpdateNewGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64, tx *gorm.DB) error {
  7945. 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
  7946. if err != nil {
  7947. tx.Rollback()
  7948. return err
  7949. }
  7950. return err
  7951. }
  7952. func CreateDrugStock(stock models.XtDrugStockCount) error {
  7953. err := XTWriteDB().Create(&stock).Error
  7954. return err
  7955. }
  7956. func UpdateDrugCount(storehouse_id int64, user_org_id int64, drug_id int64, sum_in_count int64, flush_count int64) error {
  7957. ut := XTWriteDB().Begin()
  7958. 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
  7959. if err != nil {
  7960. ut.Rollback()
  7961. return err
  7962. }
  7963. 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
  7964. if err != nil {
  7965. ut.Rollback()
  7966. return err
  7967. }
  7968. ut.Commit()
  7969. return err
  7970. }
  7971. func UpdateDrugCountOne(storehouse_id int64, user_org_id int64, drug_id int64, flush_count int64) error {
  7972. ut := XTWriteDB().Begin()
  7973. 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
  7974. if err != nil {
  7975. ut.Rollback()
  7976. return err
  7977. }
  7978. ut.Commit()
  7979. return err
  7980. }
  7981. func AddDrugSumOutCount(storehouse_id int64, sum_out_count int64, user_org_id int64, drug_id int64) error {
  7982. ut := XTWriteDB().Begin()
  7983. 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
  7984. if err != nil {
  7985. ut.Rollback()
  7986. return err
  7987. }
  7988. 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
  7989. if err != nil {
  7990. ut.Rollback()
  7991. return err
  7992. }
  7993. ut.Commit()
  7994. return err
  7995. }
  7996. func ReduceDrugSumOut(storehouse_id int64, user_org_id int64, drug_id int64, sum_out_count int64) error {
  7997. ut := XTWriteDB().Begin()
  7998. 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
  7999. if err != nil {
  8000. ut.Rollback()
  8001. return err
  8002. }
  8003. ut.Commit()
  8004. return err
  8005. }
  8006. func AddGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  8007. ut := XTWriteDB().Begin()
  8008. 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
  8009. if err != nil {
  8010. ut.Rollback()
  8011. return err
  8012. }
  8013. 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
  8014. if err != nil {
  8015. ut.Rollback()
  8016. return err
  8017. }
  8018. ut.Commit()
  8019. return err
  8020. }
  8021. func ReduceGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  8022. ut := XTWriteDB().Begin()
  8023. 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
  8024. if err != nil {
  8025. ut.Rollback()
  8026. return err
  8027. }
  8028. 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
  8029. if err != nil {
  8030. ut.Rollback()
  8031. return err
  8032. }
  8033. ut.Commit()
  8034. return err
  8035. }
  8036. func GetInvetoryWarehouseInfoList(orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  8037. if orgid == 10191 || orgid == 10138 || orgid == 10278 {
  8038. 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
  8039. return info, err
  8040. } else {
  8041. 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
  8042. return info, err
  8043. }
  8044. }
  8045. func GetSettleOpenConfig(user_org_id int64) (*models.HisSettleStockConfig, error) {
  8046. config := models.HisSettleStockConfig{}
  8047. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8048. if err == gorm.ErrRecordNotFound {
  8049. return nil, err
  8050. }
  8051. if err != nil {
  8052. return nil, err
  8053. }
  8054. return &config, nil
  8055. }
  8056. func CreateSettleOpenConfig(config models.HisSettleStockConfig) error {
  8057. err := XTWriteDB().Create(&config).Error
  8058. return err
  8059. }
  8060. func UpdateSettleOpenConfig(id int64, config models.HisSettleStockConfig) error {
  8061. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_open": config.IsOpen}).Error
  8062. return err
  8063. }
  8064. func UpdateDrugStockAutomaticReduceRecordOne(orgid int64, is_open int64) (models.DrugStockConfig, error) {
  8065. config := models.DrugStockConfig{}
  8066. err := XTWriteDB().Model(&config).Where("org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  8067. return config, err
  8068. }
  8069. func UpdatePharyConfig(orgid int64, is_open int64) (models.PharmacyConfig, error) {
  8070. stockConfig := models.PharmacyConfig{}
  8071. err := XTWriteDB().Model(&stockConfig).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  8072. return stockConfig, err
  8073. }
  8074. func UpdateSettleOpenConfigOne(id int64, is_open int64) error {
  8075. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  8076. return err
  8077. }
  8078. func UpdateSettleOpenConfigTwo(id int64, is_open int64) error {
  8079. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  8080. return err
  8081. }
  8082. func GetSettleOpenConfigByOrgId(user_org_id int64) (models.HisSettleStockConfig, error) {
  8083. config := models.HisSettleStockConfig{}
  8084. err = XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8085. return config, nil
  8086. }
  8087. func GetGoodWareHouseOutInfoByGood(good_id int64, sys_record_time int64, user_org_id int64) (info []*models.WarehouseOutInfo, err error) {
  8088. 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
  8089. return info, err
  8090. }
  8091. func GetGoodInformationByGoodIdOne(good_id int64) (models.GoodInformationFourty, error) {
  8092. info := models.GoodInformationFourty{}
  8093. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  8094. return info, err
  8095. }
  8096. func GetGoodWarehouseInfoByGoodList(good_id int64) (info []*models.WarehousingInfo, err error) {
  8097. err = XTReadDB().Where("good_id = ? and status =1 and stock_count>0 and is_check = 1", good_id).Find(&info).Error
  8098. return info, err
  8099. }
  8100. func GetAllWarehouseInfo(good_id int64, user_org_id int64) (info []*models.WarehousingInfo, err error) {
  8101. err = XTReadDB().Where("good_id = ? and user_org_id =? and status=1 and is_check =1", good_id, user_org_id).Find(&info).Error
  8102. return info, err
  8103. }
  8104. func GetHisPrescriptionByGoodId(good_id int64, user_org_id int64) (project []*models.HisPrescriptionProject, err error) {
  8105. err = XTReadDB().Where("project_id = ? and user_org_id =? and status=1 and type =3", good_id, user_org_id).Find(&project).Error
  8106. return project, err
  8107. }
  8108. func GetHisDocById(id int64, orgid int64) (models.HisDoctorAdvice, error) {
  8109. advice := models.HisDoctorAdvice{}
  8110. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  8111. return advice, err
  8112. }
  8113. func GetBloodDocById(id int64, orgid int64) (models.DoctorAdvice, error) {
  8114. advice := models.DoctorAdvice{}
  8115. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  8116. return advice, err
  8117. }
  8118. func FindAllSettleWarehouseOut(org_id int64, warehouse_out_time int64, is_sys int64) (models.WarehouseOut, error) {
  8119. out := models.WarehouseOut{}
  8120. 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
  8121. return out, err
  8122. }
  8123. func GetGoodOutOpenConfig(user_org_id int64) (*models.XtGoodOutConfig, error) {
  8124. config := models.XtGoodOutConfig{}
  8125. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8126. if err == gorm.ErrRecordNotFound {
  8127. return nil, err
  8128. }
  8129. if err != nil {
  8130. return nil, err
  8131. }
  8132. return &config, nil
  8133. }
  8134. func CreateGoodOutOpenConfig(good models.XtGoodOutConfig) error {
  8135. err := XTWriteDB().Create(&good).Error
  8136. return err
  8137. }
  8138. func UpdateGoodOutOpenConfig(id int64, good models.XtGoodOutConfig) error {
  8139. err := XTWriteDB().Model(&good).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"is_open": good.IsOpen}).Error
  8140. return err
  8141. }
  8142. func FindGoodOutConfigById(user_org_id int64) (models.XtGoodOutConfig, error) {
  8143. config := models.XtGoodOutConfig{}
  8144. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8145. return config, err
  8146. }
  8147. func UpdateGoodAutoConfig(user_org_id int64) error {
  8148. gobalConfig := models.GobalConfig{}
  8149. err := XTWriteDB().Model(&gobalConfig).Where("org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  8150. return err
  8151. }
  8152. func UpdateGoodOutConfig(user_org_id int64) error {
  8153. config := models.XtGoodOutConfig{}
  8154. err := XTWriteDB().Model(&config).Where("user_org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  8155. return err
  8156. }
  8157. func GetHisGoodList(patient_id int64, record_date int64, orgid int64) (info []*models.GoodHisPrescriptionProject, err error) {
  8158. 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
  8159. return info, err
  8160. }
  8161. func GetLastGoodWarehouseOutInfoByProjectId(good_id int64, patient_id int64, sys_record_time int64, project_id int64) (good []*models.WarehouseOutInfo, err error) {
  8162. 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
  8163. return good, err
  8164. }
  8165. func ModifyGoodWarehouseInfo(good_id int64, warehouse_info_id int64, orgid int64, count int64) error {
  8166. 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
  8167. return err
  8168. }
  8169. func DeleteGoodWarehouseOutInfo(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  8170. 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
  8171. return err
  8172. }
  8173. func DeleteGoodFlow(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  8174. 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
  8175. return err
  8176. }
  8177. func FindPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  8178. out := models.WarehouseOut{}
  8179. 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
  8180. return out, err
  8181. }
  8182. func FindNewPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  8183. out := models.WarehouseOut{}
  8184. 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
  8185. if err != gorm.ErrRecordNotFound {
  8186. if err != nil {
  8187. tx.Rollback()
  8188. return out, err
  8189. }
  8190. }
  8191. return out, err
  8192. }
  8193. func FindLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  8194. out := models.WarehouseOut{}
  8195. 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
  8196. return out, err
  8197. }
  8198. func FindNewLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  8199. out := models.WarehouseOut{}
  8200. 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
  8201. if err != gorm.ErrRecordNotFound {
  8202. if err != nil {
  8203. tx.Rollback()
  8204. return out, err
  8205. }
  8206. }
  8207. return out, err
  8208. }
  8209. func GetGoodTypeObj(goodid int64, org_id int64) (models.GoodsType, error) {
  8210. goodsType := models.GoodsType{}
  8211. err := XTReadDB().Where("good_id = ? and org_id = ? and status = 1", goodid, org_id).Find(&goodsType).Error
  8212. return goodsType, err
  8213. }
  8214. func GetAllGoodTypeName(good_type_id int64, org_id int64) (good_type_name string) {
  8215. goodsTypeList, _ := GetAllGoodType(org_id)
  8216. for _, item := range goodsTypeList {
  8217. if good_type_id == item.ID {
  8218. good_type_name = item.TypeName
  8219. }
  8220. }
  8221. return good_type_name
  8222. }
  8223. func UpdateGoodTypeName(good_id int64, good_type_name string) (models.GoodInformation, error) {
  8224. information := models.GoodInformation{}
  8225. err := XTWriteDB().Model(&information).Where("id = ? and status =1", good_id).Updates(map[string]interface{}{"good_type_name": good_type_name}).Error
  8226. return information, err
  8227. }
  8228. func GetSencondeGoodWarehouseInfo(good_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  8229. err = XTReadDB().Where("good_id = ? and org_id= ? and status= 1 and is_check = 1", good_id, org_id).Find(&info).Error
  8230. return info, err
  8231. }
  8232. func UpdateGoodSumCountSeven(sum_count int64, good_id int64, orgid int64) error {
  8233. ut := writeDb.Begin()
  8234. 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
  8235. if err != nil {
  8236. ut.Rollback()
  8237. return err
  8238. }
  8239. ut.Commit()
  8240. return err
  8241. }
  8242. func UpdateNewGoodSumCountSeven(sum_count int64, good_id int64, orgid int64, tx *gorm.DB) error {
  8243. 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
  8244. if err != nil {
  8245. tx.Rollback()
  8246. return err
  8247. }
  8248. return err
  8249. }
  8250. func GetWarehouseOutInfoIsExistOTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  8251. info := models.WarehouseOutInfo{}
  8252. var err error
  8253. 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
  8254. if err == gorm.ErrRecordNotFound {
  8255. return nil, err
  8256. }
  8257. if err != nil {
  8258. return nil, err
  8259. }
  8260. return &info, nil
  8261. }
  8262. func GetWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  8263. info := models.WarehouseOutInfo{}
  8264. 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
  8265. return info, err
  8266. }
  8267. func GetNewWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  8268. info := models.WarehouseOutInfo{}
  8269. 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
  8270. if err != gorm.ErrRecordNotFound {
  8271. if err != nil {
  8272. tx.Rollback()
  8273. return info, err
  8274. }
  8275. }
  8276. return info, err
  8277. }
  8278. func GetWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (models.WarehouseOutInfo, error) {
  8279. info := models.WarehouseOutInfo{}
  8280. 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
  8281. return info, err
  8282. }
  8283. func GetNewWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  8284. info := models.WarehouseOutInfo{}
  8285. 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
  8286. if err != gorm.ErrRecordNotFound {
  8287. if err != nil {
  8288. tx.Rollback()
  8289. return info, err
  8290. }
  8291. }
  8292. return info, err
  8293. }
  8294. func GetNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (*models.WarehouseOutInfo, error) {
  8295. info := models.WarehouseOutInfo{}
  8296. var err error
  8297. 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
  8298. if err == gorm.ErrRecordNotFound {
  8299. return nil, err
  8300. }
  8301. if err != nil {
  8302. return nil, err
  8303. }
  8304. return &info, nil
  8305. }
  8306. func GetNewNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (*models.WarehouseOutInfo, error) {
  8307. info := models.WarehouseOutInfo{}
  8308. var err error
  8309. 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
  8310. if err != gorm.ErrRecordNotFound {
  8311. if err != nil {
  8312. tx.Rollback()
  8313. return &info, err
  8314. }
  8315. }
  8316. return &info, err
  8317. }
  8318. func GetNewGoodWarehouseInfo(startime int64, endtime int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.GoodWarehousingInfo, err error) {
  8319. db := XTReadDB().Model(&info).Where("status = 1")
  8320. if startime > 0 {
  8321. db = db.Where("ctime>=?", startime)
  8322. }
  8323. if endtime > 0 {
  8324. db = db.Where("ctime<=?", endtime)
  8325. }
  8326. if orgid > 0 {
  8327. db = db.Where("org_id = ?", orgid)
  8328. }
  8329. if len(good_id) > 0 {
  8330. db = db.Where("good_id in(?)", good_id)
  8331. }
  8332. if storehouse_id > 0 {
  8333. db = db.Where("storehouse_id = ?", storehouse_id)
  8334. }
  8335. err = db.Preload("GoodInfo", "org_id = ? and status= 1", orgid).Find(&info).Error
  8336. return info, err
  8337. }
  8338. func GetNewWarehouseOutInfoList(start_time int64, end_time int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.WarehouseOutInfoNight, err error) {
  8339. db := XTReadDB().Model(&info).Where("status= 1")
  8340. if start_time > 0 {
  8341. db = db.Where("ctime>=?", start_time)
  8342. }
  8343. if end_time > 0 {
  8344. db = db.Where("ctime <=?", end_time)
  8345. }
  8346. if orgid > 0 {
  8347. db = db.Where("org_id = ?", orgid)
  8348. }
  8349. if len(good_id) > 0 {
  8350. db = db.Where("good_id in(?)", good_id)
  8351. }
  8352. if storehouse_id > 0 {
  8353. db = db.Where("storehouse_id = ?", storehouse_id)
  8354. }
  8355. err = db.Preload("GoodInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  8356. return info, err
  8357. }
  8358. func GetGoodOutOpenConfigOne(user_org_id int64) (models.XtGoodOutConfig, error) {
  8359. config := models.XtGoodOutConfig{}
  8360. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8361. return config, err
  8362. }
  8363. func UpdateDeleteAutoGood(good_id int64, record_date int64, patient_id int64) error {
  8364. 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
  8365. 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
  8366. return err
  8367. }
  8368. func UpdateWarehouseInfoMap(list []*models.WarehousingInfo, warehousing_info_id int64, orgId int64) error {
  8369. tx := XTWriteDB().Begin()
  8370. for _, item := range list {
  8371. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", item.WarehousingCount)).Error
  8372. if err != nil {
  8373. tx.Rollback()
  8374. return err
  8375. }
  8376. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).Update(map[string]interface{}{"is_check": 2}).Error
  8377. if err != nil {
  8378. tx.Rollback()
  8379. return err
  8380. }
  8381. err = tx.Model(models.VmStockFlow{}).Where("warehousing_detail_id = ?", item.ID).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  8382. if err != nil {
  8383. tx.Rollback()
  8384. return err
  8385. }
  8386. var goodList []*models.WarehousingInfo
  8387. 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
  8388. var flush_count int64
  8389. for _, it := range goodList {
  8390. flush_count += it.StockCount
  8391. }
  8392. 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
  8393. if err != nil {
  8394. tx.Rollback()
  8395. return err
  8396. }
  8397. 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
  8398. if err != nil {
  8399. tx.Rollback()
  8400. return err
  8401. }
  8402. storeHouseConfig := models.XtStorehouseConfig{}
  8403. err = tx.Where("user_org_id = ? and status = 1", orgId).First(&storeHouseConfig).Error
  8404. var stockList []*models.WarehousingInfo
  8405. //查询该耗材剩余多少库存
  8406. 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
  8407. var sum_count int64
  8408. var sum_in_count int64
  8409. for _, it := range stockList {
  8410. sum_count += it.StockCount
  8411. sum_in_count += it.WarehousingCount
  8412. }
  8413. 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
  8414. if err != nil {
  8415. tx.Rollback()
  8416. return err
  8417. }
  8418. 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
  8419. if err != nil {
  8420. tx.Rollback()
  8421. return err
  8422. }
  8423. }
  8424. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8425. if err != nil {
  8426. tx.Rollback()
  8427. return err
  8428. }
  8429. err = tx.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8430. tx.Commit()
  8431. return err
  8432. }
  8433. func UpdateNewWarehouseInfoMap(list []*models.WarehousingInfo, warehousing_info_id int64, orgId int64, tx *gorm.DB) error {
  8434. for _, item := range list {
  8435. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", item.WarehousingCount)).Error
  8436. if err != nil {
  8437. tx.Rollback()
  8438. return err
  8439. }
  8440. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).Update(map[string]interface{}{"is_check": 2}).Error
  8441. if err != nil {
  8442. tx.Rollback()
  8443. return err
  8444. }
  8445. err = tx.Model(models.VmStockFlow{}).Where("warehousing_detail_id = ?", item.ID).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  8446. if err != nil {
  8447. tx.Rollback()
  8448. return err
  8449. }
  8450. var goodList []*models.WarehousingInfo
  8451. 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
  8452. var flush_count int64
  8453. for _, it := range goodList {
  8454. flush_count += it.StockCount
  8455. }
  8456. 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
  8457. if err != nil {
  8458. tx.Rollback()
  8459. return err
  8460. }
  8461. 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
  8462. if err != nil {
  8463. tx.Rollback()
  8464. return err
  8465. }
  8466. storeHouseConfig := models.XtStorehouseConfig{}
  8467. err = tx.Where("user_org_id = ? and status = 1", orgId).First(&storeHouseConfig).Error
  8468. var stockList []*models.WarehousingInfo
  8469. //查询该耗材剩余多少库存
  8470. 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
  8471. var sum_count int64
  8472. var sum_in_count int64
  8473. for _, it := range stockList {
  8474. sum_count += it.StockCount
  8475. sum_in_count += it.WarehousingCount
  8476. }
  8477. 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
  8478. if err != nil {
  8479. tx.Rollback()
  8480. return err
  8481. }
  8482. 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
  8483. if err != nil {
  8484. tx.Rollback()
  8485. return err
  8486. }
  8487. err = tx.Model(models.XtGoodWarehouseInfoLog{}).Where("warehouse_info_id = ? and status=1", item.ID).Update(map[string]interface{}{"status": 0}).Error
  8488. if err != nil {
  8489. tx.Rollback()
  8490. return err
  8491. }
  8492. }
  8493. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8494. if err != nil {
  8495. tx.Rollback()
  8496. return err
  8497. }
  8498. err = tx.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8499. return err
  8500. }
  8501. func FindFirstWarehousingInfoByStockThree(good_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  8502. if storehouse_id == 0 {
  8503. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime").First(&info).Error
  8504. }
  8505. if storehouse_id > 0 {
  8506. 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
  8507. }
  8508. return info, err
  8509. }
  8510. func FindFirstWarehousingInfoByStockFour(good_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  8511. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and storehouse_id =? ", good_id, storehouse_id).Order("ctime").Last(&info).Error
  8512. return info, err
  8513. }
  8514. func GetHisPrescriptionNumberList(project_id int64, record_date int64, orgid int64, patient_id int64) (list []*models.HisPrescriptionProject, err error) {
  8515. 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
  8516. return list, err
  8517. }
  8518. func GetWarehouseOutnfoByProjectId(project_id int64, record_date int64, orgid int64, patient_id int64) (infor []*models.WarehouseOutInfo, err error) {
  8519. 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
  8520. return infor, err
  8521. }
  8522. func UpdateWarehouseInfoByPatientId(project_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfo, error) {
  8523. info := models.WarehouseOutInfo{}
  8524. 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
  8525. return info, err
  8526. }
  8527. func UpdateWarehouseInfoByProject(good_id int64, id int64, user_org_id int64, count int64) (models.WarehousingInfo, error) {
  8528. info := models.WarehousingInfo{}
  8529. 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
  8530. return info, err
  8531. }
  8532. func GetHisPrescripionProjectList(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  8533. err = XTReadDB().Where("project_id=? and status=1 and type =3 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  8534. return list, err
  8535. }
  8536. func GetHisPrescripionProjectListOne(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  8537. err = XTReadDB().Where("project_id=? and status=1 and type =2 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  8538. return list, err
  8539. }
  8540. func UpdateHisPrescriptionProjectList(id int64, price float64) (models.HisPrescriptionProjectTemplateL, error) {
  8541. templateL := models.HisPrescriptionProjectTemplateL{}
  8542. err := XTWriteDB().Model(&templateL).Where("id =? and status=1", id).Updates(map[string]interface{}{"price": price}).Error
  8543. return templateL, err
  8544. }
  8545. func ModifyNewGoodWarehouseInfoStockMinNumber(count int64, goodid int64, orgid int64, id int64, tx *gorm.DB) error {
  8546. 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
  8547. if err != nil {
  8548. tx.Rollback()
  8549. return err
  8550. }
  8551. return err
  8552. }
  8553. func GetDrugInvetoryByDrugId(user_org_id int64, drug_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  8554. 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
  8555. return info, err
  8556. }
  8557. func UpdateNewDrugWarehouseInfoById(id int64, last_stock_max_number int64, last_stock_min_number int64) error {
  8558. 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
  8559. return err
  8560. }
  8561. func GeteSelfPrescriptionOpen(user_org_id int64) (models.XtSelfDrugInfoConfig, error) {
  8562. drugInfoConfig := models.XtSelfDrugInfoConfig{}
  8563. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugInfoConfig).Error
  8564. return drugInfoConfig, err
  8565. }
  8566. func CreaterSelfConfig(infoConfig models.XtSelfDrugInfoConfig) error {
  8567. err := XTWriteDB().Create(&infoConfig).Error
  8568. return err
  8569. }
  8570. func SaveSelfConfig(infoConfig models.XtSelfDrugInfoConfig) error {
  8571. err := XTWriteDB().Save(&infoConfig).Error
  8572. return err
  8573. }
  8574. func GetSelfPrescriptionOutOpen(user_org_id int64) (models.XtSelfDrugOutConfig, error) {
  8575. drugOutConfig := models.XtSelfDrugOutConfig{}
  8576. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugOutConfig).Error
  8577. return drugOutConfig, err
  8578. }
  8579. func CreateSelfDrugOutConfig(config models.XtSelfDrugOutConfig) error {
  8580. err := XTWriteDB().Create(&config).Error
  8581. return err
  8582. }
  8583. func UpdateSelfDrugOutConfig(config models.XtSelfDrugOutConfig) error {
  8584. err := XTWriteDB().Save(&config).Error
  8585. return err
  8586. }
  8587. func CreateSelfDrugFlowOne(flow models.XtSelfDrugFlow, tx *gorm.DB) error {
  8588. err := tx.Create(&flow).Error
  8589. if err != nil {
  8590. tx.Rollback()
  8591. return err
  8592. }
  8593. return err
  8594. }
  8595. func UpdateWarehouseOutInfoByPrice(good_id int64, packing_price float64) error {
  8596. 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
  8597. return err
  8598. }
  8599. func GetWarehosueOutInfoByPrice(good_id int64, org_id int64) (out []*models.WarehouseOutInfo, err error) {
  8600. err = XTReadDB().Where("good_id = ? and org_id = ? and status=1 and price =0", good_id, org_id).Find(&out).Error
  8601. return out, err
  8602. }
  8603. func UpdateWarehouseOut(good_id int64, packing_price float64) error {
  8604. err := XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("id = ? and status=1", good_id).Updates(map[string]interface{}{"price": packing_price}).Error
  8605. return err
  8606. }
  8607. func UpdateStockFlowByList(good_id int64, packing_price float64) error {
  8608. 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
  8609. return err
  8610. }
  8611. func GetGoodStockInventoryByRecordate(good_id int64, record_date int64, user_org_id int64) (models.XtGoodStockInventoryList, error) {
  8612. goodStockInventoryList := models.XtGoodStockInventoryList{}
  8613. 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
  8614. return goodStockInventoryList, err
  8615. }
  8616. func CreateGoodStockInventoryList(list models.XtGoodStockInventoryList) error {
  8617. err := XTWriteDB().Create(&list).Error
  8618. return err
  8619. }
  8620. func SaveGoodStockInventoryList(list models.XtGoodStockInventoryList) error {
  8621. err := XTWriteDB().Save(&list).Error
  8622. return err
  8623. }
  8624. func GetGoodStockInventoryList(keyword string, limit int64, page int64, storehouse_id int64, startTime int64, endTime int64, orgId int64) (list []*models.XtGoodStockInventoryList, total int64, err error) {
  8625. likeKey := "%" + keyword + "%"
  8626. offset := (page - 1) * limit
  8627. db := XTReadDB().Model(&models.XtGoodStockInventoryList{}).Where("status=1")
  8628. if storehouse_id > 0 {
  8629. db = db.Where("storehouse_id = ?", storehouse_id)
  8630. }
  8631. if startTime > 0 {
  8632. db = db.Where("record_date >=?", startTime)
  8633. }
  8634. if endTime > 0 {
  8635. db = db.Where("record_date<=?", endTime)
  8636. }
  8637. if len(keyword) > 0 {
  8638. db = db.Where("good_name like ? or specification_name like ?", likeKey, likeKey)
  8639. }
  8640. if orgId > 0 {
  8641. db = db.Where("user_org_id = ?", orgId)
  8642. }
  8643. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  8644. return list, total, err
  8645. }
  8646. func GetDrugStockInventoryList(drug_id int64, record_date int64, user_org_id int64) (models.XtDrugStockInventoryList, error) {
  8647. drugStockInventoryList := models.XtDrugStockInventoryList{}
  8648. 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
  8649. return drugStockInventoryList, err
  8650. }
  8651. func CreatedDrugStockInventory(list models.XtDrugStockInventoryList) error {
  8652. err := XTWriteDB().Create(&list).Error
  8653. return err
  8654. }
  8655. func SaveDrugStockInventory(list models.XtDrugStockInventoryList) error {
  8656. err := XTWriteDB().Save(&list).Error
  8657. return err
  8658. }
  8659. func GetStockInventoryPrintList(storehouse_id int64, keyword string, limit int64, page int64, ids []string, orgId int64) (list []*models.XtGoodStockInventoryList, total int64, err error) {
  8660. db := XTReadDB().Model(&list).Where("status=1")
  8661. likeKey := "%" + keyword + "%"
  8662. offset := (page - 1) * limit
  8663. if storehouse_id > 0 {
  8664. db = db.Where("storehouse_id = ?", storehouse_id)
  8665. }
  8666. if len(keyword) > 0 {
  8667. db = db.Where("good_name like ?", likeKey)
  8668. }
  8669. if orgId > 0 {
  8670. db = db.Where("user_org_id = ?", orgId)
  8671. }
  8672. if len(ids) > 0 {
  8673. db = db.Where("id in(?)", ids)
  8674. }
  8675. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  8676. return list, total, err
  8677. }
  8678. func GetGoodWarehouseInfoList(startTime int64, endTime int64, orgId int64) (info []*models.WarehousingInfo, err error) {
  8679. db := XTReadDB().Model(&info).Where("status=1 and is_check=1")
  8680. if startTime > 0 {
  8681. db = db.Where("ctime>=?", startTime)
  8682. }
  8683. if endTime > 0 {
  8684. db = db.Where("ctime<=?", endTime)
  8685. }
  8686. if orgId > 0 {
  8687. db = db.Where("org_id = ?", orgId)
  8688. }
  8689. err = db.Find(&info).Error
  8690. return info, err
  8691. }
  8692. func GetGoodWarhouseOutList(startTime int64, endTime int64, orgId int64) (outinfo []*models.WarehouseOutInfo, err error) {
  8693. db := XTReadDB().Model(&outinfo).Where("status=1 and is_check =1")
  8694. if startTime > 0 {
  8695. db = db.Where("ctime>=?", startTime)
  8696. }
  8697. if endTime > 0 {
  8698. db = db.Where("ctime<=?", endTime)
  8699. }
  8700. if orgId > 0 {
  8701. db = db.Where("org_id = ?", orgId)
  8702. }
  8703. err = db.Find(&outinfo).Error
  8704. return outinfo, err
  8705. }
  8706. func GetGoodStockFlowProList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8707. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 10")
  8708. if startTime > 0 {
  8709. db = db.Where("ctime>=?", startTime)
  8710. }
  8711. if endTime > 0 {
  8712. db = db.Where("ctime<=?", endTime)
  8713. }
  8714. if orgId > 0 {
  8715. db = db.Where("user_org_id = ?", orgId)
  8716. }
  8717. err = db.Find(&flow).Error
  8718. return flow, err
  8719. }
  8720. func GetGoodStockFlowNoProList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8721. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 11")
  8722. if startTime > 0 {
  8723. db = db.Where("ctime>=?", startTime)
  8724. }
  8725. if endTime > 0 {
  8726. db = db.Where("ctime<=?", endTime)
  8727. }
  8728. if orgId > 0 {
  8729. db = db.Where("user_org_id = ?", orgId)
  8730. }
  8731. err = db.Find(&flow).Error
  8732. return flow, err
  8733. }
  8734. func GetCancelStockFlowList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8735. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 7")
  8736. if startTime > 0 {
  8737. db = db.Where("ctime>=?", startTime)
  8738. }
  8739. if endTime > 0 {
  8740. db = db.Where("ctime<=?", endTime)
  8741. }
  8742. if orgId > 0 {
  8743. db = db.Where("user_org_id = ?", orgId)
  8744. }
  8745. err = db.Find(&flow).Error
  8746. return flow, err
  8747. }
  8748. func GetLastMonthCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8749. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 1 or consumable_type = 10)")
  8750. if startTime > 0 {
  8751. db = db.Where("ctime <?", startTime)
  8752. }
  8753. if orgId > 0 {
  8754. db = db.Where("user_org_id = ?", orgId)
  8755. }
  8756. err = db.Find(&flow).Error
  8757. return flow, err
  8758. }
  8759. func GetLastMonthOverCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8760. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 2 or consumable_type=3 or consumable_type=11)")
  8761. if startTime > 0 {
  8762. db = db.Where("ctime <?", startTime)
  8763. }
  8764. if orgId > 0 {
  8765. db = db.Where("user_org_id = ?", orgId)
  8766. }
  8767. err = db.Find(&flow).Error
  8768. return flow, err
  8769. }
  8770. func GetLastCancelMonthCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8771. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 7)")
  8772. if startTime > 0 {
  8773. db = db.Where("ctime <?", startTime)
  8774. }
  8775. if orgId > 0 {
  8776. db = db.Where("user_org_id = ?", orgId)
  8777. }
  8778. err = db.Find(&flow).Error
  8779. return flow, err
  8780. }
  8781. func GetDrugCodeOpen(user_org_id int64) (models.XtDrugCodeOpen, error) {
  8782. drugCodeOpen := models.XtDrugCodeOpen{}
  8783. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugCodeOpen).Error
  8784. return drugCodeOpen, err
  8785. }
  8786. func CreateDrugOpen(open models.XtDrugCodeOpen) error {
  8787. err := XTWriteDB().Create(&open).Error
  8788. return err
  8789. }
  8790. func SaveDrugOpen(open models.XtDrugCodeOpen) error {
  8791. err := XTWriteDB().Save(&open).Error
  8792. return err
  8793. }
  8794. 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) {
  8795. db := XTReadDB().Model(&models.NewHisDoctorAdviceInfo{}).Preload("NewHisPrescription", "status = 1").Where("his_doctor_advice_info.status=1")
  8796. if is_settle == 2 {
  8797. 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")
  8798. } else if is_settle == 1 {
  8799. 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")
  8800. }
  8801. if is_code == 2 {
  8802. //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")
  8803. db = db.Where("his_doctor_advice_info.drug_code <> ''")
  8804. } else if is_code == 1 {
  8805. db = db.Where("length(his_doctor_advice_info.drug_code) = 0")
  8806. }
  8807. if len(keywords) > 0 {
  8808. keywords = "%" + keywords + "%"
  8809. 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)
  8810. }
  8811. if orgId > 0 {
  8812. db = db.Where("his_doctor_advice_info.user_org_id=?", orgId)
  8813. }
  8814. if start_time > 0 {
  8815. db = db.Where("his_doctor_advice_info.advice_date >= ?", start_time)
  8816. }
  8817. if end_time > 0 {
  8818. db = db.Where("his_doctor_advice_info.advice_date<=?", end_time)
  8819. }
  8820. offset := (page - 1) * limit
  8821. if is_sale == 1 {
  8822. db = db.Where("his_doctor_advice_info.is_upload = 1")
  8823. }
  8824. if is_sale == 2 {
  8825. db = db.Where("his_doctor_advice_info.is_upload = 2 or his_doctor_advice_info.is_upload =0")
  8826. }
  8827. err = db.Count(&total).Offset(offset).Limit(limit).Order("his_doctor_advice_info.record_date desc").Find(&list).Error
  8828. return list, total, err
  8829. }
  8830. func GetBloodDrugCode(orgId int64, start_time int64, end_time int64, limit int64, page int64, is_sale int64) (advice []*models.DoctorAdvice, total int64, err error) {
  8831. db := XTReadDB().Model(&advice).Where("status=1 and advice_type !=1")
  8832. if orgId > 0 {
  8833. db = db.Where("user_org_id = ?", orgId)
  8834. }
  8835. if start_time > 0 {
  8836. db = db.Where("advice_date>=?", start_time)
  8837. }
  8838. if end_time > 0 {
  8839. db = db.Where("advice_date<=?", end_time)
  8840. }
  8841. if is_sale > 0 {
  8842. db = db.Where("is_upload = ?", is_sale)
  8843. }
  8844. offset := (page - 1) * limit
  8845. err = db.Count(&total).Offset(offset).Limit(limit).Order("advice_date desc").Find(&advice).Error
  8846. return advice, total, err
  8847. }
  8848. func GetDoctorAdviceInfoList(page int64, limit int64, orgId int64, start_time int64, end_time int64, is_sale int64) ([]models.HisDoctorAdviceInfo, int64, error) {
  8849. var list []models.HisDoctorAdviceInfo
  8850. var total int64
  8851. // 计算偏移量
  8852. offset := (page - 1) * limit
  8853. // 初始化数据库查询
  8854. db := XTReadDB().Model(&models.HisDoctorAdviceInfo{})
  8855. // 构建 WHERE 子句
  8856. query := db.Where("status=1")
  8857. if orgId > 0 {
  8858. query = query.Where("user_org_id=?", orgId)
  8859. }
  8860. if start_time > 0 {
  8861. query = query.Where("advice_date >= ?", start_time)
  8862. }
  8863. if end_time > 0 {
  8864. query = query.Where("advice_date <= ?", end_time)
  8865. }
  8866. if is_sale == 1 {
  8867. query = query.Where("is_upload = 1")
  8868. }
  8869. if is_sale == 2 {
  8870. query = query.Where("is_upload IN (?, ?)", 2, 0) // 使用 IN 替代 OR,通常性能更好
  8871. }
  8872. // 获取总记录数
  8873. err := query.Count(&total).Error
  8874. if err != nil {
  8875. return nil, 0, err
  8876. }
  8877. // 获取分页后的数据
  8878. err = query.Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  8879. if err != nil {
  8880. return nil, 0, err
  8881. }
  8882. return list, total, nil
  8883. }
  8884. func GetDrugWarehouseOutInfoByDrugCode(drug_id int64, patient_id int64, advice_date int64, user_org_id int64) (outinfo []*models.XtDrugWarehouseOutInfo, err error) {
  8885. 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
  8886. return outinfo, err
  8887. }
  8888. func GetNewGoodWarehouseOutInfo(good_id int64, user_org_id int64, warehouse_out_id int64, tx *gorm.DB) (info []*models.WarehouseOutInfo, err error) {
  8889. 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
  8890. if err != gorm.ErrRecordNotFound {
  8891. if err != nil {
  8892. tx.Rollback()
  8893. return info, err
  8894. }
  8895. }
  8896. return info, err
  8897. }
  8898. func GetInitGoodFlow(user_org_id int64, good_id int64, warehousing_detail_id int64, tx *gorm.DB) (list []*models.VmStockFlow, err error) {
  8899. 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
  8900. if err != gorm.ErrRecordNotFound {
  8901. if err != nil {
  8902. tx.Rollback()
  8903. return list, err
  8904. }
  8905. }
  8906. return list, err
  8907. }
  8908. func GetNewDrugFlowList(drug_id int64, user_org_id int64, warehousing_detail_id int64, tx *gorm.DB) (flow []*models.DrugFlow, err error) {
  8909. 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
  8910. if err != gorm.ErrRecordNotFound {
  8911. if err != nil {
  8912. tx.Rollback()
  8913. return flow, err
  8914. }
  8915. }
  8916. return flow, err
  8917. }
  8918. func GetNewPatientAdviceList(patient_id int64, start_time int64, end_time int64, user_org_id int64) (advice []*models.DoctorAdvice, err error) {
  8919. db := XTReadDB().Model(&advice).Where("status=1 and advice_type =2")
  8920. if patient_id > 0 {
  8921. db = db.Where("patient_id = ?", patient_id)
  8922. }
  8923. if start_time > 0 {
  8924. db = db.Where("advice_date>=?", start_time)
  8925. }
  8926. if end_time > 0 {
  8927. db = db.Where("advice_date<=?", end_time)
  8928. }
  8929. if user_org_id > 0 {
  8930. db = db.Where("user_org_id = ?", user_org_id)
  8931. }
  8932. err = db.Order("drug_id desc").Find(&advice).Error
  8933. return advice, err
  8934. }
  8935. func GetSelfDrugWarehouseInfoByPatientId(patient_id int64, user_org_id int64) (drugInfo []*models.XtSelfDrugWarehouseInfo, err error) {
  8936. err = XTReadDB().Where("patient_id = ? and org_id = ? and status =1 and is_check =1", patient_id, user_org_id).Find(&drugInfo).Error
  8937. return drugInfo, err
  8938. }
  8939. func GetPatientAdviceListDetail(patient_id int64, start_time int64, end_time int64, user_org_id int64, advice_name string) (advice []*models.DoctorAdvice, err error) {
  8940. db := XTReadDB().Model(&advice).Where("status = 1")
  8941. if patient_id > 0 {
  8942. db = db.Where("patient_id = ?", patient_id)
  8943. }
  8944. if start_time > 0 {
  8945. db = db.Where("advice_date>=?", start_time)
  8946. }
  8947. if end_time > 0 {
  8948. db = db.Where("advice_date<=?", end_time)
  8949. }
  8950. if user_org_id > 0 {
  8951. db = db.Where("user_org_id = ?", user_org_id)
  8952. }
  8953. if len(advice_name) > 0 {
  8954. db = db.Where("advice_name =?", advice_name)
  8955. }
  8956. err = db.Order("advice_date asc").Find(&advice).Error
  8957. return advice, err
  8958. }