stock_service.go 230KB

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