package service import ( "app.yhyue.com/moapp/jyResourcesCenter/entity" "app.yhyue.com/moapp/jyResourcesCenter/rpc/resourcesCenter" "time" ) type ResourceManageService struct{} //查询用户资源权限 func (service *ResourceManageService) FindResourcesAuth(data *resourcesCenter.ResourcesReq) ([]*resourcesCenter.ResourceBalance, error) { orm := entity.Engine.NewSession() var authArr []*entity.AccountBalance authList := make([]*resourcesCenter.ResourceBalance, 0) err := orm.Table("account_resources").Select("*"). Where("accountId = ?", data.AccountId).Find(&authArr) if err != nil { return authList, err } for _, v := range authArr { rb := &resourcesCenter.ResourceBalance{ Id: v.Id, Name: v.Name, ResourceType: v.ResourceType, Number: v.Number, Spec: v.Spec, } authList = append(authList, rb) } return authList, nil } //查询账户资源余额 func (service *ResourceManageService) FindAccountBalance(in *resourcesCenter.ResourcesReq, producMap map[string]interface{}) ([]*resourcesCenter.ResourceBalance, error) { now := time.Now().Format("2006-01-02") now1 := time.Now() currentYear, currentMonth, _ := now1.Date() currentLocation := now1.Location() firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation) lastOfMonth := firstOfMonth.AddDate(0, 1, -1) endTime := lastOfMonth.Format("2006-01-02") orm := entity.Engine.NewSession() var accountBalanceArr []*entity.AccountBalance dataList := make([]*resourcesCenter.ResourceBalance, 0) var err error if producMap[in.ResourceType] != nil { err = orm.Table("account_resources").Select("ANY_VALUE(id) as id,ANY_VALUE(name) as name,resourceType,ANY_VALUE(spec)"). Where("accountId = ? and endTime =? and vipTime>? and resourceType=? ", in.AccountId, endTime, time.Now().Unix(), in.ResourceType).GroupBy("resourceType").Find(&accountBalanceArr) } else { err = orm.Table("account_resources").Select("ANY_VALUE(id) as id,ANY_VALUE(name) as name,resourceType,ANY_VALUE(spec)"). Where("accountId = ? and endTime >= ? and resourceType like ?", in.AccountId, now, "%字段包%").GroupBy("resourceType").Find(&accountBalanceArr) } if err != nil { return dataList, err } for _, v := range accountBalanceArr { now1 := time.Now() currentYear, currentMonth, _ := now1.Date() currentLocation := now1.Location() firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation) lastOfMonth := firstOfMonth.AddDate(0, 1, -1) endTime := lastOfMonth.Format("2006-01-02") //统计30内到期数量 b := false var err error var number int64 c := int64(0) //统计总数量 isOk := false if producMap[v.ResourceType] != nil { isOk, err = orm.Table("account_resources").Select("sum(number) as number"). Where("accountId = ? and resourceType = ? and endTime = ? and vipTime>=?", in.AccountId, v.ResourceType, endTime, time.Now().Unix()).Get(&number) } else { var c int64 b, err = orm.Table("account_resources").Select("sum(number) as number"). Where("accountId = ? and resourceType = ? and endTime >= ? and endTime <= ?", in.AccountId, v.ResourceType, now, endTime).Get(&c) isOk, err = orm.Table("account_resources").Select("sum(number) as number"). Where("accountId = ? and resourceType = ? and endTime >= ?", in.AccountId, v.ResourceType, now).Get(&number) } if !b && err != nil && !isOk { return dataList, err } dataList = append(dataList, &resourcesCenter.ResourceBalance{ Id: v.Id, Name: v.Name, ResourceType: v.ResourceType, Number: number, Spec: v.Spec, ThirtyNum: c, }) } return dataList, nil } //查询流水账 func (service *ResourceManageService) FindConsumeRecord(in *resourcesCenter.RecordReq) ([]*resourcesCenter.ConsumeRecord, int64, error) { orm := entity.Engine.NewSession() var recordArr []*entity.ConsumeRecord dataList := make([]*resourcesCenter.ConsumeRecord, 0) var count int64 var err error if in.ResourceType == "" { in.ResourceType = "字段包" } if in.State == 0 { if in.QueryTime != "" { count, err = orm.Table("consume_record"). Where("accountId = ? and userId = ? and userType = 0 and resourceType like ? and DATE_FORMAT( createTime, '%Y-%m' ) = ?", in.AccountId, in.UserId, "%"+in.ResourceType+"%", in.QueryTime). Limit(int(in.PageSize), (int(in.Page)-1)*int(in.PageSize)).OrderBy("id desc").FindAndCount(&recordArr) } else { count, err = orm.Table("consume_record"). Where("accountId = ? and userId = ? and userType = 0 and resourceType like ? ", in.AccountId, in.UserId, "%"+in.ResourceType+"%"). Limit(int(in.PageSize), (int(in.Page)-1)*int(in.PageSize)).OrderBy("id desc").FindAndCount(&recordArr) } if err != nil && count == 0 { return dataList, 0, err } } else { if in.QueryTime != "" { count, err = orm.Table("consume_record"). Where("accountId = ? and userType = 1 and resourceType like ? and DATE_FORMAT( createTime, '%Y-%m' ) = ?", in.AccountId, "%"+in.ResourceType+"%", in.QueryTime). Limit(int(in.PageSize), (int(in.Page)-1)*int(in.PageSize)).OrderBy("id desc").FindAndCount(&recordArr) } else { count, err = orm.Table("consume_record"). Where("accountId = ? and userType = 1 and resourceType like ? ", in.AccountId, "%"+in.ResourceType+"%"). Limit(int(in.PageSize), (int(in.Page)-1)*int(in.PageSize)).OrderBy("id desc").FindAndCount(&recordArr) } if err != nil && count == 0 { return dataList, 0, err } } for _, v := range recordArr { dataList = append(dataList, &resourcesCenter.ConsumeRecord{ Id: v.Id, Name: v.Name, ResourceType: v.ResourceType, Number: v.Number, RuleId: v.RuleId, CreateTime: v.CreateTime, UserType: v.UserType, DeductionNumb: v.DeductionNumb, Remarks: v.Remarks, }) } return dataList, count, nil }