resourceManageService.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/jyResourcesCenter/entity"
  4. "app.yhyue.com/moapp/jyResourcesCenter/rpc/resourcesCenter"
  5. "time"
  6. )
  7. type ResourceManageService struct{}
  8. //查询用户资源权限
  9. func (service *ResourceManageService) FindResourcesAuth(data *resourcesCenter.ResourcesReq) ([]*resourcesCenter.ResourceBalance, error) {
  10. orm := entity.Engine.NewSession()
  11. var authArr []*entity.AccountBalance
  12. authList := make([]*resourcesCenter.ResourceBalance, 0)
  13. err := orm.Table("account_resources").Select("*").
  14. Where("accountId = ?", data.AccountId).Find(&authArr)
  15. if err != nil {
  16. return authList, err
  17. }
  18. for _, v := range authArr {
  19. rb := &resourcesCenter.ResourceBalance{
  20. Id: v.Id,
  21. Name: v.Name,
  22. ResourceType: v.ResourceType,
  23. Number: v.Number,
  24. Spec: v.Spec,
  25. }
  26. authList = append(authList, rb)
  27. }
  28. return authList, nil
  29. }
  30. //查询账户资源余额
  31. func (service *ResourceManageService) FindAccountBalance(in *resourcesCenter.ResourcesReq, producMap map[string]interface{}) ([]*resourcesCenter.ResourceBalance, error) {
  32. now := time.Now().Format("2006-01-02")
  33. now1 := time.Now()
  34. currentYear, currentMonth, _ := now1.Date()
  35. currentLocation := now1.Location()
  36. firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
  37. lastOfMonth := firstOfMonth.AddDate(0, 1, -1)
  38. endTime := lastOfMonth.Format("2006-01-02")
  39. orm := entity.Engine.NewSession()
  40. var accountBalanceArr []*entity.AccountBalance
  41. dataList := make([]*resourcesCenter.ResourceBalance, 0)
  42. var err error
  43. if producMap[in.ResourceType] != nil {
  44. err = orm.Table("account_resources").Select("ANY_VALUE(id) as id,ANY_VALUE(name) as name,resourceType,ANY_VALUE(spec)").
  45. Where("accountId = ? and endTime =? and vipTime>? and resourceType=? ", in.AccountId, endTime, time.Now().Unix(), in.ResourceType).GroupBy("resourceType").Find(&accountBalanceArr)
  46. } else {
  47. err = orm.Table("account_resources").Select("ANY_VALUE(id) as id,ANY_VALUE(name) as name,resourceType,ANY_VALUE(spec)").
  48. Where("accountId = ? and endTime >= ? and resourceType like ?", in.AccountId, now, "%字段包%").GroupBy("resourceType").Find(&accountBalanceArr)
  49. }
  50. if err != nil {
  51. return dataList, err
  52. }
  53. for _, v := range accountBalanceArr {
  54. now1 := time.Now()
  55. currentYear, currentMonth, _ := now1.Date()
  56. currentLocation := now1.Location()
  57. firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
  58. lastOfMonth := firstOfMonth.AddDate(0, 1, -1)
  59. endTime := lastOfMonth.Format("2006-01-02")
  60. //统计30内到期数量
  61. b := false
  62. var err error
  63. var number int64
  64. c := int64(0)
  65. //统计总数量
  66. isOk := false
  67. if producMap[v.ResourceType] != nil {
  68. isOk, err = orm.Table("account_resources").Select("sum(number) as number").
  69. Where("accountId = ? and resourceType = ? and endTime = ? and vipTime>=?", in.AccountId, v.ResourceType, endTime, time.Now().Unix()).Get(&number)
  70. } else {
  71. var c int64
  72. b, err = orm.Table("account_resources").Select("sum(number) as number").
  73. Where("accountId = ? and resourceType = ? and endTime >= ? and endTime <= ?",
  74. in.AccountId, v.ResourceType, now, endTime).Get(&c)
  75. isOk, err = orm.Table("account_resources").Select("sum(number) as number").
  76. Where("accountId = ? and resourceType = ? and endTime >= ?", in.AccountId, v.ResourceType, now).Get(&number)
  77. }
  78. if !b && err != nil && !isOk {
  79. return dataList, err
  80. }
  81. dataList = append(dataList, &resourcesCenter.ResourceBalance{
  82. Id: v.Id,
  83. Name: v.Name,
  84. ResourceType: v.ResourceType,
  85. Number: number,
  86. Spec: v.Spec,
  87. ThirtyNum: c,
  88. })
  89. }
  90. return dataList, nil
  91. }
  92. //查询流水账
  93. func (service *ResourceManageService) FindConsumeRecord(in *resourcesCenter.RecordReq) ([]*resourcesCenter.ConsumeRecord, int64, error) {
  94. orm := entity.Engine.NewSession()
  95. var recordArr []*entity.ConsumeRecord
  96. dataList := make([]*resourcesCenter.ConsumeRecord, 0)
  97. var count int64
  98. var err error
  99. if in.ResourceType == "" {
  100. in.ResourceType = "字段包"
  101. }
  102. if in.State == 0 {
  103. if in.QueryTime != "" {
  104. count, err = orm.Table("consume_record").
  105. Where("accountId = ? and userId = ? and userType = 0 and resourceType like ? and DATE_FORMAT( createTime, '%Y-%m' ) = ?", in.AccountId, in.UserId, "%"+in.ResourceType+"%", in.QueryTime).
  106. Limit(int(in.PageSize), (int(in.Page)-1)*int(in.PageSize)).OrderBy("id desc").FindAndCount(&recordArr)
  107. } else {
  108. count, err = orm.Table("consume_record").
  109. Where("accountId = ? and userId = ? and userType = 0 and resourceType like ? ", in.AccountId, in.UserId, "%"+in.ResourceType+"%").
  110. Limit(int(in.PageSize), (int(in.Page)-1)*int(in.PageSize)).OrderBy("id desc").FindAndCount(&recordArr)
  111. }
  112. if err != nil && count == 0 {
  113. return dataList, 0, err
  114. }
  115. } else {
  116. if in.QueryTime != "" {
  117. count, err = orm.Table("consume_record").
  118. Where("accountId = ? and userType = 1 and resourceType like ? and DATE_FORMAT( createTime, '%Y-%m' ) = ?", in.AccountId, "%"+in.ResourceType+"%", in.QueryTime).
  119. Limit(int(in.PageSize), (int(in.Page)-1)*int(in.PageSize)).OrderBy("id desc").FindAndCount(&recordArr)
  120. } else {
  121. count, err = orm.Table("consume_record").
  122. Where("accountId = ? and userType = 1 and resourceType like ? ", in.AccountId, "%"+in.ResourceType+"%").
  123. Limit(int(in.PageSize), (int(in.Page)-1)*int(in.PageSize)).OrderBy("id desc").FindAndCount(&recordArr)
  124. }
  125. if err != nil && count == 0 {
  126. return dataList, 0, err
  127. }
  128. }
  129. for _, v := range recordArr {
  130. dataList = append(dataList, &resourcesCenter.ConsumeRecord{
  131. Id: v.Id,
  132. Name: v.Name,
  133. ResourceType: v.ResourceType,
  134. Number: v.Number,
  135. RuleId: v.RuleId,
  136. CreateTime: v.CreateTime,
  137. UserType: v.UserType,
  138. DeductionNumb: v.DeductionNumb,
  139. Remarks: v.Remarks,
  140. })
  141. }
  142. return dataList, count, nil
  143. }