stock_service.go 206KB

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