123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- package services
- import (
- "errors"
- "eta/eta_mini_crm_ht/models"
- "eta/eta_mini_crm_ht/utils"
- "fmt"
- "strconv"
- "strings"
- "time"
- )
- var (
- tranMediaMap = map[models.MediaType]string{
- models.Video: "视频",
- models.Audio: "音频",
- }
- )
- func GetUnsetProductCountByCondition(productType string, permissionIds []int, condition string, pars []interface{}) (total int, ids []int, err error) {
- setIds, err := models.GetProductSourceIdsByProductType(productType)
- if err != nil {
- return
- }
- switch productType {
- case "report":
- return getReportIdsByCondition(setIds, permissionIds, condition, pars)
- case "media":
- return getMediaIdsByCondition(setIds, permissionIds, condition, pars)
- default:
- err = errors.New("不支持的产品类型查询")
- return
- }
- }
- func getReportIdsByCondition(setIds []int, permissionIds []int, condition string, pars []interface{}) (total int, filterIds []int, err error) {
- permissionNames, err := models.GetPermissionNamesWithRiskLevel(permissionIds)
- var conditionHT string
- var conditionETA string
- if err != nil {
- utils.FileLog.Error("查询品种名称失败", err.Error)
- return
- }
- if len(permissionNames) > 0 {
- conditionHT = "(source ='HT' and plate_name in (" + utils.GetOrmReplaceHolder(len(permissionNames)) + "))"
- }
- filterPermissionIds, err := models.FilterPermissionIdsWithRiskLevel(permissionIds)
- if err != nil {
- utils.FileLog.Error("过滤品种Id失败", err.Error)
- return
- }
- classifyCondition := utils.GetOrmReplaceHolder(len(filterPermissionIds))
- var classifyPars []interface{}
- classifyPars = append(classifyPars, filterPermissionIds)
- classifyIds, err := models.GetClassifyIdsByPermissionIds(classifyCondition, classifyPars)
- if len(classifyIds) > 0 {
- conditionETA = "(source ='ETA' and classify_id in (" + utils.GetOrmReplaceHolder(len(classifyIds)) + "))"
- }
- if conditionHT != "" {
- if conditionETA != "" {
- condition += "and ( " + conditionHT + " or " + conditionETA + ")"
- pars = append(pars, permissionNames)
- pars = append(pars, classifyIds)
- } else {
- condition += "and " + conditionHT
- pars = append(pars, permissionNames)
- }
- } else {
- if conditionETA != "" {
- condition += "and " + conditionETA
- pars = append(pars, classifyIds)
- }
- }
- var ids []int
- ids, err = models.GetReportIdsByCondition(condition, pars)
- if err != nil {
- utils.FileLog.Error("查询报告Id失败", err.Error)
- return
- }
- filterMap := make(map[int]bool)
- for _, filterId := range setIds {
- filterMap[filterId] = true
- }
- for _, id := range ids {
- if !filterMap[id] {
- filterIds = append(filterIds, id)
- total++
- }
- }
- return
- }
- func getMediaIdsByCondition(setIds []int, permissionIds []int, condition string, pars []interface{}) (total int, filterIds []int, err error) {
- filterPermissionIds, err := models.FilterPermissionIdsWithRiskLevel(permissionIds)
- if err != nil {
- utils.FileLog.Error("过滤品种id失败", err.Error)
- return
- }
- mediaIds, err := models.GetMediaIdIdsByPermissionId(filterPermissionIds)
- if err != nil {
- utils.FileLog.Error("过滤媒体Id失败", err.Error)
- return
- }
- if len(mediaIds) > 0 {
- condition += "and id in (" + utils.GetOrmReplaceHolder(len(mediaIds)) + ")"
- pars = append(pars, mediaIds)
- mediaIds, err = models.GetMediaIdsByCondition(condition, pars)
- }
- filterMap := make(map[int]bool)
- for _, filterId := range setIds {
- filterMap[filterId] = true
- }
- for _, id := range mediaIds {
- if !filterMap[id] {
- total++
- filterIds = append(filterIds, id)
- }
- }
- return
- }
- func GetUnsetProductByCondition(productType string, ids []int, sortCondition string, startSize int, pageSize int) (products []*ProductView, err error) {
- switch productType {
- case "report":
- var reports []models.Report
- reports, err = models.GetReportByIds(sortCondition, ids, startSize, pageSize)
- if err != nil {
- utils.FileLog.Error("查询报告产品失败", err.Error)
- return
- }
- for _, report := range reports {
- products = append(products, &ProductView{
- ProductName: report.Title,
- SourceId: report.Id,
- ProductType: "report",
- PublishedTime: report.PublishedTime,
- })
- }
- return
- case "media":
- var medias []models.Media
- medias, err = models.GetMediasById(ids, sortCondition, startSize, pageSize)
- if err != nil {
- utils.FileLog.Error("查询报告产品失败", err.Error)
- return
- }
- for _, media := range medias {
- products = append(products, &ProductView{
- ProductName: media.MediaName,
- SourceId: media.Id,
- ProductType: string(media.MediaType),
- PublishedTime: media.PublishedTime.Format(time.DateTime),
- })
- }
- return
- default:
- err = errors.New("不支持的产品类型查询")
- return
- }
- }
- type ProductView struct {
- Id int
- ProductName string
- SourceId int
- ProductType string
- RiskLevel string
- PublishedTime string
- PermissionNames string
- UpdatedTime string
- Price string
- IsPermanent bool
- ValidDays int
- CoverSrc string
- SaleStatus string
- Creator string
- Description string
- }
- func GetRiskLevel(productType string, id int) (riskLevel string, productName string, permissionName string, err error) {
- switch productType {
- case "report":
- var report *models.Report
- report, err = models.GetReportById(id)
- if err != nil {
- utils.FileLog.Error("获取报告失败,无法确认风险等级", err.Error())
- return
- }
- switch report.Source {
- case models.SourceHT:
- var permission models.Permission
- permission, err = models.GetPermissionByName(report.PlateName)
- if err != nil {
- utils.FileLog.Error("获取品种信息,无法确认风险等级", err.Error())
- return
- }
- return permission.RiskLevel, report.Title, permission.Name, nil
- case models.SourceETA:
- var permissionIds []int
- permissionIds, err = models.GetByPermissionIdsByClassifyId(report.ClassifyId)
- if err != nil {
- utils.FileLog.Error("获取品种信息,无法确认风险等级", err.Error())
- return
- }
- var permissions []models.Permission
- var permissionNameArr []string
- permissions, err = models.GetPermissionsWithRiskLevel(permissionIds)
- for _, permission := range permissions {
- permissionNameArr = append(permissionNameArr, permission.Name)
- }
- permissionName = strings.Join(permissionNameArr, ",")
- if err != nil {
- utils.FileLog.Error("获取品种信息,无法确认风险等级", err.Error())
- return
- }
- var riskNum int
- for _, permission := range permissions {
- if permission.RiskLevel == "" {
- continue
- }
- var parseErr error
- currentNum, parseErr := strconv.Atoi(permission.RiskLevel[1:])
- if parseErr != nil {
- continue
- }
- if currentNum > riskNum {
- riskNum = currentNum
- }
- }
- if riskNum > 0 {
- return fmt.Sprintf("R%d", riskNum), report.Title, permissionName, nil
- }
- return
- }
- case "media":
- var media *models.Media
- media, err = models.GetMediaById("", id)
- if err != nil {
- utils.FileLog.Error("获取媒体信息,无法确认风险等级", err.Error())
- return
- }
- var permissionsIds []int
- permissionsIds, err = models.GetPermissionIdsByMediaId(media.MediaType, id)
- if err != nil {
- utils.FileLog.Error("获取媒体信息,无法确认风险等级", err.Error())
- return
- }
- var permissions []models.Permission
- var permissionNameArr []string
- permissions, err = models.GetPermissionsWithRiskLevel(permissionsIds)
- for _, permission := range permissions {
- permissionNameArr = append(permissionNameArr, permission.Name)
- }
- permissionName = strings.Join(permissionNameArr, ",")
- if err != nil {
- utils.FileLog.Error("获取媒体信息,无法确认风险等级", err.Error())
- return
- }
- var riskNum int
- for _, permission := range permissions {
- if permission.RiskLevel == "" {
- continue
- }
- var parseErr error
- currentNum, parseErr := strconv.Atoi(permission.RiskLevel[1:])
- if parseErr != nil {
- continue
- }
- if currentNum > riskNum {
- riskNum = currentNum
- }
- }
- if riskNum > 0 {
- return fmt.Sprintf("R%d", riskNum), media.MediaName, permissionName, nil
- }
- return
- case "package":
- var permission *models.Permission
- permission, err = models.GetPermissionByPermissionId(id)
- if err != nil {
- utils.FileLog.Error("获取套餐品种信息,无法确认风险等级", err.Error())
- return
- }
- if permission.RiskLevel == "" {
- return "", "", "", errors.New("当前品种未设置风测等级")
- }
- return permission.RiskLevel, permission.Name, "", nil
- default:
- var media *models.Media
- media, err = models.GetMediaById(models.MediaType(productType), id)
- if err != nil {
- utils.FileLog.Error("获取媒体信息,无法确认风险等级", err.Error())
- return
- }
- var permissionsIds []int
- permissionsIds, err = models.GetPermissionIdsByMediaId(media.MediaType, id)
- if err != nil {
- utils.FileLog.Error("获取媒体信息,无法确认风险等级", err.Error())
- return
- }
- var permissions []models.Permission
- permissions, err = models.GetPermissionsWithRiskLevel(permissionsIds)
- if err != nil {
- utils.FileLog.Error("获取媒体信息,无法确认风险等级", err.Error())
- return
- }
- var riskNum int
- var permissionNameArr []string
- for _, permission := range permissions {
- permissionNameArr = append(permissionNameArr, permission.Name)
- if permission.RiskLevel == "" {
- continue
- }
- var parseErr error
- currentNum, parseErr := strconv.Atoi(permission.RiskLevel[1:])
- if parseErr != nil {
- continue
- }
- if currentNum > riskNum {
- riskNum = currentNum
- }
- }
- permissionName = strings.Join(permissionNameArr, ",")
- if riskNum > 0 {
- return fmt.Sprintf("R%d", riskNum), media.MediaName, permissionName, nil
- }
- return
- }
- return
- }
|