self_drug_service.go 110KB

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