package com.cclotus.cms.controller.admin

import com.cclotus.cms.domain.CmsTerm
import com.cclotus.cms.service.CmsTermService
import com.cclotus.cms.vo.CmsTermForm
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.validation.annotation.Validated
import org.springframework.web.bind.annotation.*

@Suppress("NAME_SHADOWING")
@RestController
@RequestMapping("/cms")
class CmsTermController : BaseController() {

    @Autowired
    lateinit var cmsTermService: CmsTermService

    /**
     * 查询分类列表
     */
    @GetMapping("/{taxonomy:wxAppMenu|menu|catalog|tag}/list")
    fun list(
        @PathVariable("taxonomy") taxonomy: String,
        cmsTerm: CmsTerm,
        paging: Boolean?,
        pageParam: PageParam?,
    ): TableDataInfo {
        val pageParam = if (paging != null && !paging) null else pageParam
        cmsTerm.taxonomy = taxonomy
        return cmsTermService.list(cmsTerm, pageParam)
    }

    /**
     * 根据分类别名或Id获取分类详情
     *
     * @param taxonomy 分类系统
     * @param term 分类标识
     * @return 分类详细信息
     */
    @GetMapping("/{taxonomy:wxAppMenu|menu|catalog|tag}/{term}")
    fun detail(
        @PathVariable("taxonomy") taxonomy: String,
        @PathVariable("term") term: String,
    ): AjaxResult {
        return AjaxResult.success(cmsTermService.detail(taxonomy, term))
    }

    /**
     * 新增分类
     */
    @Log(title = "分类管理", businessType = BusinessType.INSERT)
    @PostMapping("/{taxonomy:wxAppMenu|menu|catalog|tag}")
    fun add(
        @PathVariable("taxonomy") taxonomy: String,
        @Validated @RequestBody cmsTerm: CmsTermForm,
    ): AjaxResult {
        cmsTerm.taxonomy = taxonomy
        return toAjax(cmsTermService.create(cmsTerm))
    }

    /**
     * 修改分类
     */
    @Log(title = "分类管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{taxonomy:wxAppMenu|menu|catalog|tag}")
    fun edit(
        @PathVariable("taxonomy") taxonomy: String,
        @RequestBody cmsTerm: CmsTermForm,
    ): AjaxResult {
        cmsTerm.taxonomy = taxonomy
        return toAjax(cmsTermService.update(cmsTerm))
    }

    /**
     * 删除分类
     */
    @Log(title = "分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{taxonomy:wxAppMenu|menu|catalog|tag}/{termIds}")
    fun remove(
        @PathVariable("taxonomy") taxonomy: String,
        @PathVariable termIds: Array<Int>,
    ): AjaxResult {
        return toAjax(cmsTermService.deleteByIds(termIds))
    }
}
