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 PubQuestOptionRepository : BaseRepositoryToASHermed_SinoInterface<Pub_Quest_Option>, IPubQuestOptionRepository
    {
        /// <summary>
        /// 新增返回id
        /// </summary>
        /// <param name="questTestOption"></param>
        /// <returns></returns>
        public async Task<int> InsertAndReturnIdAsync(Pub_Quest_Option questTestOption)
        {
            const string sql = @"INSERT INTO pub_quest_option ( `Name`, NameEN, NameGE, FieldId, IsDelete, Config, Value, IsOtherTxt, CreateTime, CreateUser, UpdateTime, UpdateUser,OtherPlaceHolder,OtherPlaceHolderEN,OtherPlaceHolderGE,HideLogActionIds,ShowLogActionIds,IsNone,Sort, Required)
                                VALUES
                                	(
                                		@NAME,
                                		@NameEN,
                                		@NameGE,
                                	    @FieldId,
                                	    @IsDelete,
                                        @Config, @Value, @IsOtherTxt, @CreateTime, @CreateUser, @UpdateTime, @UpdateUser, @OtherPlaceHolder, @OtherPlaceHolderEN, @OtherPlaceHolderGE, @HideLogActionIds, @ShowLogActionIds, @IsNone, @Sort, @Required)";

            var insertResult = await InsertAsync(sql, questTestOption);

            return insertResult;
        }

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

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

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

            return result.FirstOrDefault();
        }

        public async Task<List<Pub_Quest_Option>> GetOptionForFiedid(List<int> fiedId)
        {
            var sql = @" select * from pub_quest_option where IsDelete=false and FieldId IN @FieldId ";
            var result = await QueryAsync<Pub_Quest_Option>(sql, new { FieldId = fiedId });
            return result.ToList();
        }

        public async Task<int> UpdateOption(Pub_Quest_Option option)
        {
            var sql = @" UPDATE `pub_quest_option` SET `Name` = @Name, `NameEN` = @NameEN, `NameGE` = @NameGE, `FieldId` = @FieldId, `IsDelete` = @IsDelete, `Config` = @Config, `Value` = @Value, `IsOtherTxt` = @IsOtherTxt, `CreateTime` = @CreateTime, `CreateUser` = @CreateUser, `UpdateTime` = @UpdateTime, `UpdateUser` = @UpdateUser, `OtherPlaceHolder` = @OtherPlaceHolder, `HideLogActionIds` = @HideLogActionIds, `ShowLogActionIds` = @ShowLogActionIds, `OtherPlaceHolderEN` = @OtherPlaceHolderEN, `OtherPlaceHolderGE` = @OtherPlaceHolderGE, `IsNone` = @IsNone, `Sort` =@Sort, `Required` = @Required WHERE `Id` = @Id ";
            return Execute(sql, option);
        }
    }
}
