package com.ruoyi.web.controller.system;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.utils.Auth;
import com.ruoyi.system.utils.JwtUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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.system.domain.PushMessage;
import com.ruoyi.system.service.IPushMessageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 推送消息Controller
 *
 * @author ruoyi
 * @date 2025-08-01
 */
@RestController
@RequestMapping("/system/message")
public class PushMessageController extends BaseController
{
    @Autowired
    private IPushMessageService pushMessageService;

    /**
     * 骑手、商家端获取推送消息分页列表
     */
    @Anonymous
    @Auth
    @GetMapping("/getPushMessageList")
    public AjaxResult getPushMessageList(@RequestHeader String token, @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        JwtUtil jwtUtil = new JwtUtil();
        String uid = jwtUtil.getusid(token);
        Page<PushMessage> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<PushMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PushMessage::getUserId,uid).orderByDesc(PushMessage::getTime);
        IPage<PushMessage> data = pushMessageService.page(page,queryWrapper);
        return success(data);
    }

    /**
     * 查询推送消息列表
     */
    @PreAuthorize("@ss.hasPermi('system:message:list')")
    @GetMapping("/list")
    public TableDataInfo list(PushMessage pushMessage)
    {
        startPage();
        List<PushMessage> list = pushMessageService.selectPushMessageList(pushMessage);
        return getDataTable(list);
    }

    /**
     * 导出推送消息列表
     */
    @PreAuthorize("@ss.hasPermi('system:message:export')")
    @Log(title = "推送消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PushMessage pushMessage)
    {
        List<PushMessage> list = pushMessageService.selectPushMessageList(pushMessage);
        ExcelUtil<PushMessage> util = new ExcelUtil<PushMessage>(PushMessage.class);
        util.exportExcel(response, list, "推送消息数据");
    }

    /**
     * 获取推送消息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:message:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pushMessageService.selectPushMessageById(id));
    }

    /**
     * 新增推送消息
     */
    @PreAuthorize("@ss.hasPermi('system:message:add')")
    @Log(title = "推送消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PushMessage pushMessage)
    {
        return toAjax(pushMessageService.insertPushMessage(pushMessage));
    }

    /**
     * 修改推送消息
     */
    @PreAuthorize("@ss.hasPermi('system:message:edit')")
    @Log(title = "推送消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PushMessage pushMessage)
    {
        return toAjax(pushMessageService.updatePushMessage(pushMessage));
    }

    /**
     * 删除推送消息
     */
    @PreAuthorize("@ss.hasPermi('system:message:remove')")
    @Log(title = "推送消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pushMessageService.deletePushMessageByIds(ids));
    }
}
