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

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 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(List<QRCSVData> list) {
            Code.ResultCode ret;
            var table = new DataTable();

            table.Columns.Add($"{QRCSVDataDaito.ColName.SpotID}");
            table.Columns.Add($"{QRCSVDataDaito.ColName.ParkingName}");
            table.Columns.Add($"{QRCSVDataDaito.ColName.Developer}");
            table.Columns.Add($"{QRCSVDataDaito.ColName.ReceptionDatetime}");
            table.Columns.Add($"{QRCSVDataDaito.ColName.Company}");
            table.Columns.Add($"{QRCSVDataDaito.ColName.Amount}");
            table.Columns.Add($"{QRCSVDataDaito.ColName.AdjustNo}");
            table.Columns.Add($"{QRCSVDataDaito.ColName.ErrorCode}");
            table.Columns.Add($"{QRCSVDataDaito.ColName.UpdStaff}");
            table.Columns.Add($"{QRCSVDataDaito.ColName.RegStaff}");

            foreach (QRCSVDataDaito data in list) {
                var row = table.NewRow();
                row[$"{QRCSVDataDaito.ColName.SpotID}"] = data.spot_id;
                row[$"{QRCSVDataDaito.ColName.ParkingName}"] = data.parking_name;
                row[$"{QRCSVDataDaito.ColName.Developer}"] = data.developer;
                row[$"{QRCSVDataDaito.ColName.ReceptionDatetime}"] = data.reception_datetime;
                row[$"{QRCSVDataDaito.ColName.Company}"] = data.company;
                row[$"{QRCSVDataDaito.ColName.Amount}"] = data.amount;
                row[$"{QRCSVDataDaito.ColName.AdjustNo}"] = data.adjust_no;
                row[$"{QRCSVDataDaito.ColName.ErrorCode}"] = data.error_code;
                row[$"{QRCSVDataDaito.ColName.UpdStaff}"] = data.upd_staff;
                row[$"{QRCSVDataDaito.ColName.RegStaff}"] = data.reg_staff;

                table.Rows.Add(row);
            }


            ret = BulkInsertData(table_name_, table);

            if (ret != Code.ResultCode.OK) {
                throw new Exception("登録失敗 Daito 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;
        }

    }
}
