package com.mosty.base.manager.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.model.dto.base.SysMessageInfoInsertDto;
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 com.mosty.common.core.business.entity.vo.SysUserDeptVO;
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.sql.BatchUpdateException;
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();
    }

    // 根据部门deptid获取所属分县局、所属地市州
    public DeptInfoVo getOrgByDeptId(String deptid) {
        if (StringUtils.isBlank(deptid)) {
            return null;
        }
        ResponseResult<DeptInfoVo> responseResult = mostyBaseFeignService.getOrgByDeptId(deptid);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("调用部门id查询部门详情信息异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("调用部门id查询部门详情信息异常");
        }
        return responseResult.getData();
    }

    // 获取部门下的所有的子部门信息
    public List<String> getChildDept(String deptid) {
        if (StringUtils.isBlank(deptid)) {
            return null;
        }
        ResponseResult<List<String>> responseResult = mostyBaseFeignService.getChildDept(deptid);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("调用部门id查询部门部门下的子部门异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("调用部门id查询部门部门下的子部门异常");
        }
        return responseResult.getData();
    }

    // 获取部门下的所有的子部门信息
    public List<String> getChildDeptByOrgCode(String ssbmdm) {
        if (StringUtils.isBlank(ssbmdm)) {
            return null;
        }
        ResponseResult<List<String>> responseResult = mostyBaseFeignService.getChildDeptByOrgCode(ssbmdm);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("调用部门id查询部门部门下的子部门异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("调用部门id查询部门部门下的子部门异常");
        }
        return responseResult.getData();
    }

    // 发送系统消息
    public Integer sendMsg(SysMessageInfoInsertDto dto) {
        ResponseResult<Integer> responseResult = mostyBaseFeignService.sendMsg(dto);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("发送系统消息异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("发送系统消息异常");
        }
        return responseResult.getData();
    }

    // 获取部门下的用户列表，不包含子部门
    public List<SysUserDeptVO> getUserList(String deptid) {
        ResponseResult<List<SysUserDeptVO>> responseResult = mostyBaseFeignService.getUserList(deptid);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("获取部门下的用户列表异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("获取部门下的用户列表异常");
        }
        return responseResult.getData();
    }

    // 查询所有的用户数据（包含对应的部门信息）
    public List<SysUserDeptVO> getUserAll(String deptid) {
        ResponseResult<List<SysUserDeptVO>> responseResult = mostyBaseFeignService.getUserAll(deptid);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("查询所有的用户数据 异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("查询所有的用户数据异常");
        }
        return responseResult.getData();
    }

    // 根据经纬度获取位置信息
    public JSONObject getAddress(String jd, String wd) {
        ResponseResult<JSONObject> responseResult = mostyBaseFeignService.getAddress(jd, wd);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("根据经纬度获取位置信息 异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("根据经纬度获取位置信息异常");
        }
        return responseResult.getData();
    }

    // 根据经纬度获取位置信息
    public JSONObject getLzAddress(String jd, String wd) {
        ResponseResult<JSONObject> responseResult = mostyBaseFeignService.getLzAddress(jd, wd);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("根据经纬度获取位置信息 异常 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("根据经纬度获取位置信息异常");
        }
        return responseResult.getData();
    }

    // 获取配置信息
    public String getValue(String pzj) {
        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("city")) ? obj.getData().getString("city") : "";
            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 village = StringUtils.isNotBlank(obj.getData().getString("village")) ? obj.getData().getString("village") : "";
            address = city + suburb + town + village;
        }
        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();
    }
}
