analyst.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_crm_ht/models"
  5. "eta/eta_mini_crm_ht/models/request"
  6. "eta/eta_mini_crm_ht/models/response"
  7. "eta/eta_mini_crm_ht/services"
  8. "eta/eta_mini_crm_ht/utils"
  9. "fmt"
  10. "github.com/rdlucklib/rdluck_tools/paging"
  11. "os"
  12. "path"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. const ()
  18. type AnalystController struct {
  19. BaseAuthController
  20. }
  21. // UploadImage
  22. // @Title 上传头像
  23. // @Description 上传头像
  24. // @Param File query file true "文件"
  25. // @Success 200 {object} models.ReportAuthorResp
  26. // @router /uploadImage [post]
  27. func (this *AnalystController) UploadImage() {
  28. br := new(models.BaseResponse).Init()
  29. defer func() {
  30. this.Data["json"] = br
  31. this.ServeJSON()
  32. }()
  33. f, h, err := this.GetFile("File")
  34. if err != nil {
  35. br.Msg = "获取资源信息失败"
  36. br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
  37. return
  38. }
  39. defer f.Close()
  40. ext := path.Ext(h.Filename)
  41. //if ext != ".pdf" {
  42. // br.Msg = "文件格式不正确"
  43. // return
  44. //}
  45. size, err := strconv.Atoi(utils.UPLOAD_IMG_SIZE)
  46. if err != nil {
  47. size = 15
  48. }
  49. if h.Size > 1024*int64(size) {
  50. br.Msg = fmt.Sprintf("头像文件大小不能超过%dK", size)
  51. return
  52. }
  53. dateDir := time.Now().Format("20060102")
  54. uploadDir := utils.STATIC_DIR + "ht/" + dateDir
  55. err = os.MkdirAll(uploadDir, utils.DIR_MOD)
  56. if err != nil {
  57. br.Msg = "存储目录创建失败"
  58. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  59. return
  60. }
  61. randStr := utils.GetRandStringNoSpecialChar(28)
  62. fileName := randStr + ext
  63. fpath := uploadDir + "/" + fileName
  64. err = this.SaveToFile("File", fpath)
  65. if err != nil {
  66. br.Msg = "头像上传失败"
  67. br.ErrMsg = "头像上传失败,Err:" + err.Error()
  68. return
  69. }
  70. pdfUploadDir := utils.RESOURCE_DIR + "analyst/"
  71. savePdfToOssPath := pdfUploadDir + time.Now().Format("200601/20060102/")
  72. imgName := utils.GetRandStringNoSpecialChar(28)
  73. savePdfToOssPath += imgName
  74. defer func() {
  75. _ = os.Remove(fpath)
  76. }()
  77. ossClient := services.NewOssClient()
  78. if ossClient == nil {
  79. br.Msg = "头像上传失败"
  80. br.ErrMsg = "初始化OSS服务失败"
  81. return
  82. }
  83. imgUrl, err := ossClient.UploadFile("", fpath, savePdfToOssPath)
  84. if err != nil {
  85. br.Msg = "头像上传失败"
  86. br.ErrMsg = "头像上传失败,Err:" + err.Error()
  87. return
  88. }
  89. base := path.Base(h.Filename)
  90. resp := new(response.ReportPdfUploadResp)
  91. resp.Url = imgUrl
  92. resp.FileName = base
  93. br.Data = resp
  94. br.Msg = "上传成功"
  95. br.Ret = 200
  96. br.Success = true
  97. }
  98. // edit
  99. // @Title 编辑客户
  100. // @Description 编辑客户
  101. // @Param request body request.UserEidtReq true "type json string"
  102. // @Success 200 {object} request.UserAddReq
  103. // @router /edit [post]
  104. func (this *AnalystController) Edit() {
  105. br := new(models.BaseResponse).Init()
  106. defer func() {
  107. this.Data["json"] = br
  108. this.ServeJSON()
  109. }()
  110. var req request.AnalystEditReq
  111. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  112. if err != nil {
  113. br.Msg = "参数解析异常!"
  114. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  115. return
  116. }
  117. if req.Id <= 0 {
  118. br.Msg = "请选择用户"
  119. return
  120. }
  121. if req.HeadImgUrl == "" {
  122. br.Msg = "请输入姓名"
  123. return
  124. }
  125. req.Introduction = strings.TrimSpace(req.Introduction)
  126. user, err := models.GetUserById(req.UserId)
  127. if err != nil {
  128. if err.Error() == utils.ErrNoRow() {
  129. br.Msg = "用户不存在或已删除,请重新刷新页面"
  130. br.ErrMsg = "用户不存在或已删除,请重新刷新页面,Err:" + err.Error()
  131. return
  132. }
  133. br.Msg = "编辑用户失败"
  134. br.ErrMsg = "编辑用户失败, 系统错误,Err:" + err.Error()
  135. return
  136. }
  137. user.RealName = req.RealName
  138. user.AreaCode = req.AreaCode
  139. user.Phone = req.Phone
  140. user.Email = req.Email
  141. user.SellerId = req.SellerId
  142. user.ValidStartTime = validStartTime
  143. user.ValidEndTime = validEndTime
  144. user.Company = req.Company
  145. user.ModifyTime = time.Now()
  146. // 当用户状态初始为潜在客户时,才更新创建时间
  147. if user.Status == utils.UserStatusPotential {
  148. user.CreateTime = time.Now()
  149. }
  150. oldStatus := user.Status
  151. if !req.IsEnabled {
  152. user.Status = 0
  153. } else {
  154. user.Status = 2
  155. }
  156. err = models.SaveUser(user, req.ChartPermission)
  157. if err != nil {
  158. br.Msg = "添加客户失败"
  159. br.ErrMsg = "添加客户失败,系统错误,Err:" + err.Error()
  160. return
  161. }
  162. record := &models.UserChangeRecord{}
  163. record.UserId = req.UserId
  164. record.SysUserId = this.SysUser.SysUserId
  165. if oldStatus == utils.UserStatusPotential && user.Status == utils.UserStatusFormal {
  166. record.Content = this.SysUser.SysRealName + "新增用户"
  167. } else {
  168. record.Content = this.SysUser.SysRealName + "编辑用户"
  169. }
  170. record.Insert()
  171. br.Msg = "编辑成功"
  172. br.Success = true
  173. br.Ret = 200
  174. }
  175. // GetAnalystByName
  176. // @Title 查询研究员信息
  177. // @Description 查询研究员信息
  178. // @Param File query file true "文件"
  179. // @Success 200 {object} models.ReportAuthorResp
  180. // @router /analyst [get]
  181. func (this *AnalystController) GetAnalystByName(AnalystName string) {
  182. br := new(models.BaseResponse).Init()
  183. defer func() {
  184. this.Data["json"] = br
  185. this.ServeJSON()
  186. }()
  187. analyst, err := models.GetAnalystByName(AnalystName)
  188. if err != nil {
  189. br.Msg = "获取失败"
  190. br.ErrMsg = "获取失败,Err:" + err.Error()
  191. return
  192. }
  193. br.Ret = 200
  194. br.Success = true
  195. br.Msg = "获取成功"
  196. br.Data = analyst
  197. }
  198. // List
  199. // @Title 列表
  200. // @Description 列表
  201. // @Param File query file true "文件"
  202. // @Success 200 {object} models.ReportAuthorResp
  203. // @router /list [get]
  204. func (this *AnalystController) List() {
  205. br := new(models.BaseResponse).Init()
  206. defer func() {
  207. this.Data["json"] = br
  208. this.ServeJSON()
  209. }()
  210. pageSize, _ := this.GetInt("PageSize")
  211. currentIndex, _ := this.GetInt("CurrentIndex")
  212. var startSize int
  213. if pageSize <= 0 {
  214. pageSize = utils.PageSize20
  215. }
  216. if currentIndex <= 0 {
  217. currentIndex = 1
  218. }
  219. startSize = utils.StartIndex(currentIndex, pageSize)
  220. total, err := models.GetAnalystCount()
  221. if err != nil {
  222. br.Msg = "获取失败"
  223. br.ErrMsg = "获取失败,Err:" + err.Error()
  224. return
  225. }
  226. list, err := models.GetAnalystList(startSize, pageSize)
  227. if err != nil {
  228. br.Msg = "获取失败"
  229. br.ErrMsg = "获取失败,Err:" + err.Error()
  230. return
  231. }
  232. analystList := make([]*models.AnalystView, 0)
  233. for _, v := range list {
  234. analystList = append(analystList, &models.AnalystView{
  235. Id: v.Id,
  236. Name: v.Name,
  237. HeadImgURL: v.HeadImgURL,
  238. Introduction: v.Introduction,
  239. CreatedTime: v.CreatedTime.Format(time.DateTime),
  240. })
  241. }
  242. page := paging.GetPaging(currentIndex, pageSize, total)
  243. resp := new(response.AnalystListResp)
  244. resp.List = analystList
  245. resp.Paging = page
  246. br.Ret = 200
  247. br.Success = true
  248. br.Msg = "获取成功"
  249. br.Data = resp
  250. }