package cn.com.taiji.zhongxiao.service; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; import java.util.Vector; import javax.inject.Inject; import javax.persistence.EntityManager; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaBuilder.In; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import javax.persistence.criteria.Subquery; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Direction; import org.springframework.data.domain.Sort.Order; import org.springframework.data.jpa.domain.Specification; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import cn.com.taiji.sys.domain.Dept; import cn.com.taiji.sys.domain.DeptRepository; import cn.com.taiji.sys.domain.Role; import cn.com.taiji.sys.domain.User; import cn.com.taiji.sys.domain.UserRepository; import cn.com.taiji.util.Constants; import cn.com.taiji.util.Pagination; import cn.com.taiji.util.StringTool; import cn.com.taiji.util.ToolUtil; import cn.com.taiji.zhongxiao.domain.TcodeSetRepository; import cn.com.taiji.zhongxiao.domain.TdataMeta; import cn.com.taiji.zhongxiao.domain.TdataMetaRepository; import cn.com.taiji.zhongxiao.dto.TdataMetaDto; @Service public class DatametaService extends BaseService { @Inject TinfoCodeService tinfoCodeService; @Value("${database_type}") String database_type; @Value("${admin_id}") String admin_id; @Value("${bmsh_role}") String bmsh_role; @Value("${zgbmsh_role}") String zgbmsh_role; @Inject EntityManager em; @Inject JdbcTemplate jdbcTemplate; @Inject TdataMetaRepository tdataMetaRepository; @Inject TcodeSetRepository tcodeSetRepository; @Inject DeptRepository deptRepository; @Inject UserRepository userRepository; // 获取所有角色列表 @Transactional @SuppressWarnings({ "unchecked", "rawtypes" }) public Pagination findAllDatametaList(Map searchParameters, Pagination pageUtil, final TdataMetaDto dto,final User userInfo) { int page = 0; int pageSize = Constants.DEFAULT_ROWS; Page pageList; if (searchParameters != null && searchParameters.size() > 0 && searchParameters.get("page") != null) { page = Integer.parseInt(StringTool.null2Empty(searchParameters.get("page").toString())) - 1; } if (searchParameters != null && searchParameters.size() > 0 && searchParameters.get("pageSize") != null) { pageSize = Integer.parseInt(StringTool.null2Empty(searchParameters.get("pageSize").toString())); } if (pageSize < 1) pageSize = 1; if (pageSize > 1001) pageSize = 1000; List orders = new ArrayList(); PageRequest pageable; Order order3 = new Order(Direction.DESC, "state"); orders.add(order3); Sort sort = new Sort(orders); pageable = new PageRequest(page, pageSize, sort); if (dto != null) { Specification spec = new Specification() { @Override public Predicate toPredicate(Root root , CriteriaQuery query, CriteriaBuilder cb) { List pl = new ArrayList(); Set serRole = userInfo.getRoles(); boolean flag = false; boolean flag_bmsh_role = false; boolean flag_zgbmsh_role = false; for (Role role : serRole) { if(admin_id.equals(role.getRoleId())&&!"3".equals(dto.getDescription())) flag = true; if(bmsh_role.equals(role.getRoleId())&&!"3".equals(dto.getDescription())){ flag_bmsh_role=true; } if(zgbmsh_role.equals(role.getRoleId())&&!"3".equals(dto.getDescription())){ flag_zgbmsh_role=true; } } if(dto.getId()!=null && dto.getId().length() > 0 ){ pl.add(cb.like(root.get("id"), "%" + dto.getId() + "%")); } if(dto.getMetaname()!=null && dto.getMetaname().length() > 0 ){ pl.add(cb.like(root.get("metaname"), "%" + dto.getMetaname() + "%")); } if(dto.getMetacode()!=null && dto.getMetacode().length() > 0 ){ pl.add(cb.like(root.get("metacode"), "%" + dto.getMetacode() + "%")); } if(dto.getDatacode()!=null && dto.getDatacode().length() > 0 ){ pl.add(cb.like(root.get("datacode"), "%" + dto.getDatacode() + "%")); } //提供方部门 if(dto.getCreatorOrgname()!=null && dto.getCreatorOrgname().length() > 0 ){ pl.add(cb.like(root.get("creatorOrgname"), "%" + dto.getCreatorOrgname() + "%")); } if(dto.getDescription()!=null && dto.getDescription().length() > 0 ){ In in = cb.in(root.get("state")); String description = dto.getDescription(); String [] strArgs = description.split(","); for (String st : strArgs) { in.value(st); } pl.add(in); } if(dto.getState()!=null && dto.getState().length() > 0 ){ pl.add(cb.equal(root.get("state"), dto.getState())); } if(!(dto.getDescription()!=null && dto.getDescription().length() > 0&&"3".equals(dto.getDescription()) )){ if(dto.getCreator() !=null&&!flag&&!flag_bmsh_role&&!flag_zgbmsh_role){ pl.add(cb.or((cb.equal(root.get("creator") ,dto.getCreator())))); }} if(!flag&&("2,5".equals(dto.getDescription())||"3,4".equals(dto.getDescription())||dto.getCreator() !=null)){ //代码集审核 Predicate[] statList1 = new Predicate[2]; if(flag_bmsh_role) { In in = cb.in(root.get("creatorOrg")); String []strArgs=dto.getCreatorOrg().split(","); Boolean exist=false; //获取当前用户所处机构 for (String st : strArgs) { if(!ToolUtil.isNull(st)){ in.value(st); exist=true; } } if(!exist){ in.value(""); } if("2,5".equals(dto.getDescription())) { statList1[0]=cb.and(in,cb.equal(root.get("state") ,"2")); } else { statList1[0]=cb.and(in,cb.or( cb.equal(root.get("state") ,"3") ,cb.equal(root.get("state") ,"4") )); } } if(flag_zgbmsh_role) { In in = cb.in(root.get("xzqhcode")); Boolean exist=false; //获取当前用户所处机构 for (Dept st : userInfo.getDepts()) { in.value(st.getXzqhcode()); exist=true; } if(!exist){ in.value(""); } if("2,5".equals(dto.getDescription())) { statList1[0]=cb.and(in,cb.equal(root.get("state") ,"5")); } else { statList1[0]=cb.and(in,cb.or( cb.equal(root.get("state") ,"3") ,cb.equal(root.get("state") ,"4") )); } } if(statList1[0]!=null&&statList1[1]!=null) { pl.add(cb.or(statList1)); } else if(statList1[0]!=null) { pl.add(statList1[0]); } else if(statList1[1]!=null) { pl.add(statList1[1]); } } Predicate[] p = new Predicate[pl.size()]; return cb.and(pl.toArray(p)); } }; pageList = tdataMetaRepository.findAll(spec, pageable); } else { Specification spec = new Specification() { public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) { List pl = new ArrayList(); Predicate[] p = new Predicate[0]; return cb.and(pl.toArray(p)); } }; pageList = tdataMetaRepository.findAll(spec, pageable); } List list = pageList.getContent(); pageUtil.setPageResult(list);// 结果集 pageUtil.setPageTotal(pageList.getTotalElements());// 总数 pageUtil.setPageSize(pageSize);// 每页行数 pageUtil.setPageCurrentNum(page + 1);// 当前页 int totalPage = ((int) pageUtil.getPageTotal() + pageUtil.getPageSize() - 1) / pageUtil.getPageSize(); pageUtil.setPageCount(totalPage);// 总页数 return pageUtil; } public TdataMeta InsertRole(final TdataMetaDto dto) { TdataMeta cf = new TdataMeta(); BeanUtils.copyProperties(dto, cf); cf.setId(ToolUtil.getUUID()); return tdataMetaRepository.save(cf); } @Transactional(propagation = Propagation.SUPPORTS) public TdataMeta findTdataMetaById(String id) { TdataMeta cf = tdataMetaRepository.findOne(id); return cf; } public void updateTdataMeta(final TdataMetaDto dto, String createdate, String userId, String userName, String updatorOrg, String updateOrgName, String updatetime) { TdataMeta cf = tdataMetaRepository.findOne(dto.getId()); cf.setDescription(dto.getDescription()); // 说明 cf.setMetaname(dto.getMetaname());// 数据源名称 cf.setDatasize(dto.getDatasize());// 长度 cf.setRanges(dto.getRanges());// 值域 // 数据源类别 cf.setMetacodeid(dto.getMetacodeid());// 字符型 字典id cf.setMetacodeDis(dto.getMetacodeDis()); cf.setMetacode(dto.getMetacode());// 字符型 // 数据源类型 cf.setDatatypeid(dto.getDatatypeid()); cf.setDatatypeDis(dto.getDatatypeDis()); cf.setDatatype(dto.getDatatype()); cf.setCreatedate(createdate); cf.setUpdatetime(updatetime); cf.setUpdator(userId); cf.setUpdatorName(userName); cf.setUpdatorOrg(updatorOrg); cf.setUpdateOrgName(updateOrgName); tdataMetaRepository.saveAndFlush(cf); } @Transactional(propagation = Propagation.REQUIRED) public void deleteByTdataMeta(String id) { tdataMetaRepository.delete(id); } @Transactional(propagation = Propagation.REQUIRED) public void submintDatametaByIds(String id) { tdataMetaRepository.submintDatametaByIds(id); } @Transactional(propagation = Propagation.REQUIRED) public void audioSubmintDatametaByIds(String [] ids, String state) { if( ids!=null && ids.length>0 && !ids[0].isEmpty() ){ if("3".equals(state) || "4".equals(state) ){ tdataMetaRepository.audioSubmintDatametaByIds(ids, state); } } } @Transactional(propagation = Propagation.REQUIRED) public void audioTdataMeta(String checkdate, String checker, String checkerName, String checkerOrg, String checkerOrgName, String auditDescript, String state, String id,String metacode) { String datacode=""; if("3".equals(state)){ datacode=tinfoCodeService.getInfoCodeByPrecode("t_datameta"+metacode); if(datacode.length()<6) datacode=metacode+ToolUtil.lpad(datacode, 6, "0"); else datacode=metacode+datacode; } tdataMetaRepository.audioTdataMeta(checkdate, checker, checkerName, checkerOrg, checkerOrgName, auditDescript, state, id,datacode ); } public List> findTdataMetaByIds(String ids) { // TODO Auto-generated method stub List> list = jdbcTemplate.queryForList("select * from t_datameta where id in ("+ids+") "); // List list = tdataMetaRepository.findTdataMetaByIds(str); for (Map map : list) { map.put("STATE", state2Chinese(map.get("STATE").toString()) ); } return list; } private static String state2Chinese(String STATE){ switch (STATE) { case "1": return "保存"; case "2": return "待审核"; case "3": return "启用"; case "4": return "停用"; case "5": return "审核通过"; case "6": return "退回修改"; } return STATE; } public void saveInfoList(List resultList) { // TODO Auto-generated method stub tdataMetaRepository.save(resultList); } public List getAll(Vector v1, String id) { StringBuffer sql=new StringBuffer("select id,metaname from t_datameta " ); List infoList = new ArrayList(); List paramList=new ArrayList(); sql.append(" where id!=? and "); paramList.add(id); String s=""; for(String str:v1){ s+=str+"|"; } if(!ToolUtil.isNull(s)){ if("oracle".equals(database_type)){ sql.append(" REGEXP_LIKE(metaname, ?) "); paramList.add(s.substring(0, s.length()-1)+""); } else if("mysql".equals(database_type)){ sql.append(" metaname REGEXP ? "); paramList.add(s.substring(0, s.length()-1)+""); } } Object[] args = new Object[]{}; int length = paramList.size(); if(length>0){ args=new Object[length]; for(int i=0;i() { @Override public TdataMetaDto mapRow(ResultSet rs, int rowNum) throws SQLException { TdataMetaDto info = new TdataMetaDto(); info.setId(rs.getString("id")); info.setMetaname(rs.getString("metaname")); return info; } } ); return infoList; } public List findTdataMetaByDatacode(String datacode) { // TODO Auto-generated method stub return tdataMetaRepository.findTdataMetaByDatacode(datacode); } }