entsourcenumaddlogic.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package logic
  2. import (
  3. "app.yhyue.com/moapp/jyResourcesCenter/entity"
  4. "app.yhyue.com/moapp/jyResourcesCenter/rpc/internal/config"
  5. "context"
  6. "fmt"
  7. "time"
  8. "app.yhyue.com/moapp/jyResourcesCenter/rpc/internal/svc"
  9. "app.yhyue.com/moapp/jyResourcesCenter/rpc/resourcesCenter"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type EntSourceNumAddLogic struct {
  13. ctx context.Context
  14. svcCtx *svc.ServiceContext
  15. logx.Logger
  16. }
  17. func NewEntSourceNumAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EntSourceNumAddLogic {
  18. return &EntSourceNumAddLogic{
  19. ctx: ctx,
  20. svcCtx: svcCtx,
  21. Logger: logx.WithContext(ctx),
  22. }
  23. }
  24. // EntSourceNumAdd 新增资源数量
  25. func (l *EntSourceNumAddLogic) EntSourceNumAdd(in *resourcesCenter.EntSourceNumAddReq) (*resourcesCenter.Response, error) {
  26. eCode, msg := func() (int64, string) {
  27. if config.ConfigJson.ProductMap[in.ResourceType] == nil || in.CompanyId == 0 || in.EmpowerId == 0 {
  28. return entity.ErrorCode, "参数异常"
  29. }
  30. var accountBalanceArr []*entity.Balance
  31. orm := entity.Engine.NewSession()
  32. err := orm.Table(AccountResources).
  33. Where("companyId=? AND resourceType=? AND empowerId=? AND endTime>= ?", in.CompanyId, in.ResourceType, in.EmpowerId, time.Now().Format("2006-01-02")).Find(&accountBalanceArr)
  34. if err != nil {
  35. return entity.ErrorCode, "查询数据异常"
  36. }
  37. if err = orm.Begin(); err != nil {
  38. return entity.ErrorCode, "数据库操作异常"
  39. }
  40. for _, balance := range accountBalanceArr {
  41. var (
  42. detailed = entity.Detailed{
  43. AccountId: balance.AccountId,
  44. CompanyId: in.CompanyId,
  45. EmpowerId: in.EmpowerId,
  46. DepartmentId: in.DepartmentId,
  47. Name: in.Name,
  48. ResourceType: in.ResourceType,
  49. Number: in.Number,
  50. CreateTime: time.Now().Local(),
  51. UserType: 1,
  52. Remarks: in.Remarks,
  53. UserId: balance.AccountId,
  54. }
  55. )
  56. insertNumb, err := orm.Table(ConsumeRecord).Insert(&detailed)
  57. if err != nil || insertNumb <= 0 {
  58. fmt.Println("新增流水失败:", err)
  59. _ = orm.Rollback()
  60. return entity.ErrorCode, "新增流水失败"
  61. }
  62. balance.Number = balance.Number + in.Number
  63. updateNumb, err := orm.Table(AccountResources).Cols("number").
  64. Where(" id=?", balance.Id).
  65. Update(balance)
  66. if err != nil || updateNumb <= 0 {
  67. _ = orm.Rollback()
  68. fmt.Println("资源新增异常:", err)
  69. return entity.ErrorCode, "结存修改失败"
  70. }
  71. }
  72. _ = orm.Commit()
  73. return entity.SuccessCode, "操作成功"
  74. }()
  75. return &resourcesCenter.Response{
  76. Code: eCode,
  77. Message: msg,
  78. }, nil
  79. }