package services

import (
	"errors"
	"fmt"
	"hongze/hongze_cygx/models"
	"hongze/hongze_cygx/utils"
	"strings"
	"time"
)

//func init() {
//	var condition string
//	var pars []interface{}
//	condition += ` AND publish_status  = 1 `
//
//	list, err := models.GetArticleList(condition, pars)
//	fmt.Println(err)
//	for _, v := range list {
//		AddCygxReportMappingCategoryGroupByArticleId(v.ArticleId)
//	}
//}

// AddCygxReportMappingCategoryGroupByArticleId  根据文章ID建立分类分组，主客观权限分组
func AddCygxReportMappingCategoryGroupByArticleId(articleId int) {
	fmt.Println(articleId)
	time.Sleep(5 * time.Second)
	var err error
	defer func() {
		if err != nil {
			go utils.SendAlarmMsg("根据文章ID建立分类分组，主客观权限分组，失败"+err.Error(), 2)
		}
	}()
	detail, e := models.GetArticleDetailById(articleId)
	if e != nil {
		err = errors.New("GetArticleDetailById, Err: " + e.Error())
		return
	}
	categoryId := detail.CategoryId

	var condition string
	var pars []interface{}
	condition += ` AND category_id_celue = ? `
	pars = append(pars, categoryId)
	list, e := models.GetCygxReportMappingGroupList(condition, pars)
	if e != nil {
		err = errors.New("GetCygxReportMappingGroupList, Err: " + e.Error())
		return
	}
	pars = make([]interface{}, 0)
	var idCygxs []int
	for _, v := range list {
		idCygxs = append(idCygxs, v.IdCygx)
		fmt.Println(v)
	}
	condition = ` AND id  IN (` + utils.GetOrmInReplace(len(idCygxs)) + `)`
	pars = append(pars, idCygxs)
	cygxlist, e := models.GetCygxReportMappingCygxByCon(condition, pars)
	if e != nil {
		err = errors.New("GetCygxReportMappingCygxByCon, Err: " + e.Error())
		return
	}
	var items []*models.CygxReportMappingCategoryGroup
	for _, v := range cygxlist {
		item := new(models.CygxReportMappingCategoryGroup)
		item.IdCygx = v.Id
		item.ChartPermissionId = v.ChartPermissionId
		//如果类型是报告就是主观，类型是纪要就是客观
		if detail.TypeName == "报告" {
			item.PermissionType = 1
		}
		if detail.TypeName == "纪要" {
			item.PermissionType = 2
		}
		item.ArticleId = articleId
		item.CreateTime = time.Now()
		item.ModifyTime = time.Now()
		items = append(items, item)
	}
	e = models.AddCygxReportMappingCategoryGroupMulti(items, articleId)
	fmt.Println(items)
	return

}

// 校验文章主客观权限
func CheckArticlePermissionType(articleId int, user *models.WxUserItem) (checkPermissionType bool) {
	var err error
	defer func() {
		if err != nil {
			go utils.SendAlarmMsg("记录用户文章浏览记录，失败"+err.Error(), 2)
		}
	}()
	permissionStr, e := models.GetCompanyPermissionByUser(user.CompanyId)
	if e != nil {
		err = errors.New("GetCompanyPermissionByUser, Err: " + e.Error())
		return
	}
	var condition string
	var pars []interface{}
	condition += ` AND article_id = ? `
	pars = append(pars, articleId)
	list, e := models.GetCygxReportMappingCategoryGroupList(condition, pars)
	if e != nil {
		err = errors.New("GetCygxReportMappingCategoryGroupList, Err: " + e.Error())
		return
	}
	if len(list) == 0 {
		checkPermissionType = true
		return
	}
	var chartPermissionId int
	var chartPermissionName string
	var permissionTypeZg bool
	var permissionTypeKg bool
	for _, v := range list {
		chartPermissionId = v.ChartPermissionId
		if v.PermissionType == 1 || v.PermissionType == 0 {
			permissionTypeZg = true
		}
		if v.PermissionType == 2 || v.PermissionType == 0 {
			permissionTypeKg = true
		}
	}

	categoryinfo, e := models.GetChartPermissionById(chartPermissionId)
	if e != nil {
		err = errors.New("GetChartPermissionById, Err: " + e.Error())
		return
	}

	if permissionTypeZg {
		chartPermissionName = categoryinfo.PermissionName + "(主观)"
	}
	if permissionTypeKg {
		chartPermissionName = categoryinfo.PermissionName + "(客观)"
	}

	if permissionTypeKg && permissionTypeZg {
		chartPermissionName = categoryinfo.PermissionName
	}

	if strings.Contains(permissionStr, chartPermissionName) {
		checkPermissionType = true
	}
	return
}

//func init() {
//	var condition string
//	var pars []interface{}
//	condition += ` AND  id_cygx IN  (23) `
//
//	list, e := models.GetCygxReportMappingCategoryGroupList(condition, pars)
//	if e != nil {
//		fmt.Println(e)
//		return
//	}
//	var items []*models.CygxReportMappingCategoryGroup
//
//	for _, v := range list {
//		item := new(models.CygxReportMappingCategoryGroup)
//		item.ChartPermissionId = v.ChartPermissionId
//		item.ArticleId = v.ArticleId
//		item.IdCygx = 40
//		item.CreateTime = time.Now()
//		item.ModifyTime = time.Now()
//		item.PermissionType = v.PermissionType
//		items = append(items, item)
//	}
//	fmt.Println(len(items))
//	for _, v := range items {
//		fmt.Println(v)
//	}
//
//	e = models.AddCygxReportMappingCategoryGroupMultiInit(items)
//	fmt.Println(e)
//}
