using System;
using System.Collections.Generic;
using System.Data;

using MySql.Data.MySqlClient;


namespace CSVDownloader.Store.ElectronicMoneyDataStore {


    class GMOElectronicMoneyData : ElectronicMoneyCSVData {

        public String customer_code = "";
        public String acquirer_id = "";
        public String acquirer_name = "";
        public String customer_id = "";
        public String customer_name = "";
        public String shop_name = "";
        public String device_id = "";
        public String host_device_id = "";
        public String device_no = "";
        public DateTime adjust_datetime = DateTime.Now;
        public String trade_code = "";
        public int amount = 0;
        public int auto_charge_amount = 0;
        public int before_deposit = 0;
        public String brand = "";
        public String card_id = "";
        public String trade_status = "";
        public String last_proccesing_datetime = "";
        public String point = "";
        public String trade_no = "";
        public String origin_trade_no = "";


        public enum ColName {
            SpotID = 100,
            CustomerCode = 0,
            AcquirerID,
            AcquirerName,
            CustomerID, CustomerName,
            ShopName,
            DeviceID,
            HostDeviceID,
            DeviceNo,
            AdjustDatetime,
            TradeCode,
            Amount,
            AutoChargeAmount,
            BeforeDeposit,
            Brand,
            CardID,
            TradeStatus,
            LastProccesingDatetime,
            Point,
            TradeNo,
            OriginTradeNo,
            UpdDate,
            UpdStaff,
            RegDate,
            RegStaff
        };
    }

    class GMOElectronicMoneyDataStore : ElectronicMoneyCSVDataStore {

        private static readonly String table_name_ = "electronic_money_download_data_gmo";

        private String delete_sql_ = $"delete from {table_name_} where " +
            $"{GMOElectronicMoneyData.ColName.AdjustDatetime.ToString()} between " +
            $"@From{GMOElectronicMoneyData.ColName.AdjustDatetime.ToString()} and " +
            $"@To{GMOElectronicMoneyData.ColName.AdjustDatetime.ToString()};";


        public GMOElectronicMoneyDataStore(MySqlConnection conn) : base(conn) {

        }


        public override void Save(List<ElectronicMoneyCSVData> list) {
            Code.ResultCode ret;
            var table = new DataTable();

            table.Columns.Add($"{GMOElectronicMoneyData.ColName.SpotID}");
            table.Columns.Add($"{GMOElectronicMoneyData.ColName.CustomerCode}");
            table.Columns.Add($"{GMOElectronicMoneyData.ColName.AcquirerID}");
            table.Columns.Add($"{GMOElectronicMoneyData.ColName.AcquirerName}");
            table.Columns.Add($"{GMOElectronicMoneyData.ColName.CustomerID}");
            table.Columns.Add($"{GMOElectronicMoneyData.ColName.CustomerName}");
            table.Columns.Add($"{GMOElectronicMoneyData.ColName.ShopName}");
            table.Columns.Add($"{GMOElectronicMoneyData.ColName.DeviceID}");
            table.Columns.Add($"{GMOElectronicMoneyData.ColName.HostDeviceID}");
            table.Columns.Add($"{GMOElectronicMoneyData.ColName.DeviceNo}");
            table.Columns.Add($"{GMOElectronicMoneyData.ColName.AdjustDatetime}");
            table.Columns.Add($"{GMOElectronicMoneyData.ColName.TradeCode}");
            table.Columns.Add($"{GMOElectronicMoneyData.ColName.Amount}");
            table.Columns.Add($"{GMOElectronicMoneyData.ColName.AutoChargeAmount}");
            table.Columns.Add($"{GMOElectronicMoneyData.ColName.BeforeDeposit}");
            table.Columns.Add($"{GMOElectronicMoneyData.ColName.Brand}");
            table.Columns.Add($"{GMOElectronicMoneyData.ColName.CardID}");
            table.Columns.Add($"{GMOElectronicMoneyData.ColName.TradeStatus}");
            table.Columns.Add($"{GMOElectronicMoneyData.ColName.LastProccesingDatetime}");
            table.Columns.Add($"{GMOElectronicMoneyData.ColName.Point}");
            table.Columns.Add($"{GMOElectronicMoneyData.ColName.TradeNo}");
            table.Columns.Add($"{GMOElectronicMoneyData.ColName.OriginTradeNo}");
            table.Columns.Add($"{GMOElectronicMoneyData.ColName.UpdDate}");
            table.Columns.Add($"{GMOElectronicMoneyData.ColName.UpdStaff}");
            table.Columns.Add($"{GMOElectronicMoneyData.ColName.RegDate}");
            table.Columns.Add($"{GMOElectronicMoneyData.ColName.RegStaff}");


            foreach (GMOElectronicMoneyData data in list) {
                var row = table.NewRow();

                row[$"{GMOElectronicMoneyData.ColName.SpotID}"] = data.spot_id;
                row[$"{GMOElectronicMoneyData.ColName.CustomerCode}"] = data.customer_code;
                row[$"{GMOElectronicMoneyData.ColName.AcquirerID}"] = data.acquirer_id;
                row[$"{GMOElectronicMoneyData.ColName.AcquirerName}"] = data.acquirer_name;
                row[$"{GMOElectronicMoneyData.ColName.CustomerID}"] = data.customer_id;
                row[$"{GMOElectronicMoneyData.ColName.CustomerName}"] = data.customer_name;
                row[$"{GMOElectronicMoneyData.ColName.ShopName}"] = data.shop_name;
                row[$"{GMOElectronicMoneyData.ColName.DeviceID}"] = data.device_id;
                row[$"{GMOElectronicMoneyData.ColName.HostDeviceID}"] = data.host_device_id;
                row[$"{GMOElectronicMoneyData.ColName.DeviceNo}"] = data.device_no;
                row[$"{GMOElectronicMoneyData.ColName.AdjustDatetime}"] = data.adjust_datetime;
                row[$"{GMOElectronicMoneyData.ColName.TradeCode}"] = data.trade_code;
                row[$"{GMOElectronicMoneyData.ColName.Amount}"] = data.amount;
                row[$"{GMOElectronicMoneyData.ColName.AutoChargeAmount}"] = data.auto_charge_amount;
                row[$"{GMOElectronicMoneyData.ColName.BeforeDeposit}"] = data.before_deposit;
                row[$"{GMOElectronicMoneyData.ColName.Brand}"] = data.brand;
                row[$"{GMOElectronicMoneyData.ColName.CardID}"] = data.card_id;
                row[$"{GMOElectronicMoneyData.ColName.TradeStatus}"] = data.trade_status;
                row[$"{GMOElectronicMoneyData.ColName.LastProccesingDatetime}"] = data.last_proccesing_datetime;
                row[$"{GMOElectronicMoneyData.ColName.Point}"] = data.point;
                row[$"{GMOElectronicMoneyData.ColName.TradeNo}"] = data.trade_no;
                row[$"{GMOElectronicMoneyData.ColName.OriginTradeNo}"] = data.origin_trade_no;
                row[$"{GMOElectronicMoneyData.ColName.UpdDate}"] = data.upd_date;
                row[$"{GMOElectronicMoneyData.ColName.UpdStaff}"] = data.upd_staff;
                row[$"{GMOElectronicMoneyData.ColName.RegDate}"] = data.reg_date;
                row[$"{GMOElectronicMoneyData.ColName.RegStaff}"] = data.reg_staff;


                table.Rows.Add(row);
            }


            ret = BulkInsertData(table_name_, table);

            if (ret != Code.ResultCode.OK) {
                throw new Exception("登録失敗 GMO 電子マネー");
            }
        }
        public override int Delete(DateTime from, DateTime to) {

            var bindlist = new List<(String, object)> {
                ("From"+GMOElectronicMoneyData.ColName.AdjustDatetime.ToString(), from.ToString("yyyy-MM-dd")),
                ("To"+GMOElectronicMoneyData.ColName.AdjustDatetime.ToString(), to.AddDays(1).ToString("yyyy-MM-dd"))
            };

            var delete_count = DeleteData(delete_sql_, bindlist);
            if (delete_count == -1) {
                throw new Exception("削除失敗 大都QR");
            }
            return delete_count;
        }

    }
}
