income.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "go-common/app/interface/main/growup/model"
  7. "go-common/library/log"
  8. "go-common/library/time"
  9. "go-common/library/xstr"
  10. )
  11. const (
  12. // select
  13. _avIncomeByMIDSQL = "SELECT av_id, income, total_income, date FROM av_income WHERE mid = ? AND date >= ? AND date <= ?"
  14. _avIncomeByAvIDSQL = "SELECT income, date FROM av_income WHERE av_id = ? AND date <= ?"
  15. _blacklistByAvIDSQL = "SELECT av_id FROM av_black_list WHERE av_id in (%s) AND ctype = ? AND is_delete = 0"
  16. _activityInfoByAvIDSQL = "SELECT archive_id, tag_id FROM activity_info WHERE archive_id in (%s)"
  17. _tagInfoByTagIDSQL = "SELECT id, ratio, icon FROM tag_info WHERE id in (%s) and is_deleted = 0"
  18. _upIncomeTableSQL = "SELECT mid,income,av_income,column_income,bgm_income,total_income,base_income,av_base_income,column_base_income,bgm_base_income,date FROM %s WHERE mid = ? AND date >= ? AND date <= ? AND is_deleted = 0"
  19. _upAccountSQL = "SELECT mid, total_income, total_unwithdraw_income, withdraw_date_version, version FROM up_account WHERE mid = ? AND is_deleted = 0"
  20. _upIncomeSQL = "SELECT mid,base_income,income,date FROM up_income WHERE mid=? AND date>=? AND date <=? ORDER BY date"
  21. _firstUpIncomeSQL = "SELECT date FROM up_income WHERE mid = ? ORDER BY date LIMIT 1"
  22. _upIncomeCountSQL = "SELECT count(*) FROM up_income WHERE date = ?"
  23. _upDailyCharge = "SELECT inc_charge FROM up_daily_charge WHERE mid=? AND date>=?"
  24. )
  25. // GetUpDailyCharge get up daily charge
  26. func (d *Dao) GetUpDailyCharge(c context.Context, mid int64, begin string) (incs []int, err error) {
  27. rows, err := d.db.Query(c, _upDailyCharge, mid, begin)
  28. if err != nil {
  29. log.Error("GetUpDailyCharge d.db.Query error(%v)", err)
  30. return
  31. }
  32. defer rows.Close()
  33. for rows.Next() {
  34. var incCharge int
  35. err = rows.Scan(&incCharge)
  36. if err != nil {
  37. log.Error("rows Scan error(%v)", err)
  38. return
  39. }
  40. incs = append(incs, incCharge)
  41. }
  42. return
  43. }
  44. // ListAvIncome list av_income by mid
  45. func (d *Dao) ListAvIncome(c context.Context, mid int64, startTime, endTime string) (avs []*model.ArchiveIncome, err error) {
  46. avs = make([]*model.ArchiveIncome, 0)
  47. rows, err := d.db.Query(c, _avIncomeByMIDSQL, mid, startTime, endTime)
  48. if err != nil {
  49. log.Error("ListAvIncome d.db.Query error(%v)", err)
  50. return
  51. }
  52. defer rows.Close()
  53. for rows.Next() {
  54. av := &model.ArchiveIncome{}
  55. err = rows.Scan(&av.ArchiveID, &av.Income, &av.TotalIncome, &av.Date)
  56. if err != nil {
  57. log.Error("ListAvIncome rows scan error(%v)", err)
  58. return
  59. }
  60. avs = append(avs, av)
  61. }
  62. err = rows.Err()
  63. return
  64. }
  65. // ListAvIncomeByID list av_income by av_id
  66. func (d *Dao) ListAvIncomeByID(c context.Context, avID int64, endTime string) (avs []*model.ArchiveIncome, err error) {
  67. avs = make([]*model.ArchiveIncome, 0)
  68. rows, err := d.db.Query(c, _avIncomeByAvIDSQL, avID, endTime)
  69. if err != nil {
  70. log.Error("ListAvIncomeByID d.db.Query error(%v)", err)
  71. return
  72. }
  73. defer rows.Close()
  74. for rows.Next() {
  75. av := &model.ArchiveIncome{}
  76. err = rows.Scan(&av.Income, &av.Date)
  77. if err != nil {
  78. log.Error("ListAvIncomeByID rows scan error(%v)", err)
  79. return
  80. }
  81. avs = append(avs, av)
  82. }
  83. err = rows.Err()
  84. return
  85. }
  86. // ListAvBlackList list av_blakc_list by av_id
  87. func (d *Dao) ListAvBlackList(c context.Context, avIds []int64, typ int) (avb map[int64]struct{}, err error) {
  88. avb = make(map[int64]struct{})
  89. rows, err := d.db.Query(c, fmt.Sprintf(_blacklistByAvIDSQL, xstr.JoinInts(avIds)), typ)
  90. if err != nil {
  91. log.Error("ListBlackList d.db.Query error(%v)", err)
  92. return
  93. }
  94. defer rows.Close()
  95. for rows.Next() {
  96. var avID int64
  97. err = rows.Scan(&avID)
  98. if err != nil {
  99. log.Error("ListBlackList rows scan error(%v)", err)
  100. return
  101. }
  102. avb[avID] = struct{}{}
  103. }
  104. err = rows.Err()
  105. return
  106. }
  107. // ListActiveInfo list active_info by avid
  108. func (d *Dao) ListActiveInfo(c context.Context, avIds []int64) (acM map[int64]int64, err error) {
  109. acM = make(map[int64]int64)
  110. rows, err := d.db.Query(c, fmt.Sprintf(_activityInfoByAvIDSQL, xstr.JoinInts(avIds)))
  111. if err != nil {
  112. log.Error("ListActiveInfo d.db.Query error(%v)", err)
  113. return
  114. }
  115. defer rows.Close()
  116. for rows.Next() {
  117. var avID, tagID int64
  118. err = rows.Scan(&avID, &tagID)
  119. if err != nil {
  120. log.Error("ListActiveInfo rows scan error(%v)", err)
  121. return
  122. }
  123. acM[avID] = tagID
  124. }
  125. err = rows.Err()
  126. return
  127. }
  128. // ListTagInfo list tag_info by avid
  129. func (d *Dao) ListTagInfo(c context.Context, tagIds []int64) (tagM map[int64]*model.TagInfo, err error) {
  130. tagM = make(map[int64]*model.TagInfo)
  131. rows, err := d.db.Query(c, fmt.Sprintf(_tagInfoByTagIDSQL, xstr.JoinInts(tagIds)))
  132. if err != nil {
  133. log.Error("ListTagInfo d.db.Query error(%v)", err)
  134. return
  135. }
  136. defer rows.Close()
  137. for rows.Next() {
  138. tagInfo := model.TagInfo{}
  139. err = rows.Scan(&tagInfo.ID, &tagInfo.Radio, &tagInfo.Icon)
  140. if err != nil {
  141. log.Error("ListTagInfo rows scan error(%v)", err)
  142. return
  143. }
  144. if val, ok := tagM[tagInfo.ID]; !ok {
  145. tagM[tagInfo.ID] = &tagInfo
  146. } else {
  147. if val.Radio < tagInfo.Radio {
  148. tagM[tagInfo.ID] = &tagInfo
  149. }
  150. }
  151. }
  152. err = rows.Err()
  153. return
  154. }
  155. // ListUpIncome list up_income by mid
  156. func (d *Dao) ListUpIncome(c context.Context, mid int64, table, startTime, endTime string) (ups []*model.UpIncome, err error) {
  157. ups = make([]*model.UpIncome, 0)
  158. rows, err := d.db.Query(c, fmt.Sprintf(_upIncomeTableSQL, table), mid, startTime, endTime)
  159. if err != nil {
  160. log.Error("ListUpIncome d.db.Query error(%v)", err)
  161. return
  162. }
  163. defer rows.Close()
  164. for rows.Next() {
  165. up := &model.UpIncome{}
  166. err = rows.Scan(&up.MID, &up.Income, &up.AvIncome, &up.ColumnIncome, &up.BgmIncome, &up.TotalIncome, &up.BaseIncome, &up.AvBaseIncome, &up.ColumnBaseIncome, &up.BgmBaseIncome, &up.Date)
  167. if err != nil {
  168. log.Error("ListUpIncome rows scan error(%v)", err)
  169. return
  170. }
  171. ups = append(ups, up)
  172. }
  173. err = rows.Err()
  174. return
  175. }
  176. // ListUpAccount list up_account by mid
  177. func (d *Dao) ListUpAccount(c context.Context, mid int64) (up *model.UpAccount, err error) {
  178. up = &model.UpAccount{}
  179. row := d.db.QueryRow(c, _upAccountSQL, mid)
  180. if err = row.Scan(&up.MID, &up.TotalIncome, &up.TotalUnwithdrawIncome, &up.WithdrawDateVersion, &up.Version); err != nil {
  181. if err == sql.ErrNoRows {
  182. return nil, nil
  183. }
  184. log.Error("row.Scan error(%v)", err)
  185. }
  186. return
  187. }
  188. // GetUpIncome list up income by date
  189. func (d *Dao) GetUpIncome(c context.Context, mid int64, begin string, end string) (result []*model.UpIncomeStat, err error) {
  190. rows, err := d.db.Query(c, _upIncomeSQL, mid, begin, end)
  191. if err != nil {
  192. return
  193. }
  194. defer rows.Close()
  195. for rows.Next() {
  196. stat := &model.UpIncomeStat{}
  197. err = rows.Scan(&stat.MID, &stat.BaseIncome, &stat.Income, &stat.Date)
  198. if err != nil {
  199. return
  200. }
  201. stat.ExtraIncome = stat.Income - stat.BaseIncome
  202. result = append(result, stat)
  203. }
  204. return
  205. }
  206. // GetFirstUpIncome get first up income
  207. func (d *Dao) GetFirstUpIncome(c context.Context, mid int64) (date time.Time, err error) {
  208. err = d.db.QueryRow(c, _firstUpIncomeSQL, mid).Scan(&date)
  209. if err == sql.ErrNoRows {
  210. err = nil
  211. date = time.Time(0)
  212. }
  213. return
  214. }
  215. // GetUpIncomeCount get up income count by date
  216. func (d *Dao) GetUpIncomeCount(c context.Context, date string) (count int, err error) {
  217. err = d.db.QueryRow(c, _upIncomeCountSQL, date).Scan(&count)
  218. return
  219. }