using System; using System.Data; using System.Collections.Generic; using System.Text; using DSWeb.MvcShipping.Models.MsCodeGoods; using Microsoft.Practices.EnterpriseLibrary.Data; using DSWeb.Areas.CommMng.Models; using HcUtility.Comm; namespace DSWeb.MvcShipping.DAL.MsCodeGoods { public class MsCodeGoodsDAL { #region Inquery DataList static public List GetDataList(string strCondition, string userid, string usercode, string orgcode, string sort = null) { //var rangstr = GetRangDAStr("", userid, usercode, orgcode); //if (!string.IsNullOrEmpty(rangstr)) //{ // if (!string.IsNullOrEmpty(strCondition)) // { // strCondition = strCondition + " and " + rangstr; // } // else // { // strCondition = rangstr; // } //} var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append("[GID],[GOODCODE],[GOODNAME],GOODNO,[DESCRIP],[ARRATE],[APRATE],[AROUTRATE],[APOUTRATE],[ISSTOP],[GoodsTypeGID]"); strSql.Append(",(SELECT GoodsTypeName from code_goodsType where GID=GoodsTypeGID) GoodsType,ISSTOP"); strSql.Append(",CREATEUSER,CREATETIME,CORPID"); strSql.Append(",(select ShowName from [user] where GID=code_goods.CREATEUSER) as CREATEUSERREF"); strSql.Append(",GOODCODE+'-'+GOODNAME as CODEANDNAME"); strSql.Append(" from code_goods "); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" where " + strCondition); } var sortstring = DatasetSort.Getsortstring(sort); if (!string.IsNullOrEmpty(sortstring)) { strSql.Append(" order by " + sortstring); } else { strSql.Append(" order by GOODNAME"); } return SetData(strSql); } static public CodeGoods GetData(string condition, string userid, string usercode, string orgcode) { CodeGoods data = null; var list = GetDataList(condition,userid,usercode,orgcode); if (list.Count > 0) data = list[0]; if (data == null) { data = new CodeGoods(); } return data; } private static List SetData(StringBuilder strSql) { var headList = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { CodeGoods data = new CodeGoods(); #region Set DB data to Object data.GID = Convert.ToString(reader["GID"]); data.GOODCODE = Convert.ToString(reader["GOODCODE"]); data.GOODNAME = Convert.ToString(reader["GOODNAME"]); data.CODEANDNAME = Convert.ToString(reader["CODEANDNAME"]); data.GOODNO = Convert.ToString(reader["GOODNO"]); data.DESCRIP = Convert.ToString(reader["DESCRIP"]); if (reader["ARRATE"] != DBNull.Value) data.ARRATE = Convert.ToDecimal(reader["ARRATE"]); if (reader["APRATE"] != DBNull.Value) data.APRATE = Convert.ToDecimal(reader["APRATE"]); if (reader["AROUTRATE"] != DBNull.Value) data.AROUTRATE = Convert.ToDecimal(reader["AROUTRATE"]); if (reader["APOUTRATE"] != DBNull.Value) data.APOUTRATE = Convert.ToDecimal(reader["APOUTRATE"]); data.GOODSTYPE = Convert.ToString(reader["GOODSTYPE"]); data.GOODSTYPEGID = Convert.ToString(reader["GOODSTYPEGID"]); if (reader["ISSTOP"] != DBNull.Value) if (Convert.ToBoolean(reader["ISSTOP"]) == true) data.ISSTOP = "1"; data.CORPID = Convert.ToString(reader["CORPID"]); data.CREATEUSER = Convert.ToString(reader["CREATEUSER"]); data.CREATEUSERREF = Convert.ToString(reader["CREATEUSERREF"]); if (reader["CREATETIME"] != DBNull.Value) data.CREATETIME = Convert.ToDateTime(reader["CREATETIME"]); #endregion headList.Add(data); } reader.Close(); } return headList; } #endregion #region 明细表 static public List GetBodyList(string strCondition, string sort = null) { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append("GID,LINKGID,GOODSDETAILNAME,GOODSDETAILPKGS,REMARK"); strSql.Append(" from code_goods_detail "); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" where " + strCondition); } var sortstring = DatasetSort.Getsortstring(sort); if (!string.IsNullOrEmpty(sortstring)) { strSql.Append(" order by " + sortstring); } else { strSql.Append(" order by GOODSDETAILNAME "); } return SetBodyData(strSql); } private static List SetBodyData(StringBuilder strSql) { var bodyList = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { CodeGoodsDetail data = new CodeGoodsDetail(); #region Set DB data to Object data.GID = Convert.ToString(reader["GID"]); data.LINKGID = Convert.ToString(reader["LINKGID"]); data.GOODSDETAILNAME = Convert.ToString(reader["GOODSDETAILNAME"]); data.REMARK = Convert.ToString(reader["REMARK"]); if (reader["GOODSDETAILPKGS"] != DBNull.Value) data.GOODSDETAILPKGS = Convert.ToDecimal(reader["GOODSDETAILPKGS"]); #endregion bodyList.Add(data); } reader.Close(); } return bodyList; } public static DBResult SaveDetail(CodeGoods headData, List bodyList, string userid) { var result = new DBResult(); Database db = DatabaseFactory.CreateDatabase(); using (var conn = db.CreateConnection()) { conn.Open(); var tran = conn.BeginTransaction(); try { var cmdDelete = db.GetSqlStringCommand("delete from code_goods_detail where LINKGID='" + headData.GID + "'"); db.ExecuteNonQuery(cmdDelete, tran); var cmdInsert = db.GetSqlStringCommand( @"insert into code_goods_detail (GID,LINKGID,GOODSDETAILNAME,REMARK,GOODSDETAILPKGS) values (@GID,@LINKGID,@GOODSDETAILNAME,@REMARK,@GOODSDETAILPKGS) "); if (bodyList != null) { foreach (var enumValue in bodyList) { cmdInsert.Parameters.Clear(); db.AddInParameter(cmdInsert, "@GID", DbType.String, Guid.NewGuid().ToString()); db.AddInParameter(cmdInsert, "@LINKGID", DbType.String, headData.GID); db.AddInParameter(cmdInsert, "@GOODSDETAILNAME", DbType.String, enumValue.GOODSDETAILNAME); db.AddInParameter(cmdInsert, "@REMARK", DbType.String, enumValue.REMARK); db.AddInParameter(cmdInsert, "@GOODSDETAILPKGS", DbType.Decimal, enumValue.GOODSDETAILPKGS); db.ExecuteNonQuery(cmdInsert, tran); } } tran.Commit(); } catch (Exception) { tran.Rollback(); result.Success = false; result.Message = "保存出现错误,请重试或联系系统管理员"; return result; } } result.Success = true; result.Message = "保存成功"; return result; } public static DBResult DeleteDetail(CodeGoods headData) { var result = new DBResult(); Database db = DatabaseFactory.CreateDatabase(); using (var conn = db.CreateConnection()) { conn.Open(); var tran = conn.BeginTransaction(); try { var cmd = db.GetSqlStringCommand("delete from code_goods where GID='" + headData.GID + "'"); db.ExecuteNonQuery(cmd, tran); var cmdDelete = db.GetSqlStringCommand("delete from code_goods_detail where LINKGID='" + 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 #region 商品类别 static public List GetGoodsType(string strCondition, string sort = null) { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append("GID,GOODSTYPECODE+'-'+GOODSTYPENAME GOODSTYPECODENAME, GOODSTYPECODE,GOODSTYPENAME,DESCRIPTION"); strSql.Append(" from code_goodsType "); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" where " + strCondition); } var sortstring = DatasetSort.Getsortstring(sort); if (!string.IsNullOrEmpty(sortstring)) { strSql.Append(" order by " + sortstring); } else { strSql.Append(" order by GOODSTYPENAME "); } return SetGoodTypeData(strSql); } private static List SetGoodTypeData(StringBuilder strSql) { var bodyList = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { CodeGoodsType data = new CodeGoodsType(); #region Set DB data to Object data.GID = Convert.ToString(reader["GID"]); data.GOODSTYPECODE = Convert.ToString(reader["GOODSTYPECODE"]); data.GOODSTYPECODENAME = Convert.ToString(reader["GOODSTYPECODENAME"]); data.GOODSTYPENAME = Convert.ToString(reader["GOODSTYPENAME"]); data.DESCRIPTION = Convert.ToString(reader["DESCRIPTION"]); #endregion bodyList.Add(data); } reader.Close(); } return bodyList; } #endregion #region 参照部分 public static string GetRangDAStr(string tb, string userid, string usercode, string orgcode) { string str = ""; var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append(" VISIBLERANGE,OPERATERANGE "); strSql.Append(" from VW_User_Authority "); strSql.Append(" where [NAME]='modCodeFeeTemplateSet' 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=2"; } else if (visiblerange == "3") { if (tb == "index") { str = " CREATEUSER='" + userid + "'"; } else { str = " UPPER(CORPID)='" + orgcode + "'"; } } else if (visiblerange == "2") { if (tb == "index") { str = " UPPER(CORPID)='" + orgcode + "'"; } else { str = " UPPER(CORPID)='" + orgcode + "'"; } } else if (visiblerange == "1") { str = " UPPER(CORPID)='" + orgcode + "'"; } return str; } #endregion } }