using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Linq;
using MySql.Data.MySqlClient;


namespace CSVDownloader.Store.CreditCSVData {


    class CreditCSVDataGMO : CreditCSVData {

        public String customer_code = "";
        public String output_datetime = "";
        public String processing_code = "";
        public String operation_code = "";
        public String return_code = "";
        public String device_no = "";
        public String target_company_code = "";
        public String target_company_sub_code = "";
        public String device_trade_no = "";
        public DateTime processing_date = DateTime.Today;
        public String approval_no = "";
        public int amount = 0;
        public int tax_send_amount = 0;
        public String customer_name = "";
        public String shop_name = "";
        public String cancel_receipt_no = "";
        public String split_count_info = "";
        public String paying_type = "";

        public enum ColName {
            SpotID = 100,
            CustomerCode = 0,
            OutputDatetime,
            ProcessingCode,
            OperationCode,
            ReturnCode,
            DeviceNo,
            TargetCompanyCode,
            TargetCompanySubCode,
            DeviceTradeNo,
            ProcessingDate,
            ApprovalNo,
            Amount,
            TaxSendAmount,
            CustomerName,
            ShopName,
            CancelReceiptNo,
            SplitCountInfo,
            PayingType,
            UpdDate,
            UpdStaff,
            RegDate,
            RegStaff,

        };
    }

    class CreditCSVDataGMOStore : CreditCSVDataStore {

        private static readonly String table_name_ = "creditcard_download_data_gmo";
        private String delete_sql_ = $"delete from {table_name_} where " +
            $"{CreditCSVDataGMO.ColName.ProcessingDate} between " +
            $"@From{CreditCSVDataGMO.ColName.ProcessingDate} and " +
            $"@To{CreditCSVDataGMO.ColName.ProcessingDate};";


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

        }

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

            table.Columns.Add(CreditCSVDataGMO.ColName.SpotID.ToString());
            table.Columns.Add(CreditCSVDataGMO.ColName.CustomerCode.ToString());
            table.Columns.Add(CreditCSVDataGMO.ColName.OutputDatetime.ToString());
            table.Columns.Add(CreditCSVDataGMO.ColName.ProcessingCode.ToString());
            table.Columns.Add(CreditCSVDataGMO.ColName.OperationCode.ToString());



            table.Columns.Add(CreditCSVDataGMO.ColName.ReturnCode.ToString());
            table.Columns.Add(CreditCSVDataGMO.ColName.DeviceNo.ToString());
            table.Columns.Add(CreditCSVDataGMO.ColName.TargetCompanyCode.ToString());
            table.Columns.Add(CreditCSVDataGMO.ColName.TargetCompanySubCode.ToString());
            table.Columns.Add(CreditCSVDataGMO.ColName.DeviceTradeNo.ToString());
            table.Columns.Add(CreditCSVDataGMO.ColName.ProcessingDate.ToString());
            table.Columns.Add(CreditCSVDataGMO.ColName.ApprovalNo.ToString());
            table.Columns.Add(CreditCSVDataGMO.ColName.Amount.ToString());
            table.Columns.Add(CreditCSVDataGMO.ColName.TaxSendAmount.ToString());
            table.Columns.Add(CreditCSVDataGMO.ColName.CustomerName.ToString());
            table.Columns.Add(CreditCSVDataGMO.ColName.ShopName.ToString());
            table.Columns.Add(CreditCSVDataGMO.ColName.CancelReceiptNo.ToString());
            table.Columns.Add(CreditCSVDataGMO.ColName.SplitCountInfo.ToString());
            table.Columns.Add(CreditCSVDataGMO.ColName.PayingType.ToString());
            table.Columns.Add(CreditCSVDataGMO.ColName.UpdStaff.ToString());
            table.Columns.Add(CreditCSVDataGMO.ColName.RegStaff.ToString());

            foreach (CreditCSVDataGMO data in list) {
                var row = table.NewRow();
                row[CreditCSVDataGMO.ColName.SpotID.ToString()] = data.spot_id;
                row[CreditCSVDataGMO.ColName.CustomerCode.ToString()] = data.customer_code;


                row[CreditCSVDataGMO.ColName.OutputDatetime.ToString()] = data.output_datetime;
                row[CreditCSVDataGMO.ColName.ProcessingCode.ToString()] = data.processing_code;
                row[CreditCSVDataGMO.ColName.OperationCode.ToString()] = data.operation_code;
                row[CreditCSVDataGMO.ColName.ReturnCode.ToString()] = data.return_code;
                row[CreditCSVDataGMO.ColName.DeviceNo.ToString()] = data.device_no;
                row[CreditCSVDataGMO.ColName.TargetCompanyCode.ToString()] = data.target_company_code;
                row[CreditCSVDataGMO.ColName.TargetCompanySubCode.ToString()] = data.target_company_sub_code;
                row[CreditCSVDataGMO.ColName.DeviceTradeNo.ToString()] = data.device_trade_no;
                row[CreditCSVDataGMO.ColName.ProcessingDate.ToString()] = data.processing_date;
                row[CreditCSVDataGMO.ColName.ApprovalNo.ToString()] = data.approval_no;
                row[CreditCSVDataGMO.ColName.Amount.ToString()] = data.amount;
                row[CreditCSVDataGMO.ColName.TaxSendAmount.ToString()] = data.tax_send_amount;
                row[CreditCSVDataGMO.ColName.CustomerName.ToString()] = data.customer_name;
                row[CreditCSVDataGMO.ColName.ShopName.ToString()] = data.shop_name;
                row[CreditCSVDataGMO.ColName.CancelReceiptNo.ToString()] = data.cancel_receipt_no;
                row[CreditCSVDataGMO.ColName.SplitCountInfo.ToString()] = data.split_count_info;
                row[CreditCSVDataGMO.ColName.PayingType.ToString()] = data.paying_type;
                row[CreditCSVDataGMO.ColName.UpdStaff.ToString()] = data.upd_staff;
                row[CreditCSVDataGMO.ColName.RegStaff.ToString()] = data.reg_staff;

                table.Rows.Add(row);
            }


            ret = BulkInsertData(table_name_, table);

            if (ret != Code.ResultCode.OK) {
                throw new Exception("登録失敗 GMOクレジット");
            }

        }

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

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

            var delete_count = DeleteData(delete_sql_, bindlist);
            if (delete_count == -1) {
                throw new Exception("削除失敗 GMOクレジット");
            }
            return delete_count;
        }
    }
}
