Exemplo n.º 1
0
        /// <summary>
        /// 新增或修改资料信息
        /// </summary>
        /// <param name="dataInfo"></param>
        /// <param name="workUser"></param>
        /// <returns></returns>
        public ReturnValueModel AddOrUpdateProductInfo(HcpDataInfoInputDto productInfoView, WorkUser workUser)
        {
            ReturnValueModel rvm = new ReturnValueModel();

            var data = _rep.FirstOrDefault <HcpDataInfo>(s => s.Id == productInfoView.dataInfo.Id);

            var approvalEnabled = _systemService.AdminApprovalEnabled; //是否启用审核

            if (data == null)
            {
                productInfoView.dataInfo.CreateTime  = DateTime.Now;
                productInfoView.dataInfo.CreateUser  = workUser.User.Id;
                productInfoView.dataInfo.IsCompleted = approvalEnabled ? EnumComplete.AddedUnapproved : EnumComplete.Approved;
            }
            else
            {
                if (data.IsDeleted == 1)
                {
                    rvm.Msg     = "This DataInfo has been deleted.";
                    rvm.Success = false;
                    return(rvm);
                }
                switch (data.IsCompleted ?? EnumComplete.AddedUnapproved)
                {
                case EnumComplete.Approved:
                    if (approvalEnabled)
                    {
                        data.IsCompleted = EnumComplete.Locked;
                        _rep.Update(data);
                        _rep.SaveChanges();

                        //复制一条新数据
                        productInfoView.dataInfo.CreateTime  = data.CreateTime;
                        productInfoView.dataInfo.CreateUser  = data.CreateUser;
                        productInfoView.dataInfo.UpdateTime  = DateTime.Now;
                        productInfoView.dataInfo.UpdateUser  = workUser.User.Id;
                        productInfoView.dataInfo.IsCompleted = EnumComplete.UpdatedUnapproved;
                        productInfoView.dataInfo.OldId       = data.Id;
                        data = null;
                    }
                    break;

                case EnumComplete.Reject:
                case EnumComplete.AddedUnapproved:
                case EnumComplete.UpdatedUnapproved:
                    data.IsCompleted = string.IsNullOrEmpty(data.OldId) ? EnumComplete.AddedUnapproved : EnumComplete.UpdatedUnapproved;
                    break;

                default:
                    rvm.Msg     = "This DataInfo can not be changed at this time.";
                    rvm.Success = false;
                    return(rvm);
                }
            }

            if (data == null)
            {
                var dataTitle = _rep.FirstOrDefault <HcpDataInfo>(s => s.Title == productInfoView.dataInfo.Title && s.IsDeleted == 0);
                if (dataTitle != null)
                {
                    rvm.Msg     = "fail";
                    rvm.Success = false;
                    rvm.Msg     = $"已存在【{productInfoView.dataInfo.Title}】的资料";
                    return(rvm);
                }

                using (var tran = _rep.Database.BeginTransaction())
                {
                    try
                    {
                        productInfoView.dataInfo.Id                = Guid.NewGuid().ToString();
                        productInfoView.dataInfo.MediaType         = 1;
                        productInfoView.dataInfo.ProductTypeInfoId = "";
                        _rep.Insert(productInfoView.dataInfo);
                        _rep.SaveChanges();


                        if (productInfoView.ProductAndDeps != null &&
                            productInfoView.ProductAndDeps.CatalogueNameList != null &&
                            productInfoView.ProductAndDeps.BuNameList != null)
                        {
                            HcpDataCatalogueRel hcpDataCatalogueRel;
                            foreach (var buName in productInfoView.ProductAndDeps.BuNameList)
                            {
                                foreach (var catalogue in productInfoView.ProductAndDeps.CatalogueNameList)
                                {
                                    var hcpCatalogueManage = _rep.FirstOrDefault <HcpCatalogueManage>(s => s.CatalogueName == catalogue && s.BuName == buName && s.IsDeleted == 0);
                                    if (hcpCatalogueManage == null)
                                    {
                                        rvm.Msg     = "fail";
                                        rvm.Success = false;
                                        rvm.Result  = $"获取目录信息失败";
                                        return(rvm);
                                    }
                                    hcpDataCatalogueRel    = new HcpDataCatalogueRel();
                                    hcpDataCatalogueRel.Id = Guid.NewGuid().ToString();
                                    hcpDataCatalogueRel.HcpCatalogueManageId   = hcpCatalogueManage.Id;
                                    hcpDataCatalogueRel.HcpCatalogueManageName = hcpCatalogueManage.CatalogueName;
                                    hcpDataCatalogueRel.HcpDataInfoId          = productInfoView.dataInfo.Id;
                                    hcpDataCatalogueRel.CreateTime             = DateTime.Now;
                                    hcpDataCatalogueRel.CreateUser             = workUser.User.Id;
                                    _rep.Insert(hcpDataCatalogueRel);
                                }
                            }
                        }
                        if (productInfoView.ProductAndDeps != null &&
                            productInfoView.ProductAndDeps.BuNameList != null &&
                            productInfoView.ProductAndDeps.Products != null &&
                            productInfoView.ProductAndDeps.Departments != null)
                        {
                            HcpMediaDataRel mediaDataRel;
                            foreach (var buName in productInfoView.ProductAndDeps.BuNameList)
                            {
                                foreach (var pro in productInfoView.ProductAndDeps.Products)
                                {
                                    foreach (var dep in productInfoView.ProductAndDeps.Departments)
                                    {
                                        mediaDataRel            = new HcpMediaDataRel();
                                        mediaDataRel.Id         = Guid.NewGuid().ToString();
                                        mediaDataRel.DataInfoId = productInfoView.dataInfo.Id;
                                        mediaDataRel.ProId      = pro.ProId;
                                        mediaDataRel.DeptId     = dep.DeptId;
                                        mediaDataRel.BuName     = buName;
                                        mediaDataRel.CreateTime = DateTime.Now;
                                        mediaDataRel.CreateUser = workUser.User.Id;


                                        _rep.Insert(mediaDataRel);
                                    }
                                }
                            }
                            _rep.SaveChanges();
                        }
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        rvm.Msg     = ex.Message;
                        rvm.Success = false;
                        return(rvm);
                    }
                }
            }
            else
            {
                var dataTitle = _rep.FirstOrDefault <HcpDataInfo>(s => s.Title == productInfoView.dataInfo.Title && s.IsDeleted == 0 && s.Id != productInfoView.dataInfo.Id);
                if (dataTitle != null)
                {
                    rvm.Msg     = "fail";
                    rvm.Success = false;
                    rvm.Msg     = $"已存在【{productInfoView.dataInfo.Title}】的资料";
                    return(rvm);
                }

                using (var tran = _rep.Database.BeginTransaction())
                {
                    try
                    {
                        data.ProductTypeInfoId = "";
                        data.Title             = productInfoView.dataInfo.Title;
                        data.DataContent       = productInfoView.dataInfo.DataContent;
                        data.CompanyCode       = productInfoView.dataInfo.CompanyCode;
                        data.DataOrigin        = productInfoView.dataInfo.DataOrigin;
                        data.DataLink          = productInfoView.dataInfo.DataLink;
                        data.DataType          = productInfoView.dataInfo.DataType;
                        data.DataUrl           = productInfoView.dataInfo.DataUrl;
                        data.IsRead            = productInfoView.dataInfo.IsRead;
                        data.IsSelected        = productInfoView.dataInfo.IsSelected;
                        data.MediaTime         = productInfoView.dataInfo.MediaTime;
                        data.BuName            = productInfoView.dataInfo.BuName;
                        data.Dept         = productInfoView.dataInfo.Dept;
                        data.Product      = productInfoView.dataInfo.Product;
                        data.Sort         = productInfoView.dataInfo.Sort;
                        data.IsCopyRight  = productInfoView.dataInfo.IsCopyRight;
                        data.IsChoiceness = productInfoView.dataInfo.IsChoiceness;
                        //data.ClickVolume = productInfoView.dataInfo.ClickVolume;
                        data.Sort          = productInfoView.dataInfo.Sort;
                        data.UpdateTime    = DateTime.Now;
                        data.UpdateUser    = workUser.User.Id;
                        data.KnowImageUrl  = productInfoView.dataInfo.KnowImageUrl;
                        data.KnowImageName = productInfoView.dataInfo.KnowImageName;
                        data.Remark        = productInfoView.dataInfo.Remark;
                        data.IsDownload    = productInfoView.dataInfo.IsDownload;
                        _rep.Update(data);
                        _rep.SaveChanges();

                        if (productInfoView.ProductAndDeps != null &&
                            productInfoView.ProductAndDeps.CatalogueNameList != null &&
                            productInfoView.ProductAndDeps.BuNameList != null)
                        {
                            var hcpDataCatalogueRelList = _rep.Where <HcpDataCatalogueRel>(s => s.HcpDataInfoId == productInfoView.dataInfo.Id);
                            foreach (var media in hcpDataCatalogueRelList)
                            {
                                _rep.Delete(media);
                            }

                            HcpDataCatalogueRel hcpDataCatalogueRel;
                            foreach (var buName in productInfoView.ProductAndDeps.BuNameList)
                            {
                                foreach (var catalogue in productInfoView.ProductAndDeps.CatalogueNameList)
                                {
                                    var hcpCatalogueManage = _rep.FirstOrDefault <HcpCatalogueManage>(s => s.CatalogueName == catalogue && s.BuName == buName && s.IsDeleted == 0);
                                    if (hcpCatalogueManage == null)
                                    {
                                        rvm.Msg     = "fail";
                                        rvm.Success = false;
                                        rvm.Result  = $"获取目录信息失败";
                                        return(rvm);
                                    }
                                    hcpDataCatalogueRel    = new HcpDataCatalogueRel();
                                    hcpDataCatalogueRel.Id = Guid.NewGuid().ToString();
                                    hcpDataCatalogueRel.HcpCatalogueManageId   = hcpCatalogueManage.Id;
                                    hcpDataCatalogueRel.HcpCatalogueManageName = hcpCatalogueManage.CatalogueName;
                                    hcpDataCatalogueRel.HcpDataInfoId          = productInfoView.dataInfo.Id;
                                    hcpDataCatalogueRel.CreateTime             = DateTime.Now;
                                    hcpDataCatalogueRel.CreateUser             = workUser.User.Id;
                                    _rep.Insert(hcpDataCatalogueRel);
                                }
                            }
                        }

                        if (productInfoView.ProductAndDeps != null &&
                            productInfoView.ProductAndDeps.BuNameList != null &&
                            productInfoView.ProductAndDeps.Products != null &&
                            productInfoView.ProductAndDeps.Departments != null)
                        {
                            var mediaRelList = _rep.Where <HcpMediaDataRel>(s => s.DataInfoId == productInfoView.dataInfo.Id);

                            foreach (var media in mediaRelList)
                            {
                                _rep.Delete(media);
                            }
                            HcpMediaDataRel mediaDataRel;
                            foreach (var buName in productInfoView.ProductAndDeps.BuNameList)
                            {
                                foreach (var pro in productInfoView.ProductAndDeps.Products)
                                {
                                    foreach (var dep in productInfoView.ProductAndDeps.Departments)
                                    {
                                        mediaDataRel            = new HcpMediaDataRel();
                                        mediaDataRel.Id         = Guid.NewGuid().ToString();
                                        mediaDataRel.DataInfoId = productInfoView.dataInfo.Id;
                                        mediaDataRel.ProId      = pro.ProId;
                                        mediaDataRel.DeptId     = dep.DeptId;
                                        mediaDataRel.BuName     = buName;
                                        mediaDataRel.CreateTime = DateTime.Now;
                                        mediaDataRel.CreateUser = workUser.User.Id;

                                        _rep.Insert(mediaDataRel);
                                    }
                                }
                            }
                            _rep.SaveChanges();
                        }

                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        rvm.Msg     = "fail";
                        rvm.Success = false;
                        return(rvm);
                    }
                }
            }

            //新增或变更 邮件发送
            if (workUser != null && IsSendMail == "1")
            {
                LoggerHelper.WarnInTimeTest("[产品资料 SendMeetMail开始]:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));

                MailUtil.SendMeetMail(workUser?.User.ChineseName ?? "", "产品资料", productInfoView.dataInfo?.Title ?? "")
                .ContinueWith((previousTask) =>
                {
                    bool rCount = previousTask.Result;
                });
                //MailUtil.SendMeetMail(workUser?.User.ChineseName ?? "", "产品资料", productInfoView.dataInfo?.Title ?? "");
                LoggerHelper.WarnInTimeTest("[产品资料 SendMeetMail结束]:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
            }

            rvm.Msg     = "success";
            rvm.Success = true;
            rvm.Result  = data ?? productInfoView.dataInfo;

            return(rvm);
        }
        public IHttpActionResult AddOrUpdateProductInfo(HcpDataInfoInputDto productInfoView)
        {
            var ret = _hcpDataInfoService.AddOrUpdateProductInfo(productInfoView, WorkUser);

            return(Ok(ret));
        }