// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
	"samplingDate": {
		"rules": [{
			"format": "date"
		}],
		"title": "取样日期",
		"defaultValue": {
			"$env": "now"
		},
		"label": "取样日期"
	},
	"assayDate": {
		"rules": [{
			"format": "date"
		}],
		"title": "分析日期",
		"defaultValue": {
			"$env": "now"
		},
		"label": "分析日期"
	},
	"samplingLocation": {
		"rules": [{
				"format": "array"
			},
			{
				"arrayType": "string"
			}
		],
		"title": "取样地点",
		"label": "取样地点"
	},
	"sampleNumber": {
		"rules": [{
			"format": "string"
		}],
		"title": "样品编号",
		"label": "样品编号"
	},
	"NG_C1": {
		"rules": [{
			"format": "float"
		}],
		"title": "甲烷C1",
		"defaultValue": 0,
		"label": "甲烷C1"
	},
	"NG_N2": {
		"rules": [{
			"format": "float"
		}],
		"title": "氮气N2",
		"defaultValue": 0,
		"label": "氮气N2"
	},
	"NG_CO2": {
		"rules": [{
			"format": "float"
		}],
		"title": "二氧化碳CO2",
		"defaultValue": 0,
		"label": "二氧化碳CO2"
	},
	"NG_C2": {
		"rules": [{
			"format": "float"
		}],
		"title": "乙烷C2",
		"defaultValue": 0,
		"label": "乙烷C2"
	},
	"NG_C3": {
		"rules": [{
			"format": "float"
		}],
		"title": "丙烷C3",
		"defaultValue": 0,
		"label": "丙烷C3"
	},
	"NG_H2O": {
		"rules": [{
			"format": "float"
		}],
		"title": "水H2O",
		"defaultValue": 0,
		"label": "水H2O"
	},
	"NG_H2S": {
		"rules": [{
			"format": "float"
		}],
		"title": "硫化氢H2S",
		"defaultValue": 0,
		"label": "硫化氢H2S"
	},
	"NG_H2": {
		"rules": [{
			"format": "float"
		}],
		"title": "氢气H2",
		"defaultValue": 0,
		"label": "氢气H2"
	},
	"NG_CO": {
		"rules": [{
			"format": "float"
		}],
		"title": "一氧化碳CO",
		"defaultValue": 0,
		"label": "一氧化碳CO"
	},
	"NG_O2": {
		"rules": [{
			"format": "float"
		}],
		"title": "氧气O2",
		"defaultValue": 0,
		"label": "氧气O2"
	},
	"NG_iC4": {
		"rules": [{
			"format": "float"
		}],
		"title": "异丁烷iC4",
		"defaultValue": 0,
		"label": "异丁烷iC4"
	},
	"NG_nC4": {
		"rules": [{
			"format": "float"
		}],
		"title": "正丁烷nC4",
		"defaultValue": 0,
		"label": "正丁烷nC4"
	},
	"NG_iC5": {
		"rules": [{
			"format": "float"
		}],
		"title": "异戊烷iC5",
		"defaultValue": 0,
		"label": "异戊烷iC5"
	},
	"NG_nC5": {
		"rules": [{
			"format": "float"
		}],
		"title": "正戊烷nC5",
		"defaultValue": 0,
		"label": "正戊烷nC5"
	},
	"NG_C6": {
		"rules": [{
			"format": "float"
		}],
		"title": "己烷C6",
		"defaultValue": 0,
		"label": "己烷C6"
	},
	"NG_C7": {
		"rules": [{
			"format": "float"
		}],
		"title": "庚烷C7",
		"defaultValue": 0,
		"label": "庚烷C7"
	},
	"NG_C8": {
		"rules": [{
			"format": "float"
		}],
		"title": "辛烷C8",
		"defaultValue": 0,
		"label": "辛烷C8"
	},
	"NG_C9": {
		"rules": [{
			"format": "float"
		}],
		"title": "壬烷C9",
		"defaultValue": 0,
		"label": "壬烷C9"
	},
	"NG_C10": {
		"rules": [{
			"format": "float"
		}],
		"title": "癸烷C10",
		"defaultValue": 0,
		"label": "癸烷C10"
	},
	"NG_He": {
		"rules": [{
			"format": "float"
		}],
		"title": "氦气He",
		"defaultValue": 0,
		"label": "氦气He"
	},
	"NG_Ar": {
		"rules": [{
			"format": "float"
		}],
		"title": "氩气Ar",
		"defaultValue": 0,
		"label": "氩气Ar"
	},
	// "NG_SUM": {
	// 	"rules": [{
	// 			"format": "float",
	// 			"required": true,
	// 			"errorMessage": '组分输入不全'
	// 		},
	// 		{
	// 			validateFunction: function(rule, value, data, callback) {
	// 				console.log(value)
	// 				console.log(data)
	// 				if (Math.abs(parseFloat(value) - 100) > 0.0001) {
	// 					callback('组分之和不等于100%')
	// 				}
	// 				return true
	// 			}
	// 		}
	// 	],
	// 	"title": "合计",
	// 	"defaultValue": 0,
	// 	"label": "合计"
	// },
	"createTime": {
		"rules": [{
			"format": "datetime"
		}],
		"title": "创建时间",
		"label": "创建时间"
	}
}

const enumConverter = {}

function filterToWhere(filter, command) {
	let where = {}
	for (let field in filter) {
		let {
			type,
			value
		} = filter[field]
		switch (type) {
			case "search":
				if (typeof value === 'string' && value.length) {
					where[field] = new RegExp(value)
				}
				break;
			case "select":
				if (value.length) {
					let selectValue = []
					for (let s of value) {
						selectValue.push(command.eq(s))
					}
					where[field] = command.or(selectValue)
				}
				break;
			case "range":
				if (value.length) {
					let gt = value[0]
					let lt = value[1]
					where[field] = command.and([command.gte(gt), command.lte(lt)])
				}
				break;
			case "date":
				if (value.length) {
					let [s, e] = value
					let startDate = new Date(s)
					let endDate = new Date(e)
					where[field] = command.and([command.gte(startDate), command.lte(endDate)])
				}
				break;
			case "timestamp":
				if (value.length) {
					let [startDate, endDate] = value
					where[field] = command.and([command.gte(startDate), command.lte(endDate)])
				}
				break;
		}
	}
	return where
}

export {
	validator,
	enumConverter,
	filterToWhere
}