package com.persagy.server.syn.models.api

import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.persagy.base.extensions.toJson
import com.persagy.base.utils.SHttpUtil
import com.persagy.server.syn.Opts
import com.persagy.server.syn.models.DictPlatformInfo
import com.persagy.server.syn.models.DictPlatformType
import com.persagy.server.syn.models.DictProject
import com.persagy.server.syn.models.Group
import com.persagy.server.syn.models.response.*
import com.persagy.server.syn.services.dict.DClassDefService
import com.persagy.service.utils.SSpringContextUtil

/**
 * 字典API
 *
 * @author  wx
 * @date  2020/9/11 15:04
 */
object DictTypeApi {

    val opts: Opts by lazy {
        SSpringContextUtil.getBean(Opts::class.java) as Opts
    } // opts

    /**
     * 字典类型
     */
    fun dictTypeHttp(){

       val url = "${opts.dataPlatform}dict/class?&groupId=wd&schemeId=7&projectId=Pj1101050029&appId=datautils&subFlag=false"

        val request = SHttpUtil.getRequest(url)


    } //


    /**
     * 查询集团
     */
    fun groupQuery():ArrayList<Group> {
        var data = ArrayList<Group>()
        try {
            val url = "${opts.dataPlatform}/usercenter/groupCompany/query"
            val jsonObject = JSONObject()
            val postObject = SHttpUtil.postObject<GroupResponse>(url, jsonObject)
            DClassDefService.logger.debug("集团结果:${postObject.toJson()}")
            if (postObject.result == "success"){
                if (!postObject.data.isNullOrEmpty()){
                    data = postObject.data!!
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return data
    } // Fun group()

    /**
     * 查询字典项目
     */
    fun dictProjectQuery(code: String): ArrayList<DictProject>{
        val url = "${opts.dataPlatform}/usercenter/project/query"
        val jsonObject = JSONObject()
        val jsonObject2 = JSONObject()
        jsonObject2.put("groupCompanyCode",code)
        jsonObject["criteria"] = jsonObject2
        DClassDefService.logger.debug("url:${url}")
        DClassDefService.logger.debug("项目请求体:${jsonObject.toJson()}")
        val postObject = SHttpUtil.postObject<DictProjectResponse>(url, jsonObject)
        if (postObject.result == opts.SUCCESS_CODE){
                if (!postObject.data.isNullOrEmpty()){
                    return  postObject.data!!
                }
        }
        return ArrayList()
    } // Fun dictProjectQuery()

    /**
     * 平台字典对象类型
     */
    fun dictPlatform(): ArrayList<DictPlatformType>{
        val url = "${opts.dataPlatform}/def/class/"
        val jsonObject = JSONObject()
        val jsonObject2 = JSONObject()
        jsonObject2["type"] = "common"
        jsonObject["criteria"] = jsonObject2
        val postObject = SHttpUtil.postObject<DictPlatformTypeResponse>(url, jsonObject)
        if (postObject.result == opts.SUCCESS_CODE) {
            if (!postObject.data.isNullOrEmpty()){
                return postObject.data!!
            }
        }
        return ArrayList()
    } // Fun dictPlatform()


    val a = "http://192.168.3.203:8081/paas/dict/funcid/query"

    /**
     * 对象详细信息点
     *
     * @param   category    对象类型
     * @param   type        平台或集团
     */
    fun dictPlatformInfo(category: String,type: String): ArrayList<DictPlatformInfo>{
        val url = "${opts.dataPlatform}/def/funcid"
        val jsonObject = JSONObject()
        val jsonObject2 = JSONObject()
        jsonObject2["type"] = type
        jsonObject2["classCode"] = category
        jsonObject["criteria"] = jsonObject2

        val postObject = SHttpUtil.postObject<DictPlatformInfoResponse>(url, jsonObject)
        if (postObject.result == opts.SUCCESS_CODE) {
            if (!postObject.data.isNullOrEmpty()){
                return postObject.data!!
            }
        }
        return ArrayList()
    } // Fun dictPlatformInfo()

    /**
     * 获取专业
     */
    fun dictMajor(type: String): ArrayList<DictMajorResponse.DataBean>{
        val url = "${opts.dataPlatform}/dict/major/query"
        val jsonObject = JSONObject()
        val jsonObject2 = JSONObject()
        jsonObject2["type"] = type
        jsonObject["criteria"] = jsonObject2
        val postObject = SHttpUtil.postObject<DictMajorResponse>(url, jsonObject)
        if (postObject.result == opts.SUCCESS_CODE) {
            if (!postObject.data.isNullOrEmpty()){
                return postObject.data!!
            }
        }
        return ArrayList()
    } // Fun dictMajor()

    /**
     * 集团下的方案
     */
    fun dictGroupScheme(groupId: String): ArrayList<DictGroupSchemeResponse.DataBean>{
        val url = "${opts.dataPlatform}/dict/scheme/query"
        val jsonObject = JSONObject()
        val jsonObject2 = JSONObject()
        jsonObject2["groupId"] = groupId
        jsonObject["criteria"] = jsonObject2
        val postObject = SHttpUtil.postObject<DictGroupSchemeResponse>(url, jsonObject)
        if (postObject.result == opts.SUCCESS_CODE) {
            if (!postObject.data.isNullOrEmpty()){
                return postObject.data!!
            }
        }
        return ArrayList()
    } // Fun dictGroupScheme()

    /**
     * 方案订阅的对象类型
     */
    fun dictSchemeCategoty(schemeId: Int): ArrayList<DictSchemeCategoryResponse.DataBean>{
        val url = "${opts.dataPlatform}/dict/scheme/class/query"
        val jsonObject = JSONObject()
        val jsonObject2 = JSONObject()
        jsonObject2["groupId"] = schemeId.toString()
        jsonObject["criteria"] = jsonObject2
        val postObject = SHttpUtil.postObject<DictSchemeCategoryResponse>(url, jsonObject)
        if (postObject.result == opts.SUCCESS_CODE) {
            if (!postObject.data.isNullOrEmpty()){
                return postObject.data!!
            }
        }
        return ArrayList()
    } // Fun dictSchemeCategoty()

    /**
     * 方案对象对应的信息点
     */
    fun dictSchemeCategotyInfo(schemeId: Int,code: String): ArrayList<DictSchemeCategoryResponseInfo.DataBean>{
        val url = "${opts.dataPlatform}/dict/scheme/funcid/query"
        val jsonObject = JSONObject()
        val jsonObject2 = JSONObject()
        jsonObject2["schemeId"] = schemeId.toString()
        jsonObject2["classCode"] = code
        jsonObject["criteria"] = jsonObject2
        val postObject = SHttpUtil.postObject<DictSchemeCategoryResponseInfo>(url, jsonObject)
        if (postObject.result == opts.SUCCESS_CODE) {
            if (!postObject.data.isNullOrEmpty()){
                return postObject.data!!
            }
        }
        return ArrayList()
    } // Fun dictSchemeCategotyInfo()

    /**
     * 查询项目和方案的关系
     */
    fun dictSchemeAndProject(projectId: String): ArrayList<DictSchemeAndProjectResponse.DataBean>{
        val url = "${opts.dataPlatform}/dict/scheme/project/query"
        val jsonObject = JSONObject()
        val jsonObject2 = JSONObject()
        val jsonArray = JSONArray()
        jsonArray.fluentAdd(projectId)
        jsonObject2["projectId"] = jsonArray
        jsonObject["criteria"] = jsonObject2
        val postObject = SHttpUtil.postObject<DictSchemeAndProjectResponse>(url, jsonObject)
        if (postObject.result == opts.SUCCESS_CODE) {
            if (!postObject.data.isNullOrEmpty()){
                return postObject.data!!
            }
        }
        return ArrayList()
    } // dictSchemeAndProject()

    /**
     * 集团字典类型
     */
    fun dictGroup(groupId: String): ArrayList<DictPlatformType>{
        val url = "${opts.dataPlatform}/dict/class/query?groupId=$groupId"

        val dictGroupCategoryResponse = SHttpUtil.getObject<DictPlatformTypeResponse>(url)
        if (dictGroupCategoryResponse.result == opts.SUCCESS_CODE) {
            if (!dictGroupCategoryResponse.data.isNullOrEmpty()){
                return dictGroupCategoryResponse.data!!
            }
        }
        return ArrayList()
    } // dictGroup()

    /**
     * 集团下对象信息点
     */
    fun dictGroupInfo(groupId: String,code: String): ArrayList<DictPlatformInfo>{
        val url = "${opts.dataPlatform}/dict/funcid?groupId=$groupId&classCode=$code"
        val dictPlatformInfoResponse = SHttpUtil.getObject<DictPlatformInfoResponse>(url)
        if (dictPlatformInfoResponse.result == opts.SUCCESS_CODE) {
            if (!dictPlatformInfoResponse.data.isNullOrEmpty()){
                return dictPlatformInfoResponse.data!!
            }
        }
        return ArrayList()
    } // Fun dictGroupInfo()


} // Object DictTypeApi