package com.yc.ship.module.miniapplet.convert.problemtype;

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.miniapplet.controller.admin.problem.vo.ProblemRespVO;
import com.yc.ship.module.miniapplet.controller.admin.problemtype.vo.ProblemTypeRespVO;
import com.yc.ship.module.miniapplet.dal.dataobject.problem.ProblemDO;
import com.yc.ship.module.miniapplet.dal.dataobject.problemtype.ProblemTypeDO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

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

@Mapper
public interface ProblemTypeConvert {

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

    default List<ProblemTypeRespVO> convertProblemTypeList(List<ProblemTypeDO> list, Map<Long, ProblemTypeDO> map) {
        return CollectionUtils.convertList(list, problemType -> {
            ProblemTypeRespVO problemTypeVO = BeanUtils.toBean(problemType, ProblemTypeRespVO.class);
            MapUtils.findAndThen(map, problemType.getParentId(), c -> problemTypeVO.setParentName(c.getName()));
            return problemTypeVO;
        });
    }

    default List<ProblemRespVO> convertProblemList(List<ProblemDO> list, Map<Long, ProblemTypeDO> map) {
        return CollectionUtils.convertList(list, problem -> {
            ProblemRespVO problemVO = BeanUtils.toBean(problem, ProblemRespVO.class);
            MapUtils.findAndThen(map, problem.getTypeId(), c -> problemVO.setTypeName(c.getName()));
            return problemVO;
        });
    }

}
