
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MadRunFabric.Common;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using TestUserTypeApi.IRepositories;
using TestUserTypeApi;//.Model.Dot
using AutoMapper;
using Microsoft.Extensions.Logging;
using MadRunFabric.Model.TestUserTypeApi;
using MongoDB.Driver;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Web;
using System.ServiceModel;
using System.Collections;
using System.Net.Http;
using System.Reflection;
using System.Net.Http.Headers;
using Newtonsoft.Json;
using System.Data;
using Microsoft.AspNetCore.Authorization;
using System.Security.Claims;
using MadRunFabric.Model;
using NPOI.SS.Util;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace TestUserTypeApi.Controllers
{
    //[Authorize]
    [ApiVersion("6.0")]
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class TestUserTypeController : BaseController
    {

        // GET: /<controller>/
        public IActionResult Index()
        {
            return View();
        }
        #region 定义接口模块
        #region 试卷满意度调查部分
        private readonly ILogger<TestUserTypeController> _logger;
        private readonly IConfiguration _configuration;
        private readonly ItestusertypeRepository _tsertuserRepository;//每一道试题信息表
        private readonly ItesttypeRepository _ItesttypeRepository;    //试题类型表
        private readonly IUserTestRepository _IUserTestRepository;  //用户总分表
        private readonly IUserDoTestRepository _IUserDoTestRepository;//用户做每一题的信息表 
        #endregion
        #region 信息模块
        /// <summary>
        /// 信息模块
        /// </summary>
        private readonly IMassageInfoRepository _IMassageInfoRepository;//发送信息记录表
        private readonly ItestquestionRepository _ItestquestionRepository;//发送信息记录表
        private readonly IMassageTypeRepository _IMassageTypeRepository;//短信模板 
        #endregion
        #region 在线客服模块
        /// <summary>
        /// 在线客服模块
        /// </summary>
        private readonly ImsgonlineRepository _ImsgonlineRepository;//在线客服
        private readonly ImsgonlinemapRepository _ImsgonlinemapRepository;//在线客服 
        #endregion
        #region 会诊信息
        private readonly IdiagnosisinfoRepository _IdiagnosisinfoRepository;//会诊信息表 
        private readonly IpatientRepository _IpatientRepository;//患者信息
        private readonly IdiagnosisinbasesRepository _IdiagnosisinbasesRepository;//会诊通知基础信息表
        #endregion
        #region 设备调配
        private readonly IequipmentmanagementsRepository _IequipmentmanagementsRepository;
        private readonly IequipmentmanagementlistRepository _IequipmentmanagementlistRepository;
        #endregion
        #region 双向转诊模块
        private readonly ISzzysRepository _ISzzysRepository;//上转住院
        private readonly IHzjbxxRepository _IHzjbxxRepository;//患者信息
        private readonly ISzmzsRepository _ISzmzsRepository;//上转门诊
        private readonly ISzjcsRepository _ISzjcsRepository;//上转检查
        private readonly IXzzlsRepository _IXzzlsRepository;//下转治疗
        private readonly IXzzysRepository _IXzzysRepository;//下转住院
        private readonly IWjzqjsRepository _IWjzqjsRepository;//急危重抢救
        private readonly IBbsqsRepository _IBbsqsRepository;//标本收取
        private readonly IHzapsRepository _IHzapsRepository;  //会诊安排
        private readonly IGzlrbsRepository _IGzlrbsRepository;//工作量日报
        private readonly IYyzlsRepository _IYyzlsRepository;//预约诊疗
        #endregion
        #region 双重预防
        private readonly IFxdjtabsRepository _IFxdjtabsRepository;//风险等级
        private readonly IFxqytabsRepository _IFxqytabsRepository;//风险区域 
        private readonly IYhpctabsRepository _IYhpctabsRepository;//隐患排查
        #endregion
        private readonly IdxsfjlsRepository _IdxsfjlsRepository;
        #region 用户信息
        private readonly ISys_User_AccountRepository _sys_user_accountRepository;
        private readonly IGhcodesRepository _IGhcodesRepository; //工号
        #endregion
        #region 全局定义
        public TestUserTypeController(ILogger<TestUserTypeController> logger,
         IConfiguration configuration,
         ItestusertypeRepository tsertuserRepository,
         ItesttypeRepository testtypeRepository,
         IUserTestRepository UserTestRepository,
         IUserDoTestRepository UserDoTestRepository,
         IMassageInfoRepository MassageInfoRepository,
         ItestquestionRepository ItestquestionRepository,
         IMassageTypeRepository IMassageTypeRepository,
         ImsgonlineRepository ImsgonlineRepository,//在线客服
         ImsgonlinemapRepository ImsgonlinemapRepository,//在线客服
         IdiagnosisinfoRepository IdiagnosisinfoRepository,//会诊
         IdiagnosisinbasesRepository IdiagnosisinbasesRepository,
         IequipmentmanagementsRepository IequipmentmanagementsRepository,
         IequipmentmanagementlistRepository IequipmentmanagementlistRepository,
         ISzzysRepository ISzzysRepository,//上转住院
         IHzjbxxRepository IHzjbxxRepository,//患者信息
         ISzmzsRepository ISzmzsRepository,//上转门诊
         ISzjcsRepository ISzjcsRepository,//上转检查
         IXzzlsRepository IXzzlsRepository,//下转治疗
         IXzzysRepository IXzzysRepository,//下转住院
         IWjzqjsRepository IWjzqjsRepository,//急危重抢救
         IBbsqsRepository IBbsqsRepository,//标本收取
         IHzapsRepository IHzapsRepository,//会诊安排
         IGzlrbsRepository IGzlrbsRepository,//工作量日报
         IYyzlsRepository IYyzlsRepository,//预约诊疗
         IFxdjtabsRepository IFxdjtabsRepository,//风险等级
         IFxqytabsRepository IFxqytabsRepository,//风险区域 
         IYhpctabsRepository IYhpctabsRepository,//隐患排查
         IdxsfjlsRepository IdxsfjlsRepository,
         IpatientRepository IpatientRepository,
         ISys_User_AccountRepository ISys_User_AccountRepository,
         IGhcodesRepository IGhcodesRepository
         )
        {
            _logger = logger;
            _configuration = configuration;
            _tsertuserRepository = tsertuserRepository;
            _ItesttypeRepository = testtypeRepository;
            _IUserTestRepository = UserTestRepository;
            _IUserDoTestRepository = UserDoTestRepository;
            _IMassageInfoRepository = MassageInfoRepository;
            _ItestquestionRepository = ItestquestionRepository;
            _IMassageTypeRepository = IMassageTypeRepository;
            _ImsgonlineRepository = ImsgonlineRepository;
            _ImsgonlinemapRepository = ImsgonlinemapRepository;
            _IdiagnosisinfoRepository = IdiagnosisinfoRepository;
            _IdiagnosisinbasesRepository = IdiagnosisinbasesRepository;
            _IequipmentmanagementsRepository = IequipmentmanagementsRepository;
            _IequipmentmanagementlistRepository = IequipmentmanagementlistRepository;
            _ISzzysRepository = ISzzysRepository;
            _IHzjbxxRepository = IHzjbxxRepository;
            _ISzmzsRepository = ISzmzsRepository;
            _ISzjcsRepository = ISzjcsRepository;
            _IXzzlsRepository = IXzzlsRepository;
            _IXzzysRepository = IXzzysRepository;
            _IWjzqjsRepository = IWjzqjsRepository;
            _IBbsqsRepository = IBbsqsRepository;
            _IHzapsRepository = IHzapsRepository;
            _IGzlrbsRepository = IGzlrbsRepository;
            _IYyzlsRepository = IYyzlsRepository;
            _IFxdjtabsRepository = IFxdjtabsRepository;
            _IFxqytabsRepository = IFxqytabsRepository;
            _IYhpctabsRepository = IYhpctabsRepository;
            _IdxsfjlsRepository = IdxsfjlsRepository;
            _IpatientRepository = IpatientRepository;
            _sys_user_accountRepository = ISys_User_AccountRepository;
            _IGhcodesRepository = IGhcodesRepository;
        }
        #endregion
        #endregion
        #region 满意度模块
        /// <summary>
        /// 查询试题
        /// </summary>
        /// <returns></returns>
        [HttpGet("getlistdrop")]
        public async Task<IActionResult> GetListDrop(int PageIndex, int PageSize, string testtypeid)
        {
            //var list1 = await _tsertuserRepository.Get(p=>p.testtype==testtypeid);

            //if (testtypeid != null)
            //{
            //    list = await _tsertuserRepository.Get(s => s.testtype == testtypeid);
            //}
            //排序字段
            var sort = Builders<TestPager>.Sort.Ascending("testcode");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<TestPager>>();
            listApp.Add(Builders<TestPager>.Filter.Eq("isdelete", 0));
            if (testtypeid != null)
            {
                listApp.Add(Builders<TestPager>.Filter.Eq("testtype", testtypeid));
            }
            var filter = Builders<TestPager>.Filter.And(listApp);

            var list = await _tsertuserRepository.GetByPage(filter, PageIndex, PageSize, sort);
            var redCount = await _tsertuserRepository.CountAsync(filter);   //获取总数
            var listmodel = new List<TestPager>();


            int iscount = 1;
            foreach (var item in list)
            {
                ///定义选项
                var optioncenter = "";
                TestTypes ttps = await _ItesttypeRepository.GetSingle(p => p.id == item.testtype);
                var tqn = await _ItestquestionRepository.Get(p => p.testid == item.id);
                foreach (var tqnitem in tqn)
                {
                    optioncenter += tqnitem.questname;
                    if (iscount < tqn.Count())
                    {
                        optioncenter += "; ";
                    }
                    iscount++;
                }
                var model = new TestPager();
                model.id = item.id;
                model.testtitle = item.testtitle;
                model.testcode = item.testcode;
                model.testoptions = optioncenter;
                model.testtype = item.testtype;
                if (ttps != null)
                {
                    model.testtypename = ttps.Typename;
                }
                else
                {
                    model.testtypename = "";
                }
                //model.tesquestid = model.tesquestid;//选项编号
                model.isdelete = item.isdelete;
                listmodel.Add(model);
            }
            var obj = new
            {
                rows = listmodel,
                total = redCount
            };
            return Success("成功", obj);
        }
        /// <summary>
        /// 查询试题
        /// </summary>
        /// <returns></returns>
        [HttpGet("getlistdroplis")]
        public async Task<IActionResult> GetListDropLis(int PageIndex, int PageSize, string testtypeid)
        {

            //排序字段
            var sort = Builders<TestPager>.Sort.Ascending("testcode");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<TestPager>>();
            listApp.Add(Builders<TestPager>.Filter.Eq("isdelete", 0));
            if (testtypeid != null)
            {
                listApp.Add(Builders<TestPager>.Filter.Eq("testtype", testtypeid));
            }
            var filter = Builders<TestPager>.Filter.And(listApp);

            var list = await _tsertuserRepository.GetByPage(filter, PageIndex, PageSize, sort);
            var redCount = await _tsertuserRepository.CountAsync(filter);   //获取总数
            var listmodel = new List<TestPager>();


            foreach (var item in list)
            {
                TestTypes ttps = await _ItesttypeRepository.GetSingle(p => p.id == item.testtype);

                var tqn = await _ItestquestionRepository.Get(p => p.testid == item.id);
                var model = new TestPager();
                model.id = item.id;
                model.testtitle = item.testtitle;
                model.testcode = item.testcode;
                model.optionlists = tqn;//选项集合信息
                model.testtype = item.testtype;
                if (ttps != null)
                {
                    model.testtypename = ttps.Typename;
                }

                //model.tesquestid = model.tesquestid;//选项编号
                model.isdelete = item.isdelete;
                listmodel.Add(model);
            }
            var obj = new
            {
                rows = listmodel,
                total = redCount
            };
            return Success("成功", obj);
        }
        /// <summary>
        /// 添加试卷问题
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("addtestpage")]
        public async Task<IActionResult> Addtestpage(Model.Dto.TestPager tps)
        {
            TestPager tp = new TestPager();
            tp.testtype = tps.testtype;
            tp.testtitle = tps.testtitle;
            tp.testcode = tps.testcode;
            tp.standardkey = tps.standardkey;
            tp.isdelete = 0;
            tp.createdatatime = DateTime.Now;
            var a = _tsertuserRepository.AddRetID(tp);
            if (a.Result != null || a.Result != "")
            {
                return Success("成功", a.Result);
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 根据id获取详情信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("gettestpageinfo")]
        public async Task<IActionResult> Gettestpageinfo(string id)
        {
            var model = await _tsertuserRepository.GetSingle(p => p.id == id);
            var listmodel = new List<TestPager>();
            listmodel.Add(model);

            return Success("成功", listmodel);
        }
        /// <summary>
        /// 修改试卷信息表
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("updatestpage")]
        public async Task<IActionResult> updatestpage(Model.Dto.TestPager tps)
        {
            var model = await _tsertuserRepository.GetSingle(p => p.id == tps.id);

            TestPager tp = new TestPager();
            tp.id = tps.id;
            tp.testtype = tps.testtype;
            tp.testtitle = tps.testtitle;
            tp.testoptions = tps.testoptions;
            tp.testcode = tps.testcode;
            tp.standardkey = tps.standardkey;
            tp.createdatatime = DateTime.Now;
            var resua = _tsertuserRepository.Update(tp);
            if (resua.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 修改试卷信息表选项
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("updatestpageoption")]
        public async Task<IActionResult> updatestpageoption(string id, string testoptions)
        {
            var model = await _tsertuserRepository.GetSingle(p => p.id == id);
            model.testoptions = testoptions;

            var resua = _tsertuserRepository.Update(model);
            if (resua.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }

        /// <summary>
        /// 删除试卷问题
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("delTestp")]
        public async Task<IActionResult> DelTestp(string id)
        {
            var result = _tsertuserRepository.Remove(id);
            if (result.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }
        }
        /// <summary>
        /// 添加试卷类型
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("addtestType")]
        public async Task<IActionResult> AddtestType(Model.Dto.TestTypesDto tps)
        {
            TestTypes tp = new TestTypes();
            tp.Typename = tps.Typename;
            tp.isdelete = 0;
            tp.Createtime = DateTime.Now;
            var a = _ItesttypeRepository.AddRetID(tp);
            if (a.Result != null || a.Result != "")
            {
                return Success("成功", a.Result);
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 查询试题类型
        /// </summary>
        /// <returns></returns>
        [HttpGet("gettesttype")]
        public async Task<IActionResult> Gettesttype(int PageIndex, int PageSize)
        {
            //排序字段
            var sort = Builders<TestTypes>.Sort.Descending("Createtime");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<TestTypes>>();
            listApp.Add(Builders<TestTypes>.Filter.Eq("isdelete", 0));

            var filter = Builders<TestTypes>.Filter.And(listApp);

            var list = await _ItesttypeRepository.GetByPage(filter, PageIndex, PageSize, sort);
            var redCount = await _ItesttypeRepository.CountAsync(filter);   //获取总数
            var listmodel = new List<TestTypes>();
            foreach (var item in list)
            {
                var model = new TestTypes();
                model.id = item.id;
                model.Typename = item.Typename;
                model.Createtime = item.Createtime;
                listmodel.Add(model);
            }
            var obj = new
            {
                rows = listmodel,
                total = redCount
            };
            return Success("成功", obj);
        }
        /// <summary>
        /// 根据id获取详情信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("gettesttypeinfo")]
        public async Task<IActionResult> Gettesttypeinfo(string id)
        {
            var model = await _ItesttypeRepository.GetSingle(p => p.id == id);
            var listmodel = new List<TestTypes>();
            listmodel.Add(model);

            return Success("成功", listmodel);
        }
        /// <summary>
        /// 修改试题类型表
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("updatesttype")]
        public async Task<IActionResult> updatesttype(Model.Dto.TestTypesDto tps)
        {
            TestTypes tp = new TestTypes();
            tp.id = tps.id;
            tp.Typename = tps.Typename;
            tp.Createtime = DateTime.Now;
            var resua = _ItesttypeRepository.Update(tp);
            if (resua.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 删除试卷类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("delTesttype")]
        public async Task<IActionResult> DelTesttype(string id)
        {
            var result = _ItesttypeRepository.Remove(id);
            if (result.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }
        }

        /// <summary>
        /// 查询用户做的每一题
        /// </summary>
        /// <returns></returns>
        [HttpGet("getUserDoTest")]
        public async Task<IActionResult> GetUserDoTest(int PageIndex, int PageSize, string telcode)
        {
            //var list = await _IUserDoTestRepository.GetAll();

            #region 分页查询
            ////排序字段
            //var sort = Builders<UserDoTest>.Sort.Descending("createtime");
            //var flist = new List<FilterDefinition<UserDoTest>>();
            //flist.Add(Builders<UserDoTest>.Filter.Eq("telcode", "13900006971"));
            //var filter = Builders<UserDoTest>.Filter.And(flist);
            //var lists = await _IUserDoTestRepository.GetByPage(filter, 1, 2, sort); 
            #endregion
            //排序字段
            var sort = Builders<UserDoTest>.Sort.Descending("createtime");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<UserDoTest>>();
            listApp.Add(Builders<UserDoTest>.Filter.Eq("isdelete", 0));
            if (telcode != null)
            {
                listApp.Add(Builders<UserDoTest>.Filter.Eq("telcode", telcode));
            }

            var filter = Builders<UserDoTest>.Filter.And(listApp);

            var list = await _IUserDoTestRepository.GetByPage(filter, PageIndex, PageSize, sort);
            var redCount = await _IUserDoTestRepository.CountAsync(filter);   //获取总数
            var listmodel = new List<UserDoTest>();
            foreach (var item in list)
            {
                var Typename = "";
                TestPager tpser = await _tsertuserRepository.GetSingle(p => p.id == item.testid);
                TestTypes ttps = await _ItesttypeRepository.GetSingle(p => p.id == item.testtype);
                if (ttps != null)
                {
                    Typename = ttps.Typename;
                }
                var model = new UserDoTest();
                model.id = item.id;
                model.telcode = item.telcode;//电话号
                model.testid = item.testid;//试卷表id
                if (tpser != null)
                {
                    model.testname = tpser.testtitle;//试题名称
                }

                model.testoptions = item.testoptions;//选项
                model.grade = item.grade;//得分
                model.testtype = item.testtype;//试卷类型
                model.testtypename = Typename;//试卷类型名称
                model.createtime = item.createtime;//做题时间
                listmodel.Add(model);
            }
            var obj = new
            {
                rows = listmodel,
                total = redCount
            };
            return Success("成功", obj);
        }
        /// <summary>
        /// 添加用户做的每一题
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("addUserDoTest")]
        public async Task<IActionResult> AddUserDoTest(Model.Dto.UserDoTestDto tps)
        {
            ///试题类型
            var testtype = "";
            var list = _tsertuserRepository.GetUsertesttype(tps.testid);

            var model = await _tsertuserRepository.GetSingle(p => p.id == tps.testid);
            if (model != null)
            {
                testtype = model.testtype;
            }

            UserDoTest tp = new UserDoTest();
            tp.telcode = tps.telcode;//电话号
            tp.testid = tps.testid;//试卷表id
            tp.testtype = testtype;//试卷类型
            tp.testoptions = tps.testoptions;//选项
            tp.grade = tps.grade;//得分
            tp.createtime = DateTime.Now;//做题时间
            tp.isdelete = 0;

            var a = _IUserDoTestRepository.AddRetID(tp);

            if (a.Result != null || a.Result != "")
            {

                var testuserlist = await _IUserTestRepository.GetSingle(p => p.telcode == tps.telcode && p.testtype == testtype);
                if (testuserlist == null)
                {
                    UserTest usertest = new UserTest();
                    usertest.telcode = tps.telcode;//电话号
                    usertest.testcount = 1;//答题数量
                    usertest.allgrade = tps.grade;//总分数
                    //usertest.testid = tps.testid;//问题编号
                    usertest.testtype = testtype;//试卷类型
                    usertest.isdelete = 0;
                    var resua = _IUserTestRepository.AddRetID(usertest);
                    if (resua.Result != null || resua.Result != "")
                    {
                        return Success("成功", resua.Result);
                    }
                    else
                    {
                        return Error("失败");
                    }
                }
                else
                {
                    UserTest usertest = new UserTest();
                    usertest.id = testuserlist.id;
                    usertest.telcode = tps.telcode;//电话号
                    usertest.testcount = testuserlist.testcount + 1;//答题数量
                    usertest.allgrade = testuserlist.allgrade + tps.grade;//总分数
                    usertest.testtype = testtype;//试卷类型
                    usertest.createtime = DateTime.Now;
                    var resua = _IUserTestRepository.Update(usertest);
                    if (resua.Result)
                    {
                        return Success("成功", a.Result);
                    }
                    else
                    {
                        return Error("失败");
                    }
                }
                return Success("成功", a.Result);
            }
            else
            {
                return Error("失败");
            }
        }
        /// <summary>
        /// 查看用户总分信息表
        /// </summary>
        /// <returns></returns>
        [HttpGet("getUserTest")]
        public async Task<IActionResult> GetUserTest(int PageIndex, int PageSize)
        {
            //排序字段
            var sort = Builders<UserTest>.Sort.Descending("createtime");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<UserTest>>();
            listApp.Add(Builders<UserTest>.Filter.Eq("isdelete", 0));

            var filter = Builders<UserTest>.Filter.And(listApp);

            var list = await _IUserTestRepository.GetByPage(filter, PageIndex, PageSize, sort);
            var redCount = await _IUserTestRepository.CountAsync(filter);   //获取总数
            var listmodel = new List<UserTest>();
            foreach (var item in list)
            {
                TestTypes ttps = await _ItesttypeRepository.GetSingle(p => p.id == item.testtype);
                var model = new UserTest();
                model.id = item.id;
                model.telcode = item.telcode;//电话号
                model.testcount = item.testcount;//答题数量
                model.allgrade = item.allgrade;//总分数
                model.testtype = item.testtype;//试卷类型
                if (ttps != null)
                {
                    model.testtypename = ttps.Typename;//试卷类型名称
                }

                // model.testid = item.testid;//问题id

                listmodel.Add(model);
            }
            var obj = new
            {
                rows = listmodel,
                total = redCount
            };
            return Success("成功", obj);
        }
        /// <summary>
        /// 添加用户总分表
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("addUserTest")]
        public async Task<IActionResult> AddUserTest(Model.Dto.UserTestDto tps)
        {
            UserTest tp = new UserTest();
            tp.telcode = tps.telcode;//电话号
            tp.testcount = tps.testcount;//答题数量
            tp.allgrade = tps.allgrade;//总分数
            tp.testtype = tps.testtype;//试卷类型
            tp.isdelete = 0;
            var a = _IUserTestRepository.AddRetID(tp);
            if (a.Result != null || a.Result != "")
            {
                return Success("成功", a.Result);
            }
            else
            {
                return Error("失败");
            }
        }
        /// <summary>
        /// 删除用户表信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("delUserTest")]
        public async Task<IActionResult> DelUserTest(string id)
        {
            var result = _IUserTestRepository.Remove(id);
            if (result.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }
        }

        /// <summary>
        /// 添加问题选项表
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("addtestquestion")]
        public async Task<IActionResult> AddTestQuestion(Model.Dto.testquestion tps)
        {

            testquestion tp = new testquestion();
            tp.questname = tps.questname;//选项内容
            tp.testid = tps.testid;//题目编号
            tp.grade = tps.grade;//分数
            tp.isdelete = 0;
            var a = _ItestquestionRepository.AddRetID(tp);
            if (a.Result != null || a.Result != "")
            {
                return Success("成功", a.Result);
            }
            else
            {
                return Error("失败");
            }
        }
        /// <summary>
        /// 修改试题选项表
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("updatequestion")]
        public async Task<IActionResult> updateQuestion(Model.Dto.testquestion tps)
        {
            testquestion tp = new testquestion();
            tp.id = tps.id;
            tp.questname = tps.questname;//选项内容
            tp.testid = tps.testid;//题目编号
            tp.grade = tps.grade;//分数
            var resua = _ItestquestionRepository.Update(tp);
            if (resua.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 删除试题选项表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("delquestion")]
        public async Task<IActionResult> DelQuestion(string id)
        {
            var result = _ItestquestionRepository.Remove(id);
            if (result.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }
        }

        /// <summary>
        /// 根据id获取详情信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("gettestquestinfo")]
        public async Task<IActionResult> Gettestquestinfo(string testid)
        {
            var model = await _ItestquestionRepository.Get(p => p.testid == testid);
            return Success("成功", model);
        }

        /// <summary>
        /// 根据id获取详情信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("gettestquestinfobyid")]
        public async Task<IActionResult> Gettestquestinfobyid(string id)
        {
            var model = await _ItestquestionRepository.Get(p => p.id == id);
            return Success("成功", model);
        }
        #endregion
        #region 短信模块
        /// <summary>
        /// 获取短信模板信息
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="typename"></param>
        /// <returns></returns>
        [HttpGet("getlistmassagetype")]
        public async Task<IActionResult> GetListMassageType(int PageIndex, int PageSize, string typename)
        {

            //排序字段
            var sort = Builders<MassageType>.Sort.Descending("Createtime");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<MassageType>>();
            listApp.Add(Builders<MassageType>.Filter.Eq("isdelete", 0));
            if (typename != null)
            {
                listApp.Add(Builders<MassageType>.Filter.Eq("Typename", typename));
            }
            var filter = Builders<MassageType>.Filter.And(listApp);

            var list = await _IMassageTypeRepository.GetByPage(filter, PageIndex, PageSize, sort);
            var redCount = await _IMassageTypeRepository.CountAsync(filter);   //获取总数
            var listmodel = new List<MassageType>();


            int iscount = 1;
            foreach (var item in list)
            {
                var model = new MassageType();
                model.id = item.id;
                model.sencenter = item.sencenter;//短信内容
                model.Typename = item.Typename;//短信类型
                model.Createtime = item.Createtime;//添加时间
                //model.tesquestid = model.tesquestid;//选项编号
                model.isdelete = item.isdelete;
                listmodel.Add(model);
            }
            var obj = new
            {
                rows = listmodel,
                total = redCount
            };
            return Success("成功", obj);
        }
        /// <summary>
        /// 添加短信模板
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("addmassagetype")]
        public async Task<IActionResult> AddMassageType(Model.Dto.MassageType tps)
        {
            //根据短信类型获取信息是否有重复的
            var model = await _IMassageTypeRepository.GetSingle(p => p.Typename == tps.Typename);
            if (model == null)
            {
                MassageType tp = new MassageType();
                tp.sencenter = tps.sencenter;//短信内容
                tp.Typename = tps.Typename;//短信类型
                tp.Createtime = DateTime.Now;//添加时间
                tp.isdelete = 0;
                var a = _IMassageTypeRepository.AddRetID(tp);
                if (a.Result != null || a.Result != "")
                {
                    return Success("成功", a.Result);
                }
                else
                {
                    return Error("失败");
                }
            }
            else
            {
                return Error("该类型模板已存在请选择编辑");
            }

        }
        /// <summary>
        /// 删除短信模板
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("delmassagetype")]
        public async Task<IActionResult> DelMassageType(string id)
        {
            var res = 0;
            string[] arrStr = id.Split(',');
            foreach (var item in arrStr)
            {
                var result = _IMassageTypeRepository.Remove(item);
                res += 1;
            }
            if (res == arrStr.Length)
                return Success("删除成功");
            else if (res > 0 && res < arrStr.Length)
                return Error("部分删除失败，请查看后重新操作");
            else
                return Error("删除失败，请查看后重新操作");
        }
        /// <summary>
        /// 修改短信模板
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("updatemassagetype")]
        public async Task<IActionResult> updateMassageType(Model.Dto.MassageType tps)
        {
            //根据短信类型获取信息是否有重复的
            var model1 = await _IMassageTypeRepository.GetSingle(p => p.id == tps.id);
            if (model1.Typename == tps.Typename)
            {
                var model = await _IMassageTypeRepository.Get(p => p.Typename == tps.Typename);
                if (model.Count() <= 1)
                {
                    MassageType tp = new MassageType();
                    tp.id = tps.id;
                    tp.sencenter = tps.sencenter;//短信内容
                    tp.Typename = tps.Typename;//短信类型
                    var resua = _IMassageTypeRepository.Update(tp);
                    if (resua.Result)
                    {
                        return Success("成功");
                    }
                    else
                    {
                        return Error("失败");
                    }
                }
                else
                {
                    return Error("该类型模板已存在请选择相应模板进行编辑");
                }
            }
            else
            {
                var model = await _IMassageTypeRepository.Get(p => p.Typename == tps.Typename);
                if (model.Count() < 1)
                {
                    MassageType tp = new MassageType();
                    tp.id = tps.id;
                    tp.sencenter = tps.sencenter;//短信内容
                    tp.Typename = tps.Typename;//短信类型
                    var resua = _IMassageTypeRepository.Update(tp);
                    if (resua.Result)
                    {
                        return Success("成功");
                    }
                    else
                    {
                        return Error("失败");
                    }
                }
                else
                {
                    return Error("该类型模板已存在请选择相应模板进行编辑");
                }
            }



        }

        /// <summary>
        /// 根据id获取详情信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("getmassagetype")]
        public async Task<IActionResult> GetMassageType(string id)
        {
            var model = await _IMassageTypeRepository.Get(p => p.id == id);
            return Success("成功", model);
        }

        /// <summary>
        /// 调用发送短信接口
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("addmassage")]
        public async Task<IActionResult> Addmassage(Model.Dto.MassageInfo tps)
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            ////调用发送短信接口 发送成功就填写数据
            ////先转换成 GBK 编码数据
            //发送内容
            var str = tps.sendcenter;// "您好 ！感谢您对我们医院的新人月支持请对我们的服质量给予评价1 满意；2不满意；3 表扬，回复数字即可。谢谢！顺祝生活愉快【郑州市第一人民医院】";//您好 ！感谢您对我们医院的新人月支持请对我们的服质量给予评价1 满意；2不满意；3 表扬，回复数字即可。谢谢！顺祝生活愉快  
            //发送给哪个手机号
            var Mobile = tps.telcode;
            //定时发送还是立即发送
            var SendTime = tps.SendTime;
            string result1 = HttpUtility.UrlEncode(str, Encoding.GetEncoding("GB2312"));
            var ifsend = false;
            var aaa = " ";
            string url = "https://sdk2.028lk.com/sdk2/BatchSend2.aspx?CorpID=DYRMYY011170&Pwd=xxk@80188&Mobile=" + Mobile + "&Content=" + result1 + "&Cell=&SendTime=";

            //?CorpID=DYRMYY011170&Pwd=xxk@80188&Mobile=13935966971&Content="+ result1 + "&Cell=&SendTime=

            //Encoding encoding = Encoding.GetEncoding("GB2312");
            //Encoding encoding = Encoding.GetEncoding("UTF-8");
            Encoding encoding = Encoding.GetEncoding("GBK");
            IDictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("CorpID", "DYRMYY011170");
            parameters.Add("Pwd", "xxk@80188");
            parameters.Add("Mobile", Mobile);
            parameters.Add("Content", result1);
            parameters.Add("Cell", "");
            parameters.Add("SendTime", SendTime.ToString());
            HttpWebResponse response = PostHttps(url, parameters, encoding);
            //打印返回值
            Stream stream = response.GetResponseStream();   //获取响应的字符串流
            StreamReader sr = new StreamReader(stream); //创建一个stream读取流

            //返回的信息
            string html = sr.ReadToEnd();
            if (Convert.ToInt32(html) > 0)
            {
                string[] arrStr = tps.telcode.Split(',');
                foreach (var item in arrStr)
                {
                    MassageInfo tp = new MassageInfo();
                    tp.telcode = item;
                    tp.sendcenter = tps.sendcenter;
                    tp.senddate = DateTime.Now;
                    tp.isdelete = 0;
                    tp.replycenter = "";
                    tp.sendmbtype = tps.sendmbtype;//模板类型
                    tp.deptname = tps.deptname;//科室名称
                    tp.statesissuccess = "成功";//发送状态
                    tp.dxmyd = "";//短信随访满意度

                    var a = _IMassageInfoRepository.AddRetID(tp);
                }
                return Success("成功");
                //if (a.Result != null || a.Result != "")
                //{
                //    return Success("成功", a.Result);
                //}
                //else
                //{
                //    return Error("失败");
                //}
            }
            else if (Convert.ToInt32(html) == -2)
            {
                return Error("其他错误");
            }
            else if (Convert.ToInt32(html) == -3)
            {
                return Error("帐号或密码错误");
            }
            else if (Convert.ToInt32(html) == -5)
            {
                return Error("余额不足，请充值");
            }
            else if (Convert.ToInt32(html) == -6)
            {
                return Error("定时发送时间不是有效的时间格式");
            }
            else if (Convert.ToInt32(html) == -7)
            {
                return Error("提交信息末尾未签名，请添加中文的企业签名【 】");
            }
            else if (Convert.ToInt32(html) == -8)
            {
                return Error("发送内容需在1到300字之间");
            }
            else if (Convert.ToInt32(html) == -9)
            {
                return Error("发送号码为空");
            }
            else if (Convert.ToInt32(html) == -10)
            {
                return Error("定时时间不能小于系统当前时间");
            }
            else if (Convert.ToInt32(html) == -100)
            {
                return Error("IP黑名单");
            }
            else if (Convert.ToInt32(html) == -102)
            {
                return Error("账号黑名单");
            }
            else if (Convert.ToInt32(html) == -103)
            {
                return Error("IP未导白");
            }
            else
            {
                return Error("调用接口失败");
            }

        }



        /// <summary>
        /// 获取发送短信记录更改回复内容
        /// </summary>
        /// <returns></returns>
        [HttpGet("getmassagereply")]
        public async Task<IActionResult> Getmassagereply()
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            //调用接受短信接口 接受成功就填写数据
            var ifsend = true;

            //string replycent = GetFunction("https://sdk2.028lk.com/sdk2/BatchSend2.aspx?CorpID=DYRMYY011170&Pwd=xxk@80188");//
            //var replycent1 = DoGet("https://sdk2.028lk.com/sdk2/BatchSend2.aspx?CorpID=DYRMYY011170&Pwd=xxk@80188");

            string url = "https://sdk2.028lk.com/sdk2/Get.aspx?CorpID=DYRMYY011170&Pwd=xxk@80188";

            Encoding encoding = Encoding.GetEncoding("GBK");

            // var replycent = GetHttpPost(url, "gb2312");


            IDictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("CorpID", "DYRMYY011170");
            parameters.Add("Pwd", "xxk@80188");
            HttpWebResponse response = PostHttps(url, parameters, encoding);
            //打印返回值
            Stream stream = response.GetResponseStream();   //获取响应的字符串流
            StreamReader sr = new StreamReader(stream); //创建一个stream读取流

            //返回的信息
            string html = sr.ReadToEnd();
            //string html = "||19939971368#1#2020-11-17 10:33:24#||17801222151#1#2020-11-17 10:33:38#";
            var telcode = "";
            var replayscent = "";
            ///获取到接受短信的信息修改发送表信息中的回复内容
            if (html != "")
            {
                string[] arrstr1 = html.Split('|');
                {
                    foreach (var item in arrstr1)
                    {
                        if (item == "")
                        {
                            continue;
                        }
                        string[] arrStr = item.Split('#');
                        if (arrStr.Length > 1)
                        {
                            var aastr1 = arrStr[0].ToString() ;
                            telcode = arrStr[0].ToString();
                            //回复内容
                            replayscent = arrStr[1].ToString();
                        }
                        ///根据获取到的手机号查找发送信息表中的发送信息修改回复内容
                        MassageInfo testuserlist = await _IMassageInfoRepository.GetSingle(p => p.telcode == telcode);
                        if (testuserlist != null)
                        {
                            //MassageInfo tp = new MassageInfo();
                            if (arrStr.Length > 1)
                            {
                                testuserlist.replycenter = replayscent;
                                testuserlist.dxmyd = replayscent;
                            }

                            else
                            {
                                testuserlist.replycenter = "";
                                testuserlist.dxmyd = "";
                            }
                            var a = _IMassageInfoRepository.Update(testuserlist);
                        }
                    }
                    return Success("成功");
                    //if (a.Result)
                    //{
                    //    return Success("成功", a.Result);
                    //}
                    //else
                    //{
                    //    return Error("失败");
                    //}
                }
            }
            else
            {
                return Error("没有上行信息");
            }
        }

        /// <summary>
        /// 获取发送短信记录
        /// </summary>
        /// <returns></returns>
        [HttpGet("getmassage")]
        public async Task<IActionResult> Getmassage(int PageIndex, int PageSize)
        {
            //排序字段
            var sort = Builders<MassageInfo>.Sort.Descending("senddate");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<MassageInfo>>();
            listApp.Add(Builders<MassageInfo>.Filter.Eq("isdelete", 0));

            var filter = Builders<MassageInfo>.Filter.And(listApp);

            var list = await _IMassageInfoRepository.GetByPage(filter, PageIndex, PageSize, sort);
            var redCount = await _IMassageInfoRepository.CountAsync(filter);   //获取总数

            var listmodel = new List<MassageInfo>();
            foreach (var item in list)
            {
                var model = new MassageInfo();
                model.id = item.id;
                model.telcode = item.telcode;
                model.sendcenter = item.sendcenter;
                model.senddate = item.senddate;
                model.replycenter = item.replycenter;
                model.sendmbtype = item.sendmbtype;//模板类型
                model.deptname = item.deptname;//科室名称
                model.statesissuccess = item.statesissuccess;//发送状态
                model.dxmyd = item.dxmyd;//短信随访满意度
                model.isdelete = item.isdelete;
                listmodel.Add(model);
            }
            var obj = new
            {
                rows = listmodel,
                total = redCount
            };
            return Success("成功", obj);
        }

        private static readonly string DefaultUserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36";

        private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true; //总是接受   
        }


        public static HttpWebResponse PostHttps(string url, IDictionary<string, string> parameters, Encoding charset)
        {
            HttpWebRequest request = null;
            CookieContainer cookie = new CookieContainer();
            //HTTPSQ请求
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
            request = WebRequest.Create(url) as HttpWebRequest;
            request.CookieContainer = cookie;
            request.ProtocolVersion = HttpVersion.Version11;
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.UserAgent = DefaultUserAgent;
            request.KeepAlive = true;
            request.Headers["Accept-Language"] = "zh-CN,zh;q=0.9";
            //request.Headers["Cookie"] = "username=aaaaaa; Language=zh_CN";
            //如果需要POST数据   
            if (!(parameters == null || parameters.Count == 0))
            {
                StringBuilder buffer = new StringBuilder();
                int i = 0;
                foreach (string key in parameters.Keys)
                {
                    if (i > 0)
                    {
                        if (key == "Content")
                        {
                            buffer.AppendFormat("&{0}={1}", key, HttpUtility.UrlEncode(parameters[key], Encoding.GetEncoding("GB2312"))); //
                        }
                        else
                        {
                            buffer.AppendFormat("&{0}={1}", key, WebUtility.UrlEncode(parameters[key])); //Encoding.GetEncoding("GBK").GetBytes(parameters[key])
                        }

                    }
                    else
                    {
                        buffer.AppendFormat("{0}={1}", key, WebUtility.UrlEncode(parameters[key]));//
                    }
                    i++;
                }
                byte[] data = charset.GetBytes(buffer.ToString());
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }
            }

            return (HttpWebResponse)request.GetResponse();
        }

        public string GetHttpPost(string url, string sEncoding)
        {

            try
            {
                WebClient WC = new WebClient();
                WC.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                int p = url.IndexOf("?");
                string sData = url.Substring(p + 1);
                url = url.Substring(0, p);
                byte[] postData = Encoding.GetEncoding(sEncoding).GetBytes(sData);
                byte[] responseData = WC.UploadData(url, "POST", postData);
                string ct = Encoding.GetEncoding(sEncoding).GetString(responseData);
                return ct;

            }
            catch (Exception Ex)
            {
                return Ex.Message;

            }

        }
        /// <summary>
        /// Get请求发送
        /// </summary>
        /// <param name="requestUrl">url地址</param>
        /// <returns></returns>
        public static async Task<string> DoGet(string requestUrl)
        {
            string responseBody = string.Empty;
            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Add("Method", "Get");
                httpClient.DefaultRequestHeaders.Add("KeepAlive", "false");
                httpClient.DefaultRequestHeaders.Add("UserAgent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");

                HttpResponseMessage response = await httpClient.GetAsync(requestUrl);
                response.EnsureSuccessStatusCode();
                responseBody = await response.Content.ReadAsStringAsync();
            }
            return responseBody;
        }
        public string GetFunction(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
            string retString = myStreamReader.ReadToEnd();
            return retString;
        }

        #endregion
        #region 在线客服模块
        /// <summary>
        /// 根据编号删除聊天记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("delonlinservice")]
        public async Task<IActionResult> Delonlinservice(string id)
        {
            var result = _ImsgonlineRepository.Remove(id);
            if (result.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }
        }
        /// <summary>
        /// 根据编号删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("delmaponlinservice")]
        public async Task<IActionResult> Delmaponlinservice(string id)
        {
            var result = _ImsgonlinemapRepository.Remove(id);
            if (result.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }
        }
        /// <summary>
        /// 获取聊天记录
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        [HttpGet("getListonlineservice")]
        public async Task<IActionResult> GetListOnlineservice(int PageIndex, int PageSize)
        {

            //排序字段
            var sort = Builders<T_Msg_Chat>.Sort.Descending("createdatatime");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<T_Msg_Chat>>();
            listApp.Add(Builders<T_Msg_Chat>.Filter.Eq("isdelete", 0));

            var filter = Builders<T_Msg_Chat>.Filter.And(listApp);

            var list = await _ImsgonlineRepository.GetByPage(filter, PageIndex, PageSize, sort);
            var redCount = await _ImsgonlineRepository.CountAsync(filter);   //获取总数
            var listmodel = new List<T_Msg_Chat>();


            foreach (var item in list)
            {
                var model = new T_Msg_Chat();
                model.id = item.id;
                model.Msgcontent = item.Msgcontent;//信息内容
                model.fromuserid = item.fromuserid;//消息来源
                model.touserid = item.touserid;//发送给谁
                model.createtime = item.createtime;//创建时间
                model.isdelete = item.isdelete;//是否禁用
                listmodel.Add(model);
            }
            var obj = new
            {
                rows = listmodel,
                total = redCount
            };
            return Success("成功", obj);
        }
        /// <summary>
        ///网站接口进入 添加聊天记录表信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("addonlineservice")]
        public async Task<IActionResult> AddOnlineservice(Model.Dto.T_Msg_Chat tps)
        {
            T_Msg_Chat tp = new T_Msg_Chat();
            tp.Msgcontent = tps.Msgcontent;//信息内容
            tp.fromuserid = tps.fromuserid;//消息来源
            tp.touserid = tps.touserid;//发送给谁
            tp.isonetype = 0;//0是网站接口进入的
            tp.isdelete = 0;
            tp.createtime = DateTime.Now;
            var a = _ImsgonlineRepository.AddRetID(tp);
            if (a.Result != null || a.Result != "")
            {
                T_Msg_Chat_Map tmp = new T_Msg_Chat_Map();
                tmp.chatid = a.Result;//聊天记录的编号
                tmp.fromuserid = tps.fromuserid;
                tmp.touserid = tps.touserid;
                tmp.isdelete = 0;
                tmp.isread = 0;//是否查看过
                tmp.createtime = DateTime.Now;
                var b = _ImsgonlinemapRepository.AddRetID(tmp);
                if (b.Result != null || b.Result != "")
                {
                    return Success("成功", a.Result);
                }
                else
                {
                    return Error("添加关联记录失败");
                }

            }
            else
            {
                return Error("添加聊天记录失败");
            }

        }

        /// <summary>
        /// 微信接口进入 添加聊天记录表信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("whataddonlineservice")]
        public async Task<IActionResult> whatAddOnlineservice(Model.Dto.T_Msg_Chat tps)
        {
            T_Msg_Chat tp = new T_Msg_Chat();
            tp.Msgcontent = tps.Msgcontent;//信息内容
            tp.fromuserid = tps.fromuserid;//消息来源
            tp.touserid = tps.touserid;//发送给谁
            tp.isonetype = 1;//1是微信接口进入的
            tp.isdelete = 0;
            tp.createtime = DateTime.Now;
            var a = _ImsgonlineRepository.AddRetID(tp);
            if (a.Result != null || a.Result != "")
            {
                T_Msg_Chat_Map tmp = new T_Msg_Chat_Map();
                tmp.chatid = a.Result;//聊天记录的编号
                tmp.fromuserid = tps.fromuserid;
                tmp.touserid = tps.touserid;
                tmp.isdelete = 0;
                tmp.isread = 0;//是否查看过
                tmp.createtime = DateTime.Now;
                var b = _ImsgonlinemapRepository.AddRetID(tmp);
                if (b.Result != null || b.Result != "")
                {
                    return Success("成功", a.Result);
                }
                else
                {
                    return Error("添加关联记录失败");
                }

            }
            else
            {
                return Error("添加聊天记录失败");
            }

        }
        /// <summary>
        /// 根据编号获取聊天记录信息详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("getonlineservicetinfo")]
        public async Task<IActionResult> GetOnlineservicetinfo(string fromuserid, string touserid)
        {
            var listmodel = new List<T_Msg_Chat>();

            var mapmode1 = await _ImsgonlinemapRepository.Get(p => p.isdelete == 0);
            if (mapmode1 != null)
            {
                foreach (var item in mapmode1)
                {
                    if (item.fromuserid == fromuserid && item.touserid == touserid || item.fromuserid == touserid && item.touserid == fromuserid)
                    {
                        item.isread = 1;//修改成已读
                        var resua = _ImsgonlinemapRepository.Update(item);//修改数据库数据
                        var mapmode2 = await _ImsgonlineRepository.GetSingle(p => p.id == item.chatid);
                        if (mapmode2 != null)
                        {
                            listmodel.Add(mapmode2);
                        }
                    }
                }
                //var model = await _ImsgonlineRepository.Get(p => p.id == id);
                return Success("成功", listmodel);
            }
            else
            {
                return Success("空数据");
            }

        }
        /// <summary>
        /// 获取未读消息列表 已读消息列表  
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        [HttpGet("getlistonlineservicemap")]
        public async Task<IActionResult> GetListOnlineservicemap(int PageIndex, int PageSize, int isread, string fromid)
        {
            var a = _ImsgonlineRepository.GetList(PageIndex, PageSize, 0, out int count);
            //排序字段
            var sort = Builders<T_Msg_Chat_Map>.Sort.Ascending("createdatatime");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<T_Msg_Chat_Map>>();
            listApp.Add(Builders<T_Msg_Chat_Map>.Filter.Eq("isdelete", 0));
            if (isread != null)
            {
                listApp.Add(Builders<T_Msg_Chat_Map>.Filter.Eq("isread", isread));//未读 已读 
            }
            else
            {
                listApp.Add(Builders<T_Msg_Chat_Map>.Filter.Eq("isread", 0));//未读
            }
            if (fromid != null)
            {
                listApp.Add(Builders<T_Msg_Chat_Map>.Filter.Eq("touserid", fromid));//登录人编号
            }
            var filter = Builders<T_Msg_Chat_Map>.Filter.And(listApp);

            var list = await _ImsgonlinemapRepository.GetByPage(filter, PageIndex, PageSize, sort);
            var redCount = await _ImsgonlinemapRepository.CountAsync(filter);   //获取总数
            var listmodel = new List<T_Msg_Chat>();

            try
            {
                foreach (var item in list)
                {
                    var mapmode = await _ImsgonlineRepository.GetSingle(p => p.id == item.chatid);
                    var model = new T_Msg_Chat();
                    model.id = item.id;
                    if (mapmode != null)
                    {
                        model.Msgcontent = mapmode.Msgcontent;//聊天内容
                    }
                    else
                    {
                        model.Msgcontent = "";//聊天内容
                    }
                    model.fromuserid = item.fromuserid;//消息来源

                    model.touserid = item.touserid;//发送给谁

                    model.createtime = item.createtime;//创建时间
                    model.isdelete = item.isdelete;//是否禁用
                    listmodel.Add(model);
                }
                var obj = new
                {
                    rows = listmodel,
                    total = redCount
                };
                return Success("成功", obj);
            }
            catch (Exception e)
            {

                throw e;
            }

        }
        #endregion
        #region 会诊模块
        /// <summary>
        /// 获取会诊信息列表
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        [HttpGet("getlistdiagnosisinfo")]
        public async Task<IActionResult> GetListdiagnosisinfo(int PageIndex, int PageSize, string Qdoctorid, string Hzdoctorid)
        {

            //排序字段
            var sort = Builders<diagnosisinfo>.Sort.Ascending("testcode");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<diagnosisinfo>>();
            listApp.Add(Builders<diagnosisinfo>.Filter.Eq("isdelete", 0));
            //我申请的会诊工单
            if (Qdoctorid != null)
            {
                if (Qdoctorid != "")
                {
                    listApp.Add(Builders<diagnosisinfo>.Filter.Eq("Qdoctorid", Qdoctorid));
                }
            }
            ///接诊的工单
            if (Hzdoctorid != null)
            {
                if (Hzdoctorid != "")
                {
                    listApp.Add(Builders<diagnosisinfo>.Filter.Eq("Hzdoctorid", Qdoctorid));
                }
            }
            var filter = Builders<diagnosisinfo>.Filter.And(listApp);

            var list = await _IdiagnosisinfoRepository.GetByPage(filter, PageIndex, PageSize, sort);
            var redCount = await _IdiagnosisinfoRepository.CountAsync(filter);   //获取总数
            var listmodel = new List<diagnosisinfo>();


            int iscount = 1;
            foreach (var item in list)
            {
                ///定义选项
                var model = new diagnosisinfo();
                model.id = item.id;
                model.Departmentid = item.Departmentid;//科室编号
                model.Hzdoctorid = item.Hzdoctorid;//会诊医生编号
                model.Qdoctorid = item.Qdoctorid;//会诊申请医生编号
                model.diagnosiscenter = item.diagnosiscenter;//会诊内容
                model.diagnosisresult = item.diagnosisresult;//会诊结果
                model.playdate = item.playdate;//发起会诊时间
                model.enddate = item.enddate;//结束会诊时间
                model.patientid = item.patientid;//患者姓名
                model.isdelete = item.isdelete;//是否启用
                listmodel.Add(model);
            }
            var obj = new
            {
                rows = listmodel,
                total = redCount
            };
            return Success("成功", obj);
        }
        /// <summary>
        /// 添加会诊信息记录
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("adddiagnosisinfo")]
        public async Task<IActionResult> Adddiagnosisinfo(Model.Dto.diagnosisinfo tps)
        {
            diagnosisinfo tp = new diagnosisinfo();
            tp.isdelete = 0;
            tp.Departmentid = tps.Departmentid;//科室编号
            tp.Hzdoctorid = tps.Hzdoctorid;//会诊医生编号
            tp.Qdoctorid = tps.Qdoctorid;//会诊申请医生编号
            tp.diagnosiscenter = tps.diagnosiscenter;//会诊内容
            tp.diagnosisresult = tps.diagnosisresult;//会诊结果
            tp.playdate = DateTime.Now;//发起会诊时间
            tp.playingdate = tps.playingdate;//会诊准确时间点
            tp.enddate = tps.enddate;//结束会诊时间
            tp.patientid = tps.patientid;//患者id
            var a = _IdiagnosisinfoRepository.AddRetID(tp);
            if (a.Result != null || a.Result != "")
            {
                return Success("成功", a.Result);
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 删除会诊信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("deldiagnosisinfo")]
        public async Task<IActionResult> Deldiagnosisinfo(string id)
        {
            var result = _IdiagnosisinfoRepository.Remove(id);
            if (result.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }
        }
        /// <summary>
        /// 根据id获取会诊详情信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("getdiagnosisinfo")]
        public async Task<IActionResult> Getdiagnosisinfo(string id)
        {
            var model = await _IdiagnosisinfoRepository.GetSingle(p => p.id == id);
            var listmodel = new List<diagnosisinfo>();
            listmodel.Add(model);

            return Success("成功", listmodel);
        }
        /// <summary>
        /// 修改会诊信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("updatediagnosisinfo")]
        public async Task<IActionResult> updatediagnosisinfo(Model.Dto.diagnosisinfo tps)
        {
            diagnosisinfo tp = new diagnosisinfo();
            tp.id = tps.id;
            var resua = _IdiagnosisinfoRepository.Update(tp);
            if (resua.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 获取要通知会诊的人的列表
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        [HttpGet("getlistdiagnosisinbases")]
        public async Task<IActionResult> GetListdiagnosisinbases(int PageIndex, int PageSize)
        {

            //排序字段
            var sort = Builders<diagnosisinbases>.Sort.Ascending("testcode");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<diagnosisinbases>>();
            listApp.Add(Builders<diagnosisinbases>.Filter.Eq("isdelete", 0));

            var filter = Builders<diagnosisinbases>.Filter.And(listApp);

            var list = await _IdiagnosisinbasesRepository.GetByPage(filter, PageIndex, PageSize, sort);
            var redCount = await _IdiagnosisinbasesRepository.CountAsync(filter);   //获取总数
            var listmodel = new List<diagnosisinbases>();


            int iscount = 1;
            foreach (var item in list)
            {
                ///定义选项
                var model = new diagnosisinbases();
                model.id = item.id;
                model.diagnoname = item.diagnoname;// 参加会诊人姓名
                model.diagnotel = item.diagnotel;// 参加会诊人手机号
                model.diagnophone = item.diagnophone;// 参加会诊人电话号
                model.createdate = item.createdate;// 创建时间
                model.isnotice = item.isnotice;//是否通知 默认未通知为0
                model.isdelete = item.isdelete;//是否启用
                listmodel.Add(model);
            }
            var obj = new
            {
                rows = listmodel,
                total = redCount
            };
            return Success("成功", obj);
        }
        /// <summary>
        /// 修改是否通知了会诊的人
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("updatediagnosisinbases")]
        public async Task<IActionResult> updatediagnosisinbases(Model.Dto.diagnosisinbases tps)
        {
            diagnosisinbases tp = new diagnosisinbases();
            tp.id = tps.id;
            tp.isnotice = tps.isnotice;
            var resua = _IdiagnosisinbasesRepository.Update(tp);
            if (resua.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 添加参与会诊人员信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("adddiagnosisinbases")]
        public async Task<IActionResult> Adddiagnosisinbases(Model.Dto.diagnosisinbases tps)
        {
            diagnosisinbases tp = new diagnosisinbases();
            tp.isdelete = 0;
            tp.diagnoname = tps.diagnoname;// 参加会诊人姓名
            tp.diagnotel = tps.diagnotel;// 参加会诊人手机号
            tp.diagnophone = tps.diagnophone;// 参加会诊人电话号
            tp.createdate = DateTime.Now;// 创建时间
            tp.isnotice = "0";//是否通知 默认未通知为0
            tp.isdelete = 0;//是否启用
            var a = _IdiagnosisinbasesRepository.AddRetID(tp);
            if (a.Result != null || a.Result != "")
            {
                return Success("成功", a.Result);
            }
            else
            {
                return Error("失败");
            }

        }
        #endregion
        #region 设备调配
        /// <summary>
        /// 获取设备信息状态
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        [HttpGet("getequipmentmanagements")]
        public async Task<IActionResult> Getequipmentmanagements(int PageIndex, int PageSize)
        {

            //排序字段
            var sort = Builders<equipmentmanagements>.Sort.Ascending("testcode");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<equipmentmanagements>>();
            listApp.Add(Builders<equipmentmanagements>.Filter.Eq("isdelete", 0));

            var filter = Builders<equipmentmanagements>.Filter.And(listApp);

            var list = await _IequipmentmanagementsRepository.GetByPage(filter, PageIndex, PageSize, sort);
            var redCount = await _IequipmentmanagementsRepository.CountAsync(filter);   //获取总数
            var listmodel = new List<equipmentmanagements>();


            foreach (var item in list)
            {
                ///定义选项
                var model = new equipmentmanagements();
                model.id = item.id;
                model.equipmentmanagementsname = item.equipmentmanagementsname;//名称
                model.equipmentmanagementstate = item.equipmentmanagementstate;//状态
                model.isdelete = item.isdelete;//是否启用
                listmodel.Add(model);
            }
            var obj = new
            {
                rows = listmodel,
                total = redCount
            };
            return Success("成功", obj);
        }

        /// <summary>
        /// 添加设备信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("addequipmentmanagements")]
        public async Task<IActionResult> Addequipmentmanagements(Model.Dto.equipmentmanagements tps)
        {
            equipmentmanagements tp = new equipmentmanagements();
            tp.equipmentmanagementsname = tps.equipmentmanagementsname;//设备名称
            tp.equipmentmanagementstate = tps.equipmentmanagementstate;//设备状态
            tp.isdelete = 0;//是否启用
            var a = _IequipmentmanagementsRepository.AddRetID(tp);
            if (a.Result != null || a.Result != "")
            {
                return Success("成功", a.Result);
            }
            else
            {
                return Error("失败");
            }

        }

        /// <summary>
        /// 删除设备信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("delequipmentmanagements")]
        public async Task<IActionResult> delequipmentmanagements(string id)
        {
            var result = _IequipmentmanagementsRepository.Remove(id);
            if (result.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }

        /// <summary>
        /// 修改设备信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("upequipmentmanagements")]
        public async Task<IActionResult> upequipmentmanagements(Model.Dto.equipmentmanagements tps)
        {
            equipmentmanagements tp = new equipmentmanagements();
            tp.id = tps.id;
            tp.equipmentmanagementsname = tps.equipmentmanagementsname;//设备名称
            tp.equipmentmanagementstate = tps.equipmentmanagementstate;//设备状态
            tp.isdelete = 0;//是否启用
            var resua = _IequipmentmanagementsRepository.Update(tp);
            if (resua.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }

        /// <summary>
        /// 根据id获取设备详情信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("getequipmentmanagementsbyid")]
        public async Task<IActionResult> Getequipmentmanagementsbyid(string id)
        {
            var model = await _IequipmentmanagementsRepository.Get(p => p.id == id);
            return Success("成功", model);
        }

        /// <summary>
        /// 获取设备出入库记录信息
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        [HttpGet("getequipmentmanagementlist")]
        public async Task<IActionResult> Getequipmentmanagementlist(int PageIndex, int PageSize)
        {

            //排序字段
            var sort = Builders<equipmentmanagementlist>.Sort.Ascending("Qqtime");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<equipmentmanagementlist>>();
            listApp.Add(Builders<equipmentmanagementlist>.Filter.Eq("isdelete", 0));

            var filter = Builders<equipmentmanagementlist>.Filter.And(listApp);

            var list = await _IequipmentmanagementlistRepository.GetByPage(filter, PageIndex, PageSize, sort);
            var redCount = await _IequipmentmanagementlistRepository.CountAsync(filter);   //获取总数
            var listmodel = new List<equipmentmanagementlist>();


            foreach (var item in list)
            {
                equipmentmanagements stamodel = await _IequipmentmanagementsRepository.GetSingle(p => p.id == item.equipmentmanagementsid);
                ///定义选项
                var model = new equipmentmanagementlist();
                model.id = item.id;
                if (stamodel != null)
                {
                    model.equipmentmanagementsname = stamodel.equipmentmanagementsname;
                    model.equipmentmanagementstate = stamodel.equipmentmanagementstate;
                }
                else
                {
                    model.equipmentmanagementsname = "";
                    model.equipmentmanagementstate = "";
                }
                model.Qdepartment = item.Qdepartment;// 申请科室
                model.Qqtime = item.Qqtime;// 申请时间
                model.Cktime = item.Cktime;// 出库时间
                model.Rktime = item.Rktime;//入库时间
                model.isdelete = item.isdelete;//是否启用
                listmodel.Add(model);
            }
            var obj = new
            {
                rows = listmodel,
                total = redCount
            };
            return Success("成功", obj);
        }

        /// <summary>
        /// 申请接口(添加申请记录信息)
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("addquipmentmanagementlist")]
        public async Task<IActionResult> addquipmentmanagementlist(Model.Dto.equipmentmanagements tps)
        {

            equipmentmanagementlist tp = new equipmentmanagementlist();
            tp.equipmentmanagementsid = tps.id;//设备编号
            tp.Qdepartment = tps.Sqperpro;//申请科室
            tp.Qqtime = DateTime.Now;//申请时间
            tp.Cktime = null;//出库时间
            tp.Rktime = null;//入库时间
            tp.isdelete = 0;
            var a = _IequipmentmanagementlistRepository.AddRetID(tp);

            if (a.Result != null || a.Result != "")
            {
                return Success("成功", a.Result);
            }
            else
            {
                return Error("失败");
            }


        }

        /// <summary>
        /// 出库接口  传入编号修改出库时间和设备状态
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("upquipmentmanagementlistc")]
        public async Task<IActionResult> upquipmentmanagementlistc(string id)
        {
            equipmentmanagementlist model = await _IequipmentmanagementlistRepository.GetSingle(p => p.id == id);
            if (model != null)
            {
                //获取设备
                equipmentmanagements model1 = await _IequipmentmanagementsRepository.GetSingle(p => p.id == model.equipmentmanagementsid);
                if (model1 != null)
                {
                    model1.equipmentmanagementstate = "1";
                    var b = _IequipmentmanagementsRepository.Update(model1);
                }

            }
            model.id = id;
            model.Cktime = DateTime.Now;//出库时间

            var a = _IequipmentmanagementlistRepository.Update(model);

            if (a.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }
        }


        /// <summary>
        /// 入库接口
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("upquipmentmanagementlistr")]
        public async Task<IActionResult> upquipmentmanagementlistr(string id)
        {
            equipmentmanagementlist tp = await _IequipmentmanagementlistRepository.GetSingle(p => p.id == id);
            if (tp != null)
            {
                //获取设备
                equipmentmanagements model1 = await _IequipmentmanagementsRepository.GetSingle(p => p.id == tp.equipmentmanagementsid);
                if (model1 != null)
                {
                    model1.equipmentmanagementstate = "0";
                    var b = _IequipmentmanagementsRepository.Update(model1);
                }

            }
            tp.id = id;
            tp.Rktime = DateTime.Now;//入库时间
            var a = _IequipmentmanagementlistRepository.Update(tp);

            if (a.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }
        }

        #endregion
        #region 双向转诊模块
        [HttpGet("gethzinflists")]
        public async Task<IActionResult> Gethzinflists(string id,string name )
        {
            if (id != null)
            {
                var model = await _IHzjbxxRepository.Get(p => p.id == id&& p.isdelete == 0);
                return Success("成功", model);
            }
            else
            {
                var model = await _IHzjbxxRepository.Get(p => p.isdelete == 0&&p.Hzname.Contains(name));
                return Success("成功", model);
            }
        }
        #region 上转住院
        /// <summary>
        /// 添加上转住院信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("addszzy")]
        public async Task<IActionResult> AddSzzy(Model.Dto.Szzys tps)
        {
            
            Szzys tp = new Szzys();
            tp.Szzydjsj = DateTime.Now;// tps.Szzydjsj;//登记时间
            tp.Szzyzzdw = tps.Szzyzzdw;//转诊单位
            tp.Szzyzzys = tps.Szzyzzys;//转诊医师
            tp.Yslxfs = tps.Yslxfs;//联系方式
            tp.Hzname = tps.Hzname;//患者姓名
            tp.HzGender = tps.HzGender;//患者性别
            tp.Hzage = tps.Hzage;//患者年龄
            tp.Hzzd = tps.Hzzd;//患者诊断
            tp.Hzzyks = tps.Hzzyks;//住院科室
            tp.Hzzyh = tps.Hzzyh;//住院号
            tp.Hzlxfs = tps.Hzlxfs;//联系方式
            tp.Syzzzzmd = "上转住院";// tps.Syzzzzmd;//转诊目的
            tp.Syzysj =tps.Syzysj;//住院时间
            tp.Syzyfy = tps.Syzyfy;//住院费用
            tp.Syzzzrr = tps.Syzzzrr;//责任人
            tp.Syzzzzfw = tps.Syzzzzfw;//诊中服务
            tp.Syzzzrrs = tps.Syzzzrrs;//责任人
            tp.Syzzzrrss = tps.Syzzzrrss;//责任人
            tp.isred = 0;
            tp.isdelete = 0;
            var a = _ISzzysRepository.AddRetID(tp);
            if (a.Result != null || a.Result != "")
            {
                var hzinfomodel =await _IHzjbxxRepository.GetSingle(p => p.Hzname == tps.Hzname && p.HzGender == tps.HzGender && p.Hzage == tps.Hzage&&p.Hzzd==tps.Hzzd&&p.Hzlxfs==tps.Hzlxfs);
                if (hzinfomodel == null)
                {
                    //患者信息
                    Hzjbxx hzinfomode = new Hzjbxx();
                    hzinfomode.Hzname = tps.Hzname;//患者姓名
                    hzinfomode.HzGender = tps.HzGender;//患者性别
                    hzinfomode.Hzage = tps.Hzage;//患者年龄
                    hzinfomode.Hzzd = tps.Hzzd;//患者诊断
                                               //hzinfomode.Hzzyks = tps.Hzzyks;//住院科室
                                               //hzinfomode.Hzzyh = tps.Hzzyh;//住院号
                    hzinfomode.Hzlxfs = tps.Hzlxfs;//联系方式
                    hzinfomode.isdelete = 0;//是否删除

                    var hzintoid = _IHzjbxxRepository.AddRetID(hzinfomode);
                }
                return Success("成功", a.Result);
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 修改上转住院信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("upszzy")]
        public async Task<IActionResult> upSzzy(Model.Dto.Szzys tps)
        {
            Szzys tp =await _ISzzysRepository.GetSingle(p=>p.id==tps.id);
            //tp.id = tps.id;
            tp.Szzydjsj = tps.Szzydjsj;//登记时间
            tp.Szzyzzdw = tps.Szzyzzdw;//转诊单位
            tp.Szzyzzys = tps.Szzyzzys;//转诊医师
            tp.Yslxfs = tps.Yslxfs;//联系方式
            tp.Hzname = tps.Hzname;//患者姓名
            tp.HzGender = tps.HzGender;//患者性别
            tp.Hzage = tps.Hzage;//患者年龄
            tp.Hzzd = tps.Hzzd;//患者诊断
            tp.Hzzyks = tps.Hzzyks;//住院科室
            tp.Hzzyh = tps.Hzzyh;//住院号
            tp.Hzlxfs = tps.Hzlxfs;//联系方式
            //tp.Syzzzzmd = tps.Syzzzzmd;//转诊目的
            tp.Syzysj = tps.Syzysj;//住院时间
            tp.Syzyfy = tps.Syzyfy;//住院费用
            tp.Syzzzrr = tps.Syzzzrr;//责任人
            tp.Syzzzzfw = tps.Syzzzzfw;//诊中服务
            tp.Syzzzrrs = tps.Syzzzrrs;//责任人
            tp.Syzzzrrss = tps.Syzzzrrss;//责任人
            tp.isdelete = 0;
            var resua = _ISzzysRepository.UpdateOne(tp);
            if (resua.Result)
            {
                var szzysmode = await _ISzzysRepository.GetSingle(p=>p.id==tps.id);
                var hzinfomodel = await _IHzjbxxRepository.GetSingle(p => p.Hzname == szzysmode.Hzname && p.HzGender == szzysmode.HzGender && p.Hzage == szzysmode.Hzage && p.Hzzd == szzysmode.Hzzd && p.Hzlxfs == szzysmode.Hzlxfs);
                if (hzinfomodel != null)
                {
                    //患者信息
                    Hzjbxx hzinfomode = new Hzjbxx();
                    hzinfomode.Hzname = tps.Hzname;//患者姓名
                    hzinfomode.HzGender = tps.HzGender;//患者性别
                    hzinfomode.Hzage = tps.Hzage;//患者年龄
                    hzinfomode.Hzzd = tps.Hzzd;//患者诊断
                                               //hzinfomode.Hzzyks = tps.Hzzyks;//住院科室
                                               //hzinfomode.Hzzyh = tps.Hzzyh;//住院号
                    hzinfomode.Hzlxfs = tps.Hzlxfs;//联系方式
                    hzinfomode.isdelete = 0;//是否删除

                    var hzintoid = _IHzjbxxRepository.Update(hzinfomode);
                }


                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 根据id获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("getszzybyid")]
        public async Task<IActionResult> GetSzzybyid(string id)
        {
            var model = await _ISzzysRepository.Get(p => p.id == id);
            return Success("成功", model);
        }
        /// <summary>
        /// 根据id删除信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("deleszzy")]
        public async Task<IActionResult> deleSzzy(string id)
        {
            var result = _ISzzysRepository.Remove(id);
            if (result.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 获取上转住院信息
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        [HttpGet("getszzy")]
        public async Task<IActionResult> GetSzzy(int PageIndex, int PageSize, string stardate, string endtime, string zzdwname)
        {
            //排序字段
            var sort = Builders<Szzys>.Sort.Descending("Szzydjsj");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<Szzys>>();
            listApp.Add(Builders<Szzys>.Filter.Eq("isdelete", 0));
            if (!string.IsNullOrWhiteSpace(stardate))
            {
                //listApp.Add(Builders<Szzys>.Filter.Gt("Syzysj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                listApp.Add(Builders<Szzys>.Filter.Where(p=>p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
            }
            if (!string.IsNullOrWhiteSpace(endtime))
            {
                //listApp.Add(Builders<Szzys>.Filter.Lt("Syzysj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                listApp.Add(Builders<Szzys>.Filter.Where(p=>p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
            }
            if (zzdwname != null)
            {
                var normallistAppor = new List<FilterDefinition<Szzys>>();
                
                normallistAppor.Add(Builders<Szzys>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                normallistAppor.Add(Builders<Szzys>.Filter.Where(p => p.Hzzyh.Contains(zzdwname)));
                normallistAppor.Add(Builders<Szzys>.Filter.Where(p => p.Hzname.Contains(zzdwname)));
                listApp.Add(Builders<Szzys>.Filter.Or(normallistAppor));
                //listApp.Add(Builders<Szzys>.Filter.Eq("Szzyzzdw", zzdwname));
            }
            var filter = Builders<Szzys>.Filter.And(listApp);

            var list = await _ISzzysRepository.GetByPage(filter, PageIndex, PageSize, sort);
            var redCount = await _ISzzysRepository.CountAsync(filter);   //获取总数
            var listmodel = new List<Szzys>();
            foreach (var item in list)
            {
                var countlis = 0;
                //根据患者名称查找条数
                var listcounts = _ISzzysRepository.Get(p => p.Hzname == item.Hzname);
                if (listcounts != null)
                {
                    if (listcounts.Result.Count() > 1)
                    {
                        countlis = 1;
                    }

                }
                ///定义选项
                var model = new Szzys();
                model.id = item.id;
                model.Szzydjsj = item.Szzydjsj;//登记时间
                model.Szzyzzdw = item.Szzyzzdw;//转诊单位
                model.Szzyzzys = item.Szzyzzys;//转诊医师
                model.Yslxfs = item.Yslxfs;//联系方式
                model.Hzname = item.Hzname;//患者姓名
                model.HzGender = item.HzGender;//患者性别
                model.Hzage = item.Hzage;//患者年龄
                model.Hzzd = item.Hzzd;//患者诊断
                model.Hzzyks = item.Hzzyks;//住院科室
                model.Hzzyh = item.Hzzyh;//住院号
                model.Hzlxfs = item.Hzlxfs;//联系方式
                model.Syzzzzmd = item.Syzzzzmd;//转诊目的
                model.Syzysj = item.Syzysj;//住院时间
                model.Syzyfy = item.Syzyfy;//住院费用
                model.Syzzzrr = item.Syzzzrr;//责任人
                model.Syzzzzfw = item.Syzzzzfw;//诊中服务
                model.Syzzzrrs = item.Syzzzrrs;//责任人
                model.isred = countlis;//颜色状态
                model.Syzzzrrss = item.Syzzzrrss;//责任人

                model.isdelete = item.isdelete;//是否启用
                listmodel.Add(model);
            }
            var obj = new
            {
                rows = listmodel,
                total = redCount
            };
            return Success("成功", obj);
        }
        /// <summary>
        /// 导出上转住院信息
        /// </summary>
        /// <param name="stardate"></param>
        /// <param name="endtime"></param>
        /// <param name="zzdwname"></param>
        /// <returns></returns>
        [HttpGet("exportexcelszzy")]
        [AllowAnonymous]
        public async Task<IActionResult> ExportExcelAsyncwcode(string stardate, string endtime, string zzdwname)
        {
            
            try
            {
                //排序字段
                var sort = Builders<Szzys>.Sort.Descending("Szzydjsj");
                //根据条件查询集合
                var listApp = new List<FilterDefinition<Szzys>>();
                listApp.Add(Builders<Szzys>.Filter.Eq("isdelete", 0));
                if (!string.IsNullOrWhiteSpace(stardate))
                {
                    //listApp.Add(Builders<Szzys>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                }
                if (!string.IsNullOrWhiteSpace(endtime))
                {
                    //listApp.Add(Builders<Szzys>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                }
                if (zzdwname != null)
                {
                    var normallistAppor = new List<FilterDefinition<Szzys>>();

                    normallistAppor.Add(Builders<Szzys>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                    normallistAppor.Add(Builders<Szzys>.Filter.Where(p => p.Hzzyh.Contains(zzdwname)));
                    normallistAppor.Add(Builders<Szzys>.Filter.Where(p => p.Hzname.Contains(zzdwname)));
                    listApp.Add(Builders<Szzys>.Filter.Or(normallistAppor));
                    //listApp.Add(Builders<Szzys>.Filter.Eq("Szzyzzdw", zzdwname));
                }
                var filter = Builders<Szzys>.Filter.And(listApp);

                var lists = await _ISzzysRepository.Get(filter);

                List<Model.Dto.exportexcelszzy> list = new List<Model.Dto.exportexcelszzy>();
                foreach (var item in lists)
                {
                    Model.Dto.exportexcelszzy model = new Model.Dto.exportexcelszzy();
                    model.Szzydjsj = Convert.ToDateTime(item.Szzydjsj).ToString("yyyy-MM-dd  HH:mm:ss");//DateTime.ParseExact("2013-11-17T11:59:22+08:00","yyyy-MM-ddTHH:mm:ss+08:00",null)
                    model.Szzyzzdw =item.Szzyzzdw != null ? item.Szzyzzdw.ToString() : "";
                    model.Szzyzzys =item.Szzyzzys != null ? item.Szzyzzys.ToString() : "";
                    model.Yslxfs   =item.Yslxfs != null ? item.Yslxfs.ToString() : "";
                    model.Hzname   =item.Hzname != null ? item.Hzname.ToString() : "";
                    model.HzGender =item.HzGender != null ? item.HzGender.ToString() : "";
                    model.Hzage    =item.Hzage != null ? item.Hzage.ToString() : "";
                    model.Hzzd     =item.Hzzd != null ? item.Hzzd.ToString() : "";
                    model.Hzzyks   =item.Hzzyks != null ? item.Hzzyks.ToString() : "";
                    model.Hzzyh    =item.Hzzyh != null ? item.Hzzyh.ToString() : "";
                    model.Hzlxfs   =item.Hzlxfs != null ? item.Hzlxfs.ToString() : "";
                    model.Syzzzzmd =item.Syzzzzmd != null ? item.Syzzzzmd.ToString() : "";
                    model.Syzysj   =item.Syzysj != null ? Convert.ToDateTime(item.Syzysj).ToString("yyyy-MM-dd  HH:mm:ss") : "";
                    model.Syzyfy   =item.Syzyfy != null ? item.Syzyfy.ToString() : "";
                    model.Syzzzrr  =item.Syzzzrr != null ? item.Syzzzrr.ToString() : "";
                    model.Syzzzzfw =item.Syzzzzfw != null ? item.Syzzzzfw.ToString() : "";
                    model.Syzzzrrs = item.Syzzzrrs != null ? item.Syzzzrrs.ToString() : "";
                    list.Add(model);
                }
                NPOIHelper npoi = new NPOIHelper();
                var tb = ModelConvertHelper<Szzys>.ListToTable(list,null);
                if (tb != null)
                {
                    //导出dtnew 
                    string[] cols = { "登记时间","转诊单位","转诊医师","联系方式","患者姓名","患者性别","患者年龄","患者诊断","住院科室","住院号","联系方式","转诊目的","住院时间","住院费用","责任人","诊中服务","责任人" };

                    byte[] sm = npoi.ExportToExcel1(tb, cols);
                    if (sm != null)
                    {
                        return File(sm, "application/vnd.ms-excel", "上转住院.xlsx");
                    }
                    else
                    {
                        return Error("导出失败");
                    }
                }
                return Error("没有数据");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "导出异常");
                return Error("导出失败");
            }
        }

        #endregion
        #region 上转门诊
        /// <summary>
        /// 添加上门门诊信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("addszmzs")]
        public async Task<IActionResult> AddSzmzs(Model.Dto.Szmzs tps)
        {
            Szmzs tp = new Szmzs();
            tp.Szzydjsj = DateTime.Now;// tps.Szzydjsj;//登记时间
            tp.Szzyzzdw = tps.Szzyzzdw;//转诊单位
            tp.Szzyzzys = tps.Szzyzzys;//转诊医师
            tp.Yslxfs = tps.Yslxfs;//联系方式
            tp.Hzname = tps.Hzname;//患者姓名
            tp.HzGender = tps.HzGender;//患者性别
            tp.Hzage = tps.Hzage;//患者年龄
            tp.Hzzd = tps.Hzzd;//患者诊断
            tp.Hzjcxm = tps.Hzjcxm;//检查项目
            tp.Hzmzfy = tps.Hzmzfy;//门诊费用
            tp.Hzlxfs = tps.Hzlxfs;//联系方式
            tp.Syzzzzmd = "上转门诊";// tps.Syzzzzmd;//转诊目的
            tp.Syzysj = tps.Syzysj;//住院时间
            tp.Syzyfy = tps.Syzyfy;//住院费用
            tp.Syzzzrr = tps.Syzzzrr;//责任人
            tp.Syzzzzfw = tps.Syzzzzfw;//诊中服务
            tp.Syzzzrrs = tps.Syzzzrrs;//责任人
            tp.Syzzzrrss = tps.Syzzzrrss;//责任人
            tp.isred = 0;
            tp.isdelete = 0;
            var a = _ISzmzsRepository.AddRetID(tp);
            if (a.Result != null || a.Result != "")
            {
                var hzinfomodel = await _IHzjbxxRepository.GetSingle(p => p.Hzname == tps.Hzname && p.HzGender == tps.HzGender && p.Hzage == tps.Hzage && p.Hzzd == tps.Hzzd && p.Hzlxfs == tps.Hzlxfs);
                if (hzinfomodel == null)
                {
                    //患者信息
                    Hzjbxx hzinfomode = new Hzjbxx();
                    hzinfomode.Hzname = tps.Hzname;//患者姓名
                    hzinfomode.HzGender = tps.HzGender;//患者性别
                    hzinfomode.Hzage = tps.Hzage;//患者年龄
                    hzinfomode.Hzzd = tps.Hzzd;//患者诊断
                                               //hzinfomode.Hzzyks = tps.Hzzyks;//住院科室
                                               //hzinfomode.Hzzyh = tps.Hzzyh;//住院号
                    hzinfomode.Hzlxfs = tps.Hzlxfs;//联系方式
                    hzinfomode.isdelete = 0;//是否删除

                    var hzintoid = _IHzjbxxRepository.AddRetID(hzinfomode);
                }
                return Success("成功", a.Result);
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 修改上转门诊信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("upszmzs")]
        public async Task<IActionResult> upSzmzs(Model.Dto.Szmzs tps)
        {
            Szmzs tp =await _ISzmzsRepository.GetSingle(p=>p.id==tps.id);
            tp.id = tps.id;
            tp.Szzydjsj = tps.Szzydjsj;//登记时间
            tp.Szzyzzdw = tps.Szzyzzdw;//转诊单位
            tp.Szzyzzys = tps.Szzyzzys;//转诊医师
            tp.Yslxfs = tps.Yslxfs;//联系方式
            tp.Hzname = tps.Hzname;//患者姓名
            tp.HzGender = tps.HzGender;//患者性别
            tp.Hzage = tps.Hzage;//患者年龄
            tp.Hzzd = tps.Hzzd;//患者诊断
            tp.Hzjcxm = tps.Hzjcxm;//检查项目
            tp.Hzmzfy = tps.Hzmzfy;//门诊费用
            tp.Hzlxfs = tps.Hzlxfs;//联系方式
           // tp.Syzzzzmd = tps.Syzzzzmd;//转诊目的
            tp.Syzysj = tps.Syzysj;//住院时间
            tp.Syzyfy = tps.Syzyfy;//住院费用
            tp.Syzzzrr = tps.Syzzzrr;//责任人
            tp.Syzzzzfw = tps.Syzzzzfw;//诊中服务
            tp.Syzzzrrs = tps.Syzzzrrs;//责任人
            tp.Syzzzrrss = tps.Syzzzrrss;//责任人
            tp.isdelete = 0;
            var resua = _ISzmzsRepository.Update(tp);
            if (resua.Result)
            {
                var szzysmode = await _ISzmzsRepository.GetSingle(p => p.id == tps.id);
                var hzinfomodel = await _IHzjbxxRepository.GetSingle(p => p.Hzname == szzysmode.Hzname && p.HzGender == szzysmode.HzGender && p.Hzage == szzysmode.Hzage && p.Hzzd == szzysmode.Hzzd && p.Hzlxfs == szzysmode.Hzlxfs);
                if (hzinfomodel != null)
                {
                    //患者信息
                    Hzjbxx hzinfomode = new Hzjbxx();
                    hzinfomode.Hzname = tps.Hzname;//患者姓名
                    hzinfomode.HzGender = tps.HzGender;//患者性别
                    hzinfomode.Hzage = tps.Hzage;//患者年龄
                    hzinfomode.Hzzd = tps.Hzzd;//患者诊断
                                               //hzinfomode.Hzzyks = tps.Hzzyks;//住院科室
                                               //hzinfomode.Hzzyh = tps.Hzzyh;//住院号
                    hzinfomode.Hzlxfs = tps.Hzlxfs;//联系方式
                    hzinfomode.isdelete = 0;//是否删除

                    var hzintoid = _IHzjbxxRepository.Update(hzinfomode);
                }
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 根据id获取上转门诊的信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("getszmzsbyid")]
        public async Task<IActionResult> GetSzmzsbyid(string id)
        {
            var model = await _ISzmzsRepository.Get(p => p.id == id);
            return Success("成功", model);
        }
        /// <summary>
        /// 删除上转门诊的信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("deleszmzs")]
        public async Task<IActionResult> deleSzmzs(string id)
        {
            var result = _ISzmzsRepository.Remove(id);
            if (result.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        ///获取上转门诊的信息
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        [HttpGet("getszmzs")]
        public async Task<IActionResult> GetSzmzs(int PageIndex, int PageSize, string stardate, string endtime, string zzdwname)
        {
            //排序字段
            var sort = Builders<Szmzs>.Sort.Descending("Szzydjsj");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<Szmzs>>();
            listApp.Add(Builders<Szmzs>.Filter.Eq("isdelete", 0));
           
            if (!string.IsNullOrWhiteSpace(stardate))
            {
                listApp.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
            }
            if (!string.IsNullOrWhiteSpace(endtime))
            {
                listApp.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
            }
            if (zzdwname != null)
            {
                var normallistAppor = new List<FilterDefinition<Szmzs>>();

                normallistAppor.Add(Builders<Szmzs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                normallistAppor.Add(Builders<Szmzs>.Filter.Where(p => p.Hzname.Contains(zzdwname)));
                listApp.Add(Builders<Szmzs>.Filter.Or(normallistAppor));
            }
            var filter = Builders<Szmzs>.Filter.And(listApp);

            var list = await _ISzmzsRepository.GetByPage(filter, PageIndex, PageSize, sort);
            var redCount = await _ISzmzsRepository.CountAsync(filter);   //获取总数
            var listmodel = new List<Szmzs>();
            foreach (var item in list)
            {
                var countlis = 0;
                //根据患者名称查找条数
                var listcounts = _ISzmzsRepository.Get(p => p.Hzname == item.Hzname);
                if (listcounts != null)
                {
                    if (listcounts.Result.Count() > 1)
                    {
                        countlis = 1;
                    }

                }
                ///定义选项
                var model = new Szmzs();
                model.id = item.id;
                model.Szzydjsj = item.Szzydjsj;//登记时间
                model.Szzyzzdw = item.Szzyzzdw;//转诊单位
                model.Szzyzzys = item.Szzyzzys;//转诊医师
                model.Yslxfs = item.Yslxfs;//联系方式
                model.Hzname = item.Hzname;//患者姓名
                model.HzGender = item.HzGender;//患者性别
                model.Hzage = item.Hzage;//患者年龄
                model.Hzzd = item.Hzzd;//患者诊断
                model.Hzjcxm = item.Hzjcxm;//检查项目
                model.Hzmzfy = item.Hzmzfy;//门诊费用
                model.Hzlxfs = item.Hzlxfs;//联系方式
                model.Syzzzzmd = item.Syzzzzmd;//转诊目的
                model.Syzysj = item.Syzysj;//住院时间
                model.Syzyfy = item.Syzyfy;//住院费用
                model.Syzzzrr = item.Syzzzrr;//责任人
                model.Syzzzzfw = item.Syzzzzfw;//诊中服务
                model.Syzzzrrs = item.Syzzzrrs;//责任人
                model.Syzzzrrss = item.Syzzzrrss;//责任人
                model.isred = countlis;//颜色状态
                model.isdelete = item.isdelete;//是否启用
                listmodel.Add(model);
            }
            var obj = new
            {
                rows = listmodel,
                total = redCount
            };
            return Success("成功", obj);
        }

        [HttpGet("exportexcelszmz")]
        [AllowAnonymous]
        public async Task<IActionResult> ExportExcelAszmz(string stardate, string endtime, string zzdwname)
        {
            try
            {
                //排序字段
                var sort = Builders<Szmzs>.Sort.Descending("Szzydjsj");
                //根据条件查询集合
                var listApp = new List<FilterDefinition<Szmzs>>();
                listApp.Add(Builders<Szmzs>.Filter.Eq("isdelete", 0));

                if (!string.IsNullOrWhiteSpace(stardate))
                {
                    listApp.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                }
                if (!string.IsNullOrWhiteSpace(endtime))
                {
                    listApp.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                }
                if (zzdwname != null)
                {
                    var normallistAppor = new List<FilterDefinition<Szmzs>>();

                    normallistAppor.Add(Builders<Szmzs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                    normallistAppor.Add(Builders<Szmzs>.Filter.Where(p => p.Hzname.Contains(zzdwname)));
                    listApp.Add(Builders<Szmzs>.Filter.Or(normallistAppor));
                }
                var filter = Builders<Szmzs>.Filter.And(listApp);
                var lists = await _ISzmzsRepository.Get(filter);

                List<Model.exportexcelszmz> list = new List<Model.exportexcelszmz>();
                foreach (var item in lists)
                {
                    Model.exportexcelszmz model = new Model.exportexcelszmz();
                    model.Szzydjsj = item.Szzydjsj != null ? Convert.ToDateTime(item.Szzydjsj).ToString("yyyy-MM-dd  HH:mm:ss") : ""; //登记时间
                    model.Szzyzzdw = item.Szzyzzdw;//转诊单位
                    model.Szzyzzys = item.Szzyzzys;//转诊医师
                    model.Yslxfs = item.Yslxfs;//联系方式
                    model.Hzname = item.Hzname;//患者姓名
                    model.HzGender = item.HzGender;//患者性别
                    model.Hzage = item.Hzage;//患者年龄
                    model.Hzzd = item.Hzzd;//患者诊断
                    model.Hzjcxm = item.Hzjcxm;//检查项目
                    model.Hzmzfy = item.Hzmzfy;//门诊费用
                    model.Hzlxfs = item.Hzlxfs;//联系方式
                    model.Syzzzzmd = item.Syzzzzmd;//转诊目的
                    model.Syzysj = Convert.ToDateTime(item.Syzysj).ToString("yyyy-MM-dd  HH:mm:ss");//住院时间
                    model.Syzyfy = item.Syzyfy;//住院费用
                    model.Syzzzrr = item.Syzzzrr;//责任人
                    model.Syzzzzfw = item.Syzzzzfw;//诊中服务
                    model.Syzzzrrs = item.Syzzzrrs;//责任人
                    list.Add(model);
                }
                NPOIHelper npoi = new NPOIHelper();
                var tb = ModelConvertHelper<Szzys>.ListToTable(list, null);
                if (tb != null)
                {
                    //导出dtnew 
                    string[] cols = { "登记时间", "转诊单位", "转诊医师", "联系方式", "患者姓名", "患者性别", "患者年龄", "患者诊断", "检查项目","门诊费用", "联系方式", "转诊目的", "住院时间", "住院费用", "责任人", "诊中服务", "责任人" };

                    byte[] sm = npoi.ExportToExcel1(tb, cols);
                    if (sm != null)
                    {
                        return File(sm, "application/vnd.ms-excel", "上转门诊.xlsx");
                    }
                    else
                    {
                        return Error("导出失败");
                    }
                }
                return Error("没有数据");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "导出异常");
                return Error("导出失败");
            }
        }


        #endregion
        #region 上转检查
        /// <summary>
        /// 添加上转检查信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("addszjcs")]
        public async Task<IActionResult> AddSzjcs(Model.Dto.Szjcs tps)
        {
            Szjcs tp = new Szjcs();
            tp.Szzydjsj = DateTime.Now;// tps.Szzydjsj;//登记时间
            tp.Szzyzzdw = tps.Szzyzzdw;//转诊单位
            tp.Szzyzzys = tps.Szzyzzys;//转诊医师
            tp.Yslxfs = tps.Yslxfs;//联系方式
            tp.Hzname = tps.Hzname;//患者姓名
            tp.HzGender = tps.HzGender;//患者性别
            tp.Hzage = tps.Hzage;//患者年龄
            tp.Hzjcxm = tps.Hzjcxm;//检查项目
            tp.Hzjcfy = tps.Hzjcfy;//检查费用
            tp.Hzlxfs = tps.Hzlxfs;//联系方式
            tp.Syzzzrr = tps.Syzzzrr;//责任人
            tp.isdelete = 0;
            var a = _ISzjcsRepository.AddRetID(tp);
            if (a.Result != null || a.Result != "")
            {
                var hzinfomodel = await _IHzjbxxRepository.GetSingle(p => p.Hzname == tps.Hzname && p.HzGender == tps.HzGender && p.Hzage == tps.Hzage  && p.Hzlxfs == tps.Hzlxfs);
                if (hzinfomodel == null)
                {
                    //患者信息
                    Hzjbxx hzinfomode = new Hzjbxx();
                    hzinfomode.Hzname = tps.Hzname;//患者姓名
                    hzinfomode.HzGender = tps.HzGender;//患者性别
                    hzinfomode.Hzage = tps.Hzage;//患者年龄
                    hzinfomode.Hzzd = "";// tps.Hzzd;//患者诊断
                                               //hzinfomode.Hzzyks = tps.Hzzyks;//住院科室
                                               //hzinfomode.Hzzyh = tps.Hzzyh;//住院号
                    hzinfomode.Hzlxfs = tps.Hzlxfs;//联系方式
                    hzinfomode.isdelete = 0;//是否删除

                    var hzintoid = _IHzjbxxRepository.AddRetID(hzinfomode);
                }

                return Success("成功", a.Result);
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 修改上转检查信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("upszjcs")]
        public async Task<IActionResult> upSzjcs(Model.Dto.Szjcs tps)
        {
            Szjcs tp = await _ISzjcsRepository.GetSingle(p => p.id == tps.id);
            tp.id = tps.id;
            tp.Szzydjsj = tps.Szzydjsj;//登记时间
            tp.Szzyzzdw = tps.Szzyzzdw;//转诊单位
            tp.Szzyzzys = tps.Szzyzzys;//转诊医师
            tp.Yslxfs = tps.Yslxfs;//联系方式
            tp.Hzname = tps.Hzname;//患者姓名
            tp.HzGender = tps.HzGender;//患者性别
            tp.Hzage = tps.Hzage;//患者年龄
            tp.Hzjcxm = tps.Hzjcxm;//检查项目
            tp.Hzjcfy = tps.Hzjcfy;//检查费用
            tp.Hzlxfs = tps.Hzlxfs;//联系方式
            tp.Syzzzrr = tps.Syzzzrr;//责任人
            tp.isdelete = 0;
            var resua = _ISzjcsRepository.Update(tp);
            if (resua.Result)
            {
                var szzysmode = await _ISzjcsRepository.GetSingle(p => p.id == tps.id);
                var hzinfomodel = await _IHzjbxxRepository.GetSingle(p => p.Hzname == szzysmode.Hzname && p.HzGender == szzysmode.HzGender && p.Hzage == szzysmode.Hzage && p.Hzzd == "" && p.Hzlxfs == szzysmode.Hzlxfs);
                if (hzinfomodel != null)
                {
                    //患者信息
                    Hzjbxx hzinfomode = new Hzjbxx();
                    hzinfomode.Hzname = tps.Hzname;//患者姓名
                    hzinfomode.HzGender = tps.HzGender;//患者性别
                    hzinfomode.Hzage = tps.Hzage;//患者年龄
                    hzinfomode.Hzzd = ""; // tps.Hzzd;//患者诊断
                                               //hzinfomode.Hzzyks = tps.Hzzyks;//住院科室
                                               //hzinfomode.Hzzyh = tps.Hzzyh;//住院号
                    hzinfomode.Hzlxfs = tps.Hzlxfs;//联系方式
                    hzinfomode.isdelete = 0;//是否删除

                    var hzintoid = _IHzjbxxRepository.Update(hzinfomode);
                }
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 根据id获取上转检查信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("getszjcsbyid")]
        public async Task<IActionResult> GetSzjcsbyid(string id)
        {
            var model = await _ISzjcsRepository.Get(p => p.id == id);
            return Success("成功", model);
        }
        /// <summary>
        /// 根据id删除上转检查信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("deleszjcs")]
        public async Task<IActionResult> deleSzjcs(string id)
        {
            var result = _ISzjcsRepository.Remove(id);
            if (result.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 获取上转检查信息
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        [HttpGet("getszjcs")]
        public async Task<IActionResult> GetSzjcs(int PageIndex, int PageSize, string stardate, string endtime, string zzdwname)
        {
            //排序字段
            var sort = Builders<Szjcs>.Sort.Descending("Szzydjsj");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<Szjcs>>();
            listApp.Add(Builders<Szjcs>.Filter.Eq("isdelete", 0));
            if (!string.IsNullOrWhiteSpace(stardate))
            {
                listApp.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
            }
            if (!string.IsNullOrWhiteSpace(endtime))
            {
                listApp.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
            }
            if (zzdwname != null)
            {
                var normallistAppor = new List<FilterDefinition<Szjcs>>();

                normallistAppor.Add(Builders<Szjcs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                normallistAppor.Add(Builders<Szjcs>.Filter.Where(p => p.Hzname.Contains(zzdwname)));
                listApp.Add(Builders<Szjcs>.Filter.Or(normallistAppor));
            }
            var filter = Builders<Szjcs>.Filter.And(listApp);

            var list = await _ISzjcsRepository.GetByPage(filter, PageIndex, PageSize, sort);
            var redCount = await _ISzjcsRepository.CountAsync(filter);   //获取总数
            var listmodel = new List<Szjcs>();
            foreach (var item in list)
            {
                var countlis = 0;
                //根据患者名称查找条数
                var listcounts = _ISzjcsRepository.Get(p => p.Hzname == item.Hzname);
                if (listcounts != null)
                {
                    if (listcounts.Result.Count() > 1)
                    {
                        countlis = 1;
                    }

                }
                ///定义选项
                var model = new Szjcs();
                model.id = item.id;
                model.Szzydjsj = item.Szzydjsj;//登记时间
                model.Szzyzzdw = item.Szzyzzdw;//转诊单位
                model.Szzyzzys = item.Szzyzzys;//转诊医师
                model.Yslxfs = item.Yslxfs;//联系方式
                model.Hzname = item.Hzname;//患者姓名
                model.HzGender = item.HzGender;//患者性别
                model.Hzage = item.Hzage;//患者年龄
                model.Hzjcxm = item.Hzjcxm;//检查项目
                model.Hzjcfy = item.Hzjcfy;//检查费用
                model.Hzlxfs = item.Hzlxfs;//联系方式
                model.Syzzzrr = item.Syzzzrr;//责任人
                model.isred = countlis;  //颜色
                model.isdelete = item.isdelete;//是否启用
                listmodel.Add(model);
            }
            var obj = new
            {
                rows = listmodel,
                total = redCount
            };
            return Success("成功", obj);
        }

        /// <summary>
        /// 上转检查导出
        /// </summary>
        /// <returns></returns>
        [HttpGet("exportexcelszjc")]
        [AllowAnonymous]
        public async Task<IActionResult> exportexcelszjc(string stardate, string endtime, string zzdwname)
        {
            try
            {
                //排序字段
                var sort = Builders<Szjcs>.Sort.Descending("Szzydjsj");
                //根据条件查询集合
                var listApp = new List<FilterDefinition<Szjcs>>();
                listApp.Add(Builders<Szjcs>.Filter.Eq("isdelete", 0));
                if (!string.IsNullOrWhiteSpace(stardate))
                {
                    listApp.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                }
                if (!string.IsNullOrWhiteSpace(endtime))
                {
                    listApp.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                }
                if (zzdwname != null)
                {
                    var normallistAppor = new List<FilterDefinition<Szjcs>>();

                    normallistAppor.Add(Builders<Szjcs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                    normallistAppor.Add(Builders<Szjcs>.Filter.Where(p => p.Hzname.Contains(zzdwname)));
                    listApp.Add(Builders<Szjcs>.Filter.Or(normallistAppor));
                }
                var filter = Builders<Szjcs>.Filter.And(listApp);
                var lists = await _ISzjcsRepository.Get(filter);


                List<Model.exportexcelszjc> list = new List<Model.exportexcelszjc>();
                foreach (var item in lists)
                {
                  
                    Model.exportexcelszjc model = new Model.exportexcelszjc();
                    model.Szzydjsj = item.Szzydjsj!=null ? Convert.ToDateTime(item.Szzydjsj).ToString("yyyy-MM-dd  HH:mm:ss") : "";//item.Szzydjsj.ToString("yyyy-MM-dd  hh:mm:ss") ;//登记时间sf.Format()
                    model.Szzyzzdw = item.Szzyzzdw;//转诊单位
                    model.Szzyzzys = item.Szzyzzys;//转诊医师
                    model.Yslxfs = item.Yslxfs;//联系方式
                    model.Hzname = item.Hzname;//患者姓名
                    model.HzGender = item.HzGender;//患者性别
                    model.Hzage = item.Hzage;//患者年龄
                    model.Hzjcxm = item.Hzjcxm;//检查项目
                    model.Hzjcfy = item.Hzjcfy;//检查费用
                    model.Hzlxfs = item.Hzlxfs;//联系方式
                    model.Syzzzrr = item.Syzzzrr;//责任人
                    list.Add(model);
                }
                NPOIHelper npoi = new NPOIHelper();
                var tb = ModelConvertHelper<Szzys>.ListToTable(list, null);
                if (tb != null)
                {
                    //导出dtnew 
                    string[] cols = { "登记时间", "转诊单位", "转诊医师", "联系方式", "患者姓名", "患者性别", "患者年龄", "检查项目", "检查费用", "联系方式", "责任人" };

                    byte[] sm = npoi.ExportToExcel1(tb, cols);
                    if (sm != null)
                    {
                        return File(sm, "application/vnd.ms-excel", "上转检查.xlsx");
                    }
                    else
                    {
                        return Error("导出失败");
                    }
                }
                return Error("没有数据");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "导出异常");
                return Error("导出失败");
            }
        }

        #endregion
        #region 下转治疗
        /// <summary>
        /// 添加下转治疗信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("addxzzls")]
        public async Task<IActionResult> AddXzzls(Model.Dto.Xzzls tps)
        {
            Xzzls tp = new Xzzls();
            tp.Szzydjsj = DateTime.Now;// tps.Szzydjsj;//登记时间
            tp.Szzyzzdw = tps.Szzyzzdw;//转诊单位
            tp.Szzyzzys = tps.Szzyzzys;//转诊医师
            tp.Yslxfs = tps.Yslxfs;//联系方式
            tp.Hzname = tps.Hzname;//患者姓名
            tp.HzGender = tps.HzGender;//患者性别
            tp.Hzage = tps.Hzage;//患者年龄
            tp.Hzjcxm = tps.Hzjcxm;//检查项目
            tp.Hzlxfs = tps.Hzlxfs;//联系方式
            tp.Syzzzzmd = "下转治疗";// tps.Syzzzzmd;//转诊目的
            tp.Xyjzsj = tps.Xyjzsj;//就诊时间
            tp.Syzzzrr = tps.Syzzzrr;//责任人
            tp.isdelete = 0;
            var a = _IXzzlsRepository.AddRetID(tp);
            if (a.Result != null || a.Result != "")
            {
                var hzinfomodel = await _IHzjbxxRepository.GetSingle(p => p.Hzname == tps.Hzname && p.HzGender == tps.HzGender && p.Hzage == tps.Hzage && p.Hzzd == tps.Hzzd && p.Hzlxfs == tps.Hzlxfs);
                if (hzinfomodel == null)
                {
                    //患者信息
                    Hzjbxx hzinfomode = new Hzjbxx();
                    hzinfomode.Hzname = tps.Hzname;//患者姓名
                    hzinfomode.HzGender = tps.HzGender;//患者性别
                    hzinfomode.Hzage = tps.Hzage;//患者年龄
                    //hzinfomode.Hzzd = tps.Hzzd;//患者诊断
                                               //hzinfomode.Hzzyks = tps.Hzzyks;//住院科室
                                               //hzinfomode.Hzzyh = tps.Hzzyh;//住院号
                    hzinfomode.Hzlxfs = tps.Hzlxfs;//联系方式
                    hzinfomode.isdelete = 0;//是否删除

                    var hzintoid = _IHzjbxxRepository.AddRetID(hzinfomode);
                }
                return Success("成功", a.Result);
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 修改下转治疗信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("upxzzls")]
        public async Task<IActionResult> upXzzls(Model.Dto.Xzzls tps)
        {
            Xzzls tp = await _IXzzlsRepository.GetSingle(p=>p.id==tps.id);
            tp.id = tps.id;
            tp.Szzydjsj = tps.Szzydjsj;//登记时间
            tp.Szzyzzdw = tps.Szzyzzdw;//转诊单位
            tp.Szzyzzys = tps.Szzyzzys;//转诊医师
            tp.Yslxfs = tps.Yslxfs;//联系方式
            tp.Hzname = tps.Hzname;//患者姓名
            tp.HzGender = tps.HzGender;//患者性别
            tp.Hzage = tps.Hzage;//患者年龄
            tp.Hzjcxm = tps.Hzjcxm;//检查项目
            tp.Hzlxfs = tps.Hzlxfs;//联系方式
           // tp.Syzzzzmd = tps.Syzzzzmd;//转诊目的
            tp.Xyjzsj = tps.Xyjzsj;//就诊时间
            tp.Syzzzrr = tps.Syzzzrr;//责任人
            tp.isdelete = 0;
            var resua = _IXzzlsRepository.Update(tp);
            if (resua.Result)
            {
                var szzysmode = await _IXzzlsRepository.GetSingle(p => p.id == tps.id);
                var hzinfomodel = await _IHzjbxxRepository.GetSingle(p => p.Hzname == szzysmode.Hzname && p.HzGender == szzysmode.HzGender && p.Hzage == szzysmode.Hzage && p.Hzzd == szzysmode.Hzzd && p.Hzlxfs == szzysmode.Hzlxfs);
                if (hzinfomodel != null)
                {
                    //患者信息
                    Hzjbxx hzinfomode = new Hzjbxx();
                    hzinfomode.id = hzinfomodel.id;
                    hzinfomode.Hzname = tps.Hzname;//患者姓名
                    hzinfomode.HzGender = tps.HzGender;//患者性别
                    hzinfomode.Hzage = tps.Hzage;//患者年龄
                   // hzinfomode.Hzzd = ""; // tps.Hzzd;//患者诊断
                                          //hzinfomode.Hzzyks = tps.Hzzyks;//住院科室
                                          //hzinfomode.Hzzyh = tps.Hzzyh;//住院号
                    hzinfomode.Hzlxfs = tps.Hzlxfs;//联系方式
                    hzinfomode.isdelete = 0;//是否删除

                    var hzintoid = _IHzjbxxRepository.Update(hzinfomode);
                }
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 根据编号id获取下转治疗的信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("getxzzlsbyid")]
        public async Task<IActionResult> GetXzzlsbyid(string id)
        {
            var model = await _IXzzlsRepository.Get(p => p.id == id);
            return Success("成功", model);
        }
        /// <summary>
        /// 根据编号删除下转治疗的信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("delexzzls")]
        public async Task<IActionResult> deleXzzls(string id)
        {
            var result = _IXzzlsRepository.Remove(id);
            if (result.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 获取下转治疗的信息
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        [HttpGet("getxzzls")]
        public async Task<IActionResult> GetXzzls(int PageIndex, int PageSize, string stardate, string endtime, string zzdwname)
        {
            //排序字段
            var sort = Builders<Xzzls>.Sort.Descending("Szzydjsj");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<Xzzls>>();
            listApp.Add(Builders<Xzzls>.Filter.Eq("isdelete", 0));
            if (!string.IsNullOrWhiteSpace(stardate))
            {
                listApp.Add(Builders<Xzzls>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
            }
            if (!string.IsNullOrWhiteSpace(endtime))
            {
                listApp.Add(Builders<Xzzls>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
            }
            if (zzdwname != null)
            {
                var normallistAppor = new List<FilterDefinition<Xzzls>>();

                normallistAppor.Add(Builders<Xzzls>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                normallistAppor.Add(Builders<Xzzls>.Filter.Where(p => p.Hzname.Contains(zzdwname)));
                listApp.Add(Builders<Xzzls>.Filter.Or(normallistAppor));
            }
            var filter = Builders<Xzzls>.Filter.And(listApp);

            var list = await _IXzzlsRepository.GetByPage(filter, PageIndex, PageSize, sort);
            var redCount = await _IXzzlsRepository.CountAsync(filter);   //获取总数
            var listmodel = new List<Xzzls>();
            foreach (var item in list)
            {
                var countlis = 0;
                //根据患者名称查找条数
                var listcounts = _IXzzlsRepository.Get(p => p.Hzname == item.Hzname);
                if (listcounts != null)
                {
                    if (listcounts.Result.Count() > 1)
                    {
                        countlis = 1;
                    }

                }
                ///定义选项
                var model = new Xzzls();
                model.id = item.id;
                model.Szzydjsj = item.Szzydjsj;//登记时间
                model.Szzyzzdw = item.Szzyzzdw;//转诊单位
                model.Szzyzzys = item.Szzyzzys;//转诊医师
                model.Yslxfs = item.Yslxfs;//联系方式
                model.Hzname = item.Hzname;//患者姓名
                model.HzGender = item.HzGender;//患者性别
                model.Hzage = item.Hzage;//患者年龄
                model.Hzjcxm = item.Hzjcxm;//检查项目
                model.Hzlxfs = item.Hzlxfs;//联系方式
                model.Syzzzzmd = item.Syzzzzmd;//转诊目的
                model.Xyjzsj = item.Xyjzsj;//就诊时间
                model.Syzzzrr = item.Syzzzrr;//责任人
                model.isred = countlis;//颜色
                model.isdelete = item.isdelete;//是否启用
                listmodel.Add(model);
            }
            var obj = new
            {
                rows = listmodel,
                total = redCount
            };
            return Success("成功", obj);
        }
        /// <summary>
        /// 导出下转治疗
        /// </summary>
        /// <param name="stardate"></param>
        /// <param name="endtime"></param>
        /// <param name="zzdwname"></param>
        /// <returns></returns>
        [HttpGet("exportexcelxzzl")]
        [AllowAnonymous]
        public async Task<IActionResult> exportexcelxzzl(string stardate, string endtime, string zzdwname)
        {
            try
            {
                //排序字段
                var sort = Builders<Xzzls>.Sort.Descending("Szzydjsj");
                //根据条件查询集合
                var listApp = new List<FilterDefinition<Xzzls>>();
                listApp.Add(Builders<Xzzls>.Filter.Eq("isdelete", 0));
                if (!string.IsNullOrWhiteSpace(stardate))
                {
                    listApp.Add(Builders<Xzzls>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                }
                if (!string.IsNullOrWhiteSpace(endtime))
                {
                    listApp.Add(Builders<Xzzls>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                }
                if (zzdwname != null)
                {
                    var normallistAppor = new List<FilterDefinition<Xzzls>>();

                    normallistAppor.Add(Builders<Xzzls>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                    normallistAppor.Add(Builders<Xzzls>.Filter.Where(p => p.Hzname.Contains(zzdwname)));
                    listApp.Add(Builders<Xzzls>.Filter.Or(normallistAppor));
                }
                var filter = Builders<Xzzls>.Filter.And(listApp);
                var lists = await _IXzzlsRepository.Get(filter);

                List<Model.exportexcelxxzl> list = new List<Model.exportexcelxxzl>();
                foreach (var item in lists)
                {

                    Model.exportexcelxxzl model = new Model.exportexcelxxzl();
                    model.Szzydjsj = item.Szzydjsj != null ? Convert.ToDateTime(item.Szzydjsj).ToString("yyyy-MM-dd  HH:mm:ss") : "";//item.Szzydjsj.ToString("yyyy-MM-dd  hh:mm:ss") ;//登记时间sf.Format()
                
                    model.Szzyzzdw = item.Szzyzzdw;//转诊单位
                    model.Szzyzzys = item.Szzyzzys;//转诊医师
                    model.Yslxfs = item.Yslxfs;//联系方式
                    model.Hzname = item.Hzname;//患者姓名
                    model.HzGender = item.HzGender;//患者性别
                    model.Hzage = item.Hzage;//患者年龄
                    model.Hzjcxm = item.Hzjcxm;//检查项目
                    model.Hzlxfs = item.Hzlxfs;//联系方式
                    model.Syzzzzmd = item.Syzzzzmd;//转诊目的
                    model.Xyjzsj = item.Xyjzsj!=null ? Convert.ToDateTime(item.Xyjzsj).ToString("yyyy-MM-dd  HH:mm:ss") : "";//就诊时间
                    model.Syzzzrr = item.Syzzzrr;//责任人
                    list.Add(model);
                }
                NPOIHelper npoi = new NPOIHelper();
                var tb = ModelConvertHelper<Szzys>.ListToTable(list, null);
                if (tb != null)
                {
                    //导出dtnew 
                    string[] cols = { "登记时间", "转诊单位", "转诊医师", "联系方式", "患者姓名", "患者性别", "患者年龄", "检查项目",  "联系方式", "转诊目的","就诊时间", "责任人" };

                    byte[] sm = npoi.ExportToExcel1(tb, cols);
                    if (sm != null)
                    {
                        return File(sm, "application/vnd.ms-excel", "下转治疗.xlsx");
                    }
                    else
                    {
                        return Error("导出失败");
                    }
                }
                return Error("没有数据");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "导出异常");
                return Error("导出失败");
            }

        }

        #endregion
        #region 下转住院
        /// <summary>
        /// 添加下转住院信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("addxzzys")]
        public async Task<IActionResult> AddXzzys(Model.Dto.Xzzys tps)
        {
            Xzzys tp = new Xzzys();
            tp.Szzydjsj = DateTime.Now;//tps.Szzydjsj;//登记时间
            tp.Szzyzzdw = tps.Szzyzzdw;//转诊单位
            tp.Szzyzzys = tps.Szzyzzys;//转诊医师
            tp.Yslxfs = tps.Yslxfs;//联系方式
            tp.Hzname = tps.Hzname;//患者姓名
            tp.HzGender = tps.HzGender;//患者性别
            tp.Hzage = tps.Hzage;//患者年龄
            tp.Hzzd = tps.Hzzd;//诊断
            tp.Hzjcxm = tps.Hzjcxm;//检查项目
            tp.Hzlxfs = tps.Hzlxfs;//联系方式
            tp.Syzzzzmd = "下转住院";// tps.Syzzzzmd;//转诊目的
            tp.Syzysj = tps.Syzysj;//住院时间
            tp.Syzqtx = tps.Syzqtx;//诊前提醒
            tp.Syzzzrr = tps.Syzzzrr;//责任人
            tp.Syzzzzfw = tps.Syzzzzfw;//诊中服务
            tp.Syzzzrrs = tps.Syzzzrrs;//责任人
            tp.Syzzzhzzfw = tps.Syzzzhzzfw;//诊后追踪服务
            tp.Syzzzrrss = tps.Syzzzrrss;//责任人
            tp.isdelete = 0;
            var a = _IXzzysRepository.AddRetID(tp);
            if (a.Result != null || a.Result != "")
            {
                var hzinfomodel = await _IHzjbxxRepository.GetSingle(p => p.Hzname == tps.Hzname && p.HzGender == tps.HzGender && p.Hzage == tps.Hzage && p.Hzzd == tps.Hzzd && p.Hzlxfs == tps.Hzlxfs);
                if (hzinfomodel == null)
                {
                    //患者信息
                    Hzjbxx hzinfomode = new Hzjbxx();
                    hzinfomode.Hzname = tps.Hzname;//患者姓名
                    hzinfomode.HzGender = tps.HzGender;//患者性别
                    hzinfomode.Hzage = tps.Hzage;//患者年龄
                    hzinfomode.Hzzd = tps.Hzzd;//患者诊断
                                               //hzinfomode.Hzzyks = tps.Hzzyks;//住院科室
                                               //hzinfomode.Hzzyh = tps.Hzzyh;//住院号
                    hzinfomode.Hzlxfs = tps.Hzlxfs;//联系方式
                    hzinfomode.isdelete = 0;//是否删除

                    var hzintoid = _IHzjbxxRepository.AddRetID(hzinfomode);
                }
                return Success("成功", a.Result);
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 修改下转住院信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("upxzzys")]
        public async Task<IActionResult> upXzzys(Model.Dto.Xzzys tps)
        {
            Xzzys tp = await _IXzzysRepository.GetSingle(p=>p.id==tps.id);
            tp.id = tps.id;
            tp.Szzydjsj = tps.Szzydjsj;//登记时间
            tp.Szzyzzdw = tps.Szzyzzdw;//转诊单位
            tp.Szzyzzys = tps.Szzyzzys;//转诊医师
            tp.Yslxfs = tps.Yslxfs;//联系方式
            tp.Hzname = tps.Hzname;//患者姓名
            tp.HzGender = tps.HzGender;//患者性别
            tp.Hzage = tps.Hzage;//患者年龄
            tp.Hzzd = tps.Hzzd;//诊断
            tp.Hzjcxm = tps.Hzjcxm;//检查项目
            tp.Hzlxfs = tps.Hzlxfs;//联系方式
            //tp.Syzzzzmd = tps.Syzzzzmd;//转诊目的
            tp.Syzysj = tps.Syzysj;//住院时间
            tp.Syzqtx = tps.Syzqtx;//诊前提醒
            tp.Syzzzrr = tps.Syzzzrr;//责任人
            tp.Syzzzzfw = tps.Syzzzzfw;//诊中服务
            tp.Syzzzrrs = tps.Syzzzrrs;//责任人
            tp.Syzzzhzzfw = tps.Syzzzhzzfw;//诊后追踪服务
            tp.Syzzzrrss = tps.Syzzzrrss;//责任人
            tp.isdelete = 0;
            var resua = _IXzzysRepository.Update(tp);
            if (resua.Result)
            {
                var szzysmode = await _IXzzysRepository.GetSingle(p => p.id == tps.id);
                var hzinfomodel = await _IHzjbxxRepository.GetSingle(p => p.Hzname == szzysmode.Hzname && p.HzGender == szzysmode.HzGender && p.Hzage == szzysmode.Hzage && p.Hzzd == szzysmode.Hzzd && p.Hzlxfs == szzysmode.Hzlxfs);
                if (hzinfomodel != null)
                {
                    //患者信息
                    Hzjbxx hzinfomode = new Hzjbxx();
                    hzinfomode.id = hzinfomodel.id;
                    hzinfomode.Hzname = tps.Hzname;//患者姓名
                    hzinfomode.HzGender = tps.HzGender;//患者性别
                    hzinfomode.Hzage = tps.Hzage;//患者年龄
                    //hzinfomode.Hzzd = ""; // tps.Hzzd;//患者诊断
                                          //hzinfomode.Hzzyks = tps.Hzzyks;//住院科室
                                          //hzinfomode.Hzzyh = tps.Hzzyh;//住院号
                    hzinfomode.Hzlxfs = tps.Hzlxfs;//联系方式
                    hzinfomode.isdelete = 0;//是否删除

                    var hzintoid = _IHzjbxxRepository.Update(hzinfomode);
                }
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 根据编号id获取下转住院信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("getxzzysbyid")]
        public async Task<IActionResult> GetXzzysbyid(string id)
        {
            var model = await _IXzzysRepository.Get(p => p.id == id);
            return Success("成功", model);
        }
        /// <summary>
        /// 删除下转住院信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("delexzzys")]
        public async Task<IActionResult> deleXzzys(string id)
        {
            var result = _IXzzysRepository.Remove(id);
            if (result.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 获取下转住院信息
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        [HttpGet("getxzzys")]
        public async Task<IActionResult> GetXzzys(int PageIndex, int PageSize, string stardate, string endtime, string zzdwname)
        {
            //排序字段
            var sort = Builders<Xzzys>.Sort.Descending("Szzydjsj");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<Xzzys>>();
            listApp.Add(Builders<Xzzys>.Filter.Eq("isdelete", 0));
            if (!string.IsNullOrWhiteSpace(stardate))
            {
                listApp.Add(Builders<Xzzys>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
            }
            if (!string.IsNullOrWhiteSpace(endtime))
            {
                listApp.Add(Builders<Xzzys>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
            }
            if (zzdwname != null)
            {
                var normallistAppor = new List<FilterDefinition<Xzzys>>();

                normallistAppor.Add(Builders<Xzzys>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                normallistAppor.Add(Builders<Xzzys>.Filter.Where(p => p.Hzname.Contains(zzdwname)));
                listApp.Add(Builders<Xzzys>.Filter.Or(normallistAppor));
            }
            var filter = Builders<Xzzys>.Filter.And(listApp);

            var list = await _IXzzysRepository.GetByPage(filter, PageIndex, PageSize, sort);
            var redCount = await _IXzzysRepository.CountAsync(filter);   //获取总数
            var listmodel = new List<Xzzys>();
            foreach (var item in list)
            {
                var countlis = 0;
                //根据患者名称查找条数
                var listcounts = _ISzzysRepository.Get(p => p.Hzname == item.Hzname);
                if (listcounts != null)
                {
                    if (listcounts.Result.Count() > 1)
                    {
                        countlis = 1;
                    }

                }
                ///定义选项
                var model = new Xzzys();
                model.id = item.id;
                model.Szzydjsj = item.Szzydjsj;//登记时间
                model.Szzyzzdw = item.Szzyzzdw;//转诊单位
                model.Szzyzzys = item.Szzyzzys;//转诊医师
                model.Yslxfs = item.Yslxfs;//联系方式
                model.Hzname = item.Hzname;//患者姓名
                model.HzGender = item.HzGender;//患者性别
                model.Hzage = item.Hzage;//患者年龄
                model.Hzzd = item.Hzzd;//诊断
                model.Hzjcxm = item.Hzjcxm;//检查项目
                model.Hzlxfs = item.Hzlxfs;//联系方式
                model.Syzzzzmd = item.Syzzzzmd;//转诊目的
                model.Syzysj = item.Syzysj;//住院时间
                model.Syzqtx = item.Syzqtx;//诊前提醒
                model.Syzzzrr = item.Syzzzrr;//责任人
                model.Syzzzzfw = item.Syzzzzfw;//诊中服务
                model.Syzzzrrs = item.Syzzzrrs;//责任人
                model.Syzzzhzzfw = item.Syzzzhzzfw;//诊后追踪服务
                model.isred = countlis;//颜色
                model.Syzzzrrss = item.Syzzzrrss;//责任人

                model.isdelete = 0;
                listmodel.Add(model);
            }
            var obj = new
            {
                rows = listmodel,
                total = redCount
            };
            return Success("成功", obj);
        }
        /// <summary>
        /// 导出下转住院
        /// </summary>
        /// <param name="stardate"></param>
        /// <param name="endtime"></param>
        /// <param name="zzdwname"></param>
        /// <returns></returns>
        [HttpGet("exportexcelxzzy")]
        [AllowAnonymous]
        public async Task<IActionResult> exportexcelxzzy(string stardate, string endtime, string zzdwname)
        {
            try
            {
                //排序字段
                var sort = Builders<Xzzys>.Sort.Descending("Szzydjsj");
                //根据条件查询集合
                var listApp = new List<FilterDefinition<Xzzys>>();
                listApp.Add(Builders<Xzzys>.Filter.Eq("isdelete", 0));
                if (!string.IsNullOrWhiteSpace(stardate))
                {
                    listApp.Add(Builders<Xzzys>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                }
                if (!string.IsNullOrWhiteSpace(endtime))
                {
                    listApp.Add(Builders<Xzzys>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                }
                if (zzdwname != null)
                {
                    var normallistAppor = new List<FilterDefinition<Xzzys>>();

                    normallistAppor.Add(Builders<Xzzys>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                    normallistAppor.Add(Builders<Xzzys>.Filter.Where(p => p.Hzname.Contains(zzdwname)));
                    listApp.Add(Builders<Xzzys>.Filter.Or(normallistAppor));
                }
                var filter = Builders<Xzzys>.Filter.And(listApp);

                var lists = await _IXzzysRepository.Get(filter);
                List<Model.exportexcelxzzy> list = new List<Model.exportexcelxzzy>();
                foreach (var item in lists)
                {

                    Model.exportexcelxzzy model = new Model.exportexcelxzzy();
                    model.Szzydjsj = item.Szzydjsj != null ? Convert.ToDateTime(item.Szzydjsj).ToString("yyyy-MM-dd  HH:mm:ss") : "";//登记时间sf.Format()
                    model.Szzyzzdw = item.Szzyzzdw;//转诊单位
                    model.Szzyzzys = item.Szzyzzys;//转诊医师
                    model.Yslxfs = item.Yslxfs;//联系方式
                    model.Hzname = item.Hzname;//患者姓名
                    model.HzGender = item.HzGender;//患者性别
                    model.Hzage = item.Hzage;//患者年龄
                    model.Hzzd = item.Hzzd;//诊断
                    model.Hzjcxm = item.Hzjcxm;//检查项目
                    model.Hzlxfs = item.Hzlxfs;//联系方式
                    model.Syzzzzmd = item.Syzzzzmd;//转诊目的
                    model.Syzysj = item.Syzysj != null ? Convert.ToDateTime(item.Syzysj).ToString("yyyy-MM-dd  HH:mm:ss") : "" ;//住院时间
                    model.Syzqtx = item.Syzqtx;//诊前提醒
                    model.Syzzzrr = item.Syzzzrr;//责任人
                    model.Syzzzzfw = item.Syzzzzfw;//诊中服务
                    model.Syzzzrrs = item.Syzzzrrs;//责任人
                    model.Syzzzhzzfw = item.Syzzzhzzfw;//诊后追踪服务

                    list.Add(model);
                }
                NPOIHelper npoi = new NPOIHelper();
                var tb = ModelConvertHelper<Szzys>.ListToTable(list, null);
                if (tb != null)
                {
                    //导出dtnew 
                    string[] cols = { "登记时间", "转诊单位", "转诊医师", "联系方式", "患者姓名", "患者性别", "患者年龄","诊断", "检查项目", "联系方式", "转诊目的", "住院时间", "诊前提醒",  "责任人" ,"诊中服务", "责任人" , "诊后追踪服务" };

                    byte[] sm = npoi.ExportToExcel1(tb, cols);
                    if (sm != null)
                    {
                        return File(sm, "application/vnd.ms-excel", "下转住院.xlsx");
                    }
                    else
                    {
                        return Error("导出失败");
                    }
                }
                return Error("没有数据");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "导出异常");
                return Error("导出失败");
            }
        }
        #endregion
        #region 急危重抢救
        /// <summary>
        /// 添加信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("addwjzqjs")]
        public async Task<IActionResult> AddWjzqjs(Model.Dto.Wjzqjs tps)
        {
            Wjzqjs tp = new Wjzqjs();
            tp.Szzydjsj = DateTime.Now;//tps.Szzydjsj;//登记时间
            tp.Szzyzzdw = tps.Szzyzzdw;//转诊单位
            tp.Yslxfs = tps.Yslxfs;//联系方式
            tp.Hzname = tps.Hzname;//患者姓名
            tp.HzGender = tps.HzGender;//患者性别
            tp.Hzage = tps.Hzage;//患者年龄
            tp.Hzzd = tps.Hzzd;//诊断
            tp.Hzjzks = tps.Hzjzks;//接诊科室
            tp.Hzzyh = tps.Hzzyh;//住院号
            tp.Syccsj = tps.Syccsj;//出车时间
            tp.Syjhsj = tps.Syjhsj;//接回时间
            tp.Syzzzrr = tps.Syzzzrr;//责任人
            tp.Sycysj = tps.Sycysj;//出院时间
            tp.Syzzzyfy = tps.Syzzzyfy;//住院费用
            tp.Syzzzhzzfw = tps.Syzzzhzzfw;//诊后追踪服务
            tp.Syzzfkszys = tps.Syzzfkszys;//反馈首诊医师
            tp.Syzzzrrss = tps.Syzzzrrss;//责任人
            tp.isdelete = 0;
            var a = _IWjzqjsRepository.AddRetID(tp);
            if (a.Result != null || a.Result != "")
            {
                var hzinfomodel = await _IHzjbxxRepository.GetSingle(p => p.Hzname == tps.Hzname && p.HzGender == tps.HzGender && p.Hzage == tps.Hzage && p.Hzzd == tps.Hzzd && p.Hzlxfs == "");
                if (hzinfomodel == null)
                {
                    //患者信息
                    Hzjbxx hzinfomode = new Hzjbxx();
                    hzinfomode.Hzname = tps.Hzname;//患者姓名
                    hzinfomode.HzGender = tps.HzGender;//患者性别
                    hzinfomode.Hzage = tps.Hzage;//患者年龄
                    hzinfomode.Hzzd = tps.Hzzd;//患者诊断
                                               //hzinfomode.Hzzyks = tps.Hzzyks;//住院科室
                                               //hzinfomode.Hzzyh = tps.Hzzyh;//住院号
                    hzinfomode.Hzlxfs = "";//联系方式
                    hzinfomode.isdelete = 0;//是否删除

                    var hzintoid = _IHzjbxxRepository.AddRetID(hzinfomode);
                }
                return Success("成功", a.Result);
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("upwjzqjs")]
        public async Task<IActionResult> upWjzqjs(Model.Dto.Wjzqjs tps)
        {
            Wjzqjs tp = await _IWjzqjsRepository.GetSingle(p=>p.id==tps.id);
            tp.id = tps.id;
            tp.Szzydjsj = tps.Szzydjsj;//登记时间
            tp.Szzyzzdw = tps.Szzyzzdw;//转诊单位
            tp.Yslxfs = tps.Yslxfs;//联系方式
            tp.Hzname = tps.Hzname;//患者姓名
            tp.HzGender = tps.HzGender;//患者性别
            tp.Hzage = tps.Hzage;//患者年龄
            tp.Hzzd = tps.Hzzd;//诊断
            tp.Hzjzks = tps.Hzjzks;//接诊科室
            tp.Hzzyh = tps.Hzzyh;//住院号
            tp.Syccsj = tps.Syccsj;//出车时间
            tp.Syjhsj = tps.Syjhsj;//接回时间
            tp.Syzzzrr = tps.Syzzzrr;//责任人
            tp.Sycysj = tps.Sycysj;//出院时间
            tp.Syzzzyfy = tps.Syzzzyfy;//住院费用
            tp.Syzzzhzzfw = tps.Syzzzhzzfw;//诊后追踪服务
            tp.Syzzfkszys = tps.Syzzfkszys;//反馈首诊医师
            tp.Syzzzrrss = tps.Syzzzrrss;//责任人
            tp.isdelete = 0;
            var resua = _IWjzqjsRepository.Update(tp);
            if (resua.Result)
            {
                var szzysmode = await _IWjzqjsRepository.GetSingle(p => p.id == tps.id);
                var hzinfomodel = await _IHzjbxxRepository.GetSingle(p => p.Hzname == szzysmode.Hzname && p.HzGender == szzysmode.HzGender && p.Hzage == szzysmode.Hzage && p.Hzzd == szzysmode.Hzzd );
                if (hzinfomodel != null)
                {
                    //患者信息
                    Hzjbxx hzinfomode = new Hzjbxx();
                    hzinfomode.id =   hzinfomodel.id; 
                    hzinfomode.Hzname = tps.Hzname;//患者姓名
                    hzinfomode.HzGender = tps.HzGender;//患者性别
                    hzinfomode.Hzage = tps.Hzage;//患者年龄
                    hzinfomode.Hzzd =  tps.Hzzd;//患者诊断
                                          //hzinfomode.Hzzyks = tps.Hzzyks;//住院科室
                                          //hzinfomode.Hzzyh = tps.Hzzyh;//住院号
                    //hzinfomode.Hzlxfs = ""; // tps.Hzlxfs;//联系方式
                    hzinfomode.isdelete = 0;//是否删除

                    var hzintoid = _IHzjbxxRepository.Update(hzinfomode);
                }
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 根据编号获取抢救信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("getwjzqjsbyid")]
        public async Task<IActionResult> GetWjzqjsbyid(string id)
        {
            var model = await _IWjzqjsRepository.Get(p => p.id == id);
            return Success("成功", model);
        }
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("delewjzqjs")]
        public async Task<IActionResult> deleWjzqjs(string id)
        {
            var result = _IWjzqjsRepository.Remove(id);
            if (result.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        [HttpGet("getwjzqjs")]
        public async Task<IActionResult> GetWjzqjs(int PageIndex, int PageSize, string stardate, string endtime, string zzdwname)
        {
            //排序字段
            var sort = Builders<Wjzqjs>.Sort.Descending("Szzydjsj");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<Wjzqjs>>();
            listApp.Add(Builders<Wjzqjs>.Filter.Eq("isdelete", 0));
            if (!string.IsNullOrWhiteSpace(stardate))
            {
                listApp.Add(Builders<Wjzqjs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
            }
            if (!string.IsNullOrWhiteSpace(endtime))
            {
                listApp.Add(Builders<Wjzqjs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
            }
            if (zzdwname != null)
            {
                var normallistAppor = new List<FilterDefinition<Wjzqjs>>();

                normallistAppor.Add(Builders<Wjzqjs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                normallistAppor.Add(Builders<Wjzqjs>.Filter.Where(p => p.Hzzyh.Contains(zzdwname)));
                normallistAppor.Add(Builders<Wjzqjs>.Filter.Where(p => p.Hzname.Contains(zzdwname)));
                listApp.Add(Builders<Wjzqjs>.Filter.Or(normallistAppor));
            }
            var filter = Builders<Wjzqjs>.Filter.And(listApp);

            var list = await _IWjzqjsRepository.GetByPage(filter, PageIndex, PageSize, sort);
            var redCount = await _IWjzqjsRepository.CountAsync(filter);   //获取总数
            var listmodel = new List<Wjzqjs>();
            foreach (var item in list)
            {
                var countlis = 0;
                //根据患者名称查找条数
                var listcounts = _IWjzqjsRepository.Get(p => p.Hzname == item.Hzname);
                if (listcounts != null)
                {
                    if (listcounts.Result.Count() > 1)
                    {
                        countlis = 1;
                    }

                }
                ///定义选项
                var model = new Wjzqjs();
                model.id = item.id;
                model.Szzydjsj = item.Szzydjsj;//登记时间
                model.Szzyzzdw = item.Szzyzzdw;//转诊单位
                model.Yslxfs = item.Yslxfs;//联系方式
                model.Hzname = item.Hzname;//患者姓名
                model.HzGender = item.HzGender;//患者性别
                model.Hzage = item.Hzage;//患者年龄
                model.Hzzd = item.Hzzd;//诊断
                model.Hzjzks = item.Hzjzks;//接诊科室
                model.Hzzyh = item.Hzzyh;//住院号
                model.Syccsj = item.Syccsj;//出车时间
                model.Syjhsj = item.Syjhsj;//接回时间
                model.Syzzzrr = item.Syzzzrr;//责任人
                model.Sycysj = item.Sycysj;//出院时间
                model.Syzzzyfy = item.Syzzzyfy;//住院费用
                model.Syzzzhzzfw = item.Syzzzhzzfw;//诊后追踪服务
                model.Syzzfkszys = item.Syzzfkszys;//反馈首诊医师
                model.Syzzzrrss = item.Syzzzrrss;//责任人
                model.isred = countlis;//颜色
                model.isdelete = 0;
                listmodel.Add(model);
            }
            var obj = new
            {
                rows = listmodel,
                total = redCount
            };
            return Success("成功", obj);
        }
        [HttpGet("exportexceljwzzqj")]
        [AllowAnonymous]
        public async Task<IActionResult> exportexceljwzzqj(string stardate, string endtime, string zzdwname)
        {
            try
            {
                //排序字段
                var sort = Builders<Wjzqjs>.Sort.Descending("Szzydjsj");
                //根据条件查询集合
                var listApp = new List<FilterDefinition<Wjzqjs>>();
                listApp.Add(Builders<Wjzqjs>.Filter.Eq("isdelete", 0));
                if (!string.IsNullOrWhiteSpace(stardate))
                {
                    listApp.Add(Builders<Wjzqjs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                }
                if (!string.IsNullOrWhiteSpace(endtime))
                {
                    listApp.Add(Builders<Wjzqjs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                }
                if (zzdwname != null)
                {
                    var normallistAppor = new List<FilterDefinition<Wjzqjs>>();

                    normallistAppor.Add(Builders<Wjzqjs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                    normallistAppor.Add(Builders<Wjzqjs>.Filter.Where(p => p.Hzzyh.Contains(zzdwname)));
                    normallistAppor.Add(Builders<Wjzqjs>.Filter.Where(p => p.Hzname.Contains(zzdwname)));
                    listApp.Add(Builders<Wjzqjs>.Filter.Or(normallistAppor));
                }
                var filter = Builders<Wjzqjs>.Filter.And(listApp);
                var lists = await _IWjzqjsRepository.Get(filter);

                List<Model.exportexceljwzqj> list = new List<Model.exportexceljwzqj>();
                foreach (var item in lists)
                {
                    Model.exportexceljwzqj model = new Model.exportexceljwzqj();
                    model.Szzydjsj   = item.Szzydjsj != null ? Convert.ToDateTime(item.Szzydjsj).ToString("yyyy - MM - dd  HH: mm:ss") : ""; 
                    model.Szzyzzdw   =item.Szzyzzdw;
                    model.Yslxfs     =item.Yslxfs;
                    model.Hzname     =item.Hzname;
                    model.HzGender   =item.HzGender;
                    model.Hzage      =item.Hzage;
                    model.Hzzd       =item.Hzzd;
                    model.Hzjzks     =item.Hzjzks;
                    model.Hzzyh      =item.Hzzyh;
                    model.Syccsj = item.Syccsj != null ? Convert.ToDateTime(item.Syccsj).ToString("yyyy - MM - dd  HH: mm:ss") : "";
                    model.Syjhsj     = item.Syjhsj != null ? Convert.ToDateTime(item.Syjhsj).ToString("yyyy - MM - dd  HH: mm:ss") : ""; 
                    model.Sycysj     = item.Sycysj != null ? Convert.ToDateTime(item.Sycysj).ToString("yyyy - MM - dd  HH: mm:ss") : ""; 
                    model.Syzzzyfy   =item.Syzzzyfy;
                    model.Syzzzrr = item.Syzzzrr;
                    model.Syzzzhzzfw =item.Syzzzhzzfw;
                    model.Syzzfkszys =item.Syzzfkszys;
                    model.Syzzzrrss = item.Syzzzrrss;
                    list.Add(model);
                }
                NPOIHelper npoi = new NPOIHelper();
                var tb = ModelConvertHelper<Szzys>.ListToTable(list, null);
                if (tb != null)
                {
                    //导出dtnew 
                    string[] cols = { "登记时间", "转诊单位",  "联系方式", "患者姓名", "患者性别", "患者年龄", "患者诊断", "接诊科室", "住院号", "出车时间", "接回时间", "出院时间", "住院费用", "责任人", "诊后追踪服务", "反馈首诊医师", "责任人" };
                    
                    byte[] sm = npoi.ExportToExcel1(tb, cols);
                    if (sm != null)
                    {
                        return File(sm, "application/vnd.ms-excel", "急危重抢救.xlsx");
                    }
                    else
                    {
                        return Error("导出失败");
                    }
                }
                return Error("没有数据");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "导出异常");
                return Error("导出失败");
            }
          
        }
       
        #endregion
        #region 标本收取 
        /// <summary>
        /// 添加标本收取信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("addbbsqs")]
        public async Task<IActionResult> AddBbsqs(Model.Dto.Bbsqs tps)
        {
            Bbsqs tp = new Bbsqs();
            tp.Szzydjsj = DateTime.Now;//tps.Szzydjsj;//登记时间
            tp.Szzyzzdw = tps.Szzyzzdw;//转诊单位
            tp.Yslxfs = tps.Yslxfs;//联系方式
            tp.Hzname = tps.Hzname;//患者姓名
            tp.Hzjcxm = tps.Hzjcxm;//检查项目
            tp.Syzzzzmd = "标本收取";// tps.Syzzzzmd;//转诊目的
            tp.Xyjzsj = tps.Xyjzsj;//就诊时间
            tp.Syzzzrr = tps.Syzzzrr;//责任人
            tp.Xyjgcjsj = tps.Xyjgcjsj;//结果出具时间
            tp.isdelete = 0;
            var a = _IBbsqsRepository.AddRetID(tp);
            if (a.Result != null || a.Result != "")
            {
                return Success("成功", a.Result);
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 修改标本收取信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("upbbsqs")]
        public async Task<IActionResult> upBbsqs(Model.Dto.Bbsqs tps)
        {
            Bbsqs tp = await _IBbsqsRepository.GetSingle(p => p.id == tps.id); ;
            tp.id = tps.id;
            tp.Szzydjsj = tps.Szzydjsj;//登记时间
            tp.Szzyzzdw = tps.Szzyzzdw;//转诊单位
            tp.Yslxfs = tps.Yslxfs;//联系方式
            tp.Hzname = tps.Hzname;//患者姓名
            tp.Hzjcxm = tps.Hzjcxm;//检查项目
            //tp.Syzzzzmd = tps.Syzzzzmd;//转诊目的
            tp.Xyjzsj = tps.Xyjzsj;//就诊时间
            tp.Syzzzrr = tps.Syzzzrr;//责任人
            tp.Xyjgcjsj = tps.Xyjgcjsj;//结果出具时间
            tp.isdelete = 0;
            var resua = _IBbsqsRepository.Update(tp);
            if (resua.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 根据编号获取标本收取信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("getbbsqsbyid")]
        public async Task<IActionResult> GetBbsqsbyid(string id)
        {
            var model = await _IBbsqsRepository.Get(p => p.id == id);
            return Success("成功", model);
        }
        /// <summary>
        /// 删除标本收取信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("delebbsqs")]
        public async Task<IActionResult> deleBbsqs(string id)
        {
            var result = _IBbsqsRepository.Remove(id);
            if (result.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 获取标本收取信息
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        [HttpGet("getbbsqs")]
        public async Task<IActionResult> GetBbsqs(int PageIndex, int PageSize, string stardate, string endtime, string zzdwname)
        {
            //排序字段
            var sort = Builders<Bbsqs>.Sort.Descending("Szzydjsj");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<Bbsqs>>();
            listApp.Add(Builders<Bbsqs>.Filter.Eq("isdelete", 0));
            if (!string.IsNullOrWhiteSpace(stardate))
            {
                listApp.Add(Builders<Bbsqs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
            }
            if (!string.IsNullOrWhiteSpace(endtime))
            {
                listApp.Add(Builders<Bbsqs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
            }
            if (zzdwname != null)
            {
                var normallistAppor = new List<FilterDefinition<Bbsqs>>();

                normallistAppor.Add(Builders<Bbsqs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                normallistAppor.Add(Builders<Bbsqs>.Filter.Where(p => p.Hzname.Contains(zzdwname)));
                listApp.Add(Builders<Bbsqs>.Filter.Or(normallistAppor));
            }
            var filter = Builders<Bbsqs>.Filter.And(listApp);

            var list = await _IBbsqsRepository.GetByPage(filter, PageIndex, PageSize, sort);
            var redCount = await _IBbsqsRepository.CountAsync(filter);   //获取总数
            var listmodel = new List<Bbsqs>();
            foreach (var item in list)
            {
                ///定义选项
                var model = new Bbsqs();
                model.id = item.id;
                model.Szzydjsj = item.Szzydjsj;//登记时间
                model.Szzyzzdw = item.Szzyzzdw;//转诊单位
                model.Yslxfs = item.Yslxfs;//联系方式
                model.Hzname = item.Hzname;//患者姓名
                model.Hzjcxm = item.Hzjcxm;//检查项目
                model.Syzzzzmd = item.Syzzzzmd;//转诊目的
                model.Xyjzsj = item.Xyjzsj;//就诊时间
                model.Syzzzrr = item.Syzzzrr;//责任人
                model.Xyjgcjsj = item.Xyjgcjsj;//结果出具时间
                model.isdelete = 0;
                listmodel.Add(model);
            }
            var obj = new
            {
                rows = listmodel,
                total = redCount
            };
            return Success("成功", obj);
        }
        /// <summary>
        /// 导出标本收取
        /// </summary>
        /// <param name="stardate"></param>
        /// <param name="endtime"></param>
        /// <param name="zzdwname"></param>
        /// <returns></returns>
        [HttpGet("exportexcelbbsq")]
        [AllowAnonymous]
        public async Task<IActionResult> exportexcelbbsq(string stardate, string endtime, string zzdwname)
        {
            try
            {
                //排序字段
                var sort = Builders<Bbsqs>.Sort.Descending("Szzydjsj");
                //根据条件查询集合
                var listApp = new List<FilterDefinition<Bbsqs>>();
                listApp.Add(Builders<Bbsqs>.Filter.Eq("isdelete", 0));
                if (!string.IsNullOrWhiteSpace(stardate))
                {
                    listApp.Add(Builders<Bbsqs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                }
                if (!string.IsNullOrWhiteSpace(endtime))
                {
                    listApp.Add(Builders<Bbsqs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                }
                if (zzdwname != null)
                {
                    var normallistAppor = new List<FilterDefinition<Bbsqs>>();

                    normallistAppor.Add(Builders<Bbsqs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                    normallistAppor.Add(Builders<Bbsqs>.Filter.Where(p => p.Hzname.Contains(zzdwname)));
                    listApp.Add(Builders<Bbsqs>.Filter.Or(normallistAppor));
                }
                var filter = Builders<Bbsqs>.Filter.And(listApp);
                var lists = await _IBbsqsRepository.Get(filter);

                List<Model.exportexcelbbsq> list = new List<Model.exportexcelbbsq>();
                foreach (var item in lists)
                {
                    Model.exportexcelbbsq model = new Model.exportexcelbbsq();
                    model.Szzydjsj = item.Szzydjsj != null ? Convert.ToDateTime(item.Szzydjsj).ToString("yyyy - MM - dd  HH: mm:ss") : "";
                    model.Szzyzzdw = item.Szzyzzdw;//转诊单位
                    model.Yslxfs = item.Yslxfs;//联系方式
                    model.Hzname = item.Hzname;//患者姓名
                    model.Hzjcxm = item.Hzjcxm;//检查项目
                    model.Syzzzzmd = item.Syzzzzmd;//转诊目的
                    model.Xyjzsj = item.Xyjzsj != null ? Convert.ToDateTime(item.Xyjzsj).ToString("yyyy - MM - dd  HH: mm:ss") : "" ;//就诊时间
                    model.Syzzzrr = item.Syzzzrr;//责任人
                    model.Xyjgcjsj = item.Xyjgcjsj != null ? Convert.ToDateTime(item.Xyjgcjsj).ToString("yyyy - MM - dd  HH: mm:ss") : "";//结果出具时间
                    list.Add(model);
                }
                NPOIHelper npoi = new NPOIHelper();
                var tb = ModelConvertHelper<Szzys>.ListToTable(list, null);
                if (tb != null)
                {
                    //导出dtnew 
                    string[] cols = { "登记时间", "转诊单位", "联系方式", "患者姓名", "检查项目", "转诊目的", "就诊时间", "责任人", "结果出具时间" };

                    byte[] sm = npoi.ExportToExcel1(tb, cols);
                    if (sm != null)
                    {
                        return File(sm, "application/vnd.ms-excel", "标本收取.xlsx");
                    }
                    else
                    {
                        return Error("导出失败");
                    }
                }
                return Error("没有数据");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "导出异常");
                return Error("导出失败");
            }
        }

        #endregion
        #region 会诊安排 
        /// <summary>
        /// 添加会诊安排
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("addhzaps")]
        public async Task<IActionResult> AddHzaps(Model.Dto.Hzaps tps)
        {
            Hzaps tp = new Hzaps();
            tp.Hzsj = tps.Hzsj;//会诊时间
            tp.Sqhzjg = tps.Sqhzjg;//申请会诊机构
            tp.Sqhzys = tps.Sqhzys;//申请会诊医师
            tp.Lxfs = tps.Lxfs;//联系方式
            tp.Hzzj = tps.Hzzj;//  会诊专家
            tp.ifsuccess = tps.ifsuccess;//成功未成功
            tp.Syzzzhzzfw = tps.Syzzzhzzfw;//诊后追踪服务
            tp.Syzzzrrss = tps.Syzzzrrss;//责任人
            tp.isdelete = tps.isdelete;// 是否删除
            var a = _IHzapsRepository.AddRetID(tp);
            if (a.Result != null || a.Result != "")
            {
                return Success("成功", a.Result);
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 修改会诊安排信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("uphzaps")]
        public async Task<IActionResult> upHzaps(Model.Dto.Hzaps tps)
        {
            Hzaps tp =await _IHzapsRepository.GetSingle(p=>p.id==tps.id);
            tp.id = tps.id;
            tp.Hzsj = tps.Hzsj;//会诊时间
            tp.Sqhzjg = tps.Sqhzjg;//申请会诊机构
            tp.Sqhzys = tps.Sqhzys;//申请会诊医师
            tp.Lxfs = tps.Lxfs;//联系方式
            tp.Hzzj = tps.Hzzj;//  会诊专家
            tp.ifsuccess = tps.ifsuccess;//成功未成功
            tp.Syzzzhzzfw = tps.Syzzzhzzfw;//诊后追踪服务
            tp.Syzzzrrss = tps.Syzzzrrss;//责任人
            tp.isdelete = 0;// 是否删除
            var resua = _IHzapsRepository.Update(tp);
            if (resua.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 根据编号获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("gethzapsbyid")]
        public async Task<IActionResult> GetHzapsbyid(string id)
        {
            var model = await _IHzapsRepository.Get(p => p.id == id);
            return Success("成功", model);
        }
        /// <summary>
        /// 删除会诊安排信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("delehzaps")]
        public async Task<IActionResult> deleHzaps(string id)
        {
            var result = _IHzapsRepository.Remove(id);
            if (result.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 获取会诊信息
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        [HttpGet("gethzaps")]
        public async Task<IActionResult> GetHzaps(int PageIndex, int PageSize, string stardate, string endtime, string Sqhzjg)
        {
            //排序字段
            var sort = Builders<Hzaps>.Sort.Descending("Hzsj");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<Hzaps>>();
            listApp.Add(Builders<Hzaps>.Filter.Eq("isdelete", 0));
            if (!string.IsNullOrWhiteSpace(stardate))
            {
                listApp.Add(Builders<Hzaps>.Filter.Gt("Hzsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
            }
            if (!string.IsNullOrWhiteSpace(endtime))
            {
                listApp.Add(Builders<Hzaps>.Filter.Lt("Hzsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
            }
            if (Sqhzjg != null)
            {
                listApp.Add(Builders<Hzaps>.Filter.Eq("Sqhzjg", Sqhzjg));
            }
            var filter = Builders<Hzaps>.Filter.And(listApp);

            var list = await _IHzapsRepository.GetByPage(filter, PageIndex, PageSize, sort);
            var redCount = await _IHzapsRepository.CountAsync(filter);   //获取总数
            var listmodel = new List<Hzaps>();
            foreach (var item in list)
            {
                ///定义选项
                var model = new Hzaps();
                model.id = item.id;
                model.Hzsj = item.Hzsj;//会诊时间
                model.Sqhzjg = item.Sqhzjg;//申请会诊机构
                model.Sqhzys = item.Sqhzys;//申请会诊医师
                model.Lxfs = item.Lxfs;//联系方式
                model.Hzzj = item.Hzzj;//  会诊专家
                model.ifsuccess = item.ifsuccess;//成功未成功
                model.Syzzzhzzfw = item.Syzzzhzzfw;//诊后追踪服务
                model.Syzzzrrss = item.Syzzzrrss;//责任人
                model.isdelete = 0;
                listmodel.Add(model);
            }
            var obj = new
            {
                rows = listmodel,
                total = redCount
            };
            return Success("成功", obj);
        }
        [HttpGet("exportexcelhzap")]
        [AllowAnonymous]
        public async Task<IActionResult> exportexcelhzap(string stardate, string endtime, string Sqhzjg)
        {
            try
            {
                //排序字段
                var sort = Builders<Hzaps>.Sort.Descending("Hzsj");
                //根据条件查询集合
                var listApp = new List<FilterDefinition<Hzaps>>();
                listApp.Add(Builders<Hzaps>.Filter.Eq("isdelete", 0));
                if (!string.IsNullOrWhiteSpace(stardate))
                {
                    listApp.Add(Builders<Hzaps>.Filter.Gt("Hzsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                }
                if (!string.IsNullOrWhiteSpace(endtime))
                {
                    listApp.Add(Builders<Hzaps>.Filter.Lt("Hzsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                }
                if (Sqhzjg != null)
                {
                    listApp.Add(Builders<Hzaps>.Filter.Eq("Sqhzjg", Sqhzjg));
                }
                var filter = Builders<Hzaps>.Filter.And(listApp);

                var lists = await _IHzapsRepository.Get(filter);

                List<Model.exportexcelhzap> list = new List<Model.exportexcelhzap>();
                foreach (var item in lists)
                {
                    Model.exportexcelhzap model = new Model.exportexcelhzap();
                    model.Hzsj = item.Hzsj != null ? Convert.ToDateTime(item.Hzsj).ToString("yyyy - MM - dd  HH: mm:ss") : "";//会诊时间
                    model.Sqhzjg = item.Sqhzjg;//申请会诊机构
                    model.Sqhzys = item.Sqhzys;//申请会诊医师
                    model.Lxfs = item.Lxfs;//联系方式
                    model.Hzzj = item.Hzzj;//  会诊专家
                    model.ifsuccess = item.ifsuccess;//成功未成功
                    model.Syzzzhzzfw = item.Syzzzhzzfw;//诊后追踪服务
                    model.Syzzzrrss = item.Syzzzrrss;//责任人
                    list.Add(model);
                }
                NPOIHelper npoi = new NPOIHelper();
                var tb = ModelConvertHelper<Szzys>.ListToTable(list, null);
                if (tb != null)
                {
                    //导出dtnew 
                    string[] cols = { "会诊时间", "申请会诊机构", "申请会诊医师", "联系方式", "会诊专家", "成功未成功", "诊后追踪服务", "责任人" };

                    byte[] sm = npoi.ExportToExcel1(tb, cols);
                    if (sm != null)
                    {
                        return File(sm, "application/vnd.ms-excel", "会诊安排.xlsx");
                    }
                    else
                    {
                        return Error("导出失败");
                    }
                }
                return Error("没有数据");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "导出异常");
                return Error("导出失败");
            }
        }
        #endregion
        #region 工作量日报 
        /// <summary>
        /// 添加工作日报信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("addgzlrbs")]
        public async Task<IActionResult> AddGzlrbs(Model.Dto.Gzlrbs tps)
        {
            Gzlrbs tp = new Gzlrbs();
            tp.Gzlrbxm = tps.Gzlrbxm;//项目
            tp.Gztype = tps.Gztype;//分类
            tp.Gzrc = tps.Gzrc;//人次
            tp.Gzmemo = tps.Gzmemo;//备注
            tp.isdelete = 0;// 是否删除
            var a = _IGzlrbsRepository.AddRetID(tp);
            if (a.Result != null || a.Result != "")
            {
                return Success("成功", a.Result);
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 修改工作日报信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("upgzlrbs")]
        public async Task<IActionResult> upGzlrbs(Model.Dto.Gzlrbs tps)
        {
            Gzlrbs tp = new Gzlrbs();
            tp.id = tps.id;
            tp.Gzlrbxm = tps.Gzlrbxm;//项目
            tp.Gztype = tps.Gztype;//分类
            tp.Gzrc = tps.Gzrc;//人次
            tp.Gzmemo = tps.Gzmemo;//备注
            tp.isdelete = 0;
            var resua = _IGzlrbsRepository.Update(tp);
            if (resua.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 根据编号获取信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("getgzlrbsbyid")]
        public async Task<IActionResult> GetGzlrbsbyid(string id)
        {
            var model = await _IGzlrbsRepository.Get(p => p.id == id);
            return Success("成功", model);
        }
        /// <summary>
        /// 删除日报信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("delegzlrbs")]
        public async Task<IActionResult> deleGzlrbs(string id)
        {
            var result = _IGzlrbsRepository.Remove(id);
            if (result.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 获取工作量日报信息
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        [HttpGet("getgzlrbs")]
        public async Task<IActionResult> GetGzlrbs(int PageIndex, int PageSize)
        {
            //排序字段
            //var sort = Builders<Gzlrbs>.Sort.Ascending("Hzsj");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<Gzlrbs>>();
            listApp.Add(Builders<Gzlrbs>.Filter.Eq("isdelete", 0));

            var filter = Builders<Gzlrbs>.Filter.And(listApp);

            var list = await _IGzlrbsRepository.GetByPage(filter, PageIndex, PageSize);
            var redCount = await _IGzlrbsRepository.CountAsync(filter);   //获取总数
            var listmodel = new List<Gzlrbs>();
            foreach (var item in list)
            {
                ///定义选项
                var model = new Gzlrbs();
                model.id = item.id;
                model.Gzlrbxm = item.Gzlrbxm;//项目
                model.Gztype = item.Gztype;//分类
                model.Gzrc = item.Gzrc;//人次
                model.Gzmemo = item.Gzmemo;//备注
                model.isdelete = 0;
                listmodel.Add(model);
            }
            var obj = new
            {
                rows = listmodel,
                total = redCount
            };
            return Success("成功", obj);
        }



        #endregion
        #region 预约诊疗 
        /// <summary>
        /// 添加预约诊疗信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("addyyzls")]
        public async Task<IActionResult> AddYyzls(Model.Dto.Yyzls tps)
        {
            Yyzls tp = new Yyzls();
            tp.Szzydjsj = DateTime.Now;// tps.Szzydjsj;//登记日期
            tp.Szzyyydw = tps.Szzyyydw;//预约单位
            tp.Szzyyyys = tps.Szzyyyys;//预约医师
            tp.Yylxfs = tps.Yylxfs;//联系方式
            tp.Hzname = tps.Hzname;//患者姓名
            tp.Hzyyks = tps.Hzyyks;//预约科室
            tp.Hzlxfs = tps.Hzlxfs;//联系方式
            tp.Syyysj = tps.Syyysj;//预约时间
            tp.Syzzzrr = tps.Syzzzrr;//责任人
            tp.Syzzzhzzfw = tps.Syzzzhzzfw;//诊后追踪情况
            tp.isdelete = 0;// 是否删除
            var a = _IYyzlsRepository.AddRetID(tp);
            if (a.Result != null || a.Result != "")
            {
                return Success("成功", a.Result);
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("upyyzls")]
        public async Task<IActionResult> upYyzls(Model.Dto.Yyzls tps)
        {
            Yyzls tp = await _IYyzlsRepository.GetSingle(p=>p.id==tps.id);
            tp.id = tps.id;
           // tp.Szzydjsj = tps.Szzydjsj;//登记日期
            tp.Szzyyydw = tps.Szzyyydw;//预约单位
            tp.Szzyyyys = tps.Szzyyyys;//预约医师
            tp.Yylxfs = tps.Yylxfs;//联系方式
            tp.Hzname = tps.Hzname;//患者姓名
            tp.Hzyyks = tps.Hzyyks;//预约科室
            tp.Hzlxfs = tps.Hzlxfs;//联系方式
            tp.Syyysj = tps.Syyysj;//预约时间
            tp.Syzzzrr = tps.Syzzzrr;//责任人
            tp.Syzzzhzzfw = tps.Syzzzhzzfw;//诊后追踪情况
            tp.isdelete = 0;
            var resua = _IYyzlsRepository.Update(tp);
            if (resua.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 根据编号获取信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("getyyzlsbyid")]
        public async Task<IActionResult> GetYyzlsbyid(string id)
        {
            var model = await _IYyzlsRepository.Get(p => p.id == id);
            return Success("成功", model);
        }
        /// <summary>
        /// 删除预约诊疗信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("deleyyzls")]
        public async Task<IActionResult> deleYyzls(string id)
        {
            var result = _IYyzlsRepository.Remove(id);
            if (result.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 获取预约诊疗信息
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        [HttpGet("getyyzls")]
        public async Task<IActionResult> GetYyzls(int PageIndex, int PageSize, string stardate, string endtime)
        {
            //排序字段
            var sort = Builders<Yyzls>.Sort.Descending("Szzydjsj");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<Yyzls>>();
            listApp.Add(Builders<Yyzls>.Filter.Eq("isdelete", 0));
            if (!string.IsNullOrWhiteSpace(stardate))
            {
                listApp.Add(Builders<Yyzls>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
            }
            if (!string.IsNullOrWhiteSpace(endtime))
            {
                listApp.Add(Builders<Yyzls>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
            }

            var filter = Builders<Yyzls>.Filter.And(listApp);

            var list = await _IYyzlsRepository.GetByPage(filter, PageIndex, PageSize, sort);
            var redCount = await _IYyzlsRepository.CountAsync(filter);   //获取总数
            var listmodel = new List<Yyzls>();
            foreach (var item in list)
            {
                ///定义选项
                var model = new Yyzls();
                model.id = item.id;
                model.Szzydjsj = item.Szzydjsj;//登记日期
                model.Szzyyydw = item.Szzyyydw;//预约单位
                model.Szzyyyys = item.Szzyyyys;//预约医师
                model.Yylxfs = item.Yylxfs;//联系方式
                model.Hzname = item.Hzname;//患者姓名
                model.Hzyyks = item.Hzyyks;//预约科室
                model.Hzlxfs = item.Hzlxfs;//联系方式
                model.Syyysj = item.Syyysj;//预约时间
                model.Syzzzrr = item.Syzzzrr;//责任人
                model.Syzzzhzzfw = item.Syzzzhzzfw;//诊后追踪情况
                model.isdelete = item.isdelete;//是否启用
                listmodel.Add(model);
            }
            var obj = new
            {
                rows = listmodel,
                total = redCount
            };
            return Success("成功", obj);
        }

        [HttpGet("exportexcelyyzl")]
        [AllowAnonymous]
        public async Task<IActionResult> exportexcelyyzl(string stardate, string endtime)
        {
            try
            {
                //排序字段
                var sort = Builders<Yyzls>.Sort.Descending("Szzydjsj");
                //根据条件查询集合
                var listApp = new List<FilterDefinition<Yyzls>>();
                listApp.Add(Builders<Yyzls>.Filter.Eq("isdelete", 0));
                if (!string.IsNullOrWhiteSpace(stardate))
                {
                    listApp.Add(Builders<Yyzls>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                }
                if (!string.IsNullOrWhiteSpace(endtime))
                {
                    listApp.Add(Builders<Yyzls>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                }

                var filter = Builders<Yyzls>.Filter.And(listApp);

                var lists = await _IYyzlsRepository.Get(filter);
                List<Model.exportexcelyyzl> list = new List<Model.exportexcelyyzl>();
                foreach (var item in lists)
                {
                    Model.exportexcelyyzl model = new Model.exportexcelyyzl();
                    model.Szzydjsj = item.Szzydjsj!=null ? Convert.ToDateTime(item.Szzydjsj).ToString("yyyy - MM - dd  HH: mm:ss") : "";//登记日期
                    model.Szzyyydw = item.Szzyyydw;//预约单位
                    model.Szzyyyys = item.Szzyyyys;//预约医师
                    model.Yylxfs = item.Yylxfs;//联系方式
                    model.Hzname = item.Hzname;//患者姓名
                    model.Hzyyks = item.Hzyyks;//预约科室
                    model.Hzlxfs = item.Hzlxfs;//联系方式
                    model.Syyysj = item.Syyysj != null ? Convert.ToDateTime(item.Syyysj).ToString("yyyy - MM - dd  HH: mm:ss") : "";//预约时间
                    model.Syzzzrr = item.Syzzzrr;//责任人
                    model.Syzzzhzzfw = item.Syzzzhzzfw;//诊后追踪情况
                    list.Add(model);
                }
                NPOIHelper npoi = new NPOIHelper();
                var tb = ModelConvertHelper<Szzys>.ListToTable(list, null);
                if (tb != null)
                {
                    //导出dtnew 
                    string[] cols = { "会诊时间", "申请会诊机构", "申请会诊医师", "联系方式", "会诊专家", "成功未成功", "诊后追踪服务", "责任人" };

                    byte[] sm = npoi.ExportToExcel1(tb, cols);
                    if (sm != null)
                    {
                        return File(sm, "application/vnd.ms-excel", "会诊安排.xlsx");
                    }
                    else
                    {
                        return Error("导出失败");
                    }
                }
                return Error("没有数据");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "导出异常");
                return Error("导出失败");
            }
        }
        #endregion
        #region 医联体工作量统计表
        //excelsxzzallinfo
        [HttpGet("getsxzzallinfo")]
        public async Task<IActionResult> Getsxzzallinfo( string stardate, string endtime,string zzdwname)
        {
            #region 转诊单位条件检索
            List<Model.excelsxzzallinfo> excelsxzzallinfolist = new List<Model.excelsxzzallinfo>();
            
            #region 定义合计字段
            int sszyinfo = 0;
            int ssmzinfo = 0;
            int szjcinfo = 0;
            int xzzlinfo = 0;
            int xzzyinfo = 0;
            int jwzzqjinfo = 0;
            int bbsqinfo = 0;
            int zjzsinfo = 0; 
            #endregion
            #region 转诊单位不为空
            if (zzdwname != null)
            {
                string[] arrStr = zzdwname.Split(',');
                foreach (var item in arrStr)
                {
                    Model.excelsxzzallinfo excelsxzzallmodel = new Model.excelsxzzallinfo();
                    excelsxzzallmodel.deptzzdw = item;
                    #region 上转住院
                    var sort = Builders<Szzys>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listApp = new List<FilterDefinition<Szzys>>();
                    listApp.Add(Builders<Szzys>.Filter.Eq("isdelete", 0));
                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzyzzdw == item));

                    var filter = Builders<Szzys>.Filter.And(listApp);

                    var list = await _ISzzysRepository.Get(filter);
                    excelsxzzallmodel.sszyinfo = list.Count().ToString();
                    sszyinfo += list.Count();
                    #endregion
                    #region 上转门诊
                    //排序字段
                    var sortmz = Builders<Szmzs>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listAppmz = new List<FilterDefinition<Szmzs>>();
                    listAppmz.Add(Builders<Szmzs>.Filter.Eq("isdelete", 0));

                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzyzzdw == item));
                    var filtermz = Builders<Szmzs>.Filter.And(listAppmz);

                    var listmz = await _ISzmzsRepository.Get(filtermz);
                    excelsxzzallmodel.ssmzinfo = listmz.Count().ToString();
                    ssmzinfo += listmz.Count();
                    #endregion
                    #region 上转检查
                    //排序字段
                    var sortjc = Builders<Szjcs>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listAppjc = new List<FilterDefinition<Szjcs>>();
                    listAppjc.Add(Builders<Szjcs>.Filter.Eq("isdelete", 0));
                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzyzzdw == item));
                    var filterjc = Builders<Szjcs>.Filter.And(listAppjc);

                    var listjc = await _ISzjcsRepository.Get(filterjc);
                    excelsxzzallmodel.szjcinfo = listjc.Count().ToString();
                    szjcinfo += listjc.Count();
                    #endregion
                    #region 下转治疗
                    //排序字段
                    var sortxzzl = Builders<Xzzls>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listAppxzzl = new List<FilterDefinition<Xzzls>>();
                    listAppxzzl.Add(Builders<Xzzls>.Filter.Eq("isdelete", 0));
                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listAppxzzl.Add(Builders<Xzzls>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listAppxzzl.Add(Builders<Xzzls>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listAppxzzl.Add(Builders<Xzzls>.Filter.Where(p => p.Szzyzzdw == item));
                    var filterxzzl = Builders<Xzzls>.Filter.And(listAppxzzl);

                    var listxzzl = await _IXzzlsRepository.Get(filterxzzl);
                    excelsxzzallmodel.xzzlinfo = listxzzl.Count().ToString();
                    xzzlinfo += listxzzl.Count();
                    #endregion
                    #region 下转住院
                    //排序字段
                    var sortxzzy = Builders<Xzzys>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listAppxzzy = new List<FilterDefinition<Xzzys>>();
                    listAppxzzy.Add(Builders<Xzzys>.Filter.Eq("isdelete", 0));
                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listAppxzzy.Add(Builders<Xzzys>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listAppxzzy.Add(Builders<Xzzys>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listAppxzzy.Add(Builders<Xzzys>.Filter.Where(p => p.Szzyzzdw == item));
                    var filterxzzy = Builders<Xzzys>.Filter.And(listAppxzzy);

                    var listxzzy = await _IXzzysRepository.Get(filterxzzy);
                    excelsxzzallmodel.xzzyinfo = listxzzy.Count().ToString();
                    xzzyinfo += listxzzy.Count();
                    #endregion
                    #region 急危重症抢救
                    //排序字段
                    var sortjwzqj = Builders<Wjzqjs>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listAppjwzqj = new List<FilterDefinition<Wjzqjs>>();
                    listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Eq("isdelete", 0));
                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Where(p => p.Szzyzzdw == item));
                    var filterjwzqj = Builders<Wjzqjs>.Filter.And(listAppjwzqj);

                    var listjwzqj = await _IWjzqjsRepository.Get(filterjwzqj);
                    excelsxzzallmodel.jwzzqjinfo = listjwzqj.Count().ToString();
                    jwzzqjinfo += listjwzqj.Count();
                    #endregion
                    #region 标本收取
                    //排序字段
                    var sortbbsq = Builders<Bbsqs>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listAppbbsq = new List<FilterDefinition<Bbsqs>>();
                    listAppbbsq.Add(Builders<Bbsqs>.Filter.Eq("isdelete", 0));
                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listAppbbsq.Add(Builders<Bbsqs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listAppbbsq.Add(Builders<Bbsqs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listAppbbsq.Add(Builders<Bbsqs>.Filter.Where(p => p.Szzyzzdw == item));
                    var filterbbsq = Builders<Bbsqs>.Filter.And(listAppbbsq);

                    var listbbsq = await _IBbsqsRepository.Get(filterbbsq);
                    excelsxzzallmodel.bbsqinfo = listbbsq.Count().ToString();
                    bbsqinfo += listbbsq.Count();
                    #endregion
                    excelsxzzallmodel.zjzsinfo = (list.Count() + listmz.Count() + listjc.Count() + listxzzl.Count() + listxzzy.Count() + listjwzqj.Count() + listbbsq.Count()).ToString();
                    excelsxzzallinfolist.Add(excelsxzzallmodel);
                    zjzsinfo += list.Count() + listmz.Count() + listjc.Count() + listxzzl.Count() + listxzzy.Count() + listjwzqj.Count() + listbbsq.Count();
                }
            }
            #endregion
            #region 转诊单位为空
            else
            {
                //先获取所有转诊单位
                List<Model.Allzzdwinfo> allzzdwinfo = getAddzzdinfolist(stardate, endtime, zzdwname);
               
                #region 循环遍历所有转诊单位
                foreach (var item1 in allzzdwinfo)
                {
                    Model.excelsxzzallinfo excelsxzzallmodel = new Model.excelsxzzallinfo();
                    excelsxzzallmodel.deptzzdw = item1.name;
                    #region 上转住院
                    var sort = Builders<Szzys>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listApp = new List<FilterDefinition<Szzys>>();
                    listApp.Add(Builders<Szzys>.Filter.Eq("isdelete", 0));
                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzyzzdw== item1.name));
                   
                    var filter = Builders<Szzys>.Filter.And(listApp);

                    var list = await _ISzzysRepository.Get(filter);
                    excelsxzzallmodel.sszyinfo = list.Count().ToString();
                    sszyinfo += list.Count();
                    #endregion
                    #region 上转门诊
                    //排序字段
                    var sortmz = Builders<Szmzs>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listAppmz = new List<FilterDefinition<Szmzs>>();
                    listAppmz.Add(Builders<Szmzs>.Filter.Eq("isdelete", 0));

                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzyzzdw == item1.name));
                    var filtermz = Builders<Szmzs>.Filter.And(listAppmz);

                    var listmz = await _ISzmzsRepository.Get(filtermz);
                    excelsxzzallmodel.ssmzinfo = listmz.Count().ToString();
                    ssmzinfo += listmz.Count();
                    #endregion
                    #region 上转检查
                    //排序字段
                    var sortjc = Builders<Szjcs>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listAppjc = new List<FilterDefinition<Szjcs>>();
                    listAppjc.Add(Builders<Szjcs>.Filter.Eq("isdelete", 0));
                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzyzzdw == item1.name));
                    var filterjc = Builders<Szjcs>.Filter.And(listAppjc);

                    var listjc = await _ISzjcsRepository.Get(filterjc);
                    excelsxzzallmodel.szjcinfo = listjc.Count().ToString();
                    szjcinfo += listjc.Count();
                    #endregion
                    #region 下转治疗
                    //排序字段
                    var sortxzzl = Builders<Xzzls>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listAppxzzl = new List<FilterDefinition<Xzzls>>();
                    listAppxzzl.Add(Builders<Xzzls>.Filter.Eq("isdelete", 0));
                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listAppxzzl.Add(Builders<Xzzls>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listAppxzzl.Add(Builders<Xzzls>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listAppxzzl.Add(Builders<Xzzls>.Filter.Where(p => p.Szzyzzdw == item1.name));
                    var filterxzzl = Builders<Xzzls>.Filter.And(listAppxzzl);

                    var listxzzl = await _IXzzlsRepository.Get(filterxzzl);
                    excelsxzzallmodel.xzzlinfo = listxzzl.Count().ToString();
                    xzzlinfo += listxzzl.Count();
                    #endregion
                    #region 下转住院
                    //排序字段
                    var sortxzzy = Builders<Xzzys>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listAppxzzy = new List<FilterDefinition<Xzzys>>();
                    listAppxzzy.Add(Builders<Xzzys>.Filter.Eq("isdelete", 0));
                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listAppxzzy.Add(Builders<Xzzys>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listAppxzzy.Add(Builders<Xzzys>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listAppxzzy.Add(Builders<Xzzys>.Filter.Where(p => p.Szzyzzdw == item1.name));
                    var filterxzzy = Builders<Xzzys>.Filter.And(listAppxzzy);

                    var listxzzy = await _IXzzysRepository.Get(filterxzzy);
                    excelsxzzallmodel.xzzyinfo = listxzzy.Count().ToString();
                    xzzyinfo += listxzzy.Count();
                    #endregion
                    #region 急危重症抢救
                    //排序字段
                    var sortjwzqj = Builders<Wjzqjs>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listAppjwzqj = new List<FilterDefinition<Wjzqjs>>();
                    listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Eq("isdelete", 0));
                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Where(p => p.Szzyzzdw == item1.name));
                    var filterjwzqj = Builders<Wjzqjs>.Filter.And(listAppjwzqj);

                    var listjwzqj = await _IWjzqjsRepository.Get(filterjwzqj);
                    excelsxzzallmodel.jwzzqjinfo = listjwzqj.Count().ToString();
                    jwzzqjinfo += listjwzqj.Count();
                    #endregion
                    #region 标本收取
                    //排序字段
                    var sortbbsq = Builders<Bbsqs>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listAppbbsq = new List<FilterDefinition<Bbsqs>>();
                    listAppbbsq.Add(Builders<Bbsqs>.Filter.Eq("isdelete", 0));
                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listAppbbsq.Add(Builders<Bbsqs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listAppbbsq.Add(Builders<Bbsqs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listAppbbsq.Add(Builders<Bbsqs>.Filter.Where(p => p.Szzyzzdw == item1.name));
                    var filterbbsq = Builders<Bbsqs>.Filter.And(listAppbbsq);

                    var listbbsq = await _IBbsqsRepository.Get(filterbbsq);
                    excelsxzzallmodel.bbsqinfo = listbbsq.Count().ToString();
                    bbsqinfo += listbbsq.Count();
                    #endregion
                    excelsxzzallmodel.zjzsinfo = (list.Count() + listmz.Count() + listjc.Count() + listxzzl.Count() + listxzzy.Count() + listjwzqj.Count() + listbbsq.Count()).ToString();
                    excelsxzzallinfolist.Add(excelsxzzallmodel);
                    zjzsinfo += list.Count() + listmz.Count() + listjc.Count() + listxzzl.Count() + listxzzy.Count() + listjwzqj.Count() + listbbsq.Count();
                }
                #endregion
                
            }
            #endregion
            #region 合计
            Model.excelsxzzallinfo excelsxzzallmodels = new Model.excelsxzzallinfo();
            excelsxzzallmodels.deptzzdw = "合计";
            excelsxzzallmodels.sszyinfo = sszyinfo.ToString();
            excelsxzzallmodels.ssmzinfo = ssmzinfo.ToString();
            excelsxzzallmodels.szjcinfo = szjcinfo.ToString();
            excelsxzzallmodels.xzzlinfo = xzzlinfo.ToString();
            excelsxzzallmodels.xzzyinfo = xzzyinfo.ToString();
            excelsxzzallmodels.jwzzqjinfo = jwzzqjinfo.ToString();
            excelsxzzallmodels.bbsqinfo = bbsqinfo.ToString();
            excelsxzzallmodels.zjzsinfo = zjzsinfo.ToString();
            excelsxzzallinfolist.Add(excelsxzzallmodels); 
            #endregion
            #endregion
            return Success("成功", excelsxzzallinfolist);

        }
        /// <summary>
        ///导出 医联体工作量统计表
        /// </summary>
        /// <param name="stardate"></param>
        /// <param name="endtime"></param>
        /// <param name="zzdwname"></param>
        /// <returns></returns>
        [HttpGet("excelsxzzallinfo")]
        [AllowAnonymous]
        public async Task<IActionResult> excelsxzzallinfo(string stardate, string endtime, string zzdwname)
        {
            try {
                #region 转诊单位条件检索
                List<Model.excelsxzzallinfo> excelsxzzallinfolist = new List<Model.excelsxzzallinfo>();

                #region 定义合计字段
                int sszyinfo = 0;
                int ssmzinfo = 0;
                int szjcinfo = 0;
                int xzzlinfo = 0;
                int xzzyinfo = 0;
                int jwzzqjinfo = 0;
                int bbsqinfo = 0;
                int zjzsinfo = 0;
                #endregion
                #region 转诊单位不为空
                if (zzdwname != null)
                {
                    string[] arrStr = zzdwname.Split(',');
                    foreach (var item in arrStr)
                    {
                        Model.excelsxzzallinfo excelsxzzallmodel = new Model.excelsxzzallinfo();
                        excelsxzzallmodel.deptzzdw = item;
                        #region 上转住院
                        var sort = Builders<Szzys>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listApp = new List<FilterDefinition<Szzys>>();
                        listApp.Add(Builders<Szzys>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzyzzdw == item));

                        var filter = Builders<Szzys>.Filter.And(listApp);

                        var list = await _ISzzysRepository.Get(filter);
                        excelsxzzallmodel.sszyinfo = list.Count().ToString();
                        sszyinfo += list.Count();
                        #endregion
                        #region 上转门诊
                        //排序字段
                        var sortmz = Builders<Szmzs>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppmz = new List<FilterDefinition<Szmzs>>();
                        listAppmz.Add(Builders<Szmzs>.Filter.Eq("isdelete", 0));

                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzyzzdw == item));
                        var filtermz = Builders<Szmzs>.Filter.And(listAppmz);

                        var listmz = await _ISzmzsRepository.Get(filtermz);
                        excelsxzzallmodel.ssmzinfo = listmz.Count().ToString();
                        ssmzinfo += listmz.Count();
                        #endregion
                        #region 上转检查
                        //排序字段
                        var sortjc = Builders<Szjcs>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppjc = new List<FilterDefinition<Szjcs>>();
                        listAppjc.Add(Builders<Szjcs>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzyzzdw == item));
                        var filterjc = Builders<Szjcs>.Filter.And(listAppjc);

                        var listjc = await _ISzjcsRepository.Get(filterjc);
                        excelsxzzallmodel.szjcinfo = listjc.Count().ToString();
                        szjcinfo += listjc.Count();
                        #endregion
                        #region 下转治疗
                        //排序字段
                        var sortxzzl = Builders<Xzzls>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppxzzl = new List<FilterDefinition<Xzzls>>();
                        listAppxzzl.Add(Builders<Xzzls>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppxzzl.Add(Builders<Xzzls>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppxzzl.Add(Builders<Xzzls>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        listAppxzzl.Add(Builders<Xzzls>.Filter.Where(p => p.Szzyzzdw == item));
                        var filterxzzl = Builders<Xzzls>.Filter.And(listAppxzzl);

                        var listxzzl = await _IXzzlsRepository.Get(filterxzzl);
                        excelsxzzallmodel.xzzlinfo = listxzzl.Count().ToString();
                        xzzlinfo += listxzzl.Count();
                        #endregion
                        #region 下转住院
                        //排序字段
                        var sortxzzy = Builders<Xzzys>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppxzzy = new List<FilterDefinition<Xzzys>>();
                        listAppxzzy.Add(Builders<Xzzys>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppxzzy.Add(Builders<Xzzys>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppxzzy.Add(Builders<Xzzys>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        listAppxzzy.Add(Builders<Xzzys>.Filter.Where(p => p.Szzyzzdw == item));
                        var filterxzzy = Builders<Xzzys>.Filter.And(listAppxzzy);

                        var listxzzy = await _IXzzysRepository.Get(filterxzzy);
                        excelsxzzallmodel.xzzyinfo = listxzzy.Count().ToString();
                        xzzyinfo += listxzzy.Count();
                        #endregion
                        #region 急危重症抢救
                        //排序字段
                        var sortjwzqj = Builders<Wjzqjs>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppjwzqj = new List<FilterDefinition<Wjzqjs>>();
                        listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Where(p => p.Szzyzzdw == item));
                        var filterjwzqj = Builders<Wjzqjs>.Filter.And(listAppjwzqj);

                        var listjwzqj = await _IWjzqjsRepository.Get(filterjwzqj);
                        excelsxzzallmodel.jwzzqjinfo = listjwzqj.Count().ToString();
                        jwzzqjinfo += listjwzqj.Count();
                        #endregion
                        #region 标本收取
                        //排序字段
                        var sortbbsq = Builders<Bbsqs>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppbbsq = new List<FilterDefinition<Bbsqs>>();
                        listAppbbsq.Add(Builders<Bbsqs>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppbbsq.Add(Builders<Bbsqs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppbbsq.Add(Builders<Bbsqs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        listAppbbsq.Add(Builders<Bbsqs>.Filter.Where(p => p.Szzyzzdw == item));
                        var filterbbsq = Builders<Bbsqs>.Filter.And(listAppbbsq);

                        var listbbsq = await _IBbsqsRepository.Get(filterbbsq);
                        excelsxzzallmodel.bbsqinfo = listbbsq.Count().ToString();
                        bbsqinfo += listbbsq.Count();
                        #endregion
                        excelsxzzallmodel.zjzsinfo = (list.Count() + listmz.Count() + listjc.Count() + listxzzl.Count() + listxzzy.Count() + listjwzqj.Count() + listbbsq.Count()).ToString();
                        excelsxzzallinfolist.Add(excelsxzzallmodel);
                        zjzsinfo += list.Count() + listmz.Count() + listjc.Count() + listxzzl.Count() + listxzzy.Count() + listjwzqj.Count() + listbbsq.Count();
                    }
                }
                #endregion
                #region 转诊单位为空
                else
                {
                    //先获取所有转诊单位
                    List<Model.Allzzdwinfo> allzzdwinfo = getAddzzdinfolist(stardate, endtime, zzdwname);

                    #region 循环遍历所有转诊单位
                    foreach (var item1 in allzzdwinfo)
                    {
                        Model.excelsxzzallinfo excelsxzzallmodel = new Model.excelsxzzallinfo();
                        excelsxzzallmodel.deptzzdw = item1.name;
                        #region 上转住院
                        var sort = Builders<Szzys>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listApp = new List<FilterDefinition<Szzys>>();
                        listApp.Add(Builders<Szzys>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzyzzdw == item1.name));

                        var filter = Builders<Szzys>.Filter.And(listApp);

                        var list = await _ISzzysRepository.Get(filter);
                        excelsxzzallmodel.sszyinfo = list.Count().ToString();
                        sszyinfo += list.Count();
                        #endregion
                        #region 上转门诊
                        //排序字段
                        var sortmz = Builders<Szmzs>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppmz = new List<FilterDefinition<Szmzs>>();
                        listAppmz.Add(Builders<Szmzs>.Filter.Eq("isdelete", 0));

                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzyzzdw == item1.name));
                        var filtermz = Builders<Szmzs>.Filter.And(listAppmz);

                        var listmz = await _ISzmzsRepository.Get(filtermz);
                        excelsxzzallmodel.ssmzinfo = listmz.Count().ToString();
                        ssmzinfo += listmz.Count();
                        #endregion
                        #region 上转检查
                        //排序字段
                        var sortjc = Builders<Szjcs>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppjc = new List<FilterDefinition<Szjcs>>();
                        listAppjc.Add(Builders<Szjcs>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzyzzdw == item1.name));
                        var filterjc = Builders<Szjcs>.Filter.And(listAppjc);

                        var listjc = await _ISzjcsRepository.Get(filterjc);
                        excelsxzzallmodel.szjcinfo = listjc.Count().ToString();
                        szjcinfo += listjc.Count();
                        #endregion
                        #region 下转治疗
                        //排序字段
                        var sortxzzl = Builders<Xzzls>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppxzzl = new List<FilterDefinition<Xzzls>>();
                        listAppxzzl.Add(Builders<Xzzls>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppxzzl.Add(Builders<Xzzls>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppxzzl.Add(Builders<Xzzls>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        listAppxzzl.Add(Builders<Xzzls>.Filter.Where(p => p.Szzyzzdw == item1.name));
                        var filterxzzl = Builders<Xzzls>.Filter.And(listAppxzzl);

                        var listxzzl = await _IXzzlsRepository.Get(filterxzzl);
                        excelsxzzallmodel.xzzlinfo = listxzzl.Count().ToString();
                        xzzlinfo += listxzzl.Count();
                        #endregion
                        #region 下转住院
                        //排序字段
                        var sortxzzy = Builders<Xzzys>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppxzzy = new List<FilterDefinition<Xzzys>>();
                        listAppxzzy.Add(Builders<Xzzys>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppxzzy.Add(Builders<Xzzys>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppxzzy.Add(Builders<Xzzys>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        listAppxzzy.Add(Builders<Xzzys>.Filter.Where(p => p.Szzyzzdw == item1.name));
                        var filterxzzy = Builders<Xzzys>.Filter.And(listAppxzzy);

                        var listxzzy = await _IXzzysRepository.Get(filterxzzy);
                        excelsxzzallmodel.xzzyinfo = listxzzy.Count().ToString();
                        xzzyinfo += listxzzy.Count();
                        #endregion
                        #region 急危重症抢救
                        //排序字段
                        var sortjwzqj = Builders<Wjzqjs>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppjwzqj = new List<FilterDefinition<Wjzqjs>>();
                        listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Where(p => p.Szzyzzdw == item1.name));
                        var filterjwzqj = Builders<Wjzqjs>.Filter.And(listAppjwzqj);

                        var listjwzqj = await _IWjzqjsRepository.Get(filterjwzqj);
                        excelsxzzallmodel.jwzzqjinfo = listjwzqj.Count().ToString();
                        jwzzqjinfo += listjwzqj.Count();
                        #endregion
                        #region 标本收取
                        //排序字段
                        var sortbbsq = Builders<Bbsqs>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppbbsq = new List<FilterDefinition<Bbsqs>>();
                        listAppbbsq.Add(Builders<Bbsqs>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppbbsq.Add(Builders<Bbsqs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppbbsq.Add(Builders<Bbsqs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        listAppbbsq.Add(Builders<Bbsqs>.Filter.Where(p => p.Szzyzzdw == item1.name));
                        var filterbbsq = Builders<Bbsqs>.Filter.And(listAppbbsq);

                        var listbbsq = await _IBbsqsRepository.Get(filterbbsq);
                        excelsxzzallmodel.bbsqinfo = listbbsq.Count().ToString();
                        bbsqinfo += listbbsq.Count();
                        #endregion
                        excelsxzzallmodel.zjzsinfo = (list.Count() + listmz.Count() + listjc.Count() + listxzzl.Count() + listxzzy.Count() + listjwzqj.Count() + listbbsq.Count()).ToString();
                        excelsxzzallinfolist.Add(excelsxzzallmodel);
                        zjzsinfo += list.Count() + listmz.Count() + listjc.Count() + listxzzl.Count() + listxzzy.Count() + listjwzqj.Count() + listbbsq.Count();
                    }
                    #endregion

                }
                #endregion
                #region 合计
                Model.excelsxzzallinfo excelsxzzallmodels = new Model.excelsxzzallinfo();
                excelsxzzallmodels.deptzzdw = "合计";
                excelsxzzallmodels.sszyinfo = sszyinfo.ToString();
                excelsxzzallmodels.ssmzinfo = ssmzinfo.ToString();
                excelsxzzallmodels.szjcinfo = szjcinfo.ToString();
                excelsxzzallmodels.xzzlinfo = xzzlinfo.ToString();
                excelsxzzallmodels.xzzyinfo = xzzyinfo.ToString();
                excelsxzzallmodels.jwzzqjinfo = jwzzqjinfo.ToString();
                excelsxzzallmodels.bbsqinfo = bbsqinfo.ToString();
                excelsxzzallmodels.zjzsinfo = zjzsinfo.ToString();
                excelsxzzallinfolist.Add(excelsxzzallmodels);
                #endregion
                #endregion

                NPOIHelper npoi = new NPOIHelper();
                var tb = ModelConvertHelper<Model.excelsxzzallinfo>.ListToTable(excelsxzzallinfolist, null);
                if (tb != null)
                {
                    //导出dtnew 
                    string[] cols = { "转诊单位", "上转住院", "上转门诊", "上转检查", "下转治疗", "下转住院", "急危重症抢救", "标本收取", "总结单数" };

                    byte[] sm = npoi.ExportToExcel1(tb, cols);
                    if (sm != null)
                    {
                        return File(sm, "application/vnd.ms-excel", "医联体工作量统计表.xlsx");
                    }
                    else
                    {
                        return Error("导出失败");
                    }
                }
                return Error("没有数据");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "导出异常");
                return Error("导出失败");
            }
        }
        /// <summary>
        /// 获取医联体单位
        /// </summary>
        /// <param name="stardate"></param>
        /// <param name="endtime"></param>
        /// <param name="zzdwname"></param>
        /// <returns></returns>
        public List<Model.Allzzdwinfo> getAddzzdinfolist(string stardate, string endtime, string zzdwname)
        {
            //先获取所有转诊单位
            List<Model.Allzzdwinfo> allzzdwinfo = new List<Model.Allzzdwinfo>();
            #region 上转住院
            var sort = Builders<Szzys>.Sort.Descending("Szzydjsj");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<Szzys>>();
            listApp.Add(Builders<Szzys>.Filter.Eq("isdelete", 0));
            if (!string.IsNullOrWhiteSpace(stardate))
            {
                listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
            }
            if (!string.IsNullOrWhiteSpace(endtime))
            {
                listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
            }
            #region 转诊单位条件检索
            var normallistAppor = new List<FilterDefinition<Szzys>>();
            if (zzdwname != null)
            {
                string[] arrStr = zzdwname.Split(',');
                foreach (var item in arrStr)
                {
                    normallistAppor.Add(Builders<Szzys>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                }
                listApp.Add(Builders<Szzys>.Filter.Or(normallistAppor));
            }
            #endregion
            var filter = Builders<Szzys>.Filter.And(listApp);

            var list =  _ISzzysRepository.Get(filter);
            #region 追加转诊单位
            foreach (var item in list.Result)
            {
                //判断
                var zzdwlis = allzzdwinfo.Where(p => p.name == item.Szzyzzdw).ToList();
                if (zzdwlis.Count > 0)
                {
                    continue;
                }
                Model.Allzzdwinfo allzzdwmodel = new Model.Allzzdwinfo();
                allzzdwmodel.name = item.Szzyzzdw;
                allzzdwinfo.Add(allzzdwmodel);
            }
            #endregion
            #endregion
            #region 上转门诊
            //排序字段
            var sortmz = Builders<Szmzs>.Sort.Descending("Szzydjsj");
            //根据条件查询集合
            var listAppmz = new List<FilterDefinition<Szmzs>>();
            listAppmz.Add(Builders<Szmzs>.Filter.Eq("isdelete", 0));

            if (!string.IsNullOrWhiteSpace(stardate))
            {
                listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
            }
            if (!string.IsNullOrWhiteSpace(endtime))
            {
                listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
            }
            #region 转诊单位条件检索
            var normallistAppormz = new List<FilterDefinition<Szmzs>>();
            if (zzdwname != null)
            {
                string[] arrStr = zzdwname.Split(',');
                foreach (var item in arrStr)
                {
                    normallistAppormz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                }
                listAppmz.Add(Builders<Szmzs>.Filter.Or(normallistAppormz));
            }
            #endregion
            var filtermz = Builders<Szmzs>.Filter.And(listAppmz);

            var listmz =  _ISzmzsRepository.Get(filtermz);
            #region 追加转诊单位
            foreach (var item in listmz.Result)
            {
                //判断
                var zzdwlis = allzzdwinfo.Where(p => p.name == item.Szzyzzdw).ToList();
                if (zzdwlis.Count > 0)
                {
                    continue;
                }
                Model.Allzzdwinfo allzzdwmodel = new Model.Allzzdwinfo();
                allzzdwmodel.name = item.Szzyzzdw;
                allzzdwinfo.Add(allzzdwmodel);
            }
            #endregion
            #endregion
            #region 上转检查
            //排序字段
            var sortjc = Builders<Szjcs>.Sort.Descending("Szzydjsj");
            //根据条件查询集合
            var listAppjc = new List<FilterDefinition<Szjcs>>();
            listAppjc.Add(Builders<Szjcs>.Filter.Eq("isdelete", 0));
            if (!string.IsNullOrWhiteSpace(stardate))
            {
                listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
            }
            if (!string.IsNullOrWhiteSpace(endtime))
            {
                listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
            }
            #region 转诊单位条件检索
            var normallistApporjc = new List<FilterDefinition<Szjcs>>();
            if (zzdwname != null)
            {
                string[] arrStr = zzdwname.Split(',');
                foreach (var item in arrStr)
                {
                    normallistApporjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                }
                listAppjc.Add(Builders<Szjcs>.Filter.Or(normallistApporjc));
            }
            #endregion
            var filterjc = Builders<Szjcs>.Filter.And(listAppjc);

            var listjc =  _ISzjcsRepository.Get(filterjc);
            #region 追加转诊单位
            foreach (var item in listjc.Result)
            {
                //判断
                var zzdwlis = allzzdwinfo.Where(p => p.name == item.Szzyzzdw).ToList();
                if (zzdwlis.Count > 0)
                {
                    continue;
                }
                Model.Allzzdwinfo allzzdwmodel = new Model.Allzzdwinfo();
                allzzdwmodel.name = item.Szzyzzdw;
                allzzdwinfo.Add(allzzdwmodel);
            }
            #endregion
            #endregion
            #region 下转治疗
            //排序字段
            var sortxzzl = Builders<Xzzls>.Sort.Descending("Szzydjsj");
            //根据条件查询集合
            var listAppxzzl = new List<FilterDefinition<Xzzls>>();
            listAppxzzl.Add(Builders<Xzzls>.Filter.Eq("isdelete", 0));
            if (!string.IsNullOrWhiteSpace(stardate))
            {
                listAppxzzl.Add(Builders<Xzzls>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
            }
            if (!string.IsNullOrWhiteSpace(endtime))
            {
                listAppxzzl.Add(Builders<Xzzls>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
            }
            #region 转诊单位条件检索
            var normallistApporxzzl = new List<FilterDefinition<Xzzls>>();
            if (zzdwname != null)
            {
                string[] arrStr = zzdwname.Split(',');
                foreach (var item in arrStr)
                {
                    normallistApporxzzl.Add(Builders<Xzzls>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                }
                listAppxzzl.Add(Builders<Xzzls>.Filter.Or(normallistApporxzzl));
            }
            #endregion
            var filterxzzl = Builders<Xzzls>.Filter.And(listAppxzzl);

            var listxzzl = _IXzzlsRepository.Get(filterxzzl);
            #region 追加转诊单位
            foreach (var item in listmz.Result)
            {
                //判断
                var zzdwlis = allzzdwinfo.Where(p => p.name == item.Szzyzzdw).ToList();
                if (zzdwlis.Count > 0)
                {
                    continue;
                }
                Model.Allzzdwinfo allzzdwmodel = new Model.Allzzdwinfo();
                allzzdwmodel.name = item.Szzyzzdw;
                allzzdwinfo.Add(allzzdwmodel);
            }
            #endregion
            #endregion
            #region 下转住院
            //排序字段
            var sortxzzy = Builders<Xzzys>.Sort.Descending("Szzydjsj");
            //根据条件查询集合
            var listAppxzzy = new List<FilterDefinition<Xzzys>>();
            listAppxzzy.Add(Builders<Xzzys>.Filter.Eq("isdelete", 0));
            if (!string.IsNullOrWhiteSpace(stardate))
            {
                listAppxzzy.Add(Builders<Xzzys>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
            }
            if (!string.IsNullOrWhiteSpace(endtime))
            {
                listAppxzzy.Add(Builders<Xzzys>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
            }
            #region 转诊单位条件检索
            var normallistApporxzzy = new List<FilterDefinition<Xzzys>>();
            if (zzdwname != null)
            {
                string[] arrStr = zzdwname.Split(',');
                foreach (var item in arrStr)
                {
                    normallistApporxzzy.Add(Builders<Xzzys>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                }
                listAppxzzy.Add(Builders<Xzzys>.Filter.Or(normallistApporxzzy));
            }
            #endregion
            var filterxzzy = Builders<Xzzys>.Filter.And(listAppxzzy);

            var listxzzy =  _IXzzysRepository.Get(filterxzzy);
            #region 追加转诊单位
            foreach (var item in listxzzy.Result)
            {
                //判断
                var zzdwlis = allzzdwinfo.Where(p => p.name == item.Szzyzzdw).ToList();
                if (zzdwlis.Count > 0)
                {
                    continue;
                }
                Model.Allzzdwinfo allzzdwmodel = new Model.Allzzdwinfo();
                allzzdwmodel.name = item.Szzyzzdw;
                allzzdwinfo.Add(allzzdwmodel);
            }
            #endregion
            #endregion
            #region 急危重症抢救
            //排序字段
            var sortjwzqj = Builders<Wjzqjs>.Sort.Descending("Szzydjsj");
            //根据条件查询集合
            var listAppjwzqj = new List<FilterDefinition<Wjzqjs>>();
            listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Eq("isdelete", 0));
            if (!string.IsNullOrWhiteSpace(stardate))
            {
                listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
            }
            if (!string.IsNullOrWhiteSpace(endtime))
            {
                listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
            }
            #region 转诊单位条件检索
            var normallistApporjwzzqj = new List<FilterDefinition<Wjzqjs>>();
            if (zzdwname != null)
            {
                string[] arrStr = zzdwname.Split(',');
                foreach (var item in arrStr)
                {
                    normallistApporjwzzqj.Add(Builders<Wjzqjs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                }
                listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Or(normallistApporjwzzqj));
            }
            #endregion
            var filterjwzqj = Builders<Wjzqjs>.Filter.And(listAppjwzqj);

            var listjwzqj = _IWjzqjsRepository.Get(filterjwzqj);
            #region 追加转诊单位
            foreach (var item in listjwzqj.Result)
            {
                //判断
                var zzdwlis = allzzdwinfo.Where(p => p.name == item.Szzyzzdw).ToList();
                if (zzdwlis.Count > 0)
                {
                    continue;
                }
                Model.Allzzdwinfo allzzdwmodel = new Model.Allzzdwinfo();
                allzzdwmodel.name = item.Szzyzzdw;
                allzzdwinfo.Add(allzzdwmodel);
            }
            #endregion
            #endregion
            #region 标本收取
            //排序字段
            var sortbbsq = Builders<Bbsqs>.Sort.Descending("Szzydjsj");
            //根据条件查询集合
            var listAppbbsq = new List<FilterDefinition<Bbsqs>>();
            listAppbbsq.Add(Builders<Bbsqs>.Filter.Eq("isdelete", 0));
            if (!string.IsNullOrWhiteSpace(stardate))
            {
                listAppbbsq.Add(Builders<Bbsqs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
            }
            if (!string.IsNullOrWhiteSpace(endtime))
            {
                listAppbbsq.Add(Builders<Bbsqs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
            }
            #region 转诊单位条件检索
            var normallistApporbbsq = new List<FilterDefinition<Bbsqs>>();
            if (zzdwname != null)
            {
                string[] arrStr = zzdwname.Split(',');
                foreach (var item in arrStr)
                {
                    normallistApporbbsq.Add(Builders<Bbsqs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                }
                listAppbbsq.Add(Builders<Bbsqs>.Filter.Or(normallistApporbbsq));
            }
            #endregion
            var filterbbsq = Builders<Bbsqs>.Filter.And(listAppbbsq);

            var listbbsq =  _IBbsqsRepository.Get(filterbbsq);
            #region 追加转诊单位
            foreach (var item in listbbsq.Result)
            {
                //判断
                var zzdwlis = allzzdwinfo.Where(p => p.name == item.Szzyzzdw).ToList();
                if (zzdwlis.Count > 0)
                {
                    continue;
                }
                Model.Allzzdwinfo allzzdwmodel = new Model.Allzzdwinfo();
                allzzdwmodel.name = item.Szzyzzdw;
                allzzdwinfo.Add(allzzdwmodel);
            }
            #endregion
            #endregion
            return allzzdwinfo;
        }
        #endregion
        #region 医联体工作趋势图
        //startime = Convert.ToDateTime(datenow.AddDays(1 - Convert.ToInt32(datenow.DayOfWeek.ToString("d"))).ToString("yyyy-MM-dd 00:00:00"));//本周周一
        //endtime = Convert.ToDateTime(datenow.AddDays(6).ToString("yyyy-MM-dd  23:59:59"));//本周周日
        [HttpGet("getyltwokerqstale")]
        public async Task<IActionResult> Getyltwokerqstale(string stardates, string endtimes, string zzdwname)
        {
            List<Model.yltwokerqstale> yltwokerqstalelist = new List<Model.yltwokerqstale>();
            if (zzdwname != null)
            {
                if (stardates != null && endtimes != null)
                {
                    DateTime dt1 = DateTime.Parse(stardates);
                    DateTime dt2 = DateTime.Parse(endtimes);

                    for (DateTime dt = dt1; dt <= dt2; dt = dt.AddDays(1))
                    {
                        Model.yltwokerqstale yltwokermode = new Model.yltwokerqstale();
                        yltwokermode.checkdate = dt.ToString("yyyy-MM-dd");
                        var stardate = dt.ToString("yyyy-MM-dd");
                        var endtime = dt.ToString("yyyy-MM-dd ");
                        #region 上转住院
                        var sort = Builders<Szzys>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listApp = new List<FilterDefinition<Szzys>>();
                        listApp.Add(Builders<Szzys>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        #region 转诊单位条件检索
                        var normallistAppor = new List<FilterDefinition<Szzys>>();
                        if (zzdwname != null)
                        {
                            string[] arrStr = zzdwname.Split(',');
                            foreach (var item in arrStr)
                            {
                                normallistAppor.Add(Builders<Szzys>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                            }
                            listApp.Add(Builders<Szzys>.Filter.Or(normallistAppor));
                        }
                        #endregion
                        var filter = Builders<Szzys>.Filter.And(listApp);
                        var list = await _ISzzysRepository.Get(filter);
                        #endregion
                        #region 上转门诊
                        //排序字段
                        var sortmz = Builders<Szmzs>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppmz = new List<FilterDefinition<Szmzs>>();
                        listAppmz.Add(Builders<Szmzs>.Filter.Eq("isdelete", 0));

                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        #region 转诊单位条件检索
                        var normallistAppormz = new List<FilterDefinition<Szmzs>>();
                        if (zzdwname != null)
                        {
                            string[] arrStr = zzdwname.Split(',');
                            foreach (var item in arrStr)
                            {
                                normallistAppormz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                            }
                            listAppmz.Add(Builders<Szmzs>.Filter.Or(normallistAppormz));
                        }
                        #endregion
                        var filtermz = Builders<Szmzs>.Filter.And(listAppmz);

                        var listmz = await _ISzmzsRepository.Get(filtermz);
                        #endregion
                        #region 上转检查
                        //排序字段
                        var sortjc = Builders<Szjcs>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppjc = new List<FilterDefinition<Szjcs>>();
                        listAppjc.Add(Builders<Szjcs>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        #region 转诊单位条件检索
                        var normallistApporjc = new List<FilterDefinition<Szjcs>>();
                        if (zzdwname != null)
                        {
                            string[] arrStr = zzdwname.Split(',');
                            foreach (var item in arrStr)
                            {
                                normallistApporjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                            }
                            listAppjc.Add(Builders<Szjcs>.Filter.Or(normallistApporjc));
                        }
                        #endregion
                        var filterjc = Builders<Szjcs>.Filter.And(listAppjc);

                        var listjc = await _ISzjcsRepository.Get(filterjc);

                        #endregion
                        #region 下转治疗
                        //排序字段
                        var sortxzzl = Builders<Xzzls>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppxzzl = new List<FilterDefinition<Xzzls>>();
                        listAppxzzl.Add(Builders<Xzzls>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppxzzl.Add(Builders<Xzzls>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppxzzl.Add(Builders<Xzzls>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        #region 转诊单位条件检索
                        var normallistApporxzzl = new List<FilterDefinition<Xzzls>>();
                        if (zzdwname != null)
                        {
                            string[] arrStr = zzdwname.Split(',');
                            foreach (var item in arrStr)
                            {
                                normallistApporxzzl.Add(Builders<Xzzls>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                            }
                            listAppxzzl.Add(Builders<Xzzls>.Filter.Or(normallistApporxzzl));
                        }
                        #endregion
                        var filterxzzl = Builders<Xzzls>.Filter.And(listAppxzzl);

                        var listxzzl = await _IXzzlsRepository.Get(filterxzzl);
                        #endregion
                        #region 下转住院
                        //排序字段
                        var sortxzzy = Builders<Xzzys>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppxzzy = new List<FilterDefinition<Xzzys>>();
                        listAppxzzy.Add(Builders<Xzzys>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppxzzy.Add(Builders<Xzzys>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppxzzy.Add(Builders<Xzzys>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        #region 转诊单位条件检索
                        var normallistApporxzzy = new List<FilterDefinition<Xzzys>>();
                        if (zzdwname != null)
                        {
                            string[] arrStr = zzdwname.Split(',');
                            foreach (var item in arrStr)
                            {
                                normallistApporxzzy.Add(Builders<Xzzys>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                            }
                            listAppxzzy.Add(Builders<Xzzys>.Filter.Or(normallistApporxzzy));
                        }
                        #endregion
                        var filterxzzy = Builders<Xzzys>.Filter.And(listAppxzzy);

                        var listxzzy = await _IXzzysRepository.Get(filterxzzy);
                        #endregion
                        #region 急危重症抢救
                        //排序字段
                        var sortjwzqj = Builders<Wjzqjs>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppjwzqj = new List<FilterDefinition<Wjzqjs>>();
                        listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        #region 转诊单位条件检索
                        var normallistApporjwzzqj = new List<FilterDefinition<Wjzqjs>>();
                        if (zzdwname != null)
                        {
                            string[] arrStr = zzdwname.Split(',');
                            foreach (var item in arrStr)
                            {
                                normallistApporjwzzqj.Add(Builders<Wjzqjs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                            }
                            listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Or(normallistApporjwzzqj));
                        }
                        #endregion
                        var filterjwzqj = Builders<Wjzqjs>.Filter.And(listAppjwzqj);

                        var listjwzqj = await _IWjzqjsRepository.Get(filterjwzqj);
                        #endregion
                        #region 标本收取
                        //排序字段
                        var sortbbsq = Builders<Bbsqs>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppbbsq = new List<FilterDefinition<Bbsqs>>();
                        listAppbbsq.Add(Builders<Bbsqs>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppbbsq.Add(Builders<Bbsqs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppbbsq.Add(Builders<Bbsqs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        #region 转诊单位条件检索
                        var normallistApporbbsq = new List<FilterDefinition<Bbsqs>>();
                        if (zzdwname != null)
                        {
                            string[] arrStr = zzdwname.Split(',');
                            foreach (var item in arrStr)
                            {
                                normallistApporbbsq.Add(Builders<Bbsqs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                            }
                            listAppbbsq.Add(Builders<Bbsqs>.Filter.Or(normallistApporbbsq));
                        }
                        #endregion
                        var filterbbsq = Builders<Bbsqs>.Filter.And(listAppbbsq);

                        var listbbsq = await _IBbsqsRepository.Get(filterbbsq);
                        #endregion
                        yltwokermode.allcount = (list.Count() + listmz.Count() + listjc.Count() + listxzzl.Count() + listxzzy.Count() + listjwzqj.Count() + listbbsq.Count()).ToString();
                        yltwokerqstalelist.Add(yltwokermode);
                    }
                }
                #region 默认为本周
                else
                {

                    DateTime dt1 = Convert.ToDateTime(DateTime.Now.AddDays(1 - Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d"))).ToString("yyyy-MM-dd"));//本周周一
                    DateTime dt2 = Convert.ToDateTime(dt1.AddDays(6).ToString("yyyy-MM-dd"));//本周周日
                    for (DateTime dt = dt1; dt <= dt2; dt = dt.AddDays(1))
                    {
                        Model.yltwokerqstale yltwokermode = new Model.yltwokerqstale();
                        yltwokermode.checkdate = dt.ToString("yyyy-MM-dd");
                        var stardate = dt.ToString("yyyy-MM-dd");
                        var endtime = dt.ToString("yyyy-MM-dd");
                        #region 上转住院
                        var sort = Builders<Szzys>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listApp = new List<FilterDefinition<Szzys>>();
                        listApp.Add(Builders<Szzys>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        #region 转诊单位条件检索
                        var normallistAppor = new List<FilterDefinition<Szzys>>();
                        if (zzdwname != null)
                        {
                            string[] arrStr = zzdwname.Split(',');
                            foreach (var item in arrStr)
                            {
                                normallistAppor.Add(Builders<Szzys>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                            }
                            listApp.Add(Builders<Szzys>.Filter.Or(normallistAppor));
                        }
                        #endregion
                        var filter = Builders<Szzys>.Filter.And(listApp);
                        var list = await _ISzzysRepository.Get(filter);
                        #endregion
                        #region 上转门诊
                        //排序字段
                        var sortmz = Builders<Szmzs>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppmz = new List<FilterDefinition<Szmzs>>();
                        listAppmz.Add(Builders<Szmzs>.Filter.Eq("isdelete", 0));

                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        #region 转诊单位条件检索
                        var normallistAppormz = new List<FilterDefinition<Szmzs>>();
                        if (zzdwname != null)
                        {
                            string[] arrStr = zzdwname.Split(',');
                            foreach (var item in arrStr)
                            {
                                normallistAppormz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                            }
                            listAppmz.Add(Builders<Szmzs>.Filter.Or(normallistAppormz));
                        }
                        #endregion
                        var filtermz = Builders<Szmzs>.Filter.And(listAppmz);

                        var listmz = await _ISzmzsRepository.Get(filtermz);
                        #endregion
                        #region 上转检查
                        //排序字段
                        var sortjc = Builders<Szjcs>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppjc = new List<FilterDefinition<Szjcs>>();
                        listAppjc.Add(Builders<Szjcs>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        #region 转诊单位条件检索
                        var normallistApporjc = new List<FilterDefinition<Szjcs>>();
                        if (zzdwname != null)
                        {
                            string[] arrStr = zzdwname.Split(',');
                            foreach (var item in arrStr)
                            {
                                normallistApporjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                            }
                            listAppjc.Add(Builders<Szjcs>.Filter.Or(normallistApporjc));
                        }
                        #endregion
                        var filterjc = Builders<Szjcs>.Filter.And(listAppjc);

                        var listjc = await _ISzjcsRepository.Get(filterjc);

                        #endregion
                        #region 下转治疗
                        //排序字段
                        var sortxzzl = Builders<Xzzls>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppxzzl = new List<FilterDefinition<Xzzls>>();
                        listAppxzzl.Add(Builders<Xzzls>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppxzzl.Add(Builders<Xzzls>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppxzzl.Add(Builders<Xzzls>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        #region 转诊单位条件检索
                        var normallistApporxzzl = new List<FilterDefinition<Xzzls>>();
                        if (zzdwname != null)
                        {
                            string[] arrStr = zzdwname.Split(',');
                            foreach (var item in arrStr)
                            {
                                normallistApporxzzl.Add(Builders<Xzzls>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                            }
                            listAppxzzl.Add(Builders<Xzzls>.Filter.Or(normallistApporxzzl));
                        }
                        #endregion
                        var filterxzzl = Builders<Xzzls>.Filter.And(listAppxzzl);

                        var listxzzl = await _IXzzlsRepository.Get(filterxzzl);
                        #endregion
                        #region 下转住院
                        //排序字段
                        var sortxzzy = Builders<Xzzys>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppxzzy = new List<FilterDefinition<Xzzys>>();
                        listAppxzzy.Add(Builders<Xzzys>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppxzzy.Add(Builders<Xzzys>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppxzzy.Add(Builders<Xzzys>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        #region 转诊单位条件检索
                        var normallistApporxzzy = new List<FilterDefinition<Xzzys>>();
                        if (zzdwname != null)
                        {
                            string[] arrStr = zzdwname.Split(',');
                            foreach (var item in arrStr)
                            {
                                normallistApporxzzy.Add(Builders<Xzzys>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                            }
                            listAppxzzy.Add(Builders<Xzzys>.Filter.Or(normallistApporxzzy));
                        }
                        #endregion
                        var filterxzzy = Builders<Xzzys>.Filter.And(listAppxzzy);

                        var listxzzy = await _IXzzysRepository.Get(filterxzzy);
                        #endregion
                        #region 急危重症抢救
                        //排序字段
                        var sortjwzqj = Builders<Wjzqjs>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppjwzqj = new List<FilterDefinition<Wjzqjs>>();
                        listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        #region 转诊单位条件检索
                        var normallistApporjwzzqj = new List<FilterDefinition<Wjzqjs>>();
                        if (zzdwname != null)
                        {
                            string[] arrStr = zzdwname.Split(',');
                            foreach (var item in arrStr)
                            {
                                normallistApporjwzzqj.Add(Builders<Wjzqjs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                            }
                            listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Or(normallistApporjwzzqj));
                        }
                        #endregion
                        var filterjwzqj = Builders<Wjzqjs>.Filter.And(listAppjwzqj);

                        var listjwzqj = await _IWjzqjsRepository.Get(filterjwzqj);
                        #endregion
                        #region 标本收取
                        //排序字段
                        var sortbbsq = Builders<Bbsqs>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppbbsq = new List<FilterDefinition<Bbsqs>>();
                        listAppbbsq.Add(Builders<Bbsqs>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppbbsq.Add(Builders<Bbsqs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppbbsq.Add(Builders<Bbsqs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        #region 转诊单位条件检索
                        var normallistApporbbsq = new List<FilterDefinition<Bbsqs>>();
                        if (zzdwname != null)
                        {
                            string[] arrStr = zzdwname.Split(',');
                            foreach (var item in arrStr)
                            {
                                normallistApporbbsq.Add(Builders<Bbsqs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                            }
                            listAppbbsq.Add(Builders<Bbsqs>.Filter.Or(normallistApporbbsq));
                        }
                        #endregion
                        var filterbbsq = Builders<Bbsqs>.Filter.And(listAppbbsq);

                        var listbbsq = await _IBbsqsRepository.Get(filterbbsq);
                        #endregion
                        yltwokermode.allcount = (list.Count() + listmz.Count() + listjc.Count() + listxzzl.Count() + listxzzy.Count() + listjwzqj.Count() + listbbsq.Count()).ToString();
                        yltwokerqstalelist.Add(yltwokermode);
                    }
                }
                #endregion
            }
            else
            {
                if (stardates != null && endtimes != null)
                {
                    DateTime dt1 = DateTime.Parse(stardates);
                    DateTime dt2 = DateTime.Parse(endtimes);

                    for (DateTime dt = dt1; dt <= dt2; dt = dt.AddDays(1))
                    {
                        Model.yltwokerqstale yltwokermode = new Model.yltwokerqstale();
                        yltwokermode.checkdate = dt.ToString("yyyy-MM-dd");
                       var stardate  = dt.ToString("yyyy-MM-dd");
                        var endtime   = dt.ToString("yyyy-MM-dd");
                        #region 上转住院
                        var sort = Builders<Szzys>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listApp = new List<FilterDefinition<Szzys>>();
                        listApp.Add(Builders<Szzys>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        var filter = Builders<Szzys>.Filter.And(listApp);
                        var list =await _ISzzysRepository.Get(filter);
                        #endregion
                        #region 上转门诊
                        //排序字段
                        var sortmz = Builders<Szmzs>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppmz = new List<FilterDefinition<Szmzs>>();
                        listAppmz.Add(Builders<Szmzs>.Filter.Eq("isdelete", 0));

                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                       
                        var filtermz = Builders<Szmzs>.Filter.And(listAppmz);

                        var listmz = await _ISzmzsRepository.Get(filtermz);
                        #endregion
                        #region 上转检查
                        //排序字段
                        var sortjc = Builders<Szjcs>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppjc = new List<FilterDefinition<Szjcs>>();
                        listAppjc.Add(Builders<Szjcs>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                    
                        var filterjc = Builders<Szjcs>.Filter.And(listAppjc);

                        var listjc = await _ISzjcsRepository.Get(filterjc);

                        #endregion
                        #region 下转治疗
                        //排序字段
                        var sortxzzl = Builders<Xzzls>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppxzzl = new List<FilterDefinition<Xzzls>>();
                        listAppxzzl.Add(Builders<Xzzls>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppxzzl.Add(Builders<Xzzls>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppxzzl.Add(Builders<Xzzls>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        var filterxzzl = Builders<Xzzls>.Filter.And(listAppxzzl);

                        var listxzzl = await _IXzzlsRepository.Get(filterxzzl);
                        #endregion
                        #region 下转住院
                        //排序字段
                        var sortxzzy = Builders<Xzzys>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppxzzy = new List<FilterDefinition<Xzzys>>();
                        listAppxzzy.Add(Builders<Xzzys>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppxzzy.Add(Builders<Xzzys>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppxzzy.Add(Builders<Xzzys>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        var filterxzzy = Builders<Xzzys>.Filter.And(listAppxzzy);

                        var listxzzy = await _IXzzysRepository.Get(filterxzzy);
                        #endregion
                        #region 急危重症抢救
                        //排序字段
                        var sortjwzqj = Builders<Wjzqjs>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppjwzqj = new List<FilterDefinition<Wjzqjs>>();
                        listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        var filterjwzqj = Builders<Wjzqjs>.Filter.And(listAppjwzqj);

                        var listjwzqj = await _IWjzqjsRepository.Get(filterjwzqj);
                        #endregion
                        #region 标本收取
                        //排序字段
                        var sortbbsq = Builders<Bbsqs>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppbbsq = new List<FilterDefinition<Bbsqs>>();
                        listAppbbsq.Add(Builders<Bbsqs>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppbbsq.Add(Builders<Bbsqs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppbbsq.Add(Builders<Bbsqs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        var filterbbsq = Builders<Bbsqs>.Filter.And(listAppbbsq);

                        var listbbsq = await _IBbsqsRepository.Get(filterbbsq);
                        #endregion
                        yltwokermode.allcount = (list.Count() + listmz.Count() + listjc.Count() + listxzzl.Count() + listxzzy.Count() + listjwzqj.Count() + listbbsq.Count()).ToString();
                        yltwokerqstalelist.Add(yltwokermode);
                    }
                }
                #region 默认为本周
                else
                {

                    DateTime dt1 = Convert.ToDateTime(DateTime.Now.AddDays(1 - Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d"))).ToString("yyyy-MM-dd"));//本周周一
                    DateTime dt2 = Convert.ToDateTime(dt1.AddDays(6).ToString("yyyy-MM-dd"));//本周周日
                    for (DateTime dt = dt1; dt <= dt2; dt = dt.AddDays(1))
                    {
                        //dt为开始时间 dt2为结束时间
                        Model.yltwokerqstale yltwokermode = new Model.yltwokerqstale();
                        yltwokermode.checkdate = dt.ToString("yyyy-MM-dd");
                        
                       var stardate = dt.ToString("yyyy-MM-dd");
                       var endtime = dt.ToString("yyyy-MM-dd ");
                        #region 上转住院
                        var sort = Builders<Szzys>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listApp = new List<FilterDefinition<Szzys>>();
                        listApp.Add(Builders<Szzys>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        var filter = Builders<Szzys>.Filter.And(listApp);
                        var list = await _ISzzysRepository.Get(filter);
                        #endregion
                        #region 上转门诊
                        //排序字段
                        var sortmz = Builders<Szmzs>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppmz = new List<FilterDefinition<Szmzs>>();
                        listAppmz.Add(Builders<Szmzs>.Filter.Eq("isdelete", 0));

                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }

                        var filtermz = Builders<Szmzs>.Filter.And(listAppmz);

                        var listmz = await _ISzmzsRepository.Get(filtermz);
                        #endregion
                        #region 上转检查
                        //排序字段
                        var sortjc = Builders<Szjcs>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppjc = new List<FilterDefinition<Szjcs>>();
                        listAppjc.Add(Builders<Szjcs>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }

                        var filterjc = Builders<Szjcs>.Filter.And(listAppjc);

                        var listjc = await _ISzjcsRepository.Get(filterjc);

                        #endregion
                        #region 下转治疗
                        //排序字段
                        var sortxzzl = Builders<Xzzls>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppxzzl = new List<FilterDefinition<Xzzls>>();
                        listAppxzzl.Add(Builders<Xzzls>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppxzzl.Add(Builders<Xzzls>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppxzzl.Add(Builders<Xzzls>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        var filterxzzl = Builders<Xzzls>.Filter.And(listAppxzzl);

                        var listxzzl = await _IXzzlsRepository.Get(filterxzzl);
                        #endregion
                        #region 下转住院
                        //排序字段
                        var sortxzzy = Builders<Xzzys>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppxzzy = new List<FilterDefinition<Xzzys>>();
                        listAppxzzy.Add(Builders<Xzzys>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppxzzy.Add(Builders<Xzzys>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppxzzy.Add(Builders<Xzzys>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        var filterxzzy = Builders<Xzzys>.Filter.And(listAppxzzy);

                        var listxzzy = await _IXzzysRepository.Get(filterxzzy);
                        #endregion
                        #region 急危重症抢救
                        //排序字段
                        var sortjwzqj = Builders<Wjzqjs>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppjwzqj = new List<FilterDefinition<Wjzqjs>>();
                        listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        var filterjwzqj = Builders<Wjzqjs>.Filter.And(listAppjwzqj);

                        var listjwzqj = await _IWjzqjsRepository.Get(filterjwzqj);
                        #endregion
                        #region 标本收取
                        //排序字段
                        var sortbbsq = Builders<Bbsqs>.Sort.Descending("Szzydjsj");
                        //根据条件查询集合
                        var listAppbbsq = new List<FilterDefinition<Bbsqs>>();
                        listAppbbsq.Add(Builders<Bbsqs>.Filter.Eq("isdelete", 0));
                        if (!string.IsNullOrWhiteSpace(stardate))
                        {
                            listAppbbsq.Add(Builders<Bbsqs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                        }
                        if (!string.IsNullOrWhiteSpace(endtime))
                        {
                            listAppbbsq.Add(Builders<Bbsqs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                        }
                        var filterbbsq = Builders<Bbsqs>.Filter.And(listAppbbsq);

                        var listbbsq = await _IBbsqsRepository.Get(filterbbsq);
                        #endregion
                        yltwokermode.allcount = (list.Count() + listmz.Count() + listjc.Count() + listxzzl.Count() + listxzzy.Count() + listjwzqj.Count() + listbbsq.Count()).ToString();
                        yltwokerqstalelist.Add(yltwokermode);
                    }
                } 
                #endregion
            }
           
            return Success("成功", yltwokerqstalelist);
        }
        /// <summary>
        /// 导出医联体工作趋势图表信息
        /// </summary>
        /// <param name="stardates"></param>
        /// <param name="endtimes"></param>
        /// <param name="zzdwname"></param>
        /// <returns></returns>
        [HttpGet("excelyltwokerqstale")]
        [AllowAnonymous]
        public async Task<IActionResult> excelyltwokerqstale(string stardates, string endtimes, string zzdwname)
        {
            try
            { 
                List<Model.yltwokerqstale> yltwokerqstalelist = new List<Model.yltwokerqstale>();
                if (zzdwname != null)
                {
                    if (stardates != null && endtimes != null)
                    {
                        DateTime dt1 = DateTime.Parse(stardates);
                        DateTime dt2 = DateTime.Parse(endtimes);

                        for (DateTime dt = dt1; dt <= dt2; dt = dt.AddDays(1))
                        {
                            Model.yltwokerqstale yltwokermode = new Model.yltwokerqstale();
                            yltwokermode.checkdate = dt.ToString("yyyy-MM-dd");
                            var stardate = dt.ToString("yyyy-MM-dd");
                            var endtime = dt.ToString("yyyy-MM-dd ");
                            #region 上转住院
                            var sort = Builders<Szzys>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listApp = new List<FilterDefinition<Szzys>>();
                            listApp.Add(Builders<Szzys>.Filter.Eq("isdelete", 0));
                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }
                            #region 转诊单位条件检索
                            var normallistAppor = new List<FilterDefinition<Szzys>>();
                            if (zzdwname != null)
                            {
                                string[] arrStr = zzdwname.Split(',');
                                foreach (var item in arrStr)
                                {
                                    normallistAppor.Add(Builders<Szzys>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                                }
                                listApp.Add(Builders<Szzys>.Filter.Or(normallistAppor));
                            }
                            #endregion
                            var filter = Builders<Szzys>.Filter.And(listApp);
                            var list = await _ISzzysRepository.Get(filter);
                            #endregion
                            #region 上转门诊
                            //排序字段
                            var sortmz = Builders<Szmzs>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listAppmz = new List<FilterDefinition<Szmzs>>();
                            listAppmz.Add(Builders<Szmzs>.Filter.Eq("isdelete", 0));

                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }
                            #region 转诊单位条件检索
                            var normallistAppormz = new List<FilterDefinition<Szmzs>>();
                            if (zzdwname != null)
                            {
                                string[] arrStr = zzdwname.Split(',');
                                foreach (var item in arrStr)
                                {
                                    normallistAppormz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                                }
                                listAppmz.Add(Builders<Szmzs>.Filter.Or(normallistAppormz));
                            }
                            #endregion
                            var filtermz = Builders<Szmzs>.Filter.And(listAppmz);

                            var listmz = await _ISzmzsRepository.Get(filtermz);
                            #endregion
                            #region 上转检查
                            //排序字段
                            var sortjc = Builders<Szjcs>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listAppjc = new List<FilterDefinition<Szjcs>>();
                            listAppjc.Add(Builders<Szjcs>.Filter.Eq("isdelete", 0));
                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }
                            #region 转诊单位条件检索
                            var normallistApporjc = new List<FilterDefinition<Szjcs>>();
                            if (zzdwname != null)
                            {
                                string[] arrStr = zzdwname.Split(',');
                                foreach (var item in arrStr)
                                {
                                    normallistApporjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                                }
                                listAppjc.Add(Builders<Szjcs>.Filter.Or(normallistApporjc));
                            }
                            #endregion
                            var filterjc = Builders<Szjcs>.Filter.And(listAppjc);

                            var listjc = await _ISzjcsRepository.Get(filterjc);

                            #endregion
                            #region 下转治疗
                            //排序字段
                            var sortxzzl = Builders<Xzzls>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listAppxzzl = new List<FilterDefinition<Xzzls>>();
                            listAppxzzl.Add(Builders<Xzzls>.Filter.Eq("isdelete", 0));
                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listAppxzzl.Add(Builders<Xzzls>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listAppxzzl.Add(Builders<Xzzls>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }
                            #region 转诊单位条件检索
                            var normallistApporxzzl = new List<FilterDefinition<Xzzls>>();
                            if (zzdwname != null)
                            {
                                string[] arrStr = zzdwname.Split(',');
                                foreach (var item in arrStr)
                                {
                                    normallistApporxzzl.Add(Builders<Xzzls>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                                }
                                listAppxzzl.Add(Builders<Xzzls>.Filter.Or(normallistApporxzzl));
                            }
                            #endregion
                            var filterxzzl = Builders<Xzzls>.Filter.And(listAppxzzl);

                            var listxzzl = await _IXzzlsRepository.Get(filterxzzl);
                            #endregion
                            #region 下转住院
                            //排序字段
                            var sortxzzy = Builders<Xzzys>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listAppxzzy = new List<FilterDefinition<Xzzys>>();
                            listAppxzzy.Add(Builders<Xzzys>.Filter.Eq("isdelete", 0));
                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listAppxzzy.Add(Builders<Xzzys>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listAppxzzy.Add(Builders<Xzzys>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }
                            #region 转诊单位条件检索
                            var normallistApporxzzy = new List<FilterDefinition<Xzzys>>();
                            if (zzdwname != null)
                            {
                                string[] arrStr = zzdwname.Split(',');
                                foreach (var item in arrStr)
                                {
                                    normallistApporxzzy.Add(Builders<Xzzys>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                                }
                                listAppxzzy.Add(Builders<Xzzys>.Filter.Or(normallistApporxzzy));
                            }
                            #endregion
                            var filterxzzy = Builders<Xzzys>.Filter.And(listAppxzzy);

                            var listxzzy = await _IXzzysRepository.Get(filterxzzy);
                            #endregion
                            #region 急危重症抢救
                            //排序字段
                            var sortjwzqj = Builders<Wjzqjs>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listAppjwzqj = new List<FilterDefinition<Wjzqjs>>();
                            listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Eq("isdelete", 0));
                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }
                            #region 转诊单位条件检索
                            var normallistApporjwzzqj = new List<FilterDefinition<Wjzqjs>>();
                            if (zzdwname != null)
                            {
                                string[] arrStr = zzdwname.Split(',');
                                foreach (var item in arrStr)
                                {
                                    normallistApporjwzzqj.Add(Builders<Wjzqjs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                                }
                                listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Or(normallistApporjwzzqj));
                            }
                            #endregion
                            var filterjwzqj = Builders<Wjzqjs>.Filter.And(listAppjwzqj);

                            var listjwzqj = await _IWjzqjsRepository.Get(filterjwzqj);
                            #endregion
                            #region 标本收取
                            //排序字段
                            var sortbbsq = Builders<Bbsqs>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listAppbbsq = new List<FilterDefinition<Bbsqs>>();
                            listAppbbsq.Add(Builders<Bbsqs>.Filter.Eq("isdelete", 0));
                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listAppbbsq.Add(Builders<Bbsqs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listAppbbsq.Add(Builders<Bbsqs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }
                            #region 转诊单位条件检索
                            var normallistApporbbsq = new List<FilterDefinition<Bbsqs>>();
                            if (zzdwname != null)
                            {
                                string[] arrStr = zzdwname.Split(',');
                                foreach (var item in arrStr)
                                {
                                    normallistApporbbsq.Add(Builders<Bbsqs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                                }
                                listAppbbsq.Add(Builders<Bbsqs>.Filter.Or(normallistApporbbsq));
                            }
                            #endregion
                            var filterbbsq = Builders<Bbsqs>.Filter.And(listAppbbsq);

                            var listbbsq = await _IBbsqsRepository.Get(filterbbsq);
                            #endregion
                            yltwokermode.allcount = (list.Count() + listmz.Count() + listjc.Count() + listxzzl.Count() + listxzzy.Count() + listjwzqj.Count() + listbbsq.Count()).ToString();
                            yltwokerqstalelist.Add(yltwokermode);
                        }
                    }
                    #region 默认为本周
                    else
                    {

                        DateTime dt1 = Convert.ToDateTime(DateTime.Now.AddDays(1 - Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d"))).ToString("yyyy-MM-dd"));//本周周一
                        DateTime dt2 = Convert.ToDateTime(dt1.AddDays(6).ToString("yyyy-MM-dd"));//本周周日
                        for (DateTime dt = dt1; dt <= dt2; dt = dt.AddDays(1))
                        {
                            Model.yltwokerqstale yltwokermode = new Model.yltwokerqstale();
                            yltwokermode.checkdate = dt.ToString("yyyy-MM-dd");
                            var stardate = dt.ToString("yyyy-MM-dd");
                            var endtime = dt.ToString("yyyy-MM-dd");
                            #region 上转住院
                            var sort = Builders<Szzys>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listApp = new List<FilterDefinition<Szzys>>();
                            listApp.Add(Builders<Szzys>.Filter.Eq("isdelete", 0));
                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }
                            #region 转诊单位条件检索
                            var normallistAppor = new List<FilterDefinition<Szzys>>();
                            if (zzdwname != null)
                            {
                                string[] arrStr = zzdwname.Split(',');
                                foreach (var item in arrStr)
                                {
                                    normallistAppor.Add(Builders<Szzys>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                                }
                                listApp.Add(Builders<Szzys>.Filter.Or(normallistAppor));
                            }
                            #endregion
                            var filter = Builders<Szzys>.Filter.And(listApp);
                            var list = await _ISzzysRepository.Get(filter);
                            #endregion
                            #region 上转门诊
                            //排序字段
                            var sortmz = Builders<Szmzs>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listAppmz = new List<FilterDefinition<Szmzs>>();
                            listAppmz.Add(Builders<Szmzs>.Filter.Eq("isdelete", 0));

                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }
                            #region 转诊单位条件检索
                            var normallistAppormz = new List<FilterDefinition<Szmzs>>();
                            if (zzdwname != null)
                            {
                                string[] arrStr = zzdwname.Split(',');
                                foreach (var item in arrStr)
                                {
                                    normallistAppormz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                                }
                                listAppmz.Add(Builders<Szmzs>.Filter.Or(normallistAppormz));
                            }
                            #endregion
                            var filtermz = Builders<Szmzs>.Filter.And(listAppmz);

                            var listmz = await _ISzmzsRepository.Get(filtermz);
                            #endregion
                            #region 上转检查
                            //排序字段
                            var sortjc = Builders<Szjcs>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listAppjc = new List<FilterDefinition<Szjcs>>();
                            listAppjc.Add(Builders<Szjcs>.Filter.Eq("isdelete", 0));
                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }
                            #region 转诊单位条件检索
                            var normallistApporjc = new List<FilterDefinition<Szjcs>>();
                            if (zzdwname != null)
                            {
                                string[] arrStr = zzdwname.Split(',');
                                foreach (var item in arrStr)
                                {
                                    normallistApporjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                                }
                                listAppjc.Add(Builders<Szjcs>.Filter.Or(normallistApporjc));
                            }
                            #endregion
                            var filterjc = Builders<Szjcs>.Filter.And(listAppjc);

                            var listjc = await _ISzjcsRepository.Get(filterjc);

                            #endregion
                            #region 下转治疗
                            //排序字段
                            var sortxzzl = Builders<Xzzls>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listAppxzzl = new List<FilterDefinition<Xzzls>>();
                            listAppxzzl.Add(Builders<Xzzls>.Filter.Eq("isdelete", 0));
                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listAppxzzl.Add(Builders<Xzzls>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listAppxzzl.Add(Builders<Xzzls>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }
                            #region 转诊单位条件检索
                            var normallistApporxzzl = new List<FilterDefinition<Xzzls>>();
                            if (zzdwname != null)
                            {
                                string[] arrStr = zzdwname.Split(',');
                                foreach (var item in arrStr)
                                {
                                    normallistApporxzzl.Add(Builders<Xzzls>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                                }
                                listAppxzzl.Add(Builders<Xzzls>.Filter.Or(normallistApporxzzl));
                            }
                            #endregion
                            var filterxzzl = Builders<Xzzls>.Filter.And(listAppxzzl);

                            var listxzzl = await _IXzzlsRepository.Get(filterxzzl);
                            #endregion
                            #region 下转住院
                            //排序字段
                            var sortxzzy = Builders<Xzzys>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listAppxzzy = new List<FilterDefinition<Xzzys>>();
                            listAppxzzy.Add(Builders<Xzzys>.Filter.Eq("isdelete", 0));
                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listAppxzzy.Add(Builders<Xzzys>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listAppxzzy.Add(Builders<Xzzys>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }
                            #region 转诊单位条件检索
                            var normallistApporxzzy = new List<FilterDefinition<Xzzys>>();
                            if (zzdwname != null)
                            {
                                string[] arrStr = zzdwname.Split(',');
                                foreach (var item in arrStr)
                                {
                                    normallistApporxzzy.Add(Builders<Xzzys>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                                }
                                listAppxzzy.Add(Builders<Xzzys>.Filter.Or(normallistApporxzzy));
                            }
                            #endregion
                            var filterxzzy = Builders<Xzzys>.Filter.And(listAppxzzy);

                            var listxzzy = await _IXzzysRepository.Get(filterxzzy);
                            #endregion
                            #region 急危重症抢救
                            //排序字段
                            var sortjwzqj = Builders<Wjzqjs>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listAppjwzqj = new List<FilterDefinition<Wjzqjs>>();
                            listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Eq("isdelete", 0));
                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }
                            #region 转诊单位条件检索
                            var normallistApporjwzzqj = new List<FilterDefinition<Wjzqjs>>();
                            if (zzdwname != null)
                            {
                                string[] arrStr = zzdwname.Split(',');
                                foreach (var item in arrStr)
                                {
                                    normallistApporjwzzqj.Add(Builders<Wjzqjs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                                }
                                listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Or(normallistApporjwzzqj));
                            }
                            #endregion
                            var filterjwzqj = Builders<Wjzqjs>.Filter.And(listAppjwzqj);

                            var listjwzqj = await _IWjzqjsRepository.Get(filterjwzqj);
                            #endregion
                            #region 标本收取
                            //排序字段
                            var sortbbsq = Builders<Bbsqs>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listAppbbsq = new List<FilterDefinition<Bbsqs>>();
                            listAppbbsq.Add(Builders<Bbsqs>.Filter.Eq("isdelete", 0));
                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listAppbbsq.Add(Builders<Bbsqs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listAppbbsq.Add(Builders<Bbsqs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }
                            #region 转诊单位条件检索
                            var normallistApporbbsq = new List<FilterDefinition<Bbsqs>>();
                            if (zzdwname != null)
                            {
                                string[] arrStr = zzdwname.Split(',');
                                foreach (var item in arrStr)
                                {
                                    normallistApporbbsq.Add(Builders<Bbsqs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                                }
                                listAppbbsq.Add(Builders<Bbsqs>.Filter.Or(normallistApporbbsq));
                            }
                            #endregion
                            var filterbbsq = Builders<Bbsqs>.Filter.And(listAppbbsq);

                            var listbbsq = await _IBbsqsRepository.Get(filterbbsq);
                            #endregion
                            yltwokermode.allcount = (list.Count() + listmz.Count() + listjc.Count() + listxzzl.Count() + listxzzy.Count() + listjwzqj.Count() + listbbsq.Count()).ToString();
                            yltwokerqstalelist.Add(yltwokermode);
                        }
                    }
                    #endregion
                }
                else
                {
                    if (stardates != null && endtimes != null)
                    {
                        DateTime dt1 = DateTime.Parse(stardates);
                        DateTime dt2 = DateTime.Parse(endtimes);

                        for (DateTime dt = dt1; dt <= dt2; dt = dt.AddDays(1))
                        {
                            Model.yltwokerqstale yltwokermode = new Model.yltwokerqstale();
                            yltwokermode.checkdate = dt.ToString("yyyy-MM-dd");
                            var stardate = dt.ToString("yyyy-MM-dd");
                            var endtime = dt.ToString("yyyy-MM-dd");
                            #region 上转住院
                            var sort = Builders<Szzys>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listApp = new List<FilterDefinition<Szzys>>();
                            listApp.Add(Builders<Szzys>.Filter.Eq("isdelete", 0));
                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }
                            var filter = Builders<Szzys>.Filter.And(listApp);
                            var list = await _ISzzysRepository.Get(filter);
                            #endregion
                            #region 上转门诊
                            //排序字段
                            var sortmz = Builders<Szmzs>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listAppmz = new List<FilterDefinition<Szmzs>>();
                            listAppmz.Add(Builders<Szmzs>.Filter.Eq("isdelete", 0));

                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }

                            var filtermz = Builders<Szmzs>.Filter.And(listAppmz);

                            var listmz = await _ISzmzsRepository.Get(filtermz);
                            #endregion
                            #region 上转检查
                            //排序字段
                            var sortjc = Builders<Szjcs>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listAppjc = new List<FilterDefinition<Szjcs>>();
                            listAppjc.Add(Builders<Szjcs>.Filter.Eq("isdelete", 0));
                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }

                            var filterjc = Builders<Szjcs>.Filter.And(listAppjc);

                            var listjc = await _ISzjcsRepository.Get(filterjc);

                            #endregion
                            #region 下转治疗
                            //排序字段
                            var sortxzzl = Builders<Xzzls>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listAppxzzl = new List<FilterDefinition<Xzzls>>();
                            listAppxzzl.Add(Builders<Xzzls>.Filter.Eq("isdelete", 0));
                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listAppxzzl.Add(Builders<Xzzls>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listAppxzzl.Add(Builders<Xzzls>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }
                            var filterxzzl = Builders<Xzzls>.Filter.And(listAppxzzl);

                            var listxzzl = await _IXzzlsRepository.Get(filterxzzl);
                            #endregion
                            #region 下转住院
                            //排序字段
                            var sortxzzy = Builders<Xzzys>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listAppxzzy = new List<FilterDefinition<Xzzys>>();
                            listAppxzzy.Add(Builders<Xzzys>.Filter.Eq("isdelete", 0));
                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listAppxzzy.Add(Builders<Xzzys>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listAppxzzy.Add(Builders<Xzzys>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }
                            var filterxzzy = Builders<Xzzys>.Filter.And(listAppxzzy);

                            var listxzzy = await _IXzzysRepository.Get(filterxzzy);
                            #endregion
                            #region 急危重症抢救
                            //排序字段
                            var sortjwzqj = Builders<Wjzqjs>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listAppjwzqj = new List<FilterDefinition<Wjzqjs>>();
                            listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Eq("isdelete", 0));
                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }
                            var filterjwzqj = Builders<Wjzqjs>.Filter.And(listAppjwzqj);

                            var listjwzqj = await _IWjzqjsRepository.Get(filterjwzqj);
                            #endregion
                            #region 标本收取
                            //排序字段
                            var sortbbsq = Builders<Bbsqs>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listAppbbsq = new List<FilterDefinition<Bbsqs>>();
                            listAppbbsq.Add(Builders<Bbsqs>.Filter.Eq("isdelete", 0));
                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listAppbbsq.Add(Builders<Bbsqs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listAppbbsq.Add(Builders<Bbsqs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }
                            var filterbbsq = Builders<Bbsqs>.Filter.And(listAppbbsq);

                            var listbbsq = await _IBbsqsRepository.Get(filterbbsq);
                            #endregion
                            yltwokermode.allcount = (list.Count() + listmz.Count() + listjc.Count() + listxzzl.Count() + listxzzy.Count() + listjwzqj.Count() + listbbsq.Count()).ToString();
                            yltwokerqstalelist.Add(yltwokermode);
                        }
                    }
                    #region 默认为本周
                    else
                    {

                        DateTime dt1 = Convert.ToDateTime(DateTime.Now.AddDays(1 - Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d"))).ToString("yyyy-MM-dd"));//本周周一
                        DateTime dt2 = Convert.ToDateTime(dt1.AddDays(6).ToString("yyyy-MM-dd"));//本周周日
                        for (DateTime dt = dt1; dt <= dt2; dt = dt.AddDays(1))
                        {
                            //dt为开始时间 dt2为结束时间
                            Model.yltwokerqstale yltwokermode = new Model.yltwokerqstale();
                            yltwokermode.checkdate = dt.ToString("yyyy-MM-dd");

                            var stardate = dt.ToString("yyyy-MM-dd");
                            var endtime = dt.ToString("yyyy-MM-dd ");
                            #region 上转住院
                            var sort = Builders<Szzys>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listApp = new List<FilterDefinition<Szzys>>();
                            listApp.Add(Builders<Szzys>.Filter.Eq("isdelete", 0));
                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }
                            var filter = Builders<Szzys>.Filter.And(listApp);
                            var list = await _ISzzysRepository.Get(filter);
                            #endregion
                            #region 上转门诊
                            //排序字段
                            var sortmz = Builders<Szmzs>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listAppmz = new List<FilterDefinition<Szmzs>>();
                            listAppmz.Add(Builders<Szmzs>.Filter.Eq("isdelete", 0));

                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }

                            var filtermz = Builders<Szmzs>.Filter.And(listAppmz);

                            var listmz = await _ISzmzsRepository.Get(filtermz);
                            #endregion
                            #region 上转检查
                            //排序字段
                            var sortjc = Builders<Szjcs>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listAppjc = new List<FilterDefinition<Szjcs>>();
                            listAppjc.Add(Builders<Szjcs>.Filter.Eq("isdelete", 0));
                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }

                            var filterjc = Builders<Szjcs>.Filter.And(listAppjc);

                            var listjc = await _ISzjcsRepository.Get(filterjc);

                            #endregion
                            #region 下转治疗
                            //排序字段
                            var sortxzzl = Builders<Xzzls>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listAppxzzl = new List<FilterDefinition<Xzzls>>();
                            listAppxzzl.Add(Builders<Xzzls>.Filter.Eq("isdelete", 0));
                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listAppxzzl.Add(Builders<Xzzls>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listAppxzzl.Add(Builders<Xzzls>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }
                            var filterxzzl = Builders<Xzzls>.Filter.And(listAppxzzl);

                            var listxzzl = await _IXzzlsRepository.Get(filterxzzl);
                            #endregion
                            #region 下转住院
                            //排序字段
                            var sortxzzy = Builders<Xzzys>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listAppxzzy = new List<FilterDefinition<Xzzys>>();
                            listAppxzzy.Add(Builders<Xzzys>.Filter.Eq("isdelete", 0));
                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listAppxzzy.Add(Builders<Xzzys>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listAppxzzy.Add(Builders<Xzzys>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }
                            var filterxzzy = Builders<Xzzys>.Filter.And(listAppxzzy);

                            var listxzzy = await _IXzzysRepository.Get(filterxzzy);
                            #endregion
                            #region 急危重症抢救
                            //排序字段
                            var sortjwzqj = Builders<Wjzqjs>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listAppjwzqj = new List<FilterDefinition<Wjzqjs>>();
                            listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Eq("isdelete", 0));
                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }
                            var filterjwzqj = Builders<Wjzqjs>.Filter.And(listAppjwzqj);

                            var listjwzqj = await _IWjzqjsRepository.Get(filterjwzqj);
                            #endregion
                            #region 标本收取
                            //排序字段
                            var sortbbsq = Builders<Bbsqs>.Sort.Descending("Szzydjsj");
                            //根据条件查询集合
                            var listAppbbsq = new List<FilterDefinition<Bbsqs>>();
                            listAppbbsq.Add(Builders<Bbsqs>.Filter.Eq("isdelete", 0));
                            if (!string.IsNullOrWhiteSpace(stardate))
                            {
                                listAppbbsq.Add(Builders<Bbsqs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                            }
                            if (!string.IsNullOrWhiteSpace(endtime))
                            {
                                listAppbbsq.Add(Builders<Bbsqs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                            }
                            var filterbbsq = Builders<Bbsqs>.Filter.And(listAppbbsq);

                            var listbbsq = await _IBbsqsRepository.Get(filterbbsq);
                            #endregion
                            yltwokermode.allcount = (list.Count() + listmz.Count() + listjc.Count() + listxzzl.Count() + listxzzy.Count() + listjwzqj.Count() + listbbsq.Count()).ToString();
                            yltwokerqstalelist.Add(yltwokermode);
                        }
                    }
                    #endregion
                }
                NPOIHelper npoi = new NPOIHelper();
                var tb = ModelConvertHelper<Model.yltwokerqstale>.ListToTable(yltwokerqstalelist, null);
                if (tb != null)
                {
                    //导出dtnew 
                    string[] cols = { "日期", "数量"};

                    byte[] sm = npoi.ExportToExcel1(tb, cols);
                    if (sm != null)
                    {
                        return File(sm, "application/vnd.ms-excel", "医联体工作量趋势图表.xlsx");
                    }
                    else
                    {
                        return Error("导出失败");
                    }
                }
                return Error("没有数据");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "导出异常");
                return Error("导出失败");
            }

        }

        #endregion
        #region 就诊科室统计表
        [HttpGet("getsxzzallkeinfo")]
        public async Task<IActionResult> Getsxzzallksinfo(string stardate, string endtime)
        {
            #region 转诊单位条件检索
            List<Model.excelsxzzallinfo> excelsxzzallinfolist = new List<Model.excelsxzzallinfo>();

            #region 定义合计字段
            int sszyinfo = 0;
            int ssmzinfo = 0;
            int szjcinfo = 0;
            int xzzlinfo = 0;
            int xzzyinfo = 0;
            int jwzzqjinfo = 0;
            int bbsqinfo = 0;
            int zjzsinfo = 0;
            #endregion
          
            #region 转诊单位为空
           //先获取所有转诊单位
           List<Model.Allzzdwinfo> allzzdwinfo = getAddzzdinfokslist(stardate, endtime, null);
           #region 循环遍历所有转诊单位
           foreach (var item1 in allzzdwinfo)
           {
               Model.excelsxzzallinfo excelsxzzallmodel = new Model.excelsxzzallinfo();
               excelsxzzallmodel.deptzzdw = item1.name;
               #region 上转住院
                    var sort = Builders<Szzys>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listApp = new List<FilterDefinition<Szzys>>();
                    listApp.Add(Builders<Szzys>.Filter.Eq("isdelete", 0));
                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listApp.Add(Builders<Szzys>.Filter.Where(p => p.Hzzyks == item1.name));

                    var filter = Builders<Szzys>.Filter.And(listApp);

                    var list = await _ISzzysRepository.Get(filter);
                    excelsxzzallmodel.sszyinfo = list.Count().ToString();
                    sszyinfo += list.Count();
                    #endregion
               #region 上转门诊
                    //排序字段
                    var sortmz = Builders<Szmzs>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listAppmz = new List<FilterDefinition<Szmzs>>();
                    listAppmz.Add(Builders<Szmzs>.Filter.Eq("isdelete", 0));

                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Hzjcxm == item1.name));
                    var filtermz = Builders<Szmzs>.Filter.And(listAppmz);

                    var listmz = await _ISzmzsRepository.Get(filtermz);
                    excelsxzzallmodel.ssmzinfo = listmz.Count().ToString();
                    ssmzinfo += listmz.Count();
                    #endregion
               #region 上转检查
                    //排序字段
                    var sortjc = Builders<Szjcs>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listAppjc = new List<FilterDefinition<Szjcs>>();
                    listAppjc.Add(Builders<Szjcs>.Filter.Eq("isdelete", 0));
                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Hzjcxm == item1.name));
                    var filterjc = Builders<Szjcs>.Filter.And(listAppjc);

                    var listjc = await _ISzjcsRepository.Get(filterjc);
                    excelsxzzallmodel.szjcinfo = listjc.Count().ToString();
                    szjcinfo += listjc.Count();
                    #endregion
               #region 下转治疗
                    //排序字段
                    var sortxzzl = Builders<Xzzls>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listAppxzzl = new List<FilterDefinition<Xzzls>>();
                    listAppxzzl.Add(Builders<Xzzls>.Filter.Eq("isdelete", 0));
                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listAppxzzl.Add(Builders<Xzzls>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listAppxzzl.Add(Builders<Xzzls>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listAppxzzl.Add(Builders<Xzzls>.Filter.Where(p => p.Hzjcxm == item1.name));
                    var filterxzzl = Builders<Xzzls>.Filter.And(listAppxzzl);

                    var listxzzl = await _IXzzlsRepository.Get(filterxzzl);
                    excelsxzzallmodel.xzzlinfo = listxzzl.Count().ToString();
                    xzzlinfo += listxzzl.Count();
                    #endregion
               #region 下转住院
                    //排序字段
                    var sortxzzy = Builders<Xzzys>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listAppxzzy = new List<FilterDefinition<Xzzys>>();
                    listAppxzzy.Add(Builders<Xzzys>.Filter.Eq("isdelete", 0));
                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listAppxzzy.Add(Builders<Xzzys>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listAppxzzy.Add(Builders<Xzzys>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listAppxzzy.Add(Builders<Xzzys>.Filter.Where(p => p.Hzjcxm == item1.name));
                    var filterxzzy = Builders<Xzzys>.Filter.And(listAppxzzy);

                    var listxzzy = await _IXzzysRepository.Get(filterxzzy);
                    excelsxzzallmodel.xzzyinfo = listxzzy.Count().ToString();
                    xzzyinfo += listxzzy.Count();
                    #endregion
               #region 急危重症抢救
                    //排序字段
                    var sortjwzqj = Builders<Wjzqjs>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listAppjwzqj = new List<FilterDefinition<Wjzqjs>>();
                    listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Eq("isdelete", 0));
                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Where(p => p.Hzjzks == item1.name));
                    var filterjwzqj = Builders<Wjzqjs>.Filter.And(listAppjwzqj);

                    var listjwzqj = await _IWjzqjsRepository.Get(filterjwzqj);
                    excelsxzzallmodel.jwzzqjinfo = listjwzqj.Count().ToString();
                    jwzzqjinfo += listjwzqj.Count();
                    #endregion
               #region 标本收取
                    //排序字段
                    var sortbbsq = Builders<Bbsqs>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listAppbbsq = new List<FilterDefinition<Bbsqs>>();
                    listAppbbsq.Add(Builders<Bbsqs>.Filter.Eq("isdelete", 0));
                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listAppbbsq.Add(Builders<Bbsqs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listAppbbsq.Add(Builders<Bbsqs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listAppbbsq.Add(Builders<Bbsqs>.Filter.Where(p => p.Hzjcxm == item1.name));
                    var filterbbsq = Builders<Bbsqs>.Filter.And(listAppbbsq);

                    var listbbsq = await _IBbsqsRepository.Get(filterbbsq);
                    excelsxzzallmodel.bbsqinfo = listbbsq.Count().ToString();
                    bbsqinfo += listbbsq.Count();
                    #endregion
               excelsxzzallmodel.zjzsinfo = (list.Count() + listmz.Count() + listjc.Count() + listxzzl.Count() + listxzzy.Count() + listjwzqj.Count() + listbbsq.Count()).ToString();
               excelsxzzallinfolist.Add(excelsxzzallmodel);
               zjzsinfo += list.Count() + listmz.Count() + listjc.Count() + listxzzl.Count() + listxzzy.Count() + listjwzqj.Count() + listbbsq.Count();
            }
            #endregion
            #region 合计
            Model.excelsxzzallinfo excelsxzzallmodels = new Model.excelsxzzallinfo();
            excelsxzzallmodels.deptzzdw = "合计";
            excelsxzzallmodels.sszyinfo = sszyinfo.ToString();
            excelsxzzallmodels.ssmzinfo = ssmzinfo.ToString();
            excelsxzzallmodels.szjcinfo = szjcinfo.ToString();
            excelsxzzallmodels.xzzlinfo = xzzlinfo.ToString();
            excelsxzzallmodels.xzzyinfo = xzzyinfo.ToString();
            excelsxzzallmodels.jwzzqjinfo = jwzzqjinfo.ToString();
            excelsxzzallmodels.bbsqinfo = bbsqinfo.ToString();
            excelsxzzallmodels.zjzsinfo = zjzsinfo.ToString();
            excelsxzzallinfolist.Add(excelsxzzallmodels);
            #endregion
            #endregion
            #endregion
            return Success("成功", excelsxzzallinfolist);
           
        }
        /// <summary>
        /// 导出就诊科室统计表数据
        /// </summary>
        /// <param name="stardate"></param>
        /// <param name="endtime"></param>
        /// <returns></returns>
        [HttpGet("excelsxzzallksinfo")]
        [AllowAnonymous]
        public async Task<IActionResult> excelsxzzallksinfo(string stardate, string endtime)
        {
            try
            {
                #region 转诊单位条件检索
                List<Model.excelsxzzallinfo> excelsxzzallinfolist = new List<Model.excelsxzzallinfo>();

                #region 定义合计字段
                int sszyinfo = 0;
                int ssmzinfo = 0;
                int szjcinfo = 0;
                int xzzlinfo = 0;
                int xzzyinfo = 0;
                int jwzzqjinfo = 0;
                int bbsqinfo = 0;
                int zjzsinfo = 0;
                #endregion

                #region 转诊单位为空
                //先获取所有转诊单位
                List<Model.Allzzdwinfo> allzzdwinfo = getAddzzdinfolist(stardate, endtime, null);
                #region 循环遍历所有转诊单位
                foreach (var item1 in allzzdwinfo)
                {
                    Model.excelsxzzallinfo excelsxzzallmodel = new Model.excelsxzzallinfo();
                    excelsxzzallmodel.deptzzdw = item1.name;
                    #region 上转住院
                    var sort = Builders<Szzys>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listApp = new List<FilterDefinition<Szzys>>();
                    listApp.Add(Builders<Szzys>.Filter.Eq("isdelete", 0));
                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listApp.Add(Builders<Szzys>.Filter.Where(p => p.Hzzyks == item1.name));

                    var filter = Builders<Szzys>.Filter.And(listApp);

                    var list = await _ISzzysRepository.Get(filter);
                    excelsxzzallmodel.sszyinfo = list.Count().ToString();
                    sszyinfo += list.Count();
                    #endregion
                    #region 上转门诊
                    //排序字段
                    var sortmz = Builders<Szmzs>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listAppmz = new List<FilterDefinition<Szmzs>>();
                    listAppmz.Add(Builders<Szmzs>.Filter.Eq("isdelete", 0));

                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Hzjcxm == item1.name));
                    var filtermz = Builders<Szmzs>.Filter.And(listAppmz);

                    var listmz = await _ISzmzsRepository.Get(filtermz);
                    excelsxzzallmodel.ssmzinfo = listmz.Count().ToString();
                    ssmzinfo += listmz.Count();
                    #endregion
                    #region 上转检查
                    //排序字段
                    var sortjc = Builders<Szjcs>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listAppjc = new List<FilterDefinition<Szjcs>>();
                    listAppjc.Add(Builders<Szjcs>.Filter.Eq("isdelete", 0));
                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listAppjc.Add(Builders<Szjcs>.Filter.Where(p => p.Hzjcxm == item1.name));
                    var filterjc = Builders<Szjcs>.Filter.And(listAppjc);

                    var listjc = await _ISzjcsRepository.Get(filterjc);
                    excelsxzzallmodel.szjcinfo = listjc.Count().ToString();
                    szjcinfo += listjc.Count();
                    #endregion
                    #region 下转治疗
                    //排序字段
                    var sortxzzl = Builders<Xzzls>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listAppxzzl = new List<FilterDefinition<Xzzls>>();
                    listAppxzzl.Add(Builders<Xzzls>.Filter.Eq("isdelete", 0));
                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listAppxzzl.Add(Builders<Xzzls>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listAppxzzl.Add(Builders<Xzzls>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listAppxzzl.Add(Builders<Xzzls>.Filter.Where(p => p.Hzjcxm == item1.name));
                    var filterxzzl = Builders<Xzzls>.Filter.And(listAppxzzl);

                    var listxzzl = await _IXzzlsRepository.Get(filterxzzl);
                    excelsxzzallmodel.xzzlinfo = listxzzl.Count().ToString();
                    xzzlinfo += listxzzl.Count();
                    #endregion
                    #region 下转住院
                    //排序字段
                    var sortxzzy = Builders<Xzzys>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listAppxzzy = new List<FilterDefinition<Xzzys>>();
                    listAppxzzy.Add(Builders<Xzzys>.Filter.Eq("isdelete", 0));
                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listAppxzzy.Add(Builders<Xzzys>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listAppxzzy.Add(Builders<Xzzys>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listAppxzzy.Add(Builders<Xzzys>.Filter.Where(p => p.Hzjcxm == item1.name));
                    var filterxzzy = Builders<Xzzys>.Filter.And(listAppxzzy);

                    var listxzzy = await _IXzzysRepository.Get(filterxzzy);
                    excelsxzzallmodel.xzzyinfo = listxzzy.Count().ToString();
                    xzzyinfo += listxzzy.Count();
                    #endregion
                    #region 急危重症抢救
                    //排序字段
                    var sortjwzqj = Builders<Wjzqjs>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listAppjwzqj = new List<FilterDefinition<Wjzqjs>>();
                    listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Eq("isdelete", 0));
                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Where(p => p.Hzjzks == item1.name));
                    var filterjwzqj = Builders<Wjzqjs>.Filter.And(listAppjwzqj);

                    var listjwzqj = await _IWjzqjsRepository.Get(filterjwzqj);
                    excelsxzzallmodel.jwzzqjinfo = listjwzqj.Count().ToString();
                    jwzzqjinfo += listjwzqj.Count();
                    #endregion
                    #region 标本收取
                    //排序字段
                    var sortbbsq = Builders<Bbsqs>.Sort.Descending("Szzydjsj");
                    //根据条件查询集合
                    var listAppbbsq = new List<FilterDefinition<Bbsqs>>();
                    listAppbbsq.Add(Builders<Bbsqs>.Filter.Eq("isdelete", 0));
                    if (!string.IsNullOrWhiteSpace(stardate))
                    {
                        listAppbbsq.Add(Builders<Bbsqs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
                    }
                    if (!string.IsNullOrWhiteSpace(endtime))
                    {
                        listAppbbsq.Add(Builders<Bbsqs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
                    }
                    listAppbbsq.Add(Builders<Bbsqs>.Filter.Where(p => p.Hzjcxm == item1.name));
                    var filterbbsq = Builders<Bbsqs>.Filter.And(listAppbbsq);

                    var listbbsq = await _IBbsqsRepository.Get(filterbbsq);
                    excelsxzzallmodel.bbsqinfo = listbbsq.Count().ToString();
                    bbsqinfo += listbbsq.Count();
                    #endregion
                    excelsxzzallmodel.zjzsinfo = (list.Count() + listmz.Count() + listjc.Count() + listxzzl.Count() + listxzzy.Count() + listjwzqj.Count() + listbbsq.Count()).ToString();
                    excelsxzzallinfolist.Add(excelsxzzallmodel);
                    zjzsinfo += list.Count() + listmz.Count() + listjc.Count() + listxzzl.Count() + listxzzy.Count() + listjwzqj.Count() + listbbsq.Count();
                }
                #endregion
                #region 合计
                Model.excelsxzzallinfo excelsxzzallmodels = new Model.excelsxzzallinfo();
                excelsxzzallmodels.deptzzdw = "合计";
                excelsxzzallmodels.sszyinfo = sszyinfo.ToString();
                excelsxzzallmodels.ssmzinfo = ssmzinfo.ToString();
                excelsxzzallmodels.szjcinfo = szjcinfo.ToString();
                excelsxzzallmodels.xzzlinfo = xzzlinfo.ToString();
                excelsxzzallmodels.xzzyinfo = xzzyinfo.ToString();
                excelsxzzallmodels.jwzzqjinfo = jwzzqjinfo.ToString();
                excelsxzzallmodels.bbsqinfo = bbsqinfo.ToString();
                excelsxzzallmodels.zjzsinfo = zjzsinfo.ToString();
                excelsxzzallinfolist.Add(excelsxzzallmodels);
                #endregion
                #endregion

                #endregion
                NPOIHelper npoi = new NPOIHelper();
                var tb = ModelConvertHelper<Model.excelsxzzallinfo>.ListToTable(excelsxzzallinfolist, null);
                if (tb != null)
                {
                    //导出dtnew 
                    string[] cols = { "转诊单位", "上转住院", "上转门诊", "上转检查", "下转治疗", "下转住院", "急危重症抢救", "标本收取", "总结单数" };

                    byte[] sm = npoi.ExportToExcel1(tb, cols);
                    if (sm != null)
                    {
                        return File(sm, "application/vnd.ms-excel", "医联体工作量统计表.xlsx");
                    }
                    else
                    {
                        return Error("导出失败");
                    }
                }
                return Error("没有数据");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "导出异常");
                return Error("导出失败");
            }
        }
        /// <summary>
        /// 获取所有科室信息
        /// </summary>
        /// <param name="stardate"></param>
        /// <param name="endtime"></param>
        /// <param name="zzdwname"></param>
        /// <returns></returns>
        public List<Model.Allzzdwinfo> getAddzzdinfokslist(string stardate, string endtime, string zzdwname)
        {
            //先获取所有转诊单位
            List<Model.Allzzdwinfo> allzzdwinfo = new List<Model.Allzzdwinfo>();
            #region 上转住院
            var sort = Builders<Szzys>.Sort.Descending("Szzydjsj");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<Szzys>>();
            listApp.Add(Builders<Szzys>.Filter.Eq("isdelete", 0));
            if (!string.IsNullOrWhiteSpace(stardate))
            {
                listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
            }
            if (!string.IsNullOrWhiteSpace(endtime))
            {
                listApp.Add(Builders<Szzys>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
            }
            #region 转诊单位条件检索
            var normallistAppor = new List<FilterDefinition<Szzys>>();
            if (zzdwname != null)
            {
                string[] arrStr = zzdwname.Split(',');
                foreach (var item in arrStr)
                {
                    normallistAppor.Add(Builders<Szzys>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                }
                listApp.Add(Builders<Szzys>.Filter.Or(normallistAppor));
            }
            #endregion
            var filter = Builders<Szzys>.Filter.And(listApp);

            var list = _ISzzysRepository.Get(filter);
            #region 追加转诊科室
            foreach (var item in list.Result)
            {
                //判断
                var zzdwlis = allzzdwinfo.Where(p => p.name == item.Hzzyks).ToList();
                if (zzdwlis.Count > 0)
                {
                    continue;
                }
                Model.Allzzdwinfo allzzdwmodel = new Model.Allzzdwinfo();
                allzzdwmodel.name = item.Hzzyks;
                allzzdwinfo.Add(allzzdwmodel);
            }
            #endregion
            #endregion
            #region 上转门诊
            //排序字段
            var sortmz = Builders<Szmzs>.Sort.Descending("Szzydjsj");
            //根据条件查询集合
            var listAppmz = new List<FilterDefinition<Szmzs>>();
            listAppmz.Add(Builders<Szmzs>.Filter.Eq("isdelete", 0));

            if (!string.IsNullOrWhiteSpace(stardate))
            {
                listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj >= DateTime.Parse(stardate.Trim() + " 00:00:00")));
            }
            if (!string.IsNullOrWhiteSpace(endtime))
            {
                listAppmz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzydjsj <= DateTime.Parse(endtime.Trim() + " 23:59:59")));
            }
            #region 转诊单位条件检索
            var normallistAppormz = new List<FilterDefinition<Szmzs>>();
            if (zzdwname != null)
            {
                string[] arrStr = zzdwname.Split(',');
                foreach (var item in arrStr)
                {
                    normallistAppormz.Add(Builders<Szmzs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                }
                listAppmz.Add(Builders<Szmzs>.Filter.Or(normallistAppormz));
            }
            #endregion
            var filtermz = Builders<Szmzs>.Filter.And(listAppmz);

            var listmz = _ISzmzsRepository.Get(filtermz);
            #region 追加转诊单位
            foreach (var item in listmz.Result)
            {
                //判断
                var zzdwlis = allzzdwinfo.Where(p => p.name == item.Hzjcxm).ToList();
                if (zzdwlis.Count > 0)
                {
                    continue;
                }
                Model.Allzzdwinfo allzzdwmodel = new Model.Allzzdwinfo();
                allzzdwmodel.name = item.Hzjcxm;
                allzzdwinfo.Add(allzzdwmodel);
            }
            #endregion
            #endregion
            #region 下转治疗
            //排序字段
            var sortxzzl = Builders<Xzzls>.Sort.Descending("Szzydjsj");
            //根据条件查询集合
            var listAppxzzl = new List<FilterDefinition<Xzzls>>();
            listAppxzzl.Add(Builders<Xzzls>.Filter.Eq("isdelete", 0));
            if (!string.IsNullOrWhiteSpace(stardate))
            {
                listAppxzzl.Add(Builders<Xzzls>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
            }
            if (!string.IsNullOrWhiteSpace(endtime))
            {
                listAppxzzl.Add(Builders<Xzzls>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
            }
            #region 转诊单位条件检索
            var normallistApporxzzl = new List<FilterDefinition<Xzzls>>();
            if (zzdwname != null)
            {
                string[] arrStr = zzdwname.Split(',');
                foreach (var item in arrStr)
                {
                    normallistApporxzzl.Add(Builders<Xzzls>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                }
                listAppxzzl.Add(Builders<Xzzls>.Filter.Or(normallistApporxzzl));
            }
            #endregion
            var filterxzzl = Builders<Xzzls>.Filter.And(listAppxzzl);

            var listxzzl = _IXzzlsRepository.Get(filterxzzl);
            #region 追加转诊单位
            foreach (var item in listmz.Result)
            {
                //判断
                var zzdwlis = allzzdwinfo.Where(p => p.name == item.Hzjcxm).ToList();
                if (zzdwlis.Count > 0)
                {
                    continue;
                }
                Model.Allzzdwinfo allzzdwmodel = new Model.Allzzdwinfo();
                allzzdwmodel.name = item.Hzjcxm;
                allzzdwinfo.Add(allzzdwmodel);
            }
            #endregion
            #endregion
            #region 下转住院
            //排序字段
            var sortxzzy = Builders<Xzzys>.Sort.Descending("Szzydjsj");
            //根据条件查询集合
            var listAppxzzy = new List<FilterDefinition<Xzzys>>();
            listAppxzzy.Add(Builders<Xzzys>.Filter.Eq("isdelete", 0));
            if (!string.IsNullOrWhiteSpace(stardate))
            {
                listAppxzzy.Add(Builders<Xzzys>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
            }
            if (!string.IsNullOrWhiteSpace(endtime))
            {
                listAppxzzy.Add(Builders<Xzzys>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
            }
            #region 转诊单位条件检索
            var normallistApporxzzy = new List<FilterDefinition<Xzzys>>();
            if (zzdwname != null)
            {
                string[] arrStr = zzdwname.Split(',');
                foreach (var item in arrStr)
                {
                    normallistApporxzzy.Add(Builders<Xzzys>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                }
                listAppxzzy.Add(Builders<Xzzys>.Filter.Or(normallistApporxzzy));
            }
            #endregion
            var filterxzzy = Builders<Xzzys>.Filter.And(listAppxzzy);

            var listxzzy = _IXzzysRepository.Get(filterxzzy);
            #region 追加转诊单位
            foreach (var item in listxzzy.Result)
            {
                //判断
                var zzdwlis = allzzdwinfo.Where(p => p.name == item.Hzjcxm).ToList();
                if (zzdwlis.Count > 0)
                {
                    continue;
                }
                Model.Allzzdwinfo allzzdwmodel = new Model.Allzzdwinfo();
                allzzdwmodel.name = item.Hzjcxm;
                allzzdwinfo.Add(allzzdwmodel);
            }
            #endregion
            #endregion
            #region 急危重症抢救
            //排序字段
            var sortjwzqj = Builders<Wjzqjs>.Sort.Descending("Szzydjsj");
            //根据条件查询集合
            var listAppjwzqj = new List<FilterDefinition<Wjzqjs>>();
            listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Eq("isdelete", 0));
            if (!string.IsNullOrWhiteSpace(stardate))
            {
                listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Gt("Szzydjsj", DateTime.Parse(stardate.Trim() + " 00:00:00")));
            }
            if (!string.IsNullOrWhiteSpace(endtime))
            {
                listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Lt("Szzydjsj", DateTime.Parse(endtime.Trim() + " 23:59:59")));
            }
            #region 转诊单位条件检索
            var normallistApporjwzzqj = new List<FilterDefinition<Wjzqjs>>();
            if (zzdwname != null)
            {
                string[] arrStr = zzdwname.Split(',');
                foreach (var item in arrStr)
                {
                    normallistApporjwzzqj.Add(Builders<Wjzqjs>.Filter.Where(p => p.Szzyzzdw.Contains(zzdwname)));
                }
                listAppjwzqj.Add(Builders<Wjzqjs>.Filter.Or(normallistApporjwzzqj));
            }
            #endregion
            var filterjwzqj = Builders<Wjzqjs>.Filter.And(listAppjwzqj);

            var listjwzqj = _IWjzqjsRepository.Get(filterjwzqj);
            #region 追加转诊单位
            foreach (var item in listjwzqj.Result)
            {
                //判断
                var zzdwlis = allzzdwinfo.Where(p => p.name == item.Hzjzks).ToList();
                if (zzdwlis.Count > 0)
                {
                    continue;
                }
                Model.Allzzdwinfo allzzdwmodel = new Model.Allzzdwinfo();
                allzzdwmodel.name = item.Hzjzks;
                allzzdwinfo.Add(allzzdwmodel);
            }
            #endregion
            #endregion
            return allzzdwinfo;
        }

        #endregion
        #region 工作量统计报表
        //[HttpGet("getgzltjlists")]
        //public async Task<IActionResult> Getgzltjlists(string id, string name)
        //{
            
        //}
        #endregion
        #endregion
        #region 双重预防 
        #region 风险等级
        /// <summary>
        /// 添加风险等级
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("addfxdjtabs")]
        public async Task<IActionResult> AddFxdjtabs(Model.Dto.Fxdjtabs tps)
        {
            Fxdjtabs tp = new Fxdjtabs();
            tp.Fxdjname = tps.Fxdjname;//风险等级名称
            tp.isdelete = 0;// 是否删除
            var a = _IFxdjtabsRepository.AddRetID(tp);
            if (a.Result != null || a.Result != "")
            {
                return Success("成功", a.Result);
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 修改风险等级
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("upfxdjtabs")]
        public async Task<IActionResult> upFxdjtabs(Model.Dto.Fxdjtabs tps)
        {
            Fxdjtabs tp = new Fxdjtabs();
            tp.id = tps.id;
            tp.Fxdjname = tps.Fxdjname;
            tp.isdelete = 0;
            var resua = _IFxdjtabsRepository.Update(tp);
            if (resua.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 根据编号获取风险等级信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("getfxdjtabsbyid")]
        public async Task<IActionResult> GetFxdjtabsbyid(string id)
        {
            var model = await _IFxdjtabsRepository.Get(p => p.id == id);
            return Success("成功", model);
        }
        /// <summary>
        /// 删除风险等级信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("delefxdjtabs")]
        public async Task<IActionResult> deleFxdjtabs(string id)
        {
            var result = _IFxdjtabsRepository.Remove(id);
            if (result.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 获取风险等级信息
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        [HttpGet("getfxdjtabs")]
        public async Task<IActionResult> GetFxdjtabs(int PageIndex, int PageSize)
        {
            //排序字段
            //var sort = Builders<Fxdjtabs>.Sort.Ascending("Szzydjsj");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<Fxdjtabs>>();
            listApp.Add(Builders<Fxdjtabs>.Filter.Eq("isdelete", 0));

            var filter = Builders<Fxdjtabs>.Filter.And(listApp);

            var list = await _IFxdjtabsRepository.GetByPage(filter, PageIndex, PageSize);
            var redCount = await _IFxdjtabsRepository.CountAsync(filter);   //获取总数
            var listmodel = new List<Fxdjtabs>();
            foreach (var item in list)
            {
                ///定义选项
                var model = new Fxdjtabs();
                model.id = item.id;
                model.Fxdjname = item.Fxdjname;//风险等级名称
                model.isdelete = item.isdelete;//是否启用
                listmodel.Add(model);
            }
            var obj = new
            {
                rows = listmodel,
                total = redCount
            };
            return Success("成功", obj);
        }
        #endregion
        #region 风险区域 
        /// <summary>
        /// 添加风险区域
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("addfxqytabs")]
        public async Task<IActionResult> AddFxqytabs(Model.Dto.Fxqytabs tps)
        {
            Fxqytabs tp = new Fxqytabs();
            tp.Fxqyname = tps.Fxqyname;//风险区域名称
            tp.Fxqyyq = tps.Fxqyyq;//风险院区
            tp.Fxqylh = tps.Fxqylh;//风险楼号
            tp.Fxqylc = tps.Fxqylc;//风险楼层
            tp.isdelete = 0;// 是否删除
            var a = _IFxqytabsRepository.AddRetID(tp);
            if (a.Result != null || a.Result != "")
            {
                return Success("成功", a.Result);
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 修改风险区域
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("upfxqytabs")]
        public async Task<IActionResult> upFxqytabs(Model.Dto.Fxqytabs tps)
        {
            Fxqytabs tp = new Fxqytabs();
            tp.id = tps.id;
            tp.Fxqyname = tps.Fxqyname;//风险区域名称
            tp.Fxqyyq = tps.Fxqyyq;//风险院区
            tp.Fxqylh = tps.Fxqylh;//风险楼号
            tp.Fxqylc = tps.Fxqylc;//风险楼层
            tp.isdelete = 0;
            var resua = _IFxqytabsRepository.Update(tp);
            if (resua.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 根据编号获取风险区域信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("getfxqytabsbyid")]
        public async Task<IActionResult> GetFxqytabsbyid(string id)
        {
            var model = await _IFxqytabsRepository.Get(p => p.id == id);
            return Success("成功", model);
        }
        /// <summary>
        /// 删除风险区域信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("delefxqytabs")]
        public async Task<IActionResult> deleFxqytabs(string id)
        {
            var result = _IFxqytabsRepository.Remove(id);
            if (result.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 获取风险区域信息
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        [HttpGet("getfxqytabs")]
        public async Task<IActionResult> GetFxqytabs(int PageIndex, int PageSize)
        {
            //排序字段
            //var sort = Builders<Fxdjtabs>.Sort.Ascending("Szzydjsj");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<Fxqytabs>>();
            listApp.Add(Builders<Fxqytabs>.Filter.Eq("isdelete", 0));

            var filter = Builders<Fxqytabs>.Filter.And(listApp);

            var list = await _IFxqytabsRepository.GetByPage(filter, PageIndex, PageSize);
            var redCount = await _IFxqytabsRepository.CountAsync(filter);   //获取总数
            var listmodel = new List<Fxqytabs>();
            foreach (var item in list)
            {
                ///定义选项
                var model = new Fxqytabs();
                model.id = item.id;
                model.Fxqyname = item.Fxqyname;//风险区域名称
                model.Fxqyyq = item.Fxqyyq;//风险院区
                model.Fxqylh = item.Fxqylh;//风险楼号
                model.Fxqylc = item.Fxqylc;//风险楼层
                model.isdelete = item.isdelete;//是否启用
                listmodel.Add(model);
            }
            var obj = new
            {
                rows = listmodel,
                total = redCount
            };
            return Success("成功", obj);
        }

        #endregion
        #region 隐患排查表
        /// <summary>
        /// 添加隐患排查信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpPost("addyhpctabs")]
        public async Task<IActionResult> AddYhpctabs(Model.Dto.Yhpctabs tps)
        {
            Yhpctabs tp = new Yhpctabs();
            tp.Yhpctitle = tps.Yhpctitle;//标题
            tp.Fxdjid = tps.Fxdjid;//风险等级编号
            tp.Fxqyid = tps.Fxqyid;//风险区域编号
            tp.Yhpcincont = tps.Yhpcincont;//风险详情信息
            tp.Yhpcdate = DateTime.Now;//隐患排查录入时间
            tp.Pcbz = tps.Pcbz;//排查班组 有班组时让班组下的用户都可以进行操作 修改等级
            tp.Pcks = tps.Pcks;//排查科室 有科室时让科室下的用户和班组下的用户都可以进行操作
            tp.Pcbm = tps.Pcbm;//排查部门 同上以此类推
            tp.Pcyj = tps.Pcyj;//排查院级 同上以此类推
            tp.faultpic = tps.faultpic;
            tp.isdelete = 0;//是否启用
            var a = _IYhpctabsRepository.AddRetID(tp);
            if (a.Result != null || a.Result != "")
            {
                return Success("成功", a.Result);
            }
            else
            {
                return Error("失败");
            }
        }

        /// <summary>
        /// 修改隐患排查信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpPost("upyhpctabs")]
        public async Task<IActionResult> upYhpctabs(Model.Dto.Yhpctabs tps)
        {
            Yhpctabs tp = await _IYhpctabsRepository.GetSingle(p => p.id == tps.id);
            tp.id = tps.id;
            tp.Yhpctitle = tps.Yhpctitle;//标题
            tp.Fxdjid = tps.Fxdjid;//风险等级编号
            tp.Fxqyid = tps.Fxqyid;//风险区域编号
            tp.Yhpcincont = tps.Yhpcincont;//风险详情信息
            //tp.Yhpcdate = DateTime.Now ;//隐患排查录入时间
            tp.Pcbz = tps.Pcbz;//排查班组 有班组时让班组下的用户都可以进行操作 修改等级
            tp.Pcks = tps.Pcks;//排查科室 有科室时让科室下的用户和班组下的用户都可以进行操作
            tp.Pcbm = tps.Pcbm;//排查部门 同上以此类推
            tp.Pcyj = tps.Pcyj;//排查院级 同上以此类推
            tp.faultpic = tps.faultpic;
            tp.isdelete = 0;
            var resua = _IYhpctabsRepository.Update(tp);
            if (resua.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 根据编号获取隐患排查信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("getyhpctabsbyid")]
        public async Task<IActionResult> GetYhpctabsbyid(string id)
        {
            var model = await _IYhpctabsRepository.Get(p => p.id == id);
            Yhpctabs models = await _IYhpctabsRepository.GetSingle(p => p.id == id);
            if (models != null)
            {
                Fxdjtabs fxdjmodel = await _IFxdjtabsRepository.GetSingle(p => p.id == models.Fxdjid);
                if (fxdjmodel != null)
                {
                    models.Fxdjname = fxdjmodel.Fxdjname;
                }
                Fxqytabs fxqymode = await _IFxqytabsRepository.GetSingle(p => p.id == models.Fxqyid);
                {
                    models.Fxqyname = fxqymode.Fxqyname;
                    models.Fxqyyq = fxqymode.Fxqyyq;//风险区域院区
                    models.Fxqylh = fxqymode.Fxqylh;//风险区域楼号
                    models.Fxqylc = fxqymode.Fxqylc;//风险区域楼层
                }
            }

            var listmodel = new List<Yhpctabs>();
            listmodel.Add(models);

            return Success("成功", listmodel);
        }
        /// <summary>
        /// 删除隐患排查
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("deleyhpctabs")]
        public async Task<IActionResult> deleYhpctabs(string id)
        {
            var result = _IYhpctabsRepository.Remove(id);
            if (result.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 获取隐患排查信息
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        [HttpGet("getyhpctabs")]
        public async Task<IActionResult> GetYhpctabs(int PageIndex, int PageSize, string rolename, string fxdjids, string fxquids)
        {
            //排序字段
            var sort = Builders<Yhpctabs>.Sort.Descending("Yhpcdate");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<Yhpctabs>>();
            listApp.Add(Builders<Yhpctabs>.Filter.Eq("isdelete", 0));
            if (fxdjids != null)
            {
                listApp.Add(Builders<Yhpctabs>.Filter.Eq("Fxdjid", fxdjids));
            }
            if (fxquids != null)
            {
                listApp.Add(Builders<Yhpctabs>.Filter.Eq("Fxqyid", fxquids));
            }
            if (rolename != null)
            {
                if (rolename == "院级")
                {
                    var normallistAppor = new List<FilterDefinition<Yhpctabs>>();
                    normallistAppor.Add(Builders<Yhpctabs>.Filter.Eq("Pcbz", "班组"));
                    normallistAppor.Add(Builders<Yhpctabs>.Filter.Eq("Pcks", "科室"));
                    normallistAppor.Add(Builders<Yhpctabs>.Filter.Eq("Pcbm", "部门"));
                    normallistAppor.Add(Builders<Yhpctabs>.Filter.Eq("Pcyj", rolename));
                    listApp.Add(Builders<Yhpctabs>.Filter.Or(normallistAppor));
                }
                else if (rolename == "部门")
                {
                    var normallistAppor = new List<FilterDefinition<Yhpctabs>>();
                    normallistAppor.Add(Builders<Yhpctabs>.Filter.Eq("Pcbz", "班组"));
                    normallistAppor.Add(Builders<Yhpctabs>.Filter.Eq("Pcks", "科室"));
                    normallistAppor.Add(Builders<Yhpctabs>.Filter.Eq("Pcbm", rolename));
                    listApp.Add(Builders<Yhpctabs>.Filter.Or(normallistAppor));
                }
                else if (rolename == "科室")
                {
                    var normallistAppor = new List<FilterDefinition<Yhpctabs>>();
                    normallistAppor.Add(Builders<Yhpctabs>.Filter.Eq("Pcbz", "班组"));
                    normallistAppor.Add(Builders<Yhpctabs>.Filter.Eq("Pcks", rolename));
                    listApp.Add(Builders<Yhpctabs>.Filter.Or(normallistAppor));
                }
                else
                {
                    var normallistAppor = new List<FilterDefinition<Yhpctabs>>();
                    normallistAppor.Add(Builders<Yhpctabs>.Filter.Eq("Pcbz", "班组"));
                    listApp.Add(Builders<Yhpctabs>.Filter.Or(normallistAppor));
                }

            }
            var filter = Builders<Yhpctabs>.Filter.And(listApp);

            var list = await _IYhpctabsRepository.GetByPage(filter, PageIndex, PageSize, sort);
            var redCount = await _IYhpctabsRepository.CountAsync(filter);   //获取总数
            var listmodel = new List<Yhpctabs>();
            foreach (var item in list)
            {
                Fxdjtabs fxdjmodel = await _IFxdjtabsRepository.GetSingle(p => p.id == item.Fxdjid);
                Fxqytabs fxqymode = await _IFxqytabsRepository.GetSingle(p => p.id == item.Fxqyid);
                ///定义选项
                var model = new Yhpctabs();
                model.id = item.id;
                model.Yhpctitle = item.Yhpctitle;//标题
                model.Fxdjid = item.Fxdjid;//风险等级编号
                if (fxdjmodel != null)
                {
                    model.Fxdjname = fxdjmodel.Fxdjname;
                }
                if (fxqymode != null)
                {
                    model.Fxqyname = fxqymode.Fxqyname;
                    model.Fxqyyq = fxqymode.Fxqyyq;//风险区域院区
                    model.Fxqylh = fxqymode.Fxqylh;//风险区域楼号
                    model.Fxqylc = fxqymode.Fxqylc;//风险区域楼层
                }
                model.Fxqyid = item.Fxqyid;//风险区域编号
                model.Yhpcincont = item.Yhpcincont;//风险详情信息
                model.Yhpcdate = item.Yhpcdate;//隐患排查录入时间
                model.Pcbz = item.Pcbz;//排查班组 有班组时让班组下的用户都可以进行操作 修改等级
                model.Pcks = item.Pcks;//排查科室 有科室时让科室下的用户和班组下的用户都可以进行操作
                model.Pcbm = item.Pcbm;//排查部门 同上以此类推
                model.Pcyj = item.Pcyj;//排查院级 同上以此类推
                model.faultpic = item.faultpic;//图片
                model.isdelete = 0;
                listmodel.Add(model);
            }
            var obj = new
            {
                rows = listmodel,
                total = redCount
            };
            return Success("成功", obj);
        }
        #endregion
        #endregion

        #region 患者信息 
        [HttpGet("addpatient")]
        public async Task<IActionResult> Addpatient(Model.Dto.patient tps)
        {
            patient tp = new patient();
            tp.name = tps.name;//姓名
            tp.Gender = tps.Gender;// 性别
            tp.BirthPlace = tps.BirthPlace;//出生地
            tp.Telephone = tps.Telephone;//联系电话
            tp.IdCard = tps.IdCard;///身份证号
            tp.cydeptname = tps.cydeptname;//出院科室
            tp.ryday = tps.ryday;//入院时间
            tp.cyday = tps.cyday;//出院时间
            tp.isdelete = 0;// 是否删除
            var a = _IpatientRepository.AddRetID(tp);
            if (a.Result != null || a.Result != "")
            {
                return Success("成功", a.Result);
            }
            else
            {
                return Error("失败");
            }

        }
        [HttpGet("getpatient")]
        public async Task<IActionResult> Getpatient(int PageIndex, int PageSize)
        {

            //排序字段
            //var sort = Builders<Fxdjtabs>.Sort.Ascending("Szzydjsj");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<patient>>();
            listApp.Add(Builders<patient>.Filter.Eq("isdelete", 0));

            var filter = Builders<patient>.Filter.And(listApp);

            var list = await _IpatientRepository.GetByPage(filter, PageIndex, PageSize);
            var redCount = await _IpatientRepository.CountAsync(filter);   //获取总数
            var listmodel = new List<patient>();
            foreach (var item in list)
            {
                #region 调用短信接口发送短信
                //var datentime = DateTime.Now.ToString("yyyMMdd");
                //var chuytime = "";
                //DateTime cytimeday;
                //if (item.cyday != null)
                //{
                //    cytimeday = Convert.ToDateTime(item.cyday);
                //    chuytime = cytimeday.ToString("yyMMdd");
                //    if (Convert.ToInt32(datentime) - 3 >= Convert.ToInt32(chuytime))
                //    {
                //        #region 调用短信接口
                //        MassageInfo dxjl = await _IMassageInfoRepository.GetSingle(p => p.telcode == item.Telephone);
                //        if (dxjl == null)
                //        {
                //            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                //            ////调用发送短信接口 发送成功就填写数据
                //            ////先转换成 GBK 编码数据
                //            //发送内容
                //            var str = "您好 ！感谢您对我们医院的信任与支持请对我们的服质量给予评价1 满意；2不满意；3 表扬，回复数字即可。谢谢！顺祝生活愉快【郑州市第一人民医院】";
                //            var Mobile = item.Telephone;
                //            //定时发送还是立即发送
                //            var SendTime = "";
                //            string result1 = HttpUtility.UrlEncode(str, Encoding.GetEncoding("GB2312"));
                //            var ifsend = false;
                //            var aaa = " ";
                //            string url = "https://sdk2.028lk.com/sdk2/BatchSend2.aspx?CorpID=DYRMYY011170&Pwd=xxk@80188&Mobile=" + Mobile + "&Content=" + result1 + "&Cell=&SendTime=";

                //            //?CorpID=DYRMYY011170&Pwd=xxk@80188&Mobile=13935966971&Content="+ result1 + "&Cell=&SendTime=

                //            //Encoding encoding = Encoding.GetEncoding("GB2312");
                //            //Encoding encoding = Encoding.GetEncoding("UTF-8");
                //            Encoding encoding = Encoding.GetEncoding("GBK");
                //            IDictionary<string, string> parameters = new Dictionary<string, string>();
                //            parameters.Add("CorpID", "DYRMYY011170");
                //            parameters.Add("Pwd", "xxk@80188");
                //            parameters.Add("Mobile", Mobile);
                //            parameters.Add("Content", result1);
                //            parameters.Add("Cell", "");
                //            parameters.Add("SendTime", SendTime.ToString());
                //            HttpWebResponse response = PostHttps(url, parameters, encoding);
                //            //打印返回值
                //            Stream stream = response.GetResponseStream();   //获取响应的字符串流
                //            StreamReader sr = new StreamReader(stream); //创建一个stream读取流

                //            //返回的信息
                //            string html = sr.ReadToEnd();
                //            #endregion
                //            #region 调用短信之后
                //            if (Convert.ToInt32(html) > 0)
                //            {

                //                MassageInfo tp = new MassageInfo();
                //                tp.telcode = item.Telephone;
                //                tp.sendcenter = str;
                //                tp.senddate = DateTime.Now;
                //                tp.isdelete = 0;
                //                tp.replycenter = "";
                //                tp.sendmbtype = "出院";//模板类型
                //                tp.deptname = item.cydeptname;//科室名称
                //                tp.statesissuccess = "成功";//发送状态
                //                tp.dxmyd = "";//短信随访满意度

                //                var a = _IMassageInfoRepository.AddRetID(tp);

                //                patient patlist = await _IpatientRepository.GetSingle(p => p.Telephone == item.Telephone);
                //                patlist.sfstate = "随访成功";
                //                var b = _IpatientRepository.Update(patlist);
                //            }
                //            else
                //            {
                //                MassageInfo tp = new MassageInfo();
                //                tp.telcode = item.Telephone;
                //                tp.sendcenter = str;
                //                tp.senddate = DateTime.Now;
                //                tp.isdelete = 0;
                //                tp.replycenter = "";
                //                tp.sendmbtype = "出院";//模板类型
                //                tp.deptname = item.cydeptname;//科室名称
                //                tp.statesissuccess = "失败";//发送状态
                //                tp.dxmyd = "";//短信随访满意度
                //                var a = _IMassageInfoRepository.AddRetID(tp);
                //                patient patlist = await _IpatientRepository.GetSingle(p => p.Telephone == item.Telephone);
                //                patlist.sfstate = "随访失败";
                //                var b = _IpatientRepository.Update(patlist);
                //            }
                //            #endregion
                //        }
                //    }
                //} 
                #endregion
                ///定义选项
                var model = new patient();
                model.name = item.name;//姓名
                model.Gender = item.Gender;// 性别
                model.BirthPlace = item.BirthPlace;//出生地
                model.Telephone = item.Telephone;//联系电话
                model.IdCard = item.IdCard;///身份证号
                model.cydeptname = item.cydeptname;//出院科室
                model.ryday = item.ryday;//入院时间
                model.cyday = item.cyday;//出院时间
                model.sfstate = item.sfstate;//随访状态
                model.isdelete = item.isdelete;//是否启用
                listmodel.Add(model);
            }
            var obj = new
            {
                rows = listmodel,
                total = redCount
            };
            return Success("成功", obj);
        }

        public int Sendonedx(Model.Dto.MassageInfo tps)
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            ////调用发送短信接口 发送成功就填写数据
            ////先转换成 GBK 编码数据
            //发送内容
            var str = tps.sendcenter;// "您好 ！感谢您对我们医院的新人月支持请对我们的服质量给予评价1 满意；2不满意；3 表扬，回复数字即可。谢谢！顺祝生活愉快【郑州市第一人民医院】";//您好 ！感谢您对我们医院的新人月支持请对我们的服质量给予评价1 满意；2不满意；3 表扬，回复数字即可。谢谢！顺祝生活愉快  
            //发送给哪个手机号
            var Mobile = tps.telcode;
            //定时发送还是立即发送
            var SendTime = tps.SendTime;
            string result1 = HttpUtility.UrlEncode(str, Encoding.GetEncoding("GB2312"));
            var ifsend = false;
            var aaa = " ";
            string url = "https://sdk2.028lk.com/sdk2/BatchSend2.aspx?CorpID=DYRMYY011170&Pwd=xxk@80188&Mobile=" + Mobile + "&Content=" + result1 + "&Cell=&SendTime=";

            //?CorpID=DYRMYY011170&Pwd=xxk@80188&Mobile=13935966971&Content="+ result1 + "&Cell=&SendTime=

            //Encoding encoding = Encoding.GetEncoding("GB2312");
            //Encoding encoding = Encoding.GetEncoding("UTF-8");
            Encoding encoding = Encoding.GetEncoding("GBK");
            IDictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("CorpID", "DYRMYY011170");
            parameters.Add("Pwd", "xxk@80188");
            parameters.Add("Mobile", Mobile);
            parameters.Add("Content", result1);
            parameters.Add("Cell", "");
            parameters.Add("SendTime", SendTime.ToString());
            HttpWebResponse response = PostHttps(url, parameters, encoding);
            //打印返回值
            Stream stream = response.GetResponseStream();   //获取响应的字符串流
            StreamReader sr = new StreamReader(stream); //创建一个stream读取流

            //返回的信息
            string html = sr.ReadToEnd();
            if (Convert.ToInt32(html) > 0)
            {
                string[] arrStr = tps.telcode.Split(',');
                foreach (var item in arrStr)
                {
                    MassageInfo tp = new MassageInfo();
                    tp.telcode = item;
                    tp.sendcenter = tps.sendcenter;
                    tp.senddate = DateTime.Now;
                    tp.isdelete = 0;
                    tp.replycenter = "";
                    tp.sendmbtype = tps.sendmbtype;//模板类型
                    tp.deptname = tps.deptname;//科室名称
                    tp.statesissuccess = "成功";//发送状态
                    tp.dxmyd = "";//短信随访满意度

                    var a = _IMassageInfoRepository.AddRetID(tp);

                }
                return 0;

            }
            else
            {
                MassageInfo tp = new MassageInfo();
                tp.telcode = tps.telcode;
                tp.sendcenter = tps.sendcenter;
                tp.senddate = DateTime.Now;
                tp.isdelete = 0;
                tp.replycenter = "";
                tp.sendmbtype = tps.sendmbtype;//模板类型
                tp.deptname = tps.deptname;//科室名称
                tp.statesissuccess = "失败";//发送状态
                tp.dxmyd = "";//短信随访满意度

                var a = _IMassageInfoRepository.AddRetID(tp);
                return 1;
            }
        }
        #endregion

        #region 工号导出模板
        ///// <summary>
        ///// 导出模板
        ///// </summary>
        ///// <returns></returns>
        //[HttpGet("downtemplate")]
        //[AllowAnonymous]
        //public IActionResult DownTemplateAsync()
        //{
        //    string mbname = _configuration["upload:mbname"].ToString();
        //    string mbkeys = _configuration["upload:mbkeys"].ToString();

        //    NPOIHelper npoi = new NPOIHelper();
        //    byte[] sm = npoi.ExportToExcelTemplate(mbkeys.Split(","));
        //    if (sm != null)
        //    {
        //        return File(sm, "application/vnd.ms-excel", mbname);
        //    }
        //    else
        //    {
        //        return Error("下载失败");
        //    }
        //}

        ///// <summary>
        ///// 上传文件并将设备信息导入数据库
        ///// </summary>
        ///// <returns></returns>
        //[HttpPost("importexcel")]
        //public async Task<IActionResult> ImportExcel(int headrow = 0)
        //{

        //    Microsoft.AspNetCore.Http.IFormFile _upfile = Request.Form.Files[0];
        //    if (!_upfile.ContentType.Equals("application/vnd.ms-excel") && !_upfile.ContentType.Equals("application/x-xls") && !_upfile.ContentType.Equals("application/x-xlsx") && !_upfile.ContentType.Equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") && !_upfile.ContentType.Equals("application/octet-stream"))
        //        return Error($"请正确上传Excel文件：file.ContentType={_upfile.ContentType}");

        //    NPOIHelper npoi = new NPOIHelper();
        //    var dtExcel = npoi.ExcelToTable1(_upfile, headrow);
        //    int num = dtExcel.Rows.Count;
        //    var cols = dtExcel.Columns;
        //    int colnum = cols.Count;

        //    string dbkeys = _configuration["upload:dbkeys"].ToString();
        //    string[] dbcols = dbkeys.Split(",");
        //    string errmsg = string.Empty;

        //    if (num > 0)
        //    {
        //        int index = 1;
        //        foreach (System.Data.DataRow dr in dtExcel.Rows)
        //        {
        //            var model = new Ghcodes();

        //            var dbcolslist = dbcols.ToList();

        //            Type t = model.GetType();
        //            PropertyInfo[] PropertyList = t.GetProperties();

        //            foreach (PropertyInfo item in PropertyList)
        //            {
        //                if (dbcolslist.Contains(item.Name))
        //                {
        //                    object v = Convert.ChangeType(dr[dbcolslist.IndexOf(item.Name)].ToString(), item.PropertyType);
        //                    item.SetValue(model, v, null);
        //                }
        //            }

        //            bool b = await _IGhcodesRepository.Add(model);
        //            if (!b)
        //            {
        //                if (!string.IsNullOrEmpty(errmsg))
        //                {
        //                    errmsg = errmsg + "\r\n第" + index + "行导入失败！";
        //                }
        //                else
        //                {
        //                    errmsg = "第" + index + "行导入失败！";
        //                }
        //            }
        //            index++;
        //        }
        //    }
        //    else
        //    {
        //        return Error("文件中无数据");
        //    }

        //    if (!string.IsNullOrEmpty(errmsg))
        //    {
        //        //删除已导入的部分
        //        return Error(errmsg);
        //    }

        //    return Success("导入成功");
        //}
        #endregion

        #region 用户模块
        /// <summary>
        /// 用户模块
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> UserDjlist()
        {
            //var string a = HttpClientPost("",);
            return Success("成功");
        }

        public static string HttpClientPost(string url, object datajson)
        {
            HttpClient httpClient = new HttpClient();//http对象
            //表头参数
            httpClient.DefaultRequestHeaders.Accept.Clear();
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //转为链接需要的格式
            HttpContent httpContent = new JsonContent(datajson);
            //请求
            HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
            if (response.IsSuccessStatusCode)
            {
                Task<string> t = response.Content.ReadAsStringAsync();
                if (t != null)
                {
                    return t.Result;
                }
            }
            return "";
        }
        public class JsonContent : StringContent
        {
            public JsonContent(object obj) :
               base(JsonConvert.SerializeObject(obj), Encoding.UTF8, "application/json")
            { }
        }

        [HttpGet("getuserinfobycodename")]
        public async Task<IActionResult> GetUserinfobycodename(string codename)
        {
            var list = new List<FilterDefinition<Sys_User_Account>>();
            list.Add(Builders<Sys_User_Account>.Filter.Eq("delete_flag", false));
            if (!string.IsNullOrEmpty(codename))
                list.Add(Builders<Sys_User_Account>.Filter.Where(p => p.usercode.Contains(codename) || p.username.Contains(codename)));

            var filter = Builders<Sys_User_Account>.Filter.And(list);
            var model = await _sys_user_accountRepository.Get(filter, null, null);//
            return Success("成功", model);
        }

        #endregion
         

    }
}
