/******************************************************************************* * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential * 类的信息: * 1.程序名称:CommonModuleLogic.cs * 2.功能描述:共通处理。 * 编辑履历: * 作者 日期 版本 修改内容 * 张国印 2014/09/04 1.00 新建 *******************************************************************************/ using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; using Dongke.IBOSS.PRD.Basics.BaseResources; using Dongke.IBOSS.PRD.Basics.DataAccess; using Dongke.IBOSS.PRD.Service.DataModels; using Dongke.IBOSS.PRD.WCF.DataModels; using Oracle.ManagedDataAccess.Client; namespace Dongke.IBOSS.PRD.Service.CommonModuleLogic { /// /// 共通处理业务逻辑 /// public static class CommonModuleLogic { /// /// 根据配置类型获取配置表中的数据 /// /// 配置类型编码 /// 用户基本信息 /// DataSet public static DataSet GetSysSettingBySettingType(string pSettingType, SUserInfo sUserInfo) { IDBConnection oracleConn = ClsDbFactory.CreateDBConnection(DataBaseType.ORACLE, DataManager.ConnectionString); try { #region 对应要执行的SQL语句 string strSql1 = "Select * From TP_MST_SYSTEMSETTING Where SettingCode = :settingType And accountid = :accountId"; Oracle.ManagedDataAccess.Client.OracleParameter[] paras1 = new Oracle.ManagedDataAccess.Client.OracleParameter[] { new Oracle.ManagedDataAccess.Client.OracleParameter(":settingType",pSettingType), new Oracle.ManagedDataAccess.Client.OracleParameter(":accountId",sUserInfo.AccountID) }; #endregion oracleConn.Open(); DataSet result = oracleConn.GetSqlResultToDs(strSql1, paras1); oracleConn.Close(); return result; } catch (Exception ex) { if (oracleConn.ConnState == ConnectionState.Open) { oracleConn.Close(); } throw ex; } finally { if (oracleConn.ConnState == ConnectionState.Open) { oracleConn.Close(); } } } /// /// 获取账务日期 /// /// 用户基本信息 /// DateTime public static DateTime GetAccountDate(SUserInfo sUserInfo) { IDBConnection oracleConn = ClsDbFactory.CreateDBConnection(DataBaseType.ORACLE, DataManager.ConnectionString); try { #region 对应要执行的SQL语句 string strSql1 = "select FUN_CMN_GetAccountDate(:accountId) From DUAL"; Oracle.ManagedDataAccess.Client.OracleParameter[] paras1 = new Oracle.ManagedDataAccess.Client.OracleParameter[] { new Oracle.ManagedDataAccess.Client.OracleParameter(":accountId",sUserInfo.AccountID) }; #endregion oracleConn.Open(); object strResult = oracleConn.GetSqlResultToObj(strSql1, paras1); if (strResult == null || strResult == DBNull.Value) { // 服务器时间错误 throw new Exception("SystemDateTimeError"); } oracleConn.Close(); DateTime result = Convert.ToDateTime(strResult); return result; } catch (Exception ex) { if (oracleConn.ConnState == ConnectionState.Open) { oracleConn.Close(); } throw ex; } finally { if (oracleConn.ConnState == ConnectionState.Open) { oracleConn.Close(); } } } /// /// 获取账务日期(服务端用) /// /// 事务实例 /// 用户基本信息 /// DateTime public static DateTime GetAccountDate(IDBTransaction oracleTrConn, SUserInfo sUserInfo) { try { #region 对应要执行的SQL语句 string strSql1 = "select FUN_CMN_GetAccountDate(:accountId) From DUAL"; Oracle.ManagedDataAccess.Client.OracleParameter[] paras1 = new Oracle.ManagedDataAccess.Client.OracleParameter[] { new Oracle.ManagedDataAccess.Client.OracleParameter(":accountId",sUserInfo.AccountID) }; #endregion object strResult = oracleTrConn.GetSqlResultToObj(strSql1, paras1); if (strResult == null || strResult == DBNull.Value) { // 服务器时间错误 throw new Exception("SystemDateTimeError"); } DateTime result = Convert.ToDateTime(strResult); return result; } catch (Exception ex) { throw ex; } } /// /// 获得系统日期 /// /// 系统日期类型 /// 用户基本信息 /// DateTime public static DateTime GetSystemDate(Constant.SystemDateType systemDateType, SUserInfo sUserInfo) { try { IDBConnection oracleConn = ClsDbFactory.CreateDBConnection(DataBaseType.ORACLE, DataManager.ConnectionString); try { #region 对应要执行的SQL语句 string strSql = "select SystemDateType,DateValue From TP_MST_SystemDate where AccountID=:accountID"; OracleParameter[] paras = new OracleParameter[] { new Oracle.ManagedDataAccess.Client.OracleParameter(":accountId",sUserInfo.AccountID) }; DataTable dt = oracleConn.GetSqlResultToDt(strSql, paras); if (dt != null && dt.Rows.Count > Constant.INT_IS_ZERO) { DataRow[] newRow = dt.Select("SystemDateType=" + systemDateType.GetHashCode()); if (newRow != null && newRow.Length > Constant.INT_IS_ZERO) { return DateTime.Parse(newRow[0]["DateValue"].ToString()); } else { if (systemDateType == Constant.SystemDateType.OnlineDate) { // 服务器时间错误 throw new Exception("SystemDateTimeError"); } else { newRow = dt.Select("SystemDateType=" + Constant.SystemDateType.OnlineDate.GetHashCode()); if (newRow != null && newRow.Length > Constant.INT_IS_ZERO) { return DateTime.Parse(newRow[0]["DateValue"].ToString()); } else { // 服务器时间错误 throw new Exception("SystemDateTimeError"); } } } } else { // 服务器时间错误 throw new Exception("SystemDateTimeError"); } #endregion } catch (Exception ex) { throw ex; } finally { if (oracleConn.ConnState == ConnectionState.Open) { oracleConn.Close(); } } } catch (Exception ex) { throw ex; } } /// /// 获取该帐套下的所有职务信息,用于数据选择 /// /// 用户基本信息 /// DataSet public static DataSet GetMSTPostInfo(SUserInfo sUserInfo) { IDBConnection oracleConn = ClsDbFactory.CreateDBConnection(DataBaseType.ORACLE, DataManager.ConnectionString); try { #region 对应要执行的SQL语句 string strSql1 = "SELECT PostID,PostCode,PostName,Remarks FROM TP_MST_Post WHERE accountid = :in_accountid AND valueflag = 1"; Oracle.ManagedDataAccess.Client.OracleParameter[] paras1 = new Oracle.ManagedDataAccess.Client.OracleParameter[] { new Oracle.ManagedDataAccess.Client.OracleParameter(":in_accountid",sUserInfo.AccountID) }; #endregion oracleConn.Open(); DataSet dsPost = oracleConn.GetSqlResultToDs(strSql1, paras1); oracleConn.Close(); return dsPost; } catch (Exception ex) { throw ex; } finally { if (oracleConn.ConnState == ConnectionState.Open) { oracleConn.Close(); } } } #region 与数据库交换的共通方法 /// /// 查询数据字典by类别 /// /// 用户基本信息 /// 字典类型 /// DataTable public static DataTable GetDataDictionaryByType(SUserInfo sUserInfo, string dictionaryType) { IDBConnection con = ClsDbFactory.CreateDBConnection(DataBaseType.ORACLE, DataManager.ConnectionString); try { con.Open(); OracleParameter[] paras = new OracleParameter[]{ new OracleParameter("accountID",sUserInfo.AccountID), new OracleParameter("dictionaryType",dictionaryType), new OracleParameter("valueFlag",OracleDbType.Int32,1,ParameterDirection.Input), new OracleParameter("rs_Dic",OracleDbType.RefCursor), }; paras[3].Direction = ParameterDirection.Output; DataSet ds = con.ExecStoredProcedure("PRO_MST_GetDataDictionary", paras); return ds.Tables[0]; } catch (Exception ex) { throw ex; } finally { if (con.ConnState == ConnectionState.Open) { con.Close(); } } } #endregion /// /// 获取服务器上的模板文件 /// /// 模板文件名称 /// TempletFileEntity实体类 public static TempletFileEntity GetTempletFileContentByUrl(string pFileName) { try { TempletFileEntity templetFileEntity = new TempletFileEntity(); templetFileEntity.FileName = string.Empty; string path = System.AppDomain.CurrentDomain.BaseDirectory + Constant.SYSTEM_TEMPLET_FILE_PATH; string strFullPath = path + "\\" + pFileName; if (!System.IO.Directory.Exists(path)) { return templetFileEntity; } if (!System.IO.File.Exists(strFullPath)) { return templetFileEntity; } templetFileEntity.FileName = pFileName; templetFileEntity.FileContent = System.IO.File.ReadAllBytes(strFullPath); return templetFileEntity; } catch (Exception ex) { throw ex; } } /// /// 上传图片 /// /// 模块名称 /// 时间 /// 图片 /// string public static string UpLoadImg(string module, DateTime dateTime, Image image) { string dbpath = Constant.SYSTEM_IMAGE_PATH + "/" + module + "/" + dateTime.ToString("yyyyMM/dd/HHmmssfff"); string path = System.AppDomain.CurrentDomain.BaseDirectory + dbpath; // 路径不存在 需要创建 if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string filename = "/" + System.Guid.NewGuid().ToString().ToUpper() + Constant.SYSTEM_IMAGE_FORMAT; dbpath += filename; path += filename; image.Save(path); return dbpath; } /// /// 上传图片 /// /// 模块名称 /// 时间 /// 图片 /// string public static string UpLoadImg(string module, DateTime dateTime, byte[] images) { MemoryStream ms = new MemoryStream(images); Image img = Image.FromStream(ms); return UpLoadImg(module, dateTime, img); } /// /// 移动图片 /// /// 模块名称 /// 当前时间 /// 路径 /// string public static string MoveImg(string module, DateTime dateTime, string path) { try { string filepath = System.AppDomain.CurrentDomain.BaseDirectory + path; if (!File.Exists(filepath)) { return ""; } string dbpath = Constant.SYSTEM_IMAGE_PATH + "/" + module + "/" + dateTime.ToString("yyyyMM/dd/HHmmssfff"); string newPath = System.AppDomain.CurrentDomain.BaseDirectory + dbpath; // 路径不存在 需要创建 if (!Directory.Exists(newPath)) { Directory.CreateDirectory(newPath); } string filename = "/" + System.Guid.NewGuid().ToString().ToUpper() + Constant.SYSTEM_IMAGE_FORMAT; dbpath += filename; newPath += filename; File.Move(filepath, newPath); return dbpath; } //catch (Exception ex) catch { throw; } } /// /// 重绘缩略图 /// /// /// Image public static Image ConvertThumbnail(Image imgSource) { ImageFormat thisFormat = imgSource.RawFormat; int sW = 0, sH = 0; // 按比例缩放 int sWidth = imgSource.Width; int sHeight = imgSource.Height; int destWidth = 100; int destHeight = GetSmallImageHeight(sWidth, sHeight, destWidth); if (sHeight > destHeight || sWidth > destWidth) { if ((sWidth * destHeight) > (sHeight * destWidth)) { sW = destWidth; sH = (destWidth * sHeight) / sWidth; } else { sH = destHeight; sW = (sWidth * destHeight) / sHeight; } } else { sW = sWidth; sH = sHeight; } Bitmap outBmp = new Bitmap(destWidth, destHeight); Graphics g = Graphics.FromImage(outBmp); g.Clear(Color.Black); // 设置画布的描绘质量 g.CompositingQuality = CompositingQuality.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(imgSource, new Rectangle((destWidth - sW) / 2, (destHeight - sH) / 2, sW, sH), 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel); g.Dispose(); imgSource.Dispose(); return outBmp; } /// /// 重绘缩略图 /// /// /// byte[] public static byte[] ConvertThumbnail(byte[] imgSource) { if (imgSource == null) { return null; } Image img; MemoryStream ms = new MemoryStream(imgSource); img = System.Drawing.Image.FromStream(ms); ms.Flush(); Image newImg = ConvertThumbnail(img); #region Image to byte byte[] data = null; using (MemoryStream msNew = new MemoryStream()) { using (Bitmap Bitmap = new Bitmap(newImg)) { Bitmap.Save(msNew, ImageFormat.Jpeg); msNew.Position = 0; data = new byte[ms.Length]; msNew.Read(data, 0, Convert.ToInt32(ms.Length)); msNew.Flush(); } } #endregion return data; } /// /// 根据原图片宽高比获取缩略图的高(根据宽) /// /// 大图宽 /// 大图高 /// 小图宽 /// int public static int GetSmallImageHeight(int BigWidth, int BigHeight, int SmallWidth) { decimal scale = Convert.ToDecimal(BigWidth) / Convert.ToDecimal(BigHeight); return Convert.ToInt32(SmallWidth / scale); } /// /// 重绘缩略图 /// /// 模块名称 /// 时间 /// 路径 /// string public static string ConvertThumbnail(string module, DateTime dateTime, string path) { path = System.AppDomain.CurrentDomain.BaseDirectory + path; if (!File.Exists(path)) { return ""; } Image img = Image.FromFile(path); Image imgThumbnail = ConvertThumbnail(img); string imgPath = UpLoadImg(module, dateTime, imgThumbnail); return imgPath; } /// /// 重绘缩略图 /// /// 模块名称 /// 时间 /// 字节数组 /// string public static string ConvertThumbnail(string module, DateTime dateTime, byte[] buffer) { MemoryStream ms = new MemoryStream(buffer); Image image = System.Drawing.Image.FromStream(ms); Image imgThumbnail = ConvertThumbnail(image); string imgPath = UpLoadImg(module, dateTime, imgThumbnail); return imgPath; } /// /// 上传附件 /// /// 模块名称 /// 时间 /// 文件类型 /// 文件二进制 /// string public static string UpLoadFile(string module, DateTime dateTime, string fileType, byte[] fileByte) { try { string dbpath = Constant.SYSTEM_IMAGE_PATH + "/" + module + "/" + dateTime.ToString("yyyyMM/dd/HHmmssfff"); string path = System.AppDomain.CurrentDomain.BaseDirectory + dbpath; // 路径不存在 需要创建 if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string filename = "/" + System.Guid.NewGuid().ToString().ToUpper() + Constant.SYSTEM_IMAGE_FORMAT; dbpath += filename; path += filename; FileStream fs = new FileStream(path, FileMode.Create); fs.Write(fileByte, 0, fileByte.Length); return dbpath; } catch (Exception ex) { throw ex; } } /// /// 下载附件 /// /// 附件路径 /// byte[] public static byte[] DownloadFile(string filePath) { try { filePath = System.AppDomain.CurrentDomain.BaseDirectory + filePath; byte[] byteData = File.ReadAllBytes(filePath); //如果字节数大于0,则转码 if (byteData.Length > 0) { return byteData; } return null; } catch (Exception ex) { throw ex; } } #region 转换为注浆条码 /// /// 转换为注浆条码 /// /// /// /// /// public static string GetBarcode(IDBConnection conn, string barcode, SUserInfo user) { // 转换条码 string sqlString = @"select FUN_CMN_GetBarCode(:barcode,null,:accountid) From DUAL"; OracleParameter[] paras1 = new OracleParameter[]{ new OracleParameter(":barcode",OracleDbType.Varchar2, barcode,ParameterDirection.Input), new OracleParameter(":accountid",OracleDbType.Int32, user.AccountID,ParameterDirection.Input), }; barcode = conn.GetSqlResultToStr(sqlString, paras1); return barcode; } /// /// 转换为注浆条码 /// /// /// /// /// public static string GetBarcode(IDBTransaction tran, string barcode, SUserInfo user) { // 转换条码 string sqlString = @"select FUN_CMN_GetBarCode(:barcode,null,:accountid) From DUAL"; OracleParameter[] paras1 = new OracleParameter[]{ new OracleParameter(":barcode",OracleDbType.Varchar2, barcode,ParameterDirection.Input), new OracleParameter(":accountid",OracleDbType.Int32, user.AccountID,ParameterDirection.Input), }; barcode = tran.GetSqlResultToStr(sqlString, paras1); return barcode; } #endregion } }