using Microsoft.EntityFrameworkCore;
using MySql.Data.MySqlClient;
using Ropin.Inspection.Model.Entities;
using Ropin.Inspection.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Ropin.Inspection.Repository
{
    public class TispSpotContentRepository : RepositoryBase<TISP_SpotContent, Guid>, ITispSpotContentRepository
    {
        public TispSpotContentRepository(InspectionDbContext dbContext) : base(dbContext)
        {

        }
        public IEnumerable<TISP_Content>  GetBySpotCodeAsync(Guid id)
        {
            MySqlConnector.MySqlParameter[] parameters = new[]{new MySqlConnector.MySqlParameter("Id", id),new MySqlConnector.MySqlParameter("Name", "zhansan")};
            var contentlist = EntityFrameworkCoreExtensions.SqlQuery<TISP_Content>(DbContext.Database, "select A.* from TISP_Content A, TISP_Spot B ,TISP_SpotContent C WHERE A.C_ID = C.C_ContentCode AND B.C_Code = C.C_SpotCode AND C.C_SpotCode = @Id", parameters);
            //var contentlist =  (from t1 in DbContext.TISP_Content
            //                    join t2 in DbContext.TISP_SpotContent on t1.C_ID equals t2.C_ContentCode
            //                    select t1).AsEnumerable();
            return contentlist;
        }
        public Task DeleteByConditionAsync(Expression<Func<TISP_SpotContent, bool>> expression)
        {            
            return Task.FromResult(DbContext.Set<TISP_SpotContent>().Where(expression).AsEnumerable());
        }

        public Task<int> DeleteBySpotIdAsync(Guid spotId)
        {
            MySqlConnector.MySqlParameter[] parameters = new[] { new MySqlConnector.MySqlParameter("Id", spotId) };
            int result =  EntityFrameworkCoreExtensions.ExecuteSqlNoQuery(DbContext.Database, "DELETE from TISP_SpotContent WHERE C_SpotCode = @Id", parameters);
            return Task.FromResult(result);

        }
    }
}