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 org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.account.util.MemberMobileUtils;
import com.ruoyi.web.modules.order.constant.OrderAppConstants;
import com.ruoyi.web.modules.order.constant.OrderConstants;
import com.ruoyi.web.modules.order.domain.BizOrderAftersale;
import com.ruoyi.web.modules.order.dto.SellerAftersaleFinishDTO;
import com.ruoyi.web.modules.order.dto.SellerAftersaleQuery;
import com.ruoyi.web.modules.order.mapper.BizOrderAftersaleMapper;
import com.ruoyi.web.modules.order.mapper.BizOrderMapper;
import com.ruoyi.web.modules.order.service.ISellerAftersaleService;
import com.ruoyi.web.modules.order.support.OrderAppSupport;
import com.ruoyi.web.modules.pay.service.IOrderAftersaleRefundService;
import com.ruoyi.web.modules.order.vo.SellerAftersaleDetailVO;
import com.ruoyi.web.modules.order.vo.SellerAftersaleListRowVO;

@Service
public class SellerAftersaleServiceImpl implements ISellerAftersaleService
{
    @Autowired
    private BizOrderAftersaleMapper aftersaleMapper;

    @Autowired
    private BizOrderMapper orderMapper;

    @Autowired
    private OrderAppSupport orderAppSupport;

    @Autowired
    private IOrderAftersaleRefundService orderAftersaleRefundService;

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

    @Override
    public SellerAftersaleDetailVO getSellerDetail(Long shopId, Long aftersaleId)
    {
        SellerAftersaleDetailVO detail = aftersaleMapper.selectSellerDetail(shopId, aftersaleId);
        if (detail == null)
        {
            throw new ServiceException(OrderAppConstants.MSG_AFTERSALE_NOT_FOUND);
        }
        enrichDetail(detail);
        return detail;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void finish(Long shopId, Long aftersaleId, SellerAftersaleFinishDTO dto, String operator)
    {
        BizOrderAftersale aftersale = aftersaleMapper.selectByIdAndShop(aftersaleId, shopId);
        if (aftersale == null)
        {
            throw new ServiceException(OrderAppConstants.MSG_AFTERSALE_NOT_FOUND);
        }
        if (OrderAppConstants.AFTERSALE_STATUS_FINISHED.equals(aftersale.getAftersaleStatus()))
        {
            throw new ServiceException(OrderAppConstants.MSG_AFTERSALE_ALREADY_FINISHED);
        }
        String processResult = dto == null ? null : StringUtils.trim(dto.getProcessResult());
        if (StringUtils.isEmpty(processResult))
        {
            throw new ServiceException(OrderAppConstants.MSG_AFTERSALE_PROCESS_REQUIRED);
        }
        if (processResult.length() > OrderAppConstants.MAX_AFTERSALE_PROCESS_LENGTH)
        {
            processResult = processResult.substring(0, OrderAppConstants.MAX_AFTERSALE_PROCESS_LENGTH);
        }
        Date now = new Date();
        BizOrderAftersale update = new BizOrderAftersale();
        update.setAftersaleId(aftersaleId);
        update.setShopId(shopId);
        update.setProcessResult(processResult);
        update.setFinishTime(now);
        update.setUpdateTime(now);
        int rows = aftersaleMapper.updateForFinish(update);
        if (rows <= 0)
        {
            throw new ServiceException(OrderAppConstants.MSG_AFTERSALE_ALREADY_FINISHED);
        }
        orderMapper.updateForAftersaleClose(aftersale.getOrderId(), shopId, OrderConstants.CLOSE_TYPE_REFUND, operator);
        orderAftersaleRefundService.refundOnAftersaleFinish(aftersale);
    }

    private void enrichListRow(SellerAftersaleListRowVO row)
    {
        row.setApplyTypeText(orderAppSupport.aftersaleApplyTypeText(row.getApplyType()));
        row.setAftersaleStatusText(orderAppSupport.aftersaleStatusText(row.getAftersaleStatus()));
        row.setConsigneeMobile(MemberMobileUtils.mask(row.getConsigneeMobile()));
    }

    private void enrichDetail(SellerAftersaleDetailVO detail)
    {
        detail.setApplyTypeText(orderAppSupport.aftersaleApplyTypeText(detail.getApplyType()));
        detail.setAftersaleStatusText(orderAppSupport.aftersaleStatusText(detail.getAftersaleStatus()));
        detail.setProgressStage(orderAppSupport.aftersaleProgressStage(detail.getAftersaleStatus()));
        detail.setEvidencePics(orderAppSupport.splitPics(detail.getEvidencePicsRaw()));
    }
}
