using System; using System.Data; using System.Collections.Generic; using System.Text; using DSWeb.MvcShipping.Models.MsRptOpProfitZou; using Microsoft.Practices.EnterpriseLibrary.Data; using DSWeb.EntityDA; using HcUtility.Comm; using System.Web; using System.Text.RegularExpressions; using DSWeb.MvcShipping.Models.MonthDataSet; using DSWeb.Areas.CommMng.Models; using DSWeb.MvcShipping.DAL.MsSysParamSet; namespace DSWeb.Areas.MvcShipping.DAL.MsRptOpProfitZouDAL { public class MsRptOpProfitZouDAL { static public List SumListData(string condition, string sort, string sumfieldtype, string startmonth, string endmonth, string objtype, string userid, string username, string companyid) { var strDa = GetRangDAStr("index",userid, username,companyid); if (!string.IsNullOrEmpty(strDa)) { if (!string.IsNullOrEmpty(condition)) { condition = condition + " and " + strDa; } else { condition = strDa; } } var RPTPRNOCANCEL = MsSysParamSetDAL.GetData("PARAMNAME='RPTPRNOCANCEL'"); if (RPTPRNOCANCEL.PARAMVALUE == "1") { if (!string.IsNullOrEmpty(condition)) { condition = condition + " and (B.ISCANCEL IS NULL OR B.ISCANCEL=0 ) "; } else { condition = " (B.ISCANCEL IS NULL OR B.ISCANCEL=0 ) "; } } if (objtype == "会计期间") { startmonth = startmonth + "-01"; endmonth = endmonth + "-01"; } var monthlist = GetMonthList(startmonth, endmonth); var strSql = new StringBuilder(); strSql.Append("SELECT 1 as NO "); foreach (var enumValue in monthlist) { if (objtype == "会计期间") { if (sumfieldtype == "利润") { strSql.Append(",sum(case when B.ACCDATE='" + enumValue.MONTHFIELDNAME.Replace('/', '-') + "' then G.TTLDR-G.TTLCR ELSE 0 end) as ZL" + enumValue.MONTHNO.ToString() + "TTL "); strSql.Append(",sum(case when B.ACCDATE='" + enumValue.MONTHFIELDNAME.Replace('/', '-') + "' then G.RMBDR-G.RMBCR ELSE 0 end) as ZLRMB" + enumValue.MONTHNO.ToString() + "TTL "); strSql.Append(",sum(case when B.ACCDATE='" + enumValue.MONTHFIELDNAME.Replace('/', '-') + "' then G.USDDR-G.USDCR ELSE 0 end) as ZLUSD" + enumValue.MONTHNO.ToString() + "TTL "); } if (sumfieldtype == "欠费") { strSql.Append(",sum(case when B.ACCDATE='" + enumValue.MONTHFIELDNAME.Replace('/', '-') + "' then G.TTLDR-G.STLTTLDR ELSE 0 end) as ZL" + enumValue.MONTHNO.ToString() + "TTL "); strSql.Append(",sum(case when B.ACCDATE='" + enumValue.MONTHFIELDNAME.Replace('/', '-') + "' then G.RMBDR-G.STLRMBDR ELSE 0 end) as ZLRMB" + enumValue.MONTHNO.ToString() + "TTL "); strSql.Append(",sum(case when B.ACCDATE='" + enumValue.MONTHFIELDNAME.Replace('/', '-') + "' then G.USDDR-G.STLUSDDR ELSE 0 end) as ZLUSD" + enumValue.MONTHNO.ToString() + "TTL "); } if (sumfieldtype == "TEU") strSql.Append(",sum(case when OPTYPE='普通货' and B.ACCDATE='" + enumValue.MONTHFIELDNAME.Replace('/', '-') + "' then B.TEU ELSE 0 end) as ZL" + enumValue.MONTHNO.ToString() + "TTL,0 ZLRMB" + enumValue.MONTHNO.ToString() + "TTL,0 ZLUSD" + enumValue.MONTHNO.ToString() + "TTL "); if (sumfieldtype == "毛重") strSql.Append(",sum(case when OPTYPE='普通货' and B.ACCDATE='" + enumValue.MONTHFIELDNAME.Replace('/', '-') + "' then B.KGS ELSE 0 end) as ZL" + enumValue.MONTHNO.ToString() + "TTL,0 ZLRMB" + enumValue.MONTHNO.ToString() + "TTL,0 ZLUSD" + enumValue.MONTHNO.ToString() + "TTL "); if (sumfieldtype == "计费重") strSql.Append(",sum(case when OPTYPE='普通货' and B.ACCDATE='" + enumValue.MONTHFIELDNAME.Replace('/', '-') + "' then B.FEEKGS ELSE 0 end) as ZL" + enumValue.MONTHNO.ToString() + "TTL,0 ZLRMB" + enumValue.MONTHNO.ToString() + "TTL,0 ZLUSD" + enumValue.MONTHNO.ToString() + "TTL "); if (sumfieldtype == "票数") strSql.Append(",sum(case when B.ACCDATE='" + enumValue.MONTHFIELDNAME.Replace('/', '-') + "' then 1 ELSE 0 end) as ZL" + enumValue.MONTHNO.ToString() + "TTL,0 ZLRMB" + enumValue.MONTHNO.ToString() + "TTL,0 ZLUSD" + enumValue.MONTHNO.ToString() + "TTL "); } else { if (sumfieldtype == "利润") { strSql.Append(",sum(case when SUBSTRING(CONVERT(char(15), B.OPDATE, 111), 1, 7)='" + enumValue.MONTHFIELDNAME + "' then G.TTLDR-G.TTLCR ELSE 0 end) as ZL" + enumValue.MONTHNO.ToString() + "TTL "); strSql.Append(",sum(case when SUBSTRING(CONVERT(char(15), B.OPDATE, 111), 1, 7)='" + enumValue.MONTHFIELDNAME + "' then G.RMBDR-G.RMBCR ELSE 0 end) as ZLRMB" + enumValue.MONTHNO.ToString() + "TTL "); strSql.Append(",sum(case when SUBSTRING(CONVERT(char(15), B.OPDATE, 111), 1, 7)='" + enumValue.MONTHFIELDNAME + "' then G.USDDR-G.USDCR ELSE 0 end) as ZLUSD" + enumValue.MONTHNO.ToString() + "TTL "); } if (sumfieldtype == "欠费") { strSql.Append(",sum(case when SUBSTRING(CONVERT(char(15), B.OPDATE, 111), 1, 7)='" + enumValue.MONTHFIELDNAME + "' then G.TTLDR-G.STLTTLDR ELSE 0 end) as ZL" + enumValue.MONTHNO.ToString() + "TTL "); strSql.Append(",sum(case when SUBSTRING(CONVERT(char(15), B.OPDATE, 111), 1, 7)='" + enumValue.MONTHFIELDNAME + "' then G.RMBDR-G.STLRMBDR ELSE 0 end) as ZLRMB" + enumValue.MONTHNO.ToString() + "TTL "); strSql.Append(",sum(case when SUBSTRING(CONVERT(char(15), B.OPDATE, 111), 1, 7)='" + enumValue.MONTHFIELDNAME + "' then G.USDDR-G.STLUSDDR ELSE 0 end) as ZLUSD" + enumValue.MONTHNO.ToString() + "TTL "); } if (sumfieldtype == "TEU") strSql.Append(",sum(case when OPTYPE='普通货' and SUBSTRING(CONVERT(char(15), B.OPDATE, 111), 1, 7)='" + enumValue.MONTHFIELDNAME + "' then B.TEU ELSE 0 end) as ZL" + enumValue.MONTHNO.ToString() + "TTL,0 ZLRMB" + enumValue.MONTHNO.ToString() + "TTL,0 ZLUSD" + enumValue.MONTHNO.ToString() + "TTL "); if (sumfieldtype == "毛重") strSql.Append(",sum(case when OPTYPE='普通货' and SUBSTRING(CONVERT(char(15), B.OPDATE, 111), 1, 7)='" + enumValue.MONTHFIELDNAME + "' then B.KGS ELSE 0 end) as ZL" + enumValue.MONTHNO.ToString() + "TTL,0 ZLRMB" + enumValue.MONTHNO.ToString() + "TTL,0 ZLUSD" + enumValue.MONTHNO.ToString() + "TTL "); if (sumfieldtype == "计费重") strSql.Append(",sum(case when OPTYPE='普通货' and SUBSTRING(CONVERT(char(15), B.OPDATE, 111), 1, 7)='" + enumValue.MONTHFIELDNAME + "' then B.FEEKGS ELSE 0 end) as ZL" + enumValue.MONTHNO.ToString() + "TTL,0 ZLRMB" + enumValue.MONTHNO.ToString() + "TTL,0 ZLUSD" + enumValue.MONTHNO.ToString() + "TTL "); if (sumfieldtype == "票数") strSql.Append(",sum(case when SUBSTRING(CONVERT(char(15), B.OPDATE, 111), 1, 7)='" + enumValue.MONTHFIELDNAME + "' then 1 ELSE 0 end) as ZL" + enumValue.MONTHNO.ToString() + "TTL,0 ZLRMB" + enumValue.MONTHNO.ToString() + "TTL,0 ZLUSD" + enumValue.MONTHNO.ToString() + "TTL "); } } strSql.Append(" FROM V_OP_BILL B "); strSql.Append("LEFT JOIN v_op_gain_sum G ON (G.BSNO=B.BSNO) "); strSql.Append(" WHERE 1=1 "); if (!string.IsNullOrEmpty(condition)) { strSql.Append(" AND " + condition); } return SetData(strSql,monthlist); } private static List SetData(StringBuilder strSql, List monthlist) { var headList = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { foreach (var enumValue in monthlist) { ZouMonthDataSet data = new ZouMonthDataSet(); #region Set DB data to Object data.MONTHNAME = enumValue.MONTHNAME; data.MONTHNO = enumValue.MONTHNO; if (reader["ZL" + enumValue.MONTHNO.ToString() + "TTL"] != DBNull.Value) data.MONTHNUM = Convert.ToDecimal(reader["ZL" + enumValue.MONTHNO.ToString() + "TTL"]); else data.MONTHNUM =0; if (reader["ZLRMB" + enumValue.MONTHNO.ToString() + "TTL"] != DBNull.Value) data.OBJNUMRMB = Convert.ToDecimal(reader["ZLRMB" + enumValue.MONTHNO.ToString() + "TTL"]); else data.OBJNUMRMB = 0; if (reader["ZLUSD" + enumValue.MONTHNO.ToString() + "TTL"] != DBNull.Value) data.OBJNUMUSD = Convert.ToDecimal(reader["ZLUSD" + enumValue.MONTHNO.ToString() + "TTL"]); else data.OBJNUMUSD = 0; #endregion headList.Add(data); } } reader.Close(); } return headList; } static public List GetMonthList(string startmonth, string endmonth) { var startdate = startmonth.Split('-'); var enddate = endmonth.Split('-'); int monthstart = Convert.ToInt16(startdate[0]) * 12 + Convert.ToInt16(startdate[1]); int monthend = Convert.ToInt16(enddate[0]) * 12 + Convert.ToInt16(enddate[1]); var betweenmonth = monthend - monthstart + 1; DateTime dt = DateTime.ParseExact(startmonth, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture); var evList = new List(); var month = ""; for (int i = 0; i < betweenmonth; i++) { var evData = new MonthDataSet(); evData.MONTHNAME = dt.ToString("yyyy年MM月"); evData.MONTHNO = i; month = dt.Month.ToString(); if (month.Length == 1) month = "0" + month; evData.MONTHFIELDNAME = dt.Year.ToString() + "/" + month; evList.Add(evData); dt = dt.AddMonths(1); } return evList; } public static string GetRangDAStr(string tb, string userid, string usercode, string companyid) { string str = ""; var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append(" VISIBLERANGE,OPERATERANGE,AUTHORITYID "); strSql.Append(" from VW_User_Authority "); strSql.Append(" where [NAME]='modProfitzouRange' and USERID='" + userid + "' and ISDELETE=0"); string visiblerange = "4"; string operaterange = "4"; string AUTHORITYID = ""; 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"]); AUTHORITYID = Convert.ToString(reader["AUTHORITYID"]); break; } reader.Close(); } if (visiblerange == "4") { str = "1=2"; } else if (visiblerange == "3") { str = " (B.OP='" + usercode + "' OR B.SALE='" + usercode + "')"; } else if (visiblerange == "2") { if (tb == "index") { var rangeDa = new RangeDA(); var deptname = rangeDa.GetDEPTNAME(userid); var userstr = new StringBuilder(); userstr.Append(" select SHOWNAME from [user] where GID in (select USERID from user_company where COMPANYID='" + companyid + "') and GID in (select userid from user_baseinfo where DEPTNAME='" + deptname + "')"); Database userdb = DatabaseFactory.CreateDatabase(); using (IDataReader reader = userdb.ExecuteReader(CommandType.Text, userstr.ToString())) { str = ""; while (reader.Read()) { if (str == "") { str = " (B.OP='" + Convert.ToString(reader["SHOWNAME"]) + "' OR B.SALE='" + Convert.ToString(reader["SHOWNAME"]) + "'"; } else { str = str + " or B.OP='" + Convert.ToString(reader["SHOWNAME"]) + "' OR B.SALE='" + Convert.ToString(reader["SHOWNAME"]) + "'"; }; } str = str + ")"; reader.Close(); } } else { str = " (UPPER(B.Corpid)='" + companyid + "' or B.SALECORPID='" + companyid + "') "; } } else if (visiblerange == "5") { var userstr = new StringBuilder(); userstr.Append(" select COMPANYID from user_authority_range_company where userid='" + userid + "' and AUTHORITYID='" + AUTHORITYID + "' and VISIBLERANGE=1"); Database userdb = DatabaseFactory.CreateDatabase(); using (IDataReader reader = userdb.ExecuteReader(CommandType.Text, userstr.ToString())) { str = ""; while (reader.Read()) { if (str == "") { str = " (B.Corpid='" + Convert.ToString(reader["COMPANYID"]) + "' or B.SALECORPID='" + Convert.ToString(reader["COMPANYID"]) + "' "; } else { str = str + " or B.Corpid='" + Convert.ToString(reader["COMPANYID"]) + "' or B.SALECORPID='" + Convert.ToString(reader["COMPANYID"]) + "'"; }; } str = str + ")"; reader.Close(); } } else if (visiblerange == "6") { var userstr = new StringBuilder(); userstr.Append(" select OPID,(select SHOWNAME from [user] where GID=user_authority_range_op.OPID) SHOWNAME from user_authority_range_op where userid='" + userid + "' and AUTHORITYID='" + AUTHORITYID + "' and VISIBLERANGE=1"); Database userdb = DatabaseFactory.CreateDatabase(); using (IDataReader reader = userdb.ExecuteReader(CommandType.Text, userstr.ToString())) { str = ""; while (reader.Read()) { if (str == "") { str = " (B.OP='" + Convert.ToString(reader["SHOWNAME"]) + "' OR B.SALE='" + Convert.ToString(reader["SHOWNAME"]) + "'"; } else { str = str + " or B.OP='" + Convert.ToString(reader["SHOWNAME"]) + "' OR B.SALE='" + Convert.ToString(reader["SHOWNAME"]) + "'"; }; } str = str + ")"; reader.Close(); } } else if (visiblerange == "1") { str = " (UPPER(B.Corpid)='" + companyid + "' or B.SALECORPID='" + companyid + "') "; } return str; } #region 参照部分 #endregion } }