/// <summary>
        /// 添加内容信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public long AddAndUpdateContentInfo(CostContentModel model)
        {
            if (model == null)
            {
                return(0);
            }
            if (model.SpendType == 2)
            {
                //验证
                if (model.Id > 0)
                {
                    var linkModel = GetContentModel(model.LinkCostId);
                    if (linkModel == null || linkModel.LinkCostId != model.Id)
                    {
                        if (Debugger.IsAttached)
                        {
                            Trace.WriteLine("关联入账信息不匹配,更新失败");
                        }
                        return(0);
                    }
                }
                var inModel = new CostContentModel
                {
                    Cost                = model.Cost,
                    CostAddress         = model.CostAddress,
                    SpendType           = model.SpendType,
                    Id                  = model.LinkCostId,
                    CostTime            = model.CostTime,
                    CostInOrOut         = CostInOrOutEnum.In,
                    CostChannel         = model.LinkCostChannel,
                    CostChannelNo       = model.LinkCostChannelNo,
                    CostChannelName     = model.LinkCostChannelName,
                    LinkCostChannel     = model.CostChannel,
                    LinkCostChannelNo   = model.CostChannelNo,
                    LinkCostChannelName = model.CostChannelName,
                    UserId              = model.UserId,
                    CostType            = model.CostType,
                    LinkCostId          = model.Id,
                    CostThing           = model.CostThing,
                    CreateTime          = model.CreateTime,
                    CostMonth           = model.CostMonth,
                    UpdateTime          = model.UpdateTime,
                    CostYear            = model.CostYear,
                    UpdateUserId        = model.UpdateUserId,
                    CostTypeName        = model.CostTypeName,
                    CreateUserId        = model.CreateUserId,
                    IsDel               = model.IsDel
                };
                return(_dataAccess.SaveLinkCostModel(model, inModel));
            }

            return(_dataAccess.SaveModel(model));
        }
        /// <summary>
        /// 更新两个对象,通过事务
        /// </summary>
        /// <param name="modelItem1"></param>
        /// <param name="modelItem2"></param>
        /// <returns></returns>
        public long UpdateLinkCostContentInfo(CostContentModel modelItem1, CostContentModel modelItem2)
        {
            switch (modelItem1)
            {
            case null when modelItem2 == null:
                return(0);

            case null when modelItem2.Id > 0:
                return(_dataAccess.SaveModel(modelItem2));
            }

            if (modelItem2 == null && modelItem1.Id > 0)
            {
                return(_dataAccess.SaveModel(modelItem1));
            }
            return(_dataAccess.SaveLinkCostModel(modelItem1, modelItem2));
        }
        public ActionResult AddCostInfo(CostContentModel model)
        {
            var resultMode = new ResponseBaseModel <dynamic>
            {
                ResultCode = ResponceCodeEnum.Fail,
            };

            if (model == null)
            {
                resultMode.Message = "参数错误";
                return(Json(resultMode, JsonRequestBehavior.AllowGet));
            }

            var userId = CurrentModel.UserId;
            var server = new CostContentService();
            CostContentModel newModel = new CostContentModel();

            if (model.Id > 0)
            {
                newModel = server.GetContentModel(model.Id);
                //验证权限
                if (newModel == null || newModel.UserId != userId)
                {
                    resultMode.Message = "非法访问";
                    return(Json(resultMode, JsonRequestBehavior.AllowGet));
                }

                if (newModel.SpendType == 2 && (model.SpendType != 2 || model.LinkCostId < 0))
                {
                    resultMode.Message = "转移记录类型不能修改或无关联入账信息";
                    return(Json(resultMode, JsonRequestBehavior.AllowGet));
                }

                if (newModel.SpendType == 2 && newModel.CostInOrOut == CostInOrOutEnum.In)
                {
                    resultMode.Message = "转移记录入账信息不能修改";
                    return(Json(resultMode, JsonRequestBehavior.AllowGet));
                }
            }
            //验证参数
            newModel.Cost            = Math.Round(Math.Abs(model.Cost), 2);
            newModel.CostAddress     = model.CostAddress;
            newModel.CostChannel     = model.CostChannel;
            newModel.CostType        = model.SpendType == 2 ? -1 : model.CostType;
            newModel.UserId          = userId;
            newModel.CostInOrOut     = model.CostInOrOut;
            newModel.CostThing       = model.CostThing;
            newModel.CostTime        = model.CostTime;
            newModel.CostMonth       = newModel.CostTime.Month;
            newModel.CostYear        = newModel.CostTime.Year;
            newModel.CreateTime      = newModel.Id > 0 ? newModel.CreateTime : DateTime.Now;
            newModel.CreateUserId    = userId;
            newModel.SpendType       = model.SpendType;
            newModel.UpdateTime      = DateTime.Now;
            newModel.UpdateUserId    = userId;
            newModel.LinkCostChannel = model.LinkCostChannel;
            newModel.IsDel           = FlagEnum.HadZore;
            if (newModel.Cost < (decimal)0.01)
            {
                resultMode.Message = "金额设置错误";
                return(Json(resultMode, JsonRequestBehavior.AllowGet));
            }

            if (model.SpendType != 2)
            {
                var costTypeInfo = new CostTypeService().Get(newModel.CostType);
                if (costTypeInfo == null ||
                    costTypeInfo.UserId != userId ||
                    costTypeInfo.IsDel == FlagEnum.HadOne ||
                    costTypeInfo.IsValid == FlagEnum.HadZore)
                {
                    resultMode.Message = "类型选择无效";
                    return(Json(resultMode, JsonRequestBehavior.AllowGet));
                }
                newModel.CostTypeName = costTypeInfo.Name;
            }

            var costChannelServer = new CostChannelService();
            var costChannelInfo   = costChannelServer.Get(newModel.CostChannel);

            if (costChannelInfo == null ||
                costChannelInfo.UserId != userId ||
                costChannelInfo.IsDel == FlagEnum.HadOne ||
                costChannelInfo.IsValid == FlagEnum.HadZore)
            {
                resultMode.Message = "账户选择无效";
                return(Json(resultMode, JsonRequestBehavior.AllowGet));
            }
            newModel.CostChannelName = costChannelInfo.CostChannelName;
            newModel.CostChannelNo   = costChannelInfo.CostChannelNo;
            newModel.CostInOrOut     = newModel.SpendType == 1 ? CostInOrOutEnum.In : CostInOrOutEnum.Out;

            if (newModel.SpendType == 2)
            {
                if (newModel.LinkCostChannel < 1)
                {
                    resultMode.Message = "转入账户无效";
                    return(Json(resultMode, JsonRequestBehavior.AllowGet));
                }
                var linkChannelInfo = costChannelServer.Get(newModel.LinkCostChannel);
                if (linkChannelInfo == null ||
                    linkChannelInfo.UserId != userId ||
                    linkChannelInfo.IsDel == FlagEnum.HadOne ||
                    linkChannelInfo.IsValid == FlagEnum.HadZore)
                {
                    resultMode.Message = "转入账户无效";
                    return(Json(resultMode, JsonRequestBehavior.AllowGet));
                }

                newModel.LinkCostChannelName = linkChannelInfo.CostChannelName;
                newModel.LinkCostChannelNo   = linkChannelInfo.CostChannelNo;
            }
            else
            {
                newModel.LinkCostChannel     = 0;
                newModel.LinkCostId          = 0;
                newModel.LinkCostChannelNo   = "";
                newModel.LinkCostChannelName = "";
            }
            var costContentServer = new CostContentService();

            costContentServer.AddAndUpdateContentInfo(newModel);
            resultMode.ResultCode = ResponceCodeEnum.Success;
            return(Json(resultMode, JsonRequestBehavior.AllowGet));
        }