new_stock_service.go 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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. return nil, err
  289. }
  290. if err != nil {
  291. return nil, err
  292. }
  293. return &drugStockCount, nil
  294. }
  295. func GetDrugStockCountSix(storehouse_id int64, drug_id int64, org_id int64) (models.XtDrugStockCount, error) {
  296. drugStockCount := models.XtDrugStockCount{}
  297. err = XTReadDB().Where("storehouse_id = ? and drug_id = ? and user_org_id = ? and status = 1", storehouse_id, drug_id, org_id).Find(&drugStockCount).Error
  298. return drugStockCount, err
  299. }
  300. func CreateDrugStockSum(drug models.XtDrugStockCount) error {
  301. ut := XTWriteDB().Begin()
  302. err := ut.Create(&drug).Error
  303. if err != nil {
  304. ut.Rollback()
  305. return err
  306. }
  307. ut.Commit()
  308. return err
  309. }
  310. func CreateNewDrugStockSum(drug models.XtDrugStockCount, tx *gorm.DB) error {
  311. err := tx.Create(&drug).Error
  312. if err != nil {
  313. tx.Rollback()
  314. return err
  315. }
  316. return err
  317. }
  318. func AddDrugStockSum(storehouse_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64) error {
  319. db := writeDb.Begin()
  320. 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
  321. if err != nil {
  322. db.Rollback()
  323. return err
  324. }
  325. 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
  326. if err != nil {
  327. db.Rollback()
  328. return err
  329. }
  330. db.Commit()
  331. return err
  332. }
  333. func UpdateDrugStockSum(storehouse_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64) error {
  334. db := writeDb.Begin()
  335. 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
  336. if err != nil {
  337. db.Rollback()
  338. return err
  339. }
  340. 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
  341. if err != nil {
  342. db.Rollback()
  343. return err
  344. }
  345. db.Commit()
  346. return err
  347. }
  348. func UpdateNewDrugStockSum(storehouse_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64, tx *gorm.DB) error {
  349. 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
  350. if err != nil {
  351. tx.Rollback()
  352. return err
  353. }
  354. 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
  355. if err != nil {
  356. tx.Rollback()
  357. return err
  358. }
  359. return err
  360. }
  361. func ReduceDrugStockCount(storehouse_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64) error {
  362. db := writeDb.Begin()
  363. 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
  364. if err != nil {
  365. db.Rollback()
  366. return err
  367. }
  368. 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
  369. if err != nil {
  370. db.Rollback()
  371. return err
  372. }
  373. db.Commit()
  374. return err
  375. }
  376. func ReduceNewDrugStockCount(storehouse_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64, tx *gorm.DB) error {
  377. 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
  378. if err != nil {
  379. tx.Rollback()
  380. return err
  381. }
  382. 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
  383. if err != nil {
  384. tx.Rollback()
  385. return err
  386. }
  387. return err
  388. }
  389. func AddCancelSumCount(storehouse_id int64, drug_id int64, user_org_id int64, cancel_count int64, over_count int64) error {
  390. db := writeDb.Begin()
  391. 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
  392. if err != nil {
  393. db.Rollback()
  394. return err
  395. }
  396. 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
  397. if err != nil {
  398. db.Rollback()
  399. return err
  400. }
  401. db.Commit()
  402. return err
  403. }
  404. func AddCancelSumCountOne(storehouse_id int64, drug_id int64, user_org_id int64, cancel_count int64) error {
  405. db := writeDb.Begin()
  406. 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
  407. if err != nil {
  408. db.Rollback()
  409. return err
  410. }
  411. db.Commit()
  412. return err
  413. }
  414. func AddSelfCancelSumCountOne(patient_id int64, drug_id int64, user_org_id int64, cancel_count int64) error {
  415. db := writeDb.Begin()
  416. 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
  417. if err != nil {
  418. db.Rollback()
  419. return err
  420. }
  421. db.Commit()
  422. return err
  423. }
  424. func AddNewCancelSumCountOne(storehouse_id int64, drug_id int64, user_org_id int64, cancel_count int64, tx *gorm.DB) error {
  425. 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
  426. if err != nil {
  427. tx.Rollback()
  428. return err
  429. }
  430. return err
  431. }
  432. func ReduceCancelSumCount(storehouse_id int64, drug_id int64, user_org_id int64, cancel_count int64, over_count int64) error {
  433. db := writeDb.Begin()
  434. 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
  435. if err != nil {
  436. db.Rollback()
  437. return err
  438. }
  439. 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
  440. if err != nil {
  441. db.Rollback()
  442. return err
  443. }
  444. db.Commit()
  445. return err
  446. }
  447. func UpdateSumDrug(orgid int64, storehouse_id int64, drug_id int64, flush_count int64) error {
  448. db := writeDb.Begin()
  449. 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
  450. if err != nil {
  451. db.Rollback()
  452. return err
  453. }
  454. db.Commit()
  455. return err
  456. }
  457. func UpdateSumDrugTwo(orgid int64, patient_id int64, drug_id int64, flush_count int64) error {
  458. db := writeDb.Begin()
  459. 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
  460. if err != nil {
  461. db.Rollback()
  462. return err
  463. }
  464. db.Commit()
  465. return err
  466. }
  467. func UpdateSumOutDrug(orgid int64, storehouse_id int64, drug_id int64, sum_out_count int64) error {
  468. db := writeDb.Begin()
  469. 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
  470. if err != nil {
  471. db.Rollback()
  472. return err
  473. }
  474. db.Commit()
  475. return err
  476. }
  477. func UpdateSelfSumOutDrug(orgid int64, patient_id int64, drug_id int64, sum_out_count int64) error {
  478. db := writeDb.Begin()
  479. 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
  480. if err != nil {
  481. db.Rollback()
  482. return err
  483. }
  484. db.Commit()
  485. return err
  486. }
  487. func GetAutoGoodLastCount(goodid int64, count int64, record_time int64, patient_id int64) (models.AutomaticReduceDetail, error) {
  488. detail := models.AutomaticReduceDetail{}
  489. err := XTReadDB().Where("good_id = ? and count = ? and record_time = ? and patient_id = ?", goodid, count, record_time, patient_id).Last(&detail).Error
  490. return detail, err
  491. }
  492. func UpdateLastAutoCount(id int64, count int64) error {
  493. detail := models.AutomaticReduceDetail{}
  494. err := XTWriteDB().Model(&detail).Where("id = ?", id).Update(map[string]interface{}{"count": count, "status": 1}).Error
  495. return err
  496. }
  497. func GetWareOutInfo(good_id int64, record_date int64, user_org_id int64, patient_id int64, order_id int64) (outinfo []*models.WarehouseOutInfo, err error) {
  498. 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
  499. return outinfo, err
  500. }
  501. func AddGoodStockCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  502. db := writeDb.Begin()
  503. 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
  504. if err != nil {
  505. db.Rollback()
  506. return err
  507. }
  508. 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
  509. if err != nil {
  510. db.Rollback()
  511. return err
  512. }
  513. db.Commit()
  514. return err
  515. }
  516. func AddDrugStockCount(storehouse_id int64, drug_id int64, user_org_id int64, count int64) error {
  517. db := writeDb.Begin()
  518. 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
  519. if err != nil {
  520. db.Rollback()
  521. return err
  522. }
  523. 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
  524. if err != nil {
  525. db.Rollback()
  526. return err
  527. }
  528. db.Commit()
  529. return err
  530. }
  531. func ReduceStockCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  532. db := writeDb.Begin()
  533. //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
  534. //if err != nil {
  535. // db.Rollback()
  536. // return err
  537. //}
  538. 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
  539. if err != nil {
  540. db.Rollback()
  541. return err
  542. }
  543. 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
  544. if err != nil {
  545. db.Rollback()
  546. return err
  547. }
  548. 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
  549. if err != nil {
  550. db.Rollback()
  551. return err
  552. }
  553. db.Commit()
  554. return err
  555. }
  556. func ReduceStockCountTwo(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  557. db := writeDb.Begin()
  558. 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
  559. if err != nil {
  560. db.Rollback()
  561. return err
  562. }
  563. 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
  564. if err != nil {
  565. db.Rollback()
  566. return err
  567. }
  568. 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
  569. if err != nil {
  570. db.Rollback()
  571. return err
  572. }
  573. db.Commit()
  574. return err
  575. }
  576. func ReduceDrugStockCountSix(storehouse_id int64, drug_id int64, user_org_id int64, count int64) error {
  577. db := writeDb.Begin()
  578. //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
  579. //if err != nil {
  580. // db.Rollback()
  581. // return err
  582. //}
  583. 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
  584. if err != nil {
  585. db.Rollback()
  586. return err
  587. }
  588. 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
  589. if err != nil {
  590. db.Rollback()
  591. return err
  592. }
  593. 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
  594. if err != nil {
  595. db.Rollback()
  596. return err
  597. }
  598. db.Commit()
  599. return err
  600. }
  601. func ReduceDrugStockCountSeven(storehouse_id int64, drug_id int64, user_org_id int64, count int64) error {
  602. db := writeDb.Begin()
  603. 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
  604. if err != nil {
  605. db.Rollback()
  606. return err
  607. }
  608. 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
  609. if err != nil {
  610. db.Rollback()
  611. return err
  612. }
  613. 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
  614. if err != nil {
  615. db.Rollback()
  616. return err
  617. }
  618. db.Commit()
  619. return err
  620. }
  621. 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) {
  622. 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
  623. return info, err
  624. }
  625. func GetWarehouseStockFlow(warehouse_info_id int64, good_id int64, user_org_id int64, patient_id int64, system_time int64) (models.VmStockFlow, error) {
  626. stockFlow := models.VmStockFlow{}
  627. 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
  628. return stockFlow, err
  629. }
  630. 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 {
  631. stockFlow := models.VmStockFlow{}
  632. 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
  633. 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
  634. return err
  635. }
  636. func GetWarehouseOutInfo(user_org_id int64) (info []*models.WarehouseOut, err error) {
  637. err = XTReadDB().Where("org_id = ? and status =1", 10485).Find(&info).Error
  638. return info, err
  639. }
  640. func UpdateWarehosueOutInfo(id int64, sys_record_time int64) (models.WarehouseOutInfo, error) {
  641. info := models.WarehouseOutInfo{}
  642. 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
  643. return info, err
  644. }
  645. func AddGoodNewCancelSumCountOne(storehouse_id int64, good_id int64, user_org_id int64, cancel_count int64, tx *gorm.DB) error {
  646. 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
  647. if err != nil {
  648. tx.Rollback()
  649. return err
  650. }
  651. return err
  652. }
  653. func GetDrugNewInventoryList(org_id int64, keyword string) (drug []*models.BaseDrugLibTwentyOne, err error) {
  654. likeKey := "%" + keyword + "%"
  655. db := XTReadDB().Where("status=1")
  656. if org_id > 0 {
  657. db = db.Where("org_id = ?", org_id)
  658. }
  659. if len(keyword) > 0 {
  660. db = db.Where("drug_name like ?", likeKey)
  661. }
  662. err = db.Order("id desc").Find(&drug).Error
  663. return drug, err
  664. }
  665. func GetSelfDrugStockCount(patient_id int64, drug_id int64, org_id int64, tx *gorm.DB) (*models.XtSelfDrugStockCount, error) {
  666. drugStockCount := models.XtSelfDrugStockCount{}
  667. var err error
  668. err = tx.Where("patient_id = ? and drug_id = ? and user_org_id = ? and status = 1", patient_id, drug_id, org_id).Find(&drugStockCount).Error
  669. if err != gorm.ErrRecordNotFound {
  670. if err != nil {
  671. tx.Rollback()
  672. return nil, err
  673. }
  674. }
  675. return &drugStockCount, nil
  676. }
  677. func UpdateSelfDrugStockSum(patient_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64, tx *gorm.DB) error {
  678. 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
  679. if err != nil {
  680. tx.Rollback()
  681. return err
  682. }
  683. 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
  684. if err != nil {
  685. tx.Rollback()
  686. return err
  687. }
  688. return err
  689. }
  690. func GetSelfDrugBatchNumber(drug_id int64, org_id int64) (info []*models.XtSelfDrugWarehouseInfo, err error) {
  691. err = XTReadDB().Model(&info).Where("drug_id = ? and org_id = ? and status=1 and batch_number <>''", drug_id, org_id).Find(&info).Error
  692. return info, err
  693. }