report.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/beego/beego/v2/server/web"
  6. "hongze/hz_crm_api/models"
  7. "hongze/hz_crm_api/models/company"
  8. "hongze/hz_crm_api/services"
  9. "hongze/hz_crm_api/services/alarm_msg"
  10. "hongze/hz_crm_api/utils"
  11. "os"
  12. "path"
  13. "strings"
  14. "time"
  15. )
  16. // ReportController 报告
  17. type ReportController struct {
  18. BaseAuthController
  19. }
  20. // 报告上传
  21. type ReportUploadCommonController struct {
  22. web.Controller
  23. }
  24. // GetDayWeekReportChapterTypeList
  25. // @Title 获取晨报周报章节类型列表
  26. // @Description 获取晨报周报章节类型列表
  27. // @Param StopType query string true "停更类型 stop; disable;"
  28. // @Success 200 {object} models.ReportListResp
  29. // @router /getDayWeekReportChapterTypeList [get]
  30. func (this *ReportController) GetDayWeekReportChapterTypeList() {
  31. br := new(models.BaseResponse).Init()
  32. defer func() {
  33. this.Data["json"] = br
  34. this.ServeJSON()
  35. }()
  36. sysUser := this.SysUser
  37. if sysUser == nil {
  38. br.Msg = "请登录"
  39. br.ErrMsg = "请登录,SysUser Is Empty"
  40. br.Ret = 408
  41. return
  42. }
  43. var condition string
  44. var pars []interface{}
  45. // 停更类型
  46. stopType := this.GetString("StopType")
  47. switch stopType {
  48. case "", "stop":
  49. condition += ` AND enabled = ? `
  50. pars = append(pars, 1)
  51. case "disable":
  52. default:
  53. br.Msg = "停更类型异常"
  54. br.IsSendEmail = false
  55. return
  56. }
  57. dayClassifyId, err := company.GetReportClassifyIdByConfigKey("report_day_classify_id")
  58. if err != nil {
  59. br.Msg = "获取晨报ID配置失败"
  60. br.ErrMsg = "获取配置失败"
  61. br.IsSendEmail = false
  62. return
  63. }
  64. weekClassifyId, err := company.GetReportClassifyIdByConfigKey("report_week_classify_id")
  65. if err != nil {
  66. br.Msg = "获取周报ID配置失败"
  67. br.ErrMsg = "获取配置失败"
  68. br.IsSendEmail = false
  69. return
  70. }
  71. dayList := make([]*models.ReportChapterType, 0)
  72. weekList := make([]*models.ReportChapterType, 0)
  73. // 晨报
  74. {
  75. tmpCondition := condition
  76. tmpPars := pars
  77. // 报告类型
  78. tmpCondition += ` AND report_classify_id = ? `
  79. tmpPars = append(tmpPars, dayClassifyId)
  80. list, err := models.GetAllReportChapterTypeList(tmpCondition, tmpPars)
  81. if err != nil {
  82. br.Msg = "获取报告章节类型列表失败"
  83. br.ErrMsg = "获取报告章节类型列表失败, Err: " + err.Error()
  84. return
  85. }
  86. nowTime := time.Now()
  87. for _, v := range list {
  88. if v.IsSet == 1 {
  89. endTime, _ := time.Parse(utils.FormatDate, v.PauseEndTime)
  90. if nowTime.After(endTime.AddDate(0, 0, 1)) { //设置过期了已经
  91. v.IsSet = 0
  92. v.PauseStartTime = ``
  93. v.PauseEndTime = ``
  94. }
  95. }
  96. if v.PauseStartTime == `0001-01-01` {
  97. v.PauseStartTime = ``
  98. }
  99. if v.PauseEndTime == `0001-01-01` {
  100. v.PauseEndTime = ``
  101. }
  102. dayList = append(dayList, v)
  103. }
  104. }
  105. // 周报
  106. {
  107. tmpCondition := condition
  108. tmpPars := pars
  109. // 报告类型
  110. tmpCondition += ` AND report_classify_id = ? `
  111. tmpPars = append(tmpPars, weekClassifyId)
  112. list, err := models.GetAllReportChapterTypeList(tmpCondition, tmpPars)
  113. if err != nil {
  114. br.Msg = "获取报告章节类型列表失败"
  115. br.ErrMsg = "获取报告章节类型列表失败, Err: " + err.Error()
  116. return
  117. }
  118. nowTime := time.Now()
  119. for _, v := range list {
  120. if v.IsSet == 1 {
  121. endTime, _ := time.Parse(utils.FormatDate, v.PauseEndTime)
  122. if nowTime.After(endTime.AddDate(0, 0, 1)) { //设置过期了已经
  123. v.IsSet = 0
  124. v.PauseStartTime = ``
  125. v.PauseEndTime = ``
  126. }
  127. }
  128. if v.PauseStartTime == `0001-01-01` {
  129. v.PauseStartTime = ``
  130. }
  131. if v.PauseEndTime == `0001-01-01` {
  132. v.PauseEndTime = ``
  133. }
  134. weekList = append(weekList, v)
  135. }
  136. }
  137. resp := models.UpdateReportChapterTypeResp{
  138. Day: dayList,
  139. Week: weekList,
  140. }
  141. br.Ret = 200
  142. br.Success = true
  143. br.Msg = "获取成功"
  144. br.Data = resp
  145. }
  146. // @Title 获取晨报周报的更新暂停时间
  147. // @Description 获取晨报周报的更新暂停时间
  148. // @Success 200 {object} models.ReportListResp
  149. // @router /getDayWeekReportPauseTime [get]
  150. func (this *ReportController) GetDayWeekReportPauseTime() {
  151. br := new(models.BaseResponse).Init()
  152. defer func() {
  153. this.Data["json"] = br
  154. this.ServeJSON()
  155. }()
  156. sysUser := this.SysUser
  157. if sysUser == nil {
  158. br.Msg = "请登录"
  159. br.ErrMsg = "请登录,SysUser Is Empty"
  160. br.Ret = 408
  161. return
  162. }
  163. list, err := models.GetDayWeekReportPauseTimeList()
  164. if err != nil {
  165. br.Msg = "获取晨报周报的更新暂停时间失败"
  166. br.ErrMsg = "获取晨报周报的更新暂停时间失败, Err: " + err.Error()
  167. return
  168. }
  169. br.Ret = 200
  170. br.Success = true
  171. br.Msg = "获取成功"
  172. br.Data = list
  173. }
  174. // SetDayWeekReportUpdateRule
  175. // @Title 设置晨报周报的更新规则
  176. // @Description 设置晨报周报的更新规则
  177. // @Param request body models.SetDayWeekReportUpdateRuleReq true "type json string"
  178. // @Success 200 string "操作成功"
  179. // @router /setDayWeekReportUpdateRule [post]
  180. func (this *ReportController) SetDayWeekReportUpdateRule() {
  181. br := new(models.BaseResponse).Init()
  182. defer func() {
  183. this.Data["json"] = br
  184. this.ServeJSON()
  185. }()
  186. sysUser := this.SysUser
  187. if sysUser == nil {
  188. br.Msg = "请登录"
  189. br.ErrMsg = "请登录,SysUser Is Empty"
  190. br.Ret = 408
  191. return
  192. }
  193. var req models.SetDayWeekReportUpdateRuleReq
  194. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  195. if err != nil {
  196. br.Msg = "参数解析异常!"
  197. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  198. return
  199. }
  200. researchType := req.ResearchType
  201. if researchType == "" {
  202. br.Msg = "报告类型不能为空"
  203. return
  204. }
  205. classifyIdKey := ``
  206. switch researchType {
  207. case "day":
  208. classifyIdKey = `report_day_classify_id`
  209. case "week":
  210. classifyIdKey = `report_week_classify_id`
  211. default:
  212. br.Msg = "报告类型异常"
  213. return
  214. }
  215. reportClassifyId, err := company.GetReportClassifyIdByConfigKey(classifyIdKey)
  216. if err != nil {
  217. br.Msg = "获取分类ID配置失败"
  218. br.ErrMsg = "获取配置失败,key:" + classifyIdKey
  219. br.IsSendEmail = false
  220. return
  221. }
  222. // 设置章节类型的暂停时间
  223. if err := models.SetDayWeekReportUpdateRule(reportClassifyId, req.List); err != nil {
  224. br.Msg = "设置暂停时间失败"
  225. br.ErrMsg = "设置暂停时间失败, Err: " + err.Error()
  226. return
  227. }
  228. // 同步eta系统的章节更新配置
  229. go func() {
  230. ruleSyncReq := new(services.EditReportChapterTypeRuleSyncReq)
  231. ruleSyncReq.ReportClassifyId = reportClassifyId
  232. _, _ = services.EditReportChapterTypeRuleSync(ruleSyncReq)
  233. }()
  234. br.Ret = 200
  235. br.Success = true
  236. br.Msg = "操作成功"
  237. }
  238. // SetDayWeekReportEnableRule
  239. // @Title 设置晨报周报的永久暂停更新规则
  240. // @Description 设置晨报周报的永久暂停更新规则
  241. // @Param request body models.SetDayWeekReportEnableUpdateRuleReq true "type json string"
  242. // @Success 200 string "操作成功"
  243. // @router /setDayWeekReportEnableUpdateRule [post]
  244. func (this *ReportController) SetDayWeekReportEnableRule() {
  245. br := new(models.BaseResponse).Init()
  246. defer func() {
  247. this.Data["json"] = br
  248. this.ServeJSON()
  249. }()
  250. sysUser := this.SysUser
  251. if sysUser == nil {
  252. br.Msg = "请登录"
  253. br.ErrMsg = "请登录,SysUser Is Empty"
  254. br.Ret = 408
  255. return
  256. }
  257. var req models.SetDayWeekReportEnableUpdateRuleReq
  258. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  259. if err != nil {
  260. br.Msg = "参数解析异常!"
  261. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  262. return
  263. }
  264. dayReportChapterTypeIdList := strings.Split(req.DayReportChapterTypeId, ",")
  265. weekReportChapterTypeIdList := strings.Split(req.WeekReportChapterTypeId, ",")
  266. //if len(reportChapterTypeIdList) <= 0 {
  267. // br.Msg = "报告类型选择异常"
  268. // br.IsSendEmail = false
  269. // return
  270. //}
  271. // 设置章节类型的禁用状态
  272. if err := models.SetDayWeekReportEnableUpdateRule(dayReportChapterTypeIdList, weekReportChapterTypeIdList); err != nil {
  273. br.Msg = "设置永久停更失败"
  274. br.ErrMsg = "设置永久停更失败, Err: " + err.Error()
  275. return
  276. }
  277. // 同步eta系统的章节更新配置
  278. go func() {
  279. ruleSyncReq := new(services.EditReportChapterTypeRuleSyncReq)
  280. _, _ = services.EditReportChapterTypeRuleSync(ruleSyncReq)
  281. }()
  282. br.Ret = 200
  283. br.Success = true
  284. br.Msg = "操作成功"
  285. }
  286. // GetStopDayWeekReportChapterTypeList
  287. // @Title 获取暂停更新的晨报周报章节类型列表
  288. // @Description 获取暂停更新的晨报周报章节类型列表
  289. // @Success 200 {object} models.ReportListResp
  290. // @router /getStopDayWeekReportChapterTypeList [get]
  291. func (this *ReportController) GetStopDayWeekReportChapterTypeList() {
  292. br := new(models.BaseResponse).Init()
  293. defer func() {
  294. this.Data["json"] = br
  295. this.ServeJSON()
  296. }()
  297. sysUser := this.SysUser
  298. if sysUser == nil {
  299. br.Msg = "请登录"
  300. br.ErrMsg = "请登录,SysUser Is Empty"
  301. br.Ret = 408
  302. return
  303. }
  304. stopDay := make([]*models.ReportChapterType, 0)
  305. stopWeek := make([]*models.ReportChapterType, 0)
  306. disableDay := make([]*models.ReportChapterType, 0)
  307. disableWeek := make([]*models.ReportChapterType, 0)
  308. stopList, err := models.GetStopUpdateReportChapterTypeListByResearchType()
  309. if err != nil {
  310. br.Msg = "获取暂停更新报告章节类型列表失败"
  311. br.ErrMsg = "获取暂停更新报告章节类型列表失败, Err: " + err.Error()
  312. return
  313. }
  314. dayClassifyId, err := company.GetReportClassifyIdByConfigKey("report_day_classify_id")
  315. if err != nil {
  316. br.Msg = "获取晨报ID配置失败"
  317. br.ErrMsg = "获取配置失败"
  318. br.IsSendEmail = false
  319. return
  320. }
  321. weekClassifyId, err := company.GetReportClassifyIdByConfigKey("report_week_classify_id")
  322. if err != nil {
  323. br.Msg = "获取周报ID配置失败"
  324. br.ErrMsg = "获取配置失败"
  325. br.IsSendEmail = false
  326. return
  327. }
  328. for _, v := range stopList {
  329. if v.ReportClassifyId == dayClassifyId {
  330. stopDay = append(stopDay, v)
  331. } else if v.ReportClassifyId == weekClassifyId {
  332. stopWeek = append(stopWeek, v)
  333. }
  334. }
  335. disableList, err := models.GetDisableUpdateReportChapterTypeListByResearchTypeByClassifyIdList([]int{dayClassifyId, weekClassifyId})
  336. if err != nil {
  337. br.Msg = "获取停止更新报告章节类型列表失败"
  338. br.ErrMsg = "获取停止更新报告章节类型列表失败, Err: " + err.Error()
  339. return
  340. }
  341. for _, v := range disableList {
  342. if v.ReportClassifyId == dayClassifyId {
  343. disableDay = append(disableDay, v)
  344. } else if v.ReportClassifyId == weekClassifyId {
  345. disableWeek = append(disableWeek, v)
  346. }
  347. }
  348. resp := models.StopUpdateReportChapterTypeResp{
  349. StopDay: stopDay,
  350. StopWeek: stopWeek,
  351. DisableDay: disableDay,
  352. DisableWeek: disableWeek,
  353. }
  354. br.Ret = 200
  355. br.Success = true
  356. br.Msg = "获取成功"
  357. br.Data = resp
  358. }
  359. // @Title 图片上传
  360. // @Description 图片上传接口
  361. // @Param File query file true "文件"
  362. // @Success 200 上传成功
  363. // @router /uploadImg [post]
  364. func (this *ReportUploadCommonController) UploadImg() {
  365. var err error
  366. defer func() {
  367. if err != nil {
  368. fmt.Println("文件上传失败:", err.Error())
  369. go alarm_msg.SendAlarmMsg("URI:"+this.Ctx.Input.URI()+" 文件上传失败:"+err.Error(), 3)
  370. //go utils.SendEmail(utils.APPNAME+"失败提醒", "URI:"+this.Ctx.Input.URI()+" 文件上传失败:"+err.Error(), utils.EmailSendToUsers)
  371. }
  372. }()
  373. f, h, err := this.GetFile("file")
  374. if err != nil {
  375. return
  376. }
  377. ext := path.Ext(h.Filename)
  378. dateDir := time.Now().Format("20060102")
  379. uploadDir := utils.STATIC_DIR + "hongze/" + dateDir
  380. err = os.MkdirAll(uploadDir, 777)
  381. if err != nil {
  382. return
  383. }
  384. randStr := utils.GetRandStringNoSpecialChar(28)
  385. fileName := randStr + ext
  386. fpath := uploadDir + "/" + fileName
  387. defer f.Close() //关闭上传文件
  388. err = this.SaveToFile("file", fpath)
  389. if err != nil {
  390. return
  391. }
  392. //上传到阿里云
  393. resourceUrl, err := services.UploadAliyunV2(fileName, fpath)
  394. if err != nil {
  395. return
  396. }
  397. defer func() {
  398. os.Remove(fpath)
  399. }()
  400. item := new(models.Resource)
  401. item.ResourceUrl = resourceUrl
  402. item.ResourceType = 1
  403. item.CreateTime = time.Now()
  404. newId, err := models.AddResource(item)
  405. if err != nil {
  406. return
  407. }
  408. resp := new(models.ResourceResp)
  409. resp.Id = newId
  410. resp.ResourceUrl = resourceUrl
  411. this.Data["json"] = map[string]string{"link": resourceUrl}
  412. this.ServeJSON()
  413. }