trade.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package model
  2. import (
  3. "fmt"
  4. "strconv"
  5. "time"
  6. "go-common/app/admin/main/growup/util"
  7. )
  8. // GoodsInfo .
  9. type GoodsInfo struct {
  10. // internal
  11. ID int64 `json:"id"`
  12. ProductID string `json:"product_id"`
  13. ResourceID int64 `json:"-"`
  14. GoodsType GoodsType `json:"-"`
  15. Discount int `json:"discount"`
  16. IsDisplay DisplayStatus `json:"is_display"`
  17. DisplayOnTime time.Time `json:"-"`
  18. // derived
  19. GoodsTypeDesc string `json:"goods_type"` // 商品类型描述
  20. // external
  21. ProductName string `json:"product_name"` // 商品名称
  22. OriginPrice int64 `json:"origin_price"` // 实时成本, 单位分
  23. CurrentPrice int64 `json:"current_price"` // 实时售价, 单位分
  24. Month int32 `json:"month"` //有效期
  25. }
  26. // MergeExternal information from src to target
  27. func MergeExternal(target *GoodsInfo, src *GoodsInfo) error {
  28. switch target.GoodsType {
  29. case GoodsVIP:
  30. target.OriginPrice = src.OriginPrice
  31. target.ProductName = src.ProductName
  32. target.CurrentPrice = int64(util.DivWithRound(float64(target.OriginPrice*int64(target.Discount)), 100, 0))
  33. target.Month = src.Month
  34. return nil
  35. default:
  36. return fmt.Errorf("illegal type of goods(%v)", target)
  37. }
  38. }
  39. // OrderInfo .
  40. type OrderInfo struct {
  41. // internal
  42. ID int64 `json:"-"`
  43. MID int64 `json:"mid"`
  44. OrderNo string `json:"order_no"`
  45. OrderTime time.Time `json:"-"`
  46. GoodsType GoodsType `json:"-"`
  47. GoodsID string `json:"goods_id"`
  48. GoodsName string `json:"goods_name"`
  49. GoodsPrice int64 `json:"goods_price"`
  50. GoodsCost int64 `json:"goods_cost"`
  51. // desc for front end
  52. GoodsTypeDesc string `json:"goods_type"` // 商品类型描述
  53. OrderTimeDesc string `json:"order_time"` // 订单时间
  54. // derived
  55. TotalPrice int64 `json:"total_price"`
  56. TotalCost int64 `json:"total_cost"`
  57. GoodsNum int64 `json:"goods_num"`
  58. // external
  59. Nickname string `json:"nickname"`
  60. }
  61. // OrderExportFields .
  62. func OrderExportFields() []string {
  63. return []string{"订单ID", "时间", "商品ID", "商品名称", "售价", "成本", "数量", "总实收", "总成本", "UID", "昵称"}
  64. }
  65. // ExportStrings .
  66. func (v *OrderInfo) ExportStrings() []string {
  67. return []string{
  68. v.OrderNo,
  69. v.OrderTimeDesc,
  70. v.GoodsID,
  71. v.GoodsName,
  72. strconv.FormatFloat(util.Div(float64(v.GoodsPrice), float64(100)), 'f', 2, 64),
  73. strconv.FormatFloat(util.Div(float64(v.GoodsCost), float64(100)), 'f', 2, 64),
  74. strconv.FormatInt(v.GoodsNum, 10),
  75. strconv.FormatFloat(util.Div(float64(v.TotalPrice), float64(100)), 'f', 2, 64),
  76. strconv.FormatFloat(util.Div(float64(v.TotalCost), float64(100)), 'f', 2, 64),
  77. strconv.FormatInt(v.MID, 10),
  78. v.Nickname,
  79. }
  80. }
  81. // GenDerived generates derived information
  82. func (v *OrderInfo) GenDerived() *OrderInfo {
  83. v.GoodsNum = 1
  84. v.TotalPrice = v.GoodsPrice
  85. v.TotalCost = v.GoodsCost
  86. return v
  87. }
  88. // GenDesc generates descriptions
  89. func (v *OrderInfo) GenDesc() *OrderInfo {
  90. v.GoodsTypeDesc = v.GoodsType.Desc()
  91. v.OrderTimeDesc = v.OrderTime.Format("2006-01-02 15:04:05")
  92. return v
  93. }
  94. // DisplayStatus .
  95. type DisplayStatus int
  96. // DisplayStatuses enum
  97. const (
  98. DisplayOff DisplayStatus = 1
  99. DisplayOn DisplayStatus = 2
  100. )
  101. // GoodsType .
  102. type GoodsType int
  103. // GoodsTypes enum
  104. const (
  105. GoodsVIP GoodsType = 1
  106. )
  107. // Desc of GoodsType
  108. func (t GoodsType) Desc() string {
  109. switch t {
  110. case GoodsVIP:
  111. return "大会员"
  112. default:
  113. return "未定义商品类型 " + string(t)
  114. }
  115. }
  116. // TimeType .
  117. type TimeType int
  118. // TimeTypes enum
  119. const (
  120. Daily TimeType = 1 + iota
  121. Weekly
  122. Monthly
  123. )
  124. // RangeStart returns the included startTime
  125. func (t TimeType) RangeStart(date time.Time) time.Time {
  126. if t == Weekly {
  127. n := int(date.Weekday() - time.Monday)
  128. if n < 0 {
  129. n += 7
  130. }
  131. return time.Date(date.Year(), date.Month(), date.Day()-n, 0, 0, 0, 0, time.Local)
  132. } else if t == Monthly {
  133. return time.Date(date.Year(), date.Month(), 1, 0, 0, 0, 0, time.Local)
  134. }
  135. return time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, time.Local)
  136. }
  137. // RangeEnd returns the excluded endTime
  138. func (t TimeType) RangeEnd(date time.Time) time.Time {
  139. if t == Weekly {
  140. n := int(time.Monday - date.Weekday())
  141. if n <= 0 {
  142. n += 7
  143. }
  144. return time.Date(date.Year(), date.Month(), date.Day()+n, 0, 0, 0, 0, time.Local)
  145. } else if t == Monthly {
  146. return time.Date(date.Year(), date.Month()+1, 1, 0, 0, 0, 0, time.Local)
  147. } else if t == Daily {
  148. return time.Date(date.Year(), date.Month(), date.Day()+1, 0, 0, 0, 0, time.Local)
  149. }
  150. return time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, time.Local)
  151. }
  152. // RangeDesc .
  153. func (t TimeType) RangeDesc(start time.Time, end time.Time) string {
  154. if t == Daily {
  155. return start.Format("2006-01-02")
  156. }
  157. return start.Format("2006-01-02") + "~" + end.AddDate(0, 0, -1).Format("2006-01-02")
  158. }
  159. // Next returns time on next range
  160. func (t TimeType) Next() func(time.Time) time.Time {
  161. return func(start time.Time) time.Time {
  162. switch t {
  163. case Daily:
  164. return start.AddDate(0, 0, 1)
  165. case Weekly:
  166. return start.AddDate(0, 0, 7)
  167. case Monthly:
  168. return start.AddDate(0, 1, 0)
  169. default:
  170. return start.AddDate(0, 0, 1)
  171. }
  172. }
  173. }
  174. // OrderQueryArg .
  175. type OrderQueryArg struct {
  176. TimeType TimeType `form:"time_type" default:"1"`
  177. FromTime int64 `form:"from_time" validate:"required,min=1"`
  178. ToTime int64 `form:"to_time" validate:"required,min=1"`
  179. GoodsType int `form:"goods_type"`
  180. GoodsID string `form:"goods_id"`
  181. GoodsName string `form:"goods_name"`
  182. OrderNO string `form:"order_no"`
  183. MID int64 `form:"mid"`
  184. Nickname string `form:"nickname"`
  185. From int `form:"from" validate:"min=0" default:"0"`
  186. Limit int `form:"limit" validate:"min=1" default:"20"`
  187. // fromTime + toTime + timeType => (included) startTime & (excluded) endTime
  188. StartTime time.Time `form:"-"`
  189. EndTime time.Time `form:"-"`
  190. }