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

namespace ASHermed.SinoInterface.Api.MySqlRepository
{

    public class VaccineAptRepository : BaseRepositoryToASHermed_SinoInterface<Out_Vaccine_Group_List>, IVaccineAptRepository
    {

        public Out_Vaccine_Group_List GetVaccGroupList(string phone)
        {
            var sql = @" SELECT
	                                a.* 
                                FROM
	                                out_vaccine_group_list a
	                                INNER JOIN out_vaccine_appt_group b ON a.GroupId = b.ID 
                                WHERE
	                                a.PHONE = @PHONE 
	                                AND a.`STATUS` = 1 AND b.`Status` = 2 ";
            return Query<Out_Vaccine_Group_List>(sql, new { PHONE = phone })?.FirstOrDefault();
        }
        public VaccineApptGroupInfo GetOutVaccineApptGroup(int groupId)
        {
            var sql = @"select g.*,v.VACCINE_CN_NAME as VaccineNameCN,v.VACCINE_EN_NAME as VaccineNameEN from Out_Vaccine_Appt_Group g inner join dic_vaccine v on g.VaccineId = v.id where g.Id = @Id and g.`STATUS` = 2";
            return Query<VaccineApptGroupInfo>(sql, new { Id = groupId })?.FirstOrDefault();
        }

        public int CutVaccGroupListCount(string phone)
        {
            var selectSql = new StringBuilder();
            selectSql.Append(@" UPDATE out_vaccine_group_list
                                    SET AptCount= AptCount+1
                                    WHERE phone = @phone");
            var parms = new Dictionary<string, object>();
            parms.Add("@phone", phone);
            var result = Execute(selectSql.ToString(), parms);
            return result;
        }

    }
}
