using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RMYY_CallCenter_Api.Bll;
using RMYY_CallCenter_Api.DB;
using RMYY_CallCenter_Api.Model;
using RMYY_CallCenter_Api.Utility;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using System.Xml.Linq;

namespace RMYY_CallCenter_Api.Controllers
{
    [AllowAnonymous ]
    public class HospitalUserController : BaseController
    {
        private string ip = ConfigHelper.GetValue ("HospitalUserip");
        private string port = ConfigHelper.GetValue("HospitalUserport");
        private string master = ConfigHelper.GetValue("HospitalUsermaster");
        private string HouseSection = ConfigHelper.GetValue("HouseSection");
        private string DiagnoseHouse = ConfigHelper.GetValue("DiagnoseHouse");

        #region 术语查询接口
        public class Term
        {
            public int iDomainID { set; get; }//术语ID
            public string cDomainCode { set; get; }//术语编码
            public string cDomainName { set; get; }//术语名称
            public string cDomainDescription { set; get; }//描述
            public int  iState { set; get; }//描述1 有效 0 无效
        }
        public ActionResult TermQuery()
        {
            var responseString = HttpMethods.HttpPost($"http://{ip}:{port}/mdm/domain",null );
            LogHelper.Warn("TermQuery-responseString:" + responseString);
            List <Term> jo = JsonConvert.DeserializeObject<List<Term>>(responseString);
            LogHelper.Warn("TermQuery-jo:" + jo.ToJson ());
            return Success("获取成功", jo);
        }
        #endregion
        #region 术语值域查询接口
        public class Termrange
        {
            public int id { set; get; }//术语值域主键
            public string cDomainCode { set; get; }//术语代码
            public string cCode { set; get; }//值域
            public string cName { set; get; }//值描述
            public string cDomainType { set; get; }//术语类别
            public string cDescription { set; get; }//描述
            public string iState { set; get; }//1 有效 0 无效
        }
        public ActionResult TermrangeQuery()
        {
            var responseString = HttpMethods.HttpPost($"http://{ip}:{port}/mdm/domain", null);
            LogHelper.Warn("TermrangeQuery-responseString:" + responseString);
            List<Termrange> jo = JsonConvert.DeserializeObject<List<Termrange>>(responseString);
            LogHelper.Warn("TermrangeQuery-jo:" + jo.ToJson ());
            return Success("获取成功", jo);
        }
        #endregion


        #region 获取机构
        /// <summary>
        /// 机构
        /// </summary>
        public class pubDictHospital
        {
            public string cHosCode { set; get;  }//编码主键
            public string cHosName { set; get; }//名称
            public string cHelpCode { set; get; }//拼音码
            public string cHosLisCode { set; get; }//检验条码专用机构编码
            public int  useFlag { set; get; }//启用停用
        }

        public ActionResult MechanismHospitals()
        {
           // Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
            //keyValuePairs.Add("pubDictHospital", "pubDictHospital");
            var responseString = HttpMethods.HttpGet($"http://{ip}:{port}/{master}/webApi/pubDictHospital",null );
            LogHelper.Warn("MechanismHospitals-responseString:" + responseString);
            List<pubDictHospital> jo = JsonConvert.DeserializeObject<List<pubDictHospital>>(responseString);
            LogHelper.Warn("MechanismHospitals-jo:" + jo.ToJson ());
            return Success ("");
        }
        #endregion
        #region 获取院区
        /// <summary>
        /// 院区
        /// </summary>
        public class DictHospitals
        {
            public int  noHospital { set; get; }//主键id
            public string HosSectionCode { set; get; }//院区代码
            public string HosSectionName { set; get; }//院区名称
            public string pycode { set; get; }//拼音码
            public string HosStandCode { set; get; }//院区标准代码
            public string cHosCode { set; get; }//医疗机构代码
            public string firsttitle { set; get; }//医疗机构名称
            public string pycParentHosCodeode { set; get; }//上级机构代码
            public string hosTypeCode { set; get; }//机构类型
            public int useFlag { set; get; }//启用停用
        }
        //public IEnumerable <DictHospitals> YardHospitals(string cHosCode="41604745")
        //{
        //    Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
        //    keyValuePairs.Add("DictHospitals", "DictHospitals");
        //    var responseString = HttpMethods.HttpPost($"http://{ip}:{port}/{master}/webApi/DictHospitals", keyValuePairs.ToJson());
        //    LogHelper.Warn("YardHospitals-responseString:" + responseString);
        //    List<DictHospitals> jo = JsonConvert.DeserializeObject<List<DictHospitals>>(responseString);
        //    LogHelper.Warn("YardHospitals-jo:" + jo.ToJson ());
        //    var Hospitals = jo.Where  (x => x.cHosCode == cHosCode && x.useFlag == 1);
        //    var list = new T_Wowo_repairyqbll().GetListall();
        //    if (Hospitals!=null && Hospitals.Count ()>0)
        //    {
        //        foreach (var it in Hospitals)
        //        {
        //            if (list != null && list.Count > 0)
        //            {
        //                if (it.useFlag !=1)
        //                    continue;
        //                if (list.Where(x => x.T_Woname == it.HosSectionName).Count() > 0)
        //                    continue;
        //                T_Wowo_repairyq model = new T_Wowo_repairyq();
        //                model.T_Woname = it.HosSectionName;
        //                model.T_Hospital = it.noHospital;
        //                int n = new T_Wowo_repairyqbll().Addrepairitems(model);
        //                if (n >0)
        //                {
        //                    Department(n, model.T_Woname);
        //                }
        //            }
        //        }
        //    }
        //    try
        //    {
        //        foreach (var it in Hospitals)
        //        {
        //            Department(it.noHospital, it .HosSectionName);
        //        }
        //    }
        //    catch (Exception e)
        //    {
        //        LogHelper.Error(e.Message);
        //    }
        //    return Hospitals;
        //}


        public ActionResult YardHospitals1(string cHosCode = "41604745")
        {
          //  Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
           // keyValuePairs.Add("DictHospitalSection", "DictHospitalSection");
           // var responseString = HttpMethods.HttpPost($"http://{ip}:{port}/{master}/webApi/DictHospitalSection", null );
            WebClient web = new WebClient();
            web.Encoding = Encoding.UTF8;
            var responseString = web.DownloadString($"http://{ip}:{port}/{master}/webApi/DictHospitalSection");
            LogHelper.Warn("YardHospitals-responseString:" + responseString);
            List<DictHospitals> jo = JsonConvert.DeserializeObject<List<DictHospitals>>(responseString);
            LogHelper.Warn("YardHospitals-jo:" + jo.ToJson());
            var Hospitals = jo.Where(x => x.cHosCode == cHosCode && x.useFlag == 1);
            var list = new T_Wowo_repairyqbll().GetListall();
            if (Hospitals != null && Hospitals.Count() > 0)
            {
                foreach (var it in Hospitals)
                {
                    if (list != null && list.Count > 0)
                    {
                        if (it.useFlag != 1)
                            continue;
                        if (list.Where(x => x.T_Woname == it.HosSectionName).Count() > 0)
                        {
                            var model = list.Where(x => x.T_Woname == it.HosSectionName).FirstOrDefault();
                            model.T_Hospital = it.noHospital;
                            bool n = new T_Wowo_repairyqbll().Updaterepair (model);
                            continue;
                        }
                        else
                        {
                            T_Wowo_repairyq model = new T_Wowo_repairyq();
                            model.T_Woname = it.HosSectionName;
                            model.T_Hospital = it.noHospital;
                            int n = new T_Wowo_repairyqbll().Addrepairitems(model);
                        }
                           
                      
                    }
                }
            }
            return Success ("", Hospitals);
        }
        #endregion

        #region 获取药品
        /// <summary>
        /// 药品
        /// </summary>
        public class DictDrug
        {
            public int noDictdrug { set; get; }//主键id
            public string cDgDispCode { set; get; }//药品编码
            public string cDgName { set; get; }//通用名
            public string cDgNameCode { set; get; }//拼音码
            public string cDgGeneralName { set; get; }//商品名
            public string cDgSpecification { set; get; }//规格
            public decimal iDgMaxstock { set; get; }//最高库存
            public decimal iDgMinstock { set; get; }//最低库存
            public string cDgDescription { set; get; }//药品说明
            public decimal iDgStdPrice { set; get; }//国家限价
            public decimal iDgLocalStdPrice { set; get; }//地区限价

        }

        public class DictDrugact
        {
            public int NO_DrugAct { set; get; }//药品明细id
            public int NO_DictDrug { set; get; }//药品id
            public string cManufacturer { set; get; }//生产厂家
            public decimal iRetail { set; get; }//单价
          

        }
        public ActionResult  DictDrugs()
        {
            //  Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
            //  keyValuePairs.Add("DictDrug", "DictDrug");
            // var responseString = HttpMethods.HttpPost($"http://{ip}:{port}/{master}/webApi/DictDrug", keyValuePairs.ToJson());

            WebClient web = new WebClient();
            web.Encoding = Encoding.UTF8;
            var responseString = web.DownloadString($"http://{ip}:{port}/{master}/webApi/DictDrug");
            List<DictDrug> jo = JsonConvert.DeserializeObject<List<DictDrug>>(responseString);


            var DictDrugactString = web.DownloadString($"http://{ip}:{port}/{master}/webApi/DictDrugact");
            List<DictDrugact> DrugactString = JsonConvert.DeserializeObject<List<DictDrugact>>(DictDrugactString);
            if (jo != null && jo.Count() > 0)
            {
                foreach (var it in jo)
                {
                    Model.T_Sys_DictDrug dictDrug = new T_Sys_DictDrug();
                    dictDrug.F_DictDrugid  = it.noDictdrug;
                    dictDrug.cDgDispCode = it.cDgDispCode;
                    dictDrug.cDgName = it.cDgName;
                    dictDrug.cDgNameCode = it.cDgNameCode;
                    dictDrug.cDgGeneralName = it.cDgGeneralName;
                    dictDrug.cDgSpecification = it.cDgSpecification;
                    dictDrug.iDgMaxstock = it.iDgMaxstock;
                    dictDrug.iDgMinstock = it.iDgMinstock;
                    dictDrug.cDgDescription = it.cDgDescription;
                    dictDrug.iDgStdPrice = it.iDgStdPrice;
                    dictDrug.iDgLocalStdPrice = it.iDgLocalStdPrice;
                    dictDrug.F_IsDelete = 0;
                    dictDrug.F_CreateTime = DateTime.Now;
                    if(DrugactString.Where (x=>x.NO_DictDrug ==it .noDictdrug).Count() >0)
                    {
                        dictDrug.cManufacturer = DrugactString.Where(x => x.NO_DictDrug == it.noDictdrug).FirstOrDefault().cManufacturer;
                        dictDrug.iRetail = DrugactString.Where(x => x.NO_DictDrug == it.noDictdrug).FirstOrDefault().iRetail;
                    }
                    new BLL.T_Sys_DictDrug().Add(dictDrug);
                }
            }

            return Success ("", responseString);
        }

        #endregion
        #region 获取科室
        /// <summary>
        /// 科室
        /// </summary>
        public class DictDept
        {
            public int noDept { set; get; }//主键id
            public int nohospital { set; get; }//院区
            public string deptName { set; get; }//科室名称
            public string deptAlias { set; get; }//科室简介
            public string DeptCode { set; get; }//科室编码
            public bool  useFlag { set; get; }//启用停用1启用
            public string deptParentCode { set; get; }//上级急诊科室
        }


        public ActionResult  Department()
        {
          //  Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
           // keyValuePairs.Add("DictDept", "DictDept");
          //  var responseString = HttpMethods.HttpPost($"http://{ip}:{port}/{master}/webApi/DictDept", keyValuePairs.ToJson());

            WebClient web = new WebClient();
            web.Encoding = Encoding.UTF8;
            var responseString = web.DownloadString($"http://{ip}:{port}/{master}/webApi/DictDept");
            LogHelper.Warn("Department-responseString:" + responseString);
            List<DictDept> Dept = JsonConvert.DeserializeObject<List<DictDept>>(responseString);
            LogHelper.Warn("Department-jo:" + Dept.ToJson ());
          
            var list = new Bll.T_Sys_Department().GetModelList("F_State=1");
            var Wowo = new T_Wowo_repairyqbll().GetListall();
                if (Dept != null && Dept.Count() > 0)
                {

              //  var dept1 = Dept.Where(x => x.deptParentCode == "");
                if (Dept != null && Dept.Count ()>0)
                {
                    foreach (var it in Dept)
                    {
                        var model = Wowo.Where(x => x.T_Hospital == it.nohospital);
                        Model.T_Sys_Department dModel = new Model.T_Sys_Department();
                        if (list.Where(x => x.F_DeptCode == it.DeptCode).Count() > 0)
                        {
                            dModel = list.Where(x => x.F_DeptCode == it.DeptCode).First();
                            if (dModel.F_DeptName != it.deptName)
                            {
                                dModel.F_DeptName = it.deptName;
                                if (model != null && model.Count() > 0)
                                    dModel.T_Woid = model.First().T_Woid;
                                bool n = new Bll.T_Sys_Department().Update(dModel);
                                //SetDept(it.DeptCode, Dept, Wowo, dModel.F_DeptId);
                            }
                        }
                        else
                        {
                            if (!string.IsNullOrEmpty(it.deptParentCode))
                            {
                                var deptParent = list.Where(x => x.F_DeptCode == it.deptParentCode);
                                if (deptParent != null && deptParent.Count() > 0)
                                    dModel.F_ParentId = deptParent.FirstOrDefault().F_DeptId;
                            }
                            dModel.F_Sort = 0;
                            dModel.F_DeptCode = it.DeptCode;
                            dModel.F_DeptName = it.deptName;
                            if (it.useFlag)
                                dModel.F_State = 1;
                            else
                                dModel.F_State = 0;
                            dModel.F_CreateTime = DateTime.Now;
                            dModel.F_CreateUser = "系统";
                            if (model != null && model.Count() > 0)
                                dModel.T_Woid = model.First().T_Woid;
                            dModel.F_Location = "";
                            dModel.F_FloorDistribution = "";
                            dModel.F_OfficeTelephone = "";
                            int n = new Bll.T_Sys_Department().Add(dModel);
                            dModel.F_DeptId = n;
                           // SetDept(it.DeptCode, Dept, Wowo, dModel.F_DeptId);
                        }
                      
                    }
                }
            }
           
            return Success ("",Dept ) ;
        }

        /// <summary>
        /// 对应关系
        /// </summary>
        public class SSDiagnoseHouseVSDept
        {
            public int No_dept { set; get; }//部门id
            public int iHouseID { set; get; }//诊室id
        
        }
        /// <summary>
        /// 诊室
        /// </summary>
        public class SSDiagnoseHouse
        {
            public int iHouseID { set; get; }//诊室id
            public string   cLocation { set; get; }//诊室位置
            public string cName { set; get; }//诊室名称
        }


        public ActionResult DepartmentPosition()
        {
            WebClient web = new WebClient();
            web.Encoding = Encoding.UTF8;
            var responseString = web.DownloadString($"http://172.16.0.197:8086/winmdm/webApi/DictDept");
            List<DictDept> Dept = JsonConvert.DeserializeObject<List<DictDept>>(responseString);


            var SSDiagnoseHouseVSDeptg = web.DownloadString($"http://172.16.0.197:8086/winmdm/webApi/SSDiagnoseHouseVSDept");
            List<SSDiagnoseHouseVSDept> HouseVSDept = JsonConvert.DeserializeObject<List<SSDiagnoseHouseVSDept>>(SSDiagnoseHouseVSDeptg);

            var DiagnoseHouse = web.DownloadString($"http://172.16.0.197:8086/winmdm/webApi/SSDiagnoseHouse");
            List<SSDiagnoseHouse> House = JsonConvert.DeserializeObject<List<SSDiagnoseHouse>>(DiagnoseHouse);




            var list = new Bll.T_Sys_Department().GetModelList(" F_CreateUser ='系统'");
          
            if (list != null && list.Count() > 0)
            {
                if (list != null && list.Count() > 0)
                {
                    foreach (var it in list)
                    {
                        var noDept = Dept.Where(x => x.DeptCode == it.F_DeptCode);
                       if (noDept!=null&& noDept.Count ()>0)
                        {
                            int noDeptid = noDept.FirstOrDefault().noDept;
                            var iHouseVSDept= HouseVSDept.Where(x => x.No_dept  == noDeptid);
                            if (iHouseVSDept != null && iHouseVSDept.Count() > 0)
                            {
                                int iHouseID = iHouseVSDept.FirstOrDefault ().iHouseID ;
                                var DiagnoseHouses = House.Where(x => x.iHouseID == iHouseID);
                                if (DiagnoseHouses!=null && DiagnoseHouses.Count() >0)
                                {
                                    it.F_Location = DiagnoseHouses.FirstOrDefault().cLocation;
                                    new Bll.T_Sys_Department().Update(it);
                                }

                            }
                        }

                    }
                }
            }

            return Success("", Dept);
        }




        public class SSHouseSection
        {
            public int   iHouseSectionId { set; get; }
            public string cName { set; get; }
        }
       


        public ActionResult DepartmentLocation()
        {
            WebClient web = new WebClient();
            web.Encoding = Encoding.UTF8;
            var responseString = web.DownloadString($"http://{ip}:{port}/{master}/webApi/DictDept");
            LogHelper.Warn("Department-responseString:" + responseString);
            List<DictDept> Dept = JsonConvert.DeserializeObject<List<DictDept>>(responseString);
            LogHelper.Warn("Department-jo:" + Dept.ToJson());

            var list = new Bll.T_Sys_Department().GetModelList("F_State=1");
            var Wowo = new T_Wowo_repairyqbll().GetListall();
            if (Dept != null && Dept.Count() > 0)
            {

                //  var dept1 = Dept.Where(x => x.deptParentCode == "");
                if (Dept != null && Dept.Count() > 0)
                {
                    foreach (var it in Dept)
                    {
                        var model = Wowo.Where(x => x.T_Hospital == it.nohospital);
                        Model.T_Sys_Department dModel = new Model.T_Sys_Department();
                        if (list.Where(x => x.F_DeptCode == it.DeptCode).Count() > 0)
                        {
                            dModel = list.Where(x => x.F_DeptCode == it.DeptCode).First();
                            if (dModel.F_DeptName != it.deptName)
                            {
                                dModel.F_DeptName = it.deptName;
                                if (model != null && model.Count() > 0)
                                    dModel.T_Woid = model.First().T_Woid;
                                bool n = new Bll.T_Sys_Department().Update(dModel);
                                //SetDept(it.DeptCode, Dept, Wowo, dModel.F_DeptId);
                            }
                        }
                        else
                        {
                            if (!string.IsNullOrEmpty(it.deptParentCode))
                            {
                                var deptParent = list.Where(x => x.F_DeptCode == it.deptParentCode);
                                if (deptParent != null && deptParent.Count() > 0)
                                    dModel.F_ParentId = deptParent.FirstOrDefault().F_DeptId;
                            }
                            dModel.F_Sort = 0;
                            dModel.F_DeptCode = it.DeptCode;
                            dModel.F_DeptName = it.deptName;
                            if (it.useFlag)
                                dModel.F_State = 1;
                            else
                                dModel.F_State = 0;
                            dModel.F_CreateTime = DateTime.Now;
                            dModel.F_CreateUser = "系统";
                            if (model != null && model.Count() > 0)
                                dModel.T_Woid = model.First().T_Woid;
                            dModel.F_Location = "";
                            dModel.F_FloorDistribution = "";
                            dModel.F_OfficeTelephone = "";
                            int n = new Bll.T_Sys_Department().Add(dModel);
                            dModel.F_DeptId = n;
                            // SetDept(it.DeptCode, Dept, Wowo, dModel.F_DeptId);
                        }

                    }
                }
            }

            return Success("", Dept);
        }
        public void SetDept(string deptParentCode, List<DictDept> Dept,List <T_Wowo_repairyq> Wowo,int deptid)
        {
            var list = new Bll.T_Sys_Department().GetModelList("F_State=1");
            var dept1 = Dept.Where(x => x.deptParentCode == deptParentCode);
            if (dept1 != null && dept1.Count() > 0)
            {
                foreach (var it in dept1)
                {
                    var model = Wowo.Where(x => x.T_Hospital == it.nohospital);
                    Model.T_Sys_Department dModel = new Model.T_Sys_Department();
                    if (list.Where(x => x.F_DeptCode == it.DeptCode).Count() > 0)
                    {
                        dModel = list.Where(x => x.F_DeptCode == it.DeptCode).First();
                        if (dModel.F_DeptName != it.deptName)
                        {
                            dModel.F_ParentId = deptid;
                            dModel.F_DeptName = it.deptName;
                            if (model != null && model.Count() > 0)
                                dModel.T_Woid = model.First().T_Woid;
                            bool n = new Bll.T_Sys_Department().Update(dModel);
                            SetDept(it.DeptCode, Dept, Wowo, dModel.F_DeptId);
                        }
                    }
                    else
                    {
                        dModel.F_ParentId = deptid;
                        dModel.F_Sort = 0;
                        dModel.F_DeptCode = it.DeptCode;
                        dModel.F_DeptName = it.deptName;
                        if (it.useFlag)
                            dModel.F_State = 1;
                        else
                            dModel.F_State = 0;
                        dModel.F_CreateTime = DateTime.Now;
                        dModel.F_CreateUser = "系统";
                        if (model != null && model.Count() > 0)
                            dModel.T_Woid = model.First().T_Woid;
                        dModel.F_Location = "";
                        dModel.F_FloorDistribution = "";
                        dModel.F_OfficeTelephone = "";
                        int n = new Bll.T_Sys_Department().Add(dModel);
                        dModel.F_DeptId = n;
                        SetDept(it.DeptCode, Dept, Wowo, dModel.F_DeptId);
                    }

                }
            }
        }

   
        #endregion
        #region 获取人员
        /// <summary>
        /// 性别
        /// </summary>
        public class DictSex
        {
            public int No_Sex { set; get; }//性别主键
            public string SexCode { set; get; }//性别编码
            public string SexName { set; get; }//性别名称
        }
        /// <summary>
        /// 职务
        /// </summary>
        public class DictZhiwu
        {
            public int No_Zhi { set; get; }//性别主键
            public string ZhiCode { set; get; }//性别编码
            public string ZhiName { set; get; }//性别名称
        }
        /// <summary>
        /// 职称
        /// </summary>
        public class DictTitle
        {
            public int No_Title { set; get; }//主键
            public string TitleName { set; get; }//名称
            public string TitleCode { set; get; }//编码
        }
     

        /// <summary>
        /// 人员
        /// </summary>
        public class DictStaff
        {
            public int noStaff { set; get; }//主键id
            public int noSex { set; get; }//性别
            public string StaffName { set; get; }//人员姓名
            public int?  noDept { set; get; }//所属科室
            public string dhhm { set; get; }//联系电话
            public string StaffCode { set; get; }//人员编码
            public int? zw { set; get; }//职务
            public int? noTitle { set; get; }//职称
            public bool?  bStopFlag { set; get; }//停用标识
        }
        public int Getcode()
        {
            int UserCode = 0;
            string strSql = "select Max(F_UserCode) F_UserCode from T_Sys_UserAccount where (F_See!='5' or  F_See is null) ";
            object ob = DbHelperSQL.GetSingle(strSql.ToString());
            if (ob == null)
            {
                UserCode = 0;
            }
            else
            {
                UserCode = Convert.ToInt32(ob);
            }
            return UserCode + 1;
        }
        //public IEnumerable<DictStaff> Staff(int noDept,int deptid)
        //{
        //    Dictionary<string, string> keyValuePair = new Dictionary<string, string>();
        //    keyValuePair.Add("DictSex", "DictSex");
        //    var responseStrings = HttpMethods.HttpPost($"http://{ip}:{port}/{master}/webApi/DictSex", keyValuePair.ToJson());
        //    LogHelper.Warn("Staff-responseStrings:" + responseStrings);
        //    List<DictSex> sex = JsonConvert.DeserializeObject<List<DictSex>>(responseStrings);
        //    LogHelper.Warn("Staff-sex:" + sex.ToJson ());
        //    Dictionary<string, string> keyValuePairszhiwu = new Dictionary<string, string>();
        //    keyValuePairszhiwu.Add("DictZhiwu", "DictZhiwu");
        //    var respon = HttpMethods.HttpPost($"http://{ip}:{port}/{master}/webApi/DictZhiwu", keyValuePairszhiwu.ToJson());
        //    LogHelper.Warn("Staff-respon:" + respon);
        //    List<DictZhiwu> zhuwu = JsonConvert.DeserializeObject<List<DictZhiwu>>(respon);
        //    LogHelper.Warn("Staff-zhuwu:" + zhuwu.ToJson ());
        //    Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
        //    keyValuePairs.Add("DictStaff", "DictStaff");
        //    var responseString = HttpMethods.HttpPost($"http://{ip}:{port}/{master}/webApi/DictStaff", keyValuePairs.ToJson());
        //    LogHelper.Warn("Staff-responseString:" + responseString);
        //    List<DictStaff> jo = JsonConvert.DeserializeObject<List<DictStaff>>(responseString);
        //    LogHelper.Warn("Staff-jo:" + jo.ToJson ());
        //    var user  = jo.Where(x => x.noDept == noDept );
        //    var list = new Bll.T_Sys_UserAccount ().GetModelList("F_DeleteFlag=0");
        //        if (user != null && user.Count() > 0)
        //        {
        //            foreach (var it in user)
        //            {
        //                if (list != null && list.Count > 0)
        //                {
        //                    if (list.Where(x => x.F_StaffCode   == it.staffCode).Count() > 0)
        //                        continue;
        //                    Model.T_Sys_UserAccount ua = new Model.T_Sys_UserAccount();
        //                    int usercode = Getcode();
        //                    ua.F_UserCode = usercode.ToString ();
        //                    ua.F_UserName = it .staffName;
        //                    ua.F_Password = "e10adc3949ba59abbe56e057f20f883e";
        //                    ua.F_ExtensionNumber ="";
        //                    ua.F_DeptId = deptid;
        //                    ua.F_SeatFlag = 0;
        //                    ua.F_Telephone = it .dhhm;
        //                    var uasex= sex.Where(x => x.No_Sex == it.noSex);
        //                 if (uasex!=null && uasex.Count() >0)
        //                    ua.F_Sex = uasex.FirstOrDefault ().SexName ;
        //                    ua.F_CreateTime = DateTime.Now;
        //                    ua.F_EnableFlag = 1;
        //                   var uazhuwu = zhuwu.Where(x => x.No_Zhi  == it.zw );
        //                   if (uazhuwu != null && uazhuwu.Count() > 0)
        //                    ua.F_Job = uazhuwu.FirstOrDefault().ZhiName ;
        //                    ua.F_DeleteFlag = 0;
        //                    new Bll.T_Sys_UserAccount().Add(ua);
        //                }
        //            }
        //        }
        //    return user;
        //}


        public ActionResult  Staff()
        {

            WebClient web = new WebClient();
            web.Encoding = Encoding.UTF8;
            var DictDeptresponseString = web.DownloadString($"http://{ip}:{port}/{master}/webApi/DictDept");
            LogHelper.Warn("Department-responseString:" + DictDeptresponseString);
            List<DictDept> deptjo = JsonConvert.DeserializeObject<List<DictDept>>(DictDeptresponseString);
            var Department = new Bll.T_Sys_Department().GetModelList("F_State=1");


         //   Dictionary<string, string> keyValuePair = new Dictionary<string, string>();
           // keyValuePair.Add("DictSex", "DictSex");
          //  var responseStrings = HttpMethods.HttpPost($"http://{ip}:{port}/{master}/webApi/DictSex", keyValuePair.ToJson());

            //var responseStrings = web.DownloadString($"http://{ip}:{port}/{master}/webApi/DictSex");
            //LogHelper.Warn("Staff-responseStrings:" + responseStrings);
            //List<DictSex> sex = JsonConvert.DeserializeObject<List<DictSex>>(responseStrings);
            //LogHelper.Warn("Staff-sex:" + sex.ToJson());




        //    Dictionary<string, string> keyValuePairszhiwu = new Dictionary<string, string>();
         //   keyValuePairszhiwu.Add("DictZhiwu", "DictZhiwu");
         //   var respon = HttpMethods.HttpPost($"http://{ip}:{port}/{master}/webApi/DictZhiwu", keyValuePairszhiwu.ToJson());

            var respon = web.DownloadString($"http://{ip}:{port}/{master}/webApi/DictZhiwu");
            List<DictZhiwu> zhuwu = JsonConvert.DeserializeObject<List<DictZhiwu>>(respon);
           


            var zhichengrespon = web.DownloadString($"http://{ip}:{port}/{master}/webApi/DictTitle");
            List<DictTitle> zhicheng = JsonConvert.DeserializeObject<List<DictTitle>>(zhichengrespon);
           





            // Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
            //   keyValuePairs.Add("DictStaff", "DictStaff");
            //var responseString = HttpMethods.HttpPost($"http://{ip}:{port}/{master}/webApi/DictStaff", keyValuePairs.ToJson());
            var responseString = web.DownloadString($"http://{ip}:{port}/{master}/webApi/DictStaff");
            List<DictStaff> jo = JsonConvert.DeserializeObject<List<DictStaff>>(responseString);
         
          //  var user = jo.Where(x => x.noDept == noDept);
            var list = new Bll.T_Sys_UserAccount().GetModelList("F_DeleteFlag=0");
            if (jo != null && jo.Count() > 0)
            {
                foreach (var it in jo)
                {
                    if (list != null && list.Count > 0)
                    {
                        if (list.Where(x => x.F_StaffCode == it.StaffCode ).Count() > 0)
                            continue;
                        Model.T_Sys_UserAccount ua = new Model.T_Sys_UserAccount();
                        ua.F_UserCode = it.StaffCode;
                        ua.F_UserName = it.StaffName ;
                        ua.F_Password = "e10adc3949ba59abbe56e057f20f883e";
                        ua.F_ExtensionNumber = "";
                        if (it.noDept!=null )
                        {
                            if (deptjo.Where(j => j.noDept == it.noDept).Count() > 0
                            && Department.Where(x => x.F_DeptCode == deptjo.Where(j => j.noDept == it.noDept).First().DeptCode).Count() > 0)
                            {
                                ua.F_DeptId = Department.Where(x => x.F_DeptCode == deptjo.Where(j => j.noDept == it.noDept).First().DeptCode).First()
                               .F_DeptId;
                            }
                        }
                        
                        ua.F_SeatFlag = 0;
                        ua.F_Telephone = it.dhhm;
                        ua.F_StaffCode = it.StaffCode;
                        if (it.noSex == 1)
                            ua.F_Sex = "男";
                        else if (it.noSex == 2)
                            ua.F_Sex = "女";
                        ua.F_CreateTime = DateTime.Now;
                        ua.F_EnableFlag = 1;
                        if (it.zw!=null )
                        {
                            var uazhuwu = zhuwu.Where(x => x.No_Zhi == it.zw);
                            if (uazhuwu != null && uazhuwu.Count() > 0)
                                ua.F_Job = uazhuwu.FirstOrDefault().ZhiName;
                        }
                        if (it.noTitle  != null)
                        {
                            var uazhuwu = zhicheng .Where(x => x.No_Title  == it.noTitle);
                            if (uazhuwu != null && uazhuwu.Count() > 0)
                                ua.F_Title = uazhuwu.FirstOrDefault().TitleName ;
                        }
                        if (it .bStopFlag!=null &&it .bStopFlag.Value  )
                            ua.F_DeleteFlag = 1;
                        else
                            ua.F_DeleteFlag = 0;
                        new Bll.T_Sys_UserAccount().Add(ua);
                    }
                }
            }
            return Success ("", jo)  ;
        }
        #endregion
        #region 数据同步接口
        /// <summary>
        /// 数据同步接口
        /// </summary>
        /// <returns></returns>
        //[Authority]
        public ActionResult synchronization(string cHosCode)
        {
            StreamReader reader = new StreamReader(Request.InputStream);
            var postString = reader.ReadToEnd();
            JObject paras = null;
            paras = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(postString);
            LogHelper.Warn("synchronization-paras:" + paras.ToJson ());
            var DictInfo = paras["DictInfo"].ToString();
            var result = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(DictInfo);
            LogHelper.Warn("synchronization-result:" + result.ToJson());
            var DictCode = result["DictCode"].ToString();
            LogHelper.Warn("synchronization-DictCode:"+DictCode);
            var RecordChangeType = result["RecordChangeType"].ToString();
            LogHelper.Warn("synchronization-RecordChangeType:" + RecordChangeType);
            var RecordInfo= result["RecordInfo"] == null ? "" : result["RecordInfo"].ToString();
            if (DictCode== "DictHospitals")
            {
                var list = new T_Wowo_repairyqbll().GetListall();
                DictHospitals jo = JsonConvert.DeserializeObject<DictHospitals>(RecordInfo);
                if (RecordChangeType== "Add")
                {
                    if (list.Where(x => x.T_Woname == jo.HosSectionName).Count() <= 0)
                    {
                        T_Wowo_repairyq model = new T_Wowo_repairyq();
                        model.T_Woname = jo.HosSectionName;
                        model.T_Hospital = jo.noHospital;
                        int n = new T_Wowo_repairyqbll().Addrepairitems(model);
                        if (n > 0)
                        {
                            var obj = new
                            {
                                iResult= "1",
	                             cResult= "",
	                               pk= n
                            };
                            return Content(obj.ToJson());
                        }
                        else
                        {
                            var obj = new
                            {
                                iResult = "0",
                                cResult = "添加失败",
                                pk = ""
                            };
                            return Content(obj.ToJson());
                        }
                    }
                    else
                    {
                        var obj = new
                        {
                            iResult = "0",
                            cResult = "该院区已存在",
                            pk = ""
                        };
                        return Content(obj.ToJson());
                    }
                }
                else if (RecordChangeType == "Modify")
                {
                    var model = list.Where(x => x.T_Woname == jo.HosSectionName);
                    if (model!=null &&model .Count()>=0)
                    {
                        model.FirstOrDefault ().T_Woname = jo.HosSectionName;
                        model.FirstOrDefault().T_Hospital = jo.noHospital;
                        bool  n = new T_Wowo_repairyqbll().Updaterepair(model.FirstOrDefault());
                        if (n )
                        {
                            var obj = new
                            {
                                iResult = "1",
                                cResult = "",
                                pk = n
                            };
                            return Content(obj.ToJson());
                        }
                        else
                        {
                            var obj = new
                            {
                                iResult = "0",
                                cResult = "修改失败",
                                pk = ""
                            };
                            return Content(obj.ToJson());
                        }
                    }
                    else
                    {
                        var obj = new
                        {
                            iResult = "0",
                            cResult = "该院区不存在",
                            pk = ""
                        };
                        return Content(obj.ToJson());
                    }
                }
                else if (RecordChangeType == "Delete")
                {
                    var model = list.Where(x => x.T_Woname == jo.HosSectionName);
                    if (model != null && model.Count() >= 0)
                    {
                        bool n = new T_Wowo_repairyqbll().Deleterepair (model.FirstOrDefault().T_Woid );
                        if (n)
                        {
                            var obj = new
                            {
                                iResult = "1",
                                cResult = "",
                                pk = n
                            };
                            return Content(obj.ToJson());
                        }
                        else
                        {
                            var obj = new
                            {
                                iResult = "0",
                                cResult = "删除失败",
                                pk = ""
                            };
                            return Content(obj.ToJson());
                        }
                    }
                    else
                    {
                        var obj = new
                        {
                            iResult = "0",
                            cResult = "该院区不存在",
                            pk = ""
                        };
                        return Content(obj.ToJson());
                    }
                }
            }
            else if (DictCode == "DictDept")
            {

                DictDept jo = JsonConvert.DeserializeObject<DictDept>(RecordInfo);
                var list = new Bll.T_Sys_Department().GetModelList("F_State=1 ");
                var Wowo = new T_Wowo_repairyqbll().GetListall();
                var model1 = Wowo.Where(x => x.T_Hospital  == jo .nohospital );
                if (RecordChangeType == "Add")
                {
                    if (list.Where(x=>x.F_DeptCode ==jo .DeptCode).Count ()<=0 )
                    {
                        Model.T_Sys_Department dModel = new Model.T_Sys_Department();
                        if (!string.IsNullOrEmpty(jo .deptParentCode))
                        {
                            var deptParent = list.Where(x => x.F_DeptCode == jo.deptParentCode);
                            if (deptParent != null && deptParent.Count() > 0)
                                dModel.F_ParentId = deptParent.FirstOrDefault().F_DeptId;
                        }
                        dModel.F_Sort = 0;
                        dModel.F_DeptCode = jo.DeptCode;
                        dModel.F_DeptName = jo.deptName;
                        dModel.F_ParentId = 0;
                        if (jo .useFlag )
                        dModel.F_State = 1;
                        else
                            dModel.F_State = 0;
                        dModel.F_CreateTime = DateTime.Now;
                        dModel.F_CreateUser = "系统";
                        if (model1 != null && model1.Count() > 0)
                            dModel.T_Woid = model1.First().T_Woid;
                        dModel.F_Location = "";
                        dModel.F_FloorDistribution = "";
                        dModel.F_OfficeTelephone = "";
                        int n = new Bll.T_Sys_Department().Add(dModel);
                        if (n > 0)
                        {
                            var obj = new
                            {
                                iResult = "1",
                                cResult = "",
                                pk = n
                            };
                            return Content(obj.ToJson());
                        }
                        else
                        {
                            var obj = new
                            {
                                iResult = "0",
                                cResult = "添加失败",
                                pk = ""
                            };
                            return Content(obj.ToJson());
                        }
                    }
                    else
                    {
                        var obj = new
                        {
                            iResult = "0",
                            cResult = "该科室已存在",
                            pk = ""
                        };
                        return Content(obj.ToJson());
                    }
                }
                else if (RecordChangeType == "Modify")
                {
                    var model = list.Where(x => x.F_DeptCode == jo.DeptCode);
                    if (model != null && model.Count() >= 0)
                    {
                      
                        if (!string.IsNullOrEmpty(jo.deptParentCode))
                        {
                            var deptParent = list.Where(x => x.F_DeptCode == jo.deptParentCode);
                            if (deptParent != null && deptParent.Count() > 0)
                                model.FirstOrDefault ().F_ParentId = deptParent.FirstOrDefault().F_DeptId;
                        }
                        model.FirstOrDefault().F_Sort = 0;
                        model.FirstOrDefault().F_DeptCode = jo.DeptCode ;
                        model.FirstOrDefault().F_DeptName = jo.deptName;
                        if (jo.useFlag)
                            model.FirstOrDefault().F_State = 1;
                        else
                            model.FirstOrDefault().F_State = 0;
                        model.FirstOrDefault().F_CreateTime = DateTime.Now;
                        model.FirstOrDefault().F_CreateUser = "系统";
                        if (model1 != null && model1.Count() > 0)
                            model.FirstOrDefault().T_Woid = model1.First().T_Woid;
                        model.FirstOrDefault().F_Location = "";
                        model.FirstOrDefault().F_FloorDistribution = "";
                        model.FirstOrDefault().F_OfficeTelephone = "";
                        bool n = new Bll.T_Sys_Department().Update(model.FirstOrDefault());
                        if (n)
                        {
                            var obj = new
                            {
                                iResult = "1",
                                cResult = "",
                                pk = n
                            };
                            return Content(obj.ToJson());
                        }
                        else
                        {
                            var obj = new
                            {
                                iResult = "0",
                                cResult = "修改失败",
                                pk = ""
                            };
                            return Content(obj.ToJson());
                        }
                    }
                    else
                    {
                        var obj = new
                        {
                            iResult = "0",
                            cResult = "该科室不存在",
                            pk = ""
                        };
                        return Content(obj.ToJson());
                    }
                }
                else if (RecordChangeType == "Delete")
                {
                    var model = list.Where(x => x.F_DeptCode == jo.DeptCode);
                    if (model != null && model.Count() >= 0)
                    {
                        bool n = new Bll.T_Sys_Department().Delete (model.FirstOrDefault().F_DeptId );
                        if (n)
                        {
                            var obj = new
                            {
                                iResult = "1",
                                cResult = "",
                                pk = n
                            };
                            return Content(obj.ToJson());
                        }
                        else
                        {
                            var obj = new
                            {
                                iResult = "0",
                                cResult = "删除失败",
                                pk = ""
                            };
                            return Content(obj.ToJson());
                        }
                    }
                    else
                    {
                        var obj = new
                        {
                            iResult = "0",
                            cResult = "该科室不存在",
                            pk = ""
                        };
                        return Content(obj.ToJson());
                    }
                }
            }
            else if (DictCode == "DictStaff")
            {
                LogHelper.Warn("synchronization-RecordInfo:" + RecordInfo.ToJson ());
                DictStaff jo = JsonConvert.DeserializeObject<DictStaff>(RecordInfo);



                WebClient web = new WebClient();
                web.Encoding = Encoding.UTF8;
                var DictDeptresponseString = web.DownloadString($"http://{ip}:{port}/{master}/webApi/DictDept");
                LogHelper.Warn("Department-responseString:" + DictDeptresponseString);
                List<DictDept> deptjo = JsonConvert.DeserializeObject<List<DictDept>>(DictDeptresponseString);
                var Department = new Bll.T_Sys_Department().GetModelList("F_State=1");




                var respon = web.DownloadString($"http://{ip}:{port}/{master}/webApi/DictZhiwu");
                List<DictZhiwu> zhuwu = JsonConvert.DeserializeObject<List<DictZhiwu>>(respon);
              

                var zhichengrespon = web.DownloadString($"http://{ip}:{port}/{master}/webApi/DictTitle");
                List<DictTitle> zhicheng = JsonConvert.DeserializeObject<List<DictTitle>>(zhichengrespon);



                var list = new Bll.T_Sys_UserAccount().GetModelList("F_DeleteFlag=0");

                
                var dept= new Bll.T_Sys_Department().GetModelList("F_State=1 and F_DeptCode="+ deptjo.Where (x=>x.noDept  ==
                jo .noDept   ) .FirstOrDefault ().DeptCode + ")");
                if (RecordChangeType == "Add")
                {
                    if (list.Where(x => x.F_StaffCode == jo.StaffCode  ).Count() <= 0)
                    {
                        Model.T_Sys_UserAccount ua = new Model.T_Sys_UserAccount();
                        ua.F_UserCode = jo.StaffCode;
                        ua.F_UserName = jo .StaffName;
                        ua.F_StaffCode = jo.StaffCode;
                        ua.F_Password = "e10adc3949ba59abbe56e057f20f883e";
                        ua.F_ExtensionNumber = "";
                        ua.F_DeptId = dept==null ? dept .FirstOrDefault ().F_DeptId :0  ;
                        ua.F_SeatFlag = 0;
                        ua.F_Telephone = jo.dhhm;
                    

                          if (jo .noSex == 1)
                            ua.F_Sex = "男";
                        else if (jo .noSex == 2)
                            ua.F_Sex = "女";
                        ua.F_CreateTime = DateTime.Now;
                        ua.F_EnableFlag = 1;
                        if (jo .zw !=null )
                        {
                            var uazhuwu = zhuwu.Where(x => x.No_Zhi == jo.zw);
                            if (uazhuwu != null && uazhuwu.Count() > 0)
                                ua.F_Job = uazhuwu.FirstOrDefault().ZhiName;
                        }

                        if (jo .noTitle != null)
                        {
                            var uazhuwu = zhicheng.Where(x => x.No_Title == jo .noTitle);
                            if (uazhuwu != null && uazhuwu.Count() > 0)
                                ua.F_Title = uazhuwu.FirstOrDefault().TitleName;
                        }

                        if (jo.bStopFlag != null && jo.bStopFlag.Value)
                            ua.F_DeleteFlag = 1;
                        else
                            ua.F_DeleteFlag = 0;
                        int n = new Bll.T_Sys_UserAccount().Add(ua);
                        if (n > 0)
                        {
                            var obj = new
                            {
                                iResult = "1",
                                cResult = "",
                                pk = n
                            };
                            return Content(obj.ToJson());
                        }
                        else
                        {
                            var obj = new
                            {
                                iResult = "0",
                                cResult = "添加失败",
                                pk = ""
                            };
                            return Content(obj.ToJson());
                        }
                    }
                    else
                    {
                        var obj = new
                        {
                            iResult = "0",
                            cResult = "该人员已存在",
                            pk = ""
                        };
                        return Content(obj.ToJson());
                    }
                }
                else if (RecordChangeType == "Modify")
                {
                    var model = list.Where(x => x.F_StaffCode == jo.StaffCode );
                    if (model != null && model.Count() >= 0)
                    {
                        var ua = model.FirstOrDefault();
                        ua.F_UserName = jo.StaffName;
                        ua.F_StaffCode = jo.StaffCode;
                        ua.F_Password = "e10adc3949ba59abbe56e057f20f883e";
                        ua.F_ExtensionNumber = "";
                        ua.F_DeptId = dept == null ? dept.FirstOrDefault().F_DeptId : 0;
                        ua.F_SeatFlag = 0;
                        ua.F_Telephone = jo.dhhm;
                        if (jo.noSex == 1)
                            ua.F_Sex = "男";
                        else if (jo.noSex == 2)
                            ua.F_Sex = "女";
                        ua.F_CreateTime = DateTime.Now;
                        ua.F_EnableFlag = 1;
                        if (jo .zw !=null )
                        {
                            var uazhuwu = zhuwu.Where(x => x.No_Zhi == jo.zw);
                            if (uazhuwu != null && uazhuwu.Count() > 0)
                                ua.F_Job = uazhuwu.FirstOrDefault().ZhiName;
                        }
                        if (jo.noTitle != null)
                        {
                            var uazhuwu = zhicheng.Where(x => x.No_Title == jo.noTitle);
                            if (uazhuwu != null && uazhuwu.Count() > 0)
                                ua.F_Title = uazhuwu.FirstOrDefault().TitleName;
                        }
                        if (jo.bStopFlag != null && jo.bStopFlag.Value)
                            ua.F_DeleteFlag = 1;
                        else
                            ua.F_DeleteFlag = 0;
                        //int n = new Bll.T_Sys_UserAccount().Add(ua);
                        bool n = new Bll.T_Sys_UserAccount().Update(ua);
                        if (n)
                        {
                            var obj = new
                            {
                                iResult = "1",
                                cResult = "",
                                pk = n
                            };
                            return Content(obj.ToJson());
                        }
                        else
                        {
                            var obj = new
                            {
                                iResult = "0",
                                cResult = "修改失败",
                                pk = ""
                            };
                            return Content(obj.ToJson());
                        }
                    }
                    else
                    {
                        var obj = new
                        {
                            iResult = "0",
                            cResult = "该人员不存在",
                            pk = ""
                        };
                        return Content(obj.ToJson());
                    }
                }
                else if (RecordChangeType == "Delete")
                {
                    var model = list.Where(x => x.F_StaffCode == jo.StaffCode);
                    if (model != null && model.Count() >= 0)
                    {
                        bool n = new Bll.T_Sys_UserAccount().Delete(model.FirstOrDefault().F_UserId );
                        if (n)
                        {
                            var obj = new
                            {
                                iResult = "1",
                                cResult = "",
                                pk = n
                            };
                            return Content(obj.ToJson());
                        }
                        else
                        {
                            var obj = new
                            {
                                iResult = "0",
                                cResult = "删除失败",
                                pk = ""
                            };
                            return Content(obj.ToJson());
                        }
                    }
                    else
                    {
                        var obj = new
                        {
                            iResult = "0",
                            cResult = "该人员不存在",
                            pk = ""
                        };
                        return Content(obj.ToJson());
                    }
                }
            }
            else if (DictCode == "DictDrug")
            {
                var list = new BLL . T_Sys_DictDrug().GetModelList  ("F_IsDelete=0");
                DictDrug it = JsonConvert.DeserializeObject<DictDrug>(RecordInfo);
                if (RecordChangeType == "Add")
                {
                    if (list.Where(x => x.F_DictDrugid == it.noDictdrug).Count() <= 0)
                    {
                        Model.T_Sys_DictDrug dictDrug = new T_Sys_DictDrug();
                        dictDrug.F_DictDrugid = it.noDictdrug;
                        dictDrug.cDgDispCode = it.cDgDispCode;
                        dictDrug.cDgName = it.cDgName;
                        dictDrug.cDgNameCode = it.cDgNameCode;
                        dictDrug.cDgGeneralName = it.cDgGeneralName;
                        dictDrug.cDgSpecification = it.cDgSpecification;
                        dictDrug.iDgMaxstock = it.iDgMaxstock;
                        dictDrug.iDgMinstock = it.iDgMinstock;
                        dictDrug.cDgDescription = it.cDgDescription;
                        dictDrug.iDgStdPrice = it.iDgStdPrice;
                        dictDrug.iDgLocalStdPrice = it.iDgLocalStdPrice;
                        dictDrug.F_IsDelete = 0;
                        dictDrug.F_CreateTime = DateTime.Now;
                       int n= new BLL.T_Sys_DictDrug().Add(dictDrug);
                        if (n > 0)
                        {
                            var obj = new
                            {
                                iResult = "1",
                                cResult = "",
                                pk = n
                            };
                            return Content(obj.ToJson());
                        }
                        else
                        {
                            var obj = new
                            {
                                iResult = "0",
                                cResult = "添加失败",
                                pk = ""
                            };
                            return Content(obj.ToJson());
                        }
                    }
                    else
                    {
                        var obj = new
                        {
                            iResult = "0",
                            cResult = "该药品已存在",
                            pk = ""
                        };
                        return Content(obj.ToJson());
                    }
                }
                else if (RecordChangeType == "Modify")
                {
                    var model = list.Where(x => x.F_DictDrugid == it .noDictdrug);
                    if (model != null && model.Count() >= 0)
                    {
                        var dictDrug = model.FirstOrDefault();
                        dictDrug.F_DictDrugid = it.noDictdrug;
                        dictDrug.cDgDispCode = it.cDgDispCode;
                        dictDrug.cDgName = it.cDgName;
                        dictDrug.cDgNameCode = it.cDgNameCode;
                        dictDrug.cDgGeneralName = it.cDgGeneralName;
                        dictDrug.cDgSpecification = it.cDgSpecification;
                        dictDrug.iDgMaxstock = it.iDgMaxstock;
                        dictDrug.iDgMinstock = it.iDgMinstock;
                        dictDrug.cDgDescription = it.cDgDescription;
                        dictDrug.iDgStdPrice = it.iDgStdPrice;
                        dictDrug.iDgLocalStdPrice = it.iDgLocalStdPrice;
                        bool n = new BLL.T_Sys_DictDrug().Update(dictDrug);
                        if (n)
                        {
                            var obj = new
                            {
                                iResult = "1",
                                cResult = "",
                                pk = n
                            };
                            return Content(obj.ToJson());
                        }
                        else
                        {
                            var obj = new
                            {
                                iResult = "0",
                                cResult = "修改失败",
                                pk = ""
                            };
                            return Content(obj.ToJson());
                        }
                    }
                    else
                    {
                        var obj = new
                        {
                            iResult = "0",
                            cResult = "该药品不存在",
                            pk = ""
                        };
                        return Content(obj.ToJson());
                    }
                }
                else if (RecordChangeType == "Delete")
                {
                    var model = list.Where(x => x.F_DictDrugid == it.noDictdrug);
                    if (model != null && model.Count() >= 0)
                    {
                        var dictDrug = model.FirstOrDefault();
                        dictDrug.F_IsDelete  = 1;
                        bool n = new BLL.T_Sys_DictDrug().Update(dictDrug);
                        if (n)
                        {
                            var obj = new
                            {
                                iResult = "1",
                                cResult = "",
                                pk = n
                            };
                            return Content(obj.ToJson());
                        }
                        else
                        {
                            var obj = new
                            {
                                iResult = "0",
                                cResult = "删除失败",
                                pk = ""
                            };
                            return Content(obj.ToJson());
                        }
                    }
                    else
                    {
                        var obj = new
                        {
                            iResult = "0",
                            cResult = "该药品不存在",
                            pk = ""
                        };
                        return Content(obj.ToJson());
                    }
                }
            }
            var obj1 = new
            {
                iResult = "0",
                cResult = "",
                pk = ""
            };
            return Content(obj1.ToJson());
        }
        #endregion
        string xmlurl = ConfigHelper.GetValue("xmlurl");
        string SOAPAction = ConfigHelper.GetValue("SOAPAction");
        public class beonduty
        {
            public string DepartmentID_One { set; get; }
            public string DepartmentName_Name { set; get; }
            public string DepartmentID { set; get; }
            public string DepartmentName { set; get; }
            public string DoctorID { set; get; }
            public string DoctorName { set; get; }
            public string ScheduleTime { set; get; }
            public string ScheduleTimeID { set; get; }
            public string ScheduleTimeName { set; get; }
            public string DoctorLevelID { set; get; }
            public string DoctorLevel { set; get; }
            public string HospitalFlag { set; get; }
            public string HospitalName { set; get; }
            public string ScheduleID { set; get; }
        }
        private string be = "{\"Response\":{\"ResultCode\":\"0\",\"ErrorMsg\":\"查询成功！\",\"List\":{\"Item\":[{\"DepartmentID_One\":\"24\",\"DepartmentName_Name\":\"心血管内科专业\",\"DepartmentID\":\"11758\",\"DepartmentName\":\"高血压门诊（东）\",\"DoctorID\":\"12876\",\"DoctorName\":\"赵莎莎\",\"ScheduleTime\":\"2022-11-16\",\"ScheduleTimeID\":\"30\",\"ScheduleTimeName\":\"下午\",\"DoctorLevelID\":\"04 \",\"DoctorLevel\":\"主治医师\",\"HospitalFlag\":\"1\",\"HospitalName\":\"郑州人民医院郑东院区\",\"ScheduleID\":\"655365\"},{\"DepartmentID_One\":\"24\",\"DepartmentName_Name\":\"心血管内科专业\",\"DepartmentID\":\"11758\",\"DepartmentName\":\"高血压门诊（东）\",\"DoctorID\":\"12876\",\"DoctorName\":\"赵莎莎\",\"ScheduleTime\":\"2022-11-18\",\"ScheduleTimeID\":\"10\",\"ScheduleTimeName\":\"上午\",\"DoctorLevelID\":\"04 \",\"DoctorLevel\":\"主治医师\",\"HospitalFlag\":\"1\",\"HospitalName\":\"郑州人民医院郑东院区\",\"ScheduleID\":\"655372\"},{\"DepartmentID_One\":\"24\",\"DepartmentName_Name\":\"心血管内科专业\",\"DepartmentID\":\"11758\",\"DepartmentName\":\"高血压门诊（东）\",\"DoctorID\":\"12876\",\"DoctorName\":\"赵莎莎\",\"ScheduleTime\":\"2022-11-18\",\"ScheduleTimeID\":\"30\",\"ScheduleTimeName\":\"下午\",\"DoctorLevelID\":\"04 \",\"DoctorLevel\":\"主治医师\",\"HospitalFlag\":\"1\",\"HospitalName\":\"郑州人民医院郑东院区\",\"ScheduleID\":\"655373\"},{\"DepartmentID_One\":\"24\",\"DepartmentName_Name\":\"心血管内科专业\",\"DepartmentID\":\"11758\",\"DepartmentName\":\"高血压门诊（东）\",\"DoctorID\":\"12876\",\"DoctorName\":\"赵莎莎\",\"ScheduleTime\":\"2022-11-20\",\"ScheduleTimeID\":\"10\",\"ScheduleTimeName\":\"上午\",\"DoctorLevelID\":\"04 \",\"DoctorLevel\":\"主治医师\",\"HospitalFlag\":\"1\",\"HospitalName\":\"郑州人民医院郑东院区\",\"ScheduleID\":\"655380\"},{\"DepartmentID_One\":\"24\",\"DepartmentName_Name\":\"心血管内科专业\",\"DepartmentID\":\"11758\",\"DepartmentName\":\"高血压门诊（东）\",\"DoctorID\":\"12876\",\"DoctorName\":\"赵莎莎\",\"ScheduleTime\":\"2022-11-20\",\"ScheduleTimeID\":\"30\",\"ScheduleTimeName\":\"下午\",\"DoctorLevelID\":\"04 \",\"DoctorLevel\":\"主治医师\",\"HospitalFlag\":\"1\",\"HospitalName\":\"郑州人民医院郑东院区\",\"ScheduleID\":\"655381\"},{\"DepartmentID_One\":\"24\",\"DepartmentName_Name\":\"心血管内科专业\",\"DepartmentID\":\"11242\",\"DepartmentName\":\"心血管内科门诊(东)\",\"DoctorID\":\"12876\",\"DoctorName\":\"赵莎莎\",\"ScheduleTime\":\"2022-11-16\",\"ScheduleTimeID\":\"30\",\"ScheduleTimeName\":\"下午\",\"DoctorLevelID\":\"04 \",\"DoctorLevel\":\"主治医师\",\"HospitalFlag\":\"1\",\"HospitalName\":\"郑州人民医院郑东院区\",\"ScheduleID\":\"655364\"},{\"DepartmentID_One\":\"24\",\"DepartmentName_Name\":\"心血管内科专业\",\"DepartmentID\":\"11242\",\"DepartmentName\":\"心血管内科门诊(东)\",\"DoctorID\":\"12876\",\"DoctorName\":\"赵莎莎\",\"ScheduleTime\":\"2022-11-18\",\"ScheduleTimeID\":\"10\",\"ScheduleTimeName\":\"上午\",\"DoctorLevelID\":\"04 \",\"DoctorLevel\":\"主治医师\",\"HospitalFlag\":\"1\",\"HospitalName\":\"郑州人民医院郑东院区\",\"ScheduleID\":\"655374\"},{\"DepartmentID_One\":\"24\",\"DepartmentName_Name\":\"心血管内科专业\",\"DepartmentID\":\"11242\",\"DepartmentName\":\"心血管内科门诊(东)\",\"DoctorID\":\"12876\",\"DoctorName\":\"赵莎莎\",\"ScheduleTime\":\"2022-11-18\",\"ScheduleTimeID\":\"30\",\"ScheduleTimeName\":\"下午\",\"DoctorLevelID\":\"04 \",\"DoctorLevel\":\"主治医师\",\"HospitalFlag\":\"1\",\"HospitalName\":\"郑州人民医院郑东院区\",\"ScheduleID\":\"655375\"},{\"DepartmentID_One\":\"24\",\"DepartmentName_Name\":\"心血管内科专业\",\"DepartmentID\":\"11242\",\"DepartmentName\":\"心血管内科门诊(东)\",\"DoctorID\":\"12876\",\"DoctorName\":\"赵莎莎\",\"ScheduleTime\":\"2022-11-20\",\"ScheduleTimeID\":\"10\",\"ScheduleTimeName\":\"上午\",\"DoctorLevelID\":\"04 \",\"DoctorLevel\":\"主治医师\",\"HospitalFlag\":\"1\",\"HospitalName\":\"郑州人民医院郑东院区\",\"ScheduleID\":\"655382\"},{\"DepartmentID_One\":\"24\",\"DepartmentName_Name\":\"心血管内科专业\",\"DepartmentID\":\"11242\",\"DepartmentName\":\"心血管内科门诊(东)\",\"DoctorID\":\"12876\",\"DoctorName\":\"赵莎莎\",\"ScheduleTime\":\"2022-11-20\",\"ScheduleTimeID\":\"30\",\"ScheduleTimeName\":\"下午\",\"DoctorLevelID\":\"04 \",\"DoctorLevel\":\"主治医师\",\"HospitalFlag\":\"1\",\"HospitalName\":\"郑州人民医院郑东院区\",\"ScheduleID\":\"655383\"}]}}}";

        public ActionResult Getbeonduty(string id, string datetime)
        {

          // return Content(be);


            string url = xmlurl;
            string contenttype = "text/xml;charset=utf-8";
            Hashtable header = new Hashtable();
            header.Add("SOAPAction", SOAPAction);
            string data = LoadXmls($"<![CDATA[<Reques><HospitalFlag>{ id }</HospitalFlag><DateTime>{ datetime}</DateTime></Reques>]]> ");
            string filexml = Http(url, "post", contenttype, header, data);

            try
            {

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(filexml);//Load加载XML文件，LoadXML加载XML字符串
                 string value = xmlDoc.DocumentElement["soap:Body"]["SearchWeekWorkPlanResponse"]["SearchWeekWorkPlanResult"].InnerText;
                XDocument doc =  XDocument.Parse (value );
                var xmlDocument = new XmlDocument();
                using (var xmlReader = doc.CreateReader())
                {
                    xmlDocument.Load(xmlReader);
                }
               var model = Newtonsoft.Json.JsonConvert.SerializeXmlNode(xmlDocument);
                return Content(model);
            }
            catch (Exception e)
            {
                return Error ( e.ToString());
            }



        }

      
        public ActionResult GetDictDrugList(int isdc = 0)
        {
            DataTable dt = new DataTable();
            string sql = " and F_IsDelete=0 ";
            string strcode = HttpUtility.UrlDecode(RequestString.GetQueryString("code"));
            string strname = HttpUtility.UrlDecode(RequestString.GetQueryString("name"));

            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;


            if (strcode.Trim() != "" && strcode != "undefined")
            {
                sql += " and cDgDispCode = '" + strcode.Trim() + "' ";
            }
            if (strname.Trim() != "" && strname != "undefined")
            {
                sql += " and (cDgName like '%" + strname + "%'or cDgGeneralName like '%" + strname + "%'  ) ";
            }


            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }

            string cols = "*";

         
            int recordCount = 0;
            dt = Bll.PagerBll.GetListPager(
                "T_Sys_DictDrug",
                "F_Id",
                cols,
                sql,
                "ORDER BY F_CreateTime DESC",
                pagesize,
                pageindex,
                true,
                out recordCount);

            var obj = new
            {
                state = "success",
                message = "成功",
                rows = dt,
                total = recordCount
            };
            return Content(obj.ToJson());

        }


        public ActionResult GetDictDrug(int id )
        {
            var model = new BLL.T_Sys_DictDrug().GetModel(id);
           
            if (model!=null )
            {
                return Success("获取成功",model);
            }
            return Error("获取失败");

        }
        /// <summary>
        /// 接口入参拼接
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string LoadXmls(string data)
        {
            XmlDocument doc1 = new XmlDocument();
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(@"<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:tem='http://tempuri.org/'>
   <soapenv:Header/>
   <soapenv:Body>
      <tem:SearchWeekWorkPlan>
         <!--Optional:-->
         <tem:xml>");
            stringBuilder.Append(data);
            stringBuilder.Append(@"</tem:xml>
      </tem:SearchWeekWorkPlan>
   </soapenv:Body>
</soapenv:Envelope> ");
            return stringBuilder.ToString();
        }


        /// <summary>
        /// 调用soap协议接口   webservice    wsdl   
        /// </summary>
        /// <param name="url"></param>
        /// <param name="method"></param>
        /// <param name="contenttype"></param>
        /// <param name="header"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string Http(string url, string method = "post", string contenttype = "text/xml;charset=utf-8", Hashtable header = null, string data = null)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = string.IsNullOrEmpty(method) ? "post" : method;
            request.ContentType = string.IsNullOrEmpty(contenttype) ? "text/xml;charset=utf-8" : contenttype;
            if (header != null)
            {
                foreach (var i in header.Keys)
                {
                    request.Headers.Add(i.ToString(), header[i].ToString());
                }
            }
            if (!string.IsNullOrEmpty(data))
            {
                Stream RequestStream = request.GetRequestStream();
                byte[] bytes = Encoding.UTF8.GetBytes(data);
                RequestStream.Write(bytes, 0, bytes.Length);
                RequestStream.Close();
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream ResponseStream = response.GetResponseStream();
            StreamReader StreamReader = new StreamReader(ResponseStream, Encoding.GetEncoding("utf-8"));
            string re = StreamReader.ReadToEnd();
            StreamReader.Close();
            ResponseStream.Close();
            return re;
        }






        /// <summary>
        /// 解析返回的xml，取其中的结果
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static string GetResult(string xml)
        {
            var doc = new XmlDocument();
            doc.LoadXml(xml);
            var firstNode = doc.ChildNodes[1].InnerText;
            return firstNode;
        }
    }
}