package com.trs.ggzyexchange.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.trs.ggzyexchange.entity.AreaPlatform;
import com.trs.ggzyexchange.entity.TRegion;
import com.trs.ggzyexchange.service.AreaPlatformService;
import com.trs.ggzyexchange.service.TRegionService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AreaDataName {

    @Autowired
    private TRegionService tRegionService;

    @Autowired
    private AreaPlatformService areaPlatformService;

    public String getAreaDataName(String regionCode,String platformCode){
        String areaDataName = null;
        TRegion tRegion = null;
        if(!ObjectUtils.isEmpty(regionCode)){
            QueryWrapper<TRegion> tRegionQueryWrapper = new QueryWrapper<TRegion>();
            tRegionQueryWrapper.eq("code",regionCode+"000000");
            Page<TRegion> pageTRegion = new Page<TRegion>(1, 1);
            Page<TRegion> tRegionPage = tRegionService.page(pageTRegion, tRegionQueryWrapper);
            if(tRegionPage.getRecords().size() >=1){
                tRegion = tRegionPage.getRecords().get(0);
                if(!ObjectUtils.isEmpty(tRegion)){
                    areaDataName = tRegion.getName();
                }
            }
        }
        if(!ObjectUtils.isEmpty(platformCode) && ObjectUtils.isEmpty(tRegion)){
            QueryWrapper<AreaPlatform> platformQueryWrapper = new QueryWrapper<AreaPlatform>();
            platformQueryWrapper.eq("platform_code",platformCode);
            AreaPlatform areaPlatform = areaPlatformService.getOne(platformQueryWrapper);
            if(!ObjectUtils.isEmpty(areaPlatform)){
                areaDataName = areaPlatform.getRegionName();
            }
        }
        return areaDataName;
    }
}
