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

using MySql.Data.MySqlClient;


namespace CSVDownloader.Store.CreditCSVData {

    class CreditCSVDataHelloTechno : CreditCSVData {
        public String spot_i_d = "";
        public DateTime register_datetime = DateTime.Now;
        public String user_code = "";
        public String parking_code = "";
        public String device_no = "";
        public String adjust_no = "";
        public String trade = "";
        public int amount = 0;
        public String card_company_name = "";
        public String approval_no = "";
        public String response_no = "";
        public DateTime? cancel_date = null;
        public String error = "";

        public enum ColName {
            SpotID = 100,
            RegisterDatetime = 0,
            UserCode,
            ParkingCode,
            DeviceNo,
            AdjustNo,
            Trade,
            Amount,
            CardCompanyName,
            ApprovalNo,
            ResponseNo,
            CancelDate,
            Error,
            UpdDate,
            UpdStaff,
            RegDate,
            RegStaff
        }

    }
    class HelloTechnoCreditDataStore : CreditCSVDataStore {
        private static readonly String table_name_ = "creditcard_download_data_hellotechno";

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

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

        }


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

            table.Columns.Add($"{CreditCSVDataHelloTechno.ColName.SpotID}");
            table.Columns.Add($"{CreditCSVDataHelloTechno.ColName.RegisterDatetime}");
            table.Columns.Add($"{CreditCSVDataHelloTechno.ColName.UserCode}");
            table.Columns.Add($"{CreditCSVDataHelloTechno.ColName.ParkingCode}");
            table.Columns.Add($"{CreditCSVDataHelloTechno.ColName.DeviceNo}");
            table.Columns.Add($"{CreditCSVDataHelloTechno.ColName.AdjustNo}");
            table.Columns.Add($"{CreditCSVDataHelloTechno.ColName.Trade}");
            table.Columns.Add($"{CreditCSVDataHelloTechno.ColName.Amount}");
            table.Columns.Add($"{CreditCSVDataHelloTechno.ColName.CardCompanyName}");
            table.Columns.Add($"{CreditCSVDataHelloTechno.ColName.ApprovalNo}");
            table.Columns.Add($"{CreditCSVDataHelloTechno.ColName.ResponseNo}");
            table.Columns.Add($"{CreditCSVDataHelloTechno.ColName.CancelDate}");
            table.Columns.Add($"{CreditCSVDataHelloTechno.ColName.Error}");
            table.Columns.Add($"{CreditCSVDataHelloTechno.ColName.UpdStaff}");
            table.Columns.Add($"{CreditCSVDataHelloTechno.ColName.RegStaff}");

            foreach (CreditCSVDataHelloTechno data in list) {
                var row = table.NewRow();
                row[$"{CreditCSVDataHelloTechno.ColName.SpotID}"] = data.spot_id;
                row[$"{CreditCSVDataHelloTechno.ColName.RegisterDatetime}"] = data.register_datetime;
                row[$"{CreditCSVDataHelloTechno.ColName.UserCode}"] = data.user_code;
                row[$"{CreditCSVDataHelloTechno.ColName.ParkingCode}"] = data.parking_code;
                row[$"{CreditCSVDataHelloTechno.ColName.DeviceNo}"] = data.device_no;
                row[$"{CreditCSVDataHelloTechno.ColName.AdjustNo}"] = data.adjust_no;
                row[$"{CreditCSVDataHelloTechno.ColName.Trade}"] = data.trade;
                row[$"{CreditCSVDataHelloTechno.ColName.Amount}"] = data.amount;
                row[$"{CreditCSVDataHelloTechno.ColName.CardCompanyName}"] = data.card_company_name;
                row[$"{CreditCSVDataHelloTechno.ColName.ApprovalNo}"] = data.approval_no;
                row[$"{CreditCSVDataHelloTechno.ColName.ResponseNo}"] = data.response_no;
                row[$"{CreditCSVDataHelloTechno.ColName.CancelDate}"] = data.cancel_date;
                row[$"{CreditCSVDataHelloTechno.ColName.Error}"] = data.error;
                row[$"{CreditCSVDataHelloTechno.ColName.UpdStaff}"] = data.upd_staff;
                row[$"{CreditCSVDataHelloTechno.ColName.RegStaff}"] = data.reg_staff;

                table.Rows.Add(row);
            }


            ret = BulkInsertData(table_name_, table);

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

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

            var bindlist = new List<(String, object)> {
                ("From"+CreditCSVDataHelloTechno.ColName.RegisterDatetime.ToString(), from.ToString("yyyy-MM-dd")),
                ("To"+CreditCSVDataHelloTechno.ColName.RegisterDatetime.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;
        }

    }
}
