using System; using System.Data; using System.Collections.Generic; using System.Text; using DSWeb.MvcShipping.Models.MsCompanys; using Microsoft.Practices.EnterpriseLibrary.Data; using DSWeb.EntityDA; using DSWeb.Areas.CommMng.Models; using HcUtility.Comm; using System.Data.SqlClient; using System.IO; namespace DSWeb.MvcShipping.DAL.MsCompanysDAL { public class MsCompanysDAL { #region 查询 static public List GetDataList(string strPath, string strPhotoPath, string strCondition, string userid, string usercode, string companyid, string sort = null) { var rangstr = GetRangDAStr("index", userid, usercode, companyid); if (!string.IsNullOrEmpty(rangstr)) { if (!string.IsNullOrEmpty(strCondition)) { strCondition = strCondition + " and " + rangstr; } else { strCondition = rangstr; } } // String strSql = "SELECT *,MODIFIEDUSERNAME=(select top 1 SHOWNAME from [user] where gid=[company].MODIFIEDUSER) FROM [company] "; strSql = strSql + " where (ISDELETED=0 or ISDELETED is null) "; //and (ISDISABLE=0 or ISDISABLE is null) if (!string.IsNullOrEmpty(strCondition)) { strSql += " and " + strCondition; } var sortstring = DatasetSort.Getsortstring(sort); if (!string.IsNullOrEmpty(sortstring)) { strSql += " order by " + sortstring; } else { strSql += " order by [CODENAME],[NAME]"; } return SetData(strPath, strPhotoPath, strSql); } static public string GetDataListStr(string strCondition, string userid, string usercode, string companyid, string sort = null) { var rangstr = GetRangDAStr("index", userid, usercode, companyid); if (!string.IsNullOrEmpty(rangstr)) { if (!string.IsNullOrEmpty(strCondition)) { strCondition = strCondition + " and " + rangstr; } else { strCondition = rangstr; } } // String strSql = "SELECT *,MODIFIEDUSERNAME=(select top 1 SHOWNAME from [user] where gid=[company].MODIFIEDUSER) FROM [company] where (ISDELETED=0 or ISDELETED is null) and (ISDISABLE=0 or ISDISABLE is null)"; if (!string.IsNullOrEmpty(strCondition)) { strSql += " and " + strCondition; } var sortstring = DatasetSort.Getsortstring(sort); if (!string.IsNullOrEmpty(sortstring)) { strSql += " order by " + sortstring; } else { strSql += " order by [CODENAME],[NAME]"; } return strSql.ToString(); } static public MsCompanysEntity GetData(string strPath, string strPhotoPath, string strCondition) { String strSql = "SELECT *,MODIFIEDUSERNAME=(select top 1 SHOWNAME from [user] where gid=[company].MODIFIEDUSER) FROM [company] where (ISDELETED=0 or ISDELETED is null) ";//and (ISDISABLE=0 or ISDISABLE is null) if (!string.IsNullOrEmpty(strCondition)) { strSql += " and " + strCondition; } var list = SetData(strPath, strPhotoPath, strSql); if (list.Count > 0) { return list[0]; } return new MsCompanysEntity(); } static public List GetNoPicDataList(string strCondition) { // String strSql = "SELECT *,CODENAME + '-' + NAME AS CodeAndName,MODIFIEDUSERNAME=(select top 1 SHOWNAME from [user] where gid=[company].MODIFIEDUSER) FROM [company] "; strSql = strSql + " where (ISDELETED=0 or ISDELETED is null) "; //and (ISDISABLE=0 or ISDISABLE is null) if (!string.IsNullOrEmpty(strCondition)) { strSql += " and " + strCondition; } strSql += " order by [CODENAME],[NAME]"; return SetNoPicData(strSql); } static public MsCompanysEntity GetNoPicData(string strCondition) { String strSql = "SELECT *,CODENAME + '-' + NAME AS CodeAndName,MODIFIEDUSERNAME=(select top 1 SHOWNAME from [user] where gid=[company].MODIFIEDUSER) FROM [company] where (ISDELETED=0 or ISDELETED is null) and (ISDISABLE=0 or ISDISABLE is null)"; if (!string.IsNullOrEmpty(strCondition)) { strSql += " and " + strCondition; } var list = SetNoPicData(strSql); if (list.Count > 0) { return list[0]; } return new MsCompanysEntity(); } private static List SetData(string strPath,string strPhotoPath, String strSql) { var headList = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { MsCompanysEntity data = new MsCompanysEntity(); #region Set DB data to Object data.GID = (reader["GID"] == null ? "" : Convert.ToString(reader["GID"]));//GID data.CODENAME = (reader["CODENAME"] == null ? "" : Convert.ToString(reader["CODENAME"]));//CODENAME data.NAME = (reader["NAME"] == null ? "" : Convert.ToString(reader["NAME"]));//NAME data.FULLNAME = (reader["FULLNAME"] == null ? "" : Convert.ToString(reader["FULLNAME"]));//FULLNAME data.ENNAME = (reader["ENNAME"] == null ? "" : Convert.ToString(reader["ENNAME"]));//公司英文名称 data.ADDRESS = (reader["ADDRESS"] == null ? "" : Convert.ToString(reader["ADDRESS"]));//ADDRESS data.ENADDRESS = (reader["ENADDRESS"] == null ? "" : Convert.ToString(reader["ENADDRESS"]));//公司英文地址 data.POSTCODE = (reader["POSTCODE"] == null ? "" : Convert.ToString(reader["POSTCODE"]));//POSTCODE data.OFFICEPHONE = (reader["OFFICEPHONE"] == null ? "" : Convert.ToString(reader["OFFICEPHONE"]));//OFFICEPHONE data.FAX = (reader["FAX"] == null ? "" : Convert.ToString(reader["FAX"]));//FAX data.EMAIL = (reader["EMAIL"] == null ? "" : Convert.ToString(reader["EMAIL"]));//EMAIL data.WEBSITEURL = (reader["WEBSITEURL"] == null ? "" : Convert.ToString(reader["WEBSITEURL"]));//WEBSITEURL data.LICENSECODE = (reader["LICENSECODE"] == null ? "" : Convert.ToString(reader["LICENSECODE"]));//LICENSECODE data.TAXCODE = (reader["TAXCODE"] == null ? "" : Convert.ToString(reader["TAXCODE"]));//TAXCODE data.CREATEUSER = (reader["CREATEUSER"] == null ? "" : Convert.ToString(reader["CREATEUSER"]));//CREATEUSER if (reader["CREATETIME"] != DBNull.Value) data.CREATETIME = Convert.ToDateTime(reader["CREATETIME"]); data.MODIFIEDUSER = (reader["MODIFIEDUSER"] == null ? "" : Convert.ToString(reader["MODIFIEDUSER"]));//更新人gid if (reader["MODIFIEDTIME"] != DBNull.Value) data.MODIFIEDTIME = Convert.ToDateTime(reader["MODIFIEDTIME"]); data.ISDELETED = (reader["ISDELETED"] == null ? false : Convert.ToBoolean(reader["ISDELETED"]));//ISDELETED data.ISDISABLE = (reader["ISDISABLE"] == null ? false : Convert.ToBoolean(reader["ISDISABLE"]));//ISDISABLE data.PARENTID = (reader["PARENTID"] == null ? "" : Convert.ToString(reader["PARENTID"]));//PARENTID data.BILLRISES = (reader["BILLRISES"] == null ? "" : Convert.ToString(reader["BILLRISES"]));//发票抬头 data.CHEQUEPAYABLE = (reader["CHEQUEPAYABLE"] == null ? "" : Convert.ToString(reader["CHEQUEPAYABLE"]));//支票抬头 data.PRTHEADXML1 = (reader["PRTHEADXML1"] == null ? "" : Convert.ToString(reader["PRTHEADXML1"]));//打印抬头1 data.PRTHEADXML2 = (reader["PRTHEADXML2"] == null ? "" : Convert.ToString(reader["PRTHEADXML2"]));//打印抬头2 data.PRTHEADXML3 = (reader["PRTHEADXML3"] == null ? "" : Convert.ToString(reader["PRTHEADXML3"]));//打印抬头3 data.PRTHEADXML4 = (reader["PRTHEADXML4"] == null ? "" : Convert.ToString(reader["PRTHEADXML4"]));//打印抬头4 data.PRTHEADXML5 = (reader["PRTHEADXML5"] == null ? "" : Convert.ToString(reader["PRTHEADXML5"]));//打印抬头5 data.BANKSHEAD = (reader["BANKSHEAD"] == null ? "" : Convert.ToString(reader["BANKSHEAD"]));//BANKSHEAD data.GPSUser = (reader["GPSUser"] == null ? "" : Convert.ToString(reader["GPSUser"]));//GPSUser data.GPSPassWord = (reader["GPSPassWord"] == null ? "" : Convert.ToString(reader["GPSPassWord"]));//GPSPassWord data.ORGANIZATIONCODE = (reader["ORGANIZATIONCODE"] == null ? "" : Convert.ToString(reader["ORGANIZATIONCODE"])); data.MODIFIEDUSERNAME = (reader["MODIFIEDUSERNAME"] == null ? "" : Convert.ToString(reader["MODIFIEDUSERNAME"]));//更改人 data.LOGO = null;//徽标 if (reader["LOGO"] != DBNull.Value) { data.LOGO = (byte[])reader["LOGO"];//徽标 } data.LOGOURL = getLOGO(data.LOGO, data.GID, strPath, strPhotoPath); ;//徽标 data.LOCALCURR = (reader["LOCALCURR"] == null ? "" : Convert.ToString(reader["LOCALCURR"]));//本地货币 data.WORKFLOWMSG = (reader["WORKFLOWMSG"] == null ? "" : Convert.ToString(reader["WORKFLOWMSG"]));//是否消息提示 #endregion headList.Add(data); } reader.Close(); } return headList; } private static List SetNoPicData(String strSql) { var headList = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { MsCompanysEntity data = new MsCompanysEntity(); #region Set DB data to Object data.GID = (reader["GID"] == null ? "" : Convert.ToString(reader["GID"]));//GID data.CodeAndName = Convert.ToString(reader["CodeAndName"]); data.CODENAME = (reader["CODENAME"] == null ? "" : Convert.ToString(reader["CODENAME"]));//CODENAME data.NAME = (reader["NAME"] == null ? "" : Convert.ToString(reader["NAME"]));//NAME data.FULLNAME = (reader["FULLNAME"] == null ? "" : Convert.ToString(reader["FULLNAME"]));//FULLNAME data.ENNAME = (reader["ENNAME"] == null ? "" : Convert.ToString(reader["ENNAME"]));//公司英文名称 data.ADDRESS = (reader["ADDRESS"] == null ? "" : Convert.ToString(reader["ADDRESS"]));//ADDRESS data.ENADDRESS = (reader["ENADDRESS"] == null ? "" : Convert.ToString(reader["ENADDRESS"]));//公司英文地址 data.POSTCODE = (reader["POSTCODE"] == null ? "" : Convert.ToString(reader["POSTCODE"]));//POSTCODE data.OFFICEPHONE = (reader["OFFICEPHONE"] == null ? "" : Convert.ToString(reader["OFFICEPHONE"]));//OFFICEPHONE data.FAX = (reader["FAX"] == null ? "" : Convert.ToString(reader["FAX"]));//FAX data.EMAIL = (reader["EMAIL"] == null ? "" : Convert.ToString(reader["EMAIL"]));//EMAIL data.WEBSITEURL = (reader["WEBSITEURL"] == null ? "" : Convert.ToString(reader["WEBSITEURL"]));//WEBSITEURL data.LICENSECODE = (reader["LICENSECODE"] == null ? "" : Convert.ToString(reader["LICENSECODE"]));//LICENSECODE data.TAXCODE = (reader["TAXCODE"] == null ? "" : Convert.ToString(reader["TAXCODE"]));//TAXCODE data.CREATEUSER = (reader["CREATEUSER"] == null ? "" : Convert.ToString(reader["CREATEUSER"]));//CREATEUSER if (reader["CREATETIME"] != DBNull.Value) data.CREATETIME = Convert.ToDateTime(reader["CREATETIME"]); data.MODIFIEDUSER = (reader["MODIFIEDUSER"] == null ? "" : Convert.ToString(reader["MODIFIEDUSER"]));//更新人gid if (reader["MODIFIEDTIME"] != DBNull.Value) data.MODIFIEDTIME = Convert.ToDateTime(reader["MODIFIEDTIME"]); data.ISDELETED = (reader["ISDELETED"] == null ? false : Convert.ToBoolean(reader["ISDELETED"]));//ISDELETED data.ISDISABLE = (reader["ISDISABLE"] == null ? false : Convert.ToBoolean(reader["ISDISABLE"]));//ISDISABLE data.PARENTID = (reader["PARENTID"] == null ? "" : Convert.ToString(reader["PARENTID"]));//PARENTID data.BILLRISES = (reader["BILLRISES"] == null ? "" : Convert.ToString(reader["BILLRISES"]));//发票抬头 data.CHEQUEPAYABLE = (reader["CHEQUEPAYABLE"] == null ? "" : Convert.ToString(reader["CHEQUEPAYABLE"]));//支票抬头 data.PRTHEADXML1 = (reader["PRTHEADXML1"] == null ? "" : Convert.ToString(reader["PRTHEADXML1"]));//打印抬头1 data.PRTHEADXML2 = (reader["PRTHEADXML2"] == null ? "" : Convert.ToString(reader["PRTHEADXML2"]));//打印抬头2 data.PRTHEADXML3 = (reader["PRTHEADXML3"] == null ? "" : Convert.ToString(reader["PRTHEADXML3"]));//打印抬头3 data.PRTHEADXML4 = (reader["PRTHEADXML4"] == null ? "" : Convert.ToString(reader["PRTHEADXML4"]));//打印抬头4 data.PRTHEADXML5 = (reader["PRTHEADXML5"] == null ? "" : Convert.ToString(reader["PRTHEADXML5"]));//打印抬头5 data.BANKSHEAD = (reader["BANKSHEAD"] == null ? "" : Convert.ToString(reader["BANKSHEAD"]));//BANKSHEAD data.GPSUser = (reader["GPSUser"] == null ? "" : Convert.ToString(reader["GPSUser"]));//GPSUser data.GPSPassWord = (reader["GPSPassWord"] == null ? "" : Convert.ToString(reader["GPSPassWord"]));//GPSPassWord data.ORGANIZATIONCODE = (reader["ORGANIZATIONCODE"] == null ? "" : Convert.ToString(reader["ORGANIZATIONCODE"])); data.MODIFIEDUSERNAME = (reader["MODIFIEDUSERNAME"] == null ? "" : Convert.ToString(reader["MODIFIEDUSERNAME"]));//更改人 data.DBNAME = Convert.ToString(reader["DBNAME"]); data.LOCALCURR = (reader["LOCALCURR"] == null ? "" : Convert.ToString(reader["LOCALCURR"]));//本地货币 data.WORKFLOWMSG = (reader["WORKFLOWMSG"] == null ? "" : Convert.ToString(reader["WORKFLOWMSG"]));//是否消息提示 #endregion headList.Add(data); } reader.Close(); } return headList; } protected static string getLOGO(byte[] photo,string sGID,string strPath,string strPhotoPath) { //图片路径 //string strPath = "../../images/logo/" + sGID.Trim() + ".gif"; //string strPhotoPath = Server.MapPath(strPath); if (sGID.Trim() != "" && photo!=null) { strPath = strPath.Replace("noImage", sGID.Trim()); strPhotoPath = strPhotoPath.Replace("noImage", sGID.Trim()); //保存图片文件 BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath, FileMode.OpenOrCreate)); bw.Write(photo); bw.Close(); } //显示图片 return strPath; } #endregion #region Rang权限范围 public static string GetRangDAStr(string tb, string userid, string username, string companyid) { string str = ""; var strSql = new StringBuilder(); strSql.Append("SELECT"); strSql.Append(" VISIBLERANGE,OPERATERANGE"); strSql.Append(" from VW_User_Authority"); strSql.Append(" where [NAME]='modCompanys' and USERID='" + userid + "' and ISDELETE=0"); string visiblerange = "4"; string operaterange = "4"; Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { visiblerange = Convert.ToString(reader["VISIBLERANGE"]); operaterange = Convert.ToString(reader["OPERATERANGE"]); break; } reader.Close(); } if (visiblerange == "4")//无 { str = " 1<0"; } else if (visiblerange == "3")//个人 { str = " 1<0"; } else if (visiblerange == "2")//部门 { str = " 1<0"; } else if (visiblerange == "1")//公司 { str = " GID='" + companyid + "'"; } else if (visiblerange == "0")//全部 { str = " 1=1 "; } return str; } #endregion #region 添加日志 public static DBResult setLog(MsCompanysEntity modeldata, string strUserID) { DBResult result = new DBResult(); return result; } #endregion #region 保存 public static DBResult Save(MsCompanysEntity headData) { DBResult result = new DBResult(); result.Success = true; result.Message = "操作成功"; return result; } #endregion #region 删除 public static DBResult Delete(MsCompanysEntity headData) { DBResult result = new DBResult(); T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA(); string strSql = "SELECT top 1 LINKID as CORPID from sys_bank where LINKID in ('" + headData.GID + "') group by LINKID"; string strCORPID = T_ALL_DA.GetStrSQL("CORPID", strSql); if (strCORPID.Trim() != "") { result.Success = false; result.Message = "企业拥有银行信息,请先删除!"; return result; } // strSql = "SELECT top 1 LINKID as CORPID from sys_dept where LINKID in ('" + headData.GID + "') group by LINKID"; strCORPID = T_ALL_DA.GetStrSQL("CORPID", strSql); if (strCORPID.Trim() != "") { result.Success = false; result.Message = "企业拥有部门信息,请先删除!"; return result; } // strSql = "SELECT top 1 CORPID from VW_Settlement where CORPID in ('" + headData.GID + "') group by CORPID"; strCORPID = T_ALL_DA.GetStrSQL("CORPID", strSql); if (strCORPID.Trim() != "") { result.Success = false; result.Message = "企业拥有业务信息,请先删除!"; return result; } // Database db = DatabaseFactory.CreateDatabase(); using (var conn = db.CreateConnection()) { conn.Open(); var tran = conn.BeginTransaction(); try { //var cmdDelete = db.GetSqlStringCommand("delete from [company] where GID='" + headData.GID + "'"); var cmdDelete = db.GetSqlStringCommand("update [company] set ISDELETED=1 where GID='" + headData.GID + "'"); db.ExecuteNonQuery(cmdDelete, tran); tran.Commit(); } catch (Exception) { tran.Rollback(); result.Success = false; result.Message = "删除出现错误,请重试"; return result; } } result.Success = true; result.Message = "操作成功"; return result; } #endregion } }