package com.mosty.sjzx.remote;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mosty.base.model.dto.base.GetSsbmDto;
import com.mosty.base.model.dto.base.SysDeptDTO;
import com.mosty.base.feign.service.MostyBaseFeignService;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.common.base.domain.ResponseResult;
import com.mosty.common.base.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * 调用部门信息远程适配层
 *
 * @author kevin
 * @date 2022/7/6 10:37 上午
 * @since 1.0.0
 */
@Slf4j
@Service
@AllArgsConstructor
public class TbBaseAdaptRemoteService {


    private final MostyBaseFeignService mostyBaseFeignService;

    // 根据部门编码查询部门信息
    public DeptInfoVo getOrgByOrgcode(String orgCode) {
        if (StringUtils.isBlank(orgCode)) {
            return null;
        }
        ResponseResult<DeptInfoVo> responseResult = mostyBaseFeignService.getOrgByOrgcode(orgCode);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("调用部门编码查询部门信息异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("调用部门编码查询部门信息异常");
        }
        return responseResult.getData();
    }

    // 查询分局下的所有的派出所信息
    public List<SysDeptDTO> getPcsByFxjdm(String orgCode) {
        if (StringUtils.isBlank(orgCode)) {
            return new ArrayList<>();
        }
        ResponseResult<List<SysDeptDTO>> responseResult = mostyBaseFeignService.getPcsByFxjdm(orgCode);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("查询分局下的所有的派出所信息异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("查询分局下的所有的派出所信息异常");
        }
        return responseResult.getData();
    }

    // 查询系统配置
    public String getValue(String pzj) {
        if (StringUtils.isBlank(pzj)) {
            return null;
        }
        ResponseResult<String> responseResult = mostyBaseFeignService.getValue(pzj);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("查询系统配置异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("查询系统配置异常");
        }
        return responseResult.getData();
    }

    // 获取位置信息
    public String getLocation(String jd, String wd) {
        String address = "未知位置";
        ResponseResult<JSONObject> obj = this.mostyBaseFeignService.getLzAddress(jd, wd);
        if (obj != null && obj.getData() != null) {
            String city = StringUtils.isNotBlank(obj.getData().getString("state")) ? obj.getData().getString("state") : "";
            String region = StringUtils.isNotBlank(obj.getData().getString("region")) ? obj.getData().getString("region") : "";
            String district = StringUtils.isNotBlank(obj.getData().getString("district")) ? obj.getData().getString("district") : "";
            String suburb = StringUtils.isNotBlank(obj.getData().getString("suburb")) ? obj.getData().getString("suburb") : "";
            String town = StringUtils.isNotBlank(obj.getData().getString("town")) ? obj.getData().getString("town") : "";
            String road = StringUtils.isNotBlank(obj.getData().getString("road")) ? obj.getData().getString("road") : "";
            address = city + region + district + suburb + town + road;
        }
        return address;
    }
    // 获取权限查询条件
    public String getSsbm(String ssbmdm, String isChild) {
        GetSsbmDto dto = new GetSsbmDto(ssbmdm, isChild);
        ResponseResult<String> responseResult = mostyBaseFeignService.getSsbm(dto);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("获取权限查询条件异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("获取权限查询条件异常");
        }
        return responseResult.getData();
    }
}
