MerchantCommonService.java 3.89 KB
package cn.com.glsx.merchants.service;

import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.merchants.convert.ConvertResponse;
import cn.com.glsx.merchants.model.response.MerchantChannelResponse;
import cn.com.glsx.merchants.remote.MerchantRemote;
import cn.com.glsx.merchants.response.CarbrandResponse;
import cn.com.glsx.merchants.response.ChannelResponse;
import cn.com.glsx.merchants.response.MerchantTreeRopResponse;
import com.glsx.biz.common.base.entity.CarBrandSelected;
import com.glsx.biz.common.base.entity.Carbrand;
import com.glsx.biz.common.base.service.CarbrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhouhaibao
 * @version 1.0
 * @description: 商户公共服务
 * @date 2021/8/1 10:20
 */
@Service
public class MerchantCommonService {

    @Autowired
    private CarbrandService carbrandService;

    @Autowired
    private MerchantRemote merchantRemote;

    public List<CarbrandResponse> getCarbrandTree() {
        List<Carbrand> parentCarbrandList = carbrandService.getParentCarbrandList();
        List<Carbrand> allList = carbrandService.getAllList();
        List<CarbrandResponse> collect = parentCarbrandList.stream().map(index -> {
            CarbrandResponse carbrandResponse = new CarbrandResponse();
            carbrandResponse.setId(index.getBid());
            carbrandResponse.setLabel(index.getBrand());
            List<CarbrandResponse> childrens = allList.stream().filter(child -> child.getParentId().equals(index.getBid())).map(in -> {
                CarbrandResponse carbrandResponse1 = new CarbrandResponse();
                carbrandResponse1.setId(in.getBid());
                carbrandResponse1.setLabel(in.getBrand());
                return carbrandResponse1;
            }).collect(Collectors.toList());
            carbrandResponse.setChildren(childrens);
            return carbrandResponse;
        }).collect(Collectors.toList());
        return collect;
    }


    public List<CarbrandResponse> getCarbrandTreeNew() {
        List<CarBrandSelected> carInfoCompleteForSelected = carbrandService.getCarInfoCompleteForSelected();
        List<CarbrandResponse> list = new ArrayList<>(carInfoCompleteForSelected.size());
        carInfoCompleteForSelected.forEach(v-> {
            CarbrandResponse response = new CarbrandResponse();
            response.setId(v.getId());
            response.setLabel(v.getName());
            if(v.getChilds() != null){
                v.getChilds().forEach(v1 -> {
                    if(v1.getChilds() != null){
                        List<CarbrandResponse> collect = v1.getChilds().stream().map(x -> {
                            CarbrandResponse sub = new CarbrandResponse();
                            sub.setId(x.getId());
                            sub.setLabel(x.getName());
                            return sub;
                        }).collect(Collectors.toList());
                        if(collect.size() > 0){
                            response.setChildren(collect);
                        }
                    }else {
                        System.out.println(v1.getId());
                    }

                });
            }
            list.add(response);
        });
        return list;
    }

    public List<ChannelResponse> findAllChannel() {
        RpcResponse<List<MerchantChannelResponse>> allChannel = merchantRemote.findAllChannel();
        if (allChannel.isSuccess()) {
            return ConvertResponse.convertList(allChannel.getResult(), ChannelResponse.class);
        }
        throw new RpcServiceException(allChannel.getException().getErrId(), allChannel.getMessage());
    }
}