# ServiceRequestCenterLogServiceImpl.java

    package cloud.wconnect.it.service.servicerequestcenter.impl;

    import cloud.wconnect.commonlib.pojo.dto.ListPagedDTO;
    import cloud.wconnect.commonlib.pojo.query.PageSizeQuery;
    import cloud.wconnect.commonlib.service.UserInternalService;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.commonlib.util.JsonUtils;
    import cloud.wconnect.it.cons.servicerequestcenter.ServiceRequestCenterLogCons;
    import cloud.wconnect.it.entity.servicerequestcenter.ServiceRequestCenterLog;
    import cloud.wconnect.it.mapper.servicerequestcenter.ServiceRequestCenterLogMapper;
    import cloud.wconnect.it.pojo.dto.servicerequestcenter.AssignBasicDTO;
    import cloud.wconnect.it.pojo.dto.servicerequestcenter.ChangeValueBasicDTO;
    import cloud.wconnect.it.pojo.dto.servicerequestcenter.ServiceRequestCenterExpandDTO;
    import cloud.wconnect.it.pojo.dto.servicerequestcenter.ServiceRequestCenterLogDTO;
    import cloud.wconnect.it.service.servicerequestcenter.IServiceRequestCenterLogService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.google.common.collect.Lists;
    import com.google.common.collect.Sets;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    import java.util.stream.Collectors;

    /**
     * <p>
     *  服务实现类
     * </p>
     *
     * @author yyt
     * @since 2025-12-16
     */
    @Service
    @DS("it_manage")
    public class ServiceRequestCenterLogServiceImpl extends ServiceImpl<ServiceRequestCenterLogMapper, ServiceRequestCenterLog> implements IServiceRequestCenterLogService {

        @Autowired
        private UserInternalService userInternalService;
        @Autowired
        private ObjectMapper objectMapper;

        /**
         * 保存 log
         *
         * @param requestId  request id
         * @param type       1.新增，2.分配，3.变更状态，4.变更type，5.reopen
         * @param expand     2类型：{"assign_by"：分配人id }，3、4、5类型：{"oldValue"：1，“newValue”：2}
         * @param userId     操作人
         * @author yyt
         * @date 2025/12/17
         **/
        @Override
        public void save(Integer requestId, int type, String expand, int userId) {
            save(new ServiceRequestCenterLog()
                    .setRequestId(requestId)
                    .setType(type)
                    .setExpand(expand)
                    .setCreatedBy(userId)
                    .setCreatedTime(DateTimeUtils.nowTimestamp()));
        }

        /**
         * 查询 log
         *
         * @param tenantId  租户 id
         * @param requestId request id
         * @param query     查询条件
         * @return log
         * @author yyt
         * @date 2025/12/17
         **/
        @Override
        public ListPagedDTO<ServiceRequestCenterLogDTO> listLog(int tenantId, int requestId, PageSizeQuery query) {
            Page<ServiceRequestCenterLog> page = page(new Page<>(query.getPage(), query.getSize()),
                    new QueryWrapper<ServiceRequestCenterLog>().lambda()
                            .eq(ServiceRequestCenterLog::getRequestId, requestId)
                            .orderByDesc(ServiceRequestCenterLog::getId));
            List<ServiceRequestCenterLog> logs = page.getRecords();
            if (CollectionUtils.isEmpty(logs)) return new ListPagedDTO<>();

            // 提取创建人和分配人
            Set<Integer> userIds = Sets.newHashSetWithExpectedSize(logs.size());
            logs.forEach(log -> {
                userIds.add(log.getCreatedBy());
                if (log.getType() == ServiceRequestCenterLogCons.ASSIGN) {
                    AssignBasicDTO dto = JsonUtils.convertStringToObject(objectMapper, log.getExpand(), AssignBasicDTO.class);
                    userIds.add(dto.getAssignBy());
                }
            });
            Map<Integer, String> userNameById = userInternalService.getNameMapByIds(tenantId, userIds);

            List<Integer> changeValueLogTypes = Lists.newArrayList(ServiceRequestCenterLogCons.CHANGE_STATUS, ServiceRequestCenterLogCons.CHANGE_TYPE,
                    ServiceRequestCenterLogCons.REOPEN);

            return new ListPagedDTO<>(page.getTotal(), logs.stream().map(log -> {
                ServiceRequestCenterLogDTO logDTO = new ServiceRequestCenterLogDTO();
                logDTO.setId(log.getId());
                Integer logType = log.getType();
                logDTO.setType(logType);

                String expand = log.getExpand();
                ServiceRequestCenterExpandDTO expandDTO = new ServiceRequestCenterExpandDTO();
                if (logType == ServiceRequestCenterLogCons.ASSIGN) {
                    expandDTO.setAssignName(userNameById.get(JsonUtils.convertStringToObject(objectMapper, expand, AssignBasicDTO.class).getAssignBy()));
                } else if (changeValueLogTypes.contains(logType)) {
                    ChangeValueBasicDTO changeValueBasicDTO = JsonUtils.convertStringToObject(objectMapper, expand, ChangeValueBasicDTO.class);
                    expandDTO.setOldValue(changeValueBasicDTO.getOldValue());
                    expandDTO.setNewValue(changeValueBasicDTO.getNewValue());
                }
                logDTO.setExpand(expandDTO);

                Integer createdBy = log.getCreatedBy();
                logDTO.setCreatedBy(createdBy);
                logDTO.setCreatedName(userNameById.get(createdBy));
                logDTO.setCreatedTime(log.getCreatedTime());
                return logDTO;
            }).collect(Collectors.toList()));
        }

    }