package com.muyu.system.controller;

import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.system.domain.SysNotice;
import com.muyu.system.service.SysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * 公告 信息操作处理
 *
 * @author muyu
 */
@RestController
@RequestMapping("/notice")
public class SysNoticeController extends BaseController {
    @Autowired
    private SysNoticeService noticeService;

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

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

    /**
     * 新增通知公告
     */
    @RequiresPermissions("system:notice:add")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add (@Validated @RequestBody SysNotice notice) {
        notice.setCreateBy(SecurityUtils.getUsername());
        HashSet<Integer> userIdSet = new HashSet<>();
        if ("1".equals(notice.getNoticeType())){
            // 如果通知类型为1(通知)，则将整合发送的用户
            userIdSet = new HashSet<>(notice.getUserIds());
            if (notice.getDeptIds()!=null && !notice.getDeptIds().isEmpty()){
                // 获取发送部门的用户ID
                List<List<Integer>> deptIdsList = notice.getDeptIds();
                ArrayList<Integer> deptIds = new ArrayList<>();
                for (List<Integer> integers : deptIdsList) {
                    deptIds.add(integers.get(2));
                }
                userIdSet.addAll(noticeService.selUserByDeptIds(deptIds));
            }
        }else{
            // 否则为公告,查询所有用户ID
            userIdSet.addAll(noticeService.findUserIdsAll());
        }
        return toAjax(noticeService.insertNotice(notice, userIdSet));
    }

    /**
     * 修改通知公告
     */
    @RequiresPermissions("system:notice:edit")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit (@Validated @RequestBody SysNotice notice) {
        notice.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(noticeService.updateNotice(notice));
    }

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

    /**
     * 查询用户通知公告阅读列表
     */
    @GetMapping("/selNoticeByUserId")
    public Result selNoticeByUserId () {
        Long userId = SecurityUtils.getUserId();
        return success(noticeService.selNoticeByUserId(userId));
    }

    /**
     * 用户阅读通知公告
     */
    @GetMapping("/findNoticeByNoticeIdAndUserId")
    public Result findNoticeByNoticeIdAndUserId (@RequestParam("noticeId") Long noticeId,@RequestParam("sta") int sta) {
        Long userId = SecurityUtils.getUserId();
        return success(noticeService.findNoticeByNoticeIdAndUserId(noticeId,sta,userId));
    }

    /**
     * 查询详细公告信息与其阅读状态统计
     */
    @GetMapping("/findNoticeByNoticeId/{noticeId}")
    public Result findNoticeByNoticeId (@PathVariable("noticeId") Long noticeId) {
        return success(noticeService.findNoticeByNoticeId(noticeId));
    }
}
