using System;
using System.Collections.Generic;
using System.Text;

using MySql.Data.MySqlClient;


namespace CSVDownloader.Store.CreditCSVData {


    class CreditCSVDataDaito : CreditCSVData {

        public String parking_name = "";
        public String developer = "";
        public String adjust_type = "";
        public String fee_type = "";
        public DateTime reception_datetime = DateTime.Now;
        public int amount = 0;
        public String reception_no = "";
        public String response_no = "";
        public String error_code1 = "";
        public String error_code2 = "";

        public enum ColName {
            SpotID = 100,
            ParkingName = 0,
            Developer,
            AdjustType,
            FeeType,
            ReceptionDatetime,
            Amount,
            ReceptionNo,
            ResponseNo,
            ErrorCode1,
            ErrorCode2,
            UpdDate,
            UpdStaff,
            RegDate,
            RegStaff
        };
    }

    class DaitoCreitDataStore : CreditCSVDataStore {

        private static readonly String table_name_ = "creditcard_download_data_daito";
        private static readonly String insert_sql = $"insert into {table_name_} (" +
            $"{CreditCSVDataDaito.ColName.SpotID.ToString()}," +
            $"{CreditCSVDataDaito.ColName.ParkingName.ToString()}," +
            $"{CreditCSVDataDaito.ColName.Developer.ToString()}," +
            $"{CreditCSVDataDaito.ColName.AdjustType.ToString()}," +
            $"{CreditCSVDataDaito.ColName.FeeType.ToString()}," +
            $"{CreditCSVDataDaito.ColName.ReceptionDatetime.ToString()}," +
            $"{CreditCSVDataDaito.ColName.Amount.ToString()}," +
            $"{CreditCSVDataDaito.ColName.ReceptionNo.ToString()}," +
            $"{CreditCSVDataDaito.ColName.ResponseNo.ToString()}," +
            $"{CreditCSVDataDaito.ColName.ErrorCode1.ToString()}," +
            $"{CreditCSVDataDaito.ColName.ErrorCode2.ToString()}," +
            //$"{CSVDataDaito.ColName.UpdDate.ToString()}," +
            $"{CreditCSVDataDaito.ColName.UpdStaff.ToString()}," +
            //$"{CSVDataDaito.ColName.RegDate.ToString()}," +
            $"{CreditCSVDataDaito.ColName.RegStaff.ToString()}" +
            $" ) values (" +
            $"@{CreditCSVDataDaito.ColName.SpotID.ToString()}," +
            $"@{CreditCSVDataDaito.ColName.ParkingName.ToString()}," +
            $"@{CreditCSVDataDaito.ColName.Developer.ToString()}," +
            $"@{CreditCSVDataDaito.ColName.AdjustType.ToString()}," +
            $"@{CreditCSVDataDaito.ColName.FeeType.ToString()}," +
            $"@{CreditCSVDataDaito.ColName.ReceptionDatetime.ToString()}," +
            $"@{CreditCSVDataDaito.ColName.Amount.ToString()}," +
            $"@{CreditCSVDataDaito.ColName.ReceptionNo.ToString()}," +
            $"@{CreditCSVDataDaito.ColName.ResponseNo.ToString()}," +
            $"@{CreditCSVDataDaito.ColName.ErrorCode1.ToString()}," +
            $"@{CreditCSVDataDaito.ColName.ErrorCode2.ToString()}," +
            //$"@{CSVDataDaito.ColName.UpdDate.ToString()}," +
            $"@{CreditCSVDataDaito.ColName.UpdStaff.ToString()}," +
            //$"@{CSVDataDaito.ColName.RegDate.ToString()}" +
            $"@{CreditCSVDataDaito.ColName.RegStaff.ToString()}" +
            $");";



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


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

        }

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

            var bindlist = new List<(String, object)> {
                (CreditCSVDataDaito.ColName.SpotID.ToString(), data.spot_id),
                (CreditCSVDataDaito.ColName.ParkingName.ToString(), data.parking_name),
                (CreditCSVDataDaito.ColName.Developer.ToString(), data.developer),
                (CreditCSVDataDaito.ColName.AdjustType.ToString(), data.adjust_type),
                (CreditCSVDataDaito.ColName.FeeType.ToString(), data.fee_type),
                (CreditCSVDataDaito.ColName.ReceptionDatetime.ToString(), data.reception_datetime),
                (CreditCSVDataDaito.ColName.Amount.ToString(), data.amount),
                (CreditCSVDataDaito.ColName.ReceptionNo.ToString(), data.reception_no),
                (CreditCSVDataDaito.ColName.ResponseNo.ToString(), data.response_no),
                (CreditCSVDataDaito.ColName.ErrorCode1.ToString(), data.error_code1),
                (CreditCSVDataDaito.ColName.ErrorCode2.ToString(), data.error_code2),
                (CreditCSVDataDaito.ColName.UpdStaff.ToString(), data.upd_staff),
                (CreditCSVDataDaito.ColName.RegStaff.ToString(), data.reg_staff)
            };

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

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

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

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

    }
}
