media.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. package models
  2. import (
  3. "eta/eta_mini_crm_ht/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. type MediaType string
  11. type SendStatus string
  12. const (
  13. Video MediaType = "video"
  14. Audio MediaType = "audio"
  15. UNSEND SendStatus = "UNSEND"
  16. SEND SendStatus = "SEND"
  17. )
  18. func (m *Media) ToView() *ESMedia {
  19. return &ESMedia{
  20. MediaId: m.Id,
  21. AuthorId: m.AuthorId,
  22. AuthorName: m.AuthorName,
  23. MediaType: m.MediaType,
  24. CoverSrc: m.CoverSrc,
  25. Src: m.Src,
  26. MediaName: m.MediaName,
  27. SourceType: m.SourceType,
  28. MediaPlayMilliseconds: m.MediaPlayMilliseconds,
  29. PermissionIds: m.PermissionIds,
  30. PublishedTime: m.PublishedTime.Format(time.DateTime),
  31. }
  32. }
  33. type ESMedia struct {
  34. MediaId int `json:"mediaId"`
  35. AuthorId int `json:"authorId"`
  36. AuthorName string `json:"authorName"`
  37. CoverSrc string `json:"coverSrc"`
  38. MediaType MediaType `json:"mediaType"`
  39. Src string `json:"src"`
  40. MediaName string `json:"mediaName"`
  41. SourceType string `json:"sourceType"`
  42. MediaPlayMilliseconds int `json:"mediaPlayMilliseconds"`
  43. PermissionIds string `json:"permissionIds"`
  44. PublishedTime string `json:"publishedTime"`
  45. }
  46. type MediaView struct {
  47. MediaId int `json:"mediaId"`
  48. AuthorId int `json:"authorId"`
  49. AuthorName string `json:"authorName"`
  50. CoverSrc string `json:"coverSrc"`
  51. MediaType MediaType `json:"mediaType"`
  52. Src string `json:"src"`
  53. MediaName string `json:"mediaName"`
  54. SourceType string `json:"sourceType"`
  55. MediaPlayMilliseconds int `json:"mediaPlayMilliseconds"`
  56. PermissionIds string `json:"permissionIds"`
  57. PermissionNames string `json:"permission_names"`
  58. PublishedTime string `json:"publishedTime"`
  59. }
  60. func (m *Media) ToMediaView() (message *MediaView) {
  61. message = &MediaView{
  62. MediaId: m.Id,
  63. AuthorId: m.AuthorId,
  64. AuthorName: m.AuthorName,
  65. MediaType: m.MediaType,
  66. Src: m.Src,
  67. CoverSrc: m.CoverSrc,
  68. MediaName: m.MediaName,
  69. SourceType: m.SourceType,
  70. MediaPlayMilliseconds: m.MediaPlayMilliseconds,
  71. PermissionIds: m.PermissionIds,
  72. PublishedTime: m.PublishedTime.Format(time.DateTime),
  73. }
  74. ids := strings.Split(m.PermissionIds, ",")
  75. var idList []int
  76. for _, id := range ids {
  77. var idInt int
  78. idInt, err := strconv.Atoi(id)
  79. if err != nil {
  80. utils.FileLog.Error("转换品种名称失败:%v", err)
  81. break
  82. }
  83. idList = append(idList, idInt)
  84. }
  85. if len(idList) > 0 {
  86. names, err := GetPermissionNames(idList)
  87. if err != nil {
  88. utils.FileLog.Error("获取品种名称失败:%v", err)
  89. }
  90. message.PermissionNames = strings.Join(names, ",")
  91. }
  92. return
  93. }
  94. func (m *Media) ToMessageView() (message *MessageMedia) {
  95. message = &MessageMedia{
  96. MediaId: m.Id,
  97. AuthorId: m.AuthorId,
  98. AuthorName: m.AuthorName,
  99. MediaType: m.MediaType,
  100. Src: m.Src,
  101. CoverSrc: m.CoverSrc,
  102. MediaName: m.MediaName,
  103. SourceType: m.SourceType,
  104. SendStatus: m.SendStatus,
  105. MediaPlayMilliseconds: m.MediaPlayMilliseconds,
  106. PermissionIds: m.PermissionIds,
  107. PublishedTime: m.PublishedTime.Format(time.DateTime),
  108. }
  109. //image, err := GetImageById(m.CoverSrc)
  110. //if err != nil || image == nil {
  111. // message.CoverSrc = ""
  112. //} else {
  113. // message.CoverSrc = image.SrcUrl
  114. //}
  115. ids := strings.Split(m.PermissionIds, ",")
  116. var idList []int
  117. for _, id := range ids {
  118. var idInt int
  119. idInt, err := strconv.Atoi(id)
  120. if err != nil {
  121. utils.FileLog.Error("转换品种名称失败:%v", err)
  122. break
  123. }
  124. idList = append(idList, idInt)
  125. }
  126. if len(idList) > 0 {
  127. names, err := GetPermissionNames(idList)
  128. if err != nil {
  129. utils.FileLog.Error("获取品种名称失败:%v", err)
  130. }
  131. message.PermissionNames = strings.Join(names, ",")
  132. }
  133. return
  134. }
  135. type MessageMedia struct {
  136. MediaId int `json:"mediaId"`
  137. AuthorId int `json:"authorId"`
  138. AuthorName string `json:"authorName"`
  139. CoverSrc string `json:"coverSrc"`
  140. MediaType MediaType `json:"mediaType"`
  141. Src string `json:"src"`
  142. MediaName string `json:"mediaName"`
  143. SourceType string `json:"sourceType"`
  144. SendStatus SendStatus `json:"sendStatus"`
  145. MediaPlayMilliseconds int `json:"mediaPlayMilliseconds"`
  146. PermissionIds string `json:"permissionIds"`
  147. PermissionNames string `description:"permission_names"`
  148. PublishedTime string `json:"publishedTime"`
  149. }
  150. type Media struct {
  151. Id int `orm:"pk;auto" description:"id"`
  152. AuthorId int `description:"author_id"`
  153. AuthorName string `description:"author_name"`
  154. CoverSrc string `description:"cover_src"`
  155. MediaType MediaType `description:"media_type"`
  156. Src string `description:"src"`
  157. MediaName string `description:"media_name"`
  158. SourceType string `description:"source_type"`
  159. MediaPlayMilliseconds int `description:"media_play_milliseconds"`
  160. PermissionIds string `description:"permission_ids"`
  161. PublishedTime time.Time `description:"published_time"`
  162. SendStatus SendStatus `json:"sendStatus"`
  163. Deleted int `description:"deleted"`
  164. CreatedTime time.Time `description:"created_time"`
  165. UpdatedTime time.Time `description:"updated_time"`
  166. }
  167. func GetMediaCountByCondition(media MediaType, condition string, pars []interface{}) (count int, err error) {
  168. o := orm.NewOrm()
  169. sql := `SELECT COUNT(*) AS count FROM media WHERE 1 =1 AND media_type = ? and deleted=0 `
  170. if condition != "" {
  171. sql += condition
  172. }
  173. err = o.Raw(sql, media, pars).QueryRow(&count)
  174. return
  175. }
  176. func (m *Media) UpdateSendStatus(statusType SendStatus) (err error) {
  177. o := orm.NewOrm()
  178. m.SendStatus = statusType
  179. _, err = o.Update(m, "send_status")
  180. return
  181. }
  182. func GetMediaById(media MediaType, id int) (item *Media, err error) {
  183. o := orm.NewOrm()
  184. var sql string
  185. if media == "" {
  186. sql = `SELECT * FROM media WHERE 1 = 1 and id =? AND deleted=0`
  187. err = o.Raw(sql, id).QueryRow(&item)
  188. } else {
  189. sql = `SELECT * FROM media WHERE 1 = 1 AND media_type = ? and id =? AND deleted=0`
  190. err = o.Raw(sql, media, id).QueryRow(&item)
  191. }
  192. return
  193. }
  194. func GetMediasById(ids []int, sortCondition string, startPage, pageSize int) (medias []Media, err error) {
  195. o := orm.NewOrm()
  196. condition := " and id in (" + utils.GetOrmReplaceHolder(len(ids)) + ") AND deleted=0" + sortCondition + " limit ?,?"
  197. sql := `SELECT * FROM media WHERE 1=1 ` + condition
  198. _, err = o.Raw(sql, ids, startPage, pageSize).QueryRows(&medias)
  199. return
  200. }
  201. func GetMediaByCondition(media MediaType, condition string, sortCondition string, pars []interface{}, startPage, pageSize int) (items []*Media, err error) {
  202. o := orm.NewOrm()
  203. sql := `SELECT * FROM media WHERE 1 = 1 AND media_type = ? AND deleted=0`
  204. if condition != "" {
  205. sql += condition
  206. }
  207. if sortCondition != "" {
  208. sql += sortCondition // 排序
  209. }
  210. sql += ` LIMIT ?, ?`
  211. _, err = o.Raw(sql, media, pars, startPage, pageSize).QueryRows(&items)
  212. return
  213. }
  214. func InsertMedia(m *Media) (err error) {
  215. tx, err := orm.NewOrm().Begin()
  216. if err != nil {
  217. return
  218. }
  219. if err != nil {
  220. fmt.Printf("%v", err)
  221. return
  222. }
  223. defer func() {
  224. if r := recover(); r != nil {
  225. _ = tx.Rollback()
  226. return
  227. }
  228. if err != nil {
  229. _ = tx.Rollback()
  230. } else {
  231. _ = tx.Commit()
  232. }
  233. }()
  234. insertId, err := tx.Insert(m)
  235. if err != nil {
  236. _ = tx.Rollback()
  237. return
  238. }
  239. m.Id = int(insertId)
  240. permissionMappingList := make([]*MediaPermissionMapping, 0)
  241. ids := strings.Split(m.PermissionIds, ",")
  242. for _, item := range ids {
  243. x, _ := strconv.Atoi(item)
  244. permissionMappingList = append(permissionMappingList, &MediaPermissionMapping{
  245. MediaId: m.Id,
  246. MediaType: m.MediaType,
  247. PermissionId: x,
  248. CreatedTime: time.Now(),
  249. })
  250. }
  251. _, err = tx.InsertMulti(500, permissionMappingList)
  252. return
  253. }
  254. func DeleteMedia(m *Media) (err error) {
  255. o := orm.NewOrm()
  256. if err != nil {
  257. return err
  258. }
  259. tx, err := o.Begin()
  260. if err != nil {
  261. fmt.Printf("%v", err)
  262. return
  263. }
  264. defer func() {
  265. if r := recover(); r != nil {
  266. _ = tx.Rollback()
  267. return
  268. }
  269. }()
  270. err = DeleteMappingsById(m.Id)
  271. if err != nil {
  272. _ = tx.Rollback()
  273. return
  274. }
  275. _, err = o.Update(m, "deleted")
  276. if err != nil {
  277. _ = tx.Rollback()
  278. return
  279. }
  280. _ = tx.Commit()
  281. return
  282. }
  283. func UpdateMedia(m *Media) (updateId int64, err error) {
  284. o := orm.NewOrm()
  285. if err != nil {
  286. return 0, err
  287. }
  288. tx, err := o.Begin()
  289. if err != nil {
  290. fmt.Printf("%v", err)
  291. return
  292. }
  293. defer func() {
  294. if r := recover(); r != nil {
  295. _ = tx.Rollback()
  296. return
  297. }
  298. }()
  299. updateId, err = o.Update(m, "author_id", "media_name", "author_name", "cover_src", "src", "media_play_milliseconds", "permission_ids")
  300. if err != nil {
  301. _ = tx.Rollback()
  302. return
  303. }
  304. err = DeleteMappingsById(m.Id)
  305. if err != nil {
  306. _ = tx.Rollback()
  307. return
  308. }
  309. ids := strings.Split(m.PermissionIds, ",")
  310. for _, item := range ids {
  311. x, _ := strconv.Atoi(item)
  312. mapping := &MediaPermissionMapping{
  313. MediaId: m.Id,
  314. MediaType: m.MediaType,
  315. PermissionId: x,
  316. CreatedTime: time.Now(),
  317. }
  318. _, err = o.Insert(mapping)
  319. if err != nil {
  320. _ = tx.Rollback()
  321. return
  322. }
  323. }
  324. _ = tx.Commit()
  325. return
  326. }
  327. func FilterMediaIdIdsBySourceId(mediaType MediaType, ids []int) (mediaIds []int, err error) {
  328. o := orm.NewOrm()
  329. condition := "id in (" + utils.GetOrmReplaceHolder(len(ids)) + ") AND media_type = ? AND deleted=0"
  330. sql := `SELECT distinct id FROM media WHERE ` + condition
  331. _, err = o.Raw(sql, ids, mediaType).QueryRows(&mediaIds)
  332. return
  333. }
  334. func GetMediaIdsByCondition(condition string, pars []interface{}) (mediaIds []int, err error) {
  335. o := orm.NewOrm()
  336. sql := `SELECT distinct id FROM media WHERE 1=1 ` + condition
  337. _, err = o.Raw(sql, pars).QueryRows(&mediaIds)
  338. return
  339. }