cygx_yanxuan_special.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. package cygx
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hz_crm_api/models"
  6. "hongze/hz_crm_api/models/cygx"
  7. "hongze/hz_crm_api/services"
  8. "hongze/hz_crm_api/services/alarm_msg"
  9. "hongze/hz_crm_api/utils"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. // 研选专栏有新内容审核通过时,给关注此专栏的客户发送模板消息
  15. func SendWxMsgSpecialFollow(specialId int) (err error) {
  16. defer func() {
  17. if err != nil {
  18. go alarm_msg.SendAlarmMsg(fmt.Sprint("研选专栏有新内容审核通过时,给关注此专栏的客户发送模板消息失败,specialId:", specialId, err.Error()), 2)
  19. }
  20. }()
  21. var first string
  22. var keyword1 string
  23. var keyword2 string
  24. var keyword3 string
  25. var keyword4 string
  26. var remark string
  27. followers, e := cygx.GetYanxuanSpecialFollowUserById(specialId)
  28. if e != nil {
  29. err = errors.New("GetYanxuanSpecialFollowUserById, Err: " + e.Error())
  30. return
  31. }
  32. if len(followers) == 0 {
  33. return
  34. }
  35. specialItem, e := cygx.GetYanxuanSpecialItemById(specialId)
  36. if e != nil {
  37. err = errors.New("GetYanxuanSpecialFollowUserById, Err: " + e.Error())
  38. return
  39. }
  40. var allInUserId string
  41. for _, v := range followers {
  42. allInUserId += strconv.Itoa(v) + ","
  43. }
  44. allInUserId = strings.TrimRight(allInUserId, ",")
  45. userList, err := models.GetWxUserListByUserIds(allInUserId)
  46. if err != nil && err.Error() != utils.ErrNoRow() {
  47. return err
  48. }
  49. mobile := make([]string, 0)
  50. for _, v := range userList {
  51. mobile = append(mobile, v.Mobile)
  52. }
  53. openIdList, e := cygx.GetUserRecordListByMobileArr(mobile)
  54. if e != nil {
  55. err = errors.New("GetSellerByAdminId, Err: " + e.Error())
  56. return
  57. }
  58. if len(openIdList) == 0 {
  59. return
  60. }
  61. keyword1 = "研选专栏:" + specialItem.SpecialName
  62. keyword2 = "发布了新内容,点击查看详情"
  63. openIdArr := make([]string, 0)
  64. for _, v := range openIdList {
  65. openIdArr = append(openIdArr, v.OpenId)
  66. }
  67. redirectUrl := ""
  68. if utils.RunMode == "release" {
  69. redirectUrl = utils.WX_MSG_PATH_YX_SPECIAL_DETAIL + strconv.Itoa(specialId)
  70. }
  71. sendInfo := new(services.SendWxTemplate)
  72. sendInfo.First = first
  73. sendInfo.Keyword1 = keyword1
  74. sendInfo.Keyword2 = keyword2
  75. sendInfo.Keyword3 = keyword3
  76. sendInfo.Keyword4 = keyword4
  77. sendInfo.Remark = remark
  78. sendInfo.TemplateId = utils.WxMsgTemplateIdActivityChangeApplyXzs
  79. sendInfo.RedirectUrl = redirectUrl
  80. sendInfo.RedirectTarget = 3
  81. sendInfo.Resource = strconv.Itoa(specialId)
  82. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
  83. sendInfo.OpenIdArr = openIdArr
  84. err = services.SendTemplateMsg(sendInfo)
  85. if err != nil {
  86. return
  87. }
  88. return
  89. }
  90. //func init() {
  91. // SendWxMsgSpecialAuthor(190, 1)
  92. //}
  93. // 研选专栏审核完成时,给提交人发送模板消息
  94. func SendWxMsgSpecialAuthor(specialId, status int) (err error) {
  95. defer func() {
  96. if err != nil {
  97. go alarm_msg.SendAlarmMsg(fmt.Sprint("研选专栏审核完成时,给提交人发送模板消息 specialId:", specialId, err.Error()), 2)
  98. }
  99. }()
  100. var first string
  101. var keyword1 string
  102. var keyword2 string
  103. var keyword3 string
  104. var keyword4 string
  105. var remark string
  106. var redirectUrl string
  107. specialItem, e := cygx.GetYanxuanSpecialItemById(specialId)
  108. if e != nil {
  109. err = errors.New("GetYanxuanSpecialFollowUserById, Err: " + e.Error())
  110. return
  111. }
  112. user, e := models.GetWxUserByUserId(specialItem.UserId)
  113. if e != nil {
  114. err = errors.New("GetWxUserByUserId, Err: " + e.Error())
  115. return err
  116. }
  117. var mobiles []string
  118. mobiles = append(mobiles, user.Mobile)
  119. openIdList, err := cygx.GetUserRecordListByMobileArr(mobiles)
  120. if err != nil && err.Error() != utils.ErrNoRow() {
  121. return err
  122. }
  123. if len(openIdList) == 0 {
  124. return
  125. }
  126. //first =
  127. keyword1 = "研选专栏内容审核"
  128. if status == 1 {
  129. keyword2 = "已通过审核,点击查看详情"
  130. if utils.RunMode == "release" {
  131. redirectUrl = utils.WX_MSG_PATH_YX_SPECIAL_DETAIL + strconv.Itoa(specialId)
  132. }
  133. } else {
  134. keyword2 = "未通过审核,点击查看驳回原因"
  135. if utils.RunMode == "release" {
  136. redirectUrl = utils.WX_MSG_PATH_YX_SPECIAL_CENTER
  137. }
  138. }
  139. keyword3 = "-"
  140. openIdArr := make([]string, 0)
  141. for _, v := range openIdList {
  142. openIdArr = append(openIdArr, v.OpenId)
  143. }
  144. sendInfo := new(services.SendWxTemplate)
  145. sendInfo.First = first
  146. sendInfo.Keyword1 = keyword1
  147. sendInfo.Keyword2 = keyword2
  148. sendInfo.Keyword3 = keyword3
  149. sendInfo.Keyword4 = keyword4
  150. sendInfo.Remark = remark
  151. sendInfo.TemplateId = utils.WxMsgTemplateIdActivityChangeApplyXzs
  152. sendInfo.RedirectUrl = redirectUrl
  153. sendInfo.RedirectTarget = 3
  154. sendInfo.Resource = strconv.Itoa(specialId)
  155. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
  156. sendInfo.OpenIdArr = openIdArr
  157. fmt.Println(sendInfo)
  158. err = services.SendTemplateMsg(sendInfo)
  159. if err != nil {
  160. return
  161. }
  162. return
  163. }
  164. // SendReviewTemplateMsgAdmin 提交审核时给王芳,汪洋发消息
  165. //func SendReviewTemplateMsgAdmin(req models.ApplyTryReq, usermobile, applyMethod string, isResearch bool) (err error) {
  166. // defer func() {
  167. // if err != nil {
  168. // go utils.SendAlarmMsg("处理试用申请给王芳,汪洋发消息失败, ErrMsg: "+err.Error(), 3)
  169. // }
  170. // }()
  171. // var configCode string
  172. // //如果是研选的就推送给汪洋跟王芳,否则就推送给王芳
  173. // if isResearch {
  174. // configCode = utils.TPL_MSG_WANG_FANG_WANG_YANG
  175. // } else {
  176. // configCode = utils.TPL_MSG
  177. // }
  178. // cnf, e := models.GetConfigByCode(configCode)
  179. // if e != nil {
  180. // err = errors.New("GetConfigByCode, Err: " + e.Error() + configCode)
  181. // return
  182. // }
  183. // openIdList, e := models.GetUserRecordListByMobile(4, cnf.ConfigValue)
  184. // if e != nil && e.Error() != utils.ErrNoRow() {
  185. // err = errors.New("GetUserRecordListByMobile, Err: " + e.Error() + cnf.ConfigValue)
  186. // return err
  187. // }
  188. //
  189. // for _, v := range openIdList {
  190. // go SendPermissionApplyTemplateMsg(req.RealName, req.CompanyName, usermobile, applyMethod, v)
  191. // }
  192. //
  193. // return
  194. //}
  195. //func init() {
  196. // UpdateYanxuanSpecialResourceData(190)
  197. //}
  198. // 更新研选专栏 写入首页最新 cygx_resource_data 表
  199. func UpdateYanxuanSpecialResourceData(sourceId int) {
  200. var err error
  201. defer func() {
  202. if err != nil {
  203. go alarm_msg.SendAlarmMsg(fmt.Sprint("更新研选专栏失败ourceId: ", sourceId, err.Error()), 2)
  204. }
  205. }()
  206. var source = utils.CYGX_OBJ_YANXUANSPECIAL
  207. var condition string
  208. var pars []interface{}
  209. condition = ` AND status = 3 AND id = ? `
  210. pars = append(pars, sourceId)
  211. total, e := cygx.GetCygxYanxuanSpecialCount(condition, pars)
  212. if e != nil {
  213. err = errors.New("GetCygxYanxuanSpecialCount, Err: " + e.Error())
  214. return
  215. }
  216. //如果取消发布了就做删除处理
  217. if total == 0 {
  218. e = cygx.DeleteResourceData(sourceId, source)
  219. if e != nil {
  220. err = errors.New("DeleteResourceData, Err: " + e.Error())
  221. return
  222. }
  223. } else {
  224. //判断是否存在,如果不存在就新增,存在就更新
  225. totalData, e := cygx.GetCygxResourceDataBySourceAndIdCount(sourceId, source)
  226. if e != nil {
  227. err = errors.New("GetCygxReportSelectionBySourceAndId, Err: " + e.Error())
  228. return
  229. }
  230. detail, e := cygx.GetYanxuanSpecialItemById(sourceId)
  231. if e != nil {
  232. err = errors.New("GetYanxuanSpecialFollowUserById, Err: " + e.Error())
  233. return
  234. }
  235. publishDate := time.Now().Format(utils.FormatDateTime)
  236. item := new(cygx.CygxResourceData)
  237. item.SourceId = sourceId
  238. item.Source = source
  239. item.PublishDate = publishDate
  240. item.CreateTime = time.Now()
  241. item.SearchTitle = detail.Title
  242. item.SearchContent = ""
  243. item.SearchOrderTime = publishDate
  244. if totalData == 0 {
  245. _, e := cygx.AddCygxResourceData(item)
  246. if e != nil {
  247. err = errors.New("AddCygxResourceData, Err: " + e.Error())
  248. return
  249. }
  250. } else {
  251. e = cygx.UpdateResourceDataByItem(item)
  252. if e != nil {
  253. err = errors.New("UpdateResourceDataByItem, Err: " + e.Error())
  254. return
  255. }
  256. }
  257. }
  258. return
  259. }
  260. // 研选专栏审批记录
  261. func AddAddCygxYanxuanSpecialApprovalLog(user *models.WxUserItem, specialId, status int, reason string) {
  262. var err error
  263. defer func() {
  264. if err != nil {
  265. go alarm_msg.SendAlarmMsg(fmt.Sprint("获取研选专栏用户信息失败,GetYanxuanSpecialAuthorInfo Err ", err, "userId", user.UserId), 2)
  266. }
  267. }()
  268. detail, e := cygx.GetYanxuanSpecialBySpecialId(specialId)
  269. if e != nil {
  270. err = errors.New("GetYanxuanSpecialBySpecialId, Err: " + e.Error())
  271. return
  272. }
  273. specialAuthor, e := cygx.GetCygxYanxuanSpecialAuthorByUserId(detail.UserId)
  274. if e != nil {
  275. err = errors.New("GetCygxYanxuanSpecialAuthorByUserId, Err: " + e.Error())
  276. return
  277. }
  278. item := new(cygx.CygxYanxuanSpecialApprovalLog)
  279. item.UserId = detail.UserId
  280. item.Content = detail.Content
  281. item.Tags = detail.Tags
  282. item.ApprovalStatus = status
  283. item.ImgUrl = detail.ImgUrl
  284. item.DocUrl = detail.DocUrl
  285. item.Reason = reason
  286. item.Title = detail.Title
  287. item.Type = detail.Type
  288. item.CompanyTags = detail.CompanyTags
  289. item.IndustryTags = detail.IndustryTags
  290. item.YanxuanSpecialId = specialId
  291. item.AdminName = user.RealName
  292. item.AdminUserId = user.UserId
  293. item.SpecialName = specialAuthor.SpecialName
  294. item.NickName = specialAuthor.NickName
  295. item.CreateTime = time.Now()
  296. item.ModifyTime = time.Now()
  297. e = cygx.AddCygxYanxuanSpecialApprovalLog(item)
  298. if e != nil {
  299. err = errors.New("AddCygxYanxuanSpecialApprovalLog, Err: " + e.Error())
  300. return
  301. }
  302. return
  303. }
  304. // 获取专栏用户最新的一篇文章信息
  305. func GetBestNewYanxuanSpecialByUserId(userIds []int) (mapResp map[int]string) {
  306. lenArr := len(userIds)
  307. if lenArr == 0 {
  308. return
  309. }
  310. var err error
  311. defer func() {
  312. if err != nil {
  313. fmt.Println(err)
  314. go alarm_msg.SendAlarmMsg(fmt.Sprint("获取研选专栏用户信息失败,GetBestNewYanxuanSpecialByUserId Err ", err, "userIds", userIds), 2)
  315. }
  316. }()
  317. var condition string
  318. var pars []interface{}
  319. condition += ` AND a.publish_time = ( SELECT max( b.publish_time ) FROM cygx_yanxuan_special b WHERE a.user_id = b.user_id AND b.STATUS = 3 ) AND user_id IN (` + utils.GetOrmInReplace(lenArr) + `) AND a.status = 3 GROUP BY user_id ORDER BY publish_time DESC`
  320. pars = append(pars, userIds)
  321. list, e := cygx.GetYanxuanSpecialListBycondition(condition, pars, 0, lenArr)
  322. if e != nil && e.Error() != utils.ErrNoRow() {
  323. err = errors.New("GetYanxuanSpecialListBycondition, Err: " + e.Error())
  324. return
  325. }
  326. mapResp = make(map[int]string, 0)
  327. for _, v := range list {
  328. mapResp[v.UserId] = v.PublishTime.Format(utils.FormatDateTime)
  329. }
  330. return
  331. }