package cn.newfeifan.mall.module.distri.convert.usertopupconsumptionpointsorder;

import cn.newfeifan.mall.framework.common.util.string.StrUtils;
import cn.newfeifan.mall.module.distri.config.OrderProperties;
import cn.newfeifan.mall.module.distri.dal.dataobject.usertopupconsumptionpointsorder.UserTopUpConsumptionPointsOrderDO;
import cn.newfeifan.mall.module.pay.api.order.dto.PayOrderCreateReqDTO;
import cn.newfeifan.mall.module.pay.dal.dataobject.order.PayOrderDO;
import cn.newfeifan.mall.module.pay.enums.order.PayOrderStatusEnum;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import static cn.newfeifan.mall.framework.common.util.date.LocalDateTimeUtils.addTime;

@Mapper
public interface UserTopUpConsumptionPointsOrderConvert {

    UserTopUpConsumptionPointsOrderConvert INSTANCE = Mappers.getMapper(UserTopUpConsumptionPointsOrderConvert.class);
    default PayOrderDO convert(UserTopUpConsumptionPointsOrderDO saveReqVO, OrderProperties orderProperties)
    {
        PayOrderDO payOrderDO = new PayOrderDO()
                .setAppId(orderProperties.getAppId()).setUserIp(saveReqVO.getUserIp());
        // 商户相关字段
        payOrderDO.setMerchantOrderId(String.valueOf(saveReqVO.getId()));
        String subject = saveReqVO.getSubject();
        subject = StrUtils.maxLength(subject, PayOrderCreateReqDTO.SUBJECT_MAX_LENGTH); // 避免超过 32 位
        payOrderDO.setSubject(subject);
        payOrderDO.setBody(subject);
        payOrderDO.setStatus(PayOrderStatusEnum.WAITING.getStatus()).setRefundPrice(0);
        payOrderDO.setMerchantOrderId(saveReqVO.getId().toString());

        // 订单相关字段
        payOrderDO.setPrice(saveReqVO.getPayPrice().intValue()).setExpireTime(addTime(orderProperties.getPayExpireTime()));
        return payOrderDO;
    }
}
