activity_speaker.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package yb
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "hongze/hz_crm_api/controllers"
  7. "hongze/hz_crm_api/models"
  8. "hongze/hz_crm_api/models/yb"
  9. "hongze/hz_crm_api/models/yb/request"
  10. ybResp "hongze/hz_crm_api/models/yb/response"
  11. "hongze/hz_crm_api/services/alarm_msg"
  12. "hongze/hz_crm_api/utils"
  13. "time"
  14. )
  15. // ActivitySpeakerController 研报活动主讲人
  16. type ActivitySpeakerController struct {
  17. controllers.BaseAuthController
  18. }
  19. // List
  20. // @Title 主讲人列表
  21. // @Description 主讲人列表
  22. // @Param Keywords query string false "搜索关键字"
  23. // @Param PageSize query int false "每页数据量"
  24. // @Param CurrentIndex query int false "页码"
  25. // @Success 200 {object} ybResp.SpeakerPageListResp
  26. // @router /speaker/list [get]
  27. func (this *ActivitySpeakerController) List() {
  28. br := new(models.BaseResponse).Init()
  29. defer func() {
  30. this.Data["json"] = br
  31. this.ServeJSON()
  32. }()
  33. sysUser := this.SysUser
  34. if sysUser == nil {
  35. br.Msg = "请登录"
  36. br.ErrMsg = "请登录,SysUser Is Empty"
  37. br.Ret = 408
  38. return
  39. }
  40. keywords := this.GetString("Keywords")
  41. pageSize, _ := this.GetInt("PageSize")
  42. currentIndex, _ := this.GetInt("CurrentIndex")
  43. var cond string
  44. var pars []interface{}
  45. if keywords != "" {
  46. keywords = "%" + keywords + "%"
  47. cond += ` AND speaker_name LIKE ?`
  48. pars = append(pars, keywords)
  49. }
  50. var startSize int
  51. if pageSize <= 0 {
  52. pageSize = utils.PageSize20
  53. }
  54. if currentIndex <= 0 {
  55. currentIndex = 1
  56. }
  57. startSize = paging.StartIndex(currentIndex, pageSize)
  58. total, list, e := yb.GetSpeakerPageList(cond, pars, startSize, pageSize)
  59. if e != nil {
  60. br.Msg = "获取主讲人列表失败"
  61. br.ErrMsg = "获取主讲人列表失败, Err: " + e.Error()
  62. return
  63. }
  64. respList := make([]*ybResp.SpeakerRespList, 0)
  65. for i := range list {
  66. respList = append(respList, &ybResp.SpeakerRespList{
  67. SpeakerId: list[i].SpeakerId,
  68. SpeakerName: list[i].SpeakerName,
  69. SpeakerHeadPic: list[i].SpeakerHeadPic,
  70. SpeakerBackgroundPic: list[i].SpeakerBackgroundPic,
  71. CreateTime: list[i].CreateTime.Format(utils.FormatDateTime),
  72. })
  73. }
  74. page := paging.GetPaging(currentIndex, pageSize, total)
  75. resp := ybResp.SpeakerPageListResp{
  76. List: respList,
  77. Paging: page,
  78. }
  79. br.Ret = 200
  80. br.Success = true
  81. br.Msg = "获取成功"
  82. br.Data = resp
  83. }
  84. // Save
  85. // @Title 保存主讲人
  86. // @Description 保存主讲人
  87. // @Param request body request.SaveSpeakerReq true "type json string"
  88. // @Success 200 string "操作成功"
  89. // @router /speaker/save [post]
  90. func (this *ActivitySpeakerController) Save() {
  91. br := new(models.BaseResponse).Init()
  92. br.IsSendEmail = false
  93. defer func() {
  94. this.Data["json"] = br
  95. this.ServeJSON()
  96. }()
  97. sysUser := this.SysUser
  98. if sysUser == nil {
  99. br.Msg = "请登录"
  100. br.ErrMsg = "请登录,SysUser Is Empty"
  101. br.Ret = 408
  102. return
  103. }
  104. var req request.SaveSpeakerReq
  105. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  106. br.Msg = "参数解析异常!"
  107. br.ErrMsg = "参数解析失败, Err:" + err.Error()
  108. return
  109. }
  110. if req.SpeakerName == "" {
  111. br.Msg = "请输入主讲人姓名"
  112. return
  113. }
  114. if req.SpeakerHeadPic == "" {
  115. br.Msg = "请上传主讲人头像"
  116. return
  117. }
  118. exist, e := yb.GetSpeakerByName(req.SpeakerName)
  119. if e != nil && e.Error() != utils.ErrNoRow() {
  120. br.Msg = "获取主讲人信息失败"
  121. br.ErrMsg = "根据姓名获取主讲人信息失败, Err:" + e.Error()
  122. return
  123. }
  124. nowTime := time.Now().Local()
  125. if req.SpeakerId > 0 {
  126. if exist != nil && exist.SpeakerId != req.SpeakerId {
  127. br.Msg = "主讲人姓名已存在"
  128. return
  129. }
  130. // 更新
  131. item, e := yb.GetSpeakerById(req.SpeakerId)
  132. if e != nil {
  133. br.Msg = "获取主讲人信息失败"
  134. br.ErrMsg = "获取主讲人信息失败, Err:" + e.Error()
  135. return
  136. }
  137. originSpeaker := item.SpeakerName
  138. item.SpeakerName = req.SpeakerName
  139. item.SpeakerHeadPic = req.SpeakerHeadPic
  140. item.ModifyTime = nowTime
  141. updateCols := []string{"SpeakerName", "SpeakerHeadPic", "ModifyTime"}
  142. if e = item.Update(updateCols); e != nil {
  143. br.Msg = "更新主讲人信息失败"
  144. br.ErrMsg = "更新主讲人信息失败, Err:" + e.Error()
  145. return
  146. }
  147. // 更新活动主讲人
  148. go func() {
  149. if e = yb.UpdateActivitySpeaker(req.SpeakerName, req.SpeakerHeadPic, originSpeaker); e != nil {
  150. alarm_msg.SendAlarmMsg("更新活动主讲人信息失败, ErrMsg: "+e.Error(), 2)
  151. }
  152. }()
  153. } else {
  154. if exist != nil {
  155. br.Msg = "主讲人姓名已存在"
  156. return
  157. }
  158. // 新增
  159. item := new(yb.Speaker)
  160. item.SpeakerName = req.SpeakerName
  161. item.SpeakerHeadPic = req.SpeakerHeadPic
  162. item.CreateTime = nowTime
  163. item.ModifyTime = nowTime
  164. if e := yb.AddSpeaker(item); e != nil {
  165. br.Msg = "新增主讲人信息失败"
  166. br.ErrMsg = "新增主讲人信息失败, Err:" + e.Error()
  167. return
  168. }
  169. }
  170. br.Ret = 200
  171. br.Success = true
  172. br.Msg = "操作成功"
  173. }
  174. // Del
  175. // @Title 删除主讲人
  176. // @Description 删除主讲人
  177. // @Param request body request.DelSpeakerReq true "type json string"
  178. // @Success 200 string "操作成功"
  179. // @router /speaker/del [post]
  180. func (this *ActivitySpeakerController) Del() {
  181. br := new(models.BaseResponse).Init()
  182. br.IsSendEmail = false
  183. defer func() {
  184. this.Data["json"] = br
  185. this.ServeJSON()
  186. }()
  187. sysUser := this.SysUser
  188. if sysUser == nil {
  189. br.Msg = "请登录"
  190. br.ErrMsg = "请登录,SysUser Is Empty"
  191. br.Ret = 408
  192. return
  193. }
  194. var req request.DelSpeakerReq
  195. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  196. br.Msg = "参数解析异常!"
  197. br.ErrMsg = "参数解析失败, Err:" + err.Error()
  198. return
  199. }
  200. if req.SpeakerId <= 0 {
  201. br.Msg = "参数有误"
  202. return
  203. }
  204. item, e := yb.GetSpeakerById(req.SpeakerId)
  205. if e != nil {
  206. br.Msg = "主讲人已被删除, 请勿重复操作"
  207. return
  208. }
  209. count, e := yb.GetCountBySpeaker(item.SpeakerName)
  210. if e != nil {
  211. br.Msg = "获取主讲人活动次数失败"
  212. br.ErrMsg = "获取主讲人活动次数失败, Err:" + e.Error()
  213. return
  214. }
  215. if count > 0 {
  216. br.Msg = fmt.Sprintf("主讲人%s已创建活动,不可删除!", item.SpeakerName)
  217. return
  218. }
  219. if e = yb.DelSpeaker(item.SpeakerId); e != nil {
  220. br.Msg = "删除主讲人失败"
  221. br.ErrMsg = "删除主讲人失败, Err:" + e.Error()
  222. return
  223. }
  224. br.Ret = 200
  225. br.Success = true
  226. br.Msg = "操作成功"
  227. }
  228. // DelCheck
  229. // @Title 删除主讲人校验(需求上要求在触发删除前就弹提示框...所以就有了这个多余的接口=_=!)
  230. // @Description 删除主讲人校验
  231. // @Param request body request.DelSpeakerReq true "type json string"
  232. // @Success 200 string "操作成功"
  233. // @router /speaker/del_check [post]
  234. func (this *ActivitySpeakerController) DelCheck() {
  235. br := new(models.BaseResponse).Init()
  236. br.IsSendEmail = false
  237. defer func() {
  238. this.Data["json"] = br
  239. this.ServeJSON()
  240. }()
  241. sysUser := this.SysUser
  242. if sysUser == nil {
  243. br.Msg = "请登录"
  244. br.ErrMsg = "请登录,SysUser Is Empty"
  245. br.Ret = 408
  246. return
  247. }
  248. var req request.DelSpeakerReq
  249. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  250. br.Msg = "参数解析异常!"
  251. br.ErrMsg = "参数解析失败, Err:" + err.Error()
  252. return
  253. }
  254. if req.SpeakerId <= 0 {
  255. br.Msg = "参数有误"
  256. return
  257. }
  258. var ok bool
  259. var msg string
  260. item, e := yb.GetSpeakerById(req.SpeakerId)
  261. if e != nil {
  262. br.Msg = "主讲人已被删除, 请勿重复操作"
  263. return
  264. }
  265. count, e := yb.GetCountBySpeaker(item.SpeakerName)
  266. if e != nil {
  267. br.Msg = "获取主讲人活动次数失败"
  268. br.ErrMsg = "获取主讲人活动次数失败, Err:" + e.Error()
  269. return
  270. }
  271. ok = true
  272. if count > 0 {
  273. ok = false
  274. msg = fmt.Sprintf("主讲人%s已创建活动,不可删除!", item.SpeakerName)
  275. }
  276. resp := new(ybResp.SpeakerDelCheckResp)
  277. resp.Ok = ok
  278. resp.Msg = msg
  279. br.Ret = 200
  280. br.Success = true
  281. br.Msg = "操作成功"
  282. br.Data = resp
  283. }