package com.persagy.server.syn.controllers.obj

import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.datacenter.models.entities.objects.Building
import com.persagy.server.syn.models.api.obj.BuildingApi
import com.persagy.service.SObjectService
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.responses.SBaseResponse
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

/**
 * 数据中心 - 数据中台
 *
 * @author wx  <zhangweixin@sagacloud.com>
 * @date  2020/10/22 21:39
 */
@Api(tags = ["0003、对象-数据中心到中台"])
@RestController
@RequestMapping("/sync")
class ObjectToFromController {
    companion object {
        /** 日志 */
        private val logger = LoggerFactory.getLogger(ObjectToFromController::class.java)
    } // Companion object
    /** 数据中心建筑服务 */
    private val buildingService = SObjectService(SMybatisDao(Building::class.java))

    /**
     * 建筑同步
     *
     * @param projectId     项目 id
     */
    @ApiOperation(value="建筑-数据中心同步到数据中台", notes="")
    @PostMapping("/building")
    fun buildingSynCreate(projectId: String): SBaseResponse {
        try {
            val buildingList = buildingService.select(SFilter.eq("projectId", projectId)).exec()
            if (buildingList.size>0){
                for (building in buildingList){
                    val createBuilding = BuildingApi.createBuilding(projectId, building)
                    if (!createBuilding){
                        logger.debug("建筑创建失败：${building}")
                        val updateBuilding = BuildingApi.updateBuilding(projectId, building)
                        if (!updateBuilding){
                            logger.debug("建筑更新失败：${building}")
                        }
                    }
                }
            }
            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure,e.message!!)
        }
    }




}