package com.persagy.legend.services

import com.alibaba.fastjson.JSONObject
import com.persagy.legend.models.entities.DefClass
import com.persagy.legend.models.entities.LegendIcon
import com.persagy.server.services.DmpBaseService
import com.sybotan.database.SFilter
import com.sybotan.mybatis.SMybatisDao
import com.sybotan.service.SObjectService
import com.sybotan.service.models.enums.SResponseType
import com.sybotan.service.models.requests.SQueryRequest
import com.sybotan.service.models.responses.SCreateResponse
import com.sybotan.service.models.responses.SQueryResponse
import org.springframework.beans.factory.annotation.Autowired
import java.util.*
import kotlin.collections.HashMap
import kotlin.collections.HashSet

/**
 * @author  zy
 */
object LegendIconService : SObjectService<LegendIcon>(SMybatisDao(LegendIcon::class.java)) {

    @Autowired
    private val dmpBaseService: DmpBaseService? = DmpBaseService()

    /**
     * 保存
     *
     * @param legend     图标图例对象
     * @return 返回结果
     */
    fun save(legend: LegendIcon): SCreateResponse<LegendIcon> {
        /** 返回对象 */
        val sCreateResponse = SCreateResponse<LegendIcon>()
        return try {
            /** id 为空赋值 */
            if (legend.id.isNullOrEmpty()) {
                legend.id = UUID.randomUUID().toString().replace("-", "")
            }

            /** 插入数据 */
            val insert = LegendIconService.replace(legend)
            if (!insert){
                sCreateResponse.result = SResponseType.failure
                return sCreateResponse
            }

            /** 状态 */
            /** 清空图例相关的状态 */
            LegendStateService.delete(SFilter.eq("legendId", legend.id!!))
            /** 插入图例相关的状态 */
            if (!legend.stateList.isNullOrEmpty()) {
                val stateList = legend.stateList
                /** 遍历状态 */
                for (state in stateList!!){
                    /** 图例 id */
                    state!!.legendId = legend.id
                    if (state.id.isNullOrEmpty()) {
                        state.id = UUID.randomUUID().toString().replace("-", "")
                    }
                    /** 插入数据 */
                    LegendStateService.replace(state)
                }
            }
            /** 对象列表 */
            sCreateResponse.entityList = arrayListOf(legend)
            /** 标志 */
            sCreateResponse.result = SResponseType.success
            /** 对象 */
            sCreateResponse
        } catch (e: Exception) {
            e.printStackTrace()
            sCreateResponse.result = SResponseType.failure
            sCreateResponse
        }
    }

    override fun onDeleteSuccess(entityList: ArrayList<LegendIcon>) {
        super.onDeleteSuccess(entityList)
        for (entity in entityList) {
            LegendStateService.delete(SFilter.eq("legendId", entity.id!!))
        }
    }

    /**
     * 查询默认状态的图例
     *
     * @param  request  查询条件
     * @return 返回结果
     */
    fun statePageQuery(request: SQueryRequest): SQueryResponse<LegendIcon> {
        val pageQuery = pageQuery(request)
        if (pageQuery.result == SResponseType.success) {
         if (!pageQuery.content.isNullOrEmpty()) {
          for (content in pageQuery.content!!) {
              val entity =
                  LegendStateService.select(SFilter.eq("legendId", content.id!!), SFilter.eq("state", content.state!!))
                      .entity()
              if (entity!=null) {
                  content.stateList = arrayListOf(entity)
              }
            }
          }
        }
        queryDefClass(pageQuery)
        return pageQuery
    }

    fun queryDefClass(pageQuery: SQueryResponse<LegendIcon>) {
        if (pageQuery.result != SResponseType.success) {
            return
        }
        if (pageQuery.content.isNullOrEmpty()) {
            return
        }
        var classCodes:HashSet<String> = HashSet<String>()
        for (content in pageQuery.content!!) {
            //查询中台类型数据，2021.10.25日中心中台整合调整
            if (content.classCode.isNullOrBlank()){
                continue
            }
            classCodes.add(content.classCode!!)
        }
        var classCodeMap:HashMap<String, DefClass> = HashMap<String, DefClass>()
        if (!classCodes.isNullOrEmpty()){
            val param = JSONObject()
            val criteria = JSONObject()
            criteria["code"] = classCodes
            param["criteria"] = criteria
            val dictTypes =dmpBaseService!!.queryDictType(param)
            /*   val defClass =
                   defClassService.select(SFilter.eq("projectId", projectId), SFilter.eq("code", content.classCode!!)).entity()*/
            if (dictTypes!=null && dictTypes.size>0) {
                for (dictType in dictTypes){
                    classCodeMap.put(dictType.code!!,dictType)
                }
            }
        }
        for (content in pageQuery.content!!) {
            content.classInfo=classCodeMap.get(content.classCode)
        }
    }

    /**
     * 查询默认状态的图例
     *
     * @param  request  查询条件
     * @return 返回结果
     */
    fun pageQuerys(request: SQueryRequest): SQueryResponse<LegendIcon> {
        val pageQuery = pageQuery(request)
        queryDefClass(pageQuery)
        return pageQuery
    }
}