package cn.com.tenlion.systembase.service.grid.impl;

import cn.com.tenlion.systembase.pojo.dtos.grid.GridDTO;
import cn.com.tenlion.systembase.remote.grid.IGridRemoteService;
import cn.com.tenlion.systembase.service.grid.IGridService;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.module.dictionary.pojo.dtos.AreaDTO;
import ink.wgink.module.oauth2.manager.OAuth2ClientTokenManager;
import ink.wgink.mongo.module.dictionary.service.IMongoAreaService;
import ink.wgink.properties.ApiPathProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName: GridServiceImpl
 * @Description: 网格信息
 * @Author: CodeFactory
 * @Date: 2021-10-12 15:24:00
 * @Version: 3.0
 **/
@Service
public class GridServiceImpl extends DefaultBaseService implements IGridService {

    @Autowired
    private IGridRemoteService gridRemoteService;
    @Autowired
    private ApiPathProperties apiPathProperties;
    @Autowired
    private IMongoAreaService mongoAreaService;


    @Override
    public GridDTO get(String gridId) {
        String sendUrl = apiPathProperties.getUserCenter();
        String accessToken = OAuth2ClientTokenManager.getInstance().getToken().getAccessToken();
        return gridRemoteService.getGrid(sendUrl, gridId, accessToken);
    }

    @Override
    public String getLocationAll(String locationCode) {
        String locationAll = "";
        AreaDTO areaDTO = mongoAreaService.getByCode(locationCode);
        Integer parentId = Integer.valueOf(areaDTO.getAreaParentId());
        String areaName = areaDTO.getAreaName();
        locationAll += areaName;
        if(0 != parentId) {
            for(int i=1;i<=5;i++) {
                areaDTO = mongoAreaService.get(parentId.toString());
                parentId = Integer.valueOf(areaDTO.getAreaParentId());
                areaName = areaDTO.getAreaName();
                locationAll = areaName + "/" + locationAll;
                if(0 == parentId) {
                    break;
                }
            }
        }
        return locationAll;
    }

    @Override
    public String getLocationArea(String locationCode) {
        String locationArea = "";
        System.out.println("locationCode = " + locationCode);
        AreaDTO areaDTO = mongoAreaService.getByCode(locationCode);
        Integer level = Integer.valueOf(areaDTO.getAreaLevel());
        Integer parentId = Integer.valueOf(areaDTO.getAreaParentId());
        String areaName = areaDTO.getAreaName();
        locationArea = areaName;
        if(0 != level && 2 != level && level > 1) {
            for(int i=1;i<=5;i++) {
                System.out.println("parentId.toString() = " + parentId.toString());
                areaDTO = mongoAreaService.get(parentId.toString());
                System.out.println("areaDTO.toString() = " + areaDTO.toString());
                level = Integer.valueOf(areaDTO.getAreaLevel());
                parentId = Integer.valueOf(areaDTO.getAreaParentId());
                areaName = areaDTO.getAreaName();
                if(0 != parentId) {
                    if(level == 2) {
                        locationArea = areaName;
                        break;
                    }
                }else {
                    break;
                }
            }
        }
        return locationArea;
    }


}