stock_service.go 376KB

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