package com.sf.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.sf.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sf.common.annotation.Log;
import com.sf.common.core.controller.BaseController;
import com.sf.common.core.domain.AjaxResult;
import com.sf.common.enums.BusinessType;
import com.sf.system.domain.UserMember;
import com.sf.system.service.IUserMemberService;
import com.sf.common.utils.poi.ExcelUtil;
import com.sf.common.core.page.TableDataInfo;

/**
 * 会员Controller
 * 
 * @author ztzh
 * @date 2024-04-16
 */
@RestController
@RequestMapping("/system/member")
public class UserMemberController extends BaseController
{
    @Autowired
    private IUserMemberService userMemberService;

    /**
     * 查询会员列表
     */
    @PreAuthorize("@ss.hasPermi('system:member:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserMember userMember)
    {
        startPage();
        List<UserMember> list = userMemberService.selectUserMemberList(userMember);
        return getDataTable(list);
    }

    /**
     * 导出会员列表
     */
    @PreAuthorize("@ss.hasPermi('system:member:export')")
    @Log(title = "会员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserMember userMember)
    {
        List<UserMember> list = userMemberService.selectUserMemberList(userMember);
        ExcelUtil<UserMember> util = new ExcelUtil<UserMember>(UserMember.class);
        util.exportExcel(response, list, "会员数据");
    }

    /**
     * 获取会员详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:member:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userMemberService.selectUserMemberById(id));
    }

    /**
     * 获取会员详细信息,通过用户id
     * 当前用户
     */
    @GetMapping(value = "/currentUser")
    public AjaxResult getInfoByUser()
    {
        Long userId = SecurityUtils.getUserId();
        return success(userMemberService.selectUserMemberByUserId(userId));
    }

    /**
     * 新增会员
     */
    @PreAuthorize("@ss.hasPermi('system:member:add')")
    @Log(title = "会员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserMember userMember)
    {
        return toAjax(userMemberService.insertUserMember(userMember));
    }

    /**
     * 修改会员
     */
    @PreAuthorize("@ss.hasPermi('system:member:edit')")
    @Log(title = "会员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserMember userMember)
    {
        return toAjax(userMemberService.updateUserMember(userMember));
    }

    /**
     * 删除会员
     */
    @PreAuthorize("@ss.hasPermi('system:member:remove')")
    @Log(title = "会员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userMemberService.deleteUserMemberByIds(ids));
    }
}
