stock_service.go 287KB

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