search_keyword.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package cygx
  2. import (
  3. "github.com/rdlucklib/rdluck_tools/paging"
  4. "github.com/tealeg/xlsx"
  5. "hongze/hz_crm_api/controllers"
  6. "hongze/hz_crm_api/models"
  7. "hongze/hz_crm_api/models/cygx"
  8. cygxService "hongze/hz_crm_api/services/cygx"
  9. "hongze/hz_crm_api/utils"
  10. "os"
  11. "path/filepath"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. // 报告管理
  17. type SearchKeywordController struct {
  18. controllers.BaseAuthController
  19. }
  20. // @Title 7天热词
  21. // @Description 获取7天热词列表接口
  22. // @Param PageSize query int true "每页数据条数"
  23. // @Param CurrentIndex query int true "当前页页码,从1开始"
  24. // @Success 200 {object} cygx.CygxReportMappingListRep
  25. // @router /hostKeyword/list [get]
  26. func (this *SearchKeywordController) HostKeywordList() {
  27. br := new(models.BaseResponse).Init()
  28. defer func() {
  29. this.Data["json"] = br
  30. this.ServeJSON()
  31. }()
  32. sysUser := this.SysUser
  33. if sysUser == nil {
  34. br.Msg = "请登录"
  35. br.ErrMsg = "请登录,SysUser Is Empty"
  36. return
  37. }
  38. pageSize, _ := this.GetInt("PageSize")
  39. currentIndex, _ := this.GetInt("CurrentIndex")
  40. var startSize int
  41. if pageSize <= 0 {
  42. pageSize = utils.PageSize20
  43. }
  44. if currentIndex <= 0 {
  45. currentIndex = 1
  46. }
  47. startSize = utils.StartIndex(currentIndex, pageSize)
  48. var condition string
  49. var pars []interface{}
  50. currentTime := time.Now()
  51. starTime := currentTime.AddDate(0, 0, -8).Format("2006-01-02") + " 00:00:00"
  52. endTime := currentTime.AddDate(0, 0, -1).Format("2006-01-02") + " 23:59:59"
  53. condition += ` AND create_time < ` + "'" + endTime + "'" + `AND create_time > ` + "'" + starTime + "'"
  54. total, err := cygx.GetSearchKeyWordCount(condition, pars)
  55. if err != nil {
  56. br.Msg = "获取失败"
  57. br.ErrMsg = "获取失败,Err:" + err.Error()
  58. return
  59. }
  60. if total > 60 {
  61. total = 60
  62. }
  63. list, err := cygx.GetSearchKeyWordList(condition, pars, startSize, pageSize)
  64. if err != nil {
  65. br.Msg = "获取失败"
  66. br.ErrMsg = "获取失败,Err:" + err.Error()
  67. return
  68. }
  69. for k := range list {
  70. list[k].Sort = (currentIndex-1)*pageSize + k + 1
  71. }
  72. page := paging.GetPaging(currentIndex, pageSize, total)
  73. resp := new(cygx.SearchKeyWordListResp)
  74. resp.Paging = page
  75. resp.List = list
  76. br.Ret = 200
  77. br.Success = true
  78. br.Msg = "获取成功"
  79. br.Data = resp
  80. }
  81. // @Title 7天热词下载明细
  82. // @Description 获取7天热词下载明细列表接口
  83. // @Success Ret=200
  84. // @router /hostKeyword/download [get]
  85. func (this *SearchKeywordController) HostKeywordDownload() {
  86. br := new(models.BaseResponse).Init()
  87. defer func() {
  88. this.Data["json"] = br
  89. this.ServeJSON()
  90. }()
  91. sysUser := this.SysUser
  92. if sysUser == nil {
  93. br.Msg = "请登录"
  94. br.ErrMsg = "请登录,SysUser Is Empty"
  95. return
  96. }
  97. resp := new(cygx.CanDownload)
  98. if sysUser.Role == "admin" || sysUser.Role == "researcher" {
  99. resp.IsCanDownload = true
  100. }
  101. var pars []interface{}
  102. currentTime := time.Now()
  103. starTime := currentTime.AddDate(0, 0, -7).Format("2006-01-02") + " 00:00:00"
  104. endTime := currentTime.AddDate(0, 0, -1).Format("2006-01-02") + " 23:59:59"
  105. var list []*cygx.KeyWordListDownload
  106. var err error
  107. listAdmin, err := cygx.GetSearchKeyWordListDownload(starTime, endTime)
  108. if err != nil {
  109. br.Msg = "获取失败"
  110. br.ErrMsg = "获取失败,Err:" + err.Error()
  111. return
  112. }
  113. if resp.IsCanDownload == false {
  114. mapMobile, err := cygxService.GetAdminLookUserMobile(sysUser)
  115. if err != nil {
  116. br.Msg = "获取失败"
  117. br.ErrMsg = "获取失败,销售对应权限,Err:" + err.Error()
  118. return
  119. }
  120. for _, v := range listAdmin {
  121. if _, ok := mapMobile[v.Mobile]; ok {
  122. list = append(list, v)
  123. }
  124. }
  125. } else {
  126. list = listAdmin
  127. }
  128. listComPany, err := cygx.GetCompanyProductMoreOneList(starTime, endTime)
  129. var strId string
  130. for _, v := range listComPany {
  131. strId += strconv.Itoa(v.CompanyId) + ","
  132. }
  133. companyProductNameAndSellerList, err := cygx.GetCompanyProductNameAndSellerAllList(pars, strings.TrimRight(strId, ","))
  134. if err != nil {
  135. br.Msg = "获取失败"
  136. br.ErrMsg = "获取失败,Err:" + err.Error()
  137. return
  138. }
  139. for k, v := range list {
  140. if v.Mobile == "" {
  141. list[k].Mobile = v.Email
  142. }
  143. var productNamestr string
  144. var sellerNamestr string
  145. for _, v2 := range companyProductNameAndSellerList {
  146. if v.CompanyId == v2.CompanyId {
  147. productNamestr += v2.ProductName + "/"
  148. sellerNamestr += v2.SellerName + "/"
  149. }
  150. }
  151. list[k].ProductName = strings.TrimRight(productNamestr, "/")
  152. list[k].SellerName = strings.TrimRight(sellerNamestr, "/")
  153. }
  154. //创建excel
  155. dir, err := os.Executable()
  156. exPath := filepath.Dir(dir)
  157. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  158. xlsxFile := xlsx.NewFile()
  159. if err != nil {
  160. br.Msg = "生成文件失败"
  161. br.ErrMsg = "生成文件失败"
  162. return
  163. }
  164. style := xlsx.NewStyle()
  165. alignment := xlsx.Alignment{
  166. Horizontal: "center",
  167. Vertical: "center",
  168. WrapText: true,
  169. }
  170. style.Alignment = alignment
  171. style.ApplyAlignment = true
  172. sheet, err := xlsxFile.AddSheet("关键词搜索统计")
  173. if err != nil {
  174. br.Msg = "新增Sheet失败"
  175. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  176. return
  177. }
  178. //标头
  179. rowTitle := sheet.AddRow()
  180. cellA := rowTitle.AddCell()
  181. cellA.Value = "搜索词"
  182. cellB := rowTitle.AddCell()
  183. cellB.Value = "手机号/邮箱"
  184. cellC := rowTitle.AddCell()
  185. cellC.Value = "姓名"
  186. cellD := rowTitle.AddCell()
  187. cellD.Value = "公司名"
  188. cellE := rowTitle.AddCell()
  189. cellE.Value = "客户类型"
  190. cellF := rowTitle.AddCell()
  191. cellF.Value = "所属销售"
  192. cellG := rowTitle.AddCell()
  193. cellG.Value = "搜索时间"
  194. for _, item := range list {
  195. row := sheet.AddRow()
  196. cellA := row.AddCell()
  197. cellA.Value = item.KeyWord
  198. cellB := row.AddCell()
  199. cellB.Value = item.Mobile
  200. cellC := row.AddCell()
  201. cellC.Value = item.RealName
  202. cellD := row.AddCell()
  203. cellD.Value = item.CompanyName
  204. cellE := row.AddCell()
  205. cellE.Value = item.ProductName
  206. cellF := row.AddCell()
  207. cellF.Value = item.SellerName
  208. cellG := row.AddCell()
  209. cellG.Value = item.CreateTime
  210. }
  211. err = xlsxFile.Save(downLoadnFilePath)
  212. if err != nil {
  213. br.Msg = "保存文件失败"
  214. br.ErrMsg = "保存文件失败"
  215. return
  216. }
  217. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  218. downloadFileName := "关键词搜索统计列表" + randStr + ".xlsx"
  219. this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  220. defer func() {
  221. os.Remove(downLoadnFilePath)
  222. }()
  223. br.Ret = 200
  224. br.Success = true
  225. br.Msg = "导出成功"
  226. }
  227. // @Title 是否可以下载
  228. // @Description 获取是否可以下载接口
  229. // @Success Ret=200 {object} cygx.CanDownload
  230. // @router /hostKeyword/canDownload [get]
  231. func (this *SearchKeywordController) CnaDownload() {
  232. br := new(models.BaseResponse).Init()
  233. defer func() {
  234. this.Data["json"] = br
  235. this.ServeJSON()
  236. }()
  237. sysUser := this.SysUser
  238. if sysUser == nil {
  239. br.Msg = "请登录"
  240. br.ErrMsg = "请登录,SysUser Is Empty"
  241. return
  242. }
  243. resp := new(cygx.CanDownload)
  244. if sysUser.Authority == 1 || sysUser.Authority == 3 {
  245. resp.IsCanDownload = true
  246. }
  247. if sysUser.Role == "admin" {
  248. resp.IsCanDownload = true
  249. }
  250. if sysUser.RoleTypeCode == "rai_admin" || sysUser.RoleTypeCode == "ficc_admin" || sysUser.RoleTypeCode == "admin" {
  251. resp.IsCanDownload = true
  252. }
  253. br.Ret = 200
  254. br.Success = true
  255. br.Msg = "获取成功"
  256. br.Data = resp
  257. }