package com.srs.web.controller.routine;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.srs.routine.domain.NotificationManagement;
import com.srs.routine.domain.dto.SendNotificationDto;
import com.srs.routine.service.INotificationManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import com.srs.common.core.controller.BaseController;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.utils.poi.ExcelUtil;
import com.srs.common.enums.BusinessType;
import com.srs.common.annotation.Log;
import com.srs.common.core.page.TableDataInfo;
import com.srs.comprehensive.domain.SrsGrade;

/**
 * 通知管理Controller
 *
 * @author srs
 * @date 2025-07-30
 */
@RestController
@RequestMapping("/routine/NotificationManagement")
@Api(value = "通知管理管理", tags = "通知管理管理")
public class NotificationManagementController extends BaseController {
    @Autowired
    private INotificationManagementService notificationManagementService;


    /**
     * 导出通知管理列表
     */
    @PreAuthorize("@ss.hasPermi('routine:NotificationManagement:export')")
    @Log(title = "通知管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出通知管理列表")
    public void export(HttpServletResponse response, NotificationManagement notificationManagement)
    {
        List<NotificationManagement> list = notificationManagementService.selectNotificationManagementList(notificationManagement);
        ExcelUtil<NotificationManagement> util = new ExcelUtil<NotificationManagement>(NotificationManagement.class);
        util.exportExcel(response, list, "通知管理数据");
    }

    /**
     * 获取通知管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('routine:NotificationManagement:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取通知管理详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(notificationManagementService.selectNotificationManagementById(id));
    }

    /**
     * 新增通知管理
     */
    @PreAuthorize("@ss.hasPermi('routine:NotificationManagement:add')")
    @Log(title = "通知管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增通知管理")
    public AjaxResult add(@RequestBody NotificationManagement notificationManagement)
    {
        return toAjax(notificationManagementService.insertNotificationManagement(notificationManagement));
    }

    /**
     * 修改通知管理
     */
    @PreAuthorize("@ss.hasPermi('routine:NotificationManagement:edit')")
    @Log(title = "通知管理", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改通知管理")
    public AjaxResult edit(@RequestBody NotificationManagement notificationManagement)
    {
        return toAjax(notificationManagementService.updateNotificationManagement(notificationManagement));
    }

    /**
     * 删除通知管理
     */
    @PreAuthorize("@ss.hasPermi('routine:NotificationManagement:remove')")
    @Log(title = "通知管理", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    @ApiOperation("删除通知管理")
    public AjaxResult remove(@PathVariable String ids)
    {
        // 处理逗号分隔的ID字符串
        String[] idArray = ids.split(",");
        Long[] idLongArray = new Long[idArray.length];
        for (int i = 0; i < idArray.length; i++) {
            idLongArray[i] = Long.parseLong(idArray[i]);
        }

        if (idLongArray.length == 1) {
            return toAjax(notificationManagementService.deleteNotificationManagementById(idLongArray[0]));
        } else {
            return toAjax(notificationManagementService.deleteNotificationManagementByIds(idLongArray));
        }
    }

    /**
     * 获取年级列表
     */
    @PreAuthorize("@ss.hasPermi('routine:NotificationManagement:list')")
    @GetMapping("/gradeList")
    @ApiOperation("获取年级列表")
    public AjaxResult getGradeList()
    {
        List<SrsGrade> gradeList = notificationManagementService.getGradeList();
        return success(gradeList);
    }

    /**
     * 按年级发送通知
     */
    @PreAuthorize("@ss.hasPermi('routine:NotificationManagement:add')")
    @Log(title = "按年级发送通知", businessType = BusinessType.INSERT)
    @PostMapping("/sendByGrades")
    @ApiOperation("按年级发送通知")
    public AjaxResult sendNotificationByGrades(@RequestBody SendNotificationDto sendNotificationDto)
    {
        int result = notificationManagementService.sendNotificationByGrades(sendNotificationDto);
        return toAjax(result);
    }

    /**
     * 查询当前用户发送的通知列表
     */
    @PreAuthorize("@ss.hasPermi('routine:NotificationManagement:list')")
    @GetMapping("/my-sent")
    @ApiOperation("查询当前用户发送的通知列表")
    public TableDataInfo mySentList(NotificationManagement notificationManagement)
    {
        startPage();
        List<NotificationManagement> list = notificationManagementService.selectNotificationManagementListBySender(notificationManagement);
        return getDataTable(list);
    }

    /**
     * 向指定学号的学生发送通知(DTO方式)
     */
    /*@PreAuthorize("@ss.hasPermi('routine:NotificationManagement:add')")
    @Log(title = "向学生发送通知", businessType = BusinessType.INSERT)
    @PostMapping("/sendToStudentDto")
    @ApiOperation("向指定学号的学生发送通知(DTO方式)")
    public AjaxResult sendNotificationToStudentDto(@RequestBody SendToStudentDto sendToStudentDto) {
        int result = notificationManagementService.sendNotificationToStudent(
                sendToStudentDto.getStudentNo(),
                sendToStudentDto.getTitle(),
                sendToStudentDto.getContent());
        return toAjax(result);
    }*/
}
