package services

import (
	"fmt"
	"hongze/hongze_cygx/models"
	"hongze/hongze_cygx/utils"
	"strconv"
	"time"
)

func GetReportMappingMap() (mapResp map[int]bool, err error) {
	defer func() {
		if err != nil {
			go utils.SendAlarmMsg("GetReportMappingMap, ErrMsg: "+err.Error(), 3)
		}
	}()
	mapResp = make(map[int]bool, 0)
	list, err := models.GetReportMappingByPermissionName(utils.CE_LUE_NAME)
	if err != nil && err.Error() != utils.ErrNoRow() {
		return
	}
	if len(list) > 0 {
		for _, v := range list {
			mapResp[v.CategoryId] = true
		}
	}
	return

}

// 生成查研观向的报告匹配类型
func init111() {
	list, err := models.GetReportMapping()
	if err != nil {
		fmt.Print(err)
	}
	var condition string
	for _, v := range list {
		if v.MatchTypeName == "" {
			continue
		}
		item := new(models.CygxReportMappingCygx)
		item.ChartPermissionId = v.ChartPermissionId
		item.ChartPermissionName = v.ChartPermissionName
		item.MatchTypeName = v.MatchTypeName
		item.ReportType = v.ReportType
		item.Sort = v.Sort
		item.IsCustom = v.IsCustom
		item.IsSummary = v.IsSummary
		item.IsReport = v.IsReport
		item.PermissionType = v.PermissionType
		condition = ` AND match_type_name = '` + v.MatchTypeName + `' AND chart_permission_id =  ` + strconv.Itoa(v.ChartPermissionId)
		total, err := models.GetCygxReportMappingCygxCount(condition)
		if err != nil {
			fmt.Print(err)
		}
		if total == 0 {
			_, err = models.AddCygxReportMappingCygx(item)
			if err != nil {
				fmt.Print(err)
			}
		}
	}
}

// 生成多对多关系表
func init1232() {
	list, err := models.GetCygxReportMappingCygx()
	if err != nil {
		fmt.Print(err)
	}
	var condition string
	for _, v := range list {
		if v.MatchTypeName == "" {
			continue
		}
		condition = ` AND match_type_name = '` + v.MatchTypeName + `' AND chart_permission_id =  ` + strconv.Itoa(v.ChartPermissionId)
		listCelue, err := models.GetCygxReportMappingcelue(condition)
		if err != nil {
			fmt.Print(err)
		}
		for _, vC := range listCelue {
			item := new(models.CygxReportMappingGroup)
			item.IdCygx = v.Id
			item.CategoryIdCelue = vC.CategoryId
			item.CreateTime = time.Now()
			item.ModifyTime = time.Now()
			fmt.Println(item)
			_, err = models.AddCygxReportMappingGroup(item)
			if err != nil {
				fmt.Print(err)
			}
		}

	}
}
