package fake

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

type Rule struct {
	Item      string
	Name      string
	Summary   string
	Content   string
	Close     bool
	Proiority int
	Case      string
	Position  int
	Threshold string
}

type Rules []Rule

// GetRules return all rules
func GetRules(c *gin.Context) {
	data := Rules{
		{"DML.006", "injection check table exist", "目标库中表不存在", "", false, 1, "", 0, "0"},
		{"DML.030", "Select without where", "查询条件缺少where条件，将查询全表的数据", "", false, 2, "select * from projectfile;", 0, "031"},
		{"Alter.010", "Table adds column char type field", "表新增列char类型字段", "", false, 1, "ALTER TABLE db_injection_task ADD COLUMN `device` CHAR(32) NOT NULL COMMENT '设备';", 0, "0"},
		{"Create.018", "injection varchar length no more than 1024", "varchar的长度不能超过1024，建议使用text", "", false, 1, "", 0, "1024"},
		{"DML.005", "injection check insert value amount", "插入列列表与值列表个数不相同", "", false, 1, "", 0, "0"},
		{"Create.004", "injection check column comment", "列必须要有注释", "", false, 1, "", 0, "0"},
		{"Create.034", "injection check column use ENUM ", "不建议使用ENUM数据类型，建议使用tinyint代替，避免发生混淆", "", false, 1, "alter table test1 add column user ENUM('women','men','N') NOT NULL DEFAULT 'N' COMMENT '用户性别';", 0, ""},
		{"Alter002", "injection check drop or truncate table", "禁止drop、truncate表操作", "", false, 1, "", 0, "0"},
		{"Alter004", "injection don't add multi index or column once", "不支持同时创建多个索引或者列", "", false, 1, "", 0, "0"},
		{"DML.033", "SQL use Count(*)、Count(col)", "COUNT(字段)相对COUNT(*)可能性能会更差", "", false, 2, "SELECT COUNT(col_name) FROM table_name;", 0, "0"},
		{"Create.017", "injection can't use int as primary key bigInt expect", "不能使用int作为主键，请换成bigInt", "", false, 1, "", 0, "0"},
		{"Create.031", "Create table has more than one autoincrement column", "create table超过一个自增列", "", false, 1, "create table tablename(id int auto_increment primary key,id2 int auto_increment primary key); ", 0, "1"},
		{"Alter.023", "Prohibit JOIN operations between views.", "禁止在视图之间进行JOIN操作。", "", false, 1, "select id ,projectid from view1 inner join view2 on view1.id = view2.id;", 0, "0"},
		{"Create.002", "injection check table comment", "表必须要有注释", "", false, 1, "", 0, "0"},
		{"Alter005", "injection unsupported modify type", "不支持的类型更改", "", false, 1, "", 0, "0"},
		{"DML.008", "injection index match ", "删改数据索引不完全匹配", "", false, 1, "", 0, "0"},
		{"Create.006", "injection check index name", "普通索引必须要以idx_为前缀，比如idx_ab", "", false, 1, "", 0, "idx_"},
		{"Create.019", "auto increment start value limit 1w", "自增主键起始值不能超过10000", "", false, 1, "", 0, "10000"},
		{"Create.010", "injection check column not null", "每个列使用not null，text字段除外；text不能有默认值", "", false, 1, "", 0, "0"},
		{"Create.030", "The field uses unsigned bigint", "字段使用了unsigned bigint", "", false, 1, "ALTER TABLE db_injection_task ADD COLUMN `device` BIGINT UNSIGNED NOT NULL COMMENT '设备';", 0, "1"},
		{"Alter.013", "Index name is required to create an index", "创建索引必须要有索引名称", "", false, 1, "ALTER TABLE  table_name  ADD INDEX  ( `func`,`gene`,`genedetail`,`genedetail2`); ", 0, "0"},
		{"Create.008", "injection check index amount", "建表时最多有5个索引", "", false, 1, "", 0, "5"},
		{"Alter.014", "The column uses a varchar type that is too small", "列使用长度过小的varchar", "", false, 1, "ALTER TABLE db_injection_task ADD COLUMN `device` VARCHAR(4) NOT NULL COMMENT 'qwe';", 0, "5"},
		{"Alter.021", "It is forbidden to update, delete, and insert data on the view.", "禁止对视图进行更新、删除、插入数据操作。", "", false, 1, "INSERT INTO view1 (id) VALUES (15);delete from view1 where id =1;", 0, "0"},
		{"DML.009", "injection affect no more than 100", "删改数据影响行数超过100", "", false, 1, "", 0, "100"},
		{"DML.029", " add Calculation to the filter column after where", "SQL中where后的过滤列上加函数、运算将导致无法利用该字段上的索引", "", false, 2, "select * from  data_market.mkt_order_by_month_userid_type where  concat(year_id,'-',if(month_id\u003c10,'0',''),month_id)='2011-09';", 0, "0"},
		{"Create.012", "injection check joint index order", "如果是联合索引，时间字段，不能放在第一列", "", false, 1, "", 0, "0"},
		{"Alter003", "injection affect rows", "影响行数超过3000", "", false, 1, "", 0, "3000"},
		{"DML.004", "injection check insert column def", "必须指定插入列表", "", false, 1, "", 0, "0"},
		{"Create.322", "Foreign Key use fk_", "外键必须要以fk_为前缀，比如fk_tablename", "", false, 1, "", 0, "fk_"},
		{"DML.001", "injection check where", "必须有where条件", "", false, 1, "", 0, "0"},
		{"Create.015", "injection check text column amount", "表text字段不能超过3个", "", false, 1, "", 0, "3"},
		{"Create.016", "injection can't use mysql key words as column name", "不能使用mysql关键字、保留字作为列名", "", false, 1, "", 0, "0"},
		{"Alter.012", "Use complex data types", "检测到复杂的数据类型，尽可能使用简单数据类型", "", false, 1, "ALTER TABLE db_injection_task ADD COLUMN `device` text(32) NOT NULL COMMENT '设备';", 0, "0"},
		{"Alter.022", "Disallow subqueries on the view", "禁止在视图上进行子查询", "", false, 1, "select id from view1 where id in (select id from view1 where id = 1);", 0, "0"},
		{"Create.003", "injection check table primary key", "表必须要有主键", "", false, 1, "", 0, "0"},
		{"DML.002", "injection check always true", "永远为真的比较条件", "", false, 1, "", 0, "0"},
		{"DML.007", "injection check column exist", "目标库中表的字段不存在", "", false, 1, "", 0, "0"},
		{"Create.011", "injection check column character set", "不能设置列的字符集", "", false, 1, "", 0, "0"},
		{"Create.029", "Too many fields in a single table using Text type", "单表使用Text类型的字段过多", "", false, 2, "create table tablename(id text(32),id2 text(12));", 0, "1"},
		{"Create.013", "injection check joint index exclude", "联合索引，不能包含主键或者唯一索引列", "", false, 1, "", 0, "0"},
		{"Alter001", "injection check drop column", "禁止删列操作", "", false, 1, "", 0, "0"},
		{"DML.031", "DML use OrderBy", "DML中使用order by子句，通常没有必要对更新/删除项排序", "", false, 1, "", 0, "0"},
		{"Create.001", "injection check character set", "表必须指定utf8mb4字符集若指定collate则必须为utf8mb4_bin", "", false, 1, "", 0, "0"},
		{"Create.014", "injection check index column character amount", "索引列字符量\u003e128拦截", "", false, 1, "", 0, "128"},
		{"Create.005", "injection check unique index name", "Unique索引必须要以uniq_为前缀，比如uniq_ab", "", false, 1, "", 0, "uniq_"},
		{"Create.007", "injection check index column amount", "索引的列数不能超过3个", "", false, 1, "CREATE TABLE sc(Sno varchar(20) NOT NULL,\tCno varchar(10) NOT NULL,\tGrade int(11) DEFAULT NULL,\tPRIMARY KEY (Sno,Cno), \tFOREIGN KEY (Sno) REFERENCES student.Sno,\tFOREIGN KEY (Cno) REFERENCES course (Cno,Sno)) ENGINE=InnoDB DEFAULT CHARSET=utf8;", 0, "3"},
		{"Alter.016", "The character set is specified on the altertable column", "不推荐AlterTable改变字符集", "", false, 1, "alter table tweets change WeiboContent WeiboContent varchar(350) character set utf8mb4 collate utf8mb4_unicode_ci;", 0, "1"},
		{"DML.037", " SQL use Fomat Query", "%前导查询将导致查询无法使用索引，产生性能问题", "", false, 1, "select count(*) from db_injection_cluster where name like '%t%';", 0, "0"},
		{"Create.009", "injection check repeat index", "不能有重复的索引，包括(索引名不同，字段相同；冗余索引，如(a)(ab))", "", false, 1, "", 0, "0"},
		{"DML.003", "injection check use joined table when update", "不建议使用联表删除或更新", "", false, 1, "", 0, "0"},
		{"Create.321", "Primary Key use pk_", "主键必须要以pk_为前缀，比如pk_tablename", "", false, 1, "", 0, "pk_"},
		{"NotOK", "Statement does not match any existing rules", "未命中任何已有规则", "", false, 0, "", 0, ""},
	}
	c.JSON(http.StatusOK,
		gin.H{
			"code": 20000,
			"msg":  "Get rules",
			"data": data,
		},
	)
}

// GetRuleByItemName return all rules
func GetRuleByItemName(c *gin.Context) {
	data := Rule{"DML.006", "injection check table exist", "目标库中表不存在", "", false, 1, "", 0, "0"}
	c.JSON(http.StatusOK,
		gin.H{
			"code": 20000,
			"msg":  "Get rule by item name",
			"data": data,
		},
	)
}
