gobal_config_service.go 46KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. package service
  2. import (
  3. "XT_New/models"
  4. "fmt"
  5. "github.com/jinzhu/gorm"
  6. "time"
  7. )
  8. func CreateAutomaticReduceRecord(config *models.GobalConfig) (err error) {
  9. err = writeDb.Model(&models.GobalConfig{}).Create(config).Error
  10. return
  11. }
  12. func CreateDrugAutomaticReduceRecord(config *models.DrugStockConfig) (err error) {
  13. err = writeDb.Model(&models.DrugStockConfig{}).Create(config).Error
  14. return
  15. }
  16. func FindAutomaticReduceRecordByOrgId(org_id int64) (err error, config models.GobalConfig) {
  17. err = readDb.Model(&models.GobalConfig{}).Where("status = 1 AND org_id = ?", org_id).First(&config).Error
  18. return
  19. }
  20. func FindDrugStockAutomaticReduceRecordByOrgId(org_id int64) (err error, config models.DrugStockConfig) {
  21. err = readDb.Model(&models.DrugStockConfig{}).Where("status = 1 AND org_id = ?", org_id).First(&config).Error
  22. return
  23. }
  24. func UpdateAutomaticReduceRecord(config *models.GobalConfig) (err error) {
  25. err = writeDb.Save(config).Error
  26. return
  27. }
  28. func UpdateDrugStockAutomaticReduceRecord(config *models.DrugStockConfig) (err error) {
  29. err = writeDb.Save(config).Error
  30. return
  31. }
  32. func CreatePrintTemplateRecord(template *models.GobalTemplate) (err error) {
  33. err = writeDb.Model(&models.GobalTemplate{}).Create(template).Error
  34. return
  35. }
  36. func FindPrintTemplateByOrgId(org_id int64) (err error, template models.GobalTemplate) {
  37. err = readDb.Model(&models.GobalTemplate{}).Where("status = 1 AND org_id = ?", org_id).Find(&template).Error
  38. return
  39. }
  40. func UpdatePrintTemplate(template *models.GobalTemplate) (err error) {
  41. err = writeDb.Save(template).Error
  42. return
  43. }
  44. func CreateConfigData(config *models.DataUploadConfig) (err error) {
  45. err = writeDb.Create(config).Error
  46. return
  47. }
  48. func SaveConfigData(config *models.DataUploadConfig) (err error) {
  49. err = writeDb.Save(config).Error
  50. return
  51. }
  52. func GetConfigData(org_id int64, config_type int64) (config models.DataUploadConfig, err error) {
  53. err = readDb.Model(&models.DataUploadConfig{}).Where("status = 1 AND org_id = ? AND config_type = ?", org_id, config_type).First(&config).Error
  54. return
  55. }
  56. func GetDockingStatus(config_type int64, province int64, city int64) (config models.DockingStatus, err error) {
  57. err = readDb.Model(&models.DockingStatus{}).Where("status = 1 AND docking_type = ? AND province_id = ? AND city_id = ?", config_type, province, city).First(&config).Error
  58. return
  59. }
  60. func GetConfigDataById(id int64) (config models.DataUploadConfig, err error) {
  61. err = readDb.Model(&models.DataUploadConfig{}).Where("id = ?", id).First(&config).Error
  62. return
  63. }
  64. func FindDoctorAdviceRecordByOrgId(org_id int64) (err error, config models.DoctorAdviceConfig) {
  65. err = readDb.Model(&models.DoctorAdviceConfig{}).Where("status = 1 AND user_org_id = ?", org_id).Find(&config).Error
  66. return
  67. }
  68. func CreateDoctorAdviceRecord(config *models.DoctorAdviceConfig) (err error) {
  69. err = writeDb.Model(&models.DoctorAdviceConfig{}).Create(config).Error
  70. return
  71. }
  72. func UpdateDoctorAdviceRecord(config *models.DoctorAdviceConfig) (err error) {
  73. err = writeDb.Save(config).Error
  74. return
  75. }
  76. func UpdateFiledConfig(org_id int64) (err error) {
  77. err = writeDb.Model(&models.FiledConfig{}).Where("org_id = ? AND sys_module = 0", org_id).Updates(map[string]interface{}{"is_show": 1}).Error
  78. return
  79. }
  80. func FindAllHideFiledConfig(template_id int64) (err error, config []*models.FiledConfig) {
  81. err = readDb.Model(&models.FiledConfig{}).Where("sys_module = ? AND is_show = 2", template_id).Find(&config).Error
  82. return
  83. }
  84. func UpdateShowFieldConfig(org_id int64, fileds []string) (err error) {
  85. err = writeDb.Model(&models.FiledConfig{}).Where("org_id = ? AND filed_name in (?) AND sys_module = 0 ", org_id, fileds).Updates(map[string]interface{}{"is_show": 2}).Error
  86. return
  87. }
  88. func FindAllAdviceParentTemplate(org_id int64) (template []*models.DoctorAdviceParentTemplate) {
  89. readDb.Model(&models.DoctorAdviceParentTemplate{}).Preload("DoctorAdviceTemplate", func(db *gorm.DB) *gorm.DB {
  90. return db.Preload("SubDoctorAdviceTemplate", "status = 1 AND org_id = ?", org_id).Where("status = 1 AND parent_id = 0 AND org_id = ?", org_id)
  91. }).Where("status = 1 AND org_id = ? ", org_id).Find(&template)
  92. return
  93. }
  94. func CreateDoctorParentTemplate(template *models.DoctorAdviceParentTemplate) (err error) {
  95. err = writeDb.Create(&template).Error
  96. return
  97. }
  98. func FindAllAdviceTemplates(org_id int64, parent_template_id int64) (template []*models.DoctorAdviceTemplate, err error) {
  99. err = readDb.Model(&models.DoctorAdviceTemplate{}).Where("status = 1 AND org_id = ? AND template_id = ?", org_id, parent_template_id).Find(&template).Error
  100. return
  101. }
  102. func CreateDoctorTemplate(template *models.DoctorAdviceTemplate) (err error) {
  103. err = writeDb.Create(&template).Error
  104. return
  105. }
  106. func CreateSystemDialysisSolution(solution *models.SystemPrescription) (err error) {
  107. err = writeDb.Create(solution).Error
  108. return
  109. }
  110. func UpdateSystemDialysisSolution(solution *models.SystemPrescription) (err error) {
  111. err = writeDb.Save(solution).Error
  112. return
  113. }
  114. func FindSystemDialysisSolution(orgID int64, id int64) (solution models.SystemPrescription, err error) {
  115. err = readDb.Model(&models.SystemPrescription{}).Where("id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error
  116. return
  117. }
  118. func FindAllSystemPrescription(orgID int64) (solution []*models.SystemPrescription, err error) {
  119. err = readDb.Model(&models.SystemPrescription{}).Where("status=1 and user_org_id=?", orgID).Find(&solution).Error
  120. return
  121. }
  122. func FindSystemDialysisPrescriptionByMode(orgID int64, id int64) (solution models.SystemPrescription, err error) {
  123. err = readDb.Model(&models.SystemPrescription{}).Where("mode_id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error
  124. return
  125. }
  126. func CreateAdviceInitConfig(adviceInit *models.AdviceInit) (err error) {
  127. err = writeDb.Create(&adviceInit).Error
  128. return
  129. }
  130. func FindAdviceInitConfig(org_id int64) (adviceInit models.AdviceInit, err error) {
  131. err = readDb.Model(&models.AdviceInit{}).Where("user_org_id = ? AND status = 1 ", org_id).First(&adviceInit).Error
  132. return
  133. }
  134. func GetPrint(ids []int64, orgid int64, advicetype int64, stoptype int64) (doctor []*models.DoctorAdvice, err error) {
  135. db := XTReadDB().Model(&doctor).Where("status =1")
  136. if len(ids) == 1 {
  137. if orgid > 0 {
  138. db = db.Where("user_org_id = ?", orgid)
  139. }
  140. if advicetype > 0 {
  141. db = db.Where("advice_type = ?", advicetype)
  142. }
  143. if stoptype > 0 {
  144. db = db.Where("stop_state = ?", stoptype)
  145. }
  146. err = db.Where("groupno = ?", ids[0]).Find(&doctor).Error
  147. } else {
  148. if orgid > 0 {
  149. db = db.Where("user_org_id = ?", orgid)
  150. }
  151. if advicetype > 0 {
  152. db = db.Where("advice_type = ?", advicetype)
  153. }
  154. if stoptype > 0 {
  155. db = db.Where("stop_state = ?", stoptype)
  156. }
  157. err = db.Where("groupno IN(?)", ids).Find(&doctor).Error
  158. }
  159. return doctor, err
  160. }
  161. func GetExportLogByType(org_id int64, log_type int64) (log []*models.ExportLog, err error) {
  162. err = readDb.Model(&models.ExportLog{}).Where("user_org_id = ? AND status = 1 AND log_type = ?", org_id, log_type).
  163. Preload("ExportErrLog", "status = 1").Order("export_time desc").Find(&log).Error
  164. return
  165. }
  166. func FindXTHisRecordByOrgId(org_id int64) (err error, config models.XtHisConfig) {
  167. err = readDb.Model(&models.XtHisConfig{}).Where("status = 1 AND user_org_id = ?", org_id).Find(&config).Error
  168. return
  169. }
  170. func FindHisStockPriceRecordByOrgId(org_id int64) (err error, config models.HisStockPriceConfig) {
  171. err = readDb.Model(&models.HisStockPriceConfig{}).Where("status = 1 AND user_org_id = ?", org_id).Find(&config).Error
  172. return
  173. }
  174. func UpdateXTHisRecord(config *models.XtHisConfig) (err error) {
  175. err = writeDb.Save(config).Error
  176. return
  177. }
  178. func CreateXTHisRecord(config *models.XtHisConfig) (err error) {
  179. err = writeDb.Model(&models.XtHisConfig{}).Create(config).Error
  180. return
  181. }
  182. func CreateHisStockPriceRecord(config *models.HisStockPriceConfig) (err error) {
  183. err = writeDb.Model(&models.HisStockPriceConfig{}).Create(config).Error
  184. return
  185. }
  186. func UpdateHisStockPriceRecord(config *models.HisStockPriceConfig) (err error) {
  187. err = writeDb.Save(config).Error
  188. return
  189. }
  190. //TODO:项目开关
  191. func FindXTHisProjectByOrgId(org_id int64) (err error, config models.XtHisProjectConfig) {
  192. err = readDb.Model(&models.XtHisProjectConfig{}).Where("status = 1 AND user_org_id = ?", org_id).Find(&config).Error
  193. return
  194. }
  195. func FindXTHisProjectByOrgIdOne(org_id int64) (err error, config models.XtHisProjectConfig) {
  196. err = readDb.Model(&models.XtHisProjectConfig{}).Where("status = 1 AND user_org_id = ?", org_id).Find(&config).Error
  197. return
  198. }
  199. func UpdateXTHisProjectRecord(config *models.XtHisProjectConfig) (err error) {
  200. err = writeDb.Save(config).Error
  201. return
  202. }
  203. func CreateXTHisProjectRecord(config *models.XtHisProjectConfig) (err error) {
  204. err = writeDb.Model(&models.XtHisProjectConfig{}).Create(config).Error
  205. return
  206. }
  207. func GetExportHisOrderList(user_org_id int64, start_time int64, end_time int64, p_type int64) (order []*models.HisOrder, err error) {
  208. db := readDb.Model(&models.HisOrder{})
  209. if start_time != 0 {
  210. db = db.Where("his_order.settle_accounts_date>=?", start_time)
  211. }
  212. if end_time != 0 {
  213. db = db.Where("his_order.settle_accounts_date<=?", end_time)
  214. }
  215. if p_type > 0 {
  216. db = db.Where("his_order.status = 1 AND his_order.user_org_id = ? AND his_order.order_status = 2 AND p_type = ? ", user_org_id, p_type)
  217. } else {
  218. db = db.Where("his_order.status = 1 AND his_order.user_org_id = ? AND his_order.order_status = 2", user_org_id)
  219. }
  220. db = db.Preload("HisOrderInfo", "status = 1 AND user_org_id = ?", user_org_id).
  221. Preload("Patients", "status = 1 AND user_org_id = ?", user_org_id).
  222. Preload("HisPatient", "status = 1 AND user_org_id = ?", user_org_id).
  223. Preload("HisHospitalCheckRecord", "status = 1 AND user_org_id = ? AND in_hospital_status = 1 AND out_hospital_status = 1 ", user_org_id).
  224. Preload("HisPrescriptionInfo", func(db *gorm.DB) *gorm.DB {
  225. return db.Where("status = 1 AND user_org_id = ?", user_org_id).Preload("XtHisDepartment", "status = 1")
  226. })
  227. err = db.Order("setl_time asc").Find(&order).Error
  228. return
  229. }
  230. func GetDrugInOrderDetail(startime int64, endtime int64, orgid int64, orderType int64, manufacturerId int64, keyword string, page int64, limit int64, storehouse_id int64) (drugInfo []*models.BloodDrugWarehouseInfo, total int64, err error) {
  231. likeKey := "%" + keyword + "%"
  232. offset := (page - 1) * limit
  233. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  234. dbOne := XTReadDB().Table("xt_base_drug as t").Where("t.status =1")
  235. dbTwo := XTReadDB().Table("xt_drug_warehouse as s").Where("s.status = 1")
  236. dbThree := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  237. fmt.Print(dbOne, dbTwo, dbThree)
  238. if startime > 0 {
  239. db = db.Where("x.ctime >=?", startime)
  240. }
  241. if endtime > 0 {
  242. db = db.Where("x.ctime <=?", endtime)
  243. }
  244. if orgid > 0 {
  245. db = db.Where("x.org_id =?", orgid)
  246. }
  247. if storehouse_id > 0 {
  248. db = db.Where("x.storehouse_id = ?", storehouse_id)
  249. }
  250. if len(keyword) > 0 {
  251. db = db.Joins("left join sgj_xt.xt_drug_warehouse as o on o.id = x.warehousing_id left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = o.creater").Group("s.id")
  252. db = db.Where("x.warehousing_order like ? or t.drug_name like ? or r.user_name like ? ", likeKey, likeKey, likeKey)
  253. }
  254. if manufacturerId > 0 {
  255. err = db.Select("x.id,x.warehousing_id,x.drug_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.price,x.total_price,x.dealer,t.manufacturer,x.remark,x.ctime,x.org_id,x.is_return,x.warehousing_order,x.type,x.retail_price,x.retail_total_price,x.storehouse_id,x.max_unit as count_unit,t.drug_type,t.drug_name,t.drug_spec,t.min_unit,t.dose,t.dose_unit,t.max_unit,t.min_number,s.creater").Joins("left join xt_base_drug as t on t.id = x.drug_id and t.org_id = ? and t.status =1", orgid).Where("t.manufacturer = ?", manufacturerId).Joins("left join xt_drug_warehouse as s on s.id = x.warehousing_id and s.org_id = ? and s.status =1", orgid).Order("x.id desc").Count(&total).Offset(offset).Limit(limit).Scan(&drugInfo).Error
  256. } else {
  257. err = db.Select("x.id,x.warehousing_id,x.drug_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.price,x.total_price,x.dealer,t.manufacturer,x.remark,x.ctime,x.org_id,x.is_return,x.warehousing_order,x.type,x.retail_price,x.retail_total_price,x.storehouse_id,x.max_unit as count_unit,t.drug_type,t.drug_name,t.drug_spec,t.min_unit,t.dose,t.dose_unit,t.max_unit,t.min_number,s.creater").Joins("left join xt_base_drug as t on t.id = x.drug_id and t.org_id = ? and t.status =1", orgid).Joins("left join xt_drug_warehouse as s on s.id = x.warehousing_id and s.org_id = ? and s.status =1", orgid).Order("x.id desc").Count(&total).Offset(offset).Limit(limit).Scan(&drugInfo).Error
  258. }
  259. return drugInfo, total, err
  260. }
  261. func GetDrugReturnOrder(startime int64, endtime int64, orgid int64, orderType int64, manufacturerId int64, keyword string, limit int64, page int64) (returninfo []*models.BloodDrugSalesReturnInfo, total int64, err error) {
  262. likeKey := "%" + keyword + "%"
  263. offset := (page - 1) * limit
  264. db := XTReadDB().Table("xt_drug_sales_return_info as x").Where("x.status =1")
  265. dbOne := XTReadDB().Table("xt_base_drug as s").Where("s.status =1")
  266. dbTwo := XTReadDB().Table("xt_drug_sales_return as r").Where("r.status = 1")
  267. fmt.Print(dbOne, dbTwo)
  268. if startime > 0 {
  269. db = db.Where("x.ctime >= ?", startime)
  270. }
  271. if endtime > 0 {
  272. db = db.Where("x.ctime <=?", endtime)
  273. }
  274. if orgid > 0 {
  275. db = db.Where("x.org_id = ?", orgid)
  276. }
  277. if orderType > 0 {
  278. db = db.Where("x.type = ?", orderType)
  279. }
  280. if len(keyword) > 0 {
  281. db = db.Where("x.order_number like ? or s.drug_spec like ? or r.creater like ?", likeKey, likeKey, likeKey)
  282. }
  283. if manufacturerId > 0 {
  284. err = db.Select("x.id,x.drug_id,x.sales_return_id,x.count,x.price,x.total,x.product_date,x.expiry_date,x.ctime,x.org_id,x.order_number,x.type,x.dealer,x.manufacturer,x.retail_price,x.retail_total_price,x.number,s.drug_name,s.drug_spec,s.drug_type,s.min_unit,r.creater").Joins("left join xt_base_drug as s on s.id = x.drug_id and s.org_id = ? and s.status = 1", orgid).Joins("left join xt_drug_sales_return as r on r.id = x.sales_return_id and r.org_id = ? and r.status =1", orgid).Where("r.manufacturer = ?", manufacturerId).Order("x.ctime desc").Count(&total).Offset(offset).Limit(limit).Scan(&returninfo).Error
  285. } else {
  286. err = db.Select("x.id,x.drug_id,x.sales_return_id,x.count,x.price,x.total,x.product_date,x.expiry_date,x.ctime,x.org_id,x.order_number,x.type,x.dealer,x.manufacturer,x.retail_price,x.retail_total_price,x.number,s.drug_name,s.drug_spec,s.drug_type,s.min_unit,r.creater").Joins("left join xt_base_drug as s on s.id = x.drug_id and s.org_id = ? and s.status = 1", orgid).Joins("left join xt_drug_sales_return as r on r.id = x.sales_return_id and r.org_id = ? and r.status =1", orgid).Order("x.ctime desc").Count(&total).Offset(offset).Limit(limit).Scan(&returninfo).Error
  287. }
  288. return returninfo, total, err
  289. }
  290. func GetDrugOutOrder(startime int64, endtime int64, orgid int64, orderType int64, manufacturerId int64, keyword string, page int64, limit int64, storehouse_id int64) (outinfo []*models.BloodDrugWarehouseOutInfo, total int64, err error) {
  291. likeKey := "%" + keyword + "%"
  292. offset := (page - 1) * limit
  293. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status =1 and x.is_check = 1")
  294. dbOne := XTReadDB().Table("xt_base_drug as b").Where("b.status =1")
  295. dbTwo := XTReadDB().Table("xt_drug_warehouse_out as t").Where("t.status = 1")
  296. fmt.Print(dbOne, dbTwo)
  297. if startime > 0 {
  298. db = db.Where("x.sys_record_time >=?", startime)
  299. }
  300. if endtime > 0 {
  301. db = db.Where("x.sys_record_time <=?", endtime)
  302. }
  303. if orgid > 0 {
  304. db = db.Where("x.org_id = ?", orgid)
  305. }
  306. if orderType > 0 {
  307. db = db.Where("x.type = ?", orderType)
  308. }
  309. if storehouse_id > 0 {
  310. db = db.Where("x.storehouse_id = ?", storehouse_id)
  311. }
  312. if len(keyword) > 0 {
  313. db = db.Joins("left join sgj_xt.xt_drug_warehouse_out as o on o.id = x.warehouse_out_id left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = o.creater").Group("x.id")
  314. db = db.Where("x.warehouse_out_order_number like ? or b.drug_name like ? or r.user_name like ?", likeKey, likeKey, likeKey)
  315. }
  316. if manufacturerId > 0 {
  317. err = db.Select("x.id,x.warehouse_out_id,x.drug_id,x.warehousing_out_target,x.count,x.count_unit,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.remark,x.is_cancel,x.warehouse_out_order_number,x.type,x.dealer,x.manufacturer,x.is_sys,x.sys_record_time,x.retail_price,x.retail_total_price,x.storehouse_id,b.drug_name,b.drug_spec,b.drug_type,b.min_unit,b.max_unit,b.min_number,b.dose,b.dose_unit,b.last_price,t.creater").Joins("left join xt_base_drug as b on b.id = x.drug_id and b.org_id = ? and b.status =1", orgid).Joins("left join xt_drug_warehouse_out as t on t.id = x.warehouse_out_id and t.org_id = ? and t.status =1", orgid).Where("t.manufacturer = ?", manufacturerId).Order("x.ctime desc").Count(&total).Offset(offset).Limit(limit).Scan(&outinfo).Error
  318. } else {
  319. err = db.Select("x.id,x.warehouse_out_id,x.drug_id,x.warehousing_out_target,x.count,x.count_unit,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.remark,x.is_cancel,x.warehouse_out_order_number,x.type,x.dealer,x.manufacturer,x.is_sys,x.sys_record_time,x.retail_price,x.retail_total_price,x.storehouse_id,b.drug_name,b.drug_spec,b.drug_type,b.min_unit,b.max_unit,b.min_number,b.dose,b.dose_unit,b.last_price,t.creater").Joins("left join xt_base_drug as b on b.id = x.drug_id and b.org_id = ? and b.status =1", orgid).Joins("left join xt_drug_warehouse_out as t on t.id = x.warehouse_out_id and t.org_id = ? and t.status =1", orgid).Order("x.ctime desc").Count(&total).Offset(offset).Limit(limit).Scan(&outinfo).Error
  320. }
  321. return outinfo, total, err
  322. }
  323. func GetDrugCancelOrderPrint(startime int64, endtime int64, orgid int64, orderType int64, manufacturerId int64, keyword string, storehouse_id int64) (info []*models.BloodDrugCancelStockInfo, err error) {
  324. likeKey := "%" + keyword + "%"
  325. db := XTReadDB().Table("xt_drug_cancel_stock_info as x").Where("x.status =1")
  326. dbOne := XTReadDB().Table("xt_base_drug as s").Where("s.status =1")
  327. dbTwo := XTReadDB().Table("xt_drug_cancel_stock as t").Where("t.status = 1")
  328. fmt.Print(dbOne, dbTwo)
  329. if startime > 0 {
  330. db = db.Where("x.ctime >= ?", startime)
  331. }
  332. if endtime > 0 {
  333. db = db.Where("x.ctime<=?", endtime)
  334. }
  335. if orgid > 0 {
  336. db = db.Where("x.org_id = ?", orgid)
  337. }
  338. if orderType > 0 {
  339. db = db.Where("x.type = ? ", orderType)
  340. }
  341. if len(keyword) > 0 {
  342. db = db.Where("x.order_number like ? or s.drug_spec like ? or t.creater like ?", likeKey, likeKey, likeKey)
  343. }
  344. if storehouse_id > 0 {
  345. db = db.Where("x.storehouse_id = ?", storehouse_id)
  346. }
  347. if manufacturerId > 0 {
  348. err = db.Select("x.id,x.max_unit as unit,x.drug_id,x.cancel_stock_id,x.count,x.price,x.total,x.product_date,x.expiry_date,x.ctime,x.org_id,x.order_number,x.type,x.dealer,x.manufacturer,x.retail_price,x.retail_total_price,x.number,x.storehouse_id,s.drug_name,s.drug_type,s.drug_spec,s.min_unit,t.creater,s.dose,s.dose_unit,s.min_number,s.max_unit").Joins("left join xt_base_drug as s on s.id = x.drug_id and s.org_id = ? and s.status = 1", orgid).Joins("left join xt_drug_cancel_stock as t on t.id = x.cancel_stock_id and t.org_id = ? and t.status =1", orgid).Where("t.manufacturer = ?", manufacturerId).Order("x.ctime desc").Group("x.drug_id").Scan(&info).Error
  349. } else {
  350. err = db.Select("x.id,x.max_unit as unit,x.drug_id,x.cancel_stock_id,x.count,x.price,x.total,x.product_date,x.expiry_date,x.ctime,x.org_id,x.order_number,x.type,x.dealer,x.manufacturer,x.retail_price,x.retail_total_price,x.number,x.storehouse_id,s.drug_name,s.drug_type,s.drug_spec,s.min_unit,t.creater,s.dose,s.dose_unit,s.min_number,s.max_unit").Joins("left join xt_base_drug as s on s.id = x.drug_id and s.org_id = ? and s.status = 1", orgid).Joins("left join xt_drug_cancel_stock as t on t.id = x.cancel_stock_id and t.org_id = ? and t.status =1", orgid).Order("x.ctime desc").Group("x.drug_id").Scan(&info).Error
  351. }
  352. return info, err
  353. }
  354. func GetDrugCancelOrder(startime int64, endtime int64, orgid int64, orderType int64, manufacturerId int64, keyword string, page int64, limit int64, storehouse_id int64) (cancel []*models.BloodDrugCancelStockInfo, total int64, err error) {
  355. likeKey := "%" + keyword + "%"
  356. offset := (page - 1) * limit
  357. db := XTReadDB().Table("xt_drug_cancel_stock_info as x").Where("x.status =1")
  358. dbOne := XTReadDB().Table("xt_base_drug as s").Where("s.status =1")
  359. dbTwo := XTReadDB().Table("xt_drug_cancel_stock as t").Where("t.status = 1")
  360. fmt.Print(dbOne, dbTwo)
  361. if startime > 0 {
  362. db = db.Where("x.ctime >= ?", startime)
  363. }
  364. if endtime > 0 {
  365. db = db.Where("x.ctime<=?", endtime)
  366. }
  367. if orgid > 0 {
  368. db = db.Where("x.org_id = ?", orgid)
  369. }
  370. if orderType > 0 {
  371. db = db.Where("x.type = ? ", orderType)
  372. }
  373. if storehouse_id > 0 {
  374. db = db.Where("x.storehouse_id = ?", storehouse_id)
  375. }
  376. if len(keyword) > 0 {
  377. db = db.Joins("left join sgj_xt.xt_drug_cancel_stock as o on o.id = x.cancel_stock_id left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = o.creater").Group("o.id")
  378. db = db.Where("x.order_number like ? or s.drug_spec like ? or r.user_name like ? or s.drug_name like ?", likeKey, likeKey, likeKey, likeKey)
  379. }
  380. if manufacturerId > 0 {
  381. err = db.Select("x.id,x.max_unit as unit,x.drug_id,x.cancel_stock_id,x.count,x.price,x.total,x.product_date,x.expiry_date,x.ctime,x.org_id,x.order_number,x.type,x.dealer,x.manufacturer,x.retail_price,x.retail_total_price,x.number,x.storehouse_id,s.drug_name,s.drug_type,s.drug_spec,s.min_unit,t.creater,s.dose,s.dose_unit,s.min_number,s.max_unit").Joins("left join xt_base_drug as s on s.id = x.drug_id and s.org_id = ? and s.status = 1", orgid).Joins("left join xt_drug_cancel_stock as t on t.id = x.cancel_stock_id and t.org_id = ? and t.status =1", orgid).Where("t.manufacturer = ?", manufacturerId).Order("x.ctime desc").Count(&total).Offset(offset).Limit(limit).Scan(&cancel).Error
  382. } else {
  383. err = db.Select("x.id,x.max_unit as unit,x.drug_id,x.cancel_stock_id,x.count,x.price,x.total,x.product_date,x.expiry_date,x.ctime,x.org_id,x.order_number,x.type,x.dealer,x.manufacturer,x.retail_price,x.retail_total_price,x.number,x.storehouse_id,s.drug_name,s.drug_type,s.drug_spec,s.min_unit,t.creater,s.dose,s.dose_unit,s.min_number,s.max_unit").Joins("left join xt_base_drug as s on s.id = x.drug_id and s.org_id = ? and s.status = 1", orgid).Joins("left join xt_drug_cancel_stock as t on t.id = x.cancel_stock_id and t.org_id = ? and t.status =1", orgid).Order("x.ctime desc").Count(&total).Offset(offset).Limit(limit).Scan(&cancel).Error
  384. }
  385. return cancel, total, err
  386. }
  387. func FindPrintStockGoodInfoByType(types int, startTime int64, end_time int64, orgId int64) (list []*models.StockInfo, err error) {
  388. db := XTReadDB().Model(&models.StockInfo{})
  389. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgId)
  390. if types == 1 {
  391. db = db.Joins("JOIN xt_warehouse_info AS info ON info.good_id=xt_good_information.id AND info.status = 1 AND info.org_id = ?", orgId).Group("xt_good_information.id")
  392. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  393. return db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId).Joins("JOIN xt_warehouse AS warehouse ON warehouse.id = xt_warehouse_info.warehousing_id AND warehouse.status = 1 AND warehouse.warehousing_time >=? AND warehouse.warehousing_time<= ? AND warehouse.org_id = ?", startTime, end_time, orgId)
  394. })
  395. } else if types == 2 {
  396. db = db.Joins("JOIN xt_sales_return_info AS info ON info.good_id=xt_good_information.id AND info.status = 1 AND info.org_id = ?", orgId).Group("xt_good_information.id")
  397. db = db.Preload("QuerySalesReturnInfo", func(db *gorm.DB) *gorm.DB {
  398. return db.Where("xt_sales_return_info.org_id = ? AND xt_sales_return_info.status = 1", orgId).Joins("JOIN xt_sales_return AS sales ON sales.id = xt_sales_return_info.sales_return_id AND sales.status = 1 AND sales.return_time >=? AND sales.return_time<= ? AND sales.org_id = ?", startTime, end_time, orgId)
  399. })
  400. } else if types == 3 {
  401. db = db.Joins("JOIN xt_warehouse_out_info AS info ON info.good_id=xt_good_information.id AND info.status = 1 AND info.org_id = ?", orgId).Group("xt_good_information.id")
  402. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  403. return db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1", orgId).Joins("JOIN xt_warehouse_out ON xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id AND xt_warehouse_out.status = 1 AND xt_warehouse_out.warehouse_out_time >=? AND xt_warehouse_out.warehouse_out_time<= ? AND xt_warehouse_out.org_id = ? ", startTime, end_time, orgId)
  404. })
  405. } else if types == 4 {
  406. db = db.Joins("JOIN xt_cancel_stock_info AS info ON info.good_id=xt_good_information.id AND info.status = 1 AND info.org_id = ?", orgId).Group("xt_good_information.id")
  407. db = db.Preload("QueryCancelStockInfo", func(db *gorm.DB) *gorm.DB {
  408. return db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId).Joins("JOIN xt_cancel_stock AS cancel ON cancel.id = xt_cancel_stock_info.cancel_stock_id AND cancel.status = 1 AND cancel.return_time >=? AND cancel.return_time<= ? AND cancel.org_id = ?", startTime, end_time, orgId)
  409. })
  410. }
  411. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  412. err = db.Order("ctime desc").Find(&list).Error
  413. return
  414. }
  415. func GetOutStockTotalCountTwo(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  416. err = XTReadDB().Raw("SELECT good_id,SUM(b.count) as count FROM (SELECT DISTINCT x.patient_id,x.good_id,x.record_time,x.count FROM xt_automatic_reduce_detail as x WHERE x.org_id = ? and x.record_time >= ? and x.record_time<=? and `status` = 1) as b GROUP BY good_id", orgid, startime, endtime).Scan(&autoMatic).Error
  417. return autoMatic, err
  418. }
  419. func AddMonitorOpen(config *models.XtMonitorConfig) error {
  420. err := XTWriteDB().Create(&config).Error
  421. return err
  422. }
  423. func UpdateMonitorOpen(orgid int64, config *models.XtMonitorConfig) error {
  424. err := XTWriteDB().Model(&config).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"is_open": config.IsOpen, "mtime": time.Now().Unix()}).Error
  425. return err
  426. }
  427. func GetMonitorConfig(orgid int64) (models.XtMonitorConfig, error) {
  428. config := models.XtMonitorConfig{}
  429. err := XTReadDB().Model(&config).Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  430. return config, err
  431. }
  432. func AddOrderConfig(config *models.XtOrderConfig) error {
  433. err := XTWriteDB().Create(&config).Error
  434. return err
  435. }
  436. func UpdateOrderConfig(orgid int64, config *models.XtOrderConfig) error {
  437. err := XTWriteDB().Model(&config).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"is_open": config.IsOpen, "mtime": time.Now().Unix()}).Error
  438. return err
  439. }
  440. func GetOrderConfigById(orgid int64) (*models.XtOrderConfig, error) {
  441. config := models.XtOrderConfig{}
  442. err := XTReadDB().Model(&config).Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  443. if err == gorm.ErrRecordNotFound {
  444. return nil, err
  445. }
  446. if err != nil {
  447. return nil, err
  448. }
  449. return &config, nil
  450. }
  451. func GetOrderConfig(orgid int64) (models.XtOrderConfig, error) {
  452. config := models.XtOrderConfig{}
  453. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  454. return config, err
  455. }
  456. func GetMonitorConfigById(orgid int64) (*models.XtMonitorConfig, error) {
  457. config := models.XtMonitorConfig{}
  458. err := XTReadDB().Model(&config).Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  459. if err == gorm.ErrRecordNotFound {
  460. return nil, err
  461. }
  462. if err != nil {
  463. return nil, err
  464. }
  465. return &config, nil
  466. }
  467. func GetDrugAutoMaticList(orgid int64, warehouse_out_id int64, record_time int64, warehouse_out_order_number string) (auto []*models.VmDrugAutomaticReduceDetail, err error) {
  468. db := XTReadDB().Table("xt_drug_automatic_reduce_detail as x").Where("x.status = 1")
  469. err = db.Select("x.org_id,x.warehouse_out_id,x.record_time,x.drug_id,sum(x.count) as total").Where("x.org_id = ? and x.record_time = ? and x.status= 1 and x.warehouse_out_order_number = ?", orgid, record_time, warehouse_out_order_number).Group("x.drug_id").Scan(&auto).Error
  470. return auto, err
  471. }
  472. func GetAllBaseDrugList(orgid int64) (drug []*models.BaseDrugLib, err error) {
  473. err = XTReadDB().Model(&drug).Where("org_id = ? and status = 1", orgid).Find(&drug).Error
  474. return drug, err
  475. }
  476. func GetDrugWarehuseOrderInfo(orgid int64) (drug []*models.DrugWarehouseInfo, err error) {
  477. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Group("drug_id").Find(&drug).Error
  478. return drug, err
  479. }
  480. func GetDrugStockList(page int64, limit int64, keyword string, drugcategory int64, startime int64, endtime int64, orgid int64) (list []*models.StDrugWarehouseInfo, total int64, err error) {
  481. offset := (page - 1) * limit
  482. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  483. likeKey := "%" + keyword + "%"
  484. if orgid > 0 {
  485. db = db.Where("x.org_id = ?", orgid)
  486. }
  487. if startime > 0 {
  488. db = db.Where("x.ctime >=?", startime)
  489. }
  490. if endtime > 0 {
  491. db = db.Where("x.ctime<=?", endtime)
  492. }
  493. if drugcategory > 0 && len(keyword) == 0 {
  494. err = db.Select("x.id,x.warehousing_id,x.drug_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.warehouseing_unit,x.stock_max_number,x.stock_min_number,x.price,x.total_price,x.dealer,x.remark,x.org_id,x.is_return,x.warehousing_order,x.type,x.retail_price,x.retail_total_price,x.batch_number,t.drug_name,t.drug_type,t.max_unit,t.min_unit,t.min_number,t.dose,t.dose_unit,t.last_price,t.manufacturer").Joins("left join xt_base_drug as t on t.id = x.drug_id and t.org_id =?", orgid).Where("t.drug_type = ?", drugcategory).Group("x.drug_id").Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Scan(&list).Error
  495. return
  496. }
  497. if drugcategory <= 0 && len(keyword) == 0 {
  498. err = db.Select("x.id,x.warehousing_id,x.drug_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.warehouseing_unit,x.stock_max_number,x.stock_min_number,x.price,x.total_price,x.dealer,x.remark,x.org_id,x.is_return,x.warehousing_order,x.type,x.retail_price,x.retail_total_price,x.batch_number,t.drug_name,t.drug_type,t.max_unit,t.min_unit,t.min_number,t.dose,t.dose_unit,t.last_price,t.manufacturer").Joins("left join xt_base_drug as t on t.id = x.drug_id and t.org_id = ?", orgid).Group("x.drug_id").Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Scan(&list).Error
  499. return
  500. }
  501. if len(keyword) > 0 && drugcategory == 0 {
  502. err = db.Select("x.id,x.warehousing_id,x.drug_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.warehouseing_unit,x.stock_max_number,x.stock_min_number,x.price,x.total_price,x.dealer,x.remark,x.org_id,x.is_return,x.warehousing_order,x.type,x.retail_price,x.retail_total_price,x.batch_number,t.drug_name,t.drug_type,t.max_unit,t.min_unit,t.min_number,t.dose,t.dose_unit,t.last_price,t.manufacturer").Joins("left join xt_base_drug as t on t.id = x.drug_id and t.org_id=?", orgid).Where("t.drug_name like ?", likeKey).Group("x.drug_id").Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Scan(&list).Error
  503. return
  504. }
  505. if len(keyword) <= 0 && drugcategory == 0 {
  506. err = db.Select("x.id,x.warehousing_id,x.drug_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.warehouseing_unit,x.stock_max_number,x.stock_min_number,x.price,x.total_price,x.dealer,x.remark,x.org_id,x.is_return,x.warehousing_order,x.type,x.retail_price,x.retail_total_price,x.batch_number,t.drug_name,t.drug_type,t.max_unit,t.min_unit,t.min_number,t.dose,t.dose_unit,t.last_price,t.manufacturer").Joins("left join xt_base_drug as t on t.id = x.drug_id and t.org_id = ?", orgid).Group("x.drug_id").Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Scan(&list).Error
  507. return
  508. }
  509. if len(keyword) > 0 && drugcategory > 0 {
  510. err = db.Select("x.id,x.warehousing_id,x.drug_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.warehouseing_unit,x.stock_max_number,x.stock_min_number,x.price,x.total_price,x.dealer,x.remark,x.org_id,x.is_return,x.warehousing_order,x.type,x.retail_price,x.retail_total_price,x.batch_number,t.drug_name,t.drug_type,t.max_unit,t.min_unit,t.min_number,t.dose,t.dose_unit,t.last_price,t.manufacturer").Joins("left join xt_base_drug as t on t.id = x.drug_id and t.org_id = ?", orgid).Where("t.drug_type = ? or t.drug_name like ?", drugcategory, likeKey).Group("x.drug_id").Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Scan(&list).Error
  511. return
  512. }
  513. return list, total, err
  514. }
  515. func GetAllBaseDurgListCount(page int64, limit int64, keyword string, drugcategory int64, startime int64, endtime int64, orgid int64, storehouse_id int64, drug_id int64) (drug []*models.VmBaseDrug, total int64, err error) {
  516. offset := (page - 1) * limit
  517. db := XTReadDB().Table("xt_base_drug").Where("status = 1")
  518. likeKey := "%" + keyword + "%"
  519. if orgid > 0 {
  520. db = db.Where("org_id = ?", orgid)
  521. }
  522. if drugcategory > 0 {
  523. db = db.Where("drug_type = ?", drugcategory)
  524. }
  525. if len(keyword) > 0 {
  526. db = db.Where("drug_name like ?", likeKey)
  527. }
  528. if drug_id > 0 {
  529. db = db.Where("id = ?", drug_id)
  530. }
  531. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  532. if startime > 0 {
  533. db = db.Where("ctime>=?", startime)
  534. }
  535. if endtime > 0 {
  536. db = db.Where("ctime<=?", endtime)
  537. }
  538. if storehouse_id > 0 {
  539. db = db.Where("storehouse_id = ?", storehouse_id)
  540. }
  541. return db.Where("status = 1 and is_check = 1")
  542. }).Preload("DrugCancelStockInfo", func(db *gorm.DB) *gorm.DB {
  543. if startime > 0 {
  544. db = db.Where("ctime>=?", startime)
  545. }
  546. if endtime > 0 {
  547. db = db.Where("ctime<=?", endtime)
  548. }
  549. return db.Where("status = 1")
  550. }).Preload("DrugWarehouse", func(db *gorm.DB) *gorm.DB {
  551. if startime > 0 {
  552. db = db.Where("ctime>=?", startime)
  553. }
  554. if endtime > 0 {
  555. db = db.Where("ctime<=?", endtime)
  556. }
  557. if storehouse_id > 0 {
  558. db = db.Where("storehouse_id = ?", storehouse_id)
  559. }
  560. return db.Group("drug_id,storehouse_id").Where("status = 1")
  561. }).Preload("DrugWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  562. if startime > 0 {
  563. db = db.Where("ctime>=?", startime)
  564. }
  565. if endtime > 0 {
  566. db = db.Where("ctime<=?", endtime)
  567. }
  568. return db.Where("status = 1")
  569. }).Find(&drug).Error
  570. return drug, total, err
  571. }
  572. func GetDrugStockFlow(drugid int64, startime int64, endtime int64, page int64, limit int64, orgid int64) (list []*models.StDrugWarehouseInfo, total int64, err error) {
  573. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  574. table := XTReadDB().Table("xt_base_drug as t").Where("t.status =1 ")
  575. fmt.Println(table)
  576. offset := (page - 1) * limit
  577. if startime > 0 {
  578. db = db.Where("x.ctime >=?", startime)
  579. }
  580. if endtime > 0 {
  581. db = db.Where("x.ctime<=?", endtime)
  582. }
  583. if orgid > 0 {
  584. db = db.Where("x.org_id = ?", orgid)
  585. }
  586. if drugid > 0 {
  587. db = db.Where("x.drug_id = ?", drugid)
  588. }
  589. err = db.Joins("left join xt_base_drug as t on t.id = x.drug_id and t.status = 1 and t.org_id = ?", orgid).Select("x.id,x.warehousing_id,x.drug_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.warehouseing_unit,x.max_unit,x.min_unit,x.stock_max_number,x.stock_min_number,x.price,x.total_price,x.dealer,x.remark,x.org_id,x.is_return,x.warehousing_order,x.type,x.retail_price,x.retail_total_price,x.batch_number,x.ctime,t.drug_name,t.drug_type,t.max_unit,t.min_unit,t.min_number,t.dose,t.dose_unit,t.last_price,t.manufacturer").Offset(offset).Count(&total).Order("x.ctime desc").Scan(&list).Error
  590. return list, total, err
  591. }
  592. func GetDrugStockOutFlow(drugid int64, startime int64, endtime int64, page int64, limit int64, orgid int64, stocktype int64) (list []*models.VmDrugWarehouseOutInfo, total int64, err error) {
  593. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  594. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  595. fmt.Println(table)
  596. offset := (page - 1) * limit
  597. if startime > 0 {
  598. db = db.Where("x.ctime >=?", startime)
  599. }
  600. if endtime > 0 {
  601. db = db.Where("x.ctime<=?", endtime)
  602. }
  603. if orgid > 0 {
  604. db = db.Where("x.org_id = ?", orgid)
  605. }
  606. if drugid > 0 {
  607. db = db.Where("x.drug_id = ?", drugid)
  608. }
  609. if stocktype == 1 {
  610. db = db.Where("x.is_sys = 0")
  611. }
  612. if stocktype == 2 {
  613. db = db.Where("x.is_sys = 1")
  614. }
  615. err = db.Select("x.id,x.warehouse_out_id,x.drug_id,x.warehousing_out_target,x.count,x.price,x.total_price,x.product_date,x.expiry_date,x.org_id,x.remark,x.is_cancel,x.warehouse_out_order_number,x.type,x.dealer,x.manufacturer,x.is_sys,x.sys_record_time,x.retail_price,x.retail_total_price,x.warehouse_info_id,x.ctime,x.batch_number,x.count_unit,t.drug_name,t.drug_type,t.min_number,t.min_unit,t.max_unit").Joins("left join xt_base_drug as t on t.id = x.drug_id").Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Scan(&list).Error
  616. return list, total, err
  617. }
  618. func GetBatchOrderDetail(drugid int64, orgid int64, page int64, limit int64, startime int64, endtime int64, start_first_time int64, end_first_time int64) (drug []*models.DrugWarehouseInfo, total int64, err error) {
  619. offset := (page - 1) * limit
  620. db := XTReadDB().Model(&drug).Where("status = 1 and is_check = 1")
  621. if drugid > 0 {
  622. db = db.Where("drug_id = ?", drugid)
  623. }
  624. if orgid > 0 {
  625. db = db.Where("org_id = ?", orgid)
  626. }
  627. if startime > 0 {
  628. db = db.Where("ctime >=?", startime)
  629. }
  630. if endtime > 0 {
  631. db = db.Where("ctime <=?", endtime)
  632. }
  633. if start_first_time > 0 {
  634. db = db.Where("expiry_date >=?", start_first_time)
  635. }
  636. if end_first_time > 0 {
  637. db = db.Where("expiry_date <=?", end_first_time)
  638. }
  639. err = db.Count(&total).Offset(offset).Limit(limit).Order("id desc").Find(&drug).Error
  640. return drug, total, err
  641. }
  642. func GetDrugCountList(startime int64, endtime int64, orgid int64) (info []*models.VmDrugWarehouseInfo, err error) {
  643. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  644. if startime > 0 {
  645. db = db.Where("x.ctime >=?", startime)
  646. }
  647. if endtime > 0 {
  648. db = db.Where("x.ctime<=?", endtime)
  649. }
  650. if orgid > 0 {
  651. db = db.Where("x.org_id = ?", orgid)
  652. }
  653. err = db.Select("sum(x.warehousing_count) as count,x.drug_id").Group("x.drug_id").Scan(&info).Error
  654. return info, err
  655. }
  656. func GetMinCountList(startime int64, endtime int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  657. db := readDb2.Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  658. if startime > 0 {
  659. db = db.Where("x.ctime >=?", startime)
  660. }
  661. if endtime > 0 {
  662. db = db.Where("x.ctime<=?", endtime)
  663. }
  664. if orgid > 0 {
  665. db = db.Where("x.org_id = ?", orgid)
  666. }
  667. err = db.Select("sum(x.warehousing_count) as warehousing_count,x.drug_id").Group("x.drug_id").Scan(&info).Error
  668. return info, err
  669. }
  670. func GetOutDrugCountList(startime int64, endtime int64, orgid int64) (info []*models.VmDrugWarehouseInfo, err error) {
  671. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  672. if startime > 0 {
  673. db = db.Where("x.ctime >=?", startime)
  674. }
  675. if endtime > 0 {
  676. db = db.Where("x.ctime<=?", endtime)
  677. }
  678. if orgid > 0 {
  679. db = db.Where("x.org_id = ?", orgid)
  680. }
  681. err = db.Select("sum(x.count) as count,x.drug_id,x.count_unit").Group("x.drug_id").Find(&info).Error
  682. return info, err
  683. }
  684. func GetAutoDrugCountList(startime int64, endtime int64, orgid int64) (info []*models.DrugAutomaticReduceDetail, err error) {
  685. db := XTReadDB().Table("xt_drug_automatic_reduce_detail as x").Where("x.status = 1")
  686. if startime > 0 {
  687. db = db.Where("x.record_time >=?", startime)
  688. }
  689. if endtime > 0 {
  690. db = db.Where("x.record_time<=?", endtime)
  691. }
  692. if orgid > 0 {
  693. db = db.Where("x.org_id = ?", orgid)
  694. }
  695. err = db.Select("sum(x.count) as count,x.drug_id,x.count_unit").Find(&info).Error
  696. return info, err
  697. }
  698. func GetCancelDrugCountList(startime int64, endtime int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  699. db := XTReadDB().Table(" xt_drug_cancel_stock_info as x").Where("x.status = 1")
  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. if orgid > 0 {
  707. db = db.Where("x.org_id = ?", orgid)
  708. }
  709. err = db.Select("sum(x.count) as count,x.drug_id").Group("x.drug_id").Scan(&info).Error
  710. return info, err
  711. }
  712. func GetAllCountList(startime int64, endtime int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  713. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  714. if startime > 0 {
  715. db = db.Where("x.ctime >=?", startime)
  716. }
  717. if endtime > 0 {
  718. db = db.Where("x.ctime<=?", endtime)
  719. }
  720. if orgid > 0 {
  721. db = db.Where("x.org_id = ?", orgid)
  722. }
  723. err = db.Find(&info).Error
  724. return info, err
  725. }
  726. func GetSingleOrderDetail(id int64, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  727. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1 and x.count>0")
  728. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  729. fmt.Println(table)
  730. if orgid > 0 {
  731. db = db.Where("x.org_id = ?", orgid)
  732. }
  733. if id > 0 {
  734. db = db.Where("x.warehouse_out_id in(?)", id)
  735. }
  736. err = db.Select("x.id,x.warehouse_out_id,x.drug_id,sum(x.count) as count,x.count_unit,x.price,x.product_date,x.expiry_date,x.remark,x.is_cancel,x.warehouse_out_order_number,x.type,x.dealer,t.manufacturer,x.is_sys,x.sys_record_time,x.retail_price as total_price,x.retail_total_price,x.storehouse_id,x.stock_count,t.drug_name,t.drug_type,t.max_unit,t.min_unit,t.min_number,x.number,x.batch_number,x.admin_user_id,t.dose,t.dose_unit,t.last_price,t.min_price,t.medical_insurance_number,t.retail_price ,x.warehouse_info_id").Joins("left join xt_base_drug as t on t.id = x.drug_id").Group("x.drug_id").Scan(&info).Error
  737. return info, err
  738. }
  739. func GetDrugWarehouseOutDetailSeventy(id int64, org_id int64) (info []*models.DrugWarehouseOutInfo, err error) {
  740. err = XTReadDB().Where("warehouse_out_id in(?) and org_id = ? and status =1", id, org_id).Find(&info).Error
  741. return info, err
  742. }
  743. func GetDrugStockFlowDetail(record_time int64, orgid int64) (drugflow []*models.XtDrugAutomaticReduceDetail, err error) {
  744. err = XTReadDB().Where("record_time = ? and org_id = ? and status = 1", record_time, orgid).Preload("XtBaseDrug", "status = 1").Find(&drugflow).Error
  745. return drugflow, err
  746. }
  747. func GetSingeOrderFlow(id int64, orgid int64) (outInfo []*models.VmDrugWarehouseOutInfo, err error) {
  748. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  749. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  750. fmt.Println(table)
  751. if orgid > 0 {
  752. db = db.Where("x.org_id = ?", orgid)
  753. }
  754. if id > 0 {
  755. db = db.Where("x.warehouse_out_id in(?)", id)
  756. }
  757. err = db.Select("x.id,x.warehouse_out_id,x.drug_id,x.count,x.count_unit,x.price,x.total_price,x.product_date,x.expiry_date,x.remark,x.is_cancel,x.warehouse_out_order_number,x.type,x.dealer,x.manufacturer,x.is_sys,x.sys_record_time,x.retail_price,x.retail_total_price,t.drug_name,t.drug_type,t.max_unit,t.min_unit,t.min_number,t.retail_price,x.number,x.batch_number,t.dose,t.dose_unit,t.last_price,t.min_price").Joins("left join xt_base_drug as t on t.id = x.drug_id").Scan(&outInfo).Error
  758. return outInfo, err
  759. }
  760. func GetAllSingleDrugDetail(id int64, orgid int64) (outInfo []*models.VmDrugWarehouseOutInfo, err error) {
  761. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  762. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  763. fmt.Println(table)
  764. if orgid > 0 {
  765. db = db.Where("x.org_id = ?", orgid)
  766. }
  767. if id > 0 {
  768. db = db.Where("x.warehouse_out_id = ?", id)
  769. }
  770. err = db.Select("x.id,x.warehouse_out_id,x.drug_id,x.count,x.count_unit,x.price,x.total_price,x.product_date,x.expiry_date,x.remark,x.is_cancel,x.warehouse_out_order_number,x.type,x.dealer,t.manufacturer,x.is_sys,x.sys_record_time,x.retail_price,x.retail_total_price,t.drug_name,t.drug_type,t.max_unit,t.min_unit,t.min_number,x.number,x.batch_number,t.dose,t.dose_unit,t.last_price,t.min_price").Joins("left join xt_base_drug as t on t.id = x.drug_id").Scan(&outInfo).Error
  771. return outInfo, err
  772. }
  773. func GetDrugFlowList(id int64, orgid int64) (stockflow []*models.DrugFlow, err error) {
  774. err = XTReadDB().Where("warehouse_out_id in(?) and user_org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "org_id = ? and status = 1", orgid).Preload("DrugWarehouseInfoOne", "org_id = ? and status = 1", orgid).Find(&stockflow).Error
  775. return stockflow, err
  776. }
  777. func GetDrugFlowListGroupById(id int64, orgid int64) (drugFlow []*models.DrugFlow, err error) {
  778. err = XTReadDB().Where("warehouse_out_id in(?) and user_org_id = ? and status = 1", id, orgid).Group("warehousing_detail_id").Find(&drugFlow).Error
  779. return drugFlow, err
  780. }
  781. func GetDrugFlowListGroupByIdOne(id int64, orgid int64) (drugFlow []*models.DrugFlow, err error) {
  782. err = XTReadDB().Where("warehouse_out_id in(?) and user_org_id = ? and status = 1", id, orgid).Group("batch_number").Find(&drugFlow).Error
  783. return drugFlow, err
  784. }
  785. func FindDrugStockUserDetailByIdThree(id int64, record_time int64, org_id int64) (user []*DrugAutomaticReduceDetail, err error, total int64) {
  786. db := readDb.Model(&user)
  787. db = db.Preload("Drug", "org_id = ? AND status = 1", org_id)
  788. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  789. db = db.Preload("DrugWarehouseOutInfo", "drug_id = ? and sys_record_time = ? and status = 1 and org_id = ?", id, record_time, org_id)
  790. db = db.Where("status = 1 AND org_id = ? AND drug_id = ? AND record_time =?", org_id, id, record_time)
  791. db = db.Count(&total)
  792. err = db.Find(&user).Error
  793. return user, err, total
  794. }
  795. func FindeDrugWarehouserInfo(ware_out_id int64, drug_id int64, orgid int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  796. err = XTReadDB().Where("warehouse_out_id = ? and drug_id = ? and org_id = ? and status = 1", ware_out_id, drug_id, orgid).Find(&outInfo).Error
  797. return outInfo, err
  798. }
  799. func GetDrugFlowBatch(ware_out_id int64, drug_id int64, orgid int64) (flow []*models.DrugFlow, err error) {
  800. err = XTReadDB().Where("warehouse_out_id = ? and drug_id = ? and user_org_id = ? and status = 1", ware_out_id, drug_id, orgid).Find(&flow).Error
  801. return flow, err
  802. }
  803. func GetDrugWarehouseOutInfoFlow(drug_id int64, start_time int64, end_time int64, user_org_id int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  804. db := XTReadDB().Model(&info).Where("status = 1")
  805. if start_time > 0 {
  806. db = db.Where("sys_record_time >=?", start_time)
  807. }
  808. if end_time > 0 {
  809. db = db.Where("sys_record_time <=?", end_time)
  810. }
  811. if user_org_id > 0 {
  812. db = db.Where("user_org_id = ?", user_org_id)
  813. }
  814. err = db.Find(&info).Error
  815. return info, err
  816. }