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.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.poi.ExcelUtil;
import com.ruoyi.member.domain.MemberApply;
import com.ruoyi.member.domain.MemberUser;
import com.ruoyi.member.service.IMemberApplyService;
import com.ruoyi.member.vo.MemberApplyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 通告主认证申请Controller
 *
 * @author pqz
 * @date 2025-03-17
 */
@RestController
@RequestMapping("/member/apply")
public class MemberApplyController extends BaseController {
    @Autowired
    private IMemberApplyService memberApplyService;

    /**
     * 通告主认证申请列表查询
     *
     * @param memberApply {@link MemberApplyVO}
     * @param pageNum     分页参数
     * @param pageSize    分页参数
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 9:50 2025/3/18
     **/
    @GetMapping("/list")
    public AjaxResult list(MemberApplyVO memberApply,
                           @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<MemberApply> page = new Page<>(pageNum, pageSize);
        IPage<MemberApplyVO> list = memberApplyService.queryListPage(memberApply, page);
        return success(list);
    }

    /**
     * 提交通告主认证
     *
     * @param memberApply {@link MemberUser}
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 14:49 2025/4/10
     **/
    @Log(title = "提交通告主认证申请", businessType = BusinessType.INSERT)
    @PostMapping("/toApply")
    public AjaxResult toApply(@RequestBody MemberApply memberApply) {
        try{
            memberApplyService.toApply(memberApply);
        }catch (Exception e){
            return error(e.getMessage());
        }
        return success();
    }



    /**
     * 导出通告主认证申请列表
     */
    @Log(title = "通告主认证申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MemberApply memberApply) {
        List<MemberApply> list = memberApplyService.list();
        ExcelUtil<MemberApply> util = new ExcelUtil<MemberApply>(MemberApply.class);
        util.exportExcel(response, list, "通告主认证申请数据");
    }

    /**
     * 获取通告主认证详细信息
     *
     * @param id 通告主认证表id
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 9:57 2025/3/18
     **/
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(memberApplyService.getById(id));
    }


    /**
     * 审核通告主信息
     *
     * @param memberApply {@link MemberApplyVO}
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 11:47 2025/3/18
     **/
    @Log(title = "审核通告主信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult checkMemberApply(@RequestBody MemberApplyVO memberApply) {
        memberApplyService.checkMemberApply(memberApply);
        return success();
    }

}
