package iot.lidee.pay.core.convert.app;

import iot.lidee.common.core.domain.PageResult;
import iot.lidee.common.utils.collection.CollectionUtils;
import iot.lidee.pay.core.controller.admin.app.vo.PayAppCreateReqVO;
import iot.lidee.pay.core.controller.admin.app.vo.PayAppPageItemRespVO;
import iot.lidee.pay.core.controller.admin.app.vo.PayAppRespVO;
import iot.lidee.pay.core.controller.admin.app.vo.PayAppUpdateReqVO;
import iot.lidee.pay.core.domain.dataobject.app.PayApp;
import iot.lidee.pay.core.domain.dataobject.channel.PayChannel;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 支付应用信息 Convert
 *
 * @author 裕恒
 */
@Mapper
public interface PayAppConvert {

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

    PayAppPageItemRespVO pageConvert (PayApp bean);

    PayApp convert(PayAppCreateReqVO bean);

    PayApp convert(PayAppUpdateReqVO bean);

    PayAppRespVO convert(PayApp bean);

    List<PayAppRespVO> convertList(List<PayApp> list);

    PageResult<PayAppPageItemRespVO> convertPage(PageResult<PayApp> page);

    default PageResult<PayAppPageItemRespVO> convertPage(PageResult<PayApp> pageResult, List<PayChannel> channels) {
        PageResult<PayAppPageItemRespVO> voPageResult = convertPage(pageResult);
        // 处理 channel 关系
        Map<Long, Set<String>> appIdChannelMap = CollectionUtils.convertMultiMap2(channels, PayChannel::getAppId, PayChannel::getCode);
        voPageResult.getList().forEach(app -> app.setChannelCodes(appIdChannelMap.get(app.getId())));
        return voPageResult;
    }

}
