using System; using System.Data; using System.Collections.Generic; using System.Text; using DSWeb.MvcShipping.Models.MsCwAccitemsFt; using Microsoft.Practices.EnterpriseLibrary.Data; using DSWeb.Areas.CommMng.Models; using HcUtility.Comm; using DSWeb.Areas.CommMng.DAL; namespace DSWeb.MvcShipping.DAL.MsCwAccitemsFt { public class MsCwAccitemsFtDAL { #region Inquery DataList static public List GetDataList(string strCondition, string sort = null) { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append("GID,ACCID,ACCNAME,STARTGID,CREATEUSER,CREATETIME,OFFICE"); strSql.Append(",(select ShowName from [user] where GID=cw_accitems_ft.CREATEUSER) as CREATEUSERREF"); strSql.Append(" from cw_accitems_ft "); 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 CREATETIME DESC"); } return SetData(strSql); } static public CwAccitemsFt GetData(string condition) { CwAccitemsFt data = null; var list = GetDataList(condition); if (list.Count > 0) data = list[0]; if (data == null) { data = new CwAccitemsFt(); } 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()) { CwAccitemsFt data = new CwAccitemsFt(); #region Set DB data to Object data.GID = Convert.ToString(reader["GID"]); data.ACCID = Convert.ToString(reader["ACCID"]); data.ACCNAME = Convert.ToString(reader["ACCNAME"]); data.STARTGID = Convert.ToString(reader["STARTGID"]); data.CREATEUSER = Convert.ToString(reader["CREATEUSER"]); data.CREATEUSERREF = Convert.ToString(reader["CREATEUSERREF"]); if (reader["CREATETIME"] != DBNull.Value) data.CREATETIME = Convert.ToDateTime(reader["CREATETIME"]); data.OFFICE = Convert.ToString(reader["OFFICE"]); #endregion headList.Add(data); } reader.Close(); } return headList; } #endregion static public string GetDataListStr(string strCondition, string userid, string usercode, string orgcode, string sort = null) { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append("GID,ACCID,ACCNAME,STARTGID,CREATEUSER,CREATETIME,OFFICE"); strSql.Append(",(select ShowName from [user] where GID=cw_accitems_ft.CREATEUSER) as CREATEUSERREF"); strSql.Append(" from cw_accitems_ft "); 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 CREATETIME DESC"); } return strSql.ToString(); } static public List GetBodyList(string strCondition, string sort = null) { var strSql = new StringBuilder(); strSql.Append("select GID,ACCID,STARTGID,LINKGID,DEPT,EMP,OFFICE"); strSql.Append(",(select top 1 accname from cw_accitems_gl where ACCID=cw_accitems_ft_detail.ACCID) ACCNAME"); strSql.Append(" from cw_accitems_ft_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 ACCID "); } return SetBodyData(strSql); } static public List GetBodyAllList(string strCondition, string sort = null) { var strSql = new StringBuilder(); strSql.Append("select GID,ACCID,STARTGID,LINKGID,DEPT,EMP,OFFICE"); strSql.Append(",(select top 1 accname from cw_accitems_gl where ACCID=cw_accitems_ft_detail.ACCID) ACCNAME"); //strSql.Append(",(select top 1 accname from VW_user where ACCID=cw_accitems_ft_detail.ACCID) ACCNAME"); strSql.Append(" from cw_accitems_ft_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 ACCID "); } 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()) { CwAccitemsFtDetail data = new CwAccitemsFtDetail(); #region Set DB data to Object data.GID = Convert.ToString(reader["GID"]); data.LINKGID = Convert.ToString(reader["LINKGID"]); data.ACCID = Convert.ToString(reader["ACCID"]); data.ACCNAME = Convert.ToString(reader["ACCNAME"]); data.STARTGID = Convert.ToString(reader["STARTGID"]); data.DEPT = Convert.ToString(reader["DEPT"]); data.EMP = Convert.ToString(reader["EMP"]); data.OFFICE = Convert.ToString(reader["OFFICE"]); #endregion bodyList.Add(data); } reader.Close(); } return bodyList; } public static DBResult SaveDetail(CwAccitemsFt 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 cw_accitems_ft_detail where LINKGID='" + headData.GID + "'"); db.ExecuteNonQuery(cmdDelete, tran); var cmdInsert = db.GetSqlStringCommand( @"insert into cw_accitems_ft_detail (GID,LINKGID,ACCID,ACCNAME,STARTGID,DEPT,EMP,OFFICE) values (@GID,@LINKGID,@ACCID,@ACCNAME,@STARTGID,@DEPT,@EMP,@OFFICE) "); 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, "@ACCID", DbType.String, enumValue.ACCID); db.AddInParameter(cmdInsert, "@ACCNAME", DbType.String, enumValue.ACCNAME); db.AddInParameter(cmdInsert, "@STARTGID", DbType.String, enumValue.STARTGID); db.AddInParameter(cmdInsert, "@DEPT", DbType.String, enumValue.DEPT); db.AddInParameter(cmdInsert, "@EMP", DbType.String, enumValue.EMP); db.AddInParameter(cmdInsert, "@OFFICE", DbType.String, enumValue.OFFICE); db.ExecuteNonQuery(cmdInsert, tran); } } tran.Commit(); } catch (Exception e) { tran.Rollback(); result.Success = false; result.Message = "保存出现错误,请重试或联系系统管理员"+e.Message; return result; } } result.Success = true; result.Message = "保存成功"; return result; } static public List GetFtList(string ACCID, string FTAMOUNT,string userid) { string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(userid); var CwAccitemsFt = MsCwAccitemsFtDAL.GetData("ACCID='" + ACCID + "' AND STARTGID='"+ strCwSTARTGID + "'"); var FtList = GetBodyList("LINKGID='"+ CwAccitemsFt.GID + "'"); var usersqlstr = ""; if (!string.IsNullOrEmpty(CwAccitemsFt.OFFICE)) usersqlstr = " OFFICEADDR='"+ CwAccitemsFt.OFFICE + "'"; var TTLUSER = GetUserNum(usersqlstr); var USERAMOUNT = Math.Round(Convert.ToDecimal(FTAMOUNT)/TTLUSER, 2, MidpointRounding.AwayFromZero); decimal TTLUSERAMOUNT = 0; if (FtList != null && FtList.Count != 0) { foreach (var FT in FtList) { var userstr = " 1=1 "; if (!string.IsNullOrEmpty(FT.EMP)) { userstr = userstr + " AND USERNAME='" + FT.EMP + "'"; } if (!string.IsNullOrEmpty(FT.DEPT)) { userstr = userstr + " AND DEPTNAME='" + FT.DEPT + "'"; } if (!string.IsNullOrEmpty(FT.OFFICE)) { userstr = userstr + " AND OFFICEADDR='" + FT.OFFICE + "'"; } FT.TTLEMP = GetUserNum(userstr); FT.FTAMOUNT = FT.TTLEMP * USERAMOUNT; TTLUSERAMOUNT = TTLUSERAMOUNT + FT.FTAMOUNT; } } CwAccitemsFtDetail data = new CwAccitemsFtDetail(); data.GID = Guid.NewGuid().ToString(); data.LINKGID = CwAccitemsFt.GID; data.ACCID = CwAccitemsFt.ACCID; data.ACCNAME = CwAccitemsFt.ACCNAME; data.STARTGID = CwAccitemsFt.STARTGID; data.DEPT =""; data.EMP =""; data.OFFICE =""; data.FTAMOUNT = Convert.ToDecimal(FTAMOUNT) - TTLUSERAMOUNT; FtList.Add(data); if (FtList != null && FtList.Count != 0) { var AccidList = BasicDataRefDAL.GetCwAccitemsGlAll("", userid); foreach (var FT in FtList) { var accid = AccidList.Find(X => X.ACCID == FT.ACCID); if (accid != null) FT.ACCNAME = accid.ACCNAME; } } return FtList; } private static int GetUserNum(string condition) { int usernum = 0; var strSql = new StringBuilder(); strSql.Append("Select COUNT(1) CT "); strSql.Append(" from code_employee WHERE isnull([STATUS],'')<>'离职' "); if (!string.IsNullOrEmpty(condition)) { strSql.Append(" and " + condition); } Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { if (reader["CT"] != DBNull.Value) usernum = Convert.ToInt32(reader["CT"]); } reader.Close(); } return usernum; } public static DBResult DeleteDetail(CwAccitemsFt 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 cw_accitems_ft where GID='" + headData.GID + "'"); db.ExecuteNonQuery(cmd, tran); var cmdDelete = db.GetSqlStringCommand("delete from cw_accitems_ft_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; } #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 } }