using System;
using System.Collections.Generic;

using MySql.Data.MySqlClient;


namespace CSVDownloader.Store.CreditCSVData {

    class CSVDataHelloTechno : 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 static readonly String insert_sql = $"insert into {table_name_} (" +
            $"{CSVDataHelloTechno.ColName.SpotID}, " +
            $"{CSVDataHelloTechno.ColName.RegisterDatetime}, " +
            $"{CSVDataHelloTechno.ColName.UserCode}, " +
            $"{CSVDataHelloTechno.ColName.ParkingCode}, " +
            $"{CSVDataHelloTechno.ColName.DeviceNo}, " +
            $"{CSVDataHelloTechno.ColName.AdjustNo}, " +
            $"{CSVDataHelloTechno.ColName.Trade}, " +
            $"{CSVDataHelloTechno.ColName.Amount}, " +
            $"{CSVDataHelloTechno.ColName.CardCompanyName}, " +
            $"{CSVDataHelloTechno.ColName.ApprovalNo}, " +
            $"{CSVDataHelloTechno.ColName.ResponseNo}, " +
            $"{CSVDataHelloTechno.ColName.CancelDate}, " +
            $"{CSVDataHelloTechno.ColName.Error}, " +
            //$"{CSVDataHelloTechno.ColName.UpdDate}, " +
            $"{CSVDataHelloTechno.ColName.UpdStaff}, " +
            //$"{CSVDataHelloTechno.ColName.RegDate}, " +
            $"{CSVDataHelloTechno.ColName.RegStaff} " +
            $" ) values (" +
            $"@{CSVDataHelloTechno.ColName.SpotID}, " +
            $"@{CSVDataHelloTechno.ColName.RegisterDatetime}, " +
            $"@{CSVDataHelloTechno.ColName.UserCode}, " +
            $"@{CSVDataHelloTechno.ColName.ParkingCode}, " +
            $"@{CSVDataHelloTechno.ColName.DeviceNo}, " +
            $"@{CSVDataHelloTechno.ColName.AdjustNo}, " +
            $"@{CSVDataHelloTechno.ColName.Trade}, " +
            $"@{CSVDataHelloTechno.ColName.Amount}, " +
            $"@{CSVDataHelloTechno.ColName.CardCompanyName}, " +
            $"@{CSVDataHelloTechno.ColName.ApprovalNo}, " +
            $"@{CSVDataHelloTechno.ColName.ResponseNo}, " +
            $"@{CSVDataHelloTechno.ColName.CancelDate}, " +
            $"@{CSVDataHelloTechno.ColName.Error}, " +
            //$"@{CSVDataHelloTechno.ColName.UpdDate}, " +
            $"@{CSVDataHelloTechno.ColName.UpdStaff}, " +
            //$"@{CSVDataHelloTechno.ColName.RegDate}, " +
            $"@{CSVDataHelloTechno.ColName.RegStaff} " +
           $");";

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

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

        }

        public override void Save(CreditCSVData obj) {
            var data = (CSVDataHelloTechno)obj;

            var bindlist = new List<(String, object)> {
                (CSVDataHelloTechno.ColName.SpotID.ToString(), data.spot_id),
                (CSVDataHelloTechno.ColName.RegisterDatetime.ToString(), data.register_datetime.ToString()),
                (CSVDataHelloTechno.ColName.UserCode.ToString(), data.user_code),
                (CSVDataHelloTechno.ColName.ParkingCode.ToString(), data.parking_code),
                (CSVDataHelloTechno.ColName.DeviceNo.ToString(), data.device_no),
                (CSVDataHelloTechno.ColName.AdjustNo.ToString(), data.adjust_no),
                (CSVDataHelloTechno.ColName.Trade.ToString(), data.trade),
                (CSVDataHelloTechno.ColName.Amount.ToString(), data.amount),
                (CSVDataHelloTechno.ColName.CardCompanyName.ToString(), data.card_company_name),
                (CSVDataHelloTechno.ColName.ApprovalNo.ToString(), data.approval_no),
                (CSVDataHelloTechno.ColName.ResponseNo.ToString(), data.response_no),
                (CSVDataHelloTechno.ColName.CancelDate.ToString(),  data.cancel_date != null ? ((DateTime)data.cancel_date).ToString(): null),
                (CSVDataHelloTechno.ColName.Error.ToString(),  data.error),
                (CSVDataHelloTechno.ColName.UpdStaff.ToString(),  data.upd_staff),
                (CSVDataHelloTechno.ColName.RegStaff.ToString(),  data.reg_staff),

            };

            var ret = InsertData(insert_sql, bindlist);
            if (ret != Code.ResultCode.OK) {
                throw new Exception("登録失敗 Zeus");
            }
        }

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

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

    }
}
