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.

495 lines
31 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 Testpart
{
#region private const functions
private static object lockobj = new object();
private const string _onlyTableName="TestPart";
private const string _fields = "t.RecId,t.PartCode,t.PartName,t.PartType,t.PartAvg,t.PartMax,t.PartMin,t.CreateBy,t.CreateTime,t.ModifyBy,t.ModifyTime,t.IsActive";
private const string _tableName = "TestPart t";
private const string _log_fields = "t.LogId,t.RecId,t.PartCode,t.PartName,t.PartType,t.PartAvg,t.PartMax,t.PartMin,t.CreateBy,t.CreateTime,t.ModifyBy,t.ModifyTime,t.IsActive,t.OperTime,t.OperUser,t.OperType,t.OperInfo";
private const string _log_tableName = "TestPart_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)
{
TestpartEntity 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 paramPartType = new MySqlParameter("@PartType", MySqlDbType.VarChar) { Value = data.PartType };
MySqlParameter paramPartAvg = new MySqlParameter("@PartAvg", MySqlDbType.VarChar) { Value = data.PartAvg };
MySqlParameter paramPartMax = new MySqlParameter("@PartMax", MySqlDbType.VarChar) { Value = data.PartMax };
MySqlParameter paramPartMin = new MySqlParameter("@PartMin", MySqlDbType.VarChar) { Value = data.PartMin };
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 TestPart_log (LogId,RecId,PartCode,PartName,PartType,PartAvg,PartMax,PartMin,CreateBy,CreateTime,ModifyBy,ModifyTime,IsActive,OperType,OperUser,OperTime,OperInfo) values (@LogId,@RecId,@PartCode,@PartName,@PartType,@PartAvg,@PartMax,@PartMin,@CreateBy,@CreateTime,@ModifyBy,@ModifyTime,@IsActive,@OperType,@OperUser,@OperTime,@OperInfo)",
new MySqlParameter[] { paramLogId, paramRecId, paramPartCode, paramPartName, paramPartType, paramPartAvg, paramPartMax, paramPartMin, paramCreateBy, paramCreateTime, paramModifyBy, paramModifyTime, paramIsActive, paramOperType, paramOperUser, paramOperTime, paramOperInfo });
}
}
private static TestpartEntityExtension LoadResult(MySqlConnection connection, string recid)
{
MySqlParameter paramRecId = new MySqlParameter("@RecId", MySqlDbType.VarChar) { Value = recid };
DataTable dt = MysqlHelper.ExecuteDataTable(connection, "select * from TestPart t where t.RecId = @RecId and t.IsActive > 0 limit 1", paramRecId);
TestpartEntityExtension data = MysqlHelper.DataRow0ToEntity<TestpartEntityExtension>(dt) as TestpartEntityExtension;
return data;
}
private static TestpartEntityExtension LoadResult(string recid)
{
MySqlParameter paramRecId = new MySqlParameter("@RecId", MySqlDbType.VarChar) { Value = recid };
DataTable dt = MysqlHelper.ExecuteDataTable("select * from TestPart t where t.RecId = @RecId and t.IsActive > 0 limit 1", paramRecId);
TestpartEntityExtension data = MysqlHelper.DataRow0ToEntity<TestpartEntityExtension>(dt) as TestpartEntityExtension;
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<TestpartEntityExtension> rows = MysqlHelper.DataTableToIList<TestpartEntityExtension>(dt) as List<TestpartEntityExtension>;
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))
{
TestpartEntity send = JsonConvert.DeserializeObject(se.SendKey, typeof(TestpartEntity)) as TestpartEntity;
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 paramPartType = new MySqlParameter("@PartType", MySqlDbType.VarChar) { Value = send.PartType };
MySqlParameter paramPartAvg = new MySqlParameter("@PartAvg", MySqlDbType.VarChar) { Value = send.PartAvg };
MySqlParameter paramPartMax = new MySqlParameter("@PartMax", MySqlDbType.VarChar) { Value = send.PartMax };
MySqlParameter paramPartMin = new MySqlParameter("@PartMin", MySqlDbType.VarChar) { Value = send.PartMin };
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 PartCode from TestPart 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 TestPart 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 TestPart (RecId,PartCode,PartName,PartType,PartAvg,PartMax,PartMin,CreateBy,CreateTime,ModifyBy,ModifyTime,IsActive) values (@RecId,@PartCode,@PartName,@PartType,@PartAvg,@PartMax,@PartMin,@CreateBy,@CreateTime,@ModifyBy,@ModifyTime,@IsActive)",
new MySqlParameter[] { paramRecId, paramPartCode, paramPartName, paramPartType, paramPartAvg, paramPartMax, paramPartMin, paramCreateBy, paramCreateTime, paramModifyBy, paramModifyTime, paramIsActive });
}
else
{
opertype = ((int)AppEnum.OperType.Modify).ToString();
MysqlHelper.ExecuteNonQuery(connection, "update TestPart set PartCode = @PartCode, PartName = @PartName, PartType = @PartType, PartAvg = @PartAvg, PartMax = @PartMax, PartMin = @PartMin, ModifyBy = @ModifyBy, ModifyTime = @ModifyTime,IsActive = @IsActive where RecId = @RecId",
new MySqlParameter[] { paramPartCode, paramPartName, paramPartType, paramPartAvg, paramPartMax, paramPartMin, paramModifyBy, paramModifyTime, paramIsActive, paramRecId });
}
//更新扩展字段
SysDefineExtensionValue.SaveResultForTable(connection, currentUserId, currentClientId, se.SendPair, _onlyTableName, paramRecId.Value.ToString());
//save log
InsertToLog(connection, paramRecId.Value.ToString(), currentUserId, opertype);
//更新池
TestpartPool.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, TestpartEntity 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 paramPartType = new MySqlParameter("@PartType", MySqlDbType.VarChar) { Value = send.PartType };
MySqlParameter paramPartAvg = new MySqlParameter("@PartAvg", MySqlDbType.VarChar) { Value = send.PartAvg };
MySqlParameter paramPartMax = new MySqlParameter("@PartMax", MySqlDbType.VarChar) { Value = send.PartMax };
MySqlParameter paramPartMin = new MySqlParameter("@PartMin", MySqlDbType.VarChar) { Value = send.PartMin };
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 TestPart 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 TestPart (RecId,PartCode,PartName,PartType,PartAvg,PartMax,PartMin,CreateBy,CreateTime,ModifyBy,ModifyTime,IsActive) values (@RecId,@PartCode,@PartName,@PartType,@PartAvg,@PartMax,@PartMin,@CreateBy,@CreateTime,@ModifyBy,@ModifyTime,@IsActive)",
new MySqlParameter[] { paramRecId, paramPartCode, paramPartName, paramPartType, paramPartAvg, paramPartMax, paramPartMin, paramCreateBy, paramCreateTime, paramModifyBy, paramModifyTime, paramIsActive });
}
else
{
opertype = ((int)AppEnum.OperType.Modify).ToString();
paramRecId.Value = dt.Rows[0][0].ToString();
MysqlHelper.ExecuteNonQuery(connection, "update TestPart set PartCode = @PartCode, PartName = @PartName, PartType = @PartType, PartAvg = @PartAvg, PartMax = @PartMax, PartMin = @PartMin, ModifyBy = @ModifyBy,ModifyTime = @ModifyTime,IsActive = @IsActive where RecId = @RecId",
new MySqlParameter[] { paramPartCode, paramPartName, paramPartType, paramPartAvg, paramPartMax, paramPartMin, paramModifyBy, paramModifyTime, paramIsActive, paramRecId });
}
//更新池,由于事物可能未提交,先不更新缓存,在提交后在更新缓存
//TestpartPool.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, TestpartEntity 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);
//更新池
TestpartPool.Update(LoadResult(connection, recId));
tranScope.Complete();
}
}
return recId;
}
public static string LoadResult(string currentUserId, string currentClientId, string currentSendParameter)
{
var send = JsonConvert.DeserializeObject(currentSendParameter, typeof(TestpartEntity)) as TestpartEntity;
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 TestPart set IsActive =0 where RecId=@RecId", paramRecId);
//更新池
TestpartPool.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<TestpartLogEntityExtension> rows = MysqlHelper.DataTableToIList<TestpartLogEntityExtension>(dt) as List<TestpartLogEntityExtension>;
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<TestpartEntityExtension> datas = MysqlHelper.DataTableToIList<TestpartEntityExtension>(dt) as List<TestpartEntityExtension>;
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("PartType"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("PartAvg"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("PartMax"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("PartMin"); 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.PartType); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.PartAvg.ToString()); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.PartMax.ToString()); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.PartMin.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<TestpartLogEntityExtension> datas = MysqlHelper.DataTableToIList<TestpartLogEntityExtension>(dt) as List<TestpartLogEntityExtension>;
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("PartType"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("PartAvg"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("PartMax"); colIndex++;
row0.CreateCell(colIndex).SetCellValue("PartMin"); 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.PartType); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.PartAvg.ToString()); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.PartMax.ToString()); colIndex++;
row.CreateCell(colIndex).SetCellValue(data.PartMin.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();
}
}
}