package com.hz.pm.api.organization.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.basic.util.StrPool;
import com.hz.pm.api.organization.entity.GovBusinessStrip;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 服务类
 * </p>
 *
 * @author Liuxinxin
 * @since 2023-03-08
 */
public interface IGovBusinessStripService extends IService<GovBusinessStrip> {

    /**
     * 根据code查询名称
     *
     * @param codes 条线编码
     * @return 条线名称
     * @author WendyYang
     **/
    default List<String> listNameByCodes(Collection<String> codes) {
        return CollUtils.fieldList(listByCodes(codes), GovBusinessStrip::getBusinessStripName);
    }

    /**
     * 根据code查询条线
     *
     * @param codes 条线编码
     * @return 条线名称
     * @author WendyYang
     **/
    default List<GovBusinessStrip> listByCodes(Collection<String> codes) {
        LambdaQueryWrapper<GovBusinessStrip> query = Wrappers.lambdaQuery(GovBusinessStrip.class)
                .in(GovBusinessStrip::getBusinessStripCode, codes);
        return list(query);
    }

    /**
     * 根据code查询名称
     *
     * @param code 条线编码
     * @return 条线名称
     * @author WendyYang
     **/
    default String getNameByCode(String code) {
        return Optional.ofNullable(getByCode(code))
                .flatMap(w -> Optional.of(w.getBusinessStripName()))
                .orElse(StrPool.EMPTY);
    }

    /**
     * 根据code查询名称
     *
     * @param code 条线编码
     * @return 条线名称
     * @author WendyYang
     **/
    default GovBusinessStrip getByCode(String code) {
        List<GovBusinessStrip> strips = listByCodes(Collections.singletonList(code));
        return strips.isEmpty() ? null : strips.get(0);
    }

}
