analyst.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. size, err := strconv.Atoi(utils.UPLOAD_IMG_SIZE)
  42. if err != nil {
  43. size = 15
  44. }
  45. if h.Size > 1024*int64(size) {
  46. br.Msg = fmt.Sprintf("头像文件大小不能超过%dK", size)
  47. return
  48. }
  49. dateDir := time.Now().Format(time.DateOnly)
  50. uploadDir := utils.STATIC_DIR + "ht/analyst/" + dateDir
  51. err = os.MkdirAll(uploadDir, utils.DIR_MOD)
  52. if err != nil {
  53. br.Msg = "存储目录创建失败"
  54. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  55. return
  56. }
  57. randStr := utils.GetRandStringNoSpecialChar(28)
  58. fileName := randStr + ext
  59. fpath := uploadDir + "/" + fileName
  60. err = this.SaveToFile("File", fpath)
  61. if err != nil {
  62. br.Msg = "头像上传失败"
  63. br.ErrMsg = "头像上传失败,Err:" + err.Error()
  64. return
  65. }
  66. thumbnailPath, err := utils.ImageResize(fpath, 1200)
  67. if err != nil {
  68. br.Msg = "头像上传失败"
  69. br.ErrMsg = "头像上传失败,Err:" + err.Error()
  70. return
  71. }
  72. ossUploadDir := utils.RESOURCE_DIR + "analyst/"
  73. saveOssPath := ossUploadDir + time.Now().Format("200601/20060102/")
  74. imgName := utils.GetRandStringNoSpecialChar(28)
  75. saveOssPath += imgName
  76. defer func() {
  77. _ = os.Remove(fpath)
  78. _ = os.Remove(thumbnailPath)
  79. }()
  80. ossClient := services.NewOssClient()
  81. if ossClient == nil {
  82. br.Msg = "头像上传失败"
  83. br.ErrMsg = "初始化OSS服务失败"
  84. return
  85. }
  86. //新增上传缩略图
  87. thumbnailUrl, err := ossClient.UploadFile("", thumbnailPath, saveOssPath+"_thumbnail")
  88. if err != nil {
  89. br.Msg = "头像上传失败"
  90. br.ErrMsg = "头像上传失败,Err:" + err.Error()
  91. return
  92. }
  93. imgUrl, err := ossClient.UploadFile("", fpath, saveOssPath)
  94. if err != nil {
  95. br.Msg = "头像上传失败"
  96. br.ErrMsg = "头像上传失败,Err:" + err.Error()
  97. return
  98. }
  99. base := path.Base(h.Filename)
  100. thumbnailBase := path.Base(h.Filename + "_thumbnail")
  101. resp := new(response.HeadImageUploadResp)
  102. resp.Url = thumbnailUrl
  103. resp.OriginUrl = imgUrl
  104. resp.FileName = thumbnailBase
  105. resp.OriginFileName = base
  106. br.Data = resp
  107. br.Msg = "上传成功"
  108. br.Ret = 200
  109. br.Success = true
  110. }
  111. // GetAnalystByName
  112. // @Title 查询研究员信息
  113. // @Description 查询研究员信息
  114. // @Param File query file true "文件"
  115. // @Success 200 {object} models.ReportAuthorResp
  116. // @router /analyst [get]
  117. func (this *AnalystController) GetAnalystByName(AnalystName string) {
  118. br := new(models.BaseResponse).Init()
  119. defer func() {
  120. this.Data["json"] = br
  121. this.ServeJSON()
  122. }()
  123. analyst, err := models.GetAnalystByName(AnalystName)
  124. if err != nil {
  125. br.Msg = "获取失败"
  126. br.ErrMsg = "获取失败,Err:" + err.Error()
  127. return
  128. }
  129. br.Ret = 200
  130. br.Success = true
  131. br.Msg = "获取成功"
  132. br.Data = analyst.ToView()
  133. }
  134. // List
  135. // @Title 列表
  136. // @Description 列表
  137. // @Param File query file true "文件"
  138. // @Success 200 {object} models.ReportAuthorResp
  139. // @router /list [get]
  140. func (this *AnalystController) List() {
  141. br := new(models.BaseResponse).Init()
  142. defer func() {
  143. this.Data["json"] = br
  144. this.ServeJSON()
  145. }()
  146. pageSize, _ := this.GetInt("PageSize")
  147. currentIndex, _ := this.GetInt("CurrentIndex")
  148. KeyWord := this.GetString("KeyWord")
  149. var startSize int
  150. var condition string
  151. if pageSize <= 0 {
  152. pageSize = utils.PageSize20
  153. }
  154. if currentIndex <= 0 {
  155. currentIndex = 1
  156. }
  157. if KeyWord != "" {
  158. condition += " AND name like '%" + KeyWord + "%'"
  159. }
  160. startSize = utils.StartIndex(currentIndex, pageSize)
  161. total, err := models.GetAnalystCount(condition)
  162. if err != nil {
  163. br.Msg = "获取失败"
  164. br.ErrMsg = "获取失败,Err:" + err.Error()
  165. return
  166. }
  167. list, err := models.GetAnalystList(condition, startSize, pageSize)
  168. if err != nil {
  169. br.Msg = "获取失败"
  170. br.ErrMsg = "获取失败,Err:" + err.Error()
  171. return
  172. }
  173. analystList := make([]*models.AnalystView, 0)
  174. for _, v := range list {
  175. analystList = append(analystList, &models.AnalystView{
  176. Id: v.Id,
  177. Name: v.Name,
  178. HeadImgURL: v.HeadImgUrl,
  179. Position: v.Position,
  180. InvestmentCertificate: v.InvestmentCertificate,
  181. ProfessionalCertificate: v.ProfessionalCertificate,
  182. Introduction: v.Introduction,
  183. CreatedTime: v.CreatedTime.Format(time.DateTime),
  184. })
  185. }
  186. page := paging.GetPaging(currentIndex, pageSize, total)
  187. resp := new(response.AnalystListResp)
  188. resp.List = analystList
  189. resp.Paging = page
  190. br.Ret = 200
  191. br.Success = true
  192. br.Msg = "获取成功"
  193. br.Data = resp
  194. }
  195. // edit
  196. // @Title 编辑研究员
  197. // @Description 编辑研究员
  198. // @Param request body request.AnalystEditReq true "type json string"
  199. // @Success 200 string 编辑成功
  200. // @router /edit [post]
  201. func (this *AnalystController) Edit() {
  202. br := new(models.BaseResponse).Init()
  203. defer func() {
  204. this.Data["json"] = br
  205. this.ServeJSON()
  206. }()
  207. var req request.AnalystEditReq
  208. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  209. if err != nil {
  210. br.Msg = "参数解析异常!"
  211. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  212. return
  213. }
  214. if req.Id <= 0 {
  215. br.Msg = "请选择用户"
  216. return
  217. }
  218. if req.HeadImgUrl == "" {
  219. br.Msg = "请输入头像地址"
  220. return
  221. }
  222. req.Introduction = strings.TrimSpace(req.Introduction)
  223. analyst, err := models.GetAnalystById(req.Id)
  224. if err != nil {
  225. if err.Error() == utils.ErrNoRow() {
  226. br.Msg = "用户不存在或已删除,请重新刷新页面"
  227. br.ErrMsg = "用户不存在或已删除,请重新刷新页面,Err:" + err.Error()
  228. return
  229. }
  230. br.Msg = "编辑用户失败"
  231. br.ErrMsg = "编辑用户失败, 系统错误,Err:" + err.Error()
  232. return
  233. }
  234. if req.HeadImgUrl != `` {
  235. analyst.HeadImgUrl = req.HeadImgUrl
  236. }
  237. analyst.Introduction = req.Introduction
  238. analyst.Position = req.Position
  239. analyst.HeadOriginImgUrl = req.HeadOriginImgUrl
  240. analyst.ProfessionalCertificate = req.ProfessionalCertificate
  241. analyst.InvestmentCertificate = req.InvestmentCertificate
  242. analyst.UpdatedTime = time.Now()
  243. err = analyst.Update([]string{"HeadImgURL", "HeadOriginImgURL", "Introduction", "Position", "InvestmentCertificate", "ProfessionalCertificate", "UpdatedTime"})
  244. if err != nil {
  245. br.Msg = "编辑失败"
  246. br.ErrMsg = "编辑失败, 系统错误,Err:" + err.Error()
  247. return
  248. }
  249. br.Msg = "编辑成功"
  250. br.Success = true
  251. br.Ret = 200
  252. }