stock_service.go 330KB

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