using Learun.DataBase.Repository; using Learun.Util; using System; using System.Collections.Generic; using System.Data; using System.Text; namespace Learun.Application.WorkFlow { /// /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园 /// Copyright (c) 2013-2018 北京泉江科技有限公司 /// 创建人:陈彬彬 /// 日 期:2017.04.17 /// 描 述:工作流模板处理 /// public class WfSchemeService : RepositoryFactory { #region 属性 构造函数 private string schemeInfoFieldSql; private string schemeFieldSql; public WfSchemeService() { schemeInfoFieldSql = @" t.F_Id, t.F_Code, t.F_Name, t.F_Category, t.F_Kind, t.F_SchemeId, t.F_DeleteMark, t.F_EnabledMark, t.F_Description "; schemeFieldSql = @" t.F_Id, t.F_SchemeInfoId, t.F_Type, t.F_CreateDate, t.F_CreateUserId, t.F_CreateUserName "; } #endregion #region 获取数据 /// /// 获取流程分页列表 /// /// 分页参数 /// 关键字 /// 分类 /// public IEnumerable GetSchemeInfoPageList(Pagination pagination, string keyword, string category) { try { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append(schemeInfoFieldSql); strSql.Append(",t1.F_Type,t1.F_CreateDate,t1.F_CreateUserId,t1.F_CreateUserName "); strSql.Append(" FROM LR_WF_SchemeInfo t LEFT JOIN LR_WF_Scheme t1 ON t.F_SchemeId = t1.F_Id WHERE 1=1 AND t.F_DeleteMark = 0 "); if (!string.IsNullOrEmpty(keyword)) { strSql.Append(" AND ( t.F_Name like @keyword OR t.F_Code like @keyword ) "); keyword = "%" + keyword + "%"; } if (!string.IsNullOrEmpty(category)) { strSql.Append(" AND t.F_Category = @category "); } return this.BaseRepository().FindList(strSql.ToString(), new { keyword = keyword, category = category }, pagination); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } /// /// 获取流程模板分页列表 /// /// 分页参数 /// 登录者信息 /// 查询参数 /// public IEnumerable GetAppSchemeInfoPageList(Pagination pagination, UserInfo userInfo, string queryJson) { try { string userId = userInfo.userId; string postIds = userInfo.postIds; string roleIds = userInfo.roleIds; List list = (List)this.BaseRepository().FindList(t => t.F_ObjectId == null || userId.Contains(t.F_ObjectId) || postIds.Contains(t.F_ObjectId) || roleIds.Contains(t.F_ObjectId) ); string schemeinfoIds = ""; foreach (var item in list) { schemeinfoIds += "'" + item.F_SchemeInfoId + "',"; } schemeinfoIds = "(" + schemeinfoIds.Remove(schemeinfoIds.Length - 1, 1) + ")"; var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append(schemeInfoFieldSql); strSql.Append(" FROM LR_WF_SchemeInfo t WHERE 1=1 AND t.F_DeleteMark = 0 AND t.F_EnabledMark = 1 AND t.F_Kind = 1 AND F_IsApp = 1 AND t.F_Id in " + schemeinfoIds); var queryParam = queryJson.ToJObject(); string keyword = ""; if (!queryParam["keyword"].IsEmpty()) { strSql.Append(" AND ( t.F_Name like @keyword OR t.F_Code like @keyword ) "); keyword = "%" + queryParam["keyword"].ToString() + "%"; } return this.BaseRepository().FindList(strSql.ToString(), new { keyword }, pagination); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } /// /// 获取自定义流程列表 /// /// 用户信息 /// public IEnumerable GetCustmerSchemeInfoList(UserInfo userInfo) { try { string userId = userInfo.userId; string postIds = userInfo.postIds; string roleIds = userInfo.roleIds; List list = (List)this.BaseRepository().FindList(t => t.F_ObjectId == null || userId.Contains(t.F_ObjectId) || postIds.Contains(t.F_ObjectId) || roleIds.Contains(t.F_ObjectId) ); string schemeinfoIds = ""; foreach (var item in list) { schemeinfoIds += "'" + item.F_SchemeInfoId + "',"; } schemeinfoIds = "(" + schemeinfoIds.Remove(schemeinfoIds.Length - 1, 1) + ")"; var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append(schemeInfoFieldSql); strSql.Append(" FROM LR_WF_SchemeInfo t WHERE 1=1 AND t.F_DeleteMark = 0 AND t.F_EnabledMark = 1 AND t.F_Kind = 1 AND t.F_Id in " + schemeinfoIds); return this.BaseRepository().FindList(strSql.ToString()); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } /// /// 获取自定义流程列表 /// /// 用户信息 /// public IEnumerable GetAppCustmerSchemeInfoList(UserInfo userInfo) { try { string userId = userInfo.userId; string postIds = userInfo.postIds; string roleIds = userInfo.roleIds; List list = (List)this.BaseRepository().FindList(t => t.F_ObjectId == null || userId.Contains(t.F_ObjectId) || postIds.Contains(t.F_ObjectId) || roleIds.Contains(t.F_ObjectId) ); string schemeinfoIds = ""; foreach (var item in list) { schemeinfoIds += "'" + item.F_SchemeInfoId + "',"; } schemeinfoIds = "(" + schemeinfoIds.Remove(schemeinfoIds.Length - 1, 1) + ")"; var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append(schemeInfoFieldSql); strSql.Append(" FROM LR_WF_SchemeInfo t WHERE 1=1 AND t.F_DeleteMark = 0 AND t.F_EnabledMark = 1 AND t.F_Kind = 1 AND F_IsApp = 1 AND t.F_Id in " + schemeinfoIds); return this.BaseRepository().FindList(strSql.ToString()); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } /// /// 获取模板列表 /// /// 模板信息主键 /// public IEnumerable GetWfSchemeList(string schemeInfoId) { try { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append(schemeFieldSql); strSql.Append(" FROM LR_WF_Scheme t WHERE 1=1 "); strSql.Append(" AND t.F_SchemeInfoId = @schemeInfoId "); return this.BaseRepository().FindList(strSql.ToString(), new { schemeInfoId = schemeInfoId }); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } /// /// 获取模板列表 /// /// 分页参数 /// 模板信息主键 /// public IEnumerable GetSchemePageList(Pagination pagination, string schemeInfoId) { try { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append(schemeFieldSql); strSql.Append(" FROM LR_WF_Scheme t WHERE 1=1 "); strSql.Append(" AND t.F_SchemeInfoId = @schemeInfoId "); return this.BaseRepository().FindList(strSql.ToString(), new { schemeInfoId = schemeInfoId }, pagination); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } /// /// 获取模板基础信息的实体 /// /// 主键 /// public WfSchemeInfoEntity GetWfSchemeInfoEntity(string keyValue) { try { return this.BaseRepository().FindEntity(keyValue); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } /// /// 获取模板基础信息的实体 /// /// 流程编号 /// public WfSchemeInfoEntity GetWfSchemeInfoEntityByCode(string code) { try { return this.BaseRepository().FindEntity(t => t.F_Code == code && t.F_DeleteMark == 0); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } /// /// 获取模板的实体 /// /// 主键 /// public WfSchemeEntity GetWfSchemeEntity(string keyValue) { try { return this.BaseRepository().FindEntity(keyValue); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } /// /// 获取流程模板权限列表 /// /// 模板信息主键 /// public IEnumerable GetWfSchemeAuthorizeList(string schemeInfoId) { try { return this.BaseRepository().FindList(t => t.F_SchemeInfoId == schemeInfoId); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } #endregion #region 提交数据 /// /// 虚拟删除模板信息 /// /// 主键 public void VirtualDelete(string keyValue) { try { WfSchemeInfoEntity entity = new WfSchemeInfoEntity() { F_Id = keyValue, F_DeleteMark = 1 }; this.BaseRepository().Update(entity); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } /// /// 保存模板信息 /// /// 主键 /// 模板基础信息 /// 模板信息 public void SaveEntity(string keyValue, WfSchemeInfoEntity wfSchemeInfoEntity, WfSchemeEntity wfSchemeEntity, List wfSchemeAuthorizeList) { IRepository db = new RepositoryFactory().BaseRepository().BeginTrans(); try { if (string.IsNullOrEmpty(keyValue)) { wfSchemeInfoEntity.Create(); } else { wfSchemeInfoEntity.Modify(keyValue); } #region 模板信息 if (wfSchemeEntity != null) { wfSchemeEntity.F_SchemeInfoId = wfSchemeInfoEntity.F_Id; wfSchemeEntity.Create(); db.Insert(wfSchemeEntity); wfSchemeInfoEntity.F_SchemeId = wfSchemeEntity.F_Id; } #endregion #region 模板基础信息 if (!string.IsNullOrEmpty(keyValue)) { db.Update(wfSchemeInfoEntity); } else { db.Insert(wfSchemeInfoEntity); } #endregion #region 流程模板权限信息 string schemeInfoId = wfSchemeInfoEntity.F_Id; db.Delete(t => t.F_SchemeInfoId == schemeInfoId); foreach (var wfSchemeAuthorize in wfSchemeAuthorizeList) { wfSchemeAuthorize.F_SchemeInfoId = schemeInfoId; db.Insert(wfSchemeAuthorize); } #endregion db.Commit(); } catch (Exception ex) { db.Rollback(); if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } /// /// 更新流程模板 /// /// 模板信息主键 /// 模板主键 public void UpdateScheme(string schemeInfoId, string schemeId) { try { WfSchemeEntity wfSchemeEntity = GetWfSchemeEntity(schemeId); WfSchemeInfoEntity entity = new WfSchemeInfoEntity { F_Id = schemeInfoId, F_SchemeId = schemeId }; if (wfSchemeEntity.F_Type != 1) { entity.F_EnabledMark = 0; } this.BaseRepository().Update(entity); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } /// /// 保存模板基础信息 /// /// 主键 /// 模板基础信息 public void SaveSchemeInfoEntity(string keyValue, WfSchemeInfoEntity schemeInfoEntity) { try { if (!string.IsNullOrEmpty(keyValue)) { schemeInfoEntity.Modify(keyValue); this.BaseRepository().Update(schemeInfoEntity); } else { schemeInfoEntity.Create(); this.BaseRepository().Insert(schemeInfoEntity); } } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } /// /// 更新自定义表单模板状态 /// /// 模板信息主键 /// 状态1启用0禁用 public void UpdateState(string schemeInfoId, int state) { try { WfSchemeInfoEntity entity = new WfSchemeInfoEntity { F_Id = schemeInfoId, F_EnabledMark = state }; this.BaseRepository().Update(entity); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } #endregion #region 扩展数据 /// /// 获取流程模板使用次数列表 /// /// 查询参数 /// public IEnumerable GetWfSchemeUseList(string queryJson) { try { //var dp = new DynamicParameters(new { }); var dp = new object(); var strSql = new StringBuilder(); strSql.Append("select si.F_Category,si.F_Name,si.F_Code,si.F_Kind,p.F_Id,p.F_CreateDate from"); strSql.Append(" [dbo].[LR_WF_SchemeInfo] si left join [dbo].[LR_WF_Scheme] s on si.F_Id=s.F_SchemeInfoId left join [dbo].[LR_WF_ProcessInstance] p on s.F_Id=p.F_SchemeId "); strSql.Append(" where si.F_DeleteMark=0 and si.F_EnabledMark=1 and s.F_Type=1"); var queryParam = queryJson.ToJObject(); if (!queryParam["year"].IsEmpty()) { //dp.Add("year", queryParam["year"].ToInt(), DbType.Int32); dp = new { year = queryParam["year"].ToInt() }; strSql.Append(" and DATEPART(yyyy,p.F_CreateDate) = @year "); } return this.BaseRepository().FindList(strSql.ToString(), dp); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } public IEnumerable GetWfSchemeStart() { try { var strSql = new StringBuilder(); // strSql.Append(@"select si.F_Category,si.F_Name,si.F_Code,si.F_Id,count(p.F_Id) from //[dbo].[LR_WF_SchemeInfo] si left join [dbo].[LR_WF_Scheme] s on si.F_Id=s.F_SchemeInfoId left join [dbo].[LR_WF_ProcessInstance] p on s.F_Id=p.F_SchemeId // where si.F_DeleteMark=0 and si.F_EnabledMark=1 and s.F_Type=1 and si.F_Kind=1 // group by si.F_Category,si.F_Name,si.F_Code,si.F_Id order by count(p.F_Id) desc"); strSql.Append(@"SELECT t.F_Category,t.F_Name,t.F_Code,t.F_Id,COUNT(t.F_Id) FROM LR_NWF_SchemeInfo t LEFT JOIN LR_NWF_Scheme t1 ON t.F_SchemeId = t1.F_Id WHERE t.F_EnabledMark=1 AND t1.F_Type=1 GROUP BY t.F_Category,t.F_Name,t.F_Code,t.F_Id ORDER BY COUNT(t.F_Id) DESC"); return this.BaseRepository().FindList(strSql.ToString()); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } #endregion } }