classify.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. ReportDetailShowType int `description:"报告详情的展示类型:1-拼接;2:目录"`
  51. }
  52. // ClassifyMenuSaveReq 保存分类子目录请求体
  53. type ClassifyMenuSaveReq struct {
  54. MenuId int `description:"子目录ID, 0为新增, 大于0为编辑"`
  55. MenuName string `description:"子目录名称"`
  56. }
  57. type CrmEtaBaseResp struct {
  58. Code int `json:"code" description:"状态码"`
  59. Msg string `json:"msg" description:"提示信息"`
  60. ErrMsg string `json:"-" description:"错误信息,不用返回给前端,只是做日志记录"`
  61. }
  62. func crmEtaPost(url string, param interface{}) (respBody []byte, err error) {
  63. data, e := json.Marshal(param)
  64. if e != nil {
  65. err = fmt.Errorf("data json marshal err: %s", e.Error())
  66. return
  67. }
  68. body := ioutil.NopCloser(strings.NewReader(string(data)))
  69. client := &http.Client{}
  70. req, e := http.NewRequest("POST", url, body)
  71. if e != nil {
  72. err = fmt.Errorf("http create request err: %s", e.Error())
  73. return
  74. }
  75. contentType := "application/json;charset=utf-8"
  76. req.Header.Set("Content-Type", contentType)
  77. req.Header.Set("Authorization", utils.CrmEtaAuthorization)
  78. resp, e := client.Do(req)
  79. if e != nil {
  80. err = fmt.Errorf("http client do err: %s", e.Error())
  81. return
  82. }
  83. defer func() {
  84. _ = resp.Body.Close()
  85. }()
  86. b, e := ioutil.ReadAll(resp.Body)
  87. if e != nil {
  88. err = fmt.Errorf("resp body read err: %s", e.Error())
  89. return
  90. }
  91. if len(b) == 0 {
  92. err = fmt.Errorf("resp body is empty")
  93. return
  94. }
  95. // 生产环境解密, 注意有个坑前后的双引号
  96. if utils.RunMode == "release" {
  97. str := string(b)
  98. str = strings.Trim(str, `"`)
  99. b = utils.DesBase64Decrypt([]byte(str))
  100. }
  101. respBody = b
  102. return
  103. }
  104. func EditReportClassify(pars *EditClassifyReq) (err error, errMsg string) {
  105. if utils.CrmEtaServerUrl == "" {
  106. return
  107. }
  108. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/eta/classify/edit")
  109. b, err := crmEtaPost(url, pars)
  110. if err != nil {
  111. errMsg = "更新品种失败"
  112. err = fmt.Errorf("url:%s err: %s", url, err.Error())
  113. return
  114. }
  115. result := new(CrmEtaBaseResp)
  116. if e := json.Unmarshal(b, &result); e != nil {
  117. errMsg = "更新分类失败"
  118. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  119. return
  120. }
  121. utils.FileLog.Info("%s", string(b))
  122. if result.Code != 200 {
  123. err = fmt.Errorf("result: %s, err: %s", string(b), result.ErrMsg)
  124. errMsg = result.Msg
  125. return
  126. }
  127. return
  128. }
  129. type EditClassifyPermissionReq struct {
  130. Keyword string
  131. ChartPermissionIdList []int `description:"权限id数组"`
  132. NewKeyword string
  133. }
  134. // GetClassifyList 获取报告分类已绑定的权限
  135. func GetClassifyList(req *GetClassifyListReq) (list models.ClassifyListResp, err error) {
  136. if utils.CrmEtaServerUrl == "" {
  137. return
  138. }
  139. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/eta/classify/list")
  140. b, err := crmEtaPost(url, req)
  141. if err != nil {
  142. err = fmt.Errorf("url:%s err: %s", url, err.Error())
  143. return
  144. }
  145. //result := new(models.ResultData)
  146. result := new(GetClassifyListResp)
  147. if e := json.Unmarshal(b, &result); e != nil {
  148. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  149. return
  150. }
  151. utils.FileLog.Info("%s", string(b))
  152. if result.Code != 200 {
  153. err = fmt.Errorf("result: %s", string(b))
  154. return
  155. }
  156. list = result.Data
  157. return
  158. }
  159. type ClassifyPermissionReq struct {
  160. Keyword string
  161. }
  162. type ClassifyPermissionList struct {
  163. List []*models.ChartPermissionSearchKeyWordMapping
  164. }
  165. type GetClassifyListResp struct {
  166. Code int `json:"code" description:"状态码"`
  167. Msg string `json:"msg" description:"提示信息"`
  168. Data models.ClassifyListResp `json:"data" description:"返回数据"`
  169. ErrMsg string `json:"-" description:"错误信息,不用返回给前端,只是做日志记录"`
  170. }
  171. // GetParentClassifyListByParentIdList
  172. // @Description: 递归获取父级分类信息,正常来讲只有三次
  173. // @author: Roc
  174. // @datetime 2024-06-19 13:23:33
  175. // @param parentClassifyIdList []int
  176. // @return list []*models.ClassifyList
  177. // @return err error
  178. func GetParentClassifyListByParentIdList(parentClassifyIdList []int) (list []*models.Classify, err error) {
  179. num := len(parentClassifyIdList)
  180. if num <= 0 {
  181. return
  182. }
  183. list, err = models.GetClassifyListByParentIdList(parentClassifyIdList)
  184. if err != nil {
  185. return
  186. }
  187. // 是否还有上级
  188. {
  189. currParentClassifyIdList := make([]int, 0)
  190. for _, v := range list {
  191. if v.ParentId > 0 {
  192. currParentClassifyIdList = append(currParentClassifyIdList, v.ParentId)
  193. }
  194. }
  195. if len(currParentClassifyIdList) > 0 {
  196. tmpList, tmpErr := GetParentClassifyListByParentIdList(currParentClassifyIdList)
  197. if tmpErr != nil {
  198. err = tmpErr
  199. return
  200. }
  201. list = append(tmpList, list...)
  202. }
  203. }
  204. return
  205. }
  206. // GetClassifyListTreeRecursive
  207. // @Description: 递归获取分类树形结构
  208. // @author: Roc
  209. // @datetime 2024-06-19 13:23:28
  210. // @param list []*models.ClassifyList
  211. // @param parentId int
  212. // @return []*models.ClassifyList
  213. func GetClassifyListTreeRecursive(list []*models.SimpleClassifyList, parentId int) []*models.SimpleClassifyList {
  214. res := make([]*models.SimpleClassifyList, 0)
  215. for _, v := range list {
  216. if v.ParentId == parentId {
  217. v.Child = GetClassifyListTreeRecursive(list, v.Id)
  218. res = append(res, v)
  219. }
  220. }
  221. // 前端的JP需要我这么返回
  222. if len(res) <= 0 {
  223. res = nil
  224. }
  225. return res
  226. }
  227. // BySortAndCreateTime 用来排序,先按Sort字段升序排序,若Sort相同,则按照CreateTime字段升序排序。
  228. type BySortAndCreateTime []*models.SimpleClassifyList
  229. func (a BySortAndCreateTime) Len() int {
  230. return len(a)
  231. }
  232. func (a BySortAndCreateTime) Swap(i, j int) {
  233. a[i], a[j] = a[j], a[i]
  234. }
  235. func (a BySortAndCreateTime) Less(i, j int) bool {
  236. if a[i].Sort == a[j].Sort {
  237. return a[i].CreateTime.Before(a[j].CreateTime)
  238. }
  239. return a[i].Sort < a[j].Sort
  240. }
  241. // SortClassifyListBySortAndCreateTime sorts the ClassifyList slice by Sort and then CreateTime in ascending order.
  242. func SortClassifyListBySortAndCreateTime(classifyList []*models.SimpleClassifyList) {
  243. sort.Sort(BySortAndCreateTime(classifyList))
  244. }