income.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/job/main/growup/model"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. )
  10. const (
  11. // select
  12. _avTagRatioSQL = "SELECT id, tag_id, av_id FROM av_charge_ratio WHERE id > ? ORDER BY id LIMIT ?"
  13. _avIncomeInfoSQL = "SELECT av_id, mid, income, date FROM av_income WHERE av_id = ? AND date = ? AND is_deleted = 0"
  14. _tagAVTotalIncomeSQL = "SELECT total_income, date FROM up_tag_income WHERE tag_id = ? AND av_id = ? AND is_deleted = 0"
  15. _avIncomeDateSQL = "SELECT id, av_id, mid, tag_id, income, total_income, date FROM av_income where id > ? LIMIT ?"
  16. _upAccuntSQL = "SELECT mid, total_income, total_unwithdraw_income, withdraw_date_version FROM up_account WHERE withdraw_date_version = ? AND ctime < ? AND total_unwithdraw_income > 0 AND is_deleted = 0 LIMIT ?,?"
  17. _upWithdrawSQL = "SELECT mid, withdraw_income FROM up_income_withdraw WHERE date_version = ? AND state = 2 LIMIT ?,?"
  18. _upIncomeSQL = "SELECT id, mid, av_count, av_income, column_count, column_income, bgm_count, bgm_income, income, tax_money, total_income, date FROM %s WHERE id > ? AND date = ? ORDER BY id LIMIT ?"
  19. _upTotalIncomeSQL = "SELECT id, total_income, is_deleted FROM up_account WHERE id > ? ORDER BY id LIMIT ?"
  20. _upDateIncomeSQL = "SELECT id, mid, income, total_income, is_deleted FROM up_income WHERE id > ? AND date = ? ORDER BY id LIMIT ?"
  21. _avDateIncomeSQL = "SELECT id, av_id, mid, tag_id, income, base_income, total_income,tax_money,upload_time,date,is_deleted FROM av_income WHERE id > ? AND date = ? AND is_deleted = 0 ORDER BY id LIMIT ?"
  22. _getUpTotalIncomeCntSQL = "SELECT count(*) FROM up_account WHERE total_income > 0 AND is_deleted = 0"
  23. _avIncomeStatisCount = "SELECT count(*) FROM av_income_statis"
  24. // insert
  25. _insertTagIncomeSQL = "INSERT INTO up_tag_income(tag_id, mid, av_id, income, total_income, date) VALUES %s ON DUPLICATE KEY UPDATE tag_id = values(tag_id), mid = values(mid), av_id = values(av_id), income = values(income), total_income = values(total_income), date = values(date)"
  26. )
  27. // GetAvTagRatio get av tag info from av_charge_ratio.
  28. func (d *Dao) GetAvTagRatio(c context.Context, from, limit int64) (infos []*model.ActivityAVInfo, err error) {
  29. rows, err := d.db.Query(c, _avTagRatioSQL, from, limit)
  30. if err != nil {
  31. log.Error("dao.GetAvTagRatio query error(%v)", err)
  32. return
  33. }
  34. defer rows.Close()
  35. for rows.Next() {
  36. a := &model.ActivityAVInfo{}
  37. if err = rows.Scan(&a.MID, &a.TagID, &a.AVID); err != nil {
  38. log.Error("dao.GetAvTagRatio scan error(%v)", err)
  39. return
  40. }
  41. infos = append(infos, a)
  42. }
  43. return
  44. }
  45. // GetAvIncomeInfo get av income from av_income.
  46. func (d *Dao) GetAvIncomeInfo(c context.Context, avID int64, date time.Time) (info *model.TagAvIncome, err error) {
  47. info = new(model.TagAvIncome)
  48. row := d.db.QueryRow(c, _avIncomeInfoSQL, avID, date)
  49. err = row.Scan(&info.AVID, &info.MID, &info.Income, &info.Date)
  50. if err != nil {
  51. if err == sql.ErrNoRows {
  52. err = nil
  53. info = nil
  54. return
  55. }
  56. log.Error("dao.GetAvInfoInfo scan error(%v)", err)
  57. }
  58. return
  59. }
  60. // TxInsertTagIncome insert tag_income.
  61. func (d *Dao) TxInsertTagIncome(tx *sql.Tx, sql string) (rows int64, err error) {
  62. res, err := tx.Exec(fmt.Sprintf(_insertTagIncomeSQL, sql))
  63. if err != nil {
  64. log.Error("dao.TxInsertTagIncome exec error(%v)", err)
  65. return
  66. }
  67. return res.RowsAffected()
  68. }
  69. // GetTagAvTotalIncome get av total_income from up_tag_income.
  70. func (d *Dao) GetTagAvTotalIncome(c context.Context, tagID, avID int64) (infos []*model.AvIncome, err error) {
  71. rows, err := d.db.Query(c, _tagAVTotalIncomeSQL, tagID, avID)
  72. if err != nil {
  73. log.Error("dao.GetTagAvTotalIncome query error(%v)", err)
  74. return
  75. }
  76. defer rows.Close()
  77. for rows.Next() {
  78. a := &model.AvIncome{}
  79. if err = rows.Scan(&a.TotalIncome, &a.Date); err != nil {
  80. log.Error("dao.GetTagAvTotalIncome scan error(%v)", err)
  81. return
  82. }
  83. infos = append(infos, a)
  84. }
  85. return
  86. }
  87. // ListAvIncome list av income by query
  88. func (d *Dao) ListAvIncome(c context.Context, id int64, limit int) (avIncome []*model.AvIncome, err error) {
  89. avIncome = make([]*model.AvIncome, 0)
  90. rows, err := d.db.Query(c, _avIncomeDateSQL, id, limit)
  91. if err != nil {
  92. log.Error("d.db.Query error(%v)", err)
  93. return
  94. }
  95. defer rows.Close()
  96. for rows.Next() {
  97. list := &model.AvIncome{}
  98. err = rows.Scan(&list.ID, &list.AvID, &list.MID, &list.TagID, &list.Income, &list.TotalIncome, &list.Date)
  99. if err != nil {
  100. log.Error("ListAvIncome rows scan error(%v)", err)
  101. return
  102. }
  103. avIncome = append(avIncome, list)
  104. }
  105. err = rows.Err()
  106. return
  107. }
  108. // ListUpAccount list up_acoount by date
  109. func (d *Dao) ListUpAccount(c context.Context, withdrawDate, ctime string, from, limit int) (upAct []*model.UpAccount, err error) {
  110. upAct = make([]*model.UpAccount, 0)
  111. rows, err := d.db.Query(c, _upAccuntSQL, withdrawDate, ctime, from, limit)
  112. if err != nil {
  113. log.Error("d.db.Query error(%v)", err)
  114. return
  115. }
  116. defer rows.Close()
  117. for rows.Next() {
  118. list := &model.UpAccount{}
  119. err = rows.Scan(&list.MID, &list.TotalIncome, &list.TotalUnwithdrawIncome, &list.WithdrawDateVersion)
  120. if err != nil {
  121. log.Error("ListUpAccount rows scan error(%v)", err)
  122. return
  123. }
  124. upAct = append(upAct, list)
  125. }
  126. err = rows.Err()
  127. return
  128. }
  129. // ListUpIncome list up_income_? by date
  130. func (d *Dao) ListUpIncome(c context.Context, table, date string, id int64, limit int) (um []*model.UpIncome, err error) {
  131. um = make([]*model.UpIncome, 0)
  132. rows, err := d.db.Query(c, fmt.Sprintf(_upIncomeSQL, table), id, date, limit)
  133. if err != nil {
  134. log.Error("d.db.Query error(%v)", err)
  135. return
  136. }
  137. defer rows.Close()
  138. for rows.Next() {
  139. list := &model.UpIncome{}
  140. err = rows.Scan(&list.ID, &list.MID, &list.AvCount, &list.AvIncome, &list.ColumnCount, &list.ColumnIncome, &list.BgmCount, &list.BgmIncome, &list.Income, &list.TaxMoney, &list.TotalIncome, &list.Date)
  141. if err != nil {
  142. log.Error("ListUpIncome rows scan error(%v)", err)
  143. return
  144. }
  145. um = append(um, list)
  146. }
  147. err = rows.Err()
  148. return
  149. }
  150. // ListUpWithdraw list up_withdraw_income by date
  151. func (d *Dao) ListUpWithdraw(c context.Context, date string, from, limit int) (ups map[int64]int64, err error) {
  152. ups = make(map[int64]int64)
  153. rows, err := d.db.Query(c, _upWithdrawSQL, date, from, limit)
  154. if err != nil {
  155. log.Error("d.db.Query error(%v)", err)
  156. return
  157. }
  158. defer rows.Close()
  159. for rows.Next() {
  160. var mid, income int64
  161. err = rows.Scan(&mid, &income)
  162. if err != nil {
  163. log.Error("ListUpWithdraw rows scan error(%v)", err)
  164. return
  165. }
  166. ups[mid] = income
  167. }
  168. err = rows.Err()
  169. return
  170. }
  171. // GetUpTotalIncome get up totalIncome.
  172. func (d *Dao) GetUpTotalIncome(c context.Context, from, limit int64) (infos []*model.MIDInfo, err error) {
  173. infos = make([]*model.MIDInfo, 0)
  174. rows, err := d.db.Query(c, _upTotalIncomeSQL, from, limit)
  175. if err != nil {
  176. log.Error("dao.GetUpTotalIncome query error(%v)", err)
  177. return
  178. }
  179. defer rows.Close()
  180. for rows.Next() {
  181. info := &model.MIDInfo{}
  182. if err = rows.Scan(&info.ID, &info.TotalIncome, &info.IsDeleted); err != nil {
  183. log.Error("dao.GetUpTotalIncome scan error(%v)", err)
  184. return
  185. }
  186. infos = append(infos, info)
  187. }
  188. return
  189. }
  190. // GetUpIncome get up date income.
  191. func (d *Dao) GetUpIncome(c context.Context, date time.Time, from, limit int64) (infos []*model.MIDInfo, err error) {
  192. infos = make([]*model.MIDInfo, 0)
  193. rows, err := d.db.Query(c, _upDateIncomeSQL, from, date, limit)
  194. if err != nil {
  195. log.Error("dao.GetUpIncome query error(%v)", err)
  196. return
  197. }
  198. defer rows.Close()
  199. for rows.Next() {
  200. info := &model.MIDInfo{}
  201. if err = rows.Scan(&info.ID, &info.MID, &info.Income, &info.TotalIncome, &info.IsDeleted); err != nil {
  202. log.Error("dao.GetUpIncome scan error(%v)", err)
  203. return
  204. }
  205. infos = append(infos, info)
  206. }
  207. return
  208. }
  209. // GetAvIncome get av income info from av_income.
  210. func (d *Dao) GetAvIncome(c context.Context, date time.Time, id, limit int64) (infos []*model.IncomeInfo, err error) {
  211. infos = make([]*model.IncomeInfo, 0)
  212. rows, err := d.db.Query(c, _avDateIncomeSQL, id, date, limit)
  213. if err != nil {
  214. log.Error("dao.GetIncome query error(%v)", err)
  215. return
  216. }
  217. defer rows.Close()
  218. for rows.Next() {
  219. info := &model.IncomeInfo{}
  220. if err = rows.Scan(&info.ID, &info.AVID, &info.MID, &info.TagID, &info.Income, &info.BaseIncome, &info.TotalIncome, &info.TaxMoney, &info.UploadTime, &info.Date, &info.IsDeleted); err != nil {
  221. log.Error("dao.GetAvIncome scan error(%v)", err)
  222. return
  223. }
  224. infos = append(infos, info)
  225. }
  226. return
  227. }
  228. // GetUpTotalIncomeCnt get up t-2 total income > 0 upcnt
  229. func (d *Dao) GetUpTotalIncomeCnt(c context.Context) (upCnt int, err error) {
  230. row := d.db.QueryRow(c, _getUpTotalIncomeCntSQL)
  231. if err = row.Scan(&upCnt); err != nil {
  232. log.Error("growup-job dao.GetUpTotalIncomeCnt scan error(%v)", err)
  233. }
  234. return
  235. }
  236. // GetAvStatisCount get av_income_statis count
  237. func (d *Dao) GetAvStatisCount(c context.Context) (cnt int, err error) {
  238. err = d.db.QueryRow(c, _avIncomeStatisCount).Scan(&cnt)
  239. return
  240. }