package com.xindazhou.business.service.community.report;

import com.xindazhou.business.controller.admin.community.report.vo.ReportHandleReqVO;
import com.xindazhou.business.controller.admin.community.report.vo.ReportPageReqVO;
import com.xindazhou.business.controller.admin.community.report.vo.ReportRespVO;
import com.xindazhou.business.convert.community.report.ReportConvert;
import com.xindazhou.business.dal.community.dataobject.report.ReportDO;
import com.xindazhou.business.dal.community.mysql.report.ReportMapper;
import com.xindazhou.framework.common.exception.util.ServiceExceptionUtil;
import com.xindazhou.framework.common.pojo.PageResult;
import com.xindazhou.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.xindazhou.framework.security.core.util.SecurityFrameworkUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;

import static com.xindazhou.framework.common.exception.enums.GlobalErrorCodeConstants.NOT_FOUND;

/**
 * 举报 Service 实现类
 *
 * @author xindazhou
 */
@Service("communityReportServiceImpl")
@Validated
@Slf4j
public class ReportServiceImpl implements ReportService {

    @Resource
    private ReportMapper reportMapper;

    @Override
    public ReportRespVO getReport(String reportId) {
        ReportDO report = reportMapper.selectOne(new LambdaQueryWrapperX<ReportDO>()
                .eq(ReportDO::getReportId, reportId));
        if (report == null) {
            throw ServiceExceptionUtil.exception(NOT_FOUND, "举报不存在");
        }
        return ReportConvert.INSTANCE.convert(report);
    }

    @Override
    public PageResult<ReportRespVO> getReportPage(ReportPageReqVO pageReqVO) {
        LambdaQueryWrapperX<ReportDO> queryWrapper = new LambdaQueryWrapperX<ReportDO>()
                .eqIfPresent(ReportDO::getReportedPersonId, pageReqVO.getReportedPersonId())
                .likeIfPresent(ReportDO::getReportedPersonName, pageReqVO.getReportedPerson())
                .likeIfPresent(ReportDO::getHandler, pageReqVO.getHandler())
                .eqIfPresent(ReportDO::getCategory, pageReqVO.getCategory())
                .eqIfPresent(ReportDO::getStatus, pageReqVO.getStatus())
                .betweenIfPresent(ReportDO::getReportTime, pageReqVO.getReportTimeStart(), pageReqVO.getReportTimeEnd())
                .betweenIfPresent(ReportDO::getHandleTime, pageReqVO.getHandleTimeStart(), pageReqVO.getHandleTimeEnd())
                .orderByDesc(ReportDO::getReportTime);
        PageResult<ReportDO> pageResult = reportMapper.selectPage(pageReqVO, queryWrapper);
        return ReportConvert.INSTANCE.convertPage(pageResult);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void handleReport(String reportId, ReportHandleReqVO handleReqVO) {
        ReportDO report = reportMapper.selectOne(new LambdaQueryWrapperX<ReportDO>()
                .eq(ReportDO::getReportId, reportId));
        if (report == null) {
            throw ServiceExceptionUtil.exception(NOT_FOUND, "举报不存在");
        }
        
        if (report.getStatus() == 1) {
            throw ServiceExceptionUtil.exception(NOT_FOUND, "举报已处理");
        }
        
        // 更新举报
        ReportDO updateObj = new ReportDO();
        updateObj.setId(report.getId());
        updateObj.setStatus(1); // 已处理
        updateObj.setHandleResult(handleReqVO.getHandleResult());
        updateObj.setHandleRemark(handleReqVO.getHandleRemark());
        // TODO: 获取处理人用户名，暂时使用用户ID
        updateObj.setHandler("管理员" + SecurityFrameworkUtils.getLoginUserId());
        updateObj.setHandleTime(LocalDateTime.now());
        reportMapper.updateById(updateObj);
    }

}

