resource.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "math/rand"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "go-common/app/service/live/resource/model"
  11. "go-common/library/log"
  12. "github.com/siddontang/go-mysql/mysql"
  13. )
  14. const (
  15. _addResourceSQL = "INSERT INTO resource (`platform`,`build`,`limit_type`,`start_time`,`end_time`,`type`,`title`,`image_info`) values(?,?,?,?,?,?,?,?)"
  16. )
  17. // AddResource -
  18. func (d *Dao) AddResource(c context.Context, insert *model.Resource) (row int64, err error) {
  19. types := strings.Split(insert.Type, ",")
  20. for _, t := range types {
  21. insert.Type = t
  22. row, err = d.addDBResource(c, insert)
  23. if err != nil {
  24. return row, err
  25. }
  26. }
  27. return
  28. }
  29. // EditResource -
  30. func (d *Dao) EditResource(c context.Context, id int64, update map[string]interface{}) (row int64, err error) {
  31. return d.editDBResource(c, id, update)
  32. }
  33. // GetResourceList -
  34. func (d *Dao) GetResourceList(c context.Context, typ string, page int64, pageSize int64) (resp []model.Resource, err error) {
  35. return d.getDBResourceList(c, typ, page, pageSize)
  36. }
  37. // GetResourceListEx -
  38. func (d *Dao) GetResourceListEx(c context.Context, typ []string, page int64, pageSize int64, devPlatform string, status string, startTime string, endTime string) (resp []model.Resource, count int64, err error) {
  39. return d.getDBResourceListEx(c, typ, page, pageSize, devPlatform, status, startTime, endTime)
  40. }
  41. // OfflineResource -
  42. func (d *Dao) OfflineResource(c context.Context, id int64) (row int64, err error) {
  43. return d.offlineDBResource(c, id)
  44. }
  45. // SelectById -
  46. func (d *Dao) SelectById(c context.Context, id int64) (resp *model.Resource, err error) {
  47. return d.selectDBById(c, id)
  48. }
  49. // GetInfo -
  50. func (d *Dao) GetInfo(ctx context.Context, typ string, platform string, build int64) (resp *model.Resource, err error) {
  51. inst := rand.Intn(d.c.CacheInstCnt)
  52. res, ok := d.sCache[inst].Get(cacheResourceKey(typ, platform, build))
  53. if !ok {
  54. resp, err = d.getDBInfo(ctx, typ, platform, build)
  55. if err != nil {
  56. return
  57. }
  58. var resNew []model.Resource
  59. resNew = append(resNew, *resp)
  60. d.sCache[inst].Put(cacheResourceKey(typ, platform, build), resNew)
  61. return
  62. }
  63. r := res.([]model.Resource)
  64. if len(r) > 0 {
  65. return &r[0], nil
  66. }
  67. return nil, nil
  68. }
  69. // GetBanner -
  70. func (d *Dao) GetBanner(ctx context.Context, platform string, build int64, t string) (resp []model.Resource, err error) {
  71. inst := rand.Intn(d.c.CacheInstCnt)
  72. res, ok := d.sCache[inst].Get(cacheResourceKey(t, platform, build))
  73. if !ok {
  74. resp, err = d.getDBBanner(ctx, platform, build, t)
  75. if err != nil {
  76. return
  77. }
  78. d.sCache[inst].Put(cacheResourceKey(t, platform, build), resp)
  79. return
  80. }
  81. resp = res.([]model.Resource)
  82. return
  83. }
  84. // SelectByTypeAndPlatform -
  85. func (d *Dao) SelectByTypeAndPlatform(ctx context.Context, typ string, platform string) (resp *model.Resource, err error) {
  86. return d.selectDBByTypeAndPlatform(ctx, typ, platform)
  87. }
  88. // GetDBCount -
  89. func (d *Dao) GetDBCount(ctx context.Context, typ string) (resp int64, err error) {
  90. return d.getDBCount(ctx, typ)
  91. }
  92. // get data from db source
  93. // addSResource add resource to mysql
  94. func (d *Dao) addDBResource(c context.Context, insert *model.Resource) (row int64, err error) {
  95. if insert == nil {
  96. return
  97. }
  98. var reply sql.Result
  99. if _, err = d.db.Begin(c); err != nil {
  100. log.Error("db.begin error(%v)", err)
  101. return
  102. }
  103. reply, err = d.db.Exec(c, _addResourceSQL, insert.Platform, insert.Build, insert.LimitType, insert.StartTime, insert.EndTime, insert.Type, insert.Title, insert.ImageInfo)
  104. if err != nil {
  105. log.Error("resource.addSResource d.db.Exec err: %v", err)
  106. return
  107. }
  108. row, err = reply.LastInsertId()
  109. return
  110. }
  111. func (d *Dao) editDBResource(c context.Context, id int64, update map[string]interface{}) (row int64, err error) {
  112. if update == nil || id < 0 {
  113. return
  114. }
  115. var tx = d.rsDB
  116. tableInfo := &model.Resource{}
  117. var reply = tx.Model(tableInfo).Where("id=?", id).Update(update)
  118. log.Info("effected rows: %d, id : %d", reply.RowsAffected, id)
  119. if reply.Error != nil {
  120. log.Error("resource.editResource error: %v", err)
  121. return
  122. }
  123. row = reply.RowsAffected
  124. return
  125. }
  126. func (d *Dao) getDBResourceList(c context.Context, typ string, page int64, pageSize int64) (resp []model.Resource, err error) {
  127. if typ == "" {
  128. return
  129. }
  130. var tx = d.rsDBReader
  131. tableInfo := &model.Resource{}
  132. err = tx.Model(tableInfo).
  133. Select("`id`,`platform`,`build`,`limit_type`,`start_time`,`end_time`,`title`,`image_info`").
  134. Where("type=?", typ).
  135. Order("id DESC").
  136. Limit(pageSize).
  137. Offset((page - 1) * pageSize).
  138. Find(&resp).Error
  139. if err != nil {
  140. log.Error("resource.editResource error: %v", err)
  141. return
  142. }
  143. return
  144. }
  145. func (d *Dao) getDBResourceListEx(c context.Context, typ []string, page int64, pageSize int64, devPlatform string, status string, startTime string, endTime string) (resp []model.Resource, count int64, err error) {
  146. if len(typ) <= 0 {
  147. return
  148. }
  149. whereStr := "1=1"
  150. for i, t := range typ {
  151. if i != 0 {
  152. whereStr += " or "
  153. } else {
  154. whereStr += " and ("
  155. }
  156. whereStr += fmt.Sprintf("`type` like \"%s%%\"", mysql.Escape(t))
  157. }
  158. whereStr += ")"
  159. if devPlatform != "" {
  160. whereStr += fmt.Sprintf(" and `platform`=\"%s\"", mysql.Escape(devPlatform))
  161. }
  162. if status != "" {
  163. var i int
  164. if i, err = strconv.Atoi(status); err != nil {
  165. return
  166. }
  167. now := time.Now().Format("2006-01-02 15:04:05")
  168. switch i {
  169. case 0:
  170. whereStr += fmt.Sprintf(" and `start_time`>=\"%s\"", now)
  171. case 1:
  172. whereStr += fmt.Sprintf(" and `start_time`<=\"%s\" and `end_time`>=\"%s\"", now, now)
  173. case -1:
  174. whereStr += fmt.Sprintf(" and `end_time`<=\"%s\"", now)
  175. }
  176. }
  177. if startTime != "" {
  178. whereStr += fmt.Sprintf(" and `start_time`>=\"%s\"", mysql.Escape(startTime))
  179. }
  180. if endTime != "" {
  181. whereStr += fmt.Sprintf(" and `end_time`<=\"%s\"", mysql.Escape(endTime))
  182. }
  183. var tx = d.rsDBReader
  184. tableInfo := &model.Resource{}
  185. err = tx.Model(tableInfo).
  186. Select("`id`,`platform`,`build`,`limit_type`,`start_time`,`end_time`,`title`,`image_info`,`type`").
  187. Where(whereStr).
  188. Order("id DESC").
  189. Limit(pageSize).
  190. Offset((page - 1) * pageSize).
  191. Find(&resp).Error
  192. if err != nil {
  193. log.Error("resource.editResource error: %v", err)
  194. return
  195. }
  196. err = tx.Model(tableInfo).
  197. Select("`id`").
  198. Where(whereStr).
  199. Count(&count).Error
  200. if err != nil {
  201. log.Error("resource.editResource error: %v", err)
  202. return
  203. }
  204. return
  205. }
  206. func (d *Dao) getDBCount(c context.Context, typ string) (resp int64, err error) {
  207. if typ == "" {
  208. return
  209. }
  210. var tx = d.rsDBReader
  211. tableInfo := &model.Resource{}
  212. err = tx.Model(tableInfo).
  213. Select("`id`").
  214. Where("type=?", typ).
  215. Count(&resp).Error
  216. if err != nil {
  217. log.Error("resource.editResource error: %v", err)
  218. return
  219. }
  220. return
  221. }
  222. func (d *Dao) offlineDBResource(c context.Context, id int64) (row int64, err error) {
  223. if id == 0 {
  224. return
  225. }
  226. reply, err := d.SelectById(c, id)
  227. if err != nil || reply == nil {
  228. log.Error("resource.OfflineResource select error: %v reply %v", err, reply)
  229. return
  230. }
  231. var tx = d.rsDB
  232. tableInfo := &model.Resource{}
  233. var updateReply = tx.Model(tableInfo).Where("id=?", id).Update("end_time", time.Now())
  234. if updateReply.Error != nil {
  235. log.Error("resource.OfflineResource update error: %v", err)
  236. return
  237. }
  238. return
  239. }
  240. func (d *Dao) selectDBById(c context.Context, id int64) (resp *model.Resource, err error) {
  241. if id == 0 {
  242. return
  243. }
  244. resp = &model.Resource{}
  245. var tx = d.rsDBReader
  246. var reply = tx.Model(&model.Resource{}).Where("id=?", id).Find(resp)
  247. if reply.Error != nil {
  248. log.Error("resource.SelectById error: %v", err)
  249. return
  250. }
  251. return
  252. }
  253. func (d *Dao) getDBInfo(ctx context.Context, typ string, platform string, build int64) (resp *model.Resource, err error) {
  254. if platform == "" || build == 0 {
  255. return
  256. }
  257. resp = &model.Resource{}
  258. var tx = d.rsDBReader
  259. now := time.Now()
  260. var reply = tx.Model(&model.Resource{}).Where("start_time<? and end_time>? and type=? and `platform`=? and ((`limit_type`=0 and `build`<=?) or (`limit_type`=1 and `build`=?) or (`limit_type`=2 and `build`>=?))", now, now, typ, platform, build, build, build).Limit(1).Find(resp)
  261. if reply.Error != nil {
  262. log.Error("resource.GetInfo error: %v", err)
  263. return
  264. }
  265. return
  266. }
  267. func (d *Dao) getDBBanner(ctx context.Context, platform string, build int64, t string) (resp []model.Resource, err error) {
  268. if platform == "" || build == 0 || t == "" {
  269. return
  270. }
  271. var tx = d.rsDBReader
  272. var reply = tx.Model(&model.Resource{}).Where("`start_time`<? and `end_time`>? and `type`=? and (`platform`='' or `platform`=?) and ((`limit_type`=0 and `build`<=?) or (`limit_type`=1 and `build`=?) or (`limit_type`=2 and `build`>=?))", time.Now(), time.Now(), t, platform, build, build, build).Order("mtime DESC").Find(&resp)
  273. if reply.Error != nil {
  274. log.Error("resource.GetBanner error: %v", err)
  275. return
  276. }
  277. return
  278. }
  279. func (d *Dao) selectDBByTypeAndPlatform(ctx context.Context, typ string, platform string) (resp *model.Resource, err error) {
  280. if typ == "" || platform == "" {
  281. return
  282. }
  283. resp = &model.Resource{}
  284. var tx = d.rsDBReader
  285. now := time.Now()
  286. var reply = tx.Model(&model.Resource{}).Where("`type`=? and `end_time`>? and `platform`=? ", typ, now, platform).Limit(1).Find(resp)
  287. if reply.Error != nil {
  288. resp = nil
  289. log.Error("resource.SelectByTypeAndPlatform error: %v", err)
  290. return
  291. }
  292. return
  293. }