package advisory import ( "encoding/json" "fmt" "github.com/rdlucklib/rdluck_tools/paging" "hongze/hz_crm_api/controllers" "hongze/hz_crm_api/models" "hongze/hz_crm_api/models/advisory" "hongze/hz_crm_api/utils" "strconv" "time" ) type AdminBaseArticleController struct { controllers.BaseCommonController } // 管理端——资讯文章模块 type AdminArticleController struct { controllers.BaseAuthController } // @Title 新增资讯接口 发布与定时发布 // @Description 发布 // @Param request body advisory.ArticleAddReq true "type json string" // @Success 200 新增成功 // @router /article/firstadd [post] func (this *AdminArticleController) FirstAdd() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() AdminUser := this.SysUser if AdminUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,用户信息为空" br.Ret = 408 return } var req advisory.ArticleAddReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } //userFllowInfo, _ := advisory.GetUserFllowInfo(AdminUser.AdminId) //if userFllowInfo == nil { // br.Msg = "请先关注品种" // br.ErrMsg = "用户未关注品种,Err:" + err.Error() // return //} else { // fllowList := strings.Split(userFllowInfo.ChartPermissionId, ",") // isHaveFllow := advisory.CheckIsFllow(fllowList, strconv.Itoa(req.ChartPermissionId)) // if isHaveFllow == false { // br.Msg = "没有关注该品种" // br.ErrMsg = "用户未关注品种,ChartPermissionId为" + strconv.Itoa(req.ChartPermissionId) + ":的品种你暂未关注" // return // } //} if req.Title == "" { br.Msg = "标题不可为空" br.ErrMsg = "标题不可为空" return } if req.Body == "" { br.Msg = "内容不可为空" br.ErrMsg = "内容不可为空" return } charInfo, err := advisory.GetCategoryInfoById(req.ChartPermissionId) if err != nil { br.Msg = "获取品种信息失败" br.ErrMsg = "获取品种信息失败,Err:" + err.Error() return } item := new(advisory.AdvisoryArticle) item.Title = req.Title item.Body = req.Body item.ChartPermissionId = req.ChartPermissionId item.ChartPermissionName = charInfo.PermissionName item.AdminId = AdminUser.AdminId fmt.Println(req.TimingPublishTime) //判断是否定时发布 if req.TimingPublishTime != "" { resultTime := utils.StrTimeToTime(req.TimingPublishTime) //时间字符串格式转时间格式 //fmt.Println(resultTime.Format("20060102")) //fmt.Println(time.Now().Format("20060102")) if time.Now().Format("20060102") == resultTime.Format("20060102") { item.PublishStatus = 1 item.ActualPublishTime = time.Now() } else { res := time.Now().Before(resultTime) if res == false { br.Msg = "定时时间错误" br.ErrMsg = "定时时间请大于当前时间" return } var pars []interface{} var condition string condition += ` AND chart_permission_id = ` + strconv.Itoa(req.ChartPermissionId) + ` AND admin_id = ` + strconv.Itoa(AdminUser.AdminId) + ` AND DATE_FORMAT(timing_publish_time,'%Y%m%d') = ` + resultTime.Format("20060102") count, _ := advisory.GetThisDayTimingPublishTimeCount(condition, pars) if count > 0 { br.Msg = "该时间已被占用,请选择其他时间" br.ErrMsg = "该时间已被占用,请选择其他时间" return } item.TimingPublishTime = resultTime item.IsTiming = 1 fmt.Println(item.TimingPublishTime) } } else { item.ActualPublishTime = time.Now() item.PublishStatus = 1 } item.LinkName = req.LinkName item.LinkAddress = req.LinkAddress item.CreateTime = time.Now() item.LastUpdateTime = time.Now() if req.ArticleId < 1 { item.Code = utils.MD5("advisory" + utils.GetRandDigit(6)) _, err = advisory.AddArticle(item) if err != nil { br.Msg = "发布失败" br.ErrMsg = "发布失败,Err:" + err.Error() return } } else { item.ArticleId = req.ArticleId err = advisory.EditArticle(item) } if err != nil { br.Msg = "发布失败" br.ErrMsg = "发布失败,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "发布成功" } // @Title 保存资讯接口 // @Description 保存资讯 // @Param request body advisory.ArticleAddReq true "type json string" // @Success 200 保存成功 // @router /article/preservation [post] func (this *AdminArticleController) FirstPreservation() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() AdminUser := this.SysUser if AdminUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,用户信息为空" br.Ret = 408 return } var req advisory.ArticleAddReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } //userFllowInfo, _ := advisory.GetUserFllowInfo(AdminUser.AdminId) //if userFllowInfo == nil { // br.Msg = "请先关注品种" // br.ErrMsg = "用户未关注品种,Err:" + err.Error() // return //} else { // fllowList := strings.Split(userFllowInfo.ChartPermissionId, ",") // isHaveFllow := advisory.CheckIsFllow(fllowList, strconv.Itoa(req.ChartPermissionId)) // if isHaveFllow == false { // br.Msg = "没有关注该品种" // br.ErrMsg = "用户未关注品种,ChartPermissionId为" + strconv.Itoa(req.ChartPermissionId) + ":的品种你暂未关注" // return // } //} if req.Title == "" { br.Msg = "标题不可为空" br.ErrMsg = "标题不可为空" return } charInfo, err := advisory.GetCategoryInfoById(req.ChartPermissionId) if err != nil { br.Msg = "获取品种信息失败" br.ErrMsg = "获取品种信息失败,Err:" + err.Error() return } item := new(advisory.AdvisoryArticle) item.Title = req.Title item.Body = req.Body item.ChartPermissionId = req.ChartPermissionId item.ChartPermissionName = charInfo.PermissionName item.AdminId = AdminUser.AdminId item.LinkName = req.LinkName item.LinkAddress = req.LinkAddress item.CreateTime = time.Now() item.ActualPublishTime = time.Now() item.LastUpdateTime = time.Now() resp := new(advisory.ArticleDeleteReq) itemLog := new(advisory.AdvisoryArticleLog) if req.ArticleId < 1 { item.Code = utils.MD5("advisory" + utils.GetRandDigit(6)) newId, err := advisory.AddArticle(item) if err != nil { br.Msg = "保存失败" br.ErrMsg = "保存失败,Err:" + err.Error() return } resp.ArticleId = newId itemLog.ArticleId = int(newId) } else { item.ArticleId = req.ArticleId resp.ArticleId = int64(req.ArticleId) itemLog.ArticleId = req.ArticleId err = advisory.EditArticle(item) if err != nil { br.Msg = "保存失败" br.ErrMsg = "保存失败,Err:" + err.Error() return } } itemLog.Title = req.Title itemLog.Body = req.Body itemLog.ChartPermissionId = req.ChartPermissionId itemLog.ChartPermissionName = charInfo.PermissionName itemLog.AdminId = AdminUser.AdminId itemLog.LinkName = req.LinkName itemLog.LinkAddress = req.LinkAddress itemLog.CreateTime = time.Now() itemLog.LastUpdateTime = time.Now() go advisory.AddArticleLog(itemLog) br.Ret = 200 br.Success = true br.Msg = "保存成功" br.Data = resp } // @Title 资讯接口 文章修改 // @Description 文章修改 // @Param ArticleId query int true "文章ID" // @Param Title query string true "标题" // @Param Body query string true "内容" // @Param ChartPermissionId query int true "所属分类ID" // @Param TimingPublishTime query string true "定时发布时间" // @Param LinkName query string true "链接名称" // @Param LinkAddress query string true "链接地址" // @Param ArticleId query string true "文章ID" // @Success 200 修改成功 // @router /edit [post] func (this *AdminArticleController) Edit() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() AdminUser := this.SysUser if AdminUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,用户信息为空" br.Ret = 408 return } var req advisory.PreservationArticle err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } //userFllowInfo, _ := advisory.GetUserFllowInfo(AdminUser.AdminId) //if userFllowInfo == nil { // br.Msg = "请先关注品种" // br.ErrMsg = "用户未关注品种,Err:" + err.Error() // return //} else { // fllowList := strings.Split(userFllowInfo.ChartPermissionId, ",") // isHaveFllow := advisory.CheckIsFllow(fllowList, strconv.Itoa(req.ChartPermissionId)) // if isHaveFllow == false { // br.Msg = "没有关注该品种" // br.ErrMsg = "用户未关注品种,ChartPermissionId为" + strconv.Itoa(req.ChartPermissionId) + ":的品种你暂未关注" // return // } //} if req.ArticleId < 1 { br.Msg = "请传入文章ID" br.ErrMsg = "ArticleId不可为空" return } fmt.Println(strconv.Itoa(req.ArticleId)) if req.Title == "" { br.Msg = "标题不可为空" br.ErrMsg = "标题不可为空" return } if req.Body == "" { br.Msg = "内容不可为空" br.ErrMsg = "内容不可为空" return } charInfo, err := advisory.GetCategoryInfoById(req.ChartPermissionId) if err != nil { br.Msg = "获取品种信息失败" br.ErrMsg = "获取品种信息失败,Err:" + err.Error() return } userArticleInfo, err := advisory.GetUserArticleInfo(AdminUser.AdminId, int64(req.ArticleId)) if userArticleInfo == nil { br.Msg = "文章不存在" br.ErrMsg = "文章ID错误,Err:" + err.Error() return } item := new(advisory.AdvisoryArticle) item.ArticleId = req.ArticleId item.Title = req.Title item.Body = req.Body item.ChartPermissionId = req.ChartPermissionId item.ChartPermissionName = charInfo.PermissionName //判断是否定时发布 if req.TimingPublishTime != "" { resultTime := utils.StrTimeToTime(req.TimingPublishTime) //时间字符串格式转时间格式 res := time.Now().Before(resultTime) if res == false { br.Msg = "定时时间错误" br.ErrMsg = "定时时间请大于当前时间" return } item.TimingPublishTime = resultTime } item.PublishStatus = 0 item.LinkName = req.LinkName item.LinkAddress = req.LinkAddress item.CreateTime = time.Now() item.LastUpdateTime = time.Now() err = advisory.EditArticle(item) if err != nil { br.Msg = "修改失败" br.ErrMsg = "修改失败,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "修改成功" } // @Title 资讯接口 文章详情 // @Description 文章详情 // @Param ArticleId query int true "文章ID" // @Success Ret=200 {object} advisory.AdvisoryArticle // @router /article/detail [get] func (this *AdminArticleController) Detail() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() AdminUser := this.SysUser if AdminUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,用户信息为空" br.Ret = 408 return } ArticleId, _ := this.GetInt("ArticleId") if ArticleId < 1 { br.Msg = "请输入文章ID" return } userArticleInfo, err := advisory.GetUserArticleInfo(AdminUser.AdminId, int64(ArticleId)) if userArticleInfo == nil { br.Msg = "文章不存在" br.ErrMsg = "文章ID错误,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = userArticleInfo } // @Title 删除文章 // @Description 删除文章 // @Param ArticleId query int true "文章ID" // @Success 200 删除成功 // @router /article/delete [post] func (this *AdminArticleController) Delete() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() AdminUser := this.SysUser if AdminUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,用户信息为空" br.Ret = 408 return } var req advisory.ArticleDeleteReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } userArticleInfo, err := advisory.GetUserArticleInfo(AdminUser.AdminId, req.ArticleId) if userArticleInfo == nil { br.Msg = "文章不存在" br.ErrMsg = "文章ID错误,Err:" + err.Error() return } err = advisory.DeleteArticle(req.ArticleId) if err != nil { br.Msg = "删除失败" br.ErrMsg = "删除失败,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "删除成功" } // @Title 取消发布 // @Description 取消发布 // @Param ArticleId query int true "文章ID" // @Success 200 取消成功 // @router /article/unpublish [post] func (this *AdminArticleController) Unpublish() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() AdminUser := this.SysUser if AdminUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,用户信息为空" br.Ret = 408 return } var req advisory.ArticleDeleteReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } userArticleInfo, err := advisory.GetUserArticleInfo(AdminUser.AdminId, req.ArticleId) if userArticleInfo == nil { br.Msg = "文章不存在" br.ErrMsg = "文章ID错误,Err:" + err.Error() return } if userArticleInfo.PublishStatus == 0 { br.Msg = "已经取消发布请勿重复操作" return } err = advisory.UnpublishArticle(int(req.ArticleId)) if err != nil { br.Msg = "取消失败" br.ErrMsg = "取消失败,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "取消成功" } // @Title 资讯接口 // @Description 列表 // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码,从1开始" // @Param KeyWord query string false "关键词搜索" // @Param Status query string false "发布状态,1,已发布,0未发布" // @Param ChartPermissionIds query string false "关注分类ID,多个用英文逗号隔开" // @Param PublishSort query string true "desc:降序,asc 升序(预留)" // @Success Ret=200 {object} advisory.ArticleListResp // @router /article/list [get] func (this *AdminArticleController) List() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() AdminUser := this.SysUser if AdminUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,用户信息为空" br.Ret = 408 return } pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") keyWord := this.GetString("KeyWord") status := this.GetString("Status") chartPermissionIds := this.GetString("ChartPermissionIds") var startSize int var condition string var pars []interface{} //condition += ` AND admin_id = ` + strconv.Itoa(AdminUser.AdminId) + ` ` if status != "" { condition += ` AND publish_status = ` + status + ` ` } if keyWord != "" { condition += ` AND ( title LIKE '%` + keyWord + `%' OR body LIKE '%` + keyWord + `%')` } if chartPermissionIds != "" { condition += ` AND chart_permission_id in (` + chartPermissionIds + `) ` } //else { // userFllowInfo, _ := advisory.GetUserFllowInfo(AdminUser.AdminId) // if userFllowInfo != nil { // condition += ` AND chart_permission_id in (` + userFllowInfo.ChartPermissionId + `) ` // } //} if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } startSize = utils.StartIndex(currentIndex, pageSize) total, err := advisory.GetArticleListCount(condition, pars) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } condition += ` ORDER BY last_update_time DESC ` //if status == "0" { // condition += ` ORDER BY last_update_time DESC ` //} list, err := advisory.GetArticlerList(condition, pars, startSize, pageSize) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } for key, value := range list { LastUpdateTime := value.LastUpdateTime list[key].WhatWeek = utils.StrDateTimeToWeek(LastUpdateTime) list[key].LastUpdateTime = LastUpdateTime list[key].ActualPublishTime = utils.TimeRemoveHms(LastUpdateTime) } page := paging.GetPaging(currentIndex, pageSize, total) resp := new(advisory.ArticleListResp) resp.Paging = page resp.List = list br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } // @Title 资讯接口 文章详情 // @Description 通过Code获取文章详情 // @Param Code query string true "文章code" // @Success Ret=200 {object} advisory.AdvisoryArticle // @router /articleCode [get] func (this *AdminArticleController) ArticleCode() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() AdminUser := this.SysUser if AdminUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,用户信息为空" br.Ret = 408 return } code := this.GetString("Code") if code == "" { br.Msg = "请输入文章Code" return } userArticleInfo, err := advisory.GetUserArticleInfoByCode(code) if userArticleInfo == nil { br.Msg = "文章不存在" br.ErrMsg = "文章Code错误,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = userArticleInfo } // @Title 资讯接口 获取网站PV,Uv // @Description 获取网站PV,Uv // @Success Ret=200 {object} advisory.PvAndUvResp // @router /getPVAndUv [get] func (this *AdminArticleController) GetPVAndUv() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() AdminUser := this.SysUser if AdminUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,用户信息为空" br.Ret = 408 return } resp, err := advisory.GetPVAndUv() if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp }