package cn.iocoder.yudao.module.appBase.controller.admin;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.rescue.core.controller.BaseController;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.module.appBase.domain.SysAnnouncement;
import cn.iocoder.yudao.module.appBase.service.ISysAnnouncementService;
import cn.iocoder.yudao.module.rescue.utils.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.ok;

/**
 * 系统通知Controller
 * 
 * @author zcy
 * @date 2023-09-21
 */
@RestController
@RequestMapping("/announcement/announcement")
public class SysAnnouncementController extends BaseController
{
    @Autowired
    private ISysAnnouncementService sysAnnouncementService;

    /**
     * 查询系统通知列表
     */
    @PreAuthorize("@ss.hasPermission('announcement:announcement:list')")
    @GetMapping("/list")
    public CommonResult list(SysAnnouncement sysAnnouncement)
    {
        Page<SysAnnouncement> page = new Page<>(1, 10);
        IPage<SysAnnouncement> list = sysAnnouncementService.selectSysAnnouncementList(sysAnnouncement, page);
        return success(list);
    }

    /**
     * 导出系统通知列表
     */
    @PreAuthorize("@ss.hasPermission('announcement:announcement:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysAnnouncement sysAnnouncement)
    {
        List<SysAnnouncement> list = sysAnnouncementService.list();
        ExcelUtil<SysAnnouncement> util = new ExcelUtil<SysAnnouncement>(SysAnnouncement.class);
        util.exportExcel(response, list, "系统通知数据");
    }

    /**
     * 获取系统通知详细信息
     */
    @PreAuthorize("@ss.hasPermission('announcement:announcement:query')")
    @GetMapping(value = "/{id}")
    public CommonResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysAnnouncementService.selectSysAnnouncementById(id));
    }

    /**
     * 新增系统通知
     */
    @PreAuthorize("@ss.hasPermission('announcement:announcement:add')")
    @PostMapping
    public CommonResult add(@RequestBody SysAnnouncement sysAnnouncement)
    {
        return toAjax(sysAnnouncementService.insertSysAnnouncement(sysAnnouncement));
    }

    /**
     * 修改系统通知
     */
    @PreAuthorize("@ss.hasPermission('announcement:announcement:edit')")
    @PutMapping
    public CommonResult edit(@RequestBody SysAnnouncement sysAnnouncement)
    {
        return toAjax(sysAnnouncementService.updateSysAnnouncement(sysAnnouncement));
    }

    /**
     * 删除系统通知
     */
    @PreAuthorize("@ss.hasPermission('announcement:announcement:remove')")
	@DeleteMapping("/{ids}")
    public CommonResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysAnnouncementService.deleteSysAnnouncementByIds(ids));
    }

    /**
     * 查询系统通知列表
     */
    @GetMapping("/getOwnMsg")
    public CommonResult getOwnMsg()
    {
        //当前登录用户
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        SysAnnouncement sysAnnouncement =new SysAnnouncement();
        sysAnnouncement.setToUserId(loginUser.getId());
        Page<SysAnnouncement> page = new Page<>(1, 10);
        IPage<SysAnnouncement> list = sysAnnouncementService.selectSysAnnouncementList(sysAnnouncement, page);
        return success(list);
    }
    /**
     * 查询未读条数
     */
    @GetMapping("/getOwnNoRead")
    public CommonResult getOwnNoRead()
    {
        //当前登录用户
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        SysAnnouncement sysAnnouncement =new SysAnnouncement();
        sysAnnouncement.setToUserId(loginUser.getId());
        LambdaQueryWrapper<SysAnnouncement> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(SysAnnouncement::getToUserId,loginUser.getId()).eq(SysAnnouncement::getIsRead,"0");
        long count = sysAnnouncementService.count(queryWrapper);
        return CommonResult.success(count);
    }
    /**
     * 当前用户全部已读
     */
    @GetMapping("/setAllRead")
    public CommonResult setAllRead()
    {
        //当前登录用户
//        LoginUser loginUser = getLoginUser();
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        SysAnnouncement sysAnnouncement =new SysAnnouncement();
        sysAnnouncement.setToUserId(loginUser.getId());
        LambdaUpdateWrapper<SysAnnouncement> queryWrapper =new LambdaUpdateWrapper<>();
        queryWrapper.set(SysAnnouncement::getIsRead,"1").eq(SysAnnouncement::getToUserId,loginUser.getId()).eq(SysAnnouncement::getIsRead,"0");
        sysAnnouncementService.update(queryWrapper);
        return ok();
    }
}
