package com.ruoyi.cms.controller.cms;

import com.ruoyi.cms.domain.AppNotice;
import com.ruoyi.cms.domain.Notice;
import com.ruoyi.cms.domain.vo.NoticeTotal;
import com.ruoyi.cms.service.IAppNoticeService;
import com.ruoyi.cms.service.IAppUserService;
import com.ruoyi.cms.util.notice.NoticeUtils;
import com.ruoyi.cms.util.RoleUtils;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公告 信息操作处理
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/cms/notice")
public class CmsNoticeController extends BaseController
{
    @Autowired
    private IAppNoticeService noticeService;
    @Autowired
    private IAppUserService appUserService;

    /**
     * 获取通知公告列表
     */
    @PreAuthorize("@ss.hasPermi('system:notice:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppNotice notice)
    {
        startPage();
        List<AppNotice> list = noticeService.selectNoticeList(notice);
        return getDataTable(list);
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:notice:query')")
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable Long noticeId)
    {
        return success(noticeService.selectNoticeById(noticeId));
    }

    /**
     * 新增通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:add')")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody AppNotice notice)
    {
        notice.setCreateBy(getUsername());
        return toAjax(noticeService.insertNotice(notice));
    }

    /**
     * 修改通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:edit')")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody AppNotice notice)
    {
        notice.setUpdateBy(getUsername());
        return toAjax(noticeService.updateNotice(notice));
    }

    /**
     * 删除通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:remove')")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    public AjaxResult remove(@PathVariable Long[] noticeIds)
    {
        return toAjax(noticeService.deleteNoticeByIds(noticeIds));
    }

    @ApiOperation("pc端获取消息列表")
    @GetMapping("/appNoticList")
    public TableDataInfo selectListAppNotics(Notice notice)
    {
        if(!SecurityUtils.isLogin()){
            error(HttpStatus.ERROR,"未登录!");
        }
        if(notice.getUserId()==null){
            String idCard= RoleUtils.getCurrentUseridCard();
            AppUser appUser=appUserService.selectAppuserByIdcard(idCard);
            if(appUser==null){
                error(HttpStatus.ERROR,"用户信息未完善,请完善身份证信息!");
            }else{
                notice.setUserId(appUser.getUserId());
            }
        }
        startPage();
        List<Notice> notices = noticeService.selectListAppNotics(notice);
        return getDataTable(notices);
    }

    @ApiOperation("pc端获取未读消息列表")
    @GetMapping("/appNoticReadList")
    public TableDataInfo appNoticReadList(Notice notice)
    {
        if(!SecurityUtils.isLogin()){
            error(HttpStatus.ERROR,"未登录!");
        }
        if(notice.getUserId()==null){
            String idCard= RoleUtils.getCurrentUseridCard();
            AppUser appUser=appUserService.selectAppuserByIdcard(idCard);
            if(appUser==null){
                error(HttpStatus.ERROR,"用户信息未完善,请完善身份证信息!");
            }else{
                notice.setUserId(appUser.getUserId());
            }
        }
        startPage();
        List<Notice> notices = noticeService.selectListAppNotRead(notice);
        return getDataTable(notices);
    }

    @ApiOperation("pc端获已读消息列表")
    @GetMapping("/appNoticYdList")
    public TableDataInfo appNoticYdList(Notice notice)
    {
        if(!SecurityUtils.isLogin()){
            error(HttpStatus.ERROR,"未登录!");
        }
        if(notice.getUserId()==null){
            String idCard= RoleUtils.getCurrentUseridCard();
            AppUser appUser=appUserService.selectAppuserByIdcard(idCard);
            if(appUser==null){
                error(HttpStatus.ERROR,"用户信息未完善,请完善身份证信息!");
            }else{
                notice.setUserId(appUser.getUserId());
            }
        }
        startPage();
        notice.setIsRead(NoticeUtils.NOTICE_YD);
        notice.setRemark(NoticeUtils.NOTICE_REMARK);
        List<Notice> notices = noticeService.selectListAppNotices(notice);
        return getDataTable(notices);
    }

    @ApiOperation("系统通知标记已读")
    @PostMapping("/read/sysNotice")
    public AjaxResult sysNotice(@RequestParam String id)
    {
        Long userId=null;
        if(!SecurityUtils.isLogin()){
            return AjaxResult.error("未登录!");
        }
        if(userId==null){
            String idCard= RoleUtils.getCurrentUseridCard();
            AppUser appUser=appUserService.selectAppuserByIdcard(idCard);
            if(appUser==null){
                return AjaxResult.error("用户信息未完善,请完善身份证信息!");
            }else{
                userId=appUser.getUserId();
            }
        }
        noticeService.readSysNotices(id,userId);
        return AjaxResult.success();
    }

    /**
     * 获取消息条数
     */
    @ApiOperation("获取移动端用户消息条数")
    @GetMapping("/noticTotal")
    public AjaxResult getNoticTotal(Notice notice){
        if(!SecurityUtils.isLogin()){
            return AjaxResult.success("未登录!,返回为空");
        }
        if(notice.getUserId()==null){
            String idCard= RoleUtils.getCurrentUseridCard();
            AppUser appUser=appUserService.selectAppuserByIdcard(idCard);
            if(appUser==null){
                error(HttpStatus.ERROR,"用户信息未完善,请完善身份证信息!");
            }else{
                notice.setUserId(appUser.getUserId());
            }
        }
        startPage();
        NoticeTotal notices = noticeService.noticTotal(notice);
        return success(notices);
    }

    @ApiOperation("获取移动端用户消息条数")
    @DeleteMapping("/deleteNotice/{ids}")
    public AjaxResult deleteNotice(@PathVariable Long[] ids){
        return success(noticeService.removeNotice(ids));
    }
}
