using System;
using System.Data.Entity;
using System.Web.Services;
using Cmdjy.Dal;
using HisInterfaceModels;
using Newtonsoft.Json;
using CommonHelper;

namespace Cmdjy.ws
{
    /// <summary>
    /// HisInterface 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://djy.wondersgroup.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class HisInterface:WebService, IHisPostInfo
    {
        [WebMethod(Description = "医疗机构上传处方信息")]
        public string PostPrescription(string msg) {
            HisPrescriptionResult result = null;
            var info = JsonConvert.DeserializeObject<HisPrescriptionInfo>(msg);
            if(info == null) {
                result = new HisPrescriptionResult {
                    Code = ResultCode.Exception,PrescriptionId = "0",
                    Msg = "传送数据解析错误",
                };
            }
            using(var db = DbContextFactory.CreateDbContext()) {
                var en = new Dal.Tables.HisPrescriptionInfo();
                en.CopyFrom(info);
                en.RawAddress = this.Context.Request.UserHostAddress;
                en.CreateDatetime = DateTime.Now;
                db.Entry(en).State = EntityState.Added;
                try {
                    db.SaveChanges();
                    result = new HisPrescriptionResult {
                        Code = ResultCode.Success,PrescriptionId = en.Id.ToString(),
                        Msg = "保存成功",
                    };
                }
                catch(Exception ex) {
                    result = new HisPrescriptionResult {
                        Code = ResultCode.Exception,PrescriptionId = "0",
                        Msg = ex.ToString(),
                    };
                }
            }
            return JsonConvert.SerializeObject(result);
        }

        [WebMethod(Description = "医疗机构上传药品信息")]
        public string PostDrug(string msg) {
            HisDrugResult result = null;
            var info = JsonConvert.DeserializeObject<HisDrugInfo>(msg);
            if(info == null) {
                result = new HisDrugResult {
                    Code = ResultCode.Exception,DrugId = "0",
                    Msg = "传送数据解析错误",
                };
            }

            using(var db = DbContextFactory.CreateDbContext()) {
                var en = new Dal.Tables.HisDrugInfo().CopyFrom(info);
                en.RawAddress = this.Context.Request.UserHostAddress;
                en.CreateDatetime = DateTime.Now;
                db.Entry(en).State = EntityState.Added;
                try {
                    db.SaveChanges();
                    result = new HisDrugResult {
                        Code = ResultCode.Success,DrugId = en.Id.ToString(),
                        Msg = "保存成功",
                    };
                }
                catch(Exception ex) {
                    result = new HisDrugResult {
                        Code = ResultCode.Exception,DrugId = "0",
                        Msg = ex.ToString(),
                    };
                }
            }
            return JsonConvert.SerializeObject(result);
        }
    }
}
