You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

340 lines
18 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/////////////////////////////////////////////////////////////////
//
// (C) Copyright 2013, Kenneth, Inc.
// All rights reserved. Confidential. Except as pursuant
// to a written agreement with Kenneth, this software may
// not be used or distributed. This software may be covered
// by one or more patents.
//
// 本软件为Kenneth开发版权所有违者必究32032519810221811023810511@qq.com
//
/////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaseEntity;
using Newtonsoft.Json;
using BaseResource;
using Newtonsoft.Json.Linq;
using BaseCommon;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.IO;
using System.Reflection;
using System.Data;
using System.Data.SqlClient;
using System.Transactions;
namespace WebBusiness
{
public partial class {ClassName}
{
#region private const functions
private static object lockobj = new object();
private const string _onlyTableName="{TableName}";
private const string _fields = "{AllFields}";
private const string _tableName = "{TableName} t";
private const string _log_fields = "t.LogId,{AllFields},t.OperTime,t.OperUser,t.OperType,t.OperInfo";
private const string _log_tableName = "{TableName}_log t";
private static string QueryFromWhere(List<ConditionEntity> ces, out List<SqlParameter> parameters)
{
parameters = new List<SqlParameter>();
string fromwhere = string.Empty;
foreach (var ce in ces)
{
if (!string.IsNullOrWhiteSpace(ce.Expression))
{
parameters.Add(AppHelper.GetConditionValue(ce));
fromwhere = fromwhere + string.Format(" {0} {1} ", string.IsNullOrWhiteSpace(ce.Symbol) ? "and" : ce.Symbol, ce.Expression);
}
else
{
if (!string.IsNullOrWhiteSpace(ce.Value))
{
parameters.Add(AppHelper.GetConditionValue(ce));
string field = ce.Fields;
if (field.Contains(","))
{
string[] fields = field.Split(',');
string conditions = string.Empty;
string tag = " or";
foreach (var f in fields)
{
conditions = conditions + string.Format("{0} t.{1} {2} @{3} ", tag, f, string.IsNullOrWhiteSpace(ce.Fuzzy) ? "=" : ce.Fuzzy, ce.Name);
}
conditions = conditions.TrimStart(tag.ToCharArray());
fromwhere = fromwhere + string.Format(" {0} ({1}) ", string.IsNullOrWhiteSpace(ce.Symbol) ? "and" : ce.Symbol, conditions);
}
else
{
fromwhere = fromwhere + string.Format(" {0} t.{1} {2} @{3} ", string.IsNullOrWhiteSpace(ce.Symbol) ? "and" : ce.Symbol,
field, string.IsNullOrWhiteSpace(ce.Fuzzy) ? "=" : ce.Fuzzy, ce.Name);
}
}
}
}
fromwhere = fromwhere + " and t.IsActive > 0 ";
return fromwhere;
}
private static void InsertToLog(SqlConnection connection, string recid, string currentUserId, string opertype)
{
SqlHelper.ExecuteNonQuery(connection, "insert into {TableName}_log (" + _log_fields.Replace("t.", string.Empty) + ") " +
"(select '"+Guid.NewGuid().ToString()+"'," + _fields.Replace("t.", string.Empty) + ",getdate() as OperTime,'" + currentUserId + "' as OperUser,'" + opertype + "' as OperType,'' as OperInfo from {TableName} where RecId ='" + recid + "')");
}
private static {ClassName}Entity LoadResult(SqlConnection connection, string recid)
{
SqlParameter paramRecId = new SqlParameter("@RecId", SqlDbType.NVarChar) { Value = recid };
DataTable dt = SqlHelper.ExecuteDataTable(connection, "select top 1 * from {TableName} t where t.RecId = @RecId and t.IsActive > 0", paramRecId);
{ClassName}Entity data = SqlHelper.DataRow0ToEntity<{ClassName}Entity>(dt) as {ClassName}Entity;
return data;
}
private static {ClassName}Entity LoadResult(string recid)
{
SqlParameter paramRecId = new SqlParameter("@RecId", SqlDbType.NVarChar) { Value = recid };
DataTable dt = SqlHelper.ExecuteDataTable("select top 1 * from {TableName} t where t.RecId = @RecId and t.IsActive > 0", paramRecId);
{ClassName}Entity data = SqlHelper.DataRow0ToEntity<{ClassName}Entity>(dt) as {ClassName}Entity;
if (data != null)
{
ConvertSingleData(data);
}
return data;
}
#endregion
public static string LoadResults(string currentUserId, string currentStoreId, string currentSendParameter)
{
PagerEntity se = JsonConvert.DeserializeObject(currentSendParameter, typeof(PagerEntity)) as PagerEntity;
List<ConditionEntity> ces = JsonConvert.DeserializeObject(se.Conditions, typeof(List<ConditionEntity>)) as List<ConditionEntity>;
int count = 0;
List<SqlParameter> parameters = null;
string fromwhere = QueryFromWhere(ces, out parameters);
DataTable dt = SqlHelper.GetPager(out count, _fields, _tableName, fromwhere, se.OrderBy, se.SortOrder, se.PageIndex, se.Rows, parameters?.ToArray());
List<{ClassName}Entity> rows = SqlHelper.DataTableToIList<{ClassName}Entity>(dt) as List<{ClassName}Entity>;
foreach (var row in rows)
{
ConvertSingleData(row);
}
RecordEntity result = new RecordEntity();
result.total = (int)Math.Ceiling(count * 1.0 / se.Rows);
result.page = se.PageIndex;
result.records = count;
result.rows = rows;
OutEntity oe = new OutEntity() { ErrorCode = ErrorCode.Success, ReturnObj = JsonConvert.SerializeObject(result) };
return JsonConvert.SerializeObject(oe);
}
public static string SaveResult(string currentUserId, string currentStoreId, string currentSendParameter)
{
lock(lockobj)
{
TableInfo table = JsonConvert.DeserializeObject<TableInfo>(currentSendParameter);
using (var tranScope = new TransactionScope())
{
string connectionString = AppHelper.GetLinkString();
using (SqlConnection connection = new SqlConnection(connectionString))
{
string recid = (string)table.KeyColumnInfo.DefaultValue;
if (string.IsNullOrWhiteSpace(recid))
{
recid = Guid.NewGuid().ToString();
table.KeyColumnInfo.DefaultValue = recid;
}
table.FullColumnInfo.Add(new ColumnInfo() { ColumnName = "OrderBy", DefaultValue = 1, DataType = "int" });
table.FullColumnInfo.Add(new ColumnInfo() { ColumnName = "CreateBy", DefaultValue = currentUserId, DataType = "varchar" });
table.FullColumnInfo.Add(new ColumnInfo() { ColumnName = "CreateTime", DefaultValue = System.DateTime.Now, DataType = "datetime" });
table.FullColumnInfo.Add(new ColumnInfo() { ColumnName = "ModifyBy", DefaultValue = currentUserId, DataType = "varchar" });
table.FullColumnInfo.Add(new ColumnInfo() { ColumnName = "ModifyTime", DefaultValue = System.DateTime.Now, DataType = "datetime" });
table.FullColumnInfo.Add(new ColumnInfo() { ColumnName = "IsActive", DefaultValue = 1, DataType = "int" });
DataAccess.Insert(table, connection);
//save log
InsertToLog(connection, recid, currentUserId, ((int)AppEnum.OperType.New).ToString());
//更新池
{ClassName}Pool.Update(LoadResult(connection, recid));
tranScope.Complete();
OutEntity oe = new OutEntity() { ErrorCode = ErrorCode.Success, ReturnObj = recid };
return JsonConvert.SerializeObject(oe);
}
}
}
}
public static string UpdateResult(string currentUserId, string currentStoreId, string currentSendParameter)
{
lock (lockobj)
{
TableInfo table = JsonConvert.DeserializeObject<TableInfo>(currentSendParameter);
using (var tranScope = new TransactionScope())
{
string connectionString = AppHelper.GetLinkString();
using (SqlConnection connection = new SqlConnection(connectionString))
{
string recid = (string)table.KeyColumnInfo.DefaultValue;
table.FullColumnInfo.Add(new ColumnInfo() { ColumnName = "ModifyBy", DefaultValue = currentUserId, DataType = "varchar" });
table.FullColumnInfo.Add(new ColumnInfo() { ColumnName = "ModifyTime", DefaultValue = System.DateTime.Now, DataType = "datetime" });
DataAccess.Update(table, connection);
//save log
InsertToLog(connection, recid, currentUserId, ((int)AppEnum.OperType.Modify).ToString());
//更新池
{ClassName}Pool.Update(LoadResult(connection, recid));
tranScope.Complete();
OutEntity oe = new OutEntity() { ErrorCode = ErrorCode.Success, ReturnObj = recid };
return JsonConvert.SerializeObject(oe);
}
}
}
}
public static string LoadResult(string currentUserId, string currentStoreId, string currentSendParameter)
{
var send = JsonConvert.DeserializeObject(currentSendParameter, typeof({ClassName}Entity)) as {ClassName}Entity;
var data = LoadResult(send.RecId);
OutEntity oe = new OutEntity() { ErrorCode = ErrorCode.Success, ReturnObj = JsonConvert.SerializeObject(data) };
return JsonConvert.SerializeObject(oe);
}
public static string DeleteResult(string currentUserId, string currentStoreId, string currentSendParameter)
{
using (var tranScope = new TransactionScope())
{
string connectionString = AppHelper.GetLinkString();
using (SqlConnection connection = new SqlConnection(connectionString))
{
JArray listSendID = (JArray)JsonConvert.DeserializeObject(currentSendParameter);
foreach (var send in listSendID)
{
string value = (((Newtonsoft.Json.Linq.JValue)(send))).Value.ToString();
SqlParameter paramRecId = new SqlParameter("@RecId", SqlDbType.NVarChar) { Value = value };
SqlHelper.ExecuteNonQuery(connection, "update {TableName} set IsActive =0 where RecId=@RecId", paramRecId);
//更新池
{ClassName}Pool.Delete(value);
//save log
InsertToLog(connection, value, currentUserId, ((int)AppEnum.OperType.Delete).ToString());
}
tranScope.Complete();
OutEntity oe = new OutEntity() { ErrorCode = ErrorCode.Success, ReturnObj = JsonConvert.SerializeObject(true) };
return JsonConvert.SerializeObject(oe);
}
}
}
public static string LoadLogResults(string currentUserId, string currentStoreId, string currentSendParameter)
{
PagerEntity se = JsonConvert.DeserializeObject(currentSendParameter, typeof(PagerEntity)) as PagerEntity;
List<ConditionEntity> ces = JsonConvert.DeserializeObject(se.Conditions, typeof(List<ConditionEntity>)) as List<ConditionEntity>;
int count = 0;
List<SqlParameter> parameters = null;
string fromwhere = QueryFromWhere(ces, out parameters);
DataTable dt = SqlHelper.GetPager(out count, _log_fields, _log_tableName, fromwhere, se.OrderBy, se.SortOrder, se.PageIndex, se.Rows, parameters == null ? null: parameters.ToArray());
List<{ClassName}LogEntity> rows = SqlHelper.DataTableToIList<{ClassName}LogEntity>(dt) as List<{ClassName}LogEntity>;
foreach (var row in rows)
{
ConvertSingleData(row);
}
RecordEntity result = new RecordEntity();
result.total = (int)Math.Ceiling(count * 1.0 / se.Rows);
result.page = se.PageIndex;
result.records = se.PageIndex;
result.rows = rows;
OutEntity oe = new OutEntity() { ErrorCode = ErrorCode.Success, ReturnObj = JsonConvert.SerializeObject(result) };
return JsonConvert.SerializeObject(oe);
}
public static string ExportResults(string currentUserId, string currentStoreId, string currentSendParameter)
{
PagerEntity se = JsonConvert.DeserializeObject(currentSendParameter, typeof(PagerEntity)) as PagerEntity;
List<ConditionEntity> ces = JsonConvert.DeserializeObject(se.Conditions, typeof(List<ConditionEntity>)) as List<ConditionEntity>;
List<SqlParameter> parameters = null;
string fromwhere = QueryFromWhere(ces, out parameters);
string sqlStr = string.Format("select {0} from {1} where 1=1 {2}", _fields, _tableName, fromwhere);
DataTable dt = SqlHelper.ExecuteDataTable(sqlStr, parameters == null ? null: parameters.ToArray());
List<{ClassName}Entity> datas = SqlHelper.DataTableToIList<{ClassName}Entity>(dt) as List<{ClassName}Entity>;
HSSFWorkbook hssfworkbook = new HSSFWorkbook();
ISheet sheet1 = hssfworkbook.CreateSheet("Sheet1");
IRow row0 = sheet1.CreateRow(0);
int colIndex = 0;
//{ExcelHeader}
int rowIndex = 1;
foreach (var data in datas)
{
IRow row = sheet1.CreateRow(rowIndex);
colIndex = 0;
//{ExcelContent}
rowIndex++;
}
//set range border
AppHelper.SetBorder(hssfworkbook, 0, rowIndex, colIndex);
string fileName = string.Format("{0}.xls", Guid.NewGuid());
string xlsPath = string.Format("{0}\\{1}", FilesDirectory.CurrentExcelPath(), fileName);
FileStream fs = new FileStream(xlsPath, FileMode.CreateNew, FileAccess.Write);
hssfworkbook.Write(fs);
fs.Close();
OutEntity oe = new OutEntity() { ErrorCode = ErrorCode.Success, ReturnObj = JsonConvert.SerializeObject(string.Format("{0}{1}", FilesDirectory.CurrentExcelLinkFolder, fileName)) };
return JsonConvert.SerializeObject(oe);
}
public static string ExportLogResults(string currentUserId, string currentStoreId, string currentSendParameter)
{
PagerEntity se = JsonConvert.DeserializeObject(currentSendParameter, typeof(PagerEntity)) as PagerEntity;
List<ConditionEntity> ces = JsonConvert.DeserializeObject(se.Conditions, typeof(List<ConditionEntity>)) as List<ConditionEntity>;
List<SqlParameter> parameters = null;
string fromwhere = QueryFromWhere(ces, out parameters);
string sqlStr = string.Format("select {0} from {1} where 1=1 {2}", _log_fields, _log_tableName, fromwhere);
DataTable dt = SqlHelper.ExecuteDataTable(sqlStr, parameters == null ? null: parameters.ToArray());
List<{ClassName}LogEntity> datas = SqlHelper.DataTableToIList<{ClassName}LogEntity>(dt) as List<{ClassName}LogEntity>;
HSSFWorkbook hssfworkbook = new HSSFWorkbook();
ISheet sheet1 = hssfworkbook.CreateSheet("Sheet1");
IRow row0 = sheet1.CreateRow(0);
int colIndex = 0;
row0.CreateCell(colIndex).SetCellValue("LogId"); colIndex++;
//{ExcelHeader}
row0.CreateCell(colIndex).SetCellValue("OperTime"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("OperUser"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("OperType"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("OperInfo"); colIndex++;
int rowIndex = 1;
foreach (var data in datas)
{
IRow row = sheet1.CreateRow(rowIndex);
colIndex = 0;
row.CreateCell(colIndex).SetCellValue(data.LogId); colIndex++;
//{ExcelContent}
row.CreateCell(colIndex).SetCellValue(data.OperTime.ToString()); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.OperUser); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.OperType); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.OperInfo); colIndex++;
rowIndex++;
}
//set range border
AppHelper.SetBorder(hssfworkbook, 0, rowIndex, colIndex);
string fileName = string.Format("{0}.xls", Guid.NewGuid());
string xlsPath = string.Format("{0}\\{1}", FilesDirectory.CurrentExcelPath(), fileName);
FileStream fs = new FileStream(xlsPath, FileMode.CreateNew, FileAccess.Write);
hssfworkbook.Write(fs);
fs.Close();
OutEntity oe = new OutEntity() { ErrorCode = ErrorCode.Success, ReturnObj = JsonConvert.SerializeObject(string.Format("{0}{1}", FilesDirectory.CurrentExcelLinkFolder, fileName)) };
return JsonConvert.SerializeObject(oe);
}
private static string DealCellValue(IRow row, int index)
{
if (row.GetCell(index) == null)
return "";
return row.GetCell(index).CellType == CellType.Formula ? row.GetCell(index).StringCellValue : row.GetCell(index).ToString();
}
}
}