Ejemplo n.º 1
0
        public IHttpActionResult AddBusinessCard(AddBusinessCardViewModel businessCard)
        {
            var ret = _wxBusinessCardService.AddBusinessCard(businessCard, WorkUser);

            return(Ok(ret));
        }
        public ReturnValueModel AddBusinessCard(AddBusinessCardViewModel viewModel, WorkUser workUser)
        {
            ReturnValueModel rvm = new ReturnValueModel();

            if (string.IsNullOrEmpty(viewModel?.OwnerWxUserId)) //string.IsNullOrEmpty(viewModel.ObjectUnionId) &&
            {
                rvm.Success = false;
                rvm.Msg     = "The parameter 'OwnerWxUserId' is required.";
                return(rvm);
            }

            //名片上是谁的信息,谁就是cardOwner
            var cardOwner = _rep.FirstOrDefault <WxUserModel>(s => s.IsDeleted != 1 &&
                                                              (!string.IsNullOrEmpty(s.Id) && s.Id == viewModel.OwnerWxUserId)); //|| (!string.IsNullOrEmpty(s.UnionId) && s.UnionId == viewModel.ObjectUnionId)

            if (cardOwner == null)
            {
                rvm.Success = false;
                rvm.Msg     = "Target user not found.";
                return(rvm);
            }

            if (cardOwner.Id == workUser.WxUser.Id)
            {
                rvm.Success = false;
                rvm.Msg     = "不能添加自己的名片";
                return(rvm);
            }

            //自己名片夹的名片
            var card1 = _rep.FirstOrDefault <BusinessCard>(s => s.IsDeleted != 1 && s.WxUserId == workUser.WxUser.Id && s.OwnerWxUserId == cardOwner.Id);

            if (card1 != null)
            {
                rvm.Msg     = "名片已存在";
                rvm.Success = false;
                return(rvm);
            }

            //对方名片夹的名片
            var card2 = _rep.FirstOrDefault <BusinessCard>(s => s.IsDeleted != 1 && s.OwnerWxUserId == workUser.WxUser.Id && s.WxUserId == cardOwner.Id);

            using (var tran = _rep.Database.BeginTransaction())
            {
                try
                {
                    //插入本人名片夹
                    card1               = new BusinessCard();
                    card1.Id            = Guid.NewGuid().ToString();
                    card1.WxUserId      = workUser.WxUser.Id;
                    card1.OwnerWxUserId = cardOwner.Id;
                    card1.CreateTime    = DateTime.Now;
                    card1.IsDeleted     = 0;
                    card1.IsEnabled     = 0;
                    _rep.Insert(card1);
                    _rep.SaveChanges();

                    //插入对方名片夹
                    if (card2 == null)
                    {
                        card2               = new BusinessCard();
                        card2.Id            = Guid.NewGuid().ToString();
                        card2.WxUserId      = card1.OwnerWxUserId;
                        card2.OwnerWxUserId = card1.WxUserId;
                        card2.CreateTime    = DateTime.Now;
                        card2.IsDeleted     = 0;
                        card2.IsEnabled     = 0;
                        _rep.Insert(card2);
                        _rep.SaveChanges();
                    }

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

            rvm.Success = true;
            rvm.Msg     = "";
            return(rvm);
        }