SyncMesTpmInsRateFacadeControllerBean.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. package com.kingdee.eas.custom.calcdailypay.task;
  2. import com.kingdee.bos.BOSException;
  3. import com.kingdee.bos.Context;
  4. import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
  5. import com.kingdee.bos.metadata.entity.*;
  6. import com.kingdee.bos.rabbitmq.guava.Lists;
  7. import com.kingdee.bos.util.BOSUuid;
  8. import com.kingdee.eas.base.permission.UserInfo;
  9. import com.kingdee.eas.basedata.org.AdminOrgUnit;
  10. import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
  11. import com.kingdee.eas.basedata.org.CtrlUnitInfo;
  12. import com.kingdee.eas.basedata.org.PositionInfo;
  13. import com.kingdee.eas.basedata.person.PersonInfo;
  14. import com.kingdee.eas.common.EASBizException;
  15. import com.kingdee.eas.custom.calcdailypay.IInspectionRate;
  16. import com.kingdee.eas.custom.calcdailypay.InspectionRateCollection;
  17. import com.kingdee.eas.custom.calcdailypay.InspectionRateFactory;
  18. import com.kingdee.eas.custom.calcdailypay.InspectionRateInfo;
  19. import com.kingdee.eas.framework.CoreBaseCollection;
  20. import com.kingdee.eas.framework.ObjectBaseInfo;
  21. import com.kingdee.jdbc.rowset.IRowSet;
  22. import com.kingdee.jdbc.rowset.impl.JdbcRowSet;
  23. import com.kingdee.util.db.SQLUtils;
  24. import org.apache.commons.lang3.StringUtils;
  25. import org.apache.log4j.Logger;
  26. import java.io.FileInputStream;
  27. import java.sql.*;
  28. import java.text.ParseException;
  29. import java.text.SimpleDateFormat;
  30. import java.util.Calendar;
  31. import java.util.List;
  32. import java.util.Properties;
  33. public class SyncMesTpmInsRateFacadeControllerBean extends AbstractSyncMesTpmInsRateFacadeControllerBean {
  34. private static Logger logger =
  35. Logger.getLogger("com.kingdee.eas.custom.calcdailypay.task.SyncMesTpmInsRateFacadeControllerBean");
  36. private Properties propt = new Properties();
  37. public SyncMesTpmInsRateFacadeControllerBean() {
  38. try {
  39. propt.load(new FileInputStream(String.valueOf(System.getProperty("EAS_HOME")) + "/server/properties/mingquan/syncMes.properties"));
  40. } catch (Exception e) {
  41. logger.error(e);
  42. }
  43. }
  44. /**
  45. * 开始日期、结束日期为空,则结束日期默认取当天,开始日期默认取当天减一天
  46. * @description:
  47. * @author: lhbj
  48. * @date: 2025/5/30 16:48
  49. * @param: null
  50. * @return:null
  51. **/
  52. @Override
  53. protected String _syncMesToShr(Context ctx, String startDdjdate, String endDdjDate, String personNum) throws BOSException{
  54. super.syncMesToShr(ctx,startDdjdate,endDdjDate,personNum);
  55. try {
  56. SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
  57. List<String> param = Lists.newArrayList();
  58. StringBuffer selSql = new StringBuffer();
  59. selSql.append(" select id,max(dcreatetime) dcreatetime,bsysdel,cgsmc, ");
  60. selSql.append(" cgw,isbs,iydj,iwdj,iwcl, ");
  61. selSql.append(" ddjdate,cusercode,cusername,cfzname,cgwlx, ");
  62. selSql.append(" cjndj,csfxc,cbzrz,cgzxl ");
  63. selSql.append(" from vi_tpm_dj_wcllist where isnull(cusercode,'')!='' ");
  64. selSql.append(" and ddjdate>=? and ddjdate<=? ");
  65. //开始日期、结束日期为空,则结束日期默认取当天,开始日期默认取当天减一天
  66. if (StringUtils.isNotBlank(startDdjdate) && StringUtils.isNotBlank(endDdjDate)) {
  67. param.add(startDdjdate);
  68. param.add(endDdjDate);
  69. } else {
  70. Calendar calendar = Calendar.getInstance();
  71. endDdjDate = sf.format(calendar.getTime());
  72. calendar.add(Calendar.DAY_OF_MONTH, -1);
  73. startDdjdate = sf.format(calendar.getTime());
  74. param.add(startDdjdate);
  75. param.add(endDdjDate);
  76. }
  77. if (StringUtils.isNotBlank(personNum)) {
  78. selSql.append(" and cusercode=? ");
  79. param.add(personNum);
  80. }
  81. selSql.append(" group by id,bsysdel,cgsmc, ");
  82. selSql.append(" cgw,isbs,iydj,iwdj,iwcl, ");
  83. selSql.append(" ddjdate,cusercode,cusername,cfzname,cgwlx, ");
  84. selSql.append(" cjndj,csfxc,cbzrz,cgzxl ");
  85. selSql.append(" order by ddjdate ");
  86. IRowSet rs = this.executeQuery(selSql.toString(), param.toArray());
  87. IInspectionRate iir = InspectionRateFactory.getLocalInstance(ctx);
  88. CoreBaseCollection upddata = new CoreBaseCollection();
  89. CoreBaseCollection newdata = new CoreBaseCollection();
  90. while (rs.next()) {
  91. String id = rs.getString("id");
  92. String cusercode = rs.getString("cusercode");
  93. Date ddjdate = rs.getDate("ddjdate");
  94. String ddjdateStr = sf.format(ddjdate);
  95. StringBuilder selPerson = new StringBuilder();
  96. selPerson.append(" ");
  97. selPerson.append(" select p.fid pId,p.Fname_l2 pName,p.FNumber pNumber, ");
  98. selPerson.append(" company.fid companyId,dep.fid depId,pos.fid posId, ");
  99. selPerson.append(" ir.fid irId,ir.fsimpleName mesId,ir.CFBizDate irBizDate ");
  100. selPerson.append(" from t_bd_person p ");
  101. selPerson.append(" left join t_hr_Personposition pn on pn.fpersonid=p.fid ");
  102. selPerson.append(" left join t_org_admin dep on dep.fid=pn.FPersonDep ");
  103. selPerson.append(" left join t_org_admin company on company.fid=pn.fcompanyid ");
  104. selPerson.append(" left join t_org_position pos on pos.fid=pn.FPrimaryPositionID ");
  105. selPerson.append(" left join CT_CAL_InspectionRate ir on p.fid=ir.cfpersonid and ir.CFBizDate=? ");
  106. selPerson.append(" where p.fnumber=? ");
  107. IRowSet prs = this.executeQuery(selSql.toString(), new String[]{ddjdateStr,cusercode});
  108. if (prs.next()){
  109. String pId = prs.getString("pId");
  110. String pName = prs.getString("pName");
  111. String pNumber = prs.getString("pNumber");
  112. String companyId = prs.getString("companyId");
  113. String depId = prs.getString("depId");
  114. String posId = prs.getString("posId");
  115. String irId = prs.getString("irId");
  116. String mesId = prs.getString("mesId");
  117. String irBizDate = prs.getString("irBizDate");
  118. InspectionRateInfo irc = null;
  119. //当存在中间表数据,且MES系统id不一致时才更新
  120. if(StringUtils.isNotBlank(irId)&&(!id.equals(mesId))){
  121. irc = iir.getInspectionRateInfo(new ObjectUuidPK(irId));
  122. //日期
  123. irc.setBizDate(ddjdate);
  124. //点检完成率
  125. irc.setComrate(rs.getBigDecimal("iwcl"));
  126. //技能等级
  127. irc.setSkillLevel(rs.getString("cjndj"));
  128. //岗位类型
  129. irc.setPositiontype(rs.getString("cgwlx"));
  130. //是否现场作业
  131. irc.setOnSite(rs.getString("csfxc"));
  132. //班组任职
  133. irc.setTeamApp(rs.getString("cbzrz"));
  134. //工资序列
  135. irc.setSalarysEq(rs.getString("cgzxl"));
  136. //MES 的id
  137. irc.setSimpleName(id);
  138. Timestamp createtime = new Timestamp(System.currentTimeMillis());
  139. irc.setLastUpdateTime(createtime);
  140. upddata.add(irc);
  141. }else {
  142. irc = new InspectionRateInfo();
  143. //行政组织
  144. //部门
  145. AdminOrgUnitInfo adminOrgUnitInfo = new AdminOrgUnitInfo();
  146. adminOrgUnitInfo.setId(BOSUuid.read(depId));
  147. irc.setAdminOrgUnit(adminOrgUnitInfo);
  148. irc.setDepart(adminOrgUnitInfo);
  149. //职位
  150. PositionInfo position = new PositionInfo();
  151. position.setId(BOSUuid.read(posId));
  152. irc.setPosition(position);
  153. //公司
  154. AdminOrgUnitInfo cmpany = new AdminOrgUnitInfo();
  155. cmpany.setId(BOSUuid.read(companyId));
  156. irc.setCompany(cmpany);
  157. //员工编码
  158. irc.setPersonNum(pNumber);
  159. //员工
  160. PersonInfo personInfo = new PersonInfo();
  161. personInfo.setId(BOSUuid.read(pId));
  162. irc.setPerson(personInfo);
  163. //名称
  164. irc.setName(pName);
  165. //编码
  166. irc.setNumber(id);
  167. //日期
  168. irc.setBizDate(ddjdate);
  169. //点检完成率
  170. irc.setComrate(rs.getBigDecimal("iwcl"));
  171. //技能等级
  172. irc.setSkillLevel(rs.getString("cjndj"));
  173. //岗位类型
  174. irc.setPositiontype(rs.getString("cgwlx"));
  175. //是否现场作业
  176. irc.setOnSite(rs.getString("csfxc"));
  177. //班组任职
  178. irc.setTeamApp(rs.getString("cbzrz"));
  179. //工资序列
  180. irc.setSalarysEq(rs.getString("cgzxl"));
  181. //MES 的id
  182. irc.setSimpleName(id);
  183. UserInfo userInfo = new UserInfo();
  184. userInfo.setId(BOSUuid.read("256c221a-0106-1000-e000-10d7c0a813f413B7DE7F"));
  185. CtrlUnitInfo cu = new CtrlUnitInfo();
  186. cu.setId(BOSUuid.read("00000000-0000-0000-0000-000000000000CCE7AED4"));
  187. irc.setCU(cu);
  188. irc.setCreator(userInfo);
  189. Timestamp createtime = new Timestamp(System.currentTimeMillis());
  190. irc.setCreateTime(createtime);
  191. irc.setLastUpdateUser(userInfo);
  192. irc.setLastUpdateTime(createtime);
  193. newdata.add(irc);
  194. }
  195. }
  196. if(upddata.size()>=100){
  197. iir.updateBatchData(upddata);
  198. upddata.clear();
  199. }
  200. if(newdata.size()>=100){
  201. iir.addnewBatchData(newdata);
  202. newdata.clear();
  203. }
  204. }
  205. if(!upddata.isEmpty()){
  206. iir.updateBatchData(upddata);
  207. upddata.clear();
  208. }
  209. if(!newdata.isEmpty()){
  210. iir.addnewBatchData(newdata);
  211. newdata.clear();
  212. }
  213. }catch (SQLException e){
  214. logger.error(e);
  215. } catch (EASBizException e) {
  216. logger.error(e);
  217. }
  218. return "";
  219. }
  220. /**
  221. * 建立Mes系统数据库连接
  222. * @description:
  223. * @author: lhbj
  224. * @date: 2025/5/30 15:55
  225. * @param: null
  226. * @return:null
  227. **/
  228. private Connection getConnection() {
  229. String MesUrl = propt.getProperty("MesUrl");
  230. String MesUserName = propt.getProperty("MesUserName");
  231. String MesPassWord = propt.getProperty("MesPassWord");
  232. Connection connection = null;
  233. try {
  234. // 加载驱动程序
  235. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  236. // 建立连接
  237. connection = DriverManager.getConnection(MesUrl, MesUserName, MesPassWord);
  238. } catch (ClassNotFoundException | SQLException e) {
  239. logger.error(e);
  240. } finally {
  241. }
  242. return connection;
  243. }
  244. /**
  245. * Mes数据库查询
  246. * @description:
  247. * @author: lhbj
  248. * @date: 2025/5/30 15:55
  249. * @param: null
  250. * @return:null
  251. **/
  252. public IRowSet executeQuery(String sql, Object[] params) throws BOSException {
  253. Connection conn = this.getConnection();
  254. PreparedStatement ps = null;
  255. ResultSet rs = null;
  256. JdbcRowSet var18;
  257. try {
  258. ps = conn.prepareStatement(sql);
  259. for (int i = 0; i < params.length; ++i) {
  260. ps.setObject(i + 1, params[i]);
  261. }
  262. rs = ps.executeQuery();
  263. JdbcRowSet rowset = new JdbcRowSet();
  264. rowset.populate(rs);
  265. var18 = rowset;
  266. } catch (SQLException exc) {
  267. StringBuffer sb = new StringBuffer("");
  268. for (int i = 0; i < params.length; ++i) {
  269. sb.append("param ").append(i).append(" is:").append(params[i]);
  270. }
  271. logger.error("sql is:" + sql + " param is:" + sb.toString());
  272. logger.error("sql error!", exc);
  273. throw new BOSException("Sql execute exception : " + sql, exc);
  274. } finally {
  275. cleanup(rs, ps, conn);
  276. }
  277. return var18;
  278. }
  279. /**
  280. * Mes数据库查询
  281. * @description:
  282. * @author: lhbj
  283. * @date: 2025/5/30 15:55
  284. * @param: null
  285. * @return:null
  286. **/
  287. public IRowSet executeQuery(String sql) throws BOSException {
  288. Connection conn = this.getConnection();
  289. Statement statement = null;
  290. ResultSet rs = null;
  291. JdbcRowSet var6;
  292. try {
  293. statement = conn.createStatement();
  294. rs = statement.executeQuery(sql);
  295. JdbcRowSet rowset = new JdbcRowSet();
  296. rowset.populate(rs);
  297. var6 = rowset;
  298. } catch (SQLException exc) {
  299. logger.error("333 sql is:" + sql, exc);
  300. throw new BOSException("Sql3 execute exception : " + sql, exc);
  301. } finally {
  302. cleanup(rs, statement, conn);
  303. }
  304. return var6;
  305. }
  306. /**
  307. * Mes数据库执行
  308. * @description:
  309. * @author: lhbj
  310. * @date: 2025/5/30 15:55
  311. * @param: null
  312. * @return:null
  313. **/
  314. public void execute(String sql) throws BOSException {
  315. Connection conn = this.getConnection();
  316. Statement statement = null;
  317. try {
  318. statement = conn.createStatement();
  319. statement.execute(sql);
  320. } catch (SQLException exc) {
  321. logger.error("sql 1 sql is:" + sql, exc);
  322. throw new BOSException("Sql1 execute exception : " + sql, exc);
  323. } finally {
  324. cleanup(statement, conn);
  325. }
  326. }
  327. /**
  328. * Mes数据库执行
  329. * @description:
  330. * @author: lhbj
  331. * @date: 2025/5/30 15:55
  332. * @param: null
  333. * @return:null
  334. **/
  335. public void execute(String sql, Object[] params) throws BOSException {
  336. Connection conn = this.getConnection();
  337. PreparedStatement ps = null;
  338. try {
  339. ps = conn.prepareStatement(sql);
  340. for(int i = 0; i < params.length; ++i) {
  341. ps.setObject(i + 1, params[i]);
  342. }
  343. ps.execute();
  344. } catch (SQLException exc) {
  345. StringBuffer sb = new StringBuffer("");
  346. for(int i = 0; i < params.length; ++i) {
  347. sb.append("param ").append(i).append(" is:").append(params[i]);
  348. }
  349. logger.error("222 sql is:" + sql + " param is:" + sb.toString(), exc);
  350. throw new BOSException("Sql222 execute exception : " + sql, exc);
  351. } finally {
  352. cleanup(ps, conn);
  353. }
  354. }
  355. public static final void cleanup(Connection cn) {
  356. cleanup((ResultSet) null, (Statement) null, cn);
  357. }
  358. public static void cleanup(Statement stmt) {
  359. cleanup((ResultSet) null, stmt, (Connection) null);
  360. }
  361. public static void cleanup(Statement stmt, Connection cn) {
  362. cleanup((ResultSet) null, stmt, cn);
  363. }
  364. public static void cleanup(ResultSet rs) {
  365. cleanup(rs, (Statement) null, (Connection) null);
  366. }
  367. public static void cleanup(ResultSet rs, Statement stmt) {
  368. cleanup(rs, stmt, (Connection) null);
  369. }
  370. public static void cleanup(ResultSet rs, Statement stmt, Connection cn) {
  371. SQLUtils.cleanup(rs, stmt, cn);
  372. }
  373. }