forked from xuxiaowei007/FoxOne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntityController.cs
179 lines (174 loc) · 6.73 KB
/
EntityController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using FoxOne.Controls;
using FoxOne.Data;
using FoxOne.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Text;
using System.Globalization;
using FoxOne.Business.Environment;
using System.Transactions;
using FoxOne.Business;
namespace FoxOne.Web.Controllers
{
public class EntityController : BaseController
{
[HttpPost]
[ValidateInput(false)]
public JsonResult Edit()
{
IDictionary<string, object> data = Request.Form.ToDictionary();
string key = Request.Form[NamingCenter.PARAM_KEY_NAME];
string pageId = Request.QueryString[NamingCenter.PARAM_PAGE_ID];
string ctrlId = Request.QueryString[NamingCenter.PARAM_CTRL_ID];
string formViewMode = Request.Form[NamingCenter.PARAM_FORM_VIEW_MODE];
var page = PageBuilder.BuildPage(pageId);
if (page == null)
{
throw new FoxOneException("Page_Not_Found");
}
var form = page.FindControl(ctrlId) as Form;
if (form == null)
{
throw new FoxOneException("Ctrl_Not_Found");
}
var ds = form.FormService as IFormService;
int effectCount = 0;
if (formViewMode.Equals(FormMode.Edit.ToString(), StringComparison.OrdinalIgnoreCase))
{
effectCount = ds.Update(key, data);
}
else
{
effectCount = ds.Insert(data);
}
return Json(effectCount > 0);
}
[HttpPost]
public JsonResult Delete()
{
string key = Request.Form[NamingCenter.PARAM_KEY_NAME];
string pageId = Request.Form[NamingCenter.PARAM_PAGE_ID];
string ctrlId = Request.Form[NamingCenter.PARAM_CTRL_ID];
var page = PageBuilder.BuildPage(pageId);
if (page == null)
{
throw new FoxOneException("Page_Not_Found");
}
var table = page.FindControl(ctrlId) as Table;
if (table == null)
{
throw new FoxOneException("Ctrl_Not_Found");
}
var ds = table.DataSource as IFormService;
if (key.IndexOf(",") > 0)
{
var keys = key.Split(',');
int i = 0;
foreach (var k in keys)
{
i += ds.Delete(k);
}
return Json(i == keys.Length);
}
return Json(ds.Delete(key) > 0);
}
[HttpPost]
public JsonResult GenerateCRUD()
{
string on = "on";
string IsCRUD = Request.Form["IsCRUD"];
string CRUDName = Request.Form["CRUDName"];
string IsList = Request.Form["IsList"];
string ListName = Request.Form["ListName"];
string IsEdit = Request.Form["IsEdit"];
string EditName = Request.Form["EditName"];
string tableName = Request.Form["TableName"];
string pageTitle = Request.Form["PageTitle"];
var pageGenerator = new PageGenerator()
{
CRUDName = CRUDName,
EditPageName = EditName,
ListPageName = ListName,
TableName = tableName,
PageTitle = pageTitle
};
pageGenerator.AddCRUD();
if (IsList == on)
{
pageGenerator.AddListPage();
}
if (IsEdit == on)
{
pageGenerator.AddEditPage();
}
return Json(true);
}
public FileResult ExportToExcel()
{
string ctrlId = Request[NamingCenter.PARAM_CTRL_ID];
string pageId = Request[NamingCenter.PARAM_PAGE_ID];
var Table = PageBuilder.BuildPage(pageId).FindControl(ctrlId) as Table;
string templateName = Request.QueryString["TemplateFile"];
int ingoreRow = Request.QueryString["ingoreRow"].ConvertTo<int>();
string fileName = string.Format("{0}-{1}.xls", Table.Id, DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"));
if (!Request.QueryString["fileName"].IsNullOrEmpty())
{
fileName = Request.QueryString["fileName"];
}
if (!fileName.EndsWith(".xls", StringComparison.OrdinalIgnoreCase))
{
fileName = "{0}.xls".FormatTo(fileName);
}
int rowSpanColumnIndex = 0;
if (!Request.QueryString["megerColumn"].IsNullOrEmpty())
{
rowSpanColumnIndex = Request.QueryString["megerColumn"].ConvertTo<int>();
}
int freezeColumn = 0;
if (!Request.QueryString["freezeColumn"].IsNullOrEmpty())
{
freezeColumn = Request.QueryString["freezeColumn"].ConvertTo<int>();
}
string templatePath = templateName.IsNullOrEmpty() ? string.Empty : Server.MapPath("~/App_Config/ExcelTemplate/{0}".FormatTo(templateName));
var workbook = new ExcelHelper().ExportToExcel(Table, freezeColumn, rowSpanColumnIndex, templatePath, ingoreRow);
using (MemoryStream ms = new MemoryStream())
{
workbook.Write(ms);
return File(ms.GetBuffer(), "application/vnd.ms-excel", fileName);
}
}
public JsonResult UserRole(string UserId, string RoleId, bool Add)
{
bool result = false;
if(UserId.IsNullOrEmpty() || RoleId.IsNullOrEmpty())
{
throw new FoxOneException("UnValid UserId Or RoleId");
}
if (Add)
{
result = DBContext<UserRole>.Insert(new UserRole()
{
Id = Guid.NewGuid().ToString(),
RentId = 1,
RoleId = RoleId,
Status = DefaultStatus.Enabled.ToString(),
UserId = UserId
});
}
else
{
var entity = DBContext<UserRole>.Instance.FirstOrDefault(o => o.UserId.Equals(UserId, StringComparison.OrdinalIgnoreCase) && o.RoleId.Equals(RoleId, StringComparison.OrdinalIgnoreCase));
if(entity!=null)
{
result = DBContext<UserRole>.Delete(entity);
}
}
return Json(result, JsonRequestBehavior.AllowGet);
}
}
}