package com.ssm.manage.service.impl;

import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.manage.dao.BaseDao;
import com.ssm.manage.dao.RegionListDao;
import com.ssm.manage.dto.RegionListDto;
import com.ssm.manage.entity.RegionList;
import com.ssm.manage.query.RegionListVo;
import com.ssm.manage.service.RegionListService;
import com.ssm.manage.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * (RegionList)表服务实现类
 *
 * @author makejava
 * @since 2023-12-16 11:26:09
 */
@Service("regionListService")
public class RegionListServiceImpl extends BaseServiceImpl<RegionList> implements RegionListService {
    @Autowired
    private RegionListDao regionListDao;

    @Override
    public BaseDao<RegionList> getDao() {
        return regionListDao;
    }

    @Override
    public List<RegionListVo> selectPage(RegionListDto dto) {
        List<RegionListVo> regionListVos = new ArrayList<>();
        Example example = new Example(RegionList.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(dto.getRegionNum())){
            criteria.andEqualTo(RegionListDto.Fields.regionNum,dto.getRegionNum());
        }
        if (StringUtils.isNotBlank(dto.getRegionName())){
            criteria.andEqualTo(RegionListDto.Fields.regionName,dto.getRegionName());
        }
//        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<RegionList> list = this.regionListDao.selectByExample(example);
        for (RegionList regionList : list) {
            RegionListVo regionListVo = new RegionListVo();
            BeanUtils.copyProperties(regionList,regionListVo);
            regionListVos.add(regionListVo);
        }
        return getList(regionListVos);
    }


    public List getList(List<RegionListVo> list){
//        List resultList = new ArrayList();
        //一级菜单
        List<RegionListVo> regionListVoList = new ArrayList<>();
        for (RegionListVo regionListVo : list) {
            if(regionListVo.getParentId()==null || regionListVo.getParentId().equals("")){
                RegionListVo regionList = new RegionListVo();
                BeanUtils.copyProperties(regionListVo,regionList);
                regionListVoList.add(regionList);
            }
        }
        for (RegionListVo regionListVo : regionListVoList) {
            regionListVo.setRegionListVoList(getChildren(regionListVo.getId(),list));
        }
        return regionListVoList;
    }

    public List<RegionListVo> getChildren(Integer id,List<RegionListVo> list){

        List<RegionListVo> children = new ArrayList<>();
        for (RegionListVo regionListVo : list) {
            if(regionListVo.getParentId()!=null  && regionListVo.getParentId().equals(id)){
                RegionListVo region = new RegionListVo();
                BeanUtils.copyProperties(regionListVo,region);
                region.setRegionListVoList(getChildren(region.getId(),list));
                children.add(region);
            }
        }
        return children;
    }
}
