public HttpResponseMessage UploadMultipleImg([FromBody] UploadMultipleImgRequest request)
        {
            logInfo.Info("请求图片请求:" + request.ToJson());
            var result = _uploadMultipleImgService.UploadMultipleImg(request);

            return(result.ResponseToJson());
        }
        public BaseViewModel ValidateTimes(UploadMultipleImgRequest request)
        {
            BaseViewModel viewModel = new BaseViewModel();

            viewModel.BusinessStatus = 1;
            //判断次数
            if (UpImgCount != 0)
            {                         //如果=0,没有上传限制
                int alreadyTimes = 0; //已经上传的次数
                if (!CheckTimes(request.BuId, out alreadyTimes))
                {
                    return(BaseViewModel.GetBaseViewModel(-10013, "上传次数过多,请直接去保险系统修改!"));
                }
            }
            return(viewModel);
        }
        public BaseViewModel Validate(UploadMultipleImgRequest request)
        {
            BaseViewModel viewModel = new BaseViewModel();

            #region 参数验证
            bx_userinfo userinfo = new bx_userinfo();
            if (request.BuId > 0)
            {
                //userinfo是否有值,buid是否正确
                userinfo = _userInfoRepository.FindByBuid(request.BuId);
            }
            //modifybygpj20181205修改,不开放前端传buid了
            else
            {
                //如果前端传子集代理Id,则用子集代理Id来查数据
                request.Agent = request.ChildAgent > 0 ? request.ChildAgent : request.Agent;
                //参数有效性校验
                if (string.IsNullOrWhiteSpace(request.LicenseNo) || string.IsNullOrWhiteSpace(request.CustKey) || request.Agent < 1)
                {
                    return(BaseViewModel.GetBaseViewModel(-10000, "车牌号、CustKey、Agent不允许为空"));
                }
                //根据车牌、代理、custkey来取数据库userinfo
                userinfo = _userInfoRepository.FindByOpenIdAndLicense(request.CustKey, request.LicenseNo, request.Agent.ToString(), request.RenewalCarType);
            }
            //如果userinfo没记录,直接提示错误参数
            if (userinfo == null || userinfo.Id == 0)
            {
                return(BaseViewModel.GetBaseViewModel(-10000, "参数错误,无法拿到请求的车辆信息"));
            }
            //给buid赋值
            if (request.BuId == 0)
            {
                request.BuId = userinfo.Id;
            }
            int source = SourceGroupAlgorithm.GetOldSource(request.Source);
            //submitinfo是否有值
            bx_submit_info submitInfo = _submitInfoRepository.GetSubmitInfo(request.BuId, source);
            if (submitInfo == null)
            {
                return(BaseViewModel.GetBaseViewModel(-10000, "未取到核保信息,请稍后再试"));
            }
            //是否需要上传图片
            if (request.Source == 2)
            {
                if (string.IsNullOrEmpty(submitInfo.documentGroupId))
                {
                    return(BaseViewModel.GetBaseViewModel(-10000, "请确认是否需要上传类型范围内的证件资料"));
                }
            }
            else
            {
                if (string.IsNullOrEmpty(submitInfo.biz_tno) && string.IsNullOrEmpty(submitInfo.force_tno))
                {
                    return(BaseViewModel.GetBaseViewModel(-10000, "请确认是否需要上传类型范围内的证件资料"));
                }
            }
            //上传的图片最少1张
            var dic = request.ListBaseContect;//JsonConvert.DeserializeObject<Dictionary<int, string>>(request.ListBaseContect);
            if (dic.Count == 0)
            {
                viewModel.BusinessStatus = -10000;
                viewModel.StatusMessage  = "输入参数错误,ListBaseContect为空";
                return(viewModel);
            }
            //不能超过11张上传图片//以前是8张的限制
            else if (dic.Count > 11)
            {
                viewModel.BusinessStatus = -10000;
                viewModel.StatusMessage  = "最多11张图片";
                return(viewModel);
            }
            #endregion
            //校验上传次数
            var timesValidate = _checkUploadImgTimes.ValidateTimes(request);
            if (timesValidate.BusinessStatus == -10013)
            {
                viewModel.BusinessStatus = -10013;
                viewModel.StatusMessage  = timesValidate.StatusMessage;
                return(viewModel);
            }
            return(BaseViewModel.GetBaseViewModel(1, "校验成功"));
        }
        public UploadMultipleViewModel UploadMultipleImg(UploadMultipleImgRequest request)
        {
            UploadMultipleViewModel viewModel = new UploadMultipleViewModel();

            try
            {
                //验证
                var validate = _uploadImgValidate.Validate(request);
                if (validate.BusinessStatus != 1)
                {
                    //参数校验失败,返回对应结果
                    viewModel.BusinessStatus = validate.BusinessStatus;
                    viewModel.StatusMessage  = validate.StatusMessage;
                    return(viewModel);
                }

                //上传逻辑
                bool isOk = true;
                List <UrlAndType> listImg = new List <UrlAndType>();
                List <UploadMultipleFileResult> listResult = new List <UploadMultipleFileResult>();
                //上传的图片
                List <BaseContect> dic = request.ListBaseContect;
                foreach (var item in dic)
                {
                    string upImgUrl = string.Empty;
                    //获取图片路径
                    if (item.IsUpload == 1)
                    {//无需上传
                        upImgUrl = item.StrBase;
                    }
                    else if (item.IsUpload == 0)
                    {//需要上传
                        string base64   = item.StrBase.Substring(item.StrBase.IndexOf(',') + 1);
                        byte[] data     = Convert.FromBase64String(base64);
                        var    versions = new Dictionary <string, string>();
                        versions.Add("_large", "maxwidth=600&maxheight=400&format=jpg");
                        var fileUploadModel = new FileUploadModel
                        {
                            FileName   = "xxxxx.jpg",
                            VersionKey = versions
                        };
                        //上传图片至服务器
                        var      dw      = new DynamicWebService();
                        object[] postArg = new object[2];
                        postArg[0] = fileUploadModel.ToJson();
                        postArg[1] = data;
                        var ret = dw.InvokeWebservice(
                            _imageUpload + "/fileuploadcenter.asmx", "BiHuManBu.ServerCenter.FileUploadCenter",
                            "FileUploadCenter", "ImageUpload", postArg);

                        UploadMultipleFileResult itemResult = ret.ToString().FromJson <UploadMultipleFileResult>();
                        //itemResult.Index = item.ImgId;//item.Key;
                        itemResult.ImgType = item.ImgType;
                        listResult.Add(itemResult);

                        if (itemResult.ResultCode == 0)
                        {
                            upImgUrl = itemResult.FilePath;
                        }
                        else
                        {
                            isOk = false;
                        }
                    }
                    //将图片Url保存到List中,存库
                    UrlAndType imgUrl = new UrlAndType
                    {
                        Url  = upImgUrl.Contains(_uploadImgUrl) ? upImgUrl : _uploadImgUrl + upImgUrl,
                        Type = item.ImgType
                    };
                    listImg.Add(imgUrl);
                }
                viewModel.ListResult = listResult;
                if (!isOk)
                {
                    viewModel.BusinessStatus = -10002;
                    viewModel.StatusMessage  = "某个图片上传失败,具体内容查看ListResult";
                }
                else
                {
                    var redisKey     = "UpImgTimes_" + request.BuId;
                    var times        = CacheProvider.Get <int?>(redisKey);
                    int alreadyTimes = times.HasValue ? times.Value : 0;
                    //修改redis中的上传次数
                    if (_upImgCount != 0)
                    {//如果=0,没有上传限制
                        _updateImgTimes.UpdateTimes(request.BuId, ++alreadyTimes);
                    }
                    AddMultipleInput input = new AddMultipleInput
                    {
                        BuId    = request.BuId,
                        UrlList = listImg,
                        Source  = request.Source
                    };
                    logInfo.Info("请求图片请求:" + input.ToJson());
                    var upImg = _pictureService.AddMultiple(input);
                    var re    = new UploadMultipleViewModel()
                    {
                        BusinessStatus = upImg.BusinessStatus,
                        StatusMessage  = upImg.StatusMessage,
                        UrlList        = listImg
                    };
                    return(re);
                }
            }
            catch (Exception ex)
            {
                viewModel.BusinessStatus = -10003;
                viewModel.StatusMessage  = "图片上传异常";
                logError.Info("获取报价单详情接口发生异常:" + ex.Source + "\n" + ex.StackTrace + "\n" + ex.Message + "\n" + ex.InnerException);
            }
            return(null);
        }