new_stock_service.go 35KB

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