package org.dromara.work.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
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.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
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.dromara.work.domain.bo.TzUserBo;
import org.dromara.work.domain.vo.TzUserVo;
import org.dromara.work.service.ITzUserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户
 *
 * @author Lion Li
 * @date 2024-07-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mall/user")
public class TzUserController extends BaseController {

    private final ITzUserService tzUserService;

    /**
     * 查询用户列表
     */
    @SaCheckPermission("mall:user:list")
    @GetMapping("/list")
    public TableDataInfo<TzUserVo> list(TzUserBo bo, PageQuery pageQuery) {
        return tzUserService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户列表
     */
    @SaCheckPermission("mall:user:export")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TzUserBo bo, HttpServletResponse response) {
        List<TzUserVo> list = tzUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户", TzUserVo.class, response);
    }

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

    /**
     * 开启设计师开关
     *
     * @param userPhone 主键
     */
    @SaCheckPermission("mall:user:switch")
    @PostMapping("/switch")
    @Parameters({
        @Parameter(name = "userPhone", description = "手机号码", required = true),
        @Parameter(name = "status", description = "状态 1-开启  2-关闭", required = true)
    })
    public R<Void> getInfo(@RequestParam(value = "userPhone") String userPhone, @RequestParam(value = "status") Integer status) {
        return toAjax(tzUserService.queryByUserPhone(userPhone,status));
    }


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

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

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