new_stock_service.go 35KB

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