self_drug_service.go 185KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374
  1. package service
  2. import (
  3. "fmt"
  4. "strconv"
  5. "time"
  6. "XT_New/models"
  7. "github.com/jinzhu/gorm"
  8. )
  9. func GetCurrentPatient(orgid int64) (patients []*models.DrugPatients, err error) {
  10. err = XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&patients).Error
  11. return patients, err
  12. }
  13. func GetAllDrugName(orgid int64) (baseDrug []*models.XtBaseDrug, err error) {
  14. err = XTReadDB().Group("drug_name").Where("org_id = ? and status = 1 and find_in_set('停用',drug_status) = 0", orgid).Find(&baseDrug).Error
  15. return baseDrug, err
  16. }
  17. func GetDrugName(drugname string, orgid int64) (*models.XtDrugName, error) {
  18. drugName := models.XtDrugName{}
  19. err := XTReadDB().Model(&drugName).Where("drug_name = ? and user_org_id = ? and status =1", drugname, orgid).Find(&drugName).Error
  20. if err == gorm.ErrRecordNotFound {
  21. return nil, err
  22. }
  23. if err != nil {
  24. return nil, err
  25. }
  26. return &drugName, nil
  27. }
  28. func SaveDrugName(drugname *models.XtDrugName) error {
  29. err := XTWriteDB().Create(&drugname).Error
  30. return err
  31. }
  32. func GetDrugNameList(orgid int64) (druname []*models.XtDrugName, err error) {
  33. err = XTReadDB().Model(&druname).Where("user_org_id = ? and status = 1", orgid).Find(&druname).Error
  34. return druname, err
  35. }
  36. func GetRulleName(orgid int64, name string) (baseDrug []*models.XtBaseDrug, err error) {
  37. err = XTReadDB().Model(&baseDrug).Where("org_id = ? and drug_name = ? and status = 1", orgid, name).Find(&baseDrug).Error
  38. return baseDrug, err
  39. }
  40. func GetDrugId(id int64) (models.XtSelfMedical, error) {
  41. medical := models.XtSelfMedical{}
  42. err := XTReadDB().Model(&medical).Where("id = ? and status = 1", id).Find(&medical).Error
  43. return medical, err
  44. }
  45. func GetRulleList(orgid int64, id int64) (rulleName []*models.XtStandName, err error) {
  46. err = XTReadDB().Model(&rulleName).Where("user_org_id = ? and drug_id = ? and status = 1", orgid, id).Find(&rulleName).Error
  47. return rulleName, err
  48. }
  49. func GetUnitByBaseId(id int64) (models.XtBaseDrug, error) {
  50. drug := models.XtBaseDrug{}
  51. err := XTReadDB().Model(&drug).Where("id = ? and status = 1", id).Find(&drug).Error
  52. return drug, err
  53. }
  54. func IsExistStandName(drugname string, drugspec string, orgid int64) (*models.XtStandName, error) {
  55. standName := models.XtStandName{}
  56. err := XTReadDB().Model(&standName).Where("drug_name = ? and drug_spec = ? and user_org_id = ? and status = 1", drugname, drugspec, orgid).Find(&standName).Error
  57. if err == gorm.ErrRecordNotFound {
  58. return nil, err
  59. }
  60. if err != nil {
  61. return nil, err
  62. }
  63. return &standName, nil
  64. }
  65. func SaveRulleName(rullName *models.XtStandName) error {
  66. err := XTWriteDB().Model(&rullName).Create(&rullName).Error
  67. return err
  68. }
  69. func GetRullerNameDetail(id int64) (models.XtStandName, error) {
  70. rullerName := models.XtStandName{}
  71. err := XTReadDB().Model(&rullerName).Where("id= ? and status = 1", id).Find(&rullerName).Error
  72. return rullerName, err
  73. }
  74. func GetIsExit(drugname string, drugspec string, id int64) (*models.XtStandName, error) {
  75. standName := models.XtStandName{}
  76. err := XTReadDB().Model(&standName).Where("drug_name = ? and drug_spec = ? and id <> ? and status = 1", drugname, drugspec, id).Find(&standName).Error
  77. if err == gorm.ErrRecordNotFound {
  78. return nil, err
  79. }
  80. if err != nil {
  81. return nil, err
  82. }
  83. return &standName, nil
  84. }
  85. func UpdatedRullerName(id int64, re *models.XtStandName) error {
  86. err := XTWriteDB().Model(&re).Where("id=?", id).Update(map[string]interface{}{"drug_spec": re.DrugSpec, "min_unit": re.MinUnit, "price": re.Price, "drug_stock_limit": re.DrugStockLimit, "drug_name": re.DrugName, "drug_name_id": re.DrugNameId, "drug_id": re.DrugId}).Error
  87. return err
  88. }
  89. //func GetAllDrugNameList(orgid int64) (rullername []*models.XtStandName, err error) {
  90. //
  91. // err = XTReadDB().Group("drug_name").Model(&rullername).Where("user_org_id = ? and status = 1", orgid).Find(&rullername).Error
  92. // return rullername, err
  93. //}
  94. func GetAllDrugNameList(orgid int64) (drugName []*models.XtDrugName, err error) {
  95. err = XTReadDB().Model(&drugName).Where("user_org_id = ? and status = 1", orgid).Find(&drugName).Error
  96. return drugName, err
  97. }
  98. func GetRullerListByDrugName(drugname string, orgid int64) (rullername []*models.XtStandName, err error) {
  99. err = XTReadDB().Model(&rullername).Where("drug_id = ? and user_org_id = ? and status = 1", drugname, orgid).Find(&rullername).Error
  100. return rullername, err
  101. }
  102. func GetBaseDrugMedical(id int64) (models.XtBaseDrug, error) {
  103. drug := models.XtBaseDrug{}
  104. err := XTReadDB().Model(&drug).Where("id = ? and status = 1", id).Find(&drug).Error
  105. return drug, err
  106. }
  107. func GetDrugWarehosueInfoById(drug_id int64, org_id int64) (drugInfo []*models.XtDrugWarehouseInfo, err error) {
  108. err = XTReadDB().Where("drug_id = ? and org_id = ? and status =1", drug_id, org_id).Find(&drugInfo).Error
  109. return drugInfo, err
  110. }
  111. func GetBaseDrugMedicalOne(id int64, tx *gorm.DB) (models.XtBaseDrug, error) {
  112. drug := models.XtBaseDrug{}
  113. err := tx.Model(&drug).Where("id = ? and status = 1", id).Find(&drug).Error
  114. if err != gorm.ErrRecordNotFound {
  115. if err != nil {
  116. tx.Rollback()
  117. return drug, err
  118. }
  119. }
  120. return drug, err
  121. }
  122. func GetNewBaseDrugMedical(id int64, tx *gorm.DB) (models.XtBaseDrug, error) {
  123. drug := models.XtBaseDrug{}
  124. err := tx.Model(&drug).Where("id = ? and status = 1", id).Find(&drug).Error
  125. if err != gorm.ErrRecordNotFound {
  126. if err != nil {
  127. tx.Rollback()
  128. return drug, err
  129. }
  130. }
  131. return drug, err
  132. }
  133. func ModifyDrugWarehouseByInfoId(id int64, stock_max_number int64) error {
  134. info := models.DrugWarehouseInfo{}
  135. err := XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", stock_max_number)).Error
  136. return err
  137. }
  138. func ModifyDrugWarehouseByStockMin(id int64, stock_min_number int64) error {
  139. info := models.DrugWarehouseInfo{}
  140. err := XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", stock_min_number)).Error
  141. return err
  142. }
  143. func GetSelfMedicalByDrugName(drugname string, drug_spec string, patientid int64) (*models.XtSelfMedical, error) {
  144. medical := models.XtSelfMedical{}
  145. err := XTReadDB().Model(&medical).Where("drug_name = ? and drug_spec = ? and patient_id = ? and status = 1", drugname, drug_spec, patientid).Find(&medical).Error
  146. if err == gorm.ErrRecordNotFound {
  147. return nil, err
  148. }
  149. if err != nil {
  150. return nil, err
  151. }
  152. return &medical, err
  153. }
  154. func CreateSelfMedical(medical *models.XtSelfMedical) error {
  155. err = XTWriteDB().Model(&medical).Create(&medical).Error
  156. return err
  157. }
  158. func GetCurrentOrgAllStaff(orgid int64, app int64) (role []*models.SgjUserAdminRole, err error) {
  159. err = UserReadDB().Model(&role).Where("org_id = ? and app_id = ? and status = 1", orgid, app).Find(&role).Error
  160. return role, err
  161. }
  162. func CreateStock(stock *models.XtSelfStock) error {
  163. err := XTWriteDB().Model(&stock).Create(&stock).Error
  164. return err
  165. }
  166. func CreateOutStock(stock *models.XtSelfOutStock) error {
  167. err := XTWriteDB().Model(&stock).Create(&stock).Error
  168. return err
  169. }
  170. func GetStandDrugByDrugName(drugname string, orgid int64) (*models.XtStandName, error) {
  171. standname := models.XtStandName{}
  172. err := XTReadDB().Model(&standname).Where("drug_name = ? and user_org_id = ? and status = 1", drugname, orgid).Find(&standname).Error
  173. if err == gorm.ErrRecordNotFound {
  174. return nil, err
  175. }
  176. if err != nil {
  177. return nil, err
  178. }
  179. return &standname, nil
  180. }
  181. func DeleteDrugName(id int64) (models.XtDrugName, error) {
  182. standName := models.XtDrugName{}
  183. err := XTWriteDB().Model(&standName).Where("id = ?", id).Update(map[string]interface{}{"status": 0}).Error
  184. return standName, err
  185. }
  186. func GetDrugSetByUserOrgId(orgid int64) (*models.XtDrugSet, error) {
  187. set := models.XtDrugSet{}
  188. err := XTReadDB().Model(&set).Where("user_org_id = ? and status = 1", orgid).Find(&set).Error
  189. if err == gorm.ErrRecordNotFound {
  190. return nil, err
  191. }
  192. if err != nil {
  193. return nil, err
  194. }
  195. return &set, nil
  196. }
  197. func SaveRadio(drugSet *models.XtDrugSet) error {
  198. err := XTWriteDB().Model(&drugSet).Create(&drugSet).Error
  199. return err
  200. }
  201. func UpdateDrugSet(set *models.XtDrugSet, orgid int64) error {
  202. err := XTWriteDB().Model(&set).Where("user_org_id = ?", orgid).Update(map[string]interface{}{"drug_start": set.DrugStart}).Error
  203. return err
  204. }
  205. func GetMedicalsByName(orgid int64, drugname string) (*models.XtSelfMedical, error) {
  206. medical := models.XtSelfMedical{}
  207. err := XTReadDB().Model(&medical).Where("user_org_id = ? and drug_name = ? and status = 1", orgid, drugname).Find(&medical).Error
  208. if err == gorm.ErrRecordNotFound {
  209. return nil, err
  210. }
  211. if err != nil {
  212. return nil, err
  213. }
  214. return &medical, nil
  215. }
  216. func DeleteDrugStand(id int64) error {
  217. standName := models.XtStandName{}
  218. err := XTWriteDB().Model(&standName).Where("id =?", id).Update(map[string]interface{}{"status": 0}).Error
  219. return err
  220. }
  221. //func GetStockList(patientid int64, startime int64, keyword string) (stock []*models.SelfStocks, err error) {
  222. // db := XTReadDB().Table("xt_self_stock as x").Where("x.status = 1")
  223. // likeKey := "%" + keyword + "%"
  224. // table := XTReadDB().Table("xt_patients as s")
  225. // fmt.Println(table)
  226. // if patientid > 0 {
  227. // db = db.Where("x.patient_id = ?", patientid)
  228. // }
  229. // if startime > 0 {
  230. // db = db.Where("x.storck_time <= ?", startime)
  231. // }
  232. // err = db.Group("x.drug_name,x.drug_spec").Select("x.id,x.drug_name,x.drug_name_id,x.drug_spec,x.remarks,x.store_number,x.admin_user_id,x.storck_time,x.user_org_id,x.stock_in_number,x.patient_id,x.min_unit,sum(x.store_number) as total").Joins("left join xt_patients as s on s.id = x.patient_id").Where("s.name like ? or s.dialysis_no like ?", likeKey, likeKey).Scan(&stock).Error
  233. // return stock, err
  234. //}
  235. func GetStockList(patientid int64, startime int64, keyword string, orgid int64) (stock []*models.XtSelfMedicals, err error) {
  236. likeKey := "%" + keyword + "%"
  237. db := XTReadDB().Table("xt_self_medical as x").Where("x.status = 1")
  238. if patientid > 0 {
  239. db = db.Where("x.patient_id = ?", patientid)
  240. }
  241. if orgid > 0 {
  242. db = db.Where("x.user_org_id = ?", orgid)
  243. }
  244. err = db.Group("x.drug_name,x.drug_spec").Select("x.id,x.drug_name,x.drug_name_id,x.drug_spec,x.min_unit,sum(t.store_number) as total,t.remarks,x.patient_id,t.stock_in_number,t.storck_time,t.admin_user_id,t.medic_id").Joins("left join xt_self_stock as t on t.medic_id = x.id").Where("t.storck_time<=?", startime).Joins("left join xt_patients as s on s.id = x.patient_id").Where("s.name like ? or s.dialysis_no like ?", likeKey, likeKey).Scan(&stock).Error
  245. return stock, err
  246. }
  247. func GetOutStockList(patientid int64, startime int64, keyword string, orgid int64) (outStock []*models.SelfOutStock, err error) {
  248. likeKey := "%" + keyword + "%"
  249. db := XTReadDB().Table("xt_self_medical as x").Where("x.status = 1")
  250. if patientid > 0 {
  251. db = db.Where("x.patient_id = ?", patientid)
  252. }
  253. if orgid > 0 {
  254. db = db.Where("x.user_org_id = ?", orgid)
  255. }
  256. err = db.Group("x.drug_name,x.drug_spec").Select("x.id,x.drug_name,x.drug_name_id,x.drug_spec,x.min_unit,sum(t.outstore_number) as count,t.remarks,x.patient_id,t.stock_out_number,t.storck_time,t.admin_user_id,t.medic_id").Joins("left join xt_self_out_stock as t on t.medic_id = x.id").Where("t.storck_time<=?", startime).Joins("left join xt_patients as s on s.id = x.patient_id").Where("s.name like ? or s.dialysis_no like ?", likeKey, likeKey).Scan(&outStock).Error
  257. return outStock, err
  258. }
  259. func GetTotalOutStockList(patientid int64, startime int64, keyword string, orgid int64) (outStock []*models.SelfOutStock, err error) {
  260. likeKey := "%" + keyword + "%"
  261. db := XTReadDB().Table("xt_self_out_stock as x").Where("x.status = 1")
  262. if patientid > 0 {
  263. db = db.Where("x.patient_id = ?", patientid)
  264. }
  265. if orgid > 0 {
  266. db = db.Where("x.user_org_id = ?", orgid)
  267. }
  268. if startime > 0 {
  269. db = db.Where("x.storck_time<=?", startime)
  270. }
  271. err = db.Select("x.id,x.drug_name,x.drug_name_id,x.drug_spec,x.remarks,x.outstore_number,x.admin_user_id,x.storck_time,x.user_org_id,x.patient_id,x.exit_mode,x.medic_id").Joins("left join xt_patients as s on s.id = x.patient_id").Where("s.name like ? or s.dialysis_no like ?", likeKey, likeKey).Find(&outStock).Error
  272. return outStock, err
  273. }
  274. //func GetOutStockList(patientid int64, startime int64, keyword string) (outStock []*models.XtSelfOutStocks, err error) {
  275. // db := XTReadDB().Table("xt_self_out_stock as x").Where("x.status = 1")
  276. // likeKey := "%" + keyword + "%"
  277. // table := XTReadDB().Table("xt_patients as s")
  278. // fmt.Println(table)
  279. // if patientid > 0 {
  280. // db = db.Where("x.patient_id = ?", patientid)
  281. // }
  282. // if startime > 0 {
  283. // db = db.Where("x.storck_time <= ?", startime)
  284. // }
  285. // err = db.Group("x.drug_name,x.drug_spec").Select("x.drug_name,x.drug_spec,x.patient_id,x.stock_out_number,sum(x.outstore_number) as count").Joins("left join xt_patients as s on s.id = x.patient_id").Where("s.name like ? or s.dialysis_no like ?", likeKey, likeKey).Scan(&outStock).Error
  286. // return outStock, err
  287. //}
  288. func GetMedicalList(patientid int64, orgid int64, keyword string) (medical []*models.XtSelfMedicals, err error) {
  289. likeKey := "%" + keyword + "%"
  290. db := XTReadDB().Table("xt_self_medical as x").Where("x.status = 1")
  291. if patientid > 0 {
  292. db = db.Where("x.patient_id = ?", patientid)
  293. }
  294. if orgid > 0 {
  295. db = db.Where("x.user_org_id = ?", orgid)
  296. }
  297. err = db.Select("x.drug_name,x.id,x.drug_spec,x.patient_id,x.min_unit").Joins("left join xt_patients as s on s.id = x.patient_id").Where("s.name like ? or s.dialysis_no like ?", likeKey, likeKey).Group("x.drug_name,x.drug_spec").Scan(&medical).Error
  298. //err = XTReadDB().Model(&medical).Where("patient_id = ? and user_org_id = ? and status = 1", patientid, orgid).Group("drug_name,drug_spec").Find(&medical).Error
  299. return medical, err
  300. }
  301. func GetStockOutDetail(durgname string, patientid int64) (stock []*models.XtSelfOutStock, err error) {
  302. err = XTReadDB().Model(&stock).Where("drug_name = ? and patient_id = ? and status = 1", durgname, patientid).Find(&stock).Error
  303. return stock, err
  304. }
  305. func DeleteDrugStockNumber(drugname string, patientid int64) (models.XtSelfStock, error) {
  306. stock := models.XtSelfStock{}
  307. err := XTWriteDB().Model(&stock).Where("drug_name = ? and patient_id = ?", drugname, patientid).Update(map[string]interface{}{"status": 0}).Error
  308. return stock, err
  309. }
  310. func GetStockDetail(drugname string, drugspec string, startime int64, endtime int64, patientid int64, orgid int64) (stock []*models.XtSelfStock, err error) {
  311. err = XTReadDB().Model(&stock).Where("drug_name = ? and drug_spec = ? and patient_id =? and user_org_id = ? and status = 1 and storck_time>=? and storck_time<=?", drugname, drugspec, patientid, orgid, startime, endtime).Order("storck_time asc").Find(&stock).Error
  312. return stock, err
  313. }
  314. func GetOutStockDetail(drugname string, drugspec string, startime int64, endtime int64, patientid int64, orgid int64) (outStock []*models.XtSelfOutStock, err error) {
  315. err = XTReadDB().Model(&outStock).Where("drug_name = ? and drug_spec=? and patient_id = ? and user_org_id = ? and status = 1 and storck_time>=? and storck_time<=?", drugname, drugspec, patientid, orgid, startime, endtime).Order("storck_time asc").Find(&outStock).Error
  316. return outStock, err
  317. }
  318. func GetAllPatientStockList(drug_name string, drug_spec string, startime int64, endtime int64, keyword string, orgid int64) (stocks []*models.SelfStocks, err error) {
  319. db := XTReadDB().Table("xt_self_stock as x").Where("x.status = 1")
  320. likeKey := "%" + keyword + "%"
  321. table := XTReadDB().Table("xt_patients as s")
  322. fmt.Println(table)
  323. if len(drug_name) > 0 {
  324. db = db.Where("x.drug_name = ?", drug_name)
  325. }
  326. if len(drug_spec) > 0 {
  327. db = db.Where("x.drug_spec = ?", drug_spec)
  328. }
  329. //if startime > 0 {
  330. // db = db.Where("x.storck_time >= ?", startime)
  331. //}
  332. //if endtime > 0 {
  333. // db = db.Where("x.storck_time <= ?", endtime)
  334. //}
  335. if orgid > 0 {
  336. db = db.Where("x.user_org_id = ?", orgid)
  337. }
  338. err = db.Group("x.patient_id,x.drug_name,x.drug_spec").Select("t.drug_name,t.drug_spec,x.patient_id,x.store_number,sum(x.store_number) as total,s.name,s.dialysis_no,x.min_unit,x.drug_name_id").Joins("left join xt_patients as s on s.id = x.patient_id").Joins("left join xt_self_medical as t on t.id = x.medic_id").Where("s.name like ? or s.dialysis_no like ?", likeKey, likeKey).Scan(&stocks).Error
  339. return stocks, err
  340. }
  341. func GetAllPatientOutStockList(drug_name string, drug_spec string, startime int64, endtime int64, keyword string, orgid int64) (outStock []*models.XtSelfOutStocks, err error) {
  342. db := XTReadDB().Table("xt_self_out_stock as x").Where("x.status = 1")
  343. likeKey := "%" + keyword + "%"
  344. table := XTReadDB().Table("xt_patients as s")
  345. fmt.Println(table)
  346. if len(drug_name) > 0 {
  347. db = db.Where("x.drug_name = ?", drug_name)
  348. }
  349. if len(drug_spec) > 0 {
  350. db = db.Where("x.drug_spec=?", drug_spec)
  351. }
  352. if startime > 0 {
  353. db = db.Where("x.storck_time >= ?", startime)
  354. }
  355. if endtime > 0 {
  356. db = db.Where("x.storck_time<=?", endtime)
  357. }
  358. if orgid > 0 {
  359. db = db.Where("x.user_org_id = ?", orgid)
  360. }
  361. err = db.Group("x.patient_id,x.drug_name,x.drug_spec").Select("x.drug_name,x.drug_spec,x.patient_id,x.stock_out_number,sum(x.outstore_number) as count,s.name,s.dialysis_no,x.drug_name_id").Joins("left join xt_patients as s on s.id = x.patient_id").Where("s.name like ? or s.dialysis_no like ?", likeKey, likeKey).Scan(&outStock).Error
  362. return outStock, err
  363. }
  364. func GetDrugDataByPatientId(patientid int64) (selfMedical []*models.XtSelfMedical, err error) {
  365. err = XTReadDB().Group("drug_name,drug_spec").Model(&selfMedical).Where("patient_id = ? and status = 1", patientid).Find(&selfMedical).Error
  366. return selfMedical, err
  367. }
  368. func GetDruckStockConfig(orgid int64) (models.XtDrugStockConfig, error) {
  369. drugConfig := models.XtDrugStockConfig{}
  370. err := XTReadDB().Model(&drugConfig).Where("org_id = ? and status = 1", orgid).Find(&drugConfig).Error
  371. return drugConfig, err
  372. }
  373. func GetSetSelfMedical(orgid int64) (models.XtDrugSet, error) {
  374. medical := models.XtDrugSet{}
  375. err := XTReadDB().Model(&medical).Where("user_org_id = ? and status = 1", orgid).Find(&medical).Error
  376. return medical, err
  377. }
  378. func GetSelfMedicalList(patientid int64) (selfmedical []*models.XtSelfMedical, err error) {
  379. err = XTReadDB().Group("drug_name").Model(&selfmedical).Where("patient_id = ? and status = 1", patientid).Find(&selfmedical).Error
  380. return selfmedical, err
  381. }
  382. func GetDrugDescByDrugName(drugname string, patient_id int64, orgid int64) (selfMedical []*models.XtSelfMedical, err error) {
  383. err = XTReadDB().Model(&selfMedical).Where("drug_name = ? and patient_id = ? and user_org_id = ? and status = 1", drugname, patient_id, orgid).Find(&selfMedical).Error
  384. return selfMedical, err
  385. }
  386. func GetBaseMedcal(drugname string, orgid int64) (baseDrug []*models.XtBaseDrug, err error) {
  387. err = XTReadDB().Model(&baseDrug).Where("drug_name = ? and org_id = ? and status = 1", drugname, orgid).Find(&baseDrug).Error
  388. return baseDrug, err
  389. }
  390. func GetTotalBaseMedicalCount(drug_id int64, orgid int64) (drugInfo []*models.DrugWarehousingInfo, err error) {
  391. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  392. err = db.Select("x.drug_id,sum(x.warehousing_count) as count").Where("drug_id = ? and org_id = ?", drug_id, orgid).Scan(&drugInfo).Error
  393. return drugInfo, err
  394. }
  395. func GetTotalBaseMedicalCountOut(drug_id int64, orgid int64) (drugOut []*models.DrugWarehousingInfo, err error) {
  396. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  397. err = db.Select("x.drug_id,sum(x.count) as count").Where("drug_id = ? and org_id = ?", drug_id, orgid).Scan(&drugOut).Error
  398. return drugOut, err
  399. }
  400. func GetDrugWareseOut(startime int64, orgid int64) (*models.XtDrugWarehouse, error) {
  401. warehouse := models.XtDrugWarehouse{}
  402. err := XTReadDB().Model(&warehouse).Where("warehousing_time = ? and org_id = ? and status = 1", startime, orgid).Find(&warehouse).Error
  403. if err == gorm.ErrRecordNotFound {
  404. return nil, err
  405. }
  406. if err != nil {
  407. return nil, err
  408. }
  409. return &warehouse, nil
  410. }
  411. func GetManufacturer(id int64) (models.XtBaseDrug, error) {
  412. drug := models.XtBaseDrug{}
  413. err := XTReadDB().Model(&drug).Where("id = ? and status = 1", id).Find(&drug).Error
  414. return drug, err
  415. }
  416. func CreateDrugWarehoue(warehouse *models.XtDrugWarehouse) error {
  417. err := XTWriteDB().Model(&warehouse).Create(&warehouse).Error
  418. return err
  419. }
  420. func GetTodayWareHousringOrder(startime int64, orgid int64) (models.XtDrugWarehouseOut, error) {
  421. warehouse := models.XtDrugWarehouseOut{}
  422. err := XTReadDB().Model(&warehouse).Where("warehouse_out_time = ? and org_id = ? and status = 1", startime, orgid).Find(&warehouse).Error
  423. return warehouse, err
  424. }
  425. func CreateDrugWareHouseOut(out *models.XtDrugWarehouseOut) error {
  426. err := XTWriteDB().Model(&out).Create(&out).Error
  427. return err
  428. }
  429. func CreateWareHouseOutInfo(info *models.XtDrugWarehouseOutInfo) error {
  430. err := XTWriteDB().Model(&info).Create(&info).Error
  431. return err
  432. }
  433. func CreateAotoMaticReduceDetail(detail *models.XtDrugAutomaticReduceDetail) error {
  434. err := XTWriteDB().Model(&detail).Create(&detail).Error
  435. return err
  436. }
  437. func GetDrugStockConfig(orgid int64) (models.XtDrugStockConfig, error) {
  438. config := models.XtDrugStockConfig{}
  439. err := XTReadDB().Model(&config).Where("org_id =? and status = 1", orgid).Find(&config).Error
  440. return config, err
  441. }
  442. func GetDrugSet(orgid int64) (models.XtDrugSet, error) {
  443. drugSet := models.XtDrugSet{}
  444. err := XTReadDB().Model(&drugSet).Where("user_org_id =? and status = 1", orgid).Find(&drugSet).Error
  445. return drugSet, err
  446. }
  447. type Drugs struct {
  448. ID int64 `gorm:"column:id" json:"id" form:"id"`
  449. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  450. Pinyin string `gorm:"column:pinyin" json:"pinyin" form:"pinyin"`
  451. Wubi string `gorm:"column:wubi" json:"wubi" form:"wubi"`
  452. DrugAlias string `gorm:"column:drug_alias" json:"drug_alias" form:"drug_alias"`
  453. DrugAliasPinyin string `gorm:"column:drug_alias_pinyin" json:"drug_alias_pinyin" form:"drug_alias_pinyin"`
  454. DrugAliasWubi string `gorm:"column:drug_alias_wubi" json:"drug_alias_wubi" form:"drug_alias_wubi"`
  455. DrugCategory int64 `gorm:"column:drug_category" json:"drug_category" form:"drug_category"`
  456. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  457. DrugType int64 `gorm:"column:drug_type" json:"drug_type" form:"drug_type"`
  458. DrugStockLimit string `gorm:"column:drug_stock_limit" json:"drug_stock_limit" form:"drug_stock_limit"`
  459. DrugStatus string `gorm:"column:drug_status" json:"drug_status" form:"drug_status"`
  460. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  461. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  462. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  463. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  464. Status int64 `gorm:"column:status" json:"status" form:"status"`
  465. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  466. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  467. Drugs []*Drugs `gorm:"ForeignKey:DrugName;AssociationForeignKey:DrugName" json:"drug_specs"`
  468. HospApprFlag int64 `gorm:"column:hosp_appr_flag" json:"hosp_appr_flag" form:"hosp_appr_flag"`
  469. LmtUsedFlag int64 `gorm:"column:lmt_used_flag" json:"lmt_used_flag" form:"lmt_used_flag"`
  470. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  471. MinNumber int64 `gorm:"column:min_number" json:"min_number" form:"min_number"`
  472. Dose string `gorm:"column:dose" json:"dose" form:"dose"`
  473. DoseUnit string `gorm:"column:dose_unit" json:"dose_unit" form:"dose_unit"`
  474. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  475. DrugDoseUnit int64 `grom:"column:drug_dose_unit" json:"drug_dose_unit" form:"drug_dose_unit"`
  476. FirstLetter string `gorm:"column:first_letter" json:"first_letter" form:"first_letter"`
  477. }
  478. func (Drugs) TableName() string {
  479. return "xt_base_drug"
  480. }
  481. func GetAllBaseDrugLibList(org_id int64) (drugs []*Drugs, err error) {
  482. db := readDb.Model(&models.BaseDrugLib{}).Preload("Drugs", "status = 1 AND org_id = ? AND find_in_set('停用',drug_status) = 0", org_id).Where("org_id = ? AND status = 1 AND find_in_set('停用',drug_status) = 0", org_id).Group("drug_name")
  483. err = db.Find(&drugs).Error
  484. return
  485. }
  486. type PrivateDrug struct {
  487. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  488. ID int64 `gorm:"column:id" json:"id" form:"id"`
  489. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  490. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  491. DrugNameId int64 `gorm:"column:drug_name_id" json:"drug_name_id" form:"drug_name_id"`
  492. Status int64 `gorm:"column:status" json:"status" form:"status"`
  493. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  494. CreatedTime int64 `gorm:"column:created_time" json:"created_time" form:"created_time"`
  495. UpdatedTime int64 `gorm:"column:updated_time" json:"updated_time" form:"updated_time"`
  496. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  497. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  498. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  499. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  500. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  501. PrivateDrug []*PrivateDrug `gorm:"ForeignKey:DrugName;AssociationForeignKey:DrugName" json:"drug_specs"`
  502. }
  503. func (PrivateDrug) TableName() string {
  504. return "xt_self_medical"
  505. }
  506. func GetPrivateDrugList(patient_id int64, org_id int64) (pds []*PrivateDrug, err error) {
  507. err = XTReadDB().Model(&PrivateDrug{}).Preload("PrivateDrug", "status = 1 AND user_org_id = ? AND patient_id = ? ", org_id, patient_id).Where("patient_id = ? and status = 1 AND user_org_id = ?", patient_id, org_id).Group("drug_name_id").Find(&pds).Error
  508. return
  509. }
  510. func GetAllMedicalList(orgid int64) (list []*models.XtSelfMedical, err error) {
  511. err = XTReadDB().Model(&list).Where("user_org_id = ? and status = 1", orgid).Group("drug_name").Find(&list).Error
  512. return list, err
  513. }
  514. func DeleteDrugNameById(id int64) (models.XtSelfMedical, error) {
  515. medical := models.XtSelfMedical{}
  516. err := XTWriteDB().Model(&medical).Where("id = ?", id).Updates(map[string]interface{}{"status": 0}).Error
  517. return medical, err
  518. }
  519. func GetDrugNameIsExist(drugid int64, orgid int64) (*models.XtDrugName, error) {
  520. drugName := models.XtDrugName{}
  521. err := XTReadDB().Model(&drugName).Where("drug_id = ? and user_org_id = ? and status = 1", drugid, orgid).Find(&drugName).Error
  522. if err == gorm.ErrRecordNotFound {
  523. return nil, err
  524. }
  525. if err != nil {
  526. return nil, err
  527. }
  528. return &drugName, nil
  529. }
  530. func UpdatedDrugName(id int64, orgid int64, drugName *models.XtDrugName) error {
  531. err := XTWriteDB().Model(&drugName).Where("drug_id = ? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"drug_name": drugName.DrugName}).Error
  532. return err
  533. }
  534. func GetStandNameIsExist(id int64, orgid int64) (*models.XtStandName, error) {
  535. standName := models.XtStandName{}
  536. err := XTReadDB().Model(&standName).Where("drug_name_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&standName).Error
  537. if err == gorm.ErrRecordNotFound {
  538. return nil, err
  539. }
  540. if err != nil {
  541. return nil, err
  542. }
  543. return &standName, nil
  544. }
  545. func UpdateStandName(id int64, orgid int64, standName *models.XtStandName) error {
  546. err := XTWriteDB().Model(&standName).Where("drug_name_id = ? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"drug_name": standName.DrugName, "drug_spec": standName.DrugSpec, "drug_stock_limit": standName.DrugStockLimit, "min_unit": standName.MinUnit}).Error
  547. return err
  548. }
  549. func GetSelfMedicalIsExist(id int64, orgid int64) (*models.XtSelfMedical, error) {
  550. medical := models.XtSelfMedical{}
  551. err := XTReadDB().Model(&medical).Where("drug_name_id = ? and user_org_id = ? and status =1", id, orgid).Find(&medical).Error
  552. if err == gorm.ErrRecordNotFound {
  553. return nil, err
  554. }
  555. if err != nil {
  556. return nil, err
  557. }
  558. return &medical, nil
  559. }
  560. func UpdateSelfMedical(id int64, orgid int64, medical *models.XtSelfMedical) error {
  561. err := XTWriteDB().Model(&medical).Where("drug_name_id = ? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"drug_name": medical.DrugName, "drug_spec": medical.DrugSpec, "execution_frequency": medical.ExecutionFrequency, "prescribing_number": medical.PrescribingNumber, "delivery_way": medical.DeliveryWay, "single_dose": medical.SingleDose, "min_unit": medical.MinUnit}).Error
  562. return err
  563. }
  564. func GetDrugNameId(id int64, orgid int64) (models.XtSelfMedical, error) {
  565. medical := models.XtSelfMedical{}
  566. err := XTReadDB().Model(&medical).Where("drug_name_id = ? and status = 1 and user_org_id = ?", id, orgid).Find(&medical).Error
  567. return medical, err
  568. }
  569. func GetSelfStosckIsExist(id int64, orgid int64) (*models.XtSelfStock, error) {
  570. medical := models.XtSelfStock{}
  571. err := XTReadDB().Model(&medical).Where("drug_name_id = ? and user_org_id = ? and status =1", id, orgid).Find(&medical).Error
  572. if err == gorm.ErrRecordNotFound {
  573. return nil, err
  574. }
  575. if err != nil {
  576. return nil, err
  577. }
  578. return &medical, nil
  579. }
  580. func UpdatedSelfStock(stock *models.XtSelfStock, id int64, orgid int64) error {
  581. err := XTWriteDB().Model(&stock).Where("drug_name_id=? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"drug_name": stock.DrugName, "min_unit": stock.MinUnit, "drug_spec": stock.DrugSpec}).Error
  582. return err
  583. }
  584. func GetOutSelfOutStockIsExist(id int64, orgid int64) (*models.XtSelfOutStock, error) {
  585. outStock := models.XtSelfOutStock{}
  586. err := XTReadDB().Model(&outStock).Where("drug_name_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&outStock).Error
  587. if err == gorm.ErrRecordNotFound {
  588. return nil, err
  589. }
  590. if err != nil {
  591. return nil, err
  592. }
  593. return &outStock, nil
  594. }
  595. func UpdatedOutSelfStock(stock *models.XtSelfOutStock, id int64, orgid int64) error {
  596. err := XTWriteDB().Model(&stock).Where("drug_name_id = ? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"drug_name": stock.DrugName, "drug_spec": stock.DrugSpec}).Error
  597. return err
  598. }
  599. func GetDoctorAdviceIsExist(id int64, orgid int64) (*models.DoctorAdvice, error) {
  600. advice := models.DoctorAdvice{}
  601. err := XTReadDB().Model(&advice).Where("drug_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&advice).Error
  602. if err == gorm.ErrRecordNotFound {
  603. return nil, err
  604. }
  605. if err != nil {
  606. return nil, err
  607. }
  608. return &advice, nil
  609. }
  610. func UpdateNoExcuteDoctorAdvice(id int64, orgid int64, advice *models.DoctorAdvice) error {
  611. err := XTWriteDB().Model(&advice).Where("drug_id = ? and user_org_id = ? and status = 1 and execution_state = 2", id, orgid).Updates(map[string]interface{}{"advice_name": advice.AdviceName, "advice_desc": advice.AdviceDesc, "single_dose": advice.SingleDose, "single_dose_unit": advice.SingleDoseUnit, "delivery_way": advice.DeliveryWay, "execution_frequency": advice.ExecutionFrequency}).Error
  612. return err
  613. }
  614. func GetDoctorAdviceTemplateIsExist(id int64, orgid int64) (*models.DoctorAdviceTemplate, error) {
  615. template := models.DoctorAdviceTemplate{}
  616. err := XTReadDB().Model(&template).Where("drug_id = ? and org_id = ? and status = 1", id, orgid).Find(&template).Error
  617. if err == gorm.ErrRecordNotFound {
  618. return nil, err
  619. }
  620. if err != nil {
  621. return nil, err
  622. }
  623. return &template, nil
  624. }
  625. func UpdatedDoctorAdviceTemplate(id int64, orgid int64, template *models.DoctorAdviceTemplate) error {
  626. err := XTWriteDB().Model(&template).Where("drug_id = ? and org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"advice_name": template.AdviceName, "advice_desc": template.AdviceDesc, "single_dose": template.SingleDose, "single_dose_unit": template.SingleDoseUnit, "delivery_way": template.DeliveryWay, "execution_frequency": template.ExecutionFrequency}).Error
  627. return err
  628. }
  629. func ToSeachPatient(keyword string, orgid int64) (patients []*models.Patients, err error) {
  630. likeKey := "%" + keyword + "%"
  631. err = XTReadDB().Model(&patients).Where("name like ? and user_org_id = ? and status = 1", likeKey, orgid).Find(&patients).Error
  632. return patients, err
  633. }
  634. func GetMedicalDrugNameList(orgid int64) (drugName []*models.DrugName, err error) {
  635. db := XTReadDB().Model(&drugName).Where("user_org_id = ? and status = 1", orgid)
  636. err = db.Preload("XtStandName", "status =1 and user_org_id = ?", orgid).Find(&drugName).Error
  637. return drugName, err
  638. }
  639. func GetSelfOutStock(orgid int64, advicedate int64, patientid int64, adivicename string, advicedesc string) (*models.XtSelfOutStock, error) {
  640. stock := models.XtSelfOutStock{}
  641. err = XTReadDB().Model(&stock).Where("user_org_id = ? and storck_time = ? and patient_id = ? and drug_name = ? and drug_spec = ? and status = 1", orgid, advicedate, patientid, adivicename, advicedesc).Find(&stock).Error
  642. if err == gorm.ErrRecordNotFound {
  643. return nil, err
  644. }
  645. if err != nil {
  646. return nil, err
  647. }
  648. return &stock, err
  649. }
  650. func UpdatedSelfOutSatock(id int64, stock *models.XtSelfOutStock) error {
  651. err := XTWriteDB().Model(&stock).Where("id = ?", id).Updates(map[string]interface{}{"drug_name": stock.DrugName, "drug_name_id": stock.DrugNameId, "drug_spec": stock.DrugSpec, "outstore_number": stock.OutstoreNumber, "admin_user_id": stock.AdminUserId, "storck_time": stock.StorckTime, "patient_id": stock.PatientId, "stock_out_number": stock.StockOutNumber, "exit_mode": stock.ExitMode, "medic_id": stock.MedicId, "updated_time": time.Now().Unix()}).Error
  652. return err
  653. }
  654. func GetSelfStockQuery(typename int64, keywords string, startime int64, endtime int64, orgid int64, limit int64, page int64) (selfoutstock []*models.XtSelfOutStockOne, total int64, err error) {
  655. offset := (page - 1) * limit
  656. db := XTReadDB().Table("xt_self_stock as x").Where("x.status = 1")
  657. likeKey := "%" + keywords + "%"
  658. if len(keywords) > 0 {
  659. db = db.Where("x.drug_name like ? ", likeKey)
  660. }
  661. if startime > 0 {
  662. db = db.Where("x.storck_time >=?", startime)
  663. }
  664. if endtime > 0 {
  665. db = db.Where("x.storck_time<=?", endtime)
  666. }
  667. if orgid > 0 {
  668. db = db.Where("x.user_org_id = ?", orgid)
  669. }
  670. if typename > 0 {
  671. err = db.Select("x.drug_name,x.drug_name_id,x.drug_spec,Sum(store_number) as count,b.drug_type").Joins("left join xt_base_drug as b on b.id = x.drug_name_id and b.org_id = ? and b.status =1 ", orgid).Where("b.drug_type = ?", typename).Group("x.drug_name_id").Count(&total).Offset(offset).Limit(limit).Scan(&selfoutstock).Error
  672. } else {
  673. err = db.Select("x.drug_name,x.drug_name_id,x.drug_spec,Sum(store_number) as count,b.drug_type").Joins("left join xt_base_drug as b on b.id = x.drug_name_id and b.org_id = ? and b.status =1", orgid).Group("x.drug_name_id").Count(&total).Offset(offset).Limit(limit).Scan(&selfoutstock).Error
  674. }
  675. return selfoutstock, total, err
  676. }
  677. func GetSelfOutStockQuery(startTime int64, endTime int64, orgid int64) (selfoutstock []*models.XtSelfOutStockOne, err error) {
  678. db := XTReadDB().Table("xt_self_out_stock as x").Where("x.status = 1")
  679. if startTime > 0 {
  680. db = db.Where("x.storck_time >?", startTime)
  681. }
  682. if endTime > 0 {
  683. db = db.Where("x.storck_time<=?", endTime)
  684. }
  685. if orgid > 0 {
  686. db = db.Where("x.user_org_id = ?", orgid)
  687. }
  688. err = db.Select("x.drug_name_id,Sum(x.outstore_number) as count").Group("x.drug_name").Scan(&selfoutstock).Error
  689. return selfoutstock, err
  690. }
  691. func GetLastDrugWarehouseOutInfo(drugid int64, warehouseOutId int64, orgid int64) (models.XtDrugWarehouseOutInfo, error) {
  692. info := models.XtDrugWarehouseOutInfo{}
  693. err = XTReadDB().Model(&info).Where("drug_id = ? and warehouse_out_id = ? and org_id = ? and status = 1", drugid, warehouseOutId, orgid).Last(&info).Error
  694. return info, err
  695. }
  696. func PostSearchDrugWarehouseList(keyword string, orgid int64, storehouse_id int64) (info []*models.BloodDrugWarehouseInfo, err error) {
  697. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1 and (x.stock_max_number>0 or x.stock_min_number>0)")
  698. table := XTReadDB().Table("xt_base_drug as t").Where("t.status")
  699. fmt.Println(table)
  700. likeKey := "%" + keyword + "%"
  701. if orgid > 0 {
  702. db = db.Where("x.org_id = ?", orgid)
  703. }
  704. if storehouse_id > 0 {
  705. db = db.Where("x.storehouse_id = ?", storehouse_id)
  706. }
  707. err = db.Group("x.drug_id").Select("x.id,x.warehousing_id,x.drug_id,t.number,x.product_date,x.expiry_date,x.warehousing_count,x.price,x.total_price,x.dealer,x.remark,x.warehousing_order,x.warehouseing_unit,x.stock_max_number,x.stock_min_number,x.batch_number,x.storehouse_id,t.drug_origin_place,t.drug_name,t.min_unit,x.manufacturer,t.retail_price,t.max_unit,t.min_number,t.dose,t.dose_unit,t.last_price,t.drug_origin_place").Joins("left join xt_base_drug as t on t.id = x.drug_id").Where("t.drug_name like ? and t.org_id = ?", likeKey, orgid).Order("x.ctime desc").Scan(&info).Error
  708. return info, err
  709. }
  710. func CreateDrugPrice(price models.XtDrugAdjustPrice) error {
  711. err := XTWriteDB().Create(&price).Error
  712. return err
  713. }
  714. func GetDrugPriceList(startime int64, endtime int64, orgId int64, keyword string, limit int64, page int64) (adjust []*models.VmDrugAdjustPrice, total int64, err error) {
  715. likeKey := "%" + keyword + "%"
  716. offset := (page - 1) * limit
  717. db := XTReadDB().Table("xt_drug_adjust_price as x").Where("x.status = 1")
  718. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  719. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  720. fmt.Println(table, tab)
  721. if len(keyword) > 0 {
  722. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  723. }
  724. if orgId > 0 {
  725. db = db.Where("x.user_org_id = ?", orgId)
  726. }
  727. if startime > 0 {
  728. db = db.Where("x.ctime>=?", startime)
  729. }
  730. if endtime > 0 {
  731. db = db.Where("x.ctime<=?", endtime)
  732. }
  733. err = db.Group("x.id").Select("x.id,x.drug_name,x.specification_name,x.warehousing_unit,x.count,x.last_price,x.retail_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.drug_id,x.warehousing_order,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.number").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.creater").Joins("left join xt_base_drug as t on t.id =x.drug_id").Count(&total).Offset(offset).Limit(limit).Scan(&adjust).Error
  734. return adjust, total, err
  735. }
  736. func UpdateDrugAdjuestPrice(ids []string, info models.XtDrugAdjustPrice) error {
  737. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  738. return err
  739. }
  740. func GetDrugAdjuestPrice(ids []string) (info []*models.XtDrugAdjustPrice, err error) {
  741. err = XTReadDB().Model(&info).Where("id in(?) and status = 1", ids).Find(&info).Error
  742. return info, err
  743. }
  744. func GetDrugDamage(ids []string) (info []*models.XtDrugDamage, err error) {
  745. err = XTReadDB().Model(&info).Where("id in(?) and status = 1", ids).Find(&info).Error
  746. return info, err
  747. }
  748. func CreateDrugDamage(damage models.XtDrugDamage) error {
  749. err := XTWriteDB().Create(&damage).Error
  750. return err
  751. }
  752. func GetDrugDamageList(startime int64, endtime int64, orgId int64, keyword string, limit int64, page int64, storehouse_id int64) (adjust []*models.VmDrugInventory, total int64, err error) {
  753. likeKey := "%" + keyword + "%"
  754. offset := (page - 1) * limit
  755. db := XTReadDB().Table("xt_drug_inventory as x").Where("x.status = 1 and type = 4")
  756. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  757. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  758. fmt.Println(table, tab)
  759. //if len(keyword) > 0 {
  760. // db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  761. //}
  762. if len(keyword) > 0 {
  763. db = db.Where("x.drug_name like ?", likeKey)
  764. }
  765. if orgId > 0 {
  766. db = db.Where("x.user_org_id = ?", orgId)
  767. }
  768. if startime > 0 {
  769. db = db.Where("x.ctime >=?", startime)
  770. }
  771. if endtime > 0 {
  772. db = db.Where("x.ctime <=?", endtime)
  773. }
  774. if storehouse_id > 0 {
  775. db = db.Where("x.storehouse_id = ?", storehouse_id)
  776. }
  777. err = db.Group("x.drug_id").Select("x.id,x.drug_name,x.specification_name,x.warehousing_unit,x.count,x.last_price,x.retail_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.drug_id,x.warehousing_order,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.number,x.drug_origin_place,x.warehouse_info_id,x.batch_number,x.stock_max_number,x.stock_min_number,x.storehouse_id,t.min_number,t.max_unit,t.min_unit,t.min_price").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.creater").Joins("left join xt_base_drug as t on t.id =x.drug_id").Count(&total).Offset(offset).Limit(limit).Scan(&adjust).Error
  778. return adjust, total, err
  779. }
  780. func GetDrugDamageByOrgId(orgid int64) (list []*models.XtDrugInventory, err error) {
  781. err = XTReadDB().Model(&list).Where("user_org_id = ? and type = 4 and status = 1", orgid).Find(&list).Error
  782. return list, err
  783. }
  784. func UpdateDrugDamage(ids []string, damage models.XtDrugDamage) error {
  785. err := XTWriteDB().Model(&damage).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": damage.Checker, "checker_status": damage.CheckerStatus, "checker_time": damage.CheckerTime}).Error
  786. return err
  787. }
  788. func GetDrugModifyPrice(id int64) (models.XtDrugAdjustPrice, error) {
  789. adjuest := models.XtDrugAdjustPrice{}
  790. err := XTReadDB().Model(&adjuest).Where("id=? and status = 1", id).Find(&adjuest).Error
  791. return adjuest, err
  792. }
  793. func ModifyDrugPrice(id int64, adjust models.XtDrugAdjustPrice) error {
  794. err := XTWriteDB().Model(&adjust).Where("id=? and status = 1", id).Updates(map[string]interface{}{"drug_name": adjust.DrugName, "retail_price": adjust.RetailPrice, "new_price": adjust.NewPrice, "count": adjust.Count, "remark": adjust.Remark, "specification_name": adjust.SpecificationName, "warehousing_unit": adjust.WarehousingUnit, "manufacturer": adjust.Manufacturer, "number": adjust.Number, "drug_id": adjust.DrugId}).Error
  795. return err
  796. }
  797. func GetModifyPricePrint(ids []string) (info []*models.XtDrugAdjustPrice, err error) {
  798. err = XTReadDB().Model(&info).Where("id in(?) and status = 1", ids).Find(&info).Error
  799. return info, err
  800. }
  801. func GetDrugDamageDetail(id int64) (models.XtDrugDamage, error) {
  802. damage := models.XtDrugDamage{}
  803. err := XTReadDB().Model(&damage).Where("id=? and status = 1", id).Find(&damage).Error
  804. return damage, err
  805. }
  806. func ModifyDrugDamage(id int64, damage models.XtDrugDamage) error {
  807. err := XTWriteDB().Model(&damage).Where("id=? and status = 1", id).Updates(map[string]interface{}{"drug_name": damage.DrugName, "warehousing_unit": damage.WarehousingUnit, "count": damage.Count, "last_price": damage.LastPrice, "retail_price": damage.RetailPrice, "new_price": damage.NewPrice, "manufacturer": damage.Manufacturer, "remark": damage.Remark, "number": damage.Number, "drug_id": damage.DrugId, "stock_max_number": damage.StockMaxNumber, "stock_min_number": damage.StockMinNumber, "warehousing_info_id": damage.WarehousingInfoId, "drug_origin_place": damage.DrugOriginPlace}).Error
  808. return err
  809. }
  810. func DeleteDrugDamage(id int64) error {
  811. damage := models.XtDrugDamage{}
  812. err := XTWriteDB().Model(&damage).Where("id=? and status = 1").Updates(map[string]interface{}{"status": 0}).Error
  813. return err
  814. }
  815. func GetDrugDamagePrint(ids []string) (damage []*models.XtDrugDamage, err error) {
  816. err = XTReadDB().Model(&damage).Where("id in(?) and status = 1", ids).Find(&damage).Error
  817. return damage, err
  818. }
  819. func GetWarehoseInfoById(id int64, storehouse_id int64, orgid int64) (info []*models.XtDrugWarehouseInfo, err error) {
  820. if orgid == 10191 {
  821. err = XTReadDB().Model(&info).Where("drug_id=? and status = 1 and storehouse_id = ? and is_check = 1", id, storehouse_id).Preload("XtBaseDrug", "status= 1").Find(&info).Error
  822. return info, err
  823. } else {
  824. err = XTReadDB().Model(&info).Where("drug_id=? and status = 1 and storehouse_id = ? and is_check = 1 and (stock_max_number>0 or stock_min_number>0)", id, storehouse_id).Preload("XtBaseDrug", "status= 1").Find(&info).Error
  825. return info, err
  826. }
  827. }
  828. func CreateDrugInventory(inventory models.XtDrugInventory) error {
  829. err := XTWriteDB().Create(&inventory).Error
  830. return err
  831. }
  832. func GetDrugInventoryList(keyword string, page int64, limit int64, orgid int64, startime int64, endtime int64) (list []*models.VmDrugInventory, total int64, err error) {
  833. fmt.Println("startdate", limit, page)
  834. likeKey := "%" + keyword + "%"
  835. offset := (page - 1) * limit
  836. db := XTReadDB().Table("xt_drug_inventory as x").Where("x.status = 1")
  837. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  838. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  839. fmt.Println(table, tab)
  840. if len(keyword) > 0 {
  841. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  842. }
  843. if orgid > 0 {
  844. db = db.Where("x.user_org_id = ?", orgid)
  845. }
  846. if startime > 0 {
  847. db = db.Where("x.start_time >=?", startime)
  848. }
  849. if endtime > 0 {
  850. db = db.Where("x.start_time<=?", endtime)
  851. }
  852. err = db.Select("x.id,x.drug_name,x.specification_name,x.warehousing_unit,x.count,x.last_price,x.retail_price,x.manufacturer,x.dealer,x.remark,x.drug_id,x.warehousing_order,x.number,x.batch_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.min_count,x.min_unit,t.dose,t.dose_unit,t.max_unit,t.min_unit").Joins("left join sgj_users.sgj_user_admin_role as r on r.id = x.creater").Joins("left join xt_base_drug as t on t.id =x.drug_id").Count(&total).Order("x.id desc").Offset(offset).Limit(limit).Order("x.ctime desc").Scan(&list).Error
  853. return list, total, err
  854. }
  855. func UpdateDrugInventory(ids []string, inventory models.XtDrugInventory) error {
  856. err := XTWriteDB().Model(&inventory).Where("id in(?) and status = 1", ids).Updates(map[string]interface{}{"checker": inventory.Checker, "checker_status": inventory.CheckerStatus, "checker_time": inventory.CheckerTime}).Error
  857. return err
  858. }
  859. func GetDrugInventoryListByIds(ids []string) (inventory []*models.XtDrugInventory, err error) {
  860. err = XTReadDB().Model(&inventory).Where("id in(?) and status = 1", ids).Find(&inventory).Error
  861. return inventory, err
  862. }
  863. func GetDrugDetailByDrugId(drug_id int64) (models.XtBaseDrug, error) {
  864. drug := models.XtBaseDrug{}
  865. err := XTReadDB().Model(&drug).Where("id = ? and status = 1", drug_id).Find(&drug).Error
  866. return drug, err
  867. }
  868. func GetDrugWareInfoById(id int64) (models.XtDrugWarehouseInfo, error) {
  869. info := models.XtDrugWarehouseInfo{}
  870. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  871. return info, err
  872. }
  873. func GetDrugWareInfoByIdTwo(id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  874. err = XTReadDB().Model(&info).Where("(id = ? or warehousing_info_id = ?) and status = 1", id, id).Find(&info).Error
  875. return info, err
  876. }
  877. func GetLastDrugWarehouse(orgid int64) (models.DrugWarehouse, error) {
  878. info := models.DrugWarehouse{}
  879. err := XTReadDB().Model(&info).Where("org_id=? and status = 1", orgid).Last(&info).Error
  880. return info, err
  881. }
  882. func GetLastDrugWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  883. info := models.DrugWarehouseOut{}
  884. err := XTReadDB().Model(&info).Where("org_id=? and status = 1", orgid).Last(&info).Error
  885. return info, err
  886. }
  887. func GetLastDrugWarehouseInfSix(drugid int64) (models.XtDrugWarehouseInfo, error) {
  888. info := models.XtDrugWarehouseInfo{}
  889. err := XTReadDB().Model(&info).Where("drug_id=? and status = 1", drugid).Last(&info).Error
  890. return info, err
  891. }
  892. func GetFirstDrugWarehouseInfo(drugid int64) (models.XtDrugWarehouseInfo, error) {
  893. info := models.XtDrugWarehouseInfo{}
  894. err := XTReadDB().Model(&info).Where("id=? and status = 1", drugid).First(&info).Error
  895. return info, err
  896. }
  897. func GetDrugInventoryDetail(id int64) (models.XtDrugInventory, error) {
  898. inventory := models.XtDrugInventory{}
  899. err := XTReadDB().Model(&inventory).Where("id = ? and status = 1", id).Find(&inventory).Error
  900. return inventory, err
  901. }
  902. func ModifyDrugInventory(id int64, info models.XtDrugInventory) error {
  903. err := XTWriteDB().Model(&info).Where("id=? and status = 1", id).Updates(map[string]interface{}{"drug_name": info.DrugName, "specification_name": info.SpecificationName, "warehousing_unit": info.WarehousingUnit, "count": info.Count, "last_price": info.LastPrice, "manufacturer": info.Manufacturer, "remark": info.Remark, "drug_id": info.DrugId, "warehousing_order": info.WarehousingOrder, "number": info.Number, "total": info.Number, "drug_origin_place": info.DrugOriginPlace, "stock_max_number": info.StockMaxNumber, "stock_min_number": info.StockMinNumber, "warehouse_info_id": info.WarehouseInfoId, "min_count": info.MinCount, "min_unit": info.MinUnit}).Error
  904. return err
  905. }
  906. func DeleteDrugInventory(id int64) error {
  907. inventory := models.XtDrugInventory{}
  908. err := XTWriteDB().Model(&inventory).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  909. return err
  910. }
  911. func GetDrugInventoryPrintList(ids []string) (info []*models.XtDrugInventory, err error) {
  912. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&info).Error
  913. return info, err
  914. }
  915. func GetDrugInventoryDetailList(keyword string, page int64, limit int64, orgid int64, storehouse_id int64) (list []*models.VmDrugInventory, total int64, err error) {
  916. likeKey := "%" + keyword + "%"
  917. offset := (page - 1) * limit
  918. db := XTReadDB().Table("xt_drug_inventory as x").Where("x.status = 1")
  919. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  920. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  921. fmt.Println(table, tab)
  922. if len(keyword) > 0 {
  923. db = db.Where("x.warehousing_order = ? or r.user_name like ? or t.drug_name like ?", keyword, likeKey, likeKey)
  924. }
  925. if orgid > 0 {
  926. db = db.Where("x.user_org_id = ?", orgid)
  927. }
  928. if storehouse_id > 0 {
  929. db = db.Where("x.storehouse_id = ?", storehouse_id)
  930. }
  931. err = db.Select("x.id,x.drug_name,x.specification_name,x.warehousing_unit,x.count,x.last_price,x.retail_price,x.manufacturer,x.dealer,x.remark,x.drug_id,x.warehousing_order,x.number,x.batch_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.total,x.drug_origin_place,x.expiry_date,x.product_date,x.min_count,x.storehouse_id,x.ctime,x.warehouse_info_id,t.dose,t.dose_unit,t.max_unit,x.min_unit,x.stock_max_number,x.stock_min_number,x.last_stock_max_number,x.last_stock_min_number,x.inventory_type,t.min_number,t.retail_price").Joins("left join sgj_users.sgj_user_admin_role as r on r.id = x.creater").Joins("left join xt_base_drug as t on t.id =x.drug_id").Count(&total).Offset(offset).Limit(limit).Order("x.id desc").Scan(&list).Error
  932. return list, total, err
  933. }
  934. func GetDrugInventoryDetailMapList(keyword string, page int64, limit int64, orgid int64, storehouse_id int64, ids []string) (list []*models.VmDrugInventory, total int64, err error) {
  935. likeKey := "%" + keyword + "%"
  936. offset := (page - 1) * limit
  937. db := XTReadDB().Table("xt_drug_inventory as x").Where("x.status = 1")
  938. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  939. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  940. fmt.Println(table, tab)
  941. if len(keyword) > 0 {
  942. db = db.Where("x.warehousing_order = ? or r.user_name like ? or t.drug_name like ?", keyword, likeKey, likeKey)
  943. }
  944. if orgid > 0 {
  945. db = db.Where("x.user_org_id = ?", orgid)
  946. }
  947. if storehouse_id > 0 {
  948. db = db.Where("x.storehouse_id = ?", storehouse_id)
  949. }
  950. if len(ids) > 0 {
  951. db = db.Where("x.id in(?)", ids)
  952. }
  953. err = db.Select("x.id,x.drug_name,x.specification_name,x.warehousing_unit,x.count,x.last_price,x.retail_price,x.manufacturer,x.dealer,x.remark,x.drug_id,x.warehousing_order,x.number,x.batch_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.total,x.drug_origin_place,x.expiry_date,x.product_date,x.min_count,x.storehouse_id,x.warehouse_info_id,t.dose,t.dose_unit,t.max_unit,x.min_unit,x.stock_max_number,x.stock_min_number,x.last_stock_max_number,x.last_stock_min_number,x.inventory_type,t.min_number,t.retail_price").Joins("left join sgj_users.sgj_user_admin_role as r on r.id = x.creater").Joins("left join xt_base_drug as t on t.id =x.drug_id").Count(&total).Offset(offset).Limit(limit).Scan(&list).Error
  954. return list, total, err
  955. }
  956. func GetDrugInventoryDetailListTwo(keyword string, page int64, limit int64, orgid int64, storehouse_id int64) (list []*models.XtDrugInventory, total int64, err error) {
  957. db := XTReadDB().Model(&list).Where("status = 1")
  958. if orgid > 0 {
  959. db = db.Where("user_org_id = ?", orgid)
  960. }
  961. if storehouse_id > 0 {
  962. db = db.Where("storehouse_id = ?", storehouse_id)
  963. }
  964. offset := (page - 1) * limit
  965. err = db.Preload("BaseDrugLib", "org_id = ? and status = 1", orgid).Preload("DrugWarehouseInfo", "org_id = ? and status = 1", orgid).Count(&total).Offset(offset).Limit(limit).Find(&list).Error
  966. return list, total, err
  967. }
  968. func UpdateDrugProofInventory(id int64, inventory *models.XtDrugInventory) error {
  969. err := XTWriteDB().Model(&inventory).Where("id=? and status = 1", id).Updates(map[string]interface{}{"warehouse_info_id": inventory.WarehouseInfoId, "proof_count": inventory.ProofCount, "warehousing_unit": inventory.WarehousingUnit, "remark": inventory.Remark}).Error
  970. return err
  971. }
  972. func UpdateDrugWarehouseInfoById(id int64, inventory *models.XtDrugWarehouseInfo) error {
  973. err := XTWriteDB().Model(&inventory).Where("id = ?", id).Updates(map[string]interface{}{"stock_max_number": inventory.StockMaxNumber}).Error
  974. return err
  975. }
  976. func GetDrugWarehouseInfoTotal(id int64) (list []*models.XtDrugWarehouseInfo, err error) {
  977. err = XTReadDB().Where("id = ? or warehousing_info_id = ? and status = 1", id, id).Preload("XtBaseDrug", "status = 1").Find(&list).Error
  978. return list, err
  979. }
  980. func CreateDrugWarehouseInfo(info models.DrugWarehouseInfo) error {
  981. err := XTWriteDB().Create(&info).Error
  982. return err
  983. }
  984. func CreateDrugWarehouseOutInfo(info models.DrugWarehouseOutInfo) error {
  985. err := XTWriteDB().Create(&info).Error
  986. return err
  987. }
  988. func GetDamageByDrugId(drugid int64, warhousingorder string, drugtype int64) (drug []*models.VmXtDrugInventory, err error) {
  989. db := XTReadDB().Table("xt_drug_inventory").Where("status = 1")
  990. if drugid > 0 {
  991. db = db.Where("drug_id = ?", drugid)
  992. }
  993. if len(warhousingorder) > 0 {
  994. db = db.Where("warehousing_order = ?", warhousingorder)
  995. }
  996. if drugtype > 0 {
  997. db = db.Where("type = ?", drugtype)
  998. }
  999. if drugtype == 0 {
  1000. db = db.Where("type = 4")
  1001. }
  1002. err = db.Preload("XtBaseDrug", "status = 1").Find(&drug).Error
  1003. return drug, err
  1004. }
  1005. func GetInventoryModeList(orgid int64) (info []*models.PatientDrugWarehouseInfo, err error) {
  1006. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and (x.warehousing_count>0)")
  1007. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  1008. fmt.Println(table)
  1009. if orgid > 0 {
  1010. db = db.Where("x.org_id = ?", orgid)
  1011. }
  1012. err = db.Select("x.drug_id,x.number,x.product_date,x.expiry_date,x.stock_max_number,x.stock_min_number,x.batch_number,t.drug_name,t.max_unit,t.min_unit,t.min_number,t.dose,t.dose_unit,x.manufacturer,x.dealer,t.sum_count").Joins("left join xt_base_drug as t on t.id = x.drug_id").Group("x.drug_id,x.batch_number").Scan(&info).Error
  1013. return info, err
  1014. }
  1015. func GetManufacturerListByKeyword(orgid int64, keyword string) (list []*models.BloodManufacturer, err error) {
  1016. likeKey := "%" + keyword + "%"
  1017. db := XTReadDB().Model(&list).Where("status = 1")
  1018. if orgid > 0 {
  1019. db = db.Where("org_id = ?", orgid)
  1020. }
  1021. if len(keyword) > 0 {
  1022. db = db.Where("manufacturer_name like ?", likeKey)
  1023. }
  1024. err = db.Find(&list).Error
  1025. return list, err
  1026. }
  1027. func GetGoodNameByword(orgid int64, keyword string) (list []*models.GoodInfo, err error) {
  1028. likeKey := "%" + keyword + "%"
  1029. db := XTReadDB().Model(&list).Where("status = 1")
  1030. if orgid > 0 {
  1031. db = db.Where("org_id = ?", orgid)
  1032. }
  1033. if len(keyword) > 0 {
  1034. db = db.Where("good_name like ?", likeKey)
  1035. }
  1036. err = db.Find(&list).Error
  1037. return list, err
  1038. }
  1039. func GetDrugNameByword(orgid int64, keyword string) (list []*models.XtBaseDrug, err error) {
  1040. likeKey := "%" + keyword + "%"
  1041. db := XTReadDB().Model(&list).Where("status = 1")
  1042. if orgid > 0 {
  1043. db = db.Where("org_id = ?", orgid)
  1044. }
  1045. if len(keyword) > 0 {
  1046. db = db.Where("drug_name like ?", likeKey)
  1047. }
  1048. err = db.Find(&list).Error
  1049. return list, err
  1050. }
  1051. func GetDrugNameBywordOne(orgid int64, keyword string) (list []*models.XtBaseDrug, err error) {
  1052. likeKey := "%" + keyword + "%"
  1053. db := XTReadDB().Model(&list).Where("status = 1 and is_self_drug =1")
  1054. if orgid > 0 {
  1055. db = db.Where("org_id = ?", orgid)
  1056. }
  1057. if len(keyword) > 0 {
  1058. db = db.Where("drug_name like ?", likeKey)
  1059. }
  1060. err = db.Find(&list).Error
  1061. return list, err
  1062. }
  1063. func GetGoodWarehouseInfoByOrgId(orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  1064. db := XTReadDB().Model(&info).Where("is_check =1 and status =1")
  1065. if orgid > 0 {
  1066. db = db.Where("org_id = ?", orgid)
  1067. }
  1068. if storehouse_id > 0 {
  1069. db = db.Where("storehouse_id = ?", storehouse_id)
  1070. }
  1071. err = db.Group("good_id").Find(&info).Error
  1072. return info, err
  1073. }
  1074. func GetGoodWarehouseInfoByOrgIdTwo(orgid int64) (info []*models.GoodQueryNewWarehousingInfo, err error) {
  1075. db := XTReadDB().Model(&info).Where("is_check =1 and status =1")
  1076. if orgid > 0 {
  1077. db = db.Where("org_id = ?", orgid)
  1078. }
  1079. err = db.Group("good_id").Find(&info).Error
  1080. return info, err
  1081. }
  1082. func GetGoodNewQuery(orgId int64, storehouse_id int64, good_type int64, keyword string, page int64, limit int64, ids []int64, goodIds []int64, startime int64, endtime int64) (good []*models.NewGoodInformation, total int64, err error) {
  1083. offset := (page - 1) * limit
  1084. likeKey := "%" + keyword + "%"
  1085. db := XTReadDB().Table("xt_good_information as x").Where("x.status = 1 and x.total_count > 0 and x.org_id =?", orgId)
  1086. if good_type == 2 {
  1087. db = db.Where(" x.total_count<=x.stock_warn_count")
  1088. }
  1089. if len(goodIds) > 0 {
  1090. db = db.Where("id in(?)", goodIds)
  1091. }
  1092. if len(keyword) > 0 {
  1093. db = db.Where("good_name like ? or manufacturer in(?)", likeKey, ids)
  1094. }
  1095. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Find(&good).Error
  1096. return good, total, err
  1097. }
  1098. func FindGoodWarehouseInfoGroup(good_id int64, orgid int64, storhouse_id int64, startime int64, endtime int64) (info []*models.WarehousingInfo, err error) {
  1099. db := XTReadDB().Model(&info).Where("status = 1 and is_check = 1")
  1100. if good_id > 0 {
  1101. db = db.Where("good_id = ?", good_id)
  1102. }
  1103. if orgid > 0 {
  1104. db = db.Where("org_id = ?", orgid)
  1105. }
  1106. if storhouse_id > 0 {
  1107. db = db.Where("storehouse_id = ?", storhouse_id)
  1108. }
  1109. if startime > 0 {
  1110. db = db.Where("ctime >=?", startime)
  1111. }
  1112. if endtime > 0 {
  1113. db = db.Where("ctime<=?", endtime)
  1114. }
  1115. db.Group("storehouse_id").Find(&info)
  1116. return info, err
  1117. }
  1118. func FindGoodWarehouseInfo(good_id int64, orgid int64, storhouse_id int64, startime int64, endtime int64) (info []*models.WarehousingInfo, err error) {
  1119. db := XTReadDB().Model(&info).Where("status = 1 and is_check = 1")
  1120. if good_id > 0 {
  1121. db = db.Where("good_id = ?", good_id)
  1122. }
  1123. if orgid > 0 {
  1124. db = db.Where("org_id = ?", orgid)
  1125. }
  1126. if storhouse_id > 0 {
  1127. db = db.Where("storehouse_id = ?", storhouse_id)
  1128. }
  1129. if startime > 0 {
  1130. db = db.Where("ctime >=?", startime)
  1131. }
  1132. if endtime > 0 {
  1133. db = db.Where("ctime<=?", endtime)
  1134. }
  1135. db.Find(&info)
  1136. return info, err
  1137. }
  1138. func FindGoodWarehouseOutInfo(good_id int64, orgid int64, storhouse_id int64, startime int64, endtime int64) (out []*models.WarehouseOutInfo, err error) {
  1139. db := XTReadDB().Model(&out).Where("status = 1 and is_check = 1")
  1140. if good_id > 0 {
  1141. db = db.Where("good_id = ?", good_id)
  1142. }
  1143. if orgid > 0 {
  1144. db = db.Where("org_id = ?", orgid)
  1145. }
  1146. if storhouse_id > 0 {
  1147. db = db.Where("storehouse_id = ?", storhouse_id)
  1148. }
  1149. if startime > 0 {
  1150. db = db.Where("ctime>=?", startime)
  1151. }
  1152. if endtime > 0 {
  1153. db = db.Where("ctime<=?", endtime)
  1154. }
  1155. err = db.Find(&out).Error
  1156. return out, err
  1157. }
  1158. func FindCancelStockInfo(good_id int64, orgid int64, storhouse_id int64, startime int64, endtime int64) (info []*models.CancelStockInfo, err error) {
  1159. db := XTReadDB().Model(&info).Where("status = 1 and is_check = 1")
  1160. if good_id > 0 {
  1161. db = db.Where("good_id = ?", good_id)
  1162. }
  1163. if orgid > 0 {
  1164. db = db.Where("org_id = ?", orgid)
  1165. }
  1166. if storhouse_id > 0 {
  1167. db = db.Where("storehouse_id = ?", storhouse_id)
  1168. }
  1169. if startime > 0 {
  1170. db = db.Where("ctime>=?", startime)
  1171. }
  1172. if endtime > 0 {
  1173. db = db.Where("ctime<=?", endtime)
  1174. }
  1175. err = db.Find(&info).Error
  1176. return info, err
  1177. }
  1178. func FindGoodStockOutFlow(good_id int64, user_org_id int64, storhouse_id int64, startime int64, endtime int64) (info []*models.VmStockFlow, err error) {
  1179. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 2 or consumable_type = 3)")
  1180. if good_id > 0 {
  1181. db = db.Where("good_id = ?", good_id)
  1182. }
  1183. if user_org_id > 0 {
  1184. db = db.Where("user_org_id = ?", user_org_id)
  1185. }
  1186. if storhouse_id > 0 {
  1187. db = db.Where("storehouse_id = ?", storhouse_id)
  1188. }
  1189. if startime > 0 {
  1190. db = db.Where("ctime >= ?", storhouse_id)
  1191. }
  1192. if endtime > 0 {
  1193. db = db.Where("ctime <= ?", storhouse_id)
  1194. }
  1195. err = db.Find(&info).Error
  1196. return info, err
  1197. }
  1198. func FindGoodCancelFlow(good_id int64, user_org_id int64, storhouse_id int64, startime int64, endtime int64) (info []*models.VmStockFlow, err error) {
  1199. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 4 or consumable_type = 7)")
  1200. if good_id > 0 {
  1201. db = db.Where("good_id = ?", good_id)
  1202. }
  1203. if user_org_id > 0 {
  1204. db = db.Where("user_org_id = ?", user_org_id)
  1205. }
  1206. if storhouse_id > 0 {
  1207. db = db.Where("storehouse_id = ?", storhouse_id)
  1208. }
  1209. if startime > 0 {
  1210. db = db.Where("ctime >= ?", storhouse_id)
  1211. }
  1212. if endtime > 0 {
  1213. db = db.Where("ctime <= ?", storhouse_id)
  1214. }
  1215. err = db.Find(&info).Error
  1216. return info, err
  1217. }
  1218. func GetGoodWarehouseOutInfoByIdList(good_id int64, limit int64, page int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.WarehouseOutInfo, total int64, err error) {
  1219. db := XTReadDB().Model(&info).Where("status = 1")
  1220. offset := (page - 1) * limit
  1221. if good_id > 0 {
  1222. db = db.Where("good_id = ?", good_id)
  1223. }
  1224. if orgid > 0 {
  1225. db = db.Where("org_id =?", orgid)
  1226. }
  1227. if startime > 0 {
  1228. db = db.Where("ctime >=?", startime)
  1229. }
  1230. if endtime > 0 {
  1231. db = db.Where("ctime<=?", endtime)
  1232. }
  1233. if storehouse_id > 0 {
  1234. db = db.Where("storehouse_id = ?", storehouse_id)
  1235. }
  1236. err = db.Count(&total).Offset(offset).Limit(limit).Preload("VmStockFlow", "status = 1 and user_org_id = ?", orgid).Preload("GoodInfo", "status = 1 and org_id = ?", orgid).Order("id desc").Find(&info).Error
  1237. return info, total, err
  1238. }
  1239. func GetStockFlushByGoodId(good_id int64, org_id int64, startime int64, endtime int64) (flushinfo []*models.XtWarehouseFlushInfo, err error) {
  1240. db := XTReadDB().Model(&models.XtWarehouseFlushInfo{}).Where("status = 1 and type = 1")
  1241. if good_id > 0 {
  1242. db = db.Where("good_id = ?", good_id)
  1243. }
  1244. if org_id > 0 {
  1245. db = db.Where("user_org_id = ?", org_id)
  1246. }
  1247. if startime > 0 {
  1248. db = db.Where("ctime >=?", startime)
  1249. }
  1250. if endtime > 0 {
  1251. db = db.Where("ctime<=?", endtime)
  1252. }
  1253. err = db.Find(&flushinfo).Error
  1254. return flushinfo, err
  1255. }
  1256. func GetGoodWarehouseOutInfoByIdListTwenty(good_id int64, limit int64, page int64, orgid int64, startime int64, endtime int64) (info []*models.WarehouseOutInfo, err error) {
  1257. db := XTReadDB().Model(&info).Where("status = 1")
  1258. if good_id > 0 {
  1259. db = db.Where("good_id = ?", good_id)
  1260. }
  1261. if orgid > 0 {
  1262. db = db.Where("org_id =?", orgid)
  1263. }
  1264. if startime > 0 {
  1265. db = db.Where("ctime >=?", startime)
  1266. }
  1267. if endtime > 0 {
  1268. db = db.Where("ctime<=?", endtime)
  1269. }
  1270. err = db.Find(&info).Error
  1271. return info, err
  1272. }
  1273. func GetGoodWarehouseInfoByList(goodid int64, orgid int64, startime int64, endtime int64) (info []*models.WarehousingInfo, err error) {
  1274. db := XTReadDB().Model(&info).Where("status= 1 and is_check = 1")
  1275. if goodid > 0 {
  1276. db = db.Where("good_id = ?", goodid)
  1277. }
  1278. if orgid > 0 {
  1279. db = db.Where("org_id = ?", orgid)
  1280. }
  1281. err = db.Find(&info).Error
  1282. return info, err
  1283. }
  1284. func GetGoodCancelStockInfoByList(goodid int64, orgid int64, startime int64, endtime int64) (info []*models.CancelStockInfo, err error) {
  1285. db := XTReadDB().Model(&info).Where("status = 1 and is_check = 1")
  1286. if goodid > 0 {
  1287. db = db.Where("good_id = ?", goodid)
  1288. }
  1289. if orgid > 0 {
  1290. db = db.Where("org_id = ?", orgid)
  1291. }
  1292. err = db.Find(&info).Error
  1293. return info, err
  1294. }
  1295. func GetGoodPatientList(orgid int64, keyword string) (patient []*models.XtPatients, err error) {
  1296. likeKey := "%" + keyword + "%"
  1297. if len(keyword) > 0 {
  1298. err = XTReadDB().Where("user_org_id = ? and status = 1 and name like ?", orgid, likeKey).Find(&patient).Error
  1299. } else {
  1300. err = XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&patient).Error
  1301. }
  1302. return patient, err
  1303. }
  1304. func GetWarehouseOutDetailByPatientIdOne(orgid int64, patient_id int64, startime int64, endtime int64, limit int64, page int64) (info []*models.WarehouseOutInfo, total int64, err error) {
  1305. db := XTReadDB().Model(&info).Where("status = 1 and (is_sys = 1 or is_sys = 5)")
  1306. offset := (page - 1) * limit
  1307. if orgid > 0 {
  1308. db = db.Where("org_id = ?", orgid)
  1309. }
  1310. if patient_id > 0 {
  1311. db = db.Where("patient_id = ?", patient_id)
  1312. }
  1313. if startime > 0 {
  1314. db = db.Where("ctime>=?", startime)
  1315. }
  1316. if endtime > 0 {
  1317. db = db.Where("ctime<=?", endtime)
  1318. }
  1319. err = db.Group("good_id").Preload("GoodInfo", "org_id = ? and status = 1", orgid).Count(&total).Offset(offset).Limit(limit).Find(&info).Error
  1320. return info, total, err
  1321. }
  1322. func GetWarehouseOutDetailByPatientId(orgid int64, patient_id int64, startime int64, endtime int64) (info []*models.WarehouseOutInfo, err error) {
  1323. db := XTReadDB().Model(&info).Where("status = 1 and (is_sys = 1 or is_sys =5)")
  1324. if orgid > 0 {
  1325. db = db.Where("org_id = ?", orgid)
  1326. }
  1327. if patient_id > 0 {
  1328. db = db.Where("patient_id = ?", patient_id)
  1329. }
  1330. if startime > 0 {
  1331. db = db.Where("ctime>=?", startime)
  1332. }
  1333. if endtime > 0 {
  1334. db = db.Where("ctime<=?", endtime)
  1335. }
  1336. err = db.Preload("GoodInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  1337. return info, err
  1338. }
  1339. func GetGoodExpiryDateQuery(storehouse_id int64, expiry_type int64, keyword string, page int64, limit int64, orgid int64, ids []int64, goodIds []int64, startime int64, endtime int64) (info []*models.WarehousingInfo, total int64, err error) {
  1340. offset := (page - 1) * limit
  1341. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.is_check = 1 and x.stock_count >0")
  1342. if storehouse_id > 0 {
  1343. db = db.Where("x.storehouse_id = ?", storehouse_id)
  1344. }
  1345. //已过期
  1346. if expiry_type == 1 {
  1347. db = db.Where("x.expiry_date < ?", startime)
  1348. }
  1349. if expiry_type > 1 {
  1350. db = db.Where("x.expiry_date > ? and x.expiry_date<=?", startime, endtime)
  1351. }
  1352. if orgid > 0 {
  1353. db = db.Where("x.org_id = ?", orgid)
  1354. }
  1355. if len(keyword) > 0 {
  1356. db = db.Where("x.manufacturer in(?) or x.good_id in(?)", ids, goodIds)
  1357. }
  1358. err = db.Count(&total).Offset(offset).Limit(limit).Preload("GoodInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  1359. return info, total, err
  1360. }
  1361. func GetGoodPurchaseStockQuery(good_type int64, keyword string, page int64, limit int64, orgid int64, ids []int64, goodIds []int64) (goodinfo []*models.GoodInfoTwenty, total int64, err error) {
  1362. db := XTReadDB().Model(&goodinfo).Where("status = 1 and total_count > 0")
  1363. offset := (page - 1) * limit
  1364. likeKey := "%" + keyword + "%"
  1365. if good_type > 0 {
  1366. db = db.Where("good_type_id = ?", good_type)
  1367. }
  1368. if orgid > 0 {
  1369. db = db.Where("org_id = ?", orgid)
  1370. }
  1371. if len(ids) > 0 {
  1372. db = db.Where("id in(?)", goodIds)
  1373. }
  1374. if len(keyword) > 0 {
  1375. db = db.Where("good_name like ? or manufacturer in(?)", likeKey, ids)
  1376. }
  1377. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&goodinfo).Error
  1378. return goodinfo, total, err
  1379. }
  1380. func GetDrugWarehouseOutDetailByPatientIdOne(patient_id int64, orgid int64, startime int64, endtime int64, page int64, limit int64) (info []*models.DrugWarehouseOutInfo, total int64, err error) {
  1381. db := XTReadDB().Model(&info).Where("status = 1")
  1382. offset := (page - 1) * limit
  1383. if patient_id > 0 {
  1384. db = db.Where("patient_id = ?", patient_id)
  1385. }
  1386. if orgid > 0 {
  1387. db = db.Where("org_id = ?", orgid)
  1388. }
  1389. if startime > 0 {
  1390. db = db.Where("sys_record_time >= ?", startime)
  1391. }
  1392. if endtime > 0 {
  1393. db = db.Where("sys_record_time <= ?", endtime)
  1394. }
  1395. err = db.Group("drug_id").Count(&total).Offset(offset).Limit(limit).Preload("BaseDrugLib", "status = 1 and org_id = ?", orgid).Find(&info).Error
  1396. return
  1397. }
  1398. func GetDrugWarehouseOutDetailByPatientId(patient_id int64, orgid int64, startime int64, endtime int64) (info []*models.DrugWarehouseOutInfo, err error) {
  1399. db := XTReadDB().Model(&info).Where("status = 1")
  1400. if patient_id > 0 {
  1401. db = db.Where("patient_id = ?", patient_id)
  1402. }
  1403. if orgid > 0 {
  1404. db = db.Where("org_id = ?", orgid)
  1405. }
  1406. if startime > 0 {
  1407. db = db.Where("sys_record_time >= ?", startime)
  1408. }
  1409. if endtime > 0 {
  1410. db = db.Where("sys_record_time <= ?", endtime)
  1411. }
  1412. err = db.Preload("BaseDrugLib", "status = 1 and org_id = ?", orgid).Find(&info).Error
  1413. return
  1414. }
  1415. func GetDrugExpiryDateQuery(storehouse_id int64, expiry_type int64, keyword string, page int64, limit int64, orgid int64, ids []int64, goodIds []int64, startime int64, endtime int64) (info []*models.XtDrugWarehouseInfo, total int64, err error) {
  1416. offset := (page - 1) * limit
  1417. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1 and (x.stock_max_number>0 or x.stock_min_number >0)")
  1418. if storehouse_id > 0 {
  1419. db = db.Where("x.storehouse_id = ?", storehouse_id)
  1420. }
  1421. //已过期
  1422. if expiry_type == 1 {
  1423. db = db.Where("x.expiry_date < ?", startime)
  1424. }
  1425. if expiry_type > 1 {
  1426. db = db.Where("x.expiry_date > ? and x.expiry_date<=?", startime, endtime)
  1427. }
  1428. if orgid > 0 {
  1429. db = db.Where("x.org_id = ?", orgid)
  1430. }
  1431. if len(keyword) > 0 {
  1432. db = db.Where("x.manufacturer in(?) or x.drug_id in(?)", ids, goodIds)
  1433. }
  1434. err = db.Count(&total).Offset(offset).Limit(limit).Preload("XtBaseDrug", "org_id = ? and status = 1", orgid).Find(&info).Error
  1435. return info, total, err
  1436. }
  1437. func GetDrugPurchaseStockQuery(good_type int64, keyword string, page int64, limit int64, orgid int64, startime int64, endtime int64, ids []int64, infos []int64) (druginfo []*models.VmBaseDrugTwenty, total int64, err error) {
  1438. db := XTReadDB().Model(&druginfo).Where("status = 1")
  1439. offset := (page - 1) * limit
  1440. likeKey := "%" + keyword + "%"
  1441. if good_type > 0 {
  1442. db = db.Where("drug_type = ?", good_type)
  1443. }
  1444. if orgid > 0 {
  1445. db = db.Where("org_id = ?", orgid)
  1446. }
  1447. if len(keyword) > 0 {
  1448. db = db.Where("drug_name like ? or manufacturer in(?)", likeKey, ids)
  1449. }
  1450. if len(infos) > 0 {
  1451. db = db.Where("id in(?)", infos)
  1452. }
  1453. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&druginfo).Error
  1454. return druginfo, total, err
  1455. }
  1456. func GetDrugWarehouseInfoByOrgId(orgid int64, storehouse_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  1457. db := XTReadDB().Model(&info).Where("is_check = 1 and status = 1")
  1458. if orgid > 0 {
  1459. db = db.Where("org_id = ?", orgid)
  1460. }
  1461. if storehouse_id > 0 {
  1462. db = db.Where("storehouse_id = ?", storehouse_id)
  1463. }
  1464. err = db.Group("drug_id").Find(&info).Error
  1465. return info, err
  1466. }
  1467. func GetDrugWarehouseInfoByOrgIdTwo(orgid int64) (info []*models.XtDrugWarehouseInfo, err error) {
  1468. db := XTReadDB().Model(&info).Where("is_check = 1 and status = 1")
  1469. if orgid > 0 {
  1470. db = db.Where("org_id = ?", orgid)
  1471. }
  1472. err = db.Group("drug_id").Find(&info).Error
  1473. return info, err
  1474. }
  1475. func GetDrugNewQuery(orgId int64, storehouse_id int64, good_type int64, keyword string, page int64, limit int64, ids []int64, goodIds []int64, startime int64, endtime int64) (drug []*models.VmBaseDrug, total int64, err error) {
  1476. offset := (page - 1) * limit
  1477. likeKey := "%" + keyword + "%"
  1478. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1")
  1479. if good_type == 2 {
  1480. db = db.Where("x.total_count<=x.drug_stock_limit")
  1481. }
  1482. if storehouse_id > 0 {
  1483. db = db.Preload("DrugWarehouseInfo", "org_id = ? and status = 1 and storehouse_id = ? and is_check = 1", orgId, storehouse_id)
  1484. } else {
  1485. db = db.Preload("DrugWarehouseInfo", "org_id = ? and status = 1 and is_check = 1", orgId)
  1486. }
  1487. if len(goodIds) > 0 {
  1488. db = db.Where("id in(?)", goodIds)
  1489. }
  1490. if len(keyword) > 0 {
  1491. db = db.Where("drug_name like ? or manufacturer in(?)", likeKey, ids)
  1492. }
  1493. if orgId > 0 {
  1494. db = db.Where("x.org_id = ?", orgId)
  1495. }
  1496. err = db.Count(&total).Offset(offset).Limit(limit).Preload("DrugWarehouse", func(db *gorm.DB) *gorm.DB {
  1497. if startime > 0 {
  1498. db = db.Where("ctime>=?", startime)
  1499. }
  1500. if endtime > 0 {
  1501. db = db.Where("ctime<=?", endtime)
  1502. }
  1503. if storehouse_id > 0 {
  1504. db = db.Where("storehouse_id = ?", storehouse_id)
  1505. }
  1506. return db.Where("status = 1").Group("drug_id,storehouse_id")
  1507. }).Preload("DrugCancelStockInfo", func(db *gorm.DB) *gorm.DB {
  1508. if startime > 0 {
  1509. db = db.Where("ctime>=?", startime)
  1510. }
  1511. if endtime > 0 {
  1512. db = db.Where("ctime<=?", endtime)
  1513. }
  1514. return db.Where("status = 1")
  1515. }).Preload("DrugWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1516. if startime > 0 {
  1517. db = db.Where("ctime>=?", startime)
  1518. }
  1519. if endtime > 0 {
  1520. db = db.Where("ctime<=?", endtime)
  1521. }
  1522. return db.Where("status = 1")
  1523. }).Preload("DrugFlowWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  1524. if startime > 0 {
  1525. db = db.Where("ctime>=?", startime)
  1526. }
  1527. if endtime > 0 {
  1528. db = db.Where("ctime<=?", endtime)
  1529. }
  1530. if storehouse_id > 0 {
  1531. db = db.Where("storehouse_id = ?", storehouse_id)
  1532. }
  1533. return db.Where("status = 1 and (consumable_type = 2 or consumable_type = 3) and user_org_id = ?", orgId)
  1534. }).Preload("DrugActFlowWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  1535. if startime > 0 {
  1536. db = db.Where("ctime>=?", startime)
  1537. }
  1538. if endtime > 0 {
  1539. db = db.Where("ctime<=?", endtime)
  1540. }
  1541. if storehouse_id > 0 {
  1542. db = db.Where("storehouse_id = ?", storehouse_id)
  1543. }
  1544. return db.Where("status = 1 and (consumable_type = 2 or consumable_type = 3) and user_org_id = ?", orgId)
  1545. }).Order("x.ctime desc").Find(&drug).Error
  1546. return drug, total, err
  1547. }
  1548. func GetDrugStockCountList(orgId int64, storehouse_id int64, good_type int64, keyword string, page int64, limit int64, ids []int64, goodIds []int64, startime int64, endtime int64) (drug []*models.NewBaseDrugOne, total int64, err error) {
  1549. offset := (page - 1) * limit
  1550. likeKey := "%" + keyword + "%"
  1551. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1 AND find_in_set('停用',x.drug_status) = 0 and x.is_self_drug!=1")
  1552. if good_type == 2 {
  1553. db = db.Where("x.sum_count<=x.drug_stock_limit")
  1554. }
  1555. if good_type == 3 {
  1556. db = db.Where("x.sum_count = 0")
  1557. }
  1558. if good_type == 4 {
  1559. db = db.Where("x.sum_count > 0")
  1560. }
  1561. if storehouse_id > 0 {
  1562. db = db.Preload("DrugWarehouseInfo", "org_id = ? and status = 1 and storehouse_id = ? and is_check = 1", orgId, storehouse_id)
  1563. } else {
  1564. db = db.Preload("DrugWarehouseInfo", "org_id = ? and status = 1 and is_check = 1", orgId)
  1565. }
  1566. if len(keyword) > 0 {
  1567. db = db.Where("x.drug_name like ? or x.manufacturer in(?)", likeKey, ids)
  1568. }
  1569. if orgId > 0 {
  1570. db = db.Where("x.org_id = ?", orgId)
  1571. }
  1572. if orgId == 10489 {
  1573. err = db.Preload("XtDrugStockCount", func(db *gorm.DB) *gorm.DB {
  1574. if storehouse_id > 0 {
  1575. db = db.Where("storehouse_id = ?", storehouse_id)
  1576. }
  1577. return db.Where("status = 1 and user_org_id = ?", orgId)
  1578. }).Preload("DrugStockCount", func(db *gorm.DB) *gorm.DB {
  1579. if storehouse_id > 0 {
  1580. db = db.Where("storehouse_id = ?", storehouse_id)
  1581. }
  1582. return db.Where("status = 1 and user_org_id = ?", orgId).Group("drug_id,storehouse_id")
  1583. }).Count(&total).Offset(offset).Limit(limit).Order("x.sum_count desc").Find(&drug).Error
  1584. } else {
  1585. err = db.Preload("XtDrugStockCount", func(db *gorm.DB) *gorm.DB {
  1586. if storehouse_id > 0 {
  1587. db = db.Where("storehouse_id = ?", storehouse_id)
  1588. }
  1589. return db.Where("status = 1 and user_org_id = ?", orgId)
  1590. }).Preload("DrugStockCount", func(db *gorm.DB) *gorm.DB {
  1591. if storehouse_id > 0 {
  1592. db = db.Where("storehouse_id = ?", storehouse_id)
  1593. }
  1594. return db.Where("status = 1 and user_org_id = ?", orgId).Group("drug_id,storehouse_id")
  1595. }).Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Find(&drug).Error
  1596. }
  1597. return drug, total, err
  1598. }
  1599. func GetDrugWarehouseOutInfoByIdList(good_id int64, limit int64, page int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.DrugWarehouseOutInfo, total int64, err error) {
  1600. db := XTReadDB().Model(&info).Where("status = 1")
  1601. offset := (page - 1) * limit
  1602. if good_id > 0 {
  1603. db = db.Where("drug_id = ?", good_id)
  1604. }
  1605. if orgid > 0 {
  1606. db = db.Where("org_id =?", orgid)
  1607. }
  1608. if startime > 0 {
  1609. db = db.Where("ctime >=?", startime)
  1610. }
  1611. if endtime > 0 {
  1612. db = db.Where("ctime<=?", endtime)
  1613. }
  1614. if storehouse_id > 0 {
  1615. db = db.Where("storehouse_id = ?", storehouse_id)
  1616. }
  1617. err = db.Count(&total).Offset(offset).Limit(limit).Preload("BaseDrugLib", "status=1 and org_id = ?", orgid).Order("id desc").Find(&info).Error
  1618. return info, total, err
  1619. }
  1620. func GetAllGoodListSeven(orgid int64) (good []*models.GoodInfoSeven, err error) {
  1621. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&good).Error
  1622. return good, err
  1623. }
  1624. func GetAllGoodListEight(orgid int64) (good []*models.GoodInfoSeven, err error) {
  1625. err = XTReadDB().Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Find(&good).Error
  1626. return good, err
  1627. }
  1628. func GetGoodInformationWarehouseInfo(good_id int64, orgid int64) (info []*models.WarehousingInfoTen, err error) {
  1629. err = XTReadDB().Where("good_id = ? and org_id = ? and status = 1 and is_check = 1 and stock_count > 0", good_id, orgid).Find(&info).Error
  1630. return info, err
  1631. }
  1632. func UpdateGoodInformation(id int64, orgid int64, total_count int64) error {
  1633. err := XTWriteDB().Model(&models.GoodInfoTen{}).Where("id = ? and org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"total_count": total_count}).Error
  1634. return err
  1635. }
  1636. func UpdateGoodListCount(user_org_id int64, over_count int64, good_id int64, sotrehouse_id int64) error {
  1637. err := XTWriteDB().Model(&models.XtGoodStockCount{}).Where("status=1 and user_org_id =? and good_id =? and storehouse_id=?", user_org_id, good_id, sotrehouse_id).Update(map[string]interface{}{"stock_out_count": over_count}).Error
  1638. return err
  1639. }
  1640. func UpdateGoodListCountOne(user_org_id int64, over_count int64, good_id int64, storehouse_id int64) error {
  1641. err = XTWriteDB().Model(&models.XtGoodStockCount{}).Where("status=1 and user_org_id =? and good_id = ? and storehouse_id = ?", user_org_id, good_id, storehouse_id).Update(map[string]interface{}{"stock_act_out_count": over_count}).Error
  1642. return err
  1643. }
  1644. func GetBaseDrugName(orgid int64) (drug []*models.BaseDrugLibTwenty, err error) {
  1645. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&drug).Error
  1646. return drug, err
  1647. }
  1648. func GetDrugWarehouseInfoName(drugid int64, orgid int64) (info []*models.DrugWarehouseInfoTen, err error) {
  1649. err = XTReadDB().Where("drug_id = ? and org_id = ? and is_check = 1 and (stock_max_number >0 or stock_min_number > 0) and status = 1", drugid, orgid).Find(&info).Error
  1650. return info, err
  1651. }
  1652. func UpdateDrugById(id int64, orgid int64, limit_count int64, total_count int64) error {
  1653. err := XTWriteDB().Model(&models.BaseDrugLibTen{}).Where("id = ? and org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"drug_stock_limit_count": limit_count, "sum_count": total_count}).Error
  1654. return err
  1655. }
  1656. func GetDrugFlowFlush(drugid int64, org_id int64, startime int64, endtime int64) (info []*models.XtDrugWarehouseFlushInfo, err error) {
  1657. db := XTReadDB().Model(&info).Where("status = 1 and type = 1")
  1658. if drugid > 0 {
  1659. db = db.Where("drug_id = ?", drugid)
  1660. }
  1661. if org_id > 0 {
  1662. db = db.Where("user_org_id = ?", org_id)
  1663. }
  1664. if startime > 0 {
  1665. db = db.Where("ctime >=?", startime)
  1666. }
  1667. if endtime > 0 {
  1668. db = db.Where("ctime <=?", endtime)
  1669. }
  1670. err = db.Find(&info).Error
  1671. return info, err
  1672. }
  1673. func GetDrugFlowFlushOne(drugid int64, org_id int64, startime int64, endtime int64) (info []*models.XtDrugWarehouseFlushInfo, err error) {
  1674. db := XTReadDB().Model(&info).Where("status = 0 and type = 1")
  1675. if drugid > 0 {
  1676. db = db.Where("drug_id = ?", drugid)
  1677. }
  1678. if org_id > 0 {
  1679. db = db.Where("user_org_id = ?", org_id)
  1680. }
  1681. if startime > 0 {
  1682. db = db.Where("ctime >=?", startime)
  1683. }
  1684. if endtime > 0 {
  1685. db = db.Where("ctime <=?", endtime)
  1686. }
  1687. err = db.Find(&info).Error
  1688. return info, err
  1689. }
  1690. func GetStartGoodWarehouseInfoList(good_id int64, orgid int64, startime int64) (info []*models.StartWarehousingInfo, err error) {
  1691. db := XTReadDB().Model(&info).Where("status = 1 and is_check =1")
  1692. if good_id > 0 {
  1693. db = db.Where("good_id = ?", good_id)
  1694. }
  1695. if orgid > 0 {
  1696. db = db.Where("org_id = ?", orgid)
  1697. }
  1698. if startime > 0 {
  1699. db = db.Where("ctime <?", startime)
  1700. }
  1701. err = db.Find(&info).Error
  1702. return info, err
  1703. }
  1704. func GetEndGoodWarehouseInfoList(good_id int64, orgid int64, endtime int64) (info []*models.StartWarehousingInfo, err error) {
  1705. db := XTReadDB().Model(&info).Where("status = 1 and is_check =1")
  1706. if good_id > 0 {
  1707. db = db.Where("good_id = ?", good_id)
  1708. }
  1709. if orgid > 0 {
  1710. db = db.Where("org_id = ?", orgid)
  1711. }
  1712. if endtime > 0 {
  1713. db = db.Where("ctime <=?", endtime)
  1714. }
  1715. err = db.Find(&info).Error
  1716. return info, err
  1717. }
  1718. func GetAddGoodWarehouseInfoList(good_id int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.StartWarehousingInfo, err error) {
  1719. db := XTReadDB().Model(&info).Where("status = 1 and is_check =1")
  1720. if good_id > 0 {
  1721. db = db.Where("good_id = ?", good_id)
  1722. }
  1723. if orgid > 0 {
  1724. db = db.Where("org_id = ?", orgid)
  1725. }
  1726. if startime > 0 {
  1727. db = db.Where("ctime >=?", startime)
  1728. }
  1729. if endtime > 0 {
  1730. db = db.Where("ctime <=?", endtime)
  1731. }
  1732. if storehouse_id > 0 {
  1733. db = db.Where("storehouse_id = ?", storehouse_id)
  1734. }
  1735. err = db.Find(&info).Error
  1736. return info, err
  1737. }
  1738. func GetAddGoodWarehouseInfoListTwenty(good_id int64, orgid int64, startime int64, endtime int64) (info []*models.StartWarehousingInfo, err error) {
  1739. db := XTReadDB().Model(&info).Where("status = 1 and is_check =1")
  1740. if good_id > 0 {
  1741. db = db.Where("good_id = ?", good_id)
  1742. }
  1743. if orgid > 0 {
  1744. db = db.Where("org_id = ?", orgid)
  1745. }
  1746. if startime > 0 {
  1747. db = db.Where("ctime >=?", startime)
  1748. }
  1749. if endtime > 0 {
  1750. db = db.Where("ctime <=?", endtime)
  1751. }
  1752. err = db.Find(&info).Error
  1753. return info, err
  1754. }
  1755. func GetAddStartFlow(good_id int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.VmStockFlowTwenty, err error) {
  1756. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type =1 or consumable_type = 13)")
  1757. if good_id > 0 {
  1758. db = db.Where("good_id = ?", good_id)
  1759. }
  1760. if orgid > 0 {
  1761. db = db.Where("user_org_id = ?", orgid)
  1762. }
  1763. if startime > 0 {
  1764. db = db.Where("ctime >=?", startime)
  1765. }
  1766. if endtime > 0 {
  1767. db = db.Where("ctime <=?", endtime)
  1768. }
  1769. if storehouse_id > 0 {
  1770. db = db.Where("storehouse_id =?", storehouse_id)
  1771. }
  1772. err = db.Find(&info).Error
  1773. return info, err
  1774. }
  1775. func GetAddStartFlowTwenty(good_id int64, orgid int64, startime int64, endtime int64) (info []*models.VmStockFlowTwenty, err error) {
  1776. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type =1)")
  1777. if good_id > 0 {
  1778. db = db.Where("good_id = ?", good_id)
  1779. }
  1780. if orgid > 0 {
  1781. db = db.Where("user_org_id = ?", orgid)
  1782. }
  1783. if startime > 0 {
  1784. db = db.Where("ctime >=?", startime)
  1785. }
  1786. if endtime > 0 {
  1787. db = db.Where("ctime <=?", endtime)
  1788. }
  1789. err = db.Find(&info).Error
  1790. return info, err
  1791. }
  1792. func GetReduceWarehouseInfoList(good_id int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  1793. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type= 15)")
  1794. if good_id > 0 {
  1795. db = db.Where("good_id = ?", good_id)
  1796. }
  1797. if orgid > 0 {
  1798. db = db.Where("user_org_id = ?", orgid)
  1799. }
  1800. if startime > 0 {
  1801. db = db.Where("ctime >=?", startime)
  1802. }
  1803. if endtime > 0 {
  1804. db = db.Where("ctime <=?", endtime)
  1805. }
  1806. if storehouse_id > 0 {
  1807. db = db.Where("storehouse_id = ?", storehouse_id)
  1808. }
  1809. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  1810. return outinfo, err
  1811. }
  1812. func GetReduceWarehouseInfoListTwenty(good_id int64, orgid int64, startime int64, endtime int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  1813. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type= 15)")
  1814. if good_id > 0 {
  1815. db = db.Where("good_id = ?", good_id)
  1816. }
  1817. if orgid > 0 {
  1818. db = db.Where("user_org_id = ?", orgid)
  1819. }
  1820. if startime > 0 {
  1821. db = db.Where("ctime >=?", startime)
  1822. }
  1823. if endtime > 0 {
  1824. db = db.Where("ctime <=?", endtime)
  1825. }
  1826. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  1827. return outinfo, err
  1828. }
  1829. func GetStartEndCancelFLowInfo(good_id int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  1830. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 7)")
  1831. if good_id > 0 {
  1832. db = db.Where("good_id = ?", good_id)
  1833. }
  1834. if orgid > 0 {
  1835. db = db.Where("user_org_id = ?", orgid)
  1836. }
  1837. if startime > 0 {
  1838. db = db.Where("ctime >=?", startime)
  1839. }
  1840. if endtime > 0 {
  1841. db = db.Where("ctime <=?", endtime)
  1842. }
  1843. if storehouse_id > 0 {
  1844. db = db.Where("storehouse_id =?", storehouse_id)
  1845. }
  1846. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  1847. return outinfo, err
  1848. }
  1849. func GetStartEndCancelFLowInfoTwenty(good_id int64, orgid int64, startime int64, endtime int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  1850. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 7)")
  1851. if good_id > 0 {
  1852. db = db.Where("good_id = ?", good_id)
  1853. }
  1854. if orgid > 0 {
  1855. db = db.Where("user_org_id = ?", orgid)
  1856. }
  1857. if startime > 0 {
  1858. db = db.Where("ctime >=?", startime)
  1859. }
  1860. if endtime > 0 {
  1861. db = db.Where("ctime <=?", endtime)
  1862. }
  1863. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  1864. return outinfo, err
  1865. }
  1866. func FindeStartFlowInfo(good_id int64, orgid int64, startime int64) (info []*models.VmStockFlowTwenty, err error) {
  1867. db := XTReadDB().Model(&info).Where("status = 1 and consumable_type = 1")
  1868. if good_id > 0 {
  1869. db = db.Where("good_id = ?", good_id)
  1870. }
  1871. if orgid > 0 {
  1872. db = db.Where("user_org_id = ?", orgid)
  1873. }
  1874. if startime > 0 {
  1875. db = db.Where("ctime <?", startime)
  1876. }
  1877. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  1878. return info, err
  1879. }
  1880. func FindeStartFlowOutInfo(good_id int64, orgid int64, startime int64) (info []*models.VmStockFlowTwenty, err error) {
  1881. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 2 or consumable_type = 3)")
  1882. if good_id > 0 {
  1883. db = db.Where("good_id = ?", good_id)
  1884. }
  1885. if orgid > 0 {
  1886. db = db.Where("user_org_id = ?", orgid)
  1887. }
  1888. if startime > 0 {
  1889. db = db.Where("ctime <?", startime)
  1890. }
  1891. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  1892. return info, err
  1893. }
  1894. func FindeStartCancelInfo(good_id int64, orgid int64, startime int64) (info []*models.VmStockFlowTwenty, err error) {
  1895. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 4 or consumable_type = 7)")
  1896. if good_id > 0 {
  1897. db = db.Where("good_id = ?", good_id)
  1898. }
  1899. if orgid > 0 {
  1900. db = db.Where("user_org_id = ?", orgid)
  1901. }
  1902. if startime > 0 {
  1903. db = db.Where("ctime <?", startime)
  1904. }
  1905. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  1906. return info, err
  1907. }
  1908. func FindEndStartFlowInfo(good_id int64, orgid int64, endtime int64) (info []*models.VmStockFlowTwenty, err error) {
  1909. db := XTReadDB().Model(&info).Where("status = 1 and consumable_type = 1")
  1910. if good_id > 0 {
  1911. db = db.Where("good_id = ?", good_id)
  1912. }
  1913. if orgid > 0 {
  1914. db = db.Where("user_org_id = ?", orgid)
  1915. }
  1916. if endtime > 0 {
  1917. db = db.Where("ctime <=?", endtime)
  1918. }
  1919. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  1920. return info, err
  1921. }
  1922. func FindeEndFLowOutInfo(good_id int64, orgid int64, endtime int64) (info []*models.VmStockFlowTwenty, err error) {
  1923. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 2 or consumable_type = 3)")
  1924. if good_id > 0 {
  1925. db = db.Where("good_id = ?", good_id)
  1926. }
  1927. if orgid > 0 {
  1928. db = db.Where("user_org_id = ?", orgid)
  1929. }
  1930. if endtime > 0 {
  1931. db = db.Where("ctime <?", endtime)
  1932. }
  1933. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  1934. return info, err
  1935. }
  1936. func FindEndCancelInfo(good_id int64, orgid int64, endtime int64) (info []*models.VmStockFlowTwenty, err error) {
  1937. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 4 or consumable_type = 7)")
  1938. if good_id > 0 {
  1939. db = db.Where("good_id = ?", good_id)
  1940. }
  1941. if orgid > 0 {
  1942. db = db.Where("user_org_id = ?", orgid)
  1943. }
  1944. if endtime > 0 {
  1945. db = db.Where("ctime <?", endtime)
  1946. }
  1947. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  1948. return info, err
  1949. }
  1950. func FindeWarehouseOutInfo(good_id int64, orgid int64, startime int64, storehouse_id int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  1951. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15)")
  1952. if good_id > 0 {
  1953. db = db.Where("good_id = ?", good_id)
  1954. }
  1955. if orgid > 0 {
  1956. db = db.Where("user_org_id = ?", orgid)
  1957. }
  1958. if startime > 0 {
  1959. db = db.Where("ctime <?", startime)
  1960. }
  1961. if storehouse_id > 0 {
  1962. db = db.Where("storehouse_id= ?", storehouse_id)
  1963. }
  1964. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  1965. return outinfo, err
  1966. }
  1967. func FindeWarehouseOutInfoTwenty(good_id int64, orgid int64, startime int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  1968. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15)")
  1969. if good_id > 0 {
  1970. db = db.Where("good_id = ?", good_id)
  1971. }
  1972. if orgid > 0 {
  1973. db = db.Where("user_org_id = ?", orgid)
  1974. }
  1975. if startime > 0 {
  1976. db = db.Where("ctime <?", startime)
  1977. }
  1978. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  1979. return outinfo, err
  1980. }
  1981. func FindeStartStockInventoryProfit(good_id int64, orgid int64, startime int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  1982. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 10)")
  1983. if good_id > 0 {
  1984. db = db.Where("good_id = ?", good_id)
  1985. }
  1986. if orgid > 0 {
  1987. db = db.Where("user_org_id = ?", orgid)
  1988. }
  1989. if startime > 0 {
  1990. db = db.Where("ctime <?", startime)
  1991. }
  1992. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  1993. return outinfo, err
  1994. }
  1995. func FindeEndStokInventoryProfit(good_id int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  1996. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 10)")
  1997. if good_id > 0 {
  1998. db = db.Where("good_id = ?", good_id)
  1999. }
  2000. if orgid > 0 {
  2001. db = db.Where("user_org_id = ?", orgid)
  2002. }
  2003. if startime > 0 {
  2004. db = db.Where("ctime >=?", startime)
  2005. }
  2006. if endtime > 0 {
  2007. db = db.Where("ctime <=?", endtime)
  2008. }
  2009. if storehouse_id > 0 {
  2010. db = db.Where("storehouse_id = ?", storehouse_id)
  2011. }
  2012. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  2013. return outinfo, err
  2014. }
  2015. func FindeEndStokInventoryProfitTwenty(good_id int64, orgid int64, startime int64, endtime int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  2016. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 10)")
  2017. if good_id > 0 {
  2018. db = db.Where("good_id = ?", good_id)
  2019. }
  2020. if orgid > 0 {
  2021. db = db.Where("user_org_id = ?", orgid)
  2022. }
  2023. if startime > 0 {
  2024. db = db.Where("ctime >=?", startime)
  2025. }
  2026. if endtime > 0 {
  2027. db = db.Where("ctime <=?", endtime)
  2028. }
  2029. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  2030. return outinfo, err
  2031. }
  2032. func FindeEndStockInventoryLosses(good_id int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  2033. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 11)")
  2034. if good_id > 0 {
  2035. db = db.Where("good_id = ?", good_id)
  2036. }
  2037. if orgid > 0 {
  2038. db = db.Where("user_org_id = ?", orgid)
  2039. }
  2040. if startime > 0 {
  2041. db = db.Where("ctime >=?", startime)
  2042. }
  2043. if endtime > 0 {
  2044. db = db.Where("ctime <=?", endtime)
  2045. }
  2046. if storehouse_id > 0 {
  2047. db = db.Where("storehouse_id = ?", storehouse_id)
  2048. }
  2049. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  2050. return outinfo, err
  2051. }
  2052. func FindeEndStockInventoryLossesTwenty(good_id int64, orgid int64, startime int64, endtime int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  2053. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 11)")
  2054. if good_id > 0 {
  2055. db = db.Where("good_id = ?", good_id)
  2056. }
  2057. if orgid > 0 {
  2058. db = db.Where("user_org_id = ?", orgid)
  2059. }
  2060. if startime > 0 {
  2061. db = db.Where("ctime >=?", startime)
  2062. }
  2063. if endtime > 0 {
  2064. db = db.Where("ctime <=?", endtime)
  2065. }
  2066. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  2067. return outinfo, err
  2068. }
  2069. func FindStartStockInventoryLosses(good_id int64, orgid int64, startime int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  2070. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 11)")
  2071. if good_id > 0 {
  2072. db = db.Where("good_id = ?", good_id)
  2073. }
  2074. if orgid > 0 {
  2075. db = db.Where("user_org_id = ?", orgid)
  2076. }
  2077. if startime > 0 {
  2078. db = db.Where("ctime <?", startime)
  2079. }
  2080. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  2081. return outinfo, err
  2082. }
  2083. func GetDrugWarehouseInfoStart(drug_id int64, orgid int64, startTime int64) (info []*models.DrugFlowTwenty, err error) {
  2084. db := XTReadDB().Model(&info).Where("status = 1 and consumable_type=1 ")
  2085. if drug_id > 0 {
  2086. db = db.Where("drug_id = ?", drug_id)
  2087. }
  2088. if orgid > 0 {
  2089. db = db.Where("user_org_id = ?", orgid)
  2090. }
  2091. if startTime > 0 {
  2092. db = db.Where("ctime<?", startTime)
  2093. }
  2094. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", orgid).Find(&info).Error
  2095. return info, err
  2096. }
  2097. func DrugWarehouseInfoEnd(drug_id int64, orgid int64, endTime int64) (info []*models.DrugFlowTwenty, err error) {
  2098. db := XTReadDB().Model(&info).Where("status = 1 and consumable_type = 1")
  2099. if drug_id > 0 {
  2100. db = db.Where("drug_id = ?", drug_id)
  2101. }
  2102. if orgid > 0 {
  2103. db = db.Where("user_org_id = ?", orgid)
  2104. }
  2105. if endTime > 0 {
  2106. db = db.Where("ctime<=?", endTime)
  2107. }
  2108. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", orgid).Find(&info).Error
  2109. return info, err
  2110. }
  2111. func DrugWarehouseOutInfoEnd(drug_id int64, orgid int64, endTime int64) (info []*models.DrugFlowTwenty, err error) {
  2112. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 2 or consumable_type= 3 or consumable_type= 15)")
  2113. if drug_id > 0 {
  2114. db = db.Where("drug_id = ?", drug_id)
  2115. }
  2116. if orgid > 0 {
  2117. db = db.Where("user_org_id = ?", orgid)
  2118. }
  2119. if endTime > 0 {
  2120. db = db.Where("ctime<=?", endTime)
  2121. }
  2122. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", orgid).Find(&info).Error
  2123. return info, err
  2124. }
  2125. func GetEndDrugFlowCancelInfo(drug_id int64, orgid int64, endTime int64) (info []*models.DrugFlowTwenty, err error) {
  2126. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 4 or consumable_type= 7)")
  2127. if drug_id > 0 {
  2128. db = db.Where("drug_id = ?", drug_id)
  2129. }
  2130. if orgid > 0 {
  2131. db = db.Where("user_org_id = ?", orgid)
  2132. }
  2133. if endTime > 0 {
  2134. db = db.Where("ctime<=?", endTime)
  2135. }
  2136. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", orgid).Find(&info).Error
  2137. return info, err
  2138. }
  2139. func GetDrugWarehouseStartEnd(drug_id int64, orgid int64, startTime int64, endTime int64) (info []*models.DrugFlowTwenty, err error) {
  2140. db := XTReadDB().Model(&info).Where("status = 1 and consumable_type = 1")
  2141. if drug_id > 0 {
  2142. db = db.Where("drug_id = ?", drug_id)
  2143. }
  2144. if orgid > 0 {
  2145. db = db.Where("user_org_id = ?", orgid)
  2146. }
  2147. if startTime > 0 {
  2148. db = db.Where("ctime>=?", startTime)
  2149. }
  2150. if endTime > 0 {
  2151. db = db.Where("ctime<=?", endTime)
  2152. }
  2153. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", orgid).Find(&info).Error
  2154. return info, err
  2155. }
  2156. func FindeDrugWarehouseOutInfo(drug_id int64, orgid int64, startTime int64, endTime int64) (info []*models.DrugFlowTwenty, err error) {
  2157. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 2 or consumable_type= 3 or consumable_type= 15 )")
  2158. if drug_id > 0 {
  2159. db = db.Where("drug_id = ?", drug_id)
  2160. }
  2161. if orgid > 0 {
  2162. db = db.Where("user_org_id = ?", orgid)
  2163. }
  2164. if startTime > 0 {
  2165. db = db.Where("ctime>=?", startTime)
  2166. }
  2167. if endTime > 0 {
  2168. db = db.Where("ctime<=?", endTime)
  2169. }
  2170. err = db.Preload("DrugWarehouseInfoOne", "status= 1 and org_id = ?", orgid).Find(&info).Error
  2171. return info, err
  2172. }
  2173. func FindStartEndDrugWarehouseOutInfo(drug_id int64, orgid int64, startTime int64, endTime int64) (info []*models.DrugFlowTwenty, err error) {
  2174. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type= 7)")
  2175. if drug_id > 0 {
  2176. db = db.Where("drug_id = ?", drug_id)
  2177. }
  2178. if orgid > 0 {
  2179. db = db.Where("user_org_id = ?", orgid)
  2180. }
  2181. if startTime > 0 {
  2182. db = db.Where("ctime>=?", startTime)
  2183. }
  2184. if endTime > 0 {
  2185. db = db.Where("ctime<=?", endTime)
  2186. }
  2187. err = db.Preload("DrugWarehouseInfoOne", "status= 1 and org_id = ?", orgid).Find(&info).Error
  2188. return info, err
  2189. }
  2190. func FindeDrugWarehouseOutStart(drug_id int64, orgid int64, startTime int64) (info []*models.DrugFlow, err error) {
  2191. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 2 or consumable_type= 3)")
  2192. if drug_id > 0 {
  2193. db = db.Where("drug_id = ?", drug_id)
  2194. }
  2195. if orgid > 0 {
  2196. db = db.Where("user_org_id = ?", orgid)
  2197. }
  2198. if startTime > 0 {
  2199. db = db.Where("ctime <?", startTime)
  2200. }
  2201. err = db.Preload("DrugWarehouseInfoOne", "status= 1 and org_id = ?", orgid).Find(&info).Error
  2202. return info, err
  2203. }
  2204. func GetHisPrescriptionProjectByOrgId(orgid int64) (project []*models.HisPrescriptionProject, err error) {
  2205. err = XTReadDB().Where("user_org_id = ? and status = 0 and type = 3", orgid).Find(&project).Error
  2206. return project, err
  2207. }
  2208. func GetPatientStockFlow(patient_id int64, record_date int64, good_id int64) (models.VmStockFlow, error) {
  2209. flow := models.VmStockFlow{}
  2210. err = XTReadDB().Where("patient_id = ? and system_time = ? and good_id = ?", patient_id, record_date, good_id).Find(&flow).Error
  2211. return flow, err
  2212. }
  2213. func ModifyPatientStockFlow(id int64, flow models.VmStockFlow) error {
  2214. err = XTReadDB().Model(&models.VmStockFlow{}).Where("id = ?", id).Updates(map[string]interface{}{"count": flow.Count, "status": 1, "is_read": 1}).Error
  2215. return err
  2216. }
  2217. func GetDrugFlowSart(drug_id int64, user_org_id int64, ctime int64) (flow []*models.DrugFlow, err error) {
  2218. db := XTReadDB().Model(&flow).Where("status = 1 and consumable_type = 1")
  2219. if drug_id > 0 {
  2220. db = db.Where("drug_id = ?", drug_id)
  2221. }
  2222. if user_org_id > 0 {
  2223. db = db.Where("user_org_id = ?", user_org_id)
  2224. }
  2225. if ctime > 0 {
  2226. db = db.Where("ctime <?", ctime)
  2227. }
  2228. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", user_org_id).Find(&flow).Error
  2229. return flow, err
  2230. }
  2231. func GetDrugFlowOut(drug_id int64, user_org_id int64, ctime int64) (flow []*models.DrugFlowTwenty, err error) {
  2232. db := XTReadDB().Model(&flow).Where("status = 1 and (consumable_type = 2 or consumable_type= 3 or consumable_type= 15)")
  2233. if drug_id > 0 {
  2234. db = db.Where("drug_id = ?", drug_id)
  2235. }
  2236. if user_org_id > 0 {
  2237. db = db.Where("user_org_id = ?", user_org_id)
  2238. }
  2239. if ctime > 0 {
  2240. db = db.Where("ctime <?", ctime)
  2241. }
  2242. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", user_org_id).Find(&flow).Error
  2243. return flow, err
  2244. }
  2245. func GetStartDrugFlowCancelInfo(drug_id int64, user_org_id int64, ctime int64) (flow []*models.DrugFlowTwenty, err error) {
  2246. db := XTReadDB().Model(&flow).Where("status = 1 and (consumable_type = 4 or consumable_type = 7)")
  2247. if drug_id > 0 {
  2248. db = db.Where("drug_id = ?", drug_id)
  2249. }
  2250. if user_org_id > 0 {
  2251. db = db.Where("user_org_id = ?", user_org_id)
  2252. }
  2253. if ctime > 0 {
  2254. db = db.Where("ctime <?", ctime)
  2255. }
  2256. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", user_org_id).Find(&flow).Error
  2257. return flow, err
  2258. }
  2259. func GetDrugFlowStartProfit(drug_id int64, user_org_id int64, ctime int64) (flow []*models.DrugFlowTwenty, err error) {
  2260. db := XTReadDB().Model(&flow).Where("status = 1 and consumable_type = 10")
  2261. if drug_id > 0 {
  2262. db = db.Where("drug_id = ?", drug_id)
  2263. }
  2264. if user_org_id > 0 {
  2265. db = db.Where("user_org_id = ?", user_org_id)
  2266. }
  2267. if ctime > 0 {
  2268. db = db.Where("ctime <?", ctime)
  2269. }
  2270. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", user_org_id).Find(&flow).Error
  2271. return flow, err
  2272. }
  2273. func GetDrugFlowStartLosses(drug_id int64, user_org_id int64, ctime int64) (flow []*models.DrugFlowTwenty, err error) {
  2274. db := XTReadDB().Model(&flow).Where("status = 1 and consumable_type = 11 ")
  2275. if drug_id > 0 {
  2276. db = db.Where("drug_id = ?", drug_id)
  2277. }
  2278. if user_org_id > 0 {
  2279. db = db.Where("user_org_id = ?", user_org_id)
  2280. }
  2281. if ctime > 0 {
  2282. db = db.Where("ctime <?", ctime)
  2283. }
  2284. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", user_org_id).Find(&flow).Error
  2285. return flow, err
  2286. }
  2287. func GetDrugFlowEndProfit(drug_id int64, user_org_id int64, endtime int64) (flow []*models.DrugFlowTwenty, err error) {
  2288. db := XTReadDB().Model(&flow).Where("status = 1 and consumable_type = 10")
  2289. if drug_id > 0 {
  2290. db = db.Where("drug_id = ?", drug_id)
  2291. }
  2292. if user_org_id > 0 {
  2293. db = db.Where("user_org_id = ?", user_org_id)
  2294. }
  2295. if endtime > 0 {
  2296. db = db.Where("ctime <=?", endtime)
  2297. }
  2298. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", user_org_id).Find(&flow).Error
  2299. return flow, err
  2300. }
  2301. func GetDrugFlowEndLosses(drug_id int64, user_org_id int64, endtime int64) (flow []*models.DrugFlowTwenty, err error) {
  2302. db := XTReadDB().Model(&flow).Where("status = 1 and consumable_type = 11")
  2303. if drug_id > 0 {
  2304. db = db.Where("drug_id = ?", drug_id)
  2305. }
  2306. if user_org_id > 0 {
  2307. db = db.Where("user_org_id = ?", user_org_id)
  2308. }
  2309. if endtime > 0 {
  2310. db = db.Where("ctime <=?", endtime)
  2311. }
  2312. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", user_org_id).Find(&flow).Error
  2313. return flow, err
  2314. }
  2315. func GetDrugFlowStartEndProfit(drug_id int64, user_org_id int64, startime int64, endtime int64) (flow []*models.DrugFlowTwenty, err error) {
  2316. db := XTReadDB().Model(&flow).Where("status = 1 and consumable_type = 10 ")
  2317. if drug_id > 0 {
  2318. db = db.Where("drug_id = ?", drug_id)
  2319. }
  2320. if user_org_id > 0 {
  2321. db = db.Where("user_org_id = ?", user_org_id)
  2322. }
  2323. if startime > 0 {
  2324. db = db.Where("ctime >=?", startime)
  2325. }
  2326. if endtime > 0 {
  2327. db = db.Where("ctime<=?", endtime)
  2328. }
  2329. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", user_org_id).Find(&flow).Error
  2330. return flow, err
  2331. }
  2332. func GetDrugFlowStartEndLosses(drug_id int64, user_org_id int64, startime int64, endtime int64) (flow []*models.DrugFlowTwenty, err error) {
  2333. db := XTReadDB().Model(&flow).Where("status = 1 and consumable_type = 11 ")
  2334. if drug_id > 0 {
  2335. db = db.Where("drug_id = ?", drug_id)
  2336. }
  2337. if user_org_id > 0 {
  2338. db = db.Where("user_org_id = ?", user_org_id)
  2339. }
  2340. if startime > 0 {
  2341. db = db.Where("ctime >=?", startime)
  2342. }
  2343. if endtime > 0 {
  2344. db = db.Where("ctime<=?", endtime)
  2345. }
  2346. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", user_org_id).Find(&flow).Error
  2347. return flow, err
  2348. }
  2349. func GetDrugWarehouseInfoStorehouseList(orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  2350. err = XTReadDB().Where("org_id = ? and is_check= 1 and status = 1", orgid).Group("drug_id,storehouse_id").Find(&info).Error
  2351. return info, err
  2352. }
  2353. func GetDrugStockCout(drug_id int64, storehouse_id int64, user_org_id int64) (models.XtDrugStockCount, error) {
  2354. drugCount := models.XtDrugStockCount{}
  2355. err := XTReadDB().Where("drug_id = ? and storehouse_id = ? and user_org_id = ? and status = 1", drug_id, storehouse_id, user_org_id).Find(&drugCount).Error
  2356. return drugCount, err
  2357. }
  2358. func GetDrugWarehouseInfoByStorehouseId(storehouse_id int64, drug_id int64, org_id int64) (info []*models.DrugWarehouseInfo, err error) {
  2359. err = XTReadDB().Where("drug_id = ? and storehouse_id = ? and org_id = ? and status =1 and is_check = 1", drug_id, storehouse_id, org_id).Find(&info).Error
  2360. return info, err
  2361. }
  2362. func GetDrugFlowStockOutCount(storehouse_id int64, drug_id int64, org_id int64) (flow []*models.DrugFlow, err error) {
  2363. err = XTReadDB().Where("storehouse_id = ? and drug_id = ? and user_org_id = ? and status = 1 and (consumable_type = 2 or consumable_type =3)", storehouse_id, drug_id, org_id).Find(&flow).Error
  2364. return flow, err
  2365. }
  2366. func GetDrugFlowStockCancelCount(storehouse_id int64, drug_id int64, org_id int64) (flow []*models.DrugFlow, err error) {
  2367. err = XTReadDB().Where("storehouse_id = ? and drug_id = ? and user_org_id = ? and status = 1 and consumable_type = 7", storehouse_id, drug_id, org_id).Find(&flow).Error
  2368. return flow, err
  2369. }
  2370. func CreateDrugStockCount(stockCount models.XtDrugStockCount) error {
  2371. err := XTWriteDB().Create(&stockCount).Error
  2372. return err
  2373. }
  2374. func GetGoodNewPurchaseStockQuery(good_type int64, keyword string, page int64, limit int64, orgid int64, ids []int64, goodIds []int64) (goodinfo []*models.GoodInfoTwenty, total int64, err error) {
  2375. db := XTReadDB().Model(&goodinfo).Where("status = 1")
  2376. offset := (page - 1) * limit
  2377. likeKey := "%" + keyword + "%"
  2378. if good_type > 0 {
  2379. db = db.Where("good_type_id = ?", good_type)
  2380. }
  2381. if orgid > 0 {
  2382. db = db.Where("org_id = ?", orgid)
  2383. }
  2384. //if len(ids) > 0 {
  2385. // db = db.Where("id in(?)", goodIds)
  2386. //}
  2387. if len(keyword) > 0 {
  2388. db = db.Where("good_name like ? or manufacturer in(?)", likeKey, ids)
  2389. }
  2390. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&goodinfo).Error
  2391. return goodinfo, total, err
  2392. }
  2393. func GetGoodNewPurchaseStockQueryOne(good_type int64, keyword string, page int64, limit int64, orgid int64, ids []int64, goodIds []int64) (goodinfo []*models.GoodInfoTwenty, total int64, err error) {
  2394. db := XTReadDB().Model(&goodinfo).Where("status = 1")
  2395. if orgid == 10697 || orgid == 10644 {
  2396. db = db.Where("find_in_set('停用',good_status) = 0")
  2397. }
  2398. offset := (page - 1) * limit
  2399. likeKey := "%" + keyword + "%"
  2400. if good_type > 0 {
  2401. db = db.Where("good_type_id = ?", good_type)
  2402. }
  2403. if orgid > 0 {
  2404. db = db.Where("org_id = ?", orgid)
  2405. }
  2406. if len(ids) > 0 {
  2407. db = db.Where("id in(?)", goodIds)
  2408. }
  2409. if len(keyword) > 0 {
  2410. db = db.Where("good_name like ? or manufacturer in(?)", likeKey, ids)
  2411. }
  2412. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&goodinfo).Error
  2413. return goodinfo, total, err
  2414. }
  2415. func GetStartLastFLow(goodid int64, orgid int64, startime int64, storehouse_id int64) (models.VmStockFlowTwenty, error) {
  2416. flwo := models.VmStockFlowTwenty{}
  2417. err := XTReadDB().Where("good_id = ? and user_org_id = ? and status = 1 and ctime < ? and storehouse_id = ?", goodid, orgid, startime, storehouse_id).Last(&flwo).Error
  2418. return flwo, err
  2419. }
  2420. func GetStartLastFLowTwenty(goodid int64, orgid int64, startime int64) (models.VmStockFlowTwenty, error) {
  2421. flwo := models.VmStockFlowTwenty{}
  2422. err := XTReadDB().Where("good_id = ? and user_org_id = ? and status = 1 and ctime < ?", goodid, orgid, startime).Order("ctime desc").Last(&flwo).Error
  2423. return flwo, err
  2424. }
  2425. func GetEndLastFlow(goodid int64, orgid int64, endtime int64, storehouse_id int64) (models.VmStockFlowTwenty, error) {
  2426. flwo := models.VmStockFlowTwenty{}
  2427. err := XTReadDB().Where("good_id = ? and user_org_id = ? and status = 1 and ctime <= ? and storehouse_id= ?", goodid, orgid, endtime, storehouse_id).Order("ctime desc").Last(&flwo).Error
  2428. return flwo, err
  2429. }
  2430. func GetEndLastFlowTwenty(goodid int64, orgid int64, endtime int64) (models.VmStockFlowTwenty, error) {
  2431. flwo := models.VmStockFlowTwenty{}
  2432. err := XTReadDB().Where("good_id = ? and user_org_id = ? and status = 1 and ctime <= ?", goodid, orgid, endtime).Last(&flwo).Error
  2433. return flwo, err
  2434. }
  2435. func GetDrugNewPurchaseStockQuery(good_type int64, keyword string, page int64, limit int64, orgid int64, startime int64, endtime int64, ids []int64, infos []int64) (druginfo []*models.VmBaseDrugTwenty, total int64, err error) {
  2436. db := XTReadDB().Model(&druginfo).Where("status = 1 and is_self_drug!=1")
  2437. offset := (page - 1) * limit
  2438. likeKey := "%" + keyword + "%"
  2439. if good_type > 0 {
  2440. db = db.Where("drug_type = ?", good_type)
  2441. }
  2442. if orgid == 10697 || orgid == 10644 {
  2443. db = db.Where("find_in_set('停用',drug_status) = 0")
  2444. }
  2445. if orgid > 0 {
  2446. db = db.Where("org_id = ?", orgid)
  2447. }
  2448. if len(keyword) > 0 {
  2449. db = db.Where("drug_name like ? or manufacturer in(?)", likeKey, ids)
  2450. }
  2451. //if len(infos) > 0 {
  2452. // db = db.Where("id in(?)", infos)
  2453. //}
  2454. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&druginfo).Error
  2455. return druginfo, total, err
  2456. }
  2457. func GetDrugNewPurchaseStockQueryList(good_type int64, keyword string, page int64, limit int64, orgid int64, startime int64, endtime int64, ids []int64, infos []int64) (druginfo []*models.VmBaseDrugTwenty, total int64, err error) {
  2458. db := XTReadDB().Model(&druginfo).Where("status = 1")
  2459. offset := (page - 1) * limit
  2460. likeKey := "%" + keyword + "%"
  2461. if good_type > 0 {
  2462. db = db.Where("drug_type = ?", good_type)
  2463. }
  2464. if orgid > 0 {
  2465. db = db.Where("org_id = ?", orgid)
  2466. }
  2467. if len(keyword) > 0 {
  2468. db = db.Where("drug_name like ? or manufacturer in(?)", likeKey, ids)
  2469. }
  2470. if len(infos) > 0 {
  2471. db = db.Where("id in(?)", infos)
  2472. }
  2473. err = db.Count(&total).Offset(offset).Limit(limit).Order("drug_alias desc").Find(&druginfo).Error
  2474. return druginfo, total, err
  2475. }
  2476. func GetDrugStartFlow(drug_id int64, user_org_id int64, startime int64) (models.DrugFlowTwenty, error) {
  2477. flow := models.DrugFlowTwenty{}
  2478. err := XTReadDB().Where("drug_id = ? and user_org_id = ? and ctime<? and status = 1", drug_id, user_org_id, startime).Order("ctime desc").Last(&flow).Error
  2479. return flow, err
  2480. }
  2481. func GetDrugEndFlow(drug_id int64, user_org_id int64, endtime int64) (models.DrugFlowTwenty, error) {
  2482. flow := models.DrugFlowTwenty{}
  2483. err := XTReadDB().Where("drug_id = ? and user_org_id = ? and ctime<=? and status = 1", drug_id, user_org_id, endtime).Order("ctime desc").Last(&flow).Error
  2484. return flow, err
  2485. }
  2486. func GetAllStockCount(orgid int64) (stock []*models.XtGoodStockCount, err error) {
  2487. err = XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&stock).Error
  2488. return stock, err
  2489. }
  2490. func GetLastStockFlow(good_id int64) (models.VmStockFlow, error) {
  2491. flow := models.VmStockFlow{}
  2492. err := XTReadDB().Where("good_id = ? and status =1 and ctime<=1667231999", good_id).Last(&flow).Error
  2493. return flow, err
  2494. }
  2495. func ModifyStockFlowById(id int64, over_count int64) error {
  2496. err := XTWriteDB().Model(&models.VmStockFlow{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"over_count": over_count}).Error
  2497. return err
  2498. }
  2499. func GetAllDrugCount(user_org_id int64) (drug []*models.XtDrugStockCount, err error) {
  2500. err = XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&drug).Error
  2501. return drug, err
  2502. }
  2503. func GetLastDrugFlow(drug_id int64) (models.DrugFlow, error) {
  2504. flow := models.DrugFlow{}
  2505. err := XTReadDB().Where("drug_id = ? and status =1 and ctime<=1667231999", drug_id).Last(&flow).Error
  2506. return flow, err
  2507. }
  2508. func ModifyDrugFlowById(id int64, over_count int64) error {
  2509. err := XTWriteDB().Model(&models.DrugFlow{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"over_count": over_count}).Error
  2510. return err
  2511. }
  2512. func GetDrugInventoryWarehouseInfoList(org_id int64, storehouse_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  2513. if org_id == 10138 || org_id == 10278 {
  2514. err = XTReadDB().Model(&info).Where("status = 1 and storehouse_id = ? and is_check = 1 and org_id = ?", storehouse_id, org_id).Preload("XtBaseDrug", "status= 1").Order("drug_id").Find(&info).Error
  2515. return info, err
  2516. } else {
  2517. err = XTReadDB().Model(&info).Where("status = 1 and storehouse_id = ? and is_check = 1 and org_id = ? and (stock_max_number >0 or stock_min_number >0)", storehouse_id, org_id).Preload("XtBaseDrug", "status= 1").Order("drug_id").Find(&info).Error
  2518. return info, err
  2519. }
  2520. }
  2521. func GetGoodStockCountList(user_org_id int64, good_id int64) (stock []*models.XtGoodStockCount, err error) {
  2522. err = XTReadDB().Where("user_org_id =? and status=1 and good_id = ?", user_org_id, good_id).Find(&stock).Error
  2523. return stock, err
  2524. }
  2525. func GetDrugCountMapList(drug_id int64, user_org_id int64) (drug []*models.XtDrugStockCount, err error) {
  2526. err = XTReadDB().Where("drug_id = ? and user_org_id =? and status=1", drug_id, user_org_id).Find(&drug).Error
  2527. return drug, err
  2528. }
  2529. func UpdateDrugStockInCount(drugId int64, storehouseId int64, over_count int64, user_org_id int64) error {
  2530. err := XTWriteDB().Model(&models.XtDrugStockCount{}).Where("drug_id =? and storehouse_id =? and status=1 and user_org_id =?", drugId, storehouseId, user_org_id).Update(map[string]interface{}{"sum_out_count": over_count}).Error
  2531. return err
  2532. }
  2533. func UpdateDrugStockInCountOne(drugId int64, storehouseId int64, over_count int64, user_org_id int64) error {
  2534. err := XTWriteDB().Model(&models.XtDrugStockCount{}).Where("drug_id =? and storehouse_id =? and status=1 and user_org_id =?", drugId, storehouseId, user_org_id).Update(map[string]interface{}{"sum_act_out_count": over_count}).Error
  2535. return err
  2536. }
  2537. func GetPurchaseStockQueryList(good_type int64, keyword string, page int64, limit int64, orgid int64, ids []int64, goodIds []int64) (goodinfo []*models.GoodInfoTwenty, total int64, err error) {
  2538. db := XTReadDB().Model(&goodinfo).Where("status = 1")
  2539. offset := (page - 1) * limit
  2540. likeKey := "%" + keyword + "%"
  2541. if good_type > 0 {
  2542. db = db.Where("good_type_id = ?", good_type)
  2543. }
  2544. if orgid > 0 {
  2545. db = db.Where("org_id = ?", orgid)
  2546. }
  2547. if len(ids) > 0 {
  2548. db = db.Where("id in(?)", goodIds)
  2549. }
  2550. if len(keyword) > 0 {
  2551. db = db.Where("good_name like ? or manufacturer in(?)", likeKey, ids)
  2552. }
  2553. err = db.Count(&total).Offset(offset).Limit(limit).Order("good_number desc").Find(&goodinfo).Error
  2554. return goodinfo, total, err
  2555. }
  2556. func GetInitBalanceList(good_id int64, user_org_id int64, start_time int64, end_time int64, storehouse_id int64) (list []*models.StWarehousingInfoOne, err error) {
  2557. db := XTReadDB().Model(&list).Where("status=1 and is_check=1")
  2558. if good_id > 0 {
  2559. db = db.Where("good_id = ?", good_id)
  2560. }
  2561. if user_org_id > 0 {
  2562. db = db.Where("org_id = ?", user_org_id)
  2563. }
  2564. if start_time > 0 {
  2565. db = db.Where("ctime<?", start_time)
  2566. }
  2567. if storehouse_id > 0 {
  2568. db = db.Where("storehouse_id = ?", storehouse_id)
  2569. }
  2570. err = db.Find(&list).Error
  2571. return list, err
  2572. }
  2573. func GetInitOutBanceTenty(good_id int64, user_org_id int64, start_time int64, end_time int64, storehouse_id int64) (list []*models.VmStockFlowTwenty, err error) {
  2574. db := XTReadDB().Model(&list).Where("status=1 and (consumable_type=2 or consumable_type =3)")
  2575. if good_id > 0 {
  2576. db = db.Where("good_id = ?", good_id)
  2577. }
  2578. if user_org_id > 0 {
  2579. db = db.Where("user_org_id = ?", user_org_id)
  2580. }
  2581. if start_time > 0 {
  2582. db = db.Where("ctime<?", start_time)
  2583. }
  2584. if storehouse_id > 0 {
  2585. db = db.Where("storehouse_id = ?", storehouse_id)
  2586. }
  2587. err = db.Find(&list).Error
  2588. return list, err
  2589. }
  2590. func GetInitOutBanceList(good_id int64, user_org_id int64, start_time int64, end_time int64, storehouse_id int64) (list []*models.WarehouseOutInfoTenty, err error) {
  2591. db := XTReadDB().Model(&list).Where("status=1")
  2592. if good_id > 0 {
  2593. db = db.Where("good_id = ?", good_id)
  2594. }
  2595. if user_org_id > 0 {
  2596. db = db.Where("org_id = ?", user_org_id)
  2597. }
  2598. if start_time > 0 {
  2599. db = db.Where("ctime<?", start_time)
  2600. }
  2601. if storehouse_id > 0 {
  2602. db = db.Where("storehouse_id = ?", storehouse_id)
  2603. }
  2604. err = db.Group("patient_id,good_id,sys_record_time,is_sys,ctime").Find(&list).Error
  2605. return list, err
  2606. }
  2607. func GetInitInfoFlow(good_id int64, user_org_id int64, start_time int64, end_time int64, storehouse_id int64) (list []*models.VmStockFlowTwenty, err error) {
  2608. db := XTReadDB().Model(&list).Where("status=1 and consumable_type =10")
  2609. if good_id > 0 {
  2610. db = db.Where("good_id = ?", good_id)
  2611. }
  2612. if user_org_id > 0 {
  2613. db = db.Where("user_org_id = ?", user_org_id)
  2614. }
  2615. if start_time > 0 {
  2616. db = db.Where("ctime<?", start_time)
  2617. }
  2618. if storehouse_id > 0 {
  2619. db = db.Where("storehouse_id = ?", storehouse_id)
  2620. }
  2621. err = db.Find(&list).Error
  2622. return list, err
  2623. }
  2624. func GetInitOutInfoFlow(good_id int64, user_org_id int64, start_time int64, end_time int64, storehouse_id int64) (list []*models.VmStockFlowTwenty, err error) {
  2625. db := XTReadDB().Model(&list).Where("status=1 and consumable_type =11")
  2626. if good_id > 0 {
  2627. db = db.Where("good_id = ?", good_id)
  2628. }
  2629. if user_org_id > 0 {
  2630. db = db.Where("user_org_id = ?", user_org_id)
  2631. }
  2632. if start_time > 0 {
  2633. db = db.Where("ctime<?", start_time)
  2634. }
  2635. if storehouse_id > 0 {
  2636. db = db.Where("storehouse_id = ?", storehouse_id)
  2637. }
  2638. err = db.Find(&list).Error
  2639. return list, err
  2640. }
  2641. func GetNewDrugFlowInWarehouseInfo(drug_id int64, user_org_id int64, start_time int64) (list []*models.DrugFlowTwenty, err error) {
  2642. db := XTReadDB().Model(&list).Where("status=1 and consumable_type=1")
  2643. if drug_id > 0 {
  2644. db = db.Where("drug_id = ?", drug_id)
  2645. }
  2646. if user_org_id > 0 {
  2647. db = db.Where("user_org_id = ?", user_org_id)
  2648. }
  2649. if start_time > 0 {
  2650. db = db.Where("ctime<?", start_time)
  2651. }
  2652. err = db.Find(&list).Error
  2653. return list, err
  2654. }
  2655. func GetNewDrugFlowPofitWarehouseInfo(drug_id int64, user_org_id int64, start_time int64) (list []*models.DrugFlowTwenty, err error) {
  2656. db := XTReadDB().Model(&list).Where("status=1 and consumable_type=10")
  2657. if drug_id > 0 {
  2658. db = db.Where("drug_id = ?", drug_id)
  2659. }
  2660. if user_org_id > 0 {
  2661. db = db.Where("user_org_id = ?", user_org_id)
  2662. }
  2663. if start_time > 0 {
  2664. db = db.Where("ctime<?", start_time)
  2665. }
  2666. err = db.Find(&list).Error
  2667. return list, err
  2668. }
  2669. func GetNewDrugFlowOutWarehouseInfo(drug_id int64, user_org_id int64, start_time int64) (list []*models.DrugFlowTwenty, err error) {
  2670. db := XTReadDB().Model(&list).Where("status=1 and (consumable_type=3 or consumable_type=2)")
  2671. if drug_id > 0 {
  2672. db = db.Where("drug_id = ?", drug_id)
  2673. }
  2674. if user_org_id > 0 {
  2675. db = db.Where("user_org_id = ?", user_org_id)
  2676. }
  2677. if start_time > 0 {
  2678. db = db.Where("ctime<?", start_time)
  2679. }
  2680. err = db.Find(&list).Error
  2681. return list, err
  2682. }
  2683. func GetNewDrugFlowCacelWarehouseInfo(drug_id int64, user_org_id int64, start_time int64) (list []*models.DrugFlowTwenty, err error) {
  2684. db := XTReadDB().Model(&list).Where("status=1 and consumable_type=7")
  2685. if drug_id > 0 {
  2686. db = db.Where("drug_id = ?", drug_id)
  2687. }
  2688. if user_org_id > 0 {
  2689. db = db.Where("user_org_id = ?", user_org_id)
  2690. }
  2691. if start_time > 0 {
  2692. db = db.Where("ctime<?", start_time)
  2693. }
  2694. err = db.Find(&list).Error
  2695. return list, err
  2696. }
  2697. func GetNewDrugFlowNoPofitWarehouseInfo(drug_id int64, user_org_id int64, start_time int64) (list []*models.DrugFlowTwenty, err error) {
  2698. db := XTReadDB().Model(&list).Where("status=1 and consumable_type=11")
  2699. if drug_id > 0 {
  2700. db = db.Where("drug_id = ?", drug_id)
  2701. }
  2702. if user_org_id > 0 {
  2703. db = db.Where("user_org_id = ?", user_org_id)
  2704. }
  2705. if start_time > 0 {
  2706. db = db.Where("ctime<?", start_time)
  2707. }
  2708. err = db.Find(&list).Error
  2709. return list, err
  2710. }
  2711. func GetHisAdviceGroupList(drug_id int64, user_org_id int64) (advicelist []*models.HisDoctorAdviceInfo, err error) {
  2712. if user_org_id == 10265 {
  2713. err = XTReadDB().Where("drug_id = ? and user_org_id = ? and status=1 and execution_state =1", drug_id, user_org_id).Order("advice_date asc").Find(&advicelist).Error
  2714. return advicelist, err
  2715. } else {
  2716. err = XTReadDB().Where("drug_id = ? and user_org_id = ? and status=1", drug_id, user_org_id).Order("advice_date asc").Find(&advicelist).Error
  2717. return advicelist, err
  2718. }
  2719. }
  2720. func GetDrugWarehousOutInfo(drug_id int64, org_id int64) (out []*models.XtDrugWarehouseOutInfo, err error) {
  2721. err = XTReadDB().Where("drug_id = ? and status=1 and org_id=? and is_sys=1", drug_id, org_id).Order("sys_record_time asc").Find(&out).Error
  2722. return out, err
  2723. }
  2724. func GetDrugWarehousOutInfoOne(drug_id int64, org_id int64) (out []*models.XtDrugWarehouseOutInfo, err error) {
  2725. err = XTReadDB().Where("drug_id = ? and status=1 and org_id=? and is_sys=0", drug_id, org_id).Order("sys_record_time asc").Find(&out).Error
  2726. return out, err
  2727. }
  2728. func GetCheckDrugFlow(drug_id int64, user_org_id int64) (drugflow []*models.DrugFlow, err error) {
  2729. err = XTReadDB().Where("drug_id =? and status=1 and (consumable_type =10 or consumable_type =11) and user_org_id = ?", drug_id, user_org_id).Order("system_time asc").Find(&drugflow).Error
  2730. return drugflow, err
  2731. }
  2732. func GetCheckGoodBatchList(good_id int64, user_org_id int64) (project []*models.HisPrescriptionProject, err error) {
  2733. err = XTReadDB().Where("project_id = ? and user_org_id = ? and status =1 and type =3", good_id, user_org_id).Order("record_date asc").Find(&project).Error
  2734. return project, err
  2735. }
  2736. func GetCheckGoodWarehouseList(good_id int64, org_id int64) (info []*models.WarehouseOutInfo, err error) {
  2737. err = XTReadDB().Where("good_id = ? and org_id = ? and status=1 and is_sys=0", good_id, org_id).Order("sys_record_time").Find(&info).Error
  2738. return info, err
  2739. }
  2740. func GetHisAdviceListBySysRrecordTime(drug_id int64, sys_record_time int64, user_org_id int64) (info []*models.HisDoctorAdviceInfo, err error) {
  2741. err = XTReadDB().Where("drug_id=? and advice_date = ? and user_org_id = ? and status =1", drug_id, sys_record_time, user_org_id).Find(&info).Error
  2742. return info, err
  2743. }
  2744. func GetDrugWarehouseOutInfoBySysRecordTime(drug_id int64, sys_record_time int64, user_org_id int64) (info []*models.DrugWarehouseOutInfo, err error) {
  2745. err = XTReadDB().Where("drug_id=? and sys_record_time =? and org_id = ? and status =1", drug_id, sys_record_time, user_org_id).Find(&info).Error
  2746. return info, err
  2747. }
  2748. func GetGoodProjectList(good_id int64, sys_record_time int64, user_org_id int64) (project []*models.HisPrescriptionProject, err error) {
  2749. err = XTReadDB().Where("project_id = ? and status =1 and record_date = ? and user_org_id=? and type =3", good_id, sys_record_time, user_org_id).Find(&project).Error
  2750. return project, err
  2751. }
  2752. func GetWarehouseOutListBySysrecordTime(good_id int64, sys_record_time int64, user_org_id int64) (out []*models.WarehouseOutInfo, err error) {
  2753. err = XTReadDB().Where("good_id = ? and sys_record_time =? and org_id = ? and status=1 and is_sys=1", good_id, sys_record_time, user_org_id).Find(&out).Error
  2754. return out, err
  2755. }
  2756. func UpdateBaseDrugById(id int64, drug_str string) (models.XtBaseDrug, error) {
  2757. baseDrug := models.XtBaseDrug{}
  2758. err := XTWriteDB().Model(&baseDrug).Where("id = ? and status =1", id).Updates(map[string]interface{}{"drug_spec": drug_str}).Error
  2759. return baseDrug, err
  2760. }
  2761. func UpdateDosageFormListById(drug_dosage_form_name string, drug_id int64) (models.XtBaseDrug, error) {
  2762. drug := models.XtBaseDrug{}
  2763. err := XTWriteDB().Model(&drug).Where("id = ? and status =1", drug_id).Updates(map[string]interface{}{"drug_dosage_form_name": drug_dosage_form_name}).Error
  2764. return drug, err
  2765. }
  2766. func GetDosageFormListById(id int64, orgId int64) (str string) {
  2767. var drugDosageForm = "药品剂型"
  2768. drugDosageFormParent, _ := GetDrugDataConfig(0, drugDosageForm)
  2769. drugDosageFormList, _ := GetParentDataConfig(drugDosageFormParent.ID, orgId)
  2770. for _, item := range drugDosageFormList {
  2771. if id == int64(item.Value) {
  2772. str = item.Name
  2773. }
  2774. }
  2775. return str
  2776. }
  2777. func GetDrugAllInfo(org_id int64) (warehouseInfo []*models.DrugWarehouseInfo, err error) {
  2778. err = XTReadDB().Where("org_id = ? and status= 1 and ctime>=1640966400 and ctime<=1703952000", org_id).Find(&warehouseInfo).Error
  2779. return warehouseInfo, err
  2780. }
  2781. func GetDrugOutAllInfo(org_id int64) (warehouseInfo []*models.DrugWarehouseOutInfo, err error) {
  2782. err = XTReadDB().Where("org_id = ? and status= 1 and ctime>=1672502400 and ctime<=1704038399", org_id).Find(&warehouseInfo).Error
  2783. return warehouseInfo, err
  2784. }
  2785. func UpdateDealerManufacturer(id int64, dealer_name string, manufactruer string) (err error) {
  2786. warehouseInfo := models.DrugWarehouseInfo{}
  2787. err = XTWriteDB().Model(&warehouseInfo).Where("id = ? and status =1", id).Updates(map[string]interface{}{"manafacturer_name": manufactruer, "dealer_name": dealer_name}).Error
  2788. return err
  2789. }
  2790. func UpdateDealerManufacturerOne(id int64, manufactruer string) (err error) {
  2791. warehouseInfo := models.DrugWarehouseOutInfo{}
  2792. err = XTWriteDB().Model(&warehouseInfo).Where("id = ? and status =1", id).Updates(map[string]interface{}{"manafacturer_name": manufactruer}).Error
  2793. return err
  2794. }
  2795. func GetDrugWarehouseInfoByItemId(drug_id int64) (list []*models.XtDrugWarehouseInfo, err error) {
  2796. err = XTReadDB().Where("drug_id = ? and status=1 and is_check=1 and ctime<1704038399", drug_id).Find(&list).Error
  2797. return list, err
  2798. }
  2799. func GetDrugWarehosueOutInfoByItemId(drug_id int64) (list []*models.XtDrugWarehouseOutInfo, err error) {
  2800. err = XTReadDB().Where("drug_id = ? and status=1 and is_check=1 and ctime<1704038399", drug_id).Find(&list).Error
  2801. return list, err
  2802. }
  2803. func UpdateBaseDrugByIdCount(drug_id int64, flush_count int64) (models.XtBaseDrug, error) {
  2804. drug := models.XtBaseDrug{}
  2805. err := XTWriteDB().Model(&drug).Model(&drug).Where("id = ? and status=1", drug_id).Updates(map[string]interface{}{"total_count_one": flush_count}).Error
  2806. return drug, err
  2807. }
  2808. func UpdateBaseDrugByIdCountTwo(drug_id int64, flush_count int64) (models.XtBaseDrug, error) {
  2809. drug := models.XtBaseDrug{}
  2810. err := XTWriteDB().Model(&drug).Model(&drug).Where("id = ? and status=1", drug_id).Updates(map[string]interface{}{"total_count_two": flush_count}).Error
  2811. return drug, err
  2812. }
  2813. func GetAllWareHouseInforByUserOrg(org_id int64) (info []*models.WarehousingInfo, err error) {
  2814. err = XTReadDB().Where("org_id =? and status =1 and is_check=1 and ctime>=1672502400 and ctime<=1703952000", org_id).Find(&info).Error
  2815. return info, err
  2816. }
  2817. func GetAllWareHouseOutByUserOrg(org_id int64) (info []*models.WarehouseOutInfo, err error) {
  2818. err = XTReadDB().Where("org_id =? and status =1 and is_check=1 and ctime>=1672502400 and ctime<=1703952000", org_id).Find(&info).Error
  2819. return info, err
  2820. }
  2821. func UpdateGoodInfoByUserOrgId(id int64, dealer_name string, manufactruer string) (err error) {
  2822. warehousingInfo := models.WarehousingInfo{}
  2823. err = XTWriteDB().Model(&warehousingInfo).Where("id = ? and status =1", id).Updates(map[string]interface{}{"dealer_name": dealer_name, "manafacturer_name": manufactruer}).Error
  2824. return err
  2825. }
  2826. func UpdateGoodInfoByUserOrgIdTwo(id int64, dealer_name string) (err error) {
  2827. WarehouseOutInfo := models.WarehouseOutInfo{}
  2828. err = XTWriteDB().Model(&WarehouseOutInfo).Where("id = ? and status =1", id).Updates(map[string]interface{}{"dealer_name": dealer_name}).Error
  2829. return err
  2830. }
  2831. func GetAllWarehouseSumInCount(org_id int64) (infor []*models.WarehousingInfo, err error) {
  2832. err = XTReadDB().Where("id = ? and status =1 and is_check =1 and ctime<1704038399", org_id).Find(&infor).Error
  2833. return infor, err
  2834. }
  2835. func GetAllWarehouseSumOutCount(org_id int64) (infor []*models.WarehouseOutInfo, err error) {
  2836. err = XTReadDB().Where("id = ? and status =1 and is_check =1 and ctime<1704038399", org_id).Find(&infor).Error
  2837. return infor, err
  2838. }
  2839. func UpdateGoodInfoById(id int64, total_count_one int64) error {
  2840. err := XTWriteDB().Model(&models.GoodInfoSevenThirty{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"total_count_one": total_count_one}).Error
  2841. return err
  2842. }
  2843. func UpdateGoodInfoByIdOne(id int64, total_count_one int64) error {
  2844. err := XTWriteDB().Model(&models.GoodInfoSevenThirty{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"total_count_two": total_count_one}).Error
  2845. return err
  2846. }
  2847. func GetAllSelfDrugInfoList(org_id int64) (base []*models.BaseDrugLibThrity, err error) {
  2848. err = XTReadDB().Where("org_id = ? and status =1 and is_self_drug =1", org_id).Find(&base).Error
  2849. return base, err
  2850. }
  2851. func GetAllPatientListByUserOrg(org_id int64) (patient []*models.XtPatients, err error) {
  2852. err = XTReadDB().Where("user_org_id =? and status=1", org_id).Find(&patient).Error
  2853. return patient, err
  2854. }
  2855. func PostSearchSelfDrugList(keywords string, user_org_id int64) (drug []*models.BaseDrugLibThrity, err error) {
  2856. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and is_self_drug=1", user_org_id)
  2857. if len(keywords) > 0 {
  2858. likekey := "%" + keywords + "%"
  2859. err = db.Where("(drug_name LIKE ?)", likekey).Preload("LastDrugWarehouseInfo", "org_id=? and status = 1 and (stock_max_number > 0 or stock_min_number>0) and storehouse_id = ?", user_org_id).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  2860. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", user_org_id).Group("id")
  2861. }).Find(&drug).Error
  2862. } else {
  2863. err = db.Preload("LastDrugWarehouseInfo", "org_id=? and status = 1 and (stock_max_number > 0 or stock_min_number>0)", user_org_id).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  2864. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 ", user_org_id).Group("id")
  2865. }).Find(&drug).Error
  2866. }
  2867. return
  2868. }
  2869. func FindAllSelfDrugList(org_id int64, patient_id int64) (total int64, err error) {
  2870. err = readDb.Model(&models.XtSelfDrugWarehouseInfo{}).Where("org_id = ? and patient_id", org_id, patient_id).Count(&total).Error
  2871. return total, err
  2872. }
  2873. func CreatedSlefWarehouseing(info *models.XtSelfDrugWarehouseInfo, tx *gorm.DB) {
  2874. err := tx.Create(&info).Error
  2875. if err != nil {
  2876. tx.Rollback()
  2877. return
  2878. }
  2879. return
  2880. }
  2881. func UpdateSelfWarehouseing(info *models.XtSelfDrugWarehouseInfo, tx *gorm.DB) error {
  2882. err := tx.Save(&info).Error
  2883. if err != nil {
  2884. tx.Rollback()
  2885. return err
  2886. }
  2887. return err
  2888. }
  2889. func FindSelfDrugWarehouseInfoListById(id int64, org_id int64, patient_id int64, tx *gorm.DB) (info []*models.XtSelfDrugWarehouseInfo, err error) {
  2890. err = tx.Where("warehousing_id = ? and org_id = ? and patient_id = ? and status=1", id, org_id, patient_id).Preload("BaseDrugLib", "status=1").Find(&info).Error
  2891. if err != gorm.ErrRecordNotFound {
  2892. if err != nil {
  2893. tx.Rollback()
  2894. return info, err
  2895. }
  2896. }
  2897. return info, err
  2898. }
  2899. func GetAllBaseDrugListByKeyWord(keyword string, org_id int64) (base []*models.BaseDrugLibSeven, err error) {
  2900. likeKey := "%" + keyword + "%"
  2901. db := XTReadDB().Model(&base).Where("status=1 and is_self_drug = 1")
  2902. if len(keyword) > 0 {
  2903. db = db.Where("drug_name like ?", likeKey)
  2904. }
  2905. if org_id > 0 {
  2906. db = db.Where("org_id = ?", org_id)
  2907. }
  2908. err = db.Find(&base).Error
  2909. return base, err
  2910. }
  2911. func GetAllSelfDrugWarehouseInfoList(ids []int64, org_id int64, patient_id int64) (info []*models.XtSelfDrugWarehouseInfo, err error) {
  2912. db := XTReadDB().Model(&info).Where("status=1")
  2913. if len(ids) > 0 {
  2914. db = db.Where("drug_id in(?)", ids)
  2915. }
  2916. if org_id > 0 {
  2917. db = db.Where("org_id = ?", org_id)
  2918. }
  2919. if patient_id > 0 {
  2920. db = db.Where("patient_id = ?", patient_id)
  2921. }
  2922. err = db.Preload("BaseDrugLib", "status =1").Find(&info).Error
  2923. return info, err
  2924. }
  2925. func AddSelfDrugWarehouse(warehouse *models.XtSelfDrugWarehouse, tx *gorm.DB) error {
  2926. err := tx.Create(&warehouse).Error
  2927. if err != nil {
  2928. tx.Rollback()
  2929. return err
  2930. }
  2931. return err
  2932. }
  2933. func GetSelfDrugWarehouseByWaresingId(patient_id int64, record_time int64, org_id int64, tx *gorm.DB) (models.XtSelfDrugWarehouse, error) {
  2934. warehouse := models.XtSelfDrugWarehouse{}
  2935. err := tx.Where("patient_id = ? and warehousing_time = ? and org_id = ? and is_sys=1 and status=1", patient_id, record_time, org_id).Find(&warehouse).Error
  2936. if err != gorm.ErrRecordNotFound {
  2937. if err != nil {
  2938. tx.Rollback()
  2939. return warehouse, err
  2940. }
  2941. }
  2942. return warehouse, err
  2943. }
  2944. func UpdateSelfDrugWarehouse(warehouse *models.XtSelfDrugWarehouse, tx *gorm.DB) error {
  2945. err := tx.Save(&warehouse).Error
  2946. if err != nil {
  2947. tx.Rollback()
  2948. return err
  2949. }
  2950. return err
  2951. }
  2952. func FindSelfAllWarehouseTotal(org_id int64, tx *gorm.DB) (total int64, err2 error) {
  2953. err = tx.Model(&models.XtSelfDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  2954. if err != gorm.ErrRecordNotFound {
  2955. if err != nil {
  2956. tx.Rollback()
  2957. return total, err
  2958. }
  2959. }
  2960. return total, err
  2961. }
  2962. func FindLastSelfWarehousing(user_org_id int64, patient_id int64, tx *gorm.DB) (models.XtSelfDrugWarehouse, error) {
  2963. selfDrugWarehouse := models.XtSelfDrugWarehouse{}
  2964. err := tx.Where("org_id=? and patient_id =?", user_org_id, patient_id).Last(&selfDrugWarehouse).Error
  2965. if err != gorm.ErrRecordNotFound {
  2966. if err != nil {
  2967. tx.Rollback()
  2968. return selfDrugWarehouse, err
  2969. }
  2970. }
  2971. return selfDrugWarehouse, err
  2972. }
  2973. func GetSelfDrugWarehouseInfoByID(id int64) (models.XtSelfDrugWarehouseInfo, error) {
  2974. selfDrugWarehouseInfo := models.XtSelfDrugWarehouseInfo{}
  2975. err := XTReadDB().Where("warehousing_id = ? and status=1", id).Preload("BaseDrugLib", "status=1").Find(&selfDrugWarehouseInfo).Error
  2976. return selfDrugWarehouseInfo, err
  2977. }
  2978. func GetSelfDrugWarehouseInfoByIDOne(id int64) (selfDrugWarehouseInfo []*models.XtSelfDrugWarehouseInfo, err error) {
  2979. err = XTReadDB().Where("warehousing_id = ? and status=1", id).Preload("BaseDrugLib", "status=1").Find(&selfDrugWarehouseInfo).Error
  2980. return selfDrugWarehouseInfo, err
  2981. }
  2982. func GetSelfDrugWarehouseInfo(id int64) (models.XtSelfDrugWarehouse, error) {
  2983. warehouse := models.XtSelfDrugWarehouse{}
  2984. err := XTReadDB().Where("id = ? and status=1", id).Find(&warehouse).Error
  2985. return warehouse, err
  2986. }
  2987. func DeleteSelfDrugWarehouse(id int64, tx *gorm.DB) error {
  2988. info := models.XtSelfDrugWarehouseInfo{}
  2989. err := tx.Model(&info).Where("id = ? and status=1", id).Updates(map[string]interface{}{"status": 0}).Error
  2990. if err != nil {
  2991. tx.Rollback()
  2992. return err
  2993. }
  2994. return err
  2995. }
  2996. func GetSelfWarehouseInfoByWarehouseInfo(id int64, patient_id int64, org_id int64, tx *gorm.DB) (info []*models.XtSelfDrugWarehouseInfo, err error) {
  2997. err = tx.Where("warehousing_id =? and patient_id =? and status=1 and org_id =?", id, patient_id, org_id).Find(&info).Error
  2998. if err != gorm.ErrRecordNotFound {
  2999. if err != nil {
  3000. tx.Rollback()
  3001. return info, err
  3002. }
  3003. }
  3004. return info, err
  3005. }
  3006. func AddSelfDrugWarehouseStockMaxNumber(count int64, id int64, tx *gorm.DB) error {
  3007. err := tx.Model(&models.XtSelfDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  3008. if err != nil {
  3009. tx.Rollback()
  3010. return err
  3011. }
  3012. err = tx.Model(&models.XtSelfDrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  3013. if err != nil {
  3014. tx.Rollback()
  3015. return err
  3016. }
  3017. return err
  3018. }
  3019. func AddSelfDrugWarehouseStockMinNumber(count int64, id int64, tx *gorm.DB) error {
  3020. err := tx.Model(&models.XtSelfDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  3021. if err != nil {
  3022. tx.Rollback()
  3023. return err
  3024. }
  3025. err = tx.Model(&models.XtSelfDrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  3026. if err != nil {
  3027. tx.Rollback()
  3028. return err
  3029. }
  3030. return err
  3031. }
  3032. func GetDrugSumCountByPatient(patient_id int64, org_id int64, drug_id int64, tx *gorm.DB) (info []*models.XtSelfDrugWarehouseInfo, err error) {
  3033. err = tx.Where("patient_id = ? and org_id = ? and drug_id = ? and status =1", patient_id, org_id, drug_id).Find(&info).Error
  3034. if err != gorm.ErrRecordNotFound {
  3035. if err != nil {
  3036. tx.Rollback()
  3037. return info, err
  3038. }
  3039. }
  3040. return info, err
  3041. }
  3042. func CreateSelfDrugFlow(flow *models.XtSelfDrugFlow, tx *gorm.DB) error {
  3043. err := tx.Create(&flow).Error
  3044. if err != nil {
  3045. tx.Rollback()
  3046. return err
  3047. }
  3048. return err
  3049. }
  3050. func GetSelfDrugSumCountByStorehouseId(patient_id int64, orgid int64, drug_id int64, tx *gorm.DB) (info []*models.XtSelfDrugWarehouseInfo, err error) {
  3051. err = tx.Where("patient_id = ? and org_id = ? and status = 1 and drug_id =? and is_check = 1", patient_id, orgid, drug_id).Find(&info).Error
  3052. if err != gorm.ErrRecordNotFound {
  3053. if err != nil {
  3054. tx.Rollback()
  3055. return info, err
  3056. }
  3057. }
  3058. return info, err
  3059. }
  3060. func CreateSelfDrugStockSum(drug models.XtSelfDrugStockCount, tx *gorm.DB) error {
  3061. err := tx.Create(&drug).Error
  3062. if err != nil {
  3063. tx.Rollback()
  3064. return err
  3065. }
  3066. return err
  3067. }
  3068. func GetSelfDrugWarehouseOutListById(warehouse_out_id int64, orgId int64, tx *gorm.DB) (info []*models.XtSelfDrugWarehouseOutInfo, err error) {
  3069. err = tx.Where("warehouse_out_id = ? and org_id =? and status = 1", warehouse_out_id, orgId).Preload("BaseDrugLib", "status=1").Find(&info).Error
  3070. if err != gorm.ErrRecordNotFound {
  3071. if err != nil {
  3072. tx.Rollback()
  3073. return info, err
  3074. }
  3075. }
  3076. return info, err
  3077. }
  3078. func GetAllSelfDrugWarehouseOutList(ids []int64, patient_id int64, start_time int64, end_time int64, org_id int64) (outInfo []*models.XtSelfDrugWarehouseOutInfo, err error) {
  3079. db := XTReadDB().Model(&outInfo).Where("status =1")
  3080. if len(ids) > 0 {
  3081. db = db.Where("drug_id in(?)", ids)
  3082. }
  3083. if patient_id > 0 {
  3084. db = db.Where("patient_id = ?", patient_id)
  3085. }
  3086. if start_time > 0 {
  3087. db = db.Where("sys_record_time >=?", start_time)
  3088. }
  3089. if end_time > 0 {
  3090. db = db.Where("sys_record_time <=?", end_time)
  3091. }
  3092. if org_id > 0 {
  3093. db = db.Where("org_id =?", org_id)
  3094. }
  3095. err = db.Preload("BaseDrugLib", "status=1 and org_id = ?", org_id).Find(&outInfo).Error
  3096. return outInfo, err
  3097. }
  3098. func DeleteSelfWarehouseOut(id int64) (models.XtSelfDrugWarehouseOutInfo, error) {
  3099. outInfo := models.XtSelfDrugWarehouseOutInfo{}
  3100. err := XTWriteDB().Model(&outInfo).Where("id = ? and status=1", id).Updates(map[string]interface{}{"status": 0}).Error
  3101. return outInfo, err
  3102. }
  3103. func GetSelfDrugWarehouseOutById(id int64) (outInfo []*models.XtSelfDrugWarehouseOutInfo, err error) {
  3104. err = XTReadDB().Where("warehouse_out_id = ? and status =1", id).Preload("BaseDrugLib", "status=1").Find(&outInfo).Error
  3105. return outInfo, err
  3106. }
  3107. func GetSelfDrugWarehouseOutOne(id int64) (models.XtSelfDrugWarehouseOut, error) {
  3108. out := models.XtSelfDrugWarehouseOut{}
  3109. err = XTReadDB().Where("id = ? and status =1", id).Find(&out).Error
  3110. return out, err
  3111. }
  3112. func GetSelfDrugQuery(user_org_id int64, ids []int64) (drug []*models.XtSelfDrugStockCount, total int64, err error) {
  3113. db := XTReadDB().Model(&drug).Where("status=1")
  3114. if len(ids) > 0 {
  3115. db = db.Where("drug_id in(?)", ids)
  3116. }
  3117. if user_org_id > 0 {
  3118. db = db.Where("user_org_id = ?", user_org_id)
  3119. }
  3120. err = db.Preload("BaseDrugLib", "org_id = ? and status =1", user_org_id).Find(&drug).Error
  3121. return drug, total, err
  3122. }
  3123. func GetSelfDrugWarehouseOutListByIdOne(warehouse_out_id int64, orgId int64, tx *gorm.DB) (info []*models.XtSelfDrugWarehouseOutInfo, err error) {
  3124. err = tx.Where("warehouse_out_id = ? and org_id =? and status = 1", warehouse_out_id, orgId).Preload("BaseDrugLib", "status=1").Find(&info).Error
  3125. if err != gorm.ErrRecordNotFound {
  3126. if err != nil {
  3127. tx.Rollback()
  3128. return
  3129. }
  3130. }
  3131. return info, err
  3132. }
  3133. func GetSelfDrugWarehouseOutByLastId(id int64, tx *gorm.DB) (models.XtSelfDrugWarehouseOut, error) {
  3134. out := models.XtSelfDrugWarehouseOut{}
  3135. err := tx.Where("id = ? and status = 1", id).Find(&out).Error
  3136. if err != gorm.ErrRecordNotFound {
  3137. if err != nil {
  3138. tx.Rollback()
  3139. return out, err
  3140. }
  3141. }
  3142. return out, err
  3143. }
  3144. func GetSelfDrugAllStockInfo(patient_id int64, orgid int64, drugid int64, tx *gorm.DB) (info []*models.XtSelfDrugWarehouseInfo, err error) {
  3145. err = tx.Where("patient_id = ? and org_id = ? and status = 1 and drug_id = ? and is_check = 1", patient_id, orgid, drugid).Find(&info).Error
  3146. if err != gorm.ErrRecordNotFound {
  3147. if err != nil {
  3148. tx.Rollback()
  3149. return info, err
  3150. }
  3151. }
  3152. return info, err
  3153. }
  3154. func FindSelfBaseDrugLibRecord(org_id int64, id int64, tx *gorm.DB) (lib models.BaseDrugLib, err error) {
  3155. err = tx.Model(&models.BaseDrugLib{}).Where("id = ? AND org_id = ? AND status = 1", id, org_id).First(&lib).Error
  3156. if err != gorm.ErrRecordNotFound {
  3157. if err != nil {
  3158. tx.Rollback()
  3159. return lib, err
  3160. }
  3161. }
  3162. return lib, err
  3163. }
  3164. func GetSelfBaseDrugMedical(id int64, tx *gorm.DB) (models.XtBaseDrug, error) {
  3165. drug := models.XtBaseDrug{}
  3166. err := tx.Model(&drug).Where("id = ? and status = 1", id).Find(&drug).Error
  3167. if err != gorm.ErrRecordNotFound {
  3168. if err != nil {
  3169. tx.Rollback()
  3170. return drug, err
  3171. }
  3172. }
  3173. return drug, err
  3174. }
  3175. func AddSelfDrugCount(drug_id int64, org_id int64, patient_id int64, out_count int64, tx *gorm.DB) error {
  3176. err := tx.Model(&models.XtSelfDrugStockCount{}).Where("patient_id = ? and status = 1 and user_org_id = ? and drug_id = ?", patient_id, org_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count + ?", out_count)).Error
  3177. if err != nil {
  3178. tx.Rollback()
  3179. return err
  3180. }
  3181. err = tx.Model(&models.XtSelfDrugStockCount{}).Where("patient_id = ? and status = 1 and user_org_id = ? and drug_id = ?", patient_id, org_id, drug_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count + ?", out_count)).Error
  3182. if err != nil {
  3183. tx.Rollback()
  3184. return err
  3185. }
  3186. return err
  3187. }
  3188. func UpdateSelfCheckDrugOut(out models.XtSelfDrugWarehouseOut, id int64, tx *gorm.DB) error {
  3189. err := tx.Model(&models.XtSelfDrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": out.IsCheck}).Error
  3190. if err != nil {
  3191. tx.Rollback()
  3192. return err
  3193. }
  3194. err = tx.Model(&models.XtSelfDrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  3195. if err != nil {
  3196. tx.Rollback()
  3197. return err
  3198. }
  3199. return err
  3200. }
  3201. func FindSelfLastDrugWarehousingInfoByID(drug_id int64, patient_id int64, tx *gorm.DB) (info models.XtSelfDrugWarehouseInfo, err error) {
  3202. err = tx.Model(&models.XtSelfDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) and patient_id = ? and is_check = 1", drug_id, patient_id).Order("ctime").First(&info).Error
  3203. if err != gorm.ErrRecordNotFound {
  3204. if err != nil {
  3205. tx.Rollback()
  3206. return info, err
  3207. }
  3208. }
  3209. return info, err
  3210. }
  3211. func ChangeSelfMaxNumber(id int64, count int64, tx *gorm.DB) error {
  3212. err = tx.Model(&models.XtSelfDrugWarehouseInfo{}).Where("id = ? and status = 1 and is_check = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  3213. if err != nil {
  3214. tx.Rollback()
  3215. return err
  3216. }
  3217. return err
  3218. }
  3219. func UpdateSelfMinNumber(id int64, count int64, tx *gorm.DB) error {
  3220. err := tx.Model(&models.XtSelfDrugWarehouseInfo{}).Where("id = ? and status = 1 and is_check = 1", id).Update(map[string]interface{}{"stock_min_number": count}).Error
  3221. if err != nil {
  3222. tx.Rollback()
  3223. return err
  3224. }
  3225. return err
  3226. }
  3227. func ChangeSelfMaxNumberOne(id int64, count int64, tx *gorm.DB) error {
  3228. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1 and is_check = 1", id).Updates(map[string]interface{}{"stock_max_number": count, "stock_min_number": 0}).Error
  3229. if err != nil {
  3230. tx.Rollback()
  3231. return err
  3232. }
  3233. return err
  3234. }
  3235. func UpDateSelfDrugWarehouseInfoByStock(info *models.XtSelfDrugWarehouseInfo, tx *gorm.DB) (err error) {
  3236. err = tx.Save(&info).Error
  3237. if err != nil {
  3238. tx.Rollback()
  3239. return err
  3240. }
  3241. return err
  3242. }
  3243. func FindSelfDrugWarehouseInfoFlushCount(drug_id int64, orgid int64, tx *gorm.DB) (info []*models.XtSelfDrugWarehouseInfo, err error) {
  3244. err = tx.Where("drug_id = ? and org_id = ? and status = 1 and is_check = 1", drug_id, orgid).Find(&info).Error
  3245. if err != nil {
  3246. tx.Rollback()
  3247. return info, err
  3248. }
  3249. return info, err
  3250. }
  3251. func GetSelfLastWarehouseOutInfoById(drugid int64, tx *gorm.DB) (models.XtSelfDrugWarehouseOutInfo, error) {
  3252. flow := models.XtSelfDrugWarehouseOutInfo{}
  3253. err := tx.Where("drug_id = ? and status = 1", drugid).Last(&flow).Error
  3254. if err != gorm.ErrRecordNotFound {
  3255. if err != nil {
  3256. tx.Rollback()
  3257. return flow, err
  3258. }
  3259. }
  3260. return flow, err
  3261. }
  3262. func UpdateSelfDrugWarehouseOutInfo(id int64, warehouse_info_id int64, org_id int64, over_count int64, drug_price float64, tx *gorm.DB) error {
  3263. err = tx.Model(&models.XtSelfDrugWarehouseOutInfo{}).Where("id = ? and org_id = ? and status = 1", id, org_id).Update(map[string]interface{}{"warehouse_info_id": warehouse_info_id, "over_count": over_count, "price": drug_price, "retail_price": drug_price}).Error
  3264. if err != nil {
  3265. tx.Rollback()
  3266. return err
  3267. }
  3268. return err
  3269. }
  3270. func UpdateSelfCheckDrugOutInfoList(info models.XtSelfDrugWarehouseOutInfo, warehouse_out_id int64, tx *gorm.DB) error {
  3271. err := tx.Model(&models.XtSelfDrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", warehouse_out_id).Update(map[string]interface{}{"is_check": 2}).Error
  3272. if err != nil {
  3273. tx.Rollback()
  3274. return err
  3275. }
  3276. return err
  3277. }
  3278. func UpdateSelfDrugMaxNumber(count int64, id int64, tx *gorm.DB) error {
  3279. err := tx.Model(&models.XtSelfDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  3280. if err != nil {
  3281. tx.Rollback()
  3282. return err
  3283. }
  3284. return err
  3285. }
  3286. func UpdateSelfDrugMinNumber(count int64, id int64, tx *gorm.DB) error {
  3287. err := tx.Model(&models.XtSelfDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  3288. if err != nil {
  3289. tx.Rollback()
  3290. return err
  3291. }
  3292. return err
  3293. }
  3294. func ModifySelfReduceDrugInformation(id int64, count int64, orgid int64, tx *gorm.DB) error {
  3295. err = tx.Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", count)).Error
  3296. if err != nil {
  3297. tx.Rollback()
  3298. return err
  3299. }
  3300. return err
  3301. }
  3302. func DeleteSelfDrugOutFlow(id int64, orgid int64, tx *gorm.DB) error {
  3303. err := tx.Model(models.XtSelfDrugFlow{}).Where("warehouse_out_detail_id = ? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"status": 0}).Error
  3304. if err != nil {
  3305. tx.Rollback()
  3306. return err
  3307. }
  3308. return err
  3309. }
  3310. func ReduceSelfDrugCount(drug_id int64, org_id int64, patient_id int64, out_count int64, tx *gorm.DB) error {
  3311. err := tx.Model(&models.XtSelfDrugStockCount{}).Where("patient_id = ? and status = 1 and user_org_id = ? and drug_id = ?", patient_id, org_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", out_count)).Error
  3312. if err != nil {
  3313. tx.Rollback()
  3314. return err
  3315. }
  3316. err = tx.Model(&models.XtSelfDrugStockCount{}).Where("patient_id = ? and status = 1 and user_org_id = ? and drug_id = ?", patient_id, org_id, drug_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count - ?", out_count)).Error
  3317. if err != nil {
  3318. tx.Rollback()
  3319. return err
  3320. }
  3321. return err
  3322. }
  3323. func UpdateSelfDrugStockCount(drug_id int64, org_id int64, patient_id int64, flush_count int64, tx *gorm.DB) error {
  3324. drug := models.XtSelfDrugStockCount{}
  3325. err := tx.Model(&drug).Where("drug_id = ? and user_org_id = ? and patient_id = ? and status = 1", drug_id, org_id, patient_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  3326. if err != nil {
  3327. tx.Rollback()
  3328. return err
  3329. }
  3330. return err
  3331. }
  3332. func FindSelfDrugWarehouseInfoFlushCountByStorehouse(drug_id int64, orgid int64, patient_id int64, tx *gorm.DB) (info []*models.XtSelfDrugWarehouseInfo, err error) {
  3333. err = tx.Where("drug_id = ? and org_id = ? and status = 1 and is_check = 1 and patient_id =?", drug_id, orgid, patient_id).Find(&info).Error
  3334. if err != nil {
  3335. tx.Rollback()
  3336. return info, err
  3337. }
  3338. return info, err
  3339. }
  3340. func GetSelfWarehousingInfoByList(id int64, orgid int64, tx *gorm.DB) (info []*models.XtSelfDrugWarehouseInfo, err error) {
  3341. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  3342. if err != nil {
  3343. tx.Rollback()
  3344. return info, err
  3345. }
  3346. return info, err
  3347. }
  3348. func GetSelfDrugWarehouseOutByWarehouseInfoId(warehouse_info_id int64, drug_id int64, orgid int64, tx *gorm.DB) (outInfo []*models.XtSelfDrugWarehouseOutInfo, err error) {
  3349. err = tx.Where("warehouse_info_id = ? and drug_id = ? and org_id = ? and is_check = 1 and status = 1", warehouse_info_id, drug_id, orgid).Find(&outInfo).Error
  3350. if err != gorm.ErrRecordNotFound {
  3351. if err != nil {
  3352. tx.Rollback()
  3353. return outInfo, err
  3354. }
  3355. }
  3356. return outInfo, err
  3357. }
  3358. func UpdateSelfWarehousingInfoFlow(id int64, orgid int64, tx *gorm.DB) error {
  3359. err := tx.Model(&models.XtSelfDrugFlow{}).Where("warehousing_id = ? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"status": 0}).Error
  3360. if err != nil {
  3361. tx.Rollback()
  3362. return err
  3363. }
  3364. return err
  3365. }
  3366. func AddSelfDrugReturnWarehouseStockMaxNumber(count int64, id int64, tx *gorm.DB) error {
  3367. err := tx.Model(&models.XtSelfDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  3368. if err != nil {
  3369. tx.Rollback()
  3370. return err
  3371. }
  3372. err = tx.Model(&models.XtSelfDrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  3373. if err != nil {
  3374. tx.Rollback()
  3375. return err
  3376. }
  3377. return err
  3378. }
  3379. func AddSelfDrugReturnWarehouseStockMinNumber(count int64, id int64, tx *gorm.DB) error {
  3380. err := tx.Model(&models.XtSelfDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  3381. if err != nil {
  3382. tx.Rollback()
  3383. return err
  3384. }
  3385. err = tx.Model(&models.XtSelfDrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  3386. if err != nil {
  3387. tx.Rollback()
  3388. return err
  3389. }
  3390. return err
  3391. }
  3392. func ReduceSelfDrugStockCount(patient_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64, tx *gorm.DB) error {
  3393. err = tx.Model(&models.XtSelfDrugStockCount{}).Where("user_org_id = ? and patient_id = ? and drug_id = ? and status = 1", user_org_id, patient_id, drug_id).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count - ?", waresing_count)).Error
  3394. if err != nil {
  3395. tx.Rollback()
  3396. return err
  3397. }
  3398. err = tx.Model(&models.XtSelfDrugStockCount{}).Where("user_org_id = ? and patient_id = ? and drug_id = ? and status = 1", user_org_id, patient_id, drug_id).Update(map[string]interface{}{"flush_count": over_count}).Error
  3399. if err != nil {
  3400. tx.Rollback()
  3401. return err
  3402. }
  3403. return err
  3404. }
  3405. func ReturnSelfCheckWarehouseingInfo(id int64, orgid int64, tx *gorm.DB) error {
  3406. err := tx.Model(&models.XtSelfDrugWarehouse{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  3407. if err != nil {
  3408. tx.Rollback()
  3409. return err
  3410. }
  3411. err = tx.Model(&models.XtSelfDrugWarehouseInfo{}).Where("warehousing_id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  3412. if err != nil {
  3413. tx.Rollback()
  3414. return err
  3415. }
  3416. return err
  3417. }
  3418. func CheckSelfWarehousingInfo(id int64, orgid int64, checker int64, tx *gorm.DB) error {
  3419. err := tx.Model(&models.XtSelfDrugWarehouse{}).Where("id = ? and org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  3420. if err != nil {
  3421. tx.Rollback()
  3422. return err
  3423. }
  3424. return err
  3425. }
  3426. func GetSelfDrugFlow(drug_id int64, start_time int64, end_time int64, user_org_id int64, page int64, limit int64, patient_id int64) (flow []*models.XtSelfDrugFlow, total int64, err error) {
  3427. db := XTReadDB().Model(&flow).Where("status=1")
  3428. if drug_id > 0 {
  3429. db = db.Where("drug_id =?", drug_id)
  3430. }
  3431. if patient_id > 0 {
  3432. db = db.Where("patient_id = ?", patient_id)
  3433. }
  3434. if start_time > 0 {
  3435. db = db.Where("ctime>=?", start_time)
  3436. }
  3437. if end_time > 0 {
  3438. db = db.Where("etime <=?", end_time)
  3439. }
  3440. if user_org_id > 0 {
  3441. db = db.Where("user_org_id =?", user_org_id)
  3442. }
  3443. offset := (page - 1) * limit
  3444. err = db.Count(&total).Offset(offset).Limit(limit).Preload("BaseDrugLib", "status=1 and org_id = ?", user_org_id).Order("id desc").Find(&flow).Error
  3445. return flow, total, err
  3446. }
  3447. func GetSelfBasedrugByIdOne(drug_id int64, user_org_id int64) (models.BaseDrugLib, error) {
  3448. lib := models.BaseDrugLib{}
  3449. err := XTReadDB().Where("id = ? and org_id =? and status=1", drug_id, user_org_id).Find(&lib).Error
  3450. return lib, err
  3451. }
  3452. func GetSelfDrugStockCountByPatientId(drug_id int64, patient_id int64, user_org_id int64) (models.XtSelfDrugStockCount, error) {
  3453. selfDrugStockCount := models.XtSelfDrugStockCount{}
  3454. err := XTReadDB().Where("drug_id = ? and patient_id = ? and user_org_id = ? and status =1", drug_id, patient_id, user_org_id).Find(&selfDrugStockCount).Error
  3455. return selfDrugStockCount, err
  3456. }
  3457. func GetSelfBasedrugById(drug_id int64, user_org_id int64, tx *gorm.DB) (models.BaseDrugLib, error) {
  3458. lib := models.BaseDrugLib{}
  3459. err := tx.Where("id = ? and org_id =? and status=1", drug_id, user_org_id).Find(&lib).Error
  3460. if err != gorm.ErrRecordNotFound {
  3461. if err != nil {
  3462. tx.Rollback()
  3463. return lib, err
  3464. }
  3465. }
  3466. return lib, err
  3467. }
  3468. func GetSelfDrugBatchNumberList(drug_id int64, start_time int64, end_time int64, user_org_id int64, page int64, limit int64, patient_id int64) (flow []*models.XtSelfDrugFlow, total int64, err error) {
  3469. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type =1")
  3470. if drug_id > 0 {
  3471. db = db.Where("drug_id =?", drug_id)
  3472. }
  3473. if start_time > 0 {
  3474. db = db.Where("ctime>=?", start_time)
  3475. }
  3476. if end_time > 0 {
  3477. db = db.Where("etime <=?", end_time)
  3478. }
  3479. if user_org_id > 0 {
  3480. db = db.Where("user_org_id =?", user_org_id)
  3481. }
  3482. if patient_id > 0 {
  3483. db = db.Where("patient_id = ?", patient_id)
  3484. }
  3485. offset := (page - 1) * limit
  3486. err = db.Count(&total).Offset(offset).Limit(limit).Find(&flow).Error
  3487. return flow, total, err
  3488. }
  3489. func GetAllSelfDrugWarehouse(keyword string, start_time int64, end_time int64, check_type int64, orgId int64, page int64, limit int64, patient_id int64) (warehouse []*models.XtSelfDrugWarehouse, total int64, err error) {
  3490. db := XTReadDB().Model(&warehouse).Where("status=1")
  3491. likeKey := "%" + keyword + "%"
  3492. if len(keyword) > 0 {
  3493. db = db.Where("warehousing_order like ?", likeKey)
  3494. }
  3495. if start_time > 0 {
  3496. db = db.Where("operation_time>=?", start_time)
  3497. }
  3498. if end_time > 0 {
  3499. db = db.Where("operation_time<=?", end_time)
  3500. }
  3501. if check_type > 0 {
  3502. db = db.Where("is_check = ?", check_type)
  3503. }
  3504. if orgId > 0 {
  3505. db = db.Where("org_id = ?", orgId)
  3506. }
  3507. if patient_id > 0 {
  3508. db = db.Where("patient_id=?", patient_id)
  3509. }
  3510. offset := (page - 1) * limit
  3511. err = db.Count(&total).Offset(offset).Limit(limit).Order("operation_time desc").Find(&warehouse).Error
  3512. return warehouse, total, err
  3513. }
  3514. func DeleteSelfWarehouse(id int64) error {
  3515. err := XTWriteDB().Model(&models.XtSelfDrugWarehouse{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"status": 0}).Error
  3516. err = XTWriteDB().Model(&models.XtSelfDrugWarehouseInfo{}).Where("warehousing_id = ? and status=1", id).Updates(map[string]interface{}{"status": 0}).Error
  3517. return err
  3518. }
  3519. func GetAllSelfDrugWarehouseOut(start_time int64, end_time int64, org_id int64, page int64, limit int64, patient_id int64, keyword string, check_type int64) (out []*models.XtSelfDrugWarehouseOut, total int64, err error) {
  3520. db := XTReadDB().Model(&out).Where("status=1")
  3521. offset := (page - 1) * limit
  3522. likeKey := "%" + keyword + "%"
  3523. if len(keyword) > 0 {
  3524. db = db.Where("warehouse_out_order_number like ?", likeKey)
  3525. }
  3526. if start_time > 0 {
  3527. db = db.Where("operation_time>=?", start_time)
  3528. }
  3529. if end_time > 0 {
  3530. db = db.Where("operation_time<=?", end_time)
  3531. }
  3532. if org_id > 0 {
  3533. db = db.Where("org_id = ?", org_id)
  3534. }
  3535. if patient_id > 0 {
  3536. db = db.Where("patient_id = ?", patient_id)
  3537. }
  3538. if check_type > 0 {
  3539. db = db.Where("is_check=?", check_type)
  3540. }
  3541. err = db.Count(&total).Offset(offset).Limit(limit).Order("operation_time desc").Find(&out).Error
  3542. return out, total, err
  3543. }
  3544. func DeleteSelfWarehouseOutList(id int64) (models.XtSelfDrugWarehouseOut, error) {
  3545. warehouseOut := models.XtSelfDrugWarehouseOut{}
  3546. err := XTWriteDB().Model(&warehouseOut).Where("id = ? and status=1", id).Updates(map[string]interface{}{"status": 0}).Error
  3547. err = XTWriteDB().Model(&models.XtSelfDrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status=1", id).Updates(map[string]interface{}{"status": 0}).Error
  3548. return warehouseOut, err
  3549. }
  3550. func FindSelfDrugWarehouseInfoList(drugid int64, orgid int64, tx *gorm.DB) (info []*models.XtSelfDrugWarehouseInfo, err error) {
  3551. err = tx.Where("drug_id = ? and org_id = ? and status =1 and is_check =1 and (stock_max_number>0 or stock_min_number>0)", drugid, orgid).Find(&info).Error
  3552. if err != gorm.ErrRecordNotFound {
  3553. if err != nil {
  3554. tx.Rollback()
  3555. return info, err
  3556. }
  3557. }
  3558. return info, err
  3559. }
  3560. func GetSelfDrugWarehouseInfoByInfo(advice_id int64, patient_id int64, advice_date int64, user_org_id int64, drug_id int64, tx *gorm.DB) (info []*models.XtSelfDrugWarehouseInfo, err error) {
  3561. err = tx.Where("advice_id = ? and patient_id = ? and record_date = ? and org_id = ? and status=1 and drug_id = ?", advice_id, patient_id, advice_date, user_org_id, drug_id).Find(&info).Error
  3562. return info, err
  3563. }
  3564. func CreateSelfDrugWarehouseInfo(info *models.XtSelfDrugWarehouseInfo, tx *gorm.DB) error {
  3565. err := tx.Create(&info).Error
  3566. if err != nil {
  3567. tx.Rollback()
  3568. return err
  3569. }
  3570. return err
  3571. }
  3572. func GetLastSelfDrugWarehouseInfoById(org_id int64, tx *gorm.DB) (models.XtSelfDrugWarehouseInfo, error) {
  3573. info := models.XtSelfDrugWarehouseInfo{}
  3574. err := tx.Where("org_id = ? and status =1", org_id).Last(&info).Error
  3575. return info, err
  3576. }
  3577. func GetSelfDrugWarehouseOutDrugFlow(patient_id int64, drug_id int64, user_org_id int64, advice_id int64, tx *gorm.DB) (flow []*models.XtSelfDrugFlow, err error) {
  3578. err = tx.Where("patient_id = ? and drug_id = ? and user_org_id =? and status=1 and consumable_type =20 and advice_id =?", patient_id, drug_id, user_org_id, advice_id).Find(&flow).Error
  3579. if err != gorm.ErrRecordNotFound {
  3580. if err != nil {
  3581. tx.Rollback()
  3582. return
  3583. }
  3584. }
  3585. return flow, err
  3586. }
  3587. func GetSelfDrugLastWarehouseOutTwety(orgid int64, record_date int64, tx *gorm.DB, is_sys int64) (models.XtSelfDrugWarehouseOut, error) {
  3588. out := models.XtSelfDrugWarehouseOut{}
  3589. err := tx.Where("org_id = ? and status = 1 and warehouse_out_time = ? and is_sys =?", orgid, record_date, is_sys).Find(&out).Error
  3590. if err != gorm.ErrRecordNotFound {
  3591. if err != nil {
  3592. tx.Rollback()
  3593. return out, err
  3594. }
  3595. }
  3596. return out, err
  3597. }
  3598. func GetSelfHisPrescriptionAdviceByID(id int64, tx *gorm.DB) (advices []*models.HisDoctorAdviceInfo, err error) {
  3599. err = tx.Model(&models.HisDoctorAdviceInfo{}).Where("prescription_id = ? AND status = 1", id).Find(&advices).Error
  3600. if err != gorm.ErrRecordNotFound {
  3601. if err != nil {
  3602. tx.Rollback()
  3603. return advices, err
  3604. }
  3605. }
  3606. return
  3607. }
  3608. func GetSelfNewDrugWarehouseOutInfoSix(drugid int64, patient_id int64, record_date int64, org_id int64, advice_id int64, warehouse_info_id int64, tx *gorm.DB) (models.DrugWarehouseOutInfo, error) {
  3609. info := models.DrugWarehouseOutInfo{}
  3610. var err error
  3611. err = tx.Where("drug_id = ? and patient_id = ? and sys_record_time = ? and org_id = ? and status = 1 and advice_id = ? and warehouse_info_id = ?", drugid, patient_id, record_date, org_id, advice_id, warehouse_info_id).Find(&info).Error
  3612. if err != gorm.ErrRecordNotFound {
  3613. if err != nil {
  3614. tx.Rollback()
  3615. return info, err
  3616. }
  3617. }
  3618. return info, err
  3619. }
  3620. func UpdateSelfDrugWarehouseOutInfoTen(patient_id int64, sys_record_time int64, advice_id int64, tx *gorm.DB) error {
  3621. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("patient_id = ? and sys_record_time = ? and status=1 and advice_id = ?", patient_id, sys_record_time, advice_id).Updates(map[string]interface{}{"status": 0}).Error
  3622. if err != nil {
  3623. tx.Rollback()
  3624. return err
  3625. }
  3626. return err
  3627. }
  3628. func GetSelfWarehouseInfoByWarehouseInfoTwo(drug_id int64, patient_id int64, org_id int64, tx *gorm.DB) (info []*models.XtSelfDrugWarehouseInfo, err error) {
  3629. err = tx.Where("drug_id =? and patient_id =? and status=1 and org_id =? and is_check=1", drug_id, patient_id, org_id).Find(&info).Error
  3630. if err != gorm.ErrRecordNotFound {
  3631. if err != nil {
  3632. tx.Rollback()
  3633. return info, err
  3634. }
  3635. }
  3636. return info, err
  3637. }
  3638. func GetSelfDrugWarehouseOutDetailByPatientId(patient_id int64, page int64, limit int64, user_org_id int64) (out []*models.XtSelfDrugStockCount, total int64, err error) {
  3639. db := XTReadDB().Model(&out).Where("status=1")
  3640. if patient_id > 0 {
  3641. db = db.Where("patient_id = ?", patient_id)
  3642. }
  3643. if user_org_id > 0 {
  3644. db = db.Where("user_org_id = ?", user_org_id)
  3645. }
  3646. offset := (page - 1) * limit
  3647. err = db.Count(&total).Offset(offset).Limit(limit).Preload("BaseDrugLib", "status=1 and org_id = ?", user_org_id).Find(&out).Error
  3648. return out, total, err
  3649. }
  3650. func GetSelfBasedrugByIdTwo(drug_id int64, user_org_id int64) (models.BaseDrugLib, error) {
  3651. lib := models.BaseDrugLib{}
  3652. err := XTReadDB().Where("id = ? and org_id =? and status=1", drug_id, user_org_id).Find(&lib).Error
  3653. return lib, err
  3654. }
  3655. //自动退库分装方法
  3656. func SelfDrugAutoAddCancelInfo(advice *models.HisDoctorAdviceInfo, creater int64) error {
  3657. //查询该批次病人该药品出库数据
  3658. info, _ := GetSelfDrugAutoWarehouseOutEight(advice.PatientId, advice.AdviceDate, advice.DrugId, advice.ID)
  3659. list, _ := GetSelfDrugAutoWarehouseOutNight(advice.PatientId, advice.AdviceDate, advice.DrugId, advice.ID)
  3660. //退库
  3661. drug, _ := FindBaseDrugLibRecord(advice.UserOrgId, advice.DrugId)
  3662. //如果剩余数量 + 退库数量 < 总入库数量
  3663. if len(list) > 0 {
  3664. for _, its := range list {
  3665. //查询该批次病人该药品出库数据
  3666. infos, _ := GetSelfDrugAutoWarehouseOutTen(advice.PatientId, advice.AdviceDate, advice.DrugId, advice.ID, its.WarehouseInfoId)
  3667. //查询该药品出库批次总入库数量
  3668. druginfo, _ := GetSelfDrugInfoSix(its.WarehouseInfoId, infos.OrgId)
  3669. var out_count int64 //该批次出库库存
  3670. var sum_count int64 //该批次总库存
  3671. var over_count int64 //剩余数量
  3672. if druginfo.MaxUnit != druginfo.MinUnit {
  3673. over_count = druginfo.StockMaxNumber*drug.MinNumber + druginfo.StockMinNumber
  3674. }
  3675. if druginfo.MaxUnit == druginfo.MinUnit {
  3676. over_count = druginfo.StockMaxNumber + druginfo.StockMinNumber
  3677. }
  3678. if infos.CountUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
  3679. out_count = infos.Count * drug.MinNumber
  3680. }
  3681. if infos.CountUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
  3682. out_count = infos.Count
  3683. }
  3684. if infos.CountUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
  3685. out_count = infos.Count
  3686. }
  3687. if druginfo.MaxUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
  3688. sum_count = druginfo.WarehousingCount * drug.MinNumber
  3689. }
  3690. if druginfo.MaxUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
  3691. sum_count = druginfo.WarehousingCount
  3692. }
  3693. if druginfo.MaxUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
  3694. sum_count = druginfo.WarehousingCount
  3695. }
  3696. if (over_count + out_count) <= sum_count {
  3697. if its.CountUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
  3698. //退库
  3699. warehouseInfo := models.XtSelfDrugWarehouseInfo{
  3700. StockMinNumber: its.Count,
  3701. }
  3702. UpdateSelfDrugWarehouseOne(its.WarehouseInfoId, warehouseInfo)
  3703. //增加退库数量
  3704. AddSelfCancelSumCountOne(advice.PatientId, advice.DrugId, advice.UserOrgId, its.Count)
  3705. //出库数量减少
  3706. UpdateSelfSumOutDrug(advice.UserOrgId, advice.PatientId, advice.DrugId, its.Count)
  3707. }
  3708. if its.CountUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
  3709. warehouseInfo := models.XtSelfDrugWarehouseInfo{
  3710. StockMaxNumber: its.Count,
  3711. }
  3712. UpdateSelfDrugWarehouseTwo(its.WarehouseInfoId, warehouseInfo)
  3713. //增加退库数量
  3714. AddSelfCancelSumCountOne(advice.PatientId, advice.DrugId, advice.UserOrgId, its.Count*drug.MinNumber)
  3715. //出库数量减少
  3716. UpdateSelfSumOutDrug(advice.UserOrgId, advice.PatientId, advice.DrugId, its.Count*drug.MinNumber)
  3717. }
  3718. if its.CountUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
  3719. warehouseInfo := models.XtSelfDrugWarehouseInfo{
  3720. StockMaxNumber: its.Count,
  3721. }
  3722. UpdateSelfDrugWarehouseTwo(its.WarehouseInfoId, warehouseInfo)
  3723. //增加退库数量
  3724. AddSelfCancelSumCountOne(advice.PatientId, advice.DrugId, advice.UserOrgId, its.Count*drug.MinNumber)
  3725. //出库数量减少
  3726. UpdateSelfSumOutDrug(advice.UserOrgId, advice.PatientId, advice.DrugId, its.Count*drug.MinNumber)
  3727. }
  3728. }
  3729. }
  3730. floatPrescrip := strconv.FormatFloat(advice.PrescribingNumber, 'f', -1, 32)
  3731. prescribingNumber, _ := strconv.ParseInt(floatPrescrip, 10, 64)
  3732. if advice.PrescribingNumberUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
  3733. //查询剩余库存
  3734. stockInfo, _ := GetSelfDrugAllStockInfoOne(advice.PatientId, advice.UserOrgId, advice.DrugId)
  3735. var sum_count int64
  3736. for _, its := range stockInfo {
  3737. if its.MaxUnit == drug.MaxUnit {
  3738. its.StockMaxNumber = its.StockMaxNumber * drug.MinNumber
  3739. }
  3740. sum_count += its.StockMaxNumber + its.StockMinNumber
  3741. }
  3742. var count_one int64
  3743. if advice.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
  3744. count_one = prescribingNumber * drug.MinNumber
  3745. }
  3746. if advice.PrescribingNumberUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
  3747. count_one = prescribingNumber
  3748. }
  3749. if advice.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
  3750. count_one = prescribingNumber * drug.MinNumber
  3751. }
  3752. flow := models.XtSelfDrugFlow{
  3753. WarehousingId: info.WarehouseInfoId,
  3754. DrugId: info.DrugId,
  3755. Number: "",
  3756. BatchNumber: info.BatchNumber,
  3757. Count: count_one,
  3758. UserOrgId: advice.UserOrgId,
  3759. PatientId: advice.PatientId,
  3760. SystemTime: info.SysRecordTime,
  3761. ConsumableType: 7,
  3762. IsSys: 0,
  3763. WarehousingOrder: "",
  3764. WarehouseOutId: 0,
  3765. WarehouseOutOrderNumber: info.WarehouseOutOrderNumber,
  3766. IsEdit: 0,
  3767. CancelStockId: 0,
  3768. CancelOrderNumber: "",
  3769. Manufacturer: info.Manufacturer,
  3770. Dealer: info.Dealer,
  3771. Creator: creater,
  3772. UpdateCreator: 0,
  3773. Status: 1,
  3774. Ctime: info.SysRecordTime,
  3775. Mtime: 0,
  3776. Price: info.Price,
  3777. WarehousingDetailId: info.WarehouseInfoId,
  3778. WarehouseOutDetailId: 0,
  3779. CancelOutDetailId: 0,
  3780. ExpireDate: info.ExpiryDate,
  3781. ProductDate: info.ProductDate,
  3782. MaxUnit: drug.MinUnit,
  3783. MinUnit: "",
  3784. AdviceId: info.AdviceId,
  3785. StorehouseId: 0,
  3786. OverCount: sum_count,
  3787. OperateTime: time.Now().Unix(),
  3788. }
  3789. CreateSelfDrugFlowTwo(flow)
  3790. //删除出库记录
  3791. UpdateSelfDrugFlowSeven(info.PatientId, info.SysRecordTime, info.DrugId, advice.ID, info.ID)
  3792. //删除流水
  3793. UpdateDrugFlowTensOne(info.PatientId, info.WarehouseOutOrderNumber, info.DrugId, advice.ID)
  3794. }
  3795. if advice.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
  3796. //查询剩余库存
  3797. stockInfo, _ := GetSelfDrugAllStockInfoOne(advice.PatientId, advice.UserOrgId, advice.DrugId)
  3798. var sum_count int64
  3799. for _, its := range stockInfo {
  3800. if its.MaxUnit == drug.MaxUnit {
  3801. its.StockMaxNumber = its.StockMaxNumber * drug.MinNumber
  3802. }
  3803. sum_count += its.StockMaxNumber + its.StockMinNumber
  3804. }
  3805. var count_one int64
  3806. if advice.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
  3807. count_one = prescribingNumber * drug.MinNumber
  3808. }
  3809. if advice.PrescribingNumberUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
  3810. count_one = prescribingNumber
  3811. }
  3812. if advice.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
  3813. count_one = prescribingNumber * drug.MinNumber
  3814. }
  3815. flow := models.XtSelfDrugFlow{
  3816. WarehousingId: info.WarehouseInfoId,
  3817. DrugId: info.DrugId,
  3818. Number: "",
  3819. BatchNumber: info.BatchNumber,
  3820. Count: count_one,
  3821. UserOrgId: advice.UserOrgId,
  3822. PatientId: advice.PatientId,
  3823. SystemTime: info.SysRecordTime,
  3824. ConsumableType: 7,
  3825. IsSys: 0,
  3826. WarehousingOrder: "",
  3827. WarehouseOutId: 0,
  3828. WarehouseOutOrderNumber: info.WarehouseOutOrderNumber,
  3829. IsEdit: 0,
  3830. CancelStockId: 0,
  3831. CancelOrderNumber: "",
  3832. Manufacturer: info.Manufacturer,
  3833. Dealer: info.Dealer,
  3834. Creator: creater,
  3835. UpdateCreator: 0,
  3836. Status: 1,
  3837. Ctime: info.SysRecordTime,
  3838. Mtime: 0,
  3839. Price: info.Price,
  3840. WarehousingDetailId: info.WarehouseInfoId,
  3841. WarehouseOutDetailId: 0,
  3842. CancelOutDetailId: 0,
  3843. ExpireDate: info.ExpiryDate,
  3844. ProductDate: info.ProductDate,
  3845. MaxUnit: drug.MinUnit,
  3846. MinUnit: "",
  3847. AdviceId: info.AdviceId,
  3848. StorehouseId: 0,
  3849. OverCount: sum_count,
  3850. OperateTime: time.Now().Unix(),
  3851. }
  3852. CreateSelfDrugFlowTwo(flow)
  3853. //删除出库记录
  3854. UpdateSelfDrugFlowSeven(info.PatientId, info.SysRecordTime, info.DrugId, advice.ID, info.ID)
  3855. //删除流水
  3856. UpdateDrugFlowTensOne(info.PatientId, info.WarehouseOutOrderNumber, info.DrugId, advice.ID)
  3857. }
  3858. if advice.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
  3859. //查询剩余库存
  3860. stockInfo, _ := GetSelfDrugAllStockInfoOne(advice.PatientId, advice.UserOrgId, advice.DrugId)
  3861. var sum_count int64
  3862. for _, its := range stockInfo {
  3863. if its.MaxUnit == drug.MaxUnit {
  3864. its.StockMaxNumber = its.StockMaxNumber * drug.MinNumber
  3865. }
  3866. sum_count += its.StockMaxNumber + its.StockMinNumber
  3867. }
  3868. flow := models.XtSelfDrugFlow{
  3869. WarehousingId: info.WarehouseInfoId,
  3870. DrugId: info.DrugId,
  3871. Number: "",
  3872. BatchNumber: info.BatchNumber,
  3873. Count: prescribingNumber,
  3874. UserOrgId: advice.UserOrgId,
  3875. PatientId: advice.PatientId,
  3876. SystemTime: info.SysRecordTime,
  3877. ConsumableType: 7,
  3878. IsSys: 0,
  3879. WarehousingOrder: "",
  3880. WarehouseOutId: 0,
  3881. WarehouseOutOrderNumber: info.WarehouseOutOrderNumber,
  3882. IsEdit: 0,
  3883. CancelStockId: 0,
  3884. CancelOrderNumber: "",
  3885. Manufacturer: info.Manufacturer,
  3886. Dealer: info.Dealer,
  3887. Creator: creater,
  3888. UpdateCreator: 0,
  3889. Status: 1,
  3890. Ctime: info.SysRecordTime,
  3891. Mtime: 0,
  3892. Price: info.Price,
  3893. WarehousingDetailId: info.WarehouseInfoId,
  3894. WarehouseOutDetailId: 0,
  3895. CancelOutDetailId: 0,
  3896. ExpireDate: info.ExpiryDate,
  3897. ProductDate: info.ProductDate,
  3898. MaxUnit: advice.PrescribingNumberUnit,
  3899. MinUnit: "",
  3900. AdviceId: info.AdviceId,
  3901. StorehouseId: 0,
  3902. OverCount: sum_count,
  3903. OperateTime: time.Now().Unix(),
  3904. }
  3905. CreateSelfDrugFlowTwo(flow)
  3906. //删除出库记录
  3907. UpdateSelfDrugFlowSeven(info.PatientId, info.SysRecordTime, info.DrugId, advice.ID, info.ID)
  3908. //删除流水
  3909. UpdateDrugFlowTensOne(info.PatientId, info.WarehouseOutOrderNumber, info.DrugId, advice.ID)
  3910. }
  3911. //更新字典里面的库存
  3912. stockInfo, _ := GetSelfDrugAllStockInfoOne(advice.PatientId, advice.UserOrgId, advice.DrugId)
  3913. //查询改药品信息
  3914. medical, _ := GetBaseDrugMedical(advice.DrugId)
  3915. var sum_count_one int64
  3916. for _, its := range stockInfo {
  3917. if its.MaxUnit == medical.MaxUnit {
  3918. its.StockMaxNumber = its.StockMaxNumber * medical.MinNumber
  3919. }
  3920. sum_count_one += its.StockMaxNumber + its.StockMinNumber
  3921. }
  3922. //更新库存
  3923. UpdateSumDrugTwo(advice.UserOrgId, advice.PatientId, advice.DrugId, sum_count_one)
  3924. }
  3925. return err
  3926. }
  3927. func SelfDrugAutoAddCancelInfoOne(advice *models.DoctorAdvice, creater int64) error {
  3928. //查询该批次病人该药品出库数据
  3929. info, _ := GetSelfDrugAutoWarehouseOutEight(advice.PatientId, advice.AdviceDate, advice.DrugId, advice.ID)
  3930. list, _ := GetSelfDrugAutoWarehouseOutNight(advice.PatientId, advice.AdviceDate, advice.DrugId, advice.ID)
  3931. //退库
  3932. drug, _ := FindBaseDrugLibRecord(advice.UserOrgId, advice.DrugId)
  3933. //如果剩余数量 + 退库数量 < 总入库数量
  3934. if len(list) > 0 {
  3935. for _, its := range list {
  3936. //查询该批次病人该药品出库数据
  3937. infos, _ := GetSelfDrugAutoWarehouseOutTen(advice.PatientId, advice.AdviceDate, advice.DrugId, advice.ID, its.WarehouseInfoId)
  3938. //查询该药品出库批次总入库数量
  3939. druginfo, _ := GetSelfDrugInfoSix(its.WarehouseInfoId, infos.OrgId)
  3940. var out_count int64 //该批次出库库存
  3941. var sum_count int64 //该批次总库存
  3942. var over_count int64 //剩余数量
  3943. if druginfo.MaxUnit != druginfo.MinUnit {
  3944. over_count = druginfo.StockMaxNumber*drug.MinNumber + druginfo.StockMinNumber
  3945. }
  3946. if druginfo.MaxUnit == druginfo.MinUnit {
  3947. over_count = druginfo.StockMaxNumber + druginfo.StockMinNumber
  3948. }
  3949. if infos.CountUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
  3950. out_count = infos.Count * drug.MinNumber
  3951. }
  3952. if infos.CountUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
  3953. out_count = infos.Count
  3954. }
  3955. if infos.CountUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
  3956. out_count = infos.Count
  3957. }
  3958. if druginfo.MaxUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
  3959. sum_count = druginfo.WarehousingCount * drug.MinNumber
  3960. }
  3961. if druginfo.MaxUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
  3962. sum_count = druginfo.WarehousingCount
  3963. }
  3964. if druginfo.MaxUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
  3965. sum_count = druginfo.WarehousingCount
  3966. }
  3967. fmt.Println("over_count-----------", over_count)
  3968. fmt.Println("out_count-----------", out_count)
  3969. fmt.Println("sum_count-----------", sum_count)
  3970. fmt.Println("its.count-------------", its.CountUnit)
  3971. fmt.Println("drug.MaxUnit-------------", drug.MaxUnit)
  3972. fmt.Println("drug.MinUnit-------------", drug.MinUnit)
  3973. if (over_count + out_count) <= sum_count {
  3974. if its.CountUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
  3975. //退库
  3976. warehouseInfo := models.XtSelfDrugWarehouseInfo{
  3977. StockMinNumber: its.Count,
  3978. }
  3979. UpdateSelfDrugWarehouseOne(its.WarehouseInfoId, warehouseInfo)
  3980. //增加退库数量
  3981. AddSelfCancelSumCountOne(advice.PatientId, advice.DrugId, advice.UserOrgId, its.Count)
  3982. //出库数量减少
  3983. UpdateSelfSumOutDrug(advice.UserOrgId, advice.PatientId, advice.DrugId, its.Count)
  3984. }
  3985. if its.CountUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
  3986. warehouseInfo := models.XtSelfDrugWarehouseInfo{
  3987. StockMaxNumber: its.Count,
  3988. }
  3989. UpdateSelfDrugWarehouseTwo(its.WarehouseInfoId, warehouseInfo)
  3990. //增加退库数量
  3991. AddSelfCancelSumCountOne(advice.PatientId, advice.DrugId, advice.UserOrgId, its.Count*drug.MinNumber)
  3992. //出库数量减少
  3993. UpdateSelfSumOutDrug(advice.UserOrgId, advice.PatientId, advice.DrugId, its.Count*drug.MinNumber)
  3994. }
  3995. if its.CountUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
  3996. warehouseInfo := models.XtSelfDrugWarehouseInfo{
  3997. StockMaxNumber: its.Count,
  3998. }
  3999. UpdateSelfDrugWarehouseTwo(its.WarehouseInfoId, warehouseInfo)
  4000. //增加退库数量
  4001. AddSelfCancelSumCountOne(advice.PatientId, advice.DrugId, advice.UserOrgId, its.Count*drug.MinNumber)
  4002. //出库数量减少
  4003. UpdateSelfSumOutDrug(advice.UserOrgId, advice.PatientId, advice.DrugId, its.Count*drug.MinNumber)
  4004. }
  4005. }
  4006. }
  4007. floatPrescrip := strconv.FormatFloat(advice.PrescribingNumber, 'f', -1, 32)
  4008. prescribingNumber, _ := strconv.ParseInt(floatPrescrip, 10, 64)
  4009. if advice.PrescribingNumberUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
  4010. //查询剩余库存
  4011. stockInfo, _ := GetSelfDrugAllStockInfoOne(advice.PatientId, advice.UserOrgId, advice.DrugId)
  4012. var sum_count int64
  4013. for _, its := range stockInfo {
  4014. if its.MaxUnit == drug.MaxUnit {
  4015. its.StockMaxNumber = its.StockMaxNumber * drug.MinNumber
  4016. }
  4017. sum_count += its.StockMaxNumber + its.StockMinNumber
  4018. }
  4019. var count_one int64
  4020. if advice.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
  4021. count_one = prescribingNumber * drug.MinNumber
  4022. }
  4023. if advice.PrescribingNumberUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
  4024. count_one = prescribingNumber
  4025. }
  4026. if advice.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
  4027. count_one = prescribingNumber * drug.MinNumber
  4028. }
  4029. flow := models.XtSelfDrugFlow{
  4030. WarehousingId: info.WarehouseInfoId,
  4031. DrugId: info.DrugId,
  4032. Number: "",
  4033. BatchNumber: info.BatchNumber,
  4034. Count: count_one,
  4035. UserOrgId: advice.UserOrgId,
  4036. PatientId: advice.PatientId,
  4037. SystemTime: info.SysRecordTime,
  4038. ConsumableType: 7,
  4039. IsSys: 0,
  4040. WarehousingOrder: "",
  4041. WarehouseOutId: 0,
  4042. WarehouseOutOrderNumber: info.WarehouseOutOrderNumber,
  4043. IsEdit: 0,
  4044. CancelStockId: 0,
  4045. CancelOrderNumber: "",
  4046. Manufacturer: info.Manufacturer,
  4047. Dealer: info.Dealer,
  4048. Creator: creater,
  4049. UpdateCreator: 0,
  4050. Status: 1,
  4051. Ctime: info.SysRecordTime,
  4052. Mtime: 0,
  4053. Price: info.Price,
  4054. WarehousingDetailId: info.WarehouseInfoId,
  4055. WarehouseOutDetailId: 0,
  4056. CancelOutDetailId: 0,
  4057. ExpireDate: info.ExpiryDate,
  4058. ProductDate: info.ProductDate,
  4059. MaxUnit: drug.MinUnit,
  4060. MinUnit: "",
  4061. AdviceId: info.AdviceId,
  4062. StorehouseId: 0,
  4063. OverCount: sum_count,
  4064. OperateTime: time.Now().Unix(),
  4065. }
  4066. CreateSelfDrugFlowTwo(flow)
  4067. //删除出库记录
  4068. UpdateSelfDrugFlowSeven(info.PatientId, info.SysRecordTime, info.DrugId, advice.ID, info.ID)
  4069. //删除流水
  4070. UpdateDrugFlowTensOne(info.PatientId, info.WarehouseOutOrderNumber, info.DrugId, advice.ID)
  4071. }
  4072. if advice.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
  4073. //查询剩余库存
  4074. stockInfo, _ := GetSelfDrugAllStockInfoOne(advice.PatientId, advice.UserOrgId, advice.DrugId)
  4075. var sum_count int64
  4076. for _, its := range stockInfo {
  4077. if its.MaxUnit == drug.MaxUnit {
  4078. its.StockMaxNumber = its.StockMaxNumber * drug.MinNumber
  4079. }
  4080. sum_count += its.StockMaxNumber + its.StockMinNumber
  4081. }
  4082. var count_one int64
  4083. if advice.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
  4084. count_one = prescribingNumber * drug.MinNumber
  4085. }
  4086. if advice.PrescribingNumberUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
  4087. count_one = prescribingNumber
  4088. }
  4089. if advice.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
  4090. count_one = prescribingNumber * drug.MinNumber
  4091. }
  4092. flow := models.XtSelfDrugFlow{
  4093. WarehousingId: info.WarehouseInfoId,
  4094. DrugId: info.DrugId,
  4095. Number: "",
  4096. BatchNumber: info.BatchNumber,
  4097. Count: count_one,
  4098. UserOrgId: advice.UserOrgId,
  4099. PatientId: advice.PatientId,
  4100. SystemTime: info.SysRecordTime,
  4101. ConsumableType: 7,
  4102. IsSys: 0,
  4103. WarehousingOrder: "",
  4104. WarehouseOutId: 0,
  4105. WarehouseOutOrderNumber: info.WarehouseOutOrderNumber,
  4106. IsEdit: 0,
  4107. CancelStockId: 0,
  4108. CancelOrderNumber: "",
  4109. Manufacturer: info.Manufacturer,
  4110. Dealer: info.Dealer,
  4111. Creator: creater,
  4112. UpdateCreator: 0,
  4113. Status: 1,
  4114. Ctime: info.SysRecordTime,
  4115. Mtime: 0,
  4116. Price: info.Price,
  4117. WarehousingDetailId: info.WarehouseInfoId,
  4118. WarehouseOutDetailId: 0,
  4119. CancelOutDetailId: 0,
  4120. ExpireDate: info.ExpiryDate,
  4121. ProductDate: info.ProductDate,
  4122. MaxUnit: drug.MinUnit,
  4123. MinUnit: "",
  4124. AdviceId: info.AdviceId,
  4125. StorehouseId: 0,
  4126. OverCount: sum_count,
  4127. OperateTime: time.Now().Unix(),
  4128. }
  4129. CreateSelfDrugFlowTwo(flow)
  4130. //删除出库记录
  4131. UpdateSelfDrugFlowSeven(info.PatientId, info.SysRecordTime, info.DrugId, advice.ID, info.ID)
  4132. //删除流水
  4133. UpdateDrugFlowTensOne(info.PatientId, info.WarehouseOutOrderNumber, info.DrugId, advice.ID)
  4134. }
  4135. if advice.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
  4136. //查询剩余库存
  4137. stockInfo, _ := GetSelfDrugAllStockInfoOne(advice.PatientId, advice.UserOrgId, advice.DrugId)
  4138. var sum_count int64
  4139. for _, its := range stockInfo {
  4140. if its.MaxUnit == drug.MaxUnit {
  4141. its.StockMaxNumber = its.StockMaxNumber * drug.MinNumber
  4142. }
  4143. sum_count += its.StockMaxNumber + its.StockMinNumber
  4144. }
  4145. flow := models.XtSelfDrugFlow{
  4146. WarehousingId: info.WarehouseInfoId,
  4147. DrugId: info.DrugId,
  4148. Number: "",
  4149. BatchNumber: info.BatchNumber,
  4150. Count: prescribingNumber,
  4151. UserOrgId: advice.UserOrgId,
  4152. PatientId: advice.PatientId,
  4153. SystemTime: info.SysRecordTime,
  4154. ConsumableType: 7,
  4155. IsSys: 0,
  4156. WarehousingOrder: "",
  4157. WarehouseOutId: 0,
  4158. WarehouseOutOrderNumber: info.WarehouseOutOrderNumber,
  4159. IsEdit: 0,
  4160. CancelStockId: 0,
  4161. CancelOrderNumber: "",
  4162. Manufacturer: info.Manufacturer,
  4163. Dealer: info.Dealer,
  4164. Creator: creater,
  4165. UpdateCreator: 0,
  4166. Status: 1,
  4167. Ctime: info.SysRecordTime,
  4168. Mtime: 0,
  4169. Price: info.Price,
  4170. WarehousingDetailId: info.WarehouseInfoId,
  4171. WarehouseOutDetailId: 0,
  4172. CancelOutDetailId: 0,
  4173. ExpireDate: info.ExpiryDate,
  4174. ProductDate: info.ProductDate,
  4175. MaxUnit: advice.PrescribingNumberUnit,
  4176. MinUnit: "",
  4177. AdviceId: info.AdviceId,
  4178. StorehouseId: 0,
  4179. OverCount: sum_count,
  4180. OperateTime: time.Now().Unix(),
  4181. }
  4182. CreateSelfDrugFlowTwo(flow)
  4183. //删除出库记录
  4184. UpdateSelfDrugFlowSeven(info.PatientId, info.SysRecordTime, info.DrugId, advice.ID, info.ID)
  4185. //删除流水
  4186. UpdateDrugFlowTensOne(info.PatientId, info.WarehouseOutOrderNumber, info.DrugId, advice.ID)
  4187. }
  4188. //更新字典里面的库存
  4189. stockInfo, _ := GetSelfDrugAllStockInfoOne(advice.PatientId, advice.UserOrgId, advice.DrugId)
  4190. //查询改药品信息
  4191. medical, _ := GetBaseDrugMedical(advice.DrugId)
  4192. var sum_count_one int64
  4193. for _, its := range stockInfo {
  4194. if its.MaxUnit == medical.MaxUnit {
  4195. its.StockMaxNumber = its.StockMaxNumber * medical.MinNumber
  4196. }
  4197. sum_count_one += its.StockMaxNumber + its.StockMinNumber
  4198. }
  4199. //更新库存
  4200. UpdateSumDrugTwo(advice.UserOrgId, advice.PatientId, advice.DrugId, sum_count_one)
  4201. }
  4202. return err
  4203. }
  4204. func GetDrugInventoryRecordList(limit int64, page int64, keyword string, storehouse_id int64, startTime int64, endTime int64, orgId int64) (list []*models.XtDrugStockInventoryList, total int64, err error) {
  4205. likeKey := "%" + keyword + "%"
  4206. offset := (page - 1) * limit
  4207. db := XTReadDB().Model(&list).Where("status=1")
  4208. if len(keyword) > 0 {
  4209. db = db.Where("drug_name like ?", likeKey)
  4210. }
  4211. if storehouse_id > 0 {
  4212. db = db.Where("storehouse_id = ?", storehouse_id)
  4213. }
  4214. if startTime > 0 {
  4215. db = db.Where("record_date>=?", startTime)
  4216. }
  4217. if endTime > 0 {
  4218. db = db.Where("record_date<=?", endTime)
  4219. }
  4220. if orgId > 0 {
  4221. db = db.Where("user_org_id =?", orgId)
  4222. }
  4223. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  4224. return list, total, err
  4225. }
  4226. func GetDrugInventoryRecordPrintList(limit int64, page int64, keyword string, storehouse_id int64, orgId int64, startTime int64, endTime int64, idSplit []string) (list []*models.XtDrugStockInventoryList, total int64, err error) {
  4227. offset := (page - 1) * limit
  4228. likeKey := "%" + keyword + "%"
  4229. db := XTReadDB().Model(&list).Where("status =1")
  4230. if len(keyword) > 0 {
  4231. db = db.Where("drug_name like ?", likeKey)
  4232. }
  4233. if storehouse_id > 0 {
  4234. db = db.Where("storehouse_id = ?", storehouse_id)
  4235. }
  4236. if orgId > 0 {
  4237. db = db.Where("user_org_id = ?", orgId)
  4238. }
  4239. if startTime > 0 {
  4240. db = db.Where("record_date>=?", startTime)
  4241. }
  4242. if endTime > 0 {
  4243. db = db.Where("record_date<=?", endTime)
  4244. }
  4245. if len(idSplit) > 0 {
  4246. db = db.Where("id in(?)", idSplit)
  4247. }
  4248. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  4249. return list, total, err
  4250. }
  4251. func GetDrugWarehosueInfoList(org_id int64, start_time int64, end_time int64) (flow []*models.XtDrugFlowList, err error) {
  4252. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type=1 or consumable_type =10)")
  4253. if org_id > 0 {
  4254. db = db.Where("user_org_id = ?", org_id)
  4255. }
  4256. if start_time > 0 {
  4257. db = db.Where("ctime >= ?", start_time)
  4258. }
  4259. if end_time > 0 {
  4260. db = db.Where("ctime<=?", end_time)
  4261. }
  4262. err = db.Find(&flow).Error
  4263. return flow, err
  4264. }
  4265. func GetDrugWarhouseHouseOutInfo(org_id int64, start_time int64, end_time int64) (flow []*models.XtDrugFlowList, err error) {
  4266. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type=2 or consumable_type=3 or consumable_type=11)")
  4267. if org_id > 0 {
  4268. db = db.Where("user_org_id = ?", org_id)
  4269. }
  4270. if start_time > 0 {
  4271. db = db.Where("ctime >= ?", start_time)
  4272. }
  4273. if end_time > 0 {
  4274. db = db.Where("ctime<=?", end_time)
  4275. }
  4276. err = db.Find(&flow).Error
  4277. return flow, err
  4278. }
  4279. func GetDrugWarehouseCancelOutInfo(org_id int64, start_time int64, end_time int64) (flow []*models.XtDrugFlowList, err error) {
  4280. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type= 7)")
  4281. if org_id > 0 {
  4282. db = db.Where("user_org_id = ?", org_id)
  4283. }
  4284. if start_time > 0 {
  4285. db = db.Where("ctime >= ?", start_time)
  4286. }
  4287. if end_time > 0 {
  4288. db = db.Where("ctime<=?", end_time)
  4289. }
  4290. err = db.Find(&flow).Error
  4291. return flow, err
  4292. }
  4293. func GetDrugMonthWarehouseInfo(org_id int64, start_time int64) (flow []*models.XtDrugFlowList, err error) {
  4294. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type=1 or consumable_type=10)")
  4295. if org_id > 0 {
  4296. db = db.Where("user_org_id = ?", org_id)
  4297. }
  4298. if start_time > 0 {
  4299. db = db.Where("ctime < ?", start_time)
  4300. }
  4301. err = db.Find(&flow).Error
  4302. return flow, err
  4303. }
  4304. func GetDrugMonthWarehouseOutInfo(org_id int64, start_time int64) (flow []*models.XtDrugFlowList, err error) {
  4305. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type=2 or consumable_type=3 or consumable_type=11)")
  4306. if org_id > 0 {
  4307. db = db.Where("user_org_id = ?", org_id)
  4308. }
  4309. if start_time > 0 {
  4310. db = db.Where("ctime < ?", start_time)
  4311. }
  4312. err = db.Find(&flow).Error
  4313. return flow, err
  4314. }
  4315. func GetDrugMonthCancelOutInfo(org_id int64, start_time int64) (flow []*models.XtDrugFlowList, err error) {
  4316. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type=7)")
  4317. if org_id > 0 {
  4318. db = db.Where("user_org_id = ?", org_id)
  4319. }
  4320. if start_time > 0 {
  4321. db = db.Where("ctime < ?", start_time)
  4322. }
  4323. err = db.Find(&flow).Error
  4324. return flow, err
  4325. }