mcn_manage.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. package up
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/pkg/errors"
  10. "go-common/app/admin/main/mcn/model"
  11. xsql "go-common/library/database/sql"
  12. "go-common/library/xstr"
  13. )
  14. const (
  15. _inMCNRenewalSQL = `INSERT INTO mcn_sign(mcn_mid,company_name,company_license_id,company_license_link,
  16. contract_link,contact_name,contact_title,contact_idcard,contact_phone,begin_date,end_date,state,permission) VALUES (?,?,?,?,?,?,?,?,?,?,?,15,?)`
  17. _inMCNPaySQL = "INSERT INTO mcn_sign_pay(mcn_mid,sign_id,due_date,pay_value) VALUES %s"
  18. _inMCNUPsSQL = "INSERT INTO mcn_up(sign_id,mcn_mid,up_mid,begin_date,end_date,contract_link,up_auth_link,state,state_change_time,up_type,site_link,confirm_time,permission,publication_price) VALUES %s"
  19. _upMCNStateSQL = "UPDATE mcn_sign SET state=? WHERE id=? AND mcn_mid=?"
  20. _upMCNPaySQL = "UPDATE mcn_sign_pay SET due_date=?,pay_value=? WHERE id=? AND mcn_mid=? AND sign_id=?"
  21. _upMCNPayStateSQL = "UPDATE mcn_sign_pay SET state=? WHERE id=? AND mcn_mid=? AND sign_id=?"
  22. // _upMCNSignRenewaIDSQL = "UPDATE mcn_sign SET renewal_id=? WHERE id=?"
  23. _upMCNUPStateSQL = "UPDATE mcn_up SET state=?,state_change_time=? WHERE id =? AND sign_id=? AND mcn_mid=? AND up_mid=?"
  24. _upMCNImportUPSQL = "UPDATE mcn_data_import_up SET is_reward=1 WHERE sign_id=? AND up_mid=?"
  25. _upMCNPermissionSQL = "UPDATE mcn_sign SET permission = ? WHERE id=?"
  26. _selMCNListSQL = `SELECT s.id, s.mcn_mid,s.state,s.begin_date,s.end_date,s.permission,ifnull(ds.up_count,0),ifnull(ds.fans_count_accumulate,0),
  27. ifnull(ds.fans_count_online_accumulate,0),ifnull(ds.fans_count_real_accumulate,0),ifnull(ds.fans_count_cheat_accumulate,0),ifnull(ds.generate_date,0)
  28. FROM (SELECT * FROM mcn_data_summary WHERE generate_date=(SELECT MAX(generate_date) FROM mcn_data_summary a WHERE a.sign_id=mcn_data_summary.sign_id) AND active_tid=? AND data_type=1) AS ds
  29. RIGHT JOIN mcn_sign s ON s.id=ds.sign_id WHERE `
  30. _countMCNListSQL = `SELECT COUNT(*) FROM (SELECT * FROM mcn_data_summary WHERE generate_date=(SELECT MAX(generate_date) FROM mcn_data_summary a
  31. WHERE a.sign_id=mcn_data_summary.sign_id) AND active_tid=? AND data_type=1) AS ds RIGHT JOIN mcn_sign s ON s.id=ds.sign_id WHERE %s`
  32. _selMCNPayInfosByID = "SELECT id,mcn_mid,sign_id,due_date,pay_value,state FROM mcn_sign_pay WHERE id=?"
  33. _selMCNPayInfosBySignIDsSQL = "SELECT id,mcn_mid,sign_id,due_date,pay_value,state FROM mcn_sign_pay WHERE sign_id in(%s) ORDER BY due_date ASC"
  34. _selMCNRenewalUPsSQL = "SELECT sign_id,mcn_mid,up_mid,begin_date,end_date,contract_link,up_auth_link,reject_reason,reject_time,state,state_change_time,up_type,site_link,confirm_time,permission,publication_price FROM mcn_up WHERE sign_id=? AND mcn_mid=? AND state IN(10,11,15)"
  35. _selMCNInfoSQL = `SELECT s.id,s.mcn_mid,s.company_name,s.company_license_id,s.company_license_link,s.contract_link,s.contact_name,s.contact_title,s.contact_idcard,s.contact_phone,
  36. s.begin_date,s.end_date,ifnull(s.state,0),ifnull(ds.up_count,0),ifnull(ds.fans_count_accumulate,0),ifnull(ds.archive_count_accumulate,0),ifnull(ds.play_count_accumulate,0),
  37. ifnull(ds.fans_count_cheat_accumulate,0),ifnull(ds.fans_count_real_accumulate,0),ifnull(ds.fans_count_online_accumulate,0)
  38. FROM mcn_sign s LEFT JOIN mcn_data_summary ds ON s.id=ds.sign_id AND ds.active_tid=? AND ds.data_type=1 WHERE s.id = ? ORDER BY ds.generate_date DESC LIMIT 1`
  39. _selMCNUPListSQL = `SELECT u.id,u.sign_id,u.mcn_mid,u.up_mid,u.publication_price,u.permission,ifnull(du.active_tid,0),ifnull(du.fans_count,0),ifnull(du.fans_count_active,0),u.begin_date,u.end_date,u.state,
  40. ifnull(du.fans_increase_accumulate,0),ifnull(du.archive_count,0),ifnull(du.play_count,0),u.contract_link,u.up_auth_link,u.up_type, ifnull(u.site_link, "")
  41. FROM (SELECT * FROM mcn_data_up WHERE generate_date=(SELECT MAX(generate_date) FROM mcn_data_up a WHERE a.up_mid=mcn_data_up.up_mid)) du
  42. RIGHT JOIN mcn_up u ON u.sign_id=du.sign_id AND u.up_mid=du.up_mid AND du.data_type=? WHERE u.state IN(10,11,12,14,15) AND `
  43. _countMCNUPListSQL = `SELECT COUNT(*) FROM (SELECT * FROM mcn_data_up WHERE generate_date=(SELECT MAX(generate_date) FROM mcn_data_up a WHERE a.up_mid=mcn_data_up.up_mid)) du
  44. RIGHT JOIN mcn_up u ON u.sign_id=du.sign_id AND u.up_mid=du.up_mid AND du.data_type=? WHERE u.state IN(10,11,12,14,15) AND %s`
  45. _selMCNByMCNMIDSQL = `SELECT id,mcn_mid,company_name,company_license_id,company_license_link,contract_link,contact_name,
  46. contact_title,contact_idcard,contact_phone,begin_date,end_date,reject_reason,reject_time,state,ctime,mtime FROM mcn_sign WHERE state IN(10,11,15) AND mcn_mid = ? ORDER BY begin_date DESC LIMIT 1`
  47. _selMCNCheatListSQL = `SELECT fans_count_accumulate,fans_count_cheat_accumulate,mcn_mid,sign_id,up_mid,fans_count_cheat_increase_day,fans_count_cheat_cleaned_accumulate FROM mcn_data_up_cheat WHERE `
  48. _countMCNCheatListSQL = "SELECT COUNT(*) FROM mcn_data_up_cheat WHERE %s"
  49. _selMCNCheatUPListSQL = `SELECT sign_id,generate_date,fans_count_accumulate,fans_count_cheat_accumulate,mcn_mid,fans_count_cheat_increase_day,fans_count_cheat_cleaned_accumulate FROM mcn_data_up_cheat WHERE up_mid=? ORDER BY generate_date DESC LIMIT ?,?`
  50. _countMCNCheatUPListSQL = "SELECT COUNT(*) FROM mcn_data_up_cheat WHERE up_mid=?"
  51. _selMCNImportUPInfoSQL = "SELECT id,mcn_mid,sign_id,up_mid,standard_fans_date,standard_archive_count,standard_fans_count,is_reward FROM mcn_data_import_up WHERE sign_id=? AND up_mid=? AND standard_fans_type=1"
  52. _selMCNDataSummaryListSQL = `SELECT id,sign_id,data_type,active_tid,generate_date,up_count,fans_count_online_accumulate,fans_count_real_accumulate,fans_count_cheat_accumulate,fans_count_increase_day,archive_count_accumulate,archive_count_day,
  53. play_count_accumulate,play_count_increase_day,fans_count_accumulate FROM mcn_data_summary WHERE `
  54. _countMCNDataSummaryListSQL = "SELECT COUNT(*) FROM mcn_data_summary WHERE %s"
  55. )
  56. // TxAddMCNRenewal .
  57. func (d *Dao) TxAddMCNRenewal(tx *xsql.Tx, arg *model.MCNSign) (lastID int64, err error) {
  58. var res sql.Result
  59. if res, err = tx.Exec(_inMCNRenewalSQL, arg.MCNMID, arg.CompanyName, arg.CompanyLicenseID, arg.CompanyLicenseLink, arg.ContractLink, arg.ContactName, arg.ContactTitle, arg.ContactIdcard, arg.ContactPhone, arg.BeginDate, arg.EndDate, arg.Permission); err != nil {
  60. return lastID, err
  61. }
  62. if lastID, err = res.LastInsertId(); err != nil {
  63. return lastID, errors.Errorf("TxAddMCNRenewal res.LastInsertId(%+v) error(%+v)", arg, err)
  64. }
  65. return lastID, nil
  66. }
  67. // TxAddMCNPays .
  68. func (d *Dao) TxAddMCNPays(tx *xsql.Tx, lastID, mcnMID int64, arg []*model.SignPayReq) (err error) {
  69. l := len(arg)
  70. valueStrings := make([]string, 0, l)
  71. valueArgs := make([]interface{}, 0, l*5)
  72. for _, v := range arg {
  73. valueStrings = append(valueStrings, "(?,?,?,?)")
  74. valueArgs = append(valueArgs, strconv.FormatInt(mcnMID, 10))
  75. valueArgs = append(valueArgs, strconv.FormatInt(lastID, 10))
  76. valueArgs = append(valueArgs, v.DueDate)
  77. valueArgs = append(valueArgs, strconv.FormatInt(v.PayValue, 10))
  78. }
  79. stmt := fmt.Sprintf(_inMCNPaySQL, strings.Join(valueStrings, ","))
  80. _, err = tx.Exec(stmt, valueArgs...)
  81. return
  82. }
  83. // TxAddMCNUPs .
  84. func (d *Dao) TxAddMCNUPs(tx *xsql.Tx, signID, mcnMID int64, arg []*model.MCNUP) (err error) {
  85. l := len(arg)
  86. valueStrings := make([]string, 0, l)
  87. valueArgs := make([]interface{}, 0, l*15)
  88. for _, v := range arg {
  89. valueStrings = append(valueStrings, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  90. valueArgs = append(valueArgs, strconv.FormatInt(signID, 10))
  91. valueArgs = append(valueArgs, strconv.FormatInt(mcnMID, 10))
  92. valueArgs = append(valueArgs, strconv.FormatInt(v.UPMID, 10))
  93. valueArgs = append(valueArgs, v.BeginDate.Time().Format(model.TimeFormatDay))
  94. valueArgs = append(valueArgs, v.EndDate.Time().Format(model.TimeFormatDay))
  95. valueArgs = append(valueArgs, v.ContractLink)
  96. valueArgs = append(valueArgs, v.UPAuthLink)
  97. valueArgs = append(valueArgs, strconv.FormatInt(int64(v.State), 10))
  98. valueArgs = append(valueArgs, v.StateChangeTime.Time().Format(model.TimeFormatSec))
  99. valueArgs = append(valueArgs, strconv.FormatInt(int64(v.UpType), 10))
  100. valueArgs = append(valueArgs, v.SiteLink)
  101. valueArgs = append(valueArgs, v.ConfirmTime.Time().Format(model.TimeFormatSec))
  102. valueArgs = append(valueArgs, strconv.FormatInt(int64(v.Permission), 10))
  103. valueArgs = append(valueArgs, strconv.FormatInt(int64(v.PublicationPrice), 10))
  104. }
  105. stmt := fmt.Sprintf(_inMCNUPsSQL, strings.Join(valueStrings, ","))
  106. _, err = tx.Exec(stmt, valueArgs...)
  107. return
  108. }
  109. // // TxUpMCNSignRenewaID .
  110. // func (d *Dao) TxUpMCNSignRenewaID(tx *xsql.Tx, signID, renewalID int64) (rows int64, err error) {
  111. // var res sql.Result
  112. // if res, err = tx.Exec(_upMCNSignRenewaIDSQL, renewalID, signID); err != nil {
  113. // return rows, err
  114. // }
  115. // return res.RowsAffected()
  116. // }
  117. // UpMCNState .
  118. func (d *Dao) UpMCNState(c context.Context, arg *model.MCNStateEditReq) (rows int64, err error) {
  119. var res sql.Result
  120. if res, err = d.db.Exec(c, _upMCNStateSQL, arg.State, arg.ID, arg.MCNMID); err != nil {
  121. return rows, err
  122. }
  123. return res.RowsAffected()
  124. }
  125. // UpMCNPay update mcn_sign_pay.
  126. func (d *Dao) UpMCNPay(c context.Context, arg *model.MCNPayEditReq) (rows int64, err error) {
  127. var res sql.Result
  128. if res, err = d.db.Exec(c, _upMCNPaySQL, arg.DueDate, arg.PayValue, arg.ID, arg.MCNMID, arg.SignID); err != nil {
  129. return rows, err
  130. }
  131. return res.RowsAffected()
  132. }
  133. // UpMCNPayState update mcn_sign_pay state.
  134. func (d *Dao) UpMCNPayState(c context.Context, arg *model.MCNPayStateEditReq) (rows int64, err error) {
  135. var res sql.Result
  136. if res, err = d.db.Exec(c, _upMCNPayStateSQL, arg.State, arg.ID, arg.MCNMID, arg.SignID); err != nil {
  137. return rows, err
  138. }
  139. return res.RowsAffected()
  140. }
  141. // UpMCNImportUPRewardSign .
  142. func (d *Dao) UpMCNImportUPRewardSign(c context.Context, arg *model.MCNImportUPRewardSignReq) (rows int64, err error) {
  143. var res sql.Result
  144. if res, err = d.db.Exec(c, _upMCNImportUPSQL, arg.SignID, arg.UPMID); err != nil {
  145. return rows, err
  146. }
  147. return res.RowsAffected()
  148. }
  149. // UpMCNPermission .
  150. func (d *Dao) UpMCNPermission(c context.Context, signID int64, permission uint32) (rows int64, err error) {
  151. var res sql.Result
  152. if res, err = d.db.Exec(c, _upMCNPermissionSQL, permission, signID); err != nil {
  153. return rows, err
  154. }
  155. return res.RowsAffected()
  156. }
  157. // MCNList .
  158. func (d *Dao) MCNList(c context.Context, arg *model.MCNListReq) (res []*model.MCNListOne, ids, mids []int64, err error) {
  159. sql, values := d.buildMCNListSQL("list", arg)
  160. rows, err := d.db.Query(c, sql, values...)
  161. if err != nil {
  162. return
  163. }
  164. defer rows.Close()
  165. for rows.Next() {
  166. m := &model.MCNListOne{}
  167. err = rows.Scan(&m.ID, &m.MCNMID, &m.State, &m.BeginDate, &m.EndDate, &m.Permission, &m.UPCount, &m.FansCountAccumulate, &m.FansCountOnlineAccumulate, &m.FansCountRealAccumulate, &m.FansCountCheatAccumulate, &m.GenerateDate)
  168. if err != nil {
  169. return
  170. }
  171. res = append(res, m)
  172. ids = append(ids, m.ID)
  173. mids = append(mids, m.MCNMID)
  174. }
  175. return
  176. }
  177. // buildMCNListSQL build a MCNList sql string.
  178. func (d *Dao) buildMCNListSQL(SQLType string, arg *model.MCNListReq) (sql string, values []interface{}) {
  179. values = make([]interface{}, 0, 11)
  180. var (
  181. cond []string
  182. condStr string
  183. curTime = time.Now()
  184. date = time.Date(curTime.Year(), curTime.Month(), curTime.Day(), 0, 0, 0, 0, time.Local)
  185. orderSign = false
  186. )
  187. // defualt where
  188. cond = append(cond, "s.state IN(10,11,12,13,14,15)")
  189. values = append(values, model.AllActiveTid)
  190. if arg.ExpirePay {
  191. cond = append(cond, "s.pay_expire_state = ?")
  192. values = append(values, model.MCNStatePayed)
  193. }
  194. if arg.ExpireSign {
  195. cond = append(cond, "s.end_date <= ? AND s.state = 10")
  196. values = append(values, date.AddDate(0, 0, 30))
  197. }
  198. if arg.FansNumMin != 0 {
  199. cond = append(cond, "ds.fans_count_accumulate >= ?")
  200. values = append(values, arg.FansNumMin)
  201. }
  202. if arg.FansNumMax != 0 && arg.FansNumMax >= arg.FansNumMin {
  203. cond = append(cond, "ds.fans_count_accumulate <= ?")
  204. values = append(values, arg.FansNumMax)
  205. }
  206. if arg.MCNMID != 0 {
  207. cond = append(cond, "s.mcn_mid=?")
  208. values = append(values, arg.MCNMID)
  209. }
  210. if arg.State != -1 {
  211. cond = append(cond, "s.state=?")
  212. values = append(values, arg.State)
  213. }
  214. var permission = arg.GetAttrPermitVal()
  215. if permission != 0 {
  216. cond = append(cond, "((permission & ?) = ?)")
  217. values = append(values, permission, permission)
  218. }
  219. if checkSort(arg.SortUP, orderSign) {
  220. arg.Order = "ds.up_count"
  221. arg.Sort = arg.SortUP
  222. orderSign = true
  223. }
  224. if checkSort(arg.SortAllFans, orderSign) {
  225. arg.Order = "ds.fans_count_accumulate"
  226. arg.Sort = arg.SortAllFans
  227. orderSign = true
  228. }
  229. if checkSort(arg.SortRiseFans, orderSign) {
  230. arg.Order = "ds.fans_count_online_accumulate"
  231. arg.Sort = arg.SortRiseFans
  232. orderSign = true
  233. }
  234. if checkSort(arg.SortTrueRiseFans, orderSign) {
  235. arg.Order = "ds.fans_count_real_accumulate"
  236. arg.Sort = arg.SortTrueRiseFans
  237. orderSign = true
  238. }
  239. if checkSort(arg.SortCheatFans, orderSign) {
  240. arg.Order = "ds.fans_count_cheat_accumulate"
  241. arg.Sort = arg.SortCheatFans
  242. }
  243. condStr = d.joinStringSQL(cond)
  244. switch SQLType {
  245. case "list":
  246. if arg.Export == model.ResponeModelCSV {
  247. sql = fmt.Sprintf(_selMCNListSQL+_orderByConditionNotLimitSQL, condStr, arg.Order, arg.Sort)
  248. return
  249. }
  250. sql = fmt.Sprintf(_selMCNListSQL+_orderByConditionSQL, condStr, arg.Order, arg.Sort)
  251. limit, offset := arg.PageArg.CheckPageValidation()
  252. values = append(values, offset, limit)
  253. case "count":
  254. sql = fmt.Sprintf(_countMCNListSQL, condStr)
  255. }
  256. return
  257. }
  258. func checkSort(arg string, orderSign bool) bool {
  259. return arg != "" && (arg == "asc" || arg == "desc") && !orderSign
  260. }
  261. // MCNListTotal .
  262. func (d *Dao) MCNListTotal(c context.Context, arg *model.MCNListReq) (count int64, err error) {
  263. sql, values := d.buildMCNListSQL("count", arg)
  264. row := d.db.QueryRow(c, sql, values...)
  265. if err = row.Scan(&count); err != nil {
  266. if err == xsql.ErrNoRows {
  267. err = nil
  268. return
  269. }
  270. }
  271. return
  272. }
  273. // MCNPayInfo .
  274. func (d *Dao) MCNPayInfo(c context.Context, arg *model.MCNPayStateEditReq) (m *model.SignPayInfoReply, err error) {
  275. row := d.db.QueryRow(c, _selMCNPayInfosByID, arg.ID)
  276. m = new(model.SignPayInfoReply)
  277. // id,mcn_mid,sign_id,due_date,pay_value,state
  278. if err = row.Scan(&m.SignPayID, &m.McnMid, &m.SignID, &m.DueDate, &m.PayValue, &m.State); err != nil {
  279. if err == xsql.ErrNoRows {
  280. err = nil
  281. m = nil
  282. return
  283. }
  284. }
  285. return
  286. }
  287. // MCNPayInfos .
  288. func (d *Dao) MCNPayInfos(c context.Context, ids []int64) (res map[int64][]*model.SignPayInfoReply, err error) {
  289. rows, err := d.db.Query(c, fmt.Sprintf(_selMCNPayInfosBySignIDsSQL, xstr.JoinInts(ids)))
  290. if err != nil {
  291. return
  292. }
  293. defer rows.Close()
  294. res = make(map[int64][]*model.SignPayInfoReply, len(ids))
  295. for rows.Next() {
  296. m := &model.SignPayInfoReply{}
  297. err = rows.Scan(&m.SignPayID, &m.McnMid, &m.SignID, &m.DueDate, &m.PayValue, &m.State)
  298. if err != nil {
  299. return
  300. }
  301. res[m.SignID] = append(res[m.SignID], m)
  302. }
  303. return
  304. }
  305. // TxMCNRenewalUPs .
  306. func (d *Dao) TxMCNRenewalUPs(tx *xsql.Tx, signID, mcnID int64) (ups []*model.MCNUP, err error) {
  307. var rows *xsql.Rows
  308. if rows, err = tx.Query(_selMCNRenewalUPsSQL, signID, mcnID); err != nil {
  309. return
  310. }
  311. defer rows.Close()
  312. for rows.Next() {
  313. up := new(model.MCNUP)
  314. if err = rows.Scan(&up.SignID, &up.MCNMID, &up.UPMID, &up.BeginDate, &up.EndDate, &up.ContractLink,
  315. &up.UPAuthLink, &up.RejectReason, &up.RejectTime, &up.State, &up.StateChangeTime, &up.UpType,
  316. &up.SiteLink, &up.ConfirmTime, &up.Permission, &up.PublicationPrice); err != nil {
  317. return
  318. }
  319. ups = append(ups, up)
  320. }
  321. err = rows.Err()
  322. return
  323. }
  324. // MCNInfo .
  325. func (d *Dao) MCNInfo(c context.Context, arg *model.MCNInfoReq) (m *model.MCNInfoReply, err error) {
  326. row := d.db.QueryRow(c, _selMCNInfoSQL, model.AllActiveTid, arg.ID)
  327. m = new(model.MCNInfoReply)
  328. if err = row.Scan(&m.ID, &m.MCNMID, &m.CompanyName, &m.CompanyLicenseID, &m.CompanyLicenseLink, &m.ContractLink, &m.ContactName,
  329. &m.ContactTitle, &m.ContactIdcard, &m.ContactPhone, &m.BeginDate, &m.EndDate, &m.State, &m.UPCount, &m.FansCountAccumulate,
  330. &m.ArchiveCountAccumulate, &m.PlayCountAccumulate, &m.FansCountOnline, &m.FansCountRealAccumulate, &m.FansCountOnlineAccumulate); err != nil {
  331. if err == xsql.ErrNoRows {
  332. err = nil
  333. m = nil
  334. return
  335. }
  336. }
  337. return
  338. }
  339. // MCNUPList .
  340. func (d *Dao) MCNUPList(c context.Context, arg *model.MCNUPListReq) (res []*model.MCNUPInfoReply, err error) {
  341. sql, values := d.buildMCNUPListSQL("list", arg)
  342. rows, err := d.db.Query(c, sql, values...)
  343. if err != nil {
  344. return
  345. }
  346. defer rows.Close()
  347. for rows.Next() {
  348. m := &model.MCNUPInfoReply{}
  349. err = rows.Scan(&m.SignUpID, &m.SignID, &m.McnMid, &m.UpMid, &m.PubPrice, &m.Permission, &m.ActiveTid, &m.FansCount, &m.FansCountActive,
  350. &m.BeginDate, &m.EndDate, &m.State, &m.FansIncreaseAccumulate, &m.ArchiveCount, &m.PlayCount, &m.ContractLink, &m.UpAuthLink, &m.UPType, &m.SiteLink)
  351. if err != nil {
  352. return
  353. }
  354. res = append(res, m)
  355. }
  356. return
  357. }
  358. // MCNUPListTotal .
  359. func (d *Dao) MCNUPListTotal(c context.Context, arg *model.MCNUPListReq) (count int64, err error) {
  360. sql, values := d.buildMCNUPListSQL("count", arg)
  361. row := d.db.QueryRow(c, sql, values...)
  362. if err = row.Scan(&count); err != nil {
  363. if err == xsql.ErrNoRows {
  364. err = nil
  365. return
  366. }
  367. }
  368. return
  369. }
  370. // buildMCNUPListSQL build a MCNUPList sql string.
  371. func (d *Dao) buildMCNUPListSQL(SQLType string, arg *model.MCNUPListReq) (sql string, values []interface{}) {
  372. values = make([]interface{}, 0, 8)
  373. var (
  374. cond []string
  375. condStr string
  376. orderSign = false
  377. )
  378. values = append(values, arg.DataType)
  379. cond = append(cond, "u.sign_id=?")
  380. values = append(values, arg.SignID)
  381. if arg.FansNumMin != 0 {
  382. cond = append(cond, "du.fans_count>=?")
  383. values = append(values, arg.FansNumMin)
  384. }
  385. if arg.FansNumMax != 0 && arg.FansNumMax >= arg.FansNumMin {
  386. cond = append(cond, "du.fans_count<=?")
  387. values = append(values, arg.FansNumMax)
  388. }
  389. if arg.ActiveTID != 0 {
  390. cond = append(cond, "du.active_tid=?")
  391. values = append(values, arg.ActiveTID)
  392. }
  393. if arg.State != -1 {
  394. cond = append(cond, "u.state=?")
  395. values = append(values, arg.State)
  396. }
  397. if arg.UPMID != 0 {
  398. cond = append(cond, "u.up_mid=?")
  399. values = append(values, arg.UPMID)
  400. }
  401. if arg.UpType != -1 {
  402. cond = append(cond, "u.up_type=?")
  403. values = append(values, arg.UpType)
  404. }
  405. var permission = arg.GetAttrPermitVal()
  406. if permission != 0 {
  407. cond = append(cond, "((permission & ?) = ?)")
  408. values = append(values, permission, permission)
  409. }
  410. if checkSort(arg.SortFansCount, orderSign) {
  411. arg.Order = "du.fans_count"
  412. arg.Sort = arg.SortFansCount
  413. orderSign = true
  414. }
  415. if checkSort(arg.SortFansCountActive, orderSign) {
  416. arg.Order = "du.fans_count_active"
  417. arg.Sort = arg.SortFansCountActive
  418. orderSign = true
  419. }
  420. if checkSort(arg.SortFansIncreaseAccumulate, orderSign) {
  421. arg.Order = "du.fans_increase_accumulate"
  422. arg.Sort = arg.SortFansIncreaseAccumulate
  423. orderSign = true
  424. }
  425. if checkSort(arg.SortArchiveCount, orderSign) {
  426. arg.Order = "du.archive_count"
  427. arg.Sort = arg.SortArchiveCount
  428. orderSign = true
  429. }
  430. if checkSort(arg.SortPlayCount, orderSign) {
  431. arg.Order = "du.play_count"
  432. arg.Sort = arg.SortPlayCount
  433. orderSign = true
  434. }
  435. if checkSort(arg.SortPubPrice, orderSign) {
  436. arg.Order = "u.publication_price"
  437. arg.Sort = arg.SortPubPrice
  438. }
  439. condStr = d.joinStringSQL(cond)
  440. switch SQLType {
  441. case "list":
  442. if arg.Export == model.ResponeModelCSV {
  443. sql = fmt.Sprintf(_selMCNUPListSQL+_orderByConditionNotLimitSQL, condStr, arg.Order, arg.Sort)
  444. return
  445. }
  446. sql = fmt.Sprintf(_selMCNUPListSQL+_orderByConditionSQL, condStr, arg.Order, arg.Sort)
  447. limit, offset := arg.PageArg.CheckPageValidation()
  448. values = append(values, offset, limit)
  449. case "count":
  450. sql = fmt.Sprintf(_countMCNUPListSQL, condStr)
  451. fmt.Println(sql)
  452. }
  453. return
  454. }
  455. // UpMCNUPState .
  456. func (d *Dao) UpMCNUPState(c context.Context, arg *model.MCNUPStateEditReq) (rows int64, err error) {
  457. var res sql.Result
  458. if res, err = d.db.Exec(c, _upMCNUPStateSQL, arg.State, time.Now(), arg.ID, arg.SignID, arg.MCNMID, arg.UPMID); err != nil {
  459. return rows, err
  460. }
  461. return res.RowsAffected()
  462. }
  463. // McnSignByMCNMID .
  464. func (d *Dao) McnSignByMCNMID(c context.Context, MCNID int64) (m *model.MCNSignInfoReply, err error) {
  465. row := d.db.QueryRow(c, _selMCNByMCNMIDSQL, MCNID)
  466. m = new(model.MCNSignInfoReply)
  467. if err = row.Scan(&m.SignID, &m.McnMid, &m.CompanyName, &m.CompanyLicenseID, &m.CompanyLicenseLink, &m.ContractLink, &m.ContactName,
  468. &m.ContactTitle, &m.ContactIdcard, &m.ContactPhone, &m.BeginDate, &m.EndDate, &m.RejectReason, &m.RejectTime, &m.State, &m.Ctime, &m.Mtime); err != nil {
  469. if err == xsql.ErrNoRows {
  470. err = nil
  471. m = nil
  472. return
  473. }
  474. }
  475. return
  476. }
  477. // MCNCheatList .
  478. func (d *Dao) MCNCheatList(c context.Context, arg *model.MCNCheatListReq) (res []*model.MCNCheatReply, mids []int64, err error) {
  479. sql, values := d.buildMCNCheatListSQL("list", arg)
  480. rows, err := d.db.Query(c, sql, values...)
  481. if err != nil {
  482. return
  483. }
  484. defer rows.Close()
  485. for rows.Next() {
  486. m := &model.MCNCheatReply{}
  487. err = rows.Scan(&m.FansCountReal, &m.FansCountCheatAccumulate, &m.MCNMID, &m.SignID, &m.UpMID, &m.FansCountCheatIncreaseDay, &m.FansCountCheatCleanedAccumulate)
  488. if err != nil {
  489. return
  490. }
  491. res = append(res, m)
  492. mids = append(mids, m.UpMID, m.MCNMID)
  493. }
  494. mids = SliceUnique(mids)
  495. return
  496. }
  497. // MCNCheatListTotal .
  498. func (d *Dao) MCNCheatListTotal(c context.Context, arg *model.MCNCheatListReq) (count int64, err error) {
  499. sql, values := d.buildMCNCheatListSQL("count", arg)
  500. row := d.db.QueryRow(c, sql, values...)
  501. if err = row.Scan(&count); err != nil {
  502. if err == xsql.ErrNoRows {
  503. err = nil
  504. return
  505. }
  506. }
  507. return
  508. }
  509. //SliceUnique unique
  510. func SliceUnique(s []int64) []int64 {
  511. result := make([]int64, 0, len(s))
  512. temp := map[int64]struct{}{}
  513. for _, item := range s {
  514. if _, ok := temp[item]; !ok {
  515. temp[item] = struct{}{}
  516. result = append(result, item)
  517. }
  518. }
  519. return result
  520. }
  521. // buildMCNCheatListSQL build a MCNCheatList sql string.
  522. func (d *Dao) buildMCNCheatListSQL(SQLType string, arg *model.MCNCheatListReq) (sql string, values []interface{}) {
  523. values = make([]interface{}, 0, 3)
  524. var (
  525. cond []string
  526. condStr string
  527. )
  528. cond = append(cond, "generate_date=(SELECT generate_date FROM mcn_data_up_cheat ORDER BY generate_date DESC LIMIT 1)")
  529. if arg.MCNMID > 0 {
  530. cond = append(cond, "mcn_mid=?")
  531. values = append(values, arg.MCNMID)
  532. }
  533. if arg.UPMID > 0 {
  534. cond = append(cond, "up_mid=?")
  535. values = append(values, arg.UPMID)
  536. }
  537. condStr = d.joinStringSQL(cond)
  538. switch SQLType {
  539. case "list":
  540. sql = fmt.Sprintf(_selMCNCheatListSQL+_orderByConditionSQL, condStr, "generate_date DESC,fans_count_cheat_accumulate", "DESC")
  541. limit, offset := arg.PageArg.CheckPageValidation()
  542. values = append(values, offset, limit)
  543. case "count":
  544. sql = fmt.Sprintf(_countMCNCheatListSQL, condStr)
  545. }
  546. return
  547. }
  548. // MCNCheatUPList .
  549. func (d *Dao) MCNCheatUPList(c context.Context, arg *model.MCNCheatUPListReq) (res []*model.MCNCheatUPReply, err error) {
  550. limit, offset := arg.PageArg.CheckPageValidation()
  551. rows, err := d.db.Query(c, _selMCNCheatUPListSQL, arg.UPMID, offset, limit)
  552. if err != nil {
  553. return
  554. }
  555. defer rows.Close()
  556. for rows.Next() {
  557. m := &model.MCNCheatUPReply{}
  558. err = rows.Scan(&m.SignID, &m.GenerateDate, &m.FansCountReal, &m.FansCountCheatAccumulate, &m.MCNMID, &m.FansCountCheatIncreaseDay, &m.FansCountCheatCleanedAccumulate)
  559. if err != nil {
  560. return
  561. }
  562. res = append(res, m)
  563. }
  564. return
  565. }
  566. // MCNCheatUPListTotal .
  567. func (d *Dao) MCNCheatUPListTotal(c context.Context, arg *model.MCNCheatUPListReq) (count int64, err error) {
  568. row := d.db.QueryRow(c, _countMCNCheatUPListSQL, arg.UPMID)
  569. if err = row.Scan(&count); err != nil {
  570. if err == xsql.ErrNoRows {
  571. err = nil
  572. return
  573. }
  574. }
  575. return
  576. }
  577. // MCNImportUPInfo .
  578. func (d *Dao) MCNImportUPInfo(c context.Context, arg *model.MCNImportUPInfoReq) (m *model.MCNImportUPInfoReply, err error) {
  579. row := d.db.QueryRow(c, _selMCNImportUPInfoSQL, arg.SignID, arg.UPMID)
  580. m = new(model.MCNImportUPInfoReply)
  581. if err = row.Scan(&m.ID, &m.MCNMID, &m.SignID, &m.UpMID, &m.StandardFansDate, &m.StandardArchiveCount, &m.StandardFansCount, &m.IsReward); err != nil {
  582. if err == xsql.ErrNoRows {
  583. err = nil
  584. return
  585. }
  586. }
  587. return
  588. }
  589. // MCNIncreaseList .
  590. func (d *Dao) MCNIncreaseList(c context.Context, arg *model.MCNIncreaseListReq) (res []*model.MCNIncreaseReply, err error) {
  591. sql, values := d.buildMCNIncreaseListSQL("list", arg)
  592. rows, err := d.db.Query(c, sql, values...)
  593. if err != nil {
  594. return
  595. }
  596. defer rows.Close()
  597. for rows.Next() {
  598. m := &model.MCNIncreaseReply{}
  599. err = rows.Scan(&m.ID, &m.SignID, &m.DataType, &m.ActiveTID, &m.GenerateDate, &m.UPCount, &m.FansCountOnlineAccumulate, &m.FansCountRealAccumulate, &m.FansCountCheatAccumulate, &m.FansCountIncreaseDay,
  600. &m.ArchiveCountAccumulate, &m.ArchiveCountDay, &m.PlayCountAccumulate, &m.PlayCountIncreaseDay, &m.FansCountAccumulate)
  601. if err != nil {
  602. return
  603. }
  604. res = append(res, m)
  605. }
  606. return
  607. }
  608. // MCNIncreaseListTotal .
  609. func (d *Dao) MCNIncreaseListTotal(c context.Context, arg *model.MCNIncreaseListReq) (count int64, err error) {
  610. sql, values := d.buildMCNIncreaseListSQL("count", arg)
  611. row := d.db.QueryRow(c, sql, values...)
  612. if err = row.Scan(&count); err != nil {
  613. if err == xsql.ErrNoRows {
  614. err = nil
  615. return
  616. }
  617. }
  618. return
  619. }
  620. // buildMCNIncreaseListSQL build a MCNIncreaseList sql string.
  621. func (d *Dao) buildMCNIncreaseListSQL(SQLType string, arg *model.MCNIncreaseListReq) (sql string, values []interface{}) {
  622. values = make([]interface{}, 0, 3)
  623. var (
  624. cond []string
  625. condStr string
  626. )
  627. cond = append(cond, "sign_id=?")
  628. values = append(values, arg.SignID)
  629. if arg.DataType > 0 {
  630. cond = append(cond, "data_type=?")
  631. values = append(values, arg.DataType)
  632. }
  633. if arg.ActiveTID == 0 {
  634. arg.ActiveTID = model.AllActiveTid
  635. }
  636. if arg.ActiveTID > 0 {
  637. cond = append(cond, "active_tid=?")
  638. values = append(values, arg.ActiveTID)
  639. }
  640. condStr = d.joinStringSQL(cond)
  641. switch SQLType {
  642. case "list":
  643. sql = fmt.Sprintf(_selMCNDataSummaryListSQL+_orderByConditionSQL, condStr, "generate_date", "DESC")
  644. limit, offset := arg.PageArg.CheckPageValidation()
  645. values = append(values, offset, limit)
  646. case "count":
  647. sql = fmt.Sprintf(_countMCNDataSummaryListSQL, condStr)
  648. }
  649. return
  650. }