package com.cclotus.cms.controller.admin

import com.cclotus.cms.service.CmsTermRelationshipsService
import com.ruoyi.common.annotation.Log
import com.ruoyi.common.core.controller.BaseController
import com.ruoyi.common.core.domain.AjaxResult
import com.ruoyi.common.core.page.TableDataInfo
import com.ruoyi.common.enums.BusinessType
import com.ruoyi.common.reqParam.PageParam
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*


@RestController
@RequestMapping("/cms/relation")
class CmsTermRelationshipsController : BaseController() {

    @Autowired
    lateinit var cmsTermRelationshipsService: CmsTermRelationshipsService

    /**
     * 查询termId绑定的对象列表
     */
    @GetMapping(path = ["/{termId}/objects"])
    fun listObjects(
        @PathVariable("termId") termId: Int,
        pageParam: PageParam,
    ): TableDataInfo {
        return getDataTable(cmsTermRelationshipsService.getBindingObjectsByTermId(termId, pageParam))
    }

    /**
     * 为内容绑定一组分类，如：一篇文章添加多个标签
     */
    @Log(title = "关联关系管理", businessType = BusinessType.INSERT)
    @PostMapping("/p/{postId}/t/{termIds}")
    fun bindObjectToTerms(
        @PathVariable("postId") postId: Int,
        @PathVariable("termIds") termIds: Array<Int>,
    ): AjaxResult {
        return toAjax(cmsTermRelationshipsService.bindPostToTerms(postId, termIds))
    }

    /**
     * 为分类绑定一组对象，如：一个栏目下添加多篇文章
     */
    @Log(title = "关联关系管理", businessType = BusinessType.INSERT)
    @PostMapping("/t/{termId}/o/{objectIds}")
    fun bindTermToObjects(
        @PathVariable("termId") termId: Int,
        @PathVariable("objectIds") objectIds: Array<Int>,
    ): AjaxResult {
        return toAjax(cmsTermRelationshipsService.bindTermToObjects(termId, objectIds))
    }

    /**
     * 从关联关系中，删除与termId关联的一组objectIds
     */
    @Log(title = "关联关系管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/t/{termId}/o/{objectIds}")
    fun removeObjects(
        @PathVariable("termId") termId: Int,
        @PathVariable("objectIds") objectIds: Array<Int>,
    ): AjaxResult {
        return toAjax(cmsTermRelationshipsService.deleteObjectIds(termId, objectIds))
    }

    /**
     * 从关联关系中，删除与objectId关联的一组termIds
     */
    @Log(title = "关联关系管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/o/{objectId}/t/{termIds}")
    fun removeTerms(
        @PathVariable("objectId") objectId: Int,
        @PathVariable("termIds") termIds: Array<Int>,
    ): AjaxResult {
        return toAjax(cmsTermRelationshipsService.deleteTermIds(objectId, termIds))
    }
}
