stock_service.go 246KB

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