package com.yc.ship.module.resource.convert.machine;

import com.yc.ship.framework.common.util.collection.CollectionUtils;
import com.yc.ship.framework.common.util.collection.MapUtils;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.resource.controller.admin.machine.vo.ScenicMachineRespVO;
import com.yc.ship.module.resource.dal.dataobject.channel.ScenicChannelDO;
import com.yc.ship.module.resource.dal.dataobject.machine.ScenicMachineDO;
import com.yc.ship.module.resource.dal.dataobject.site.ScenicSiteDO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

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

@Mapper
public interface MachineConvert {

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


    default List<ScenicMachineRespVO> convertList(List<ScenicMachineDO> list, Map<Long, ScenicChannelDO> channelMap) {
        return CollectionUtils.convertList(list, scenicMachine -> {
            ScenicMachineRespVO machineUserVO = BeanUtils.toBean(scenicMachine, ScenicMachineRespVO.class);
//            MapUtils.findAndThen(channelMap, scenicMachine.getChannelId(), channel -> machineUserVO.setChannelName(channel.getName()));
            MapUtils.findAndThen(channelMap, scenicMachine.getChannelId(), channel -> {
                machineUserVO.setChannelName(channel.getName());
                machineUserVO.setSiteId(channel.getSiteId());
            });
            return machineUserVO;
        });
    }

    default List<ScenicMachineRespVO> convertRespList(List<ScenicMachineRespVO> list, Map<Long, Object> map) {
        return CollectionUtils.convertList(list, scenicMachine -> {
            MapUtils.findAndThen(map, scenicMachine.getChannelId(), m -> {
                ScenicChannelDO channel = (ScenicChannelDO) m;
                scenicMachine.setChannelName(channel.getName());
                scenicMachine.setSiteId(channel.getSiteId());
            });
            MapUtils.findAndThen(map, scenicMachine.getSiteId(), m -> {
                ScenicSiteDO site = (ScenicSiteDO) m;
                scenicMachine.setSiteName(site.getName());
                scenicMachine.setSiteId(site.getId());
                scenicMachine.setSiteType(site.getSiteType());
            });
            return scenicMachine;
        });
    }
}
