gobal_config_service.go 42KB

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