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

namespace Ropin.Inspection.Repository.DEV
{
    //命令
    public class DevCmdRepository : RepositoryBase<TDEV_Cmd, string>, IDevCmdRepository
    {
        public DevCmdRepository(InspectionDbContext DbContext) : base(DbContext)
        {

        }
        public Task<IEnumerable<DevCmdViewModel>> GetConditionAsync(DevCmdSearchModel searchModel)
        {
            MySqlConnector.MySqlParameter[] parameters = new[] {
                new MySqlConnector.MySqlParameter("name",  "%"+searchModel?.C_Name+"%"),
                new MySqlConnector.MySqlParameter("type", searchModel.C_Type),
                new MySqlConnector.MySqlParameter("orgCode", searchModel.C_OrgCode),
                new MySqlConnector.MySqlParameter("status", searchModel.C_Status),
                new MySqlConnector.MySqlParameter("Id ", searchModel.C_ID)
            };
            StringBuilder sql = new StringBuilder();
            sql.Append(@"select * from(
select c.*,d.C_Name as C_TypeName,o.C_Name as C_OrgName from TDEV_Cmd c
LEFT JOIN TBDM_CodeDetail d on (c.C_Type=d.C_Code)
LEFT JOIN TSYS_Org o on (c.C_OrgCode=o.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_Type))
            {
                sql.Append(" and C_Type=@type ");
            }
            if (!string.IsNullOrEmpty(searchModel.C_OrgCode))
            {
                sql.Append(" and C_OrgCode=@orgCode ");
            }
            if (!string.IsNullOrEmpty(searchModel.C_ID))
            {
                sql.Append(" and C_ID=@Id ");
            }
            sql.Append(" order by D_CreateOn desc ");

            IEnumerable<DevCmdViewModel> recordItemlist = EntityFrameworkCoreExtensions.GetList<DevCmdViewModel>(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 class DevInstructionRepository : RepositoryBase<TDEV_Instruction, string>, IDevInstructionRepository
    {
        public DevInstructionRepository(InspectionDbContext DbContext) : base(DbContext)
        {

        }
        public Task<IEnumerable<DevInstructionViewModel>> GetConditionAsync(DevInstructionSearchModel searchModel)
        {
            MySqlConnector.MySqlParameter[] parameters = new[] {
                new MySqlConnector.MySqlParameter("name",  "%"+searchModel?.C_Name+"%"),
                new MySqlConnector.MySqlParameter("protocol", searchModel.C_Protocol),
                new MySqlConnector.MySqlParameter("boxCode", searchModel.C_BoxCode),
                new MySqlConnector.MySqlParameter("status", searchModel.C_Status),
                new MySqlConnector.MySqlParameter("Id ", searchModel.C_ID),
                new MySqlConnector.MySqlParameter("cmdCode", searchModel.CmdCode)
            };
            StringBuilder sql = new StringBuilder();
            sql.Append(@"select * from(
select t.*,d.C_Name as C_ProtocolName,b.C_Name as C_BoxName,cI.C_CmdCode,cI.I_ExeOrder from TDEV_Instruction t
LEFT JOIN TBDM_CodeDetail d on (t.C_Protocol=d.C_Code)
LEFT JOIN TDEV_Box b on (t.C_BoxCode=b.C_ID)
LEFT JOIN TDEV_CmdInstruction cI on (cI.C_Instruction=t.C_ID)
)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_Protocol))
            {
                sql.Append(" and C_Protocol=@protocol ");
            }
            if (!string.IsNullOrEmpty(searchModel.C_BoxCode))
            {
                sql.Append(" and C_BoxCode=@boxCode ");
            }
            if (!string.IsNullOrEmpty(searchModel.C_ID))
            {
                sql.Append(" and C_ID=@Id ");
            }
            if (!string.IsNullOrEmpty(searchModel.CmdCode))
            {
                sql.Append(" and C_CmdCode=@cmdCode ");
            }
            sql.Append(" order by D_CreateOn desc ");

            IEnumerable<DevInstructionViewModel> recordItemlist = EntityFrameworkCoreExtensions.GetList<DevInstructionViewModel>(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);
        }

        /// <summary>
        /// 根据命令获取指令信息
        /// </summary>
        /// <param name="searchModel"></param>
        /// <returns></returns>
        public Task<List<DevInstructionInfo>> GetInstructionByCmdCode(DevInstructionSearchModel searchModel)
        {
            MySqlConnector.MySqlParameter[] parameters = new[] {
                new MySqlConnector.MySqlParameter("cmdCode", searchModel.CmdCode)
            };
            StringBuilder sql = new StringBuilder();
            sql.Append(@"select * from (
select cI.C_CmdCode,cI.I_ExeOrder,I.*
from TDEV_CmdInstruction cI
LEFT JOIN TDEV_Instruction I on (cI.C_Instruction=I.C_ID)
) tab 
where C_Status='1' 
and C_CmdCode=@cmdCode
order by I_ExeOrder asc");
            IEnumerable<DevInstructionInfo> recordItemlist = EntityFrameworkCoreExtensions.GetList<DevInstructionInfo>(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(recordItemlist?.ToList());
        }
    }
    //命令-指令
    public class DevCmdInstructionRepository : RepositoryBase<TDEV_CmdInstruction, string>, IDevCmdInstructionRepository
    {
        public DevCmdInstructionRepository(InspectionDbContext DbContext) : base(DbContext)
        {

        }
        public Task<IEnumerable<DevCmdInstructionViewModel>> GetConditionAsync(DevCmdInstructionSearchModel searchModel)
        {
            MySqlConnector.MySqlParameter[] parameters = new[] {
                new MySqlConnector.MySqlParameter("cmdCode", searchModel.C_CmdCode),
                new MySqlConnector.MySqlParameter("cmdName",  "%"+searchModel?.C_CmdName+"%"),
                new MySqlConnector.MySqlParameter("instruction", searchModel.C_Instruction),
                new MySqlConnector.MySqlParameter("InstructionName",  "%"+searchModel?.C_InstructionName+"%"),
                new MySqlConnector.MySqlParameter("Id ", searchModel.C_ID)
            };
            StringBuilder sql = new StringBuilder();
            sql.Append(@"select * from (
select t.*,c.C_Name as C_CmdName,i.C_Name as C_InstructionName from TDEV_CmdInstruction t
LEFT JOIN TDEV_Cmd c on (t.C_CmdCode=c.C_ID)
LEFT JOIN TDEV_Instruction i on (t.C_Instruction=i.C_ID)
) tab where 1=1");
            if (!string.IsNullOrEmpty(searchModel.C_CmdCode))
            {
                sql.Append(" and C_CmdCode=@cmdCode ");
            }
            if (!string.IsNullOrEmpty(searchModel.C_CmdName))
            {
                sql.Append(" and C_CmdName like @cmdName ");
            }
            if (!string.IsNullOrEmpty(searchModel.C_Instruction))
            {
                sql.Append(" and C_Instruction=@instruction ");
            }
            if (!string.IsNullOrEmpty(searchModel.C_InstructionName))
            {
                sql.Append(" and C_InstructionName like @InstructionName ");
            }
            if (!string.IsNullOrEmpty(searchModel.C_ID))
            {
                sql.Append(" and C_ID=@Id ");
            }
            sql.Append(" order by D_CreateOn desc ");

            IEnumerable<DevCmdInstructionViewModel> recordItemlist = EntityFrameworkCoreExtensions.GetList<DevCmdInstructionViewModel>(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> DeleteBYCmdCode(string cmdCode)
        {
            MySqlConnector.MySqlParameter[] parameters = new[] { new MySqlConnector.MySqlParameter("cmdCode", cmdCode) };
            string sql = "DELETE FROM TDEV_CmdInstruction where C_CmdCode=@cmdCode ";
            int iResult = EntityFrameworkCoreExtensions.ExecuteSqlNoQuery(DbContext.Database, sql, parameters);
            return Task.FromResult(true);
        }
        public Task<bool> DeleteBYInstruction(string Instruction)
        {
            MySqlConnector.MySqlParameter[] parameters = new[] { new MySqlConnector.MySqlParameter("Instruction", Instruction) };
            string sql = "DELETE FROM TDEV_CmdInstruction where C_Instruction=@Instruction ";
            int iResult = EntityFrameworkCoreExtensions.ExecuteSqlNoQuery(DbContext.Database, sql, parameters);
            return Task.FromResult(true);
        }
    }
}