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

template<typename _Table>
struct _update_ {};
template<>
struct _update_<web_drop_config_info> {
	_update_(web_drop_config_info const&value) {
		if (xml_context::from<web_drop_config_info>().where([value](auto it) {return it->type == value.type; }).empty()) {
			web_drop_config_info info{
				.id = 0,
				.modify_time = v3::datetime::current_time_stamp(),
				.type = value.type,
				.name = value.name,
				.enable = value.enable,
				.remarks = value.remarks };
			xml_context::from<web_drop_config_info>().
				max(info.id, [](auto it) {return it->id; }).
				inc(info.id).
				append(info).
				submit();
		}
		else {
			xml_context::from<web_drop_config_info>().
				where([value](auto it)
					{
						return it->type == value.type;
					}).
				update([value](auto it)
					{
						it->modify_time = v3::datetime::current_time_stamp();
						it->type = value.type;
						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_regex_config_info> {
	_update_(web_regex_config_info const&value) {
		if (xml_context::from<web_regex_config_info>().where([value](auto it) {return it->type == value.type; }).empty()) {
			web_regex_config_info info{
				.id = 0,
				.modify_time = v3::datetime::current_time_stamp(),
				.type = value.type,
				.name = value.name,
				.regex = value.regex,
				.enable = value.enable,
				.remarks = value.remarks };
			xml_context::from<web_regex_config_info>().
				max(info.id, [](auto it) {return it->id; }).
				inc(info.id).
				append(info).
				submit();
		}
		else {
			xml_context::from<web_regex_config_info>().
				where([value](auto it)
					{
						return it->type == value.type;
					}).
				update([value](auto it)
					{
						it->modify_time = v3::datetime::current_time_stamp();
						it->type = value.type;
						it->name = value.name;
						it->regex = value.regex;
						it->enable = value.enable;
						it->remarks = value.remarks;
						return true;
					}).
				submit();
		}
	}
};
template<>
struct _update_<web_sync_config_info> {
	_update_(web_sync_config_info const&value) {
		if (xml_context::from<web_sync_config_info>().where([value](auto it) {return it->id == value.id; }).empty()) {
			web_sync_config_info info{
				.id = 0,
				.create_time = v3::datetime::current_time_stamp(),
				.device_id = value.device_id,
				.summary_time = value.summary_time,
				.running_distance = value.running_distance,
				.speed = value.speed,
				.express_type = value.express_type,
				.lattice_count = value.lattice_count,
				.supply_count = value.supply_count,
				.reflux_count = value.reflux_count,
				.supply_summay = value.supply_summay,
				.lattice_summay = value.lattice_summay,
				.reflux_summay = value.reflux_summay,
				.enable = value.enable,
				.remarks = value.remarks };
			xml_context::from<web_sync_config_info>().
				max(info.id, [](auto it) {return it->id; }).
				inc(info.id).
				append(info).
				submit();
		}
		else {
			xml_context::from<web_sync_config_info>().
				where([value](auto it)
					{
						return it->id == value.id;
					}).
				update([value](auto it)
					{
						it->create_time = v3::datetime::current_time_stamp();
						it->device_id = value.device_id;
						it->summary_time = value.summary_time;
						it->running_distance = value.running_distance;
						it->speed = value.speed;
						it->express_type = value.express_type;
						it->lattice_count = value.lattice_count;
						it->supply_count = value.supply_count;
						it->reflux_count = value.reflux_count;
						it->supply_summay = value.supply_summay;
						it->lattice_summay = value.lattice_summay;
						it->reflux_summay = value.reflux_summay;
						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->id == value.id; }).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->id == value.id;
					}).
				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);
		}
	}
};