using System;
using System.Collections.Generic;


using MySql.Data.MySqlClient;

namespace CSVDownloader.Store.CreditCSVData {

    class CSVDataItec : 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 static readonly String insert_sql = $"insert into {table_name_} (" +
            $"{CSVDataItec.ColName.SpotID}," +
            $"{CSVDataItec.ColName.Code}," +
            $"{CSVDataItec.ColName.ParkingName}," +
            $"{CSVDataItec.ColName.DeviceType}," +
            $"{CSVDataItec.ColName.UseDatetime}," +
            $"{CSVDataItec.ColName.Trade}," +
            $"{CSVDataItec.ColName.CardCompanyName}," +
            $"{CSVDataItec.ColName.Amount}," +
            $"{CSVDataItec.ColName.TaxSendFee}," +
            $"{CSVDataItec.ColName.TotalAmount}," +
            $"{CSVDataItec.ColName.ErrorCode}," +
            //$"{CSVDataItec.ColName.UpdDate}," +
            $"{CSVDataItec.ColName.UpdStaff}," +
            //$"{CSVDataItec.ColName.RegDate}," +
            $"{CSVDataItec.ColName.RegStaff}" +
            $" ) values (" +
            $"@{CSVDataItec.ColName.SpotID}," +
            $"@{CSVDataItec.ColName.Code}," +
            $"@{CSVDataItec.ColName.ParkingName}," +
            $"@{CSVDataItec.ColName.DeviceType}," +
            $"@{CSVDataItec.ColName.UseDatetime}," +
            $"@{CSVDataItec.ColName.Trade}," +
            $"@{CSVDataItec.ColName.CardCompanyName}," +
            $"@{CSVDataItec.ColName.Amount}," +
            $"@{CSVDataItec.ColName.TaxSendFee}," +
            $"@{CSVDataItec.ColName.TotalAmount}," +
            $"@{CSVDataItec.ColName.ErrorCode}," +
            //$"@{CSVDataItec.ColName.UpdDate}," +
            $"@{CSVDataItec.ColName.UpdStaff}," +
            //$"@{CSVDataItec.ColName.RegDate}," +
            $"@{CSVDataItec.ColName.RegStaff}" +
            $");";

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

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

        }

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

            var bindlist = new List<(String, object)> {
                (CSVDataItec.ColName.SpotID.ToString(), data.spot_id),
                (CSVDataItec.ColName.Code.ToString(), data.code),
                (CSVDataItec.ColName.ParkingName.ToString(), data.parking_name),
                (CSVDataItec.ColName.DeviceType.ToString(), data.device_type),
                (CSVDataItec.ColName.UseDatetime.ToString(), data.use_datetime.ToString()),
                (CSVDataItec.ColName.Trade.ToString(), data.trade),
                (CSVDataItec.ColName.CardCompanyName.ToString(), data.card_company_name),
                (CSVDataItec.ColName.Amount.ToString(), data.amount),
                (CSVDataItec.ColName.TaxSendFee.ToString(), data.tax_send_fee),
                (CSVDataItec.ColName.TotalAmount.ToString(), data.total_amount),
                (CSVDataItec.ColName.ErrorCode.ToString(), data.error_code),
                (CSVDataItec.ColName.UpdStaff.ToString(), data.upd_staff),
                (CSVDataItec.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"+CSVDataItec.ColName.UseDatetime.ToString(), from.ToString("yyyy-MM-dd")),
                ("To"+CSVDataItec.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;
        }


    }


}
