mcn.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "time"
  7. "go-common/app/job/main/mcn/model"
  8. xsql "go-common/library/database/sql"
  9. xtime "go-common/library/time"
  10. "go-common/library/xstr"
  11. "github.com/pkg/errors"
  12. )
  13. const (
  14. _upMcnSignStateOPSQL = "UPDATE mcn_sign SET state = ? WHERE id = ?"
  15. _upMcnUpStateOPSQL = "UPDATE mcn_up SET state = ?, state_change_time = ? WHERE id = ?"
  16. _upMcnSignPayExpOPSQL = "UPDATE mcn_sign SET pay_expire_state = 2 WHERE id = ?"
  17. _upMcnSignEmailStateSQL = "UPDATE mcn_sign SET email_state = 2 WHERE id IN (%s)"
  18. _upMcnSignPayEmailStateSQL = "UPDATE mcn_sign_pay SET email_state = 2 WHERE id IN (%s)"
  19. _inMcnDataSummarySQL = "INSERT mcn_data_summary(mcn_mid,sign_id,up_count,fans_count_accumulate,generate_date,data_type) VALUES (?,?,?,?,?,1)"
  20. _selMcnSignsSQL = `SELECT id,begin_date,end_date,state FROM mcn_sign`
  21. _selMcnUpsSQL = `SELECT id,begin_date,end_date,state FROM mcn_up LIMIT ?,?`
  22. _selMcnSignPayWarnsSQL = `SELECT p.sign_id,p.due_date,p.pay_value FROM mcn_sign_pay p INNER JOIN mcn_sign s ON p.sign_id = s.id WHERE p.state = 0 AND
  23. s.state = 10 AND s.end_date >= ? AND s.begin_date <= p.due_date AND p.due_date <= s.end_date AND date_sub(p.due_date,interval 7 day) <= ?`
  24. _selMcnSignMidsSQL = "SELECT id,mcn_mid FROM mcn_sign WHERE state = 10"
  25. _selMcnUPCountSQL = "SELECT sign_id,count(up_mid) as count FROM mcn_up WHERE sign_id IN (%s) AND state = 10 GROUP BY sign_id"
  26. _selMcnUPMidsSQL = "SELECT sign_id,up_mid FROM mcn_up WHERE sign_id IN (%s) AND state = 10"
  27. _selCrmUpMidsSumSQL = "SELECT SUM(fans_count) as count FROM up_base_info WHERE mid IN (%s)"
  28. _selMcnSignPayDues = `SELECT p.id, p.mcn_mid, p.sign_id, p.due_date, p.pay_value FROM mcn_sign_pay p LEFT JOIN mcn_sign s ON p.sign_id = s.id
  29. WHERE p.due_date <= ? AND p.email_state = 1 AND p.state = 0 AND s.state = 10 AND s.end_date >= ?`
  30. _selMcnSignDues = "SELECT id, mcn_mid, begin_date, end_date FROM mcn_sign WHERE end_date <= ? and end_date >= ? and email_state = 1"
  31. )
  32. // UpMcnSignStateOP .
  33. func (d *Dao) UpMcnSignStateOP(c context.Context, signID int64, state int8) (rows int64, err error) {
  34. var res sql.Result
  35. if res, err = d.db.Exec(c, _upMcnSignStateOPSQL, state, signID); err != nil {
  36. return rows, err
  37. }
  38. return res.RowsAffected()
  39. }
  40. // UpMcnUpStateOP .
  41. func (d *Dao) UpMcnUpStateOP(c context.Context, signUpID int64, state int8) (rows int64, err error) {
  42. var res sql.Result
  43. if res, err = d.db.Exec(c, _upMcnUpStateOPSQL, state, time.Now(), signUpID); err != nil {
  44. return rows, err
  45. }
  46. return res.RowsAffected()
  47. }
  48. // UpMcnSignPayExpOP .
  49. func (d *Dao) UpMcnSignPayExpOP(c context.Context, signPayID int64) (rows int64, err error) {
  50. var res sql.Result
  51. if res, err = d.db.Exec(c, _upMcnSignPayExpOPSQL, signPayID); err != nil {
  52. return rows, err
  53. }
  54. return res.RowsAffected()
  55. }
  56. // UpMcnSignPayEmailState .
  57. func (d *Dao) UpMcnSignPayEmailState(c context.Context, ids []int64) (rows int64, err error) {
  58. var res sql.Result
  59. if res, err = d.db.Exec(c, fmt.Sprintf(_upMcnSignPayEmailStateSQL, xstr.JoinInts(ids))); err != nil {
  60. return rows, err
  61. }
  62. return res.RowsAffected()
  63. }
  64. // UpMcnSignEmailState .
  65. func (d *Dao) UpMcnSignEmailState(c context.Context, ids []int64) (rows int64, err error) {
  66. var res sql.Result
  67. if res, err = d.db.Exec(c, fmt.Sprintf(_upMcnSignEmailStateSQL, xstr.JoinInts(ids))); err != nil {
  68. return rows, err
  69. }
  70. return res.RowsAffected()
  71. }
  72. // AddMcnDataSummary .
  73. func (d *Dao) AddMcnDataSummary(c context.Context, mcnMid, signID, upCount, fansCountAccumulate int64, genDate xtime.Time) (err error) {
  74. _, err = d.db.Exec(c, _inMcnDataSummarySQL, mcnMid, signID, upCount, fansCountAccumulate, genDate)
  75. return
  76. }
  77. // McnSigns .
  78. func (d *Dao) McnSigns(c context.Context) (mss []*model.MCNSignInfo, err error) {
  79. var rows *xsql.Rows
  80. if rows, err = d.db.Query(c, _selMcnSignsSQL); err != nil {
  81. return
  82. }
  83. defer rows.Close()
  84. for rows.Next() {
  85. ms := new(model.MCNSignInfo)
  86. if err = rows.Scan(&ms.SignID, &ms.BeginDate, &ms.EndDate, &ms.State); err != nil {
  87. if err == xsql.ErrNoRows {
  88. err = nil
  89. return
  90. }
  91. return
  92. }
  93. mss = append(mss, ms)
  94. }
  95. err = rows.Err()
  96. return
  97. }
  98. // McnUps .
  99. func (d *Dao) McnUps(c context.Context, offset, limit int64) (ups []*model.MCNUPInfo, err error) {
  100. var rows *xsql.Rows
  101. if rows, err = d.db.Query(c, _selMcnUpsSQL, offset, limit); err != nil {
  102. return
  103. }
  104. defer rows.Close()
  105. for rows.Next() {
  106. up := new(model.MCNUPInfo)
  107. if err = rows.Scan(&up.SignUpID, &up.BeginDate, &up.EndDate, &up.State); err != nil {
  108. if err == xsql.ErrNoRows {
  109. err = nil
  110. return
  111. }
  112. return
  113. }
  114. ups = append(ups, up)
  115. }
  116. err = rows.Err()
  117. return
  118. }
  119. // McnSignPayWarns .
  120. func (d *Dao) McnSignPayWarns(c context.Context) (sps []*model.SignPayInfo, err error) {
  121. var (
  122. rows *xsql.Rows
  123. now time.Time
  124. template = time.Now().Format(model.TimeFormatDay)
  125. )
  126. if now, err = time.ParseInLocation(model.TimeFormatDay, template, time.Local); err != nil {
  127. err = errors.Errorf("time.ParseInLocation(%s) error(%+v)", template, err)
  128. return
  129. }
  130. if rows, err = d.db.Query(c, _selMcnSignPayWarnsSQL, now, now); err != nil {
  131. return
  132. }
  133. defer rows.Close()
  134. for rows.Next() {
  135. sp := new(model.SignPayInfo)
  136. if err = rows.Scan(&sp.SignID, &sp.DueDate, &sp.PayValue); err != nil {
  137. if err == xsql.ErrNoRows {
  138. err = nil
  139. return
  140. }
  141. return
  142. }
  143. sps = append(sps, sp)
  144. }
  145. err = rows.Err()
  146. return
  147. }
  148. // McnSignMids .
  149. func (d *Dao) McnSignMids(c context.Context) (msid map[int64]int64, sids []int64, err error) {
  150. var rows *xsql.Rows
  151. if rows, err = d.db.Query(c, _selMcnSignMidsSQL); err != nil {
  152. return
  153. }
  154. defer rows.Close()
  155. msid = make(map[int64]int64)
  156. for rows.Next() {
  157. var signID, mcnMid int64
  158. if err = rows.Scan(&signID, &mcnMid); err != nil {
  159. if err == xsql.ErrNoRows {
  160. err = nil
  161. return
  162. }
  163. return
  164. }
  165. msid[signID] = mcnMid
  166. sids = append(sids, signID)
  167. }
  168. err = rows.Err()
  169. return
  170. }
  171. // McnUPCount .
  172. func (d *Dao) McnUPCount(c context.Context, signIDs []int64) (mmc map[int64]int64, err error) {
  173. var rows *xsql.Rows
  174. if rows, err = d.db.Query(c, fmt.Sprintf(_selMcnUPCountSQL, xstr.JoinInts(signIDs))); err != nil {
  175. return
  176. }
  177. defer rows.Close()
  178. mmc = make(map[int64]int64)
  179. for rows.Next() {
  180. var signID, count int64
  181. if err = rows.Scan(&signID, &count); err != nil {
  182. if err == xsql.ErrNoRows {
  183. err = nil
  184. return
  185. }
  186. return
  187. }
  188. mmc[signID] = count
  189. }
  190. err = rows.Err()
  191. return
  192. }
  193. // McnUPMids .
  194. func (d *Dao) McnUPMids(c context.Context, signIDs []int64) (mup map[int64][]int64, err error) {
  195. var rows *xsql.Rows
  196. if rows, err = d.db.Query(c, fmt.Sprintf(_selMcnUPMidsSQL, xstr.JoinInts(signIDs))); err != nil {
  197. return
  198. }
  199. defer rows.Close()
  200. mup = make(map[int64][]int64)
  201. for rows.Next() {
  202. var signID, upMid int64
  203. if err = rows.Scan(&signID, &upMid); err != nil {
  204. if err == xsql.ErrNoRows {
  205. err = nil
  206. return
  207. }
  208. return
  209. }
  210. mup[signID] = append(mup[signID], upMid)
  211. }
  212. err = rows.Err()
  213. return
  214. }
  215. // CrmUpMidsSum .
  216. func (d *Dao) CrmUpMidsSum(c context.Context, upMids []int64) (count int64, err error) {
  217. row := d.db.QueryRow(c, fmt.Sprintf(_selCrmUpMidsSumSQL, xstr.JoinInts(upMids)))
  218. var countNull sql.NullInt64
  219. if err = row.Scan(&countNull); err != nil {
  220. if err == sql.ErrNoRows {
  221. err = nil
  222. return
  223. }
  224. }
  225. count = countNull.Int64
  226. return
  227. }
  228. // McnSignPayDues .
  229. func (d *Dao) McnSignPayDues(c context.Context) (sps []*model.SignPayInfo, err error) {
  230. var (
  231. rows *xsql.Rows
  232. now, future time.Time
  233. nowDate = time.Now()
  234. date = nowDate.AddDate(0, 0, 7)
  235. template = date.Format(model.TimeFormatDay)
  236. nowTemplate = nowDate.Format(model.TimeFormatDay)
  237. )
  238. if now, err = time.ParseInLocation(model.TimeFormatDay, nowTemplate, time.Local); err != nil {
  239. err = errors.Errorf("time.ParseInLocation(%s) now error(%+v)", nowTemplate, err)
  240. return
  241. }
  242. if future, err = time.ParseInLocation(model.TimeFormatDay, template, time.Local); err != nil {
  243. err = errors.Errorf("time.ParseInLocation(%s) error(%+v)", template, err)
  244. return
  245. }
  246. if rows, err = d.db.Query(c, _selMcnSignPayDues, future, now); err != nil {
  247. return
  248. }
  249. defer rows.Close()
  250. for rows.Next() {
  251. sp := new(model.SignPayInfo)
  252. if err = rows.Scan(&sp.SignPayID, &sp.McnMid, &sp.SignID, &sp.DueDate, &sp.PayValue); err != nil {
  253. if err == xsql.ErrNoRows {
  254. err = nil
  255. return
  256. }
  257. return
  258. }
  259. sps = append(sps, sp)
  260. }
  261. err = rows.Err()
  262. return
  263. }
  264. // McnSignDues .
  265. func (d *Dao) McnSignDues(c context.Context) (mss []*model.MCNSignInfo, err error) {
  266. var (
  267. rows *xsql.Rows
  268. now, future time.Time
  269. nowDate = time.Now()
  270. nowTemplate = nowDate.Format(model.TimeFormatDay)
  271. futureDate = nowDate.AddDate(0, 0, 30)
  272. futureTemplate = futureDate.Format(model.TimeFormatDay)
  273. )
  274. if now, err = time.ParseInLocation(model.TimeFormatDay, nowTemplate, time.Local); err != nil {
  275. err = errors.Errorf("time.ParseInLocation(%s) now error(%+v)", nowTemplate, err)
  276. return
  277. }
  278. if future, err = time.ParseInLocation(model.TimeFormatDay, futureTemplate, time.Local); err != nil {
  279. err = errors.Errorf("time.ParseInLocation(%s) future error(%+v)", futureTemplate, err)
  280. return
  281. }
  282. if rows, err = d.db.Query(c, _selMcnSignDues, future, now); err != nil {
  283. return
  284. }
  285. defer rows.Close()
  286. for rows.Next() {
  287. ms := new(model.MCNSignInfo)
  288. if err = rows.Scan(&ms.SignID, &ms.McnMid, &ms.BeginDate, &ms.EndDate); err != nil {
  289. if err == xsql.ErrNoRows {
  290. err = nil
  291. return
  292. }
  293. return
  294. }
  295. mss = append(mss, ms)
  296. }
  297. err = rows.Err()
  298. return
  299. }