new_stock_service.go 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. package service
  2. import (
  3. "XT_New/models"
  4. "github.com/jinzhu/gorm"
  5. )
  6. func GetGoodStockCount(user_org_id int64, storehouse_id int64, good_id int64) (*models.XtGoodStockCount, error) {
  7. goodStock := models.XtGoodStockCount{}
  8. var err error
  9. err = XTReadDB().Where("user_org_id = ? and storehouse_id = ? and good_id = ?", user_org_id, storehouse_id, good_id).Find(&goodStock).Error
  10. if err == gorm.ErrRecordNotFound {
  11. return nil, err
  12. }
  13. if err != nil {
  14. return nil, err
  15. }
  16. return &goodStock, nil
  17. }
  18. func CreateGoodCount(good models.XtGoodStockCount) error {
  19. db := writeDb.Begin()
  20. err := db.Create(&good).Error
  21. if err != nil {
  22. db.Rollback()
  23. return err
  24. }
  25. db.Commit()
  26. return err
  27. }
  28. func UpdateGoodStockCount(user_org_id int64, storehouse_id int64, good_id int64, count int64, flush_count int64) error {
  29. db := writeDb.Begin()
  30. err = db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count + ?", count)).Error
  31. if err != nil {
  32. db.Rollback()
  33. return err
  34. }
  35. err = db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  36. if err != nil {
  37. db.Rollback()
  38. return err
  39. }
  40. db.Commit()
  41. return err
  42. }
  43. func UpdateGoodInCount(count int64, user_org_id int64, good_id int64, storehouse_id int64, flush_count int64) error {
  44. db := writeDb.Begin()
  45. err := db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and good_id = ? and storehouse_id = ? and status = 1", user_org_id, good_id, storehouse_id).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count - ?", count)).Error
  46. if err != nil {
  47. db.Rollback()
  48. return err
  49. }
  50. err = db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and good_id = ? and storehouse_id = ? and status = 1", user_org_id, good_id, storehouse_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  51. if err != nil {
  52. db.Rollback()
  53. return err
  54. }
  55. db.Commit()
  56. return err
  57. }
  58. func CreateGoodErrcode(errcode models.XtGoodErrcode) error {
  59. err := writeDb.Create(&errcode).Error
  60. return err
  61. }
  62. func GetGoodStockList(orgId int64, storehouse_id int64, good_type int64, keyword string, page int64, limit int64, ids []int64) (good []*models.GoodInformation, total int64, err error) {
  63. offset := (page - 1) * limit
  64. likeKey := "%" + keyword + "%"
  65. db := XTReadDB().Table("xt_good_information as x").Where("x.status = 1 and x.org_id =? and find_in_set('停用',x.good_status) = 0", orgId)
  66. if good_type == 2 {
  67. db = db.Where(" x.total_count<=x.stock_warn_count")
  68. }
  69. if good_type == 3 {
  70. db = db.Where("x.sum_count = 0")
  71. }
  72. if good_type == 4 {
  73. db = db.Where("x.sum_count > 0")
  74. }
  75. if len(keyword) > 0 {
  76. db = db.Where("good_name like ? or manufacturer in(?)", likeKey, ids)
  77. }
  78. db = db.Preload("XtGoodStockCount", func(db *gorm.DB) *gorm.DB {
  79. if storehouse_id > 0 {
  80. db = db.Where("storehouse_id = ?", storehouse_id)
  81. }
  82. return db.Where("status = 1 and user_org_id = ?", orgId)
  83. })
  84. db = db.Preload("GoodStockCount", func(db *gorm.DB) *gorm.DB {
  85. if storehouse_id > 0 {
  86. db = db.Where("storehouse_id = ?", storehouse_id)
  87. }
  88. return db.Where("status = 1 and user_org_id = ?", orgId).Group("good_id,storehouse_id")
  89. })
  90. db = db.Preload("WarehousingInfo", func(db *gorm.DB) *gorm.DB {
  91. if storehouse_id > 0 {
  92. db = db.Where("storehouse_id = ?", storehouse_id)
  93. }
  94. return db.Where("status = 1 and org_id = ? and is_check = 1", orgId)
  95. })
  96. if orgId == 10489 {
  97. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.sum_count desc").Find(&good).Error
  98. } else {
  99. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Find(&good).Error
  100. }
  101. return good, total, err
  102. }
  103. func GetSumGoodList(orgid int64, storehouse_id int64, good_id int64) (info []*models.WarehousingInfo, err error) {
  104. err = XTReadDB().Where("org_id = ? and storehouse_id = ? and good_id = ? and status =1 and is_check = 1", orgid, storehouse_id, good_id).Find(&info).Error
  105. return info, err
  106. }
  107. func UpdateSumGood(orgid int64, storehouse_id int64, good_id int64, flush_count int64) error {
  108. db := writeDb.Begin()
  109. err := db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", orgid, storehouse_id, good_id).Updates(map[string]interface{}{"flush_count": flush_count}).Error
  110. if err != nil {
  111. db.Rollback()
  112. return err
  113. }
  114. db.Commit()
  115. return err
  116. }
  117. func ReduceSumOutCount(good_id int64, total_count int64, storehouse_id int64, user_org_id int64) error {
  118. ut := XTWriteDB().Begin()
  119. err = ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_out_count", gorm.Expr("stock_out_count - ?", total_count)).Error
  120. err = ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_act_out_count", gorm.Expr("stock_act_out_count - ?", total_count)).Error
  121. if err != nil {
  122. ut.Rollback()
  123. return err
  124. }
  125. ut.Commit()
  126. return err
  127. }
  128. func ReduceSumInCount(good_id int64, total_count int64, storehouse_id int64, user_org_id int64) error {
  129. ut := XTWriteDB().Begin()
  130. err = ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_out_count", gorm.Expr("stock_out_count + ?", total_count)).Error
  131. err = ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_act_out_count", gorm.Expr("stock_act_out_count + ?", total_count)).Error
  132. if err != nil {
  133. ut.Rollback()
  134. return err
  135. }
  136. ut.Commit()
  137. return err
  138. }
  139. func UpdateSumCount(user_org_id int64, storehouse_id int64, good_id int64, count int64) error {
  140. ut := XTWriteDB().Begin()
  141. err = ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_out_count", gorm.Expr("stock_out_count - ?", count)).Error
  142. if err != nil {
  143. ut.Rollback()
  144. return err
  145. }
  146. ut.Commit()
  147. return err
  148. }
  149. func UpdateActSumCount(user_org_id int64, storehouse_id int64, good_id int64, count int64) error {
  150. ut := XTWriteDB().Begin()
  151. err = ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_act_out_count", gorm.Expr("stock_act_out_count - ?", count)).Error
  152. if err != nil {
  153. ut.Rollback()
  154. return err
  155. }
  156. ut.Commit()
  157. return err
  158. }
  159. func GetHisPrescriptionList(orgid int64) (project []*models.HisPrescriptionProject, err error) {
  160. err = XTReadDB().Where("user_org_id = ? and status= 1 and record_date =1690819200", orgid).Find(&project).Error
  161. return project, err
  162. }
  163. func UpdateGoodFlow(project_id int64, price float64) error {
  164. flow := models.VmStockFlow{}
  165. err := XTWriteDB().Model(&flow).Where("warehousing_id = ? and user_org_id =10265", project_id).Updates(map[string]interface{}{"price": price}).Error
  166. err = XTWriteDB().Model(models.WarehouseOutInfo{}).Where("warehouse_info_id = ? and org_id =10265", project_id).Updates(map[string]interface{}{"price": price}).Error
  167. return err
  168. }
  169. func GetSendGoodInformation(org_id int64) (info []*models.WarehousingInfo, err error) {
  170. err = XTReadDB().Where("org_id = ? and is_check = 1 and status = 1", org_id).Find(&info).Error
  171. return info, err
  172. }
  173. func GetStockFlush(storehouse_id int64, good_id int64, orgid int64) (*models.XtGoodStockCount, error) {
  174. stockCount := models.XtGoodStockCount{}
  175. var err error
  176. err = XTReadDB().Where("storehouse_id =? and good_id = ? and user_org_id = ?", storehouse_id, good_id, orgid).Find(&stockCount).Error
  177. if err == gorm.ErrRecordNotFound {
  178. return nil, err
  179. }
  180. if err != nil {
  181. return nil, err
  182. }
  183. return &stockCount, nil
  184. }
  185. func CreateGoodCountSix(good models.XtGoodStockCount) error {
  186. err := XTWriteDB().Create(&good).Error
  187. return err
  188. }
  189. func UpdateGoodCount(warehousingCount int64, stock_count int64, out_count int64, storehouse_id int64, good_id int64, orgid int64, cancel_count int64, act_count int64) error {
  190. err := XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and good_id = ? and status =1 and user_org_id = ?", storehouse_id, good_id, orgid).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count+?", warehousingCount)).Error
  191. err = XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and good_id = ? and status =1 and user_org_id = ?", storehouse_id, good_id, orgid).Update(map[string]interface{}{"stock_out_count": out_count}).Error
  192. err = XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and good_id = ? and status =1 and user_org_id = ?", storehouse_id, good_id, orgid).UpdateColumn("flush_count", gorm.Expr("flush_count + ?", stock_count)).Error
  193. err = XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and good_id = ? and status =1 and user_org_id = ?", storehouse_id, good_id, orgid).Update(map[string]interface{}{"stock_cancel_count": cancel_count}).Error
  194. err = XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and good_id = ? and status =1 and user_org_id = ?", storehouse_id, good_id, orgid).Update(map[string]interface{}{"stock_act_out_count": act_count}).Error
  195. return err
  196. }
  197. func UpdateGoodCountSend(warehousingCount int64, stock_count int64, out_count int64, storehouse_id int64, good_id int64, orgid int64, cancel_count int64, act_count int64) error {
  198. err := XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and good_id = ? and status =1 and user_org_id = ?", storehouse_id, good_id, orgid).Updates(map[string]interface{}{"stock_in_count": warehousingCount}).Error
  199. err = XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and good_id = ? and status =1 and user_org_id = ?", storehouse_id, good_id, orgid).Update(map[string]interface{}{"stock_out_count": out_count}).Error
  200. err = XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and good_id = ? and status =1 and user_org_id = ?", storehouse_id, good_id, orgid).Update(map[string]interface{}{"flush_count": stock_count}).Error
  201. err = XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and good_id = ? and status =1 and user_org_id = ?", storehouse_id, good_id, orgid).Update(map[string]interface{}{"stock_cancel_count": cancel_count}).Error
  202. err = XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and good_id = ? and status =1 and user_org_id = ?", storehouse_id, good_id, orgid).Update(map[string]interface{}{"stock_act_out_count": act_count}).Error
  203. return err
  204. }
  205. func GetAllCancelCount(storehouse_id int64, good_id int64, org_id int64) (flow []*models.VmStockFlow, err error) {
  206. err = XTReadDB().Where("storehouse_id = ? and good_id = ? and user_org_id = ? and (consumable_type = 4 or consumable_type= 7) and status = 1", storehouse_id, good_id, org_id).Find(&flow).Error
  207. return flow, err
  208. }
  209. func GetAllStockOutCount(storehouse_id int64, good_id int64, org_id int64) (flow []*models.VmStockFlow, err error) {
  210. err = XTReadDB().Where("storehouse_id = ? and good_id = ? and user_org_id = ? and (consumable_type = 2 or consumable_type= 3)", storehouse_id, good_id, org_id).Find(&flow).Error
  211. return flow, err
  212. }
  213. func GetActStockOutCount(storehouse_id int64, good_id int64, org_id int64) (flow []*models.VmStockFlow, err error) {
  214. err = XTReadDB().Where("storehouse_id = ? and good_id = ? and user_org_id = ? and (consumable_type = 2 or consumable_type= 3) and status = 1", storehouse_id, good_id, org_id).Find(&flow).Error
  215. return flow, err
  216. }
  217. func GetDrugStockCount(storehouse_id int64, drug_id int64, org_id int64) (*models.XtDrugStockCount, error) {
  218. drugStockCount := models.XtDrugStockCount{}
  219. var err error
  220. err = XTReadDB().Where("storehouse_id = ? and drug_id = ? and user_org_id = ? and status = 1", storehouse_id, drug_id, org_id).Find(&drugStockCount).Error
  221. if err == gorm.ErrRecordNotFound {
  222. return nil, err
  223. }
  224. if err != nil {
  225. return nil, err
  226. }
  227. return &drugStockCount, nil
  228. }
  229. func GetDrugStockCountSix(storehouse_id int64, drug_id int64, org_id int64) (models.XtDrugStockCount, error) {
  230. drugStockCount := models.XtDrugStockCount{}
  231. err = XTReadDB().Where("storehouse_id = ? and drug_id = ? and user_org_id = ? and status = 1", storehouse_id, drug_id, org_id).Find(&drugStockCount).Error
  232. return drugStockCount, err
  233. }
  234. func CreateDrugStockSum(drug models.XtDrugStockCount) error {
  235. ut := XTWriteDB().Begin()
  236. err := ut.Create(&drug).Error
  237. if err != nil {
  238. ut.Rollback()
  239. return err
  240. }
  241. ut.Commit()
  242. return err
  243. }
  244. func AddDrugStockSum(storehouse_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64) error {
  245. db := writeDb.Begin()
  246. err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count + ?", waresing_count)).Error
  247. if err != nil {
  248. db.Rollback()
  249. return err
  250. }
  251. err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).Update(map[string]interface{}{"flush_count": over_count}).Error
  252. if err != nil {
  253. db.Rollback()
  254. return err
  255. }
  256. db.Commit()
  257. return err
  258. }
  259. func UpdateDrugStockSum(storehouse_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64) error {
  260. db := writeDb.Begin()
  261. err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).Update(map[string]interface{}{"sum_in_count": waresing_count}).Error
  262. if err != nil {
  263. db.Rollback()
  264. return err
  265. }
  266. err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).Update(map[string]interface{}{"flush_count": over_count}).Error
  267. if err != nil {
  268. db.Rollback()
  269. return err
  270. }
  271. db.Commit()
  272. return err
  273. }
  274. func ReduceDrugStockCount(storehouse_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64) error {
  275. db := writeDb.Begin()
  276. err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count - ?", waresing_count)).Error
  277. if err != nil {
  278. db.Rollback()
  279. return err
  280. }
  281. err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).Update(map[string]interface{}{"flush_count": over_count}).Error
  282. if err != nil {
  283. db.Rollback()
  284. return err
  285. }
  286. db.Commit()
  287. return err
  288. }
  289. func AddCancelSumCount(storehouse_id int64, drug_id int64, user_org_id int64, cancel_count int64, over_count int64) error {
  290. db := writeDb.Begin()
  291. err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count + ?", cancel_count)).Error
  292. if err != nil {
  293. db.Rollback()
  294. return err
  295. }
  296. err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).Update(map[string]interface{}{"flush_count": over_count}).Error
  297. if err != nil {
  298. db.Rollback()
  299. return err
  300. }
  301. db.Commit()
  302. return err
  303. }
  304. func AddCancelSumCountOne(storehouse_id int64, drug_id int64, user_org_id int64, cancel_count int64) error {
  305. db := writeDb.Begin()
  306. err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count + ?", cancel_count)).Error
  307. if err != nil {
  308. db.Rollback()
  309. return err
  310. }
  311. db.Commit()
  312. return err
  313. }
  314. func AddSelfCancelSumCountOne(patient_id int64, drug_id int64, user_org_id int64, cancel_count int64) error {
  315. db := writeDb.Begin()
  316. err = db.Model(&models.XtSelfDrugStockCount{}).Where("user_org_id = ? and patient_id = ? and drug_id = ? and status = 1", user_org_id, patient_id, drug_id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count + ?", cancel_count)).Error
  317. if err != nil {
  318. db.Rollback()
  319. return err
  320. }
  321. db.Commit()
  322. return err
  323. }
  324. func AddNewCancelSumCountOne(storehouse_id int64, drug_id int64, user_org_id int64, cancel_count int64, tx *gorm.DB) error {
  325. err = tx.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count + ?", cancel_count)).Error
  326. if err != nil {
  327. tx.Rollback()
  328. return err
  329. }
  330. return err
  331. }
  332. func ReduceCancelSumCount(storehouse_id int64, drug_id int64, user_org_id int64, cancel_count int64, over_count int64) error {
  333. db := writeDb.Begin()
  334. err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count - ?", cancel_count)).Error
  335. if err != nil {
  336. db.Rollback()
  337. return err
  338. }
  339. err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).Update(map[string]interface{}{"flush_count": over_count}).Error
  340. if err != nil {
  341. db.Rollback()
  342. return err
  343. }
  344. db.Commit()
  345. return err
  346. }
  347. func UpdateSumDrug(orgid int64, storehouse_id int64, drug_id int64, flush_count int64) error {
  348. db := writeDb.Begin()
  349. err := db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", orgid, storehouse_id, drug_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  350. if err != nil {
  351. db.Rollback()
  352. return err
  353. }
  354. db.Commit()
  355. return err
  356. }
  357. func UpdateSumDrugTwo(orgid int64, patient_id int64, drug_id int64, flush_count int64) error {
  358. db := writeDb.Begin()
  359. err := db.Model(&models.XtSelfDrugStockCount{}).Where("user_org_id = ? and patient_id = ? and drug_id = ? and status = 1", orgid, patient_id, drug_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  360. if err != nil {
  361. db.Rollback()
  362. return err
  363. }
  364. db.Commit()
  365. return err
  366. }
  367. func UpdateSumOutDrug(orgid int64, storehouse_id int64, drug_id int64, sum_out_count int64) error {
  368. db := writeDb.Begin()
  369. err := db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", orgid, storehouse_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", sum_out_count)).Error
  370. if err != nil {
  371. db.Rollback()
  372. return err
  373. }
  374. db.Commit()
  375. return err
  376. }
  377. func UpdateSelfSumOutDrug(orgid int64, patient_id int64, drug_id int64, sum_out_count int64) error {
  378. db := writeDb.Begin()
  379. err := db.Model(&models.XtSelfDrugStockCount{}).Where("user_org_id = ? and patient_id = ? and drug_id = ? and status = 1", orgid, patient_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", sum_out_count)).Error
  380. if err != nil {
  381. db.Rollback()
  382. return err
  383. }
  384. db.Commit()
  385. return err
  386. }
  387. func GetAutoGoodLastCount(goodid int64, count int64, record_time int64, patient_id int64) (models.AutomaticReduceDetail, error) {
  388. detail := models.AutomaticReduceDetail{}
  389. err := XTReadDB().Where("good_id = ? and count = ? and record_time = ? and patient_id = ?", goodid, count, record_time, patient_id).Last(&detail).Error
  390. return detail, err
  391. }
  392. func UpdateLastAutoCount(id int64, count int64) error {
  393. detail := models.AutomaticReduceDetail{}
  394. err := XTWriteDB().Model(&detail).Where("id = ?", id).Update(map[string]interface{}{"count": count, "status": 1}).Error
  395. return err
  396. }
  397. func GetWareOutInfo(good_id int64, record_date int64, user_org_id int64, patient_id int64, order_id int64) (outinfo []*models.WarehouseOutInfo, err error) {
  398. err = XTReadDB().Where("good_id = ? and sys_record_time = ? and org_id = ? and status = 1 and patient_id = ? and order_id = ?", good_id, record_date, user_org_id, patient_id, order_id).Find(&outinfo).Error
  399. return outinfo, err
  400. }
  401. func AddGoodStockCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  402. db := writeDb.Begin()
  403. err = db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count + ?", count)).Error
  404. if err != nil {
  405. db.Rollback()
  406. return err
  407. }
  408. err = db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).UpdateColumn("flush_count", gorm.Expr("flush_count + ?", count)).Error
  409. if err != nil {
  410. db.Rollback()
  411. return err
  412. }
  413. db.Commit()
  414. return err
  415. }
  416. func AddDrugStockCount(storehouse_id int64, drug_id int64, user_org_id int64, count int64) error {
  417. db := writeDb.Begin()
  418. err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count + ?", count)).Error
  419. if err != nil {
  420. db.Rollback()
  421. return err
  422. }
  423. err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("flush_count", gorm.Expr("flush_count + ?", count)).Error
  424. if err != nil {
  425. db.Rollback()
  426. return err
  427. }
  428. db.Commit()
  429. return err
  430. }
  431. func ReduceStockCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  432. db := writeDb.Begin()
  433. //err = db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count - ?", count)).Error
  434. //if err != nil {
  435. // db.Rollback()
  436. // return err
  437. //}
  438. err = db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).UpdateColumn("stock_out_count", gorm.Expr("stock_out_count + ?", count)).Error
  439. if err != nil {
  440. db.Rollback()
  441. return err
  442. }
  443. err = db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).UpdateColumn("stock_act_out_count", gorm.Expr("stock_act_out_count + ?", count)).Error
  444. if err != nil {
  445. db.Rollback()
  446. return err
  447. }
  448. err = db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).UpdateColumn("flush_count", gorm.Expr("flush_count - ?", count)).Error
  449. if err != nil {
  450. db.Rollback()
  451. return err
  452. }
  453. db.Commit()
  454. return err
  455. }
  456. func ReduceStockCountTwo(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  457. db := writeDb.Begin()
  458. err = db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).UpdateColumn("stock_out_count", gorm.Expr("stock_out_count + ?", count)).Error
  459. if err != nil {
  460. db.Rollback()
  461. return err
  462. }
  463. err = db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).UpdateColumn("stock_act_out_count", gorm.Expr("stock_act_out_count + ?", count)).Error
  464. if err != nil {
  465. db.Rollback()
  466. return err
  467. }
  468. err = db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).UpdateColumn("flush_count", gorm.Expr("flush_count - ?", count)).Error
  469. if err != nil {
  470. db.Rollback()
  471. return err
  472. }
  473. db.Commit()
  474. return err
  475. }
  476. func ReduceDrugStockCountSix(storehouse_id int64, drug_id int64, user_org_id int64, count int64) error {
  477. db := writeDb.Begin()
  478. //err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count - ?", count)).Error
  479. //if err != nil {
  480. // db.Rollback()
  481. // return err
  482. //}
  483. err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count + ?", count)).Error
  484. if err != nil {
  485. db.Rollback()
  486. return err
  487. }
  488. err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count + ?", count)).Error
  489. if err != nil {
  490. db.Rollback()
  491. return err
  492. }
  493. err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("flush_count", gorm.Expr("flush_count - ?", count)).Error
  494. if err != nil {
  495. db.Rollback()
  496. return err
  497. }
  498. db.Commit()
  499. return err
  500. }
  501. func ReduceDrugStockCountSeven(storehouse_id int64, drug_id int64, user_org_id int64, count int64) error {
  502. db := writeDb.Begin()
  503. err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count + ?", count)).Error
  504. if err != nil {
  505. db.Rollback()
  506. return err
  507. }
  508. err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count + ?", count)).Error
  509. if err != nil {
  510. db.Rollback()
  511. return err
  512. }
  513. err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("flush_count", gorm.Expr("flush_count - ?", count)).Error
  514. if err != nil {
  515. db.Rollback()
  516. return err
  517. }
  518. db.Commit()
  519. return err
  520. }
  521. func GetWarehouseOutInfoByProjectIdList(project_id int64, org_id int64, storehouse_id int64, good_id int64, patient_id int64, sys_record_time int64) (info []*models.WarehouseOutInfoNight, err error) {
  522. err = XTReadDB().Where("project_id = ? and org_id = ? and storehouse_id = ? and good_id = ? and patient_id = ? and sys_record_time = ? and status = 1", project_id, org_id, storehouse_id, good_id, patient_id, sys_record_time).Find(&info).Error
  523. return info, err
  524. }
  525. func GetWarehouseStockFlow(warehouse_info_id int64, good_id int64, user_org_id int64, patient_id int64, system_time int64) (models.VmStockFlow, error) {
  526. stockFlow := models.VmStockFlow{}
  527. err := XTReadDB().Where("warehousing_detail_id = ? and good_id = ? and user_org_id = ? and patient_id = ? AND consumable_type = 7 and system_time = ?", warehouse_info_id, good_id, user_org_id, patient_id, system_time).Find(&stockFlow).Error
  528. return stockFlow, err
  529. }
  530. func AddCountFlowOne(warehouse_info_id int64, good_id int64, user_org_id int64, patient_id int64, count int64, system_time int64, over_count int64) error {
  531. stockFlow := models.VmStockFlow{}
  532. err := XTWriteDB().Model(&stockFlow).Where("warehousing_detail_id = ? and good_id = ? and user_org_id = ? and patient_id = ? AND consumable_type = 7 and system_time = ?", warehouse_info_id, good_id, user_org_id, patient_id, system_time).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
  533. err = XTWriteDB().Model(&stockFlow).Where("warehousing_detail_id = ? and good_id = ? and user_org_id = ? and patient_id = ? AND consumable_type = 7 and system_time = ?").Update(map[string]interface{}{"over_count": over_count}).Error
  534. return err
  535. }
  536. func GetWarehouseOutInfo(user_org_id int64) (info []*models.WarehouseOut, err error) {
  537. err = XTReadDB().Where("org_id = ? and status =1", 10485).Find(&info).Error
  538. return info, err
  539. }
  540. func UpdateWarehosueOutInfo(id int64, sys_record_time int64) (models.WarehouseOutInfo, error) {
  541. info := models.WarehouseOutInfo{}
  542. err := XTWriteDB().Model(&info).Where("warehouse_out_id = ? and org_id = ? ", id, 10485).Updates(map[string]interface{}{"sys_record_time": sys_record_time, "ctime": sys_record_time}).Error
  543. return info, err
  544. }
  545. func AddGoodNewCancelSumCountOne(storehouse_id int64, good_id int64, user_org_id int64, cancel_count int64, tx *gorm.DB) error {
  546. err = tx.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).UpdateColumn("stock_cancel_count", gorm.Expr("stock_cancel_count + ?", cancel_count)).Error
  547. if err != nil {
  548. tx.Rollback()
  549. return err
  550. }
  551. return err
  552. }
  553. func GetDrugNewInventoryList(org_id int64, keyword string) (drug []*models.BaseDrugLibTwentyOne, err error) {
  554. likeKey := "%" + keyword + "%"
  555. db := XTReadDB().Where("status=1")
  556. if org_id > 0 {
  557. db = db.Where("org_id = ?", org_id)
  558. }
  559. if len(keyword) > 0 {
  560. db = db.Where("drug_name like ?", likeKey)
  561. }
  562. err = db.Order("id desc").Find(&drug).Error
  563. return drug, err
  564. }
  565. func GetSelfDrugStockCount(patient_id int64, drug_id int64, org_id int64, tx *gorm.DB) (*models.XtSelfDrugStockCount, error) {
  566. drugStockCount := models.XtSelfDrugStockCount{}
  567. var err error
  568. err = tx.Where("patient_id = ? and drug_id = ? and user_org_id = ? and status = 1", patient_id, drug_id, org_id).Find(&drugStockCount).Error
  569. if err != gorm.ErrRecordNotFound {
  570. if err != nil {
  571. tx.Rollback()
  572. return nil, err
  573. }
  574. }
  575. return &drugStockCount, nil
  576. }
  577. func UpdateSelfDrugStockSum(patient_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64, tx *gorm.DB) error {
  578. err = tx.Model(&models.XtSelfDrugStockCount{}).Where("user_org_id = ? and patient_id = ? and drug_id = ? and status = 1", user_org_id, patient_id, drug_id).Update(map[string]interface{}{"sum_in_count": waresing_count}).Error
  579. if err != nil {
  580. tx.Rollback()
  581. return err
  582. }
  583. err = tx.Model(&models.XtSelfDrugStockCount{}).Where("user_org_id = ? and patient_id = ? and drug_id = ? and status = 1", user_org_id, patient_id, drug_id).Update(map[string]interface{}{"flush_count": over_count}).Error
  584. if err != nil {
  585. tx.Rollback()
  586. return err
  587. }
  588. return err
  589. }
  590. func GetSelfDrugBatchNumber(drug_id int64, org_id int64) (info []*models.XtSelfDrugWarehouseInfo, err error) {
  591. err = XTReadDB().Model(&info).Where("drug_id = ? and org_id = ? and status=1 and batch_number <>''", drug_id, org_id).Find(&info).Error
  592. return info, err
  593. }