self_drug_service.go 186KB

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