drug_stock_service.go 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package service
  2. import (
  3. "XT_New/models"
  4. "github.com/jinzhu/gorm"
  5. )
  6. type DrugConfig struct {
  7. ID int64 `gorm:"column:id" json:"id" form:"id"`
  8. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  9. DrugCategory int64 `gorm:"column:drug_category" json:"drug_category" form:"drug_category"`
  10. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  11. DrugType int64 `gorm:"column:drug_type" json:"drug_type" form:"drug_type"`
  12. DrugStatus string `gorm:"column:drug_status" json:"drug_status" form:"drug_status"`
  13. Status int64 `gorm:"column:status" json:"status" form:"status"`
  14. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  15. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  16. DrugSpecs []DrugConfig `gorm:"ForeignKey:DrugName;AssociationForeignKey:DrugName" json:"drug_specs"`
  17. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  18. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  19. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  20. HospApprFlag int64 `gorm:"column:hosp_appr_flag" json:"hosp_appr_flag" form:"hosp_appr_flag"`
  21. LmtUsedFlag int64 `gorm:"column:lmt_used_flag" json:"lmt_used_flag" form:"lmt_used_flag"`
  22. Total float64 `gorm:"column:total" json:"total" form:"total"`
  23. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  24. }
  25. func (DrugConfig) TableName() string {
  26. return "xt_base_drug"
  27. }
  28. func GetDrugs(orgId int64) (drugs []*DrugConfig, err error) {
  29. err = readDb.Model(&DrugConfig{}).Where("org_id = ? AND status = 1 AND find_in_set('停用',drug_status) = 0", orgId).Preload("DrugSpecs", "status = 1 AND find_in_set('停用',drug_status) = 0 AND org_id = ?", orgId).Group("drug_name").Find(&drugs).Error
  30. return
  31. }
  32. func FindAllDrugByManufactureId(manufacturer_id int64, dealer_id int64, org_id int64) (goodInfo []*DrugConfig, err error) {
  33. db := readDb.Model(&DrugConfig{}).Where("org_id = ? AND status = 1 AND find_in_set('停用',drug_status) = 0", org_id)
  34. if manufacturer_id > 0 {
  35. db = db.Where("manufacturer = ?", manufacturer_id)
  36. }
  37. if dealer_id > 0 {
  38. db = db.Where("dealer = ?", dealer_id)
  39. }
  40. err = db.Preload("DrugSpecs", "status = 1 AND org_id = ? AND find_in_set('停用',drug_status) = 0", org_id).Group("drug_name").Find(&goodInfo).Error
  41. return goodInfo, err
  42. }
  43. type MedicineInsuranceDate struct {
  44. RecordTime int64
  45. }
  46. func GetLastMedicineInsurance(orgID int64, type_id int64, module int64) (mip []*models.MedicineInsurancePercentage, err error) {
  47. var mip_other models.MedicineInsurancePercentage
  48. err = readDb.Model(&models.MedicineInsurancePercentage{}).Where("user_org_id = ? AND status=1 AND type_id = ? AND module = ?", orgID, type_id, module).Last(&mip_other).Error
  49. err = readDb.Model(&models.MedicineInsurancePercentage{}).Where("user_org_id = ? AND status=1 AND record_time = ? AND type_id = ? AND module = ?", orgID, mip_other.RecordTime, type_id, module).Find(&mip).Error
  50. return
  51. }
  52. type DrugWarehouseInfo struct {
  53. ID int64 `gorm:"column:id" json:"id" form:"id"`
  54. Number string `gorm:"column:number" json:"number" form:"number"`
  55. Status int64 `gorm:"column:status" json:"status" form:"status"`
  56. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  57. }
  58. func (DrugWarehouseInfo) TableName() string {
  59. return "xt_drug_warehouse_info"
  60. }
  61. func FindStockInNumber(orgId int64) (stockIns []*DrugWarehouseInfo, err error) {
  62. err = readDb.Model(&DrugWarehouseInfo{}).Where("org_id = ? AND status = 1", orgId).Group("number").Find(&stockIns).Error
  63. return
  64. }
  65. type DrugStockInfo struct {
  66. ID int64 `gorm:"column:id" json:"id" form:"id"`
  67. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  68. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  69. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  70. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  71. DrugStatus string `gorm:"column:drug_status" json:"drug_status" form:"drug_status"`
  72. Status int64 `gorm:"column:status" json:"status" form:"status"`
  73. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  74. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  75. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  76. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  77. HospApprFlag int64 `gorm:"column:hosp_appr_flag" json:"hosp_appr_flag" form:"hosp_appr_flag"`
  78. LmtUsedFlag int64 `gorm:"column:lmt_used_flag" json:"lmt_used_flag" form:"lmt_used_flag"`
  79. QueryWarehousingInfo []models.DrugWarehouseInfo `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"query_drug_warehousing_info"`
  80. QuerySalesReturnInfo []models.DrugSalesReturnInfo `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"query_drug_sales_return_info"`
  81. QueryWarehouseOutInfo []models.DrugWarehouseOutInfo `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"query_drug_warehouseout_info"`
  82. QueryCancelStockInfo []models.DrugCancelStockInfo `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"query_drug_cancel_stock_info"`
  83. Total float64 `gorm:"column:total" json:"total" form:"total"`
  84. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  85. }
  86. func (DrugStockInfo) TableName() string {
  87. return "xt_base_drug"
  88. }
  89. func FindAllDrugStockInfo(orgId int64, page int64, limit int64, keyword string, drugCategory int64, startTime int64, endTime int64) (list []*DrugStockInfo, total int64, err error) {
  90. db := readDb.Model(&DrugStockInfo{})
  91. if drugCategory > 0 {
  92. db = db.Where("xt_base_drug.org_id = ? AND xt_base_drug.status = 1 AND xt_base_drug.drug_category = ?", orgId, drugCategory)
  93. } else {
  94. db = db.Where("xt_base_drug.org_id = ? AND xt_base_drug.status = 1", orgId)
  95. }
  96. if startTime > 0 {
  97. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  98. return db.Where("org_id = ? AND status = 1 and ctime >=?", orgId, startTime)
  99. })
  100. db = db.Preload("QuerySalesReturnInfo", "org_id = ? AND status = 1 and ctime>=?", orgId, startTime)
  101. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  102. return db.Where("org_id = ? AND status = 1 and sys_record_time>=?", orgId, startTime)
  103. })
  104. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime>=?", orgId, startTime)
  105. } else {
  106. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  107. return db.Where("org_id = ? AND status = 1", orgId)
  108. })
  109. db = db.Preload("QuerySalesReturnInfo", "org_id = ? AND status = 1", orgId)
  110. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  111. return db.Where("org_id = ? AND status = 1", orgId)
  112. })
  113. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1", orgId)
  114. }
  115. if endTime > 0 {
  116. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  117. return db.Where("org_id = ? AND status = 1 and ctime <=?", orgId, startTime, endTime)
  118. })
  119. db = db.Preload("QuerySalesReturnInfo", "org_id = ? AND status = 1 and ctime>=? and ctime<=?", orgId, startTime, endTime)
  120. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  121. return db.Where("org_id = ? AND status = 1 and sys_record_time>=? and sys_record_time<=?", orgId, startTime, endTime)
  122. })
  123. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=? and ctime<=?", orgId, startTime, endTime)
  124. } else {
  125. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  126. return db.Where("org_id = ? AND status = 1", orgId)
  127. })
  128. db = db.Preload("QuerySalesReturnInfo", "org_id = ? AND status = 1", orgId)
  129. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  130. return db.Where("org_id = ? AND status = 1", orgId)
  131. })
  132. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1", orgId)
  133. }
  134. if len(keyword) > 0 {
  135. likeKey := "%" + keyword + "%"
  136. db = db.Where("xt_base_drug.drug_name LIKE ? OR xt_base_drug.drug_spec LIKE ?", likeKey, likeKey)
  137. }
  138. db = db.Count(&total)
  139. offset := (page - 1) * limit
  140. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  141. return
  142. }
  143. func GetCancelDrugOrderPrint(id []string, orgid int64) (info []*models.DrugCancelStock, err error) {
  144. db := XTReadDB().Model(&info)
  145. if len(id) > 0 {
  146. db = db.Where("id in(?)", id)
  147. }
  148. if orgid > 0 {
  149. db = db.Where("org_id= ?", orgid)
  150. }
  151. err = db.Preload("DrugCancelStockInfo", func(db *gorm.DB) *gorm.DB {
  152. return db.Where("status = 1 AND org_id = ?", orgid).Preload("BaseDrugLib", "status = 1 AND org_id= ?", orgid)
  153. }).Find(&info).Error
  154. return info, err
  155. }
  156. func GetDrugCancelExportList(orderid []string, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  157. db := XTReadDB().Model(&info)
  158. if len(orderid) > 0 {
  159. db = db.Where("cancel_stock_id in(?)", orderid)
  160. }
  161. if orgid > 0 {
  162. db = db.Where("org_id = ?", orgid)
  163. }
  164. db = db.Preload("BaseDrugLib", "status = 1 and org_id = ?", orgid)
  165. err = db.Find(&info).Error
  166. return info, err
  167. }
  168. func GetDrugWarehouseInfoPrint(startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  169. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  170. if startime > 0 {
  171. db = db.Where("x.ctime >=?", startime)
  172. }
  173. if endtime > 0 {
  174. db = db.Where("x.ctime<=?", endtime)
  175. }
  176. if orgid > 0 {
  177. db = db.Where("x.org_id = ?", orgid)
  178. }
  179. if storehouse_id > 0 {
  180. db = db.Where("storehouse_id =?", storehouse_id)
  181. }
  182. err = db.Select("x.id,x.warehousing_id,x.drug_id,x.number,x.product_date,x.expiry_date,sum(x.warehousing_count) as warehousing_count,x.price,x.total_price,x.manufacturer,x.remark,x.is_return,x.warehousing_order,x.type,x.retail_price,x.retail_total_price,x.warehouseing_unit,x.max_unit,x.min_unit,x.stock_max_number,x.stock_min_number,x.batch_number,x.dealer,x.manufacturer").Preload("XtBaseDrug", "status = 1 and org_id = ?", orgid).Group("x.drug_id").Find(&info).Error
  183. //err = db.Select("x.id,x.warehousing_id,x.drug_id,x.number,x.product_date,x.expiry_date,sum(x.warehousing_count) as warehousing_count,x.price,x.total_price,x.dealer,x.manufacturer,x.remark,x.is_return,x.warehousing_order,x.type,x.retail_price,x.retail_total_price,x.warehouseing_unit,x.max_unit,x.min_unit,x.stock_max_number,x.stock_min_number,x.batch_number").Group("x.drug_id").Find(&DrugWarehouseInfo{}).Error
  184. return info, err
  185. }
  186. //func GetDrugWarehouseInfoList(startime int64,endtime int64,orgid int64)(info []*models.BloodAutomaticReduceDetail,err error) {
  187. //
  188. //
  189. // db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  190. // if startime > 0 {
  191. // db = db.Where("x.ctime >=?", startime)
  192. // }
  193. // if endtime>0{
  194. // db = db.Where("x.ctime<=?", endtime)
  195. // }
  196. // if orgid > 0{
  197. // db = db.Where("x.org_id = ?", orgid)
  198. // }
  199. //
  200. //
  201. //}
  202. func GetDrugOutOrderPrint(startime int64, endtime int64, orgid int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  203. db := XTReadDB().Table("xt_drug_warehouse_out_info").Where("status = 1")
  204. if startime > 0 {
  205. db = db.Where("ctime >=?", startime)
  206. }
  207. if endtime > 0 {
  208. db = db.Where("ctime<=?", endtime)
  209. }
  210. if orgid > 0 {
  211. db = db.Where("org_id = ?", orgid)
  212. }
  213. err = db.Preload("XtBaseDrug", "status = 1 and org_id = ?", orgid).Group("drug_id").Find(&info).Error
  214. return info, err
  215. }
  216. func GetDrugOutOrderPrintList(startime int64, endtime int64, orgid int64) (list []*models.XtDrugAutomaticReduceDetail, err error) {
  217. db := XTReadDB().Table("xt_drug_automatic_reduce_detail").Where("status = 1")
  218. if startime > 0 {
  219. db = db.Where("ctime >=?", startime)
  220. }
  221. if endtime > 0 {
  222. db = db.Where("ctime<=?", endtime)
  223. }
  224. if orgid > 0 {
  225. db = db.Where("org_id = ?", orgid)
  226. }
  227. err = db.Preload("XtBaseDrug", "status = 1 and org_id = ?", orgid).Find(&list).Error
  228. return list, err
  229. }
  230. func GetDrugOutOrderInfoPrintList(startime int64, endtime int64, orgid int64, storehouse_id int64) (list []*models.SgjDrugWarehouseOutInfo, err error) {
  231. db := XTReadDB().Table("xt_drug_warehouse_out_info").Where("status = 1 and is_check=1")
  232. if startime > 0 {
  233. db = db.Where("ctime >=?", startime)
  234. }
  235. if endtime > 0 {
  236. db = db.Where("ctime<=?", endtime)
  237. }
  238. if orgid > 0 {
  239. db = db.Where("org_id = ?", orgid)
  240. }
  241. if storehouse_id > 0 {
  242. db = db.Where("storehouse_id=?", storehouse_id)
  243. }
  244. err = db.Preload("XtDrugWarehouseInfo", "status=1 and org_id =?", orgid).Preload("XtBaseDrug", "status = 1 and org_id = ?", orgid).Find(&list).Error
  245. return list, err
  246. }