package com.jeesharp.business.modules.mall.convert;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.mall.dto.MallOrderSaveDTO;
import com.jeesharp.business.modules.mall.entity.MallOrder;
import com.jeesharp.business.modules.mall.enums.MallPayTypeEnum;
import com.jeesharp.business.modules.mall.vo.MallOrderInfoVO;
import com.jeesharp.business.modules.mall.vo.MallOrderListVO;
import com.jeesharp.business.modules.mall.vo.MallOrderQueryVO;
import com.jeesharp.framework.common.pojo.convert.BasePojoConvert;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * 商城订单 MapStruct 转换器；分页与列表转换复用 {@link BasePojoConvert#toPageRespVO}、{@link BasePojoConvert#toListVO}。
 *
 * @author jeesharp
 * @since 2026-06-05
 */
@Mapper
public interface MallOrderConvert extends BasePojoConvert<MallOrder, MallOrderSaveDTO, MallOrderInfoVO, MallOrderListVO, MallOrderQueryVO> {

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

    @Override
    @Mapping(target = "memberNo", ignore = true)
    @Mapping(target = "memberNickname", ignore = true)
    @Mapping(target = "memberMobile", ignore = true)
    @Mapping(target = "payTypeName", source = "payType", qualifiedByName = "payTypeToName")
    @Mapping(target = "payTime", source = "payTime", qualifiedByName = "dateToLocalDateTime")
    MallOrderInfoVO toInfoVO(MallOrder entity);

    @Override
    @Mapping(target = "memberNo", ignore = true)
    @Mapping(target = "memberNickname", ignore = true)
    @Mapping(target = "memberMobile", ignore = true)
    @Mapping(target = "payTypeName", source = "payType", qualifiedByName = "payTypeToName")
    @Mapping(target = "payTime", source = "payTime", qualifiedByName = "dateToLocalDateTime")
    MallOrderListVO toListSingleVO(MallOrder entity);

    @Named("dateToLocalDateTime")
    default LocalDateTime dateToLocalDateTime(Date value) {
        if (value == null) {
            return null;
        }
        return value.toInstant().atZone(ZoneId.of("Asia/Shanghai")).toLocalDateTime();
    }

    @Named("payTypeToName")
    default String payTypeToName(String payType) {
        String desc = MallPayTypeEnum.getDescByCode(payType);
        return StrUtil.isBlank(desc) ? "—" : desc;
    }
}
