package com.fuyuanshen.app.controller;

import java.util.List;

import com.fuyuanshen.common.core.enums.UserType;
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 com.fuyuanshen.common.idempotent.annotation.RepeatSubmit;
import com.fuyuanshen.common.log.annotation.Log;
import com.fuyuanshen.common.web.core.BaseController;
import com.fuyuanshen.common.mybatis.core.page.PageQuery;
import com.fuyuanshen.common.core.domain.R;
import com.fuyuanshen.common.core.validate.AddGroup;
import com.fuyuanshen.common.core.validate.EditGroup;
import com.fuyuanshen.common.log.enums.BusinessType;
import com.fuyuanshen.common.excel.utils.ExcelUtil;
import com.fuyuanshen.app.domain.vo.AppUserVo;
import com.fuyuanshen.app.domain.bo.AppUserBo;
import com.fuyuanshen.app.service.IAppUserService;
import com.fuyuanshen.common.mybatis.core.page.TableDataInfo;

/**
 * APP用户信息
 *
 * @author Lion Li
 * @date 2025-06-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/user")
public class AppUserController extends BaseController {

    private final IAppUserService appUserService;


    /**
     * 查询APP用户信息列表
     */
//    @SaCheckPermission("app:user:list")
    @GetMapping("/list")
    public TableDataInfo<AppUserVo> list(AppUserBo bo, PageQuery pageQuery) {
        bo.setUserType(UserType.APP_USER.getUserType());
        return appUserService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出APP用户信息列表
     */
    @SaCheckPermission("app:user:export")
    @Log(title = "APP用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppUserBo bo, HttpServletResponse response) {
        List<AppUserVo> list = appUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "APP用户信息", AppUserVo.class, response);
    }

    /**
     * 获取APP用户信息详细信息
     *
     * @param userId 主键
     */
    @SaCheckPermission("app:user:query")
    @GetMapping("/{userId}")
    public R<AppUserVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long userId) {
        return R.ok(appUserService.queryById(userId));
    }

    /**
     * 新增APP用户信息
     */
    @SaCheckPermission("app:user:add")
    @Log(title = "APP用户信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppUserBo bo) {
        return toAjax(appUserService.insertByBo(bo));
    }

    /**
     * 修改APP用户信息
     */
    @SaCheckPermission("app:user:edit")
    @Log(title = "APP用户信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppUserBo bo) {
        return toAjax(appUserService.updateByBo(bo));
    }

    /**
     * 删除APP用户信息
     *
     * @param userIds 主键串
     */
    @SaCheckPermission("app:user:remove")
    @Log(title = "APP用户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] userIds) {
        return toAjax(appUserService.deleteWithValidByIds(List.of(userIds), true));
    }
}
