package com.product.administration.service; import java.util.*; import com.product.core.entity.DataTableEntity; import com.product.util.BaseUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.product.admin.service.CodeService; import com.product.administration.config.CmnConst; import com.product.administration.config.SystemCode; import com.product.administration.service.ide.ITeamService; import com.product.common.lang.StringUtils; import com.product.core.dao.BaseDao; import com.product.core.entity.FieldSetEntity; import com.product.core.exception.BaseException; import com.product.core.service.support.AbstractBaseService; import com.product.core.spring.context.SpringMVCContextHolder; import com.product.core.transfer.Transactional; @Component public class TeamService extends AbstractBaseService implements ITeamService { @Autowired BaseDao baseDao; @Autowired CodeService codeService; /** * 团队树 * * @return */ public DataTableEntity treeTeam() { String sql = "SELECT *,(SELECT GROUP_CONCAT(user_id) FROM product_sys_staffs WHERE team_code =`code`)user_ids FROM product_sys_team_config"; DataTableEntity dt = baseDao.listTable(sql, new Object[]{}); return BaseUtil.dataTableToTreeTable(dt, "code", "parent_code", null); } /** * 详情 * * @return */ public FieldSetEntity findTeam(String uuid) { String sql = "SELECT *,(SELECT GROUP_CONCAT(user_id) FROM product_sys_staffs WHERE team_code =`code`)user_ids FROM product_sys_team_config where uuid =?"; return baseDao.getFieldSetEntityBySQL(sql, new Object[]{uuid}, false); } /** * 团队保存 * * @param fse */ @Transactional public void saveTeam(FieldSetEntity fse) { //获取团队名称 String teanName = fse.getString("name"); FieldSetEntity fseValitation = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_TEAM_CONFIG, "name=?", new Object[]{teanName}, false); if (fseValitation != null) { throw new BaseException(SystemCode.TEAM_SAVE_FAIL_DUPLICATE_NAME.getValue(), SystemCode.TEAM_SAVE_FAIL_DUPLICATE_NAME.getText()); } //是否生成新上级 boolean isNewParent = false; //获取上级编码 String newParentCode = fse.getString("parent_code"); if (newParentCode == null) { newParentCode = ""; } //原编码 String originCode = ""; //判断是否修改 if (!StringUtils.isEmpty(fse.getUUID())) { //查询原数据 FieldSetEntity fseOriginTeam = this.findTeam(fse.getUUID()); //获取原上级编码 String originparentCode = fseOriginTeam.getString("parent_code"); if (originparentCode == null) { originparentCode = ""; } //判断上级是否变化 if (!newParentCode.equals(originparentCode)) { isNewParent = true; originCode = fseOriginTeam.getString("code"); } } else { //新增 isNewParent = true; } //判断是否需要重新生成编码 if (isNewParent) { //生成新编码 String newCode = codeService.createCode(CmnConst.PRODUCT_SYS_TEAM_CONFIG, "code", newParentCode); fse.setValue("code", newCode); //判断是否修改 if (!StringUtils.isEmpty(fse.getUUID())) { //变更员工关联编码 StringBuilder sqlStaff = new StringBuilder(); sqlStaff.append(" update mrbase_sys_staffs "); sqlStaff.append(" set team_code=concat(replace(substring(team_code,1,locate(?,team_code)+length(?)),?,?),substring(team_code,locate(?,team_code)+length(?)+1)) "); sqlStaff.append(" where team_code like ? "); baseDao.executeUpdate(sqlStaff.toString(), new Object[]{originCode, originCode, originCode, newCode, originCode, originCode, originCode + "%"}); //变更子代团队编码 StringBuilder sqlTeam = new StringBuilder(); sqlTeam.append(" update product_sys_team_config "); sqlTeam.append(" set code=concat(replace(substring(code,1,locate(?,code)+length(?)),?,?),substring(code,locate(?,code)+length(?)+1)), "); sqlTeam.append(" parent_code=concat(replace(substring(parent_code,1,locate(?,parent_code)+length(?)),?,?),substring(parent_code,locate(?,parent_code)+length(?)+1)) "); sqlTeam.append(" where code like ? "); baseDao.executeUpdate(sqlStaff.toString(), new Object[]{originCode, originCode, originCode, newCode, originCode, originCode, originCode, originCode, originCode, newCode, originCode, originCode, originCode + "%"}); } } BaseUtil.createCreatorAndCreationTime(fse); baseDao.saveFieldSetEntity(fse); } /** * 删除团队 * * @param code 编码 * @return * @throws BaseException */ @Transactional public boolean deleteTeam(String code) throws BaseException { //子团队验证 DataTableEntity subTeam = baseDao.listTable(CmnConst.PRODUCT_SYS_TEAM_CONFIG, "parent_code like ?", new Object[]{code + "%"}); if (!BaseUtil.dataTableIsEmpty(subTeam)) { throw new BaseException(SystemCode.TEAM_MANAGER_DELETE_FAIL_HAS_SUB_TEAM.getValue(), SystemCode.TEAM_MANAGER_DELETE_FAIL_HAS_SUB_TEAM.getText()); } //员工验证 DataTableEntity staffTeam = baseDao.listTable(CmnConst.PRODUCT_SYS_STAFFS, "team_code like ?", new Object[]{code + "%"}); if (!BaseUtil.dataTableIsEmpty(staffTeam)) { throw new BaseException(SystemCode.TEAM_MANAGER_DELETE_FAIL_HAS_STAFF_REFERED.getValue(), SystemCode.TEAM_MANAGER_DELETE_FAIL_HAS_STAFF_REFERED.getText()); } //删除团队及及下级团队 return baseDao.delete(CmnConst.PRODUCT_SYS_TEAM_CONFIG, "code = ? ", new Object[]{code}); } }