package com.cyl.manager.ums.convert;

import com.cyl.manager.ums.domain.Pet;
import com.cyl.manager.ums.pojo.vo.PetVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 宠物档案  DO <=> DTO <=> VO / BO / Query
 *
 * @author daixiande
 */
@Mapper(componentModel = "spring")
public interface PetConvert  {

    List<PetVO> dos2vos(List<Pet> list);

    @Mappings({
            @Mapping(target = "healthStatus", expression = "java(getHealthStatus(petVO.getHealthStatus()))"),
            @Mapping(target = "personality", expression = "java(getPersonality(petVO.getPersonality()))")
    })

    Pet vo2do(PetVO petVO);

    @Mappings({
            @Mapping(target = "healthStatus", expression = "java(getHealthStatus(pet.getHealthStatus()))"),
            @Mapping(target = "personality", expression = "java(getPersonality(pet.getPersonality()))")
    })
    PetVO do2vo(Pet pet);

    default String getPersonality(List<String> personality) {
        if(CollectionUtils.isEmpty(personality)){
            return null;
        }else{
            return personality.stream()
                    .map(String::valueOf)
                    .collect(Collectors.joining(","));
        }
    }

    default List<String> getPersonality(String personality) {
        if(StringUtils.isEmpty(personality)){
            return null;
        }else{
            return Arrays.asList(personality.split(","));
        }
    }

    default String getHealthStatus(List<String> healthStatus) {
        if(CollectionUtils.isEmpty(healthStatus)){
            return null;
        }else{
            return healthStatus.stream()
                    .map(String::valueOf)
                    .collect(Collectors.joining(","));
        }
    }

    default List<String> getHealthStatus(String healthStatus) {
        if(StringUtils.isEmpty(healthStatus)){
            return null;
        }else{
            return Arrays.asList(healthStatus.split(","));
        }
    }
}
