package org.eco.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.eco.system.domain.bo.SysClientBo;
import org.eco.system.domain.vo.SysClientVo;
import org.eco.common.core.core.domain.CommonResult;
import org.eco.common.core.core.page.PageResult;
import org.eco.common.excel.utils.ExcelUtil;
import org.eco.common.log.annotation.Log;
import org.eco.common.log.enums.BusinessType;
import org.eco.common.web.annotation.RepeatSubmit;
import org.eco.system.service.ISysClientService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 系统授权表 控制层。
 *
 * @author wgk
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/client")
public class SysClientController {

    @Resource
    private ISysClientService sysClientService;

    /**
     * 查询客户端管理列表
     */
    @SaCheckPermission("system:client:list")
    @GetMapping("/list")
    public CommonResult<PageResult<SysClientVo>> list(SysClientBo sysClientBo) {
        return CommonResult.success(sysClientService.selectPage(sysClientBo));
    }

    /**
     * 导出客户端管理列表
     */
    @SaCheckPermission("system:client:export")
    @Log(title = "客户端管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysClientBo sysClientBo, HttpServletResponse response) {
        List<SysClientVo> list = sysClientService.selectList(sysClientBo);
        ExcelUtil.exportExcel(list, "客户端管理", SysClientVo.class, response);
    }

    /**
     * 获取客户端管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:client:query")
    @GetMapping("/{id}")
    public CommonResult<SysClientVo> getInfo(@NotNull(message = "主键不能为空")
                                             @PathVariable Long id) {
        return CommonResult.success(sysClientService.selectById(id));
    }

    /**
     * 新增客户端管理
     */
    @SaCheckPermission("system:client:add")
    @Log(title = "客户端管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public CommonResult<Void> add(@Validated @RequestBody SysClientBo sysClientBo) {
        boolean inserted = sysClientService.insert(sysClientBo);
        if (!inserted) {
            return CommonResult.fail("新增客户端管理记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改客户端管理
     */
    @SaCheckPermission("system:client:edit")
    @Log(title = "客户端管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public CommonResult<Void> edit(@Validated @RequestBody SysClientBo sysClientBo) {
        boolean updated = sysClientService.update(sysClientBo);
        if (!updated) {
            return CommonResult.fail("修改客户端管理记录失败!");
        }
        return CommonResult.success();
    }

    /**
     * 状态修改
     */
    @SaCheckPermission("system:client:edit")
    @Log(title = "客户端管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public CommonResult<Void> changeStatus(@RequestBody SysClientBo sysClientBo) {
        boolean updated = sysClientService.updateStatus(sysClientBo);
        if (!updated) {
            return CommonResult.fail("修改客户端管理状态失败!");
        }
        return CommonResult.success();
    }

    /**
     * 删除客户端管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:client:remove")
    @Log(title = "客户端管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public CommonResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                     @PathVariable Long[] ids) {
        boolean deleted = sysClientService.deleteByIds(List.of(ids));
        if (!deleted) {
            return CommonResult.fail("删除客户端管理记录失败!");
        }
        return CommonResult.success();
    }
}
