public bx_userinfo_renewal_info FindByBuidAsync(long buid)
        {
            var bxWorkOrder = new bx_userinfo_renewal_info();

            try
            {
                bxWorkOrder = new EntityContext().bx_userinfo_renewal_info.OrderByDescending(o => o.create_time).FirstOrDefault(x => x.b_uid == buid);
            }
            catch (Exception ex)
            {
                logError.Info("发生异常:" + ex.Source + "\n" + ex.StackTrace + "\n" + ex.Message + "\n" + ex.InnerException);
            }
            return(bxWorkOrder);
        }
        public bx_userinfo_renewal_info FindById(int workOrderId)
        {
            var bxWorkOrder = new bx_userinfo_renewal_info();

            try
            {
                bxWorkOrder = DataContextFactory.GetDataContext().bx_userinfo_renewal_info.FirstOrDefault(x => x.id == workOrderId);
            }
            catch (Exception ex)
            {
                logError.Info("发生异常:" + ex.Source + "\n" + ex.StackTrace + "\n" + ex.Message + "\n" + ex.InnerException);
            }
            return(bxWorkOrder);
        }
        public int Update(bx_userinfo_renewal_info bxWorkOrder)
        {
            int count = 0;

            try
            {
                DataContextFactory.GetDataContext().bx_userinfo_renewal_info.AddOrUpdate(bxWorkOrder);
                count = DataContextFactory.GetDataContext().SaveChanges();
            }
            catch (Exception ex)
            {
                logError.Info("发生异常:" + ex.Source + "\n" + ex.StackTrace + "\n" + ex.Message + "\n" + ex.InnerException);
            }
            return(count);
        }
        public int Add(bx_userinfo_renewal_info bxWorkOrder)
        {
            int workOrderId = 0;

            try
            {
                var t = DataContextFactory.GetDataContext().bx_userinfo_renewal_info.Add(bxWorkOrder);
                DataContextFactory.GetDataContext().SaveChanges();
                workOrderId = t.id;
            }
            catch (Exception ex)
            {
                logError.Info("发生异常:" + ex.Source + "\n" + ex.StackTrace + "\n" + ex.Message + "\n" + ex.InnerException);
                workOrderId = 0;
            }
            return(workOrderId);
        }
        public static WorkOrderViewModel ConverToViewModel(this bx_userinfo_renewal_info bxWorkOrder)
        {
            WorkOrderViewModel model = new WorkOrderViewModel();

            if (bxWorkOrder != null)
            {
                model.Id               = bxWorkOrder.id;
                model.Intention_View   = bxWorkOrder.client_intention;
                model.Customer_Name    = bxWorkOrder.client_name;
                model.Phone            = bxWorkOrder.client_mobile;
                model.Sa_Agent_Id      = bxWorkOrder.sa_id;
                model.SaAgentName      = bxWorkOrder.sa_name;
                model.Status           = bxWorkOrder.result_status;
                model.Create_Time      = bxWorkOrder.create_time.ToString("yyyy-MM-dd HH:mm");
                model.Adv_Agent_Id     = bxWorkOrder.xubao_id;
                model.AdvAgentName     = bxWorkOrder.xubao_name;
                model.Buid             = bxWorkOrder.b_uid.HasValue?bxWorkOrder.b_uid.Value:0;
                model.IntentionCompany = bxWorkOrder.intentioncompany;
                model.Remark           = bxWorkOrder.remark;
            }
            return(model);
        }
        public ReVisitedListViewModel WorkOrderList(long buid)
        {
            var viewModel = new ReVisitedListViewModel();
            bx_userinfo_renewal_info  model = _userinfoRenewalInfoRepository.FindByBuid(buid);
            List <bx_consumer_review> list  = _consumerReviewRepository.FindDetails(buid);

            if (!list.Any())//model == null &&
            {
                viewModel.BusinessStatus = -10002;
                viewModel.StatusMessage  = "获取信息失败";
                return(viewModel);
            }
            viewModel.BusinessStatus = 1;
            if (model != null)
            {
                var info = new VisitedInfo
                {
                    CustomerName = model.client_name,
                    Phone        = model.client_mobile
                };
                viewModel.VisitedInfo = info;
            }
            if (list.Any())
            {
                var visitedList = new List <VisitedDetail>();
                var item        = new VisitedDetail();
                foreach (var obj in list)
                {
                    item.Id           = obj.id;
                    item.RecallTime   = obj.next_review_date.HasValue ? obj.next_review_date.Value.ToString() : "";
                    item.ResultStatus = obj.result_status;
                    item.CreateTime   = obj.create_time.HasValue ? obj.create_time.Value.ToString() : "";
                    item.Remark       = obj.content;
                    visitedList.Add(item);
                }
                viewModel.VisitedList = visitedList;
            }
            return(viewModel);
        }
        public BaseViewModel AddReVisited(AddReVisitedRequest request)
        {
            var viewModel = new BaseViewModel();

            try
            {
                DateTime dateTime = DateTime.Now;

                var bxUserInfo = new bx_userinfo();
                if (request.Buid.HasValue && request.Buid.Value != 0)
                {//根据buid获取userinfo对象
                    bxUserInfo = _userInfoRepository.FindByBuid(request.Buid.Value);
                }

                if (bxUserInfo == null)
                {
                    viewModel.BusinessStatus = -10002;
                    viewModel.StatusMessage  = "获取信息失败";
                    return(viewModel);
                }
                int workOrderId = 0;
                bx_userinfo_renewal_info workorder = _userinfoRenewalInfoRepository.FindByBuid(request.Buid.Value);
                if (workorder == null)
                {
                    workorder                  = new bx_userinfo_renewal_info();
                    workorder.sa_id            = request.ChildAgent;
                    workorder.sa_name          = request.ChildName;
                    workorder.client_name      = request.CustomerName;
                    workorder.client_mobile    = request.Phone;
                    workorder.b_uid            = request.Buid.Value;
                    workorder.client_intention = 3;//默认添加,其他情况
                    workorder.create_time      = dateTime;
                    workorder.xubao_id         = request.ChildAgent;
                    workorder.xubao_name       = request.ChildName;
                    workOrderId                = _userinfoRenewalInfoRepository.Add(workorder);
                }
                else
                {
                    workOrderId = workorder.id;
                    if (!string.IsNullOrWhiteSpace(request.CustomerName) || !string.IsNullOrWhiteSpace(request.Phone))
                    {
                        workorder.client_name   = request.CustomerName;
                        workorder.client_mobile = request.Phone;
                        if (_userinfoRenewalInfoRepository.Update(workorder) == 0)
                        {
                            //将失败信息插入到error里
                        }
                    }
                }
                if (workOrderId > 0)
                {
                    var workOrderDetail = new bx_consumer_review
                    {
                        b_uid         = (int)bxUserInfo.Id,
                        content       = request.Remark,
                        operatorId    = request.ChildAgent,
                        operatorName  = request.ChildName,
                        read_status   = 0,              //0未读,1已读
                        status        = request.Status, //跟进类型,0回访,1投诉,2跟进..
                        result_status = 4,              //受理结果,默认 其他。目前暂不使用
                        create_time   = dateTime
                    };
                    if (!string.IsNullOrWhiteSpace(request.NextReviewDate))
                    {
                        workOrderDetail.next_review_date = DateTime.Parse(request.NextReviewDate);
                    }
                    int workOrderDetailId = _consumerReviewRepository.AddDetail(workOrderDetail);
                    if (workOrderDetailId > 0)
                    {
                        viewModel.BusinessStatus = 1;
                    }
                    else
                    {
                        viewModel.BusinessStatus = -10002;
                        viewModel.StatusMessage  = "添加信息失败";
                    }
                }
            }
            catch (Exception ex)
            {
                viewModel.BusinessStatus = -10003;
                viewModel.StatusMessage  = "服务器发生异常";
                logError.Info("添加受理记录异常,受理记录信息:" + request.ToJson() + "\n 异常信息:" + ex.StackTrace + " \n " + ex.Message);
            }
            return(viewModel);
        }
        /// <summary>
        /// 业务:sa用。第一次创建WorkOrder
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public AddOrUpdateWorkOrderResponse AddOrUpdateWorkOrder(AddOrUpdateWorkOrderRequest request, IEnumerable <KeyValuePair <string, string> > pairs)
        {
            var response = new AddOrUpdateWorkOrderResponse();

            //bhToken校验
            if (!AppTokenValidateReqest(request.BhToken, request.ChildAgent))
            {
                response.ErrCode = -300;
                response.ErrMsg  = "登录信息已过期,请重新登录";
                return(response);
            }
            //传参校验
            IBxAgent agentModel = GetAgentModelFactory(request.Agent);

            //参数校验
            if (agentModel == null)// || childAgent == null)
            {
                response.Status = HttpStatusCode.BadRequest;
                return(response);
            }
            if (!AppValidateReqest(pairs, request.SecCode))
            {
                response.Status = HttpStatusCode.Forbidden;
                return(response);
            }
            if (request.IntentionView == 0)
            {
                response.ErrCode = -1;
                response.ErrMsg  = "意向不能为空";
                return(response);
            }
            if (request.OwnerAgent == 0)
            {
                response.ErrCode = -2;
                response.ErrMsg  = "车辆当前受理人Id不能为空";
                return(response);
            }
            try
            {
                bx_userinfo bxUserInfo;
                if (request.Buid.HasValue && request.Buid.Value != 0)
                {
                    //如果传buid过来,重新赋值request
                    bxUserInfo = _userInfoRepository.FindByBuid(request.Buid.Value);
                    if (bxUserInfo != null)
                    {
                        request.LicenseNo  = bxUserInfo.LicenseNo;
                        request.OwnerAgent = int.Parse(bxUserInfo.Agent);
                    }
                }
                else
                {
                    //根据OpenId、车牌号、代理人Id找userinfo对象
                    bxUserInfo = _userInfoRepository.FindByAgentLicense(request.LicenseNo, request.OwnerAgent.ToString());
                }

                if (bxUserInfo == null)
                {
                    response.ErrCode = 0;
                    response.ErrMsg  = "查不到此续保信息";
                    return(response);
                }
                int      workOrderId = 0;//标记添加修改意向成功与否
                DateTime dateTime    = DateTime.Now;
                bx_userinfo_renewal_info workOrder = _userinfoRenewalInfoRepository.FindByBuid(bxUserInfo.Id);
                if (workOrder != null)
                {//如果库里有记录,执行更新
                    workOrder.sa_id            = request.ChildAgent;
                    workOrder.sa_name          = request.ChildName;
                    workOrder.client_name      = request.CustomerName;
                    workOrder.client_mobile    = request.Phone;
                    workOrder.client_intention = request.IntentionView;
                    workOrder.remark           = request.Remark;
                    workOrderId = _userinfoRenewalInfoRepository.Update(workOrder);
                }
                else
                {//如果库里无记录,执行插入
                    workOrder                  = new bx_userinfo_renewal_info();
                    workOrder.sa_id            = request.ChildAgent;
                    workOrder.sa_name          = request.ChildName;
                    workOrder.client_name      = request.CustomerName;
                    workOrder.client_mobile    = request.Phone;
                    workOrder.b_uid            = bxUserInfo.Id;
                    workOrder.client_intention = request.IntentionView;
                    workOrder.remark           = request.Remark;
                    workOrder.create_time      = dateTime;
                    workOrderId                = _userinfoRenewalInfoRepository.Add(workOrder);
                }
                if (workOrderId > 0)
                {
                    response.ErrCode     = 1;
                    response.WorkOrderId = workOrderId;
                }
                else
                {
                    response.ErrCode = -10002;
                    response.ErrMsg  = "添加意向失败,未存在可更新的内容";
                }
            }
            catch (Exception ex)
            {
                response.ErrCode = -10003;
                logError.Info("意向接口异常,添加意向信息:" + request.ToJson() + "\n 异常信息:" + ex.StackTrace + " \n " + ex.Message);
            }
            return(response);
        }
        /// <summary>
        /// 业务:续保顾问用。创建WorkOrderDetail对象和更新WorkOrder对象
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public AddOrUpdateWorkOrderDetailResponse AddOrUpdateWorkOrderDetail(AddOrUpdateWorkOrderDetailRequest request, IEnumerable <KeyValuePair <string, string> > pairs)
        {
            var response = new AddOrUpdateWorkOrderDetailResponse();

            //bhToken校验
            if (!AppTokenValidateReqest(request.BhToken, request.ChildAgent))
            {
                response.ErrCode = -300;
                response.ErrMsg  = "登录信息已过期,请重新登录";
                return(response);
            }
            //传参校验
            IBxAgent agentModel = GetAgentModelFactory(request.Agent);

            //参数校验
            if (!agentModel.AgentCanUse())
            {
                response.Status = HttpStatusCode.BadRequest;
                return(response);
            }
            if (!AppValidateReqest(pairs, request.SecCode))
            {
                response.Status = HttpStatusCode.Forbidden;
                return(response);
            }

            if (request.Status == 0)
            {
                response.ErrCode = -1;
                response.ErrMsg  = "受理状态不能为空";
                return(response);
            }
            try
            {
                DateTime dateTime = DateTime.Now;
                bx_userinfo_renewal_info workOrder;

                bx_userinfo bxUserInfo;
                if (request.Buid.HasValue && request.Buid.Value != 0)
                {
                    //如果传buid过来,重新赋值request
                    bxUserInfo = _userInfoRepository.FindByBuid(request.Buid.Value);
                    if (bxUserInfo != null)
                    {
                        request.LicenseNo  = bxUserInfo.LicenseNo;
                        request.OwnerAgent = int.Parse(bxUserInfo.Agent);
                    }
                }
                else
                {
                    //根据OpenId、车牌号、代理人Id找userinfo对象
                    bxUserInfo = _userInfoRepository.FindByAgentLicense(request.LicenseNo, request.OwnerAgent.ToString());
                }

                if (bxUserInfo == null)
                {
                    response.ErrCode = 0;
                    response.ErrMsg  = "查不到此续保信息";
                    return(response);
                }
                int workOrderId = request.WorkOrderId;
                if (workOrderId == 0)
                {
                    workOrder                  = new bx_userinfo_renewal_info();
                    workOrder.sa_id            = request.SaAgent;
                    workOrder.sa_name          = request.SaAgentName;
                    workOrder.client_name      = request.CustomerName;
                    workOrder.client_mobile    = request.Phone;
                    workOrder.b_uid            = (int)bxUserInfo.Id;
                    workOrder.client_intention = 3;//默认添加,其他情况
                    //workOrder.remark = request.Remark;
                    workOrder.create_time = dateTime;
                    workOrder.xubao_id    = request.ChildAgent;
                    workOrder.xubao_name  = request.ChildName;
                    workOrderId           = _userinfoRenewalInfoRepository.Add(workOrder);
                }
                if (workOrderId > 0)
                {
                    var workOrderDetail = new bx_consumer_review
                    {
                        b_uid        = (int)bxUserInfo.Id,
                        content      = request.Remark,
                        operatorId   = request.ChildAgent,
                        operatorName = request.ChildName
                    };
                    if (!string.IsNullOrWhiteSpace(request.NextReviewDate))
                    {
                        workOrderDetail.next_review_date = DateTime.Parse(request.NextReviewDate);
                    }
                    workOrderDetail.read_status      = 0;
                    workOrderDetail.status           = 0;//跟进类型,0回访,1投诉,2跟进..
                    workOrderDetail.result_status    = request.Status;
                    workOrderDetail.intentioncompany = request.IntentionCompany;
                    workOrderDetail.create_time      = dateTime;
                    int workOrderDetailId = _consumerReviewRepository.AddDetail(workOrderDetail);
                    if (workOrderDetailId > 0)
                    {
                        if (workOrderId != 0)
                        {
                            if (request.Status == 1)
                            {
                                //5,添加出单通知
                                var msgRequest = new AddMessageRequest
                                {
                                    Body       = request.LicenseNo,
                                    MsgType    = 7,
                                    ToAgentId  = request.Agent,
                                    MsgLevel   = 1,
                                    SendTime   = DateTime.Now.ToString(),
                                    ChildAgent = request.ChildAgent,
                                    LicenseNo  = request.LicenseNo,
                                    Buid       = bxUserInfo.Id,
                                    Source     = request.IntentionCompany
                                };
                                _messageService.AddMessage(msgRequest);
                            }
                            //修改意向里的主记录
                            workOrder = new bx_userinfo_renewal_info();
                            workOrder = _userinfoRenewalInfoRepository.FindById(workOrderId);
                            if (workOrder != null)
                            {
                                workOrder.intentioncompany = request.IntentionCompany;
                                workOrder.result_status    = request.Status;
                                workOrder.xubao_id         = request.ChildAgent;
                                workOrder.xubao_name       = request.ChildName;
                                _userinfoRenewalInfoRepository.Update(workOrder);
                                //if (_userinfoRenewalInfoRepository.Update(workOrder) > 0)
                                //{
                                response.ErrCode           = 1;
                                response.WorkOrderDetailId = workOrderDetailId;
                                //}
                                //else
                                //{
                                //    response.ErrCode = 0;
                                //}
                            }
                        }
                        else
                        {
                            response.ErrCode = 0;
                            //response.ErrCode = 1;
                            //response.WorkOrderDetailId = workOrderDetailId;
                        }
                    }
                    else
                    {
                        response.ErrCode = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                response.ErrCode = 0;
                logError.Info("添加受理记录异常,受理记录信息:" + request.ToJson() + "\n 异常信息:" + ex.StackTrace + " \n " + ex.Message);
            }
            return(response);
        }