123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- package report
- import (
- "errors"
- "fmt"
- "hongze/hongze_yb/models/tables/company"
- "hongze/hongze_yb/models/tables/company_report_permission"
- "hongze/hongze_yb/models/tables/research_report"
- "hongze/hongze_yb/models/tables/research_report_type"
- "hongze/hongze_yb/models/tables/user_view_history"
- "hongze/hongze_yb/models/tables/wx_user"
- "hongze/hongze_yb/utils"
- "strconv"
- "time"
- )
- type ResearchReportInfo struct {
- ResearchReportInfo *research_report.ResearchReport `json:"research_report_info"`
- ResearchReportTypeList []*company_report_permission.ResearchReportTypeList `json:"research_report_type_list"`
- HasMenu int `json:"has_menu"`
- ResearchReportTypeContentList []*research_report.ResearchReportTypeContent `description:"报告详情"`
- }
- // GetResearchReportInfo 获取报告详情
- func GetResearchReportInfo(researchReportId, userId uint64) (result ResearchReportInfo, hasPermission bool, err error) {
- //获取报告详情
- reportInfo, err := research_report.GetByResearchReportId(researchReportId)
- if err != nil {
- return
- }
- reportType := reportInfo.Type
- //这些个报告需要做权限校验
- if utils.InArray(reportInfo.Type, []string{"month", "two_week", "other"}) {
- list, tmpErr := company_report_permission.GetReportVarietyList(userId, reportType)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- for _, v := range list {
- if reportInfo.ResearchReportID == v.ReportChapterTypeId {
- hasPermission = true
- break
- }
- }
- if !hasPermission {
- //permissionName, tmpErr := company_report_permission.GetPermissionNameByReportId(reportInfo.ResearchReportID, reportType)
- //if tmpErr != nil {
- // err = tmpErr
- // return
- //}
- return
- }
- } else {
- hasPermission = true
- }
- researchReportTypeList := make([]*company_report_permission.ResearchReportTypeList, 0)
- tmpResearchReportTypeList, err := company_report_permission.GetResearchReportType(reportInfo.ResearchReportID, userId, reportInfo.Type)
- if err != nil {
- return
- }
- reportDate := reportInfo.ResearchReportDate
- for _, v := range tmpResearchReportTypeList {
- if reportDate.Before(v.PauseStartTime) || reportDate.After(v.PauseEndTime) {
- researchReportTypeList = append(researchReportTypeList, v)
- }
- }
- // 联系人信息
- strInt64 := strconv.FormatUint(userId, 10)
- id, _ := strconv.Atoi(strInt64)
- wxUserInfo, err := wx_user.GetByUserId(id)
- if err != nil {
- fmt.Println("GetByUserId:", err.Error())
- return
- }
- companyInfo, tmpErr := company.GetByCompanyId(wxUserInfo.CompanyID)
- if tmpErr != nil {
- err = tmpErr
- if tmpErr == utils.ErrNoRow {
- err = errors.New("找不到该客户")
- return
- }
- return
- }
- //查询是否读过这篇报告,如果未读过则阅读人数+1
- _, err = user_view_history.GetReportByUserId(userId, reportInfo.ResearchReportID)
- if err != nil {
- err = reportInfo.UpdateViewers()
- if err != nil {
- fmt.Println("UpdateViewers err:", err.Error())
- }
- }
- //新增userViewHistory记录
- userViewHistory := &user_view_history.UserViewHistory{
- ViewHistoryID: 0,
- UserID: userId,
- Mobile: wxUserInfo.Mobile,
- Email: wxUserInfo.Email,
- RealName: wxUserInfo.RealName,
- CompanyName: companyInfo.CompanyName,
- ViewTitle: "",
- ViewPage: "",
- ReportChapterModule: "",
- CreatedTime: time.Now(),
- LastUpdatedTime: time.Now(),
- Type: "weekly_report",
- ResearchReportID: reportInfo.ResearchReportID,
- ResearchReportTypeID: 0,
- }
- err = userViewHistory.AddUserViewHistory()
- if err != nil {
- fmt.Println("AddUserViewHistory err", err.Error())
- }
- result = ResearchReportInfo{
- ResearchReportInfo: reportInfo,
- ResearchReportTypeList: researchReportTypeList,
- HasMenu: 1,
- }
- if len(researchReportTypeList) <= 0 {
- } else if len(researchReportTypeList) == 1 {
- //只有一个章节,即没有目录的时候,需要直接返回章节详情
- result.HasMenu = 0
- researchReportTypeContent, tmpErr := research_report.GetResearchReportTypeContentList(researchReportTypeList[0].ResearchReportTypeId)
- if tmpErr != nil {
- return
- }
- result.ResearchReportTypeContentList = researchReportTypeContent
- }
- return
- }
- type ResearchReportTypeContentInfo struct {
- ResearchReportTypeInfo *research_report_type.ResearchReportTypeInfo `json:"research_report_type_info"`
- Add int `json:"add"`
- ResearchReportTypeContentList []*research_report.ResearchReportTypeContent `description:"报告详情" json:"research_report_type_content_list"`
- }
- // GetResearchReportTypeContentInfo 获取报告章节详情
- func GetResearchReportTypeContentInfo(researchReportTypeId, userId uint64) (result ResearchReportTypeContentInfo, hasPermission bool, err error) {
- //获取章节详情
- researchReportTypeContentList, err := research_report.GetResearchReportTypeContentList(researchReportTypeId)
- if err != nil {
- return
- }
- researchReportTypeInfo, err := research_report_type.GetResearchReportTypeInfo(researchReportTypeId)
- //获取报告详情
- reportInfo, err := research_report.GetByResearchReportId(researchReportTypeInfo.ResearchReportID)
- if err != nil {
- return
- }
- reportType := reportInfo.Type
- //这些个报告需要做权限校验
- if utils.InArray(reportInfo.Type, []string{"week", "month", "two_week", "other"}) {
- list, tmpErr := company_report_permission.GetReportVarietyList(userId, reportType)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- if reportInfo.Type == "week" {
- //周报校验章节是否在权限内
- for _, v := range list {
- if researchReportTypeInfo.ReportChapterTypeId == v.ReportChapterTypeId {
- hasPermission = true
- break
- }
- }
- } else {
- //双周报和月报校验 类型是否在权限内
- for _, v := range list {
- if reportInfo.ResearchReportID == v.ReportChapterTypeId {
- hasPermission = true
- break
- }
- }
- }
- if !hasPermission {
- //permissionName, tmpErr := company_report_permission.GetPermissionNameByReportId(reportInfo.ResearchReportID, reportType)
- //if tmpErr != nil {
- // err = tmpErr
- // return
- //}
- return
- }
- } else {
- hasPermission = true
- }
- add := 1
- if len(researchReportTypeContentList) > 0 {
- add = 0
- }
- // 联系人信息
- strInt64 := strconv.FormatUint(userId, 10)
- id, _ := strconv.Atoi(strInt64)
- wxUserInfo, err := wx_user.GetByUserId(id)
- if err != nil {
- fmt.Println("GetByUserId:", err.Error())
- return
- }
- companyInfo, tmpErr := company.GetByCompanyId(wxUserInfo.CompanyID)
- if tmpErr != nil {
- err = tmpErr
- if tmpErr == utils.ErrNoRow {
- err = errors.New("找不到该客户")
- return
- }
- return
- }
- //查询是否读过这篇章节,如果未读过则阅读人数+1
- _, err = user_view_history.GetReportTypeByUserId(userId, researchReportTypeId)
- if err != nil {
- err = reportInfo.UpdateViewers()
- if err != nil {
- fmt.Println("UpdateViewers err:", err.Error())
- }
- }
- //新增userViewHistory记录
- userViewHistory := &user_view_history.UserViewHistory{
- ViewHistoryID: 0,
- UserID: userId,
- Mobile: wxUserInfo.Mobile,
- Email: wxUserInfo.Email,
- RealName: wxUserInfo.RealName,
- CompanyName: companyInfo.CompanyName,
- ViewTitle: "",
- ViewPage: "",
- ReportChapterModule: "",
- CreatedTime: time.Now(),
- LastUpdatedTime: time.Now(),
- Type: "weekly_report",
- ResearchReportID: reportInfo.ResearchReportID,
- ResearchReportTypeID: researchReportTypeId,
- }
- err = userViewHistory.AddUserViewHistory()
- if err != nil {
- fmt.Println("AddUserViewHistory err", err.Error())
- }
- result = ResearchReportTypeContentInfo{
- ResearchReportTypeContentList: researchReportTypeContentList,
- ResearchReportTypeInfo: researchReportTypeInfo,
- Add: add,
- }
- return
- }
|