using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ASHermed.SinoInterface.Api.ContractModel.Quest;
using ASHermed.SinoInterface.Api.IRepository.Quest;
using ASHermed.SinoInterface.Repository.MySql;

namespace ASHermed.SinoInterface.Api.MySqlRepository.Quest
{
    public class PubQuestFieIdRepository : BaseRepositoryToASHermed_SinoInterface<Pub_Quest_FieId>, IPubQuestFieIdRepository
    {
        /// <summary>
        /// 新增返回id
        /// </summary>
        /// <param name="questTest"></param>
        /// <returns></returns>
        public async Task<int> InsertAndReturnIdAsync(Pub_Quest_FieId questTest)
        {
            const string sql = @"INSERT INTO pub_quest_fieid ( `Name`, NameEN, NameGE, Type, IsDelete, CreateTime, CreateUser, Config, CategoryId, UpdateTime, UpdateUser, Required, PlaceHolder, PlaceHolderEN, PlaceHolderGE, IsShow, Sort, OptionValue)
                                 VALUES
                                 	(
                                 	  @NAME,
                                 	  @NameEN,
                                 	  @NameGE,
                                 	  @Type,
                                 	  @IsDelete,
                                      @CreateTime,
                                      @CreateUser,
                                      @Config, @CategoryId, @UpdateTime, @UpdateUser,@Required, @PlaceHolder, @PlaceHolderEN, @PlaceHolderGE, @IsShow, @Sort, @OptionValue)";

            var result = await InsertAsync(sql, questTest);

            return result;
        }

        /// <summary>
        /// 根据id查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Pub_Quest_FieId> GetByIdAsync(int id)
        {
            const string sql = "select * from pub_quest_fieid where IsDelete=false and Id=@id ";

            var parms = new Dictionary<string, object>
            {
                {"@id", id}
            };

            var result = await QueryAsync<Pub_Quest_FieId>(sql, parms);

            return result.FirstOrDefault();
        }

        /// <summary>
        /// 根据name查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Pub_Quest_FieId> GetByNameAsync(string name)
        {
            const string sql = "select * from pub_quest_fieid where IsDelete=false and Name=@Name ";

            var parms = new Dictionary<string, object>
            {
                {"@Name", name}
            };

            var result = await QueryAsync<Pub_Quest_FieId>(sql, parms);

            return result.FirstOrDefault();
        }

        public async Task<List<Pub_Quest_FieId>> GetFieIdForCateId(List<int> cateId)
        {
            var sql = @" select * from pub_quest_fieid where IsDelete=false and CategoryId IN @CategoryId ";
            var result = await QueryAsync<Pub_Quest_FieId>(sql, new { CategoryId = cateId });
            return result.ToList();
        }

        public async Task<int> UpdateFied(Pub_Quest_FieId fieId)
        {
            var sql = @" UPDATE `pub_quest_fieid` SET `Name` = @Name, `NameEN` = @NameEN, `NameGE` = @NameGE, `Type` = @Type, `IsDelete` = @IsDelete, `CreateTime` = @CreateTime, `CreateUser` = @CreateUser, `Config` = @Config, `CategoryId` = @CategoryId, `UpdateTime` = @UpdateTime, `UpdateUser` = @UpdateUser, `Required` = @Required, `PlaceHolder` = @PlaceHolder, `PlaceHolderEN` = @PlaceHolderEN, `PlaceHolderGE` = @PlaceHolderGE, `IsShow` = @IsShow, `Sort` = @Sort, `OptionValue` = @OptionValue WHERE `Id` = @Id ";
            return Execute(sql, fieId);
        }
    }
}
