stock_service.go 401KB

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