ScheduleTaskServiceImpl.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package com.uas.eis.service.Impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.alibaba.fastjson.serializer.SerializerFeature;
  5. import com.uas.eis.core.support.TokenProperties;
  6. import com.uas.eis.dao.BaseDao;
  7. import com.uas.eis.dao.SqlRowList;
  8. import com.uas.eis.entity.Make;
  9. import com.uas.eis.entity.MakeMaterial;
  10. import com.uas.eis.entity.Product;
  11. import com.uas.eis.service.ScheduleTaskService;
  12. import com.uas.eis.utils.MD5Util;
  13. import com.uas.eis.utils.PSHttpUtils;
  14. import com.uas.eis.utils.StringUtil;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  17. import org.springframework.stereotype.Service;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20. /**
  21. * @author koul
  22. * @email koul@usoftchina.com
  23. * @date 2021-12-23 15:30
  24. */
  25. @Service
  26. public class ScheduleTaskServiceImpl implements ScheduleTaskService {
  27. private static Map<String,String> tokenConfig = TokenProperties.getAllProperty();
  28. @Autowired
  29. private BaseDao baseDao;
  30. /**
  31. * STATUS(W等待执行,F执行失败,D已执行)
  32. *
  33. */
  34. @Override
  35. public void syncProducts() {
  36. SqlRowList rs = baseDao.queryForRowSet("select id from mes_product where status='W' order by id");
  37. while (rs.next()){
  38. Product product = baseDao.getJdbcTemplate().queryForObject("select pr_kh_user,pr_code ,pr_detail ," +
  39. "pr_spec ,nvl(pr_unit,'PCS') pr_unit,nvl(pr_cop,'BYT') pr_cop,nvl(pr_version,'0') " +
  40. "pr_version,pr_serial,nvl(pr_validdays,'0') pr_validdays,pr_msdlevel ,pr_fhelpcode ," +
  41. "pr_f_115 ,pr_f_116 ,pr_f_102,pr_kind,wh_mesc_user pr_whcode,wh_mesn_user pr_whname,flag," +
  42. "pr_statuscode,pr_status from mes_product left join Warehouse on pr_whcode=wh_code where id=?",
  43. new BeanPropertyRowMapper<Product>(Product.class), rs.getInt("id"));
  44. product.setMesUser(tokenConfig.get("mesUser"));
  45. product.setMesPwd(MD5Util.encodeByMD5(tokenConfig.get("mesPwd")));
  46. Map<String, Object> map = syncMES(JSON.toJSONString(product, SerializerFeature.WriteMapNullValue).replace("null", "\"\""), tokenConfig.get("syncProduct"));
  47. if (map!=null){
  48. int code = Integer.parseInt(StringUtil.nvl(map.get("code"), "-1"));
  49. if (code==0){
  50. baseDao.updateByCondition("mes_product","status='D'","status='W' and id="+rs.getInt("id"));
  51. }else {
  52. baseDao.updateByCondition("mes_product","status='F'","status='W' and id="+rs.getInt("id"));
  53. baseDao.execute("insert into meserrorlog (mel_id,mel_url,mel_error,mel_indate,mel_djcode,mel_type) values (MESERRORLOG_SEQ.NEXTVAL,'"+tokenConfig.get("syncProduct")+"','"+StringUtil.nvl(map.get("msg"),"")+"',sysdate,'"+product.getPr_code()+"','物料资料')");
  54. }
  55. }
  56. }
  57. }
  58. @Override
  59. public void syncMakeBases() {
  60. SqlRowList rs = baseDao.queryForRowSet("select id from mes_make where status='W' order by id");
  61. while (rs.next()){
  62. Make make = baseDao.getJdbcTemplate().queryForObject("select id,ma_code,ma_prodcode,ma_qty,ma_custcode,pr_unit,pr_detail,pr_spec,ma_salecode,to_char(ma_planbegindate,'yyyy-MM-dd') ma_planbegindate,to_char(ma_planenddate,'yyyy-MM-dd') ma_planenddate,nvl(ma_cop,'BYT') ma_cop,'S' status,to_char(ma_date,'yyyy-MM-dd HH24:mi:ss') ma_date,ma_recorder,nvl(ma_version,0) ma_version,erpid,wc_id,pr_id,to_char(ma_modifydate,'yyyy-MM-dd HH24:mi:ss') ma_modifydate,ma_modifier,flag from mes_make left join product on ma_prodcode=pr_code left join workcenter on ma_wccode=wc_code where id=?",
  63. new BeanPropertyRowMapper<Make>(Make.class),rs.getInt("id"));
  64. make.setMesPwd(MD5Util.encodeByMD5(tokenConfig.get("mesPwd")));
  65. make.setMesUser(tokenConfig.get("mesUser"));
  66. Map<String, Object> map = syncMES(JSON.toJSONString(make, SerializerFeature.WriteMapNullValue).replace("null", "\"\""), tokenConfig.get("syncMakeBase"));
  67. if (map!=null){
  68. int code = Integer.parseInt(StringUtil.nvl(map.get("code"), "-1"));
  69. if (code==0){
  70. baseDao.updateByCondition("mes_make","status='D'","status='W' and id="+rs.getInt("id"));
  71. }else {
  72. baseDao.updateByCondition("mes_make","status='F'","status='W' and id="+rs.getInt("id"));
  73. baseDao.execute("insert into meserrorlog (mel_id,mel_url,mel_error,mel_indate,mel_djcode,mel_type) values (MESERRORLOG_SEQ.NEXTVAL,'"+tokenConfig.get("syncMakeBase")+"','"+StringUtil.nvl(map.get("msg"),"")+"',sysdate,'"+make.getMa_code()+"','工单')");
  74. }
  75. }
  76. }
  77. }
  78. @Override
  79. public void syncMakeBaseDetails() {
  80. SqlRowList rs = baseDao.queryForRowSet("select id,mm_code,mm_detno from mes_makematerial where status='W' order by id");
  81. while (rs.next()){
  82. int count = baseDao.getCount("select count(1) from mes_make where status='D' and action<>'D' and ma_code='" + rs.getGeneralString("mm_code") + "'");
  83. if (count>0) {
  84. MakeMaterial makeMaterial = baseDao.getJdbcTemplate().queryForObject("select mm_code,erpid,mm_prodcode,pr_detail,pr_spec,pr_unit,mm_oneuseqty qty,mm_qty,mm_oneuseqty,case when mm_ifrep=-1 then 'alternative' else 'main' end type,bo_recorder,to_char(bo_date,'yyyy-MM-dd HH24:mi:ss') bo_date,nvl(bo_version,0) bo_version,nvl(bo_cop,'BYT') bo_cop,mm_repprodcode,mm_balance,ma_custcode,to_char(mc_indate,'yyyy-MM-dd HH24:mi:ss') mc_indate,mc_recorder,flag from mes_makematerial left join make on ma_id=mm_maid left join Product on mm_prodcode=pr_code left join bom on bo_id=mm_bomid left join MakeMaterialChangeDet on md_makecode=ma_code and md_mmdetno=mm_detno left join MakeMaterialChange on mc_id=md_mcid where id=? order by mm_detno", new BeanPropertyRowMapper<MakeMaterial>(MakeMaterial.class), rs.getInt("id"));
  85. makeMaterial.setMesUser(tokenConfig.get("mesUser"));
  86. makeMaterial.setMesPwd(MD5Util.encodeByMD5(tokenConfig.get("mesPwd")));
  87. Map<String, Object> map = syncMES(JSON.toJSONString(makeMaterial, SerializerFeature.WriteMapNullValue).replace("null", "\"\""), tokenConfig.get("syncMakeBaseDetail"));
  88. if (map != null) {
  89. int code = Integer.parseInt(StringUtil.nvl(map.get("code"), "-1"));
  90. if (code == 0) {
  91. baseDao.updateByCondition("mes_makematerial", "status='D'", "status='W' and id=" + rs.getInt("id"));
  92. } else {
  93. baseDao.updateByCondition("mes_makematerial", "status='F'", "status='W' and id=" + rs.getInt("id"));
  94. baseDao.execute("insert into meserrorlog (mel_id,mel_url,mel_error,mel_indate,mel_djcode,mel_djdetno,mel_type) values(MESERRORLOG_SEQ.NEXTVAL,'" + tokenConfig.get("syncMakeBaseDetail") + "','" + StringUtil.nvl(map.get("msg"), "") + "',sysdate,'" + rs.getGeneralString("mm_code") + "',"+rs.getInt("mm_detno")+",'工单BOM')");
  95. }
  96. }
  97. }
  98. }
  99. }
  100. private Map<String, Object> syncMES(String params,String url){
  101. Map<String, Object> map = new HashMap<>();
  102. map.put("code",-1);
  103. System.err.println(params);
  104. try {
  105. String post = PSHttpUtils.sendPost(tokenConfig.get("mesHttp") + url, params);
  106. JSONObject jsonObject = JSON.parseObject(post);
  107. int code = jsonObject.getIntValue("result");
  108. if (code==0){
  109. map.put("code",code);
  110. map.put("msg","同步MES成功");
  111. }else if(code==1){
  112. map.put("code",code);
  113. map.put("msg",jsonObject.getString("message"));
  114. }else {
  115. map.put("code",-1);
  116. map.put("msg","接口调用异常,请联系MES处理!");
  117. }
  118. }catch (Exception e){
  119. map.put("msg","接口调用异常,请联系MES处理!");
  120. e.printStackTrace();
  121. }
  122. return map;
  123. }
  124. }