|
|
@@ -0,0 +1,408 @@
|
|
|
+package biz.xygz;
|
|
|
+
|
|
|
+import base.po.RccyPo;
|
|
|
+import jakarta.servlet.http.HttpServletRequest;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.logging.log4j.LogManager;
|
|
|
+import org.apache.logging.log4j.Logger;
|
|
|
+import ss.WebC;
|
|
|
+import ss.cmn.str.StrJson;
|
|
|
+import ss.db.Db;
|
|
|
+import ss.db.SqlCmd;
|
|
|
+import ss.ddp.DdpC;
|
|
|
+import ss.obj.ObjC;
|
|
|
+import ss.serv.Serv;
|
|
|
+import ss.serv.ServC;
|
|
|
+import ss.user.UserRunB;
|
|
|
+import ss.user.UserU;
|
|
|
+
|
|
|
+import java.sql.Timestamp;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.LinkedHashSet;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * 值班人员维护。
|
|
|
+ *
|
|
|
+ * 值班人员只写 RCCY;不会新增、停用或删除团体的 TTCY/TTWCY 记录。
|
|
|
+ */
|
|
|
+public class TtZbMemberServ extends Serv {
|
|
|
+
|
|
|
+ private static final Logger log4j = LogManager.getLogger(TtZbMemberServ.class);
|
|
|
+
|
|
|
+ @SuppressWarnings("unused")
|
|
|
+ private String obj;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 值班人员列表中的普通“增加”按钮。
|
|
|
+ */
|
|
|
+ @SuppressWarnings("unused")
|
|
|
+ public String saveByA() throws Exception {
|
|
|
+ HttpServletRequest request = super.req;
|
|
|
+ UserRunB user = UserU.rdUserByReq(request);
|
|
|
+ if (user == null)
|
|
|
+ return fail(request, "登录状态已失效,请重新登录");
|
|
|
+
|
|
|
+ try (Db gdba = Db.makeConn()) {
|
|
|
+ long rcid = rdLongParam(request, "rcid", "缺少值班ID");
|
|
|
+ long ryid = rdLongParam(request, "ryid", "请选择值班人员");
|
|
|
+ DutyContext context = loadDuty(gdba, rcid, true);
|
|
|
+ validateSelectedMembers(gdba, context.ttid,
|
|
|
+ new LinkedHashSet<>(List.of(ryid)));
|
|
|
+
|
|
|
+ LinkedHashSet<Long> desired = new LinkedHashSet<>(
|
|
|
+ loadActiveMemberIds(gdba, rcid));
|
|
|
+ desired.add(ryid);
|
|
|
+ syncMembers(gdba, context, desired, user);
|
|
|
+ gdba.commit();
|
|
|
+
|
|
|
+ request.setAttribute(ServC.REQ_msg, "增加成功");
|
|
|
+ return WebC.FWD_info;
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ return fail(request, e.getMessage());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log4j.error("TtZbMemberServ.saveByA() 异常", e);
|
|
|
+ return fail(request, "值班人员保存失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * “拖放选择”页面的保存。
|
|
|
+ *
|
|
|
+ * 已选项是本次值班的最终人员集合;未再选中的人员会软停用。
|
|
|
+ */
|
|
|
+ @SuppressWarnings("unused")
|
|
|
+ public String saveDdpByA() throws Exception {
|
|
|
+ HttpServletRequest request = super.req;
|
|
|
+ UserRunB user = UserU.rdUserByReq(request);
|
|
|
+ if (user == null)
|
|
|
+ return fail(request, "登录状态已失效,请重新登录");
|
|
|
+
|
|
|
+ try (Db gdba = Db.makeConn()) {
|
|
|
+ long rcid = rdDdpParentId(request);
|
|
|
+ DutyContext context = loadDuty(gdba, rcid, true);
|
|
|
+ LinkedHashSet<Long> selected = rdSelectedMemberIds(
|
|
|
+ request.getParameter(DdpC.REQ_ddpSelId));
|
|
|
+ validateSelectedMembers(gdba, context.ttid, selected);
|
|
|
+
|
|
|
+ syncMembers(gdba, context, selected, user);
|
|
|
+ gdba.commit();
|
|
|
+
|
|
|
+ request.setAttribute(ServC.REQ_msg, "保存成功");
|
|
|
+ return WebC.FWD_info;
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ return fail(request, e.getMessage());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log4j.error("TtZbMemberServ.saveDdpByA() 异常", e);
|
|
|
+ return fail(request, "值班人员保存失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private DutyContext loadDuty(Db gdba, long rcid, boolean checkEditable)
|
|
|
+ throws Exception {
|
|
|
+ SqlCmd cmd = new SqlCmd(
|
|
|
+ "select RCID,DXID,RCLBM,DXM,JLZTM,JSSJ,CJRYID,GLRYID"
|
|
|
+ + " from RC where RCID=:rcid");
|
|
|
+ cmd.wrLongParm("rcid", rcid);
|
|
|
+ gdba.doSelByC(cmd);
|
|
|
+ if (gdba.rowNum != 1)
|
|
|
+ throw new IllegalArgumentException("值班记录不存在,请刷新后重试");
|
|
|
+
|
|
|
+ String dxm = StringUtils.trimToEmpty(gdba.rdStrCol("dxm"));
|
|
|
+ int rclbm = gdba.rdIntCol("rclbm");
|
|
|
+ int jlztm = gdba.rdIntCol("jlztm");
|
|
|
+ Long ttid = gdba.rdLongCol("dxid");
|
|
|
+
|
|
|
+ if (!"xshZb".equals(dxm) && !"xyttZb".equals(dxm))
|
|
|
+ throw new IllegalArgumentException("当前记录不是值班记录");
|
|
|
+ if (rclbm != 75)
|
|
|
+ throw new IllegalArgumentException("当前记录不是值班记录");
|
|
|
+ if (ttid == null)
|
|
|
+ throw new IllegalArgumentException("值班记录未关联所属团体");
|
|
|
+ if (jlztm != ObjC.JLZT_inUse)
|
|
|
+ throw new IllegalArgumentException("当前值班不是有效状态,不能维护值班人员");
|
|
|
+
|
|
|
+ Timestamp endTime = gdba.rdTimestampCol("jssj");
|
|
|
+ if (checkEditable && endTime != null
|
|
|
+ && !endTime.after(new Timestamp(System.currentTimeMillis())))
|
|
|
+ throw new IllegalArgumentException("值班已结束,只能查看值班人员");
|
|
|
+
|
|
|
+ DutyContext context = new DutyContext();
|
|
|
+ context.rcid = rcid;
|
|
|
+ context.ttid = ttid;
|
|
|
+ context.creatorId = gdba.rdLongCol("cjryid");
|
|
|
+ context.managerId = gdba.rdLongCol("glryid");
|
|
|
+ validateTt(gdba, ttid);
|
|
|
+ return context;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void validateTt(Db gdba, long ttid) throws Exception {
|
|
|
+ SqlCmd cmd = new SqlCmd(
|
|
|
+ "select TTID from TT where TTID=:ttid and JLZTM in(22,25,51)"
|
|
|
+ + " and (KSRQ is null or KSRQ<=trunc(sysdate))"
|
|
|
+ + " and (JSRQ is null or JSRQ>=trunc(sysdate))");
|
|
|
+ cmd.wrLongParm("ttid", ttid);
|
|
|
+ gdba.doSelByC(cmd);
|
|
|
+ if (gdba.rowNum != 1)
|
|
|
+ throw new IllegalArgumentException("值班所属团体不存在、已停用或不在有效期内");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void validateSelectedMembers(Db gdba, long ttid,
|
|
|
+ Set<Long> selected) throws Exception {
|
|
|
+ if (selected == null || selected.isEmpty())
|
|
|
+ return;
|
|
|
+
|
|
|
+ Set<Long> allowed = loadAllowedMemberIds(gdba, ttid);
|
|
|
+ List<Long> invalid = selected.stream()
|
|
|
+ .filter(ryid -> !allowed.contains(ryid))
|
|
|
+ .toList();
|
|
|
+ if (!invalid.isEmpty())
|
|
|
+ throw new IllegalArgumentException(
|
|
|
+ "所选人员不属于当前值班团体或已失效,请刷新后重试");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private Set<Long> loadAllowedMemberIds(Db gdba, long ttid)
|
|
|
+ throws Exception {
|
|
|
+ SqlCmd cmd = new SqlCmd(
|
|
|
+ "select RYID from XYGZ_TT_MEMBER_V"
|
|
|
+ + " where TTID=:ttid and JLZTM=51");
|
|
|
+ cmd.wrLongParm("ttid", ttid);
|
|
|
+ List<Map<String, Object>> rows = gdba.doSelMapListByC(cmd);
|
|
|
+ Set<Long> result = new HashSet<>();
|
|
|
+ if (rows != null) {
|
|
|
+ for (Map<String, Object> row : rows) {
|
|
|
+ Object value = row.get("ryid");
|
|
|
+ if (value != null)
|
|
|
+ result.add(Long.parseLong(String.valueOf(value)));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private List<Long> loadActiveMemberIds(Db gdba, long rcid)
|
|
|
+ throws Exception {
|
|
|
+ SqlCmd cmd = new SqlCmd(
|
|
|
+ "select RYID from RCCY where RCID=:rcid"
|
|
|
+ + " and JLZTM=:jlztm and DLRYID is null"
|
|
|
+ + " order by RCCYID");
|
|
|
+ cmd.wrLongParm("rcid", rcid);
|
|
|
+ cmd.wrIntParm("jlztm", ObjC.JLZT_inUse);
|
|
|
+ List<Map<String, Object>> rows = gdba.doSelMapListByC(cmd);
|
|
|
+ List<Long> result = new ArrayList<>();
|
|
|
+ if (rows != null) {
|
|
|
+ for (Map<String, Object> row : rows) {
|
|
|
+ Object value = row.get("ryid");
|
|
|
+ if (value != null)
|
|
|
+ result.add(Long.parseLong(String.valueOf(value)));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void syncMembers(Db gdba, DutyContext context,
|
|
|
+ LinkedHashSet<Long> selected, UserRunB user)
|
|
|
+ throws Exception {
|
|
|
+ List<MemberRow> rows = loadMemberRows(gdba, context.rcid);
|
|
|
+ Map<Long, Boolean> handled = new HashMap<>();
|
|
|
+
|
|
|
+ for (MemberRow row : rows) {
|
|
|
+ boolean wanted = selected.contains(row.ryid);
|
|
|
+ boolean first = !handled.containsKey(row.ryid);
|
|
|
+ handled.put(row.ryid, true);
|
|
|
+
|
|
|
+ if (wanted && first && row.jlztm != ObjC.JLZT_inUse)
|
|
|
+ updateStatus(gdba, row.rccyid, ObjC.JLZT_inUse, user);
|
|
|
+ else if ((!wanted || !first) && row.jlztm == ObjC.JLZT_inUse)
|
|
|
+ updateStatus(gdba, row.rccyid, ObjC.JLZT_inSus, user);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (Long ryid : selected) {
|
|
|
+ if (!handled.containsKey(ryid))
|
|
|
+ insertMember(gdba, context.rcid, ryid, user);
|
|
|
+ }
|
|
|
+
|
|
|
+ syncPrimaryMember(gdba, context, selected, user);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private List<MemberRow> loadMemberRows(Db gdba, long rcid)
|
|
|
+ throws Exception {
|
|
|
+ SqlCmd cmd = new SqlCmd(
|
|
|
+ "select RCCYID,RYID,JLZTM from RCCY"
|
|
|
+ + " where RCID=:rcid and DLRYID is null"
|
|
|
+ + " order by RYID,"
|
|
|
+ + " case when JLZTM=51 then 0 when JLZTM=55 then 1 else 2 end,"
|
|
|
+ + " RCCYID");
|
|
|
+ cmd.wrLongParm("rcid", rcid);
|
|
|
+ List<Map<String, Object>> rows = gdba.doSelMapListByC(cmd);
|
|
|
+ List<MemberRow> result = new ArrayList<>();
|
|
|
+ if (rows != null) {
|
|
|
+ for (Map<String, Object> row : rows) {
|
|
|
+ MemberRow item = new MemberRow();
|
|
|
+ item.rccyid = rdLong(row.get("rccyid"));
|
|
|
+ item.ryid = rdLong(row.get("ryid"));
|
|
|
+ item.jlztm = rdInt(row.get("jlztm"));
|
|
|
+ result.add(item);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void insertMember(Db gdba, long rcid, long ryid, UserRunB user)
|
|
|
+ throws Exception {
|
|
|
+ RccyPo po = new RccyPo();
|
|
|
+ po.setRcid(rcid);
|
|
|
+ po.setRyid(ryid);
|
|
|
+ po.setGzlfxbl(0F);
|
|
|
+ po.setJlztm(ObjC.JLZT_inUse);
|
|
|
+ po.setCjryid(user.ryid);
|
|
|
+ po.setGlryid(user.ryid);
|
|
|
+ po.setGlbmid(user.bmid);
|
|
|
+ gdba.doInsPo(po, Set.of("rcid", "ryid", "gzlfxbl", "jlztm",
|
|
|
+ "cjryid", "glryid", "glbmid"));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void updateStatus(Db gdba, long rccyid,
|
|
|
+ int jlztm, UserRunB user) throws Exception {
|
|
|
+ SqlCmd cmd = new SqlCmd(
|
|
|
+ "update RCCY set JLZTM=:jlztm,GLRYID=:glryid,GLBMID=:glbmid"
|
|
|
+ + " where RCCYID=:rccyid");
|
|
|
+ cmd.wrIntParm("jlztm", jlztm);
|
|
|
+ cmd.wrLongParm("glryid", user.ryid);
|
|
|
+ cmd.wrLongParm("glbmid", user.bmid);
|
|
|
+ cmd.wrLongParm("rccyid", rccyid);
|
|
|
+ gdba.doModByC(cmd);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void syncPrimaryMember(Db gdba, DutyContext context,
|
|
|
+ LinkedHashSet<Long> selected,
|
|
|
+ UserRunB user) throws Exception {
|
|
|
+ Long primaryRyid = selected.isEmpty()
|
|
|
+ ? firstNonNull(context.managerId, context.creatorId, user.ryid)
|
|
|
+ : selected.iterator().next();
|
|
|
+ Long primaryBmid = null;
|
|
|
+
|
|
|
+ if (primaryRyid != null) {
|
|
|
+ SqlCmd bmCmd = new SqlCmd(
|
|
|
+ "select BMID from RY where RYID=:ryid and JLZTM in(22,25,51)");
|
|
|
+ bmCmd.wrLongParm("ryid", primaryRyid);
|
|
|
+ gdba.doSelByC(bmCmd);
|
|
|
+ if (gdba.rowNum == 1)
|
|
|
+ primaryBmid = gdba.rdLongCol("bmid");
|
|
|
+ }
|
|
|
+ if (primaryRyid == null)
|
|
|
+ throw new IllegalArgumentException("没有可写入的值班负责人");
|
|
|
+
|
|
|
+ SqlCmd cmd = new SqlCmd(
|
|
|
+ "update RC set FZRYID=:fzryid,FZBMID=:fzbmid,"
|
|
|
+ + "GLRYID=:glryid,GLBMID=:glbmid"
|
|
|
+ + " where RCID=:rcid and RCLBM=75 and JLZTM=51");
|
|
|
+ cmd.wrLongParm("fzryid", primaryRyid);
|
|
|
+ cmd.wrLongParm("fzbmid", primaryBmid);
|
|
|
+ cmd.wrLongParm("glryid", user.ryid);
|
|
|
+ cmd.wrLongParm("glbmid", user.bmid);
|
|
|
+ cmd.wrLongParm("rcid", context.rcid);
|
|
|
+ if (gdba.doModByC(cmd) != 1)
|
|
|
+ throw new IllegalArgumentException("值班记录已变化,请刷新后重试");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private LinkedHashSet<Long> rdSelectedMemberIds(String json) {
|
|
|
+ LinkedHashSet<Long> result = new LinkedHashSet<>();
|
|
|
+ List<String> values = StrJson.rdListByKeyByArr(json, DdpC.DDP_idName);
|
|
|
+ if (values != null) {
|
|
|
+ for (String value : values) {
|
|
|
+ if (StringUtils.isBlank(value))
|
|
|
+ continue;
|
|
|
+ result.add(Long.parseLong(value));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private long rdDdpParentId(HttpServletRequest request) {
|
|
|
+ String value = StringUtils.trimToNull(
|
|
|
+ request.getParameter(DdpC.REQ_ddpPobjIdVal));
|
|
|
+ if (value == null)
|
|
|
+ value = StringUtils.trimToNull(request.getParameter("rcid"));
|
|
|
+ if (value == null)
|
|
|
+ value = StringUtils.trimToNull(request.getParameter(WebC.REQ_pobjId));
|
|
|
+ if (value == null)
|
|
|
+ throw new IllegalArgumentException("缺少值班ID,无法保存值班人员");
|
|
|
+ try {
|
|
|
+ return Long.parseLong(value);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ throw new IllegalArgumentException("值班ID格式不正确");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private long rdLongParam(HttpServletRequest request,
|
|
|
+ String name, String msg) {
|
|
|
+ String value = StringUtils.trimToNull(request.getParameter(name));
|
|
|
+ if (value == null)
|
|
|
+ throw new IllegalArgumentException(msg);
|
|
|
+ try {
|
|
|
+ return Long.parseLong(value);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ throw new IllegalArgumentException(name + " 格式不正确");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private long rdLong(Object value) {
|
|
|
+ return value == null ? 0L : Long.parseLong(String.valueOf(value));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private int rdInt(Object value) {
|
|
|
+ return value == null ? 0 : Integer.parseInt(String.valueOf(value));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private Long firstNonNull(Long... values) {
|
|
|
+ for (Long value : values)
|
|
|
+ if (value != null)
|
|
|
+ return value;
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private String fail(HttpServletRequest request, String message) {
|
|
|
+ request.setAttribute(ServC.REQ_msg,
|
|
|
+ StringUtils.defaultIfBlank(message, "值班人员保存失败"));
|
|
|
+ return WebC.FWD_info;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static final class DutyContext {
|
|
|
+ long rcid;
|
|
|
+ long ttid;
|
|
|
+ Long creatorId;
|
|
|
+ Long managerId;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static final class MemberRow {
|
|
|
+ long rccyid;
|
|
|
+ long ryid;
|
|
|
+ int jlztm;
|
|
|
+ }
|
|
|
+}
|