billingresource.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package models
  15. import (
  16. "context"
  17. "time"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/sqlchemy"
  20. api "yunion.io/x/onecloud/pkg/apis/billing"
  21. billing_api "yunion.io/x/onecloud/pkg/apis/billing"
  22. "yunion.io/x/onecloud/pkg/compute/options"
  23. "yunion.io/x/onecloud/pkg/mcclient"
  24. "yunion.io/x/onecloud/pkg/util/stringutils2"
  25. )
  26. type SBillingTypeBase struct {
  27. // 计费类型, 按量、包年包月
  28. // example: prepaid, postpaid
  29. BillingType api.TBillingType `width:"36" charset:"ascii" nullable:"true" default:"postpaid" list:"user" create:"optional" json:"billing_type"`
  30. }
  31. type SBillingChargeTypeBase struct {
  32. // 计费类型: 流量、带宽
  33. // example: bandwidth
  34. ChargeType billing_api.TNetChargeType `width:"64" name:"charge_type" list:"user" create:"optional"`
  35. }
  36. type SBillingResourceBase struct {
  37. SBillingTypeBase
  38. // 包年包月到期时间
  39. ExpiredAt time.Time `nullable:"true" list:"user" json:"expired_at"`
  40. // 到期释放时间
  41. ReleaseAt time.Time `nullable:"true" list:"user" create:"optional" json:"release_at"`
  42. // 计费周期
  43. BillingCycle string `width:"10" charset:"ascii" nullable:"true" list:"user" create:"optional" json:"billing_cycle"`
  44. // 是否自动续费
  45. AutoRenew bool `default:"false" list:"user" create:"optional" json:"auto_renew"`
  46. }
  47. type SBillingResourceBaseManager struct{}
  48. func (self *SBillingResourceBase) GetChargeType() api.TBillingType {
  49. if len(self.BillingType) > 0 {
  50. return self.BillingType
  51. } else {
  52. return api.BILLING_TYPE_POSTPAID
  53. }
  54. }
  55. func (self *SBillingResourceBase) SetReleaseAt(releaseAt time.Time) {
  56. self.ReleaseAt = releaseAt
  57. }
  58. func (self *SBillingResourceBase) GetExpiredAt() time.Time {
  59. return self.ExpiredAt
  60. }
  61. func (self *SBillingResourceBase) GetReleaseAt() time.Time {
  62. return self.ReleaseAt
  63. }
  64. func (self *SBillingResourceBase) GetAutoRenew() bool {
  65. return self.AutoRenew
  66. }
  67. func (self *SBillingResourceBase) SetExpiredAt(expireAt time.Time) {
  68. self.ExpiredAt = expireAt
  69. }
  70. func (self *SBillingResourceBase) SetBillingCycle(billingCycle string) {
  71. self.BillingCycle = billingCycle
  72. }
  73. func (self *SBillingResourceBase) SetBillingType(billingType api.TBillingType) {
  74. self.BillingType = billingType
  75. }
  76. func (self *SBillingResourceBase) GetBillingType() api.TBillingType {
  77. return self.BillingType
  78. }
  79. func (self *SBillingResourceBase) getBillingBaseInfo() SBillingBaseInfo {
  80. info := SBillingBaseInfo{}
  81. info.ChargeType = self.GetChargeType()
  82. info.ExpiredAt = self.ExpiredAt
  83. info.ReleaseAt = self.ReleaseAt
  84. if self.GetChargeType() == api.BILLING_TYPE_PREPAID {
  85. info.BillingCycle = self.BillingCycle
  86. }
  87. return info
  88. }
  89. func (self *SBillingResourceBase) IsNotDeletablePrePaid() bool {
  90. if options.Options.PrepaidDeleteExpireCheck {
  91. return self.IsValidPrePaid()
  92. }
  93. return false
  94. }
  95. func (self *SBillingResourceBase) IsValidPrePaid() bool {
  96. if self.BillingType == api.BILLING_TYPE_PREPAID {
  97. now := time.Now().UTC()
  98. if self.ExpiredAt.After(now) {
  99. return true
  100. }
  101. }
  102. return false
  103. }
  104. func (self *SBillingResourceBase) IsValidPostPaid() bool {
  105. if self.BillingType == api.BILLING_TYPE_POSTPAID {
  106. now := time.Now().UTC()
  107. if self.ReleaseAt.After(now) {
  108. return true
  109. }
  110. }
  111. return false
  112. }
  113. type SBillingBaseInfo struct {
  114. ChargeType api.TBillingType `json:",omitempty"`
  115. ExpiredAt time.Time `json:",omitempty"`
  116. ReleaseAt time.Time `json:",omitempty"`
  117. BillingCycle string `json:",omitempty"`
  118. }
  119. type SCloudBillingInfo struct {
  120. SCloudProviderInfo
  121. SBillingBaseInfo
  122. PriceKey string `json:",omitempty"`
  123. InternetChargeType api.TNetChargeType `json:",omitempty"`
  124. }
  125. func (manager *SBillingResourceBaseManager) FetchCustomizeColumns(
  126. ctx context.Context,
  127. userCred mcclient.TokenCredential,
  128. query jsonutils.JSONObject,
  129. objs []interface{},
  130. fields stringutils2.SSortedStrings,
  131. isList bool,
  132. ) []api.BillingDetailsInfo {
  133. rows := make([]api.BillingDetailsInfo, len(objs))
  134. for i := range rows {
  135. rows[i] = api.BillingDetailsInfo{}
  136. }
  137. return rows
  138. }
  139. func (manager *SBillingResourceBaseManager) ListItemFilter(
  140. ctx context.Context,
  141. q *sqlchemy.SQuery,
  142. userCred mcclient.TokenCredential,
  143. query api.BillingResourceListInput,
  144. ) (*sqlchemy.SQuery, error) {
  145. if len(query.BillingType) > 0 {
  146. if query.BillingType == api.BILLING_TYPE_POSTPAID {
  147. q = q.Filter(sqlchemy.OR(
  148. sqlchemy.IsNullOrEmpty(q.Field("billing_type")),
  149. sqlchemy.Equals(q.Field("billing_type"), api.BILLING_TYPE_POSTPAID),
  150. ))
  151. } else {
  152. q = q.Equals("billing_type", api.BILLING_TYPE_PREPAID)
  153. }
  154. }
  155. if !query.BillingExpireBefore.IsZero() {
  156. q = q.LT("expired_at", query.BillingExpireBefore)
  157. }
  158. if !query.BillingExpireSince.IsZero() {
  159. q = q.GE("expired_at", query.BillingExpireBefore)
  160. }
  161. if len(query.BillingCycle) > 0 {
  162. q = q.Equals("billing_cycle", query.BillingCycle)
  163. }
  164. return q, nil
  165. }
  166. func (manager *SBillingResourceBaseManager) OrderByExtraFields(
  167. ctx context.Context,
  168. q *sqlchemy.SQuery,
  169. userCred mcclient.TokenCredential,
  170. query api.BillingResourceListInput,
  171. ) (*sqlchemy.SQuery, error) {
  172. return q, nil
  173. }
  174. func ListExpiredPostpaidResources(
  175. q *sqlchemy.SQuery, limit int) *sqlchemy.SQuery {
  176. q = q.Equals("billing_type", api.BILLING_TYPE_POSTPAID)
  177. q = q.IsNotNull("release_at")
  178. q = q.LT("release_at", time.Now())
  179. if limit > 0 {
  180. q = q.Limit(limit)
  181. }
  182. return q
  183. }