package org.dromara.system.controller.system;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.SysUnitDomainVo;
import org.dromara.system.domain.bo.SysUnitDomainBo;
import org.dromara.system.service.ISysUnitDomainService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 单位域名配置
 * 前端访问路由地址为:/system/unitDomain
 *
 * @author LionLi
 * @date 2024-03-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/unitDomain")
public class SysUnitDomainController extends BaseController {

    private final ISysUnitDomainService sysUnitDomainService;

    /**
     * 查询单位域名配置列表
     */
    @SaCheckPermission("system:unitDomain:list")
    @GetMapping("/list")
    public TableDataInfo<SysUnitDomainVo> list(SysUnitDomainBo bo, PageQuery pageQuery) {
        return sysUnitDomainService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出单位域名配置列表
     */
    @SaCheckPermission("system:unitDomain:export")
    @Log(title = "单位域名配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysUnitDomainBo bo, HttpServletResponse response) {
        List<SysUnitDomainVo> list = sysUnitDomainService.queryList(bo);
        ExcelUtil.exportExcel(list, "单位域名配置", SysUnitDomainVo.class, response);
    }

    /**
     * 获取单位域名配置详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:unitDomain:query")
    @GetMapping("/{id}")
    public R<SysUnitDomainVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(sysUnitDomainService.queryById(id));
    }

    /**
     * 根据域名获取站点配置信息
     * @param domain 域名地址
     * @return
     */
    @PostMapping("/queryByDomain")
    public R<SysUnitDomainVo> queryByDomainName(@NotBlank(message = "域名不能为空") String domain) {
        return R.ok(sysUnitDomainService.queryByDomainName(domain));
    }

    /**
     * 新增单位域名配置
     */
    @SaCheckPermission("system:unitDomain:add")
    @Log(title = "单位域名配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysUnitDomainBo bo) {
        return toAjax(sysUnitDomainService.insertByBo(bo));
    }

    /**
     * 修改单位域名配置
     */
    @SaCheckPermission("system:unitDomain:edit")
    @Log(title = "单位域名配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysUnitDomainBo bo) {
        return toAjax(sysUnitDomainService.updateByBo(bo));
    }

    /**
     * 删除单位域名配置
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:unitDomain:remove")
    @Log(title = "单位域名配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(sysUnitDomainService.deleteWithValidByIds(List.of(ids), true));
    }
}
