road_video.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. package yb
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "hongze/hz_crm_api/models"
  6. "hongze/hz_crm_api/models/company"
  7. "hongze/hz_crm_api/models/system"
  8. "hongze/hz_crm_api/models/yb"
  9. "hongze/hz_crm_api/models/yb/response"
  10. "hongze/hz_crm_api/services"
  11. "hongze/hz_crm_api/utils"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. // GetRoadVideoList 路演视频列表
  17. func GetRoadVideoList(condition string, pars []interface{}, orderStr string, startSize, pageSize int) (total int, resp *response.RoadVideoListResp, err error, errMsg string) {
  18. resp = new(response.RoadVideoListResp)
  19. total, list, e := yb.GetRoadVideoList(condition, pars, orderStr, startSize, pageSize)
  20. if e != nil {
  21. errMsg = "获取视频列表失败"
  22. err = errors.New("获取视频列表失败, Err:" + e.Error())
  23. return
  24. }
  25. if len(list) == 0 {
  26. return
  27. }
  28. allChartPermissionId := ""
  29. chartNameMap := make(map[string]string)
  30. reportIds := make([]int, 0)
  31. for _, v := range list {
  32. allChartPermissionId += v.ChartPermissionIds + ","
  33. if v.ReportId > 0 {
  34. reportIds = append(reportIds, v.ReportId)
  35. }
  36. }
  37. if allChartPermissionId != "" {
  38. allChartPermissionId = strings.Trim(allChartPermissionId, ",")
  39. chartPermissionIdSlice := strings.Split(allChartPermissionId, ",")
  40. chartList, e := models.GetChartPermissionByIds(chartPermissionIdSlice)
  41. if e != nil {
  42. errMsg = "品种信息有误"
  43. err = errors.New("获取品种信息失败, Err:" + e.Error())
  44. return
  45. }
  46. for _, v := range chartList {
  47. chartNameMap["'"+strconv.Itoa(v.ChartPermissionId)+"'"] = v.PermissionName
  48. }
  49. }
  50. // 视频绑定的报告标题
  51. reportTitleMap := make(map[int]string)
  52. if len(reportIds) > 0 {
  53. reportCond := ` AND id IN (` + utils.GetOrmInReplace(len(reportIds)) + `)`
  54. reportPars := make([]interface{}, 0)
  55. reportPars = append(reportPars, reportIds)
  56. fieldArr := []string{"id", "title"}
  57. reports, e := models.GetReportByCondition(reportCond, reportPars, fieldArr, "", false, 0, 0)
  58. if e != nil {
  59. errMsg = "获取失败"
  60. err = errors.New("获取视频绑定报告信息失败, Err:" + e.Error())
  61. return
  62. }
  63. for i := range reports {
  64. reportTitleMap[reports[i].Id] = reports[i].Title
  65. }
  66. }
  67. chartPermissionNames := ""
  68. chartPermissionIds := ""
  69. respList := make([]*response.RoadVideoItem, 0)
  70. for _, v := range list {
  71. chartPermissionNames = ""
  72. chartPermissionIds = ""
  73. if v.ChartPermissionIds != "" {
  74. tmpSlice := strings.Split(v.ChartPermissionIds, ",")
  75. if len(tmpSlice) > 0 {
  76. for _, cid := range tmpSlice {
  77. if name, ok := chartNameMap[cid]; ok {
  78. chartPermissionNames += name + ","
  79. }
  80. chartPermissionIds += strings.Trim(cid, "'") + ","
  81. }
  82. }
  83. if chartPermissionNames != "" {
  84. chartPermissionNames = strings.Trim(chartPermissionNames, ",")
  85. }
  86. if chartPermissionIds != "" {
  87. chartPermissionIds = strings.Trim(chartPermissionIds, ",")
  88. }
  89. }
  90. item := &response.RoadVideoItem{
  91. RoadVideoId: v.RoadVideoId,
  92. Title: v.Title,
  93. ChartPermissionIds: chartPermissionIds,
  94. ChartPermissionNames: chartPermissionNames,
  95. CoverImgUrl: v.CoverImgUrl,
  96. VideoUrl: v.VideoUrl,
  97. VideoSeconds: v.VideoSeconds,
  98. PublishState: v.PublishState,
  99. SendThsState: v.SendThsState,
  100. SendTemplateState: v.SendTemplateState,
  101. AdminId: v.AdminId,
  102. AdminRealName: v.AdminRealName,
  103. CreateTime: v.CreateTime.Format(utils.FormatDateTime),
  104. ModifyTime: v.ModifyTime.Format(utils.FormatDateTime),
  105. ClickNum: v.ClickNum,
  106. ReportId: v.ReportId,
  107. ReportTitle: reportTitleMap[v.ReportId],
  108. }
  109. respList = append(respList, item)
  110. }
  111. resp.List = respList
  112. return
  113. }
  114. // SaveRoadVideo 保存视频
  115. func SaveRoadVideo(sysUser *system.Admin, videoId, reportId int, chartPermissionIds, title, coverImgUrl, videoUrl, videoSeconds string) (resp *response.RoadVideoItem, errMsg string, err error) {
  116. permissionStr := ""
  117. if chartPermissionIds != "" {
  118. chartPermissionIdSlice := strings.Split(chartPermissionIds, ",")
  119. chartList, e := models.GetChartPermissionByIds(chartPermissionIdSlice)
  120. if e != nil {
  121. errMsg = "品种信息有误"
  122. err = errors.New("获取品种信息失败, Err:" + e.Error())
  123. return
  124. }
  125. if len(chartPermissionIdSlice) != len(chartList) {
  126. errMsg = "品种信息有误"
  127. err = errors.New("获取品种信息失败")
  128. return
  129. }
  130. for _, v := range chartList {
  131. permissionStr += "'" + strconv.Itoa(v.ChartPermissionId) + "',"
  132. }
  133. if permissionStr != "" {
  134. permissionStr = strings.Trim(permissionStr, ",")
  135. }
  136. }
  137. resp = new(response.RoadVideoItem)
  138. // 更新
  139. nowTime := time.Now().Local()
  140. if videoId > 0 {
  141. item, e := yb.GetRoadVideoById(videoId)
  142. if e != nil {
  143. errMsg = "视频信息有误"
  144. err = errors.New("获取视频信息失败, Err:" + e.Error())
  145. return
  146. }
  147. updateCols := make([]string, 0)
  148. updateCols = append(updateCols, "Title", "ChartPermissionIds", "CoverImgUrl", "VideoUrl", "VideoSeconds", "ModifyTime", "ReportId")
  149. item.Title = title
  150. item.ChartPermissionIds = permissionStr
  151. item.CoverImgUrl = coverImgUrl
  152. item.VideoUrl = videoUrl
  153. item.VideoSeconds = videoSeconds
  154. item.ModifyTime = nowTime
  155. item.ReportId = reportId
  156. if e := item.Update(updateCols); e != nil {
  157. errMsg = "更新失败"
  158. err = errors.New("更新视频信息失败, Err:" + e.Error())
  159. }
  160. resp = formatRoadVideoItem(item)
  161. return
  162. }
  163. // 新增
  164. videoItem := &yb.RoadVideo{
  165. Title: title,
  166. ChartPermissionIds: permissionStr,
  167. CoverImgUrl: coverImgUrl,
  168. VideoUrl: videoUrl,
  169. VideoSeconds: videoSeconds,
  170. CreateTime: nowTime,
  171. ModifyTime: nowTime,
  172. AdminId: sysUser.AdminId,
  173. AdminRealName: sysUser.RealName,
  174. ReportId: reportId,
  175. }
  176. if e := videoItem.Add(); e != nil {
  177. errMsg = "新增失败"
  178. err = errors.New("新增视频信息失败, Err:" + e.Error())
  179. }
  180. resp = formatRoadVideoItem(videoItem)
  181. return
  182. }
  183. func formatRoadVideoItem(item *yb.RoadVideo) (resp *response.RoadVideoItem) {
  184. //根据chartPermissionId,查询权限名称
  185. ChartPermissionNames := ""
  186. if item.ChartPermissionIds != "" {
  187. chartPermissionIdSlice := strings.Split(item.ChartPermissionIds, ",")
  188. chartList, e := models.GetChartPermissionByIds(chartPermissionIdSlice)
  189. if e == nil {
  190. for _, v := range chartList {
  191. ChartPermissionNames += v.ChartPermissionName + ","
  192. }
  193. if ChartPermissionNames != "" {
  194. ChartPermissionNames = strings.Trim(ChartPermissionNames, ",")
  195. }
  196. }
  197. }
  198. resp = new(response.RoadVideoItem)
  199. if item != nil {
  200. resp.RoadVideoId = item.RoadVideoId
  201. resp.Title = item.Title
  202. resp.ChartPermissionIds = item.ChartPermissionIds
  203. resp.ChartPermissionNames = ChartPermissionNames
  204. resp.CoverImgUrl = item.CoverImgUrl
  205. resp.VideoUrl = item.VideoUrl
  206. resp.VideoSeconds = item.VideoSeconds
  207. resp.PublishState = item.PublishState
  208. resp.SendThsState = item.SendThsState
  209. resp.SendTemplateState = item.SendTemplateState
  210. resp.AdminId = item.AdminId
  211. resp.AdminRealName = item.AdminRealName
  212. resp.CreateTime = item.CreateTime.Format(utils.FormatDateTime)
  213. resp.ModifyTime = item.ModifyTime.Format(utils.FormatDateTime)
  214. resp.ReportId = item.ReportId
  215. }
  216. return
  217. }
  218. // SoftDeleteRoadVideo 删除视频
  219. func SoftDeleteRoadVideo(videoId int) (errMsg string, err error) {
  220. if _, e := yb.GetRoadVideoById(videoId); e != nil {
  221. errMsg = "视频信息有误"
  222. err = errors.New("获取视频信息失败, Err:" + e.Error())
  223. return
  224. }
  225. if e := yb.DeleteRoadVideo(videoId); e != nil {
  226. errMsg = "删除失败"
  227. err = errors.New("删除视频失败, Err:" + e.Error())
  228. }
  229. return
  230. }
  231. // PublishRoadVideo 发布/取消发布视频
  232. func PublishRoadVideo(videoId, publishState int) (errMsg string, err error) {
  233. item, e := yb.GetRoadVideoById(videoId)
  234. if e != nil {
  235. errMsg = "视频信息有误"
  236. err = errors.New("获取视频信息失败, Err:" + e.Error())
  237. return
  238. }
  239. nowTime := time.Now().Local()
  240. updateCols := make([]string, 0)
  241. updateCols = append(updateCols, "PublishState", "ModifyTime")
  242. if publishState == 1 {
  243. updateCols = append(updateCols, "PublishTime")
  244. item.PublishTime = nowTime
  245. }
  246. item.PublishState = publishState
  247. item.ModifyTime = nowTime
  248. if e := item.Update(updateCols); e != nil {
  249. errMsg = "操作失败"
  250. err = errors.New("更新视频信息失败, Err:" + e.Error())
  251. }
  252. return
  253. }
  254. // SendRoadVideoThsMsg 推送视频客群消息
  255. func SendRoadVideoThsMsg(videoId int) (errMsg string, err error) {
  256. item, e := yb.GetRoadVideoById(videoId)
  257. if e != nil {
  258. errMsg = "视频信息有误"
  259. err = errors.New("获取视频信息失败, Err:" + e.Error())
  260. return
  261. }
  262. if item.PublishState != 1 {
  263. errMsg = "视频未发布, 不可推送"
  264. err = errors.New("视频未发布, 不可推送")
  265. return
  266. }
  267. if item.SendThsState != 0 {
  268. errMsg = "请勿重复推送"
  269. err = errors.New("请勿重复推送客群消息")
  270. return
  271. }
  272. nowTime := time.Now().Local()
  273. updateCols := make([]string, 0)
  274. updateCols = append(updateCols, "SendThsState", "SendThsTime")
  275. item.SendThsState = 1
  276. item.SendThsTime = nowTime
  277. if e := item.Update(updateCols); e != nil {
  278. err = errors.New("更新视频信息失败, Err:" + e.Error())
  279. }
  280. //go func() {
  281. // _ = services.SendYbRoadVideoThs(item.RoadVideoId, item.Title, item.ChartPermissionIds)
  282. //}()
  283. return
  284. }
  285. // SendRoadVideoTemplateMsg 推送模板消息
  286. func SendRoadVideoTemplateMsg(videoId int) (errMsg string, err error) {
  287. item, e := yb.GetRoadVideoById(videoId)
  288. if e != nil {
  289. errMsg = "视频信息有误"
  290. err = errors.New("获取视频信息失败, Err:" + e.Error())
  291. return
  292. }
  293. if item.PublishState != 1 {
  294. errMsg = "视频未发布, 不可推送"
  295. err = errors.New("视频未发布, 不可推送")
  296. return
  297. }
  298. if item.SendTemplateState != 0 {
  299. errMsg = "请勿重复推送"
  300. err = errors.New("请勿重复推送模板消息")
  301. return
  302. }
  303. updateCols := make([]string, 0)
  304. updateCols = append(updateCols, "SendTemplateState", "SendTemplateTime")
  305. item.SendTemplateState = 1
  306. item.SendTemplateTime = time.Now().Local()
  307. if e := item.Update(updateCols); e != nil {
  308. err = errors.New("更新视频信息失败, Err:" + e.Error())
  309. }
  310. go func() {
  311. _ = services.SendYbRoadVideoWxMsg(item.RoadVideoId, item.Title, item.PublishTime.Format(utils.FormatDateTime), item.ChartPermissionIds)
  312. }()
  313. return
  314. }
  315. func GetFiccPermission() (ret response.RoadPermissionResp, err error) {
  316. permissionList, err := models.GetFiccPermissionExceptTactic()
  317. if err != nil {
  318. return
  319. }
  320. permissionFirstMap := make(map[int]*response.RoadPermissionItem)
  321. /*//查询首页展示的图标
  322. permissionFirstList, err := models.GetYbChartPermissionFirst()
  323. if err != nil {
  324. return
  325. }*/
  326. permissionFirstList := make([]*response.RoadPermissionItem, 0)
  327. for _, v := range permissionList {
  328. if v.ParentId == 0 {
  329. tmp := &response.RoadPermissionItem{
  330. Id: v.ChartPermissionId,
  331. ClassifyName: v.PermissionName,
  332. }
  333. permissionFirstMap[v.ChartPermissionId] = tmp
  334. permissionFirstList = append(permissionFirstList, tmp)
  335. }
  336. }
  337. for _, v := range permissionList {
  338. if v.ParentId > 0 {
  339. temp := new(response.RoadPermissionSecondItem)
  340. temp.ChartPermissionID = v.ChartPermissionId
  341. temp.ChartPermissionName = v.PermissionName
  342. if _, ok := permissionFirstMap[v.ParentId]; ok {
  343. permissionFirstMap[v.ParentId].List = append(permissionFirstMap[v.ParentId].List, temp)
  344. }
  345. }
  346. }
  347. var list []*response.RoadPermissionItem
  348. for _, v := range permissionFirstList {
  349. temp := new(response.RoadPermissionItem)
  350. temp.Id = v.Id
  351. temp.ClassifyName = v.ClassifyName
  352. l, ok := permissionFirstMap[v.Id]
  353. if ok {
  354. temp.List = l.List
  355. }
  356. list = append(list, temp)
  357. }
  358. ret.List = list
  359. return
  360. }
  361. // GetVideoRelationReportClassifyId 获取视频关联报告的一级分类ID列表
  362. func GetVideoRelationReportClassifyId() (classifyIdList []int, err error) {
  363. // 别问为啥要从配置里拿=_=!
  364. confKey := "video_relation_report_classify_id"
  365. conf, e := company.GetConfigDetailByCode(confKey)
  366. if e != nil {
  367. err = errors.New("获取双周报分类列表失败, Err: " + e.Error())
  368. return
  369. }
  370. if conf.ConfigValue == "" {
  371. err = errors.New("双周报ID配置有误")
  372. return
  373. }
  374. type TwoWeekIdConf struct {
  375. Debug []int
  376. Release []int
  377. }
  378. classifyIdConf := new(TwoWeekIdConf)
  379. if e = json.Unmarshal([]byte(conf.ConfigValue), &classifyIdConf); e != nil {
  380. err = errors.New("解析双周报ID配置失败, Err: " + e.Error())
  381. return
  382. }
  383. if utils.RunMode == "debug" {
  384. classifyIdList = classifyIdConf.Debug
  385. } else {
  386. classifyIdList = classifyIdConf.Release
  387. }
  388. return
  389. }