OrgCompositeService.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package com.goldenwater.web.service;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Service;
  4. import org.springframework.transaction.annotation.Transactional;
  5. import com.goldenwater.common.core.domain.entity.SysDept;
  6. import com.goldenwater.common.core.domain.entity.SysOrganizationExt;
  7. import com.goldenwater.common.exception.ServiceException;
  8. import com.goldenwater.common.utils.SecurityUtils;
  9. import com.goldenwater.common.utils.StringUtils;
  10. import com.goldenwater.common.utils.uuid.IdUtils;
  11. import com.goldenwater.slaj.hidd.domain.AttOrgBase;
  12. import com.goldenwater.slaj.hidd.domain.AttOrgExt;
  13. import com.goldenwater.slaj.hidd.service.IAttOrgBaseService;
  14. import com.goldenwater.slaj.hidd.service.IAttOrgExtService;
  15. import com.goldenwater.system.mapper.SysOrganizationExtMapper;
  16. import com.goldenwater.system.service.ISysDeptService;
  17. import com.goldenwater.web.domain.OrgTypeConst;
  18. import java.text.SimpleDateFormat;
  19. import java.util.Date;
  20. /**
  21. * 水利机构联合服务:新增水利机构时同一事务写入
  22. * att_org_base(机构基础信息)、sys_dept(部门)、att_org_ext(机构扩展信息),
  23. * 三者要么全部成功,要么全部回滚。三张表以同一 GUID 关联:
  24. * sys_dept.dept_id = att_org_base.guid = att_org_ext.org_guid。
  25. */
  26. @Service
  27. public class OrgCompositeService
  28. {
  29. @Autowired
  30. private ISysDeptService deptService;
  31. @Autowired
  32. private IAttOrgBaseService attOrgBaseService;
  33. @Autowired
  34. private IAttOrgExtService attOrgExtService;
  35. @Autowired
  36. private SysOrganizationExtMapper sysOrganizationExtMapper;
  37. private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  38. private String nowText()
  39. {
  40. return DATE_FORMAT.format(new Date());
  41. }
  42. private SysOrganizationExt buildUnifiedExt(String extGuid, String orgGuid, AttOrgExt ext)
  43. {
  44. SysOrganizationExt orgExt = new SysOrganizationExt();
  45. orgExt.setGuid(extGuid);
  46. orgExt.setOrgGuid(orgGuid);
  47. orgExt.setStanGrad(ext.getStanGrad());
  48. orgExt.setFundSour(ext.getFundSour());
  49. orgExt.setAddr(ext.getAddr());
  50. orgExt.setRegLoc(ext.getRegLoc());
  51. orgExt.setLegPers(ext.getLegPers());
  52. orgExt.setLegPersTel(ext.getLegPersTel());
  53. orgExt.setSafetyLegPers(ext.getSafetyLegPers());
  54. orgExt.setSafetyLegPersTel(ext.getSafetyLegPersTel());
  55. orgExt.setSafeProdConta(ext.getSafeProdConta());
  56. orgExt.setSafeProdContaTel(ext.getSafeProdContaTel());
  57. if (ext.getWorkPopNum() != null)
  58. {
  59. orgExt.setWorkPopNum(ext.getWorkPopNum().intValue());
  60. }
  61. return orgExt;
  62. }
  63. private void insertUnifiedExt(String extGuid, String orgGuid, AttOrgExt ext)
  64. {
  65. SysOrganizationExt orgExt = buildUnifiedExt(extGuid, orgGuid, ext);
  66. orgExt.setCreateUser(SecurityUtils.getUserId());
  67. orgExt.setCreateTime(nowText());
  68. sysOrganizationExtMapper.insertSysOrganizationExt(orgExt);
  69. }
  70. private void upsertUnifiedExt(String orgGuid, AttOrgExt ext)
  71. {
  72. String extGuid = resolveExtGuid(orgGuid, ext.getGuid());
  73. SysOrganizationExt orgExt = buildUnifiedExt(extGuid, orgGuid, ext);
  74. orgExt.setModifyUser(SecurityUtils.getUserId());
  75. orgExt.setModifyTime(nowText());
  76. sysOrganizationExtMapper.upsertSysOrganizationExt(orgExt);
  77. }
  78. private String resolveExtGuid(String orgGuid, String fallbackGuid)
  79. {
  80. SysOrganizationExt existing = sysOrganizationExtMapper.selectByOrgGuid(orgGuid);
  81. if (existing != null && StringUtils.isNotEmpty(existing.getGuid()))
  82. {
  83. return existing.getGuid();
  84. }
  85. return StringUtils.isNotEmpty(fallbackGuid) ? fallbackGuid : IdUtils.fastSimpleUUID();
  86. }
  87. /**
  88. * 新增水利机构及其基础信息、扩展信息(同一事务)
  89. *
  90. * @param dept 机构(sys_dept)
  91. * @param ext 机构扩展信息(att_org_ext)
  92. * @return 新机构 deptId(= att_org_base.guid)
  93. */
  94. @Transactional
  95. public String insertOrgWithExt(SysDept dept, AttOrgExt ext, String orgCode)
  96. {
  97. if (!deptService.checkDeptNameUnique(dept))
  98. {
  99. throw new ServiceException("新增机构'" + dept.getDeptName() + "'失败,机构名称已存在");
  100. }
  101. // 上级机构 = 扩展表里的上级单位(proOrgGuid),也是 sys_dept 的 parent_id
  102. String parentGuid = ext.getProOrgGuid();
  103. // 1) 先插入 sys_dept(框架 insertDept 内部会重新生成 deptId)
  104. dept.setParentId(parentGuid);
  105. dept.setOrgType(OrgTypeConst.WATER);
  106. dept.setOrgCheckType(OrgTypeConst.ORG_CHECK_WATER);
  107. dept.setType(ext.getOrgType());
  108. dept.setCreateBy(SecurityUtils.getUserId());
  109. deptService.insertDept(dept);
  110. // 以 sys_dept 实际保存的 deptId 作为三表统一主键/外键
  111. String guid = dept.getDeptId();
  112. // 2) att_org_base 机构基础信息
  113. AttOrgBase base = new AttOrgBase();
  114. base.setGuid(guid);
  115. base.setOrgCode(orgCode);
  116. base.setPguid(parentGuid);
  117. base.setOrgName(dept.getDeptName());
  118. base.setOrgLevel(ext.getOrgLevel());
  119. base.setOrgType(ext.getOrgType());
  120. base.setStatus(dept.getStatus());
  121. base.setModifier(SecurityUtils.getUserId());
  122. attOrgBaseService.insertAttOrgBase(base);
  123. // 3) att_org_ext 机构扩展信息,从 dept 同步冗余字段
  124. String extGuid = IdUtils.fastSimpleUUID();
  125. ext.setGuid(extGuid);
  126. ext.setOrgGuid(guid);
  127. ext.setWiunName(dept.getDeptName());
  128. attOrgExtService.insertAttOrgExt(ext);
  129. // 4) sys_organization_ext 统一扩展表(与 sys_dept 一一对应)
  130. insertUnifiedExt(extGuid, guid, ext);
  131. return guid;
  132. }
  133. /**
  134. * 修改水利机构及其基础信息、扩展信息(同一事务)
  135. * 同时更新 att_org_base,使机构代码(org_code)等基础信息在编辑时一并落库。
  136. *
  137. * @param dept 机构(sys_dept),deptId 必须已存在
  138. * @param ext 机构扩展信息(att_org_ext)
  139. * @param orgCode 机构代码(att_org_base.org_code,前端填写)
  140. */
  141. @Transactional
  142. public void updateOrgWithExt(SysDept dept, AttOrgExt ext, String orgCode)
  143. {
  144. // 上级机构 = 扩展表里的上级单位(proOrgGuid),也是 sys_dept 的 parent_id
  145. String parentGuid = ext.getProOrgGuid();
  146. // 1) att_org_base 机构基础信息(机构代码由前端填写)
  147. AttOrgBase base = new AttOrgBase();
  148. base.setGuid(dept.getDeptId());
  149. base.setOrgCode(orgCode);
  150. base.setPguid(parentGuid);
  151. base.setOrgName(dept.getDeptName());
  152. base.setOrgLevel(ext.getOrgLevel());
  153. base.setOrgType(ext.getOrgType());
  154. base.setStatus(dept.getStatus());
  155. base.setModifier(SecurityUtils.getUserId());
  156. attOrgBaseService.updateAttOrgBase(base);
  157. // 2) sys_dept 部门,parent_id = 上级单位
  158. dept.setParentId(parentGuid);
  159. dept.setOrgType(OrgTypeConst.WATER);
  160. dept.setOrgCheckType(OrgTypeConst.ORG_CHECK_WATER);
  161. dept.setType(ext.getOrgType());
  162. dept.setUpdateBy(SecurityUtils.getUserId());
  163. deptService.updateDept(dept);
  164. // 3) att_org_ext 机构扩展信息,从 dept 同步冗余字段保证数据一致
  165. String extGuid = resolveExtGuid(dept.getDeptId(), ext.getGuid());
  166. AttOrgExt oldExt = attOrgExtService.selectAttOrgExtByOrgGuid(dept.getDeptId());
  167. if (oldExt != null){
  168. ext.setGuid(oldExt.getGuid());
  169. ext.setOrgGuid(dept.getDeptId());
  170. ext.setWiunName(dept.getDeptName());
  171. attOrgExtService.updateAttOrgExt(ext);
  172. } else {
  173. ext.setGuid(extGuid);
  174. ext.setOrgGuid(dept.getDeptId());
  175. ext.setWiunName(dept.getDeptName());
  176. attOrgExtService.insertAttOrgExt(ext);
  177. }
  178. // 4) 同步统一扩展表 sys_organization_ext
  179. upsertUnifiedExt(dept.getDeptId(), ext);
  180. }
  181. /**
  182. * 删除水利机构:三表全删,任一失败即回滚
  183. *
  184. * @param extGuid att_org_ext 主键 GUID
  185. */
  186. @Transactional
  187. public void deleteOrgWithExt(String extGuid)
  188. {
  189. AttOrgExt ext = attOrgExtService.selectAttOrgExtByGuid(extGuid);
  190. if (ext == null) return;
  191. String orgGuid = ext.getOrgGuid();
  192. // 1) 删除扩展表 att_org_ext
  193. attOrgExtService.deleteAttOrgExtByGuid(extGuid);
  194. // 2) 删除基础信息表 att_org_base
  195. attOrgBaseService.deleteAttOrgBaseByGuid(orgGuid);
  196. // 4) 删除统一扩展表 sys_organization_ext
  197. sysOrganizationExtMapper.deleteByOrgGuid(orgGuid);
  198. // 3) 删除机构表 sys_dept(软删除)
  199. deptService.deleteDeptById(orgGuid);
  200. }
  201. }