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

namespace CncRepository.Impl
{
    /// <summary>
    /// 品牌实现
    /// </summary>
    public class BrandRepository : BusinessRepository, IBrandRepository
    {
        public BrandRepository(string connectionString) : base(connectionString) { }

        public Brand GetById(int id)
        {
            using (var conn = CreateConnection())
            {
                var sql = @"SELECT id as Id, brand_name as BrandName, device_field as DeviceField, tags_path as TagsPath, is_enabled as IsEnabled, created_at as CreatedAt, updated_at as UpdatedAt 
                            FROM cnc_brand WHERE id = @Id";
                return conn.QuerySingleOrDefault<Brand>(sql, new { Id = id });
            }
        }

        public List<Brand> GetAll()
        {
            using (var conn = CreateConnection())
            {
                var sql = @"SELECT id as Id, brand_name as BrandName, device_field as DeviceField, tags_path as TagsPath, is_enabled as IsEnabled, created_at as CreatedAt, updated_at as UpdatedAt FROM cnc_brand ORDER BY id";
                return conn.Query<Brand>(sql).ToList();
            }
        }

        public int Create(Brand entity)
        {
            using (var conn = CreateConnection())
            {
                var sql = @"INSERT INTO cnc_brand (brand_name, device_field, tags_path, is_enabled, created_at, updated_at) 
                            VALUES (@BrandName, @DeviceField, @TagsPath, @IsEnabled, @CreatedAt, @UpdatedAt); 
                          SELECT LAST_INSERT_ID();";
                return conn.QuerySingle<int>(sql, entity);
            }
        }

        public bool Update(Brand entity)
        {
            using (var conn = CreateConnection())
            {
                var sql = @"UPDATE cnc_brand SET brand_name = @BrandName, device_field = @DeviceField, tags_path = @TagsPath, is_enabled = @IsEnabled, updated_at = @UpdatedAt WHERE id = @Id";
                return conn.Execute(sql, entity) > 0;
            }
        }

        public bool Delete(int id)
        {
            using (var conn = CreateConnection())
            {
                var sql = @"DELETE FROM cnc_brand WHERE id = @Id";
                return conn.Execute(sql, new { Id = id }) > 0;
            }
        }

        public bool ToggleEnabled(int id)
        {
            using (var conn = CreateConnection())
            {
                var sql = @"UPDATE cnc_brand SET is_enabled = CASE WHEN is_enabled = 1 THEN 0 ELSE 1 END, updated_at = NOW() WHERE id = @Id";
                return conn.Execute(sql, new { Id = id }) > 0;
            }
        }

        public int GetFieldMappingCount(int brandId)
        {
            using (var conn = CreateConnection())
            {
                var sql = @"SELECT COUNT(*) FROM cnc_brand_field_mapping WHERE brand_id = @BrandId";
                return conn.ExecuteScalar<int>(sql, new { BrandId = brandId });
            }
        }

        public int GetCollectAddressCount(int brandId)
        {
            using (var conn = CreateConnection())
            {
                var sql = @"SELECT COUNT(*) FROM cnc_collect_address WHERE brand_id = @BrandId";
                return conn.ExecuteScalar<int>(sql, new { BrandId = brandId });
            }
        }
    }
}
