package com.ruoyi.web.modules.order.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.order.constant.ReviewConstants;
import com.ruoyi.web.modules.order.domain.BizGoodsReview;
import com.ruoyi.web.modules.order.dto.SellerReviewQuery;
import com.ruoyi.web.modules.order.dto.SellerReviewReplyDTO;
import com.ruoyi.web.modules.order.mapper.BizGoodsReviewMapper;
import com.ruoyi.web.modules.order.service.ISellerReviewService;
import com.ruoyi.web.modules.order.support.OrderAppSupport;
import com.ruoyi.web.modules.order.vo.SellerReviewDetailVO;
import com.ruoyi.web.modules.order.vo.SellerReviewListRowVO;

@Service
public class SellerReviewServiceImpl implements ISellerReviewService
{
    @Autowired
    private BizGoodsReviewMapper reviewMapper;

    @Autowired
    private OrderAppSupport orderAppSupport;

    @Override
    public List<SellerReviewListRowVO> selectSellerList(Long shopId, SellerReviewQuery query)
    {
        List<SellerReviewListRowVO> rows = reviewMapper.selectSellerList(shopId, query);
        for (SellerReviewListRowVO row : rows)
        {
            enrichListRow(row);
        }
        return rows;
    }

    @Override
    public SellerReviewDetailVO getSellerDetail(Long shopId, Long reviewId)
    {
        SellerReviewDetailVO detail = reviewMapper.selectSellerDetail(shopId, reviewId);
        if (detail == null)
        {
            throw new ServiceException(ReviewConstants.MSG_NOT_FOUND);
        }
        detail.setReviewPics(orderAppSupport.splitPics(detail.getReviewPicsRaw()));
        detail.setReplyStatus(resolveReplyStatus(detail.getReplyContent()));
        return detail;
    }

    @Override
    public void reply(Long shopId, Long reviewId, SellerReviewReplyDTO dto, String operator)
    {
        BizGoodsReview review = requireAccessibleReview(shopId, reviewId);
        String replyContent = dto == null ? null : StringUtils.trim(dto.getReplyContent());
        if (StringUtils.isEmpty(replyContent))
        {
            throw new ServiceException(ReviewConstants.MSG_REPLY_CONTENT_REQUIRED);
        }
        if (replyContent.length() > ReviewConstants.MAX_REPLY_LENGTH)
        {
            replyContent = replyContent.substring(0, ReviewConstants.MAX_REPLY_LENGTH);
        }
        Date now = new Date();
        BizGoodsReview update = new BizGoodsReview();
        update.setReviewId(review.getReviewId());
        update.setReplyContent(replyContent);
        update.setReplyBy(operator);
        update.setReplyTime(now);
        update.setUpdateTime(now);
        reviewMapper.updateReply(update);
    }

    @Override
    public void deleteReview(Long shopId, Long reviewId, String operator)
    {
        requireAccessibleReview(shopId, reviewId);
        int rows = reviewMapper.updateDelFlag(reviewId, shopId);
        if (rows <= 0)
        {
            throw new ServiceException(ReviewConstants.MSG_NOT_FOUND);
        }
    }

    private BizGoodsReview requireAccessibleReview(Long shopId, Long reviewId)
    {
        BizGoodsReview review = reviewMapper.selectById(reviewId);
        if (review == null || !shopId.equals(review.getShopId())
                || !ReviewConstants.DEL_NORMAL.equals(review.getDelFlag()))
        {
            throw new ServiceException(ReviewConstants.MSG_NOT_FOUND);
        }
        return review;
    }

    private void enrichListRow(SellerReviewListRowVO row)
    {
        row.setReviewPics(orderAppSupport.splitPics(row.getReviewPicsRaw()));
        row.setReplyStatus(resolveReplyStatus(row.getReplyContent()));
    }

    private String resolveReplyStatus(String replyContent)
    {
        if (StringUtils.isEmpty(replyContent))
        {
            return ReviewConstants.REPLY_STATUS_UNREPLIED;
        }
        return ReviewConstants.REPLY_STATUS_REPLIED;
    }
}
