gobal_config_service.go 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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).Find(&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).Find(&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) (doctor []*models.DoctorAdvice, err error) {
  135. if len(ids) == 1 {
  136. err = XTReadDB().Model(&doctor).Where("groupno = ? and user_org_id = ?", ids[0], orgid).Find(&doctor).Error
  137. } else {
  138. err = XTReadDB().Model(&doctor).Where("groupno IN(?) and user_org_id = ?", ids, orgid).Find(&doctor).Error
  139. }
  140. return doctor, err
  141. }
  142. func GetExportLogByType(org_id int64, log_type int64) (log []*models.ExportLog, err error) {
  143. err = readDb.Model(&models.ExportLog{}).Where("user_org_id = ? AND status = 1 AND log_type = ?", org_id, log_type).
  144. Preload("ExportErrLog", "status = 1").Order("export_time desc").Find(&log).Error
  145. return
  146. }
  147. func FindXTHisRecordByOrgId(org_id int64) (err error, config models.XtHisConfig) {
  148. err = readDb.Model(&models.XtHisConfig{}).Where("status = 1 AND user_org_id = ?", org_id).Find(&config).Error
  149. return
  150. }
  151. func UpdateXTHisRecord(config *models.XtHisConfig) (err error) {
  152. err = writeDb.Save(config).Error
  153. return
  154. }
  155. func CreateXTHisRecord(config *models.XtHisConfig) (err error) {
  156. err = writeDb.Model(&models.XtHisConfig{}).Create(config).Error
  157. return
  158. }
  159. //TODO:项目开关
  160. func FindXTHisProjectByOrgId(org_id int64) (err error, config models.XtHisProjectConfig) {
  161. err = readDb.Model(&models.XtHisProjectConfig{}).Where("status = 1 AND user_org_id = ?", org_id).Find(&config).Error
  162. return
  163. }
  164. func UpdateXTHisProjectRecord(config *models.XtHisProjectConfig) (err error) {
  165. err = writeDb.Save(config).Error
  166. return
  167. }
  168. func CreateXTHisProjectRecord(config *models.XtHisProjectConfig) (err error) {
  169. err = writeDb.Model(&models.XtHisProjectConfig{}).Create(config).Error
  170. return
  171. }
  172. func GetExportHisOrderList(user_org_id int64, start_time int64, end_time int64, p_type int64) (order []*models.HisOrder, err error) {
  173. db := readDb.Model(&models.HisOrder{})
  174. if start_time != 0 {
  175. db = db.Where("his_order.settle_accounts_date>=?", start_time)
  176. }
  177. if end_time != 0 {
  178. db = db.Where("his_order.settle_accounts_date<=?", end_time)
  179. }
  180. db = db.Where("his_order.status = 1 AND his_order.user_org_id = ? AND his_order.order_status > 1 AND p_type = ? ", user_org_id, p_type)
  181. db = db.Preload("HisOrderInfo", "status = 1 AND user_org_id = ?", user_org_id).
  182. Preload("Patients", "status = 1 AND user_org_id = ?", user_org_id).
  183. Preload("HisPatient", "status = 1 AND user_org_id = ?", user_org_id).
  184. Preload("HisPrescriptionInfo", func(db *gorm.DB) *gorm.DB {
  185. return db.Where("status = 1 AND user_org_id = ?", user_org_id).Preload("XtHisDepartment", "status = 1")
  186. })
  187. err = db.Order("ctime desc").Find(&order).Error
  188. return
  189. }
  190. func GetDrugInOrderDetail(startime int64, endtime int64, orgid int64, orderType int64, manufacturerId int64, keyword string, page int64, limit int64) (drugInfo []*models.BloodDrugWarehouseInfo, total int64, err error) {
  191. likeKey := "%" + keyword + "%"
  192. offset := (page - 1) * limit
  193. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  194. dbOne := XTReadDB().Table("xt_base_drug as t").Where("t.status =1")
  195. dbTwo := XTReadDB().Table("xt_drug_warehouse as s").Where("s.status = 1")
  196. fmt.Print(dbOne, dbTwo)
  197. if startime > 0 {
  198. db = db.Where("x.ctime >=?", startime)
  199. }
  200. if endtime > 0 {
  201. db = db.Where("x.ctime <=?", endtime)
  202. }
  203. if orgid > 0 {
  204. db = db.Where("x.org_id =?", orgid)
  205. }
  206. if orderType > 0 {
  207. db = db.Where("x.type = ?", orderType)
  208. }
  209. if len(keyword) > 0 {
  210. db = db.Where("x.warehousing_order like ? or t.drug_spec like ? or s.creater like ?", likeKey, likeKey, likeKey)
  211. }
  212. if manufacturerId > 0 {
  213. 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,t.drug_type,t.drug_name,t.drug_spec,t.min_unit,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.ctime desc").Count(&total).Offset(offset).Limit(limit).Scan(&drugInfo).Error
  214. } else {
  215. 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,t.drug_type,t.drug_name,t.drug_spec,t.min_unit,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.ctime desc").Count(&total).Offset(offset).Limit(limit).Scan(&drugInfo).Error
  216. }
  217. return drugInfo, total, err
  218. }
  219. 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) {
  220. likeKey := "%" + keyword + "%"
  221. offset := (page - 1) * limit
  222. db := XTReadDB().Table("xt_drug_sales_return_info as x").Where("x.status =1")
  223. dbOne := XTReadDB().Table("xt_base_drug as s").Where("s.status =1")
  224. dbTwo := XTReadDB().Table("xt_drug_sales_return as r").Where("r.status = 1")
  225. fmt.Print(dbOne, dbTwo)
  226. if startime > 0 {
  227. db = db.Where("x.ctime >= ?", startime)
  228. }
  229. if endtime > 0 {
  230. db = db.Where("x.ctime <=?", endtime)
  231. }
  232. if orgid > 0 {
  233. db = db.Where("x.org_id = ?", orgid)
  234. }
  235. if orderType > 0 {
  236. db = db.Where("x.type = ?", orderType)
  237. }
  238. if len(keyword) > 0 {
  239. db = db.Where("x.order_number like ? or s.drug_spec like ? or r.creater like ?", likeKey, likeKey, likeKey)
  240. }
  241. if manufacturerId > 0 {
  242. 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
  243. } else {
  244. 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
  245. }
  246. return returninfo, total, err
  247. }
  248. func GetDrugOutOrder(startime int64, endtime int64, orgid int64, orderType int64, manufacturerId int64, keyword string, page int64, limit int64) (outinfo []*models.BloodDrugWarehouseOutInfo, total int64, err error) {
  249. likeKey := "%" + keyword + "%"
  250. offset := (page - 1) * limit
  251. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status =1")
  252. dbOne := XTReadDB().Table("xt_base_drug as b").Where("b.status =1")
  253. dbTwo := XTReadDB().Table("xt_drug_warehouse_out as t").Where("t.status = 1")
  254. fmt.Print(dbOne, dbTwo)
  255. if startime > 0 {
  256. db = db.Where("x.ctime >=?", startime)
  257. }
  258. if endtime > 0 {
  259. db = db.Where("x.ctime <=?", endtime)
  260. }
  261. if orgid > 0 {
  262. db = db.Where("x.org_id = ?", orgid)
  263. }
  264. if orderType > 0 {
  265. db = db.Where("x.type = ?", orderType)
  266. }
  267. if len(keyword) > 0 {
  268. db = db.Where("x.warehouse_out_order_number like ? or b.drug_spec like ? or t.creater like ?", likeKey, likeKey, likeKey)
  269. }
  270. if manufacturerId > 0 {
  271. 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.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,b.drug_name,b.drug_spec,b.drug_type,b.min_unit,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
  272. } else {
  273. 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.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,b.drug_name,b.drug_spec,b.drug_type,b.min_unit,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
  274. }
  275. return outinfo, total, err
  276. }
  277. func GetDrugCancelOrder(startime int64, endtime int64, orgid int64, orderType int64, manufacturerId int64, keyword string, page int64, limit int64) (cancel []*models.BloodDrugCancelStockInfo, total int64, err error) {
  278. likeKey := "%" + keyword + "%"
  279. offset := (page - 1) * limit
  280. db := XTReadDB().Table("xt_drug_cancel_stock_info as x").Where("x.status =1")
  281. dbOne := XTReadDB().Table("xt_base_drug as s").Where("s.status =1")
  282. dbTwo := XTReadDB().Table("xt_drug_cancel_stock as t").Where("t.status = 1")
  283. fmt.Print(dbOne, dbTwo)
  284. if startime > 0 {
  285. db = db.Where("x.ctime >= ?", startime)
  286. }
  287. if endtime > 0 {
  288. db = db.Where("x.ctime<=?", endtime)
  289. }
  290. if orgid > 0 {
  291. db = db.Where("x.org_id = ?", orgid)
  292. }
  293. if orderType > 0 {
  294. db = db.Where("x.type = ? ", orderType)
  295. }
  296. if len(keyword) > 0 {
  297. db = db.Where("x.order_number like ? or s.drug_spec like ? or t.creater like ?", likeKey, likeKey, likeKey)
  298. }
  299. if manufacturerId > 0 {
  300. err = db.Select("x.id,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,s.drug_name,s.drug_type,s.drug_spec,s.min_unit,t.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_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
  301. } else {
  302. err = db.Select("x.id,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,s.drug_name,s.drug_type,s.drug_spec,s.min_unit,t.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_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
  303. }
  304. return cancel, total, err
  305. }
  306. func FindPrintStockGoodInfoByType(types int, startTime int64, end_time int64, orgId int64) (list []*models.StockInfo, err error) {
  307. db := XTReadDB().Model(&models.StockInfo{})
  308. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgId)
  309. if types == 1 {
  310. 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")
  311. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  312. 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)
  313. })
  314. } else if types == 2 {
  315. 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")
  316. db = db.Preload("QuerySalesReturnInfo", func(db *gorm.DB) *gorm.DB {
  317. 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)
  318. })
  319. } else if types == 3 {
  320. 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")
  321. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  322. 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)
  323. })
  324. } else if types == 4 {
  325. 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")
  326. db = db.Preload("QueryCancelStockInfo", func(db *gorm.DB) *gorm.DB {
  327. 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)
  328. })
  329. }
  330. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  331. err = db.Order("ctime desc").Find(&list).Error
  332. return
  333. }
  334. func GetOutStockTotalCountTwo(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  335. 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
  336. return autoMatic, err
  337. }
  338. func AddMonitorOpen(config *models.XtMonitorConfig) error {
  339. err := XTWriteDB().Create(&config).Error
  340. return err
  341. }
  342. func UpdateMonitorOpen(orgid int64, config *models.XtMonitorConfig) error {
  343. 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
  344. return err
  345. }
  346. func GetMonitorConfig(orgid int64) (models.XtMonitorConfig, error) {
  347. config := models.XtMonitorConfig{}
  348. err := XTReadDB().Model(&config).Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  349. return config, err
  350. }
  351. func AddOrderConfig(config *models.XtOrderConfig) error {
  352. err := XTWriteDB().Create(&config).Error
  353. return err
  354. }
  355. func UpdateOrderConfig(orgid int64, config *models.XtOrderConfig) error {
  356. 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
  357. return err
  358. }
  359. func GetOrderConfigById(orgid int64) (*models.XtOrderConfig, error) {
  360. config := models.XtOrderConfig{}
  361. err := XTReadDB().Model(&config).Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  362. if err == gorm.ErrRecordNotFound {
  363. return nil, err
  364. }
  365. if err != nil {
  366. return nil, err
  367. }
  368. return &config, nil
  369. }
  370. func GetOrderConfig(orgid int64) (models.XtOrderConfig, error) {
  371. config := models.XtOrderConfig{}
  372. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  373. return config, err
  374. }
  375. func GetMonitorConfigById(orgid int64) (*models.XtMonitorConfig, error) {
  376. config := models.XtMonitorConfig{}
  377. err := XTReadDB().Model(&config).Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  378. if err == gorm.ErrRecordNotFound {
  379. return nil, err
  380. }
  381. if err != nil {
  382. return nil, err
  383. }
  384. return &config, nil
  385. }
  386. func GetDrugAutoMaticList(orgid int64, warehouse_out_id int64, record_time int64, warehouse_out_order_number string) (auto []*models.VmDrugAutomaticReduceDetail, err error) {
  387. db := XTReadDB().Table("xt_drug_automatic_reduce_detail as x").Where("x.status = 1")
  388. 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
  389. return auto, err
  390. }
  391. func GetAllBaseDrugList(orgid int64) (drug []*models.BaseDrugLib, err error) {
  392. err = XTReadDB().Model(&drug).Where("org_id = ? and status = 1", orgid).Find(&drug).Error
  393. return drug, err
  394. }
  395. func GetDrugWarehuseOrderInfo(orgid int64) (drug []*models.DrugWarehouseInfo, err error) {
  396. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Group("drug_id").Find(&drug).Error
  397. return drug, err
  398. }
  399. func GetDrugStockList(page int64, limit int64, keyword string, drugcategory int64, startime int64, endtime int64, orgid int64) (list []*models.StDrugWarehouseInfo, total int64, err error) {
  400. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  401. likeKey := "%" + keyword + "%"
  402. offset := (page - 1) * limit
  403. if len(keyword) > 0 {
  404. db = db.Joins("left join xt_base_drug as t on t.id = x.drug_id and t.status= 1 and t.org_id = ? and t.drug_name like ?", orgid, likeKey)
  405. } else {
  406. db.Joins("left join xt_base_drug as t on t.id = x.drug_id and t.status= 1 and t.org_id = ?", orgid)
  407. }
  408. if drugcategory > 0 {
  409. db = db.Joins("left join xt_base_drug as t on t.id = x.drug_id and t.status= 1 and t.org_id = ? and t.drug_category = ?", orgid, drugcategory)
  410. } else {
  411. db = db.Joins("left join xt_base_drug as t on t.id = x.drug_id and t.status= 1 and t.org_id = ? and t.drug_category = ?", orgid, drugcategory)
  412. }
  413. if startime > 0 {
  414. db = db.Where("x.ctime >=?", startime)
  415. }
  416. if endtime > 0 {
  417. db = db.Where("x.ctime<=?", endtime)
  418. }
  419. if orgid > 0 {
  420. db = db.Where("x.org_id = ?", orgid)
  421. }
  422. 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.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,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).Group("x.drug_id").Order("x.ctime desc").Scan(&list).Error
  423. return list, total, err
  424. }
  425. func GetDrugStockFlow(drugid int64, startime int64, endtime int64, page int64, limit int64, orgid int64) (list []*models.StDrugWarehouseInfo, total int64, err error) {
  426. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  427. offset := (page - 1) * limit
  428. if startime > 0 {
  429. db = db.Where("x.ctime >=?", startime)
  430. }
  431. if endtime > 0 {
  432. db = db.Where("x.ctime<=?", endtime)
  433. }
  434. if orgid > 0 {
  435. db = db.Where("x.org_id = ?", orgid)
  436. }
  437. if drugid > 0 {
  438. db = db.Where("x.drug_id = ?", drugid)
  439. }
  440. 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,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
  441. return list, total, err
  442. }
  443. func GetDrugStockOutFlow(drugid int64, startime int64, endtime int64, page int64, limit int64, orgid int64, stocktype int64) (list []*models.XtDrugWarehouseOutInfo, total int64, err error) {
  444. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  445. offset := (page - 1) * limit
  446. if startime > 0 {
  447. db = db.Where("x.ctime >=?", startime)
  448. }
  449. if endtime > 0 {
  450. db = db.Where("x.ctime<=?", endtime)
  451. }
  452. if orgid > 0 {
  453. db = db.Where("x.org_id = ?", orgid)
  454. }
  455. if drugid > 0 {
  456. db = db.Where("x.drug_id = ?", drugid)
  457. }
  458. if stocktype == 1 {
  459. db = db.Where("x.is_sys = 0")
  460. }
  461. if stocktype == 2 {
  462. db = db.Where("x,is_sys = 1")
  463. }
  464. 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.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").Offset(offset).Count(&total).Order("x.ctime desc").Scan(&list).Error
  465. return list, total, err
  466. }
  467. func GetBatchOrderDetail(drugid int64, orgid int64, page int64, limit int64) (drug []*models.DrugWarehouseInfo, total int64, err error) {
  468. offset := (page - 1) * limit
  469. err = XTReadDB().Model(&drug).Where("drug_id = ? and org_id = ?", drugid, orgid).Offset(offset).Count(&total).Find(&drug).Error
  470. return drug, total, err
  471. }