stock_service.go 251KB

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