official_user.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. "github.com/tealeg/xlsx"
  6. "hongze/hz_crm_api/models"
  7. "hongze/hz_crm_api/utils"
  8. "os"
  9. "path/filepath"
  10. "time"
  11. )
  12. // 官网试用用户管理
  13. type OfficialUserController struct {
  14. BaseAuthController
  15. }
  16. // @Title 官网试用用户列表
  17. // @Description 官网试用用户列表接口
  18. // @Param PageSize query int true "每页数据条数"
  19. // @Param CurrentIndex query int true "当前页页码,从1开始"
  20. // @Param KeyWord query string true "搜索关键词"
  21. // @Param SourceType query string true "申请来源,枚举值:可选范围:'中文官网','英文官网'"
  22. // @Success 200 {object} models.UserTrialApplyListResp
  23. // @router /official/user/list [get]
  24. func (this *OfficialUserController) OfficialUserList() {
  25. br := new(models.BaseResponse).Init()
  26. defer func() {
  27. this.Data["json"] = br
  28. this.ServeJSON()
  29. }()
  30. sysUser := this.SysUser
  31. if sysUser == nil {
  32. br.Msg = "请登录"
  33. br.ErrMsg = "请登录,SysUser Is Empty"
  34. br.Ret = 408
  35. return
  36. }
  37. pageSize, _ := this.GetInt("PageSize")
  38. currentIndex, _ := this.GetInt("CurrentIndex")
  39. keyWord := this.GetString("KeyWord")
  40. sourceType := this.GetString("SourceType")
  41. if sourceType == "" {
  42. br.Msg = "获取失败"
  43. br.ErrMsg = "请选择类型"
  44. return
  45. }
  46. var startSize int
  47. if pageSize <= 0 {
  48. pageSize = utils.PageSize20
  49. }
  50. if currentIndex <= 0 {
  51. currentIndex = 1
  52. }
  53. startSize = utils.StartIndex(currentIndex, pageSize)
  54. var condition string
  55. var pars []interface{}
  56. startTime := "2022-01-01"
  57. condition += `source_type = ? and create_time > ? `
  58. pars = append(pars, sourceType)
  59. pars = append(pars, startTime)
  60. if keyWord != "" {
  61. condition += ` and (company_name LIKE '%` + keyWord + `%' OR phone LIKE '%` + keyWord + `%' OR email LIKE '%` + keyWord + `%' OR real_name LIKE '%` + keyWord + `%') `
  62. }
  63. //获取总数据数
  64. total, err := models.GetOfficialApplyUserListListCount(condition, pars)
  65. if err != nil {
  66. br.Msg = "获取失败"
  67. br.ErrMsg = "获取数据总数失败,Err:" + err.Error()
  68. return
  69. }
  70. list, err := models.GetOfficialApplyUserListList(condition, pars, startSize, pageSize)
  71. if err != nil {
  72. br.Msg = "获取失败"
  73. br.ErrMsg = "获取失败,Err:" + err.Error()
  74. return
  75. }
  76. lenList := len(list)
  77. for i := 0; i < lenList; i++ {
  78. list[i].CreateTimeStr = list[i].CreateTime.Format(utils.FormatDateTime)
  79. list[i].ModifyTimeStr = list[i].ModifyTime.Format(utils.FormatDateTime)
  80. }
  81. page := paging.GetPaging(currentIndex, pageSize, int(total))
  82. resp := new(models.UserTrialApplyListResp)
  83. resp.List = list
  84. resp.Paging = page
  85. br.Ret = 200
  86. br.Success = true
  87. br.Msg = "获取成功"
  88. br.Data = resp
  89. }
  90. // @Title 官网试用用户确认
  91. // @Description 官网试用用户确认接口
  92. // @Param Id query int true "申请记录id"
  93. // @Success Ret=200 确认成功
  94. // @router /official/user/confirm [post]
  95. func (this *OfficialUserController) OfficialUserConfirm() {
  96. br := new(models.BaseResponse).Init()
  97. defer func() {
  98. this.Data["json"] = br
  99. this.ServeJSON()
  100. }()
  101. sysUser := this.SysUser
  102. if sysUser == nil {
  103. br.Msg = "请登录"
  104. br.ErrMsg = "请登录,SysUser Is Empty"
  105. br.Ret = 408
  106. return
  107. }
  108. //参数拼接
  109. var req models.UserTrialApplyConfirmReq
  110. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  111. id := req.Id
  112. //查询记录信息,并做初步判断
  113. info, err := models.GetOfficialApplyUserById(id)
  114. if err != nil && err.Error() == utils.ErrNoRow() {
  115. br.Msg = "确认失败"
  116. br.ErrMsg = "确认失败,记录异常"
  117. return
  118. }
  119. if info.Status != "待处理" {
  120. br.Msg = "确认失败"
  121. br.ErrMsg = "确认失败,当前记录状态信息异常,请刷新页面"
  122. return
  123. }
  124. err = models.ConfirmOfficialApplyUser(id, sysUser.AdminId, sysUser.RealName)
  125. if err != nil {
  126. br.Msg = "确认失败"
  127. br.ErrMsg = "确认失败,Err:" + err.Error()
  128. return
  129. }
  130. br.Ret = 200
  131. br.Success = true
  132. br.Msg = "确认成功"
  133. //br.Data = resp
  134. }
  135. // @Title 官网试用用户列表导出
  136. // @Description 官网试用用户列表接口
  137. // @Param PageSize query int true "每页数据条数"
  138. // @Param CurrentIndex query int true "当前页页码,从1开始"
  139. // @Param KeyWord query string true "搜索关键词"
  140. // @Param SourceType query string true "申请来源,枚举值:可选范围:'中文官网','英文官网'"
  141. // @Success Ret=200 导出成功
  142. // @router /official/user/list/export [get]
  143. func (this *OfficialUserController) OfficialUserListExport() {
  144. br := new(models.BaseResponse).Init()
  145. defer func() {
  146. this.Data["json"] = br
  147. this.ServeJSON()
  148. }()
  149. sysUser := this.SysUser
  150. if sysUser == nil {
  151. br.Msg = "请登录"
  152. br.ErrMsg = "请登录,SysUser Is Empty"
  153. br.Ret = 408
  154. return
  155. }
  156. keyWord := this.GetString("KeyWord")
  157. sourceType := this.GetString("SourceType")
  158. if sourceType == "" {
  159. br.Msg = "获取失败"
  160. br.ErrMsg = "请选择类型"
  161. return
  162. }
  163. var condition string
  164. var pars []interface{}
  165. //管理员才让导出数据
  166. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  167. br.Msg = "没有权限"
  168. br.ErrMsg = "没有权限"
  169. return
  170. }
  171. startTime := "2022-01-01"
  172. condition += `source_type = ? and create_time > ? `
  173. pars = append(pars, sourceType)
  174. pars = append(pars, startTime)
  175. if keyWord != "" {
  176. condition += ` and (company_name LIKE '%` + keyWord + `%' OR phone LIKE '%` + keyWord + `%' OR email LIKE '%` + keyWord + `%' OR real_name LIKE '%` + keyWord + `%') `
  177. }
  178. list, err := models.GetOfficialApplyUserListListExport(condition, pars)
  179. if err != nil {
  180. br.Msg = "获取失败"
  181. br.ErrMsg = "获取失败,Err:" + err.Error()
  182. return
  183. }
  184. dir, err := os.Executable()
  185. exPath := filepath.Dir(dir)
  186. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  187. xlsxFile := xlsx.NewFile()
  188. if err != nil {
  189. br.Msg = "生成文件失败"
  190. br.ErrMsg = "生成文件失败"
  191. return
  192. }
  193. style := xlsx.NewStyle()
  194. alignment := xlsx.Alignment{
  195. Horizontal: "center",
  196. Vertical: "center",
  197. WrapText: true,
  198. }
  199. style.Alignment = alignment
  200. style.ApplyAlignment = true
  201. sheel, err := xlsxFile.AddSheet("客户数据")
  202. if err != nil {
  203. br.Msg = "新增Sheet失败"
  204. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  205. return
  206. }
  207. titleRow := sheel.AddRow()
  208. if sourceType == "中文官网" {
  209. titleRow.AddCell().SetValue("企业名称")
  210. titleRow.AddCell().SetValue("姓名")
  211. titleRow.AddCell().SetValue("手机号/邮箱")
  212. titleRow.AddCell().SetValue("所属行业")
  213. titleRow.AddCell().SetValue("申请时间")
  214. titleRow.AddCell().SetValue("申请品种")
  215. for _, v := range list {
  216. dataRow := sheel.AddRow()
  217. dataRow.SetHeight(20)
  218. dataRow.AddCell().SetString(v.CompanyName)
  219. dataRow.AddCell().SetString(v.RealName)
  220. if v.Phone != "" {
  221. dataRow.AddCell().SetString(v.Phone)
  222. } else {
  223. dataRow.AddCell().SetString(v.Email)
  224. }
  225. dataRow.AddCell().SetString(v.Industry)
  226. dataRow.AddCell().SetString(v.CreateTime.Format(utils.FormatDateTime))
  227. dataRow.AddCell().SetString(v.ProductInfo)
  228. }
  229. } else {
  230. titleRow.AddCell().SetValue("企业名称")
  231. titleRow.AddCell().SetValue("姓名")
  232. titleRow.AddCell().SetValue("邮箱")
  233. titleRow.AddCell().SetValue("信息")
  234. titleRow.AddCell().SetValue("发送时间")
  235. for _, v := range list {
  236. dataRow := sheel.AddRow()
  237. dataRow.SetHeight(20)
  238. dataRow.AddCell().SetString(v.CompanyName)
  239. dataRow.AddCell().SetString(v.RealName)
  240. dataRow.AddCell().SetString(v.Email)
  241. dataRow.AddCell().SetString(v.Message)
  242. dataRow.AddCell().SetString(v.CreateTime.Format(utils.FormatDateTime))
  243. }
  244. }
  245. err = xlsxFile.Save(downLoadnFilePath)
  246. if err != nil {
  247. br.Msg = "保存文件失败"
  248. br.ErrMsg = "保存文件失败"
  249. return
  250. }
  251. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  252. downloadFileName := "官网试用申请列表_" + sourceType + "_" + randStr + ".xlsx"
  253. this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  254. defer func() {
  255. _ = os.Remove(downLoadnFilePath)
  256. }()
  257. br.Ret = 200
  258. br.Success = true
  259. br.Msg = "导出成功"
  260. }