package com.yingpai.miniapp.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yingpai.miniapp.domain.bo.MiniappUserBo;
import com.yingpai.miniapp.domain.vo.MiniappUserVo;
import com.yingpai.miniapp.service.IMiniappUserService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 小程序用户管理
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/miniapp/user")
public class MiniappUserController extends BaseController {

    private final IMiniappUserService miniappUserService;

    /**
     * 查询小程序用户列表
     */
    @SaCheckPermission("miniapp:user:list")
    @GetMapping("/list")
    public TableDataInfo<MiniappUserVo> list(MiniappUserBo bo, PageQuery pageQuery) {
        return miniappUserService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出小程序用户列表
     */
    @SaCheckPermission("miniapp:user:export")
    @Log(title = "小程序用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MiniappUserBo bo, HttpServletResponse response) {
        List<MiniappUserVo> list = miniappUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "小程序用户数据", MiniappUserVo.class, response);
    }

    /**
     * 获取小程序用户详细信息
     */
    @SaCheckPermission("miniapp:user:query")
    @GetMapping("/{id}")
    public R<MiniappUserVo> getInfo(@PathVariable Long id) {
        return R.ok(miniappUserService.queryById(id));
    }

    /**
     * 新增小程序用户
     */
    @SaCheckPermission("miniapp:user:add")
    @Log(title = "小程序用户", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody MiniappUserBo bo) {
        return toAjax(miniappUserService.insertUser(bo));
    }

    /**
     * 修改小程序用户
     */
    @SaCheckPermission("miniapp:user:edit")
    @Log(title = "小程序用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody MiniappUserBo bo) {
        return toAjax(miniappUserService.updateUser(bo));
    }

    /**
     * 删除小程序用户
     */
    @SaCheckPermission("miniapp:user:remove")
    @Log(title = "小程序用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> remove(@PathVariable Long id) {
        return toAjax(miniappUserService.deleteUser(id));
    }

    /**
     * 修改用户状态
     */
    @SaCheckPermission("miniapp:user:edit")
    @Log(title = "小程序用户", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@RequestParam Long id, @RequestParam Integer status) {
        return toAjax(miniappUserService.updateStatus(id, status));
    }
}
