ths_send.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. "hongze/hz_crm_api/utils"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. type ThsSendController struct {
  16. controllers.BaseAuthController
  17. }
  18. // @Title 推送客群待发送列表
  19. // @Description 推送客群待发送列表
  20. // @Param PageSize query int true "每页数据条数"
  21. // @Param CurrentIndex query int true "当前页页码,从1开始"
  22. // @Param Title query string true "推送标题"
  23. // @Param ReportType query string true "内容类型"
  24. // @Param CreateTimeStart query string true "创建时间起始时间"
  25. // @Param CreateTimeEnd query string true "创建时间截止时间"
  26. // @Param PushTimeStart query string true "待推送时间起始时间"
  27. // @Param PushTimeEnd query string true "待推送时间截止时间"
  28. // @Success 200 {object} models.ReportSendThsListResp
  29. // @router /ths_send/list [get]
  30. func (t *ThsSendController) List() {
  31. br := new(models.BaseResponse).Init()
  32. defer func() {
  33. t.Data["json"] = br
  34. t.ServeJSON()
  35. }()
  36. sysUser := t.SysUser
  37. if sysUser == nil {
  38. br.Msg = "请登录"
  39. br.ErrMsg = "请登录,SysUser Is Empty"
  40. br.Ret = 408
  41. return
  42. }
  43. pageSize, _ := t.GetInt("PageSize")
  44. currentIndex, _ := t.GetInt("CurrentIndex")
  45. title := t.GetString("Title")
  46. reportType := t.GetString("ReportType")
  47. createTimeStart := t.GetString("CreateTimeStart")
  48. createTimeEnd := t.GetString("CreateTimeEnd")
  49. pushTimeStart := t.GetString("PushTimeStart")
  50. pushTimeEnd := t.GetString("PushTimeEnd")
  51. var startSize int
  52. if pageSize <= 0 {
  53. pageSize = utils.PageSize20
  54. }
  55. if currentIndex <= 0 {
  56. currentIndex = 1
  57. }
  58. startSize = paging.StartIndex(currentIndex, pageSize)
  59. var condition string
  60. var pars []interface{}
  61. if title != "" {
  62. condition += ` AND title LIKE '%` + title + `%' `
  63. }
  64. if reportType != "" {
  65. condition += ` AND report_type = ? `
  66. pars = append(pars, reportType)
  67. }
  68. if createTimeStart != "" {
  69. condition += ` AND create_time >= ? `
  70. pars = append(pars, createTimeStart)
  71. }
  72. if createTimeEnd != "" {
  73. tmpDate, err := time.ParseInLocation(utils.FormatDate, createTimeEnd, time.Local)
  74. if err != nil {
  75. br.Msg = "截止日期格式错误"
  76. return
  77. }
  78. tmpDate = tmpDate.AddDate(0, 0, 1)
  79. condition += ` AND create_time < ? `
  80. pars = append(pars, tmpDate)
  81. }
  82. if pushTimeStart != "" {
  83. condition += ` AND push_time >= ? `
  84. pars = append(pars, pushTimeStart)
  85. }
  86. if pushTimeEnd != "" {
  87. tmpDate, err := time.ParseInLocation(utils.FormatDate, pushTimeEnd, time.Local)
  88. if err != nil {
  89. br.Msg = "待推送截止日期格式错误"
  90. return
  91. }
  92. tmpDate = tmpDate.AddDate(0, 0, 1)
  93. condition += ` AND push_time < ? `
  94. pars = append(pars, tmpDate)
  95. }
  96. total, err := models.GetWaitReportSendThsCount(condition, pars)
  97. if err != nil {
  98. br.Msg = "获取失败"
  99. br.ErrMsg = "获取数据总数失败,Err:" + err.Error()
  100. return
  101. }
  102. list := make([]*models.ReportSendThsListItem, 0)
  103. tmpList, err := models.GetWaitReportSendThsList(condition, pars, startSize, pageSize)
  104. if err != nil {
  105. br.Msg = "获取失败"
  106. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  107. return
  108. }
  109. for _, v := range tmpList {
  110. tmp := &models.ReportSendThsListItem{
  111. SendId: v.SendId,
  112. ReportId: v.ReportId,
  113. ReportType: v.ReportType,
  114. Status: v.Status,
  115. Remark: v.Remark,
  116. Title: v.Title,
  117. LabelStr: v.LabelStr,
  118. Level: v.Level,
  119. PushTime: v.PushTime.Format(utils.FormatDateTime),
  120. CreateTime: v.CreateTime.Format(utils.FormatDateTime),
  121. }
  122. list = append(list, tmp)
  123. }
  124. page := paging.GetPaging(currentIndex, pageSize, int(total))
  125. resp := new(models.ReportSendThsListResp)
  126. resp.List = list
  127. resp.Paging = page
  128. br.Ret = 200
  129. br.Success = true
  130. br.Msg = "获取成功"
  131. br.Data = resp
  132. }
  133. // Save
  134. // @Title 设置同花顺客群待推送时间
  135. // @Description 设置同花顺客群待推送时间
  136. // @Param request body request.ModifyReportThsSendPushTimeReq true "type json string"
  137. // @Success 200 string "操作成功"
  138. // @router /ths_send/modify_push_time [post]
  139. func (t *ThsSendController) ModifyPushTime() {
  140. br := new(models.BaseResponse).Init()
  141. br.IsSendEmail = false
  142. defer func() {
  143. t.Data["json"] = br
  144. t.ServeJSON()
  145. }()
  146. sysUser := t.SysUser
  147. if sysUser == nil {
  148. br.Msg = "请登录"
  149. br.ErrMsg = "请登录,SysUser Is Empty"
  150. br.Ret = 408
  151. return
  152. }
  153. var req request.ModifyReportThsSendPushTimeReq
  154. if err := json.Unmarshal(t.Ctx.Input.RequestBody, &req); err != nil {
  155. br.Msg = "参数解析异常!"
  156. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  157. return
  158. }
  159. if req.SendId == 0 {
  160. br.Msg = "请选择要修改的记录"
  161. return
  162. }
  163. if req.PushTime == "" {
  164. br.Msg = "请输入新的推送时间"
  165. return
  166. }
  167. newPushTime, err := time.ParseInLocation(utils.FormatDateTime, req.PushTime, time.Local)
  168. if err != nil {
  169. br.Msg = "新的推送时间格式错误"
  170. return
  171. }
  172. if newPushTime.Before(time.Now()) {
  173. br.Msg = "新的推送时间不能设置成过去时间"
  174. return
  175. }
  176. //查询当前记录是否允许更改
  177. info, err := models.GetReportSendThsBySendId(req.SendId)
  178. if err != nil {
  179. br.Msg = "查询推送记录失败"
  180. br.ErrMsg = "查询推送记录失败 ErrMsg:" + err.Error()
  181. return
  182. }
  183. if info.Status != 2 {
  184. br.Msg = "当前状态不是待推送,禁止修改"
  185. return
  186. }
  187. //查询新的推送时间和最近一条待推送的时间是否间隔5分钟以上
  188. last, err := models.GetLatelyWaitReportSendThs()
  189. if err != nil {
  190. br.Msg = "查询最近的待推送记录失败"
  191. br.ErrMsg = "查询最近的待推送记录失败 ErrMsg:" + err.Error()
  192. return
  193. }
  194. var sub time.Duration
  195. if last.PushTime.Before(newPushTime) {
  196. sub = newPushTime.Sub(last.PushTime)
  197. } else {
  198. sub = last.PushTime.Sub(newPushTime)
  199. }
  200. if sub.Minutes() <= 5 {
  201. br.Msg = "新的推送时间与最近待推送的时间相隔过近,不允许修改"
  202. return
  203. }
  204. //查询是否存在和设置时间相近的待推送记录
  205. newPushTimeStart := newPushTime.Add(-5 * time.Minute)
  206. newPushTimeEnd := newPushTime.Add(5 * time.Minute)
  207. nearly, e := models.GetNearlyWaitReportSendThsByPushTime(info.SendId, newPushTimeStart, newPushTimeEnd)
  208. if e != nil && e.Error() != utils.ErrNoRow() {
  209. br.Msg = "查询最近的待推送记录失败"
  210. br.ErrMsg = "查询最近的待推送记录失败 ErrMsg:" + e.Error()
  211. return
  212. }
  213. if e == nil && nearly.SendId > 0 {
  214. br.Msg = "新的推送时间与某条待推送记录时间相隔过近,不允许修改"
  215. return
  216. }
  217. var condition string
  218. var pars []interface{}
  219. condition = " and push_time > ?"
  220. pars = append(pars, req.PushTime)
  221. //查询
  222. allList, err := models.GetWaitReportSendThsAll(condition, pars)
  223. if err != nil {
  224. br.Msg = "查询推送记录失败"
  225. br.ErrMsg = "查询推送记录失败 ErrMsg:" + err.Error()
  226. return
  227. }
  228. needChangeSendIds := ""
  229. multi := ""
  230. if len(allList) > 0 {
  231. //整理需要更新推送时间的记录
  232. n := 0
  233. for _, v := range allList {
  234. if v.SendId == req.SendId {
  235. continue
  236. }
  237. n = n + 1
  238. needChangeSendIds += strconv.Itoa(v.SendId) + ","
  239. add := time.Duration((n)*40) * time.Minute
  240. newTime := newPushTime.Add(add).Format(utils.FormatDateTime)
  241. multi += ` WHEN ` + strconv.Itoa(v.SendId) + ` THEN "` + newTime + `"`
  242. }
  243. }
  244. if needChangeSendIds != "" {
  245. needChangeSendIds = strings.Trim(needChangeSendIds, ",")
  246. }
  247. err = models.MultiUpdateReportSendThsPushTime(req.SendId, req.PushTime, multi, needChangeSendIds)
  248. if err != nil {
  249. br.Msg = "设置失败"
  250. br.ErrMsg = "设置失败 ErrMsg:" + err.Error()
  251. return
  252. }
  253. br.Ret = 200
  254. br.Success = true
  255. br.Msg = "操作成功"
  256. }
  257. // @Title 推送客群待发送类型列表
  258. // @Description 推送客群待发送类型列表
  259. // @Success 200 {object} models.ReportSendThsListResp
  260. // @router /ths_send/report_type [get]
  261. func (t *ThsSendController) ReportTypeList() {
  262. br := new(models.BaseResponse).Init()
  263. defer func() {
  264. t.Data["json"] = br
  265. t.ServeJSON()
  266. }()
  267. sysUser := t.SysUser
  268. if sysUser == nil {
  269. br.Msg = "请登录"
  270. br.ErrMsg = "请登录,SysUser Is Empty"
  271. br.Ret = 408
  272. return
  273. }
  274. list := make([]*models.ReportSendThsTypeListItem, 0)
  275. /*tmpList, err := models.GetWaitReportSendThsType()
  276. if err != nil {
  277. br.Msg = "获取失败"
  278. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  279. return
  280. }
  281. for _, v := range tmpList {
  282. tmp := &models.ReportSendThsTypeListItem{ReportType: v.ReportType}
  283. list = append(list, tmp)
  284. }*/
  285. reportTypes := []string{"研报", "语音播报", "视频社区", "线上路演"}
  286. for _, v := range reportTypes {
  287. tmp := &models.ReportSendThsTypeListItem{ReportType: v}
  288. list = append(list, tmp)
  289. }
  290. resp := new(models.ReportSendThsTypeListResp)
  291. resp.List = list
  292. br.Ret = 200
  293. br.Success = true
  294. br.Msg = "获取成功"
  295. br.Data = resp
  296. }
  297. // ThsSentConfList
  298. // @Title 推送客户群设置-产品优先列表
  299. // @Description 推送客户群设置-产品优先列表
  300. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:'level':优先类型"
  301. // @Param SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`"
  302. // @Param PageSize query int true "每页数据条数"
  303. // @Param CurrentIndex query int true "当前页页码,从1开始"
  304. // @Success 200 {object} models.ReportSendThsListResp
  305. // @router /ths_send/config/list [get]
  306. func (t *ThsSendController) ThsSentConfList() {
  307. br := new(models.BaseResponse).Init()
  308. defer func() {
  309. t.Data["json"] = br
  310. t.ServeJSON()
  311. }()
  312. sysUser := t.SysUser
  313. if sysUser == nil {
  314. br.Msg = "请登录"
  315. br.ErrMsg = "请登录,SysUser Is Empty"
  316. br.Ret = 408
  317. return
  318. }
  319. pageSize, _ := t.GetInt("PageSize")
  320. currentIndex, _ := t.GetInt("CurrentIndex")
  321. sortParam := t.GetString("SortParam")
  322. sortType := t.GetString("SortType")
  323. var startSize int
  324. if pageSize <= 0 {
  325. pageSize = utils.PageSize20
  326. }
  327. if currentIndex <= 0 {
  328. currentIndex = 1
  329. }
  330. startSize = paging.StartIndex(currentIndex, pageSize)
  331. var condition string
  332. var pars []interface{}
  333. sortStr := ``
  334. if sortParam != `` {
  335. sortStr = fmt.Sprintf("%s %s,modify_time desc ", sortParam, sortType)
  336. } else {
  337. sortStr = " modify_time desc,config_id desc "
  338. }
  339. total, err := models.GetReportSendThsConfigCount(condition, pars)
  340. if err != nil {
  341. br.Msg = "获取失败"
  342. br.ErrMsg = "获取数据总数失败,Err:" + err.Error()
  343. return
  344. }
  345. list := make([]*models.ReportSendThsConfigListItem, 0)
  346. tmpList, err := models.GetReportSendThsConfigList(condition, pars, startSize, pageSize, sortStr)
  347. if err != nil {
  348. br.Msg = "获取失败"
  349. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  350. return
  351. }
  352. for _, v := range tmpList {
  353. tmp := &models.ReportSendThsConfigListItem{
  354. ConfigId: v.ConfigId,
  355. ConfigType: v.ConfigType,
  356. ClassifyId: v.ClassifyId,
  357. ClassifyName: v.ClassifyName,
  358. Level: v.Level,
  359. ModifyTime: v.ModifyTime.Format(utils.FormatDateTime),
  360. CreateTime: v.CreateTime.Format(utils.FormatDateTime),
  361. }
  362. list = append(list, tmp)
  363. }
  364. page := paging.GetPaging(currentIndex, pageSize, int(total))
  365. resp := new(models.ReportSendThsConfigListResp)
  366. resp.List = list
  367. resp.Paging = page
  368. br.Ret = 200
  369. br.Success = true
  370. br.Msg = "获取成功"
  371. br.Data = resp
  372. }
  373. // AddThsSentConf
  374. // @Title 推送客户群设置-添加产品优先配置
  375. // @Description 推送客户群设置-添加产品优先配置
  376. // @Param request body request.AddThsSentConfReq true "type json string"
  377. // @Success 200 string "操作成功"
  378. // @router /ths_send/config/add [post]
  379. func (t *ThsSendController) AddThsSentConf() {
  380. br := new(models.BaseResponse).Init()
  381. defer func() {
  382. t.Data["json"] = br
  383. t.ServeJSON()
  384. }()
  385. sysUser := t.SysUser
  386. if sysUser == nil {
  387. br.Msg = "请登录"
  388. br.ErrMsg = "请登录,SysUser Is Empty"
  389. br.Ret = 408
  390. return
  391. }
  392. var req request.AddThsSentConfReq
  393. if err := json.Unmarshal(t.Ctx.Input.RequestBody, &req); err != nil {
  394. br.Msg = "参数解析异常!"
  395. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  396. return
  397. }
  398. req.ConfigType = utils.TrimStr(req.ConfigType)
  399. if req.ConfigType == "" {
  400. br.Msg = "请选择产品类型"
  401. br.IsSendEmail = false
  402. return
  403. }
  404. if req.ClassifyId == 0 && req.ConfigType != "线上路演" && req.ConfigType != "视频社区" {
  405. br.Msg = "请选择产品"
  406. br.IsSendEmail = false
  407. return
  408. }
  409. intervalTime := 40 //间隔时间
  410. switch req.Level {
  411. case 1:
  412. intervalTime = 0
  413. case 2:
  414. intervalTime = 20
  415. case 3:
  416. intervalTime = 40
  417. default:
  418. br.Msg = "错误的优先类型"
  419. br.IsSendEmail = false
  420. return
  421. }
  422. item := &models.ReportSendThsConfig{
  423. //ConfigId: 0,
  424. ConfigType: req.ConfigType,
  425. ClassifyId: req.ClassifyId,
  426. ClassifyName: "",
  427. Level: req.Level,
  428. Time: intervalTime,
  429. SysUserId: sysUser.AdminId,
  430. SysRealName: sysUser.RealName,
  431. ModifyTime: time.Now(),
  432. CreateTime: time.Now(),
  433. }
  434. //研报、语音播报、视频社区、线上路演
  435. switch req.ConfigType {
  436. case "研报":
  437. classifyName := ``
  438. secondInfo, err := models.GetClassifyById(req.ClassifyId)
  439. if err != nil {
  440. br.Msg = "查询研报二级分类信息异常"
  441. br.ErrMsg = "查询研报二级分类信息异常,err:" + err.Error()
  442. return
  443. }
  444. classifyName = secondInfo.ClassifyName
  445. if secondInfo.ParentId > 0 {
  446. firstInfo, err := models.GetClassifyById(secondInfo.ParentId)
  447. if err != nil {
  448. br.Msg = "查询研报一级分类信息异常"
  449. br.ErrMsg = "查询研报一级分类信息异常,err:" + err.Error()
  450. return
  451. }
  452. classifyName += "/" + firstInfo.ClassifyName
  453. }
  454. item.ClassifyName = classifyName
  455. case "语音播报":
  456. voiceSection, err := yb.GetVoiceSectionById(req.ClassifyId)
  457. if err != nil {
  458. br.Msg = "查询语音播报版块信息异常"
  459. br.ErrMsg = "查询语音播报版块信息异常,err:" + err.Error()
  460. return
  461. }
  462. item.ClassifyName = voiceSection.SectionName
  463. case "视频社区", "线上路演":
  464. item.ClassifyName = req.ConfigType
  465. default:
  466. br.Msg = "错误的产品类型"
  467. br.IsSendEmail = false
  468. return
  469. }
  470. count, err := models.GetCountReportSendThsConfigByClassifyId(req.ConfigType, req.ClassifyId)
  471. if err != nil {
  472. br.Msg = "添加配置异常"
  473. br.ErrMsg = "查询现有的配置异常,err:" + err.Error()
  474. return
  475. }
  476. if count > 0 {
  477. br.Msg = "该产品已配置,请不要重复配置"
  478. br.IsSendEmail = false
  479. return
  480. }
  481. err = models.AddReportSendThsConfig(item)
  482. if err != nil {
  483. br.Msg = "添加配置失败"
  484. br.ErrMsg = "添加配置失败,err:" + err.Error()
  485. return
  486. }
  487. br.Ret = 200
  488. br.Success = true
  489. br.Msg = "配置成功"
  490. }
  491. // EditThsSentConf
  492. // @Title 推送客户群设置-编辑产品优先配置
  493. // @Description 推送客户群设置-编辑产品优先配置
  494. // @Param request body request.EditThsSentConfReq true "type json string"
  495. // @Success 200 string "操作成功"
  496. // @router /ths_send/config/edit [post]
  497. func (t *ThsSendController) EditThsSentConf() {
  498. br := new(models.BaseResponse).Init()
  499. defer func() {
  500. t.Data["json"] = br
  501. t.ServeJSON()
  502. }()
  503. sysUser := t.SysUser
  504. if sysUser == nil {
  505. br.Msg = "请登录"
  506. br.ErrMsg = "请登录,SysUser Is Empty"
  507. br.Ret = 408
  508. return
  509. }
  510. var req request.EditThsSentConfReq
  511. if err := json.Unmarshal(t.Ctx.Input.RequestBody, &req); err != nil {
  512. br.Msg = "参数解析异常!"
  513. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  514. return
  515. }
  516. if req.ConfigId < 0 {
  517. br.Msg = "请选择配置"
  518. br.IsSendEmail = false
  519. return
  520. }
  521. configInfo, err := models.GetReportSendThsConfigByConfigId(req.ConfigId)
  522. if err != nil {
  523. if err.Error() == utils.ErrNoRow() {
  524. br.Msg = "不存在该配置信息"
  525. br.IsSendEmail = false
  526. return
  527. }
  528. br.Msg = "查询配置信息异常"
  529. br.ErrMsg = "查询现有的配置异常,err:" + err.Error()
  530. return
  531. }
  532. configInfo.Level = req.Level
  533. configInfo.ModifyTime = time.Now()
  534. err = configInfo.Update([]string{"Level", "ModifyTime"})
  535. if err != nil {
  536. br.Msg = "添加配置失败"
  537. br.ErrMsg = "添加配置失败,err:" + err.Error()
  538. return
  539. }
  540. br.Ret = 200
  541. br.Success = true
  542. br.Msg = "修改成功"
  543. }
  544. // DeleteThsSentConf
  545. // @Title 推送客户群设置-删除产品优先配置
  546. // @Description 推送客户群设置-删除产品优先配置
  547. // @Param request body request.EditThsSentConfReq true "type json string"
  548. // @Success 200 string "操作成功"
  549. // @router /ths_send/config/delete [post]
  550. func (t *ThsSendController) DeleteThsSentConf() {
  551. br := new(models.BaseResponse).Init()
  552. defer func() {
  553. t.Data["json"] = br
  554. t.ServeJSON()
  555. }()
  556. sysUser := t.SysUser
  557. if sysUser == nil {
  558. br.Msg = "请登录"
  559. br.ErrMsg = "请登录,SysUser Is Empty"
  560. br.Ret = 408
  561. return
  562. }
  563. var req request.EditThsSentConfReq
  564. if err := json.Unmarshal(t.Ctx.Input.RequestBody, &req); err != nil {
  565. br.Msg = "参数解析异常!"
  566. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  567. return
  568. }
  569. if req.ConfigId < 0 {
  570. br.Msg = "请选择配置"
  571. br.IsSendEmail = false
  572. return
  573. }
  574. err := models.DeleteReportSendThsConfigByConfigId(req.ConfigId)
  575. if err != nil {
  576. br.Msg = "删除配置信息失败"
  577. br.ErrMsg = "删除配置信息失败,err:" + err.Error()
  578. return
  579. }
  580. br.Ret = 200
  581. br.Success = true
  582. br.Msg = "删除成功"
  583. }