package com.jwipc.sys.client.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jwipc.sys.client.FactoryClient;
import com.jwipc.sys.client.dto.FactoryDTO;
import com.jwipc.sys.client.convert.FactoryClientConvert;
import com.jwipc.sys.entity.Factory;
import com.jwipc.sys.service.IFactoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@RequiredArgsConstructor
public class FactoryClientImpl implements FactoryClient {
    private final IFactoryService factoryService;
    private final FactoryClientConvert factoryClientConvert;

    /**
     * 通过工厂编号查询工厂地址
     *
     * @param factoryCode 工厂编号
     * @return
     */
    public String getFacotryAddress(String factoryCode) {
        final Factory factory = factoryService.getOne(
                new LambdaQueryWrapper<Factory>().eq(Factory::getFactory, factoryCode));
        if (Objects.nonNull(factory)) {
            return factory.getAddress();
        }
        return StrUtil.EMPTY;
    }

    /**
     * 通过工厂编号查询工厂
     *
     * @param factoryCode 工厂编号
     * @return
     */
    public FactoryDTO getByCode(String factoryCode) {
        final Factory factory = factoryService.getOne(
                new LambdaQueryWrapper<Factory>().eq(Factory::getFactory, factoryCode));
        return factoryClientConvert.entity2Dto(factory);
    }

    /**
     * 查询全部工厂数据
     */
    public List<FactoryDTO> getAll() {
        final List<Factory> factory = factoryService.list();
        return factoryClientConvert.entities2Dtos(factory);
    }

}
