stock_service.go 246KB

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