package com.persagy.server.controllers.relation_calc

import com.persagy.server.datacenter.models.entities.graphtype.block.MepSourceTypeEntity
import com.persagy.server.services.relation_calc.MepSystemCalcService
import com.persagy.database.SFilter
import com.persagy.service.SPageContext
import com.persagy.service.models.requests.SCreateRequest
import com.persagy.service.models.requests.SQueryRequest
import com.persagy.service.models.responses.SCreateResponse
import com.persagy.service.models.responses.SQueryResponse
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController


/**
 * 机电管网系统关系计算
 *
 * @author jxing
 */
@Api(tags = ["014、关系计算-管网计算"])
@RestController
@RequestMapping("/mep-system-calc")
class MepSystemCalcController {

    @Autowired
    lateinit var mepSystemCalc: MepSystemCalcService

    /**
    * 添加源设备
    */
    @ApiOperation(value = "添加源设备", notes = "")
    @PostMapping(value = ["/source/add"])
    fun query(@RequestBody entity: SCreateRequest<MepSourceTypeEntity>): SCreateResponse<MepSourceTypeEntity> {
        val projectId = SPageContext.getHeader("ProjectId").toString()
        var calcName : String? = null
        entity.content.forEach {
            if(it.projectId.isNullOrBlank())
                it.projectId = projectId
            calcName = it.calcName
        }
        // 删除以前卡片的源设备记录
        mepSystemCalc.mepSourceTypeService.delete(SFilter.and(SFilter.eq("projectId", projectId), SFilter.eq("calcName", calcName!!)))
        return mepSystemCalc.mepSourceTypeService.createList(entity)
    } // Fun

    /**
     * 添加关系
     */
    @ApiOperation(value = "查看源设备", notes = "")
    @PostMapping(value = ["/source/query"])
    fun addRels(@RequestBody request: SQueryRequest): SQueryResponse<MepSourceTypeEntity> {
        return mepSystemCalc.mepSourceTypeService.pageQuery(request)
    } // Fun
}