package com.ruoyi.member.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.member.domain.MemberUser;
import com.ruoyi.member.service.IMemberUserService;
import com.ruoyi.member.vo.MemberUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 会员Controller
 *
 * @author pqz
 * @date 2025-03-17
 */
@RestController
@RequestMapping("/member/member")
public class MemberUserController extends BaseController {
    @Autowired
    private IMemberUserService memberUserService;


    /**
     * 博主/通告主列表查询
     *
     * @param memberUser {@link MemberUserVO}
     * @param pageNum    分页参数
     * @param pageSize   分页参数
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 14:58 2025/3/18
     **/
    @GetMapping("/list")
    public AjaxResult list(MemberUserVO memberUser,
                           @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<MemberUserVO> page = new Page<>(pageNum, pageSize);
        IPage<MemberUserVO> list = memberUserService.queryListPage(memberUser, page);
        return success(list);
    }

    /**
     * 获取会员详细信息
     *
     * @param userId 用户表id
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 11:36 2025/3/21
     **/
    @GetMapping(value = "/announcerDetail/{userId}")
    public AjaxResult announcerDetail(@PathVariable("userId") Long userId) {
        return success(memberUserService.announcerDetail(userId));
    }


    /**
     * 获取会员详细信息2
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 11:36 2025/3/21
     **/
    @GetMapping(value = "/getMemberDetail")
    public AjaxResult getMemberDetail() {
        return success(memberUserService.announcerDetail(SecurityUtils.getUserId()));
    }

    /**
     * 获取博主详细信息
     *
     * @param userId 用户表id
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 10:40 2025/3/22
     **/
    @GetMapping(value = "/bloggerDetail/{userId}")
    public AjaxResult bloggerDetail(@PathVariable("userId") Long userId) {
        return success(memberUserService.bloggerDetail(userId));
    }

    /**
     * 保存博主相信信息
     *
     * @param memberUser {@link MemberUserVO}
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 14:50 2025/4/9
     **/
    @PostMapping("/uniSaveMember")
    public AjaxResult uniSaveMember(@RequestBody MemberUserVO memberUser) {
        memberUserService.uniSaveMember(memberUser);
        return success();
    }

    /**
     * 查询用户详情
     *
     * @param userId   用户id
     * @param userType 用户类型
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 11:46 2025/4/9
     **/
    @GetMapping(value = "/queryDetail")
    public AjaxResult queryDetail(@RequestParam("userId") Long userId,
                                  @RequestParam("userType") String userType) {
        return success(memberUserService.queryByUserId(userId, userType));
    }


    /**
     * 导出会员列表
     */
    @Log(title = "会员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MemberUser memberUser) {
        List<MemberUser> list = memberUserService.list();
        ExcelUtil<MemberUser> util = new ExcelUtil<MemberUser>(MemberUser.class);
        util.exportExcel(response, list, "会员数据");
    }

    /**
     * 新增会员
     */
    @Log(title = "会员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MemberUser memberUser) {
        return toAjax(memberUserService.save(memberUser));
    }

    /**
     * 修改会员
     */
    @Log(title = "会员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MemberUser memberUser) {
        return toAjax(memberUserService.updateById(memberUser));
    }

    /**
     * 删除会员
     */
    @Log(title = "会员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        List<String> list = new ArrayList<>(Arrays.asList(ids));
        return toAjax(memberUserService.removeByIds(list));
    }


    /**
     * 赠送新人好礼
     */
    @Log(title = "赠送新人好礼", businessType = BusinessType.INSERT)
    @PostMapping("/sendNewGift")
    public AjaxResult sendNewGift() throws Exception {
        memberUserService.sendNewGift();
        return success();
    }

    /**
     * 判断是否领取过新人好礼
     */
    @Log(title = "判断是否领取过新人好礼", businessType = BusinessType.INSERT)
    @PostMapping("/isGetGift")
    public AjaxResult isGetGift() {

        return success(memberUserService.isGetGift());
    }
}
