using Microsoft.VisualStudio.TestTools.UnitTesting;
using Cmdjy.ws;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CmdjyTests.HisServices;
using Newtonsoft.Json;
using Cmdjy.Dal;
using System.Reflection;
using HisInterfaceModels;

namespace Cmdjy.ws.Tests
{
    [TestClass()]
    public class HisInterfaceTests
    {
        [TestMethod()]
        public void PostPrescriptionInfoTest() {
            var service = new HisInterfaceSoapClient();
            var newpres = new HisPrescriptionInfo {
                Area = "Area",Bed = "Bed",CardNo = "CardNo",CompanyCode = "CompanyCode",
                CompanyName = "CompanyName",DepName = "DepName",Diagnosis = "Diagnosis",District = "District",
                DoctorName = "DoctorName",DrugCount = "DrugCount",HosName = "HosName",HosNo = "HosNo",InvoiceNo = "InvoiceNo",
                OpDatatime = "OpDatatime",OpType = "OpType",OpUser = "OpUser",PatientAge = "PatientAge",
                PatientBrithday = "PatientBrithday",PatientMobile = "PatientMobile",PatientName = "PatientName",
                PatientNo = "PatientNo",PatientPhone = "PatientPhone",PatientSex = "PatientSex",PostAddress = "PostAddress",
                PostDatatime = "PostDatatime",PrescriptionDatatime = "PrescriptionDatatime",PrescriptionNo = "PrescriptionNo",
                RawRecordId = "RawRecordId",Remark = "Remark",Sffyxh = "Sffyxh",Sfxh = 1,TakeBack = "TakeBack",
                Type = PrescriptionType.Order,Usage = "Usage",UseCount = "UseCount",Zlh = "Zlh",
            };
            var msg = JsonConvert.SerializeObject(newpres);
            var result = service.PostPrescription(msg);
            Console.WriteLine($"result:{result}");
            var rObj = JsonConvert.DeserializeObject<HisPrescriptionResult>(result);
            Assert.IsTrue(rObj.Code == ResultCode.Success);
            using(var db = new DjyDbContext()) {
                var qu = db.PrescriptionInfos.Where(m => m.Id.ToString() == rObj.PrescriptionId).ToList();
                Assert.IsTrue(qu.Any());
                Assert.IsTrue(qu.Count == 1);
                var fst = qu.First();
                foreach(PropertyInfo p in newpres.GetType().GetProperties()) {
                    var dbp = fst.GetType().GetProperties().Where(m => m.Name == p.Name);
                    Assert.IsTrue(dbp.Any(),$"不包含属性{p.Name}");
                    var lv = p.GetValue(newpres);
                    var dbv = fst.GetType().GetProperties().First(m => m.Name == p.Name).GetValue(fst);
                    var bj =
                        p.Name == "Type" ? dbv.ToString() == lv.ToString() :
                        dbv.Equals(lv);
                    Assert.IsTrue(bj,$"属性比较失败：{p.Name}:{dbv}:{lv}");
                }
                foreach(var item in qu) {
                    db.Entry(item).State = System.Data.Entity.EntityState.Deleted;
                    db.SaveChanges();
                }
            }
        }
    }
}