# ServiceRequestCenterCommentLogController.java

    package cloud.wconnect.it.controller.servicerequestcenter;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.SessionContext;
    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.exception.ForbiddenException;
    import cloud.wconnect.commonlib.pojo.dto.ListPagedDTO;
    import cloud.wconnect.commonlib.pojo.query.PageSizeQuery;
    import cloud.wconnect.it.cons.servicerequestcenter.ServiceRequestCenterAddCommentCons;
    import cloud.wconnect.it.entity.servicerequestcenter.ServiceRequestCenter;
    import cloud.wconnect.it.pojo.dto.servicerequestcenter.SaveServiceRequestCenterCommentDTO;
    import cloud.wconnect.it.pojo.dto.servicerequestcenter.ServiceRequestCenterCommentDTO;
    import cloud.wconnect.it.pojo.dto.servicerequestcenter.ServiceRequestCenterLogDTO;
    import cloud.wconnect.it.service.servicerequestcenter.IServiceRequestCenterCheckService;
    import cloud.wconnect.it.service.servicerequestcenter.IServiceRequestCenterCommentService;
    import cloud.wconnect.it.service.servicerequestcenter.IServiceRequestCenterLogService;
    import jakarta.validation.Valid;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;

    import java.util.Collections;
    import java.util.Map;

    /**
     * <p>
     *  前端控制器
     * </p>
     *
     * @author yyt
     * @since 2025-12-16
     */
    @RestController
    @RequestMapping("/dashboard/service-request-center")
    public class ServiceRequestCenterCommentLogController {

        @Autowired
        private IServiceRequestCenterCommentService serviceRequestCenterCommentService;
        @Autowired
        private IServiceRequestCenterLogService serviceRequestCenterLogService;
        @Autowired
        private IServiceRequestCenterCheckService serviceRequestCenterCheckService;
        @Autowired
        private SessionContext sessionContext;

        /**
         * 1670 - 查看 request 备注
         *
         * @param id    request id
         * @param query 分页查询条件
         * @return 备注
         * @author yyt
         * @date 2025/12/17
         **/
        @ApiId(1670)
        @GetMapping("/{id:\\d+}/comment")
        public ListPagedDTO<ServiceRequestCenterCommentDTO> listComment(@PathVariable int id, @Valid PageSizeQuery query) {
            ServiceRequestCenter request = serviceRequestCenterCheckService.checkExist(id);
            serviceRequestCenterCheckService.checkProperty(sessionContext.getTenantSymbolBO().getId(), request, sessionContext.getPropertyIds());

            return serviceRequestCenterCommentService.listComment(sessionContext.getTenantSymbolBO().getId(), id, query);
        }

        /**
         * 1671 - 查看 request 操作 log
         *
         * @param id    request id
         * @param query 分页查询条件
         * @return log
         * @author yyt
         * @date 2025/12/17
         **/
        @ApiId(1671)
        @GetMapping("/{id:\\d+}/log")
        public ListPagedDTO<ServiceRequestCenterLogDTO> listLog(@PathVariable int id, @Valid PageSizeQuery query) {
            ServiceRequestCenter request = serviceRequestCenterCheckService.checkExist(id);
            Integer tenantId = sessionContext.getTenantSymbolBO().getId();
            serviceRequestCenterCheckService.checkProperty(tenantId, request, sessionContext.getPropertyIds());

            return serviceRequestCenterLogService.listLog(tenantId, id, query);
        }

        /**
         * 1672 - request 填写备注
         *
         * @param property       资产标识
         * @param id             request id
         * @param saveCommentDTO 备注信息
         * @return {}
         * @author yyt
         * @date 2025/12/17
         **/
        @ApiId(1672)
        @PostMapping("/{id:\\d+}/comment")
        public Map<String, Object> saveComment(String property, @PathVariable int id, @Valid @RequestBody SaveServiceRequestCenterCommentDTO saveCommentDTO) {
            int userId = sessionContext.getLoginUserId();
            ServiceRequestCenter request = serviceRequestCenterCheckService.checkExist(id);
            serviceRequestCenterCheckService.checkProperty(sessionContext.getTenantSymbolBO().getId(), request, sessionContext.getPropertyIds());

            Integer tenantId = sessionContext.getTenantSymbolBO().getId();
            if (saveCommentDTO.getType() == ServiceRequestCenterAddCommentCons.USER_ADD_COMMENT) {
                // 只要创建者可以填写备注
                if (request.getCreatedBy() != userId) {
                    throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                            BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(), "the request is not created by you");
                }

                serviceRequestCenterCommentService.saveCommentForMyRequest(tenantId, request, saveCommentDTO);
            } else {
                // 未分配责任人时，只有管理员可以添加备注，且添加备注后，自动分配到自己名下
                Integer dutyUser = request.getDutyUser();
                if (dutyUser == BaseConstant.ZERO) {
                    if (!serviceRequestCenterCheckService.checkUserIsManager(sessionContext.getTenantSymbolBO().getId(), userId, request.getType())) {
                        throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                                BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(), "you are not manager");
                    }

                    serviceRequestCenterCommentService.saveCommentForManager(tenantId, request, saveCommentDTO, userId);
                } else {
                    // 已分配责任人时，只有责任人可以添加备注
                    serviceRequestCenterCheckService.checkUserIsDutyUser(dutyUser, userId);

                    serviceRequestCenterCommentService.saveCommentForDutyUser(tenantId, request, saveCommentDTO, userId);
                }
            }

            return Collections.emptyMap();
        }

    }