///////////////////////////////////////////////////////////////// // // (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; using MySql.Data.MySqlClient; namespace QuiltingBusiness { public partial class Testphotofiles { #region private const functions private static object lockobj = new object(); private const string _onlyTableName="TestPhotoFiles"; private const string _fields = "t.RecId,t.FileContent,t.IRContent,t.CreateBy,t.CreateTime,t.ModifyBy,t.ModifyTime,t.IsActive"; private const string _tableName = "TestPhotoFiles t"; private const string _log_fields = "t.LogId,t.RecId,t.FileContent,t.IRContent,t.CreateBy,t.CreateTime,t.ModifyBy,t.ModifyTime,t.IsActive,t.OperTime,t.OperUser,t.OperType,t.OperInfo"; private const string _log_tableName = "TestPhotoFiles_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. like '%{0}%' or t. like '%{0}%') ", keyword1)); fromwhere = fromwhere + ((keyword2 == null || keyword2 == "") ? "" : string.Format(" and (t. like '%{0}%' or t. like '%{0}%') ", keyword2)); fromwhere = fromwhere + " and t.IsActive > 0 "; return fromwhere; } private static string GetConditionValue(List 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 ce = JsonConvert.DeserializeObject(se.Conditions, typeof(List)) as List; 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 rows = MysqlHelper.DataTableToIList(dt) as List; 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 ImportResult(string currentUserId, string currentClientId, TestphotofilesEntity send) { lock (lockobj) { using (var tranScope = new TransactionScope()) { string connectionString = AppHelper.GetLinkString(); using (MySqlConnection connection = new MySqlConnection(connectionString)) { MySqlParameter paramRecId = new MySqlParameter("@RecId", MySqlDbType.VarChar) { Value = send.RecId }; MySqlParameter paramFileContent = new MySqlParameter("@FileContent", MySqlDbType.MediumBlob) { Value = send.FileContent }; MySqlParameter paramIRContent = new MySqlParameter("@IRContent", MySqlDbType.MediumBlob) { Value = send.IRContent, Size = send.IRContent.Length }; MySqlParameter paramPointsContent = new MySqlParameter("@PointsContent", MySqlDbType.VarChar) { Value = send.PointsContent }; MySqlParameter paramEvaluationContent = new MySqlParameter("@EvaluationContent", MySqlDbType.VarChar) { Value = send.EvaluationContent }; 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.UInt16) { Value = 1 }; MysqlHelper.ExecuteNonQuery(connection, "delete from TestPhotoFiles where RecId=@RecId", paramRecId); MysqlHelper.ExecuteNonQuery(connection, "insert into TestPhotoFiles (RecId,FileContent,IRContent,PointsContent,EvaluationContent,CreateBy,CreateTime,ModifyBy,ModifyTime,IsActive) values (@RecId,@FileContent,@IRContent,@PointsContent,@EvaluationContent,@CreateBy,@CreateTime,@ModifyBy,@ModifyTime,@IsActive)", new MySqlParameter[] { paramRecId, paramFileContent, paramIRContent, paramPointsContent, paramEvaluationContent, paramCreateBy, paramCreateTime, paramModifyBy, paramModifyTime, paramIsActive }); tranScope.Complete(); OutEntity oe = new OutEntity() { ErrorCode = ErrorCode.Success, ReturnObj = paramRecId.Value.ToString() }; return JsonConvert.SerializeObject(oe); } } } } public static string SaveResult(string currentUserId, string currentClientId, TestphotofilesEntity send) { lock(lockobj) { using (var tranScope = new TransactionScope()) { string connectionString = AppHelper.GetLinkString(); using (MySqlConnection connection = new MySqlConnection(connectionString)) { MySqlParameter paramRecId = new MySqlParameter("@RecId", MySqlDbType.VarChar) { Value = send.RecId }; MySqlParameter paramFileContent = new MySqlParameter("@FileContent", MySqlDbType.MediumBlob) { Value = send.FileContent }; MySqlParameter paramIRContent = new MySqlParameter("@IRContent", MySqlDbType.MediumBlob) { Value = send.IRContent, Size = send.IRContent.Length }; 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.UInt16) { Value = 1 }; MysqlHelper.ExecuteNonQuery(connection, "delete from TestPhotoFiles where RecId=@RecId", paramRecId); MysqlHelper.ExecuteNonQuery(connection, "insert into TestPhotoFiles (RecId,FileContent,IRContent,CreateBy,CreateTime,ModifyBy,ModifyTime,IsActive) values (@RecId,@FileContent,@IRContent,@CreateBy,@CreateTime,@ModifyBy,@ModifyTime,@IsActive)", new MySqlParameter[] { paramRecId, paramFileContent, paramIRContent, paramCreateBy, paramCreateTime, paramModifyBy, paramModifyTime, paramIsActive }); tranScope.Complete(); OutEntity oe = new OutEntity() { ErrorCode = ErrorCode.Success, ReturnObj = paramRecId.Value.ToString() }; return JsonConvert.SerializeObject(oe); } } } } public static byte[] ReadImgFile(string currentUserId, string currentClientId, string recid) { try { string connectionString = AppHelper.GetLinkString(); using (MySqlConnection connection = new MySqlConnection(connectionString)) { if (connection.State != ConnectionState.Open) connection.Open(); MySqlCommand cmd = new MySqlCommand(string.Format("select FileContent from TestPhotoFiles t where t.RecId = '{0}' and t.IsActive > 0 limit 1", recid), connection); MySqlDataReader reader = cmd.ExecuteReader(); reader.Read(); return (byte[])reader[0]; } } catch { } return null; } public static byte[] ReadIrFile(string currentUserId, string currentClientId, string recid) { try { string connectionString = AppHelper.GetLinkString(); using (MySqlConnection connection = new MySqlConnection(connectionString)) { if (connection.State != ConnectionState.Open) connection.Open(); MySqlCommand cmd = new MySqlCommand(string.Format("select IRContent from TestPhotoFiles t where t.RecId = '{0}' and t.IsActive > 0 limit 1", recid), connection); MySqlDataReader reader = cmd.ExecuteReader(); reader.Read(); return (byte[])reader[0]; } } catch { } return null; } 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, "delete from TestPhotoFiles where RecId=@RecId", paramRecId); } 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 ce = JsonConvert.DeserializeObject(se.Conditions, typeof(List)) as List; 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 rows = MysqlHelper.DataTableToIList(dt) as List; 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); } } }