package fe_calendar_matter

import (
	"fmt"
	"hongze/hongze_yb/global"
	"hongze/hongze_yb/utils"
)

type FeCalendarMatterItem struct {
	FeCalendarMatterId  int    `description:"事项ID" json:"fe_calendar_matter_id"`
	ChartPermissionId   int    `description:"品种ID" json:"chart_permission_id"`
	ChartPermissionName string `description:"品种名称" json:"chart_permission_name"`
	MatterDate          string `description:"事项日期" json:"matter_date"`
	Title               string `description:"标题" json:"title"`
	MatterType          int    `description:"事项类型：1-自定义事项；2-基础指标；3-预测指标" json:"matter_type"`
	EdbInfoId           int    `description:"指标ID" json:"edb_info_id"`
	EdbUniqueCode       string `description:"指标唯一编码" json:"edb_unique_code"`
	EdbCode             string `description:"指标编码" json:"edb_code"`
	FontColor           string `description:"字体颜色" json:"font_color"`
	FillingColor        string `description:"填充颜色" json:"filling_color"`
	FontBold            int    `description:"字体加粗：0-否；1-是" json:"font_bold"`
	Sort                int    `description:"排序" json:"sort"`
}

func FormatFeCalendarMatter2Item(origin *FeCalendarMatter) (item *FeCalendarMatterItem) {
	if origin == nil {
		return
	}
	item = new(FeCalendarMatterItem)
	item.FeCalendarMatterId = origin.FeCalendarMatterId
	item.ChartPermissionId = origin.ChartPermissionId
	item.ChartPermissionName = origin.ChartPermissionName
	item.MatterDate = utils.TimeTransferString(utils.FormatDate, origin.MatterDate)
	item.Title = origin.Title
	item.MatterType = origin.MatterType
	item.EdbInfoId = origin.EdbInfoId
	item.EdbUniqueCode = origin.EdbUniqueCode
	item.EdbCode = origin.EdbCode
	item.FontColor = origin.FontColor
	item.FillingColor = origin.FillingColor
	item.FontBold = origin.FontBold
	item.Sort = origin.Sort
	return
}

// FeCalendarMatterListItem 事项列表
type FeCalendarMatterListItem struct {
	Date    string                  `description:"日期" json:"date"`
	Matters []*FeCalendarMatterItem `description:"日期事项" json:"matters"`
}

func (m *FeCalendarMatter) GetItemByCondition(cond string, pars []interface{}) (item *FeCalendarMatter, err error) {
	err = global.MYSQL["data"].Where(cond, pars...).First(&item).Error
	return
}

func (m *FeCalendarMatter) GetItemsByCondition(cond string, pars []interface{}, fieldArr []string, orderRule string) (items []*FeCalendarMatter, err error) {
	order := fmt.Sprintf(`%s DESC`, FeCalendarMatterColumns.CreateTime)
	if orderRule != "" {
		order = orderRule
	}
	err = global.MYSQL["data"].Where(cond, pars...).Order(order).Find(&items).Error
	return
}

func (m *FeCalendarMatter) GetLastestMonthItemByPermissionId(permissionId int) (item *FeCalendarMatter, err error) {
	err = global.MYSQL["data"].Where("chart_permission_id = ?", permissionId).Order("matter_date DESC").First(&item).Error
	return
}

func (m *FeCalendarMatter) GetLaststModifyMatter() (item *FeCalendarMatter, err error) {
	err = global.MYSQL["data"].Order("modify_time DESC").First(&item).Error
	return
}

func (m *FeCalendarMatter) GetAllPermissionIds() (chartPermissionIds []int, err error) {
	err = global.MYSQL["data"].Model(m).Select(" distinct chart_permission_id ").Scan(&chartPermissionIds).Error
	return
}
