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 LargeScreenEventRepository : RepositoryBase<TLGS_LargeScreenEvent, string>, ILargeScreenEventRepository
    {
        public LargeScreenEventRepository(InspectionDbContext dbContext) : base(dbContext)
        {
        }
        public Task<IEnumerable<LargeScreenEventViewModel>> GetConditionAsync(LargeScreenEventSearch searchModel)
        {
            MySqlConnector.MySqlParameter[] parameters = new[] {
                new MySqlConnector.MySqlParameter("Status", searchModel?.C_Status),
                new MySqlConnector.MySqlParameter("name",  "%"+searchModel?.C_Name+"%"),
                new MySqlConnector.MySqlParameter("ControlCode", searchModel?.C_ControlCode),
                new MySqlConnector.MySqlParameter("devStoreCode", searchModel?.C_DevStoreCode),
                new MySqlConnector.MySqlParameter("boxCode", searchModel?.C_BoxCode),
                new MySqlConnector.MySqlParameter("type ", searchModel?.C_Type),
                new MySqlConnector.MySqlParameter("Id ", searchModel?.C_ID)
            };
            StringBuilder sql = new StringBuilder();
            sql.Append(@"select * from (
select t.*,c.C_ControlName,s.C_Name as C_DevStoreName,b.C_Name as C_BoxName,d.C_Name as C_TypeName
from TLGS_LargeScreenEvent t
LEFT JOIN TLGS_LargeScreenControl c on (t.C_ControlCode=c.C_ID)
LEFT JOIN TDEV_DevStore s on (t.C_DevStoreCode=s.C_ID)
LEFT JOIN TDEV_Box b on (t.C_BoxCode=b.C_ID)
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_Name))
            {
                sql.Append(" and C_Name like @name");
            }
            if (!string.IsNullOrEmpty(searchModel.C_ControlCode))
            {
                sql.Append(" and C_ControlCode=@ControlCode ");
            }
            if (!string.IsNullOrEmpty(searchModel.C_DevStoreCode))
            {
                sql.Append(" and C_DevStoreCode=@devStoreCode ");
            }
            if (!string.IsNullOrEmpty(searchModel.C_BoxCode))
            {
                sql.Append(" and C_BoxCode=@boxCode ");
            }
            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<LargeScreenEventViewModel> recordItemlist = EntityFrameworkCoreExtensions.GetList<LargeScreenEventViewModel>(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);
        }

        public Task<bool> UpdateLargeScreenEventStatusBYId(string Id,string satus)
        {
            MySqlConnector.MySqlParameter[] parameters = new[] { 
                new MySqlConnector.MySqlParameter("satus", satus),
                new MySqlConnector.MySqlParameter("id", Id)
            };
            string sql = "UPDATE TLGS_LargeScreenEvent SET C_Status = @satus WHERE (C_ID =@id); ";
            int iResult = EntityFrameworkCoreExtensions.ExecuteSqlNoQuery(DbContext.Database, sql, parameters);
            return Task.FromResult(true);
        }
    }
}