package com.jeesharp.business.modules.mall.service.impl;

import com.jeesharp.business.modules.mall.entity.MallAfterSale;
import com.jeesharp.business.modules.mall.mapper.MallAfterSaleMapper;
import com.jeesharp.business.modules.mall.service.MallAfterSaleService;
import com.jeesharp.business.modules.mall.vo.MallAfterSaleQueryVO;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 商城售后单领域服务：申请、审核、处理完成等状态流转持久化。
 *
 * <p>退款完成后优惠券退回由 Application 层 {@link com.jeesharp.business.modules.mall.application.MallAfterSaleApplicationService#handleAfterSale} 触发。</p>
 *
 * @author jeesharp
 * @since 2026-05-08
 */
@Service
@Transactional(readOnly = true)
public class MallAfterSaleServiceImpl extends CrudService<MallAfterSaleMapper, MallAfterSale, MallAfterSaleQueryVO> implements MallAfterSaleService {

    private static final int PENDING_AUDIT_STATUS = 0;
    private static final int AUDIT_APPROVED_STATUS = 1;
    private static final int REFUNDING_STATUS = 3;
    private static final int REFUNDED_STATUS = 4;
    private static final int RETURNING_STATUS = 5;
    private static final int RETURNED_STATUS = 6;
    private static final int EXCHANGING_STATUS = 7;
    private static final int EXCHANGED_STATUS = 8;
    private static final int SYSTEM_SETTING = 1;
    private static final String IDS_SEPARATOR = ",";
    private static final String REFUND_ONLY_TYPE = "REFUND_ONLY";
    private static final String RETURN_REFUND_TYPE = "RETURN_REFUND";
    private static final String EXCHANGE_TYPE = "EXCHANGE";
    private static final String AFTER_SALE_NO_PREFIX = "AS";

    @Override
    @Transactional(readOnly = false)
    public void createAfterSale(MallAfterSale entity) {
        if (entity.getAfterSaleNo() == null || entity.getAfterSaleNo().isEmpty()) {
            entity.setAfterSaleNo(generateAfterSaleNo());
        }
        if (entity.getStatus() == null) {
            entity.setStatus(PENDING_AUDIT_STATUS);
        }
        save(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateAfterSale(MallAfterSale entity) {
        update(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public void auditAfterSale(String afterSaleId, Integer status, String auditBy, String auditRemark) {
        MallAfterSale afterSale = get(afterSaleId);
        if (afterSale == null) {
            return;
        }
        afterSale.setStatus(status);
        afterSale.setAuditBy(auditBy);
        afterSale.setAuditTime(new Date());
        afterSale.setAuditRemark(auditRemark);
        if (AUDIT_APPROVED_STATUS == status) {
            if (REFUND_ONLY_TYPE.equals(afterSale.getAfterSaleType())) {
                afterSale.setStatus(REFUNDING_STATUS);
            } else if (RETURN_REFUND_TYPE.equals(afterSale.getAfterSaleType())) {
                afterSale.setStatus(RETURNING_STATUS);
            } else if (EXCHANGE_TYPE.equals(afterSale.getAfterSaleType())) {
                afterSale.setStatus(EXCHANGING_STATUS);
            }
        }
        updateAfterSale(afterSale);
    }

    @Override
    @Transactional(readOnly = false)
    public void handleAfterSale(String afterSaleId, String handlerId, String handleRemark) {
        MallAfterSale afterSale = get(afterSaleId);
        if (afterSale == null) {
            return;
        }
        afterSale.setHandlerId(handlerId);
        afterSale.setHandleTime(new Date());
        afterSale.setHandleRemark(handleRemark);
        if (afterSale.getStatus() == REFUNDING_STATUS) {
            afterSale.setStatus(REFUNDED_STATUS);
        } else if (afterSale.getStatus() == RETURNING_STATUS) {
            afterSale.setStatus(RETURNED_STATUS);
        } else if (afterSale.getStatus() == EXCHANGING_STATUS) {
            afterSale.setStatus(EXCHANGED_STATUS);
        }
        updateAfterSale(afterSale);
    }

    @Override
    @Transactional(readOnly = false)
    public void applyAfterSale(String orderId, String orderItemId, String memberId,
                               String skuId, String spuId, String afterSaleType,
                               Integer applyQuantity, BigDecimal refundAmount,
                               String refundReason, String refundReasonDesc, String refundDesc) {
        MallAfterSale afterSale = new MallAfterSale();
        afterSale.setOrderId(orderId);
        afterSale.setOrderItemId(orderItemId);
        afterSale.setMemberId(memberId);
        afterSale.setSkuId(skuId);
        afterSale.setSpuId(spuId);
        afterSale.setAfterSaleType(afterSaleType);
        afterSale.setApplyQuantity(applyQuantity);
        afterSale.setRefundAmount(refundAmount);
        afterSale.setRefundReason(refundReason);
        afterSale.setRefundReasonDesc(refundReasonDesc);
        afterSale.setRefundDesc(refundDesc);
        createAfterSale(afterSale);
    }

    @Override
    public List<MallAfterSale> getMemberAfterSales(String memberId) {
        MallAfterSaleQueryVO query = new MallAfterSaleQueryVO();
        query.setMemberId(memberId);
        return findList(query);
    }

    private String generateAfterSaleNo() {
        return AFTER_SALE_NO_PREFIX + System.currentTimeMillis() + (int) (Math.random() * 1000);
    }
}
