purchase.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. modernPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/xdmy"
  6. "gitee.com/xuyiping_admin/pkg/xerr"
  7. "github.com/pkg/errors"
  8. "gorm.io/gorm"
  9. "kpt.xdmy/apiserver/model"
  10. "strconv"
  11. )
  12. // CreateSpecialtyPurchase 创建特殊申购信息
  13. func CreateSpecialtyPurchase(ctx context.Context, req *modernPb.SparePartsRequisitionsRequest) error {
  14. var bigPartPurchaseId int64 = 0
  15. if req.PurchaseType == 1 {
  16. sql := `SELECT
  17. count(1) count
  18. FROM
  19. bigpartpurchase bp
  20. JOIN pasture p
  21. on p.id = bp.pastureId
  22. WHERE
  23. bp.pastureId = ?
  24. AND bp.statue < 3 and bp.purchase_type = 1 and createTime >'2024-03-18'
  25. and ( select name from department where id = bp.departmentId) != '挤奶处' order by bp.id desc limit 1 `
  26. var total int64
  27. s.d.DB.Raw(sql, req.PastureId).Count(&total)
  28. if total > 0 {
  29. return errors.New("单号已生成,请返回开始页面重新申购!")
  30. }
  31. s.d.DB.Exec(` delete from partpurchase p where (select pastureId from department where id = p.departmentId ) = ? and reject = 1 and employeId = ?
  32. and (select purchase_type from bigpartpurchase where id = p.bigId ) = 1 `, req.DepartmentId, req.EmployeId)
  33. }
  34. // 赠品
  35. defer func() {
  36. if req.PurchaseType == modernPb.PurchaseType_Complimentary_Item {
  37. if err := SpecialtyToProcure(bigPartPurchaseId); err != nil {
  38. fmt.Println("SpecialtyToProcure===>", bigPartPurchaseId)
  39. }
  40. }
  41. }()
  42. return s.d.DB.Transaction(func(tx *gorm.DB) error {
  43. newBigPartPurchase := model.NewBigPartPurchase(
  44. int64(req.PastureId), int64(req.DepartmentId), int64(req.EmployeId),
  45. int64(req.ProviderId), int64(req.FunderId),
  46. req.PurchaseType, req.OrderNumber, req.CreateTime, req.ProviderName,
  47. )
  48. if err := tx.Model(new(model.BigPartPurchase)).Create(newBigPartPurchase).Error; err != nil {
  49. return xerr.WithStack(err)
  50. }
  51. partPurchaseList := make([]*model.PartPurchase, 0)
  52. //for _, v := range req.SpotList {
  53. // amount, _ := strconv.Atoi(v.Amount)
  54. // partPurchaseList = append(partPurchaseList, model.NewPartPurchase(
  55. // newBigPartPurchase.Id, int64(req.PastureId), v.PartId, int64(v.BrandId), int64(amount),
  56. // v.ContractId, v.PartCode, v.PartName, v.Specification, v.Unit, v.Purpose, v.Price,
  57. // float64(v.StorageAmount)))
  58. //}
  59. for _, v := range req.SpotList {
  60. amount, _ := strconv.Atoi(v.Amount)
  61. emp, _ := strconv.ParseInt(v.EmployeId, 10, 64)
  62. partPurchaseList = append(partPurchaseList, model.NewPartPurchase(
  63. newBigPartPurchase.Id, int64(req.PastureId), v.PartId, int64(v.BrandId), int64(amount),
  64. v.ContractId, v.PartCode, v.PartName, v.Specification, v.Unit, v.Purpose, v.Price,
  65. float64(v.StorageAmount), v.DepartmentId, v.Date, emp))
  66. }
  67. if len(partPurchaseList) > 0 {
  68. if err := tx.Model(new(model.PartPurchase)).Create(partPurchaseList).Error; err != nil {
  69. return xerr.WithStack(err)
  70. }
  71. }
  72. bigPartPurchaseId = newBigPartPurchase.Id
  73. return nil
  74. })
  75. }
  76. // SpecialtyToProcure 特殊申购-自动流转采购单
  77. func SpecialtyToProcure(id int64) error {
  78. // 获取特殊申购单信息
  79. newBigPartPurchase := &model.BigPartPurchase{Id: id}
  80. if err := s.d.DB.Model(new(model.BigPartPurchase)).First(newBigPartPurchase).Error; err != nil {
  81. return xerr.WithStack(err)
  82. }
  83. // 过滤掉非特殊申购的数据
  84. if newBigPartPurchase.PurchaseType <= 0 {
  85. return nil
  86. }
  87. newPartPurchase := make([]*model.PartPurchase, 0)
  88. if err := s.d.DB.Model(new(model.PartPurchase)).Where("bigId = ?", newBigPartPurchase.Id).Find(&newPartPurchase).Error; err != nil {
  89. return xerr.WithStack(err)
  90. }
  91. return s.d.DB.Transaction(func(tx *gorm.DB) error {
  92. // 获取申购单号
  93. var orderCode string
  94. if err := tx.Raw(fmt.Sprintf("CALL createCodeN(%d,'%s')", newBigPartPurchase.PastureId, "CG")).Scan(&orderCode).Error; err != nil {
  95. return xerr.WithStack(err)
  96. }
  97. historyBigBuyDetail := &model.BigBuyDetail{}
  98. if err := tx.Model(new(model.BigBuyDetail)).Where("buyeCode = ?", orderCode).First(historyBigBuyDetail).Error; err != nil {
  99. if !errors.Is(err, gorm.ErrRecordNotFound) {
  100. return xerr.WithStack(err)
  101. }
  102. }
  103. // 避免重复提交
  104. if historyBigBuyDetail.BuyeCode == orderCode {
  105. return xerr.Customf("该申购单号已经存在:%s", orderCode)
  106. }
  107. newBigBuyDetail := model.NewBigBuyDetail(
  108. orderCode, newBigPartPurchase.OrderNumber, newBigPartPurchase.ProviderName, newBigPartPurchase.PastureId,
  109. newBigPartPurchase.ProviderId, newBigPartPurchase.EmployeId, newBigPartPurchase.PurchaseType)
  110. if err := tx.Model(new(model.BigBuyDetail)).Create(newBigBuyDetail).Error; err != nil {
  111. return xerr.WithStack(err)
  112. }
  113. buyDetailList := make([]*model.BuyDetail, 0)
  114. var allAmount int64 = 0
  115. for _, v := range newPartPurchase {
  116. contractId, _ := strconv.Atoi(v.ContractId)
  117. //newContract := &model.Contract{}
  118. //if err := tx.Model(new(model.Contract)).Where("id = ?", contractId).First(newContract).Error; err != nil {
  119. // fmt.Println("newContract", err)
  120. //}
  121. //
  122. //bigContract := &model.BigContract{}
  123. //if err := tx.Model(new(model.BigContract)).Where("id = ?", newContract.BigID).First(bigContract).Error; err != nil {
  124. // fmt.Println("CreateSpecialtyPurchase", err)
  125. //}
  126. department := &model.Department{}
  127. if err := tx.Model(new(model.Department)).Where("pastureId = ?", newBigPartPurchase.PastureId).
  128. Where("id = ?", newBigPartPurchase.DepartmentId).First(department).Error; err != nil {
  129. return xerr.WithStack(err)
  130. }
  131. price, _ := strconv.ParseFloat(v.Price, 64)
  132. buyDetailList = append(buyDetailList, model.NewBuyDetail(int64(newBigBuyDetail.ID), int64(newBigBuyDetail.PastureID),
  133. v.Amount, int64(contractId), v.BrandId, v.PartCode, "", v.Purpose, department.Name, price))
  134. allAmount += v.Amount
  135. }
  136. if err := s.d.DB.Model(new(model.BigBuyDetail)).Where("id = ?", newBigBuyDetail.ID).Updates(map[string]interface{}{
  137. "zeroCou": allAmount,
  138. }).Error; err != nil {
  139. return xerr.WithStack(err)
  140. }
  141. if len(buyDetailList) > 0 {
  142. if err := tx.Model(new(model.BuyDetail)).Create(buyDetailList).Error; err != nil {
  143. return xerr.WithStack(err)
  144. }
  145. }
  146. return nil
  147. })
  148. }