package com.goldenwater.web.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.goldenwater.common.core.domain.entity.SysDept; import com.goldenwater.common.core.domain.entity.SysOrganizationExt; import com.goldenwater.common.exception.ServiceException; import com.goldenwater.common.utils.SecurityUtils; import com.goldenwater.common.utils.StringUtils; import com.goldenwater.common.utils.uuid.IdUtils; import com.goldenwater.slaj.hidd.domain.AttOrgBase; import com.goldenwater.slaj.hidd.domain.AttOrgExt; import com.goldenwater.slaj.hidd.service.IAttOrgBaseService; import com.goldenwater.slaj.hidd.service.IAttOrgExtService; import com.goldenwater.system.mapper.SysOrganizationExtMapper; import com.goldenwater.system.service.ISysDeptService; import com.goldenwater.web.domain.OrgTypeConst; import java.text.SimpleDateFormat; import java.util.Date; /** * 水利机构联合服务:新增水利机构时同一事务写入 * att_org_base(机构基础信息)、sys_dept(部门)、att_org_ext(机构扩展信息), * 三者要么全部成功,要么全部回滚。三张表以同一 GUID 关联: * sys_dept.dept_id = att_org_base.guid = att_org_ext.org_guid。 */ @Service public class OrgCompositeService { @Autowired private ISysDeptService deptService; @Autowired private IAttOrgBaseService attOrgBaseService; @Autowired private IAttOrgExtService attOrgExtService; @Autowired private SysOrganizationExtMapper sysOrganizationExtMapper; private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private String nowText() { return DATE_FORMAT.format(new Date()); } private SysOrganizationExt buildUnifiedExt(String extGuid, String orgGuid, AttOrgExt ext) { SysOrganizationExt orgExt = new SysOrganizationExt(); orgExt.setGuid(extGuid); orgExt.setOrgGuid(orgGuid); orgExt.setStanGrad(ext.getStanGrad()); orgExt.setFundSour(ext.getFundSour()); orgExt.setAddr(ext.getAddr()); orgExt.setRegLoc(ext.getRegLoc()); orgExt.setLegPers(ext.getLegPers()); orgExt.setLegPersTel(ext.getLegPersTel()); orgExt.setSafetyLegPers(ext.getSafetyLegPers()); orgExt.setSafetyLegPersTel(ext.getSafetyLegPersTel()); orgExt.setSafeProdConta(ext.getSafeProdConta()); orgExt.setSafeProdContaTel(ext.getSafeProdContaTel()); if (ext.getWorkPopNum() != null) { orgExt.setWorkPopNum(ext.getWorkPopNum().intValue()); } return orgExt; } private void insertUnifiedExt(String extGuid, String orgGuid, AttOrgExt ext) { SysOrganizationExt orgExt = buildUnifiedExt(extGuid, orgGuid, ext); orgExt.setCreateUser(SecurityUtils.getUserId()); orgExt.setCreateTime(nowText()); sysOrganizationExtMapper.insertSysOrganizationExt(orgExt); } private void upsertUnifiedExt(String orgGuid, AttOrgExt ext) { String extGuid = resolveExtGuid(orgGuid, ext.getGuid()); SysOrganizationExt orgExt = buildUnifiedExt(extGuid, orgGuid, ext); orgExt.setModifyUser(SecurityUtils.getUserId()); orgExt.setModifyTime(nowText()); sysOrganizationExtMapper.upsertSysOrganizationExt(orgExt); } private String resolveExtGuid(String orgGuid, String fallbackGuid) { SysOrganizationExt existing = sysOrganizationExtMapper.selectByOrgGuid(orgGuid); if (existing != null && StringUtils.isNotEmpty(existing.getGuid())) { return existing.getGuid(); } return StringUtils.isNotEmpty(fallbackGuid) ? fallbackGuid : IdUtils.fastSimpleUUID(); } /** * 新增水利机构及其基础信息、扩展信息(同一事务) * * @param dept 机构(sys_dept) * @param ext 机构扩展信息(att_org_ext) * @return 新机构 deptId(= att_org_base.guid) */ @Transactional public String insertOrgWithExt(SysDept dept, AttOrgExt ext, String orgCode) { if (!deptService.checkDeptNameUnique(dept)) { throw new ServiceException("新增机构'" + dept.getDeptName() + "'失败,机构名称已存在"); } // 上级机构 = 扩展表里的上级单位(proOrgGuid),也是 sys_dept 的 parent_id String parentGuid = ext.getProOrgGuid(); // 1) 先插入 sys_dept(框架 insertDept 内部会重新生成 deptId) dept.setParentId(parentGuid); dept.setOrgType(OrgTypeConst.WATER); dept.setOrgCheckType(OrgTypeConst.ORG_CHECK_WATER); dept.setType(ext.getOrgType()); dept.setCreateBy(SecurityUtils.getUserId()); deptService.insertDept(dept); // 以 sys_dept 实际保存的 deptId 作为三表统一主键/外键 String guid = dept.getDeptId(); // 2) att_org_base 机构基础信息 AttOrgBase base = new AttOrgBase(); base.setGuid(guid); base.setOrgCode(orgCode); base.setPguid(parentGuid); base.setOrgName(dept.getDeptName()); base.setOrgLevel(ext.getOrgLevel()); base.setOrgType(ext.getOrgType()); base.setStatus(dept.getStatus()); base.setModifier(SecurityUtils.getUserId()); attOrgBaseService.insertAttOrgBase(base); // 3) att_org_ext 机构扩展信息,从 dept 同步冗余字段 String extGuid = IdUtils.fastSimpleUUID(); ext.setGuid(extGuid); ext.setOrgGuid(guid); ext.setWiunName(dept.getDeptName()); attOrgExtService.insertAttOrgExt(ext); // 4) sys_organization_ext 统一扩展表(与 sys_dept 一一对应) insertUnifiedExt(extGuid, guid, ext); return guid; } /** * 修改水利机构及其基础信息、扩展信息(同一事务) * 同时更新 att_org_base,使机构代码(org_code)等基础信息在编辑时一并落库。 * * @param dept 机构(sys_dept),deptId 必须已存在 * @param ext 机构扩展信息(att_org_ext) * @param orgCode 机构代码(att_org_base.org_code,前端填写) */ @Transactional public void updateOrgWithExt(SysDept dept, AttOrgExt ext, String orgCode) { // 上级机构 = 扩展表里的上级单位(proOrgGuid),也是 sys_dept 的 parent_id String parentGuid = ext.getProOrgGuid(); // 1) att_org_base 机构基础信息(机构代码由前端填写) AttOrgBase base = new AttOrgBase(); base.setGuid(dept.getDeptId()); base.setOrgCode(orgCode); base.setPguid(parentGuid); base.setOrgName(dept.getDeptName()); base.setOrgLevel(ext.getOrgLevel()); base.setOrgType(ext.getOrgType()); base.setStatus(dept.getStatus()); base.setModifier(SecurityUtils.getUserId()); attOrgBaseService.updateAttOrgBase(base); // 2) sys_dept 部门,parent_id = 上级单位 dept.setParentId(parentGuid); dept.setOrgType(OrgTypeConst.WATER); dept.setOrgCheckType(OrgTypeConst.ORG_CHECK_WATER); dept.setType(ext.getOrgType()); dept.setUpdateBy(SecurityUtils.getUserId()); deptService.updateDept(dept); // 3) att_org_ext 机构扩展信息,从 dept 同步冗余字段保证数据一致 String extGuid = resolveExtGuid(dept.getDeptId(), ext.getGuid()); AttOrgExt oldExt = attOrgExtService.selectAttOrgExtByOrgGuid(dept.getDeptId()); if (oldExt != null){ ext.setGuid(oldExt.getGuid()); ext.setOrgGuid(dept.getDeptId()); ext.setWiunName(dept.getDeptName()); attOrgExtService.updateAttOrgExt(ext); } else { ext.setGuid(extGuid); ext.setOrgGuid(dept.getDeptId()); ext.setWiunName(dept.getDeptName()); attOrgExtService.insertAttOrgExt(ext); } // 4) 同步统一扩展表 sys_organization_ext upsertUnifiedExt(dept.getDeptId(), ext); } /** * 删除水利机构:三表全删,任一失败即回滚 * * @param extGuid att_org_ext 主键 GUID */ @Transactional public void deleteOrgWithExt(String extGuid) { AttOrgExt ext = attOrgExtService.selectAttOrgExtByGuid(extGuid); if (ext == null) return; String orgGuid = ext.getOrgGuid(); // 1) 删除扩展表 att_org_ext attOrgExtService.deleteAttOrgExtByGuid(extGuid); // 2) 删除基础信息表 att_org_base attOrgBaseService.deleteAttOrgBaseByGuid(orgGuid); // 4) 删除统一扩展表 sys_organization_ext sysOrganizationExtMapper.deleteByOrgGuid(orgGuid); // 3) 删除机构表 sys_dept(软删除) deptService.deleteDeptById(orgGuid); } }