stock_service.go 214KB

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