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 PubQuestDataDetailRepository : BaseRepositoryToASHermed_SinoInterface<Pub_Quest_DataDetail>, IPubQuestDataDetailRepository
    {
        /// <summary>
        /// 新增返回id
        /// </summary>
        /// <param name="questPatTestInfo"></param>
        /// <returns></returns>
        public async Task<int> InsertAndReturnIdAsync(Pub_Quest_DataDetail questPatTestInfo)
        {
            const string sql = @"INSERT INTO pub_quest_datadetail ( InfoId, FieIdId, FieIdOptionId, `Value`, OtherTxt, CategoryId )
                                VALUES
                                  	(
                                  		@InfoId,
                                  		@FieIdId,
                                  		@FieIdOptionId,
                                  	    @Value, @OtherTxt, @CategoryId
                                  	)";

            var insertResult = await ExecuteAsync(sql, questPatTestInfo);

            return insertResult;
        }

        public async Task<int> InsertAsync(List<Pub_Quest_DataDetail> questPatTestInfo)
        {
            const string sql = @"INSERT INTO pub_quest_datadetail ( InfoId, FieIdId, FieIdOptionId, `Value`, OtherTxt, CategoryId, DataId, IsDelete )
                                VALUES
                                  	(
                                  		@InfoId,
                                  		@FieIdId,
                                  		@FieIdOptionId,
                                  	    @Value, @OtherTxt, @CategoryId, @DataId, @IsDelete 
                                  	)";

            var insertResult = await ExecuteAsync(sql, questPatTestInfo);

            return insertResult;
        }

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

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

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

            return result.FirstOrDefault();
        }

        public async Task<List<Pub_Quest_DataDetail>> GetByDataIdAsync(int dataId)
        {
            const string sql = "select * from pub_quest_datadetail where DataId=@DataId And IsDelete = 0 ";

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

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

            return result.ToList();
        }

        public async Task<int> DeleteAsync(int dataId)
        {
            var sql = " Update pub_quest_datadetail Set IsDelete = 1 Where DataId = @DataId ";
            return await ExecuteAsync(sql, new { DataId = dataId });
        }
    }
}
