/// <summary>
 /// 新增组织机构
 /// </summary>
 /// <param name="loginId">登录ID</param>
 /// <param name="orgEntity"></param>
 /// <returns></returns>
 public EntityBase AddOrg(string loginId, OrgEntity orgEntity)
 {
     TradeUser TdUser = new TradeUser();
     if (ComFunction.ExistUserLoginID(loginId, ref TdUser) == false)
     {
         return entityBase;
     }
     UserLogEntity userLogEntity = new UserLogEntity();
     userLogEntity.Account = TdUser.Account;
     userLogEntity.DESC = string.Format(@"后台新增组织机构;{0}", orgEntity.OrgName);
     userLogEntity.UserType = (int)TdUser.UType;
     //检测使用组织编码作为默认组织账户时,组织账户是否已经存在
     if(ComFunction.TradeAccountExist(orgEntity.TelePhone))
     {
         entityBase.Result = false;
         entityBase.Desc = string.Format("新增失败,已存在组织账户{0}!", orgEntity.TelePhone);
         return entityBase;
     }
     if (ComFunction.IsExitOrgName(orgEntity.OrgName, orgEntity.TelePhone) >= 1)
     {
         entityBase.Result = false;
         entityBase.Desc = "新增失败,存在相同的机构名称或组织编码!";
         return entityBase;
     }
     if (string.IsNullOrEmpty(orgEntity.ParentOrgId))
     {
         orgEntity.ParentOrgId = "";
         orgEntity.ParentOrgName = "";
     }
     YicelTransaction tran = new YicelTransaction();
     try
     {
         string ipmac = string.Empty;
         if (!string.IsNullOrEmpty(TdUser.Ip))
         {
             ipmac += string.Format("IP={0},", TdUser.Ip);
         }
         if (!string.IsNullOrEmpty(TdUser.Mac))
         {
             ipmac += string.Format("MAC={0},", TdUser.Mac);
         }
         tran.BeginTransaction();
         ComFunction.AddOrg(orgEntity, tran);
         ComFunction.CreateLogEx(userLogEntity,ipmac, tran);
         tran.Commit();
         entityBase.Result = true;
         entityBase.Desc = "组织机构新增成功";
     }
     catch (Exception ex)
     {
         tran.Rollback();
         entityBase.Result = false;
         entityBase.Desc = "组织机构新增失败";
         SetException("组织机构新增失败,原因:", ex);
     }
     return entityBase;
 }
        /// <summary>
        /// 新增组织机构
        /// </summary>
        /// <param name="orgEntity"></param>
        /// <param name="tran"></param>
        /// <returns></returns>
        public static int AddOrg(OrgEntity orgEntity, YicelTransaction tran)
        {
            SqlParameter[] parms = new SqlParameter[] {
                new SqlParameter("@OrgID",orgEntity.OrgID),
                new SqlParameter("@OrgName",orgEntity.OrgName),
                new SqlParameter("@Coperson",orgEntity.Coperson),
                new SqlParameter("@CardType",orgEntity.CardType),
                new SqlParameter("@CardNum",orgEntity.CardNum),
                new SqlParameter("@ParentOrgId",orgEntity.ParentOrgId),
                new SqlParameter("@ParentOrgName",orgEntity.ParentOrgName),
                new SqlParameter("@Reperson",orgEntity.Reperson),
                new SqlParameter("@PhoneNum",orgEntity.PhoneNum),
                new SqlParameter("@TelePhone",orgEntity.TelePhone),
                new SqlParameter("@Email",orgEntity.Email),
                new SqlParameter("@Address",orgEntity.Address),
                new SqlParameter("@AddTime",orgEntity.AddTime),
                new SqlParameter("@Status",(int)orgEntity.Status)
            };
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("insert into Base_Org({0})", Fields.Org_FIELD_List);
            strSql.AppendFormat(" values ({0})", "@" + Fields.Org_FIELD_List.Replace(",", ",@"));
            object obj = DbHelper.ExecuteNonQuery(strSql.ToString(), parms, tran.Transaction);
            StringBuilder strbld = new StringBuilder();
            string strdt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //构造新增组织用户信息的sql语句
            strbld.AppendFormat(@"insert into Base_User([userId],[userName],[status],[Accounttype],[Account],[LoginPwd],
                                        [CardType],[CardNum],[OrgId],[PhoneNum],[TelNum],[Email],[LinkAdress],[sex],[OpenTime],
                                        [LastUpdateTime],[LastUpdateID],[Online],[UserType],[BindAccount])
                                        values('{0}','{1}','{2}','{3}','{4}','{5}',",
                                orgEntity.OrgID, string.IsNullOrEmpty(orgEntity.OrgName) ? string.Empty : orgEntity.OrgName,
                                1, 1, orgEntity.TelePhone, com.individual.helper.Des3.Des3EncodeCBC("123456"));
            strbld.AppendFormat("'{0}','{1}','{2}','{3}','{4}','{5}',", 1, string.IsNullOrEmpty(orgEntity.CardNum) ? string.Empty : orgEntity.CardNum, orgEntity.OrgID,
                orgEntity.PhoneNum, string.Empty, string.IsNullOrEmpty(orgEntity.Email) ? string.Empty : orgEntity.Email);
            strbld.AppendFormat("'{0}','{1}','{2}','{3}','{4}',{5},{6},'{7}')",
                string.IsNullOrEmpty(orgEntity.Address) ? string.Empty : orgEntity.Address, 1, strdt, strdt,
                string.Empty, 0, 2, string.Empty);

            obj = DbHelper.ExecuteNonQuery(strbld.ToString(), null, tran.Transaction);
            return obj == null || obj == DBNull.Value ? 0 : Convert.ToInt32(obj);
        }
 /// <summary>
 /// 读取组织机构
 /// </summary>
 /// <param name="loginId">登录ID</param>
 /// <param name="orgId"></param>
 /// <returns></returns>
 public OrgEntity ReadOrg(string loginId, string orgId)
 {
     OrgEntity orgEntity = new OrgEntity();
     if (ComFunction.ExistUserLoginID(loginId) == false)
     {
         orgEntity.Result = false;
         orgEntity.Desc = ResCode.UL003Desc;
         return orgEntity;
     }
     try
     {
         DataTable dt = ComFunction.ReadOrg(orgId);
         foreach (DataRow dr in dt.Rows)
         {
             orgEntity.OrgID = dr["OrgID"].ToString();
             orgEntity.OrgName = dr["OrgName"].ToString();
             orgEntity.Coperson = dr["Coperson"].ToString();
             orgEntity.CardType =(IDType) dr["CardType"];
             orgEntity.CardNum = dr["CardNum"].ToString();
             orgEntity.ParentOrgId = dr["ParentOrgID"].ToString();
             orgEntity.Reperson = dr["Reperson"].ToString();
             orgEntity.PhoneNum = dr["PhoneNum"].ToString();
             orgEntity.TelePhone = dr["TelePhone"].ToString();
             orgEntity.Email = dr["Email"].ToString();
             orgEntity.Address = dr["Address"].ToString();
             orgEntity.AddTime = dr["AddTime"].ToString();
             orgEntity.Status = (Status)dr["Status"];
         }
         orgEntity.Desc = "获取系统权限成功!";
         orgEntity.Result = true;
     }
     catch (Exception ex)
     {
         orgEntity.Result = false;
         orgEntity.Desc = "获取组织机构记录出错,原因:" + ex.ToString();
         ManagerLog.WriteErr(ex);
     }
     return orgEntity;
 }
 /// <summary>
 /// 组织机构查询
 /// </summary>
 /// <param name="loginId"></param>
 /// <param name="orgEntity"></param>
 /// <param name="pageindex"></param>
 /// <param name="pagesize"></param>
 /// <param name="page"></param>
 /// <param name="list"></param>
 /// <returns></returns>
 public EntityBase GetOrgList(string loginId, OrgEntity orgEntity, int pageindex, int pagesize, ref int page, ref List<OrgEntity> list)
 {
     TradeUser TdUser = new TradeUser();
     if (ComFunction.ExistUserLoginID(loginId,ref TdUser) == false)
     {
         return entityBase;
     }
     try
     {
         DataTable dt = ComFunction.GetOrgList(orgEntity,TdUser, pageindex, pagesize, ref page);
         orgEntity = new OrgEntity();
         foreach (DataRow dr in dt.Rows)
         {
             orgEntity = new OrgEntity();
             orgEntity.OrgID = dr["OrgID"].ToString();
             orgEntity.OrgName = dr["OrgName"].ToString();
             orgEntity.Coperson = dr["Coperson"].ToString();
             orgEntity.CardType = (IDType)dr["CardType"];
             orgEntity.CardNum = dr["CardNum"].ToString();
             orgEntity.ParentOrgId = dr["ParentOrgID"].ToString();
             orgEntity.ParentOrgName = dr["ParentOrgName"].ToString();
             orgEntity.Reperson = dr["Reperson"].ToString();
             orgEntity.PhoneNum = dr["PhoneNum"].ToString();
             orgEntity.TelePhone = dr["TelePhone"].ToString();
             orgEntity.Email = dr["Email"].ToString();
             orgEntity.Address = dr["Address"].ToString();
             orgEntity.AddTime = dr["AddTime"].ToString();
             orgEntity.Status = (Status)dr["Status"];
             list.Add(orgEntity);
         }
         entityBase.Desc = "获取组织机构成功!";
         entityBase.Result = true;
     }
     catch (Exception ex)
     {
         SetException("获取组织机构记录数出错,原因:", ex);
     }
     return entityBase;
 }
 /// <summary>
 /// 修改组织机构数据
 /// </summary>
 /// <param name="loginId"></param>
 /// <param name="orgEntity"></param>
 /// <returns></returns>
 public EntityBase UpdateOrg(string loginId, OrgEntity orgEntity)
 {
     TradeUser TdUser = new TradeUser();
     if (ComFunction.ExistUserLoginID(loginId, ref TdUser) == false)
     {
         return entityBase;
     }
     UserLogEntity userLogEntity = new UserLogEntity();
     userLogEntity.Account = TdUser.Account;
     userLogEntity.DESC = string.Format(@"后台修改组织机构;{0}", orgEntity.OrgName);
     userLogEntity.UserType = (int)TdUser.UType;
     if (ComFunction.IsExitOrgName(orgEntity.OrgName, orgEntity.TelePhone,orgEntity.OrgID) >= 1)
     {
         entityBase.Result = false;
         entityBase.Desc = "修改失败,存在相同的机构名称或组织编码!";
         return entityBase;
     }
     if (ComFunction.IsExistHuWeiFuzi(orgEntity.OrgID, orgEntity.ParentOrgId)>=1)
     {
         entityBase.Result = false;
         entityBase.Desc = "修改失败,上级组织不能是该组织的下级组织!";
         return entityBase;
     }
     YicelTransaction tran = new YicelTransaction();
     try
     {
         string ipmac = string.Empty;
         if (!string.IsNullOrEmpty(TdUser.Ip))
         {
             ipmac += string.Format("IP={0},", TdUser.Ip);
         }
         if (!string.IsNullOrEmpty(TdUser.Mac))
         {
             ipmac += string.Format("MAC={0},", TdUser.Mac);
         }
         tran.BeginTransaction();
         ComFunction.UpdateOrg(orgEntity, tran);
         ComFunction.UpdateChileOrg(orgEntity, tran);
         ComFunction.CreateLogEx(userLogEntity,ipmac, tran);
         tran.Commit();
         entityBase.Result = true;
         entityBase.Desc = "组织机构修改成功";
     }
     catch (Exception ex)
     {
         tran.Rollback();
         SetException("组织机构修改失败,原因:", ex);
     }
     return entityBase;
 }
 /// <summary>
 /// 修改组织机构
 /// </summary>
 /// <param name="orgEntity"></param>
 /// <param name="tran"></param>
 /// <returns></returns>
 public static int UpdateOrg(OrgEntity orgEntity, YicelTransaction tran)
 {
     SqlParameter[] parms = new SqlParameter[] {
         new SqlParameter("@OrgID",orgEntity.OrgID),
         new SqlParameter("@OrgName",orgEntity.OrgName),
         new SqlParameter("@Coperson",orgEntity.Coperson),
         new SqlParameter("@CardType",orgEntity.CardType),
         new SqlParameter("@CardNum",orgEntity.CardNum),
         new SqlParameter("@ParentOrgID",orgEntity.ParentOrgId),
          new SqlParameter("@ParentOrgName",orgEntity.ParentOrgName),
         new SqlParameter("@Reperson",orgEntity.Reperson),
         new SqlParameter("@PhoneNum",orgEntity.PhoneNum),
        // new SqlParameter("@TelePhone",orgEntity.TelePhone),//组织编码不能修改
         new SqlParameter("@Email",orgEntity.Email),
         new SqlParameter("@Address",orgEntity.Address),
         new SqlParameter("@AddTime",orgEntity.AddTime),
         new SqlParameter("@Status",(int)orgEntity.Status)
     };
     StringBuilder strSql = new StringBuilder();
     //            strSql.AppendFormat(@"UPDATE  Base_Org SET
     //                                OrgName = @OrgName,Coperson = @Coperson,CardType =@CardType,CardNum = @CardNum,ParentOrgID = @ParentOrgID,ParentOrgName=@ParentOrgName,Reperson = @Reperson,
     //                                PhoneNum = @PhoneNum,TelePhone = @TelePhone,Email = @Email,Address =@Address,AddTime = @AddTime,Status = @Status
     //                                WHERE   OrgID = @OrgID", Fields.Org_FIELD_List);
     strSql.AppendFormat(@"UPDATE  Base_Org SET
                                     OrgName = @OrgName,Coperson = @Coperson,CardType =@CardType,CardNum = @CardNum,ParentOrgID = @ParentOrgID,ParentOrgName=@ParentOrgName,Reperson = @Reperson,
                                     PhoneNum = @PhoneNum,Email = @Email,Address =@Address,AddTime = @AddTime,Status = @Status
                                     WHERE   OrgID = @OrgID", Fields.Org_FIELD_List);
     object obj = DbHelper.ExecuteNonQuery(strSql.ToString(), parms, tran.Transaction);
     return obj == null || obj == DBNull.Value ? 0 : Convert.ToInt32(obj);
 }
 public static int UpdateChileOrg(OrgEntity orgEntity, YicelTransaction tran)
 {
     SqlParameter[] parms = new SqlParameter[] {
         new SqlParameter("@ParentOrgID",orgEntity.ParentOrgId),
          new SqlParameter("@ParentOrgName",orgEntity.ParentOrgName)
     };
     StringBuilder strSql = new StringBuilder();
     strSql.AppendFormat(@"UPDATE  Base_Org SET  ParentOrgName=@ParentOrgName   WHERE   ParentOrgID = @ParentOrgID");
     object obj = DbHelper.ExecuteNonQuery(strSql.ToString(), parms, tran.Transaction);
     return obj == null || obj == DBNull.Value ? 0 : Convert.ToInt32(obj);
 }
        /// <summary>
        /// 组织机构查询
        /// </summary>
        /// <param name="orgEntity"></param>   
        /// <param name="TdUser"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public static DataTable GetOrgList(OrgEntity orgEntity,TradeUser TdUser, int pageindex, int pagesize, ref int page)
        {
            SqlParameter[] paras = null;
            StringBuilder sb = new StringBuilder();

            string PartSearchCondition = string.Empty;
            string ParentOrgID = string.Empty;
            if (UserType.OrgType == TdUser.UType && !string.IsNullOrEmpty(TdUser.OrgId))
            {
                PartSearchCondition = " and orgid in (select orgid from #tmp) ";
                ParentOrgID = TdUser.OrgId;
            }

            if (!string.IsNullOrEmpty(orgEntity.OrgName))
            {
                //PartSearchCondition = " and orgid in (select orgid from #tmp) ";
                //ParentOrgID = orgEntity.OrgName;
                sb.AppendFormat(@" and orgid='{0}'", orgEntity.OrgName);
            }

            if (!string.IsNullOrEmpty(orgEntity.CardNum))
            {
                sb.AppendFormat(@" and CardNum like '{0}%'", orgEntity.CardNum);
            }
            if (!string.IsNullOrEmpty(orgEntity.Reperson))
            {
                sb.AppendFormat(@" and Reperson like '{0}%'", orgEntity.Reperson);
            }
            paras = new SqlParameter[]
            {
                new SqlParameter("@selectlist", "OrgID,OrgName,Coperson,CardType,CardNum,ParentOrgID,ParentOrgName,Reperson,PhoneNum,TelePhone,Email,Address,AddTime,Status"),
                new SqlParameter("@SubSelectList", "OrgID,OrgName,Coperson,CardType,CardNum,ParentOrgID,ParentOrgName,Reperson,PhoneNum,TelePhone,Email,Address,AddTime,Status"),
                new SqlParameter("@TableSource", "V_Base_org"),
                new SqlParameter("@TableOrder", "a"),
                new SqlParameter("@SearchCondition", string.Format(@" 1=1 {0} {1}",sb.ToString(),PartSearchCondition)),
                new SqlParameter("@OrderExpression", " order by OrgName desc"),
                new SqlParameter("@ParentOrgID", ParentOrgID),
                new SqlParameter("@PageIndex", pageindex),
                new SqlParameter("@PageSize", pagesize),
                new SqlParameter("@PageCount", page)
              };
            paras[9].Direction = ParameterDirection.Output;
            DataTable dt = DbHelper.RunProcedure("GetRecordFromPageEx", paras, "Base_org").Tables[0];
            page = Convert.ToInt32(paras[9].Value);
            return dt;
        }