Ejemplo n.º 1
0
        private void SaveCodeTableToDb(CodeTableViewModel viewModel)
        {
            if (!string.IsNullOrEmpty(viewModel.TableName))
            {
                viewModel.TableName = viewModel.TableName.Trim();
            }
            if (!string.IsNullOrEmpty(viewModel.ClassName))
            {
                viewModel.ClassName = viewModel.ClassName.Trim();
            }
            base.Check(string.IsNullOrEmpty(viewModel.TableName) || string.IsNullOrEmpty(viewModel.ClassName), "表名或者实体类名必须填一个");
            viewModel.ColumnInfoList = viewModel.ColumnInfoList
                                       .Where(it => !string.IsNullOrEmpty(it.ClassProperName) || !string.IsNullOrEmpty(it.DbColumnName)).ToList();
            base.Check(viewModel.ColumnInfoList.Count == 0, "请配置实体属性");
            var dbTable = mapper.Map <CodeTable>(viewModel);

            AutoFillTable(dbTable);
            var dbColumns = mapper.Map <List <CodeColumns> >(viewModel.ColumnInfoList);

            AutoFillColumns(dbColumns);
            if (viewModel.Id == null || viewModel.Id == 0)
            {
                CheckAddName(viewModel, CodeTableDb);
                var id = CodeTableDb.InsertReturnIdentity(dbTable);
                foreach (var item in dbColumns)
                {
                    item.CodeTableId  = id;
                    item.DbColumnName = GetColumnName(item.DbColumnName);
                }
                CodeColumnsDb.InsertRange(dbColumns);
            }
            else
            {
                CheckUpdateName(viewModel, CodeTableDb);
                CodeTableDb.Update(dbTable);
                foreach (var item in dbColumns)
                {
                    item.CodeTableId  = dbTable.Id;
                    item.DbColumnName = GetColumnName(item.DbColumnName);
                }

                var oldIds = CodeColumnsDb.GetList(it => it.CodeTableId == dbTable.Id).Select(it => it.Id).ToList();
                var delIds = oldIds.Where(it => !dbColumns.Select(y => y.Id).Contains(it)).ToList();
                CodeColumnsDb.DeleteByIds(delIds.Select(it => (object)it).ToArray());

                var updateColumns = dbColumns.Where(it => it.Id > 0).ToList();
                if (updateColumns.Count > 0)
                {
                    CodeColumnsDb.UpdateRange(updateColumns);
                }

                var insertColumns = dbColumns.Where(it => it.Id == 0).ToList();
                if (insertColumns.Count > 0)
                {
                    CodeColumnsDb.InsertRange(insertColumns);
                }
            }
        }
Ejemplo n.º 2
0
        public ActionResult <ApiResult <string> > SaveCommField([FromForm] string Field, [FromForm] string model, [FromForm] int?dbid)
        {
            ApiResult <string> result = new ApiResult <string>();
            var fields                    = (Field + "").Split(',').Where(it => !string.IsNullOrEmpty(it)).ToList();
            var fieldInfoList             = CommonFieldDb.GetList(it => fields.Contains(it.Id.ToString()));
            var list                      = Newtonsoft.Json.JsonConvert.DeserializeObject <List <CodeTableGridViewModel> >(model);
            List <CodeColumns> addcolumns = new List <CodeColumns>();

            foreach (var item in list)
            {
                var columns = CodeColumnsDb.GetList(it => it.CodeTableId == item.Id);

                foreach (var filedItem in fieldInfoList)
                {
                    if (!columns.Any(y => y.DbColumnName.ToLower() == filedItem.DbColumnName.ToLower()) && !columns.Any(y => y.ClassProperName.ToLower() == filedItem.ClassProperName.ToLower()))
                    {
                        if (!addcolumns.Any(it => it.DbColumnName == filedItem.DbColumnName && it.CodeTableId == Convert.ToInt32(item.Id)))
                        {
                            addcolumns.Add(new CodeColumns()
                            {
                                CodeTableId     = Convert.ToInt32(item.Id),
                                DbColumnName    = filedItem.DbColumnName,
                                ClassProperName = filedItem.ClassProperName,
                                CodeType        = CodeTypeDb.GetById(filedItem.CodeType).Name,
                                IsIdentity      = filedItem.IsIdentity,
                                IsPrimaryKey    = filedItem.IsPrimaryKey,
                                Required        = filedItem.Required,
                                Description     = filedItem.Description
                            });
                        }
                    }
                }
            }
            CodeColumnsDb.InsertRange(addcolumns);
            result.IsSuccess = true;
            result.Data      = "追加成功";
            return(result);
        }
Ejemplo n.º 3
0
        private void UpdateEntityItem(int dbid, SqlSugarClient tableDb, List <CodeTableViewModel> list)
        {
            var oldList     = CodeTableDb.AsQueryable().In(list.Select(it => it.Id).ToList()).ToList();
            var oldColumns  = Db.Queryable <CodeColumns, CodeTable>((c, t) => c.CodeTableId == t.Id).Where((c, t) => oldList.Select(it => it.Id).Contains(t.Id)).Select((c, t) => new { TableId = t.Id, TableName = t.TableName, Columns = c }).ToList();
            var alltables   = tableDb.DbMaintenance.GetTableInfoList(false).Select(it => it.Name.ToLower()).ToList();
            var ids         = list.Select(it => it.Id).ToList();
            var tableNames  = list.Select(it => it.TableName.ToLower()).ToList();
            var errorTables = list.Where(it => !alltables.Contains(it.TableName.ToLower()) && !alltables.Contains(it.ClassName.ToLower())).ToList();

            base.Check(errorTables.Any(), string.Join(",", errorTables.Select(y => y.TableName ?? y.ClassName)) + "未创建表不能同步");
            try
            {
                Db.BeginTran();
                CodeTableDb.DeleteByIds(ids.Select(it => (object)it).ToArray());
                var dbTableGridList = tableDb.DbMaintenance.GetTableInfoList(false).Where(it => tableNames.Contains(it.Name.ToLower())).Select(it => new DbTableGridViewModel()
                {
                    Description = it.Description,
                    Name        = it.Name
                });
                if (dbTableGridList.Any())
                {
                    SaveCodetableImport(dbid, Newtonsoft.Json.JsonConvert.SerializeObject(dbTableGridList));
                }
                foreach (var item in oldList)
                {
                    CodeTableDb.AsUpdateable(item).UpdateColumns(it => it.ClassName).WhereColumns(it => it.TableName).ExecuteCommand();
                }
                List <CodeColumns> UpdateColumns = new List <CodeColumns>();
                foreach (var item in oldColumns.GroupBy(it => new { it.TableId, it.TableName }).ToList())
                {
                    var tableId = CodeTableDb.AsQueryable().Where(it => it.TableName == item.Key.TableName && it.DbId == dbid).First()?.Id;
                    if (tableId != null)
                    {
                        var columns = CodeColumnsDb.AsQueryable().Where(it => it.CodeTableId == tableId).ToList();
                        foreach (var col in columns)
                        {
                            var addColumn = item.FirstOrDefault(it => it.Columns.DbColumnName == col.DbColumnName);
                            if (addColumn != null)
                            {
                                col.ClassProperName = addColumn.Columns.ClassProperName;
                                UpdateColumns.Add(col);
                            }
                            else
                            {
                            }
                        }
                        foreach (var oldItem in item.ToList())
                        {
                            if (oldItem.Columns.CodeType.Equals("ignore", StringComparison.CurrentCultureIgnoreCase))
                            {
                                var mapp = _mapper.Map <CodeColumns>(oldItem.Columns);
                                mapp.CodeTableId = columns[0].CodeTableId;
                                CodeColumnsDb.AsInsertable(mapp).ExecuteCommand();
                            }
                        }
                    }
                }
                CodeColumnsDb.AsUpdateable(UpdateColumns).UpdateColumns(it => it.ClassProperName).ExecuteCommand();
                Db.CommitTran();
            }
            catch (Exception ex)
            {
                Db.RollbackTran();
                throw ex;
            }
        }