package com.persagy.server.services.graphtype

import com.persagy.server.datacenter.models.entities.graphtype.GraphicType
import com.persagy.server.datacenter.models.entities.graphtype.RelationType
import com.persagy.server.datacenter.models.entities.graphtype.RelationTypeProject
import com.persagy.server.services.base.RService
import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.service.SObjectService
import com.persagy.service.SPageContext
import com.persagy.service.models.SCascadeQuery
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.requests.SQueryRequest
import com.persagy.service.models.responses.SQueryResponse
import org.slf4j.LoggerFactory
import java.util.*

/**
 * 查询总览
 *
 * @author  邓婷婷
 */
object GraphicTypeService : RService<GraphicType>(SMybatisDao(GraphicType::class.java))  {
    // 日志
    private val logger = LoggerFactory.getLogger(GraphicTypeService::class.java)
    /**  */
    val service = SObjectService(RelationTypeProject::class.java)
    /**
     * 查询关系总览
     */
    fun overview(): SQueryResponse<GraphicType> {
        return try {
            val projectId = SPageContext.getHeader("ProjectId")
            val request = SQueryRequest()
            request.pageSize = 1000
            request.orders = "sort"
            request.filters = "parentId isnull"
            request.cascade = ArrayList<SCascadeQuery>()

            val childType = SCascadeQuery()
            childType.name = "childGraphicTypeList"
            childType.cascade = ArrayList<SCascadeQuery>()
            childType.orders = "sort,GraphTypeCode"
            request.cascade!!.add(childType)

//            val relationType = SCascadeQuery()
//            relationType.name = "relationTypeProjectList"
//            relationType.filters = "projectId = '$projectId'"
//            childType.cascade!!.add(relationType)

            copyRelationType(projectId!!)

            val pageQuery = GraphicTypeService.pageQuery(request)
            if (!pageQuery.content.isNullOrEmpty()){
                for ( content in pageQuery.content!!){
                    if (!content.childGraphicTypeList.isNullOrEmpty()){
                        val childGraphicTypeList = content.childGraphicTypeList
                        for (childGraphicType in childGraphicTypeList!!){
                            val relationTypeProjectList = service.select(SFilter.eq("projectId", projectId), SFilter.eq("graphicId", childGraphicType.graphTypeId!!)).order("RelationTypeCode").exec()
                            if (relationTypeProjectList.size>0){
                                childGraphicType.relationTypeProjectList = relationTypeProjectList
                            }
                        }
                    }
                }
            }

            pageQuery
        } catch (e: Exception) {
            e.printStackTrace()
            SQueryResponse(SResponseType.failure, e.message!!)
        }
    } // Function overview()

    /**
     * 复制关系类型表
     *
     * @param   projectId       项目ID
     */
    private fun copyRelationType(projectId: String) {
        val relationTypeProjectService = SObjectService(SMybatisDao(RelationTypeProject::class.java))
        val relationTypeService = SObjectService(SMybatisDao(RelationType::class.java))
        val n = relationTypeProjectService.select(SFilter.eq("projectId", projectId)).execCount()

        if (n == 0L) {//需要录入数据
            val requests = SQueryRequest()
            val relationTypeList = relationTypeService.selectAll().exec()
            logger.debug("數據長度= ${relationTypeList.size}")
            for (relationType in relationTypeList) {
                try {
                    val relationTypeProject = RelationTypeProject()
                    relationTypeProject.id = UUID.randomUUID().toString().replace("-", "")
                    relationTypeProject.relationTypeName = relationType.relationTypeName
                    relationTypeProject.relationTypeCode = relationType.relationTypeCode
                    relationTypeProject.graphicId = relationType.graphicId
                    relationTypeProject.note = relationType.note
                    relationTypeProject.conneObject = relationType.conneObject
                    relationTypeProject.isSource = relationType.isSource
                    relationTypeProject.isAutomatic = relationType.isAutomatic
                    relationTypeProject.manual = relationType.manual
                    relationTypeProject.prompting = relationType.prompting
                    relationTypeProject.tableName = relationType.tableName
                    relationTypeProject.isThorough = relationType.isThorough
                    relationTypeProject.relationType = relationType.relationType;
                    relationTypeProject.zoneType = relationType.zoneType;
                    relationTypeProject.projectId = projectId
                    relationTypeProject.computationalState = 1
                    relationTypeProject.manualQuery = relationType.manualQuery
                    relationTypeProject.mainType = relationType.mainType
                    relationTypeProject.fromType = relationType.fromType
                    relationTypeProject.addRel = relationType.addRel
                    relationTypeProject.delRel = relationType.delRel
                    relationTypeProject.relManualType = relationType.relManualType
                    relationTypeProject.mainObject = relationType.mainObject
                    relationTypeProject.fromObject = relationType.fromObject
                    relationTypeProject.mepSystemType = relationType.mepSystemType
                    relationTypeProjectService.insert(relationTypeProject)


//                    try {
//                        val relationTypeProjectService = SObjectService(SMybatisDao(RelationTypeProject::class.java))
//                        val relationType = relationTypeProjectService.select(SFilter.eq("projectId", projectId!!),SFilter.eq("RelationType", relationAddRequest.relType!!)).entity()
//                        if (relationType!=null){
//                            relationType.computingTime = time()
//                            relationTypeProjectService.update(relationType)
//                        }
//                    } catch (e: Exception) {
//                        e.printStackTrace()
//                    }

                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
        }
    } // Function copyRelationType()
} // Class GraphicTypeService