new_stock_service.go 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. db := writeDb.Begin()
  60. err := db.Create(&errcode).Error
  61. if err != nil {
  62. db.Rollback()
  63. return err
  64. }
  65. db.Commit()
  66. return err
  67. }
  68. 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) {
  69. offset := (page - 1) * limit
  70. likeKey := "%" + keyword + "%"
  71. 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)
  72. if good_type == 2 {
  73. db = db.Where(" x.total_count<=x.stock_warn_count")
  74. }
  75. if good_type == 3 {
  76. db = db.Where("x.sum_count = 0")
  77. }
  78. if len(keyword) > 0 {
  79. db = db.Where("good_name like ? or manufacturer in(?)", likeKey, ids)
  80. }
  81. db = db.Preload("XtGoodStockCount", func(db *gorm.DB) *gorm.DB {
  82. if storehouse_id > 0 {
  83. db = db.Where("storehouse_id = ?", storehouse_id)
  84. }
  85. return db.Where("status = 1 and user_org_id = ?", orgId)
  86. })
  87. db = db.Preload("GoodStockCount", func(db *gorm.DB) *gorm.DB {
  88. if storehouse_id > 0 {
  89. db = db.Where("storehouse_id = ?", storehouse_id)
  90. }
  91. return db.Where("status = 1 and user_org_id = ?", orgId).Group("good_id,storehouse_id")
  92. })
  93. db = db.Preload("WarehousingInfo", func(db *gorm.DB) *gorm.DB {
  94. if storehouse_id > 0 {
  95. db = db.Where("storehouse_id = ?", storehouse_id)
  96. }
  97. return db.Where("status = 1 and org_id = ? and is_check = 1", orgId)
  98. })
  99. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Find(&good).Error
  100. return good, total, err
  101. }
  102. func GetSumGoodList(orgid int64, storehouse_id int64, good_id int64) (info []*models.WarehousingInfo, err error) {
  103. 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
  104. return info, err
  105. }
  106. func UpdateSumGood(orgid int64, storehouse_id int64, good_id int64, flush_count int64) error {
  107. db := writeDb.Begin()
  108. 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
  109. if err != nil {
  110. db.Rollback()
  111. return err
  112. }
  113. db.Commit()
  114. return err
  115. }
  116. func UpdateSumCount(user_org_id int64, storehouse_id int64, good_id int64, count int64) error {
  117. ut := XTWriteDB().Begin()
  118. 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
  119. if err != nil {
  120. ut.Rollback()
  121. return err
  122. }
  123. ut.Commit()
  124. return err
  125. }
  126. func UpdateActSumCount(user_org_id int64, storehouse_id int64, good_id int64, count int64) error {
  127. ut := XTWriteDB().Begin()
  128. 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
  129. if err != nil {
  130. ut.Rollback()
  131. return err
  132. }
  133. ut.Commit()
  134. return err
  135. }
  136. func GetSendGoodInformation(org_id int64) (info []*models.WarehousingInfo, err error) {
  137. err = XTReadDB().Where("org_id = ? and is_check = 1 and status = 1", org_id).Find(&info).Error
  138. return info, err
  139. }
  140. func GetStockFlush(storehouse_id int64, good_id int64, orgid int64) (*models.XtGoodStockCount, error) {
  141. stockCount := models.XtGoodStockCount{}
  142. var err error
  143. err = XTReadDB().Where("storehouse_id =? and good_id = ? and user_org_id = ?", storehouse_id, good_id, orgid).Find(&stockCount).Error
  144. if err == gorm.ErrRecordNotFound {
  145. return nil, err
  146. }
  147. if err != nil {
  148. return nil, err
  149. }
  150. return &stockCount, nil
  151. }
  152. func CreateGoodCountSix(good models.XtGoodStockCount) error {
  153. err := XTWriteDB().Create(&good).Error
  154. return err
  155. }
  156. 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 {
  157. 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
  158. 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
  159. 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
  160. 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
  161. 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
  162. return err
  163. }
  164. 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 {
  165. 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
  166. 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
  167. 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
  168. 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
  169. 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
  170. return err
  171. }
  172. func GetAllCancelCount(storehouse_id int64, good_id int64, org_id int64) (flow []*models.VmStockFlow, err error) {
  173. 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
  174. return flow, err
  175. }
  176. func GetAllStockOutCount(storehouse_id int64, good_id int64, org_id int64) (flow []*models.VmStockFlow, err error) {
  177. 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
  178. return flow, err
  179. }
  180. func GetActStockOutCount(storehouse_id int64, good_id int64, org_id int64) (flow []*models.VmStockFlow, err error) {
  181. 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
  182. return flow, err
  183. }
  184. func GetDrugStockCount(storehouse_id int64, drug_id int64, org_id int64) (*models.XtDrugStockCount, error) {
  185. drugStockCount := models.XtDrugStockCount{}
  186. var err error
  187. err = XTReadDB().Where("storehouse_id = ? and drug_id = ? and user_org_id = ? and status = 1", storehouse_id, drug_id, org_id).Find(&drugStockCount).Error
  188. if err == gorm.ErrRecordNotFound {
  189. return nil, err
  190. }
  191. if err != nil {
  192. return nil, err
  193. }
  194. return &drugStockCount, nil
  195. }
  196. func GetDrugStockCountSix(storehouse_id int64, drug_id int64, org_id int64) (models.XtDrugStockCount, error) {
  197. drugStockCount := models.XtDrugStockCount{}
  198. err = XTReadDB().Where("storehouse_id = ? and drug_id = ? and user_org_id = ? and status = 1", storehouse_id, drug_id, org_id).Find(&drugStockCount).Error
  199. return drugStockCount, err
  200. }
  201. func CreateDrugStockSum(drug models.XtDrugStockCount) error {
  202. ut := XTWriteDB().Begin()
  203. err := ut.Create(&drug).Error
  204. if err != nil {
  205. ut.Rollback()
  206. return err
  207. }
  208. ut.Commit()
  209. return err
  210. }
  211. func AddDrugStockSum(storehouse_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64) error {
  212. db := writeDb.Begin()
  213. 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
  214. if err != nil {
  215. db.Rollback()
  216. return err
  217. }
  218. 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
  219. if err != nil {
  220. db.Rollback()
  221. return err
  222. }
  223. db.Commit()
  224. return err
  225. }
  226. func UpdateDrugStockSum(storehouse_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64) error {
  227. db := writeDb.Begin()
  228. 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
  229. if err != nil {
  230. db.Rollback()
  231. return err
  232. }
  233. 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
  234. if err != nil {
  235. db.Rollback()
  236. return err
  237. }
  238. db.Commit()
  239. return err
  240. }
  241. func ReduceDrugStockCount(storehouse_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64) error {
  242. db := writeDb.Begin()
  243. 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
  244. if err != nil {
  245. db.Rollback()
  246. return err
  247. }
  248. 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
  249. if err != nil {
  250. db.Rollback()
  251. return err
  252. }
  253. db.Commit()
  254. return err
  255. }
  256. func AddCancelSumCount(storehouse_id int64, drug_id int64, user_org_id int64, cancel_count int64, over_count int64) error {
  257. db := writeDb.Begin()
  258. 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
  259. if err != nil {
  260. db.Rollback()
  261. return err
  262. }
  263. 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
  264. if err != nil {
  265. db.Rollback()
  266. return err
  267. }
  268. db.Commit()
  269. return err
  270. }
  271. func AddCancelSumCountOne(storehouse_id int64, drug_id int64, user_org_id int64, cancel_count int64) error {
  272. db := writeDb.Begin()
  273. 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
  274. if err != nil {
  275. db.Rollback()
  276. return err
  277. }
  278. db.Commit()
  279. return err
  280. }
  281. func ReduceCancelSumCount(storehouse_id int64, drug_id int64, user_org_id int64, cancel_count int64, over_count int64) error {
  282. db := writeDb.Begin()
  283. 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
  284. if err != nil {
  285. db.Rollback()
  286. return err
  287. }
  288. 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
  289. if err != nil {
  290. db.Rollback()
  291. return err
  292. }
  293. db.Commit()
  294. return err
  295. }
  296. func UpdateSumDrug(orgid int64, storehouse_id int64, drug_id int64, flush_count int64) error {
  297. db := writeDb.Begin()
  298. 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
  299. if err != nil {
  300. db.Rollback()
  301. return err
  302. }
  303. db.Commit()
  304. return err
  305. }
  306. func UpdateSumOutDrug(orgid int64, storehouse_id int64, drug_id int64, sum_out_count int64) error {
  307. db := writeDb.Begin()
  308. 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
  309. if err != nil {
  310. db.Rollback()
  311. return err
  312. }
  313. db.Commit()
  314. return err
  315. }
  316. func GetAutoGoodLastCount(goodid int64, count int64, record_time int64, patient_id int64) (models.AutomaticReduceDetail, error) {
  317. detail := models.AutomaticReduceDetail{}
  318. err := XTReadDB().Where("good_id = ? and count = ? and record_time = ? and patient_id = ?", goodid, count, record_time, patient_id).Last(&detail).Error
  319. return detail, err
  320. }
  321. func UpdateLastAutoCount(id int64, count int64) error {
  322. detail := models.AutomaticReduceDetail{}
  323. err := XTWriteDB().Model(&detail).Where("id = ?", id).Update(map[string]interface{}{"count": count, "status": 1}).Error
  324. return err
  325. }
  326. func GetWareOutInfo(good_id int64, record_date int64, user_org_id int64, patient_id int64, order_id int64) (outinfo []*models.WarehouseOutInfo, err error) {
  327. 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
  328. return outinfo, err
  329. }
  330. func AddGoodStockCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  331. db := writeDb.Begin()
  332. 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
  333. if err != nil {
  334. db.Rollback()
  335. return err
  336. }
  337. 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
  338. if err != nil {
  339. db.Rollback()
  340. return err
  341. }
  342. db.Commit()
  343. return err
  344. }
  345. func AddDrugStockCount(storehouse_id int64, drug_id int64, user_org_id int64, count int64) error {
  346. db := writeDb.Begin()
  347. 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
  348. if err != nil {
  349. db.Rollback()
  350. return err
  351. }
  352. 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
  353. if err != nil {
  354. db.Rollback()
  355. return err
  356. }
  357. db.Commit()
  358. return err
  359. }
  360. func ReduceStockCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  361. db := writeDb.Begin()
  362. //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
  363. //if err != nil {
  364. // db.Rollback()
  365. // return err
  366. //}
  367. 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
  368. if err != nil {
  369. db.Rollback()
  370. return err
  371. }
  372. 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
  373. if err != nil {
  374. db.Rollback()
  375. return err
  376. }
  377. 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
  378. if err != nil {
  379. db.Rollback()
  380. return err
  381. }
  382. db.Commit()
  383. return err
  384. }
  385. func ReduceStockCountTwo(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  386. db := writeDb.Begin()
  387. 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
  388. if err != nil {
  389. db.Rollback()
  390. return err
  391. }
  392. 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
  393. if err != nil {
  394. db.Rollback()
  395. return err
  396. }
  397. 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
  398. if err != nil {
  399. db.Rollback()
  400. return err
  401. }
  402. db.Commit()
  403. return err
  404. }
  405. func ReduceDrugStockCountSix(storehouse_id int64, drug_id int64, user_org_id int64, count int64) error {
  406. db := writeDb.Begin()
  407. //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
  408. //if err != nil {
  409. // db.Rollback()
  410. // return err
  411. //}
  412. 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
  413. if err != nil {
  414. db.Rollback()
  415. return err
  416. }
  417. 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
  418. if err != nil {
  419. db.Rollback()
  420. return err
  421. }
  422. 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
  423. if err != nil {
  424. db.Rollback()
  425. return err
  426. }
  427. db.Commit()
  428. return err
  429. }
  430. func ReduceDrugStockCountSeven(storehouse_id int64, drug_id int64, user_org_id int64, count int64) error {
  431. db := writeDb.Begin()
  432. 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
  433. if err != nil {
  434. db.Rollback()
  435. return err
  436. }
  437. 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
  438. if err != nil {
  439. db.Rollback()
  440. return err
  441. }
  442. 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
  443. if err != nil {
  444. db.Rollback()
  445. return err
  446. }
  447. db.Commit()
  448. return err
  449. }
  450. 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) {
  451. 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
  452. return info, err
  453. }
  454. func GetWarehouseStockFlow(warehouse_info_id int64, good_id int64, user_org_id int64, patient_id int64, system_time int64) (models.VmStockFlow, error) {
  455. stockFlow := models.VmStockFlow{}
  456. 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
  457. return stockFlow, err
  458. }
  459. 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 {
  460. stockFlow := models.VmStockFlow{}
  461. 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
  462. 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
  463. return err
  464. }