stock_service.go 279KB

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