using System.Collections.Generic;
using System.Linq;
using Dapper;
using CncModels.Entity;
using CncRepository.Base;
using CncRepository.Interface;
using System.Data;

namespace CncRepository.Impl
{
    /// <summary>
    /// 品牌字段映射实现
    /// </summary>
    public class BrandFieldMappingRepository : BusinessRepository, IBrandFieldMappingRepository
    {
        public BrandFieldMappingRepository(string connectionString) : base(connectionString) { }

        public List<BrandFieldMapping> GetByBrandId(int brandId)
        {
            using (var conn = CreateConnection())
            {
                var sql = @"SELECT id as Id, brand_id as BrandId, standard_field as StandardField, field_name as FieldName, match_by as MatchBy, data_type as DataType, is_required as IsRequired, is_enabled as IsEnabled, created_at as CreatedAt 
                             FROM cnc_brand_field_mapping WHERE brand_id = @BrandId ORDER BY id";
                return conn.Query<BrandFieldMapping>(sql, new { BrandId = brandId }).ToList();
            }
        }

        public BrandFieldMapping GetById(int id)
        {
            using (var conn = CreateConnection())
            {
                var sql = @"SELECT id as Id, brand_id as BrandId, standard_field as StandardField, field_name as FieldName, match_by as MatchBy, data_type as DataType, is_required as IsRequired, is_enabled as IsEnabled, created_at as CreatedAt 
                             FROM cnc_brand_field_mapping WHERE id = @Id";
                return conn.QuerySingleOrDefault<BrandFieldMapping>(sql, new { Id = id });
            }
        }

        public int Create(BrandFieldMapping entity)
        {
            using (var conn = CreateConnection())
            {
                var sql = @"INSERT INTO cnc_brand_field_mapping (brand_id, standard_field, field_name, match_by, data_type, is_required, is_enabled, created_at) 
                            VALUES (@BrandId, @StandardField, @FieldName, @MatchBy, @DataType, @IsRequired, @IsEnabled, @CreatedAt); 
                              SELECT LAST_INSERT_ID();";
                return conn.QuerySingle<int>(sql, entity);
            }
        }

        public bool Update(BrandFieldMapping entity)
        {
            using (var conn = CreateConnection())
            {
                var sql = @"UPDATE cnc_brand_field_mapping SET brand_id = @BrandId, standard_field = @StandardField, field_name = @FieldName, match_by = @MatchBy, data_type = @DataType, is_required = @IsRequired, is_enabled = @IsEnabled, created_at = @CreatedAt WHERE id = @Id";
                return conn.Execute(sql, entity) > 0;
            }
        }

        public bool DeleteByBrandId(int brandId)
        {
            using (var conn = CreateConnection())
            {
                var sql = @"DELETE FROM cnc_brand_field_mapping WHERE brand_id = @BrandId";
                return conn.Execute(sql, new { BrandId = brandId }) > 0;
            }
        }

        public int BatchCreate(int brandId, List<BrandFieldMapping> mappings)
        {
            using (var conn = CreateConnection())
            {
                conn.Open();
                using (var tran = conn.BeginTransaction())
                {
                    try
                    {
                        int count = 0;
                                var sql = @"INSERT INTO cnc_brand_field_mapping (brand_id, standard_field, field_name, match_by, data_type, is_required, is_enabled, created_at) 
                                    VALUES (@BrandId, @StandardField, @FieldName, @MatchBy, @DataType, @IsRequired, @IsEnabled, @CreatedAt); 
                                      SELECT LAST_INSERT_ID();";
                        foreach (var m in mappings)
                        {
                            m.BrandId = brandId;
                            var id = conn.QuerySingle<int>(sql, m, tran);
                            count++;
                        }
                        tran.Commit();
                        return count;
                    }
                    catch
                    {
                        tran.Rollback();
                        throw;
                    }
                }
            }
        }

        public List<BrandFieldMapping> GetEnabledByBrandId(int brandId)
        {
            using (var conn = CreateConnection())
            {
                var sql = @"SELECT id as Id, brand_id as BrandId, standard_field as StandardField, field_name as FieldName, match_by as MatchBy, data_type as DataType, is_required as IsRequired, is_enabled as IsEnabled, created_at as CreatedAt 
                            FROM cnc_brand_field_mapping WHERE brand_id = @BrandId AND is_enabled = 1 ORDER BY id";
                return conn.Query<BrandFieldMapping>(sql, new { BrandId = brandId }).ToList();
            }
        }
    }
}
