gobal_config_service.go 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. package service
  2. import (
  3. "XT_New/models"
  4. "fmt"
  5. "github.com/jinzhu/gorm"
  6. )
  7. func CreateAutomaticReduceRecord(config *models.GobalConfig) (err error) {
  8. err = writeDb.Model(&models.GobalConfig{}).Create(config).Error
  9. return
  10. }
  11. func CreateDrugAutomaticReduceRecord(config *models.DrugStockConfig) (err error) {
  12. err = writeDb.Model(&models.DrugStockConfig{}).Create(config).Error
  13. return
  14. }
  15. func FindAutomaticReduceRecordByOrgId(org_id int64) (err error, config models.GobalConfig) {
  16. err = readDb.Model(&models.GobalConfig{}).Where("status = 1 AND org_id = ?", org_id).Find(&config).Error
  17. return
  18. }
  19. func FindDrugStockAutomaticReduceRecordByOrgId(org_id int64) (err error, config models.DrugStockConfig) {
  20. err = readDb.Model(&models.DrugStockConfig{}).Where("status = 1 AND org_id = ?", org_id).Find(&config).Error
  21. return
  22. }
  23. func UpdateAutomaticReduceRecord(config *models.GobalConfig) (err error) {
  24. err = writeDb.Save(config).Error
  25. return
  26. }
  27. func UpdateDrugStockAutomaticReduceRecord(config *models.DrugStockConfig) (err error) {
  28. err = writeDb.Save(config).Error
  29. return
  30. }
  31. func CreatePrintTemplateRecord(template *models.GobalTemplate) (err error) {
  32. err = writeDb.Model(&models.GobalTemplate{}).Create(template).Error
  33. return
  34. }
  35. func FindPrintTemplateByOrgId(org_id int64) (err error, template models.GobalTemplate) {
  36. err = readDb.Model(&models.GobalTemplate{}).Where("status = 1 AND org_id = ?", org_id).Find(&template).Error
  37. return
  38. }
  39. func UpdatePrintTemplate(template *models.GobalTemplate) (err error) {
  40. err = writeDb.Save(template).Error
  41. return
  42. }
  43. func CreateConfigData(config *models.DataUploadConfig) (err error) {
  44. err = writeDb.Create(config).Error
  45. return
  46. }
  47. func SaveConfigData(config *models.DataUploadConfig) (err error) {
  48. err = writeDb.Save(config).Error
  49. return
  50. }
  51. func GetConfigData(org_id int64, config_type int64) (config models.DataUploadConfig, err error) {
  52. err = readDb.Model(&models.DataUploadConfig{}).Where("status = 1 AND org_id = ? AND config_type = ?", org_id, config_type).First(&config).Error
  53. return
  54. }
  55. func GetDockingStatus(config_type int64, province int64, city int64) (config models.DockingStatus, err error) {
  56. err = readDb.Model(&models.DockingStatus{}).Where("status = 1 AND docking_type = ? AND province_id = ? AND city_id = ?", config_type, province, city).First(&config).Error
  57. return
  58. }
  59. func GetConfigDataById(id int64) (config models.DataUploadConfig, err error) {
  60. err = readDb.Model(&models.DataUploadConfig{}).Where("id = ?", id).First(&config).Error
  61. return
  62. }
  63. func FindDoctorAdviceRecordByOrgId(org_id int64) (err error, config models.DoctorAdviceConfig) {
  64. err = readDb.Model(&models.DoctorAdviceConfig{}).Where("status = 1 AND user_org_id = ?", org_id).Find(&config).Error
  65. return
  66. }
  67. func CreateDoctorAdviceRecord(config *models.DoctorAdviceConfig) (err error) {
  68. err = writeDb.Model(&models.DoctorAdviceConfig{}).Create(config).Error
  69. return
  70. }
  71. func UpdateDoctorAdviceRecord(config *models.DoctorAdviceConfig) (err error) {
  72. err = writeDb.Save(config).Error
  73. return
  74. }
  75. func UpdateFiledConfig(org_id int64) (err error) {
  76. err = writeDb.Model(&models.FiledConfig{}).Where("org_id = ? AND sys_module = 0", org_id).Updates(map[string]interface{}{"is_show": 1}).Error
  77. return
  78. }
  79. func FindAllHideFiledConfig(template_id int64) (err error, config []*models.FiledConfig) {
  80. err = readDb.Model(&models.FiledConfig{}).Where("sys_module = ? AND is_show = 2", template_id).Find(&config).Error
  81. return
  82. }
  83. func UpdateShowFieldConfig(org_id int64, fileds []string) (err error) {
  84. 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
  85. return
  86. }
  87. func FindAllAdviceParentTemplate(org_id int64) (template []*models.DoctorAdviceParentTemplate) {
  88. readDb.Model(&models.DoctorAdviceParentTemplate{}).Preload("DoctorAdviceTemplate", func(db *gorm.DB) *gorm.DB {
  89. return db.Preload("SubDoctorAdviceTemplate", "status = 1 AND org_id = ?", org_id).Where("status = 1 AND parent_id = 0 AND org_id = ?", org_id)
  90. }).Where("status = 1 AND org_id = ? ", org_id).Find(&template)
  91. return
  92. }
  93. func CreateDoctorParentTemplate(template *models.DoctorAdviceParentTemplate) (err error) {
  94. err = writeDb.Create(&template).Error
  95. return
  96. }
  97. func FindAllAdviceTemplates(org_id int64, parent_template_id int64) (template []*models.DoctorAdviceTemplate, err error) {
  98. err = readDb.Model(&models.DoctorAdviceTemplate{}).Where("status = 1 AND org_id = ? AND template_id = ?", org_id, parent_template_id).Find(&template).Error
  99. return
  100. }
  101. func CreateDoctorTemplate(template *models.DoctorAdviceTemplate) (err error) {
  102. err = writeDb.Create(&template).Error
  103. return
  104. }
  105. func CreateSystemDialysisSolution(solution *models.SystemPrescription) (err error) {
  106. err = writeDb.Create(solution).Error
  107. return
  108. }
  109. func UpdateSystemDialysisSolution(solution *models.SystemPrescription) (err error) {
  110. err = writeDb.Save(solution).Error
  111. return
  112. }
  113. func FindSystemDialysisSolution(orgID int64, id int64) (solution models.SystemPrescription, err error) {
  114. err = readDb.Model(&models.SystemPrescription{}).Where("id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error
  115. return
  116. }
  117. func FindAllSystemPrescription(orgID int64) (solution []*models.SystemPrescription, err error) {
  118. err = readDb.Model(&models.SystemPrescription{}).Where("status=1 and user_org_id=?", orgID).Find(&solution).Error
  119. return
  120. }
  121. func FindSystemDialysisPrescriptionByMode(orgID int64, id int64) (solution models.SystemPrescription, err error) {
  122. err = readDb.Model(&models.SystemPrescription{}).Where("mode_id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error
  123. return
  124. }
  125. func CreateAdviceInitConfig(adviceInit *models.AdviceInit) (err error) {
  126. err = writeDb.Create(&adviceInit).Error
  127. return
  128. }
  129. func FindAdviceInitConfig(org_id int64) (adviceInit models.AdviceInit, err error) {
  130. err = readDb.Model(&models.AdviceInit{}).Where("user_org_id = ? AND status = 1 ", org_id).First(&adviceInit).Error
  131. return
  132. }
  133. func GetPrint(ids []int64, orgid int64) (doctor []*models.DoctorAdvice, err error) {
  134. if len(ids) == 1 {
  135. err = XTReadDB().Model(&doctor).Where("groupno = ? and user_org_id = ?", ids[0], orgid).Find(&doctor).Error
  136. } else {
  137. err = XTReadDB().Model(&doctor).Where("groupno IN(?) and user_org_id = ?", ids, orgid).Find(&doctor).Error
  138. }
  139. return doctor, err
  140. }
  141. func GetExportLogByType(org_id int64, log_type int64) (log []*models.ExportLog, err error) {
  142. err = readDb.Model(&models.ExportLog{}).Where("user_org_id = ? AND status = 1 AND log_type = ?", org_id, log_type).
  143. Preload("ExportErrLog", "status = 1").Order("export_time desc").Find(&log).Error
  144. return
  145. }
  146. func FindXTHisRecordByOrgId(org_id int64) (err error, config models.XtHisConfig) {
  147. err = readDb.Model(&models.XtHisConfig{}).Where("status = 1 AND user_org_id = ?", org_id).Find(&config).Error
  148. return
  149. }
  150. func UpdateXTHisRecord(config *models.XtHisConfig) (err error) {
  151. err = writeDb.Save(config).Error
  152. return
  153. }
  154. func CreateXTHisRecord(config *models.XtHisConfig) (err error) {
  155. err = writeDb.Model(&models.XtHisConfig{}).Create(config).Error
  156. return
  157. }
  158. func GetExportHisOrderList(user_org_id int64, start_time int64, end_time int64) (order []*models.HisOrder, err error) {
  159. db := readDb.Model(&models.HisOrder{})
  160. if start_time != 0 {
  161. db = db.Where("his_order.settle_accounts_date>=?", start_time)
  162. }
  163. if end_time != 0 {
  164. db = db.Where("his_order.settle_accounts_date<=?", end_time)
  165. }
  166. db = db.Where("his_order.status = 1 AND his_order.user_org_id = ?", user_org_id)
  167. db = db.Preload("HisOrderInfo", "status = 1 AND user_org_id = ?", user_org_id).
  168. Preload("Patients", "status = 1 AND user_org_id = ?", user_org_id).
  169. Preload("HisPatient", "status = 1 AND user_org_id = ?", user_org_id).
  170. Preload("HisPrescriptionInfo", func(db *gorm.DB) *gorm.DB {
  171. return db.Where("status = 1 AND user_org_id = ?", user_org_id).Preload("XtHisDepartment", "status = 1")
  172. })
  173. err = db.Order("ctime desc").Find(&order).Error
  174. return
  175. }
  176. 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) {
  177. likeKey := "%" + keyword + "%"
  178. offset := (page - 1) * limit
  179. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  180. dbOne := XTReadDB().Table("xt_base_drug as t").Where("t.status =1")
  181. dbTwo := XTReadDB().Table("xt_drug_warehouse as s").Where("s.status = 1")
  182. fmt.Print(dbOne, dbTwo)
  183. if startime > 0 {
  184. db = db.Where("x.ctime >=?", startime)
  185. }
  186. if endtime > 0 {
  187. db = db.Where("x.ctime <=?", endtime)
  188. }
  189. if orgid > 0 {
  190. db = db.Where("x.org_id =?", orgid)
  191. }
  192. if orderType > 0 {
  193. db = db.Where("x.type = ?", orderType)
  194. }
  195. if len(keyword) > 0 {
  196. db = db.Where("x.warehousing_order like ? or t.drug_spec like ? or s.creater like ?", likeKey, likeKey, likeKey)
  197. }
  198. if manufacturerId > 0 {
  199. 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
  200. } else {
  201. 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
  202. }
  203. return drugInfo, total, err
  204. }
  205. 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) {
  206. likeKey := "%" + keyword + "%"
  207. offset := (page - 1) * limit
  208. db := XTReadDB().Table("xt_drug_sales_return_info as x").Where("x.status =1")
  209. dbOne := XTReadDB().Table("xt_base_drug as s").Where("s.status =1")
  210. dbTwo := XTReadDB().Table("xt_drug_sales_return as r").Where("r.status = 1")
  211. fmt.Print(dbOne, dbTwo)
  212. if startime > 0 {
  213. db = db.Where("x.ctime >= ?", startime)
  214. }
  215. if endtime > 0 {
  216. db = db.Where("x.ctime <=?", endtime)
  217. }
  218. if orgid > 0 {
  219. db = db.Where("x.org_id = ?", orgid)
  220. }
  221. if orderType > 0 {
  222. db = db.Where("x.type = ?", orderType)
  223. }
  224. if len(keyword) > 0 {
  225. db = db.Where("x.order_number like ? or s.drug_spec like ? or r.creater like ?", likeKey, likeKey, likeKey)
  226. }
  227. if manufacturerId > 0 {
  228. 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
  229. } else {
  230. 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
  231. }
  232. return returninfo, total, err
  233. }
  234. 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) {
  235. likeKey := "%" + keyword + "%"
  236. offset := (page - 1) * limit
  237. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status =1")
  238. dbOne := XTReadDB().Table("xt_base_drug as b").Where("b.status =1")
  239. dbTwo := XTReadDB().Table("xt_drug_warehouse_out as t").Where("t.status = 1")
  240. fmt.Print(dbOne, dbTwo)
  241. if startime > 0 {
  242. db = db.Where("x.ctime >=?", startime)
  243. }
  244. if endtime > 0 {
  245. db = db.Where("x.ctime <=?", endtime)
  246. }
  247. if orgid > 0 {
  248. db = db.Where("x.org_id = ?", orgid)
  249. }
  250. if orderType > 0 {
  251. db = db.Where("x.type = ?", orderType)
  252. }
  253. if len(keyword) > 0 {
  254. db = db.Where("x.warehouse_out_order_number like ? or b.drug_spec like ? or t.creater like ?", likeKey, likeKey, likeKey)
  255. }
  256. if manufacturerId > 0 {
  257. 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
  258. } else {
  259. 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
  260. }
  261. return outinfo, total, err
  262. }
  263. 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) {
  264. likeKey := "%" + keyword + "%"
  265. offset := (page - 1) * limit
  266. db := XTReadDB().Table("xt_drug_cancel_stock_info as x").Where("x.status =1")
  267. dbOne := XTReadDB().Table("xt_base_drug as s").Where("s.status =1")
  268. dbTwo := XTReadDB().Table("xt_drug_cancel_stock as t").Where("t.status = 1")
  269. fmt.Print(dbOne, dbTwo)
  270. if startime > 0 {
  271. db = db.Where("x.ctime >= ?", startime)
  272. }
  273. if endtime > 0 {
  274. db = db.Where("x.ctime<=?", endtime)
  275. }
  276. if orgid > 0 {
  277. db = db.Where("x.org_id = ?", orgid)
  278. }
  279. if orderType > 0 {
  280. db = db.Where("x.type = ? ", orderType)
  281. }
  282. if len(keyword) > 0 {
  283. db = db.Where("x.order_number like ? or s.drug_spec like ? or t.creater like ?", likeKey, likeKey, likeKey)
  284. }
  285. if manufacturerId > 0 {
  286. 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
  287. } else {
  288. 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
  289. }
  290. return cancel, total, err
  291. }