123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- package controllers
- import (
- "encoding/json"
- "fmt"
- "github.com/beego/beego/v2/server/web"
- "hongze/hz_crm_api/models"
- "hongze/hz_crm_api/models/company"
- "hongze/hz_crm_api/services"
- "hongze/hz_crm_api/services/alarm_msg"
- "hongze/hz_crm_api/utils"
- "os"
- "path"
- "strings"
- "time"
- )
- // ReportController 报告
- type ReportController struct {
- BaseAuthController
- }
- // 报告上传
- type ReportUploadCommonController struct {
- web.Controller
- }
- // GetDayWeekReportChapterTypeList
- // @Title 获取晨报周报章节类型列表
- // @Description 获取晨报周报章节类型列表
- // @Param StopType query string true "停更类型 stop; disable;"
- // @Success 200 {object} models.ReportListResp
- // @router /getDayWeekReportChapterTypeList [get]
- func (this *ReportController) GetDayWeekReportChapterTypeList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var condition string
- var pars []interface{}
- // 停更类型
- stopType := this.GetString("StopType")
- switch stopType {
- case "", "stop":
- condition += ` AND enabled = ? `
- pars = append(pars, 1)
- case "disable":
- default:
- br.Msg = "停更类型异常"
- br.IsSendEmail = false
- return
- }
- dayClassifyId, err := company.GetReportClassifyIdByConfigKey("report_day_classify_id")
- if err != nil {
- br.Msg = "获取晨报ID配置失败"
- br.ErrMsg = "获取配置失败"
- br.IsSendEmail = false
- return
- }
- weekClassifyId, err := company.GetReportClassifyIdByConfigKey("report_week_classify_id")
- if err != nil {
- br.Msg = "获取周报ID配置失败"
- br.ErrMsg = "获取配置失败"
- br.IsSendEmail = false
- return
- }
- dayList := make([]*models.ReportChapterType, 0)
- weekList := make([]*models.ReportChapterType, 0)
- // 晨报
- {
- tmpCondition := condition
- tmpPars := pars
- // 报告类型
- tmpCondition += ` AND report_classify_id = ? `
- tmpPars = append(tmpPars, dayClassifyId)
- list, err := models.GetAllReportChapterTypeList(tmpCondition, tmpPars)
- if err != nil {
- br.Msg = "获取报告章节类型列表失败"
- br.ErrMsg = "获取报告章节类型列表失败, Err: " + err.Error()
- return
- }
- nowTime := time.Now()
- for _, v := range list {
- if v.IsSet == 1 {
- endTime, _ := time.Parse(utils.FormatDate, v.PauseEndTime)
- if nowTime.After(endTime.AddDate(0, 0, 1)) { //设置过期了已经
- v.IsSet = 0
- v.PauseStartTime = ``
- v.PauseEndTime = ``
- }
- }
- if v.PauseStartTime == `0001-01-01` {
- v.PauseStartTime = ``
- }
- if v.PauseEndTime == `0001-01-01` {
- v.PauseEndTime = ``
- }
- dayList = append(dayList, v)
- }
- }
- // 周报
- {
- tmpCondition := condition
- tmpPars := pars
- // 报告类型
- tmpCondition += ` AND report_classify_id = ? `
- tmpPars = append(tmpPars, weekClassifyId)
- list, err := models.GetAllReportChapterTypeList(tmpCondition, tmpPars)
- if err != nil {
- br.Msg = "获取报告章节类型列表失败"
- br.ErrMsg = "获取报告章节类型列表失败, Err: " + err.Error()
- return
- }
- nowTime := time.Now()
- for _, v := range list {
- if v.IsSet == 1 {
- endTime, _ := time.Parse(utils.FormatDate, v.PauseEndTime)
- if nowTime.After(endTime.AddDate(0, 0, 1)) { //设置过期了已经
- v.IsSet = 0
- v.PauseStartTime = ``
- v.PauseEndTime = ``
- }
- }
- if v.PauseStartTime == `0001-01-01` {
- v.PauseStartTime = ``
- }
- if v.PauseEndTime == `0001-01-01` {
- v.PauseEndTime = ``
- }
-
- weekList = append(weekList, v)
- }
- }
- resp := models.UpdateReportChapterTypeResp{
- Day: dayList,
- Week: weekList,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // @Title 获取晨报周报的更新暂停时间
- // @Description 获取晨报周报的更新暂停时间
- // @Success 200 {object} models.ReportListResp
- // @router /getDayWeekReportPauseTime [get]
- func (this *ReportController) GetDayWeekReportPauseTime() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- list, err := models.GetDayWeekReportPauseTimeList()
- if err != nil {
- br.Msg = "获取晨报周报的更新暂停时间失败"
- br.ErrMsg = "获取晨报周报的更新暂停时间失败, Err: " + err.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = list
- }
- // SetDayWeekReportUpdateRule
- // @Title 设置晨报周报的更新规则
- // @Description 设置晨报周报的更新规则
- // @Param request body models.SetDayWeekReportUpdateRuleReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /setDayWeekReportUpdateRule [post]
- func (this *ReportController) SetDayWeekReportUpdateRule() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req models.SetDayWeekReportUpdateRuleReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- researchType := req.ResearchType
- if researchType == "" {
- br.Msg = "报告类型不能为空"
- return
- }
- classifyIdKey := ``
- switch researchType {
- case "day":
- classifyIdKey = `report_day_classify_id`
- case "week":
- classifyIdKey = `report_week_classify_id`
- default:
- br.Msg = "报告类型异常"
- return
- }
- reportClassifyId, err := company.GetReportClassifyIdByConfigKey(classifyIdKey)
- if err != nil {
- br.Msg = "获取分类ID配置失败"
- br.ErrMsg = "获取配置失败,key:" + classifyIdKey
- br.IsSendEmail = false
- return
- }
- // 设置章节类型的暂停时间
- if err := models.SetDayWeekReportUpdateRule(reportClassifyId, req.List); err != nil {
- br.Msg = "设置暂停时间失败"
- br.ErrMsg = "设置暂停时间失败, Err: " + err.Error()
- return
- }
- // 同步eta系统的章节更新配置
- go func() {
- ruleSyncReq := new(services.EditReportChapterTypeRuleSyncReq)
- ruleSyncReq.ReportClassifyId = reportClassifyId
- _, _ = services.EditReportChapterTypeRuleSync(ruleSyncReq)
- }()
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // SetDayWeekReportEnableRule
- // @Title 设置晨报周报的永久暂停更新规则
- // @Description 设置晨报周报的永久暂停更新规则
- // @Param request body models.SetDayWeekReportEnableUpdateRuleReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /setDayWeekReportEnableUpdateRule [post]
- func (this *ReportController) SetDayWeekReportEnableRule() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req models.SetDayWeekReportEnableUpdateRuleReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- dayReportChapterTypeIdList := strings.Split(req.DayReportChapterTypeId, ",")
- weekReportChapterTypeIdList := strings.Split(req.WeekReportChapterTypeId, ",")
- //if len(reportChapterTypeIdList) <= 0 {
- // br.Msg = "报告类型选择异常"
- // br.IsSendEmail = false
- // return
- //}
- // 设置章节类型的禁用状态
- if err := models.SetDayWeekReportEnableUpdateRule(dayReportChapterTypeIdList, weekReportChapterTypeIdList); err != nil {
- br.Msg = "设置永久停更失败"
- br.ErrMsg = "设置永久停更失败, Err: " + err.Error()
- return
- }
- // 同步eta系统的章节更新配置
- go func() {
- ruleSyncReq := new(services.EditReportChapterTypeRuleSyncReq)
- _, _ = services.EditReportChapterTypeRuleSync(ruleSyncReq)
- }()
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // GetStopDayWeekReportChapterTypeList
- // @Title 获取暂停更新的晨报周报章节类型列表
- // @Description 获取暂停更新的晨报周报章节类型列表
- // @Success 200 {object} models.ReportListResp
- // @router /getStopDayWeekReportChapterTypeList [get]
- func (this *ReportController) GetStopDayWeekReportChapterTypeList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- stopDay := make([]*models.ReportChapterType, 0)
- stopWeek := make([]*models.ReportChapterType, 0)
- disableDay := make([]*models.ReportChapterType, 0)
- disableWeek := make([]*models.ReportChapterType, 0)
- stopList, err := models.GetStopUpdateReportChapterTypeListByResearchType()
- if err != nil {
- br.Msg = "获取暂停更新报告章节类型列表失败"
- br.ErrMsg = "获取暂停更新报告章节类型列表失败, Err: " + err.Error()
- return
- }
- dayClassifyId, err := company.GetReportClassifyIdByConfigKey("report_day_classify_id")
- if err != nil {
- br.Msg = "获取晨报ID配置失败"
- br.ErrMsg = "获取配置失败"
- br.IsSendEmail = false
- return
- }
- weekClassifyId, err := company.GetReportClassifyIdByConfigKey("report_week_classify_id")
- if err != nil {
- br.Msg = "获取周报ID配置失败"
- br.ErrMsg = "获取配置失败"
- br.IsSendEmail = false
- return
- }
- for _, v := range stopList {
- if v.ReportClassifyId == dayClassifyId {
- stopDay = append(stopDay, v)
- } else if v.ReportClassifyId == weekClassifyId {
- stopWeek = append(stopWeek, v)
- }
- }
- disableList, err := models.GetDisableUpdateReportChapterTypeListByResearchTypeByClassifyIdList([]int{dayClassifyId, weekClassifyId})
- if err != nil {
- br.Msg = "获取停止更新报告章节类型列表失败"
- br.ErrMsg = "获取停止更新报告章节类型列表失败, Err: " + err.Error()
- return
- }
- for _, v := range disableList {
- if v.ReportClassifyId == dayClassifyId {
- disableDay = append(disableDay, v)
- } else if v.ReportClassifyId == weekClassifyId {
- disableWeek = append(disableWeek, v)
- }
- }
- resp := models.StopUpdateReportChapterTypeResp{
- StopDay: stopDay,
- StopWeek: stopWeek,
- DisableDay: disableDay,
- DisableWeek: disableWeek,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // @Title 图片上传
- // @Description 图片上传接口
- // @Param File query file true "文件"
- // @Success 200 上传成功
- // @router /uploadImg [post]
- func (this *ReportUploadCommonController) UploadImg() {
- var err error
- defer func() {
- if err != nil {
- fmt.Println("文件上传失败:", err.Error())
- go alarm_msg.SendAlarmMsg("URI:"+this.Ctx.Input.URI()+" 文件上传失败:"+err.Error(), 3)
- //go utils.SendEmail(utils.APPNAME+"失败提醒", "URI:"+this.Ctx.Input.URI()+" 文件上传失败:"+err.Error(), utils.EmailSendToUsers)
- }
- }()
- f, h, err := this.GetFile("file")
- if err != nil {
- return
- }
- ext := path.Ext(h.Filename)
- dateDir := time.Now().Format("20060102")
- uploadDir := utils.STATIC_DIR + "hongze/" + dateDir
- err = os.MkdirAll(uploadDir, 777)
- if err != nil {
- return
- }
- randStr := utils.GetRandStringNoSpecialChar(28)
- fileName := randStr + ext
- fpath := uploadDir + "/" + fileName
- defer f.Close() //关闭上传文件
- err = this.SaveToFile("file", fpath)
- if err != nil {
- return
- }
- //上传到阿里云
- resourceUrl, err := services.UploadAliyunV2(fileName, fpath)
- if err != nil {
- return
- }
- defer func() {
- os.Remove(fpath)
- }()
- item := new(models.Resource)
- item.ResourceUrl = resourceUrl
- item.ResourceType = 1
- item.CreateTime = time.Now()
- newId, err := models.AddResource(item)
- if err != nil {
- return
- }
- resp := new(models.ResourceResp)
- resp.Id = newId
- resp.ResourceUrl = resourceUrl
- this.Data["json"] = map[string]string{"link": resourceUrl}
- this.ServeJSON()
- }
|