package com.ankaibei.workFlow.webadmin.upms.service.impl;

import com.ankaibei.workFlow.common.core.base.dao.BaseDaoMapper;
import com.ankaibei.workFlow.common.core.base.service.BaseService;
import com.ankaibei.workFlow.webadmin.upms.dao.SysRegionMapper;
import com.ankaibei.workFlow.webadmin.upms.model.SysRegion;
import com.ankaibei.workFlow.webadmin.upms.service.SysRegionService;
import com.ankaibei.workflow.vo.AreaInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;


@Slf4j
@Service
public class SysRegionServiceImpl extends BaseService<SysRegion, Long> implements SysRegionService {

    @Autowired
    private SysRegionMapper sysRegionMapper;

    @Override
    protected BaseDaoMapper<SysRegion> mapper() {
        return sysRegionMapper;
    }


    @Override
    @Async
    public boolean insertAll(List<AreaInfo> list) {
        recursiveInsertion(list,"0");
        return true;
    }

    private void   recursiveInsertion(List<AreaInfo> list,String parentAdcode){
        if(!ObjectUtils.isEmpty(list) && list.size() >0){
            for (AreaInfo areaInfo : list) {
                if(areaInfo.getLevel().equals("street")){
                     return;
                }
                SysRegion sysRegion = new SysRegion();
                BeanUtils.copyProperties(areaInfo,sysRegion);
                sysRegion.setParentAdcode(parentAdcode);
                log.info("sysRegion",sysRegion);
                sysRegionMapper.insert(sysRegion);
                if(!ObjectUtils.isEmpty(areaInfo.getDistricts())
                        && areaInfo.getDistricts().size()>0){
                    recursiveInsertion(areaInfo.getDistricts(),areaInfo.getAdcode());
                }
            }
        }
    }
}
