using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Cmdjy.Dal.Tables;
using Cmdjy.Dal.Queryes;
using Cmdjy.Dal;

namespace Cmdjy.Models
{
    public static class HisInfo
    {
        public static IEnumerable<HisPrescriptionInfo> GetPreInfos(PreFilter filter) {
            using(var db = DbContextFactory.CreateDbContext()) {
                var qu = db.PrescriptionInfos.AsQueryable();
                if(filter == null) return qu.ToList();
                if(!string.IsNullOrEmpty(filter.Start) && int.TryParse(filter.Start,out var s)) {
                    qu = qu.Where(m => m.Id >= s);
                }
                if(!string.IsNullOrEmpty(filter.End) && int.TryParse(filter.End,out var e)) {
                    qu = qu.Where(m => m.Id < e);
                }
                return qu.ToList();
            }
        }

        public static IEnumerable<HisDrugInfo> GetDrugInfos(int preId) {
            using(var db = DbContextFactory.CreateDbContext()) {
                var qu = db.DrugInfos.Where(m => m.PrescriptionId == preId);
                return qu.ToList();
            }
        }
    }

    public class PreFilter
    {
        public string Start { get; set; }
        public string End { get; set; }
    }
}