package com.ruoyi.web.modules.trading.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.web.modules.trading.domain.BizSupplierSettlement;
import com.ruoyi.web.modules.trading.domain.BizTradeOrder;
import com.ruoyi.web.modules.trading.mapper.BizSupplierSettlementMapper;
import com.ruoyi.web.modules.trading.mapper.BizTradeOrderMapper;
import com.ruoyi.web.modules.trading.service.IBizSupplierSettlementService;
import com.ruoyi.web.modules.trading.support.SettlementRules;
import com.ruoyi.web.modules.trading.support.SettlementValidation;
import com.ruoyi.web.modules.trading.support.SupplierRules;
import com.ruoyi.web.modules.trading.support.TradeOrderRules;

/**
 * 供应商结算单 Service 实现。
 */
@Service
public class BizSupplierSettlementServiceImpl implements IBizSupplierSettlementService
{
    @Autowired
    private BizSupplierSettlementMapper bizSupplierSettlementMapper;

    @Autowired
    private BizTradeOrderMapper bizTradeOrderMapper;

    @Autowired
    private SettlementValidation settlementValidation;

    @Override
    public List<BizSupplierSettlement> selectBizSupplierSettlementList(BizSupplierSettlement query)
    {
        List<BizSupplierSettlement> list = bizSupplierSettlementMapper.selectBizSupplierSettlementList(query);
        if (list != null)
        {
            list.forEach(this::enrichDisplayFields);
        }
        return list;
    }

    @Override
    public BizSupplierSettlement selectBizSupplierSettlementById(Long id)
    {
        BizSupplierSettlement row = bizSupplierSettlementMapper.selectBizSupplierSettlementById(id);
        if (row == null)
        {
            throw new ServiceException(SettlementRules.MSG_NOT_FOUND);
        }
        enrichDisplayFields(row);
        return row;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BizSupplierSettlement completeSettle(BizSupplierSettlement request, String operator)
    {
        settlementValidation.validateSettleVoucher(request);
        BizSupplierSettlement existing = bizSupplierSettlementMapper.selectBizSupplierSettlementById(request.getId());
        if (existing == null)
        {
            throw new ServiceException(SettlementRules.MSG_NOT_FOUND);
        }
        if (!SettlementRules.SETTLEMENT_STATUS_PENDING.equals(existing.getSettlementStatus()))
        {
            throw new ServiceException(SettlementRules.MSG_NOT_PENDING);
        }
        assertLinkedOrderCompleted(existing.getOrderId());

        Date now = new Date();
        existing.setSettleVoucherUrl(request.getSettleVoucherUrl());
        existing.setSettleVoucherPath(request.getSettleVoucherPath());
        existing.setSettleFinishTime(now);
        existing.setSettlementStatus(SettlementRules.SETTLEMENT_STATUS_COMPLETED);
        existing.setUpdateBy(operator);
        existing.setUpdateTime(now);

        int updated = bizSupplierSettlementMapper.updateCompleteSettle(existing);
        if (updated == 0)
        {
            throw new ServiceException(SettlementRules.MSG_ALREADY_COMPLETED);
        }
        enrichDisplayFields(existing);
        return existing;
    }

    private void assertLinkedOrderCompleted(Long orderId)
    {
        BizTradeOrder order = bizTradeOrderMapper.selectBizTradeOrderById(orderId);
        if (order == null
                || TradeOrderRules.DEL_FLAG_DELETED.equals(order.getDelFlag())
                || !TradeOrderRules.ORDER_STATUS_COMPLETED.equals(order.getOrderStatus()))
        {
            throw new ServiceException(SettlementRules.MSG_ORDER_INVALID);
        }
    }

    private void enrichDisplayFields(BizSupplierSettlement row)
    {
        row.setSettlementStatusName(SettlementRules.settlementStatusName(row.getSettlementStatus()));
        row.setFeeMethodName(SupplierRules.feeMethodName(row.getFeeMethod()));
    }
}
