UnitService.java 2.59 KB
package com.huaheng.framework.web.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.framework.web.entity.UnitRelation;
import com.huaheng.pc.config.materialUnit.domain.MaterialUnit;
import com.huaheng.pc.config.materialUnit.service.MaterialUnitService;
import com.huaheng.pc.system.dict.domain.DictData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * huaheng首创 html调用 thymeleaf 实现字典读取
 *
 * @author huaheng
 */
@Service("unit")
public class UnitService {

    private static final Logger logger = LoggerFactory.getLogger(UnitService.class);
    @Autowired
    private MaterialUnitService materialUnitService;

    public static List<UnitRelation> unitRelations = null;

    public static Map<String, List<UnitRelation>> unitRelationMap = new HashMap<>();

    /**
     * 根据中文单位类型查询物料单位数据信息
     */
    public List<UnitRelation> getType(String code) {
        System.out.println(code);
        if (StringUtils.isNotEmpty(code) && code.equals("refresh")) {
            unitRelations = null;
        }
        if (unitRelations == null ||unitRelations.size()==0)
        {

            unitRelations = getUnitAll();
        }
        List<UnitRelation> result = unitRelations;

        return result;
    }

    /**
     * 根据根据中文单位查找英文单位
     * @return 字典标签
     */
    public String getEnCode(String code) {
        if (unitRelations == null) {
            unitRelations = getUnitAll();
        }
        String result = unitRelations.stream().
                filter(x -> x.getCode().equals(code) ).
                findFirst().
                map(UnitRelation::getEnCode).
                orElse(null);
        return result;
    }

    public List<UnitRelation> getUnitAll(){
        LambdaQueryWrapper<MaterialUnit> wrapper=new LambdaQueryWrapper<>();
        wrapper.isNotNull(MaterialUnit::getUserDef1);
        List<MaterialUnit> list=materialUnitService.list(wrapper);
        if(list==null||list.size()==0)
        {
            return null;
        }
        return list.stream().map(x->{
            UnitRelation relation=new UnitRelation();
            relation.setCode(x.getUnit());
            relation.setEnCode(x.getUserDef1());
            return relation;
        }).collect(Collectors.toList());
    }

}