SyncMesTpmInsRateFacadeControllerBean.java 16 KB

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