run_test.go 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. package income
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "go-common/app/job/main/growup/conf"
  7. "strconv"
  8. "strings"
  9. "testing"
  10. "time"
  11. model "go-common/app/job/main/growup/model/income"
  12. xtime "go-common/library/time"
  13. . "github.com/smartystreets/goconvey/convey"
  14. )
  15. func Test_Run(t *testing.T) {
  16. Convey("Test Run creative one day one record\n", t, func() {
  17. c := context.Background()
  18. deleteAll(c)
  19. date := "2018-06-01"
  20. dateSec := "2018-06-01 15:02:03"
  21. d, _ := time.ParseInLocation("2006-01-02", date, time.Local)
  22. ac := insertAvDailyCharge(c, date, dateSec, 1101, 110, 10)
  23. err := s.run(c, d)
  24. So(err, ShouldBeNil)
  25. err = s.runStatis(c, d)
  26. So(err, ShouldBeNil)
  27. var totalIncome, taxMoney, count int64 = 10, 0, 1
  28. checkAllData(c, ac, totalIncome, taxMoney, count, date)
  29. })
  30. }
  31. func Test_RunWithBubble(t *testing.T) {
  32. var (
  33. c = context.TODO()
  34. date = time.Now()
  35. avid = int64(2333)
  36. mid = int64(233)
  37. charge = int64(10)
  38. err error
  39. )
  40. Convey("Test Run ", t, func() {
  41. deleteAll(c)
  42. s.conf.Bubble = &conf.BubbleConfig{BRatio: []*conf.BRatio{{BType: 1, Ratio: 0.8}}}
  43. insertBubbleMeta(c, fmt.Sprintf("(%d,%d,'%s')", avid, 1, date.Format("2006-01-02")))
  44. dailyCharge := insertAvDailyCharge(c, date.Format("2006-01-02"), date.Format("2006-01-02 15:04:05"), avid, mid, charge)
  45. err = s.run(c, date)
  46. So(err, ShouldBeNil)
  47. err = s.runStatis(c, date)
  48. So(err, ShouldBeNil)
  49. checkAllData(c, dailyCharge, 8, 0, 1, date.Format("2006-01-02"))
  50. })
  51. }
  52. func Test_RunWithMultiBubble(t *testing.T) {
  53. var (
  54. c = context.TODO()
  55. date = time.Now()
  56. avid = int64(2333)
  57. mid = int64(233)
  58. charge = int64(10)
  59. err error
  60. )
  61. Convey("Test Run ", t, func() {
  62. deleteAll(c)
  63. s.conf.Bubble = &conf.BubbleConfig{BRatio: []*conf.BRatio{{BType: 1, Ratio: 0.8}, {BType: 2, Ratio: 0.7}}}
  64. insertBubbleMeta(c, fmt.Sprintf("(%d,%d,'%s')", avid, 1, date.Format("2006-01-02")))
  65. insertBubbleMeta(c, fmt.Sprintf("(%d,%d,'%s')", avid, 2, date.Format("2006-01-02")))
  66. dailyCharge := insertAvDailyCharge(c, date.Format("2006-01-02"), date.Format("2006-01-02 15:04:05"), avid, mid, charge)
  67. err = s.run(c, date)
  68. So(err, ShouldBeNil)
  69. err = s.runStatis(c, date)
  70. So(err, ShouldBeNil)
  71. checkAllData(c, dailyCharge, 7, 0, 1, date.Format("2006-01-02"))
  72. })
  73. }
  74. func Test_AvIncome(t *testing.T) {
  75. Convey("Test av income\n", t, func() {
  76. c := context.Background()
  77. deleteAll(c)
  78. ac1 := insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1101, 110, 10)
  79. ac2 := insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1102, 110, 10)
  80. d, _ := time.ParseInLocation("2006-01-02", "2018-06-01", time.Local)
  81. err := s.run(c, d)
  82. So(err, ShouldBeNil)
  83. err = s.runStatis(c, d)
  84. So(err, ShouldBeNil)
  85. avIncome1 := checkAvIncome(c, ac1, 10, 0, 1, "2018-06-01")
  86. avIncome2 := checkAvIncome(c, ac2, 10, 0, 1, "2018-06-01")
  87. checkAvIncomeStatis(c, avIncome1, 10)
  88. checkAvIncomeStatis(c, avIncome2, 10)
  89. ac3 := insertAvDailyCharge(c, "2018-06-02", "2018-06-01 15:02:03", 1101, 110, 20)
  90. d, _ = time.ParseInLocation("2006-01-02", "2018-06-02", time.Local)
  91. err = s.run(c, d)
  92. So(err, ShouldBeNil)
  93. err = s.runStatis(c, d)
  94. So(err, ShouldBeNil)
  95. avIncome3 := checkAvIncome(c, ac3, 30, 0, 1, "2018-06-02")
  96. checkAvIncomeStatis(c, avIncome2, 10)
  97. checkAvIncomeStatis(c, avIncome3, 30)
  98. ac3 = insertAvDailyCharge(c, "2018-07-01", "2018-06-01 15:02:03", 1101, 110, 20)
  99. d, _ = time.ParseInLocation("2006-01-02", "2018-07-01", time.Local)
  100. err = s.run(c, d)
  101. So(err, ShouldBeNil)
  102. err = s.runStatis(c, d)
  103. So(err, ShouldBeNil)
  104. avIncome3 = checkAvIncome(c, ac3, 50, 0, 1, "2018-07-01")
  105. checkAvIncomeStatis(c, avIncome3, 50)
  106. })
  107. }
  108. func Test_AvIncomeDateStatis(t *testing.T) {
  109. Convey("Test av income daily weekly monthly statis\n", t, func() {
  110. c := context.Background()
  111. deleteAll(c)
  112. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1101, 110, 10)
  113. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1102, 110, 10)
  114. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1103, 110, 3500)
  115. d, _ := time.ParseInLocation("2006-01-02", "2018-06-01", time.Local)
  116. err := s.run(c, d)
  117. So(err, ShouldBeNil)
  118. err = s.runStatis(c, d)
  119. So(err, ShouldBeNil)
  120. checkAvIncomeDateStatis(c, 0, 2, 1, 3494, 12, d, "av_income_daily_statis")
  121. checkAvIncomeDateStatis(c, 4, 1, 1, 3494, 12, d, "av_income_daily_statis")
  122. weekD := getStartWeeklyDate(d)
  123. checkAvIncomeDateStatis(c, 0, 2, 1, 3494, 12, weekD, "av_income_weekly_statis")
  124. checkAvIncomeDateStatis(c, 4, 1, 1, 3494, 12, weekD, "av_income_weekly_statis")
  125. monthD := getStartMonthlyDate(d)
  126. checkAvIncomeDateStatis(c, 0, 2, 1, 3494, 12, monthD, "av_income_monthly_statis")
  127. checkAvIncomeDateStatis(c, 4, 1, 1, 3494, 12, monthD, "av_income_monthly_statis")
  128. // insertAvDailyCharge(c, "2018-06-02", "2018-06-01 15:02:03", 1101, 110, 1000)
  129. // insertAvDailyCharge(c, "2018-06-02", "2018-06-01 15:02:03", 1102, 110, 1000)
  130. // insertAvDailyCharge(c, "2018-06-02", "2018-06-01 15:02:03", 1103, 110, 4000)
  131. // d, _ = time.ParseInLocation("2006-01-02", "2018-06-02", time.Local)
  132. // err = s.run(c, d)
  133. // So(err, ShouldBeNil)
  134. // checkAvIncomeDateStatis(c, 0, 2, 1, 5801, 12, d, "av_income_daily_statis")
  135. // checkAvIncomeDateStatis(c, 1, 1, 1, 5801, 12, d, "av_income_daily_statis")
  136. // weekD = getStartWeeklyDate(d)
  137. // income := 5801 + 3494
  138. // checkAvIncomeDateStatis(c, 0, 2, 1, income, 12, weekD, "av_income_weekly_statis")
  139. // checkAvIncomeDateStatis(c, 1, 0, 1, income, 12, weekD, "av_income_weekly_statis")
  140. // checkAvIncomeDateStatis(c, 2, 1, 1, income, 12, weekD, "av_income_weekly_statis")
  141. // monthD = getStartMonthlyDate(d)
  142. // checkAvIncomeDateStatis(c, 0, 2, 1, income, 12, monthD, "av_income_monthly_statis")
  143. // checkAvIncomeDateStatis(c, 1, 0, 1, income, 12, monthD, "av_income_monthly_statis")
  144. // checkAvIncomeDateStatis(c, 2, 1, 1, income, 12, monthD, "av_income_monthly_statis")
  145. // insertAvDailyCharge(c, "2018-06-10", "2018-06-01 15:02:03", 1101, 110, 15000)
  146. // insertAvDailyCharge(c, "2018-06-10", "2018-06-01 15:02:03", 1102, 110, 15000)
  147. // insertAvDailyCharge(c, "2018-06-10", "2018-06-01 15:02:03", 1103, 110, 40000)
  148. // d, _ = time.ParseInLocation("2006-01-02", "2018-06-10", time.Local)
  149. // err = s.run(c, d)
  150. // So(err, ShouldBeNil)
  151. // checkAvIncomeDateStatis(c, 0, 0, 1, 54651, 12, d, "av_income_daily_statis")
  152. // checkAvIncomeDateStatis(c, 1, 0, 1, 54651, 12, d, "av_income_daily_statis")
  153. // checkAvIncomeDateStatis(c, 3, 2, 1, 54651, 12, d, "av_income_daily_statis")
  154. // checkAvIncomeDateStatis(c, 4, 1, 1, 54651, 12, d, "av_income_daily_statis")
  155. // weekD = getStartWeeklyDate(d)
  156. // checkAvIncomeDateStatis(c, 0, 0, 1, 54651, 12, weekD, "av_income_weekly_statis")
  157. // checkAvIncomeDateStatis(c, 1, 0, 1, 54651, 12, weekD, "av_income_weekly_statis")
  158. // checkAvIncomeDateStatis(c, 3, 2, 1, 54651, 12, weekD, "av_income_weekly_statis")
  159. // checkAvIncomeDateStatis(c, 4, 1, 1, 54651, 12, weekD, "av_income_weekly_statis")
  160. // monthD = getStartMonthlyDate(d)
  161. // checkAvIncomeDateStatis(c, 0, 0, 1, 63946, 12, monthD, "av_income_monthly_statis")
  162. // checkAvIncomeDateStatis(c, 1, 0, 1, 63946, 12, monthD, "av_income_monthly_statis")
  163. // checkAvIncomeDateStatis(c, 3, 2, 1, 63946, 12, monthD, "av_income_monthly_statis")
  164. // checkAvIncomeDateStatis(c, 4, 1, 1, 63946, 12, monthD, "av_income_monthly_statis")
  165. // insertAvDailyCharge(c, "2018-07-10", "2018-06-01 15:02:03", 1101, 110, 100)
  166. // d, _ = time.ParseInLocation("2006-01-02", "2018-07-10", time.Local)
  167. // err = s.run(c, d)
  168. // So(err, ShouldBeNil)
  169. // monthD = getStartMonthlyDate(d)
  170. // checkAvIncomeDateStatis(c, 0, 1, 1, 100, 12, monthD, "av_income_monthly_statis")
  171. // checkAvIncomeDateStatis(c, 1, 0, 1, 100, 12, monthD, "av_income_monthly_statis")
  172. })
  173. }
  174. func Test_UpIncome(t *testing.T) {
  175. Convey("Test up income\n", t, func() {
  176. c := context.Background()
  177. deleteAll(c)
  178. ac1 := insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1101, 110, 10)
  179. ac2 := insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1102, 110, 10)
  180. d, _ := time.ParseInLocation("2006-01-02", "2018-06-01", time.Local)
  181. err := s.run(c, d)
  182. So(err, ShouldBeNil)
  183. err = s.runStatis(c, d)
  184. So(err, ShouldBeNil)
  185. avIncome1 := checkAvIncome(c, ac1, 10, 0, 1, "2018-06-01")
  186. avIncome2 := checkAvIncome(c, ac2, 10, 0, 1, "2018-06-01")
  187. avs := []*model.AvIncome{avIncome1, avIncome2}
  188. up := checkUpIncome(c, avs, 20, "up_income", "", 2)
  189. checkUpIncomeWeekly(c, avs, 20, 2)
  190. checkUpIncomeMonthly(c, avs, 20, 2)
  191. checkUpIncomeStatis(c, up.MID, 20)
  192. ac3 := insertAvDailyCharge(c, "2018-06-02", "2018-06-01 15:02:03", 1101, 110, 10)
  193. ac4 := insertAvDailyCharge(c, "2018-06-02", "2018-06-01 15:02:03", 1102, 110, 10)
  194. ac5 := insertAvDailyCharge(c, "2018-06-02", "2018-06-01 15:02:03", 1103, 110, 10)
  195. d, _ = time.ParseInLocation("2006-01-02", "2018-06-02", time.Local)
  196. err = s.run(c, d)
  197. So(err, ShouldBeNil)
  198. err = s.runStatis(c, d)
  199. So(err, ShouldBeNil)
  200. avIncome3 := checkAvIncome(c, ac3, 20, 0, 1, "2018-06-02")
  201. avIncome4 := checkAvIncome(c, ac4, 20, 0, 1, "2018-06-02")
  202. avIncome5 := checkAvIncome(c, ac5, 10, 0, 1, "2018-06-02")
  203. avs = append(avs, avIncome3)
  204. avs = append(avs, avIncome4)
  205. avs = append(avs, avIncome5)
  206. up = checkUpIncome(c, []*model.AvIncome{avIncome3, avIncome4, avIncome5}, 50, "up_income", "", 3)
  207. checkUpIncomeWeekly(c, avs, 50, 3)
  208. checkUpIncomeMonthly(c, avs, 50, 3)
  209. checkUpIncomeStatis(c, up.MID, 50)
  210. checkUpAccount(c, 110, 50, 50, 0, "2018-05")
  211. })
  212. }
  213. func Test_UpIncomeDailyStatis(t *testing.T) {
  214. Convey("Test up income daily statis\n", t, func() {
  215. c := context.Background()
  216. deleteAll(c)
  217. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1101, 110, 10)
  218. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1102, 110, 10)
  219. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1103, 111, 10)
  220. d, _ := time.ParseInLocation("2006-01-02", "2018-06-01", time.Local)
  221. err := s.run(c, d)
  222. So(err, ShouldBeNil)
  223. err = s.runStatis(c, d)
  224. So(err, ShouldBeNil)
  225. checkUpIncomeDailyStatis(c, 0, 2, 30, d)
  226. insertAvDailyCharge(c, "2018-06-02", "2018-06-01 15:02:03", 1101, 110, 10000)
  227. insertAvDailyCharge(c, "2018-06-02", "2018-06-01 15:02:03", 1102, 111, 30000)
  228. insertAvDailyCharge(c, "2018-06-02", "2018-06-01 15:02:03", 1103, 110, 70000)
  229. d, _ = time.ParseInLocation("2006-01-02", "2018-06-02", time.Local)
  230. err = s.run(c, d)
  231. So(err, ShouldBeNil)
  232. err = s.runStatis(c, d)
  233. So(err, ShouldBeNil)
  234. checkUpIncomeDailyStatis(c, 7, 1, 87300, d)
  235. checkUpIncomeDailyStatis(c, 8, 1, 87300, d)
  236. })
  237. }
  238. func Test_UpIncomeStatis(t *testing.T) {
  239. Convey("Test up income statis\n", t, func() {
  240. c := context.Background()
  241. deleteAll(c)
  242. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1101, 110, 10)
  243. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1102, 110, 10)
  244. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1103, 111, 10)
  245. d, _ := time.ParseInLocation("2006-01-02", "2018-06-01", time.Local)
  246. err := s.run(c, d)
  247. So(err, ShouldBeNil)
  248. err = s.runStatis(c, d)
  249. So(err, ShouldBeNil)
  250. checkUpIncomeStatis(c, 110, 20)
  251. checkUpIncomeStatis(c, 111, 10)
  252. insertAvDailyCharge(c, "2018-06-02", "2018-06-01 15:02:03", 1101, 110, 10)
  253. insertAvDailyCharge(c, "2018-06-02", "2018-06-01 15:02:03", 1102, 110, 10)
  254. insertAvDailyCharge(c, "2018-06-02", "2018-06-01 15:02:03", 1103, 111, 10)
  255. insertAvDailyCharge(c, "2018-06-02", "2018-06-01 15:02:03", 1104, 112, 10)
  256. d, _ = time.ParseInLocation("2006-01-02", "2018-06-02", time.Local)
  257. err = s.run(c, d)
  258. So(err, ShouldBeNil)
  259. err = s.runStatis(c, d)
  260. So(err, ShouldBeNil)
  261. checkUpIncomeStatis(c, 110, 40)
  262. checkUpIncomeStatis(c, 111, 20)
  263. checkUpIncomeStatis(c, 112, 10)
  264. insertAvDailyCharge(c, "2018-07-02", "2018-06-01 15:02:03", 1101, 110, 10)
  265. insertAvDailyCharge(c, "2018-07-02", "2018-06-01 15:02:03", 1102, 110, 10)
  266. insertAvDailyCharge(c, "2018-07-02", "2018-06-01 15:02:03", 1103, 111, 10)
  267. insertAvDailyCharge(c, "2018-07-02", "2018-06-01 15:02:03", 1104, 112, 10)
  268. d, _ = time.ParseInLocation("2006-01-02", "2018-07-02", time.Local)
  269. err = s.run(c, d)
  270. So(err, ShouldBeNil)
  271. err = s.runStatis(c, d)
  272. So(err, ShouldBeNil)
  273. checkUpIncomeStatis(c, 110, 60)
  274. checkUpIncomeStatis(c, 111, 30)
  275. checkUpIncomeStatis(c, 112, 20)
  276. })
  277. }
  278. func Test_AvIncomeTag(t *testing.T) {
  279. Convey("Test av income tag\n", t, func() {
  280. c := context.Background()
  281. deleteAll(c)
  282. s.dao.Exec(c, "insert into av_charge_ratio(av_id,ratio,adjust_type) values(1101,200,0)")
  283. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1101, 110, 10)
  284. d, _ := time.ParseInLocation("2006-01-02", "2018-06-01", time.Local)
  285. err := s.run(c, d)
  286. So(err, ShouldBeNil)
  287. err = s.runStatis(c, d)
  288. So(err, ShouldBeNil)
  289. checkAvIncomeTag(c, 1101, 20, 0, 20, "2018-06-01")
  290. s.dao.Exec(c, "delete from av_charge_ratio")
  291. s.dao.Exec(c, "insert into av_charge_ratio(av_id,ratio,adjust_type) values(1101,20,1)")
  292. insertAvDailyCharge(c, "2018-06-02", "2018-06-01 15:02:03", 1101, 110, 10)
  293. d, _ = time.ParseInLocation("2006-01-02", "2018-06-02", time.Local)
  294. err = s.run(c, d)
  295. So(err, ShouldBeNil)
  296. err = s.runStatis(c, d)
  297. So(err, ShouldBeNil)
  298. checkAvIncomeTag(c, 1101, 30, 0, 50, "2018-06-02")
  299. // tax
  300. s.dao.Exec(c, "delete from av_charge_ratio")
  301. s.dao.Exec(c, "insert into av_charge_ratio(av_id,ratio,adjust_type) values(1101,20000,1)")
  302. insertAvDailyCharge(c, "2018-06-03", "2018-06-01 15:02:03", 1101, 110, 10)
  303. d, _ = time.ParseInLocation("2006-01-02", "2018-06-03", time.Local)
  304. err = s.run(c, d)
  305. So(err, ShouldBeNil)
  306. err = s.runStatis(c, d)
  307. So(err, ShouldBeNil)
  308. checkAvIncomeTag(c, 1101, 20010, 0, 20060, "2018-06-03")
  309. })
  310. }
  311. func Test_IncomeTax(t *testing.T) {
  312. Convey("Test income tax\n", t, func() {
  313. c := context.Background()
  314. deleteAll(c)
  315. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1101, 110, 3000)
  316. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1102, 111, 4000)
  317. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1103, 112, 8000)
  318. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1104, 113, 12000)
  319. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1105, 114, 18000)
  320. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1106, 115, 40000)
  321. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1107, 116, 80000)
  322. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1108, 117, 150000)
  323. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1109, 118, 250000)
  324. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 11010, 119, 500000)
  325. d, _ := time.ParseInLocation("2006-01-02", "2018-06-01", time.Local)
  326. err := s.run(c, d)
  327. So(err, ShouldBeNil)
  328. err = s.runStatis(c, d)
  329. So(err, ShouldBeNil)
  330. checkAvIncomeTag(c, 1101, 3000, 0, 3000, "2018-06-01")
  331. checkAvIncomeTag(c, 1102, 3950, 50, 3950, "2018-06-01")
  332. checkAvIncomeTag(c, 1103, 7600, 400, 7600, "2018-06-01")
  333. checkAvIncomeTag(c, 1104, 11100, 900, 11100, "2018-06-01")
  334. checkAvIncomeTag(c, 1105, 16050, 1950, 16050, "2018-06-01")
  335. checkAvIncomeTag(c, 1106, 33150, 6850, 33150, "2018-06-01")
  336. checkAvIncomeTag(c, 1107, 61650, 18350, 61650, "2018-06-01")
  337. checkAvIncomeTag(c, 1108, 105650, 44350, 105650, "2018-06-01")
  338. checkAvIncomeTag(c, 1109, 160650, 89350, 160650, "2018-06-01")
  339. checkAvIncomeTag(c, 11010, 265650, 234350, 265650, "2018-06-01")
  340. })
  341. }
  342. func Test_UpIncomeTag(t *testing.T) {
  343. Convey("Test up income tag\n", t, func() {
  344. c := context.Background()
  345. deleteAll(c)
  346. s.dao.Exec(c, "insert into up_charge_ratio(mid,ratio,adjust_type) values(110,200,0)")
  347. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1101, 110, 10)
  348. d, _ := time.ParseInLocation("2006-01-02", "2018-06-01", time.Local)
  349. err := s.run(c, d)
  350. So(err, ShouldBeNil)
  351. err = s.runStatis(c, d)
  352. So(err, ShouldBeNil)
  353. checkUpIncomeTag(c, 110, 20, 0, 20, "2018-06-01", "up_income")
  354. s.dao.Exec(c, "delete from up_charge_ratio")
  355. s.dao.Exec(c, "insert into up_charge_ratio(mid,ratio,adjust_type) values(110,20000,1)")
  356. insertAvDailyCharge(c, "2018-06-02", "2018-06-01 15:02:03", 1101, 110, 10)
  357. d, _ = time.ParseInLocation("2006-01-02", "2018-06-02", time.Local)
  358. err = s.run(c, d)
  359. So(err, ShouldBeNil)
  360. err = s.runStatis(c, d)
  361. So(err, ShouldBeNil)
  362. checkUpIncomeTag(c, 110, 20010, 0, 20030, "2018-06-02", "up_income")
  363. // av ratio + up ratio float + float
  364. s.dao.Exec(c, "delete from av_charge_ratio")
  365. s.dao.Exec(c, "delete from up_charge_ratio")
  366. s.dao.Exec(c, "insert into av_charge_ratio(av_id,ratio,adjust_type) values(1101,200,0)")
  367. s.dao.Exec(c, "insert into up_charge_ratio(mid,ratio,adjust_type) values(110,200,0)")
  368. insertAvDailyCharge(c, "2018-06-03", "2018-06-01 15:02:03", 1101, 110, 10)
  369. insertAvDailyCharge(c, "2018-06-03", "2018-06-01 15:02:03", 1102, 110, 10)
  370. d, _ = time.ParseInLocation("2006-01-02", "2018-06-03", time.Local)
  371. err = s.run(c, d)
  372. So(err, ShouldBeNil)
  373. err = s.runStatis(c, d)
  374. So(err, ShouldBeNil)
  375. var income int64 = (10*2 + 10) * 2
  376. checkUpIncomeTag(c, 110, income, 0, 20030+income, "2018-06-03", "up_income")
  377. // av ratio + up ratio float + fixed
  378. s.dao.Exec(c, "delete from av_charge_ratio")
  379. s.dao.Exec(c, "delete from up_charge_ratio")
  380. s.dao.Exec(c, "insert into av_charge_ratio(av_id,ratio,adjust_type) values(1101,200,0)")
  381. s.dao.Exec(c, "insert into up_charge_ratio(mid,ratio,adjust_type) values(110,200,1)")
  382. insertAvDailyCharge(c, "2018-06-04", "2018-06-01 15:02:03", 1101, 110, 10)
  383. insertAvDailyCharge(c, "2018-06-04", "2018-06-01 15:02:03", 1102, 110, 10)
  384. d, _ = time.ParseInLocation("2006-01-02", "2018-06-04", time.Local)
  385. err = s.run(c, d)
  386. So(err, ShouldBeNil)
  387. err = s.runStatis(c, d)
  388. So(err, ShouldBeNil)
  389. var income1 int64 = (10*2 + 10) + 200
  390. checkUpIncomeTag(c, 110, income1, 0, 20030+income+income1, "2018-06-04", "up_income")
  391. // av ratio + up ratio fixed + float
  392. s.dao.Exec(c, "delete from av_charge_ratio")
  393. s.dao.Exec(c, "delete from up_charge_ratio")
  394. s.dao.Exec(c, "insert into av_charge_ratio(av_id,ratio,adjust_type) values(1101,200,1)")
  395. s.dao.Exec(c, "insert into up_charge_ratio(mid,ratio,adjust_type) values(110,200,0)")
  396. insertAvDailyCharge(c, "2018-06-05", "2018-06-01 15:02:03", 1101, 110, 10)
  397. insertAvDailyCharge(c, "2018-06-05", "2018-06-01 15:02:03", 1102, 110, 10)
  398. d, _ = time.ParseInLocation("2006-01-02", "2018-06-05", time.Local)
  399. err = s.run(c, d)
  400. So(err, ShouldBeNil)
  401. err = s.runStatis(c, d)
  402. So(err, ShouldBeNil)
  403. var income2 int64 = 10*2 + 200 + 10*2
  404. checkUpIncomeTag(c, 110, income2, 0, 20030+income+income1+income2, "2018-06-05", "up_income")
  405. // av ratio + up ratio fixed + fixed
  406. s.dao.Exec(c, "delete from av_charge_ratio")
  407. s.dao.Exec(c, "delete from up_charge_ratio")
  408. s.dao.Exec(c, "insert into av_charge_ratio(av_id,ratio,adjust_type) values(1101,200,1)")
  409. s.dao.Exec(c, "insert into up_charge_ratio(mid,ratio,adjust_type) values(110,200,1)")
  410. insertAvDailyCharge(c, "2018-06-06", "2018-06-01 15:02:03", 1101, 110, 10)
  411. insertAvDailyCharge(c, "2018-06-06", "2018-06-01 15:02:03", 1102, 110, 10)
  412. d, _ = time.ParseInLocation("2006-01-02", "2018-06-06", time.Local)
  413. err = s.run(c, d)
  414. So(err, ShouldBeNil)
  415. err = s.runStatis(c, d)
  416. So(err, ShouldBeNil)
  417. var income3 int64 = 10 + 10 + 200 + 200
  418. checkUpIncomeTag(c, 110, income3, 0, 20030+income+income1+income2+income3, "2018-06-06", "up_income")
  419. })
  420. }
  421. // black_list
  422. func Test_AvBlackList(t *testing.T) {
  423. Convey("Test av black list\n", t, func() {
  424. c := context.Background()
  425. deleteAll(c)
  426. s.dao.Exec(c, "insert into av_black_list(av_id,mid,reason) values(1101,110,1)")
  427. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1101, 110, 10)
  428. d, _ := time.ParseInLocation("2006-01-02", "2018-06-01", time.Local)
  429. err := s.run(c, d)
  430. So(err, ShouldBeNil)
  431. err = s.runStatis(c, d)
  432. So(err, ShouldBeNil)
  433. var count int
  434. s.dao.QueryRow(c, "select count(*) from av_income").Scan(&count)
  435. So(count, ShouldEqual, 0)
  436. s.dao.QueryRow(c, "select count(*) from up_income").Scan(&count)
  437. So(count, ShouldEqual, 0)
  438. })
  439. }
  440. func Test_UpInfoVideo(t *testing.T) {
  441. Convey("Test up_info_video\n", t, func() {
  442. c := context.Background()
  443. deleteAll(c)
  444. s.dao.Exec(c, "delete from up_info_video where mid = 1115")
  445. s.dao.Exec(c, "insert into up_info_video(mid,account_type,account_state,signed_at) values(1115,1,4,'2018-05-30')")
  446. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 20:02:03", 1101, 1115, 10)
  447. d, _ := time.ParseInLocation("2006-01-02", "2018-06-01", time.Local)
  448. err := s.run(c, d)
  449. So(err, ShouldBeNil)
  450. err = s.runStatis(c, d)
  451. So(err, ShouldBeNil)
  452. var count int
  453. s.dao.QueryRow(c, "select count(*) from av_income").Scan(&count)
  454. So(count, ShouldEqual, 0)
  455. s.dao.QueryRow(c, "select count(*) from up_income").Scan(&count)
  456. So(count, ShouldEqual, 0)
  457. s.dao.Exec(c, "delete from up_info_video where mid = 1115")
  458. deleteAll(c)
  459. s.dao.Exec(c, "delete from up_info_video where mid = 1115")
  460. s.dao.Exec(c, "insert into up_info_video(mid,account_type,account_state,signed_at) values(1115,1,3,'2018-05-30')")
  461. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 20:02:03", 1101, 1115, 10)
  462. d, _ = time.ParseInLocation("2006-01-02", "2018-06-01", time.Local)
  463. err = s.run(c, d)
  464. So(err, ShouldBeNil)
  465. err = s.runStatis(c, d)
  466. So(err, ShouldBeNil)
  467. s.dao.QueryRow(c, "select count(*) from av_income").Scan(&count)
  468. So(count, ShouldEqual, 1)
  469. s.dao.QueryRow(c, "select count(*) from up_income").Scan(&count)
  470. So(count, ShouldEqual, 1)
  471. s.dao.Exec(c, "delete from up_info_video where mid = 1115")
  472. deleteAll(c)
  473. s.dao.Exec(c, "delete from up_info_video where mid = 1115")
  474. s.dao.Exec(c, "insert into up_info_video(mid,account_type,account_state,quit_at,signed_at) values(1115,1,5,'2018-06-01','2018-05-30')")
  475. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 20:02:03", 1101, 1115, 10)
  476. d, _ = time.ParseInLocation("2006-01-02", "2018-06-01", time.Local)
  477. err = s.run(c, d)
  478. So(err, ShouldBeNil)
  479. err = s.runStatis(c, d)
  480. So(err, ShouldBeNil)
  481. s.dao.QueryRow(c, "select count(*) from av_income").Scan(&count)
  482. So(count, ShouldEqual, 0)
  483. s.dao.QueryRow(c, "select count(*) from up_income").Scan(&count)
  484. So(count, ShouldEqual, 0)
  485. s.dao.Exec(c, "delete from up_info_video where mid = 1115")
  486. deleteAll(c)
  487. s.dao.Exec(c, "delete from up_info_video where mid = 1115")
  488. s.dao.Exec(c, "insert into up_info_video(mid,account_type,account_state,quit_at,signed_at) values(1115,1,5,'2018-06-02','2018-05-30')")
  489. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 20:02:03", 1101, 1115, 10)
  490. d, _ = time.ParseInLocation("2006-01-02", "2018-06-01", time.Local)
  491. err = s.run(c, d)
  492. So(err, ShouldBeNil)
  493. err = s.runStatis(c, d)
  494. So(err, ShouldBeNil)
  495. s.dao.QueryRow(c, "select count(*) from av_income").Scan(&count)
  496. So(count, ShouldEqual, 1)
  497. s.dao.QueryRow(c, "select count(*) from up_income").Scan(&count)
  498. So(count, ShouldEqual, 1)
  499. s.dao.Exec(c, "delete from up_info_video where mid = 1115")
  500. })
  501. }
  502. func Test_UpIncomeWeekAndMonthly(t *testing.T) {
  503. Convey("Test up weekly and monthly income\n", t, func() {
  504. c := context.Background()
  505. deleteAll(c)
  506. ac1 := insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1101, 110, 10)
  507. ac2 := insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1102, 111, 10)
  508. d, _ := time.ParseInLocation("2006-01-02", "2018-06-01", time.Local)
  509. err := s.run(c, d)
  510. So(err, ShouldBeNil)
  511. err = s.runStatis(c, d)
  512. So(err, ShouldBeNil)
  513. avIncome1 := checkAvIncome(c, ac1, 10, 0, 1, "2018-06-01")
  514. avIncome2 := checkAvIncome(c, ac2, 10, 0, 1, "2018-06-01")
  515. checkUpIncome(c, []*model.AvIncome{avIncome1}, 10, "up_income", "", 1)
  516. checkUpIncome(c, []*model.AvIncome{avIncome2}, 10, "up_income", "", 1)
  517. checkUpIncomeWeekly(c, []*model.AvIncome{avIncome1}, 10, 1)
  518. checkUpIncomeMonthly(c, []*model.AvIncome{avIncome2}, 10, 1)
  519. ac11 := insertAvDailyCharge(c, "2018-06-02", "2018-06-01 15:02:03", 1101, 110, 10)
  520. ac21 := insertAvDailyCharge(c, "2018-06-02", "2018-06-01 15:02:03", 1102, 111, 10)
  521. d, _ = time.ParseInLocation("2006-01-02", "2018-06-02", time.Local)
  522. err = s.run(c, d)
  523. So(err, ShouldBeNil)
  524. err = s.runStatis(c, d)
  525. So(err, ShouldBeNil)
  526. avIncome11 := checkAvIncome(c, ac11, 20, 0, 1, "2018-06-02")
  527. avIncome21 := checkAvIncome(c, ac21, 20, 0, 1, "2018-06-02")
  528. checkUpIncome(c, []*model.AvIncome{avIncome11}, 20, "up_income", "", 1)
  529. checkUpIncome(c, []*model.AvIncome{avIncome21}, 20, "up_income", "", 1)
  530. checkUpIncomeWeekly(c, []*model.AvIncome{avIncome1, avIncome11}, 20, 1)
  531. checkUpIncomeMonthly(c, []*model.AvIncome{avIncome2, avIncome21}, 20, 1)
  532. ac12 := insertAvDailyCharge(c, "2018-06-05", "2018-06-01 15:02:03", 1101, 110, 10)
  533. ac22 := insertAvDailyCharge(c, "2018-06-05", "2018-06-01 15:02:03", 1102, 111, 10)
  534. d, _ = time.ParseInLocation("2006-01-02", "2018-06-05", time.Local)
  535. err = s.run(c, d)
  536. So(err, ShouldBeNil)
  537. err = s.runStatis(c, d)
  538. So(err, ShouldBeNil)
  539. avIncome12 := checkAvIncome(c, ac12, 30, 0, 1, "2018-06-05")
  540. avIncome22 := checkAvIncome(c, ac22, 30, 0, 1, "2018-06-05")
  541. checkUpIncome(c, []*model.AvIncome{avIncome12}, 30, "up_income", "", 1)
  542. checkUpIncome(c, []*model.AvIncome{avIncome22}, 30, "up_income", "", 1)
  543. checkUpIncomeWeekly(c, []*model.AvIncome{avIncome12}, 30, 1)
  544. checkUpIncomeMonthly(c, []*model.AvIncome{avIncome2, avIncome21, avIncome22}, 30, 1)
  545. ac13 := insertAvDailyCharge(c, "2018-07-01", "2018-06-01 15:02:03", 1101, 110, 10)
  546. ac23 := insertAvDailyCharge(c, "2018-07-01", "2018-06-01 15:02:03", 1102, 111, 10)
  547. d, _ = time.ParseInLocation("2006-01-02", "2018-07-01", time.Local)
  548. err = s.run(c, d)
  549. So(err, ShouldBeNil)
  550. err = s.runStatis(c, d)
  551. So(err, ShouldBeNil)
  552. avIncome13 := checkAvIncome(c, ac13, 40, 0, 1, "2018-07-01")
  553. avIncome23 := checkAvIncome(c, ac23, 40, 0, 1, "2018-07-01")
  554. checkUpIncome(c, []*model.AvIncome{avIncome13}, 40, "up_income", "", 1)
  555. checkUpIncome(c, []*model.AvIncome{avIncome23}, 40, "up_income", "", 1)
  556. checkUpIncomeWeekly(c, []*model.AvIncome{avIncome13}, 40, 1)
  557. checkUpIncomeMonthly(c, []*model.AvIncome{avIncome23}, 40, 1)
  558. })
  559. }
  560. func Test_UpAccount(t *testing.T) {
  561. Convey("Test up_account\n", t, func() {
  562. c := context.Background()
  563. deleteAll(c)
  564. insertAvDailyCharge(c, "2018-06-30", "2018-06-01 15:02:03", 1101, 110, 10)
  565. insertAvDailyCharge(c, "2018-06-30", "2018-06-01 15:02:03", 1102, 110, 10)
  566. d, _ := time.ParseInLocation("2006-01-02", "2018-06-30", time.Local)
  567. err := s.run(c, d)
  568. So(err, ShouldBeNil)
  569. checkUpAccount(c, 110, 20, 20, 0, "2018-05")
  570. insertAvDailyCharge(c, "2018-07-01", "2018-06-01 15:02:03", 1103, 110, 10)
  571. d, _ = time.ParseInLocation("2006-01-02", "2018-07-01", time.Local)
  572. err = s.run(c, d)
  573. So(err, ShouldBeNil)
  574. checkUpAccount(c, 110, 30, 20, 0, "2018-05")
  575. insertAvDailyCharge(c, "2018-07-02", "2018-06-01 15:02:03", 1101, 110, 50)
  576. d, _ = time.ParseInLocation("2006-01-02", "2018-07-02", time.Local)
  577. err = s.run(c, d)
  578. So(err, ShouldBeNil)
  579. checkUpAccount(c, 110, 80, 20, 0, "2018-05")
  580. s.dao.Exec(c, "update up_account set total_unwithdraw_income = 60, total_withdraw_income = 20, withdraw_date_version = '2018-06' where mid = 110")
  581. insertAvDailyCharge(c, "2018-07-03", "2018-06-01 15:02:03", 1101, 110, 50)
  582. d, _ = time.ParseInLocation("2006-01-02", "2018-07-03", time.Local)
  583. err = s.run(c, d)
  584. So(err, ShouldBeNil)
  585. checkUpAccount(c, 110, 130, 110, 20, "2018-06")
  586. })
  587. }
  588. func Test_MuchUpAccount(t *testing.T) {
  589. Convey("Test much up account\n", t, func() {
  590. c := context.Background()
  591. deleteAll(c)
  592. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1101, 110, 10)
  593. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1102, 110, 10)
  594. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1103, 110, 10)
  595. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1104, 111, 10)
  596. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1105, 111, 10)
  597. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1106, 112, 10)
  598. d, _ := time.ParseInLocation("2006-01-02", "2018-06-01", time.Local)
  599. err := s.run(c, d)
  600. So(err, ShouldBeNil)
  601. checkUpAccount(c, 110, 30, 30, 0, "2018-05")
  602. checkUpAccount(c, 111, 20, 20, 0, "2018-05")
  603. checkUpAccount(c, 112, 10, 10, 0, "2018-05")
  604. insertAvDailyCharge(c, "2018-06-03", "2018-06-01 15:02:03", 1101, 110, 10)
  605. insertAvDailyCharge(c, "2018-06-03", "2018-06-01 15:02:03", 1103, 110, 10)
  606. insertAvDailyCharge(c, "2018-06-03", "2018-06-01 15:02:03", 1104, 111, 10)
  607. insertAvDailyCharge(c, "2018-06-03", "2018-06-01 15:02:03", 1105, 111, 10)
  608. insertAvDailyCharge(c, "2018-06-03", "2018-06-01 15:02:03", 1106, 112, 10)
  609. insertAvDailyCharge(c, "2018-06-03", "2018-06-01 15:02:03", 1107, 112, 10)
  610. d, _ = time.ParseInLocation("2006-01-02", "2018-06-03", time.Local)
  611. err = s.run(c, d)
  612. So(err, ShouldBeNil)
  613. checkUpAccount(c, 110, 50, 50, 0, "2018-05")
  614. checkUpAccount(c, 111, 40, 40, 0, "2018-05")
  615. checkUpAccount(c, 112, 30, 30, 0, "2018-05")
  616. })
  617. }
  618. func checkAllData(c context.Context, ac *model.AvCharge, totalIncome, taxMoney, count int64, date string) {
  619. d, _ := time.ParseInLocation(_layout, date, time.Local)
  620. // av
  621. avIncome := checkAvIncome(c, ac, totalIncome, taxMoney, count, date)
  622. checkAvIncomeStatis(c, avIncome, totalIncome) // Background
  623. checkAvIncomeDateStatis(c, int(0), int(1), int(ac.TagID), int(totalIncome), int(12), d, "av_income_daily_statis")
  624. weekD := getStartWeeklyDate(d)
  625. checkAvIncomeDateStatis(c, int(0), int(1), int(ac.TagID), int(totalIncome), int(12), weekD, "av_income_weekly_statis")
  626. monthD := getStartMonthlyDate(d)
  627. checkAvIncomeDateStatis(c, int(0), int(1), int(ac.TagID), int(totalIncome), int(12), monthD, "av_income_monthly_statis")
  628. // up
  629. up := checkUpIncome(c, []*model.AvIncome{avIncome}, avIncome.Income, "up_income", "", 1)
  630. checkUpIncomeWeekly(c, []*model.AvIncome{avIncome}, avIncome.Income, 1)
  631. checkUpIncomeMonthly(c, []*model.AvIncome{avIncome}, avIncome.Income, 1)
  632. checkUpIncomeStatis(c, up.MID, avIncome.Income)
  633. checkUpIncomeDailyStatis(c, 0, 1, avIncome.Income, d)
  634. checkUpAccount(c, up.MID, avIncome.Income, avIncome.Income, 0, "2018-05")
  635. }
  636. func checkAvIncome(c context.Context, ac *model.AvCharge, totalIncome, taxMoney, count int64, date string) *model.AvIncome {
  637. ai := &model.AvIncome{}
  638. err := s.dao.QueryRow(c, fmt.Sprintf("select av_id,mid,tag_id,is_original,upload_time,play_count,total_income,income,tax_money,date from av_income where av_id = %d and date = '%s'", ac.AvID, date)).Scan(
  639. &ai.AvID, &ai.MID, &ai.TagID, &ai.IsOriginal, &ai.UploadTime, &ai.PlayCount, &ai.TotalIncome, &ai.Income, &ai.TaxMoney, &ai.Date)
  640. So(err, ShouldBeNil)
  641. So(ac.AvID, ShouldEqual, ai.AvID)
  642. So(ac.MID, ShouldEqual, ai.MID)
  643. So(ac.TagID, ShouldEqual, ai.TagID)
  644. So(ac.IsOriginal, ShouldEqual, ai.IsOriginal)
  645. So(ac.UploadTime, ShouldEqual, ai.UploadTime)
  646. So(ac.TotalPlayCount, ShouldEqual, ai.PlayCount)
  647. So(totalIncome, ShouldEqual, ai.TotalIncome)
  648. So(taxMoney, ShouldEqual, ai.TaxMoney)
  649. So(ac.IncCharge, ShouldEqual, ai.Income)
  650. So(ac.Date, ShouldEqual, ai.Date)
  651. return ai
  652. }
  653. func checkAvIncomeTag(c context.Context, avID, income, taxMoney, totalIncome int64, date string) {
  654. ai := &model.AvIncome{}
  655. err := s.dao.QueryRow(c, fmt.Sprintf("select total_income,income,tax_money from av_income where av_id = %d and date = '%s'", avID, date)).Scan(
  656. &ai.TotalIncome, &ai.Income, &ai.TaxMoney)
  657. So(err, ShouldBeNil)
  658. So(totalIncome, ShouldEqual, ai.TotalIncome)
  659. So(taxMoney, ShouldEqual, ai.TaxMoney)
  660. So(income, ShouldEqual, ai.Income)
  661. }
  662. func checkAvIncomeStatis(c context.Context, ac *model.AvIncome, totalIncome int64) {
  663. ai := &model.AvIncomeStat{}
  664. err := s.dao.QueryRow(c, fmt.Sprintf("select av_id,mid,tag_id,is_original,upload_time,total_income from av_income_statis where av_id = %d", ac.AvID)).Scan(
  665. &ai.AvID, &ai.MID, &ai.TagID, &ai.IsOriginal, &ai.UploadTime, &ai.TotalIncome)
  666. So(err, ShouldBeNil)
  667. So(ac.AvID, ShouldEqual, ai.AvID)
  668. So(ac.MID, ShouldEqual, ai.MID)
  669. So(ac.TagID, ShouldEqual, ai.TagID)
  670. So(ac.IsOriginal, ShouldEqual, ai.IsOriginal)
  671. So(ac.UploadTime, ShouldEqual, ai.UploadTime)
  672. So(ac.TotalIncome, ShouldEqual, ai.TotalIncome)
  673. }
  674. func checkAvIncomeDateStatis(c context.Context, section, avs, categoryID, income, count int, d time.Time, table string) {
  675. xd := xtime.Time(d.Unix())
  676. ads := &model.DateStatis{}
  677. err := s.dao.QueryRow(c, fmt.Sprintf("select avs,income,cdate from %s where money_section = %d and category_id = %d and cdate = '%s'", table, section, categoryID, d.Format(_layout))).Scan(
  678. &ads.Count, &ads.Income, &ads.CDate)
  679. So(err, ShouldBeNil)
  680. So(income, ShouldEqual, ads.Income)
  681. So(xd, ShouldEqual, ads.CDate)
  682. So(avs, ShouldEqual, ads.Count)
  683. var ccount int64
  684. err = s.dao.QueryRow(c, fmt.Sprintf("select count(*) from %s where category_id = %d and cdate = '%s'", table, categoryID, d.Format(_layout))).Scan(&ccount)
  685. So(err, ShouldBeNil)
  686. So(count, ShouldEqual, ccount)
  687. }
  688. func checkUpIncome(c context.Context, avs []*model.AvIncome, totalIncome int64, table string, date string, count int64) (up *model.UpIncome) {
  689. if len(avs) == 0 {
  690. return
  691. }
  692. mid := avs[0].MID
  693. if date == "" {
  694. date = avs[0].Date.Time().Format(_layout)
  695. }
  696. var playCount, avIncome, taxMoney int64
  697. for _, av := range avs {
  698. playCount += av.PlayCount
  699. avIncome += av.Income
  700. taxMoney += av.TaxMoney
  701. }
  702. up = &model.UpIncome{}
  703. err := s.dao.QueryRow(c, fmt.Sprintf("select mid,av_count,play_count,av_income,audio_income,column_income,tax_money,income,total_income from %s where mid = %d and date = '%s'", table, mid, date)).Scan(
  704. &up.MID, &up.AvCount, &up.PlayCount, &up.AvIncome, &up.AudioIncome, &up.ColumnIncome, &up.TaxMoney, &up.Income, &up.TotalIncome)
  705. So(err, ShouldBeNil)
  706. So(count, ShouldEqual, up.AvCount)
  707. // So(playCount, ShouldEqual, up.PlayCount)
  708. So(avIncome, ShouldEqual, up.AvIncome)
  709. So(0, ShouldEqual, up.AudioIncome)
  710. So(0, ShouldEqual, up.ColumnIncome)
  711. So(taxMoney, ShouldEqual, up.TaxMoney)
  712. So(totalIncome, ShouldEqual, up.TotalIncome)
  713. return up
  714. }
  715. func checkUpIncomeTag(c context.Context, mid, income, taxMoney, totalIncome int64, date, table string) {
  716. up := &model.UpIncome{}
  717. err := s.dao.QueryRow(c, fmt.Sprintf("select tax_money,income,total_income from %s where mid = %d and date = '%s'", table, mid, date)).Scan(
  718. &up.TaxMoney, &up.Income, &up.TotalIncome)
  719. So(err, ShouldBeNil)
  720. So(income, ShouldEqual, up.Income)
  721. So(taxMoney, ShouldEqual, up.TaxMoney)
  722. So(totalIncome, ShouldEqual, up.TotalIncome)
  723. }
  724. func checkUpIncomeWeekly(c context.Context, avs []*model.AvIncome, totalIncome, count int64) {
  725. m := make(map[string][]*model.AvIncome)
  726. for _, av := range avs {
  727. d := getStartWeeklyDate(av.Date.Time()).Format(_layout)
  728. if _, ok := m[d]; !ok {
  729. m[d] = make([]*model.AvIncome, 0)
  730. }
  731. m[d] = append(m[d], av)
  732. }
  733. for date, avs := range m {
  734. checkUpIncome(c, avs, totalIncome, "up_income_weekly", date, count)
  735. }
  736. }
  737. func checkUpIncomeMonthly(c context.Context, avs []*model.AvIncome, totalIncome, count int64) {
  738. m := make(map[string][]*model.AvIncome)
  739. for _, av := range avs {
  740. d := getStartMonthlyDate(av.Date.Time()).Format(_layout)
  741. if _, ok := m[d]; !ok {
  742. m[d] = make([]*model.AvIncome, 0)
  743. }
  744. m[d] = append(m[d], av)
  745. }
  746. for date, avs := range m {
  747. checkUpIncome(c, avs, totalIncome, "up_income_monthly", date, count)
  748. }
  749. }
  750. func checkUpIncomeStatis(c context.Context, mid, totalIncome int64) {
  751. var income int64
  752. err := s.dao.QueryRow(c, fmt.Sprintf("select total_income from up_income_statis where mid = %d", mid)).Scan(&income)
  753. So(err, ShouldBeNil)
  754. So(totalIncome, ShouldEqual, income)
  755. }
  756. func checkUpIncomeDailyStatis(c context.Context, section, ups, income int64, d time.Time) {
  757. xd := xtime.Time(d.Unix())
  758. ads := &model.DateStatis{}
  759. err := s.dao.QueryRow(c, fmt.Sprintf("select ups,income,cdate from up_income_daily_statis where money_section = %d and cdate = '%s'", section, d.Format(_layout))).Scan(
  760. &ads.Count, &ads.Income, &ads.CDate)
  761. So(err, ShouldBeNil)
  762. So(income, ShouldEqual, ads.Income)
  763. So(xd, ShouldEqual, ads.CDate)
  764. So(ups, ShouldEqual, ads.Count)
  765. var ccount int64
  766. err = s.dao.QueryRow(c, fmt.Sprintf("select count(*) from up_income_daily_statis where cdate = '%s'", d.Format(_layout))).Scan(&ccount)
  767. So(err, ShouldBeNil)
  768. So(12, ShouldEqual, ccount)
  769. }
  770. func checkUpAccount(c context.Context, mid, total, unwithdraw, withdraw int64, dateVersion string) {
  771. up := &model.UpAccount{}
  772. err := s.dao.QueryRow(c, fmt.Sprintf("select total_income,total_unwithdraw_income,total_withdraw_income,withdraw_date_version from up_account where mid = %d", mid)).Scan(
  773. &up.TotalIncome, &up.TotalUnwithdrawIncome, &up.TotalWithdrawIncome, &up.WithdrawDateVersion)
  774. So(err, ShouldBeNil)
  775. So(up.TotalIncome, ShouldEqual, total)
  776. So(up.TotalUnwithdrawIncome, ShouldEqual, unwithdraw)
  777. So(up.TotalWithdrawIncome, ShouldEqual, withdraw)
  778. So(up.WithdrawDateVersion, ShouldEqual, dateVersion)
  779. }
  780. // func prepareTest(c context.Context) {
  781. // s.dao.Exec(c, "delete from av_daily_charge_06")
  782. // s.dao.Exec(c, "delete from av_daily_charge_07")
  783. // s.dao.Exec(c, "delete from av_income where av_id = 1101")
  784. // s.dao.Exec(c, "delete from av_income_statis where av_id = 1101")
  785. // s.dao.Exec(c, "delete from av_income_daily_statis")
  786. // s.dao.Exec(c, "delete from av_income_weekly_statis")
  787. // s.dao.Exec(c, "delete from av_income_monthly_statis")
  788. // s.dao.Exec(c, "delete from av_weekly_charge where av_id = 1101")
  789. // s.dao.Exec(c, "delete from av_monthly_charge where av_id = 1101")
  790. // s.dao.Exec(c, "delete from av_charge_statis where av_id = 1101")
  791. // s.dao.Exec(c, "delete from up_income where mid = 110")
  792. // s.dao.Exec(c, "delete from up_income_weekly where mid = 110")
  793. // s.dao.Exec(c, "delete from up_income_monthly where mid = 110")
  794. // s.dao.Exec(c, "delete from up_income_statis where mid = 110")
  795. // s.dao.Exec(c, "delete from up_income_daily_statis")
  796. // s.dao.Exec(c, "delete from up_account where mid = 110")
  797. // s.dao.Exec(c, "delete from up_daily_charge where mid = 110")
  798. // s.dao.Exec(c, "delete from up_weekly_charge where mid = 110")
  799. // s.dao.Exec(c, "delete from up_monthly_charge where mid = 110")
  800. // s.dao.Exec(c, "delete from up_av_statis where mid = 110")
  801. // }
  802. func deleteAll(c context.Context) {
  803. // s.dao.Exec(c, "truncate up_info_video")
  804. // s.dao.Exec(c, "truncate av_daily_charge_07")
  805. // s.dao.Exec(c, "truncate av_daily_charge_06")
  806. s.dao.Exec(c, "truncate av_black_list")
  807. s.dao.Exec(c, "truncate av_charge_ratio")
  808. s.dao.Exec(c, "truncate up_charge_ratio")
  809. s.dao.Exec(c, "truncate av_income")
  810. s.dao.Exec(c, "truncate av_income_statis")
  811. s.dao.Exec(c, "truncate av_income_daily_statis")
  812. s.dao.Exec(c, "truncate av_income_weekly_statis")
  813. s.dao.Exec(c, "truncate av_income_monthly_statis")
  814. s.dao.Exec(c, "truncate up_income")
  815. s.dao.Exec(c, "truncate up_income_weekly")
  816. s.dao.Exec(c, "truncate up_income_monthly")
  817. s.dao.Exec(c, "truncate up_income_statis")
  818. s.dao.Exec(c, "truncate up_income_daily_statis")
  819. s.dao.Exec(c, "truncate up_account")
  820. s.dao.Exec(c, "truncate up_av_daily_statis")
  821. s.dao.Exec(c, "truncate up_column_daily_statis")
  822. s.dao.Exec(c, "truncate column_income_daily_statis")
  823. s.dao.Exec(c, "truncate column_income_weekly_statis")
  824. s.dao.Exec(c, "truncate column_income_monthly_statis")
  825. // s.dao.Exec(c, "truncate up_info_column")
  826. s.dao.Exec(c, "truncate column_income")
  827. s.dao.Exec(c, "truncate column_income_statis")
  828. }
  829. func getAvDailyChargeStruct(date string, uploadDate string, avID, mid, charge int64) *model.AvCharge {
  830. d, _ := time.ParseInLocation(_layout, date, time.Local)
  831. upD, _ := time.ParseInLocation(_layoutSec, uploadDate, time.Local)
  832. ac := &model.AvCharge{
  833. AvID: avID,
  834. MID: mid,
  835. TagID: 1,
  836. IsOriginal: 1,
  837. DanmakuCount: charge,
  838. CommentCount: charge,
  839. CollectCount: charge,
  840. CoinCount: charge,
  841. ShareCount: charge,
  842. ElecPayCount: charge,
  843. TotalPlayCount: charge * int64(3),
  844. WebPlayCount: charge,
  845. AppPlayCount: charge,
  846. H5PlayCount: charge,
  847. LvUnknown: charge,
  848. Lv0: charge,
  849. Lv1: charge,
  850. Lv2: charge,
  851. Lv3: charge,
  852. Lv4: charge,
  853. Lv5: charge,
  854. Lv6: charge,
  855. VScore: charge,
  856. IncCharge: charge,
  857. TotalCharge: 0,
  858. Date: xtime.Time(d.Unix()),
  859. UploadTime: xtime.Time(upD.Unix()),
  860. DBState: _dbInsert,
  861. }
  862. return ac
  863. }
  864. func getColumnDailyChargeStruct(date string, uploadDate string, aid, mid, charge int64) *model.ColumnCharge {
  865. d, _ := time.ParseInLocation(_layout, date, time.Local)
  866. upD, _ := time.ParseInLocation(_layoutSec, uploadDate, time.Local)
  867. cc := &model.ColumnCharge{
  868. ArticleID: aid,
  869. Title: "sssss",
  870. MID: mid,
  871. TagID: 1,
  872. IncCharge: charge,
  873. IncViewCount: 10,
  874. Date: xtime.Time(d.Unix()),
  875. UploadTime: xtime.Time(upD.Unix()),
  876. }
  877. return cc
  878. }
  879. func insertAvDailyCharge(c context.Context, date string, uploadDate string, avID, mid, charge int64) *model.AvCharge {
  880. s.dao.Exec(c, fmt.Sprintf("insert into task_status(type,date,status) values(1,'%s',1) on duplicate key update date=values(date)", date))
  881. ac := getAvDailyChargeStruct(date, uploadDate, avID, mid, charge)
  882. _, err := s.avCharge.avChargeBatchInsert(c, []*model.AvCharge{ac}, fmt.Sprintf("av_daily_charge_%s", strings.Split(date, "-")[1]))
  883. So(err, ShouldBeNil)
  884. return ac
  885. }
  886. func insertBubbleMeta(c context.Context, values string) {
  887. s.dao.Exec(c, fmt.Sprintf("insert into lottery_av_info(av_id,b_type,date) values %s on duplicate key update b_type=values(b_type) date=values(date)", values))
  888. }
  889. func insertAvDailyChargeBatch(c context.Context, date string, acs []*model.AvCharge) {
  890. s.avCharge.avChargeBatchInsert(c, acs, fmt.Sprintf("av_daily_charge_%s", strings.Split(date, "-")[1]))
  891. }
  892. func insertColumnDailyChargeBatch(c context.Context, date string, acs []*model.ColumnCharge) {
  893. s.avCharge.columnChargeBatchInsert(c, acs, "column_daily_charge")
  894. }
  895. func batchInsertUpInfoVideo(c context.Context, mids []int64) {
  896. var buf bytes.Buffer
  897. for _, mid := range mids {
  898. buf.WriteString("(")
  899. buf.WriteString(strconv.FormatInt(mid, 10))
  900. buf.WriteByte(',')
  901. buf.WriteString("1")
  902. buf.WriteByte(',')
  903. buf.WriteString("3")
  904. buf.WriteString(")")
  905. buf.WriteByte(',')
  906. }
  907. if buf.Len() > 0 {
  908. buf.Truncate(buf.Len() - 1)
  909. }
  910. values := buf.String()
  911. buf.Reset()
  912. s.dao.Exec(c, "delete from up_info_video")
  913. s.dao.Exec(c, "delete from up_info_column")
  914. s.dao.Exec(c, fmt.Sprintf("insert into up_info_video(mid,account_type,account_state) values %s", values))
  915. s.dao.Exec(c, fmt.Sprintf("insert into up_info_column(mid,account_type,account_state) values %s", values))
  916. }
  917. // test 2018-09
  918. func BenchmarkRunOneDay(b *testing.B) {
  919. c := context.Background()
  920. deleteAll(c)
  921. date := "2018-09-02"
  922. s.dao.Exec(c, "truncate av_daily_charge_09")
  923. s.dao.Exec(c, "truncate column_daily_charge")
  924. uploadDate := "2018-09-01 21:20:30"
  925. acs := make([]*model.AvCharge, 20000)
  926. ccs := make([]*model.ColumnCharge, 20000)
  927. j := 0
  928. for i := 1; i <= 1000000; i++ {
  929. if j >= 20000 {
  930. insertAvDailyChargeBatch(c, date, acs)
  931. insertColumnDailyChargeBatch(c, date, ccs)
  932. j = 0
  933. }
  934. acs[j] = getAvDailyChargeStruct(date, uploadDate, int64(i), int64(j+1), 100)
  935. ccs[j] = getColumnDailyChargeStruct(date, uploadDate, int64(i), int64(j+1), 100)
  936. j++
  937. }
  938. if j > 0 {
  939. insertAvDailyChargeBatch(c, date, acs[:j])
  940. insertColumnDailyChargeBatch(c, date, ccs[:j])
  941. }
  942. mids := make([]int64, 20000)
  943. for i := 1; i <= 20000; i++ {
  944. mids[i-1] = int64(i)
  945. }
  946. batchInsertUpInfoVideo(c, mids)
  947. fmt.Println("start run...........")
  948. d, _ := time.ParseInLocation("2006-01-02", date, time.Local)
  949. for n := 0; n < b.N; n++ {
  950. s.run(c, d)
  951. }
  952. }