using MonumentDefine; using OT.COM.ArsenalDB; using OT.COM.SignalerMessage; using SoldierData.EnterprizeV4; using System; using System.Collections.Generic; using System.Linq; namespace CounsellorBL.Helper { public class ProjectHelper : DBService { public override string MainTable => null; #region 使用者資訊 /// /// 取得使用者資訊 /// /// /// public static tb_hr_employee GetLoginUser(CRequestMessage i_crmInput) { tb_hr_employee loginUser = null; if (i_crmInput == null) { throw new ArgumentNullException(nameof(i_crmInput)); } var token = i_crmInput.token; if (!string.IsNullOrEmpty(token)) { QueryJsonElementCollection lBlocks = new QueryJsonElementCollection(); QueryJsonElement qjeSession = lBlocks.GetInst(); qjeSession.table = tb_sys_session.TABLENAME; qjeSession.wherecols = new WhereNode(tb_sys_session.CN_UID, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_sys_session), token); // 人員資訊 QueryJsonElement qjeEmployee = lBlocks.GetInst(); qjeEmployee.table = tb_hr_employee.TABLENAME; qjeEmployee.jointype = QueryJsonElement.JOIN; qjeEmployee.joincols = new Dictionary() { { tb_hr_employee.CN_UID, tb_sys_session.CN_CREATE_USER_UID } }; qjeEmployee.jointable = qjeSession; qjeEmployee.displaycols = new List() { tb_hr_employee.CN_UID, tb_hr_employee.CN_RECEIVE_BRANCH_UID }; lBlocks.Add(qjeSession); lBlocks.Add(qjeEmployee); var sMsg = MakeSelectJoinByBlocks(lBlocks, out Command cRes); if (sMsg != null) { return loginUser; } cRes.ReadLevel = Command.EReadLevel.ERL_DIRTY; ArsenalInterface ai = ArsenalDBMgr.GetInst(cRes); loginUser = ai.RunQuerySingleORM(cRes); } return loginUser; } /// /// 判斷是否為admin角色 /// /// /// private static bool CheckIsAdmin(string i_sUserUID) { bool isAdmin = false; tb_sys_user2role cRole = new tb_sys_user2role(); QueryJsonElementCollection lBlocks = new QueryJsonElementCollection(); QueryJsonElement qjUser2eRole = lBlocks.GetInst(); qjUser2eRole.table = tb_sys_user2role.TABLENAME; qjUser2eRole.wherecols = new WhereNode(tb_sys_user2role.CN_USER_UID, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_sys_user2role), i_sUserUID); lBlocks.Add(qjUser2eRole); QueryJsonElement qjeRole = lBlocks.GetInst(); qjeRole.table = tb_sys_role.TABLENAME; qjeRole.jointype = QueryJsonElement.LEFT_JOIN; qjeRole.joincols = new Dictionary() { { tb_sys_role.CN_UID, tb_sys_user2role.CN_ROLE_UID } }; qjeRole.jointable = qjUser2eRole; qjeRole.displaycols = new List() { tb_sys_role.CN_NAME }; lBlocks.Add(qjeRole); // 查詢資料 MakeSelectJoinByBlocks(lBlocks, out Command cSelect); ArsenalInterface ai = ArsenalDBMgr.GetInst(cSelect); List lRes = ai.RunQueryList(cSelect, null); var lsRoleName = lRes.Select(x => x.name).ToList(); if (CustomizeDBMgr.SettingData.ContainsKey(BLWording.ADMIN_NAME)) { if (lsRoleName.Contains(CustomizeDBMgr.SettingData[BLWording.ADMIN_NAME])) { isAdmin = true; } } return isAdmin; } /// /// 取得登入者所屬社團 /// /// /// public static List GetUserGroup(CRequestMessage i_crmInput) { var userUID = GetLoginUser(i_crmInput)?.uid; if (!string.IsNullOrEmpty(userUID)) { ArsenalInterface ai = null; bool isAdmin = CheckIsAdmin(userUID); // 判斷admin var lsGroup = new List(); if (isAdmin) { // 取得所有社團 tb_grp_group cGroup = new tb_grp_group(); cGroup.SetDirty(tb_grp_group.CN_UID); tb_grp_group cGroupCon = new tb_grp_group() { status_flag = (int)Enums.Flag.Enable }; Command cGroupSelect = Command.SetupSelectCmd(cGroup, cGroupCon); ai = ArsenalDBMgr.GetInst(cGroupSelect); List qdsGroup = ai.RunQueryList(cGroupSelect); lsGroup = qdsGroup.Select(x => x.uid).ToList(); } else { // 所屬社團 QueryJsonElementCollection lBlocks = new QueryJsonElementCollection(); QueryJsonElement qjeEmp2Branch = lBlocks.GetInst(); qjeEmp2Branch.table = tb_hr_employee2branch.TABLENAME; qjeEmp2Branch.wherecols = new WhereNode(tb_hr_employee2branch.CN_UID, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_hr_employee2branch), userUID); lBlocks.Add(qjeEmp2Branch); QueryJsonElement qjeBranch = lBlocks.GetInst(); qjeBranch.table = tb_grp_branch.TABLENAME; qjeBranch.jointype = QueryJsonElement.LEFT_JOIN; qjeBranch.joincols = new Dictionary() { { tb_grp_branch.CN_UID, tb_hr_employee2branch.CN_BRANCH_UID } }; qjeBranch.jointable = qjeEmp2Branch; qjeBranch.wherecols = new WhereNode(tb_grp_branch.CN_STATUS_FLAG, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_grp_branch), (int)Enums.Flag.Enable); lBlocks.Add(qjeBranch); QueryJsonElement qjeGroup = lBlocks.GetInst(); qjeGroup.table = tb_grp_group.TABLENAME; qjeGroup.jointype = QueryJsonElement.LEFT_JOIN; qjeGroup.joincols = new Dictionary() { { tb_grp_group.CN_UID, tb_grp_branch.CN_GROUP_UID } }; qjeGroup.jointable = qjeBranch; qjeGroup.displaycols = new List() { tb_grp_group.CN_UID }; qjeGroup.wherecols = new WhereNode(tb_grp_group.CN_STATUS_FLAG, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_grp_group), (int)Enums.Flag.Enable); lBlocks.Add(qjeGroup); // 查詢資料 MakeSelectJoinByBlocks(lBlocks, out Command cBranchSelect); ai = ArsenalDBMgr.GetInst(cBranchSelect); List lResGroup = ai.RunQueryList(cBranchSelect, null); // 自行建立的社團 tb_grp_group cGroup = new tb_grp_group(); cGroup.SetDirty(tb_grp_group.CN_UID); tb_grp_group cGroupCon = new tb_grp_group() { create_user_uid = userUID, status_flag = (int)Enums.Flag.Enable }; Command cGroupSelect = Command.SetupSelectCmd(cGroup, cGroupCon); ai = ArsenalDBMgr.GetInst(cGroupSelect); List qdsGroup = ai.RunQueryList(cGroupSelect); lsGroup = lResGroup.Union(qdsGroup).Select(x => x.uid).ToList(); } return lsGroup; } else { return new List(); } } /// /// 取得登入者人員管理所設定之所屬分店 /// /// /// public static List GetUserBranch(CRequestMessage i_crmInput) { var userUID = GetLoginUser(i_crmInput)?.uid; if (!string.IsNullOrEmpty(userUID)) { ArsenalInterface ai = null; bool isAdmin = CheckIsAdmin(userUID); // 判斷admin var lsBranch = new List(); if (isAdmin) { // 取得所有分店 tb_grp_branch cBranch = new tb_grp_branch(); cBranch.SetDirty(tb_grp_branch.CN_UID); tb_grp_branch cBranchCon = new tb_grp_branch() { status_flag = (int)Enums.Flag.Enable }; Command cSelect = Command.SetupSelectCmd(cBranch, cBranchCon); ai = ArsenalDBMgr.GetInst(cSelect); List qdsBranch = ai.RunQueryList(cSelect); lsBranch = qdsBranch.Select(x => x.uid).ToList(); } else { QueryJsonElementCollection lBlocks = new QueryJsonElementCollection(); QueryJsonElement qjeEmp2Branch = lBlocks.GetInst(); qjeEmp2Branch.table = tb_hr_employee2branch.TABLENAME; qjeEmp2Branch.wherecols = new WhereNode(tb_hr_employee2branch.CN_UID, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_hr_employee2branch), userUID); qjeEmp2Branch.displaycols = new List() { tb_hr_employee2branch.CN_BRANCH_UID }; lBlocks.Add(qjeEmp2Branch); // 查詢資料 MakeSelectJoinByBlocks(lBlocks, out Command cBranchSelect); ai = ArsenalDBMgr.GetInst(cBranchSelect); List lRes = ai.RunQueryList(cBranchSelect, null); lsBranch = lRes.Select(x => x.branch_uid).ToList(); } return lsBranch; } else { return new List(); } } /// /// 取得登入者人員管理所設定之所屬分店Key與分店名稱, 權限綁定 /// /// /// public static List<(string Uid, string BranchName)> GetUserBranchAndName(CRequestMessage i_crmInput) { var branch = GetUserBranch(i_crmInput); var branchAndName = new List<(string, string)>(); QueryJsonElementCollection lBlocks = new QueryJsonElementCollection(); QueryJsonElement qjeMain = lBlocks.GetInst(); qjeMain.table = tb_grp_branch.TABLENAME; qjeMain.displaycols = new List() { tb_grp_branch.CN_UID, tb_grp_branch.CN_BRANCH_NAME }; qjeMain.wherecols = new WhereNode(tb_grp_branch.CN_UID, WhereNode.EColumnOperation.EOT_IN, typeof(tb_grp_branch), branch.ToArray()); lBlocks.Add(qjeMain); var sMsg = MakeSelectJoinByBlocks(lBlocks, out Command cSelect); var ai = ArsenalDBMgr.GetInst(cSelect); List qdsBranch = ai.RunQueryList(cSelect); branchAndName = qdsBranch.Select(x => (x.uid, x.branch_name)).ToList(); return branchAndName; } #endregion } }