RetMoneyService.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package com.kingdee.shr.customer.gtiit.osf;
  2. import com.kingdee.bos.BOSException;
  3. import com.kingdee.bos.Context;
  4. import com.kingdee.bos.bsf.service.app.IHRMsfService;
  5. import com.kingdee.eas.common.EASBizException;
  6. import com.kingdee.eas.util.app.DbUtil;
  7. import com.kingdee.jdbc.rowset.IRowSet;
  8. import com.kingdee.util.DateTimeUtils;
  9. import org.apache.commons.lang3.StringUtils;
  10. import org.apache.log4j.Logger;
  11. import java.math.BigDecimal;
  12. import java.text.ParseException;
  13. import java.time.LocalDate;
  14. import java.time.ZoneId;
  15. import java.time.format.DateTimeFormatter;
  16. import java.util.*;
  17. /**
  18. * description: RetMoneyService <br>
  19. * date: 2025/4/21 10:37 <br>
  20. * author: lhbj <br>
  21. * version: 1.0 <br>
  22. */
  23. public class RetMoneyService implements IHRMsfService {
  24. // 定义日志记录器
  25. private static Logger logger = Logger.getLogger("com.kingdee.shr.customer.gtiit.osf.RetMoneyService");
  26. @Override
  27. public Object process(Context ctx, Map<String, Object> map) throws EASBizException, BOSException {
  28. try {
  29. String sql = "\n" +
  30. " /*dialect*/SELECT FMONEY ,FID ,FPERSONID ,FCMPITEMID,FEFFECTDAY ,FLEFFECTDAY,CFTraceability,CFRetroactiveDate,foldfixadjustsalaryid \n" +
  31. " FROM T_HR_SFixAdjustSalary where sysdate between FEFFECTDAY and FLEFFECTDAY \n" +
  32. " and CFTraceability='Yes' and FEFFECTDAY < cfretroactivedate ";
  33. HashSet<String> list = new HashSet<>(); // 用于存储处理过的薪资调整记录ID
  34. logger.error("sql" + sql); // 记录SQL查询语句到日志中
  35. IRowSet rs = DbUtil.executeQuery(ctx, sql); // 执行SQL查询,获取结果集
  36. while (rs.next()) {
  37. BigDecimal sumMoney = BigDecimal.ZERO; // 初始化回溯薪资总额为0
  38. BigDecimal money = rs.getBigDecimal("FMONEY"); // 获取当前记录的薪资调整金额
  39. Date cfretroactivedate = new Date(rs.getDate("cfretroactivedate").getTime()); // 获取回溯日期
  40. String personid = rs.getString("FPERSONID"); // 获取员工ID
  41. String cmpitemid = rs.getString("FCMPITEMID"); // 获取薪资调整项目ID
  42. Date mainEffectday = new Date(rs.getDate("FEFFECTDAY").getTime()); // 获取薪资调整生效日期
  43. Date mainLeffectday = rs.getDate("FLEFFECTDAY"); // 获取薪资调整失效日期
  44. logger.error("dettaiRs-mainEffectday:"+mainEffectday+",leffectday:"+mainLeffectday); //每工作日工资
  45. String mainleftdayYearMonth = DateTimeUtils.format(mainEffectday, "yyyy-MM"); // 格式化生效日期为“年-月”
  46. String oldfixadjustsalaryid = rs.getString("foldfixadjustsalaryid"); // 获取旧的薪资调整ID
  47. String id = rs.getString("fid"); // 获取当前薪资调整记录ID
  48. if (StringUtils.isNotBlank(oldfixadjustsalaryid)) {
  49. String sqlOld = "\n" +
  50. " SELECT FMONEY ,FID ,FPERSONID ,FCMPITEMID,FEFFECTDAY ,FLEFFECTDAY,CFTraceability,CFRetroactiveDate,foldfixadjustsalaryid \n" +
  51. " FROM T_HR_SFixAdjustSalary where fid ='" + oldfixadjustsalaryid + "' ";
  52. logger.error("sqlOld================================= " + sqlOld);
  53. IRowSet dettaiRs = DbUtil.executeQuery(ctx, sqlOld); // 执行SQL查询,获取结果集
  54. String detialid;
  55. if (dettaiRs.next()) {
  56. detialid = dettaiRs.getString("fid"); // 获取子查询记录的ID
  57. logger.error("start detialid================================= " + detialid); // 记录子查询记录的ID到日志中
  58. BigDecimal detailMony = dettaiRs.getBigDecimal("FMONEY"); // 获取子查询记录的薪资调整金额 10000
  59. logger.error("dettaiRs:retMoney " + detailMony); // 记录子查询记录的薪资调整金额到日志中
  60. BigDecimal retMoney = money.subtract(detailMony); // 计算回溯薪资
  61. logger.error("retMoney " + retMoney); // 记录回溯薪资到日志中 2000
  62. Date effectday = dettaiRs.getDate("FEFFECTDAY"); // 获取子查询记录的生效日期
  63. Date leffectday = dettaiRs.getDate("FLEFFECTDAY"); // 获取子查询记录的失效日期
  64. logger.error("dettaiRs-effectday:"+effectday+",leffectday:"+leffectday); //每工作日工资
  65. //获取月份差
  66. List<String> stringList = this.getMonthBetweenDate_V1(mainEffectday,cfretroactivedate);
  67. //获取开始日期
  68. Calendar cal = Calendar.getInstance();
  69. cal.setTime(mainEffectday);
  70. cal.set(Calendar.DAY_OF_MONTH, 1);
  71. String startDate = DateTimeUtils.format(cal.getTime(), "yyyy-MM-dd");
  72. //结束日期
  73. boolean endBool=false;
  74. Calendar cal2 = Calendar.getInstance();
  75. cal2.setTime(cfretroactivedate);
  76. if(cal2.get(Calendar.DAY_OF_MONTH)>1){
  77. cal2.add(Calendar.MONTH, 1);
  78. endBool=true;
  79. }
  80. cal2.set(Calendar.DAY_OF_MONTH, 1);
  81. cal2.add(Calendar.DAY_OF_MONTH, -1);
  82. String endDate = DateTimeUtils.format(cal2.getTime(), "yyyy-MM-dd");
  83. //有效结束日期
  84. String leffectDate = endDate;
  85. if(endBool){
  86. leffectDate = DateTimeUtils.format(cfretroactivedate, "yyyy-MM-dd");
  87. }
  88. //有效日期
  89. String effectDate = DateTimeUtils.format(mainEffectday, "yyyy-MM-dd");
  90. StringBuilder sqlDay = new StringBuilder();
  91. sqlDay.append(" select to_char(wItem.fdate,'yyyy-MM-dd') from T_HR_ATS_WORKCALENDARITEM wItem ");
  92. sqlDay.append(" left join T_HR_ATS_WORKCALENDAR w on w.fid=wItem.fcalendargroupid ");
  93. sqlDay.append(" left join ( ");
  94. sqlDay.append(" select fcalendarid from T_HR_ATS_ATTENDANCEFILE ");
  95. sqlDay.append(" where fproposerid = ? ");
  96. sqlDay.append(" order by fleffdt desc ");
  97. sqlDay.append(" ) cal on cal.fcalendarid=w.fid ");
  98. sqlDay.append(" where wItem.fweek not in(0,6) ");
  99. sqlDay.append(" and to_char(wItem.fdate,'yyyy-MM-dd') >= ? ");
  100. sqlDay.append(" and to_char(wItem.fdate,'yyyy-MM-dd') <= ? ");
  101. sqlDay.append(" order by wItem.fdate desc ");
  102. logger.error("sqlDay:" + sqlDay.toString()); //每工作日工资
  103. //由于每月的天数不一致所以需要每月分开这算
  104. for(String yearMonth : stringList) {
  105. startDate=yearMonth+"-01";
  106. Calendar endCal = Calendar.getInstance();
  107. endCal.setTime(DateTimeUtils.parseDate(startDate));
  108. endCal.add(Calendar.MONTH, 1);
  109. endCal.add(Calendar.DAY_OF_MONTH, -1);
  110. endDate=DateTimeUtils.format(endCal.getTime(), "yyyy-MM-dd");;
  111. logger.error("sqlDay-param:personid:" + personid+",startDate:"+startDate+",endDate:"+endDate); //每工作日工资
  112. IRowSet dayRs1 = DbUtil.executeQuery(ctx, sqlDay.toString(), new String[]{personid, startDate, endDate}); // 执行SQL查询,获取结果集
  113. BigDecimal days1 = BigDecimal.ZERO;
  114. if (dayRs1.next()) {
  115. days1 = new BigDecimal(dayRs1.size());
  116. }
  117. BigDecimal days2 = BigDecimal.ZERO;
  118. String startDate2=startDate.compareTo(effectDate)>0?startDate:effectDate;
  119. String endDate2=endDate.compareTo(leffectDate)>0?leffectDate:endDate;
  120. logger.error("折算sqlDay-param:personid:" + personid+",effectDate:"+effectDate+",endDate:"+endDate); //每工作日工资
  121. logger.error("折算sqlDay-param:personid:" + personid+",startDate2:"+startDate2+",endDate2:"+endDate2); //每工作日工资
  122. IRowSet dayRs2 = DbUtil.executeQuery(ctx, sqlDay.toString(),new String[]{personid,startDate2,endDate2}); // 执行SQL查询,获取结果集
  123. if (dayRs2.next()){
  124. days2= new BigDecimal(dayRs2.size());
  125. }
  126. logger.error("sumMoney-days1:" + days1); //每工作日工资
  127. logger.error("sumMoney-days2:" + days2); //每工作日工资
  128. logger.error("sumMoney:" + sumMoney); //每工作日工资
  129. if(days1.compareTo(BigDecimal.ZERO) > 0) {
  130. BigDecimal dayMoney = retMoney.divide(days1,BigDecimal.ROUND_HALF_UP,BigDecimal.ROUND_HALF_UP);
  131. logger.error("sumMoney-day:" + dayMoney); //每工作日工资
  132. sumMoney = sumMoney.add(dayMoney.multiply(days2));
  133. logger.error("sumMoney-days:" + sumMoney); //折算工资
  134. }else {
  135. sumMoney= sumMoney.add(BigDecimal.ZERO);
  136. }
  137. }
  138. // 定义SQL语句,用于检查是否存在特定的员工薪资支付表记录
  139. String exiSql = "select * from CT_CUS_PersonPayForm where CFPersonID ='" + personid + "' and CFAdjustSalary='" + id + "' ";
  140. logger.error("exiSql sql " + exiSql); // 记录SQL语句到日志中
  141. IRowSet extRs = DbUtil.executeQuery(ctx, exiSql); // 执行SQL查询,获取结果集
  142. String insertSql;
  143. // 如果结果集不为空且包含记录
  144. if (extRs != null && extRs.size() > 0) {
  145. // 定义SQL语句,用于更新员工薪资支付表中的回溯薪资
  146. insertSql = "/*dialect*/ update CT_CUS_PersonPayForm set CFRetMoney=" + sumMoney.setScale(2, 4) +",CFMonth=to_date('" + DateTimeUtils.format(cfretroactivedate) + "','yyyy-MM-dd')"+ " where CFAdjustSalary='" + id + "' ";
  147. logger.error("updateSql " + insertSql); // 记录SQL语句到日志中
  148. DbUtil.execute(ctx, insertSql); // 执行SQL更新操作
  149. } else {
  150. // 如果结果集为空或不包含记录
  151. // 定义SQL语句,用于向员工薪资支付表中插入新的回溯薪资记录
  152. insertSql = " /*dialect*/ insert into CT_CUS_PersonPayForm(fid,CFPersonID,CFMonth,CFRetMoney,cfadjustsalary) values(newbosid('5FB9A7AC'),'" + personid + "',to_date('" + DateTimeUtils.format(cfretroactivedate) + "','yyyy-MM-dd')" + "," + sumMoney.setScale(2, 5) + ",'" + id + "')";
  153. logger.error("insertSql " + insertSql); // 记录SQL语句到日志中
  154. DbUtil.execute(ctx, insertSql); // 执行SQL插入操作
  155. }
  156. }
  157. }
  158. }
  159. String sft = "update CT_CUS_PersonPayForm set CFRetMoney=0 where CFAdjustSalary in(select fid from T_HR_SFixAdjustSalary where CFTraceability='No')";
  160. DbUtil.execute(ctx, sft); // 执行SQL插入操作
  161. } catch (Exception e) {
  162. e.printStackTrace();
  163. throw new BOSException(e);
  164. }
  165. return null;
  166. }
  167. // 获取两个日期之间的月份集合
  168. public static List<String> getMonthBetweenDate_V1(Date startTime, Date endTime) {
  169. List<String> list = new ArrayList<>();
  170. LocalDate startDate = startTime.toInstant()
  171. .atZone(ZoneId.systemDefault())
  172. .toLocalDate();
  173. LocalDate endDate = endTime.toInstant()
  174. .atZone(ZoneId.systemDefault())
  175. .toLocalDate();
  176. // 计算年份和月份差值
  177. int startYear = startDate.getYear();
  178. int startMonth = startDate.getMonthValue();
  179. int endYear = endDate.getYear();
  180. int endMonth = endDate.getMonthValue();
  181. int yearDifference = endYear - startYear;
  182. int monthDifference = yearDifference * 12 + (endMonth - startMonth);
  183. // 如果 endDate 小于 startLocalDate 的下个月的第一天,则减去 1
  184. LocalDate nextMonthFirstDay = startDate.plusMonths(monthDifference).withDayOfMonth(1);
  185. if (endDate.isBefore(nextMonthFirstDay)) {
  186. monthDifference--;
  187. }
  188. if (endDate.isAfter(nextMonthFirstDay)) {
  189. monthDifference++;
  190. }
  191. int month=0;
  192. do{
  193. month++;
  194. String monthStr =startMonth+"";
  195. if(startMonth<10){
  196. monthStr="0"+monthStr;
  197. }
  198. String yearMonth = startYear + "-" + monthStr;
  199. if(12==startMonth){
  200. startMonth=1;
  201. startYear=startYear+1;
  202. }else {
  203. startMonth++;
  204. }
  205. list.add(yearMonth);
  206. }while (month < monthDifference);
  207. return list;
  208. }
  209. }