new_stock_service.go 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. package service
  2. import (
  3. "XT_New/models"
  4. "github.com/jinzhu/gorm"
  5. )
  6. func GetGoodStockCount(user_org_id int64, storehouse_id int64, good_id int64) (*models.XtGoodStockCount, error) {
  7. goodStock := models.XtGoodStockCount{}
  8. var err error
  9. err = XTReadDB().Where("user_org_id = ? and storehouse_id = ? and good_id = ?", user_org_id, storehouse_id, good_id).Find(&goodStock).Error
  10. if err == gorm.ErrRecordNotFound {
  11. return nil, err
  12. }
  13. if err != nil {
  14. return nil, err
  15. }
  16. return &goodStock, nil
  17. }
  18. func CreateGoodCount(good models.XtGoodStockCount) error {
  19. db := writeDb.Begin()
  20. err := db.Create(&good).Error
  21. if err != nil {
  22. db.Rollback()
  23. return err
  24. }
  25. db.Commit()
  26. return err
  27. }
  28. func UpdateGoodStockCount(user_org_id int64, storehouse_id int64, good_id int64, count int64, flush_count int64) error {
  29. db := writeDb.Begin()
  30. err = db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count + ?", count)).Error
  31. if err != nil {
  32. db.Rollback()
  33. return err
  34. }
  35. err = db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  36. if err != nil {
  37. db.Rollback()
  38. return err
  39. }
  40. db.Commit()
  41. return err
  42. }
  43. func UpdateGoodInCount(count int64, user_org_id int64, good_id int64, storehouse_id int64, flush_count int64) error {
  44. db := writeDb.Begin()
  45. err := db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and good_id = ? and storehouse_id = ? and status = 1", user_org_id, good_id, storehouse_id).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count - ?", count)).Error
  46. if err != nil {
  47. db.Rollback()
  48. return err
  49. }
  50. err = db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and good_id = ? and storehouse_id = ? and status = 1", user_org_id, good_id, storehouse_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  51. if err != nil {
  52. db.Rollback()
  53. return err
  54. }
  55. db.Commit()
  56. return err
  57. }
  58. func CreateGoodErrcode(errcode models.XtGoodErrcode) error {
  59. db := writeDb.Begin()
  60. err := db.Create(&errcode).Error
  61. if err != nil {
  62. db.Rollback()
  63. return err
  64. }
  65. db.Commit()
  66. return err
  67. }
  68. func GetGoodStockList(orgId int64, storehouse_id int64, good_type int64, keyword string, page int64, limit int64, ids []int64) (good []*models.GoodInformation, total int64, err error) {
  69. offset := (page - 1) * limit
  70. likeKey := "%" + keyword + "%"
  71. db := XTReadDB().Table("xt_good_information as x").Where("x.status = 1 and x.org_id =?", orgId)
  72. if good_type == 2 {
  73. db = db.Where(" x.total_count<=x.stock_warn_count")
  74. }
  75. if len(keyword) > 0 {
  76. db = db.Where("good_name like ? or manufacturer in(?)", likeKey, ids)
  77. }
  78. db = db.Preload("XtGoodStockCount", func(db *gorm.DB) *gorm.DB {
  79. if storehouse_id > 0 {
  80. db = db.Where("storehouse_id = ?", storehouse_id)
  81. }
  82. return db.Where("status = 1 and user_org_id = ?", orgId)
  83. })
  84. db = db.Preload("GoodStockCount", func(db *gorm.DB) *gorm.DB {
  85. if storehouse_id > 0 {
  86. db = db.Where("storehouse_id = ?", storehouse_id)
  87. }
  88. return db.Where("status = 1 and user_org_id = ?", orgId).Group("good_id,storehouse_id")
  89. })
  90. db = db.Preload("WarehousingInfo", func(db *gorm.DB) *gorm.DB {
  91. if storehouse_id > 0 {
  92. db = db.Where("storehouse_id = ?", storehouse_id)
  93. }
  94. return db.Where("status = 1 and org_id = ? and is_check = 1", orgId)
  95. })
  96. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Find(&good).Error
  97. return good, total, err
  98. }
  99. func GetSumGoodList(orgid int64, storehouse_id int64, good_id int64) (info []*models.WarehousingInfo, err error) {
  100. 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
  101. return info, err
  102. }
  103. func UpdateSumGood(orgid int64, storehouse_id int64, good_id int64, flush_count int64) error {
  104. db := writeDb.Begin()
  105. err := db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", orgid, storehouse_id, good_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  106. if err != nil {
  107. db.Rollback()
  108. return err
  109. }
  110. db.Commit()
  111. return err
  112. }
  113. func UpdateSumCount(user_org_id int64, storehouse_id int64, good_id int64, count int64) error {
  114. ut := XTWriteDB().Begin()
  115. 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
  116. if err != nil {
  117. ut.Rollback()
  118. return err
  119. }
  120. ut.Commit()
  121. return err
  122. }
  123. func GetSendGoodInformation(org_id int64) (info []*models.WarehousingInfo, err error) {
  124. err = XTReadDB().Where("org_id = ? and is_check = 1 and status = 1", org_id).Find(&info).Error
  125. return info, err
  126. }
  127. func GetStockFlush(storehouse_id int64, good_id int64, orgid int64) (*models.XtGoodStockCount, error) {
  128. stockCount := models.XtGoodStockCount{}
  129. var err error
  130. err = XTReadDB().Where("storehouse_id =? and good_id = ? and user_org_id = ?", storehouse_id, good_id, orgid).Find(&stockCount).Error
  131. if err == gorm.ErrRecordNotFound {
  132. return nil, err
  133. }
  134. if err != nil {
  135. return nil, err
  136. }
  137. return &stockCount, nil
  138. }
  139. func CreateGoodCountSix(good models.XtGoodStockCount) error {
  140. err := XTWriteDB().Create(&good).Error
  141. return err
  142. }
  143. 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 {
  144. 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
  145. 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
  146. 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
  147. 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
  148. 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
  149. return err
  150. }
  151. func GetAllCancelCount(storehouse_id int64, good_id int64, org_id int64) (flow []*models.VmStockFlow, err error) {
  152. 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
  153. return flow, err
  154. }
  155. func GetAllStockOutCount(storehouse_id int64, good_id int64, org_id int64) (flow []*models.VmStockFlow, err error) {
  156. err = XTReadDB().Where("storehouse_id = ? and good_id = ? and user_org_id = ? and (consumable_type = 2 or consumable_type= 3) and is_read = 0", storehouse_id, good_id, org_id).Find(&flow).Error
  157. return flow, err
  158. }
  159. func GetActStockOutCount(storehouse_id int64, good_id int64, org_id int64) (flow []*models.VmStockFlow, err error) {
  160. 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
  161. return flow, err
  162. }
  163. func GetDrugStockCount(storehouse_id int64, drug_id int64, org_id int64) (*models.XtDrugStockCount, error) {
  164. drugStockCount := models.XtDrugStockCount{}
  165. var err error
  166. err = XTReadDB().Where("storehouse_id = ? and drug_id = ? and user_org_id = ? and status = 1", storehouse_id, drug_id, org_id).Find(&drugStockCount).Error
  167. if err == gorm.ErrRecordNotFound {
  168. return nil, err
  169. }
  170. if err != nil {
  171. return nil, err
  172. }
  173. return &drugStockCount, nil
  174. }
  175. func GetDrugStockCountSix(storehouse_id int64, drug_id int64, org_id int64) (models.XtDrugStockCount, error) {
  176. drugStockCount := models.XtDrugStockCount{}
  177. err = XTReadDB().Where("storehouse_id = ? and drug_id = ? and user_org_id = ? and status = 1", storehouse_id, drug_id, org_id).Find(&drugStockCount).Error
  178. return drugStockCount, err
  179. }
  180. func CreateDrugStockSum(drug models.XtDrugStockCount) error {
  181. ut := XTWriteDB().Begin()
  182. err := ut.Create(&drug).Error
  183. if err != nil {
  184. ut.Rollback()
  185. return err
  186. }
  187. ut.Commit()
  188. return err
  189. }
  190. func AddDrugStockSum(storehouse_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64) error {
  191. db := writeDb.Begin()
  192. 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
  193. if err != nil {
  194. db.Rollback()
  195. return err
  196. }
  197. 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
  198. if err != nil {
  199. db.Rollback()
  200. return err
  201. }
  202. db.Commit()
  203. return err
  204. }
  205. func UpdateDrugStockSum(storehouse_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64) error {
  206. db := writeDb.Begin()
  207. 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
  208. if err != nil {
  209. db.Rollback()
  210. return err
  211. }
  212. 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
  213. if err != nil {
  214. db.Rollback()
  215. return err
  216. }
  217. db.Commit()
  218. return err
  219. }
  220. func ReduceDrugStockCount(storehouse_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64) error {
  221. db := writeDb.Begin()
  222. 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
  223. if err != nil {
  224. db.Rollback()
  225. return err
  226. }
  227. 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
  228. if err != nil {
  229. db.Rollback()
  230. return err
  231. }
  232. db.Commit()
  233. return err
  234. }
  235. func AddCancelSumCount(storehouse_id int64, drug_id int64, user_org_id int64, cancel_count int64, over_count int64) error {
  236. db := writeDb.Begin()
  237. 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
  238. if err != nil {
  239. db.Rollback()
  240. return err
  241. }
  242. 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
  243. if err != nil {
  244. db.Rollback()
  245. return err
  246. }
  247. db.Commit()
  248. return err
  249. }
  250. func AddCancelSumCountOne(storehouse_id int64, drug_id int64, user_org_id int64, cancel_count int64) error {
  251. db := writeDb.Begin()
  252. 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
  253. if err != nil {
  254. db.Rollback()
  255. return err
  256. }
  257. db.Commit()
  258. return err
  259. }
  260. func ReduceCancelSumCount(storehouse_id int64, drug_id int64, user_org_id int64, cancel_count int64, over_count int64) error {
  261. db := writeDb.Begin()
  262. 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
  263. if err != nil {
  264. db.Rollback()
  265. return err
  266. }
  267. 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
  268. if err != nil {
  269. db.Rollback()
  270. return err
  271. }
  272. db.Commit()
  273. return err
  274. }
  275. func UpdateSumDrug(orgid int64, storehouse_id int64, drug_id int64, flush_count int64) error {
  276. db := writeDb.Begin()
  277. 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
  278. if err != nil {
  279. db.Rollback()
  280. return err
  281. }
  282. db.Commit()
  283. return err
  284. }
  285. func UpdateSumOutDrug(orgid int64, storehouse_id int64, drug_id int64, sum_out_count int64) error {
  286. db := writeDb.Begin()
  287. 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
  288. if err != nil {
  289. db.Rollback()
  290. return err
  291. }
  292. db.Commit()
  293. return err
  294. }
  295. func GetAutoGoodLastCount(goodid int64, count int64, record_time int64, patient_id int64) (models.AutomaticReduceDetail, error) {
  296. detail := models.AutomaticReduceDetail{}
  297. err := XTReadDB().Where("good_id = ? and count = ? and record_time = ? and patient_id = ?", goodid, count, record_time, patient_id).Last(&detail).Error
  298. return detail, err
  299. }
  300. func UpdateLastAutoCount(id int64, count int64) error {
  301. detail := models.AutomaticReduceDetail{}
  302. err := XTWriteDB().Model(&detail).Where("id = ?", id).Update(map[string]interface{}{"count": count, "status": 1}).Error
  303. return err
  304. }
  305. func GetWareOutInfo(good_id int64, record_date int64, user_org_id int64) (outinfo []*models.WarehouseOutInfo, err error) {
  306. err = XTReadDB().Where("good_id = ? and sys_record_time = ? and org_id = ? and status = 1", good_id, record_date, user_org_id).Find(&outinfo).Error
  307. return outinfo, err
  308. }