stock_service.go 409KB

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