package com.ruoyi.member.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.member.domain.MemberBusiCard;
import com.ruoyi.member.query.MemberBusiCardQuery;
import com.ruoyi.member.service.IMemberBusiCardService;
import com.ruoyi.member.vo.MemberBusiCardVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 博主名片Controller
 *
 * @author pqz
 * @date 2025-03-21
 */
@RestController
@RequestMapping("/member/busiCard")
public class MemberBusiCardController extends BaseController {
    @Autowired
    private IMemberBusiCardService memberBusiCardService;

    /**
     * 分页查询博主名片信息
     *
     * @param memberBusiCard {@link MemberBusiCard}
     * @param pageNum        分页参数
     * @param pageSize       分页参数
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 21:47 2025/3/21
     **/
    @GetMapping("/list")
    public AjaxResult list(MemberBusiCard memberBusiCard,
                           @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<MemberBusiCard> page = new Page<>(pageNum, pageSize);
        IPage<MemberBusiCard> list = memberBusiCardService.queryListPage(memberBusiCard, page);
        return success(list);
    }

    /**
     * 查询当前登录博主名片信息
     *
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 10:42 2025/4/1
     **/
    @GetMapping("/listByUser")
    public AjaxResult listByUserId() {
        Long userId = SecurityUtils.getUserId();
        return success(memberBusiCardService.queryListByUserId(userId,null));
    }

    /**
     * 通过id获取博主名片信息
     *
     * @param id 博主名片id
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 22:37 2025/3/21
     **/
    @GetMapping(value = "/getBusiCardById")
    public AjaxResult getInfo(@RequestParam("id") String id) {
        return success(memberBusiCardService.queryById(id));
    }

    /**
     * 审核博主名片
     *
     * @param memberBusiCard {@link MemberBusiCard}
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 23:19 2025/3/21
     **/
    @Log(title = "博主名片", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult checkBusiCard(@RequestBody MemberBusiCard memberBusiCard) {
        memberBusiCardService.checkBusiCard(memberBusiCard);
        return toAjax(true);
    }

    /**
     * 手机端新增博主名片，提交审核
     *
     * @param memberBusiCard {@link MemberBusiCard}
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 10:35 2025/3/29
     **/
    @Log(title = "博主名片", businessType = BusinessType.INSERT)
    @PostMapping("/toApply")
    public AjaxResult save(@RequestBody MemberBusiCard memberBusiCard) {
        memberBusiCardService.saveBusiCard(memberBusiCard);
        return success();
    }


    /**
     * 删除博主名片信息
     *
     * @param id 博主名片id
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 13:53 2025/4/3
     **/
    @Log(title = "博主名片", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove")
    public AjaxResult remove(@RequestParam("id") String id) {
        memberBusiCardService.removeById(id);
        return success();
    }


    /**
     * 查询博主名片符合信息
     * @author zcy
     * &#064;date  21:47 2025/3/21
     **/
    @GetMapping("/getSignCard")
    public AjaxResult getSignCard(MemberBusiCardQuery query) {
        JSONObject res = memberBusiCardService.getSignCard(query);
        return success(res);
    }
}
