package com.ruoyi.web.controller.back;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.Notifications;
import com.ruoyi.common.core.domain.entity.ShopUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.INotificationRecordsService;
import com.ruoyi.system.service.INotificationsService;
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 ruoyi
 * @date 2025-08-23
 */
@RestController
@RequestMapping("/back/notifications")
public class NotificationsController extends BaseController
{
    @Autowired
    private INotificationsService notificationsService;
    @Autowired
    private INotificationRecordsService recordsService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:notifications:list')")
    @GetMapping("/list")
    public TableDataInfo list(Notifications notifications)
    {
        startPage();
        List<Notifications> list = notificationsService.selectNotificationsList(notifications);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:notifications:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Notifications notifications)
    {
        List<Notifications> list = notificationsService.selectNotificationsList(notifications);
        ExcelUtil<Notifications> util = new ExcelUtil<Notifications>(Notifications.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 绑定用户到通知
     */
    @Log(title = "绑定通知用户", businessType = BusinessType.OTHER)
    @PostMapping("/bind")
    public AjaxResult bind(@RequestParam Long id, @RequestBody List<ShopUser> shopUsers){
        recordsService.bind(id, shopUsers);
        return AjaxResult.success("绑定用户成功");
    }
    
    /**
     * 获取通知已绑定的用户列表
     */
    @GetMapping("/bindUsers/{id}")
    public AjaxResult getBindUsers(@PathVariable("id") Long id) {
        List<ShopUser> list = recordsService.getBindUsers(id);
        return AjaxResult.success(list);
    }
    
    /**
     * 发布通知
     */
    @Log(title = "发布通知", businessType = BusinessType.OTHER)
    @PostMapping("/publish/{id}")
    public AjaxResult publish(@PathVariable("id") Long id) {
        return notificationsService.publishNotification(id);
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:notifications:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Notifications notifications)
    {
        return toAjax(notificationsService.insertNotifications(notifications));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:notifications:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Notifications notifications)
    {
        return toAjax(notificationsService.updateNotifications(notifications));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:notifications:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(notificationsService.deleteNotificationsByIds(ids));
    }


}
