self_drug_service.go 119KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289
  1. package service
  2. import (
  3. "fmt"
  4. "time"
  5. "XT_New/models"
  6. "github.com/jinzhu/gorm"
  7. )
  8. func GetCurrentPatient(orgid int64) (patients []*models.DrugPatients, err error) {
  9. err = XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&patients).Error
  10. return patients, err
  11. }
  12. func GetAllDrugName(orgid int64) (baseDrug []*models.XtBaseDrug, err error) {
  13. err = XTReadDB().Group("drug_name").Where("org_id = ? and status = 1 and find_in_set('停用',drug_status) = 0", orgid).Find(&baseDrug).Error
  14. return baseDrug, err
  15. }
  16. func GetDrugName(drugname string, orgid int64) (*models.XtDrugName, error) {
  17. drugName := models.XtDrugName{}
  18. err := XTReadDB().Model(&drugName).Where("drug_name = ? and user_org_id = ? and status =1", drugname, orgid).Find(&drugName).Error
  19. if err == gorm.ErrRecordNotFound {
  20. return nil, err
  21. }
  22. if err != nil {
  23. return nil, err
  24. }
  25. return &drugName, nil
  26. }
  27. func SaveDrugName(drugname *models.XtDrugName) error {
  28. err := XTWriteDB().Create(&drugname).Error
  29. return err
  30. }
  31. func GetDrugNameList(orgid int64) (druname []*models.XtDrugName, err error) {
  32. err = XTReadDB().Model(&druname).Where("user_org_id = ? and status = 1", orgid).Find(&druname).Error
  33. return druname, err
  34. }
  35. func GetRulleName(orgid int64, name string) (baseDrug []*models.XtBaseDrug, err error) {
  36. err = XTReadDB().Model(&baseDrug).Where("org_id = ? and drug_name = ? and status = 1", orgid, name).Find(&baseDrug).Error
  37. return baseDrug, err
  38. }
  39. func GetDrugId(id int64) (models.XtSelfMedical, error) {
  40. medical := models.XtSelfMedical{}
  41. err := XTReadDB().Model(&medical).Where("id = ? and status = 1", id).Find(&medical).Error
  42. return medical, err
  43. }
  44. func GetRulleList(orgid int64, id int64) (rulleName []*models.XtStandName, err error) {
  45. err = XTReadDB().Model(&rulleName).Where("user_org_id = ? and drug_id = ? and status = 1", orgid, id).Find(&rulleName).Error
  46. return rulleName, err
  47. }
  48. func GetUnitByBaseId(id int64) (models.XtBaseDrug, error) {
  49. drug := models.XtBaseDrug{}
  50. err := XTReadDB().Model(&drug).Where("id = ? and status = 1", id).Find(&drug).Error
  51. return drug, err
  52. }
  53. func IsExistStandName(drugname string, drugspec string, orgid int64) (*models.XtStandName, error) {
  54. standName := models.XtStandName{}
  55. err := XTReadDB().Model(&standName).Where("drug_name = ? and drug_spec = ? and user_org_id = ? and status = 1", drugname, drugspec, orgid).Find(&standName).Error
  56. if err == gorm.ErrRecordNotFound {
  57. return nil, err
  58. }
  59. if err != nil {
  60. return nil, err
  61. }
  62. return &standName, nil
  63. }
  64. func SaveRulleName(rullName *models.XtStandName) error {
  65. err := XTWriteDB().Model(&rullName).Create(&rullName).Error
  66. return err
  67. }
  68. func GetRullerNameDetail(id int64) (models.XtStandName, error) {
  69. rullerName := models.XtStandName{}
  70. err := XTReadDB().Model(&rullerName).Where("id= ? and status = 1", id).Find(&rullerName).Error
  71. return rullerName, err
  72. }
  73. func GetIsExit(drugname string, drugspec string, id int64) (*models.XtStandName, error) {
  74. standName := models.XtStandName{}
  75. err := XTReadDB().Model(&standName).Where("drug_name = ? and drug_spec = ? and id <> ? and status = 1", drugname, drugspec, id).Find(&standName).Error
  76. if err == gorm.ErrRecordNotFound {
  77. return nil, err
  78. }
  79. if err != nil {
  80. return nil, err
  81. }
  82. return &standName, nil
  83. }
  84. func UpdatedRullerName(id int64, re *models.XtStandName) error {
  85. 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
  86. return err
  87. }
  88. //func GetAllDrugNameList(orgid int64) (rullername []*models.XtStandName, err error) {
  89. //
  90. // err = XTReadDB().Group("drug_name").Model(&rullername).Where("user_org_id = ? and status = 1", orgid).Find(&rullername).Error
  91. // return rullername, err
  92. //}
  93. func GetAllDrugNameList(orgid int64) (drugName []*models.XtDrugName, err error) {
  94. err = XTReadDB().Model(&drugName).Where("user_org_id = ? and status = 1", orgid).Find(&drugName).Error
  95. return drugName, err
  96. }
  97. func GetRullerListByDrugName(drugname string, orgid int64) (rullername []*models.XtStandName, err error) {
  98. err = XTReadDB().Model(&rullername).Where("drug_id = ? and user_org_id = ? and status = 1", drugname, orgid).Find(&rullername).Error
  99. return rullername, err
  100. }
  101. func GetBaseDrugMedical(id int64) (models.XtBaseDrug, error) {
  102. drug := models.XtBaseDrug{}
  103. err := XTReadDB().Model(&drug).Where("id = ? and status = 1", id).Find(&drug).Error
  104. return drug, err
  105. }
  106. func GetNewBaseDrugMedical(id int64, tx *gorm.DB) (models.XtBaseDrug, error) {
  107. drug := models.XtBaseDrug{}
  108. err := tx.Model(&drug).Where("id = ? and status = 1", id).Find(&drug).Error
  109. if err != gorm.ErrRecordNotFound {
  110. if err != nil {
  111. tx.Rollback()
  112. return drug, err
  113. }
  114. }
  115. return drug, err
  116. }
  117. func ModifyDrugWarehouseByInfoId(id int64, stock_max_number int64) error {
  118. info := models.DrugWarehouseInfo{}
  119. err := XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", stock_max_number)).Error
  120. return err
  121. }
  122. func ModifyDrugWarehouseByStockMin(id int64, stock_min_number int64) error {
  123. info := models.DrugWarehouseInfo{}
  124. err := XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", stock_min_number)).Error
  125. return err
  126. }
  127. func GetSelfMedicalByDrugName(drugname string, drug_spec string, patientid int64) (*models.XtSelfMedical, error) {
  128. medical := models.XtSelfMedical{}
  129. err := XTReadDB().Model(&medical).Where("drug_name = ? and drug_spec = ? and patient_id = ? and status = 1", drugname, drug_spec, patientid).Find(&medical).Error
  130. if err == gorm.ErrRecordNotFound {
  131. return nil, err
  132. }
  133. if err != nil {
  134. return nil, err
  135. }
  136. return &medical, err
  137. }
  138. func CreateSelfMedical(medical *models.XtSelfMedical) error {
  139. err = XTWriteDB().Model(&medical).Create(&medical).Error
  140. return err
  141. }
  142. func GetCurrentOrgAllStaff(orgid int64, app int64) (role []*models.SgjUserAdminRole, err error) {
  143. err = UserReadDB().Model(&role).Where("org_id = ? and app_id = ? and status = 1", orgid, app).Find(&role).Error
  144. return role, err
  145. }
  146. func CreateStock(stock *models.XtSelfStock) error {
  147. err := XTWriteDB().Model(&stock).Create(&stock).Error
  148. return err
  149. }
  150. func CreateOutStock(stock *models.XtSelfOutStock) error {
  151. err := XTWriteDB().Model(&stock).Create(&stock).Error
  152. return err
  153. }
  154. func GetStandDrugByDrugName(drugname string, orgid int64) (*models.XtStandName, error) {
  155. standname := models.XtStandName{}
  156. err := XTReadDB().Model(&standname).Where("drug_name = ? and user_org_id = ? and status = 1", drugname, orgid).Find(&standname).Error
  157. if err == gorm.ErrRecordNotFound {
  158. return nil, err
  159. }
  160. if err != nil {
  161. return nil, err
  162. }
  163. return &standname, nil
  164. }
  165. func DeleteDrugName(id int64) (models.XtDrugName, error) {
  166. standName := models.XtDrugName{}
  167. err := XTWriteDB().Model(&standName).Where("id = ?", id).Update(map[string]interface{}{"status": 0}).Error
  168. return standName, err
  169. }
  170. func GetDrugSetByUserOrgId(orgid int64) (*models.XtDrugSet, error) {
  171. set := models.XtDrugSet{}
  172. err := XTReadDB().Model(&set).Where("user_org_id = ? and status = 1", orgid).Find(&set).Error
  173. if err == gorm.ErrRecordNotFound {
  174. return nil, err
  175. }
  176. if err != nil {
  177. return nil, err
  178. }
  179. return &set, nil
  180. }
  181. func SaveRadio(drugSet *models.XtDrugSet) error {
  182. err := XTWriteDB().Model(&drugSet).Create(&drugSet).Error
  183. return err
  184. }
  185. func UpdateDrugSet(set *models.XtDrugSet, orgid int64) error {
  186. err := XTWriteDB().Model(&set).Where("user_org_id = ?", orgid).Update(map[string]interface{}{"drug_start": set.DrugStart}).Error
  187. return err
  188. }
  189. func GetMedicalsByName(orgid int64, drugname string) (*models.XtSelfMedical, error) {
  190. medical := models.XtSelfMedical{}
  191. err := XTReadDB().Model(&medical).Where("user_org_id = ? and drug_name = ? and status = 1", orgid, drugname).Find(&medical).Error
  192. if err == gorm.ErrRecordNotFound {
  193. return nil, err
  194. }
  195. if err != nil {
  196. return nil, err
  197. }
  198. return &medical, nil
  199. }
  200. func DeleteDrugStand(id int64) error {
  201. standName := models.XtStandName{}
  202. err := XTWriteDB().Model(&standName).Where("id =?", id).Update(map[string]interface{}{"status": 0}).Error
  203. return err
  204. }
  205. //func GetStockList(patientid int64, startime int64, keyword string) (stock []*models.SelfStocks, err error) {
  206. // db := XTReadDB().Table("xt_self_stock as x").Where("x.status = 1")
  207. // likeKey := "%" + keyword + "%"
  208. // table := XTReadDB().Table("xt_patients as s")
  209. // fmt.Println(table)
  210. // if patientid > 0 {
  211. // db = db.Where("x.patient_id = ?", patientid)
  212. // }
  213. // if startime > 0 {
  214. // db = db.Where("x.storck_time <= ?", startime)
  215. // }
  216. // 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
  217. // return stock, err
  218. //}
  219. func GetStockList(patientid int64, startime int64, keyword string, orgid int64) (stock []*models.XtSelfMedicals, err error) {
  220. likeKey := "%" + keyword + "%"
  221. db := XTReadDB().Table("xt_self_medical as x").Where("x.status = 1")
  222. if patientid > 0 {
  223. db = db.Where("x.patient_id = ?", patientid)
  224. }
  225. if orgid > 0 {
  226. db = db.Where("x.user_org_id = ?", orgid)
  227. }
  228. 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
  229. return stock, err
  230. }
  231. func GetOutStockList(patientid int64, startime int64, keyword string, orgid int64) (outStock []*models.SelfOutStock, err error) {
  232. likeKey := "%" + keyword + "%"
  233. db := XTReadDB().Table("xt_self_medical as x").Where("x.status = 1")
  234. if patientid > 0 {
  235. db = db.Where("x.patient_id = ?", patientid)
  236. }
  237. if orgid > 0 {
  238. db = db.Where("x.user_org_id = ?", orgid)
  239. }
  240. 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
  241. return outStock, err
  242. }
  243. func GetTotalOutStockList(patientid int64, startime int64, keyword string, orgid int64) (outStock []*models.SelfOutStock, err error) {
  244. likeKey := "%" + keyword + "%"
  245. db := XTReadDB().Table("xt_self_out_stock as x").Where("x.status = 1")
  246. if patientid > 0 {
  247. db = db.Where("x.patient_id = ?", patientid)
  248. }
  249. if orgid > 0 {
  250. db = db.Where("x.user_org_id = ?", orgid)
  251. }
  252. if startime > 0 {
  253. db = db.Where("x.storck_time<=?", startime)
  254. }
  255. 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
  256. return outStock, err
  257. }
  258. //func GetOutStockList(patientid int64, startime int64, keyword string) (outStock []*models.XtSelfOutStocks, err error) {
  259. // db := XTReadDB().Table("xt_self_out_stock as x").Where("x.status = 1")
  260. // likeKey := "%" + keyword + "%"
  261. // table := XTReadDB().Table("xt_patients as s")
  262. // fmt.Println(table)
  263. // if patientid > 0 {
  264. // db = db.Where("x.patient_id = ?", patientid)
  265. // }
  266. // if startime > 0 {
  267. // db = db.Where("x.storck_time <= ?", startime)
  268. // }
  269. // 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
  270. // return outStock, err
  271. //}
  272. func GetMedicalList(patientid int64, orgid int64, keyword string) (medical []*models.XtSelfMedicals, err error) {
  273. likeKey := "%" + keyword + "%"
  274. db := XTReadDB().Table("xt_self_medical as x").Where("x.status = 1")
  275. if patientid > 0 {
  276. db = db.Where("x.patient_id = ?", patientid)
  277. }
  278. if orgid > 0 {
  279. db = db.Where("x.user_org_id = ?", orgid)
  280. }
  281. 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
  282. //err = XTReadDB().Model(&medical).Where("patient_id = ? and user_org_id = ? and status = 1", patientid, orgid).Group("drug_name,drug_spec").Find(&medical).Error
  283. return medical, err
  284. }
  285. func GetStockOutDetail(durgname string, patientid int64) (stock []*models.XtSelfOutStock, err error) {
  286. err = XTReadDB().Model(&stock).Where("drug_name = ? and patient_id = ? and status = 1", durgname, patientid).Find(&stock).Error
  287. return stock, err
  288. }
  289. func DeleteDrugStockNumber(drugname string, patientid int64) (models.XtSelfStock, error) {
  290. stock := models.XtSelfStock{}
  291. err := XTWriteDB().Model(&stock).Where("drug_name = ? and patient_id = ?", drugname, patientid).Update(map[string]interface{}{"status": 0}).Error
  292. return stock, err
  293. }
  294. func GetStockDetail(drugname string, drugspec string, startime int64, endtime int64, patientid int64, orgid int64) (stock []*models.XtSelfStock, err error) {
  295. 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
  296. return stock, err
  297. }
  298. func GetOutStockDetail(drugname string, drugspec string, startime int64, endtime int64, patientid int64, orgid int64) (outStock []*models.XtSelfOutStock, err error) {
  299. 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
  300. return outStock, err
  301. }
  302. func GetAllPatientStockList(drug_name string, drug_spec string, startime int64, endtime int64, keyword string, orgid int64) (stocks []*models.SelfStocks, err error) {
  303. db := XTReadDB().Table("xt_self_stock as x").Where("x.status = 1")
  304. likeKey := "%" + keyword + "%"
  305. table := XTReadDB().Table("xt_patients as s")
  306. fmt.Println(table)
  307. if len(drug_name) > 0 {
  308. db = db.Where("x.drug_name = ?", drug_name)
  309. }
  310. if len(drug_spec) > 0 {
  311. db = db.Where("x.drug_spec = ?", drug_spec)
  312. }
  313. //if startime > 0 {
  314. // db = db.Where("x.storck_time >= ?", startime)
  315. //}
  316. //if endtime > 0 {
  317. // db = db.Where("x.storck_time <= ?", endtime)
  318. //}
  319. if orgid > 0 {
  320. db = db.Where("x.user_org_id = ?", orgid)
  321. }
  322. 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
  323. return stocks, err
  324. }
  325. func GetAllPatientOutStockList(drug_name string, drug_spec string, startime int64, endtime int64, keyword string, orgid int64) (outStock []*models.XtSelfOutStocks, err error) {
  326. db := XTReadDB().Table("xt_self_out_stock as x").Where("x.status = 1")
  327. likeKey := "%" + keyword + "%"
  328. table := XTReadDB().Table("xt_patients as s")
  329. fmt.Println(table)
  330. if len(drug_name) > 0 {
  331. db = db.Where("x.drug_name = ?", drug_name)
  332. }
  333. if len(drug_spec) > 0 {
  334. db = db.Where("x.drug_spec=?", drug_spec)
  335. }
  336. if startime > 0 {
  337. db = db.Where("x.storck_time >= ?", startime)
  338. }
  339. if endtime > 0 {
  340. db = db.Where("x.storck_time<=?", endtime)
  341. }
  342. if orgid > 0 {
  343. db = db.Where("x.user_org_id = ?", orgid)
  344. }
  345. 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
  346. return outStock, err
  347. }
  348. func GetDrugDataByPatientId(patientid int64) (selfMedical []*models.XtSelfMedical, err error) {
  349. err = XTReadDB().Group("drug_name,drug_spec").Model(&selfMedical).Where("patient_id = ? and status = 1", patientid).Find(&selfMedical).Error
  350. return selfMedical, err
  351. }
  352. func GetDruckStockConfig(orgid int64) (models.XtDrugStockConfig, error) {
  353. drugConfig := models.XtDrugStockConfig{}
  354. err := XTReadDB().Model(&drugConfig).Where("org_id = ? and status = 1", orgid).Find(&drugConfig).Error
  355. return drugConfig, err
  356. }
  357. func GetSetSelfMedical(orgid int64) (models.XtDrugSet, error) {
  358. medical := models.XtDrugSet{}
  359. err := XTReadDB().Model(&medical).Where("user_org_id = ? and status = 1", orgid).Find(&medical).Error
  360. return medical, err
  361. }
  362. func GetSelfMedicalList(patientid int64) (selfmedical []*models.XtSelfMedical, err error) {
  363. err = XTReadDB().Group("drug_name").Model(&selfmedical).Where("patient_id = ? and status = 1", patientid).Find(&selfmedical).Error
  364. return selfmedical, err
  365. }
  366. func GetDrugDescByDrugName(drugname string, patient_id int64, orgid int64) (selfMedical []*models.XtSelfMedical, err error) {
  367. err = XTReadDB().Model(&selfMedical).Where("drug_name = ? and patient_id = ? and user_org_id = ? and status = 1", drugname, patient_id, orgid).Find(&selfMedical).Error
  368. return selfMedical, err
  369. }
  370. func GetBaseMedcal(drugname string, orgid int64) (baseDrug []*models.XtBaseDrug, err error) {
  371. err = XTReadDB().Model(&baseDrug).Where("drug_name = ? and org_id = ? and status = 1", drugname, orgid).Find(&baseDrug).Error
  372. return baseDrug, err
  373. }
  374. func GetTotalBaseMedicalCount(drug_id int64, orgid int64) (drugInfo []*models.DrugWarehousingInfo, err error) {
  375. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  376. err = db.Select("x.drug_id,sum(x.warehousing_count) as count").Where("drug_id = ? and org_id = ?", drug_id, orgid).Scan(&drugInfo).Error
  377. return drugInfo, err
  378. }
  379. func GetTotalBaseMedicalCountOut(drug_id int64, orgid int64) (drugOut []*models.DrugWarehousingInfo, err error) {
  380. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  381. err = db.Select("x.drug_id,sum(x.count) as count").Where("drug_id = ? and org_id = ?", drug_id, orgid).Scan(&drugOut).Error
  382. return drugOut, err
  383. }
  384. func GetDrugWareseOut(startime int64, orgid int64) (*models.XtDrugWarehouse, error) {
  385. warehouse := models.XtDrugWarehouse{}
  386. err := XTReadDB().Model(&warehouse).Where("warehousing_time = ? and org_id = ? and status = 1", startime, orgid).Find(&warehouse).Error
  387. if err == gorm.ErrRecordNotFound {
  388. return nil, err
  389. }
  390. if err != nil {
  391. return nil, err
  392. }
  393. return &warehouse, nil
  394. }
  395. func GetManufacturer(id int64) (models.XtBaseDrug, error) {
  396. drug := models.XtBaseDrug{}
  397. err := XTReadDB().Model(&drug).Where("id = ? and status = 1", id).Find(&drug).Error
  398. return drug, err
  399. }
  400. func CreateDrugWarehoue(warehouse *models.XtDrugWarehouse) error {
  401. err := XTWriteDB().Model(&warehouse).Create(&warehouse).Error
  402. return err
  403. }
  404. func GetTodayWareHousringOrder(startime int64, orgid int64) (models.XtDrugWarehouseOut, error) {
  405. warehouse := models.XtDrugWarehouseOut{}
  406. err := XTReadDB().Model(&warehouse).Where("warehouse_out_time = ? and org_id = ? and status = 1", startime, orgid).Find(&warehouse).Error
  407. return warehouse, err
  408. }
  409. func CreateDrugWareHouseOut(out *models.XtDrugWarehouseOut) error {
  410. err := XTWriteDB().Model(&out).Create(&out).Error
  411. return err
  412. }
  413. func CreateWareHouseOutInfo(info *models.XtDrugWarehouseOutInfo) error {
  414. err := XTWriteDB().Model(&info).Create(&info).Error
  415. return err
  416. }
  417. func CreateAotoMaticReduceDetail(detail *models.XtDrugAutomaticReduceDetail) error {
  418. err := XTWriteDB().Model(&detail).Create(&detail).Error
  419. return err
  420. }
  421. func GetDrugStockConfig(orgid int64) (models.XtDrugStockConfig, error) {
  422. config := models.XtDrugStockConfig{}
  423. err := XTReadDB().Model(&config).Where("org_id =? and status = 1", orgid).Find(&config).Error
  424. return config, err
  425. }
  426. func GetDrugSet(orgid int64) (models.XtDrugSet, error) {
  427. drugSet := models.XtDrugSet{}
  428. err := XTReadDB().Model(&drugSet).Where("user_org_id =? and status = 1", orgid).Find(&drugSet).Error
  429. return drugSet, err
  430. }
  431. type Drugs struct {
  432. ID int64 `gorm:"column:id" json:"id" form:"id"`
  433. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  434. Pinyin string `gorm:"column:pinyin" json:"pinyin" form:"pinyin"`
  435. Wubi string `gorm:"column:wubi" json:"wubi" form:"wubi"`
  436. DrugAlias string `gorm:"column:drug_alias" json:"drug_alias" form:"drug_alias"`
  437. DrugAliasPinyin string `gorm:"column:drug_alias_pinyin" json:"drug_alias_pinyin" form:"drug_alias_pinyin"`
  438. DrugAliasWubi string `gorm:"column:drug_alias_wubi" json:"drug_alias_wubi" form:"drug_alias_wubi"`
  439. DrugCategory int64 `gorm:"column:drug_category" json:"drug_category" form:"drug_category"`
  440. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  441. DrugType int64 `gorm:"column:drug_type" json:"drug_type" form:"drug_type"`
  442. DrugStockLimit string `gorm:"column:drug_stock_limit" json:"drug_stock_limit" form:"drug_stock_limit"`
  443. DrugStatus string `gorm:"column:drug_status" json:"drug_status" form:"drug_status"`
  444. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  445. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  446. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  447. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  448. Status int64 `gorm:"column:status" json:"status" form:"status"`
  449. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  450. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  451. Drugs []*Drugs `gorm:"ForeignKey:DrugName;AssociationForeignKey:DrugName" json:"drug_specs"`
  452. HospApprFlag int64 `gorm:"column:hosp_appr_flag" json:"hosp_appr_flag" form:"hosp_appr_flag"`
  453. LmtUsedFlag int64 `gorm:"column:lmt_used_flag" json:"lmt_used_flag" form:"lmt_used_flag"`
  454. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  455. MinNumber int64 `gorm:"column:min_number" json:"min_number" form:"min_number"`
  456. Dose string `gorm:"column:dose" json:"dose" form:"dose"`
  457. DoseUnit string `gorm:"column:dose_unit" json:"dose_unit" form:"dose_unit"`
  458. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  459. DrugDoseUnit int64 `grom:"column:drug_dose_unit" json:"drug_dose_unit" form:"drug_dose_unit"`
  460. FirstLetter string `gorm:"column:first_letter" json:"first_letter" form:"first_letter"`
  461. }
  462. func (Drugs) TableName() string {
  463. return "xt_base_drug"
  464. }
  465. func GetAllBaseDrugLibList(org_id int64) (drugs []*Drugs, err error) {
  466. 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")
  467. err = db.Find(&drugs).Error
  468. return
  469. }
  470. type PrivateDrug struct {
  471. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  472. ID int64 `gorm:"column:id" json:"id" form:"id"`
  473. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  474. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  475. DrugNameId int64 `gorm:"column:drug_name_id" json:"drug_name_id" form:"drug_name_id"`
  476. Status int64 `gorm:"column:status" json:"status" form:"status"`
  477. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  478. CreatedTime int64 `gorm:"column:created_time" json:"created_time" form:"created_time"`
  479. UpdatedTime int64 `gorm:"column:updated_time" json:"updated_time" form:"updated_time"`
  480. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  481. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  482. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  483. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  484. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  485. PrivateDrug []*PrivateDrug `gorm:"ForeignKey:DrugName;AssociationForeignKey:DrugName" json:"drug_specs"`
  486. }
  487. func (PrivateDrug) TableName() string {
  488. return "xt_self_medical"
  489. }
  490. func GetPrivateDrugList(patient_id int64, org_id int64) (pds []*PrivateDrug, err error) {
  491. 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
  492. return
  493. }
  494. func GetAllMedicalList(orgid int64) (list []*models.XtSelfMedical, err error) {
  495. err = XTReadDB().Model(&list).Where("user_org_id = ? and status = 1", orgid).Group("drug_name").Find(&list).Error
  496. return list, err
  497. }
  498. func DeleteDrugNameById(id int64) (models.XtSelfMedical, error) {
  499. medical := models.XtSelfMedical{}
  500. err := XTWriteDB().Model(&medical).Where("id = ?", id).Updates(map[string]interface{}{"status": 0}).Error
  501. return medical, err
  502. }
  503. func GetDrugNameIsExist(drugid int64, orgid int64) (*models.XtDrugName, error) {
  504. drugName := models.XtDrugName{}
  505. err := XTReadDB().Model(&drugName).Where("drug_id = ? and user_org_id = ? and status = 1", drugid, orgid).Find(&drugName).Error
  506. if err == gorm.ErrRecordNotFound {
  507. return nil, err
  508. }
  509. if err != nil {
  510. return nil, err
  511. }
  512. return &drugName, nil
  513. }
  514. func UpdatedDrugName(id int64, orgid int64, drugName *models.XtDrugName) error {
  515. 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
  516. return err
  517. }
  518. func GetStandNameIsExist(id int64, orgid int64) (*models.XtStandName, error) {
  519. standName := models.XtStandName{}
  520. err := XTReadDB().Model(&standName).Where("drug_name_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&standName).Error
  521. if err == gorm.ErrRecordNotFound {
  522. return nil, err
  523. }
  524. if err != nil {
  525. return nil, err
  526. }
  527. return &standName, nil
  528. }
  529. func UpdateStandName(id int64, orgid int64, standName *models.XtStandName) error {
  530. 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
  531. return err
  532. }
  533. func GetSelfMedicalIsExist(id int64, orgid int64) (*models.XtSelfMedical, error) {
  534. medical := models.XtSelfMedical{}
  535. err := XTReadDB().Model(&medical).Where("drug_name_id = ? and user_org_id = ? and status =1", id, orgid).Find(&medical).Error
  536. if err == gorm.ErrRecordNotFound {
  537. return nil, err
  538. }
  539. if err != nil {
  540. return nil, err
  541. }
  542. return &medical, nil
  543. }
  544. func UpdateSelfMedical(id int64, orgid int64, medical *models.XtSelfMedical) error {
  545. 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
  546. return err
  547. }
  548. func GetDrugNameId(id int64, orgid int64) (models.XtSelfMedical, error) {
  549. medical := models.XtSelfMedical{}
  550. err := XTReadDB().Model(&medical).Where("drug_name_id = ? and status = 1 and user_org_id = ?", id, orgid).Find(&medical).Error
  551. return medical, err
  552. }
  553. func GetSelfStosckIsExist(id int64, orgid int64) (*models.XtSelfStock, error) {
  554. medical := models.XtSelfStock{}
  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 UpdatedSelfStock(stock *models.XtSelfStock, id int64, orgid int64) error {
  565. 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
  566. return err
  567. }
  568. func GetOutSelfOutStockIsExist(id int64, orgid int64) (*models.XtSelfOutStock, error) {
  569. outStock := models.XtSelfOutStock{}
  570. err := XTReadDB().Model(&outStock).Where("drug_name_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&outStock).Error
  571. if err == gorm.ErrRecordNotFound {
  572. return nil, err
  573. }
  574. if err != nil {
  575. return nil, err
  576. }
  577. return &outStock, nil
  578. }
  579. func UpdatedOutSelfStock(stock *models.XtSelfOutStock, id int64, orgid int64) error {
  580. 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
  581. return err
  582. }
  583. func GetDoctorAdviceIsExist(id int64, orgid int64) (*models.DoctorAdvice, error) {
  584. advice := models.DoctorAdvice{}
  585. err := XTReadDB().Model(&advice).Where("drug_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&advice).Error
  586. if err == gorm.ErrRecordNotFound {
  587. return nil, err
  588. }
  589. if err != nil {
  590. return nil, err
  591. }
  592. return &advice, nil
  593. }
  594. func UpdateNoExcuteDoctorAdvice(id int64, orgid int64, advice *models.DoctorAdvice) error {
  595. 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
  596. return err
  597. }
  598. func GetDoctorAdviceTemplateIsExist(id int64, orgid int64) (*models.DoctorAdviceTemplate, error) {
  599. template := models.DoctorAdviceTemplate{}
  600. err := XTReadDB().Model(&template).Where("drug_id = ? and org_id = ? and status = 1", id, orgid).Find(&template).Error
  601. if err == gorm.ErrRecordNotFound {
  602. return nil, err
  603. }
  604. if err != nil {
  605. return nil, err
  606. }
  607. return &template, nil
  608. }
  609. func UpdatedDoctorAdviceTemplate(id int64, orgid int64, template *models.DoctorAdviceTemplate) error {
  610. 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
  611. return err
  612. }
  613. func ToSeachPatient(keyword string, orgid int64) (patients []*models.Patients, err error) {
  614. likeKey := "%" + keyword + "%"
  615. err = XTReadDB().Model(&patients).Where("name like ? and user_org_id = ? and status = 1", likeKey, orgid).Find(&patients).Error
  616. return patients, err
  617. }
  618. func GetMedicalDrugNameList(orgid int64) (drugName []*models.DrugName, err error) {
  619. db := XTReadDB().Model(&drugName).Where("user_org_id = ? and status = 1", orgid)
  620. err = db.Preload("XtStandName", "status =1 and user_org_id = ?", orgid).Find(&drugName).Error
  621. return drugName, err
  622. }
  623. func GetSelfOutStock(orgid int64, advicedate int64, patientid int64, adivicename string, advicedesc string) (*models.XtSelfOutStock, error) {
  624. stock := models.XtSelfOutStock{}
  625. 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
  626. if err == gorm.ErrRecordNotFound {
  627. return nil, err
  628. }
  629. if err != nil {
  630. return nil, err
  631. }
  632. return &stock, err
  633. }
  634. func UpdatedSelfOutSatock(id int64, stock *models.XtSelfOutStock) error {
  635. 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
  636. return err
  637. }
  638. func GetSelfStockQuery(typename int64, keywords string, startime int64, endtime int64, orgid int64, limit int64, page int64) (selfoutstock []*models.XtSelfOutStockOne, total int64, err error) {
  639. offset := (page - 1) * limit
  640. db := XTReadDB().Table("xt_self_stock as x").Where("x.status = 1")
  641. likeKey := "%" + keywords + "%"
  642. if len(keywords) > 0 {
  643. db = db.Where("x.drug_name like ? ", likeKey)
  644. }
  645. if startime > 0 {
  646. db = db.Where("x.storck_time >=?", startime)
  647. }
  648. if endtime > 0 {
  649. db = db.Where("x.storck_time<=?", endtime)
  650. }
  651. if orgid > 0 {
  652. db = db.Where("x.user_org_id = ?", orgid)
  653. }
  654. if typename > 0 {
  655. 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
  656. } else {
  657. 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
  658. }
  659. return selfoutstock, total, err
  660. }
  661. func GetSelfOutStockQuery(startTime int64, endTime int64, orgid int64) (selfoutstock []*models.XtSelfOutStockOne, err error) {
  662. db := XTReadDB().Table("xt_self_out_stock as x").Where("x.status = 1")
  663. if startTime > 0 {
  664. db = db.Where("x.storck_time >?", startTime)
  665. }
  666. if endTime > 0 {
  667. db = db.Where("x.storck_time<=?", endTime)
  668. }
  669. if orgid > 0 {
  670. db = db.Where("x.user_org_id = ?", orgid)
  671. }
  672. err = db.Select("x.drug_name_id,Sum(x.outstore_number) as count").Group("x.drug_name").Scan(&selfoutstock).Error
  673. return selfoutstock, err
  674. }
  675. func GetLastDrugWarehouseOutInfo(drugid int64, warehouseOutId int64, orgid int64) (models.XtDrugWarehouseOutInfo, error) {
  676. info := models.XtDrugWarehouseOutInfo{}
  677. err = XTReadDB().Model(&info).Where("drug_id = ? and warehouse_out_id = ? and org_id = ? and status = 1", drugid, warehouseOutId, orgid).Last(&info).Error
  678. return info, err
  679. }
  680. func PostSearchDrugWarehouseList(keyword string, orgid int64, storehouse_id int64) (info []*models.BloodDrugWarehouseInfo, err error) {
  681. 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)")
  682. table := XTReadDB().Table("xt_base_drug as t").Where("t.status")
  683. fmt.Println(table)
  684. likeKey := "%" + keyword + "%"
  685. if orgid > 0 {
  686. db = db.Where("x.org_id = ?", orgid)
  687. }
  688. if storehouse_id > 0 {
  689. db = db.Where("x.storehouse_id = ?", storehouse_id)
  690. }
  691. 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
  692. return info, err
  693. }
  694. func CreateDrugPrice(price models.XtDrugAdjustPrice) error {
  695. err := XTWriteDB().Create(&price).Error
  696. return err
  697. }
  698. func GetDrugPriceList(startime int64, endtime int64, orgId int64, keyword string, limit int64, page int64) (adjust []*models.VmDrugAdjustPrice, total int64, err error) {
  699. likeKey := "%" + keyword + "%"
  700. offset := (page - 1) * limit
  701. db := XTReadDB().Table("xt_drug_adjust_price as x").Where("x.status = 1")
  702. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  703. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  704. fmt.Println(table, tab)
  705. if len(keyword) > 0 {
  706. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  707. }
  708. if orgId > 0 {
  709. db = db.Where("x.user_org_id = ?", orgId)
  710. }
  711. if startime > 0 {
  712. db = db.Where("x.ctime>=?", startime)
  713. }
  714. if endtime > 0 {
  715. db = db.Where("x.ctime<=?", endtime)
  716. }
  717. 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
  718. return adjust, total, err
  719. }
  720. func UpdateDrugAdjuestPrice(ids []string, info models.XtDrugAdjustPrice) error {
  721. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  722. return err
  723. }
  724. func GetDrugAdjuestPrice(ids []string) (info []*models.XtDrugAdjustPrice, err error) {
  725. err = XTReadDB().Model(&info).Where("id in(?) and status = 1", ids).Find(&info).Error
  726. return info, err
  727. }
  728. func GetDrugDamage(ids []string) (info []*models.XtDrugDamage, err error) {
  729. err = XTReadDB().Model(&info).Where("id in(?) and status = 1", ids).Find(&info).Error
  730. return info, err
  731. }
  732. func CreateDrugDamage(damage models.XtDrugDamage) error {
  733. err := XTWriteDB().Create(&damage).Error
  734. return err
  735. }
  736. func GetDrugDamageList(startime int64, endtime int64, orgId int64, keyword string, limit int64, page int64, storehouse_id int64) (adjust []*models.VmDrugInventory, total int64, err error) {
  737. likeKey := "%" + keyword + "%"
  738. offset := (page - 1) * limit
  739. db := XTReadDB().Table("xt_drug_inventory as x").Where("x.status = 1 and type = 4")
  740. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  741. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  742. fmt.Println(table, tab)
  743. //if len(keyword) > 0 {
  744. // db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  745. //}
  746. if len(keyword) > 0 {
  747. db = db.Where("x.drug_name like ?", likeKey)
  748. }
  749. if orgId > 0 {
  750. db = db.Where("x.user_org_id = ?", orgId)
  751. }
  752. if startime > 0 {
  753. db = db.Where("x.ctime >=?", startime)
  754. }
  755. if endtime > 0 {
  756. db = db.Where("x.ctime <=?", endtime)
  757. }
  758. if storehouse_id > 0 {
  759. db = db.Where("x.storehouse_id = ?", storehouse_id)
  760. }
  761. 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
  762. return adjust, total, err
  763. }
  764. func GetDrugDamageByOrgId(orgid int64) (list []*models.XtDrugInventory, err error) {
  765. err = XTReadDB().Model(&list).Where("user_org_id = ? and type = 4 and status = 1", orgid).Find(&list).Error
  766. return list, err
  767. }
  768. func UpdateDrugDamage(ids []string, damage models.XtDrugDamage) error {
  769. err := XTWriteDB().Model(&damage).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": damage.Checker, "checker_status": damage.CheckerStatus, "checker_time": damage.CheckerTime}).Error
  770. return err
  771. }
  772. func GetDrugModifyPrice(id int64) (models.XtDrugAdjustPrice, error) {
  773. adjuest := models.XtDrugAdjustPrice{}
  774. err := XTReadDB().Model(&adjuest).Where("id=? and status = 1", id).Find(&adjuest).Error
  775. return adjuest, err
  776. }
  777. func ModifyDrugPrice(id int64, adjust models.XtDrugAdjustPrice) error {
  778. 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
  779. return err
  780. }
  781. func GetModifyPricePrint(ids []string) (info []*models.XtDrugAdjustPrice, err error) {
  782. err = XTReadDB().Model(&info).Where("id in(?) and status = 1", ids).Find(&info).Error
  783. return info, err
  784. }
  785. func GetDrugDamageDetail(id int64) (models.XtDrugDamage, error) {
  786. damage := models.XtDrugDamage{}
  787. err := XTReadDB().Model(&damage).Where("id=? and status = 1", id).Find(&damage).Error
  788. return damage, err
  789. }
  790. func ModifyDrugDamage(id int64, damage models.XtDrugDamage) error {
  791. 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
  792. return err
  793. }
  794. func DeleteDrugDamage(id int64) error {
  795. damage := models.XtDrugDamage{}
  796. err := XTWriteDB().Model(&damage).Where("id=? and status = 1").Updates(map[string]interface{}{"status": 0}).Error
  797. return err
  798. }
  799. func GetDrugDamagePrint(ids []string) (damage []*models.XtDrugDamage, err error) {
  800. err = XTReadDB().Model(&damage).Where("id in(?) and status = 1", ids).Find(&damage).Error
  801. return damage, err
  802. }
  803. func GetWarehoseInfoById(id int64, storehouse_id int64, orgid int64) (info []*models.XtDrugWarehouseInfo, err error) {
  804. if orgid == 10191 {
  805. 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
  806. return info, err
  807. } else {
  808. 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
  809. return info, err
  810. }
  811. }
  812. func CreateDrugInventory(inventory models.XtDrugInventory) error {
  813. err := XTWriteDB().Create(&inventory).Error
  814. return err
  815. }
  816. func GetDrugInventoryList(keyword string, page int64, limit int64, orgid int64, startime int64, endtime int64) (list []*models.VmDrugInventory, total int64, err error) {
  817. fmt.Println("startdate", limit, page)
  818. likeKey := "%" + keyword + "%"
  819. offset := (page - 1) * limit
  820. db := XTReadDB().Table("xt_drug_inventory as x").Where("x.status = 1")
  821. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  822. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  823. fmt.Println(table, tab)
  824. if len(keyword) > 0 {
  825. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  826. }
  827. if orgid > 0 {
  828. db = db.Where("x.user_org_id = ?", orgid)
  829. }
  830. if startime > 0 {
  831. db = db.Where("x.start_time >=?", startime)
  832. }
  833. if endtime > 0 {
  834. db = db.Where("x.start_time<=?", endtime)
  835. }
  836. 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
  837. return list, total, err
  838. }
  839. func UpdateDrugInventory(ids []string, inventory models.XtDrugInventory) error {
  840. 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
  841. return err
  842. }
  843. func GetDrugInventoryListByIds(ids []string) (inventory []*models.XtDrugInventory, err error) {
  844. err = XTReadDB().Model(&inventory).Where("id in(?) and status = 1", ids).Find(&inventory).Error
  845. return inventory, err
  846. }
  847. func GetDrugDetailByDrugId(drug_id int64) (models.XtBaseDrug, error) {
  848. drug := models.XtBaseDrug{}
  849. err := XTReadDB().Model(&drug).Where("id = ? and status = 1", drug_id).Find(&drug).Error
  850. return drug, err
  851. }
  852. func GetDrugWareInfoById(id int64) (models.XtDrugWarehouseInfo, error) {
  853. info := models.XtDrugWarehouseInfo{}
  854. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  855. return info, err
  856. }
  857. func GetDrugWareInfoByIdTwo(id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  858. err = XTReadDB().Model(&info).Where("(id = ? or warehousing_info_id = ?) and status = 1", id, id).Find(&info).Error
  859. return info, err
  860. }
  861. func GetLastDrugWarehouse(orgid int64) (models.DrugWarehouse, error) {
  862. info := models.DrugWarehouse{}
  863. err := XTReadDB().Model(&info).Where("org_id=? and status = 1", orgid).Last(&info).Error
  864. return info, err
  865. }
  866. func GetLastDrugWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  867. info := models.DrugWarehouseOut{}
  868. err := XTReadDB().Model(&info).Where("org_id=? and status = 1", orgid).Last(&info).Error
  869. return info, err
  870. }
  871. func GetLastDrugWarehouseInfSix(drugid int64) (models.XtDrugWarehouseInfo, error) {
  872. info := models.XtDrugWarehouseInfo{}
  873. err := XTReadDB().Model(&info).Where("drug_id=? and status = 1", drugid).Last(&info).Error
  874. return info, err
  875. }
  876. func GetFirstDrugWarehouseInfo(drugid int64) (models.XtDrugWarehouseInfo, error) {
  877. info := models.XtDrugWarehouseInfo{}
  878. err := XTReadDB().Model(&info).Where("id=? and status = 1", drugid).First(&info).Error
  879. return info, err
  880. }
  881. func GetDrugInventoryDetail(id int64) (models.XtDrugInventory, error) {
  882. inventory := models.XtDrugInventory{}
  883. err := XTReadDB().Model(&inventory).Where("id = ? and status = 1", id).Find(&inventory).Error
  884. return inventory, err
  885. }
  886. func ModifyDrugInventory(id int64, info models.XtDrugInventory) error {
  887. 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
  888. return err
  889. }
  890. func DeleteDrugInventory(id int64) error {
  891. inventory := models.XtDrugInventory{}
  892. err := XTWriteDB().Model(&inventory).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  893. return err
  894. }
  895. func GetDrugInventoryPrintList(ids []string) (info []*models.XtDrugInventory, err error) {
  896. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&info).Error
  897. return info, err
  898. }
  899. func GetDrugInventoryDetailList(keyword string, page int64, limit int64, orgid int64, storehouse_id int64) (list []*models.VmDrugInventory, total int64, err error) {
  900. likeKey := "%" + keyword + "%"
  901. offset := (page - 1) * limit
  902. db := XTReadDB().Table("xt_drug_inventory as x").Where("x.status = 1")
  903. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  904. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  905. fmt.Println(table, tab)
  906. if len(keyword) > 0 {
  907. db = db.Where("x.warehousing_order = ? or r.user_name like ? or t.drug_name like ?", keyword, likeKey, likeKey)
  908. }
  909. if orgid > 0 {
  910. db = db.Where("x.user_org_id = ?", orgid)
  911. }
  912. if storehouse_id > 0 {
  913. db = db.Where("x.storehouse_id = ?", storehouse_id)
  914. }
  915. 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
  916. return list, total, err
  917. }
  918. func GetDrugInventoryDetailMapList(keyword string, page int64, limit int64, orgid int64, storehouse_id int64, ids []string) (list []*models.VmDrugInventory, total int64, err error) {
  919. likeKey := "%" + keyword + "%"
  920. offset := (page - 1) * limit
  921. db := XTReadDB().Table("xt_drug_inventory as x").Where("x.status = 1")
  922. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  923. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  924. fmt.Println(table, tab)
  925. if len(keyword) > 0 {
  926. db = db.Where("x.warehousing_order = ? or r.user_name like ? or t.drug_name like ?", keyword, likeKey, likeKey)
  927. }
  928. if orgid > 0 {
  929. db = db.Where("x.user_org_id = ?", orgid)
  930. }
  931. if storehouse_id > 0 {
  932. db = db.Where("x.storehouse_id = ?", storehouse_id)
  933. }
  934. if len(ids) > 0 {
  935. db = db.Where("x.id in(?)", ids)
  936. }
  937. 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
  938. return list, total, err
  939. }
  940. func GetDrugInventoryDetailListTwo(keyword string, page int64, limit int64, orgid int64, storehouse_id int64) (list []*models.XtDrugInventory, total int64, err error) {
  941. db := XTReadDB().Model(&list).Where("status = 1")
  942. if orgid > 0 {
  943. db = db.Where("user_org_id = ?", orgid)
  944. }
  945. if storehouse_id > 0 {
  946. db = db.Where("storehouse_id = ?", storehouse_id)
  947. }
  948. offset := (page - 1) * limit
  949. 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
  950. return list, total, err
  951. }
  952. func UpdateDrugProofInventory(id int64, inventory *models.XtDrugInventory) error {
  953. 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
  954. return err
  955. }
  956. func UpdateDrugWarehouseInfoById(id int64, inventory *models.XtDrugWarehouseInfo) error {
  957. err := XTWriteDB().Model(&inventory).Where("id = ?", id).Updates(map[string]interface{}{"stock_max_number": inventory.StockMaxNumber}).Error
  958. return err
  959. }
  960. func GetDrugWarehouseInfoTotal(id int64) (list []*models.XtDrugWarehouseInfo, err error) {
  961. err = XTReadDB().Where("id = ? or warehousing_info_id = ? and status = 1", id, id).Preload("XtBaseDrug", "status = 1").Find(&list).Error
  962. return list, err
  963. }
  964. func CreateDrugWarehouseInfo(info models.DrugWarehouseInfo) error {
  965. err := XTWriteDB().Create(&info).Error
  966. return err
  967. }
  968. func CreateDrugWarehouseOutInfo(info models.DrugWarehouseOutInfo) error {
  969. err := XTWriteDB().Create(&info).Error
  970. return err
  971. }
  972. func GetDamageByDrugId(drugid int64, warhousingorder string, drugtype int64) (drug []*models.VmXtDrugInventory, err error) {
  973. db := XTReadDB().Table("xt_drug_inventory").Where("status = 1")
  974. if drugid > 0 {
  975. db = db.Where("drug_id = ?", drugid)
  976. }
  977. if len(warhousingorder) > 0 {
  978. db = db.Where("warehousing_order = ?", warhousingorder)
  979. }
  980. if drugtype > 0 {
  981. db = db.Where("type = ?", drugtype)
  982. }
  983. if drugtype == 0 {
  984. db = db.Where("type = 4")
  985. }
  986. err = db.Preload("XtBaseDrug", "status = 1").Find(&drug).Error
  987. return drug, err
  988. }
  989. func GetInventoryModeList(orgid int64) (info []*models.PatientDrugWarehouseInfo, err error) {
  990. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  991. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  992. fmt.Println(table)
  993. if orgid > 0 {
  994. db = db.Where("x.org_id = ?", orgid)
  995. }
  996. 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").Joins("left join xt_base_drug as t on t.id = x.drug_id").Group("x.drug_id,x.batch_number").Scan(&info).Error
  997. return info, err
  998. }
  999. func GetManufacturerListByKeyword(orgid int64, keyword string) (list []*models.BloodManufacturer, err error) {
  1000. likeKey := "%" + keyword + "%"
  1001. db := XTReadDB().Model(&list).Where("status = 1")
  1002. if orgid > 0 {
  1003. db = db.Where("org_id = ?", orgid)
  1004. }
  1005. if len(keyword) > 0 {
  1006. db = db.Where("manufacturer_name like ?", likeKey)
  1007. }
  1008. err = db.Find(&list).Error
  1009. return list, err
  1010. }
  1011. func GetGoodNameByword(orgid int64, keyword string) (list []*models.GoodInfo, err error) {
  1012. likeKey := "%" + keyword + "%"
  1013. db := XTReadDB().Model(&list).Where("status = 1")
  1014. if orgid > 0 {
  1015. db = db.Where("org_id = ?", orgid)
  1016. }
  1017. if len(keyword) > 0 {
  1018. db = db.Where("good_name like ?", likeKey)
  1019. }
  1020. err = db.Find(&list).Error
  1021. return list, err
  1022. }
  1023. func GetDrugNameByword(orgid int64, keyword string) (list []*models.XtBaseDrug, err error) {
  1024. likeKey := "%" + keyword + "%"
  1025. db := XTReadDB().Model(&list).Where("status = 1")
  1026. if orgid > 0 {
  1027. db = db.Where("org_id = ?", orgid)
  1028. }
  1029. if len(keyword) > 0 {
  1030. db = db.Where("drug_name like ?", likeKey)
  1031. }
  1032. err = db.Find(&list).Error
  1033. return list, err
  1034. }
  1035. func GetGoodWarehouseInfoByOrgId(orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  1036. db := XTReadDB().Model(&info).Where("is_check =1 and status =1")
  1037. if orgid > 0 {
  1038. db = db.Where("org_id = ?", orgid)
  1039. }
  1040. if storehouse_id > 0 {
  1041. db = db.Where("storehouse_id = ?", storehouse_id)
  1042. }
  1043. err = db.Group("good_id").Find(&info).Error
  1044. return info, err
  1045. }
  1046. func GetGoodWarehouseInfoByOrgIdTwo(orgid int64) (info []*models.GoodQueryNewWarehousingInfo, err error) {
  1047. db := XTReadDB().Model(&info).Where("is_check =1 and status =1")
  1048. if orgid > 0 {
  1049. db = db.Where("org_id = ?", orgid)
  1050. }
  1051. err = db.Group("good_id").Find(&info).Error
  1052. return info, err
  1053. }
  1054. 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) {
  1055. offset := (page - 1) * limit
  1056. likeKey := "%" + keyword + "%"
  1057. db := XTReadDB().Table("xt_good_information as x").Where("x.status = 1 and x.total_count > 0 and x.org_id =?", orgId)
  1058. if good_type == 2 {
  1059. db = db.Where(" x.total_count<=x.stock_warn_count")
  1060. }
  1061. if len(goodIds) > 0 {
  1062. db = db.Where("id in(?)", goodIds)
  1063. }
  1064. if len(keyword) > 0 {
  1065. db = db.Where("good_name like ? or manufacturer in(?)", likeKey, ids)
  1066. }
  1067. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Find(&good).Error
  1068. return good, total, err
  1069. }
  1070. func FindGoodWarehouseInfoGroup(good_id int64, orgid int64, storhouse_id int64, startime int64, endtime int64) (info []*models.WarehousingInfo, err error) {
  1071. db := XTReadDB().Model(&info).Where("status = 1 and is_check = 1")
  1072. if good_id > 0 {
  1073. db = db.Where("good_id = ?", good_id)
  1074. }
  1075. if orgid > 0 {
  1076. db = db.Where("org_id = ?", orgid)
  1077. }
  1078. if storhouse_id > 0 {
  1079. db = db.Where("storehouse_id = ?", storhouse_id)
  1080. }
  1081. if startime > 0 {
  1082. db = db.Where("ctime >=?", startime)
  1083. }
  1084. if endtime > 0 {
  1085. db = db.Where("ctime<=?", endtime)
  1086. }
  1087. db.Group("storehouse_id").Find(&info)
  1088. return info, err
  1089. }
  1090. func FindGoodWarehouseInfo(good_id int64, orgid int64, storhouse_id int64, startime int64, endtime int64) (info []*models.WarehousingInfo, err error) {
  1091. db := XTReadDB().Model(&info).Where("status = 1 and is_check = 1")
  1092. if good_id > 0 {
  1093. db = db.Where("good_id = ?", good_id)
  1094. }
  1095. if orgid > 0 {
  1096. db = db.Where("org_id = ?", orgid)
  1097. }
  1098. if storhouse_id > 0 {
  1099. db = db.Where("storehouse_id = ?", storhouse_id)
  1100. }
  1101. if startime > 0 {
  1102. db = db.Where("ctime >=?", startime)
  1103. }
  1104. if endtime > 0 {
  1105. db = db.Where("ctime<=?", endtime)
  1106. }
  1107. db.Find(&info)
  1108. return info, err
  1109. }
  1110. func FindGoodWarehouseOutInfo(good_id int64, orgid int64, storhouse_id int64, startime int64, endtime int64) (out []*models.WarehouseOutInfo, err error) {
  1111. db := XTReadDB().Model(&out).Where("status = 1 and is_check = 1")
  1112. if good_id > 0 {
  1113. db = db.Where("good_id = ?", good_id)
  1114. }
  1115. if orgid > 0 {
  1116. db = db.Where("org_id = ?", orgid)
  1117. }
  1118. if storhouse_id > 0 {
  1119. db = db.Where("storehouse_id = ?", storhouse_id)
  1120. }
  1121. if startime > 0 {
  1122. db = db.Where("ctime>=?", startime)
  1123. }
  1124. if endtime > 0 {
  1125. db = db.Where("ctime<=?", endtime)
  1126. }
  1127. err = db.Find(&out).Error
  1128. return out, err
  1129. }
  1130. func FindCancelStockInfo(good_id int64, orgid int64, storhouse_id int64, startime int64, endtime int64) (info []*models.CancelStockInfo, err error) {
  1131. db := XTReadDB().Model(&info).Where("status = 1 and is_check = 1")
  1132. if good_id > 0 {
  1133. db = db.Where("good_id = ?", good_id)
  1134. }
  1135. if orgid > 0 {
  1136. db = db.Where("org_id = ?", orgid)
  1137. }
  1138. if storhouse_id > 0 {
  1139. db = db.Where("storehouse_id = ?", storhouse_id)
  1140. }
  1141. if startime > 0 {
  1142. db = db.Where("ctime>=?", startime)
  1143. }
  1144. if endtime > 0 {
  1145. db = db.Where("ctime<=?", endtime)
  1146. }
  1147. err = db.Find(&info).Error
  1148. return info, err
  1149. }
  1150. func FindGoodStockOutFlow(good_id int64, user_org_id int64, storhouse_id int64, startime int64, endtime int64) (info []*models.VmStockFlow, err error) {
  1151. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 2 or consumable_type = 3)")
  1152. if good_id > 0 {
  1153. db = db.Where("good_id = ?", good_id)
  1154. }
  1155. if user_org_id > 0 {
  1156. db = db.Where("user_org_id = ?", user_org_id)
  1157. }
  1158. if storhouse_id > 0 {
  1159. db = db.Where("storehouse_id = ?", storhouse_id)
  1160. }
  1161. if startime > 0 {
  1162. db = db.Where("ctime >= ?", storhouse_id)
  1163. }
  1164. if endtime > 0 {
  1165. db = db.Where("ctime <= ?", storhouse_id)
  1166. }
  1167. err = db.Find(&info).Error
  1168. return info, err
  1169. }
  1170. func FindGoodCancelFlow(good_id int64, user_org_id int64, storhouse_id int64, startime int64, endtime int64) (info []*models.VmStockFlow, err error) {
  1171. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 4 or consumable_type = 7)")
  1172. if good_id > 0 {
  1173. db = db.Where("good_id = ?", good_id)
  1174. }
  1175. if user_org_id > 0 {
  1176. db = db.Where("user_org_id = ?", user_org_id)
  1177. }
  1178. if storhouse_id > 0 {
  1179. db = db.Where("storehouse_id = ?", storhouse_id)
  1180. }
  1181. if startime > 0 {
  1182. db = db.Where("ctime >= ?", storhouse_id)
  1183. }
  1184. if endtime > 0 {
  1185. db = db.Where("ctime <= ?", storhouse_id)
  1186. }
  1187. err = db.Find(&info).Error
  1188. return info, err
  1189. }
  1190. 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) {
  1191. db := XTReadDB().Model(&info).Where("status = 1")
  1192. offset := (page - 1) * limit
  1193. if good_id > 0 {
  1194. db = db.Where("good_id = ?", good_id)
  1195. }
  1196. if orgid > 0 {
  1197. db = db.Where("org_id =?", orgid)
  1198. }
  1199. if startime > 0 {
  1200. db = db.Where("ctime >=?", startime)
  1201. }
  1202. if endtime > 0 {
  1203. db = db.Where("ctime<=?", endtime)
  1204. }
  1205. if storehouse_id > 0 {
  1206. db = db.Where("storehouse_id = ?", storehouse_id)
  1207. }
  1208. 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
  1209. return info, total, err
  1210. }
  1211. func GetStockFlushByGoodId(good_id int64, org_id int64, startime int64, endtime int64) (flushinfo []*models.XtWarehouseFlushInfo, err error) {
  1212. db := XTReadDB().Model(&models.XtWarehouseFlushInfo{}).Where("status = 1 and type = 1")
  1213. if good_id > 0 {
  1214. db = db.Where("good_id = ?", good_id)
  1215. }
  1216. if org_id > 0 {
  1217. db = db.Where("user_org_id = ?", org_id)
  1218. }
  1219. if startime > 0 {
  1220. db = db.Where("ctime >=?", startime)
  1221. }
  1222. if endtime > 0 {
  1223. db = db.Where("ctime<=?", endtime)
  1224. }
  1225. err = db.Find(&flushinfo).Error
  1226. return flushinfo, err
  1227. }
  1228. func GetGoodWarehouseOutInfoByIdListTwenty(good_id int64, limit int64, page int64, orgid int64, startime int64, endtime int64) (info []*models.WarehouseOutInfo, err error) {
  1229. db := XTReadDB().Model(&info).Where("status = 1")
  1230. if good_id > 0 {
  1231. db = db.Where("good_id = ?", good_id)
  1232. }
  1233. if orgid > 0 {
  1234. db = db.Where("org_id =?", orgid)
  1235. }
  1236. if startime > 0 {
  1237. db = db.Where("ctime >=?", startime)
  1238. }
  1239. if endtime > 0 {
  1240. db = db.Where("ctime<=?", endtime)
  1241. }
  1242. err = db.Find(&info).Error
  1243. return info, err
  1244. }
  1245. func GetGoodWarehouseInfoByList(goodid int64, orgid int64, startime int64, endtime int64) (info []*models.WarehousingInfo, err error) {
  1246. db := XTReadDB().Model(&info).Where("status= 1 and is_check = 1")
  1247. if goodid > 0 {
  1248. db = db.Where("good_id = ?", goodid)
  1249. }
  1250. if orgid > 0 {
  1251. db = db.Where("org_id = ?", orgid)
  1252. }
  1253. err = db.Find(&info).Error
  1254. return info, err
  1255. }
  1256. func GetGoodCancelStockInfoByList(goodid int64, orgid int64, startime int64, endtime int64) (info []*models.CancelStockInfo, err error) {
  1257. db := XTReadDB().Model(&info).Where("status = 1 and is_check = 1")
  1258. if goodid > 0 {
  1259. db = db.Where("good_id = ?", goodid)
  1260. }
  1261. if orgid > 0 {
  1262. db = db.Where("org_id = ?", orgid)
  1263. }
  1264. err = db.Find(&info).Error
  1265. return info, err
  1266. }
  1267. func GetGoodPatientList(orgid int64, keyword string) (patient []*models.XtPatients, err error) {
  1268. likeKey := "%" + keyword + "%"
  1269. if len(keyword) > 0 {
  1270. err = XTReadDB().Where("user_org_id = ? and status = 1 and name like ?", orgid, likeKey).Find(&patient).Error
  1271. } else {
  1272. err = XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&patient).Error
  1273. }
  1274. return patient, err
  1275. }
  1276. func GetWarehouseOutDetailByPatientIdOne(orgid int64, patient_id int64, startime int64, endtime int64, limit int64, page int64) (info []*models.WarehouseOutInfo, total int64, err error) {
  1277. db := XTReadDB().Model(&info).Where("status = 1 and (is_sys = 1 or is_sys = 5)")
  1278. offset := (page - 1) * limit
  1279. if orgid > 0 {
  1280. db = db.Where("org_id = ?", orgid)
  1281. }
  1282. if patient_id > 0 {
  1283. db = db.Where("patient_id = ?", patient_id)
  1284. }
  1285. if startime > 0 {
  1286. db = db.Where("ctime>=?", startime)
  1287. }
  1288. if endtime > 0 {
  1289. db = db.Where("ctime<=?", endtime)
  1290. }
  1291. err = db.Group("good_id").Preload("GoodInfo", "org_id = ? and status = 1", orgid).Count(&total).Offset(offset).Limit(limit).Find(&info).Error
  1292. return info, total, err
  1293. }
  1294. func GetWarehouseOutDetailByPatientId(orgid int64, patient_id int64, startime int64, endtime int64) (info []*models.WarehouseOutInfo, err error) {
  1295. db := XTReadDB().Model(&info).Where("status = 1 and (is_sys = 1 or is_sys =5)")
  1296. if orgid > 0 {
  1297. db = db.Where("org_id = ?", orgid)
  1298. }
  1299. if patient_id > 0 {
  1300. db = db.Where("patient_id = ?", patient_id)
  1301. }
  1302. if startime > 0 {
  1303. db = db.Where("ctime>=?", startime)
  1304. }
  1305. if endtime > 0 {
  1306. db = db.Where("ctime<=?", endtime)
  1307. }
  1308. err = db.Preload("GoodInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  1309. return info, err
  1310. }
  1311. 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) {
  1312. offset := (page - 1) * limit
  1313. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.is_check = 1 and x.stock_count >0")
  1314. if storehouse_id > 0 {
  1315. db = db.Where("x.storehouse_id = ?", storehouse_id)
  1316. }
  1317. //已过期
  1318. if expiry_type == 1 {
  1319. db = db.Where("x.expiry_date < ?", startime)
  1320. }
  1321. if expiry_type > 1 {
  1322. db = db.Where("x.expiry_date > ? and x.expiry_date<=?", startime, endtime)
  1323. }
  1324. if orgid > 0 {
  1325. db = db.Where("x.org_id = ?", orgid)
  1326. }
  1327. if len(keyword) > 0 {
  1328. db = db.Where("x.manufacturer in(?) or x.good_id in(?)", ids, goodIds)
  1329. }
  1330. err = db.Count(&total).Offset(offset).Limit(limit).Preload("GoodInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  1331. return info, total, err
  1332. }
  1333. func GetGoodPurchaseStockQuery(good_type int64, keyword string, page int64, limit int64, orgid int64, ids []int64, goodIds []int64) (goodinfo []*models.GoodInfoTwenty, total int64, err error) {
  1334. db := XTReadDB().Model(&goodinfo).Where("status = 1 and total_count > 0")
  1335. offset := (page - 1) * limit
  1336. likeKey := "%" + keyword + "%"
  1337. if good_type > 0 {
  1338. db = db.Where("good_type_id = ?", good_type)
  1339. }
  1340. if orgid > 0 {
  1341. db = db.Where("org_id = ?", orgid)
  1342. }
  1343. if len(ids) > 0 {
  1344. db = db.Where("id in(?)", goodIds)
  1345. }
  1346. if len(keyword) > 0 {
  1347. db = db.Where("good_name like ? or manufacturer in(?)", likeKey, ids)
  1348. }
  1349. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&goodinfo).Error
  1350. return goodinfo, total, err
  1351. }
  1352. func GetDrugWarehouseOutDetailByPatientIdOne(patient_id int64, orgid int64, startime int64, endtime int64, page int64, limit int64) (info []*models.DrugWarehouseOutInfo, total int64, err error) {
  1353. db := XTReadDB().Model(&info).Where("status = 1")
  1354. offset := (page - 1) * limit
  1355. if patient_id > 0 {
  1356. db = db.Where("patient_id = ?", patient_id)
  1357. }
  1358. if orgid > 0 {
  1359. db = db.Where("org_id = ?", orgid)
  1360. }
  1361. if startime > 0 {
  1362. db = db.Where("sys_record_time >= ?", startime)
  1363. }
  1364. if endtime > 0 {
  1365. db = db.Where("sys_record_time <= ?", endtime)
  1366. }
  1367. err = db.Group("drug_id").Count(&total).Offset(offset).Limit(limit).Preload("BaseDrugLib", "status = 1 and org_id = ?", orgid).Find(&info).Error
  1368. return
  1369. }
  1370. func GetDrugWarehouseOutDetailByPatientId(patient_id int64, orgid int64, startime int64, endtime int64) (info []*models.DrugWarehouseOutInfo, err error) {
  1371. db := XTReadDB().Model(&info).Where("status = 1")
  1372. if patient_id > 0 {
  1373. db = db.Where("patient_id = ?", patient_id)
  1374. }
  1375. if orgid > 0 {
  1376. db = db.Where("org_id = ?", orgid)
  1377. }
  1378. if startime > 0 {
  1379. db = db.Where("sys_record_time >= ?", startime)
  1380. }
  1381. if endtime > 0 {
  1382. db = db.Where("sys_record_time <= ?", endtime)
  1383. }
  1384. err = db.Preload("BaseDrugLib", "status = 1 and org_id = ?", orgid).Find(&info).Error
  1385. return
  1386. }
  1387. 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) {
  1388. offset := (page - 1) * limit
  1389. 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)")
  1390. if storehouse_id > 0 {
  1391. db = db.Where("x.storehouse_id = ?", storehouse_id)
  1392. }
  1393. //已过期
  1394. if expiry_type == 1 {
  1395. db = db.Where("x.expiry_date < ?", startime)
  1396. }
  1397. if expiry_type > 1 {
  1398. db = db.Where("x.expiry_date > ? and x.expiry_date<=?", startime, endtime)
  1399. }
  1400. if orgid > 0 {
  1401. db = db.Where("x.org_id = ?", orgid)
  1402. }
  1403. if len(keyword) > 0 {
  1404. db = db.Where("x.manufacturer in(?) or x.drug_id in(?)", ids, goodIds)
  1405. }
  1406. err = db.Count(&total).Offset(offset).Limit(limit).Preload("XtBaseDrug", "org_id = ? and status = 1", orgid).Find(&info).Error
  1407. return info, total, err
  1408. }
  1409. 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) {
  1410. db := XTReadDB().Model(&druginfo).Where("status = 1")
  1411. offset := (page - 1) * limit
  1412. likeKey := "%" + keyword + "%"
  1413. if good_type > 0 {
  1414. db = db.Where("drug_type = ?", good_type)
  1415. }
  1416. if orgid > 0 {
  1417. db = db.Where("org_id = ?", orgid)
  1418. }
  1419. if len(keyword) > 0 {
  1420. db = db.Where("drug_name like ? or manufacturer in(?)", likeKey, ids)
  1421. }
  1422. if len(infos) > 0 {
  1423. db = db.Where("id in(?)", infos)
  1424. }
  1425. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&druginfo).Error
  1426. return druginfo, total, err
  1427. }
  1428. func GetDrugWarehouseInfoByOrgId(orgid int64, storehouse_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  1429. db := XTReadDB().Model(&info).Where("is_check = 1 and status = 1")
  1430. if orgid > 0 {
  1431. db = db.Where("org_id = ?", orgid)
  1432. }
  1433. if storehouse_id > 0 {
  1434. db = db.Where("storehouse_id = ?", storehouse_id)
  1435. }
  1436. err = db.Group("drug_id").Find(&info).Error
  1437. return info, err
  1438. }
  1439. func GetDrugWarehouseInfoByOrgIdTwo(orgid int64) (info []*models.XtDrugWarehouseInfo, err error) {
  1440. db := XTReadDB().Model(&info).Where("is_check = 1 and status = 1")
  1441. if orgid > 0 {
  1442. db = db.Where("org_id = ?", orgid)
  1443. }
  1444. err = db.Group("drug_id").Find(&info).Error
  1445. return info, err
  1446. }
  1447. 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) {
  1448. offset := (page - 1) * limit
  1449. likeKey := "%" + keyword + "%"
  1450. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1")
  1451. if good_type == 2 {
  1452. db = db.Where("x.total_count<=x.drug_stock_limit")
  1453. }
  1454. if storehouse_id > 0 {
  1455. db = db.Preload("DrugWarehouseInfo", "org_id = ? and status = 1 and storehouse_id = ? and is_check = 1", orgId, storehouse_id)
  1456. } else {
  1457. db = db.Preload("DrugWarehouseInfo", "org_id = ? and status = 1 and is_check = 1", orgId)
  1458. }
  1459. if len(goodIds) > 0 {
  1460. db = db.Where("id in(?)", goodIds)
  1461. }
  1462. if len(keyword) > 0 {
  1463. db = db.Where("drug_name like ? or manufacturer in(?)", likeKey, ids)
  1464. }
  1465. if orgId > 0 {
  1466. db = db.Where("x.org_id = ?", orgId)
  1467. }
  1468. err = db.Count(&total).Offset(offset).Limit(limit).Preload("DrugWarehouse", func(db *gorm.DB) *gorm.DB {
  1469. if startime > 0 {
  1470. db = db.Where("ctime>=?", startime)
  1471. }
  1472. if endtime > 0 {
  1473. db = db.Where("ctime<=?", endtime)
  1474. }
  1475. if storehouse_id > 0 {
  1476. db = db.Where("storehouse_id = ?", storehouse_id)
  1477. }
  1478. return db.Where("status = 1").Group("drug_id,storehouse_id")
  1479. }).Preload("DrugCancelStockInfo", func(db *gorm.DB) *gorm.DB {
  1480. if startime > 0 {
  1481. db = db.Where("ctime>=?", startime)
  1482. }
  1483. if endtime > 0 {
  1484. db = db.Where("ctime<=?", endtime)
  1485. }
  1486. return db.Where("status = 1")
  1487. }).Preload("DrugWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1488. if startime > 0 {
  1489. db = db.Where("ctime>=?", startime)
  1490. }
  1491. if endtime > 0 {
  1492. db = db.Where("ctime<=?", endtime)
  1493. }
  1494. return db.Where("status = 1")
  1495. }).Preload("DrugFlowWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  1496. if startime > 0 {
  1497. db = db.Where("ctime>=?", startime)
  1498. }
  1499. if endtime > 0 {
  1500. db = db.Where("ctime<=?", endtime)
  1501. }
  1502. if storehouse_id > 0 {
  1503. db = db.Where("storehouse_id = ?", storehouse_id)
  1504. }
  1505. return db.Where("status = 1 and (consumable_type = 2 or consumable_type = 3) and user_org_id = ?", orgId)
  1506. }).Preload("DrugActFlowWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  1507. if startime > 0 {
  1508. db = db.Where("ctime>=?", startime)
  1509. }
  1510. if endtime > 0 {
  1511. db = db.Where("ctime<=?", endtime)
  1512. }
  1513. if storehouse_id > 0 {
  1514. db = db.Where("storehouse_id = ?", storehouse_id)
  1515. }
  1516. return db.Where("status = 1 and (consumable_type = 2 or consumable_type = 3) and user_org_id = ?", orgId)
  1517. }).Order("x.ctime desc").Find(&drug).Error
  1518. return drug, total, err
  1519. }
  1520. 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) {
  1521. offset := (page - 1) * limit
  1522. likeKey := "%" + keyword + "%"
  1523. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1 AND find_in_set('停用',x.drug_status) = 0")
  1524. if good_type == 2 {
  1525. db = db.Where("x.total_count<=x.drug_stock_limit")
  1526. }
  1527. if good_type == 3 {
  1528. db = db.Where("x.sum_count = 0")
  1529. }
  1530. if good_type == 4 {
  1531. db = db.Where("x.sum_count > 0")
  1532. }
  1533. if storehouse_id > 0 {
  1534. db = db.Preload("DrugWarehouseInfo", "org_id = ? and status = 1 and storehouse_id = ? and is_check = 1", orgId, storehouse_id)
  1535. } else {
  1536. db = db.Preload("DrugWarehouseInfo", "org_id = ? and status = 1 and is_check = 1", orgId)
  1537. }
  1538. if len(keyword) > 0 {
  1539. db = db.Where("drug_name like ? or manufacturer in(?)", likeKey, ids)
  1540. }
  1541. if orgId > 0 {
  1542. db = db.Where("x.org_id = ?", orgId)
  1543. }
  1544. if orgId == 10489 {
  1545. err = db.Preload("XtDrugStockCount", func(db *gorm.DB) *gorm.DB {
  1546. if storehouse_id > 0 {
  1547. db = db.Where("storehouse_id = ?", storehouse_id)
  1548. }
  1549. return db.Where("status = 1 and user_org_id = ?", orgId)
  1550. }).Preload("DrugStockCount", func(db *gorm.DB) *gorm.DB {
  1551. if storehouse_id > 0 {
  1552. db = db.Where("storehouse_id = ?", storehouse_id)
  1553. }
  1554. return db.Where("status = 1 and user_org_id = ?", orgId).Group("drug_id,storehouse_id")
  1555. }).Count(&total).Offset(offset).Limit(limit).Order("x.sum_count desc").Find(&drug).Error
  1556. } else {
  1557. err = db.Preload("XtDrugStockCount", func(db *gorm.DB) *gorm.DB {
  1558. if storehouse_id > 0 {
  1559. db = db.Where("storehouse_id = ?", storehouse_id)
  1560. }
  1561. return db.Where("status = 1 and user_org_id = ?", orgId)
  1562. }).Preload("DrugStockCount", func(db *gorm.DB) *gorm.DB {
  1563. if storehouse_id > 0 {
  1564. db = db.Where("storehouse_id = ?", storehouse_id)
  1565. }
  1566. return db.Where("status = 1 and user_org_id = ?", orgId).Group("drug_id,storehouse_id")
  1567. }).Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Find(&drug).Error
  1568. }
  1569. return drug, total, err
  1570. }
  1571. 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) {
  1572. db := XTReadDB().Model(&info).Where("status = 1")
  1573. offset := (page - 1) * limit
  1574. if good_id > 0 {
  1575. db = db.Where("drug_id = ?", good_id)
  1576. }
  1577. if orgid > 0 {
  1578. db = db.Where("org_id =?", orgid)
  1579. }
  1580. if startime > 0 {
  1581. db = db.Where("ctime >=?", startime)
  1582. }
  1583. if endtime > 0 {
  1584. db = db.Where("ctime<=?", endtime)
  1585. }
  1586. if storehouse_id > 0 {
  1587. db = db.Where("storehouse_id = ?", storehouse_id)
  1588. }
  1589. err = db.Count(&total).Offset(offset).Limit(limit).Preload("BaseDrugLib", "status=1 and org_id = ?", orgid).Order("id desc").Find(&info).Error
  1590. return info, total, err
  1591. }
  1592. func GetAllGoodListSeven(orgid int64) (good []*models.GoodInfoSeven, err error) {
  1593. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&good).Error
  1594. return good, err
  1595. }
  1596. func GetAllGoodListEight(orgid int64) (good []*models.GoodInfoSeven, err error) {
  1597. err = XTReadDB().Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Find(&good).Error
  1598. return good, err
  1599. }
  1600. func GetGoodInformationWarehouseInfo(good_id int64, orgid int64) (info []*models.WarehousingInfoTen, err error) {
  1601. 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
  1602. return info, err
  1603. }
  1604. func UpdateGoodInformation(id int64, orgid int64, total_count int64) error {
  1605. err := XTWriteDB().Model(&models.GoodInfoTen{}).Where("id = ? and org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"total_count": total_count}).Error
  1606. return err
  1607. }
  1608. func UpdateGoodListCount(user_org_id int64, over_count int64, good_id int64, sotrehouse_id int64) error {
  1609. 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
  1610. return err
  1611. }
  1612. func UpdateGoodListCountOne(user_org_id int64, over_count int64, good_id int64, storehouse_id int64) error {
  1613. 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
  1614. return err
  1615. }
  1616. func GetBaseDrugName(orgid int64) (drug []*models.BaseDrugLibTwenty, err error) {
  1617. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&drug).Error
  1618. return drug, err
  1619. }
  1620. func GetDrugWarehouseInfoName(drugid int64, orgid int64) (info []*models.DrugWarehouseInfoTen, err error) {
  1621. 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
  1622. return info, err
  1623. }
  1624. func UpdateDrugById(id int64, orgid int64, limit_count int64, total_count int64) error {
  1625. 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, "total_count": total_count}).Error
  1626. return err
  1627. }
  1628. func GetDrugFlowFlush(drugid int64, org_id int64, startime int64, endtime int64) (info []*models.XtDrugWarehouseFlushInfo, err error) {
  1629. db := XTReadDB().Model(&info).Where("status = 1 and type = 1")
  1630. if drugid > 0 {
  1631. db = db.Where("drug_id = ?", drugid)
  1632. }
  1633. if org_id > 0 {
  1634. db = db.Where("user_org_id = ?", org_id)
  1635. }
  1636. if startime > 0 {
  1637. db = db.Where("ctime >=?", startime)
  1638. }
  1639. if endtime > 0 {
  1640. db = db.Where("ctime <=?", endtime)
  1641. }
  1642. err = db.Find(&info).Error
  1643. return info, err
  1644. }
  1645. func GetDrugFlowFlushOne(drugid int64, org_id int64, startime int64, endtime int64) (info []*models.XtDrugWarehouseFlushInfo, err error) {
  1646. db := XTReadDB().Model(&info).Where("status = 0 and type = 1")
  1647. if drugid > 0 {
  1648. db = db.Where("drug_id = ?", drugid)
  1649. }
  1650. if org_id > 0 {
  1651. db = db.Where("user_org_id = ?", org_id)
  1652. }
  1653. if startime > 0 {
  1654. db = db.Where("ctime >=?", startime)
  1655. }
  1656. if endtime > 0 {
  1657. db = db.Where("ctime <=?", endtime)
  1658. }
  1659. err = db.Find(&info).Error
  1660. return info, err
  1661. }
  1662. func GetStartGoodWarehouseInfoList(good_id int64, orgid int64, startime int64) (info []*models.StartWarehousingInfo, err error) {
  1663. db := XTReadDB().Model(&info).Where("status = 1 and is_check =1")
  1664. if good_id > 0 {
  1665. db = db.Where("good_id = ?", good_id)
  1666. }
  1667. if orgid > 0 {
  1668. db = db.Where("org_id = ?", orgid)
  1669. }
  1670. if startime > 0 {
  1671. db = db.Where("ctime <?", startime)
  1672. }
  1673. err = db.Find(&info).Error
  1674. return info, err
  1675. }
  1676. func GetEndGoodWarehouseInfoList(good_id int64, orgid int64, endtime int64) (info []*models.StartWarehousingInfo, err error) {
  1677. db := XTReadDB().Model(&info).Where("status = 1 and is_check =1")
  1678. if good_id > 0 {
  1679. db = db.Where("good_id = ?", good_id)
  1680. }
  1681. if orgid > 0 {
  1682. db = db.Where("org_id = ?", orgid)
  1683. }
  1684. if endtime > 0 {
  1685. db = db.Where("ctime <=?", endtime)
  1686. }
  1687. err = db.Find(&info).Error
  1688. return info, err
  1689. }
  1690. func GetAddGoodWarehouseInfoList(good_id int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.StartWarehousingInfo, err error) {
  1691. db := XTReadDB().Model(&info).Where("status = 1 and is_check =1")
  1692. if good_id > 0 {
  1693. db = db.Where("good_id = ?", good_id)
  1694. }
  1695. if orgid > 0 {
  1696. db = db.Where("org_id = ?", orgid)
  1697. }
  1698. if startime > 0 {
  1699. db = db.Where("ctime >=?", startime)
  1700. }
  1701. if endtime > 0 {
  1702. db = db.Where("ctime <=?", endtime)
  1703. }
  1704. if storehouse_id > 0 {
  1705. db = db.Where("storehouse_id = ?", storehouse_id)
  1706. }
  1707. err = db.Find(&info).Error
  1708. return info, err
  1709. }
  1710. func GetAddGoodWarehouseInfoListTwenty(good_id int64, orgid int64, startime int64, endtime int64) (info []*models.StartWarehousingInfo, err error) {
  1711. db := XTReadDB().Model(&info).Where("status = 1 and is_check =1")
  1712. if good_id > 0 {
  1713. db = db.Where("good_id = ?", good_id)
  1714. }
  1715. if orgid > 0 {
  1716. db = db.Where("org_id = ?", orgid)
  1717. }
  1718. if startime > 0 {
  1719. db = db.Where("ctime >=?", startime)
  1720. }
  1721. if endtime > 0 {
  1722. db = db.Where("ctime <=?", endtime)
  1723. }
  1724. err = db.Find(&info).Error
  1725. return info, err
  1726. }
  1727. func GetAddStartFlow(good_id int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.VmStockFlowTwenty, err error) {
  1728. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type =1 or consumable_type = 13)")
  1729. if good_id > 0 {
  1730. db = db.Where("good_id = ?", good_id)
  1731. }
  1732. if orgid > 0 {
  1733. db = db.Where("user_org_id = ?", orgid)
  1734. }
  1735. if startime > 0 {
  1736. db = db.Where("ctime >=?", startime)
  1737. }
  1738. if endtime > 0 {
  1739. db = db.Where("ctime <=?", endtime)
  1740. }
  1741. if storehouse_id > 0 {
  1742. db = db.Where("storehouse_id =?", storehouse_id)
  1743. }
  1744. err = db.Find(&info).Error
  1745. return info, err
  1746. }
  1747. func GetAddStartFlowTwenty(good_id int64, orgid int64, startime int64, endtime int64) (info []*models.VmStockFlowTwenty, err error) {
  1748. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type =1)")
  1749. if good_id > 0 {
  1750. db = db.Where("good_id = ?", good_id)
  1751. }
  1752. if orgid > 0 {
  1753. db = db.Where("user_org_id = ?", orgid)
  1754. }
  1755. if startime > 0 {
  1756. db = db.Where("ctime >=?", startime)
  1757. }
  1758. if endtime > 0 {
  1759. db = db.Where("ctime <=?", endtime)
  1760. }
  1761. err = db.Find(&info).Error
  1762. return info, err
  1763. }
  1764. func GetReduceWarehouseInfoList(good_id int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  1765. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type= 15)")
  1766. if good_id > 0 {
  1767. db = db.Where("good_id = ?", good_id)
  1768. }
  1769. if orgid > 0 {
  1770. db = db.Where("user_org_id = ?", orgid)
  1771. }
  1772. if startime > 0 {
  1773. db = db.Where("ctime >=?", startime)
  1774. }
  1775. if endtime > 0 {
  1776. db = db.Where("ctime <=?", endtime)
  1777. }
  1778. if storehouse_id > 0 {
  1779. db = db.Where("storehouse_id = ?", storehouse_id)
  1780. }
  1781. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  1782. return outinfo, err
  1783. }
  1784. func GetReduceWarehouseInfoListTwenty(good_id int64, orgid int64, startime int64, endtime int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  1785. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type= 15)")
  1786. if good_id > 0 {
  1787. db = db.Where("good_id = ?", good_id)
  1788. }
  1789. if orgid > 0 {
  1790. db = db.Where("user_org_id = ?", orgid)
  1791. }
  1792. if startime > 0 {
  1793. db = db.Where("ctime >=?", startime)
  1794. }
  1795. if endtime > 0 {
  1796. db = db.Where("ctime <=?", endtime)
  1797. }
  1798. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  1799. return outinfo, err
  1800. }
  1801. func GetStartEndCancelFLowInfo(good_id int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  1802. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 7)")
  1803. if good_id > 0 {
  1804. db = db.Where("good_id = ?", good_id)
  1805. }
  1806. if orgid > 0 {
  1807. db = db.Where("user_org_id = ?", orgid)
  1808. }
  1809. if startime > 0 {
  1810. db = db.Where("ctime >=?", startime)
  1811. }
  1812. if endtime > 0 {
  1813. db = db.Where("ctime <=?", endtime)
  1814. }
  1815. if storehouse_id > 0 {
  1816. db = db.Where("storehouse_id =?", storehouse_id)
  1817. }
  1818. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  1819. return outinfo, err
  1820. }
  1821. func GetStartEndCancelFLowInfoTwenty(good_id int64, orgid int64, startime int64, endtime int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  1822. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 7)")
  1823. if good_id > 0 {
  1824. db = db.Where("good_id = ?", good_id)
  1825. }
  1826. if orgid > 0 {
  1827. db = db.Where("user_org_id = ?", orgid)
  1828. }
  1829. if startime > 0 {
  1830. db = db.Where("ctime >=?", startime)
  1831. }
  1832. if endtime > 0 {
  1833. db = db.Where("ctime <=?", endtime)
  1834. }
  1835. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  1836. return outinfo, err
  1837. }
  1838. func FindeStartFlowInfo(good_id int64, orgid int64, startime int64) (info []*models.VmStockFlowTwenty, err error) {
  1839. db := XTReadDB().Model(&info).Where("status = 1 and consumable_type = 1")
  1840. if good_id > 0 {
  1841. db = db.Where("good_id = ?", good_id)
  1842. }
  1843. if orgid > 0 {
  1844. db = db.Where("user_org_id = ?", orgid)
  1845. }
  1846. if startime > 0 {
  1847. db = db.Where("ctime <?", startime)
  1848. }
  1849. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  1850. return info, err
  1851. }
  1852. func FindeStartFlowOutInfo(good_id int64, orgid int64, startime int64) (info []*models.VmStockFlowTwenty, err error) {
  1853. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 2 or consumable_type = 3)")
  1854. if good_id > 0 {
  1855. db = db.Where("good_id = ?", good_id)
  1856. }
  1857. if orgid > 0 {
  1858. db = db.Where("user_org_id = ?", orgid)
  1859. }
  1860. if startime > 0 {
  1861. db = db.Where("ctime <?", startime)
  1862. }
  1863. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  1864. return info, err
  1865. }
  1866. func FindeStartCancelInfo(good_id int64, orgid int64, startime int64) (info []*models.VmStockFlowTwenty, err error) {
  1867. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 4 or consumable_type = 7)")
  1868. if good_id > 0 {
  1869. db = db.Where("good_id = ?", good_id)
  1870. }
  1871. if orgid > 0 {
  1872. db = db.Where("user_org_id = ?", orgid)
  1873. }
  1874. if startime > 0 {
  1875. db = db.Where("ctime <?", startime)
  1876. }
  1877. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  1878. return info, err
  1879. }
  1880. func FindEndStartFlowInfo(good_id int64, orgid int64, endtime int64) (info []*models.VmStockFlowTwenty, err error) {
  1881. db := XTReadDB().Model(&info).Where("status = 1 and consumable_type = 1")
  1882. if good_id > 0 {
  1883. db = db.Where("good_id = ?", good_id)
  1884. }
  1885. if orgid > 0 {
  1886. db = db.Where("user_org_id = ?", orgid)
  1887. }
  1888. if endtime > 0 {
  1889. db = db.Where("ctime <=?", endtime)
  1890. }
  1891. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  1892. return info, err
  1893. }
  1894. func FindeEndFLowOutInfo(good_id int64, orgid int64, endtime int64) (info []*models.VmStockFlowTwenty, err error) {
  1895. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 2 or consumable_type = 3)")
  1896. if good_id > 0 {
  1897. db = db.Where("good_id = ?", good_id)
  1898. }
  1899. if orgid > 0 {
  1900. db = db.Where("user_org_id = ?", orgid)
  1901. }
  1902. if endtime > 0 {
  1903. db = db.Where("ctime <?", endtime)
  1904. }
  1905. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  1906. return info, err
  1907. }
  1908. func FindEndCancelInfo(good_id int64, orgid int64, endtime int64) (info []*models.VmStockFlowTwenty, err error) {
  1909. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 4 or consumable_type = 7)")
  1910. if good_id > 0 {
  1911. db = db.Where("good_id = ?", good_id)
  1912. }
  1913. if orgid > 0 {
  1914. db = db.Where("user_org_id = ?", orgid)
  1915. }
  1916. if endtime > 0 {
  1917. db = db.Where("ctime <?", endtime)
  1918. }
  1919. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  1920. return info, err
  1921. }
  1922. func FindeWarehouseOutInfo(good_id int64, orgid int64, startime int64, storehouse_id int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  1923. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15)")
  1924. if good_id > 0 {
  1925. db = db.Where("good_id = ?", good_id)
  1926. }
  1927. if orgid > 0 {
  1928. db = db.Where("user_org_id = ?", orgid)
  1929. }
  1930. if startime > 0 {
  1931. db = db.Where("ctime <?", startime)
  1932. }
  1933. if storehouse_id > 0 {
  1934. db = db.Where("storehouse_id= ?", storehouse_id)
  1935. }
  1936. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  1937. return outinfo, err
  1938. }
  1939. func FindeWarehouseOutInfoTwenty(good_id int64, orgid int64, startime int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  1940. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15)")
  1941. if good_id > 0 {
  1942. db = db.Where("good_id = ?", good_id)
  1943. }
  1944. if orgid > 0 {
  1945. db = db.Where("user_org_id = ?", orgid)
  1946. }
  1947. if startime > 0 {
  1948. db = db.Where("ctime <?", startime)
  1949. }
  1950. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  1951. return outinfo, err
  1952. }
  1953. func FindeStartStockInventoryProfit(good_id int64, orgid int64, startime int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  1954. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 10)")
  1955. if good_id > 0 {
  1956. db = db.Where("good_id = ?", good_id)
  1957. }
  1958. if orgid > 0 {
  1959. db = db.Where("user_org_id = ?", orgid)
  1960. }
  1961. if startime > 0 {
  1962. db = db.Where("ctime <?", startime)
  1963. }
  1964. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  1965. return outinfo, err
  1966. }
  1967. func FindeEndStokInventoryProfit(good_id int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  1968. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 10)")
  1969. if good_id > 0 {
  1970. db = db.Where("good_id = ?", good_id)
  1971. }
  1972. if orgid > 0 {
  1973. db = db.Where("user_org_id = ?", orgid)
  1974. }
  1975. if startime > 0 {
  1976. db = db.Where("ctime >=?", startime)
  1977. }
  1978. if endtime > 0 {
  1979. db = db.Where("ctime <=?", endtime)
  1980. }
  1981. if storehouse_id > 0 {
  1982. db = db.Where("storehouse_id = ?", storehouse_id)
  1983. }
  1984. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  1985. return outinfo, err
  1986. }
  1987. func FindeEndStokInventoryProfitTwenty(good_id int64, orgid int64, startime int64, endtime int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  1988. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 10)")
  1989. if good_id > 0 {
  1990. db = db.Where("good_id = ?", good_id)
  1991. }
  1992. if orgid > 0 {
  1993. db = db.Where("user_org_id = ?", orgid)
  1994. }
  1995. if startime > 0 {
  1996. db = db.Where("ctime >=?", startime)
  1997. }
  1998. if endtime > 0 {
  1999. db = db.Where("ctime <=?", endtime)
  2000. }
  2001. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  2002. return outinfo, err
  2003. }
  2004. func FindeEndStockInventoryLosses(good_id int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  2005. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 11)")
  2006. if good_id > 0 {
  2007. db = db.Where("good_id = ?", good_id)
  2008. }
  2009. if orgid > 0 {
  2010. db = db.Where("user_org_id = ?", orgid)
  2011. }
  2012. if startime > 0 {
  2013. db = db.Where("ctime >=?", startime)
  2014. }
  2015. if endtime > 0 {
  2016. db = db.Where("ctime <=?", endtime)
  2017. }
  2018. if storehouse_id > 0 {
  2019. db = db.Where("storehouse_id = ?", storehouse_id)
  2020. }
  2021. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  2022. return outinfo, err
  2023. }
  2024. func FindeEndStockInventoryLossesTwenty(good_id int64, orgid int64, startime int64, endtime int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  2025. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 11)")
  2026. if good_id > 0 {
  2027. db = db.Where("good_id = ?", good_id)
  2028. }
  2029. if orgid > 0 {
  2030. db = db.Where("user_org_id = ?", orgid)
  2031. }
  2032. if startime > 0 {
  2033. db = db.Where("ctime >=?", startime)
  2034. }
  2035. if endtime > 0 {
  2036. db = db.Where("ctime <=?", endtime)
  2037. }
  2038. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  2039. return outinfo, err
  2040. }
  2041. func FindStartStockInventoryLosses(good_id int64, orgid int64, startime int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  2042. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 11)")
  2043. if good_id > 0 {
  2044. db = db.Where("good_id = ?", good_id)
  2045. }
  2046. if orgid > 0 {
  2047. db = db.Where("user_org_id = ?", orgid)
  2048. }
  2049. if startime > 0 {
  2050. db = db.Where("ctime <?", startime)
  2051. }
  2052. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  2053. return outinfo, err
  2054. }
  2055. func GetDrugWarehouseInfoStart(drug_id int64, orgid int64, startTime int64) (info []*models.DrugFlowTwenty, err error) {
  2056. db := XTReadDB().Model(&info).Where("status = 1 and consumable_type=1 ")
  2057. if drug_id > 0 {
  2058. db = db.Where("drug_id = ?", drug_id)
  2059. }
  2060. if orgid > 0 {
  2061. db = db.Where("user_org_id = ?", orgid)
  2062. }
  2063. if startTime > 0 {
  2064. db = db.Where("ctime<?", startTime)
  2065. }
  2066. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", orgid).Find(&info).Error
  2067. return info, err
  2068. }
  2069. func DrugWarehouseInfoEnd(drug_id int64, orgid int64, endTime int64) (info []*models.DrugFlowTwenty, err error) {
  2070. db := XTReadDB().Model(&info).Where("status = 1 and consumable_type = 1")
  2071. if drug_id > 0 {
  2072. db = db.Where("drug_id = ?", drug_id)
  2073. }
  2074. if orgid > 0 {
  2075. db = db.Where("user_org_id = ?", orgid)
  2076. }
  2077. if endTime > 0 {
  2078. db = db.Where("ctime<=?", endTime)
  2079. }
  2080. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", orgid).Find(&info).Error
  2081. return info, err
  2082. }
  2083. func DrugWarehouseOutInfoEnd(drug_id int64, orgid int64, endTime int64) (info []*models.DrugFlowTwenty, err error) {
  2084. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 2 or consumable_type= 3 or consumable_type= 15)")
  2085. if drug_id > 0 {
  2086. db = db.Where("drug_id = ?", drug_id)
  2087. }
  2088. if orgid > 0 {
  2089. db = db.Where("user_org_id = ?", orgid)
  2090. }
  2091. if endTime > 0 {
  2092. db = db.Where("ctime<=?", endTime)
  2093. }
  2094. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", orgid).Find(&info).Error
  2095. return info, err
  2096. }
  2097. func GetEndDrugFlowCancelInfo(drug_id int64, orgid int64, endTime int64) (info []*models.DrugFlowTwenty, err error) {
  2098. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 4 or consumable_type= 7)")
  2099. if drug_id > 0 {
  2100. db = db.Where("drug_id = ?", drug_id)
  2101. }
  2102. if orgid > 0 {
  2103. db = db.Where("user_org_id = ?", orgid)
  2104. }
  2105. if endTime > 0 {
  2106. db = db.Where("ctime<=?", endTime)
  2107. }
  2108. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", orgid).Find(&info).Error
  2109. return info, err
  2110. }
  2111. func GetDrugWarehouseStartEnd(drug_id int64, orgid int64, startTime int64, endTime int64) (info []*models.DrugFlowTwenty, err error) {
  2112. db := XTReadDB().Model(&info).Where("status = 1 and consumable_type = 1")
  2113. if drug_id > 0 {
  2114. db = db.Where("drug_id = ?", drug_id)
  2115. }
  2116. if orgid > 0 {
  2117. db = db.Where("user_org_id = ?", orgid)
  2118. }
  2119. if startTime > 0 {
  2120. db = db.Where("ctime>=?", startTime)
  2121. }
  2122. if endTime > 0 {
  2123. db = db.Where("ctime<=?", endTime)
  2124. }
  2125. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", orgid).Find(&info).Error
  2126. return info, err
  2127. }
  2128. func FindeDrugWarehouseOutInfo(drug_id int64, orgid int64, startTime int64, endTime int64) (info []*models.DrugFlowTwenty, err error) {
  2129. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 2 or consumable_type= 3 or consumable_type= 15 )")
  2130. if drug_id > 0 {
  2131. db = db.Where("drug_id = ?", drug_id)
  2132. }
  2133. if orgid > 0 {
  2134. db = db.Where("user_org_id = ?", orgid)
  2135. }
  2136. if startTime > 0 {
  2137. db = db.Where("ctime>=?", startTime)
  2138. }
  2139. if endTime > 0 {
  2140. db = db.Where("ctime<=?", endTime)
  2141. }
  2142. err = db.Preload("DrugWarehouseInfoOne", "status= 1 and org_id = ?", orgid).Find(&info).Error
  2143. return info, err
  2144. }
  2145. func FindStartEndDrugWarehouseOutInfo(drug_id int64, orgid int64, startTime int64, endTime int64) (info []*models.DrugFlowTwenty, err error) {
  2146. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type= 7)")
  2147. if drug_id > 0 {
  2148. db = db.Where("drug_id = ?", drug_id)
  2149. }
  2150. if orgid > 0 {
  2151. db = db.Where("user_org_id = ?", orgid)
  2152. }
  2153. if startTime > 0 {
  2154. db = db.Where("ctime>=?", startTime)
  2155. }
  2156. if endTime > 0 {
  2157. db = db.Where("ctime<=?", endTime)
  2158. }
  2159. err = db.Preload("DrugWarehouseInfoOne", "status= 1 and org_id = ?", orgid).Find(&info).Error
  2160. return info, err
  2161. }
  2162. func FindeDrugWarehouseOutStart(drug_id int64, orgid int64, startTime int64) (info []*models.DrugFlow, err error) {
  2163. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 2 or consumable_type= 3)")
  2164. if drug_id > 0 {
  2165. db = db.Where("drug_id = ?", drug_id)
  2166. }
  2167. if orgid > 0 {
  2168. db = db.Where("user_org_id = ?", orgid)
  2169. }
  2170. if startTime > 0 {
  2171. db = db.Where("ctime <?", startTime)
  2172. }
  2173. err = db.Preload("DrugWarehouseInfoOne", "status= 1 and org_id = ?", orgid).Find(&info).Error
  2174. return info, err
  2175. }
  2176. func GetHisPrescriptionProjectByOrgId(orgid int64) (project []*models.HisPrescriptionProject, err error) {
  2177. err = XTReadDB().Where("user_org_id = ? and status = 0 and type = 3", orgid).Find(&project).Error
  2178. return project, err
  2179. }
  2180. func GetPatientStockFlow(patient_id int64, record_date int64, good_id int64) (models.VmStockFlow, error) {
  2181. flow := models.VmStockFlow{}
  2182. err = XTReadDB().Where("patient_id = ? and system_time = ? and good_id = ?", patient_id, record_date, good_id).Find(&flow).Error
  2183. return flow, err
  2184. }
  2185. func ModifyPatientStockFlow(id int64, flow models.VmStockFlow) error {
  2186. err = XTReadDB().Model(&models.VmStockFlow{}).Where("id = ?", id).Updates(map[string]interface{}{"count": flow.Count, "status": 1, "is_read": 1}).Error
  2187. return err
  2188. }
  2189. func GetDrugFlowSart(drug_id int64, user_org_id int64, ctime int64) (flow []*models.DrugFlow, err error) {
  2190. db := XTReadDB().Model(&flow).Where("status = 1 and consumable_type = 1")
  2191. if drug_id > 0 {
  2192. db = db.Where("drug_id = ?", drug_id)
  2193. }
  2194. if user_org_id > 0 {
  2195. db = db.Where("user_org_id = ?", user_org_id)
  2196. }
  2197. if ctime > 0 {
  2198. db = db.Where("ctime <?", ctime)
  2199. }
  2200. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", user_org_id).Find(&flow).Error
  2201. return flow, err
  2202. }
  2203. func GetDrugFlowOut(drug_id int64, user_org_id int64, ctime int64) (flow []*models.DrugFlowTwenty, err error) {
  2204. db := XTReadDB().Model(&flow).Where("status = 1 and (consumable_type = 2 or consumable_type= 3 or consumable_type= 15)")
  2205. if drug_id > 0 {
  2206. db = db.Where("drug_id = ?", drug_id)
  2207. }
  2208. if user_org_id > 0 {
  2209. db = db.Where("user_org_id = ?", user_org_id)
  2210. }
  2211. if ctime > 0 {
  2212. db = db.Where("ctime <?", ctime)
  2213. }
  2214. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", user_org_id).Find(&flow).Error
  2215. return flow, err
  2216. }
  2217. func GetStartDrugFlowCancelInfo(drug_id int64, user_org_id int64, ctime int64) (flow []*models.DrugFlowTwenty, err error) {
  2218. db := XTReadDB().Model(&flow).Where("status = 1 and (consumable_type = 4 or consumable_type = 7)")
  2219. if drug_id > 0 {
  2220. db = db.Where("drug_id = ?", drug_id)
  2221. }
  2222. if user_org_id > 0 {
  2223. db = db.Where("user_org_id = ?", user_org_id)
  2224. }
  2225. if ctime > 0 {
  2226. db = db.Where("ctime <?", ctime)
  2227. }
  2228. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", user_org_id).Find(&flow).Error
  2229. return flow, err
  2230. }
  2231. func GetDrugFlowStartProfit(drug_id int64, user_org_id int64, ctime int64) (flow []*models.DrugFlowTwenty, err error) {
  2232. db := XTReadDB().Model(&flow).Where("status = 1 and consumable_type = 10")
  2233. if drug_id > 0 {
  2234. db = db.Where("drug_id = ?", drug_id)
  2235. }
  2236. if user_org_id > 0 {
  2237. db = db.Where("user_org_id = ?", user_org_id)
  2238. }
  2239. if ctime > 0 {
  2240. db = db.Where("ctime <?", ctime)
  2241. }
  2242. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", user_org_id).Find(&flow).Error
  2243. return flow, err
  2244. }
  2245. func GetDrugFlowStartLosses(drug_id int64, user_org_id int64, ctime int64) (flow []*models.DrugFlowTwenty, err error) {
  2246. db := XTReadDB().Model(&flow).Where("status = 1 and consumable_type = 11 ")
  2247. if drug_id > 0 {
  2248. db = db.Where("drug_id = ?", drug_id)
  2249. }
  2250. if user_org_id > 0 {
  2251. db = db.Where("user_org_id = ?", user_org_id)
  2252. }
  2253. if ctime > 0 {
  2254. db = db.Where("ctime <?", ctime)
  2255. }
  2256. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", user_org_id).Find(&flow).Error
  2257. return flow, err
  2258. }
  2259. func GetDrugFlowEndProfit(drug_id int64, user_org_id int64, endtime int64) (flow []*models.DrugFlowTwenty, err error) {
  2260. db := XTReadDB().Model(&flow).Where("status = 1 and consumable_type = 10")
  2261. if drug_id > 0 {
  2262. db = db.Where("drug_id = ?", drug_id)
  2263. }
  2264. if user_org_id > 0 {
  2265. db = db.Where("user_org_id = ?", user_org_id)
  2266. }
  2267. if endtime > 0 {
  2268. db = db.Where("ctime <=?", endtime)
  2269. }
  2270. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", user_org_id).Find(&flow).Error
  2271. return flow, err
  2272. }
  2273. func GetDrugFlowEndLosses(drug_id int64, user_org_id int64, endtime int64) (flow []*models.DrugFlowTwenty, err error) {
  2274. db := XTReadDB().Model(&flow).Where("status = 1 and consumable_type = 11")
  2275. if drug_id > 0 {
  2276. db = db.Where("drug_id = ?", drug_id)
  2277. }
  2278. if user_org_id > 0 {
  2279. db = db.Where("user_org_id = ?", user_org_id)
  2280. }
  2281. if endtime > 0 {
  2282. db = db.Where("ctime <=?", endtime)
  2283. }
  2284. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", user_org_id).Find(&flow).Error
  2285. return flow, err
  2286. }
  2287. func GetDrugFlowStartEndProfit(drug_id int64, user_org_id int64, startime int64, endtime int64) (flow []*models.DrugFlowTwenty, err error) {
  2288. db := XTReadDB().Model(&flow).Where("status = 1 and consumable_type = 10 ")
  2289. if drug_id > 0 {
  2290. db = db.Where("drug_id = ?", drug_id)
  2291. }
  2292. if user_org_id > 0 {
  2293. db = db.Where("user_org_id = ?", user_org_id)
  2294. }
  2295. if startime > 0 {
  2296. db = db.Where("ctime >=?", startime)
  2297. }
  2298. if endtime > 0 {
  2299. db = db.Where("ctime<=?", endtime)
  2300. }
  2301. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", user_org_id).Find(&flow).Error
  2302. return flow, err
  2303. }
  2304. func GetDrugFlowStartEndLosses(drug_id int64, user_org_id int64, startime int64, endtime int64) (flow []*models.DrugFlowTwenty, err error) {
  2305. db := XTReadDB().Model(&flow).Where("status = 1 and consumable_type = 11 ")
  2306. if drug_id > 0 {
  2307. db = db.Where("drug_id = ?", drug_id)
  2308. }
  2309. if user_org_id > 0 {
  2310. db = db.Where("user_org_id = ?", user_org_id)
  2311. }
  2312. if startime > 0 {
  2313. db = db.Where("ctime >=?", startime)
  2314. }
  2315. if endtime > 0 {
  2316. db = db.Where("ctime<=?", endtime)
  2317. }
  2318. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", user_org_id).Find(&flow).Error
  2319. return flow, err
  2320. }
  2321. func GetDrugWarehouseInfoStorehouseList(orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  2322. err = XTReadDB().Where("org_id = ? and is_check= 1 and status = 1", orgid).Group("drug_id,storehouse_id").Find(&info).Error
  2323. return info, err
  2324. }
  2325. func GetDrugStockCout(drug_id int64, storehouse_id int64, user_org_id int64) (models.XtDrugStockCount, error) {
  2326. drugCount := models.XtDrugStockCount{}
  2327. err := XTReadDB().Where("drug_id = ? and storehouse_id = ? and user_org_id = ? and is_check =1 and status = 1", drug_id, storehouse_id, user_org_id).Find(&drugCount).Error
  2328. return drugCount, err
  2329. }
  2330. func GetDrugWarehouseInfoByStorehouseId(storehouse_id int64, drug_id int64, org_id int64) (info []*models.DrugWarehouseInfo, err error) {
  2331. 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
  2332. return info, err
  2333. }
  2334. func GetDrugFlowStockOutCount(storehouse_id int64, drug_id int64, org_id int64) (flow []*models.DrugFlow, err error) {
  2335. 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
  2336. return flow, err
  2337. }
  2338. func GetDrugFlowStockCancelCount(storehouse_id int64, drug_id int64, org_id int64) (flow []*models.DrugFlow, err error) {
  2339. 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
  2340. return flow, err
  2341. }
  2342. func CreateDrugStockCount(stockCount models.XtDrugStockCount) error {
  2343. err := XTWriteDB().Create(&stockCount).Error
  2344. return err
  2345. }
  2346. func GetGoodNewPurchaseStockQuery(good_type int64, keyword string, page int64, limit int64, orgid int64, ids []int64, goodIds []int64) (goodinfo []*models.GoodInfoTwenty, total int64, err error) {
  2347. db := XTReadDB().Model(&goodinfo).Where("status = 1 and total_count > 0")
  2348. offset := (page - 1) * limit
  2349. likeKey := "%" + keyword + "%"
  2350. if good_type > 0 {
  2351. db = db.Where("good_type_id = ?", good_type)
  2352. }
  2353. if orgid > 0 {
  2354. db = db.Where("org_id = ?", orgid)
  2355. }
  2356. if len(ids) > 0 {
  2357. db = db.Where("id in(?)", goodIds)
  2358. }
  2359. if len(keyword) > 0 {
  2360. db = db.Where("good_name like ? or manufacturer in(?)", likeKey, ids)
  2361. }
  2362. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&goodinfo).Error
  2363. return goodinfo, total, err
  2364. }
  2365. func GetGoodNewPurchaseStockQueryOne(good_type int64, keyword string, page int64, limit int64, orgid int64, ids []int64, goodIds []int64) (goodinfo []*models.GoodInfoTwenty, total int64, err error) {
  2366. db := XTReadDB().Model(&goodinfo).Where("status = 1")
  2367. offset := (page - 1) * limit
  2368. likeKey := "%" + keyword + "%"
  2369. if good_type > 0 {
  2370. db = db.Where("good_type_id = ?", good_type)
  2371. }
  2372. if orgid > 0 {
  2373. db = db.Where("org_id = ?", orgid)
  2374. }
  2375. if len(ids) > 0 {
  2376. db = db.Where("id in(?)", goodIds)
  2377. }
  2378. if len(keyword) > 0 {
  2379. db = db.Where("good_name like ? or manufacturer in(?)", likeKey, ids)
  2380. }
  2381. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&goodinfo).Error
  2382. return goodinfo, total, err
  2383. }
  2384. func GetStartLastFLow(goodid int64, orgid int64, startime int64, storehouse_id int64) (models.VmStockFlowTwenty, error) {
  2385. flwo := models.VmStockFlowTwenty{}
  2386. 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
  2387. return flwo, err
  2388. }
  2389. func GetStartLastFLowTwenty(goodid int64, orgid int64, startime int64) (models.VmStockFlowTwenty, error) {
  2390. flwo := models.VmStockFlowTwenty{}
  2391. err := XTReadDB().Where("good_id = ? and user_org_id = ? and status = 1 and ctime < ?", goodid, orgid, startime).Order("ctime desc").Last(&flwo).Error
  2392. return flwo, err
  2393. }
  2394. func GetEndLastFlow(goodid int64, orgid int64, endtime int64, storehouse_id int64) (models.VmStockFlowTwenty, error) {
  2395. flwo := models.VmStockFlowTwenty{}
  2396. 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
  2397. return flwo, err
  2398. }
  2399. func GetEndLastFlowTwenty(goodid int64, orgid int64, endtime int64) (models.VmStockFlowTwenty, error) {
  2400. flwo := models.VmStockFlowTwenty{}
  2401. err := XTReadDB().Where("good_id = ? and user_org_id = ? and status = 1 and ctime <= ?", goodid, orgid, endtime).Last(&flwo).Error
  2402. return flwo, err
  2403. }
  2404. 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) {
  2405. db := XTReadDB().Model(&druginfo).Where("status = 1")
  2406. offset := (page - 1) * limit
  2407. likeKey := "%" + keyword + "%"
  2408. if good_type > 0 {
  2409. db = db.Where("drug_type = ?", good_type)
  2410. }
  2411. if orgid > 0 {
  2412. db = db.Where("org_id = ?", orgid)
  2413. }
  2414. if len(keyword) > 0 {
  2415. db = db.Where("drug_name like ? or manufacturer in(?)", likeKey, ids)
  2416. }
  2417. if len(infos) > 0 {
  2418. db = db.Where("id in(?)", infos)
  2419. }
  2420. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&druginfo).Error
  2421. return druginfo, total, err
  2422. }
  2423. 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) {
  2424. db := XTReadDB().Model(&druginfo).Where("status = 1")
  2425. offset := (page - 1) * limit
  2426. likeKey := "%" + keyword + "%"
  2427. if good_type > 0 {
  2428. db = db.Where("drug_type = ?", good_type)
  2429. }
  2430. if orgid > 0 {
  2431. db = db.Where("org_id = ?", orgid)
  2432. }
  2433. if len(keyword) > 0 {
  2434. db = db.Where("drug_name like ? or manufacturer in(?)", likeKey, ids)
  2435. }
  2436. if len(infos) > 0 {
  2437. db = db.Where("id in(?)", infos)
  2438. }
  2439. err = db.Count(&total).Offset(offset).Limit(limit).Order("drug_alias desc").Find(&druginfo).Error
  2440. return druginfo, total, err
  2441. }
  2442. func GetDrugStartFlow(drug_id int64, user_org_id int64, startime int64) (models.DrugFlowTwenty, error) {
  2443. flow := models.DrugFlowTwenty{}
  2444. 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
  2445. return flow, err
  2446. }
  2447. func GetDrugEndFlow(drug_id int64, user_org_id int64, endtime int64) (models.DrugFlowTwenty, error) {
  2448. flow := models.DrugFlowTwenty{}
  2449. 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
  2450. return flow, err
  2451. }
  2452. func GetAllStockCount(orgid int64) (stock []*models.XtGoodStockCount, err error) {
  2453. err = XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&stock).Error
  2454. return stock, err
  2455. }
  2456. func GetLastStockFlow(good_id int64) (models.VmStockFlow, error) {
  2457. flow := models.VmStockFlow{}
  2458. err := XTReadDB().Where("good_id = ? and status =1 and ctime<=1667231999", good_id).Last(&flow).Error
  2459. return flow, err
  2460. }
  2461. func ModifyStockFlowById(id int64, over_count int64) error {
  2462. err := XTWriteDB().Model(&models.VmStockFlow{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"over_count": over_count}).Error
  2463. return err
  2464. }
  2465. func GetAllDrugCount(user_org_id int64) (drug []*models.XtDrugStockCount, err error) {
  2466. err = XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&drug).Error
  2467. return drug, err
  2468. }
  2469. func GetLastDrugFlow(drug_id int64) (models.DrugFlow, error) {
  2470. flow := models.DrugFlow{}
  2471. err := XTReadDB().Where("drug_id = ? and status =1 and ctime<=1667231999", drug_id).Last(&flow).Error
  2472. return flow, err
  2473. }
  2474. func ModifyDrugFlowById(id int64, over_count int64) error {
  2475. err := XTWriteDB().Model(&models.DrugFlow{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"over_count": over_count}).Error
  2476. return err
  2477. }
  2478. func GetDrugInventoryWarehouseInfoList(org_id int64, storehouse_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  2479. if org_id == 10138 || org_id == 10278 {
  2480. 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
  2481. return info, err
  2482. } else {
  2483. 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
  2484. return info, err
  2485. }
  2486. }
  2487. func GetGoodStockCountList(user_org_id int64, good_id int64) (stock []*models.XtGoodStockCount, err error) {
  2488. err = XTReadDB().Where("user_org_id =? and status=1 and good_id = ?", user_org_id, good_id).Find(&stock).Error
  2489. return stock, err
  2490. }
  2491. func GetDrugCountMapList(drug_id int64, user_org_id int64) (drug []*models.XtDrugStockCount, err error) {
  2492. err = XTReadDB().Where("drug_id = ? and user_org_id =? and status=1", drug_id, user_org_id).Find(&drug).Error
  2493. return drug, err
  2494. }
  2495. func UpdateDrugStockInCount(drugId int64, storehouseId int64, over_count int64, user_org_id int64) error {
  2496. 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
  2497. return err
  2498. }
  2499. func UpdateDrugStockInCountOne(drugId int64, storehouseId int64, over_count int64, user_org_id int64) error {
  2500. 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
  2501. return err
  2502. }
  2503. func GetPurchaseStockQueryList(good_type int64, keyword string, page int64, limit int64, orgid int64, ids []int64, goodIds []int64) (goodinfo []*models.GoodInfoTwenty, total int64, err error) {
  2504. db := XTReadDB().Model(&goodinfo).Where("status = 1")
  2505. offset := (page - 1) * limit
  2506. likeKey := "%" + keyword + "%"
  2507. if good_type > 0 {
  2508. db = db.Where("good_type_id = ?", good_type)
  2509. }
  2510. if orgid > 0 {
  2511. db = db.Where("org_id = ?", orgid)
  2512. }
  2513. if len(ids) > 0 {
  2514. db = db.Where("id in(?)", goodIds)
  2515. }
  2516. if len(keyword) > 0 {
  2517. db = db.Where("good_name like ? or manufacturer in(?)", likeKey, ids)
  2518. }
  2519. err = db.Count(&total).Offset(offset).Limit(limit).Order("good_number desc").Find(&goodinfo).Error
  2520. return goodinfo, total, err
  2521. }
  2522. func GetInitBalanceList(good_id int64, user_org_id int64, start_time int64, end_time int64, storehouse_id int64) (list []*models.StWarehousingInfoOne, err error) {
  2523. db := XTReadDB().Model(&list).Where("status=1 and is_check=1")
  2524. if good_id > 0 {
  2525. db = db.Where("good_id = ?", good_id)
  2526. }
  2527. if user_org_id > 0 {
  2528. db = db.Where("org_id = ?", user_org_id)
  2529. }
  2530. if start_time > 0 {
  2531. db = db.Where("ctime<?", start_time)
  2532. }
  2533. if storehouse_id > 0 {
  2534. db = db.Where("storehouse_id = ?", storehouse_id)
  2535. }
  2536. err = db.Find(&list).Error
  2537. return list, err
  2538. }
  2539. func GetInitOutBanceList(good_id int64, user_org_id int64, start_time int64, end_time int64, storehouse_id int64) (list []*models.WarehouseOutInfoTenty, err error) {
  2540. db := XTReadDB().Model(&list).Where("status=1")
  2541. if good_id > 0 {
  2542. db = db.Where("good_id = ?", good_id)
  2543. }
  2544. if user_org_id > 0 {
  2545. db = db.Where("org_id = ?", user_org_id)
  2546. }
  2547. if start_time > 0 {
  2548. db = db.Where("ctime<?", start_time)
  2549. }
  2550. if storehouse_id > 0 {
  2551. db = db.Where("storehouse_id = ?", storehouse_id)
  2552. }
  2553. err = db.Group("patient_id,good_id,sys_record_time,is_sys").Find(&list).Error
  2554. return list, err
  2555. }
  2556. func GetInitInfoFlow(good_id int64, user_org_id int64, start_time int64, end_time int64, storehouse_id int64) (list []*models.VmStockFlowTwenty, err error) {
  2557. db := XTReadDB().Model(&list).Where("status=1 and consumable_type =10")
  2558. if good_id > 0 {
  2559. db = db.Where("good_id = ?", good_id)
  2560. }
  2561. if user_org_id > 0 {
  2562. db = db.Where("user_org_id = ?", user_org_id)
  2563. }
  2564. if start_time > 0 {
  2565. db = db.Where("ctime<?", start_time)
  2566. }
  2567. if storehouse_id > 0 {
  2568. db = db.Where("storehouse_id = ?", storehouse_id)
  2569. }
  2570. err = db.Find(&list).Error
  2571. return list, err
  2572. }
  2573. func GetInitOutInfoFlow(good_id int64, user_org_id int64, start_time int64, end_time int64, storehouse_id int64) (list []*models.VmStockFlowTwenty, err error) {
  2574. db := XTReadDB().Model(&list).Where("status=1 and consumable_type =11")
  2575. if good_id > 0 {
  2576. db = db.Where("good_id = ?", good_id)
  2577. }
  2578. if user_org_id > 0 {
  2579. db = db.Where("user_org_id = ?", user_org_id)
  2580. }
  2581. if start_time > 0 {
  2582. db = db.Where("ctime<?", start_time)
  2583. }
  2584. if storehouse_id > 0 {
  2585. db = db.Where("storehouse_id = ?", storehouse_id)
  2586. }
  2587. err = db.Find(&list).Error
  2588. return list, err
  2589. }
  2590. func GetNewDrugFlowInWarehouseInfo(drug_id int64, user_org_id int64, start_time int64) (list []*models.DrugFlowTwenty, err error) {
  2591. db := XTReadDB().Model(&list).Where("status=1 and consumable_type=1")
  2592. if drug_id > 0 {
  2593. db = db.Where("drug_id = ?", drug_id)
  2594. }
  2595. if user_org_id > 0 {
  2596. db = db.Where("user_org_id = ?", user_org_id)
  2597. }
  2598. if start_time > 0 {
  2599. db = db.Where("ctime<?", start_time)
  2600. }
  2601. err = db.Find(&list).Error
  2602. return list, err
  2603. }
  2604. func GetNewDrugFlowPofitWarehouseInfo(drug_id int64, user_org_id int64, start_time int64) (list []*models.DrugFlowTwenty, err error) {
  2605. db := XTReadDB().Model(&list).Where("status=1 and consumable_type=10")
  2606. if drug_id > 0 {
  2607. db = db.Where("drug_id = ?", drug_id)
  2608. }
  2609. if user_org_id > 0 {
  2610. db = db.Where("user_org_id = ?", user_org_id)
  2611. }
  2612. if start_time > 0 {
  2613. db = db.Where("ctime<?", start_time)
  2614. }
  2615. err = db.Find(&list).Error
  2616. return list, err
  2617. }
  2618. func GetNewDrugFlowOutWarehouseInfo(drug_id int64, user_org_id int64, start_time int64) (list []*models.DrugFlowTwenty, err error) {
  2619. db := XTReadDB().Model(&list).Where("status=1 and (consumable_type=3 or consumable_type=2)")
  2620. if drug_id > 0 {
  2621. db = db.Where("drug_id = ?", drug_id)
  2622. }
  2623. if user_org_id > 0 {
  2624. db = db.Where("user_org_id = ?", user_org_id)
  2625. }
  2626. if start_time > 0 {
  2627. db = db.Where("ctime<?", start_time)
  2628. }
  2629. err = db.Find(&list).Error
  2630. return list, err
  2631. }
  2632. func GetNewDrugFlowCacelWarehouseInfo(drug_id int64, user_org_id int64, start_time int64) (list []*models.DrugFlowTwenty, err error) {
  2633. db := XTReadDB().Model(&list).Where("status=1 and consumable_type=7")
  2634. if drug_id > 0 {
  2635. db = db.Where("drug_id = ?", drug_id)
  2636. }
  2637. if user_org_id > 0 {
  2638. db = db.Where("user_org_id = ?", user_org_id)
  2639. }
  2640. if start_time > 0 {
  2641. db = db.Where("ctime<?", start_time)
  2642. }
  2643. err = db.Find(&list).Error
  2644. return list, err
  2645. }
  2646. func GetNewDrugFlowNoPofitWarehouseInfo(drug_id int64, user_org_id int64, start_time int64) (list []*models.DrugFlowTwenty, err error) {
  2647. db := XTReadDB().Model(&list).Where("status=1 and consumable_type=11")
  2648. if drug_id > 0 {
  2649. db = db.Where("drug_id = ?", drug_id)
  2650. }
  2651. if user_org_id > 0 {
  2652. db = db.Where("user_org_id = ?", user_org_id)
  2653. }
  2654. if start_time > 0 {
  2655. db = db.Where("ctime<?", start_time)
  2656. }
  2657. err = db.Find(&list).Error
  2658. return list, err
  2659. }
  2660. func GetHisAdviceGroupList(drug_id int64, user_org_id int64) (advicelist []*models.HisDoctorAdviceInfo, err error) {
  2661. if user_org_id == 10265 {
  2662. 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
  2663. return advicelist, err
  2664. } else {
  2665. err = XTReadDB().Where("drug_id = ? and user_org_id = ? and status=1", drug_id, user_org_id).Order("advice_date asc").Find(&advicelist).Error
  2666. return advicelist, err
  2667. }
  2668. }
  2669. func GetDrugWarehousOutInfo(drug_id int64, org_id int64) (out []*models.XtDrugWarehouseOutInfo, err error) {
  2670. 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
  2671. return out, err
  2672. }
  2673. func GetDrugWarehousOutInfoOne(drug_id int64, org_id int64) (out []*models.XtDrugWarehouseOutInfo, err error) {
  2674. 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
  2675. return out, err
  2676. }
  2677. func GetCheckDrugFlow(drug_id int64, user_org_id int64) (drugflow []*models.DrugFlow, err error) {
  2678. 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
  2679. return drugflow, err
  2680. }
  2681. func GetCheckGoodBatchList(good_id int64, user_org_id int64) (project []*models.HisPrescriptionProject, err error) {
  2682. 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
  2683. return project, err
  2684. }
  2685. func GetCheckGoodWarehouseList(good_id int64, org_id int64) (info []*models.WarehouseOutInfo, err error) {
  2686. 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
  2687. return info, err
  2688. }
  2689. func GetHisAdviceListBySysRrecordTime(drug_id int64, sys_record_time int64, user_org_id int64) (info []*models.HisDoctorAdviceInfo, err error) {
  2690. 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
  2691. return info, err
  2692. }
  2693. func GetDrugWarehouseOutInfoBySysRecordTime(drug_id int64, sys_record_time int64, user_org_id int64) (info []*models.DrugWarehouseOutInfo, err error) {
  2694. 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
  2695. return info, err
  2696. }
  2697. func GetGoodProjectList(good_id int64, sys_record_time int64, user_org_id int64) (project []*models.HisPrescriptionProject, err error) {
  2698. 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
  2699. return project, err
  2700. }
  2701. func GetWarehouseOutListBySysrecordTime(good_id int64, sys_record_time int64, user_org_id int64) (out []*models.WarehouseOutInfo, err error) {
  2702. 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
  2703. return out, err
  2704. }