package com.ruoyi.docking.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.exception.job.TaskException;
import com.ruoyi.docking.entity.SmartReminders;
import com.ruoyi.docking.entity.request.JAddJobSmart;
import com.ruoyi.docking.entity.request.SREnterRequest;
import com.ruoyi.docking.entity.request.SmartRemindersPageRequest;
import com.ruoyi.docking.service.SmartRemindersService;
import com.ruoyi.gysl.regular.NoticeTiming;
import com.ruoyi.quartz.domain.SysJob;
import com.ruoyi.quartz.service.ISysJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.Serializable;

/**
 * 智能提醒（j_smart_reminders）表控制层
 *
 * @author du
 * @since 2024/7/1 13:33
 */
@RestController
@RequestMapping("/gysl/jSmartReminders")
@Api(tags = "智能提醒")
public class SmartRemindersController extends BaseController {

    @Resource
    private SmartRemindersService smartRemindersService;


    @Autowired
    private ISysJobService jobService;

    @Resource
    private NoticeTiming noticeService;

    /**
     * 定时器更新不定期提醒数据（暂存）
     *
     * @return 所有数据
     */
//    @PreAuthorize("@ss.hasAnyRoles('admin')")
    @ApiOperation(value = "定时器更新不定期提醒数据（暂存）")
    @GetMapping("/updateNoRegularReminders")
    public AjaxResult updateNoRegularReminders() {
        noticeService.changeNdq();
        return success();
    }

    /**
     * 分页查询智能提醒规则数据
     *
     * @param page           分页对象
     * @param smartReminders 查询实体
     * @return 所有数据
     */
//    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    @ApiOperation(value = "分页查询智能提醒规则数据", response = SmartReminders.class)
    @GetMapping
    public AjaxResult selectAll(Page<SmartReminders> page, SmartRemindersPageRequest smartReminders) {
        return success(smartRemindersService.page(page, smartReminders));
    }

    /**
     * 通过主键查询单条智能提醒规则数据
     *
     * @param id 主键
     * @return 单条数据
     */
//    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    @ApiOperation(value = "通过主键查询单条智能提醒规则数据", response = SmartReminders.class)
    @GetMapping("/{id}")
    public AjaxResult selectOneSmart(@PathVariable Serializable id) {
        return success(smartRemindersService.getById(id));
    }

    /**
     * 已读智能提醒
     *
     * @param id 主键
     * @return 单条数据
     */
//    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    @ApiOperation(value = "已读智能提醒")
    @GetMapping("/isRead/{id}")
    public AjaxResult isRead(@PathVariable Serializable id) {
        return success(smartRemindersService.isRead(id));
    }

    /**
     * 新增定期提醒数据（政务端和企业端）
     *
     * @param jSmartRemindersDq 实体对象
     * @return 新增结果
     */
//    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    @ApiOperation(value = "新增定期提醒")
    @PostMapping("/add")
    public AjaxResult insertDs(@RequestBody @Valid SREnterRequest jSmartRemindersDq) throws SchedulerException, TaskException {
        if (jSmartRemindersDq.getProjectId() != null) {
            jSmartRemindersDq.setAlertType(4);
        } else {
            jSmartRemindersDq.setAlertType(1);
        }
        return success(smartRemindersService.add(jSmartRemindersDq));
    }

    /**
     * 修改定期提醒数据和不定期提醒数据
     *
     * @param dq 实体对象
     * @return 修改结果
     */
//    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    @ApiOperation(value = "修改定期提醒数据和不定期提醒数据")
    @PostMapping("/updateDq")
    public AjaxResult updateDq(@RequestBody @Valid SmartReminders dq) throws SchedulerException, TaskException {
        if (dq.getAlertType() == 2 || dq.getAlertType() == 3) {
            if (dq.getAlertManner() == 2) {
                smartRemindersService.updateAndAdd(dq,false);
            }else {
                throw new ServiceException("参数错误!");
            }
        }else {
            if (dq.getAlertManner() == 1) {
                smartRemindersService.updateById(dq);
            }else {
                throw new ServiceException("参数错误!");
            }
        }
        return success();
    }

    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
//    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    @ApiOperation(value = "删除数据")
    @PostMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Long id) throws SchedulerException {
        SmartReminders js = smartRemindersService.getById(id);
        if (js.getAlertManner() == 2) {
            throw new ServiceException("不定期提醒不允许删除");
        }
        JAddJobSmart jAddJobSmart = smartRemindersService.selectJobSmart(id);
        if (jAddJobSmart.getJobId() != null) {
            SysJob sysJob = new SysJob();
            sysJob.setJobId(jAddJobSmart.getJobId());
            jobService.deleteJob(sysJob);
        }
        return success(smartRemindersService.removeById(id));
    }
}
