stock_service.go 409KB

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