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

using MySql.Data.MySqlClient;


namespace CSVDownloader.Store.QRCSVData {


    class QRCSVDataDaito : QRCSVData {

        public String parking_name = "";
        public String developer = "";
        public DateTime reception_datetime = DateTime.Now;
        public String company = "";
        public int amount = 0;
        public String adjust_no = "";
        public String error_code = "";

        public enum ColName {
            SpotID = 100,
            ParkingName = 0,
            Developer,
            ReceptionDatetime,
            Company,
            Amount,
            AdjustNo,
            ErrorCode,
            UpdDate,
            UpdStaff,
            RegDate,
            RegStaff
        };
    }

    class DaitoQRDataStore : QRCSVDataStore {

        private static readonly String table_name_ = "qrcode_download_data_daito";
        private static readonly String insert_sql = $"insert into {table_name_} (" +
            $"{QRCSVDataDaito.ColName.SpotID.ToString()}," +
            $"{QRCSVDataDaito.ColName.ParkingName.ToString()}," +
            $"{QRCSVDataDaito.ColName.Developer.ToString()}," +
            $"{QRCSVDataDaito.ColName.ReceptionDatetime.ToString()}," +
            $"{QRCSVDataDaito.ColName.Company.ToString()}," +
            $"{QRCSVDataDaito.ColName.Amount.ToString()}," +
            $"{QRCSVDataDaito.ColName.AdjustNo.ToString()}," +
            $"{QRCSVDataDaito.ColName.ErrorCode.ToString()}," +
            //$"{CSVDataDaitoQR.ColName.UpdDate.ToString()}," +
            $"{QRCSVDataDaito.ColName.UpdStaff.ToString()}," +
            //$"{CSVDataDaitoQR.ColName.RegDate.ToString()}," +
            $"{QRCSVDataDaito.ColName.RegStaff.ToString()}" +
            $" ) values (" +
            $"@{QRCSVDataDaito.ColName.SpotID.ToString()}," +
            $"@{QRCSVDataDaito.ColName.ParkingName.ToString()}," +
            $"@{QRCSVDataDaito.ColName.Developer.ToString()}," +
            $"@{QRCSVDataDaito.ColName.ReceptionDatetime.ToString()}," +
            $"@{QRCSVDataDaito.ColName.Company.ToString()}," +
            $"@{QRCSVDataDaito.ColName.Amount.ToString()}," +
            $"@{QRCSVDataDaito.ColName.AdjustNo.ToString()}," +
            $"@{QRCSVDataDaito.ColName.ErrorCode.ToString()}," +
            //$"@{CSVDataDaitoQR.ColName.UpdDate.ToString()}," +
            $"@{QRCSVDataDaito.ColName.UpdStaff.ToString()}," +
            //$"@{CSVDataDaitoQR.ColName.RegDate.ToString()}" +
            $"@{QRCSVDataDaito.ColName.RegStaff.ToString()}" +
            $");";



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


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

        }

        public override void Save(QRCSVData obj) {
            var data = (QRCSVDataDaito)obj;

            var bindlist = new List<(String, object)> {
                (QRCSVDataDaito.ColName.SpotID.ToString(), data.spot_id),
                (QRCSVDataDaito.ColName.ParkingName.ToString(), data.parking_name),
                (QRCSVDataDaito.ColName.Developer.ToString(), data.developer),
                (QRCSVDataDaito.ColName.ReceptionDatetime.ToString(), data.reception_datetime),
                (QRCSVDataDaito.ColName.Company.ToString(), data.company),
                (QRCSVDataDaito.ColName.Amount.ToString(), data.amount),
                (QRCSVDataDaito.ColName.AdjustNo.ToString(), data.adjust_no),
                (QRCSVDataDaito.ColName.ErrorCode.ToString(), data.error_code),
                (QRCSVDataDaito.ColName.UpdStaff.ToString(), data.upd_staff),
                (QRCSVDataDaito.ColName.RegStaff.ToString(), data.reg_staff)
            };

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

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

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

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

    }
}
