fb611932b4b1f53dd12dcf4882b39f7e90e78862.svn-base 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package com.kingdee.eas.custom.facade;
  2. import java.math.BigDecimal;
  3. import java.sql.SQLException;
  4. import java.sql.Timestamp;
  5. import java.util.ArrayList;
  6. import java.util.Calendar;
  7. import java.util.Date;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. import org.apache.commons.lang3.StringUtils;
  12. import org.apache.log4j.Logger;
  13. import com.kingdee.bos.BOSException;
  14. import com.kingdee.bos.Context;
  15. import com.kingdee.eas.common.EASBizException;
  16. import com.kingdee.eas.custom.utils.DateTimeUtils;
  17. import com.kingdee.eas.custom.utils.SplitLeaveUtils;
  18. import com.kingdee.eas.framework.CoreBaseCollection;
  19. import com.kingdee.eas.hr.ats.AtsLeaveBillCollection;
  20. import com.kingdee.eas.hr.ats.AtsLeaveBillEntryCollection;
  21. import com.kingdee.eas.hr.ats.AtsLeaveBillEntryFactory;
  22. import com.kingdee.eas.hr.ats.AtsLeaveBillEntryInfo;
  23. import com.kingdee.eas.hr.ats.AtsLeaveBillFactory;
  24. import com.kingdee.eas.hr.ats.AtsLeaveBillInfo;
  25. import com.kingdee.eas.hr.ats.HolidayPolicyCollection;
  26. import com.kingdee.eas.hr.ats.HolidayPolicyFactory;
  27. import com.kingdee.eas.hr.ats.HolidayTypeCollection;
  28. import com.kingdee.eas.hr.ats.HolidayTypeFactory;
  29. import com.kingdee.eas.util.app.DbUtil;
  30. import com.kingdee.jdbc.rowset.IRowSet;
  31. public class SickQuotaFacadeControllerBean extends AbstractSickQuotaFacadeControllerBean
  32. {
  33. private static Logger logger =
  34. Logger.getLogger("com.kingdee.eas.custom.facade.SickQuotaFacadeControllerBean");
  35. @Override
  36. protected void _createQuota(Context ctx, String executeDate) throws BOSException, EASBizException {
  37. try {
  38. Date nowDate = new Date();
  39. logger.error("日期参数:"+executeDate);
  40. if(StringUtils.isNotBlank(executeDate)) {
  41. nowDate = DateTimeUtils.parseDate(executeDate, "yyyy-MM-dd");
  42. }
  43. Calendar calendar = Calendar.getInstance();
  44. calendar.setTime(nowDate);
  45. String dateFormat = DateTimeUtils.dateFormat(nowDate, "yyyy-MM-dd");
  46. BigDecimal zero = BigDecimal.ZERO;
  47. String holidayPolicyIds = this.getHolidayPolicyIds(ctx, "JQLX000003Y");
  48. logger.error("假期制度为病假的id集合:"+holidayPolicyIds);
  49. if(StringUtils.isBlank(holidayPolicyIds)) {
  50. return;
  51. }
  52. //获取该月已授予的额度
  53. String realitySql = "select cfpersonid,CFRealityGrantLimit from CT_CUS_SickLeaveLimit where year(cfmonth) = "+calendar.get(Calendar.YEAR)+" and month(cfmonth) = "+(calendar.get(Calendar.MONTH)+1)+" and (cfisimport is null or cfisimport <> '1') and CFIsGrant = '1'";
  54. logger.error("获取该月已授予额度sql:"+realitySql);
  55. IRowSet realityRow = DbUtil.executeQuery(ctx, realitySql);
  56. Map<String, BigDecimal> realityMap = new HashMap<>();
  57. while(realityRow.next()) {
  58. realityMap.put(realityRow.getString("cfpersonid"), realityRow.getBigDecimal("CFRealityGrantLimit"));
  59. }
  60. String countSql = "select count(1) count from CT_CUS_SickLeaveLimit where (cfisimport is null or cfisimport <> '1')";
  61. logger.error("是否已初始化SQL额度:"+countSql);
  62. IRowSet countRow = DbUtil.executeQuery(ctx, countSql);
  63. BigDecimal dataCount = zero;
  64. while(countRow.next()) {
  65. dataCount = countRow.getBigDecimal("count")==null?zero:countRow.getBigDecimal("count");
  66. }
  67. //删除当月非导入的数据
  68. String deleteSql = "delete from CT_CUS_SickLeaveLimit where year(cfmonth) = "+calendar.get(Calendar.YEAR)+" and month(cfmonth) = "+(calendar.get(Calendar.MONTH)+1)+" and (cfisimport is null or cfisimport <> '1')";
  69. logger.error("删除当月非导入数据sql:"+deleteSql);
  70. DbUtil.execute(ctx, deleteSql);
  71. String sql = "select e.fpersonid personid,p.cffse fse,p.cffsme fsme,w.fname_l2 percategoryname,w.fnumber percategoryNumber,r.fenterdate enterdate from T_HR_EmpOrgRelation e " +
  72. "left join T_BD_Person p on e.fpersonid=p.fid " +
  73. "left join CT_MP_WorkerCategory w on e.cfworkercategoryid=w.fid " +
  74. "left join T_HR_EmpLaborRelationHis r on r.FPERSONID=e.fpersonid " +
  75. "left join T_HR_BDEmployeeType b on b.fid=r.flaborrelationstateid " +
  76. "where e.feffdt <= '"+dateFormat+"' and e.fleffdt >= '"+dateFormat+"' and e.fassignType = '1' and e.fislatestinaday = '1' " +
  77. "and b.finservice not in ('2','3') and r.fstartdatetime <= '"+dateFormat+"' and r.fenddatetime >= '"+dateFormat+"' " +
  78. "and e.fpersonid in (select FPROPOSERID from T_HR_ATS_HolidayLimit where FHOLIDAYPOLICYID in ("+holidayPolicyIds+") )";
  79. logger.error("查询员工病假信息SQL:"+sql);
  80. StringBuffer strBuffer = new StringBuffer();
  81. StringBuffer uptBuffer = new StringBuffer();
  82. String holidaySql = "select b.fid,b.fremainlimit,b.fstandardlimit,b.fproposerid from (SELECT fproposerid,max(FEFFECTDATE) maxdate FROM T_HR_ATS_HolidayLimit where FHOLIDAYPOLICYID in ("+holidayPolicyIds+") group by fproposerid) a left join T_HR_ATS_HolidayLimit b on a.fproposerid = b.fproposerid and a.maxdate = b.FEFFECTDATE where b.FHOLIDAYPOLICYID in ("+holidayPolicyIds+")";
  83. logger.error("查询员工假期额度信息SQL:"+holidaySql);
  84. IRowSet holidayRow = DbUtil.executeQuery(ctx, holidaySql);
  85. Map<String, BigDecimal> holidayMap = new HashMap<>();
  86. Map<String, String> personHolidayMap = new HashMap<>();
  87. Map<String, BigDecimal> standLimitMap = new HashMap<>();
  88. while (holidayRow.next()){
  89. personHolidayMap.put(holidayRow.getString("fproposerid"), holidayRow.getString("fid"));
  90. holidayMap.put(holidayRow.getString("fproposerid"), holidayRow.getObject("fremainlimit")==null?zero:holidayRow.getBigDecimal("fremainlimit"));
  91. standLimitMap.put(holidayRow.getString("fproposerid"), holidayRow.getObject("fstandardlimit")==null?zero:holidayRow.getBigDecimal("fstandardlimit"));
  92. }
  93. //初始额度
  94. String initialSql = "SELECT cfpersonid,cfinitiallimit FROM CT_CUS_SickLeaveLimit where cfisimport = '1' and cfpersonid not in (SELECT FPROPOSERID FROM T_HR_ATS_HolidayLimit where FHOLIDAYPOLICYID in ("+holidayPolicyIds+") group by FPROPOSERID having count(1)>1)";
  95. logger.error("查询员工初始额度信息SQL:"+initialSql);
  96. IRowSet initialRow = DbUtil.executeQuery(ctx, initialSql);
  97. Map<String, BigDecimal> initialMap = new HashMap<>();
  98. while (initialRow.next()){
  99. initialMap.put(initialRow.getString("cfpersonid"), initialRow.getObject("cfinitiallimit")==null?zero:initialRow.getBigDecimal("cfinitiallimit"));
  100. }
  101. //员工历史每月实际授予额度汇总
  102. String grantSql = "SELECT cfpersonid,sum(CFRealityGrantLimit) sumGrantLimit FROM CT_CUS_SickLeaveLimit where (cfisimport is null or cfisimport <> '1') and cfholidayid in (select b.fid from (SELECT fproposerid,max(FEFFECTDATE) maxdate FROM T_HR_ATS_HolidayLimit where FHOLIDAYPOLICYID in ("+holidayPolicyIds+") group by fproposerid) a left join T_HR_ATS_HolidayLimit b on a.fproposerid = b.fproposerid and a.maxdate = b.FEFFECTDATE where b.FHOLIDAYPOLICYID in ("+holidayPolicyIds+")) group by cfpersonid";
  103. IRowSet grantRow = DbUtil.executeQuery(ctx, grantSql);
  104. Map<String, BigDecimal> grantMap = new HashMap<>();
  105. while (grantRow.next()){
  106. grantMap.put(grantRow.getString("cfpersonid"), grantRow.getObject("sumGrantLimit")==null?zero:grantRow.getBigDecimal("sumGrantLimit"));
  107. }
  108. calendar.set(Calendar.DAY_OF_MONTH, 1);
  109. calendar.add(Calendar.MONTH, 1);
  110. String nextDateFormat = DateTimeUtils.dateFormat(calendar.getTime(), "yyyy-MM-dd");
  111. String leaveSql = "SELECT a.fid billId,b.fid entryId FROM T_HR_ATS_LeaveBill a left join T_HR_ATS_LeaveBillEntry b on a.FID = b.FBILLID left join T_HR_ATS_HolidayPolicy c on b.FPOLICYID = c.FID left join T_HR_ATS_HolidayType d on c.FHOLIDAYTYPEID = d.FID "
  112. + "where d.FNUMBER = 'JQLX000003Y' and a.FBILLSTATE in ('1','2','3') and b.FREALENDTIME >= '"+nextDateFormat+"' ";
  113. // String leaveSql = "SELECT top 2 a.fid billId,b.fid entryId FROM T_HR_ATS_LeaveBill a left join T_HR_ATS_LeaveBillEntry b on a.FID = b.FBILLID left join T_HR_ATS_HolidayPolicy c on b.FPOLICYID = c.FID left join T_HR_ATS_HolidayType d on c.FHOLIDAYTYPEID = d.FID "
  114. // + "where d.FNUMBER = 'JQLX000001Y'";
  115. IRowSet leaveRow = DbUtil.executeQuery(ctx, leaveSql);
  116. StringBuffer leaveBuffer = new StringBuffer();
  117. while(leaveRow.next()) {
  118. leaveBuffer.append("'"+leaveRow.getString("billId")+"',");
  119. }
  120. //额度拆分
  121. List<Map<String, Object>> leaveDetailList = new ArrayList<>();
  122. if(leaveBuffer.length()>0) {
  123. leaveBuffer = leaveBuffer.deleteCharAt(leaveBuffer.length()-1);
  124. AtsLeaveBillCollection atsLeaveBillCollection = AtsLeaveBillFactory.getLocalInstance(ctx).getAtsLeaveBillCollection("where id in ("+leaveBuffer.toString()+")");
  125. for(int i = 0;i<atsLeaveBillCollection.size();i++) {
  126. AtsLeaveBillInfo atsLeaveBillInfo = atsLeaveBillCollection.get(i);
  127. AtsLeaveBillEntryCollection entryColl = atsLeaveBillInfo.getEntries();
  128. for(int j = 0;j<entryColl.size();j++) {
  129. AtsLeaveBillEntryInfo entryInfo = entryColl.get(j);
  130. Date realEndDate = new Date(entryInfo.getRealEndTime().getTime());
  131. //只拆分实际结束日期在本月之后的
  132. if(realEndDate.compareTo(calendar.getTime())<0) {
  133. continue;
  134. }
  135. SplitLeaveUtils splitLeaveUtils = new SplitLeaveUtils(ctx, atsLeaveBillInfo, entryInfo);
  136. leaveDetailList.addAll(splitLeaveUtils.getSplitLeaveBill());
  137. }
  138. }
  139. }
  140. Map<String, BigDecimal> leaveLimitMap = new HashMap<>();
  141. for(int i=0;i<leaveDetailList.size();i++) {
  142. Map<String, Object> detailMap = leaveDetailList.get(i);
  143. Date currDate = (Date) detailMap.get("currDate");
  144. String personId = (String) detailMap.get("personId");
  145. BigDecimal leaveLength = (BigDecimal)detailMap.get("leaveLength");
  146. //只统计日期在本月之后的
  147. if(currDate.compareTo(calendar.getTime())<0) {
  148. continue;
  149. }
  150. if(leaveLimitMap.containsKey(personId)) {
  151. BigDecimal personLeave = leaveLimitMap.get(personId);
  152. leaveLimitMap.put(personId, personLeave.add(leaveLength));
  153. }else {
  154. leaveLimitMap.put(personId, leaveLength);
  155. }
  156. }
  157. IRowSet iRowSet = DbUtil.executeQuery(ctx, sql);
  158. int count = 0;
  159. while (iRowSet.next()){
  160. String personId = iRowSet.getString("personid");
  161. String percategoryNumber = iRowSet.getString("percategoryNumber")==null?"":iRowSet.getString("percategoryNumber");
  162. BigDecimal sickQuota = iRowSet.getObject("fse")==null?zero:iRowSet.getBigDecimal("fse");
  163. BigDecimal highestQuota = iRowSet.getObject("fsme")==null?zero:iRowSet.getBigDecimal("fsme");
  164. //行政人员
  165. if(percategoryNumber.equals("GTIIT_GAS") || percategoryNumber.equals("GTIIT_SAS") || percategoryNumber.equals("GTIIT_PSS")){
  166. if(sickQuota.compareTo(zero)<=0) {
  167. sickQuota = BigDecimal.valueOf(15);
  168. }
  169. highestQuota = BigDecimal.valueOf(75);
  170. }
  171. //学术人员教师最高病假额度小于等于0时不限制
  172. boolean flag = highestQuota.compareTo(zero)>0;
  173. //平均每月额度
  174. BigDecimal avgMonthLimit = sickQuota.divide(BigDecimal.valueOf(12),9, BigDecimal.ROUND_HALF_UP);
  175. //理论每月额度
  176. BigDecimal theoryMonthLimit = avgMonthLimit.setScale(2, BigDecimal.ROUND_HALF_DOWN);
  177. //实际授予额度
  178. BigDecimal realityLimit= theoryMonthLimit;
  179. //剩余额度
  180. BigDecimal residueLimit = holidayMap.get(personId)==null?zero:holidayMap.get(personId);
  181. //标准额度
  182. BigDecimal standLimit = standLimitMap.get(personId)==null?zero:standLimitMap.get(personId);
  183. //初始额度
  184. BigDecimal initialLimit = initialMap.get(personId)==null?zero:initialMap.get(personId);
  185. if(initialLimit.compareTo(highestQuota)>0 && flag) {
  186. initialLimit = highestQuota;
  187. }
  188. if(dataCount.compareTo(zero)==0) {
  189. //本次增减剩余额度和标准额度的天数
  190. BigDecimal addLimit = initialLimit;
  191. if(residueLimit.add(initialLimit).compareTo(highestQuota)>0 && flag) {
  192. addLimit = highestQuota.subtract(residueLimit).subtract(initialLimit).compareTo(zero)<0?zero:highestQuota.subtract(residueLimit).subtract(initialLimit);
  193. }
  194. if(residueLimit.compareTo(highestQuota)>0 && flag) {
  195. addLimit = zero;
  196. }else if(residueLimit.add(initialLimit).compareTo(highestQuota)>0 && flag) {
  197. addLimit = highestQuota.subtract(residueLimit);
  198. }
  199. standLimit = standLimit.add(addLimit);
  200. residueLimit = residueLimit.add(addLimit);
  201. }
  202. //减去当月已授予额度
  203. BigDecimal deductionsLimit = realityMap.get(personId)==null?zero:realityMap.get(personId);
  204. standLimit = standLimit.subtract(deductionsLimit);
  205. residueLimit = residueLimit.subtract(deductionsLimit);
  206. BigDecimal personLeave = leaveLimitMap.get(personId)==null?zero:leaveLimitMap.get(personId);
  207. // residueLimit = residueLimit.add(personLeave);
  208. BigDecimal realResidueLimit = residueLimit.add(personLeave);
  209. //计算实际授予额度
  210. /*if(residueLimit.compareTo(highestQuota)>0 && flag) {
  211. realityLimit = zero;
  212. }else if(residueLimit.add(theoryMonthLimit).compareTo(highestQuota)>0 && flag) {
  213. realityLimit = highestQuota.subtract(residueLimit);
  214. }*/
  215. if(realResidueLimit.compareTo(highestQuota)>0 && flag) {
  216. realityLimit = highestQuota.subtract(realResidueLimit);
  217. }else if(realResidueLimit.add(theoryMonthLimit).compareTo(highestQuota)>0 && flag) {
  218. realityLimit = highestQuota.subtract(realResidueLimit);
  219. }
  220. String HolidayId = personHolidayMap.get(personId)==null?"":personHolidayMap.get(personId);
  221. //是否已授予
  222. String isGrant = "0";
  223. if(isLastDay(nowDate)) {
  224. isGrant = "1";
  225. standLimit = standLimit.add(realityLimit);
  226. residueLimit = residueLimit.add(realityLimit);
  227. }
  228. String insertSql = "insert into CT_CUS_SickLeaveLimit(fid,cfmonth,cfpersonid,cfavgmonthlimit,CFTheoryMonthLimit,CFRealityGrantLimit,cfholidayid,CFIsGrant) values(newbosid('5B27CA8D'),'"+dateFormat+"','"+iRowSet.getString("personid")+"','"+avgMonthLimit.toPlainString()+"','"+theoryMonthLimit.toPlainString()+"','"+realityLimit.toPlainString()+"','"+HolidayId+"','"+isGrant+"');";
  229. strBuffer.append(insertSql);
  230. String uptSql = "update T_HR_ATS_HolidayLimit set FSTANDARDLIMIT = '"+standLimit+"',FREALLIMIT = "+standLimit+"+FADDORSUBLIMIT,FREMAINLIMIT = "+residueLimit+" where fid = '"+HolidayId+"';";
  231. uptBuffer.append(uptSql);
  232. count++;
  233. if(count > 3000){
  234. DbUtil.execute(ctx, strBuffer.toString());
  235. strBuffer = new StringBuffer();
  236. DbUtil.execute(ctx, uptBuffer.toString());
  237. uptBuffer = new StringBuffer();
  238. count = 0;
  239. }
  240. }
  241. if(strBuffer.length()>0){
  242. DbUtil.execute(ctx, strBuffer.toString());
  243. }
  244. if(uptBuffer.length()>0){
  245. DbUtil.execute(ctx, uptBuffer.toString());
  246. }
  247. } catch (BOSException e) {
  248. e.printStackTrace();
  249. } catch (SQLException throwables) {
  250. throwables.printStackTrace();
  251. }
  252. }
  253. public boolean isLastDay(Date nowDate) {
  254. Calendar calendar = Calendar.getInstance();
  255. calendar.setTime(nowDate);
  256. int currentDate = calendar.get(Calendar.DAY_OF_MONTH);
  257. int lastDate = calendar.getActualMaximum(Calendar.DATE);
  258. if (currentDate == lastDate) {
  259. return true;
  260. } else {
  261. return false;
  262. }
  263. }
  264. /**
  265. * 获取假期类型为对应的假期制度id集合
  266. * @param ctx
  267. * @param holidayNumber
  268. * @return
  269. */
  270. public String getHolidayPolicyIds(Context ctx, String holidayNumber) {
  271. StringBuffer holidayPolicyIds = new StringBuffer();
  272. try {
  273. HolidayTypeCollection holidayTypeCollection = HolidayTypeFactory.getLocalInstance(ctx).getHolidayTypeCollection("where number = '"+holidayNumber+"'");
  274. if(holidayTypeCollection!=null && holidayTypeCollection.size()>0) {
  275. HolidayPolicyCollection holidayPolicyCollection = HolidayPolicyFactory.getLocalInstance(ctx).getHolidayPolicyCollection("where holidayType = '"+holidayTypeCollection.get(0).getId().toString()+"'");
  276. for(int i=0;i<holidayPolicyCollection.size();i++) {
  277. holidayPolicyIds.append("'"+holidayPolicyCollection.get(i).getId().toString()+"',");
  278. }
  279. if(holidayPolicyIds.length()>0) {
  280. holidayPolicyIds = holidayPolicyIds.deleteCharAt(holidayPolicyIds.length()-1);
  281. }
  282. }
  283. } catch (BOSException e) {
  284. e.printStackTrace();
  285. }
  286. return holidayPolicyIds.toString();
  287. }
  288. }