ScheduleTaskServiceImpl.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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.Customer;
  9. import com.uas.eis.entity.Make;
  10. import com.uas.eis.entity.MakeMaterial;
  11. import com.uas.eis.entity.Product;
  12. import com.uas.eis.service.ScheduleTaskService;
  13. import com.uas.eis.utils.MD5Util;
  14. import com.uas.eis.utils.PSHttpUtils;
  15. import com.uas.eis.utils.StringUtil;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  20. import org.springframework.stereotype.Service;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. /**
  25. * @author koul
  26. * @email koul@usoftchina.com
  27. * @date 2021-12-23 15:30
  28. */
  29. @Service
  30. public class ScheduleTaskServiceImpl implements ScheduleTaskService {
  31. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  32. private static Map<String,String> tokenConfig = TokenProperties.getAllProperty();
  33. @Autowired
  34. private BaseDao baseDao;
  35. /**
  36. * STATUS(W等待执行,F执行失败,D已执行)
  37. *
  38. */
  39. @Override
  40. public void syncProducts() {
  41. try {
  42. SqlRowList rs = baseDao.queryForRowSet("select id from mes_product where status='W' order by id");
  43. while (rs.next()){
  44. Product product = baseDao.getJdbcTemplate().queryForObject("select case when pr_kh_user='柏英特' or nvl(pr_kh_user,' ')=' ' then 'HS' else nvl(cu_mescode,cu_code) end pr_kh_user,pr_code ,pr_detail ,replace(PR_SPEC,'''','''''') pr_spec,nvl(pr_unit,'PCS') pr_unit,nvl(pr_cop,'HS') 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_kind3 ,pr_f_115 ,pr_f_116 ,pr_f_102,pr_kind,flag,pr_statuscode,pr_status,pr_whcode,pr_kind2,pr_acceptmethod from mes_product left join customer on pr_kh_user=cu_shortname where id=?",
  45. new BeanPropertyRowMapper<Product>(Product.class), rs.getInt("id"));
  46. //获取MES对应仓库
  47. String prWhcode = product.getPr_whcode();
  48. if (prWhcode!=null&&!"".equals(prWhcode)) {
  49. SqlRowList rowSet = baseDao.queryForRowSet("select cd_varchar50_1,cd_varchar50_2,cd_varchar50_3,cd_varchar50_5 from customtabledetail left join customtable on ct_id=cd_ctid where ct_caller='MESWarehouse' and ct_statuscode='AUDITED' and cd_varchar50_3='" + prWhcode + "' order by ct_id desc,cd_id desc");
  50. if (rowSet.next()){
  51. product.setPr_whcode(rowSet.getGeneralString("cd_varchar50_1"));
  52. }
  53. }
  54. product.setMesUser(tokenConfig.get("mesUser"));
  55. product.setMesPwd(MD5Util.encodeByMD5(tokenConfig.get("mesPwd")));
  56. Map<String, Object> map = syncMES(JSON.toJSONString(product, SerializerFeature.WriteMapNullValue).replace("null", "\"\""), tokenConfig.get("syncProduct"));
  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. int flag = product.getFlag();
  63. if (flag == 1) {
  64. product.setFlag(0);
  65. } else if (flag == 0) {
  66. product.setFlag(1);
  67. }
  68. Map<String, Object> map1 = syncMES(JSON.toJSONString(product, SerializerFeature.WriteMapNullValue).replace("null", "\"\""), tokenConfig.get("syncProduct"));
  69. if (map1!=null){
  70. int code1 = Integer.parseInt(StringUtil.nvl(map.get("code"), "-1"));
  71. if (code1==0){
  72. baseDao.updateByCondition("mes_product","status='D'","status='W' and id="+rs.getInt("id"));
  73. }else {
  74. baseDao.updateByCondition("mes_product","status='F'","status='W' and id="+rs.getInt("id"));
  75. 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()+"','物料资料')");
  76. }
  77. }
  78. }
  79. }
  80. if ("ZG".equals(StringUtil.nvl(product.getPr_kh_user(),""))){
  81. product.setPr_kh_user("ZGSDC");
  82. Map<String, Object> map1 =
  83. syncMES(JSON.toJSONString(product, SerializerFeature.WriteMapNullValue).replace("null", "\"\""), tokenConfig.get("syncProduct"));
  84. if (map1!=null) {
  85. int code1 = Integer.parseInt(StringUtil.nvl(map1.get("code"), "-1"));
  86. if (code1>0) {
  87. int flag = product.getFlag();
  88. if (flag == 1) {
  89. product.setFlag(0);
  90. } else if (flag == 0) {
  91. product.setFlag(1);
  92. }
  93. Map<String, Object> map2 =
  94. syncMES(JSON.toJSONString(product, SerializerFeature.WriteMapNullValue).replace("null", "\"\""), tokenConfig.get("syncProduct"));
  95. if (map2 != null) {
  96. int code2 = Integer.parseInt(StringUtil.nvl(map1.get("code"), "100"));
  97. if (code2 > 0) {
  98. 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() + "','物料资料')");
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105. }catch (Exception e){
  106. logger.info("物料资料异常信息:"+e.getMessage());
  107. e.printStackTrace();
  108. }
  109. }
  110. @Override
  111. public void syncMakeBases() {
  112. try {
  113. SqlRowList rs = baseDao.queryForRowSet("select id,ma_code from mes_make where status='W' order by id");
  114. while (rs.next()) {
  115. int count = baseDao.getCount("select count(1) from make where ma_statuscode='FINISH' and ma_code='" + rs.getString("ma_code") + "'");
  116. String status = "S";
  117. if (count > 0) {
  118. status = "C";
  119. }
  120. Make make = baseDao.getJdbcTemplate().queryForObject("select ma_code,ma_prodcode,ma_qty,nvl(cu_mescode,'HS') ma_custcode,pr_unit,pr_detail,replace(PR_SPEC,'''','''''') pr_spec,nvl(ma_zgrwl,ma_salecode) ma_salecode,to_char(ma_planbegindate,'yyyy-MM-dd') ma_planbegindate,to_char(ma_planenddate,'yyyy-MM-dd') ma_planenddate,nvl(ma_cop,'HS') ma_cop,'" + status + "' status,to_char(ma_date,'yyyy-MM-dd HH24:mi:ss') ma_date,ma_recorder,0 ma_version,erpid,wc_id,nvl(mk_id,0) ma_kind,to_char(ma_modifydate,'yyyy-MM-dd HH24:mi:ss') ma_modifydate,ma_modifier,flag from mes_make left join MakeKind on ma_kind=mk_name left join product on ma_prodcode=pr_code left join workcenter on ma_wccode=wc_code left join customer on pr_kh_user=cu_shortname where id=?", new BeanPropertyRowMapper<Make>(Make.class), rs.getInt("id"));
  121. make.setMesPwd(MD5Util.encodeByMD5(tokenConfig.get("mesPwd")));
  122. make.setMesUser(tokenConfig.get("mesUser"));
  123. Map<String, Object> map = syncMES(JSON.toJSONString(make, SerializerFeature.WriteMapNullValue).replace("null", "\"\""), tokenConfig.get("syncMakeBase"));
  124. if (map != null) {
  125. int code = Integer.parseInt(StringUtil.nvl(map.get("code"), "-1"));
  126. if (code == 0) {
  127. baseDao.updateByCondition("mes_make", "status='D'", "status='W' and id=" + rs.getInt("id"));
  128. } else {
  129. int flag = make.getFlag();
  130. if (flag == 1) {
  131. make.setFlag(0);
  132. } else if (flag == 0) {
  133. make.setFlag(1);
  134. }
  135. Map<String, Object> map1 = syncMES(JSON.toJSONString(make,
  136. SerializerFeature.WriteMapNullValue).replace("null", "\"\""), tokenConfig.get("syncMakeBase"));
  137. if (map1 != null) {
  138. int code1 = Integer.parseInt(StringUtil.nvl(map1.get("code"), "-1"));
  139. if (code1 == 0) {
  140. baseDao.updateByCondition("mes_makematerial", "status='D'", "status='W' and id=" + rs.getInt("id"));
  141. } else {
  142. baseDao.updateByCondition("mes_make", "status='F'", "status='W' and id=" + rs.getInt("id"));
  143. 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() + "','工单')");
  144. }
  145. }
  146. }
  147. }
  148. }
  149. }catch (Exception e){
  150. logger.info("同步工单异常信息:"+e.getMessage());
  151. e.printStackTrace();
  152. }
  153. }
  154. @Override
  155. public void syncMakeBaseDetails() {
  156. try {
  157. SqlRowList rs = baseDao.queryForRowSet("select id,mm_code,mm_detno from mes_makematerial where status='W' order by id");
  158. while (rs.next()) {
  159. int count = baseDao.getCount("select count(1) from mes_make where status='D' and ma_code='" + rs.getGeneralString("mm_code") + "'");
  160. if (count > 0) {
  161. MakeMaterial makeMaterial = baseDao.getJdbcTemplate().queryForObject("select mm_code,erpid,mm_prodcode,pr_detail,replace(PR_SPEC,'''','''''') 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,0 bo_version,nvl(bo_cop,'HS') bo_cop,mm_prodcode mm_repprodcode,mm_balance,(select nvl(cu_mescode,'HS') from Product left join customer on pr_kh_user=cu_shortname where pr_code=ma_prodcode) ma_custcode,to_char(mc_indate,'yyyy-MM-dd HH24:mi:ss') mc_indate,mc_recorder,flag,0 rpid 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 where id=? order by mm_detno", new BeanPropertyRowMapper<MakeMaterial>(MakeMaterial.class), rs.getInt("id"));
  162. makeMaterial.setMesUser(tokenConfig.get("mesUser"));
  163. makeMaterial.setMesPwd(MD5Util.encodeByMD5(tokenConfig.get("mesPwd")));
  164. Map<String, Object> map = syncMES(JSON.toJSONString(makeMaterial, SerializerFeature.WriteMapNullValue).replace("null", "\"\""), tokenConfig.get("syncMakeBaseDetail"));
  165. if (map != null) {
  166. int code = Integer.parseInt(StringUtil.nvl(map.get("code"), "-1"));
  167. if (code == 0) {
  168. baseDao.updateByCondition("mes_makematerial", "status='D'", "status='W' and id=" + rs.getInt("id"));
  169. } else {
  170. int flag = makeMaterial.getFlag();
  171. if (flag==1) {
  172. makeMaterial.setFlag(0);
  173. }else if (flag==0) {
  174. makeMaterial.setFlag(1);
  175. }
  176. Map<String, Object> map1 = syncMES(JSON.toJSONString(makeMaterial,
  177. SerializerFeature.WriteMapNullValue).replace("null", "\"\""), tokenConfig.get("syncMakeBaseDetail"));
  178. if (map1 != null) {
  179. int code1 = Integer.parseInt(StringUtil.nvl(map1.get("code"), "-1"));
  180. if (code1 == 0) {
  181. baseDao.updateByCondition("mes_makematerial", "status='D'", "status='W' and id=" + rs.getInt("id"));
  182. }else {
  183. baseDao.updateByCondition("mes_makematerial", "status='F'", "status='W' and id=" + rs.getInt("id"));
  184. 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')");
  185. }
  186. }
  187. }
  188. }
  189. }
  190. }
  191. }catch (Exception e){
  192. logger.info("同步工单BOM异常信息:"+e.getMessage());
  193. e.printStackTrace();
  194. }
  195. }
  196. @Override
  197. public void syncMakematerialReplace() {
  198. try {
  199. SqlRowList rs = baseDao.queryForRowSet("select id,mp_mmcode, mp_mmdetno,mp_mmid from mes_makematerialreplace where status='W' order by id");
  200. while (rs.next()) {
  201. int count = baseDao.getCount("select count(1) from mes_makematerial where status='D' and " + "erpid=" + rs.getInt("mp_mmid"));
  202. if (count > 0) {
  203. MakeMaterial makeMaterial = baseDao.getJdbcTemplate().queryForObject("select mm.mm_code,mm.erpid,mr.mp_prodcode mm_prodcode,pr_detail,replace(PR_SPEC,'''','''''') 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,0 bo_version,nvl(bo_cop,'HS') bo_cop,mm.mm_prodcode mm_repprodcode,mm.mm_balance,(select nvl(cu_mescode,'HS') from Product left join customer on pr_kh_user=cu_shortname where pr_code=ma_prodcode) ma_custcode,to_char(mc_indate,'yyyy-MM-dd HH24:mi:ss') mc_indate,mc_recorder,mr.flag,mr.erpid rpid 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 where mr.id=?",
  204. new BeanPropertyRowMapper<MakeMaterial>(MakeMaterial.class), rs.getInt("id"));
  205. makeMaterial.setMesUser(tokenConfig.get("mesUser"));
  206. makeMaterial.setMesPwd(MD5Util.encodeByMD5(tokenConfig.get("mesPwd")));
  207. Map<String, Object> map = syncMES(JSON.toJSONString(makeMaterial,
  208. SerializerFeature.WriteMapNullValue).replace("null", "\"\""), tokenConfig.get("syncMakeBaseDetail"));
  209. if (map != null) {
  210. int code = Integer.parseInt(StringUtil.nvl(map.get("code"), "-1"));
  211. if (code == 0) {
  212. baseDao.updateByCondition("mes_makematerialreplace", "status='D'", "status='W' and id=" + rs.getInt("id"));
  213. } else {
  214. int flag = makeMaterial.getFlag();
  215. if (flag==1) {
  216. makeMaterial.setFlag(0);
  217. }else if (flag==0) {
  218. makeMaterial.setFlag(1);
  219. }
  220. Map<String, Object> map1 = syncMES(JSON.toJSONString(makeMaterial,
  221. SerializerFeature.WriteMapNullValue).replace("null", "\"\""), tokenConfig.get("syncMakeBaseDetail"));
  222. if (map1 != null) {
  223. int code1 = Integer.parseInt(StringUtil.nvl(map1.get("code"), "-1"));
  224. if (code1 == 0) {
  225. baseDao.updateByCondition("mes_makematerialreplace", "status='D'", "status='W' and id=" + rs.getInt("id"));
  226. } else {
  227. baseDao.updateByCondition("mes_makematerialreplace", "status='F'", "status='W' and id=" + rs.getInt("id"));
  228. 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替代料')");
  229. }
  230. }
  231. }
  232. }
  233. }
  234. }
  235. }catch (Exception e){
  236. logger.info("同步替代料异常信息:"+e.getMessage());
  237. e.printStackTrace();
  238. }
  239. }
  240. @Override
  241. public void syncCustomer() {
  242. try {
  243. List<Customer> customers = baseDao.query("select cu_id,nvl(cu_mescode,cu_code) cu_oldcode,nvl(cu_mescode,cu_code) cu_code,cu_name,cu_shortname,nvl(cu_cop,'HS') cu_cop,flag from mes_customer where status='W' order by cu_id", Customer.class);
  244. if (customers!=null&&customers.size()>0){
  245. for (Customer customer:customers) {
  246. customer.setMesUser(tokenConfig.get("mesUser"));
  247. customer.setMesPwd(MD5Util.encodeByMD5(tokenConfig.get("mesPwd")));
  248. Map<String, Object> map = syncMES(JSON.toJSONString(customer, SerializerFeature.WriteMapNullValue).replace("null", "\"\""), tokenConfig.get("syncCustomer"));
  249. if (map!=null){
  250. int code = Integer.parseInt(StringUtil.nvl(map.get("msgId"), "-1"));
  251. if (code==0){
  252. baseDao.updateByCondition("mes_customer","status='D'","status='W' and cu_id="+customer.getCu_id());
  253. }else {
  254. int flag = customer.getFlag();
  255. if (flag == 1) {
  256. customer.setFlag(0);
  257. } else if (flag == 0) {
  258. customer.setFlag(1);
  259. }
  260. Map<String, Object> map1 = syncMES(JSON.toJSONString(customer, SerializerFeature.WriteMapNullValue).replace("null", "\"\""), tokenConfig.get("syncCustomer"));
  261. if (map1!=null){
  262. int code1 = Integer.parseInt(StringUtil.nvl(map.get("msgId"), "-1"));
  263. if (code1==0){
  264. baseDao.updateByCondition("mes_customer","status='D'","status='W' and cu_id="+customer.getCu_id());
  265. }else {
  266. baseDao.updateByCondition("mes_customer","status='F'","status='W' and cu_id="+customer.getCu_id());
  267. baseDao.execute("insert into meserrorlog (mel_id,mel_url,mel_error,mel_indate,mel_djcode,mel_type) values (MESERRORLOG_SEQ.NEXTVAL,'"+tokenConfig.get("syncCustomer")+"','"+StringUtil.nvl(map.get("msg"),"")+"',sysdate,'"+customer.getCu_code()+"','客户资料')");
  268. }
  269. }
  270. }
  271. }
  272. }
  273. }
  274. }catch (Exception e){
  275. logger.info("客户资料异常信息:"+e.getMessage());
  276. e.printStackTrace();
  277. }
  278. }
  279. private Map<String, Object> syncMES(String params,String url){
  280. Map<String, Object> map = new HashMap<>();
  281. map.put("code",-1);
  282. logger.info("params:"+params);
  283. try {
  284. String post = PSHttpUtils.sendPost(tokenConfig.get("mesHttp") + url, params);
  285. logger.info("post:"+post);
  286. JSONObject jsonObject = JSON.parseObject(post);
  287. int code = jsonObject.getIntValue("result");
  288. logger.info("code:"+code);
  289. if (code==0){
  290. map.put("code",code);
  291. map.put("msg","同步MES成功");
  292. }else if(code==1){
  293. map.put("code",code);
  294. logger.info("msg:"+jsonObject.getString("message"));
  295. map.put("msg",jsonObject.getString("message"));
  296. }else {
  297. map.put("code",-1);
  298. map.put("msg","接口调用异常,错误代码:"+code+",请联系MES处理!");
  299. }
  300. }catch (Exception e){
  301. map.put("msg","接口调用异常,异常信息:"+e.getMessage()+",请联系MES处理!");
  302. logger.info("异常信息:"+e.getMessage());
  303. e.printStackTrace();
  304. }
  305. return map;
  306. }
  307. }