package com.ozs.system.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ozs.system.domain.SysRegion;
import com.ozs.system.domain.vo.SysRegionVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 *
 * @author makejava
 * @since 2021-05-21 00:08:38
 */
@Service
public class SysRegionService {

    @Resource
    private ISysRegionService gtAdministrativeDivisionService;



    /**
     * 获取行政区域列表
     * @return
     */
    public List<SysRegionVO> geAdministrativeDivisionList(Long parentId, int level) {
        List<SysRegionVO> result = new ArrayList<>();

        QueryWrapper<SysRegion> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysRegion::getLevel,level)
                .eq(parentId != null,SysRegion::getPid,parentId)
                .orderByAsc(SysRegion::getId);

        List<SysRegion>  gtAdministrativeDivisionList = this.gtAdministrativeDivisionService.list(queryWrapper);
        if(!ObjectUtils.isEmpty(gtAdministrativeDivisionList)){
            gtAdministrativeDivisionList.forEach(item ->{
                SysRegionVO vo = new SysRegionVO();
                BeanUtils.copyProperties(item, vo);
                result.add(vo);
            });
        }
        return result;
    }



    /**
     *  递归获取administrativeDivisionCodes 上级编码列表 省,市,区（包含自身）
     * @return
     */
    public String getParentAdministrativeDivisionCodes(String administrativeDivisionCode) {
        List<String> list = getParentAdministrativeDivisionCodeList(administrativeDivisionCode,true);
        if(!ObjectUtils.isEmpty(list)){
            StringBuilder sb = new StringBuilder();
            list.forEach(item ->{
                sb.append(item).append(",");
            });
            String parentAdministrativeDivisionCodes = sb.toString();
            if (parentAdministrativeDivisionCodes.endsWith(",")) {
                parentAdministrativeDivisionCodes = parentAdministrativeDivisionCodes.substring(0, parentAdministrativeDivisionCodes.length() - 1);
            }
            return parentAdministrativeDivisionCodes;
        } else {
            return administrativeDivisionCode;
        }
    }

    /**
     *  递归获取administrativeDivisionCodes 上级编码列表 省,市,区（包含自身）
     * @return
     */
    public List<String> getParentAdministrativeDivisionCodeList(String administrativeDivisionCode) {
        return getParentAdministrativeDivisionCodeList(administrativeDivisionCode,false);
    }

    /**
     *  递归获取administrativeDivisionCodes 上级编码列表 省,市,区（包含自身）
     * @return
     */
    public List<String> getParentAdministrativeDivisionCodeList(String administrativeDivisionCode,boolean hasChina) {
        List<String> administrativeDivisionCodes = new ArrayList<>();
        QueryWrapper<SysRegion> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysRegion::getCode,administrativeDivisionCode);
        SysRegion gtAdministrativeDivision = this.gtAdministrativeDivisionService.getOne(queryWrapper);
        if(gtAdministrativeDivision == null){
            return null;
        } else {
            administrativeDivisionCodes.add(administrativeDivisionCode);
        }
        getParentAdministrativeDivisionCodeList(gtAdministrativeDivision.getPid(), administrativeDivisionCodes);

        List<String> administrativeDivisionCodes2 = new ArrayList<>();
       for(int i = administrativeDivisionCodes.size() - 1; i >= 0; i--){
           administrativeDivisionCodes2.add(administrativeDivisionCodes.get(i));
       }
        if(hasChina){
            administrativeDivisionCodes2.add(0,"000000");
        }
        return administrativeDivisionCodes2;
    }

    private void getParentAdministrativeDivisionCodeList(Long pid,List<String> administrativeDivisionCodes) {
        SysRegion parent = this.gtAdministrativeDivisionService.getById(pid);
        if (parent != null && 0 != parent.getLevel()) {
            administrativeDivisionCodes.add(parent.getCode());
            getParentAdministrativeDivisionCodeList(parent.getPid(),administrativeDivisionCodes);
        }
    }

    /**
     *  递归获取administrativeDivisionNames 父名称： 省/市/区,包含自身
     * @return
             */
    public String getParentAdministrativeDivisionNames(String administrativeDivisionCode) {
        QueryWrapper<SysRegion> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysRegion::getCode,administrativeDivisionCode);
        SysRegion gtAdministrativeDivision = this.gtAdministrativeDivisionService.getOne(queryWrapper);
        if(gtAdministrativeDivision == null){
            return null;
        }
      return getParentAdministrativeDivisionName(gtAdministrativeDivision.getPid(),gtAdministrativeDivision.getAdministrativeDivision());
    }

    private String getParentAdministrativeDivisionName(Long pid,String pAdministrativeDivisionName) {
        SysRegion parent = this.gtAdministrativeDivisionService.getById(pid);
        if (parent != null && 0 != parent.getLevel()) {
            pAdministrativeDivisionName = parent.getAdministrativeDivision() + "/" + pAdministrativeDivisionName;
            pAdministrativeDivisionName = getParentAdministrativeDivisionName(parent.getPid(), pAdministrativeDivisionName);
        }

        return pAdministrativeDivisionName;
    }


    public SysRegionVO selectInfoByCode(String code) {
        return gtAdministrativeDivisionService.selectInfoByCode(code);
    }
}
