DatametaService.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. package cn.com.taiji.zhongxiao.service;
  2. import java.sql.ResultSet;
  3. import java.sql.SQLException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Set;
  8. import java.util.Vector;
  9. import javax.inject.Inject;
  10. import javax.persistence.EntityManager;
  11. import javax.persistence.criteria.CriteriaBuilder;
  12. import javax.persistence.criteria.CriteriaBuilder.In;
  13. import javax.persistence.criteria.CriteriaQuery;
  14. import javax.persistence.criteria.Predicate;
  15. import javax.persistence.criteria.Root;
  16. import javax.persistence.criteria.Subquery;
  17. import org.springframework.beans.BeanUtils;
  18. import org.springframework.beans.factory.annotation.Value;
  19. import org.springframework.data.domain.Page;
  20. import org.springframework.data.domain.PageRequest;
  21. import org.springframework.data.domain.Sort;
  22. import org.springframework.data.domain.Sort.Direction;
  23. import org.springframework.data.domain.Sort.Order;
  24. import org.springframework.data.jpa.domain.Specification;
  25. import org.springframework.jdbc.core.JdbcTemplate;
  26. import org.springframework.jdbc.core.RowMapper;
  27. import org.springframework.stereotype.Service;
  28. import org.springframework.transaction.annotation.Propagation;
  29. import org.springframework.transaction.annotation.Transactional;
  30. import cn.com.taiji.sys.domain.Dept;
  31. import cn.com.taiji.sys.domain.DeptRepository;
  32. import cn.com.taiji.sys.domain.Role;
  33. import cn.com.taiji.sys.domain.User;
  34. import cn.com.taiji.sys.domain.UserRepository;
  35. import cn.com.taiji.util.Constants;
  36. import cn.com.taiji.util.Pagination;
  37. import cn.com.taiji.util.StringTool;
  38. import cn.com.taiji.util.ToolUtil;
  39. import cn.com.taiji.zhongxiao.domain.TcodeSetRepository;
  40. import cn.com.taiji.zhongxiao.domain.TdataMeta;
  41. import cn.com.taiji.zhongxiao.domain.TdataMetaRepository;
  42. import cn.com.taiji.zhongxiao.dto.TdataMetaDto;
  43. @Service
  44. public class DatametaService extends BaseService {
  45. @Inject
  46. TinfoCodeService tinfoCodeService;
  47. @Value("${database_type}")
  48. String database_type;
  49. @Value("${admin_id}")
  50. String admin_id;
  51. @Value("${bmsh_role}")
  52. String bmsh_role;
  53. @Value("${zgbmsh_role}")
  54. String zgbmsh_role;
  55. @Inject
  56. EntityManager em;
  57. @Inject
  58. JdbcTemplate jdbcTemplate;
  59. @Inject
  60. TdataMetaRepository tdataMetaRepository;
  61. @Inject
  62. TcodeSetRepository tcodeSetRepository;
  63. @Inject
  64. DeptRepository deptRepository;
  65. @Inject
  66. UserRepository userRepository;
  67. // 获取所有角色列表
  68. @Transactional
  69. @SuppressWarnings({ "unchecked", "rawtypes" })
  70. public Pagination<TdataMeta> findAllDatametaList(Map<String, Object> searchParameters, Pagination pageUtil,
  71. final TdataMetaDto dto,final User userInfo) {
  72. int page = 0;
  73. int pageSize = Constants.DEFAULT_ROWS;
  74. Page<TdataMeta> pageList;
  75. if (searchParameters != null && searchParameters.size() > 0 && searchParameters.get("page") != null) {
  76. page = Integer.parseInt(StringTool.null2Empty(searchParameters.get("page").toString())) - 1;
  77. }
  78. if (searchParameters != null && searchParameters.size() > 0 && searchParameters.get("pageSize") != null) {
  79. pageSize = Integer.parseInt(StringTool.null2Empty(searchParameters.get("pageSize").toString()));
  80. }
  81. if (pageSize < 1)
  82. pageSize = 1;
  83. if (pageSize > 1001)
  84. pageSize = 1000;
  85. List<Order> orders = new ArrayList<Sort.Order>();
  86. PageRequest pageable;
  87. Order order3 = new Order(Direction.DESC, "state");
  88. orders.add(order3);
  89. Sort sort = new Sort(orders);
  90. pageable = new PageRequest(page, pageSize, sort);
  91. if (dto != null) {
  92. Specification<TdataMeta> spec = new Specification<TdataMeta>() {
  93. @Override
  94. public Predicate toPredicate(Root<TdataMeta> root , CriteriaQuery<?> query, CriteriaBuilder cb) {
  95. List<Predicate> pl = new ArrayList<Predicate>();
  96. Set<Role> serRole = userInfo.getRoles();
  97. boolean flag = false;
  98. boolean flag_bmsh_role = false;
  99. boolean flag_zgbmsh_role = false;
  100. for (Role role : serRole) {
  101. if(admin_id.equals(role.getRoleId())&&!"3".equals(dto.getDescription()))
  102. flag = true;
  103. if(bmsh_role.equals(role.getRoleId())&&!"3".equals(dto.getDescription())){
  104. flag_bmsh_role=true;
  105. }
  106. if(zgbmsh_role.equals(role.getRoleId())&&!"3".equals(dto.getDescription())){
  107. flag_zgbmsh_role=true;
  108. }
  109. }
  110. if(dto.getId()!=null && dto.getId().length() > 0 ){
  111. pl.add(cb.like(root.<String>get("id"), "%" + dto.getId() + "%"));
  112. }
  113. if(dto.getMetaname()!=null && dto.getMetaname().length() > 0 ){
  114. pl.add(cb.like(root.<String>get("metaname"), "%" + dto.getMetaname() + "%"));
  115. }
  116. if(dto.getMetacode()!=null && dto.getMetacode().length() > 0 ){
  117. pl.add(cb.like(root.<String>get("metacode"), "%" + dto.getMetacode() + "%"));
  118. }
  119. if(dto.getDatacode()!=null && dto.getDatacode().length() > 0 ){
  120. pl.add(cb.like(root.<String>get("datacode"), "%" + dto.getDatacode() + "%"));
  121. }
  122. //提供方部门
  123. if(dto.getCreatorOrgname()!=null && dto.getCreatorOrgname().length() > 0 ){
  124. pl.add(cb.like(root.<String>get("creatorOrgname"), "%" + dto.getCreatorOrgname() + "%"));
  125. }
  126. if(dto.getDescription()!=null && dto.getDescription().length() > 0 ){
  127. In<Object> in = cb.in(root.get("state"));
  128. String description = dto.getDescription();
  129. String [] strArgs = description.split(",");
  130. for (String st : strArgs) {
  131. in.value(st);
  132. }
  133. pl.add(in);
  134. }
  135. if(dto.getState()!=null && dto.getState().length() > 0 ){
  136. pl.add(cb.equal(root.<String>get("state"), dto.getState()));
  137. }
  138. if(!(dto.getDescription()!=null && dto.getDescription().length() > 0&&"3".equals(dto.getDescription()) )){
  139. if(dto.getCreator() !=null&&!flag&&!flag_bmsh_role&&!flag_zgbmsh_role){
  140. pl.add(cb.or((cb.equal(root.get("creator") ,dto.getCreator()))));
  141. }}
  142. if(!flag&&("2,5".equals(dto.getDescription())||"3,4".equals(dto.getDescription())||dto.getCreator() !=null)){
  143. //代码集审核
  144. Predicate[] statList1 = new Predicate[2];
  145. if(flag_bmsh_role) {
  146. In<Object> in = cb.in(root.get("creatorOrg"));
  147. String []strArgs=dto.getCreatorOrg().split(",");
  148. Boolean exist=false;
  149. //获取当前用户所处机构
  150. for (String st : strArgs) {
  151. if(!ToolUtil.isNull(st)){
  152. in.value(st);
  153. exist=true;
  154. }
  155. }
  156. if(!exist){
  157. in.value("");
  158. }
  159. if("2,5".equals(dto.getDescription())) {
  160. statList1[0]=cb.and(in,cb.equal(root.get("state") ,"2"));
  161. }
  162. else {
  163. statList1[0]=cb.and(in,cb.or(
  164. cb.equal(root.get("state") ,"3")
  165. ,cb.equal(root.get("state") ,"4")
  166. ));
  167. }
  168. }
  169. if(flag_zgbmsh_role) {
  170. In<Object> in = cb.in(root.get("xzqhcode"));
  171. Boolean exist=false;
  172. //获取当前用户所处机构
  173. for (Dept st : userInfo.getDepts()) {
  174. in.value(st.getXzqhcode());
  175. exist=true;
  176. }
  177. if(!exist){
  178. in.value("");
  179. }
  180. if("2,5".equals(dto.getDescription())) {
  181. statList1[0]=cb.and(in,cb.equal(root.get("state") ,"5"));
  182. }
  183. else {
  184. statList1[0]=cb.and(in,cb.or(
  185. cb.equal(root.get("state") ,"3")
  186. ,cb.equal(root.get("state") ,"4")
  187. ));
  188. }
  189. }
  190. if(statList1[0]!=null&&statList1[1]!=null) {
  191. pl.add(cb.or(statList1));
  192. }
  193. else if(statList1[0]!=null) {
  194. pl.add(statList1[0]);
  195. }
  196. else if(statList1[1]!=null) {
  197. pl.add(statList1[1]);
  198. }
  199. }
  200. Predicate[] p = new Predicate[pl.size()];
  201. return cb.and(pl.toArray(p));
  202. }
  203. };
  204. pageList = tdataMetaRepository.findAll(spec, pageable);
  205. } else {
  206. Specification<TdataMeta> spec = new Specification<TdataMeta>() {
  207. public Predicate toPredicate(Root<TdataMeta> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
  208. List<Predicate> pl = new ArrayList<Predicate>();
  209. Predicate[] p = new Predicate[0];
  210. return cb.and(pl.toArray(p));
  211. }
  212. };
  213. pageList = tdataMetaRepository.findAll(spec, pageable);
  214. }
  215. List<TdataMeta> list = pageList.getContent();
  216. pageUtil.setPageResult(list);// 结果集
  217. pageUtil.setPageTotal(pageList.getTotalElements());// 总数
  218. pageUtil.setPageSize(pageSize);// 每页行数
  219. pageUtil.setPageCurrentNum(page + 1);// 当前页
  220. int totalPage = ((int) pageUtil.getPageTotal() + pageUtil.getPageSize() - 1) / pageUtil.getPageSize();
  221. pageUtil.setPageCount(totalPage);// 总页数
  222. return pageUtil;
  223. }
  224. public TdataMeta InsertRole(final TdataMetaDto dto) {
  225. TdataMeta cf = new TdataMeta();
  226. BeanUtils.copyProperties(dto, cf);
  227. cf.setId(ToolUtil.getUUID());
  228. return tdataMetaRepository.save(cf);
  229. }
  230. @Transactional(propagation = Propagation.SUPPORTS)
  231. public TdataMeta findTdataMetaById(String id) {
  232. TdataMeta cf = tdataMetaRepository.findOne(id);
  233. return cf;
  234. }
  235. public void updateTdataMeta(final TdataMetaDto dto, String createdate, String userId, String userName,
  236. String updatorOrg, String updateOrgName, String updatetime) {
  237. TdataMeta cf = tdataMetaRepository.findOne(dto.getId());
  238. cf.setDescription(dto.getDescription()); // 说明
  239. cf.setMetaname(dto.getMetaname());// 数据源名称
  240. cf.setDatasize(dto.getDatasize());// 长度
  241. cf.setRanges(dto.getRanges());// 值域
  242. // 数据源类别
  243. cf.setMetacodeid(dto.getMetacodeid());// 字符型 字典id
  244. cf.setMetacodeDis(dto.getMetacodeDis());
  245. cf.setMetacode(dto.getMetacode());// 字符型
  246. // 数据源类型
  247. cf.setDatatypeid(dto.getDatatypeid());
  248. cf.setDatatypeDis(dto.getDatatypeDis());
  249. cf.setDatatype(dto.getDatatype());
  250. cf.setCreatedate(createdate);
  251. cf.setUpdatetime(updatetime);
  252. cf.setUpdator(userId);
  253. cf.setUpdatorName(userName);
  254. cf.setUpdatorOrg(updatorOrg);
  255. cf.setUpdateOrgName(updateOrgName);
  256. tdataMetaRepository.saveAndFlush(cf);
  257. }
  258. @Transactional(propagation = Propagation.REQUIRED)
  259. public void deleteByTdataMeta(String id) {
  260. tdataMetaRepository.delete(id);
  261. }
  262. @Transactional(propagation = Propagation.REQUIRED)
  263. public void submintDatametaByIds(String id) {
  264. tdataMetaRepository.submintDatametaByIds(id);
  265. }
  266. @Transactional(propagation = Propagation.REQUIRED)
  267. public void audioSubmintDatametaByIds(String [] ids, String state) {
  268. if( ids!=null && ids.length>0 && !ids[0].isEmpty() ){
  269. if("3".equals(state) || "4".equals(state) ){
  270. tdataMetaRepository.audioSubmintDatametaByIds(ids, state);
  271. }
  272. }
  273. }
  274. @Transactional(propagation = Propagation.REQUIRED)
  275. public void audioTdataMeta(String checkdate, String checker, String checkerName, String checkerOrg,
  276. String checkerOrgName, String auditDescript, String state, String id,String metacode) {
  277. String datacode="";
  278. if("3".equals(state)){
  279. datacode=tinfoCodeService.getInfoCodeByPrecode("t_datameta"+metacode);
  280. if(datacode.length()<6)
  281. datacode=metacode+ToolUtil.lpad(datacode, 6, "0");
  282. else
  283. datacode=metacode+datacode;
  284. }
  285. tdataMetaRepository.audioTdataMeta(checkdate, checker, checkerName, checkerOrg, checkerOrgName, auditDescript, state, id,datacode );
  286. }
  287. public List<Map<String,Object>> findTdataMetaByIds(String ids) {
  288. // TODO Auto-generated method stub
  289. List<Map<String,Object>> list = jdbcTemplate.queryForList("select * from t_datameta where id in ("+ids+") ");
  290. // List list = tdataMetaRepository.findTdataMetaByIds(str);
  291. for (Map<String, Object> map : list) {
  292. map.put("STATE", state2Chinese(map.get("STATE").toString()) );
  293. }
  294. return list;
  295. }
  296. private static String state2Chinese(String STATE){
  297. switch (STATE) {
  298. case "1":
  299. return "保存";
  300. case "2":
  301. return "待审核";
  302. case "3":
  303. return "启用";
  304. case "4":
  305. return "停用";
  306. case "5":
  307. return "审核通过";
  308. case "6":
  309. return "退回修改";
  310. }
  311. return STATE;
  312. }
  313. public void saveInfoList(List<TdataMeta> resultList) {
  314. // TODO Auto-generated method stub
  315. tdataMetaRepository.save(resultList);
  316. }
  317. public List<TdataMetaDto> getAll(Vector<String> v1, String id) {
  318. StringBuffer sql=new StringBuffer("select id,metaname from t_datameta " );
  319. List<TdataMetaDto> infoList = new ArrayList<TdataMetaDto>();
  320. List<String> paramList=new ArrayList<String>();
  321. sql.append(" where id!=? and ");
  322. paramList.add(id);
  323. String s="";
  324. for(String str:v1){
  325. s+=str+"|";
  326. }
  327. if(!ToolUtil.isNull(s)){
  328. if("oracle".equals(database_type)){
  329. sql.append(" REGEXP_LIKE(metaname, ?) ");
  330. paramList.add(s.substring(0, s.length()-1)+"");
  331. }
  332. else if("mysql".equals(database_type)){
  333. sql.append(" metaname REGEXP ? ");
  334. paramList.add(s.substring(0, s.length()-1)+"");
  335. }
  336. }
  337. Object[] args = new Object[]{};
  338. int length = paramList.size();
  339. if(length>0){
  340. args=new Object[length];
  341. for(int i=0;i<length;i++){
  342. args[i] = paramList.get(i);
  343. }
  344. }
  345. infoList = jdbcTemplate.query(sql.toString(), args,
  346. new RowMapper<TdataMetaDto>() {
  347. @Override
  348. public TdataMetaDto mapRow(ResultSet rs, int rowNum)
  349. throws SQLException {
  350. TdataMetaDto info = new TdataMetaDto();
  351. info.setId(rs.getString("id"));
  352. info.setMetaname(rs.getString("metaname"));
  353. return info;
  354. }
  355. }
  356. );
  357. return infoList;
  358. }
  359. public List<TdataMeta> findTdataMetaByDatacode(String datacode) {
  360. // TODO Auto-generated method stub
  361. return tdataMetaRepository.findTdataMetaByDatacode(datacode);
  362. }
  363. }