/*
 * *********************************************************************************************************************
 *
 *          !!
 *        .F88X
 *        X8888Y
 *      .}888888N;
 *        i888888N;        .:!              .I$WI:
 *          R888888I      .'N88~            i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8&
 *          .R888888I    .;N8888~          .X8'  "8I.!,/8"  !%NY8`"8I8~~8>,88I
 *            +888888N;  .8888888Y                                  "&&8Y.}8,
 *            ./888888N;  .R888888Y        .'}~    .>}'.`+>  i}!    "i'  +/'  .'i~  !11,.:">,  .~]!  .i}i
 *              ~888888%:  .I888888l      .]88~`1/iY88Ii+1'.R$8$8]"888888888>  Y8$  W8E  X8E  W8888'188Il}Y88$*
 *              18888888    E8888881    .]W%8$`R8X'&8%++N8i,8N%N8+l8%`  .}8N:.R$RE%N88N%N$K$R  188,FE$8%~Y88I
 *            .E888888I  .i8888888'      .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~
 *            8888888I  .,N888888~        ~88i"8W,!N8*.I88.}888%F,i$88"F88"  888:E8X.>88!i88>`888*.}Fl1]*}1YKi'
 *          i888888N'      I888Y          ]88;/EX*IFKFK88X  K8R  .l8W  88Y  ~88}'88E&%8W.X8N``]88!.$8K  .:W8I
 *        .i888888N;        I8Y          .&8$  .X88!  i881.:%888>I88  ;88]  +88+.';;;;:.Y88X  18N.,88l  .+88/
 *      .:R888888I
 *      .&888888I                                          Copyright (c) 2009-2020.  博锐尚格科技股份有限公司
 *        ~8888'
 *        .!88~                                                                     All rights reserved.
 *
 * *********************************************************************************************************************
 */

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

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
import org.slf4j.LoggerFactory

/**
 * 字典API
 *
 * @author  wx
 * @date  2020/9/11 15:04
 */
object DictTypeApi {
    // 日志
    private val logger = LoggerFactory.getLogger(DictTypeApi::class.java)
    /** 配置实例 */
    private 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 dictTypeHttp()


    /**
     * 查询集团
     */
    fun groupQuery():ArrayList<Group> {
        var data = ArrayList<Group>()
        try {
            val url = "${opts.dataPlatform}/org/group/query"
            val jsonObject = JSONObject()
            val jsonObject2 = JSONObject()
            jsonObject["criteria"] = jsonObject2
            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()

    /**
     * 查询字典项目
     *
     * @param code  集团编码
     * @return 项目对象列表
     */
    fun dictProjectQuery(code: String): ArrayList<DictProject>{
        val url = "${opts.dataPlatform}/org/project/query?userId=123456&groupCode=$code"
        val jsonObject = JSONObject()
        val jsonObject2 = JSONObject()
        jsonObject2["groupCode"] = 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}/rwd/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()


    /**
     * 对象详细信息点
     *
     * @param   category    对象类型
     * @param   type        平台或集团
     */
    fun dictPlatformInfo(category: String,type: String): ArrayList<DictPlatformInfo>{
        val url = "${opts.dataPlatform}/rwd/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()

    /**
     * 获取专业
     *
     * @param type  类型
     * @return 专业列表
     */
    fun dictMajor(type: String): ArrayList<DictMajorResponse.DataBean>{
        val url = "${opts.dataPlatform}/dic/dt/data/query?groupCode=WD&userId=123456"
        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()

    /**
     * 集团字典类型
     *
     * @param groupId   集团id
     */
    fun dictGroup(groupId: String): ArrayList<DictPlatformType>{
        //type=group&groupCode=wd
        val url = "${opts.dataPlatform}/rwd/def/class?groupCode=$groupId"
        val jsonObject = JSONObject()
        val jsonObject2 = JSONObject()
        jsonObject["criteria"] = jsonObject2
        val dictGroupCategoryResponse = SHttpUtil.postObject<DictPlatformTypeResponse>(url,jsonObject)
        if (dictGroupCategoryResponse.result == opts.SUCCESS_CODE) {
            if (!dictGroupCategoryResponse.data.isNullOrEmpty()){
                return dictGroupCategoryResponse.data!!
            }
        }
        return ArrayList()
    } // Fun dictGroup()

    /**
     * 集团下对象信息点
     */
    fun dictGroupInfo(groupId: String,code: String): ArrayList<DictPlatformInfo>{
        val url = "${opts.dataPlatform}/rwd/def/funcid?groupCode=$groupId"
        val jsonObject = JSONObject()
        val jsonObject2 = JSONObject()
        jsonObject2["classCode"] = code
        jsonObject["criteria"] = jsonObject2
        val dictPlatformInfoResponse = SHttpUtil.postObject<DictPlatformInfoResponse>(url,jsonObject)
        if (dictPlatformInfoResponse.result == opts.SUCCESS_CODE) {
            if (!dictPlatformInfoResponse.data.isNullOrEmpty()){
                return dictPlatformInfoResponse.data!!
            }
        }
        return ArrayList()
    } // Fun dictGroupInfo()

    /**
     * 项目字典类型
     *
     * @param groupCode   集团id
     */
    fun dictProject(groupCode: String,projectId: String): ArrayList<DictPlatformType>{
        //type=group&groupCode=wd
        val url = "${opts.dataPlatform}/rwd/def/class?groupCode=$groupCode&projectId=$projectId"
        val jsonObject = JSONObject()
        val jsonObject2 = JSONObject()
        jsonObject["criteria"] = jsonObject2
        logger.debug("url = $url")
        logger.debug("boby = ${jsonObject.toJson()}")
        val dictGroupCategoryResponse = SHttpUtil.postObject<DictPlatformTypeResponse>(url,jsonObject)
        if (dictGroupCategoryResponse.result == opts.SUCCESS_CODE) {
            if (!dictGroupCategoryResponse.data.isNullOrEmpty()){
                return dictGroupCategoryResponse.data!!
            }
        }
        return ArrayList()
    } // Fun dictGroup()

    /**
     * 集团下对象信息点
     */
    fun dictProjectInfo(groupId: String,code: String,projectId: String): ArrayList<DictPlatformInfo>{
        val url = "${opts.dataPlatform}/rwd/def/funcid?groupCode=$groupId&projectId=$projectId"
        val jsonObject = JSONObject()
        val jsonObject2 = JSONObject()
        jsonObject2["classCode"] = code
        jsonObject["criteria"] = jsonObject2
        val dictPlatformInfoResponse = SHttpUtil.postObject<DictPlatformInfoResponse>(url,jsonObject)
        if (dictPlatformInfoResponse.result == opts.SUCCESS_CODE) {
            if (!dictPlatformInfoResponse.data.isNullOrEmpty()){
                return dictPlatformInfoResponse.data!!
            }
        }
        return ArrayList()
    } // Fun dictGroupInfo()



} // Object DictTypeApi