ScheduleTaskServiceImpl.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  19. import org.springframework.stereotype.Service;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. /**
  23. * @author koul
  24. * @email koul@usoftchina.com
  25. * @date 2021-12-23 15:30
  26. */
  27. @Service
  28. public class ScheduleTaskServiceImpl implements ScheduleTaskService {
  29. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  30. private static Map<String,String> tokenConfig = TokenProperties.getAllProperty();
  31. @Autowired
  32. private BaseDao baseDao;
  33. /**
  34. * STATUS(W等待执行,F执行失败,D已执行)
  35. *
  36. */
  37. @Override
  38. public void syncProducts() {
  39. SqlRowList rs = baseDao.queryForRowSet("select id from mes_product where status='W' order by id");
  40. while (rs.next()){
  41. Product product = baseDao.getJdbcTemplate().queryForObject("select case when pr_kh_user='柏英特' or nvl(pr_kh_user,' ')=' ' then 'BYT' else nvl(cu_mescode,cu_code) end pr_kh_user,pr_code ,pr_detail ,pr_spec,nvl(pr_unit,'PCS') pr_unit,nvl(pr_cop,'BYT') pr_cop,nvl(pr_version,'0') pr_version,case when nvl(pr_kind,'')='半成品' or nvl(pr_kind,' ')='成品' then 'product' else 'raw' end pr_serial,nvl(pr_validdays,'0') pr_validdays,pr_msdlevel ,pr_fhelpcode ,pr_f_115 ,pr_f_116 ,pr_f_102,pr_kind,flag,pr_statuscode,pr_status from mes_product left join customer on pr_kh_user=cu_shortname where id=?",
  42. new BeanPropertyRowMapper<Product>(Product.class), rs.getInt("id"));
  43. product.setMesUser(tokenConfig.get("mesUser"));
  44. product.setMesPwd(MD5Util.encodeByMD5(tokenConfig.get("mesPwd")));
  45. Map<String, Object> map = syncMES(JSON.toJSONString(product, SerializerFeature.WriteMapNullValue).replace("null", "\"\""), tokenConfig.get("syncProduct"));
  46. if ("ZG".equals(StringUtil.nvl(product.getPr_kh_user(),""))){
  47. product.setPr_kh_user("ZGSDC");
  48. Map<String, Object> map1 =
  49. syncMES(JSON.toJSONString(product, SerializerFeature.WriteMapNullValue).replace("null", "\"\""), tokenConfig.get("syncProduct"));
  50. if (map1!=null) {
  51. int code1 = Integer.parseInt(StringUtil.nvl(map1.get("code"), "100"));
  52. if (code1>0){
  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. if (map!=null){
  58. int code = Integer.parseInt(StringUtil.nvl(map.get("code"), "-1"));
  59. if (code==0){
  60. baseDao.updateByCondition("mes_product","status='D'","status='W' and id="+rs.getInt("id"));
  61. }else {
  62. baseDao.updateByCondition("mes_product","status='F'","status='W' and id="+rs.getInt("id"));
  63. 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()+"','物料资料')");
  64. }
  65. }
  66. }
  67. }
  68. @Override
  69. public void syncMakeBases() {
  70. SqlRowList rs = baseDao.queryForRowSet("select id,ma_code from mes_make where status='W' order by id");
  71. while (rs.next()){
  72. int count = baseDao.getCount("select count(1) from make where ma_statuscode='FINISH' and ma_code='" + rs.getString("ma_code") + "'");
  73. String status="S";
  74. if (count>0){
  75. status="C";
  76. }
  77. Make make = baseDao.getJdbcTemplate().queryForObject("select ma_code,ma_prodcode,ma_qty,nvl(cu_mescode,'BYT') 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,'"+status+"' 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 left join customer on ma_custcode=cu_code where id=?",new BeanPropertyRowMapper<Make>(Make.class),rs.getInt("id"));
  78. make.setMesPwd(MD5Util.encodeByMD5(tokenConfig.get("mesPwd")));
  79. make.setMesUser(tokenConfig.get("mesUser"));
  80. Map<String, Object> map = syncMES(JSON.toJSONString(make, SerializerFeature.WriteMapNullValue).replace("null", "\"\""), tokenConfig.get("syncMakeBase"));
  81. if (map!=null){
  82. int code = Integer.parseInt(StringUtil.nvl(map.get("code"), "-1"));
  83. if (code==0){
  84. baseDao.updateByCondition("mes_make","status='D'","status='W' and id="+rs.getInt("id"));
  85. }else {
  86. baseDao.updateByCondition("mes_make","status='F'","status='W' and id="+rs.getInt("id"));
  87. 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()+"','工单')");
  88. }
  89. }
  90. }
  91. }
  92. @Override
  93. public void syncMakeBaseDetails() {
  94. SqlRowList rs = baseDao.queryForRowSet("select id,mm_code,mm_detno from mes_makematerial where status='W' order by id");
  95. while (rs.next()) {
  96. int count = baseDao.getCount("select count(1) from mes_make where status='D' and action<>'D' and ma_code='" + rs.getGeneralString("mm_code") + "'");
  97. if (count > 0) {
  98. MakeMaterial makeMaterial = baseDao.getJdbcTemplate().queryForObject("select mm_code,erpid,mm_prodcode,pr_detail,pr_spec,pr_unit,mm_oneuseqty qty,mm_qty,mm_oneuseqty,'main' 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_prodcode mm_repprodcode,mm_balance,nvl(cu_mescode,'BYT') 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 (select max(mc_indate) mc_indate,max(mc_recorder) mc_recorder,max(md_mmdetno) md_mmdetno,max(md_makecode) md_makecode from MakeMaterialChangeDet left join MakeMaterialChange on mc_id=md_mcid) on md_makecode=ma_code and md_mmdetno=mm_detno left join customer on ma_custcode=cu_code where id=? order by mm_detno", new BeanPropertyRowMapper<MakeMaterial>(MakeMaterial.class), rs.getInt("id"));
  99. makeMaterial.setMesUser(tokenConfig.get("mesUser"));
  100. makeMaterial.setMesPwd(MD5Util.encodeByMD5(tokenConfig.get("mesPwd")));
  101. Map<String, Object> map = syncMES(JSON.toJSONString(makeMaterial, SerializerFeature.WriteMapNullValue).replace("null", "\"\""), tokenConfig.get("syncMakeBaseDetail"));
  102. if (map != null) {
  103. int code = Integer.parseInt(StringUtil.nvl(map.get("code"), "-1"));
  104. if (code == 0) {
  105. baseDao.updateByCondition("mes_makematerial", "status='D'", "status='W' and id=" + rs.getInt("id"));
  106. } else {
  107. baseDao.updateByCondition("mes_makematerial", "status='F'", "status='W' and id=" + rs.getInt("id"));
  108. 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')");
  109. }
  110. }
  111. }
  112. }
  113. }
  114. @Override
  115. public void syncMakematerialReplace() {
  116. SqlRowList rs = baseDao.queryForRowSet("select id,mp_mmcode, mp_mmdetno,mp_mmid from mes_makematerialreplace where status='W' order by id");
  117. while (rs.next()) {
  118. int count =
  119. baseDao.getCount("select count(1) from mes_makematerial where status='D' and action<>'D' and " + "erpid=" + rs.getInt("mp_mmid"));
  120. if (count > 0) {
  121. MakeMaterial makeMaterial = baseDao.getJdbcTemplate().queryForObject("select mm.mm_code,mm.erpid,mr.mp_prodcode mm_prodcode,pr_detail,pr_spec,pr_unit,mm.mm_oneuseqty qty,mr.mp_canuseqty mm_qty,mm.mm_oneuseqty,'alternative' 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.mm_prodcode mm_repprodcode,mm.mm_balance,nvl(cu_mescode,'BYT') ma_custcode,to_char(mc_indate,'yyyy-MM-dd HH24:mi:ss') mc_indate,mc_recorder,mr.flag from mes_makematerialreplace mr left join mes_makematerial mm on mr.mp_mmid=mm.erpid left join make on mm.mm_maid=ma_id left join Product on mr.mp_prodcode=pr_code left join bom on bo_id=mm.mm_bomid left join (select max(mc_indate) mc_indate,max(mc_recorder) mc_recorder,max(md_mmdetno) md_mmdetno,max(md_makecode) md_makecode from MakeMaterialChangeDet left join MakeMaterialChange on mc_id=md_mcid) on md_makecode=ma_code and md_mmdetno=mm.mm_detno left join customer on ma_custcode=cu_code where mr.id=?",
  122. new BeanPropertyRowMapper<MakeMaterial>(MakeMaterial.class), rs.getInt("id"));
  123. makeMaterial.setMesUser(tokenConfig.get("mesUser"));
  124. makeMaterial.setMesPwd(MD5Util.encodeByMD5(tokenConfig.get("mesPwd")));
  125. Map<String, Object> map = syncMES(JSON.toJSONString(makeMaterial,
  126. SerializerFeature.WriteMapNullValue).replace("null", "\"\""), tokenConfig.get("syncMakeBaseDetail"));
  127. if (map != null) {
  128. int code1 = Integer.parseInt(StringUtil.nvl(map.get("code"), "-1"));
  129. if (code1 == 0) {
  130. baseDao.updateByCondition("mes_makematerialreplace", "status='D'", "status='W' and id=" + rs.getInt("id"));
  131. } else {
  132. baseDao.updateByCondition("mes_makematerialreplace", "status='F'", "status='W' and id=" + rs.getInt("id"));
  133. 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("mp_mmcode") + "'," + rs.getInt("mp_mmdetno") + ",'工单BOM替代料')");
  134. }
  135. }
  136. }
  137. }
  138. }
  139. private Map<String, Object> syncMES(String params,String url){
  140. Map<String, Object> map = new HashMap<>();
  141. map.put("code",-1);
  142. logger.info("params:"+params);
  143. try {
  144. String post = PSHttpUtils.sendPost(tokenConfig.get("mesHttp") + url, params);
  145. logger.info("post:"+post);
  146. JSONObject jsonObject = JSON.parseObject(post);
  147. int code = jsonObject.getIntValue("result");
  148. logger.info("code:"+code);
  149. if (code==0){
  150. map.put("code",code);
  151. map.put("msg","同步MES成功");
  152. }else if(code==1){
  153. map.put("code",code);
  154. logger.info("msg:"+jsonObject.getString("message"));
  155. map.put("msg",jsonObject.getString("message"));
  156. }else {
  157. map.put("code",-1);
  158. map.put("msg","接口调用异常,请联系MES处理!");
  159. }
  160. }catch (Exception e){
  161. map.put("msg","接口调用异常,请联系MES处理!");
  162. logger.info("异常信息:"+e.getMessage());
  163. e.printStackTrace();
  164. }
  165. return map;
  166. }
  167. }