package report

import (
	"errors"
	logger "eta/eta_mini_ht_api/common/component/log"
	"eta/eta_mini_ht_api/common/utils/date"
	silce_utils "eta/eta_mini_ht_api/common/utils/silce"
	"eta/eta_mini_ht_api/models"
	"fmt"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"time"
)

type ReportStatus string
type ReportSource string
type SendStatus string

const (
	SEND            SendStatus   = "SEND"
	UNSEND          SendStatus   = "UNSEND"
	SourceETA       ReportSource = "ETA"
	SourceHT        ReportSource = "HT"
	StatusPublish                = "PUBLISH"
	StatusUnPublish ReportStatus = "UNPUBLISH"

	MaxBatchNum   = 1000
	CommonColumns = "id,org_id,author,abstract,title,source,cover_src,published_time,status"
	taskColumns   = "id,author,published_time,status"
)

type Report struct {
	ID            int          `gorm:"column:id;primary_key;comment:'id'" json:"id"`
	OrgID         int          `gorm:"column:org_id;comment:'原始id'" json:"org_id"`
	Source        ReportSource `gorm:"column:source;comment:'研报来源1:eta 2:海通'" json:"source"`
	Title         string       `gorm:"column:title;comment:'标题'" json:"title"`
	Abstract      string       `gorm:"column:abstract;comment:'摘要'" json:"abstract"`
	PlateName     string       `gorm:"column:plate_name;comment:'板块'" json:"plate_name"`
	Author        string       `gorm:"column:author;comment:'作者'" json:"author"`
	CoverSrc      int          `gorm:"column:cover_src;comment:'封面图片'" json:"cover_src"`
	Status        ReportStatus `gorm:"column:status;comment:'报告状态 publish:发布 unpublish:未发布" json:"status"`
	SendStatus    SendStatus   `gorm:"column:send_status;comment:'发送状态'" json:"send_status"`
	PublishedTime string       `gorm:"column:published_time;comment:'发布时间'" json:"published_time"`
	CreatedTime   time.Time    `gorm:"column:created_time;comment:'创建时间'" json:"created_time"`
	UpdatedTime   time.Time    `gorm:"column:updated_time;comment:'修改时间'" json:"updated_time"`
}

func GetIdsByPlateNames(plateName []string) (ids []int, err error) {
	db := models.Main()
	err = db.Model(&Report{}).Select("distinct org_id").Where(" source ='HT' and plate_name in ?  ", plateName).Scan(&ids).Error
	return
}
func BatchInsertReport(list *[]Report) (err error) {
	db := models.Main()
	//手动事务
	tx := db.Begin()
	err = tx.CreateInBatches(list, MaxBatchNum).Error
	if err != nil {
		logger.Error("批量插入研报失败:%v", err)
		tx.Rollback()
		return
	}
	tx.Commit()
	return nil
}

func InsertOrUpdateReport(list []Report, source string) (result []Report, err error) {
	var orgIds []int
	//现有的作者名字
	for _, report := range list {
		orgIds = append(orgIds, report.OrgID)
	}
	orgIds = silce_utils.RemoveDuplicates(orgIds)
	db := models.Main()
	//数据库找到作者的名字
	//var dbAuthors []string
	//err = db.Model(&Report{}).Select("distinct author").Where("org_id in ? and source =? ", orgIds, source).Scan(&dbAuthors).Error
	if err != nil {
		logger.Error("查询研报失败:%v", err)
	}
	//var deleteAuthors []string
	//for _, dbAuthor := range dbAuthors {
	//	found := false
	//	for _, insertAuthor := range authorNames {
	//		if dbAuthor == insertAuthor {
	//			found = true
	//			break
	//		}
	//	}
	//	if !found {
	//		deleteAuthors = append(deleteAuthors, dbAuthor)
	//	}
	//}
	//手动事务
	tx := db.Begin()
	OnConflictFunc := clause.OnConflict{
		Columns:   []clause.Column{{Name: "org_id"}, {Name: "source"}},
		DoUpdates: clause.AssignmentColumns([]string{"abstract", "title", "author", "published_time", "status"}),
	}
	// 执行批量插入或更新操作
	err = tx.Clauses(OnConflictFunc).Create(&list).Error
	//if deleteAuthors != nil {
	//	for _, deleteAuthor := range deleteAuthors {
	//		err = tx.Where("org_id in ? and source =? and author=?", orgIds, source, deleteAuthor).Delete(&Report{}).Error
	//		if err != nil {
	//			logger.Error("批量删除研报失败:%v", err)
	//			tx.Rollback()
	//			return
	//		}
	//	}
	//}
	if err != nil {
		logger.Error("批量插入或更新研报失败:%v", err)
		tx.Rollback()
		return
	}
	tx.Commit()
	err = db.Select(CommonColumns).Where("org_id in ? and source =? ", orgIds, source).Find(&result).Error
	if err != nil {
		logger.Error("查询更新的研报数据失败:%v", err)
		err = nil
	}
	return
}
func (t *Report) BeforeCreate(_ *gorm.DB) (err error) {
	t.CreatedTime = time.Now()
	return
}

func GetAuthorByOrgId(orgId int, source string) (names []string, err error) {
	db := models.Main()
	err = db.Model(&Report{}).Select("author").Where("org_id = ? and source =? ", orgId, source).Scan(&names).Error
	return
}

func GetReportByOrgId(orgId int, source string) (reports []Report, err error) {
	db := models.Main()
	err = db.Select(CommonColumns).Where("org_id = ? and source =? ", orgId, source).Find(&reports).Error
	return
}
func GetReportById(reportId int) (report Report, err error) {
	db := models.Main()
	err = db.Select(CommonColumns).Where("id = ?", reportId).First(&report).Error
	if err != nil {
		logger.Error("查询报告失败:%v", err)
	}
	return
}
func GetLatestReportIdBySource(source ReportSource) (id int, err error) {
	sql := "select IFNULL(max(org_id),0)  from reports where source = ?"
	err = DoSql(sql, &id, source)
	return
}
func DoSql(sql string, result interface{}, values ...interface{}) (err error) {
	db := models.Main()
	err = db.Raw(sql, values...).Scan(result).Error
	if err != nil {
		logger.Error("执行sql[%v]失败:%v", sql, err)
	}
	return
}

func GetListOrderByCondition(week bool, column string, limit int, order models.Order) (reports []Report, err error) {
	db := models.Main()
	if week {
		end := time.Now()
		begin := date.GetBeginOfTheWeek(end, time.Monday)
		err = db.Select(CommonColumns).Where("status = ?", StatusPublish).Where("DATE(published_time) BETWEEN ? AND ?", begin, end).Order(fmt.Sprintf("%s %s", column, order)).Limit(limit).Find(&reports).Error
	} else {
		err = db.Select(CommonColumns).Where("status = ?", StatusPublish).Order(fmt.Sprintf("%s %s", column, order)).Limit(limit).Find(&reports).Error
	}
	if err != nil {
		logger.Error("查询报告列表失败:%v", err)
	}
	if reports == nil {
		return []Report{}, nil
	}
	return
}

func GetListByCondition[T any](column string, values []T) (reports []Report, err error) {
	if len(values) == 0 {
		logger.Error("查询条件的值不能为空")
		return []Report{}, nil
	}
	db := models.Main()
	err = db.Select(CommonColumns).Where(fmt.Sprintf("%s in ?", column), values).Find(&reports).Error
	if err != nil {
		logger.Error("查询报告列表失败:%v", err)
	}
	if reports == nil {
		return []Report{}, nil
	}
	return
}
func GetMaxIdByPermissionIds(orgIds map[string][]int) (total int64, maxId int64, err error) {
	db := models.Main()
	if len(orgIds["ETA"]) == 0 && len(orgIds["HT"]) == 0 {
		//err := db.Model(&Report{}).Select("MAX(id) id").Where("status = ?", StatusPublish).Scan(&maxId).Error
		//if err != nil {
		//	logger.Error("获取报告最大ID失败:%v", err)
		//	return 0
		//}
		maxId = 0
		total = 0
		return
	}
	if len(orgIds["ETA"]) == 0 {
		err = db.Model(&Report{}).Select("count(*)").Where("status = ?", StatusPublish).Where(" source='HT' and org_id in ?", orgIds["HT"]).Scan(&total).Error
		if err != nil {
			logger.Error("获取记录条数失败:%v", err)
			return
		}
		err = db.Model(&Report{}).Select("MAX(id) id").Where("status = ?", StatusPublish).Where(" source='HT' and org_id in ?", orgIds["HT"]).Scan(&maxId).Error
		if err != nil {
			logger.Error("获取报告最大ID失败:%v", err)
			return
		}
		return
	}
	if len(orgIds["HT"]) == 0 {
		err = db.Model(&Report{}).Select("count(*)").Where("status = ?", StatusPublish).Where(" source='ETA' and org_id in ?", orgIds["ETA"]).Scan(&maxId).Error
		if err != nil {
			logger.Error("获取报告最大ID失败:%v", err)
			return
		}
		err = db.Model(&Report{}).Select("MAX(id) id").Where("status = ?", StatusPublish).Where(" source='ETA' and org_id in ?", orgIds["ETA"]).Scan(&maxId).Error
		if err != nil {
			logger.Error("获取报告最大ID失败:%v", err)
			return
		}
		return
	}
	err = db.Model(&Report{}).Select("count(*)").Where("status = ?", StatusPublish).Where(" source='ETA' and org_id in ?", orgIds["ETA"]).Or("source='HT' and org_id in ?", orgIds["HT"]).Scan(&maxId).Error
	if err != nil {
		logger.Error("获取报告最大ID失败:%v", err)
		return
	}
	err = db.Model(&Report{}).Select("MAX(id) id").Where("status = ?", StatusPublish).Where(" source='ETA' and org_id in ?", orgIds["ETA"]).Or("source='HT' and org_id in ?", orgIds["HT"]).Scan(&maxId).Error
	if err != nil {
		logger.Error("获取报告最大ID失败:%v", err)
		return
	}
	return
}
func GetTotalPageCount() (total int64, latestId int64, err error) {
	db := models.Main()
	err = db.Model(&Report{}).Select("MAX(id) id").Where("status = ?", StatusPublish).Scan(&latestId).Error
	if err != nil {
		logger.Error("获取最大id失败:%v", err)
	}
	err = db.Model(&Report{}).Where("status = ?", StatusPublish).Count(&total).Error
	if err != nil {
		logger.Error("统计报告数量失败:%v", err)
	}
	return
}

func GetTotalPageCountByAnalyst(analyst string) (total int64, latestId int64) {
	db := models.Main()
	err := db.Model(&Report{}).Where("status = ?", StatusPublish).Where("author like ?", "%"+analyst+"%").Count(&total).Error
	if err != nil {
		return
	}
	err = db.Model(&Report{}).Select("Max(id)").Where("status = ?", StatusPublish).Where("author like ?", "%"+analyst+"%").Scan(&latestId).Error
	if err != nil {
		return
	}
	return
}
func GetReportPage(latestId int64, limit int, offset int) (list []Report, err error) {
	if latestId < 0 {
		err = errors.New("非法的id参数")
		logger.Error("非法的id参数:%d", latestId)
		return
	}
	if limit <= 0 {
		err = errors.New("非法的limit参数")
		logger.Error("非法的limit参数:%d", limit)
	}
	db := models.Main()
	err = db.Select(CommonColumns).Where("id<= ?", latestId).Where("status = ?", StatusPublish).Order("published_time desc").Limit(limit).Offset(offset).Find(&list).Error
	return
}

func GetReportPageByOrgIds(latestId int64, limit int, offset int, orgIds map[string][]int, searchAll bool) (list []Report, err error) {
	//有传入品种 但是没有筛选出来的已经过滤掉了，现在orgIds为空只有是
	if searchAll {
		return GetReportPage(latestId, limit, offset)
	}
	//if len(orgIds["ETA"]) == 0 && len(orgIds["HT"]) == 0 {
	//
	//}
	if latestId < 0 {
		err = errors.New("非法的id参数")
		logger.Error("非法的id参数:%d", latestId)
		return
	}
	if limit <= 0 {
		err = errors.New("非法的limit参数")
		logger.Error("非法的limit参数:%d", limit)
	}
	db := models.Main()
	if len(orgIds["ETA"]) == 0 {
		err = db.Select(CommonColumns).Where("id<= ?", latestId).Where("status = ?", StatusPublish).Where(" source='HT' and org_id in ?", orgIds["HT"]).Order("published_time desc").Limit(limit).Offset(offset).Find(&list).Error
		return
	}
	if len(orgIds["HT"]) == 0 {
		err = db.Select(CommonColumns).Where("id<= ?", latestId).Where("status = ?", StatusPublish).Where("source='ETA' and org_id in ?", orgIds["ETA"]).Order("published_time desc").Limit(limit).Offset(offset).Find(&list).Error
		return
	}
	err = db.Select(CommonColumns).Where("id<= ?", latestId).Where("status = ?", StatusPublish).Where("(source='ETA' and org_id in ? ) or (source='HT' and org_id in ?) ", orgIds["ETA"], orgIds["HT"]).Order("published_time desc").Limit(limit).Offset(offset).Find(&list).Error
	return
}

func GetNewReportByPublishTime(time time.Time) (list []Report) {
	db := models.Main()
	err := db.Select(taskColumns).Where("status = ?", StatusPublish).Where("published_time >= ?", time).Order("published_time desc").Find(&list).Error
	if err != nil {
		logger.Error("查询新发布的报告列表失败:%v", err)
	}
	return
}
func GetReportPageByAnalyst(latestId int64, limit int, offset int, analyst string) (list []Report, err error) {
	if latestId < 0 {
		err = errors.New("非法的id参数")
		logger.Error("非法的id参数:%d", latestId)
		return
	}
	if limit <= 0 {
		err = errors.New("非法的limit参数")
		logger.Error("非法的limit参数:%d", limit)
	}
	db := models.Main()
	err = db.Select(CommonColumns).Where("status = ?", StatusPublish).Where("id<= ? and author like  ?", latestId, "%"+analyst+"%").Order("published_time desc").Limit(limit).Offset(offset).Find(&list).Error
	return
}
