package com.jeesharp.business.modules.finance.invoice.convert;

import com.jeesharp.business.modules.finance.invoice.dto.MemberInvoiceApplySaveDTO;
import com.jeesharp.business.modules.finance.invoice.entity.MemberInvoiceApply;
import com.jeesharp.business.modules.finance.invoice.enums.MemberInvoiceApplyBizTypeEnum;
import com.jeesharp.business.modules.finance.invoice.enums.MemberInvoiceApplyInvoiceTypeEnum;
import com.jeesharp.business.modules.finance.invoice.enums.MemberInvoiceApplyStatusEnum;
import com.jeesharp.business.modules.finance.invoice.vo.MemberInvoiceApplyInfoVO;
import com.jeesharp.business.modules.finance.invoice.vo.MemberInvoiceApplyListVO;
import com.jeesharp.business.modules.finance.invoice.vo.MemberInvoiceApplyQueryVO;
import com.jeesharp.framework.common.pojo.convert.BasePojoConvert;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
import org.mapstruct.factory.Mappers;

/**
 * 发票申请 MapStruct 转换器。
 *
 * @author jeesharp
 * @since 2026-05-08
 */
@Mapper
public interface MemberInvoiceApplyConvert
        extends BasePojoConvert<MemberInvoiceApply, MemberInvoiceApplySaveDTO, MemberInvoiceApplyInfoVO,
        MemberInvoiceApplyListVO, MemberInvoiceApplyQueryVO> {

    MemberInvoiceApplyConvert INSTANCE = Mappers.getMapper(MemberInvoiceApplyConvert.class);

    @Override
    MemberInvoiceApplyInfoVO toInfoVO(MemberInvoiceApply entity);

    @AfterMapping
    default void fillInfoDisplay(@MappingTarget MemberInvoiceApplyInfoVO vo, MemberInvoiceApply entity) {
        fillDisplayFields(vo, entity);
    }

    @AfterMapping
    default void fillListDisplay(@MappingTarget MemberInvoiceApplyListVO vo, MemberInvoiceApply entity) {
        fillDisplayFields(vo, entity);
    }

    private static void fillDisplayFields(Object vo, MemberInvoiceApply entity) {
        if (entity == null || vo == null) {
            return;
        }
        String statusName = MemberInvoiceApplyStatusEnum.getDescByValue(entity.getStatus());
        String bizTypeName = MemberInvoiceApplyBizTypeEnum.getDescByValue(entity.getBizType());
        String invoiceTypeName = MemberInvoiceApplyInvoiceTypeEnum.getDescByValue(entity.getInvoiceType());
        if (vo instanceof MemberInvoiceApplyInfoVO infoVo) {
            infoVo.setStatusName(statusName);
            infoVo.setBizTypeName(bizTypeName);
            infoVo.setInvoiceTypeName(invoiceTypeName);
        } else if (vo instanceof MemberInvoiceApplyListVO listVo) {
            listVo.setStatusName(statusName);
            listVo.setBizTypeName(bizTypeName);
            listVo.setInvoiceTypeName(invoiceTypeName);
        }
    }
}
