예제 #1
0
        /// <summary>
        /// 修改一条数据
        /// </summary>
        /// <param name="parm">T</param>
        /// <returns></returns>
        public async Task <ApiResult <string> > UpdateAsync(CmsColumn parm)
        {
            var res = new ApiResult <string>()
            {
                statusCode = (int)ApiEnum.Error
            };

            try
            {
                //先查出原来的
                var sourceModel = CmsColumnDb.GetSingle(m => m.Id == parm.Id);
                //根据模板ID查询相关内容
                var mbModel = CmsTemplateDb.GetSingle(m => m.Id == parm.TempId);
                if (parm.TempId != sourceModel.TempId && mbModel != null)
                {
                    parm.TempName = mbModel.Title;
                    parm.TempUrl  = mbModel.Url;
                }
                else
                {
                    parm.TempName = sourceModel.TempName;
                    parm.TempUrl  = sourceModel.TempUrl;
                }
                if (sourceModel.ParentId != parm.ParentId)
                {
                    //不相等更改等级
                    var parModel = CmsColumnDb.GetSingle(m => m.Id == parm.ParentId);
                    if (parModel != null)
                    {
                        parm.ClassList  = parModel.ClassList + parm.Id + ",";
                        parm.ClassLayer = parModel.ClassLayer + 1;
                    }
                }
                else
                {
                    parm.ClassList  = sourceModel.ClassList;
                    parm.ClassLayer = sourceModel.ClassLayer;
                }
                await Db.Updateable(parm).IgnoreColumns(m => new { m.Number, m.Sort, }).ExecuteCommandAsync();

                res.statusCode = (int)ApiEnum.Status;
            }
            catch (Exception ex)
            {
                res.message = ApiEnum.Error.GetEnumText() + ex.Message;
            }
            return(res);
        }
예제 #2
0
        /// <summary>
        /// 添加一条数据
        /// </summary>
        /// <param name="parm">T</param>
        /// <returns></returns>
        public new async Task <ApiResult <string> > AddAsync(CmsColumn parm)
        {
            var res = new ApiResult <string>()
            {
                statusCode = (int)ApiEnum.Error
            };

            try
            {
                parm.Number = Utils.Number(10);
                //根据模板ID查询相关内容
                var mbModel = CmsTemplateDb.GetSingle(m => m.Id == parm.TempId);
                if (mbModel != null)
                {
                    parm.TempName = mbModel.Title;
                    parm.TempUrl  = mbModel.Url;
                }
                //如果描述不写,直接读取内容
                if (!string.IsNullOrEmpty(parm.Summary))
                {
                    parm.Summary = Utils.CutString(parm.Content, 160);
                }
                //生成排序数字
                var sorts = Db.Queryable <CmsColumn>().OrderBy(m => m.Sort, SqlSugar.OrderByType.Desc).Take(1).First();
                if (sorts != null)
                {
                    parm.Sort = sorts.Sort + 1;
                }
                else
                {
                    parm.Sort = 1;
                }
                try
                {
                    var result = Db.Ado.UseTran(() =>
                    {
                        var addId = Db.Insertable(parm).ExecuteReturnIdentity();
                        if (parm.ParentId > 0)
                        {
                            //说明有父级  根据父级,查询对应的模型
                            var parModel = Db.Queryable <CmsColumn>().Single(m => m.Id == parm.ParentId);
                            if (parModel != null)
                            {
                                parm.ClassList  = parModel.ClassList + addId + ",";
                                parm.ClassLayer = parModel.ClassLayer + 1;
                                parm.Id         = addId;
                            }
                        }
                        else
                        {
                            //没有父级
                            parm.ClassList = "," + addId + ",";
                        }
                        Db.Updateable(parm).ExecuteCommand();
                    });
                    if (result.IsSuccess)
                    {
                        res.statusCode = (int)ApiEnum.Status;
                    }
                    else
                    {
                        res.message = result.ErrorMessage;
                    }
                }
                catch (Exception ex)
                {
                    res.message = ex.Message;
                }
            }
            catch (Exception ex)
            {
                res.message = ApiEnum.Error.GetEnumText() + ex.Message;
            }
            return(await Task.Run(() => res));
        }