classify.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package services
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hz_crm_api/models"
  6. "hongze/hz_crm_api/utils"
  7. "io/ioutil"
  8. "net/http"
  9. "sort"
  10. "strings"
  11. )
  12. type GetClassifyListReq struct {
  13. Keyword string
  14. CompanyType string
  15. HideDayWeek int
  16. }
  17. type ClassifySetEnabledReq struct {
  18. ClassifyId int `description:"分类ID"`
  19. Enabled int `description:"是否可用,1可用,0禁用"`
  20. }
  21. type EditClassifyReq struct {
  22. ClassifyId int `description:"分类ID"`
  23. /*Abstract string `description:"栏目简介"`
  24. Descript string `description:"分享描述"`
  25. ReportAuthor string `description:"栏目作者"`
  26. AuthorDescript string `description:"作者简介"`
  27. ColumnImgUrl string `description:"栏目配图"`
  28. ReportImgUrl string `description:"报告配图"`
  29. HeadImgUrl string `description:"头部banner"`
  30. AvatarImgUrl string `description:"头像"`
  31. HomeImgUrl string `description:"首页配图"`*/
  32. ClassifyLabel string `description:"分类标签"`
  33. ShowType int `description:"展示类型:1-列表 2-专栏"`
  34. /*HasTeleconference int `description:"是否有电话会:0-否 1-是"`
  35. VipTitle string `description:"研究员头衔"`*/
  36. //Sort int `description:"后台排序"`
  37. IsShow int `description:"是否在小程序显示:1-显示 0-隐藏"`
  38. YbFiccSort int `description:"小程序FICC页排序"`
  39. YbFiccIcon string `description:"小程序FICC页icon"`
  40. YbFiccPcIcon string `description:"小程序PC端FICC页背景图"`
  41. YbIconUrl string `description:"小程序已购页icon"`
  42. YbBgUrl string `description:"小程序已购详情背景图"`
  43. YbListImg string `description:"小程序研报列表封面图"`
  44. YbShareBgImg string `description:"小程序研报详情分享背景图"`
  45. YbRightBanner string `description:"Pc端详情页,右侧,报告合集背景图"`
  46. MenuList []*ClassifyMenuSaveReq `description:"子目录列表"`
  47. ClassifyMenuId int `description:"二级分类-子目录ID"`
  48. RelateTel int `description:"是否在电话会中可选: 0-否; 1-是"`
  49. RelateVideo int `description:"是否在路演视频中可选: 0-否; 1-是"`
  50. }
  51. // ClassifyMenuSaveReq 保存分类子目录请求体
  52. type ClassifyMenuSaveReq struct {
  53. MenuId int `description:"子目录ID, 0为新增, 大于0为编辑"`
  54. MenuName string `description:"子目录名称"`
  55. }
  56. type CrmEtaBaseResp struct {
  57. Code int `json:"code" description:"状态码"`
  58. Msg string `json:"msg" description:"提示信息"`
  59. ErrMsg string `json:"-" description:"错误信息,不用返回给前端,只是做日志记录"`
  60. }
  61. func crmEtaPost(url string, param interface{}) (respBody []byte, err error) {
  62. data, e := json.Marshal(param)
  63. if e != nil {
  64. err = fmt.Errorf("data json marshal err: %s", e.Error())
  65. return
  66. }
  67. body := ioutil.NopCloser(strings.NewReader(string(data)))
  68. client := &http.Client{}
  69. req, e := http.NewRequest("POST", url, body)
  70. if e != nil {
  71. err = fmt.Errorf("http create request err: %s", e.Error())
  72. return
  73. }
  74. contentType := "application/json;charset=utf-8"
  75. req.Header.Set("Content-Type", contentType)
  76. req.Header.Set("Authorization", utils.CrmEtaAuthorization)
  77. resp, e := client.Do(req)
  78. if e != nil {
  79. err = fmt.Errorf("http client do err: %s", e.Error())
  80. return
  81. }
  82. defer func() {
  83. _ = resp.Body.Close()
  84. }()
  85. b, e := ioutil.ReadAll(resp.Body)
  86. if e != nil {
  87. err = fmt.Errorf("resp body read err: %s", e.Error())
  88. return
  89. }
  90. if len(b) == 0 {
  91. err = fmt.Errorf("resp body is empty")
  92. return
  93. }
  94. // 生产环境解密, 注意有个坑前后的双引号
  95. if utils.RunMode == "release" {
  96. str := string(b)
  97. str = strings.Trim(str, `"`)
  98. b = utils.DesBase64Decrypt([]byte(str))
  99. }
  100. respBody = b
  101. return
  102. }
  103. func EditReportClassify(pars *EditClassifyReq) (err error, errMsg string) {
  104. if utils.CrmEtaServerUrl == "" {
  105. return
  106. }
  107. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/eta/classify/edit")
  108. b, err := crmEtaPost(url, pars)
  109. if err != nil {
  110. errMsg = "更新品种失败"
  111. err = fmt.Errorf("url:%s err: %s", url, err.Error())
  112. return
  113. }
  114. result := new(CrmEtaBaseResp)
  115. if e := json.Unmarshal(b, &result); e != nil {
  116. errMsg = "更新分类失败"
  117. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  118. return
  119. }
  120. utils.FileLog.Info("%s", string(b))
  121. if result.Code != 200 {
  122. err = fmt.Errorf("result: %s, err: %s", string(b), result.ErrMsg)
  123. errMsg = result.Msg
  124. return
  125. }
  126. return
  127. }
  128. type EditClassifyPermissionReq struct {
  129. Keyword string
  130. ChartPermissionIdList []int `description:"权限id数组"`
  131. NewKeyword string
  132. }
  133. // GetClassifyList 获取报告分类已绑定的权限
  134. func GetClassifyList(req *GetClassifyListReq) (list models.ClassifyListResp, err error) {
  135. if utils.CrmEtaServerUrl == "" {
  136. return
  137. }
  138. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/eta/classify/list")
  139. b, err := crmEtaPost(url, req)
  140. if err != nil {
  141. err = fmt.Errorf("url:%s err: %s", url, err.Error())
  142. return
  143. }
  144. //result := new(models.ResultData)
  145. result := new(GetClassifyListResp)
  146. if e := json.Unmarshal(b, &result); e != nil {
  147. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  148. return
  149. }
  150. utils.FileLog.Info("%s", string(b))
  151. if result.Code != 200 {
  152. err = fmt.Errorf("result: %s", string(b))
  153. return
  154. }
  155. list = result.Data
  156. return
  157. }
  158. type ClassifyPermissionReq struct {
  159. Keyword string
  160. }
  161. type ClassifyPermissionList struct {
  162. List []*models.ChartPermissionSearchKeyWordMapping
  163. }
  164. type GetClassifyListResp struct {
  165. Code int `json:"code" description:"状态码"`
  166. Msg string `json:"msg" description:"提示信息"`
  167. Data models.ClassifyListResp `json:"data" description:"返回数据"`
  168. ErrMsg string `json:"-" description:"错误信息,不用返回给前端,只是做日志记录"`
  169. }
  170. // GetParentClassifyListByParentIdList
  171. // @Description: 递归获取父级分类信息,正常来讲只有三次
  172. // @author: Roc
  173. // @datetime 2024-06-19 13:23:33
  174. // @param parentClassifyIdList []int
  175. // @return list []*models.ClassifyList
  176. // @return err error
  177. func GetParentClassifyListByParentIdList(parentClassifyIdList []int) (list []*models.Classify, err error) {
  178. num := len(parentClassifyIdList)
  179. if num <= 0 {
  180. return
  181. }
  182. list, err = models.GetClassifyListByParentIdList(parentClassifyIdList)
  183. if err != nil {
  184. return
  185. }
  186. // 是否还有上级
  187. {
  188. currParentClassifyIdList := make([]int, 0)
  189. for _, v := range list {
  190. if v.ParentId > 0 {
  191. currParentClassifyIdList = append(currParentClassifyIdList, v.ParentId)
  192. }
  193. }
  194. if len(currParentClassifyIdList) > 0 {
  195. tmpList, tmpErr := GetParentClassifyListByParentIdList(currParentClassifyIdList)
  196. if tmpErr != nil {
  197. err = tmpErr
  198. return
  199. }
  200. list = append(tmpList, list...)
  201. }
  202. }
  203. return
  204. }
  205. // GetClassifyListTreeRecursive
  206. // @Description: 递归获取分类树形结构
  207. // @author: Roc
  208. // @datetime 2024-06-19 13:23:28
  209. // @param list []*models.ClassifyList
  210. // @param parentId int
  211. // @return []*models.ClassifyList
  212. func GetClassifyListTreeRecursive(list []*models.SimpleClassifyList, parentId int) []*models.SimpleClassifyList {
  213. res := make([]*models.SimpleClassifyList, 0)
  214. for _, v := range list {
  215. if v.ParentId == parentId {
  216. v.Child = GetClassifyListTreeRecursive(list, v.Id)
  217. res = append(res, v)
  218. }
  219. }
  220. // 前端的JP需要我这么返回
  221. if len(res) <= 0 {
  222. res = nil
  223. }
  224. return res
  225. }
  226. // BySortAndCreateTime 用来排序,先按Sort字段升序排序,若Sort相同,则按照CreateTime字段升序排序。
  227. type BySortAndCreateTime []*models.SimpleClassifyList
  228. func (a BySortAndCreateTime) Len() int {
  229. return len(a)
  230. }
  231. func (a BySortAndCreateTime) Swap(i, j int) {
  232. a[i], a[j] = a[j], a[i]
  233. }
  234. func (a BySortAndCreateTime) Less(i, j int) bool {
  235. if a[i].Sort == a[j].Sort {
  236. return a[i].CreateTime.Before(a[j].CreateTime)
  237. }
  238. return a[i].Sort < a[j].Sort
  239. }
  240. // SortClassifyListBySortAndCreateTime sorts the ClassifyList slice by Sort and then CreateTime in ascending order.
  241. func SortClassifyListBySortAndCreateTime(classifyList []*models.SimpleClassifyList) {
  242. sort.Sort(BySortAndCreateTime(classifyList))
  243. }