using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CommonClass.Factory;
using CmdjyHisFront.Dal;
using HisInterfaceModels;
using CommonHelper;
using Newtonsoft.Json;
using CmdjyHisFront.Dal.Tables;

namespace CmdjyHisFront.Bll
{
    public interface IPostHelper
    {
        void PostPrescription();
        void PostPrescriptionById(int id);
        void PostDrug();
        void PostDrugByDrugId(int id);
        void PostDrugByPrescriptionId(int id);
    }

    public class PostHelper:IPostHelper, IRegisterBaseInterface
    {
        public Lazy<HisFrontDbContext> Db { get; set; }

        public void PostDrug() {
            var db = this.Db.Value;
            var qu = db.DrugInfo.Join(db.DrugInfoLogs,a => a.Id,b => b.HisDrugInfoId,(a,b) => new { info = a,log = b,})
                .Where();
        }

        public void PostPrescription() {
            throw new NotImplementedException();
        }

        public void PostPrescriptionById(int id) {
            var qu = this.Db.Value.PrescriptionInfo.Where(m => m.Id == id);
            if(!qu.Any()) {
                throw new Exception($"处方没有找到，编号{id}。");
            }
            var data = new HisPrescriptionInfo();
            data.CopyFrom(qu.First());
            var result = prescription(data);
            var model = new DjyPrescriptionInfoLog();
            if(int.TryParse(result.PrescriptionId,out var ipid)) {
                model.PrescriptionId = ipid;
            }
            model.HisPrescriptionInfoId = id;
            model.PostDatatime = DateTime.Now;
            model.ResultCode = (int)result.Code;
            model.ResultMsg = result.Msg;
            this.Db.Value.Entry(model).State = System.Data.Entity.EntityState.Added;
            this.Db.Value.SaveChanges();
        }

        public void PostDrugByDrugId(int id) {
            var db = this.Db.Value;
            var qu = db.DrugInfo.Where(m => m.Id == id);
            if(!qu.Any()) {
                throw new Exception($"药品没有找到，编号{id}");
            }
            var data = new HisDrugInfo();
            data.CopyFrom(qu.First());
            var result = drug(data);
            var model = new DjyDrugInfoLog();
            if(int.TryParse(result.DrugId,out var idid)) {
                model.DrugId = idid;
            }
            model.HisDrugInfoId = id;
            model.PostDatatime = DateTime.Now;
            model.ResultCode = (int)result.Code;
            model.ResultMsg = result.Msg;
            db.Entry(model).State = System.Data.Entity.EntityState.Added;
            db.SaveChanges();
        }

        public void PostDrugByPrescriptionId(int id) {
            var db = this.Db.Value;
            var qu = db.DrugInfo.Where(m => m.PrescriptionId == id);
            foreach(var item in qu) {
                var data = new HisDrugInfo();
                data.CopyFrom(item);
                var result = drug(data);
                var model = new DjyDrugInfoLog();
                if(int.TryParse(result.DrugId,out var idid)) {
                    model.DrugId = idid;
                }
                model.HisDrugInfoId = id;
                model.PostDatatime = DateTime.Now;
                model.ResultCode = (int)result.Code;
                model.ResultMsg = result.Msg;
                db.Entry(model).State = System.Data.Entity.EntityState.Added;
                db.SaveChanges();
            }
        }

        private HisPrescriptionResult prescription(HisPrescriptionInfo info) {
            using(var server = new HisServer.HisInterfaceSoapClient()) {
                var msg = JsonConvert.SerializeObject(info);
                return JsonConvert.DeserializeObject<HisPrescriptionResult>(msg);
            }
        }

        private HisDrugResult drug(HisDrugInfo info) {
            using(var server = new HisServer.HisInterfaceSoapClient()) {
                var msg = JsonConvert.SerializeObject(info);
                return JsonConvert.DeserializeObject<HisDrugResult>(msg);
            }
        }
    }
}