stock_service.go 282KB

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