PerAtsOverTimeBillBatchEditHandlerTestEx.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package com.kingdee.shr.customer.gtiit.handler;
  2. import com.kingdee.bos.BOSException;
  3. import com.kingdee.bos.Context;
  4. import com.kingdee.eas.basedata.person.PersonInfo;
  5. import com.kingdee.eas.framework.CoreBaseInfo;
  6. import com.kingdee.eas.hr.ats.AtsOverTimeBillEntryCollection;
  7. import com.kingdee.eas.hr.ats.AtsOverTimeBillEntryInfo;
  8. import com.kingdee.eas.hr.ats.AtsOverTimeBillInfo;
  9. import com.kingdee.eas.hr.ats.AttendFileStateEnum;
  10. import com.kingdee.eas.util.app.DbUtil;
  11. import com.kingdee.jdbc.rowset.IRowSet;
  12. import com.kingdee.shr.ats.web.handler.PerAtsOverTimeBillBatchEditHandler;
  13. import com.kingdee.shr.base.syssetting.context.SHRContext;
  14. import com.kingdee.shr.base.syssetting.exception.SHRWebException;
  15. import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
  16. import com.kingdee.shr.base.syssetting.web.json.JSONUtils;
  17. import com.kingdee.shr.customer.gtiit.util.BaseUtil;
  18. import com.kingdee.shr.customer.gtiit.util.DateTimeUtils;
  19. import org.apache.commons.lang3.StringUtils;
  20. import org.apache.log4j.Logger;
  21. import org.springframework.ui.ModelMap;
  22. import org.springframework.util.LinkedMultiValueMap;
  23. import javax.servlet.http.HttpServletRequest;
  24. import javax.servlet.http.HttpServletResponse;
  25. import java.sql.SQLException;
  26. import java.text.SimpleDateFormat;
  27. import java.time.Instant;
  28. import java.time.LocalTime;
  29. import java.time.ZoneId;
  30. import java.util.*;
  31. /**
  32. * test
  33. */
  34. public class PerAtsOverTimeBillBatchEditHandlerTestEx extends PerAtsOverTimeBillBatchEditHandler {
  35. private static Logger logger = Logger
  36. .getLogger("com.kingdee.shr.customer.gtiit.handler.PerAtsOverTimeBillBatchEditHandlerExExEx");
  37. public void workMultipleAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
  38. throws SHRWebException {
  39. Context ctx = SHRContext.getInstance().getContext();
  40. String otDate = request.getParameter("otDate");
  41. logger.error("加班日期:" + otDate);
  42. String sql = "SELECT b.CFWORKMULTIPLE FROM T_HR_ATS_LegalHoliday a left join T_HR_ATS_LegalHolidayItem b on a.fid = b.FGROUPID where a.FSTATE = '1' and b.FSTARTDATE <= '"
  43. + otDate + "' and b.FENDDATE >= '" + otDate + "'";
  44. logger.error("查询加班类型SQL:" + sql);
  45. Map<String, Map<String, String>> overTimeTypeMap = BaseUtil.getOverTimeType(ctx);
  46. Map<String, String> returnMap = new HashMap<String, String>();
  47. try {
  48. IRowSet iRowSet = DbUtil.executeQuery(ctx, sql);
  49. while (iRowSet.next()) {
  50. if (iRowSet.getString("CFWORKMULTIPLE").equals("1")) {
  51. // 工作日加班
  52. returnMap = overTimeTypeMap.get("001");
  53. } else if (iRowSet.getString("CFWORKMULTIPLE").equals("2")) {
  54. // 休息日加班
  55. returnMap = overTimeTypeMap.get("002");
  56. } else if (iRowSet.getString("CFWORKMULTIPLE").equals("3")) {
  57. // 法定节假日加班
  58. returnMap = overTimeTypeMap.get("003");
  59. }
  60. }
  61. } catch (BOSException e) {
  62. e.printStackTrace();
  63. } catch (SQLException throwables) {
  64. throwables.printStackTrace();
  65. }
  66. JSONUtils.writeJson(response, returnMap);
  67. }
  68. public void jobTypeAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
  69. throws SHRWebException {
  70. Calendar calendar = Calendar.getInstance();
  71. Context ctx = SHRContext.getInstance().getContext();
  72. Map<String, Map<String, String>> overTimeTypeMap = BaseUtil.getOverTimeType(ctx);
  73. Map<String, String> returnMap = new HashMap<String, String>();
  74. String otDate = request.getParameter("otDate");
  75. logger.error("加班日期:" + otDate);
  76. Date parseDate = DateTimeUtils.parseDate(otDate, "yyyy-MM-dd");
  77. calendar.setTime(parseDate);
  78. int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
  79. if (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY) {
  80. // 休息日加班
  81. returnMap = overTimeTypeMap.get("002");
  82. } else {
  83. // 工作日加班
  84. returnMap = overTimeTypeMap.get("001");
  85. }
  86. JSONUtils.writeJson(response, returnMap);
  87. }
  88. @Override
  89. protected void verifyModel(HttpServletRequest request, HttpServletResponse response, CoreBaseInfo model)
  90. throws SHRWebException {
  91. super.verifyModel(request, response, model);
  92. checkOverTime(model);
  93. checkCompensate(model);
  94. //增加一位员工每个自然月只能提一张加班单校验
  95. checkOnlyTakeOneBill(request, response, model);
  96. }
  97. @Override
  98. protected void beforeSubmit(HttpServletRequest request, HttpServletResponse response, CoreBaseInfo model)
  99. throws SHRWebException {
  100. super.beforeSubmit(request, response, model);
  101. //增加一位员工每个自然月只能提一张加班单校验
  102. checkOnlyTakeOneBill(request, response, model);
  103. checkOverTime(model);
  104. checkCompensate(model);
  105. }
  106. private void checkCompensate(CoreBaseInfo model) throws ShrWebBizException {
  107. Context ctx = SHRContext.getInstance().getContext();
  108. AtsOverTimeBillInfo billInfo = (AtsOverTimeBillInfo) model;
  109. AtsOverTimeBillEntryCollection entries = billInfo.getEntries();
  110. Set<String> personIdSet = new HashSet<String>();
  111. String compenSql = "select fid from T_HR_ATS_OverTimeCompens where fnumber='002'";
  112. String compenId = "";
  113. try {
  114. IRowSet compenRowSet = DbUtil.executeQuery(ctx, compenSql);
  115. if (compenRowSet.next()) {
  116. compenId = compenRowSet.getString("fid");
  117. }
  118. } catch (SQLException e1) {
  119. e1.printStackTrace();
  120. } catch (BOSException e) {
  121. e.printStackTrace();
  122. }
  123. for (int i = 0; i < entries.size(); i++) {
  124. AtsOverTimeBillEntryInfo entryInfo = entries.get(i);
  125. if (StringUtils.equals(entryInfo.getOtCompens().getId().toString(), compenId)) {
  126. personIdSet.add(entryInfo.getPerson().getId().toString());
  127. }
  128. }
  129. logger.error("员工ID集合:" + personIdSet.toString());
  130. StringBuffer strBuffer = new StringBuffer();
  131. for (String personId : personIdSet) {
  132. strBuffer.append("'" + personId + "',");
  133. }
  134. if (strBuffer.length() > 0) {
  135. strBuffer = strBuffer.deleteCharAt(strBuffer.length() - 1);
  136. Map<String, String> personIdByNameMap = BaseUtil.getPersonIdByName(ctx, personIdSet);
  137. String sql = "SELECT p.fid,emp.jobgradename FROM t_bd_person p "
  138. + "left join (select gra.fname_l2 jobgradename,e.fpersonid from "
  139. + "(SELECT FPERSONID,max(fleffdt) as maxDate FROM T_HR_EmpPostExperienceHis group by FPERSONID) as h "
  140. + "left join T_HR_EmpPostExperienceHis e on e.fpersonid= h.FPERSONID and fleffdt = h.maxDate "
  141. + "left join (SELECT * FROM T_HR_EmpPostRank where fislatest='1') r on r.fpersonid = e.fpersonid "
  142. + "left join T_HR_JobGrade gra on gra.fid=r.fjobgradeid) emp on p.fid=emp.fpersonid "
  143. + "where p.fid in (" + strBuffer.toString() + ")";
  144. try {
  145. IRowSet rowSet = DbUtil.executeQuery(ctx, sql);
  146. while (rowSet.next()) {
  147. String jobGradeName = rowSet.getString("jobgradename");
  148. if (StringUtils.isNotBlank(jobGradeName)) {
  149. jobGradeName = jobGradeName.replace("L", "");
  150. int gradeLevel = Integer.parseInt(jobGradeName);
  151. if (gradeLevel >= 8) {
  152. throw new ShrWebBizException("Employee (" + personIdByNameMap.get(rowSet.getString("fid"))
  153. + ") with a rank greater than or equal to L8 is not allowed to submit overtime compensation forms with overtime pay as the compensation type");
  154. }
  155. }
  156. }
  157. String personTypeSql = "SELECT a.fid,e.fnumber FROM T_BD_Person a "
  158. + "left join (select b.fpersonid,c.CFWorkercategoryID from (SELECT FPERSONID,max(FEFFDT) as maxDate FROM T_HR_EmpOrgRelation where fassignType = '1' group by FPERSONID) b "
  159. + "left join T_HR_EmpOrgRelation c on b.fpersonid = c.fpersonid and b.maxdate=c.FEFFDT and c.fassignType = '1') d on a.fid=d.fpersonid "
  160. + "left join CT_MP_WorkerCategory e on e.fid=d.CFWorkercategoryID " + "where a.fid in ("
  161. + strBuffer.toString() + ")";
  162. IRowSet personTypeRow = DbUtil.executeQuery(ctx, personTypeSql);
  163. while (personTypeRow.next()) {
  164. String workCategoryNumber = personTypeRow.getString("fnumber");
  165. if (StringUtils.equals("GTIIT_FACULTY", workCategoryNumber)
  166. || StringUtils.equals("GTIIT_OTHER", workCategoryNumber)) {
  167. throw new ShrWebBizException("Employee ("
  168. + personIdByNameMap.get(personTypeRow.getString("fid"))
  169. + ") is an academic staff member and cannot submit overtime compensation forms with overtime pay as the compensation method");
  170. }
  171. }
  172. } catch (BOSException e) {
  173. e.printStackTrace();
  174. } catch (SQLException e) {
  175. e.printStackTrace();
  176. }
  177. }
  178. }
  179. private void checkOverTime(CoreBaseInfo model) throws ShrWebBizException {
  180. Context ctx = SHRContext.getInstance().getContext();
  181. AtsOverTimeBillInfo billInfo = (AtsOverTimeBillInfo) model;
  182. AtsOverTimeBillEntryCollection entries = billInfo.getEntries();
  183. LinkedMultiValueMap<String, Map<String, Date>> multiMap = new LinkedMultiValueMap<>();
  184. StringBuffer personIdBuffer = new StringBuffer();
  185. for (int i = 0; i < entries.size(); i++) {
  186. AtsOverTimeBillEntryInfo entryInfo = entries.get(i);
  187. if (!entryInfo.getOtType().getString("id").equals("rBy0u1YgQ9C1OxcM85mxyY6C/nU=")) {
  188. continue;
  189. }
  190. Date otDate = entryInfo.getOtDate();
  191. String dateFormat = DateTimeUtils.dateFormat(otDate, "yyyy-MM-dd");
  192. String sql = "SELECT b.CFWORKMULTIPLE FROM T_HR_ATS_LegalHoliday a left join T_HR_ATS_LegalHolidayItem b on a.fid = b.FGROUPID where a.FSTATE = '1' and b.FSTARTDATE <= '"
  193. + dateFormat + "' and b.FENDDATE >= '" + dateFormat + "'";
  194. try {
  195. IRowSet iRowSet = DbUtil.executeQuery(ctx, sql);
  196. if (iRowSet.next()) {
  197. continue;
  198. }
  199. } catch (BOSException e) {
  200. e.printStackTrace();
  201. } catch (SQLException e) {
  202. e.printStackTrace();
  203. }
  204. Map<String, Date> map = new HashMap<>();
  205. map.put("startTime", entryInfo.getStartTime());
  206. map.put("endTime", entryInfo.getEndTime());
  207. multiMap.add(entryInfo.getPerson().getString("id"), map);
  208. personIdBuffer.append("'" + entryInfo.getPerson().getString("id") + "',");
  209. }
  210. Map<String, String> personIdByNameMap = BaseUtil.getPersonIdByName(ctx, multiMap.keySet());
  211. if (personIdBuffer.length() > 0) {
  212. String sql = "SELECT a.FPROPOSERID,c.FRESTPRETIME,c.FRESTNEXTTIME FROM T_HR_ATS_AttendanceFile a left join T_HR_ATS_Shift b on a.FATSSHIFTID = b.fid left join T_HR_ATS_ShiftItem c on c.FGROUPID = b.fid "
  213. + "where FPROPOSERID in (" + personIdBuffer.deleteCharAt(personIdBuffer.length() - 1)
  214. + ") and a.FATTENDFILESTATE = '" + AttendFileStateEnum.ENABLE_VALUE + "'";
  215. try {
  216. IRowSet rowSet = DbUtil.executeQuery(ctx, sql);
  217. while (rowSet.next()) {
  218. String personId = rowSet.getString("FPROPOSERID");
  219. String restStart = rowSet.getObject("FRESTPRETIME") == null ? "12:00"
  220. : rowSet.getString("FRESTPRETIME");
  221. String restEnd = rowSet.getObject("FRESTNEXTTIME") == null ? "13:30"
  222. : rowSet.getString("FRESTNEXTTIME");
  223. List<Map<String, Date>> list = multiMap.get(personId);
  224. for (Map<String, Date> dateMap : list) {
  225. Date startTime = dateMap.get("startTime");
  226. Date endTime = dateMap.get("endTime");
  227. Instant startInstant = startTime.toInstant();
  228. Instant endInstant = endTime.toInstant();
  229. LocalTime startLocalTime = startInstant.atZone(ZoneId.systemDefault()).toLocalTime();
  230. LocalTime endLocalTime = endInstant.atZone(ZoneId.systemDefault()).toLocalTime();
  231. LocalTime shiftStartTime = LocalTime.parse(restStart);
  232. LocalTime shiftendTime = LocalTime.parse(restEnd);
  233. if (startLocalTime.compareTo(shiftendTime) >= 0
  234. || endLocalTime.compareTo(shiftStartTime) <= 0) {
  235. continue;
  236. }
  237. if ((startLocalTime.compareTo(shiftStartTime) >= 0
  238. && startLocalTime.compareTo(shiftendTime) <= 0)
  239. || (endLocalTime.compareTo(shiftStartTime) >= 0
  240. && endLocalTime.compareTo(shiftendTime) <= 0)) {
  241. throw new ShrWebBizException("Employee (" + personIdByNameMap.get(personId)
  242. + ") cannot work overtime during rest time on weekdays");
  243. }
  244. }
  245. }
  246. } catch (BOSException e) {
  247. e.printStackTrace();
  248. } catch (SQLException e) {
  249. e.printStackTrace();
  250. }
  251. }
  252. }
  253. /**
  254. * 一位员工每个自然月只能提一张加班单校验
  255. * @param request
  256. * @param response
  257. * @param model
  258. */
  259. protected void checkOnlyTakeOneBill(HttpServletRequest request, HttpServletResponse response, CoreBaseInfo model) throws ShrWebBizException {
  260. Context ctx = SHRContext.getInstance().getContext();
  261. try {
  262. AtsOverTimeBillInfo billInfo = (AtsOverTimeBillInfo) model;
  263. AtsOverTimeBillEntryCollection entries = billInfo.getEntries();
  264. StringBuilder sql = new StringBuilder(" select e.fid from T_HR_ATS_OVERTIMEBILL b ");
  265. sql.append(" left join T_HR_ATS_OVERTIMEBILLentry e on b.fid= e.fbillid ");
  266. sql.append(" where e.fpersonid=? and b.fbillstate in ('1','2','3') and to_char(e.fotdate,'yyyy-MM-dd')>= ? and to_char(e.fotdate,'yyyy-MM-dd')<= ? ");
  267. SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
  268. for (int i = 0; i < entries.size(); i++) {
  269. AtsOverTimeBillEntryInfo ats = entries.get(i);
  270. Date otdate = ats.getOtDate();
  271. PersonInfo personInfo = ats.getPerson();
  272. Calendar calendar = Calendar.getInstance();
  273. calendar.setTime(otdate);
  274. calendar.set(Calendar.DAY_OF_MONTH, 1);
  275. String fotdate1 = sf.format(calendar.getTime());
  276. calendar.add(Calendar.MONTH, 1);
  277. calendar.add(Calendar.DAY_OF_MONTH, -1);
  278. String fotdate2 = sf.format(calendar.getTime());
  279. IRowSet rs = DbUtil.executeQuery(ctx, sql.toString(), new String[]{personInfo.getId().toString(), fotdate1, fotdate2});
  280. if(rs.size()>0){
  281. throw new ShrWebBizException("Each employee can apply for overtime only once per natural month.Employee ("+personInfo.getName()+") has already applied.");
  282. }
  283. }
  284. }catch (BOSException e){
  285. e.printStackTrace();
  286. }
  287. }
  288. }