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

namespace ASHermed.SinoInterface.Api.MySqlRepository
{
    public class DataRepository : BaseRepositoryToASHermed_SinoInterface<Pub_Message_Qimo>, IDataRepository
    {


        public List<SurveyModel> GetSurveySendMsgCount(string startDate, string endDate)
        {
            var selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            selectSql.Append(@"select `NAME`,SUM(dayamount) as amount from (
select  cast(APT_TIME as date) ,h.CN_ABBREVIATED_ADDR as `NAME`,count(DISTINCT a.PAT_ID) as dayamount
from out_apt_info a left join dic_hos h ON a.HOS_ID=h.ID  where APT_TIME >= @startDate and APT_TIME < @endDate and (INNER_STATUS & 8 > 0 or INNER_STATUS & 16 > 0 or INNER_STATUS & 32 > 0) and  CANCEL_REMARK is null group by cast(APT_TIME as date),HOS_ID) t GROUP BY t.`NAME`");
            parms.Add("@startDate", startDate);
            parms.Add("@endDate", endDate);
            var result = Query<SurveyModel>(selectSql.ToString(), parms);
            return result.ToList();
        }

        public List<Pub_Sms_Survey> GetSurveyReplyMsgCount(string startDate, string endDate)
        {
            var selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            selectSql.Append(@"select * from pub_sms_survey where APT_TIME >= @startDate and APT_TIME < @endDate ");
            parms.Add("@startDate", startDate);
            parms.Add("@endDate", endDate);
            var result = Query<Pub_Sms_Survey>(selectSql.ToString(), parms);
            return result.ToList();
        }

        public List<DataApts> GetAptListFromMonth(string startDate, string endDate)
        {
            var selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            selectSql.Append(@"select DEPT_ID,`STATUS`,INNER_STATUS,CREATE_TIME from out_apt_info where CREATE_TIME > @startDate and CREATE_TIME < @endDate and SOURCE_TYPE != 2 and SOURCE_TYPE != 256 ");
            parms.Add("@startDate", startDate);
            parms.Add("@endDate", endDate);
            var result = Query<DataApts>(selectSql.ToString(), parms);
            return result.ToList();
        }

        public List<DataPatAptMore> GetDeptAptMore(string startDate, string endDate)
        {
            var selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            selectSql.Append(@"select t.CN_NAME as `NAME`,count(1) as PatCount from  (select d.CN_NAME,a.DEPT_ID,a.PAT_ID,COUNT(1) as times from out_apt_info a LEFT JOIN dic_dept d on a.DEPT_ID = d.ID where a.CREATE_TIME > @startDate and a.CREATE_TIME < @endDate and SOURCE_TYPE !=2 and SOURCE_TYPE != 256 GROUP BY a.DEPT_ID,a.PAT_ID) t where t.times > 1 GROUP BY t.CN_NAME ");
            parms.Add("@startDate", startDate);
            parms.Add("@endDate", endDate);
            var result = Query<DataPatAptMore>(selectSql.ToString(), parms);
            return result.ToList();
        }

    }
}
