stock_service.go 220KB

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