package domain

import (
	"fmt"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/constant"
)

var (
	ErrorNotFound = fmt.Errorf("没有此资源")
)

var (
	GenerateMainTable OperationType = "GenerateMainTable" // 主表生成
	SpiltMainTable    OperationType = "SpiltMainTable"    //主表拆分
	AppendData        OperationType = "AppendData"        //数据追加
	EditSubTable      OperationType = "EditSubTable"      //分表编辑
	CopyTable         OperationType = "CopyTable"         //表复制
	RowEdit           OperationType = "RowEdit"           // 编辑记录
	DeleteTable       OperationType = "DeleteTable"       // 表删除
	FileUpload        OperationType = "FileUpload"        // 文件上传
	FileVerify        OperationType = "FileVerify"        // 文件校验

	ExcelTableEdit OperationType = "ExcelTableEdit" // 文档表格编辑
)

var OperationTypeMap = map[string]string{
	GenerateMainTable.ToString(): "主表生成",
	SpiltMainTable.ToString():    "主表拆分",
	AppendData.ToString():        "数据追加",
	EditSubTable.ToString():      "分表编辑",
	CopyTable.ToString():         "表复制",
	RowEdit.ToString():           "编辑记录",
	DeleteTable.ToString():       "表删除",
	FileUpload.ToString():        "文件上传",
	FileVerify.ToString():        "文件校验",
	ExcelTableEdit.ToString():    "文档表格编辑",
}

var (
	VerifiedStepLog LogType = "VerifiedStepLog"
	CommonLog       LogType = "CommonLog"
)

var (
	PKField        int = 1 // 主键字段
	MainTableField int = 2 // 主表字段
	ManualField    int = 3 // 手动添加
)

var (
	MainTable  TableType = "MainTable"
	SideTable  TableType = "SideTable"
	SubTable   TableType = "SubTable"
	ExcelTable TableType = "ExcelTable"
)

var (
	SourceFile    FileType = "SourceFile"
	VerifiedFile  FileType = "VerifiedFile"
	TemporaryFile FileType = "TemporaryFile"
)

var ObjectTypeMap = map[string]string{
	MainTable.ToString():    "主表",
	SideTable.ToString():    "副表",
	SubTable.ToString():     "分表",
	SourceFile.ToString():   "源文件",
	VerifiedFile.ToString(): "校验文件",
	ObjectDBTable:           "业务表",
}

var (
	XLS  = ".xls"
	XLSX = ".xlsx"
)

var (
	String   SQLType = "STRING"
	Int      SQLType = "INT"
	BigInt   SQLType = "BIGINT"
	Float    SQLType = "FLOAT"
	Date     SQLType = "DATE"
	Datetime SQLType = "DATETIME"
)

var SQLTypeMap = map[string]string{
	String.ToString():   "文本",
	Int.ToString():      "整数",
	BigInt.ToString():   "整数",
	Float.ToString():    "小数",
	Date.ToString():     "日期",
	Datetime.ToString(): "日期时间",
}

type FileType string

func (t FileType) ToString() string {
	return string(t)
}

type LogType string

func (t LogType) ToString() string {
	return string(t)
}

type TableType string

func (t TableType) ToString() string {
	return string(t)
}

type ObjectType TableType

type OperationType string

func (t OperationType) ToString() string {
	return string(t)
}

type SQLType string

func (t SQLType) ToString() string {
	return string(t)
}

func (t SQLType) IsString() bool {
	if t == String {
		return true
	}
	if t == Date {
		return true
	}
	if t == Datetime {
		return true
	}
	return false
}

func EnumsDescription(m map[string]string, key string) string {
	if v, ok := m[key]; ok {
		return v
	}
	return ""
}

func DownloadUrl(filename string) string {
	return fmt.Sprintf("%v/%v/%v", constant.METADATA_BASTION_HOST, "static", filename)
}

var DBTables = map[int]*Table{
	DBTableTableOperateLog.ToInt(): &Table{
		TableId:        1,
		TableType:      ObjectDBTable,
		Name:           "日志信息",
		SQLName:        "metadata.logs",
		DataFieldIndex: 6,
		PK: &Field{
			Index:   0,
			Name:    "日志ID",
			SQLName: "log_id",
			SQLType: Int.ToString(),
			Flag:    PKField,
		},
		DataFields: []*Field{
			{
				Index:   1,
				Name:    "数据表表名/文件名",
				SQLName: "object_name",
				SQLType: String.ToString(),
				Flag:    MainTableField,
			},
			{
				Index:   2,
				Name:    "类型",
				SQLName: "object_type",
				SQLType: String.ToString(),
				Flag:    MainTableField,
			},
			{
				Index:   3,
				Name:    "操作类型",
				SQLName: "operation_type",
				SQLType: String.ToString(),
				Flag:    MainTableField,
			},
			{
				Index:   4,
				Name:    "日志内容",
				SQLName: "content",
				SQLType: String.ToString(),
				Flag:    MainTableField,
			},
			{
				Index:   5,
				Name:    "操作时间",
				SQLName: "log_time", //"created_at",
				SQLType: String.ToString(),
				Flag:    MainTableField,
			},
			{
				Index:   6,
				Name:    "操作人",
				SQLName: "operator_name",
				SQLType: String.ToString(),
				Flag:    MainTableField,
			},
		},
	},
}

type DBTable int

func (t DBTable) ToInt() int {
	return int(t)
}

const (
	DBTableTableOperateLog DBTable = 1
	DBTableBusinessLog     DBTable = 2
)

const (
	ObjectFile      = "File"
	ObjectMetaTable = "MetaTable"
	ObjectDBTable   = "DBTable"
)

type LogLevel string

const (
	LevelInfo  LogLevel = "info"
	LevelError LogLevel = "error"
	LevelWarn  LogLevel = "warn"
)

func (t LogLevel) ToString() string {
	return string(t)
}

const (
	DefaultPkField = "id"
)

type Action struct {
	// 操作名
	Desc string `json:"desc"`
	Name string `json:"name"`
}

const (
	RemoveColumn      = "remove-column"
	CopyColumn        = "copy-column"
	RenameColumn      = "rename-column"
	ReplaceColumn     = "replace-column"
	ConvertColumnType = "convert-column-type"

	FormatColumn  = "format-column"
	SplitColumn   = "split-column"
	ExtractColumn = "extract-column"
)

var MapActionCommon = map[string]Action{
	// 常规
	RemoveColumn: {
		Desc: "删除列", Name: RemoveColumn,
	},
	CopyColumn: {
		Desc: "复制列", Name: CopyColumn,
	},
	RenameColumn: {
		Desc: "重命名", Name: RenameColumn,
	},
	ConvertColumnType: {
		Desc: "数据类型更改", Name: ConvertColumnType,
	},
}

var MapActionFormat = map[string]Action{
	// 格式
	"upper": {
		Desc: "大写", Name: "upper",
	},
	"lower": {
		Desc: "小写", Name: "lower",
	},
	"capitalize": {
		Desc: "首字母大写", Name: "capitalize",
	},
	"f4": {
		Desc: "删除列", Name: "cc",
	},
	"strip": {
		Desc: "休整", Name: "strip",
	},
}

var MapActionReplaceColumn = map[string]Action{
	"add-prefix": {
		Desc: "添加前缀", Name: "add-prefix",
	},
	"add-postfix": {
		Desc: "添加后缀", Name: "add-postfix",
	},
	"remove-prefix": {
		Desc: "去除前缀", Name: "remove-prefix",
	},
	"remove-postfix": {
		Desc: "去除后缀", Name: "remove-postfix",
	},
	"remove-chars": {
		Desc: "去除固定字符", Name: "remove-chars",
	},
	"replace": {
		Desc: "替换值", Name: "replace",
	},
	"clean": {
		Desc: "清除", Name: "clean",
	},
}

var MapActionSplitColumn = map[string]Action{
	"separator": {
		Desc: "按按分隔符拆分", Name: "separator",
	},
	"char-length": {
		Desc: "按字符数拆分", Name: "char-length",
	},
}

var MapActionExtractColumn = map[string]Action{
	"by-date": {
		Desc: "按日期提取", Name: "by-date",
	},
	"by-number": {
		Desc: "按数值提取", Name: "by-number",
	},
}