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.

482 lines
29 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 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 System.Data.SqlServerCe;
using MySql.Data.MySqlClient;
namespace QuiltingBusiness
{
public partial class Testriskpart
{
#region private const functions
private static object lockobj = new object();
private const string _onlyTableName="TestRiskPart";
private const string _fields = "t.RecId,t.PartCode,t.PartName,t.PartContent,t.PartIndex,t.CreateBy,t.CreateTime,t.ModifyBy,t.ModifyTime,t.IsActive";
private const string _tableName = "TestRiskPart t";
private const string _log_fields = "t.LogId,t.RecId,t.PartCode,t.PartName,t.PartContent,t.PartIndex,t.CreateBy,t.CreateTime,t.ModifyBy,t.ModifyTime,t.IsActive,t.OperTime,t.OperUser,t.OperType,t.OperInfo";
private const string _log_tableName = "TestRiskPart_log t";
private static string QueryFromWhere(string dateFrom, DateTime dtStart, DateTime dtEnd, string keyword1, string keyword2, 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.PartCode like '%{0}%' or t.PartName like '%{0}%') ", keyword1));
fromwhere = fromwhere + ((keyword2 == null || keyword2 == "") ? "" : string.Format(" and (t.PartCode like '%{0}%' or t.PartName like '%{0}%') ", keyword2));
fromwhere = fromwhere + " and t.IsActive > 0 ";
return fromwhere;
}
private static void InsertToLog(MySqlConnection connection, string recid, string currentUserId, string opertype)
{
TestriskpartEntity 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 paramPartCode = new MySqlParameter("@PartCode", MySqlDbType.VarChar) { Value = data.PartCode };
MySqlParameter paramPartName = new MySqlParameter("@PartName", MySqlDbType.VarChar) { Value = data.PartName };
MySqlParameter paramPartContent = new MySqlParameter("@PartContent", MySqlDbType.VarChar) { Value = data.PartContent };
MySqlParameter paramPartIndex = new MySqlParameter("@PartIndex", MySqlDbType.VarChar) { Value = data.PartIndex };
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 TestRiskPart_log (LogId,RecId,PartCode,PartName,PartContent,PartIndex,CreateBy,CreateTime,ModifyBy,ModifyTime,IsActive,OperType,OperUser,OperTime,OperInfo) values (@LogId,@RecId,@PartCode,@PartName,@PartContent,@PartIndex,@CreateBy,@CreateTime,@ModifyBy,@ModifyTime,@IsActive,@OperType,@OperUser,@OperTime,@OperInfo)",
new MySqlParameter[] { paramLogId, paramRecId, paramPartCode, paramPartName, paramPartContent, paramPartIndex, paramCreateBy, paramCreateTime, paramModifyBy, paramModifyTime, paramIsActive, paramOperType, paramOperUser, paramOperTime, paramOperInfo });
}
}
private static TestriskpartEntity LoadResult(MySqlConnection connection, string recid)
{
MySqlParameter paramRecId = new MySqlParameter("@RecId", MySqlDbType.VarChar) { Value = recid };
DataTable dt = MysqlHelper.ExecuteDataTable(connection, "select * from TestRiskPart t where t.RecId = @RecId and t.IsActive > 0 limit 1", paramRecId);
TestriskpartEntity data = MysqlHelper.DataRow0ToEntity<TestriskpartEntity>(dt) as TestriskpartEntity;
return data;
}
private static TestriskpartEntityExtension LoadResult(string recid)
{
MySqlParameter paramRecId = new MySqlParameter("@RecId", MySqlDbType.VarChar) { Value = recid };
DataTable dt = MysqlHelper.ExecuteDataTable("select * from TestRiskPart t where t.RecId = @RecId and t.IsActive > 0 limit 1", paramRecId);
TestriskpartEntityExtension data = MysqlHelper.DataRow0ToEntity<TestriskpartEntityExtension>(dt) as TestriskpartEntityExtension;
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");
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, createby);
DataTable dt = MysqlHelper.GetPager(out count, _fields, _tableName, fromwhere, se.OrderBy, se.SortOrder, se.PageIndex, se.Rows);
List<TestriskpartEntityExtension> rows = MysqlHelper.DataTableToIList<TestriskpartEntityExtension>(dt) as List<TestriskpartEntityExtension>;
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))
{
TestriskpartEntity send = JsonConvert.DeserializeObject(se.SendKey, typeof(TestriskpartEntity)) as TestriskpartEntity;
MySqlParameter paramRecId = new MySqlParameter("@RecId", MySqlDbType.VarChar) { Value = send.RecId };
MySqlParameter paramPartCode = new MySqlParameter("@PartCode", MySqlDbType.VarChar) { Value = send.PartCode };
MySqlParameter paramPartName = new MySqlParameter("@PartName", MySqlDbType.VarChar) { Value = send.PartName };
MySqlParameter paramPartContent = new MySqlParameter("@PartContent", MySqlDbType.VarChar) { Value = send.PartContent };
MySqlParameter paramPartIndex = new MySqlParameter("@PartIndex", MySqlDbType.VarChar) { Value = send.PartIndex };
MySqlParameter paramCreateBy = new MySqlParameter("@CreateBy", MySqlDbType.VarChar) { Value = currentUserId };
MySqlParameter paramCreateTime = new MySqlParameter("@CreateTime", MySqlDbType.DateTime) { Value = System.DateTime.Now };
MySqlParameter paramModifyBy = new MySqlParameter("@ModifyBy", MySqlDbType.VarChar) { Value = currentUserId };
MySqlParameter paramModifyTime = new MySqlParameter("@ModifyTime", MySqlDbType.DateTime) { Value = System.DateTime.Now };
MySqlParameter paramIsActive = new MySqlParameter("@IsActive", MySqlDbType.Int16) { Value = 1 };
DataTable dt = MysqlHelper.ExecuteDataTable(connection, "select PartCode from TestRiskPart where RecId <> @RecId and PartCode = @PartCode and IsActive > 0 limit 1",
paramRecId, paramPartCode);
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 TestRiskPart 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 TestRiskPart (RecId,PartCode,PartName,PartContent,PartIndex,CreateBy,CreateTime,ModifyBy,ModifyTime,IsActive) values (@RecId,@PartCode,@PartName,@PartContent,@PartIndex,@CreateBy,@CreateTime,@ModifyBy,@ModifyTime,@IsActive)",
new MySqlParameter[] { paramRecId, paramPartCode, paramPartName, paramPartContent, paramPartIndex, paramCreateBy, paramCreateTime, paramModifyBy, paramModifyTime, paramIsActive });
}
else
{
opertype = ((int)AppEnum.OperType.Modify).ToString();
MysqlHelper.ExecuteNonQuery(connection, "update TestRiskPart set PartCode = @PartCode, PartName = @PartName, PartContent = @PartContent, PartIndex = @PartIndex, ModifyBy = @ModifyBy, ModifyTime = @ModifyTime,IsActive = @IsActive where RecId = @RecId",
new MySqlParameter[] { paramPartCode, paramPartName, paramPartContent, paramPartIndex, paramModifyBy, paramModifyTime, paramIsActive, paramRecId });
}
//更新扩展字段
SysDefineExtensionValue.SaveResultForTable(connection, currentUserId, currentClientId, se.SendPair, _onlyTableName, paramRecId.Value.ToString());
//save log
InsertToLog(connection, paramRecId.Value.ToString(), currentUserId, opertype);
//更新池
TestriskpartPool.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, TestriskpartEntity send)
{
lock(lockobj)
{
MySqlParameter paramRecId = new MySqlParameter("@RecId", MySqlDbType.VarChar) { Value = send.RecId };
MySqlParameter paramPartCode = new MySqlParameter("@PartCode", MySqlDbType.VarChar) { Value = send.PartCode };
MySqlParameter paramPartName = new MySqlParameter("@PartName", MySqlDbType.VarChar) { Value = send.PartName };
MySqlParameter paramPartContent = new MySqlParameter("@PartContent", MySqlDbType.VarChar) { Value = send.PartContent };
MySqlParameter paramPartIndex = new MySqlParameter("@PartIndex", MySqlDbType.VarChar) { Value = send.PartIndex };
MySqlParameter paramCreateBy = new MySqlParameter("@CreateBy", MySqlDbType.VarChar) { Value = currentUserId };
MySqlParameter paramCreateTime = new MySqlParameter("@CreateTime", MySqlDbType.DateTime) { Value = System.DateTime.Now };
MySqlParameter paramModifyBy = new MySqlParameter("@ModifyBy", MySqlDbType.VarChar) { Value = currentUserId };
MySqlParameter paramModifyTime = new MySqlParameter("@ModifyTime", MySqlDbType.DateTime) { Value = System.DateTime.Now };
MySqlParameter paramIsActive = new MySqlParameter("@IsActive", MySqlDbType.Int16) { Value = 1 };
DataTable dt = MysqlHelper.ExecuteDataTable(connection, "select RecId from TestRiskPart where PartCode = @PartCode and IsActive > 0 limit 1", paramPartCode);
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 TestRiskPart (RecId,PartCode,PartName,PartContent,PartIndex,CreateBy,CreateTime,ModifyBy,ModifyTime,IsActive) values (@RecId,@PartCode,@PartName,@PartContent,@PartIndex,@CreateBy,@CreateTime,@ModifyBy,@ModifyTime,@IsActive)",
new MySqlParameter[] { paramRecId, paramPartCode, paramPartName, paramPartContent, paramPartIndex, paramCreateBy, paramCreateTime, paramModifyBy, paramModifyTime, paramIsActive });
}
else
{
opertype = ((int)AppEnum.OperType.Modify).ToString();
paramRecId.Value = dt.Rows[0][0].ToString();
MysqlHelper.ExecuteNonQuery(connection, "update TestRiskPart set PartCode = @PartCode, PartName = @PartName, PartContent = @PartContent, PartIndex = @PartIndex, ModifyBy = @ModifyBy,ModifyTime = @ModifyTime,IsActive = @IsActive where RecId = @RecId",
new MySqlParameter[] { paramPartCode, paramPartName, paramPartContent, paramPartIndex, paramModifyBy, paramModifyTime, paramIsActive, paramRecId });
}
//更新池,由于事物可能未提交,先不更新缓存,在提交后在更新缓存
//TestriskpartPool.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, TestriskpartEntity 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();
//更新池
TestriskpartPool.Update(LoadResult(connection, recId));
}
}
return recId;
}
public static string LoadResult(string currentUserId, string currentClientId, string currentSendParameter)
{
var send = JsonConvert.DeserializeObject(currentSendParameter, typeof(TestriskpartEntity)) as TestriskpartEntity;
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 TestRiskPart set IsActive =0 where RecId=@RecId", paramRecId);
//更新池
TestriskpartPool.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<TestriskpartLogEntityExtension> rows = MysqlHelper.DataTableToIList<TestriskpartLogEntityExtension>(dt) as List<TestriskpartLogEntityExtension>;
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<TestriskpartEntityExtension> datas = MysqlHelper.DataTableToIList<TestriskpartEntityExtension>(dt) as List<TestriskpartEntityExtension>;
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("PartCode"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("PartName"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("PartContent"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("PartIndex"); 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.PartCode); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.PartName); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.PartContent); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.PartIndex.ToString()); 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<TestriskpartLogEntityExtension> datas = MysqlHelper.DataTableToIList<TestriskpartLogEntityExtension>(dt) as List<TestriskpartLogEntityExtension>;
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("PartCode"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("PartName"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("PartContent"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("PartIndex"); 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.PartCode); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.PartName); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.PartContent); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.PartIndex.ToString()); 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();
}
}
}