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

using MySql.Data.MySqlClient;

namespace CSVDownloader.Store {


    class ParkingCreditCardAgenciesSpotIDError {

        public string creditcard_agencies_id = "";
        public string creditcard_agencies_spot_name = "";
        public DateTime upd_date = DateTime.Now;
        public string upd_staff = "auto_tool";
        public DateTime reg_date = DateTime.Now;
        public string reg_staff = "auto_tool";

        public enum ColName {
            CreditcardAgenciesID,
            CreditcardAgenciesSpotName,
            UpdDate,
            UpdStaff,
            RegDate,
            RegStaff
        };
    }

    class ParkingCreditCardAgenciesSpotIDErrorDAO : MySQL {


        private static readonly string table_name_ = "parking_creditcard_agencies_spotid_error";
        public ParkingCreditCardAgenciesSpotIDErrorDAO(MySqlConnection conn) : base(conn) {

        }

        public void Save(List<ParkingCreditCardAgenciesSpotIDError> list) {
            Code.ResultCode ret;
            var table = new DataTable();

            table.Columns.Add(ParkingCreditCardAgenciesSpotIDError.ColName.CreditcardAgenciesID.ToString());
            table.Columns.Add(ParkingCreditCardAgenciesSpotIDError.ColName.CreditcardAgenciesSpotName.ToString());
            table.Columns.Add(ParkingCreditCardAgenciesSpotIDError.ColName.UpdStaff.ToString());
            table.Columns.Add(ParkingCreditCardAgenciesSpotIDError.ColName.RegStaff.ToString());

            foreach (ParkingCreditCardAgenciesSpotIDError data in list) {
                var row = table.NewRow();
                row[ParkingCreditCardAgenciesSpotIDError.ColName.CreditcardAgenciesID.ToString()] = data.creditcard_agencies_id;
                row[ParkingCreditCardAgenciesSpotIDError.ColName.CreditcardAgenciesSpotName.ToString()] = data.creditcard_agencies_spot_name;
                row[ParkingCreditCardAgenciesSpotIDError.ColName.UpdStaff.ToString()] = data.upd_staff;
                row[ParkingCreditCardAgenciesSpotIDError.ColName.RegStaff.ToString()] = data.reg_staff;

                table.Rows.Add(row);
            }


            ret = BulkInsertData(table_name_, table);

            if (ret != Code.ResultCode.OK) {
                throw new Exception("登録失敗 紐づけエラー");
            }

        }

        public bool Exists(Code.CreditAgent agent, String spot_name) {

            string code = Code.CreditAgenciesMap.GetID(agent);

            var tbl = new DataTable();

            string replace_id = "@id";
            string replace_spot_name = "@spot_name";
            string sql = $"select count(*) from {table_name_} " +
                $"where CreditcardAgenciesID = {replace_id} ;" +
                $"CreditcardAgenciesSpotName = {replace_spot_name};";

            using (var adpt = new MySqlDataAdapter(sql, conn_)) {

                adpt.SelectCommand.Parameters.AddWithValue(replace_id, code);
                adpt.SelectCommand.Parameters.AddWithValue(replace_spot_name, spot_name);
                adpt.SelectCommand.Prepare();
                adpt.Fill(tbl);
            }


            return 0 < int.Parse(tbl.Rows[0][0].ToString());

        }


        public int DeleteAll() {
            var bind_list = new List<(string, object)>();
            string sql = $"delete from {table_name_} ";
            return DeleteData(sql, bind_list);

        }
        public int Delete(Code.CreditAgent agent, String spot_name) {


            string code = Code.CreditAgenciesMap.GetID(agent);

            var bind_list = new List<(string, object)>();

            string replace_id = "@id";
            string replace_spot_name = "@spot_name";
            string sql = $"delete from {table_name_} " +
                $"where CreditcardAgenciesID = {replace_id} ;" +
                $"CreditcardAgenciesSpotName = {replace_spot_name};";

            bind_list.Add((replace_id, code));
            bind_list.Add((replace_spot_name, spot_name));

            return DeleteData(sql, bind_list);
        }
    }

}