package com.jwipc.sys.client.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jwipc.sys.client.CompanyClient;
import com.jwipc.sys.client.dto.CompanyDTO;
import com.jwipc.sys.client.convert.CompanyClientConvert;
import com.jwipc.sys.entity.Company;
import com.jwipc.sys.service.ICompanyService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@RequiredArgsConstructor
public class CompanyClientImpl implements CompanyClient {
    private final ICompanyService companyService;
    private final CompanyClientConvert companyClientConvert;

    public List<CompanyDTO> getByCodes(Collection<? extends Serializable> codes) {
        if (CollUtil.isEmpty(codes)) {
            return Collections.emptyList();
        }
        final List<Company> list = companyService.list(
                new LambdaQueryWrapper<Company>().in(Company::getCompanyCode, codes));
        return companyClientConvert.entites2Dtos(list);
    }

    public CompanyDTO getByCode(String code) {
        final Company list = companyService.getOne(
                new LambdaQueryWrapper<Company>().eq(Company::getCompanyCode, code),false);
        return companyClientConvert.entity2Dto(list);
    }

    public List<CompanyDTO> getAll() {
        final List<Company> list = companyService.list();
        return companyClientConvert.entities2Dtos(list);
    }
}
