analyst.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. // GetAnalystByName
  99. // @Title 查询研究员信息
  100. // @Description 查询研究员信息
  101. // @Param File query file true "文件"
  102. // @Success 200 {object} models.ReportAuthorResp
  103. // @router /analyst [get]
  104. func (this *AnalystController) GetAnalystByName(AnalystName string) {
  105. br := new(models.BaseResponse).Init()
  106. defer func() {
  107. this.Data["json"] = br
  108. this.ServeJSON()
  109. }()
  110. analyst, err := models.GetAnalystByName(AnalystName)
  111. if err != nil {
  112. br.Msg = "获取失败"
  113. br.ErrMsg = "获取失败,Err:" + err.Error()
  114. return
  115. }
  116. br.Ret = 200
  117. br.Success = true
  118. br.Msg = "获取成功"
  119. br.Data = analyst.ToView()
  120. }
  121. // List
  122. // @Title 列表
  123. // @Description 列表
  124. // @Param File query file true "文件"
  125. // @Success 200 {object} models.ReportAuthorResp
  126. // @router /list [get]
  127. func (this *AnalystController) List() {
  128. br := new(models.BaseResponse).Init()
  129. defer func() {
  130. this.Data["json"] = br
  131. this.ServeJSON()
  132. }()
  133. pageSize, _ := this.GetInt("PageSize")
  134. currentIndex, _ := this.GetInt("CurrentIndex")
  135. KeyWord := this.GetString("KeyWord")
  136. var startSize int
  137. var condition string
  138. if pageSize <= 0 {
  139. pageSize = utils.PageSize20
  140. }
  141. if currentIndex <= 0 {
  142. currentIndex = 1
  143. }
  144. if KeyWord != "" {
  145. condition += " AND name like '%" + KeyWord + "%'"
  146. }
  147. startSize = utils.StartIndex(currentIndex, pageSize)
  148. total, err := models.GetAnalystCount(condition)
  149. if err != nil {
  150. br.Msg = "获取失败"
  151. br.ErrMsg = "获取失败,Err:" + err.Error()
  152. return
  153. }
  154. list, err := models.GetAnalystList(condition, startSize, pageSize)
  155. if err != nil {
  156. br.Msg = "获取失败"
  157. br.ErrMsg = "获取失败,Err:" + err.Error()
  158. return
  159. }
  160. analystList := make([]*models.AnalystView, 0)
  161. for _, v := range list {
  162. analystList = append(analystList, &models.AnalystView{
  163. Id: v.Id,
  164. Name: v.Name,
  165. HeadImgURL: v.HeadImgUrl,
  166. Position: v.Position,
  167. InvestmentCertificate: v.InvestmentCertificate,
  168. ProfessionalCertificate: v.ProfessionalCertificate,
  169. Introduction: v.Introduction,
  170. CreatedTime: v.CreatedTime.Format(time.DateTime),
  171. })
  172. }
  173. page := paging.GetPaging(currentIndex, pageSize, total)
  174. resp := new(response.AnalystListResp)
  175. resp.List = analystList
  176. resp.Paging = page
  177. br.Ret = 200
  178. br.Success = true
  179. br.Msg = "获取成功"
  180. br.Data = resp
  181. }
  182. // edit
  183. // @Title 编辑研究员
  184. // @Description 编辑研究员
  185. // @Param request body request.AnalystEditReq true "type json string"
  186. // @Success 200 string 编辑成功
  187. // @router /edit [post]
  188. func (this *AnalystController) Edit() {
  189. br := new(models.BaseResponse).Init()
  190. defer func() {
  191. this.Data["json"] = br
  192. this.ServeJSON()
  193. }()
  194. var req request.AnalystEditReq
  195. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  196. if err != nil {
  197. br.Msg = "参数解析异常!"
  198. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  199. return
  200. }
  201. if req.Id <= 0 {
  202. br.Msg = "请选择用户"
  203. return
  204. }
  205. if req.HeadImgUrl == "" {
  206. br.Msg = "请输入头像地址"
  207. return
  208. }
  209. req.Introduction = strings.TrimSpace(req.Introduction)
  210. analyst, err := models.GetAnalystById(req.Id)
  211. if err != nil {
  212. if err.Error() == utils.ErrNoRow() {
  213. br.Msg = "用户不存在或已删除,请重新刷新页面"
  214. br.ErrMsg = "用户不存在或已删除,请重新刷新页面,Err:" + err.Error()
  215. return
  216. }
  217. br.Msg = "编辑用户失败"
  218. br.ErrMsg = "编辑用户失败, 系统错误,Err:" + err.Error()
  219. return
  220. }
  221. if req.HeadImgUrl != `` {
  222. analyst.HeadImgUrl = req.HeadImgUrl
  223. }
  224. analyst.Introduction = req.Introduction
  225. analyst.Position = req.Position
  226. analyst.ProfessionalCertificate = req.ProfessionalCertificate
  227. analyst.InvestmentCertificate = req.InvestmentCertificate
  228. analyst.UpdatedTime = time.Now()
  229. err = analyst.Update([]string{"HeadImgURL", "Introduction", "Position", "InvestmentCertificate", "ProfessionalCertificate", "UpdatedTime"})
  230. if err != nil {
  231. br.Msg = "编辑失败"
  232. br.ErrMsg = "编辑失败, 系统错误,Err:" + err.Error()
  233. return
  234. }
  235. br.Msg = "编辑成功"
  236. br.Success = true
  237. br.Ret = 200
  238. }