using DSWeb.Common.DB; using DSWeb.Common.DbContexts; using DSWeb.Common.DbContexts.Model; using log4net; using Newtonsoft.Json.Linq; using Quartz; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; namespace DSWeb.Job.InsuranceTask { public class InsuranceTaskJob : IJob { private readonly ILog _logger = LogManager.GetLogger(typeof(InsuranceTaskJob)); public void Execute(IJobExecutionContext context) { _logger.Debug("调度执行..."); GetInsuranceState(); } public void GetInsuranceState() { CommonDataContext commonData = new CommonDataContext(); string HTUrl = ""; string userKey = ""; var paraUrl = commonData.ParamSets.AsNoTracking().FirstOrDefault(p => p.PARAMNAME == "HuaiTaiInsuranceUrl"); if (paraUrl == null || string.IsNullOrEmpty(paraUrl.PARAMVALUE)) { throw new Exception("华泰保险url配置有误,请联系管理员"); } HTUrl = paraUrl.PARAMVALUE; if (!HTUrl.EndsWith("/")) { HTUrl = HTUrl + "/"; } var paraUserKey = commonData.ParamSets.AsNoTracking().FirstOrDefault(p => p.PARAMNAME == "HuaiTaiInsuranceUserKey"); if (paraUserKey == null || string.IsNullOrEmpty(paraUserKey.PARAMVALUE)) { throw new Exception("华泰保险用户KEY配置有误,请联系管理员"); } userKey = paraUserKey.PARAMVALUE; OpSeaePxDbContenxt context = new OpSeaePxDbContenxt(); HttpClient httpclient = new HttpClient(); var lis = context.Insurance.Where(p => p.states != "保单生效" && p.states != "发送失败" && p.states != "已录入").ToList(); _logger.Debug("本次调度" + lis.Count() + "条记录"); foreach (var item in lis) { try { System.Threading.Tasks.Task responseMessageState; _logger.Debug($"调用流水号:{item.SerialNumber}"); responseMessageState = httpclient.GetAsync(HTUrl + "api/ApiInsurancePolicy/GetInsuranceState?apiUserKey=" + userKey + "&serialNumber=" + item.SerialNumber); var resultState = responseMessageState.Result.Content.ReadAsStringAsync().Result; _logger.Debug($"华泰返回:{resultState}"); var resultState_ = Newtonsoft.Json.JsonConvert.DeserializeObject(resultState); var SendData = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(resultState_.ToString()); var rtnFlag = Convert.ToInt32(SendData["Flag"]); _logger.Debug("返回状态:" + rtnFlag); if (rtnFlag > 0) { InsuranceRider ride = new InsuranceRider(); ride.Rider = DateTime.Now; item.PdfURL = SendData["PdfURL"].ToString(); item.InsuranceNumber = SendData["InsurancePolicy"].ToString(); item.TheBathNum = SendData.ToString().Contains("EdrPolicy") ? SendData["EdrPolicy"].ToString() : ""; item.states = "发送成功"; ride.Remark = "保单已提交"; if (rtnFlag == 1) { if (SendData["Premium"] != null && SendData["Premium"].ToString() != "") { item.Premium = Convert.ToDecimal(SendData["Premium"]); } item.states = "保单生效"; ride.Remark = "保单已生效"; } if (rtnFlag == 2) { item.states = "人工核保"; ride.Remark = "正在人工核保"; } if (rtnFlag == 3) { item.states = "核保通过待确认"; if (!string.IsNullOrEmpty(SendData["ChargeRate"].ToString()) && !string.IsNullOrEmpty(SendData["Premium"].ToString()) && !string.IsNullOrEmpty(SendData["ExchangeRate"].ToString())) { item.ChargeRate = System.Decimal.Round(Convert.ToDecimal(SendData["ChargeRate"]), 5); item.SpecialAgreement = SendData["SpecialAgreement"].ToString(); item.Premium = System.Decimal.Round(Convert.ToDecimal(SendData["Premium"]), 5); item.ExchangeRate = System.Decimal.Round(Convert.ToDecimal(SendData["ExchangeRate"]), 5); _logger.Debug($"核保通过待确认:{item.ChargeRate}"); ride.Remark = "保单待确认,免赔声明更新,保费更新为" + System.Decimal.Round(Convert.ToDecimal(SendData["Premium"]), 5) + " 汇率更新为:" + System.Decimal.Round(Convert.ToDecimal(SendData["ExchangeRate"]), 5) + " 费率更新为万分之" + System.Decimal.Round(Convert.ToDecimal(SendData["ChargeRate"]) * 10000, 5); } var mianpeiContent = SendData["SpecialAgreement"].ToString(); item.MianPeiContent = mianpeiContent; } if (rtnFlag == 4) { item.states = "待确认"; ride.Remark = "保单待确认"; } var log = context.InsuranceRider.Where(p => p.NID == item.GID).OrderByDescending(p => p.Rider).FirstOrDefault(); if (log.RiderState == item.states) { _logger.Debug(item.SerialNumber + "状态未改变," + item.states); continue; } ride.RiderState = item.states; ride.RiderAudit = "华泰保险"; ride.Gid = Guid.NewGuid().ToString(); ride.NID = item.GID; context.InsuranceRider.Add(ride); context.SaveChanges(); } else { var backs = context.InsuranceBack.Where(p => p.NID == item.GID & p.BackCause == "保单生效").ToList(); var sids = context.InsuranceRider.Where(p => p.NID == item.GID & p.RiderState == "保单生效").ToList(); if (backs.Count() <= 0 || sids.Count() <= 0) { item.SerialNumber = "QDDJY" + DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond; _logger.Debug("流水号已重新生成:" + item.SerialNumber); } item.states = "发送失败"; InsuranceBack back = new InsuranceBack(); back.BackTim = DateTime.Now; back.BackCause = "驳回保单"; back.BackSide = "华泰保险"; back.Remark = SendData["Msg"].ToString(); back.Gid = Guid.NewGuid().ToString(); back.NID = item.GID; context.InsuranceBack.Add(back); context.SaveChanges(); } _logger.Debug(item.SerialNumber + "执行完成," + item.states); } catch (Exception ex) { _logger.Debug("error:" + ex.Message.ToString()+" 流水号:"+item.SerialNumber); continue; } } } } }