#pragma once
//stl
#include <iostream>
#include <sstream>
#include <vector>
//local
#include "xml_context.hpp"

template<typename _Table>
struct _update_ {};
template<>
struct _update_<web_alarm_config_info> {
	_update_(web_alarm_config_info const&value) {
		if (xml_context::from<web_alarm_config_info>().where([value](auto it) {return it->number == value.number; }).empty()) {
			web_alarm_config_info info{
				.id = 0,
				.modify_time = v3::datetime::current_time_stamp(),
				.number = value.number,
				.description = value.description,
				.brand_no = value.brand_no,
				.spec_no = value.spec_no,
				.level_no = value.level_no,
				.enable = value.enable,
				.remarks = value.remarks };
			xml_context::from<web_alarm_config_info>().
				max(info.id, [](auto it) {return it->id; }).
				inc(info.id).
				append(info).
				submit();
		}
		else {
			xml_context::from<web_alarm_config_info>().
				where([value](auto it)
					{
						return it->number == value.number;
					}).
				update([value](auto it)
					{
						it->modify_time = v3::datetime::current_time_stamp();
						it->number = value.number;
						it->description = value.description;
						it->brand_no = value.brand_no;
						it->spec_no = value.spec_no;
						it->level_no = value.level_no;
						it->enable = value.enable;
						it->remarks = value.remarks;
						return true;
					}).
				submit();
		}
	}
};
template<>
struct _update_<web_brand_config_info> {
	_update_(web_brand_config_info const&value) {
		if (xml_context::from<web_brand_config_info>().where([value](auto it) {return it->number == value.number; }).empty()) {
			web_brand_config_info info{
				.id = 0,
				.modify_time = v3::datetime::current_time_stamp(),
				.number = value.number,
				.name = value.name,
				.enable = value.enable,
				.remarks = value.remarks };
			xml_context::from<web_brand_config_info>().
				max(info.id, [](auto it) {return it->id; }).
				inc(info.id).
				append(info).
				submit();
		}
		else {
			xml_context::from<web_brand_config_info>().
				where([value](auto it)
					{
						return it->number == value.number;
					}).
				update([value](auto it)
					{
						it->modify_time = v3::datetime::current_time_stamp();
						it->number = value.number;
						it->name = value.name;
						it->enable = value.enable;
						it->remarks = value.remarks;
						return true;
					}).
				submit();
		}
	}
};
template<>
struct _update_<web_device_config_info> {
	_update_(web_device_config_info const&value) {
		if (xml_context::from<web_device_config_info>().where([value](auto it) {return it->number == value.number; }).empty()) {
			web_device_config_info info{
				.id = 0,
				.modify_time = v3::datetime::current_time_stamp(),
				.number = value.number,
				.name = value.name,
				.enable = value.enable,
				.remarks = value.remarks };
			xml_context::from<web_device_config_info>().
				max(info.id, [](auto it) {return it->id; }).
				inc(info.id).
				append(info).
				submit();
		}
		else {
			xml_context::from<web_device_config_info>().
				where([value](auto it)
					{
						return it->number == value.number;
					}).
				update([value](auto it)
					{
						it->modify_time = v3::datetime::current_time_stamp();
						it->number = value.number;
						it->name = value.name;
						it->enable = value.enable;
						it->remarks = value.remarks;
						return true;
					}).
				submit();
		}
	}
};
template<>
struct _update_<web_fitting_config_info> {
	_update_(web_fitting_config_info const&value) {
		if (xml_context::from<web_fitting_config_info>().where([value](auto it) {return it->number == value.number; }).empty()) {
			web_fitting_config_info info{
				.id = 0,
				.modify_time = v3::datetime::current_time_stamp(),
				.number = value.number,
				.name = value.name,
				.enable = value.enable,
				.remarks = value.remarks };
			xml_context::from<web_fitting_config_info>().
				max(info.id, [](auto it) {return it->id; }).
				inc(info.id).
				append(info).
				submit();
		}
		else {
			xml_context::from<web_fitting_config_info>().
				where([value](auto it)
					{
						return it->number == value.number;
					}).
				update([value](auto it)
					{
						it->modify_time = v3::datetime::current_time_stamp();
						it->number = value.number;
						it->name = value.name;
						it->enable = value.enable;
						it->remarks = value.remarks;
						return true;
					}).
				submit();
		}
	}
};
template<>
struct _update_<web_general_config_info> {
	_update_(web_general_config_info const&value) {
		if (xml_context::from<web_general_config_info>().where([value](auto it) {return it->key == value.key; }).empty()) {
			web_general_config_info info{
				.id = 0,
				.modify_time = v3::datetime::current_time_stamp(),
				.key = value.key,
				.value = value.value,
				.enable = value.enable,
				.remarks = value.remarks };
			xml_context::from<web_general_config_info>().
				max(info.id, [](auto it) {return it->id; }).
				inc(info.id).
				append(info).
				submit();
		}
		else {
			xml_context::from<web_general_config_info>().
				where([value](auto it)
					{
						return it->key == value.key;
					}).
				update([value](auto it)
					{
						it->modify_time = v3::datetime::current_time_stamp();
						it->key = value.key;
						it->value = value.value;
						it->enable = value.enable;
						it->remarks = value.remarks;
						return true;
					}).
				submit();
		}
	}
};
template<>
struct _update_<web_level_config_info> {
	_update_(web_level_config_info const&value) {
		if (xml_context::from<web_level_config_info>().where([value](auto it) {return it->number == value.number; }).empty()) {
			web_level_config_info info{
				.id = 0,
				.modify_time = v3::datetime::current_time_stamp(),
				.number = value.number,
				.name = value.name,
				.enable = value.enable,
				.remarks = value.remarks };
			xml_context::from<web_level_config_info>().
				max(info.id, [](auto it) {return it->id; }).
				inc(info.id).
				append(info).
				submit();
		}
		else {
			xml_context::from<web_level_config_info>().
				where([value](auto it)
					{
						return it->number == value.number;
					}).
				update([value](auto it)
					{
						it->modify_time = v3::datetime::current_time_stamp();
						it->number = value.number;
						it->name = value.name;
						it->enable = value.enable;
						it->remarks = value.remarks;
						return true;
					}).
				submit();
		}
	}
};
template<>
struct _update_<web_spec_config_info> {
	_update_(web_spec_config_info const&value) {
		if (xml_context::from<web_spec_config_info>().where([value](auto it) {return it->number == value.number; }).empty()) {
			web_spec_config_info info{
				.id = 0,
				.modify_time = v3::datetime::current_time_stamp(),
				.number = value.number,
				.name = value.name,
				.brand_no = value.brand_no,
				.fitting_no = value.fitting_no,
				.enable = value.enable,
				.remarks = value.remarks };
			xml_context::from<web_spec_config_info>().
				max(info.id, [](auto it) {return it->id; }).
				inc(info.id).
				append(info).
				submit();
		}
		else {
			xml_context::from<web_spec_config_info>().
				where([value](auto it)
					{
						return it->number == value.number;
					}).
				update([value](auto it)
					{
						it->modify_time = v3::datetime::current_time_stamp();
						it->number = value.number;
						it->name = value.name;
						it->brand_no = value.brand_no;
						it->fitting_no = value.fitting_no;
						it->enable = value.enable;
						it->remarks = value.remarks;
						return true;
					}).
				submit();
		}
	}
};
template<>
struct _update_<web_user_config_info> {
	_update_(web_user_config_info const&value) {
		if (xml_context::from<web_user_config_info>().where([value](auto it) {return it->code == value.code; }).empty()) {
			web_user_config_info info{
				.id = 0,
				.modify_time = v3::datetime::current_time_stamp(),
				.name = value.name,
				.code = value.code,
				.password = value.password,
				.enable = value.enable,
				.remarks = value.remarks };
			xml_context::from<web_user_config_info>().
				max(info.id, [](auto it) {return it->id; }).
				inc(info.id).
				append(info).
				submit();
		}
		else {
			xml_context::from<web_user_config_info>().
				where([value](auto it)
					{
						return it->code == value.code;
					}).
				update([value](auto it)
					{
						it->modify_time = v3::datetime::current_time_stamp();
						it->name = value.name;
						it->code = value.code;
						it->password = value.password;
						it->enable = value.enable;
						it->remarks = value.remarks;
						return true;
					}).
				submit();
		}
	}
};
class xml_update {
public:
	template<typename _Table>
	static void update(_Table const& value) {
		(_update_<_Table>)(value);
	}
	template<typename _Table>
	static void update(std::vector<_Table> const& values) {
		for (auto const& value : values) {
			(_update_<_Table>)(value);
		}
	}
};