package com.jwipc.sys.client.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jwipc.sys.client.AreaClient;
import com.jwipc.sys.entity.CountryProvinceCity;
import com.jwipc.sys.service.ICountryProvinceCityService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@RequiredArgsConstructor
public class AreaClientImpl implements AreaClient {
    private final ICountryProvinceCityService countryProvinceCityService;

    public String getAreaName(String code, String pCode) {
        final LambdaQueryWrapper<CountryProvinceCity> wrapper = new LambdaQueryWrapper<CountryProvinceCity>().eq(
                        StrUtil.isNotEmpty(pCode), CountryProvinceCity::getPCode, pCode)
                .eq(CountryProvinceCity::getCode, code);
        final CountryProvinceCity one = countryProvinceCityService.getOne(wrapper, false);
        if (Objects.nonNull(one)) {
            return one.getName();
        }
        return StrUtil.EMPTY;
    }

    public String getAreaName(String code) {
        return getAreaName(code, null);
    }

    /**
     * 根据父级编号和自身名字获取自身的编码
     * @param pCode 父级编码
     * @param name 自身名字
     * @return
     */
    public String getAreaCode(String pCode,String name){
        final LambdaQueryWrapper<CountryProvinceCity> wrapper = new LambdaQueryWrapper<CountryProvinceCity>().eq(
                        StrUtil.isNotEmpty(pCode), CountryProvinceCity::getPCode, pCode)
                .eq(CountryProvinceCity::getName, name);
        final CountryProvinceCity one = countryProvinceCityService.getOne(wrapper, false);
        if (Objects.nonNull(one)) {
            return one.getCode();
        }
        return StrUtil.EMPTY;
    }
}
