package com.hwrj.cloud.portal.service.impl;

import com.hwrj.cloud.portal.domain.AreaVo;
import com.hwrj.cloud.portal.mapper.PmsBasePositionMapper;
import com.hwrj.cloud.portal.model.PmsBasePosition;
import com.hwrj.cloud.portal.model.PmsBasePositionExample;
import com.hwrj.cloud.portal.service.PmsBasePositiontService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

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

@Service
public class PmsBasePositiontServiceImpl implements PmsBasePositiontService {
    @Autowired
    private PmsBasePositionMapper pmsBasePositionMapper;

    @Override
    public List<AreaVo> search(String parentId) {

        PmsBasePositionExample example=new PmsBasePositionExample();
        PmsBasePositionExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(parentId)){
            criteria.andUpcodeEqualTo(parentId);
        }else {
            criteria.andUpcodeEqualTo("#");
        }
        List<PmsBasePosition> pmsBasePositions = pmsBasePositionMapper.selectByExample(example);
        List<AreaVo> collect = pmsBasePositions.stream().map(x -> {
            AreaVo areaVo = new AreaVo();
            String id = x.getCode();
            areaVo.setAreaId(id);
            areaVo.setAreaName(x.getName());
            areaVo.setParentId(x.getUpcode());
            return areaVo;
        }).collect(Collectors.toList());
        return collect;
    }
}
