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.

478 lines
30 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.
//
// 本软件为 ** 公司开发版权所有违者必究23810511@qq.com
//
/////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.IO;
using System.Reflection;
using System.Data;
using System.Data.SqlClient;
using System.Transactions;
using QuiltingCommon;
using MySql.Data.MySqlClient;
namespace QuiltingBusiness
{
public partial class Pagetype
{
#region private const functions
private static object lockobj = new object();
private const string _onlyTableName = "PageType";
private const string _fields = "t.RecId,t.TypeCode,t.TypeName,t.TypeGroup,t.CreateBy,t.CreateTime,t.ModifyBy,t.ModifyTime,t.IsActive";
private const string _tableName = "PageType t";
private const string _log_fields = "t.LogId,t.RecId,t.TypeCode,t.TypeName,t.TypeGroup,t.CreateBy,t.CreateTime,t.ModifyBy,t.ModifyTime,t.IsActive,t.OperTime,t.OperUser,t.OperType,t.OperInfo";
private const string _log_tableName = "PageType_log t";
private static string QueryFromWhere(string dateFrom, DateTime dtStart, DateTime dtEnd, string keyword1,
string keyword2, string pageGroup, string createby = null)
{
string fromwhere = string.Empty;
fromwhere = fromwhere + ((createby == null || createby == "") ? "" : string.Format(" and t.CreateBy = '{0}' ", createby));
fromwhere = fromwhere + ((dateFrom == null || dateFrom == "") ? "" : string.Format(" and t.CreateTime >= '{0}' and t.CreateTime <= '{1}' ", dtStart, dtEnd));
fromwhere = fromwhere + ((keyword1 == null || keyword1 == "") ? "" : string.Format(" and (t.TypeCode like '%{0}%' or t.TypeName like '%{0}%') ", keyword1));
fromwhere = fromwhere + ((keyword2 == null || keyword2 == "") ? "" : string.Format(" and (t.TypeCode like '%{0}%' or t.TypeName like '%{0}%') ", keyword2));
fromwhere = fromwhere + ((pageGroup == null || pageGroup == "") ? "" : string.Format(" and lower(t.TypeGroup) = '{0}' ", pageGroup));
fromwhere = fromwhere + " and t.IsActive > 0 ";
return fromwhere;
}
private static void InsertToLog(MySqlConnection connection, string recid, string currentUserId, string opertype)
{
PagetypeEntity data = LoadResult(connection, recid);
if (data != null)
{
MySqlParameter paramLogId = new MySqlParameter("@LogId", MySqlDbType.VarChar) { Value = Guid.NewGuid().ToString() };
MySqlParameter paramRecId = new MySqlParameter("@RecId", MySqlDbType.VarChar) { Value = data.RecId };
MySqlParameter paramTypeCode = new MySqlParameter("@TypeCode", MySqlDbType.VarChar) { Value = data.TypeCode };
MySqlParameter paramTypeName = new MySqlParameter("@TypeName", MySqlDbType.VarChar) { Value = data.TypeName };
MySqlParameter paramTypeGroup = new MySqlParameter("@TypeGroup", MySqlDbType.VarChar) { Value = data.TypeGroup };
MySqlParameter paramCreateBy = new MySqlParameter("@CreateBy", MySqlDbType.VarChar) { Value = data.CreateBy };
MySqlParameter paramCreateTime = new MySqlParameter("@CreateTime", MySqlDbType.DateTime) { Value = data.CreateTime };
MySqlParameter paramModifyBy = new MySqlParameter("@ModifyBy", MySqlDbType.VarChar) { Value = data.ModifyBy };
MySqlParameter paramModifyTime = new MySqlParameter("@ModifyTime", MySqlDbType.DateTime) { Value = data.ModifyTime };
MySqlParameter paramIsActive = new MySqlParameter("@IsActive", MySqlDbType.VarChar) { Value = data.IsActive };
MySqlParameter paramOperType = new MySqlParameter("@OperType", MySqlDbType.VarChar) { Value = opertype };
MySqlParameter paramOperUser = new MySqlParameter("@OperUser", MySqlDbType.VarChar) { Value = currentUserId };
MySqlParameter paramOperTime = new MySqlParameter("@OperTime", MySqlDbType.DateTime) { Value = DateTime.Now };
MySqlParameter paramOperInfo = new MySqlParameter("@OperInfo", MySqlDbType.VarChar) { Value = "" };
MysqlHelper.ExecuteNonQuery(connection, "insert into PageType_log (LogId,RecId,TypeCode,TypeName,TypeGroup,CreateBy,CreateTime,ModifyBy,ModifyTime,IsActive,OperType,OperUser,OperTime,OperInfo) values (@LogId,@RecId,@TypeCode,@TypeName,@TypeGroup,@CreateBy,@CreateTime,@ModifyBy,@ModifyTime,@IsActive,@OperType,@OperUser,@OperTime,@OperInfo)",
new MySqlParameter[] { paramLogId, paramRecId, paramTypeCode, paramTypeName, paramTypeGroup, paramCreateBy, paramCreateTime, paramModifyBy, paramModifyTime, paramIsActive, paramOperType, paramOperUser, paramOperTime, paramOperInfo });
}
}
private static PagetypeEntity LoadResult(MySqlConnection connection, string recid)
{
MySqlParameter paramRecId = new MySqlParameter("@RecId", MySqlDbType.VarChar) { Value = recid };
DataTable dt = MysqlHelper.ExecuteDataTable(connection, "select * from PageType t where t.RecId = @RecId and t.IsActive > 0 limit 1", paramRecId);
PagetypeEntity data = MysqlHelper.DataRow0ToEntity<PagetypeEntity>(dt) as PagetypeEntity;
return data;
}
private static PagetypeEntityExtension LoadResult(string recid)
{
MySqlParameter paramRecId = new MySqlParameter("@RecId", MySqlDbType.VarChar) { Value = recid };
DataTable dt = MysqlHelper.ExecuteDataTable("select * from PageType t where t.RecId = @RecId and t.IsActive > 0 limit 1", paramRecId);
PagetypeEntityExtension data = MysqlHelper.DataRow0ToEntity<PagetypeEntityExtension>(dt) as PagetypeEntityExtension;
if (data != null)
{
ConvertSingleData(data);
}
return data;
}
private static string GetConditionValue(List<ConditionEntity> ce, string keyword)
{
if (ce.Where(q => q.Name.Equals(keyword)).FirstOrDefault() != null)
{
return ce.Where(q => q.Name.Equals(keyword, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault().Value;
}
return string.Empty;
}
#endregion
public static string LoadResults(string currentUserId, string currentClientId, string currentSendParameter)
{
PagerEntity se = JsonConvert.DeserializeObject(currentSendParameter, typeof(PagerEntity)) as PagerEntity;
List<ConditionEntity> ce = JsonConvert.DeserializeObject(se.Conditions, typeof(List<ConditionEntity>)) as List<ConditionEntity>;
string createby = GetConditionValue(ce, "CreateBy");
string keyword1 = GetConditionValue(ce, "KeyWord1");
string keyword2 = GetConditionValue(ce, "KeyWord2");
string dateFrom = GetConditionValue(ce, "DateFrom");
string dateTo = GetConditionValue(ce, "DateTo");
string pageGroup = GetConditionValue(ce, "PageGroup");
DateTime dtStart;
DateTime dtEnd;
if (!DateTime.TryParse(dateFrom, out dtStart))
dtStart = new DateTime(1900, 1, 1);
if (!DateTime.TryParse(dateTo, out dtEnd))
dtEnd = new DateTime(2100, 1, 1);
int count = 0;
string fromwhere = QueryFromWhere(dateFrom, dtStart, dtEnd, keyword1, keyword2, pageGroup, createby);
DataTable dt = MysqlHelper.GetPager(out count, _fields, _tableName, fromwhere, se.OrderBy, se.SortOrder, se.PageIndex, se.Rows);
List<PagetypeEntityExtension> rows = MysqlHelper.DataTableToIList<PagetypeEntityExtension>(dt) as List<PagetypeEntityExtension>;
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 currentClientId, string currentSendParameter)
{
lock (lockobj)
{
var se = JsonConvert.DeserializeObject(currentSendParameter, typeof(BaseSendEntity)) as BaseSendEntity;
using (var tranScope = new TransactionScope())
{
string connectionString = AppHelper.GetLinkString();
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
PagetypeEntity send = JsonConvert.DeserializeObject(se.SendKey, typeof(PagetypeEntity)) as PagetypeEntity;
MySqlParameter paramRecId = new MySqlParameter("@RecId", MySqlDbType.VarChar) { Value = send.RecId };
MySqlParameter paramTypeCode = new MySqlParameter("@TypeCode", MySqlDbType.VarChar) { Value = send.TypeCode };
MySqlParameter paramTypeName = new MySqlParameter("@TypeName", MySqlDbType.VarChar) { Value = send.TypeName };
MySqlParameter paramTypeGroup = new MySqlParameter("@TypeGroup", MySqlDbType.VarChar) { Value = send.TypeGroup };
MySqlParameter paramCreateBy = new MySqlParameter("@CreateBy", MySqlDbType.VarChar) { Value = currentUserId };
MySqlParameter paramCreateTime = new MySqlParameter("@CreateTime", MySqlDbType.DateTime) { Value = send.CreateTime == null ? System.DateTime.Now : send.CreateTime };
MySqlParameter paramModifyBy = new MySqlParameter("@ModifyBy", MySqlDbType.VarChar) { Value = currentUserId };
MySqlParameter paramModifyTime = new MySqlParameter("@ModifyTime", MySqlDbType.DateTime) { Value = send.ModifyTime == null ? System.DateTime.Now : send.ModifyTime };
MySqlParameter paramIsActive = new MySqlParameter("@IsActive", MySqlDbType.Int16) { Value = 1 };
DataTable dt = MysqlHelper.ExecuteDataTable(connection, "select TypeCode from PageType where RecId <> @RecId and TypeCode = @TypeCode and IsActive > 0 limit 1",
paramRecId, paramTypeCode);
if (dt.Rows.Count > 0)
{
OutEntity oex1 = new OutEntity() { ErrorCode = ErrorCode.UnexpectError, ErrorMessage = "当前编号:" + dt.Rows[0][0].ToString() + "已添加" };
return JsonConvert.SerializeObject(oex1);
}
dt = MysqlHelper.ExecuteDataTable(connection, "select RecId from PageType where RecId = @RecId and IsActive > 0 limit 1", paramRecId);
var opertype = "";
if (dt.Rows.Count == 0)
{
opertype = ((int)AppEnum.OperType.New).ToString();
if (string.IsNullOrEmpty(send.RecId))
{
paramRecId.Value = Guid.NewGuid().ToString();
}
paramIsActive.Value = 1;
MysqlHelper.ExecuteNonQuery(connection, "insert into PageType (RecId,TypeCode,TypeName,TypeGroup,CreateBy,CreateTime,ModifyBy,ModifyTime,IsActive) values (@RecId,@TypeCode,@TypeName,@TypeGroup,@CreateBy,@CreateTime,@ModifyBy,@ModifyTime,@IsActive)",
new MySqlParameter[] { paramRecId, paramTypeCode, paramTypeName, paramTypeGroup, paramCreateBy, paramCreateTime, paramModifyBy, paramModifyTime, paramIsActive });
}
else
{
opertype = ((int)AppEnum.OperType.Modify).ToString();
MysqlHelper.ExecuteNonQuery(connection, "update PageType set TypeCode = @TypeCode, TypeName = @TypeName, TypeGroup = @TypeGroup, ModifyBy = @ModifyBy, ModifyTime = @ModifyTime,IsActive = @IsActive where RecId = @RecId",
new MySqlParameter[] { paramTypeCode, paramTypeName, paramTypeGroup, paramModifyBy, paramModifyTime, paramIsActive, paramRecId });
}
//更新扩展字段
SysDefineExtensionValue.SaveResultForTable(connection, currentUserId, currentClientId, se.SendPair, _onlyTableName, paramRecId.Value.ToString());
//save log
InsertToLog(connection, paramRecId.Value.ToString(), currentUserId, opertype);
//更新池
PagetypePool.Update(LoadResult(connection, paramRecId.Value.ToString()));
tranScope.Complete();
OutEntity oe = new OutEntity() { ErrorCode = ErrorCode.Success, ReturnObj = paramRecId.Value.ToString() };
return JsonConvert.SerializeObject(oe);
}
}
}
}
public static string SaveResultByCode(MySqlConnection connection, string currentUserId, PagetypeEntity send)
{
lock (lockobj)
{
MySqlParameter paramRecId = new MySqlParameter("@RecId", MySqlDbType.VarChar) { Value = send.RecId };
MySqlParameter paramTypeCode = new MySqlParameter("@TypeCode", MySqlDbType.VarChar) { Value = send.TypeCode };
MySqlParameter paramTypeName = new MySqlParameter("@TypeName", MySqlDbType.VarChar) { Value = send.TypeName };
MySqlParameter paramTypeGroup = new MySqlParameter("@TypeGroup", MySqlDbType.VarChar) { Value = send.TypeGroup };
MySqlParameter paramCreateBy = new MySqlParameter("@CreateBy", MySqlDbType.VarChar) { Value = currentUserId };
MySqlParameter paramCreateTime = new MySqlParameter("@CreateTime", MySqlDbType.DateTime) { Value = send.CreateTime == null ? System.DateTime.Now : send.CreateTime };
MySqlParameter paramModifyBy = new MySqlParameter("@ModifyBy", MySqlDbType.VarChar) { Value = currentUserId };
MySqlParameter paramModifyTime = new MySqlParameter("@ModifyTime", MySqlDbType.DateTime) { Value = send.ModifyTime == null ? System.DateTime.Now : send.ModifyTime };
MySqlParameter paramIsActive = new MySqlParameter("@IsActive", MySqlDbType.Int16) { Value = 1 };
DataTable dt = MysqlHelper.ExecuteDataTable(connection, "select RecId from PageType where TypeCode = @TypeCode and IsActive > 0 limit 1", paramTypeCode);
var opertype = "";
if (dt.Rows.Count == 0)
{
opertype = ((int)AppEnum.OperType.New).ToString();
paramRecId.Value = Guid.NewGuid().ToString();
paramIsActive.Value = 1;
MysqlHelper.ExecuteNonQuery(connection, "insert into PageType (RecId,TypeCode,TypeName,TypeGroup,CreateBy,CreateTime,ModifyBy,ModifyTime,IsActive) values (@RecId,@TypeCode,@TypeName,@TypeGroup,@CreateBy,@CreateTime,@ModifyBy,@ModifyTime,@IsActive)",
new MySqlParameter[] { paramRecId, paramTypeCode, paramTypeName, paramTypeGroup, paramCreateBy, paramCreateTime, paramModifyBy, paramModifyTime, paramIsActive });
}
else
{
opertype = ((int)AppEnum.OperType.Modify).ToString();
paramRecId.Value = dt.Rows[0][0].ToString();
MysqlHelper.ExecuteNonQuery(connection, "update PageType set TypeCode = @TypeCode, TypeName = @TypeName, TypeGroup = @TypeGroup, ModifyBy = @ModifyBy,ModifyTime = @ModifyTime,IsActive = @IsActive where RecId = @RecId",
new MySqlParameter[] { paramTypeCode, paramTypeName, paramTypeGroup, paramModifyBy, paramModifyTime, paramIsActive, paramRecId });
}
//更新池,由于事物可能未提交,先不更新缓存,在提交后在更新缓存
//PagetypePool.Update(LoadResult(connection, paramRecId.Value.ToString()));
//save log
InsertToLog(connection, paramRecId.Value.ToString(), currentUserId, opertype);
return paramRecId.Value.ToString();
}
}
public static string SaveResultByCode(string currentUserId, PagetypeEntity send)
{
string recId = string.Empty;
using (var tranScope = new TransactionScope())
{
string connectionString = AppHelper.GetLinkString();
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
recId = SaveResultByCode(connection, currentUserId, send);
tranScope.Complete();
//更新池
PagetypePool.Update(LoadResult(connection, recId));
}
}
return recId;
}
public static string LoadResult(string currentUserId, string currentClientId, string currentSendParameter)
{
var send = JsonConvert.DeserializeObject(currentSendParameter, typeof(PagetypeEntity)) as PagetypeEntity;
var data = LoadResult(send.RecId);
var extend = SysDefineExtensionValue.LoadResultByTableNameAndRefrenceId(_onlyTableName, send.RecId, currentUserId);
BaseSendEntity bse = new BaseSendEntity()
{
SendKey = JsonConvert.SerializeObject(data),
SendPair = extend.Any() ? JsonConvert.SerializeObject(extend) : null
};
OutEntity oe = new OutEntity() { ErrorCode = ErrorCode.Success, ReturnObj = JsonConvert.SerializeObject(bse) };
return JsonConvert.SerializeObject(oe);
}
public static string DeleteResult(string currentUserId, string currentClientId, string currentSendParameter)
{
using (var tranScope = new TransactionScope())
{
string connectionString = AppHelper.GetLinkString();
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
JArray listSendID = (JArray)JsonConvert.DeserializeObject(currentSendParameter);
foreach (var send in listSendID)
{
string value = (((Newtonsoft.Json.Linq.JValue)(send))).Value.ToString();
MySqlParameter paramRecId = new MySqlParameter("@RecId", MySqlDbType.VarChar) { Value = value };
MysqlHelper.ExecuteNonQuery(connection, "update PageType set IsActive =0 where RecId=@RecId", paramRecId);
//更新池
PagetypePool.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 currentClientId, string currentSendParameter)
{
PagerEntity se = JsonConvert.DeserializeObject(currentSendParameter, typeof(PagerEntity)) as PagerEntity;
List<ConditionEntity> ce = JsonConvert.DeserializeObject(se.Conditions, typeof(List<ConditionEntity>)) as List<ConditionEntity>;
string keyword1 = GetConditionValue(ce, "KeyWord1");
string keyword2 = GetConditionValue(ce, "KeyWord2");
string dateFrom = GetConditionValue(ce, "DateFrom");
string dateTo = GetConditionValue(ce, "DateTo");
DateTime dtStart;
DateTime dtEnd;
if (!DateTime.TryParse(dateFrom, out dtStart))
dtStart = new DateTime(1900, 1, 1);
if (!DateTime.TryParse(dateTo, out dtEnd))
dtEnd = new DateTime(2100, 1, 1);
int count = 0;
string fromwhere = QueryFromWhere(dateFrom, dtStart, dtEnd, keyword1, keyword2, "");
DataTable dt = MysqlHelper.GetPager(out count, _log_fields, _log_tableName, fromwhere, se.OrderBy, se.SortOrder, se.PageIndex, se.Rows);
List<PagetypeLogEntityExtension> rows = MysqlHelper.DataTableToIList<PagetypeLogEntityExtension>(dt) as List<PagetypeLogEntityExtension>;
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 currentClientId, string currentSendParameter)
{
PagerEntity se = JsonConvert.DeserializeObject(currentSendParameter, typeof(PagerEntity)) as PagerEntity;
List<ConditionEntity> ce = JsonConvert.DeserializeObject(se.Conditions, typeof(List<ConditionEntity>)) as List<ConditionEntity>;
string keyword1 = GetConditionValue(ce, "KeyWord1");
string keyword2 = GetConditionValue(ce, "KeyWord2");
string dateFrom = GetConditionValue(ce, "DateFrom");
string dateTo = GetConditionValue(ce, "DateTo");
DateTime dtStart;
DateTime dtEnd;
if (!DateTime.TryParse(dateFrom, out dtStart))
dtStart = new DateTime(1900, 1, 1);
if (!DateTime.TryParse(dateTo, out dtEnd))
dtEnd = new DateTime(2100, 1, 1);
string fromwhere = QueryFromWhere(dateFrom, dtStart, dtEnd, keyword1, keyword2, "");
string sqlStr = string.Format("select {0} from {1} where 1=1 {2}", _fields, _tableName, fromwhere);
DataTable dt = MysqlHelper.ExecuteDataTable(sqlStr);
List<PagetypeEntityExtension> datas = MysqlHelper.DataTableToIList<PagetypeEntityExtension>(dt) as List<PagetypeEntityExtension>;
HSSFWorkbook hssfworkbook = new HSSFWorkbook();
ISheet sheet1 = hssfworkbook.CreateSheet("Sheet1");
IRow row0 = sheet1.CreateRow(0);
int colIndex = 0;
row0.CreateCell(colIndex).SetCellValue("RecId"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("TypeCode"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("TypeName"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("TypeGroup"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("CreateBy"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("CreateTime"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("ModifyBy"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("ModifyTime"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("IsActive"); colIndex++;
int rowIndex = 1;
foreach (var data in datas)
{
IRow row = sheet1.CreateRow(rowIndex);
colIndex = 0;
row.CreateCell(colIndex).SetCellValue(data.RecId); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.TypeCode); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.TypeName); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.TypeGroup); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.CreateBy); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.CreateTime.ToString()); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.ModifyBy); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.ModifyTime.ToString()); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.IsActive.ToString()); 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}", SystemConst.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}", SystemConst.CurrentExcelLinkFolder, fileName)) };
return JsonConvert.SerializeObject(oe);
}
public static string ExportLogResults(string currentUserId, string currentClientId, string currentSendParameter)
{
PagerEntity se = JsonConvert.DeserializeObject(currentSendParameter, typeof(PagerEntity)) as PagerEntity;
List<ConditionEntity> ce = JsonConvert.DeserializeObject(se.Conditions, typeof(List<ConditionEntity>)) as List<ConditionEntity>;
string keyword1 = GetConditionValue(ce, "KeyWord1");
string keyword2 = GetConditionValue(ce, "KeyWord2");
string dateFrom = GetConditionValue(ce, "DateFrom");
string dateTo = GetConditionValue(ce, "DateTo");
DateTime dtStart;
DateTime dtEnd;
if (!DateTime.TryParse(dateFrom, out dtStart))
dtStart = new DateTime(1900, 1, 1);
if (!DateTime.TryParse(dateTo, out dtEnd))
dtEnd = new DateTime(2100, 1, 1);
string fromwhere = QueryFromWhere(dateFrom, dtStart, dtEnd, keyword1, keyword2, "");
string sqlStr = string.Format("select {0} from {1} where 1=1 {2}", _log_fields, _log_tableName, fromwhere);
DataTable dt = MysqlHelper.ExecuteDataTable(sqlStr);
List<PagetypeLogEntityExtension> datas = MysqlHelper.DataTableToIList<PagetypeLogEntityExtension>(dt) as List<PagetypeLogEntityExtension>;
HSSFWorkbook hssfworkbook = new HSSFWorkbook();
ISheet sheet1 = hssfworkbook.CreateSheet("Sheet1");
IRow row0 = sheet1.CreateRow(0);
int colIndex = 0;
row0.CreateCell(colIndex).SetCellValue("LogId"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("RecId"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("TypeCode"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("TypeName"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("TypeGroup"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("CreateBy"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("CreateTime"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("ModifyBy"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("ModifyTime"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("IsActive"); colIndex++;
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++;
row.CreateCell(colIndex).SetCellValue(data.RecId); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.TypeCode); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.TypeName); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.TypeGroup); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.CreateBy); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.CreateTime.ToString()); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.ModifyBy); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.ModifyTime.ToString()); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.IsActive.ToString()); colIndex++;
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}", SystemConst.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}", SystemConst.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();
}
}
}