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


using MySql.Data.MySqlClient;

namespace CSVDownloader.Store.CreditCSVData {

    class CreditCSVDataItec : CreditCSVData {
        public String code = "";
        public String parking_name = "";
        public String device_type = "";
        public DateTime use_datetime = DateTime.Now;
        public String trade = "";
        public String card_company_name = "";
        public int amount = 0;
        public int tax_send_fee = 0;
        public int total_amount = 0;
        public String error_code = "";

        public enum ColName {
            SpotID = 100,
            Code = 0,
            ParkingName,
            DeviceType,
            UseDatetime,
            Trade,
            CardCompanyName,
            Amount,
            TaxSendFee,
            TotalAmount,
            ErrorCode,
            UpdDate,
            UpdStaff,
            RegDate,
            RegStaff
        };
    }
    class ItecCreditDataStore : CreditCSVDataStore {
        private static readonly String table_name_ = "creditcard_download_data_itec";

        private String delete_sql_ = $"delete from {table_name_} where " +
            $"{CreditCSVDataItec.ColName.UseDatetime} between " +
            $"@From{CreditCSVDataItec.ColName.UseDatetime} and " +
            $"@To{CreditCSVDataItec.ColName.UseDatetime};";

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

        }


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

            table.Columns.Add($"{CreditCSVDataItec.ColName.SpotID}");
            table.Columns.Add($"{CreditCSVDataItec.ColName.Code}");
            table.Columns.Add($"{CreditCSVDataItec.ColName.ParkingName}");
            table.Columns.Add($"{CreditCSVDataItec.ColName.DeviceType}");
            table.Columns.Add($"{CreditCSVDataItec.ColName.UseDatetime}");
            table.Columns.Add($"{CreditCSVDataItec.ColName.Trade}");
            table.Columns.Add($"{CreditCSVDataItec.ColName.CardCompanyName}");
            table.Columns.Add($"{CreditCSVDataItec.ColName.Amount}");
            table.Columns.Add($"{CreditCSVDataItec.ColName.TaxSendFee}");
            table.Columns.Add($"{CreditCSVDataItec.ColName.TotalAmount}");
            table.Columns.Add($"{CreditCSVDataItec.ColName.ErrorCode}");
            table.Columns.Add($"{CreditCSVDataItec.ColName.UpdStaff}");
            table.Columns.Add($"{CreditCSVDataItec.ColName.RegStaff}");

            foreach (CreditCSVDataItec data in list) {
                var row = table.NewRow();
                row[$"{CreditCSVDataItec.ColName.SpotID}"] = data.spot_id;
                row[$"{CreditCSVDataItec.ColName.Code}"] = data.code;
                row[$"{CreditCSVDataItec.ColName.ParkingName}"] = data.parking_name;
                row[$"{CreditCSVDataItec.ColName.DeviceType}"] = data.device_type;
                row[$"{CreditCSVDataItec.ColName.UseDatetime}"] = data.use_datetime;
                row[$"{CreditCSVDataItec.ColName.Trade}"] = data.trade;
                row[$"{CreditCSVDataItec.ColName.CardCompanyName}"] = data.card_company_name;
                row[$"{CreditCSVDataItec.ColName.Amount}"] = data.amount;
                row[$"{CreditCSVDataItec.ColName.TaxSendFee}"] = data.tax_send_fee;
                row[$"{CreditCSVDataItec.ColName.TotalAmount}"] = data.total_amount;
                row[$"{CreditCSVDataItec.ColName.ErrorCode}"] = data.error_code;
                row[$"{CreditCSVDataItec.ColName.UpdStaff}"] = data.upd_staff;
                row[$"{CreditCSVDataItec.ColName.RegStaff}"] = data.reg_staff;

                table.Rows.Add(row);
            }


            ret = BulkInsertData(table_name_, table);

            if (ret != Code.ResultCode.OK) {
                throw new Exception("登録失敗 Itec");
            }
        }

        public override int Delete(DateTime from, DateTime to) {

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

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


    }


}
