entsourcenumaddlogic.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.EntSourceNumAdd) (*resourcesCenter.Response, error) {
  26. eCode, msg := func() (int64, string) {
  27. if config.ConfigJson.ProductMap[in.ResourceType] == nil || in.CompanyId == 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 endTime>= ?", in.CompanyId, in.ResourceType, 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. DepartmentId: in.DepartmentId,
  46. Name: in.Name,
  47. ResourceType: in.ResourceType,
  48. Number: in.Number,
  49. CreateTime: time.Now().Local(),
  50. UserType: 1,
  51. Remarks: in.Remarks,
  52. UserId: balance.AccountId,
  53. }
  54. )
  55. insertNumb, err := orm.Table(ConsumeRecord).Insert(&detailed)
  56. if err != nil || insertNumb <= 0 {
  57. fmt.Println("新增流水失败:", err)
  58. _ = orm.Rollback()
  59. return entity.ErrorCode, "新增流水失败"
  60. }
  61. balance.Number = balance.Number + in.Number
  62. updateNumb, err := orm.Table(AccountResources).Cols("number").
  63. Where(" id=?", balance.Id).
  64. Update(&balance)
  65. if err != nil || updateNumb <= 0 {
  66. _ = orm.Rollback()
  67. return entity.ErrorCode, "结存修改失败"
  68. }
  69. }
  70. _ = orm.Commit()
  71. return entity.SuccessCode, "操作成功"
  72. }()
  73. return &resourcesCenter.Response{
  74. Code: eCode,
  75. Message: msg,
  76. }, nil
  77. }