package org.dromara.work.controller;

import java.util.List;
import java.util.Map;

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.work.domain.vo.TzSharedAccountVo;
import org.dromara.work.domain.bo.TzSharedAccountBo;
import org.dromara.work.service.ITzSharedAccountService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 共享账号
 *
 * @author Maosw
 * @date 2025-05-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/sharedAccount")
public class TzSharedAccountController extends BaseController {

    private final ITzSharedAccountService tzSharedAccountService;

    /**
     * 查询共享账号列表
     */
    @SaCheckPermission("work:sharedAccount:list")
    @GetMapping("/list")
    public TableDataInfo<TzSharedAccountVo> list(TzSharedAccountBo bo, PageQuery pageQuery) {
        return tzSharedAccountService.queryPageList(bo, pageQuery);
    }

    /**
     * 共享账号统计
     */
    @SaCheckPermission("work:sharedAccount:list")
    @GetMapping("/count")
    public R<Map<String, Object>> count(TzSharedAccountBo bo) {
        return R.ok(tzSharedAccountService.queryCount(bo));
    }
    /**
     * 导出共享账号列表
     */
    @SaCheckPermission("work:sharedAccount:export")
    @Log(title = "共享账号", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TzSharedAccountBo bo, HttpServletResponse response) {
        List<TzSharedAccountVo> list = tzSharedAccountService.queryList(bo);
        ExcelUtil.exportExcel(list, "共享账号", TzSharedAccountVo.class, response);
    }

    /**
     * 获取共享账号详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("work:sharedAccount:query")
    @GetMapping("/{id}")
    public R<TzSharedAccountVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tzSharedAccountService.queryById(id));
    }

    /**
     * 新增共享账号
     */
    @SaCheckPermission("work:sharedAccount:add")
    @Log(title = "共享账号", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TzSharedAccountBo bo) {
        return toAjax(tzSharedAccountService.insertByBo(bo));
    }

    /**
     * 修改共享账号
     */
    @SaCheckPermission("work:sharedAccount:edit")
    @Log(title = "共享账号", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TzSharedAccountBo bo) {
        return toAjax(tzSharedAccountService.updateByBo(bo));
    }

    /**
     * 删除共享账号
     *
     * @param ids 主键串
     */
    @SaCheckPermission("work:sharedAccount:remove")
    @Log(title = "共享账号", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tzSharedAccountService.deleteWithValidByIds(List.of(ids), true));
    }
}
