package org.dromara.zhishu.controller;

import java.util.HashMap;
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.zhishu.domain.vo.UserAccountVo;
import org.dromara.zhishu.domain.bo.UserAccountBo;
import org.dromara.zhishu.service.IUserAccountService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 账号管理
 *
 * @author yxy
 * @date 2026-02-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zhishu/userAccount")
public class UserAccountController extends BaseController {

    private final IUserAccountService userAccountService;

    /**
     * 查询账号管理列表
     */
    @SaCheckPermission("zhishu:userAccount:list")
    @GetMapping("/list")
    public TableDataInfo<UserAccountVo> list(UserAccountBo bo, PageQuery pageQuery) {
        return userAccountService.queryPageList(bo, pageQuery);
    }

    @SaCheckPermission("zhishu:userAccount:list")
    @GetMapping("/listNoPage")
    public Map<String,List<UserAccountVo>> listNoPage(UserAccountBo bo) {
        Map<String,List<UserAccountVo>> map = new HashMap<>();
        map.put("adminList",userAccountService.queryListAdmin(bo));
        map.put("userList",userAccountService.queryList(bo));
        return map;
    }

    /**
     * 导出账号管理列表
     */
    @SaCheckPermission("zhishu:userAccount:export")
    @Log(title = "账号管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UserAccountBo bo, HttpServletResponse response) {
        List<UserAccountVo> list = userAccountService.queryList(bo);
        ExcelUtil.exportExcel(list, "账号管理", UserAccountVo.class, response);
    }

    /**
     * 获取账号管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("zhishu:userAccount:query")
    @GetMapping("/{id}")
    public R<UserAccountVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(userAccountService.queryById(id));
    }

    /**
     * 新增账号管理
     */
    @SaCheckPermission("zhishu:userAccount:add")
    @Log(title = "账号管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody UserAccountBo bo) {
        return toAjax(userAccountService.insertByBo(bo));
    }

    /**
     * 修改账号管理
     */
    @SaCheckPermission("zhishu:userAccount:edit")
    @Log(title = "账号管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserAccountBo bo) {
        return toAjax(userAccountService.updateByBo(bo));
    }

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