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

namespace ASHermed.SinoInterface.Api.MySqlRepository.App
{
    public class PubMenberCountRepository : BaseRepositoryToASHermed_SinoInterface<Pub_Menber_Count>, IPubMenberCountRepository
    {
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="patMrn">patMrn</param>
        /// <param name="category">类别</param>
        /// <returns></returns>
        public async Task<List<Pub_Menber_Count>> QueryAsync(int id, string patMrn, int category)
        {
            var sqlQueryBuilder = new StringBuilder();
            sqlQueryBuilder.Append(" select * from pub_menber_count where 1=1 ");
            
            var parms = new Dictionary<string, object>();

            if (id != 0)
            {
                sqlQueryBuilder.Append(" and id=@id ");
                parms.Add("@id",id);
            }

            if (!string.IsNullOrEmpty(patMrn))
            {
                sqlQueryBuilder.Append(" and PatMrn=@patMrn ");
                parms.Add("@patMrn", patMrn);
            }
            else
            {
                sqlQueryBuilder.Append(" and PatMrn is null ");
            }

            if (category != 0)
            {
                sqlQueryBuilder.Append(" and Category=@category ");
                parms.Add("@category", category);
            }

            var entities = await QueryAsync<Pub_Menber_Count>(sqlQueryBuilder.ToString(), parms);

            return entities.ToList();
        }

        public async Task<int> InsertAsync(Pub_Menber_Count entity)
        {
            const string sql = "insert into pub_menber_count (PatMrn,Category,Quantity) values(@patMrn,@category,@quantity)";

            var parms = new Dictionary<string, object>
            {
                {"@category", entity.Category},
                {"@quantity", entity.Quantity},
                {"@patMrn", !string.IsNullOrEmpty(entity.PatMrn) ? entity.PatMrn : null}
            };

            var result = await ExecuteAsync(sql, parms);
            return result;
        }

        public async Task<int> UpdateQuantityAsync(int id, int quantity)
        {
            const string sql = "update pub_menber_count set Quantity=@quantity where Id=@id";

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

            var result = await ExecuteAsync(sql, parms);
            return result;
        }
    }
}