package com.yc.layoutIcon.newLayout.service.impl; import com.yc.action.grid.GridUtils; import com.yc.layoutIcon.newLayout.entity.*; import com.yc.layoutIcon.newLayout.service.LayoutIconGroupService; import com.yc.layoutIcon.newLayout.utils.layOutIconUtils; import com.yc.service.BaseService; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.sql.SQLException; import java.util.List; /** * @USER: xinyb_ * @DATE: 2021-09-26 14:22 */ @Service public class LayoutIconGroupServiceImpl extends BaseService implements LayoutIconGroupService { @Override public List getLayoutTree(String userCode, Integer menuId) { try { String sql = "select u.menuid as id,u.menuname as name,u.formid as formId,u.formtype as formType,u.largimagepath,case when isnull(a.usercode,'') <> '' then isnull(a.ishide,0) else 1 end as ishide \n" + " from usermenu(" + GridUtils.prossSqlParm(userCode) + ") u \n" + " left join _sysWebMenuAccess a on a.menuid=u.menuid and a.UserCode=" + GridUtils.prossSqlParm(userCode) + " where u.parentmenu=" + menuId + " order by u.sortBy"; return super.jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(LayoutTree.class)); } catch (Exception e) { throw e; } } @Override public List selectIconGroup(LayoutParam param) { try { String sql = "select g.menuid,g.menuname,g.largImagePath,g.sortBy,g.formid,g.formname,g.formtype,g.menuType " + " from getSysWebMenuAccess(" +GridUtils.prossSqlParm(param.getUserCode()) + ",''," + param.getMenuId() + ",'') g where g.dispLageImageYN=1 order by g.sortBy"; return layOutIconUtils.assembleIconGroup(super.jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(LayoutIconGroup.class))); } catch (Exception e) { throw e; } } @Override //当我们直接使用@Transactional不指定rollbackFor时,Exception及其子类都不会触发回滚。所以要添加导致事务回滚的异常类数组 @Transactional(rollbackFor = {Exception.class, SQLException.class}) public Integer setIconGroup(LayoutParam param) throws Exception { try { if (param.getIcons() != null && param.getIcons().size() > 0) { Integer cont = 0; String sql = "set nocount on ; \n" + " declare @table table (UserCode varchar(50),UserName varchar(100),menuid int,menuname varchar(50),menuType varchar(20),SortBy int)"; for (LayoutIconEntity i : param.getIcons()) { sql += " insert into @table(UserCode,UserName,menuid,menuname,menuType,SortBy) " + " values(" + GridUtils.prossSqlParm(param.getUserCode()) + "," + GridUtils.prossSqlParm(param.getUserName()) + "," + i.getMenuId() + "," + GridUtils.prossSqlParm(i.getMenuName()) + "," + GridUtils.prossSqlParm(i.getMenuType()) + "," + i.getSortBy() + ") \n"; } sql += " update a set SortBy = b.SortBy,isHide=0 from _sysWebMenuAccess a \n" + " join @table b on a.UserCode = b.UserCode and a.MenuId = b.MenuId \n" + " insert into _sysWebMenuAccess(UserCode,UserName,menuid,menuname,menuType,SortBy,isHide) \n" + " select a.UserCode,a.UserName,a.menuid,a.menuname,a.menuType,a.SortBy,0 \n" + " from @table a \n" + " where not exists(select 1 from _sysWebMenuAccess b where a.UserCode=b.UserCode and a.MenuId = b.MenuId ) \n" + " update a set isHide = 1 \n" + " from _sysWebMenuAccess a where a.UserCode=" + GridUtils.prossSqlParm(param.getUserCode()) + " and isnull(a.ProfileId,'') <> '' " + " and not exists(select 1 from @table b where a.UserCode = b.UserCode and a.MenuId = b.MenuId ) \n" + " delete a from _sysWebMenuAccess a \n" + " where a.UserCode=" + GridUtils.prossSqlParm(param.getUserCode()) + " and isnull(a.ProfileId,'') = '' \n" + " and not exists(select 1 from @table b where a.UserCode = b.UserCode and a.MenuId = b.MenuId ) \n" + " select 1 \n"; cont = super.jdbcTemplate.queryForObject(sql, Integer.class); return cont; } else { throw new Exception("至少保留一个大图标,不能全部删除。"); } } catch (Exception e) { throw e; } } }