using Ropin.Inspection.Model.Entities;
using Ropin.Inspection.Model.SearchModel.LGS;
using Ropin.Inspection.Model.ViewModel.LGS;
using Ropin.Inspection.Repository.LGS.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ropin.Inspection.Repository.LGS
{
    public class LargeScreenControlRepository : RepositoryBase<TLGS_LargeScreenControl, string>, ILargeScreenControlRepository
    {
        public LargeScreenControlRepository(InspectionDbContext dbContext) : base(dbContext)
        {
        }
        public Task<bool> DeleteByLargeScreenCodeAsync(string largeScreenCode)
        {
            MySqlConnector.MySqlParameter[] parameters = new[] { new MySqlConnector.MySqlParameter("code", largeScreenCode) };
            string sql = "DELETE FROM TLGS_LargeScreenControl where C_LargeScreenCode=@code ";
            int iResult = EntityFrameworkCoreExtensions.ExecuteSqlNoQuery(DbContext.Database, sql, parameters);
            bool result = iResult > 0;
            return Task.FromResult(result);
        }
        public Task<IEnumerable<LargeScreenControlViewModel>> GetConditionAsync(LargeScreenControlSearch searchModel)
        {
            MySqlConnector.MySqlParameter[] parameters = new[] {
                new MySqlConnector.MySqlParameter("Status", searchModel?.C_Status),
                new MySqlConnector.MySqlParameter("ControlName",  "%"+searchModel?.C_ControlName+"%"),
                new MySqlConnector.MySqlParameter("LargeScreenCode", searchModel?.C_LargeScreenCode),
                new MySqlConnector.MySqlParameter("type ", searchModel?.C_Type),
                new MySqlConnector.MySqlParameter("Id ", searchModel?.C_ID)
            };
            StringBuilder sql = new StringBuilder();
            sql.Append(@"select * from (
select t.*,d.C_Name as C_TypeName
from TLGS_LargeScreenControl t
LEFT JOIN TBDM_CodeDetail d on (t.C_Type=d.C_Code)
) tab  where 1=1");

            if (!string.IsNullOrEmpty(searchModel.C_Status))
            {
                sql.Append(" and C_Status=@Status ");
            }
            if (!string.IsNullOrEmpty(searchModel.C_ControlName))
            {
                sql.Append(" and C_ControlName like @ControlName");
            }
            if (!string.IsNullOrEmpty(searchModel.C_LargeScreenCode))
            {
                sql.Append(" and C_LargeScreenCode=@LargeScreenCode ");
            }
            if (!string.IsNullOrEmpty(searchModel.C_Type))
            {
                sql.Append(" and C_Type=@type ");
            }
            if (!string.IsNullOrEmpty(searchModel.C_ID))
            {
                sql.Append(" and C_ID=@Id ");
            }
            sql.Append(" order by D_CreateOn desc ");

            IEnumerable<LargeScreenControlViewModel> recordItemlist = EntityFrameworkCoreExtensions.GetList<LargeScreenControlViewModel>(DbContext.Database, sql.ToString(), parameters);
            searchModel.TotalCount = recordItemlist.First() != null ? recordItemlist.ToList().Count : 0;
            if (recordItemlist.Count() == 1 && recordItemlist.First() == null)
            {
                recordItemlist = null;
            }
            return Task.FromResult(searchModel.IsPagination ? recordItemlist?.Skip((searchModel.PageIndex - 1) * searchModel.PageSize).Take(searchModel.PageSize) : recordItemlist);
        }
    }
}