package jnpf.model.personnels.dto.recruitmentchannels;

import jnpf.model.personnels.po.FtbPersonnelsRecruitmentChannels;
import lombok.Data;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Data
public class PersonnelsRecruitmentChannelsAddDTO {

    /**
     * 自定义招聘渠道，不传内置招聘渠道
     */
    @Valid
    @NotNull(message = "自定义招聘渠道信息不能为空")
    private List<RecruitmentChannelsInnerDTO> regularNames;

    @Data
    public static class RecruitmentChannelsInnerDTO {
        /**
         * 招聘渠道名称
         */
        @NotBlank(message = "招聘渠道名称不能为空")
        @Size(max = 20, message = "招聘渠道名称长度不能超过20位")
        private String regularName;
    }

    public List<FtbPersonnelsRecruitmentChannels> convert(PersonnelsRecruitmentChannelsAddDTO personnelsRecruitmentChannelsAddDTO) {
        if (personnelsRecruitmentChannelsAddDTO == null) {
            return null;
        }
        // 上限10个
        if (personnelsRecruitmentChannelsAddDTO.getRegularNames().size() > 10) {
            throw new RuntimeException("招聘渠道自定义上限为10个");
        }
        // 校验名称是否重复
        Set<String> uniqueSet = personnelsRecruitmentChannelsAddDTO.getRegularNames()
                .stream()
                .map(RecruitmentChannelsInnerDTO::getRegularName)
                .collect(Collectors.toSet());
        if (uniqueSet.size() != personnelsRecruitmentChannelsAddDTO.getRegularNames().size()) {
            throw new RuntimeException("招聘渠道名称重复");
        }
        return personnelsRecruitmentChannelsAddDTO.getRegularNames().stream().map(a -> {
            FtbPersonnelsRecruitmentChannels ftbPersonnelsRecruitmentChannels = new FtbPersonnelsRecruitmentChannels();
            ftbPersonnelsRecruitmentChannels.setRegularName(a.getRegularName());
            ftbPersonnelsRecruitmentChannels.setInternalStatus(1);
            return ftbPersonnelsRecruitmentChannels;
        }).collect(Collectors.toList());

    }
}
