package com.njcn.rdms.module.project.service.product;

import com.njcn.rdms.framework.common.util.json.JsonUtils;
import com.njcn.rdms.framework.security.core.util.SecurityFrameworkUtils;
import com.njcn.rdms.module.project.constant.ObjectActivityConstants;
import com.njcn.rdms.module.project.constant.ObjectRoleConstants;
import com.njcn.rdms.module.project.constant.ProductObjectConstants;
import com.njcn.rdms.module.project.controller.admin.product.vo.member.ProductMemberBatchCreateReqVO;
import com.njcn.rdms.module.project.controller.admin.product.vo.member.ProductMemberBatchInactiveReqVO;
import com.njcn.rdms.module.project.controller.admin.product.vo.member.ProductMemberInactiveReqVO;
import com.njcn.rdms.module.project.controller.admin.product.vo.member.ProductMemberRespVO;
import com.njcn.rdms.module.project.controller.admin.product.vo.member.ProductMemberSaveReqVO;
import com.njcn.rdms.module.project.controller.admin.product.vo.member.ProductMemberUpdateReqVO;
import com.njcn.rdms.module.project.framework.security.annotation.CheckObjectPermission;
import com.njcn.rdms.module.project.dal.dataobject.audit.BizAuditLogDO;
import com.njcn.rdms.module.project.dal.dataobject.member.UserObjectRoleDO;
import com.njcn.rdms.module.project.dal.dataobject.product.ProductDO;
import com.njcn.rdms.module.project.dal.dataobject.status.ObjectStatusModelDO;
import com.njcn.rdms.module.project.dal.mysql.audit.BizAuditLogMapper;
import com.njcn.rdms.module.project.dal.mysql.member.UserObjectRoleMapper;
import com.njcn.rdms.module.project.dal.mysql.product.ProductMapper;
import com.njcn.rdms.module.project.dal.mysql.status.ObjectStatusModelMapper;
import com.njcn.rdms.module.project.enums.ErrorCodeConstants;
import com.njcn.rdms.module.system.api.permission.ObjectPermissionApi;
import com.njcn.rdms.module.system.api.permission.dto.ObjectRoleRespDTO;
import com.njcn.rdms.module.system.api.user.AdminUserApi;
import com.njcn.rdms.module.system.api.user.dto.AdminUserRespDTO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.njcn.rdms.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 产品团队 Service 实现类
 */
@Service
public class ProductMemberServiceImpl implements ProductMemberService {

    @Resource
    private ProductMapper productMapper;
    @Resource
    private UserObjectRoleMapper userObjectRoleMapper;
    @Resource
    private ObjectPermissionApi objectPermissionApi;
    @Resource
    private BizAuditLogMapper bizAuditLogMapper;
    @Resource
    private ObjectStatusModelMapper objectStatusModelMapper;
    @Resource
    private AdminUserApi adminUserApi;

    @Override
    @CheckObjectPermission(objectType = ProductObjectConstants.OBJECT_TYPE, objectId = "#productId", accessible = true)
    public List<ProductMemberRespVO> getProductMemberList(Long productId) {
        ProductDO product = validateProductExists(productId);
        List<UserObjectRoleDO> members = userObjectRoleMapper.selectListByObject(ProductObjectConstants.OBJECT_TYPE, productId);
        Map<Long, ObjectRoleRespDTO> roleMap = getRoleMap(members.stream().map(UserObjectRoleDO::getRoleId).collect(Collectors.toSet()));
        // 过滤掉 visible=0 的角色行（业务自动赋予的角色，如创建者 / 隐式观察者；不在团队列表显示）；
        // visible=null 视同显示，兼容旧数据未回填的场景。ACTIVE / INACTIVE 行一并过滤。
        members = members.stream()
                .filter(m -> {
                    ObjectRoleRespDTO role = roleMap.get(m.getRoleId());
                    return role == null || !Integer.valueOf(0).equals(role.getVisible());
                })
                .toList();
        Map<Long, AdminUserRespDTO> userMap = getUserMap(members.stream().map(UserObjectRoleDO::getUserId).collect(Collectors.toSet()));

        // 拆分 ACTIVE / INACTIVE：
        // - ACTIVE 行按 userId 聚合，同人多角色合并成一行（manager 优先做主），非主角色名放 additionalRoleNames
        // - INACTIVE 行保持独立（历史角色行各自留痕，便于审计）
        List<UserObjectRoleDO> activeRows = new ArrayList<>();
        List<UserObjectRoleDO> inactiveRows = new ArrayList<>();
        for (UserObjectRoleDO m : members) {
            if (Objects.equals(m.getStatus(), ObjectRoleConstants.MEMBER_STATUS_ACTIVE)) {
                activeRows.add(m);
            } else {
                inactiveRows.add(m);
            }
        }
        Map<Long, List<UserObjectRoleDO>> activeByUser = activeRows.stream()
                .collect(Collectors.groupingBy(UserObjectRoleDO::getUserId, LinkedHashMap::new, Collectors.toList()));

        List<ProductMemberRespVO> result = new ArrayList<>();
        activeByUser.forEach((userId, rows) -> {
            UserObjectRoleDO primary = pickPrimaryRow(rows, roleMap);
            List<String> additionalRoleNames = rows.stream()
                    .filter(r -> !Objects.equals(r.getId(), primary.getId()))
                    .map(r -> {
                        ObjectRoleRespDTO role = roleMap.get(r.getRoleId());
                        return role == null ? null : role.getName();
                    })
                    .filter(Objects::nonNull)
                    .sorted()
                    .collect(Collectors.toList());
            result.add(toRespVO(primary, roleMap, userMap, product, additionalRoleNames));
        });
        // INACTIVE 行各自独立成行
        inactiveRows.forEach(m -> result.add(toRespVO(m, roleMap, userMap, product, Collections.emptyList())));
        return result;
    }

    /**
     * 同 userId 多角色时选主角色行：MANAGER_ROLE_CODE 优先，否则按 roleId 升序兜底。
     */
    private UserObjectRoleDO pickPrimaryRow(List<UserObjectRoleDO> rows, Map<Long, ObjectRoleRespDTO> roleMap) {
        return rows.stream()
                .filter(r -> {
                    ObjectRoleRespDTO role = roleMap.get(r.getRoleId());
                    return role != null && Objects.equals(ProductObjectConstants.MANAGER_ROLE_CODE, role.getCode());
                })
                .findFirst()
                .orElseGet(() -> rows.stream()
                        .min(Comparator.comparing(UserObjectRoleDO::getRoleId))
                        .orElseThrow());
    }

    private ProductMemberRespVO toRespVO(UserObjectRoleDO member,
                                         Map<Long, ObjectRoleRespDTO> roleMap,
                                         Map<Long, AdminUserRespDTO> userMap,
                                         ProductDO product,
                                         List<String> additionalRoleNames) {
        ProductMemberRespVO respVO = new ProductMemberRespVO();
        respVO.setId(member.getId());
        respVO.setUserId(member.getUserId());
        AdminUserRespDTO user = userMap.get(member.getUserId());
        respVO.setUserNickname(user == null ? null : user.getNickname());
        respVO.setRoleId(member.getRoleId());
        ObjectRoleRespDTO role = roleMap.get(member.getRoleId());
        respVO.setRoleName(role == null ? null : role.getName());
        respVO.setRoleCode(role == null ? null : role.getCode());
        respVO.setManagerFlag(Objects.equals(member.getUserId(), product.getManagerUserId())
                && Objects.equals(member.getStatus(), ObjectRoleConstants.MEMBER_STATUS_ACTIVE));
        respVO.setStatus(member.getStatus());
        respVO.setJoinedTime(member.getJoinedTime());
        respVO.setLeftTime(member.getLeftTime());
        respVO.setRemark(member.getRemark());
        respVO.setAdditionalRoleNames(additionalRoleNames);
        return respVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CheckObjectPermission(objectType = ProductObjectConstants.OBJECT_TYPE, objectId = "#productId",
            permission = ProductObjectConstants.PERMISSION_UPDATE)
    public Long createProductMember(Long productId, ProductMemberSaveReqVO reqVO) {
        ProductDO product = validateProductEditable(productId);
        ObjectRoleRespDTO targetRole = validateProductRole(reqVO.getRoleId());
        // 多角色支持：按 (user, object, role) 三元组判存在；不带 status 过滤以便 INACTIVE 行复活（避免唯一索引 INSERT 冲突）
        UserObjectRoleDO existingMember = userObjectRoleMapper
                .selectByObjectUserAndRole(ProductObjectConstants.OBJECT_TYPE, productId,
                        reqVO.getUserId(), targetRole.getId());
        if (existingMember != null && Objects.equals(existingMember.getStatus(), ObjectRoleConstants.MEMBER_STATUS_ACTIVE)) {
            throw exception(ErrorCodeConstants.PRODUCT_MEMBER_ALREADY_EXISTS);
        }

        UserObjectRoleDO member;
        UserObjectRoleDO before = null;
        LocalDateTime now = LocalDateTime.now();
        // 物理 INSERT vs INACTIVE 复活的 audit 语义区分：null=新增；INACTIVE 复活=REACTIVATE
        String actionType;
        if (existingMember == null) {
            member = new UserObjectRoleDO();
            member.setUserId(reqVO.getUserId());
            member.setObjectType(ProductObjectConstants.OBJECT_TYPE);
            member.setObjectId(productId);
            member.setRoleId(targetRole.getId());
            member.setStatus(ObjectRoleConstants.MEMBER_STATUS_ACTIVE);
            member.setJoinedTime(now);
            member.setLeftTime(null);
            member.setRemark(normalizeNullableText(reqVO.getRemark()));
            userObjectRoleMapper.insert(member);
            actionType = ObjectActivityConstants.MEMBER_ACTION_ADD;
        } else {
            before = cloneMember(existingMember);
            member = existingMember;
            member.setRoleId(targetRole.getId());
            member.setStatus(ObjectRoleConstants.MEMBER_STATUS_ACTIVE);
            member.setJoinedTime(now);
            member.setLeftTime(null);
            member.setRemark(normalizeNullableText(reqVO.getRemark()));
            userObjectRoleMapper.updateById(member);
            actionType = ObjectActivityConstants.MEMBER_ACTION_REACTIVATE;
        }

        writeMemberAuditLog(member, actionType, before, member, null);
        if (isManagerRole(targetRole)) {
            transferManager(product, member, reqVO.getPreviousManagerUserId(), reqVO.getPreviousManagerRoleId(), null);
        }
        return member.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CheckObjectPermission(objectType = ProductObjectConstants.OBJECT_TYPE, objectId = "#productId",
            permission = ProductObjectConstants.PERMISSION_UPDATE)
    public List<Long> batchCreateProductMembers(Long productId, ProductMemberBatchCreateReqVO reqVO) {
        validateProductEditable(productId);

        List<ProductMemberBatchCreateReqVO.Item> items = reqVO.getMembers();
        // 同请求内 userId 重复一律拒绝（user-level），即使是不同角色也算重复——批量语义就是一人一行
        Set<Long> seenUserIds = new HashSet<>(items.size());
        for (ProductMemberBatchCreateReqVO.Item item : items) {
            if (!seenUserIds.add(item.getUserId())) {
                throw exception(ErrorCodeConstants.PRODUCT_MEMBER_BATCH_USER_DUPLICATE);
            }
        }
        // 批量校验用户存在/启用——产品域单条接口未做此校验（历史负债），批量接口按需求 §4 兜底
        validateMemberUsers(seenUserIds);

        List<Long> memberIds = new ArrayList<>(items.size());
        LocalDateTime now = LocalDateTime.now();
        for (ProductMemberBatchCreateReqVO.Item item : items) {
            ObjectRoleRespDTO targetRole = validateProductRole(item.getRoleId());
            if (isManagerRole(targetRole)) {
                throw exception(ErrorCodeConstants.PRODUCT_MEMBER_BATCH_MANAGER_NOT_ALLOWED);
            }
            // user-level 拒绝：该用户在本产品下有任意 ACTIVE 行（含其他角色），不允许再批量加入
            List<UserObjectRoleDO> activeRows = userObjectRoleMapper
                    .selectActiveListByObjectAndUserId(ProductObjectConstants.OBJECT_TYPE, productId, item.getUserId());
            if (!activeRows.isEmpty()) {
                throw exception(ErrorCodeConstants.PRODUCT_MEMBER_ALREADY_EXISTS);
            }

            // 多角色支持：按 (user, object, role) 三元组判存在；INACTIVE 行复活，避免唯一索引 INSERT 冲突
            UserObjectRoleDO existingMember = userObjectRoleMapper
                    .selectByObjectUserAndRole(ProductObjectConstants.OBJECT_TYPE, productId,
                            item.getUserId(), targetRole.getId());
            UserObjectRoleDO before = existingMember == null ? null : cloneMember(existingMember);
            UserObjectRoleDO member;
            String actionType;
            if (existingMember == null) {
                member = new UserObjectRoleDO();
                member.setUserId(item.getUserId());
                member.setObjectType(ProductObjectConstants.OBJECT_TYPE);
                member.setObjectId(productId);
                member.setRoleId(targetRole.getId());
                member.setStatus(ObjectRoleConstants.MEMBER_STATUS_ACTIVE);
                member.setJoinedTime(now);
                member.setLeftTime(null);
                member.setRemark(normalizeNullableText(item.getRemark()));
                userObjectRoleMapper.insert(member);
                actionType = ObjectActivityConstants.MEMBER_ACTION_ADD;
            } else {
                member = existingMember;
                member.setRoleId(targetRole.getId());
                member.setStatus(ObjectRoleConstants.MEMBER_STATUS_ACTIVE);
                member.setJoinedTime(now);
                member.setLeftTime(null);
                member.setRemark(normalizeNullableText(item.getRemark()));
                userObjectRoleMapper.updateById(member);
                actionType = ObjectActivityConstants.MEMBER_ACTION_REACTIVATE;
            }
            writeMemberAuditLog(member, actionType, before, member, null);
            memberIds.add(member.getId());
        }
        return memberIds;
    }

    private void validateMemberUsers(Set<Long> userIds) {
        try {
            Boolean valid = adminUserApi.validateUserList(new ArrayList<>(userIds)).getCheckedData();
            if (Boolean.TRUE.equals(valid)) {
                return;
            }
        } catch (RuntimeException ex) {
            throw exception(ErrorCodeConstants.PRODUCT_MEMBER_USER_INVALID);
        }
        throw exception(ErrorCodeConstants.PRODUCT_MEMBER_USER_INVALID);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CheckObjectPermission(objectType = ProductObjectConstants.OBJECT_TYPE, objectId = "#productId",
            permission = ProductObjectConstants.PERMISSION_UPDATE)
    public void updateProductMember(Long productId, Long memberId, ProductMemberUpdateReqVO reqVO) {
        ProductDO product = validateProductEditable(productId);
        UserObjectRoleDO member = validateMemberExists(productId, memberId);
        if (!Objects.equals(member.getStatus(), ObjectRoleConstants.MEMBER_STATUS_ACTIVE)) {
            throw exception(ErrorCodeConstants.PRODUCT_MEMBER_NOT_ACTIVE);
        }

        ObjectRoleRespDTO targetRole = validateProductRole(reqVO.getRoleId());
        UserObjectRoleDO before = cloneMember(member);
        member.setRemark(normalizeNullableText(reqVO.getRemark()));

        // 多角色边界：避免 setRoleId 撞唯一索引——若 user 在该 product 已有 (targetRole) 的另一行（含 INACTIVE 历史行），
        // 直接 update 会触发唯一索引 (user_id, object_type, object_id, role_id, deleted) 冲突。业务侧明确报错。
        if (!Objects.equals(member.getRoleId(), targetRole.getId())) {
            UserObjectRoleDO conflict = userObjectRoleMapper.selectByObjectUserAndRole(
                    ProductObjectConstants.OBJECT_TYPE, productId, member.getUserId(), targetRole.getId());
            if (conflict != null && !Objects.equals(conflict.getId(), member.getId())) {
                throw exception(ErrorCodeConstants.PRODUCT_MEMBER_ALREADY_EXISTS);
            }
        }

        if (isManagerRole(targetRole)) {
            member.setRoleId(targetRole.getId());
            userObjectRoleMapper.updateById(member);
            transferManager(product, member, reqVO.getPreviousManagerUserId(),
                    reqVO.getPreviousManagerRoleId(), normalizeNullableText(reqVO.getReason()));
        } else {
            if (Objects.equals(member.getUserId(), product.getManagerUserId())) {
                throw exception(ErrorCodeConstants.PRODUCT_MANAGER_MEMBER_NOT_ALLOW_DOWNGRADE);
            }
            member.setRoleId(targetRole.getId());
            userObjectRoleMapper.updateById(member);
        }

        writeMemberAuditLog(member, ObjectActivityConstants.MEMBER_ACTION_UPDATE,
                before, member, normalizeNullableText(reqVO.getReason()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CheckObjectPermission(objectType = ProductObjectConstants.OBJECT_TYPE, objectId = "#productId",
            permission = ProductObjectConstants.PERMISSION_UPDATE)
    public void inactiveProductMember(Long productId, Long memberId, ProductMemberInactiveReqVO reqVO) {
        ProductDO product = validateProductEditable(productId);
        UserObjectRoleDO member = validateMemberExists(productId, memberId);
        if (!Objects.equals(member.getStatus(), ObjectRoleConstants.MEMBER_STATUS_ACTIVE)) {
            throw exception(ErrorCodeConstants.PRODUCT_MEMBER_NOT_ACTIVE);
        }
        if (Objects.equals(member.getUserId(), product.getManagerUserId())) {
            throw exception(ErrorCodeConstants.PRODUCT_MANAGER_MEMBER_NOT_ALLOW_REMOVE);
        }

        UserObjectRoleDO before = cloneMember(member);
        member.setStatus(ObjectRoleConstants.MEMBER_STATUS_INACTIVE);
        member.setLeftTime(LocalDateTime.now());
        userObjectRoleMapper.updateById(member);

        writeMemberAuditLog(member, ObjectActivityConstants.MEMBER_ACTION_REMOVE, before, member,
                normalizeNullableText(reqVO.getReason()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CheckObjectPermission(objectType = ProductObjectConstants.OBJECT_TYPE, objectId = "#productId",
            permission = ProductObjectConstants.PERMISSION_UPDATE)
    public void batchInactiveProductMembers(Long productId, ProductMemberBatchInactiveReqVO reqVO) {
        ProductDO product = validateProductEditable(productId);

        List<Long> memberIds = reqVO.getMemberIds();
        // 同请求 memberId 重复一律拒绝，避免对同一行重复 update 造成 audit 重复落库
        Set<Long> seen = new HashSet<>(memberIds.size());
        for (Long memberId : memberIds) {
            if (!seen.add(memberId)) {
                throw exception(ErrorCodeConstants.PRODUCT_MEMBER_BATCH_INACTIVE_MEMBER_DUPLICATE);
            }
        }

        String reason = normalizeNullableText(reqVO.getReason());
        LocalDateTime now = LocalDateTime.now();
        // 收集批次摘要，循环外聚合写一条审计日志（需求 §6 口径）
        List<UserObjectRoleDO> removed = new ArrayList<>(memberIds.size());
        for (Long memberId : memberIds) {
            UserObjectRoleDO member = validateMemberExists(productId, memberId);
            if (!Objects.equals(member.getStatus(), ObjectRoleConstants.MEMBER_STATUS_ACTIVE)) {
                throw exception(ErrorCodeConstants.PRODUCT_MEMBER_NOT_ACTIVE);
            }
            // 经理判定沿用单条 inactive 口径：按 product.managerUserId 比对，不引入 role.code 判定避免分叉
            if (Objects.equals(member.getUserId(), product.getManagerUserId())) {
                throw exception(ErrorCodeConstants.PRODUCT_MANAGER_MEMBER_NOT_ALLOW_REMOVE);
            }

            member.setStatus(ObjectRoleConstants.MEMBER_STATUS_INACTIVE);
            member.setLeftTime(now);
            userObjectRoleMapper.updateById(member);
            removed.add(member);
        }

        writeBatchInactiveAuditLog(productId, removed, reason);
    }

    /**
     * 批量移出场景的聚合审计：写一条对象维度日志（bizType=product, bizId=productId），
     * fieldChanges 体现批量摘要 batchCount + members 数组，与 writeManagerChangeAuditLog 风格对齐。
     * 单条 before/after 字段差异在聚合视图下故意丢弃，由读侧按需展开。
     */
    private void writeBatchInactiveAuditLog(Long productId, List<UserObjectRoleDO> members, String reason) {
        if (members.isEmpty()) {
            return;
        }
        BizAuditLogDO auditLog = new BizAuditLogDO();
        auditLog.setBizType(ProductObjectConstants.OBJECT_TYPE);
        auditLog.setBizId(productId);
        auditLog.setActionType(ObjectActivityConstants.MEMBER_ACTION_REMOVE);
        auditLog.setFieldChanges(buildBatchInactiveFieldChanges(members));
        auditLog.setReason(reason);
        auditLog.setOperatorUserId(SecurityFrameworkUtils.getLoginUserId());
        auditLog.setOperatorName(defaultText(SecurityFrameworkUtils.getLoginUserNickname()));
        bizAuditLogMapper.insert(auditLog);
    }

    private String buildBatchInactiveFieldChanges(List<UserObjectRoleDO> members) {
        List<Map<String, Object>> memberSummaries = new ArrayList<>(members.size());
        for (UserObjectRoleDO member : members) {
            Map<String, Object> summary = new LinkedHashMap<>();
            summary.put("memberId", member.getId());
            summary.put("userId", member.getUserId());
            summary.put("roleId", member.getRoleId());
            memberSummaries.add(summary);
        }
        Map<String, Object> root = new LinkedHashMap<>();
        root.put("batchCount", members.size());
        root.put("members", memberSummaries);
        return JsonUtils.toJsonString(root);
    }

    private ProductDO validateProductExists(Long productId) {
        if (productId == null) {
            throw exception(ErrorCodeConstants.PRODUCT_NOT_EXISTS);
        }
        ProductDO product = productMapper.selectById(productId);
        if (product == null) {
            throw exception(ErrorCodeConstants.PRODUCT_NOT_EXISTS);
        }
        return product;
    }

    private ProductDO validateProductEditable(Long productId) {
        ProductDO product = validateProductExists(productId);
        ObjectStatusModelDO statusModel = objectStatusModelMapper
                .selectByObjectTypeAndStatusCodeEnabled(ProductObjectConstants.OBJECT_TYPE, product.getStatusCode());
        if (statusModel == null) {
            throw exception(ErrorCodeConstants.PRODUCT_STATUS_MODEL_NOT_EXISTS_OR_DISABLED);
        }
        if (!Boolean.TRUE.equals(statusModel.getAllowEdit())) {
            throw exception(ErrorCodeConstants.PRODUCT_STATUS_NOT_ALLOW_EDIT);
        }
        return product;
    }

    private UserObjectRoleDO validateMemberExists(Long productId, Long memberId) {
        UserObjectRoleDO member = userObjectRoleMapper.selectByIdAndObject(memberId, ProductObjectConstants.OBJECT_TYPE, productId);
        if (member == null) {
            throw exception(ErrorCodeConstants.PRODUCT_MEMBER_NOT_EXISTS);
        }
        return member;
    }

    private ObjectRoleRespDTO validateProductRole(Long roleId) {
        ObjectRoleRespDTO role = objectPermissionApi
                .getObjectRoleById(roleId, ObjectRoleConstants.ROLE_SCOPE_OBJECT, ProductObjectConstants.OBJECT_TYPE)
                .getCheckedData();
        if (role == null) {
            throw exception(ErrorCodeConstants.PRODUCT_MEMBER_ROLE_INVALID);
        }
        return role;
    }

    private void transferManager(ProductDO product,
                                 UserObjectRoleDO targetManagerMember,
                                 Long previousManagerUserId,
                                 Long previousManagerRoleId,
                                 String reason) {
        Long currentManagerUserId = product.getManagerUserId();
        Long targetManagerUserId = targetManagerMember.getUserId();
        if (Objects.equals(currentManagerUserId, targetManagerUserId)) {
            product.setManagerUserId(targetManagerUserId);
            productMapper.updateById(product);
            return;
        }

        ObjectRoleRespDTO previousManagerRole = validatePreviousManagerTransfer(currentManagerUserId,
                previousManagerUserId, previousManagerRoleId);
        transferPreviousManager(product.getId(), previousManagerUserId, previousManagerRole.getId(), reason);

        product.setManagerUserId(targetManagerUserId);
        productMapper.updateById(product);
        writeManagerChangeAuditLog(product.getId(), currentManagerUserId, targetManagerUserId, reason);
    }

    private ObjectRoleRespDTO validatePreviousManagerTransfer(Long currentManagerUserId,
                                                              Long previousManagerUserId,
                                                              Long previousManagerRoleId) {
        if (currentManagerUserId == null
                || previousManagerUserId == null
                || previousManagerRoleId == null) {
            throw exception(ErrorCodeConstants.PRODUCT_MANAGER_TRANSFER_INFO_REQUIRED);
        }
        if (!Objects.equals(currentManagerUserId, previousManagerUserId)) {
            throw exception(ErrorCodeConstants.PRODUCT_MANAGER_TRANSFER_SOURCE_INVALID);
        }
        ObjectRoleRespDTO previousManagerRole = validateProductRole(previousManagerRoleId);
        if (isManagerRole(previousManagerRole)) {
            throw exception(ErrorCodeConstants.PRODUCT_MANAGER_TRANSFER_ROLE_INVALID);
        }
        return previousManagerRole;
    }

    private void transferPreviousManager(Long productId, Long previousManagerUserId, Long previousManagerRoleId, String reason) {
        // 多角色边界校验：若 user 在 (product, previousManagerRoleId) 已有任意行（ACTIVE 或 INACTIVE 历史行），
        // 后续 update / INSERT 都会触发唯一索引 (user_id, object_type, object_id, role_id, deleted) 冲突 ——
        // 一刀切抛业务异常，让用户先去人工清理历史行。selectByObjectUserAndRole 不带 status 过滤。
        UserObjectRoleDO targetRoleExisting = userObjectRoleMapper.selectByObjectUserAndRole(
                ProductObjectConstants.OBJECT_TYPE, productId, previousManagerUserId, previousManagerRoleId);
        if (targetRoleExisting != null) {
            throw exception(ErrorCodeConstants.PRODUCT_MANAGER_TRANSFER_TARGET_ROLE_DUPLICATE, previousManagerRoleId);
        }

        // 多角色支持：按 (user, object, manager_role_id) 三元组定位 manager 行；user 的 creator/specialist 等其他角色行不动
        Long productManagerRoleId = resolveProductManagerRoleId();
        UserObjectRoleDO existingMember = userObjectRoleMapper
                .selectActiveByObjectUserAndRole(ProductObjectConstants.OBJECT_TYPE, productId,
                        previousManagerUserId, productManagerRoleId);
        UserObjectRoleDO before = existingMember == null ? null : cloneMember(existingMember);
        LocalDateTime now = LocalDateTime.now();
        UserObjectRoleDO member;
        String actionType;
        if (existingMember == null) {
            // user 当前没有 manager 角色 ACTIVE 行 —— 兼容老逻辑：插入 previousManagerRoleId 行
            member = new UserObjectRoleDO();
            member.setUserId(previousManagerUserId);
            member.setObjectType(ProductObjectConstants.OBJECT_TYPE);
            member.setObjectId(productId);
            member.setRoleId(previousManagerRoleId);
            member.setStatus(ObjectRoleConstants.MEMBER_STATUS_ACTIVE);
            member.setJoinedTime(now);
            member.setLeftTime(null);
            member.setRemark(null);
            userObjectRoleMapper.insert(member);
            actionType = ObjectActivityConstants.MEMBER_ACTION_ADD;
        } else {
            // existingMember 是 manager 行 ACTIVE，update 改 role_id 成 previousManagerRoleId（"降级"该行）
            member = existingMember;
            member.setRoleId(previousManagerRoleId);
            member.setStatus(ObjectRoleConstants.MEMBER_STATUS_ACTIVE);
            member.setLeftTime(null);
            actionType = ObjectActivityConstants.MEMBER_ACTION_UPDATE;
            userObjectRoleMapper.updateById(member);
        }
        writeMemberAuditLog(member, actionType, before, member, reason);
    }

    private Long resolveProductManagerRoleId() {
        ObjectRoleRespDTO role = objectPermissionApi
                .getObjectRoleByCode(ProductObjectConstants.MANAGER_ROLE_CODE,
                        ObjectRoleConstants.ROLE_SCOPE_OBJECT,
                        ProductObjectConstants.OBJECT_TYPE)
                .getCheckedData();
        if (role == null || role.getId() == null) {
            throw exception(ErrorCodeConstants.PRODUCT_INTERNAL_ROLE_NOT_CONFIGURED,
                    ProductObjectConstants.MANAGER_ROLE_CODE);
        }
        return role.getId();
    }

    private boolean isManagerRole(ObjectRoleRespDTO role) {
        return Objects.equals(ProductObjectConstants.MANAGER_ROLE_CODE, role.getCode());
    }

    private Map<Long, ObjectRoleRespDTO> getRoleMap(Set<Long> roleIds) {
        if (roleIds.isEmpty()) {
            return Collections.emptyMap();
        }
        List<ObjectRoleRespDTO> roles = objectPermissionApi
                .getObjectRoleList(roleIds, ObjectRoleConstants.ROLE_SCOPE_OBJECT, ProductObjectConstants.OBJECT_TYPE)
                .getCheckedData();
        if (roles == null || roles.isEmpty()) {
            return Collections.emptyMap();
        }
        return roles.stream().collect(Collectors.toMap(ObjectRoleRespDTO::getId, Function.identity()));
    }

    private Map<Long, AdminUserRespDTO> getUserMap(Set<Long> userIds) {
        if (userIds.isEmpty()) {
            return Collections.emptyMap();
        }
        return adminUserApi.getUserMap(userIds);
    }

    private void writeMemberAuditLog(UserObjectRoleDO member,
                                     String actionType,
                                     UserObjectRoleDO before,
                                     UserObjectRoleDO after,
                                     String reason) {
        BizAuditLogDO auditLog = new BizAuditLogDO();
        auditLog.setBizType(ObjectActivityConstants.MEMBER_BIZ_TYPE);
        auditLog.setBizId(member.getId());
        auditLog.setActionType(actionType);
        auditLog.setFieldChanges(buildMemberFieldChanges(before, after));
        auditLog.setReason(reason);
        auditLog.setOperatorUserId(SecurityFrameworkUtils.getLoginUserId());
        auditLog.setOperatorName(defaultText(SecurityFrameworkUtils.getLoginUserNickname()));
        bizAuditLogMapper.insert(auditLog);
    }

    private void writeManagerChangeAuditLog(Long productId, Long beforeManagerUserId, Long afterManagerUserId, String reason) {
        if (Objects.equals(beforeManagerUserId, afterManagerUserId)) {
            return;
        }
        BizAuditLogDO auditLog = new BizAuditLogDO();
        auditLog.setBizType(ProductObjectConstants.OBJECT_TYPE);
        auditLog.setBizId(productId);
        auditLog.setActionType(ObjectActivityConstants.PRODUCT_ACTION_CHANGE_MANAGER);
        auditLog.setFieldChanges(buildManagerFieldChanges(beforeManagerUserId, afterManagerUserId));
        auditLog.setReason(reason);
        auditLog.setOperatorUserId(SecurityFrameworkUtils.getLoginUserId());
        auditLog.setOperatorName(defaultText(SecurityFrameworkUtils.getLoginUserNickname()));
        bizAuditLogMapper.insert(auditLog);
    }

    private String buildMemberFieldChanges(UserObjectRoleDO before, UserObjectRoleDO after) {
        Map<String, Object> fieldChanges = new LinkedHashMap<>();
        appendFieldChange(fieldChanges, "userId", valueOf(before, UserObjectRoleDO::getUserId),
                valueOf(after, UserObjectRoleDO::getUserId));
        appendFieldChange(fieldChanges, "roleId", valueOf(before, UserObjectRoleDO::getRoleId),
                valueOf(after, UserObjectRoleDO::getRoleId));
        appendFieldChange(fieldChanges, "status", valueOf(before, UserObjectRoleDO::getStatus),
                valueOf(after, UserObjectRoleDO::getStatus));
        appendFieldChange(fieldChanges, "joinedTime", valueOf(before, UserObjectRoleDO::getJoinedTime),
                valueOf(after, UserObjectRoleDO::getJoinedTime));
        appendFieldChange(fieldChanges, "leftTime", valueOf(before, UserObjectRoleDO::getLeftTime),
                valueOf(after, UserObjectRoleDO::getLeftTime));
        appendFieldChange(fieldChanges, "remark", valueOf(before, UserObjectRoleDO::getRemark),
                valueOf(after, UserObjectRoleDO::getRemark));
        return fieldChanges.isEmpty() ? null : JsonUtils.toJsonString(fieldChanges);
    }

    private String buildManagerFieldChanges(Long beforeManagerUserId, Long afterManagerUserId) {
        Map<String, Object> fieldChanges = new LinkedHashMap<>();
        appendFieldChange(fieldChanges, "managerUserId", beforeManagerUserId, afterManagerUserId);
        return JsonUtils.toJsonString(fieldChanges);
    }

    private UserObjectRoleDO cloneMember(UserObjectRoleDO source) {
        UserObjectRoleDO clone = new UserObjectRoleDO();
        clone.setId(source.getId());
        clone.setUserId(source.getUserId());
        clone.setObjectType(source.getObjectType());
        clone.setObjectId(source.getObjectId());
        clone.setRoleId(source.getRoleId());
        clone.setStatus(source.getStatus());
        clone.setJoinedTime(source.getJoinedTime());
        clone.setLeftTime(source.getLeftTime());
        clone.setRemark(source.getRemark());
        return clone;
    }

    private <T> T valueOf(UserObjectRoleDO member, Function<UserObjectRoleDO, T> getter) {
        return member == null ? null : getter.apply(member);
    }

    private void appendFieldChange(Map<String, Object> fieldChanges, String fieldName, Object before, Object after) {
        if (Objects.equals(before, after)) {
            return;
        }
        Map<String, Object> value = new LinkedHashMap<>();
        value.put("before", before);
        value.put("after", after);
        fieldChanges.put(fieldName, value);
    }

    private String normalizeNullableText(String value) {
        if (!StringUtils.hasText(value)) {
            return null;
        }
        return value.trim();
    }

    private String defaultText(String value) {
        return StringUtils.hasText(value) ? value : "";
    }

}
