using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Cmdjy.Bll;
using Cmdjy.Dal;
using CommonHelper;
using Newtonsoft.Json;
using WsdInterfaceModels;

namespace Cmdjy.Controllers
{
    public class WsdInterfaceController:ControllerBase
    {
        public Lazy<DjyDbContext> Db { get; set; }

        public ActionResult Index() {
            var model = new WsdRequest {
                StartNo = "0",MaxCount = "10",
                CompanyCode = "1",CompanyName = "万仕达",
            };
            return PartialView(model);
        }

        public ActionResult GetData(WsdRequest info,string noDes,string needNull) {
            var result = new WsdResult {
                Code = EnumCode.Success,
            };
            string cKey = null;
            var db = this.Db.Value;
            var companyInfo = db.CompanyInfos.Where(m => m.Id.ToString() == info.CompanyCode);
            if(companyInfo.Any()) {
                cKey = companyInfo.First().SecurityKey;
            }
            if(string.IsNullOrEmpty(cKey)) {
                result.Code = EnumCode.Exception;
                result.Msg = "CompanyCode值错误或平台未注册该机构";
            }
            //获取数据
            int start = 0;
            if(!int.TryParse(info.StartNo,out start)) {
                result.Code = EnumCode.Exception;
                result.Msg = "Start值错误";
            }
            int count = 0;
            if(!int.TryParse(info.MaxCount,out count)) {
                result.Code = EnumCode.Exception;
                result.Msg = "MaxCount值错误";
            }
            if(count <= 0) {
                result.Code = EnumCode.Exception;
                result.Msg = "Count值错误。";
            }
            if(result.Code != EnumCode.Exception) {
                result.Prescriptions = new List<WsdPrescriptionInfo>();
                count = count > WebSettings.WsdMaxCount ? WebSettings.WsdMaxCount : count;
                var cache = CacheFactory.Cache;
                for(int i = start;i < start + count;i++) {
                    //从缓存获取
                    var pkey = $"Company:Id:{info.CompanyCode}:Pre:Id:{i}";
                    var one = cache.GetData<WsdPrescriptionInfo>(pkey);
                    if(one == null) {
                        //从数据库获取处方信息
                        one = getPrescriptionInfoFromDb(info.CompanyCode,i);
                        //判断是否为
                        //获取药品信息
                        if(one != null) one.Drugs = getDrugInfoFromDb(one.Id);
                        //存缓存
                        if(one != null) cache.SetData(pkey,one);
                    }
                    if(needNull != null && one == null) {
                        one = new WsdPrescriptionInfo {
                            Type = WsdPrescriptionType.NullOrder,Id = i,
                        };
                    }
                    if(one != null) {
                        result.Prescriptions.Add(one);
                    }
                }
                result.Code = EnumCode.Success;
                result.Count = result.Prescriptions.Where(m => m.Type != WsdPrescriptionType.NullOrder).Count();
                result.Msg = "";
                result.MaxId = db.PrescriptionInfos.Max(m => m.Id).ToString();
                result.More = db.PrescriptionInfos.Max(m => m.Id) > (start + count);
            }
            //编码：json串 +  加密信息
            //return Json(result);
            var resStr = JsonConvert.SerializeObject(result);
            var mw = noDes == null ? DesHelper.GetHelper().Encrypty(cKey,resStr) : resStr;
            logWsdRequest(info,result,resStr,mw);
            return Content(mw);
        }
        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="info">请求信息</param>
        /// <param name="result">响应对象</param>
        /// <param name="resStr">响应字符串</param>
        /// <param name="mw">响应密文</param>
        private void logWsdRequest(WsdRequest info,WsdResult result,string resStr,string mw) {
            var db = this.Db.Value;
            if(result != null && result.Prescriptions != null) {
                foreach(var p in result.Prescriptions) {
                    if(p.Drugs != null) {
                        foreach(var d in p.Drugs) {
                            db.WsdRequestLogs.Add(new Dal.Tables.WsdRequestLog {
                                StartNo = info.StartNo,MaxCount = info.MaxCount,
                                ClientAddress = Request.UserHostAddress,
                                LogDatatime = DateTime.Now,DrugId = d.Id.ToString(),
                                PrescriptionId = p.Id.ToString(),
                                Code = result.Code.ToString(),Msg = result.Msg,
                            });
                        }
                    }
                }
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 从数据库获取药品列表
        /// </summary>
        /// <param name="id">处方号</param>
        private List<WsdDrugInfo> getDrugInfoFromDb(int id) {
            List<WsdDrugInfo> result = null;
            var db = this.Db.Value;
            var qu = db.DrugInfos.Where(m => m.PrescriptionId == id);
            if(qu.Any()) {
                result = new List<WsdDrugInfo>();
                foreach(var item in qu) {
                    var od = new WsdDrugInfo();
                    od.CopyFrom(item);
                    od.PrescriptionId = item.PrescriptionId.ToString();
                    result.Add(od);
                }
            }
            return result;
        }
        /// <summary>
        /// 从数据库获取处方
        /// </summary>
        /// <param name="i">处方号</param>
        private WsdPrescriptionInfo getPrescriptionInfoFromDb(string cId,int i) {
            WsdPrescriptionInfo result = null;
            var db = this.Db.Value;
            var qu = db.PrescriptionInfos
                .Where(m => m.Id == i && m.CompanyCode == cId)
                .Where(m => m.Type == Dal.Tables.PrescriptionType.Order || m.Type == Dal.Tables.PrescriptionType.CancelOrder);
            if(qu.Any()) {
                var fir = qu.First();
                result = new WsdPrescriptionInfo();
                result.CopyFrom(fir);
            }
            return result;
        }

    }
}