package excel

import (
	"eta/eta_api/utils"
	"time"

	"eta/eta_api/global"
)

type ExcelInfoRuleMapping struct {
	ExcelInfoRuleMappingId int       `orm:"pk" gorm:"primaryKey"  description:"主键"`
	ExcelInfoId            int       `description:"Excel信息ID"`
	RuleType               int       `description:"规则类型"`
	LeftValue              string    `description:"左值"`
	LeftValueBack          string    `description:"左值后端存储"`
	LeftValueType          int       `description:"左值类型"`
	RightValue             string    `description:"右值"`
	RightValueBack         string    `description:"右值后端存储"`
	RightValueType         int       `description:"右值类型"`
	FontColor              string    `description:"字体颜色"`
	BackgroundColor        string    `description:"背景颜色"`
	Remark                 string    `description:"预设颜色说明"`
	RemarkEn               string    `description:"预设颜色英文说明"`
	Scope                  string    `description:"作用范围"`
	ScopeCoord             string    `description:"作用范围坐标"`
	ScopeShow              string    `description:"作用范围坐标前端显示"`
	CreateTime             time.Time `description:"创建时间"`
}

type ExcelInfoRuleMappingView struct {
	ExcelInfoRuleMappingId int    `orm:"pk" gorm:"primaryKey"  description:"主键"`
	ExcelInfoId            int    `description:"Excel信息ID"`
	RuleType               int    `description:"规则类型"`
	LeftValue              string `description:"左值"`
	LeftValueBack          string `description:"左值后端存储" json:"-"`
	LeftValueType          int    `description:"左值类型"`
	RightValue             string `description:"右值"`
	RightValueBack         string `description:"右值后端存储" json:"-"`
	RightValueType         int    `description:"右值类型"`
	FontColor              string `description:"字体颜色"`
	BackgroundColor        string `description:"背景颜色"`
	Remark                 string `description:"预设颜色说明"`
	RemarkEn               string `description:"预设颜色英文说明"`
	Scope                  string `description:"作用范围"`
	ScopeCoord             string `description:"作用范围坐标"`
	ScopeShow              string `description:"作用范围坐标前端显示"`
	CreateTime             string `description:"创建时间"`
}

func (e *ExcelInfoRuleMapping) Insert() (insertId int64, err error) {
	err = global.DbMap[utils.DbNameIndex].Create(e).Error
	return
}

func (e *ExcelInfoRuleMapping) Update(cols []string) (err error) {
	err = global.DbMap[utils.DbNameIndex].Select(cols).Updates(e).Error
	return
}

// GetExcelRuleMappingByExcelInfoId 根据excelInfoId获取规则映射信息
func GetExcelRuleMappingByExcelInfoId(id int) (items []*ExcelInfoRuleMappingView, err error) {
	sql := `SELECT * FROM excel_info_rule_mapping WHERE excel_info_id = ? ORDER BY create_time ASC`
	err = global.DbMap[utils.DbNameIndex].Raw(sql, id).Find(&items).Error
	return
}

// GetExcelRuleMappingById 根据主键Id获取规则映射信息
func GetExcelRuleMappingById(id int) (item *ExcelInfoRuleMappingView, err error) {
	sql := `SELECT * FROM excel_info_rule_mapping WHERE excel_info_rule_mapping_id = ?`
	err = global.DbMap[utils.DbNameIndex].Raw(sql, id).First(&item).Error
	return
}

func DeleteExcelRuleMappingById(id int) (err error) {
	sql := `DELETE FROM excel_info_rule_mapping WHERE excel_info_rule_mapping_id = ?`
	err = global.DbMap[utils.DbNameIndex].Exec(sql, id).Error
	return
}

func (e *ExcelInfoRuleMapping) CreateMulti(items []*ExcelInfoRuleMapping) (err error) {
	if len(items) == 0 {
		return
	}
	err = global.DbMap[utils.DbNameIndex].CreateInBatches(items, utils.MultiAddNum).Error
	return
}

// GetExcelRuleMappingsByExcelInfoId 根据excelInfoId获取规则映射信息
func GetExcelRuleMappingsByExcelInfoId(id int) (items []*ExcelInfoRuleMapping, err error) {
	sql := `SELECT * FROM excel_info_rule_mapping WHERE excel_info_id = ? ORDER BY create_time ASC`
	err = global.DbMap[utils.DbNameIndex].Raw(sql, id).Find(&items).Error
	return
}