using Newtonsoft.Json.Linq; 
 | 
using Pub_Class; 
 | 
using System; 
 | 
using System.Collections; 
 | 
using System.Collections.Generic; 
 | 
using System.Data; 
 | 
using System.Data.SqlClient; 
 | 
using System.Web.Http; 
 | 
using WebAPI.Models; 
 | 
namespace WebAPI.Controllers 
 | 
{ 
 | 
    public class Xt_UserGroupController : ApiController 
 | 
    { 
 | 
        public DBUtility.ClsPub.Enum_BillStatus BillStatus; 
 | 
  
 | 
        private json objJsonResult = new json(); 
 | 
        SQLHelper.ClsCN oCN = new SQLHelper.ClsCN(); 
 | 
        DataSet ds; 
 | 
  
 | 
        #region 角色列表 
 | 
        /// <summary> 
 | 
        /// 返回用户角色列表 
 | 
        ///参数:string sql。 
 | 
        ///返回值:object。 
 | 
        /// </summary> 
 | 
        [Route("Xt_UserGroup/list")] 
 | 
        [HttpGet] 
 | 
        public object list(string sWhere) 
 | 
        { 
 | 
            try 
 | 
            { 
 | 
                ds = oCN.RunProcReturn("select * from h_v_System_UserGroup   where 1 = 1 " + sWhere, "h_v_System_UserGroup"); 
 | 
  
 | 
                objJsonResult.code = "1"; 
 | 
                objJsonResult.count = 1; 
 | 
                objJsonResult.Message = "Sucess!"; 
 | 
                objJsonResult.data = ds.Tables[0]; 
 | 
                return objJsonResult; 
 | 
            } 
 | 
            catch (Exception e) 
 | 
            { 
 | 
                objJsonResult.code = "0"; 
 | 
                objJsonResult.count = 0; 
 | 
                objJsonResult.Message = "Exception!" + e.ToString(); 
 | 
                objJsonResult.data = null; 
 | 
                return objJsonResult; 
 | 
            } 
 | 
        } 
 | 
        #endregion 
 | 
  
 | 
        #region 角色绑定用户数据 
 | 
        [Route("Xt_UserGroup/UserGroupInfoList")] 
 | 
        [HttpGet] 
 | 
        public object UserGroupInfoList(string sWhere) 
 | 
        { 
 | 
            try 
 | 
            { 
 | 
                string sql = "select * from System_UserGroupInfo where 1=1 " + sWhere; 
 | 
                ds = oCN.RunProcReturn(sql, "System_UserGroupInfo"); 
 | 
  
 | 
                objJsonResult.code = "1"; 
 | 
                objJsonResult.count = 1; 
 | 
                objJsonResult.Message = "Sucess!"; 
 | 
                objJsonResult.data = ds.Tables[0]; 
 | 
                return objJsonResult; 
 | 
            } 
 | 
            catch (Exception e) 
 | 
            { 
 | 
                objJsonResult.code = "0"; 
 | 
                objJsonResult.count = 0; 
 | 
                objJsonResult.Message = "Exception!" + e.ToString(); 
 | 
                objJsonResult.data = null; 
 | 
                return objJsonResult; 
 | 
            } 
 | 
        } 
 | 
        #endregion 
 | 
  
 | 
        #region 角色列表  添加/修改 
 | 
        public class Xt_UserGroup 
 | 
        { 
 | 
            public int HGroupID; 
 | 
            public string GroupName { get; set; } 
 | 
            public string HLev { get; set; } 
 | 
            public string Explain { get; set; } 
 | 
            public int HUSEORGID { get; set; } 
 | 
            public int HCREATEORGID { get; set; } 
 | 
        } 
 | 
        [Route("Xt_UserGroup/UserGroupInfoEdit")] 
 | 
        [HttpPost] 
 | 
        public object UserGroupInfoEdit([FromBody] JObject sMainSub) 
 | 
        { 
 | 
            var _value = sMainSub["sMainSub"].ToString(); 
 | 
            string msg1 = _value.ToString(); 
 | 
            oCN.BeginTran(); 
 | 
            //保存主表 
 | 
            objJsonResult = AddBillMain(msg1); 
 | 
            if (objJsonResult.code == "0") 
 | 
            { 
 | 
                oCN.RollBack(); 
 | 
                objJsonResult.code = "0"; 
 | 
                objJsonResult.count = 0; 
 | 
                objJsonResult.Message = objJsonResult.Message; 
 | 
                objJsonResult.data = null; 
 | 
                return objJsonResult; 
 | 
            } 
 | 
            oCN.Commit(); 
 | 
            objJsonResult.code = "1"; 
 | 
            objJsonResult.count = 1; 
 | 
            objJsonResult.Message = "成功!"; 
 | 
            objJsonResult.data = null; 
 | 
            return objJsonResult; 
 | 
        } 
 | 
  
 | 
        public json AddBillMain(string msg1) 
 | 
        { 
 | 
            string[] sArray = msg1.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries); 
 | 
            string msg2 = sArray[0].ToString(); 
 | 
            string msg3 = sArray[1].ToString(); 
 | 
            int OperationType = int.Parse(sArray[2].ToString());//数据类型 1添加 3修改 
 | 
            string user = sArray[3].ToString(); 
 | 
            try 
 | 
            { 
 | 
                Xt_UserGroup omdelMian = Newtonsoft.Json.JsonConvert.DeserializeObject<Xt_UserGroup>(msg2); 
 | 
  
 | 
                var num = msg3.Split(','); 
 | 
  
 | 
                if (OperationType == 1)//新增 
 | 
                { 
 | 
                    //新增前查询  确保角色名未重复新增 
 | 
                    ds = oCN.RunProcReturn("Select * from  System_UserGroup where GroupName='" + omdelMian.GroupName + "'", "System_UserGroup"); 
 | 
                    if (ds.Tables[0].Rows.Count > 0) 
 | 
                    { 
 | 
                        objJsonResult.code = "0"; 
 | 
                        objJsonResult.count = 0; 
 | 
                        objJsonResult.Message = "角色新增失败!当前角色名已经存在,不可重复"; 
 | 
                        objJsonResult.data = null; 
 | 
                        return objJsonResult; 
 | 
                    } 
 | 
  
 | 
                    //主表 
 | 
                    oCN.RunProc("insert into  System_UserGroup (GroupName,HLev,Explain,HUSEORGID,HCREATEORGID)values" + 
 | 
                        $"('{omdelMian.GroupName}','{omdelMian.HLev}','{omdelMian.Explain}','{omdelMian.HUSEORGID}','{omdelMian.HCREATEORGID}')"); 
 | 
  
 | 
                    //新增后查询  确保数据添加成功 
 | 
                    ds = oCN.RunProcReturn("Select * from  System_UserGroup where GroupName='" + omdelMian.GroupName + "'", "System_UserGroup"); 
 | 
                    if (ds == null || ds.Tables[0].Rows.Count == 0) 
 | 
                    { 
 | 
                        objJsonResult.code = "0"; 
 | 
                        objJsonResult.count = 0; 
 | 
                        objJsonResult.Message = "用户新增失败!"; 
 | 
                        objJsonResult.data = null; 
 | 
                        return objJsonResult; 
 | 
                    } 
 | 
  
 | 
                    var HGroupID = DBUtility.ClsPub.isLong(ds.Tables[0].Rows[0]["GroupID"]); 
 | 
                    oCN.RunProc("delete from  System_UserGroupInfo where GroupId=" + HGroupID.ToString()); 
 | 
  
 | 
                    if (num[0] != "-1") 
 | 
                    { 
 | 
                        for (int i = 0; i < num.Length; i++) 
 | 
                        { 
 | 
                            oCN.RunProc("insert into  System_UserGroupInfo (GroupId,UserId) values (" + HGroupID.ToString() + ",'" + num[i] + "')"); 
 | 
                        } 
 | 
                    } 
 | 
                   
 | 
                } 
 | 
                else if (OperationType == 2)//复制 
 | 
                { 
 | 
                    //新增前查询  确保角色名未重复新增 
 | 
                    ds = oCN.RunProcReturn("Select * from  System_UserGroup where GroupName='" + omdelMian.GroupName + "'", "System_UserGroup"); 
 | 
                    if (ds.Tables[0].Rows.Count > 0) 
 | 
                    { 
 | 
                        objJsonResult.code = "0"; 
 | 
                        objJsonResult.count = 0; 
 | 
                        objJsonResult.Message = "角色新增失败!当前角色名已经存在,不可重复"; 
 | 
                        objJsonResult.data = null; 
 | 
                        return objJsonResult; 
 | 
                    } 
 | 
  
 | 
                    string realExplain = omdelMian.Explain.Substring(0, omdelMian.Explain.IndexOf("#复制原角色ID:"));        //获取界面设置的Explain值 
 | 
                    string sourceBill_GroupID = omdelMian.Explain.Replace(realExplain + "#复制原角色ID:", "");                     //获取复制的源单据的ID 
 | 
                    omdelMian.Explain = realExplain; 
 | 
  
 | 
                    //主表 
 | 
                    oCN.RunProc("insert into  System_UserGroup (GroupName,HLev,Explain,HUSEORGID,HCREATEORGID)values" + 
 | 
                        $"('{omdelMian.GroupName}','{omdelMian.HLev}','{omdelMian.Explain}','{omdelMian.HUSEORGID}','{omdelMian.HCREATEORGID}')"); 
 | 
  
 | 
  
 | 
                    //新增后查询  确保数据添加成功 
 | 
                    ds = oCN.RunProcReturn("Select * from  System_UserGroup where GroupName='" + omdelMian.GroupName + "'", "System_UserGroup"); 
 | 
                    if (ds == null || ds.Tables[0].Rows.Count == 0) 
 | 
                    { 
 | 
                        objJsonResult.code = "0"; 
 | 
                        objJsonResult.count = 0; 
 | 
                        objJsonResult.Message = "用户新增失败!"; 
 | 
                        objJsonResult.data = null; 
 | 
                        return objJsonResult; 
 | 
                    } 
 | 
                    var HGroupID = DBUtility.ClsPub.isLong(ds.Tables[0].Rows[0]["GroupID"]); 
 | 
  
 | 
  
 | 
                    //根据复制的源单据的ID,获取源单据的ID对应角色的权限 
 | 
                    ds = oCN.RunProcReturn("Select * from  System_UserGroup where GroupID='" + sourceBill_GroupID + "'", "System_UserGroup"); 
 | 
                    if (ds == null || ds.Tables[0].Rows.Count == 0) 
 | 
                    { 
 | 
                        objJsonResult.code = "0"; 
 | 
                        objJsonResult.count = 0; 
 | 
                        objJsonResult.Message = "被复制的角色不存在!"; 
 | 
                        objJsonResult.data = null; 
 | 
                        return objJsonResult; 
 | 
                    } 
 | 
                    string AuthorityID = ds.Tables[0].Rows[0]["AuthorityID"].ToString(); 
 | 
                    oCN.RunProc("update System_UserGroup set AuthorityID = '" + AuthorityID + "' where GroupID = " + HGroupID); 
 | 
  
 | 
  
 | 
                    //设置角色绑定的用户 
 | 
                    oCN.RunProc("delete from  System_UserGroupInfo where GroupId=" + HGroupID.ToString()); 
 | 
  
 | 
                    if (num[0] != "-1") 
 | 
                    { 
 | 
                        for (int i = 0; i < num.Length; i++) 
 | 
                        { 
 | 
                            oCN.RunProc("insert into  System_UserGroupInfo (GroupId,UserId) values (" + HGroupID.ToString() + ",'" + num[i] + "')"); 
 | 
                        } 
 | 
                    } 
 | 
  
 | 
                } 
 | 
                else if (OperationType == 3) 
 | 
                { 
 | 
                    oCN.RunProc("update  System_UserGroup set GroupName='" + omdelMian.GroupName + "',HLev='" + omdelMian.HLev + "',Explain='" + omdelMian.Explain + "',HUSEORGID='" + omdelMian.HUSEORGID + "',HCREATEORGID='" + omdelMian.HCREATEORGID + "'" 
 | 
                    + " where GroupId=" + omdelMian.HGroupID); 
 | 
  
 | 
                    oCN.RunProc("delete from  System_UserGroupInfo where GroupId=" + omdelMian.HGroupID); 
 | 
                    for (int i = 0; i < num.Length; i++) 
 | 
                    { 
 | 
                        oCN.RunProc("insert into  System_UserGroupInfo (GroupId,UserId) values (" + omdelMian.HGroupID + ",'" + num[i] + "')"); 
 | 
                    } 
 | 
                } 
 | 
               
 | 
                objJsonResult.code = "1"; 
 | 
                objJsonResult.count = 1; 
 | 
                objJsonResult.Message = null; 
 | 
                objJsonResult.data = null; 
 | 
                return objJsonResult; 
 | 
            } 
 | 
            catch (Exception e) 
 | 
            { 
 | 
                objJsonResult.code = "0"; 
 | 
                objJsonResult.count = 0; 
 | 
                objJsonResult.Message = "Exception!" + e.ToString(); 
 | 
                objJsonResult.data = null; 
 | 
                return objJsonResult; 
 | 
            } 
 | 
        } 
 | 
        #endregion 
 | 
  
 | 
        #region 角色列表 删除 
 | 
        [Route("Xt_UserGroup/DelXt_UserGroupList")] 
 | 
        [HttpGet] 
 | 
        public object DelXt_UserGroupList(string HInterID,string User) 
 | 
        { 
 | 
            try 
 | 
            { 
 | 
                ds = oCN.RunProcReturn("select * from h_v_System_UserGroup where 1=1 and HGroupID=" + HInterID, "h_v_System_UserGroup"); 
 | 
  
 | 
                if (ds.Tables[0].Rows.Count > 0) 
 | 
                { 
 | 
                    oCN.BeginTran();//开启事务 
 | 
                    //删除角色表 
 | 
                    oCN.RunProc($"delete from System_UserGroup where GroupID={HInterID}"); 
 | 
                    //删除关联的用户关系 
 | 
                    oCN.RunProc($"delete from System_UserGroupInfo where GroupId={HInterID}"); 
 | 
  
 | 
                    oCN.Commit();//结束事务 
 | 
  
 | 
                    objJsonResult.code = "1"; 
 | 
                    objJsonResult.count = 1; 
 | 
                    objJsonResult.Message = "删除成功!"; 
 | 
                    objJsonResult.data = null; 
 | 
                    return objJsonResult; 
 | 
                } 
 | 
  
 | 
                objJsonResult.code = "0"; 
 | 
                objJsonResult.count = 0; 
 | 
                objJsonResult.Message = "当前单据不存在,无法删除!"; 
 | 
                objJsonResult.data = null; 
 | 
                return objJsonResult; 
 | 
            } 
 | 
            catch (Exception e) 
 | 
            { 
 | 
                oCN.RollBack();//回滚事务 
 | 
                objJsonResult.code = "0"; 
 | 
                objJsonResult.count = 0; 
 | 
                objJsonResult.Message = e.ToString(); 
 | 
                objJsonResult.data = null; 
 | 
                return objJsonResult; 
 | 
            } 
 | 
        } 
 | 
        #endregion 
 | 
  
 | 
  
 | 
    } 
 | 
} 
 |