medal.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "go-common/app/admin/main/usersuit/model"
  7. xsql "go-common/library/database/sql"
  8. "go-common/library/log"
  9. "github.com/pkg/errors"
  10. )
  11. const (
  12. _sharding = 10
  13. _selMedal = "SELECT id,name,description,cond,gid,level,level_rank,sort,is_online FROM medal_info"
  14. _selMedalByID = "SELECT id,name,description,image,image_small,cond,gid,level,level_rank,sort,is_online FROM medal_info WHERE id= ?"
  15. _insertMedal = "INSERT INTO medal_info (name,description,cond,gid,level,sort,level_rank,is_online,image,image_small) VALUES(?,?,?,?,?,?,?,?,?,?)"
  16. _updateMedal = "UPDATE medal_info SET name=?,description=?,cond=?,gid=?,level=?,sort=?,level_rank=?,is_online=?,image=?,image_small=? WHERE id=?"
  17. _sellMedalGroup = "SELECT id,name,pid,rank,is_online FROM medal_group ORDER BY id ASC, rank ASC"
  18. _sellMedalGroupInfo = "SELECT m1.id,m1.name,m1.pid,m1.rank,m1.is_online,ifnull(m2.name,'无') FROM medal_group m1 left join medal_group m2 on m1.pid=m2.id ORDER BY pid ASC, rank ASC"
  19. _selMedalGroupParent = "SELECT id,name,pid,rank,is_online FROM medal_group WHERE pid=0"
  20. _selMedalGroupByID = "SELECT id,name,pid,rank,is_online FROM medal_group WHERE id= ?"
  21. _insertMedalGroup = "INSERT INTO medal_group (name,pid,rank,is_online) VALUES (?,?,?,?)"
  22. _updateMedalGroupByID = "UPDATE medal_group SET name=?,pid=?,rank=?,is_online=? WHERE id =?"
  23. _selMedalOwnerByMID = "SELECT o.id,o.nid,o.is_activated,o.is_del,i.name FROM medal_owner_%s o LEFT JOIN medal_info i ON o.nid=i.id WHERE o.mid=?"
  24. _selMedalAddList = "SELECT id,name FROM medal_info WHERE id NOT IN (SELECT nid FROM medal_owner_%s WHERE mid=?)"
  25. _countOwnerBYNidMidSQL = "SELECT COUNT(*) FROM medal_owner_%s WHERE mid=? AND nid=?"
  26. _insertMedalOwner = "INSERT INTO medal_owner_%s (mid,nid) VALUES (?,?)"
  27. _updatMedalOwnerActivated = "UPDATE medal_owner_%s SET is_activated=1 WHERE mid=? AND nid=?"
  28. _updatMedalOwnerNoActivated = "UPDATE medal_owner_%s SET is_activated=0 WHERE mid=? AND nid!=?"
  29. _updatMedalOwnerDel = "UPDATE medal_owner_%s SET is_del=? WHERE mid=? AND nid=?"
  30. _insertMedalOperationLogSQL = "INSERT INTO medal_operation_log(oper_id,mid,medal_id,source_type,action) VALUES (?,?,?,?,?)"
  31. _medalOperationLogTotalSQL = "SELECT COUNT(*) FROM medal_operation_log WHERE mid=?"
  32. _medalOperationLogSQL = "SELECT oper_id,action,mid,medal_id,source_type,ctime,mtime FROM medal_operation_log WHERE mid=? ORDER BY mtime DESC LIMIT ?,?"
  33. )
  34. func (d *Dao) hit(id int64) string {
  35. return fmt.Sprintf("%d", id%_sharding)
  36. }
  37. // Medal medal info .
  38. func (d *Dao) Medal(c context.Context) (ms []*model.Medal, err error) {
  39. var rows *xsql.Rows
  40. if rows, err = d.db.Query(c, _selMedal); err != nil {
  41. log.Error("Medal, d.db.Query() error(%v)", err)
  42. return
  43. }
  44. defer rows.Close()
  45. for rows.Next() {
  46. m := &model.Medal{}
  47. if err = rows.Scan(&m.ID, &m.Name, &m.Description, &m.Condition, &m.GID, &m.Level, &m.LevelRank, &m.Sort, &m.IsOnline); err != nil {
  48. log.Error("Medal, rows.Scan(%+v) error(%v)", m, err)
  49. return
  50. }
  51. ms = append(ms, m)
  52. }
  53. err = rows.Err()
  54. return
  55. }
  56. // MedalByID medal info by id .
  57. func (d *Dao) MedalByID(c context.Context, id int64) (m *model.Medal, err error) {
  58. var row = d.db.QueryRow(c, _selMedalByID, id)
  59. m = &model.Medal{}
  60. if err = row.Scan(&m.ID, &m.Name, &m.Description, &m.Image, &m.ImageSmall, &m.Condition, &m.GID, &m.Level, &m.LevelRank, &m.Sort, &m.IsOnline); err != nil {
  61. log.Error("MedalByID, rows.Scan(%+v) error(%v)", m, err)
  62. }
  63. return
  64. }
  65. // AddMedal add medal .
  66. func (d *Dao) AddMedal(c context.Context, m *model.Medal) (affected int64, err error) {
  67. var res sql.Result
  68. if res, err = d.db.Exec(c, _insertMedal, m.Name, m.Description, m.Condition, m.GID, m.Level, m.Sort, m.LevelRank, m.IsOnline, m.Image, m.ImageSmall); err != nil {
  69. log.Error("AddMedal, rows.Exec(%+v) error(%v)", m, err)
  70. return
  71. }
  72. return res.RowsAffected()
  73. }
  74. // UpMedal update nameplate .
  75. func (d *Dao) UpMedal(c context.Context, id int64, m *model.Medal) (affected int64, err error) {
  76. var res sql.Result
  77. if res, err = d.db.Exec(c, _updateMedal, m.Name, m.Description, m.Condition, m.GID, m.Level, m.Sort, m.LevelRank, m.IsOnline, m.Image, m.ImageSmall, id); err != nil {
  78. log.Error("UpMedal, d.db.Exec(%+v,%d) error(%v)", m, id, err)
  79. return
  80. }
  81. return res.RowsAffected()
  82. }
  83. // MedalGroup return medal group all .
  84. func (d *Dao) MedalGroup(c context.Context) (res map[int64]*model.MedalGroup, err error) {
  85. res = make(map[int64]*model.MedalGroup)
  86. var rows *xsql.Rows
  87. if rows, err = d.db.Query(c, _sellMedalGroup); err != nil {
  88. log.Error("MedalGroup, d.db.Query() error(%v)", err)
  89. return
  90. }
  91. defer rows.Close()
  92. for rows.Next() {
  93. re := &model.MedalGroup{}
  94. if err = rows.Scan(&re.ID, &re.Name, &re.PID, &re.Rank, &re.IsOnline); err != nil {
  95. log.Error("MedalGroup, rows.Scan(%+v) error(%v)", re, err)
  96. return
  97. }
  98. res[re.ID] = re
  99. }
  100. err = rows.Err()
  101. return
  102. }
  103. // MedalGroupInfo return medal group all info include parent group name .
  104. func (d *Dao) MedalGroupInfo(c context.Context) (res []*model.MedalGroup, err error) {
  105. res = make([]*model.MedalGroup, 0)
  106. var rows *xsql.Rows
  107. if rows, err = d.db.Query(c, _sellMedalGroupInfo); err != nil {
  108. log.Error("MedalGroupInfo, d.db.Query() error(%v)", err)
  109. return
  110. }
  111. defer rows.Close()
  112. for rows.Next() {
  113. re := &model.MedalGroup{}
  114. if err = rows.Scan(&re.ID, &re.Name, &re.PID, &re.Rank, &re.IsOnline, &re.PName); err != nil {
  115. log.Error("MedalGroupInfo, rows.Scan(%+v) error(%v)", re, err)
  116. return
  117. }
  118. res = append(res, re)
  119. }
  120. err = rows.Err()
  121. return
  122. }
  123. // MedalGroupParent return medal group parent info .
  124. func (d *Dao) MedalGroupParent(c context.Context) (res []*model.MedalGroup, err error) {
  125. res = make([]*model.MedalGroup, 0)
  126. var rows *xsql.Rows
  127. if rows, err = d.db.Query(c, _selMedalGroupParent); err != nil {
  128. log.Error("MedalGroupInfo, d.db.Query() error(%v)", err)
  129. return
  130. }
  131. defer rows.Close()
  132. for rows.Next() {
  133. re := &model.MedalGroup{}
  134. if err = rows.Scan(&re.ID, &re.Name, &re.PID, &re.Rank, &re.IsOnline); err != nil {
  135. log.Error("MedalGroupParent, rows.Scan(%+v) error(%v)", re, err)
  136. return
  137. }
  138. res = append(res, re)
  139. }
  140. err = rows.Err()
  141. return
  142. }
  143. // MedalGroupByID medal group by gid .
  144. func (d *Dao) MedalGroupByID(c context.Context, id int64) (mg *model.MedalGroup, err error) {
  145. var row = d.db.QueryRow(c, _selMedalGroupByID, id)
  146. mg = &model.MedalGroup{}
  147. if err = row.Scan(&mg.ID, &mg.Name, &mg.PID, &mg.Rank, &mg.IsOnline); err != nil {
  148. log.Error("MedalGroupByID, rows.Scan(%+v) error(%v)", mg, err)
  149. return
  150. }
  151. return
  152. }
  153. // MedalGroupAdd add medal group .
  154. func (d *Dao) MedalGroupAdd(c context.Context, mg *model.MedalGroup) (affected int64, err error) {
  155. var res sql.Result
  156. if res, err = d.db.Exec(c, _insertMedalGroup, mg.Name, mg.PID, mg.Rank, mg.IsOnline); err != nil {
  157. log.Error("MedalGroupAdd, d.db.Exec(%+v) error(%v)", mg, err)
  158. return
  159. }
  160. return res.RowsAffected()
  161. }
  162. // MedalGroupUp update name group .
  163. func (d *Dao) MedalGroupUp(c context.Context, id int64, mg *model.MedalGroup) (affected int64, err error) {
  164. var res sql.Result
  165. if res, err = d.db.Exec(c, _updateMedalGroupByID, mg.Name, mg.PID, mg.Rank, mg.IsOnline, id); err != nil {
  166. log.Error("MedalGroupUp, d.db.Exec(%+v %d) error(%v)", mg, id, err)
  167. return
  168. }
  169. return res.RowsAffected()
  170. }
  171. // MedalOwner medal owner .
  172. func (d *Dao) MedalOwner(c context.Context, mid int64) (res []*model.MedalMemberMID, err error) {
  173. var (
  174. rows *xsql.Rows
  175. sqlStr string
  176. )
  177. res = make([]*model.MedalMemberMID, 0)
  178. sqlStr = fmt.Sprintf(_selMedalOwnerByMID, d.hit(mid))
  179. if rows, err = d.db.Query(c, sqlStr, mid); err != nil {
  180. log.Error("MedalOwner, d.db.Query(%s %d) error(%v)", sqlStr, mid, err)
  181. return
  182. }
  183. defer rows.Close()
  184. for rows.Next() {
  185. re := &model.MedalMemberMID{}
  186. if err = rows.Scan(&re.ID, &re.NID, &re.IsActivated, &re.IsDel, &re.MedalName); err != nil {
  187. log.Error("MedalOwner, rows.Scan(%+v) error(%v)", re, err)
  188. return
  189. }
  190. res = append(res, re)
  191. }
  192. err = rows.Err()
  193. return
  194. }
  195. // CountOwnerBYNidMid retun number of medal_owner by mid and nid.
  196. func (d *Dao) CountOwnerBYNidMid(c context.Context, mid, nid int64) (count int64, err error) {
  197. row := d.db.QueryRow(c, fmt.Sprintf(_countOwnerBYNidMidSQL, d.hit(mid)), mid, nid)
  198. if err = row.Scan(&count); err != nil {
  199. if err != sql.ErrNoRows {
  200. err = errors.Wrap(err, "CountOwnerBYNidMid")
  201. return
  202. }
  203. count = 0
  204. err = nil
  205. }
  206. return
  207. }
  208. // MedalOwnerAdd add medal owner .
  209. func (d *Dao) MedalOwnerAdd(c context.Context, mid, nid int64) (affected int64, err error) {
  210. var (
  211. res sql.Result
  212. sqlStr string
  213. )
  214. sqlStr = fmt.Sprintf(_insertMedalOwner, d.hit(mid))
  215. if res, err = d.db.Exec(c, sqlStr, mid, nid); err != nil {
  216. log.Error("MedalOwnerAdd, d.db.Exec(%s %d %d) error(%v)", sqlStr, mid, nid, err)
  217. return
  218. }
  219. return res.RowsAffected()
  220. }
  221. // MedalAddList .
  222. func (d *Dao) MedalAddList(c context.Context, mid int64) (ms []*model.MedalMemberAddList, err error) {
  223. var (
  224. rows *xsql.Rows
  225. sqlStr string
  226. )
  227. sqlStr = fmt.Sprintf(_selMedalAddList, d.hit(mid))
  228. if rows, err = d.db.Query(c, sqlStr, mid); err != nil {
  229. log.Error("MedalAddList, d.db.Query(%s %d) error(%v)", sqlStr, mid, err)
  230. return
  231. }
  232. defer rows.Close()
  233. for rows.Next() {
  234. m := &model.MedalMemberAddList{}
  235. if err = rows.Scan(&m.ID, &m.MedalName); err != nil {
  236. log.Error("MedalAddList, rows.Scan(%+v) error(%v)", m, err)
  237. return
  238. }
  239. ms = append(ms, m)
  240. }
  241. err = rows.Err()
  242. return
  243. }
  244. // MedalOwnerUpActivated update medal owner is_activated=1.
  245. func (d *Dao) MedalOwnerUpActivated(c context.Context, mid, nid int64) (affected int64, err error) {
  246. var (
  247. res sql.Result
  248. sqlStr string
  249. )
  250. sqlStr = fmt.Sprintf(_updatMedalOwnerActivated, d.hit(mid))
  251. if res, err = d.db.Exec(c, sqlStr, mid, nid); err != nil {
  252. log.Error("MedalOwnerUpActivated, d.db.Exec(%s %d %d) error(%v)", sqlStr, mid, nid, err)
  253. return
  254. }
  255. return res.RowsAffected()
  256. }
  257. // MedalOwnerUpNotActivated update medal owner is_activated=0.
  258. func (d *Dao) MedalOwnerUpNotActivated(c context.Context, mid, nid int64) (affected int64, err error) {
  259. var (
  260. res sql.Result
  261. sqlStr string
  262. )
  263. sqlStr = fmt.Sprintf(_updatMedalOwnerNoActivated, d.hit(mid))
  264. if res, err = d.db.Exec(c, sqlStr, mid, nid); err != nil {
  265. log.Error("MedalOwnerUpNotActivated, d.db.Exec(%s %d %d) error(%v)", sqlStr, mid, nid, err)
  266. return
  267. }
  268. return res.RowsAffected()
  269. }
  270. // MedalOwnerDel update medal owner is_del=1.
  271. func (d *Dao) MedalOwnerDel(c context.Context, mid, nid int64, isDel int8) (affected int64, err error) {
  272. var (
  273. res sql.Result
  274. sqlStr string
  275. )
  276. sqlStr = fmt.Sprintf(_updatMedalOwnerDel, d.hit(mid))
  277. if res, err = d.db.Exec(c, sqlStr, isDel, mid, nid); err != nil {
  278. log.Error("MedalOwnerDel, d.db.Exec(%s %d %d %d) error(%v)", sqlStr, isDel, mid, nid, err)
  279. return
  280. }
  281. return res.RowsAffected()
  282. }
  283. // AddMedalOperLog insert medal operation log.
  284. func (d *Dao) AddMedalOperLog(c context.Context, oid int64, mid int64, medalID int64, action string) (affected int64, err error) {
  285. var res sql.Result
  286. // oper_id,mid,medal_id,source_type,action
  287. if res, err = d.db.Exec(c, _insertMedalOperationLogSQL, oid, mid, medalID, model.MedalSourceTypeAdmin, action); err != nil {
  288. log.Error("MedalGroupAdd, d.db.Exec() error(%v)", err)
  289. return
  290. }
  291. return res.RowsAffected()
  292. }
  293. // MedalOperLog get medal operation log.
  294. func (d *Dao) MedalOperLog(c context.Context, mid int64, pn, ps int) (opers []*model.MedalOperLog, uids []int64, err error) {
  295. var (
  296. rows *xsql.Rows
  297. offset = (pn - 1) * ps
  298. )
  299. if rows, err = d.db.Query(c, _medalOperationLogSQL, mid, offset, ps); err != nil {
  300. err = errors.Wrapf(err, "MedalOperLog d.db.Query(%d,%d,%d)", mid, offset, ps)
  301. return
  302. }
  303. defer rows.Close()
  304. for rows.Next() {
  305. oper := new(model.MedalOperLog)
  306. if err = rows.Scan(&oper.OID, &oper.Action, &oper.MID, &oper.MedalID, &oper.SourceType, &oper.CTime, &oper.MTime); err != nil {
  307. err = errors.Wrap(err, "MedalOperLog row.Scan()")
  308. return
  309. }
  310. opers = append(opers, oper)
  311. uids = append(uids, oper.MID)
  312. }
  313. err = rows.Err()
  314. return
  315. }
  316. // MedalOperationLogTotal medal operation log total.
  317. func (d *Dao) MedalOperationLogTotal(c context.Context, mid int64) (count int64, err error) {
  318. row := d.db.QueryRow(c, _medalOperationLogTotalSQL, mid)
  319. if err = row.Scan(&count); err != nil {
  320. err = errors.Wrap(err, "d.dao.MedalOperationLogTotal")
  321. return
  322. }
  323. return
  324. }