package com.gxwebsoft.common.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.entity.Notice;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.param.NoticeParam;
import com.gxwebsoft.common.system.service.NoticeService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 消息记录表控制器
 *
 * @author 科技小王子
 * @since 2023-10-08 13:22:21
 */
@Tag(name = "通知")
@RestController
@RequestMapping("/api/system/notice")
public class NoticeController extends BaseController {
    @Resource
    private NoticeService noticeService;

    @PreAuthorize("hasAuthority('sys:notice:list')")
    @Operation(summary = "分页查询消息记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<Notice>> page(NoticeParam param) {
        // 使用关联查询
        return success(noticeService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('sys:notice:list')")
    @Operation(summary = "查询全部消息记录表")
    @GetMapping()
    public ApiResult<List<Notice>> list(NoticeParam param) {
        // 使用关联查询
        return success(noticeService.listRel(param));
    }

    @PreAuthorize("hasAuthority('sys:notice:list')")
    @Operation(summary = "根据id查询消息记录表")
    @GetMapping("/{id}")
    public ApiResult<Notice> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(noticeService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('sys:notice:save')")
    @Operation(summary = "添加消息记录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Notice notice) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          notice.setUserId(loginUser.getUserId());
        }
        if (noticeService.save(notice)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:notice:update')")
    @Operation(summary = "修改消息记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Notice notice) {
        if (noticeService.updateById(notice)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:notice:remove')")
    @Operation(summary = "删除消息记录表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (noticeService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('sys:notice:save')")
    @Operation(summary = "批量添加消息记录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Notice> list) {
        if (noticeService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:notice:update')")
    @Operation(summary = "批量修改消息记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Notice> batchParam) {
        if (batchParam.update(noticeService, "notice_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:notice:remove')")
    @Operation(summary = "批量删除消息记录表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (noticeService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }
  @Operation(summary = "统计信息")
  @GetMapping("/getUnReadNum")
  public ApiResult<?> getUnReadNum(){
    JSONObject json = new JSONObject();
    json.put("notice",noticeService.count(new LambdaQueryWrapper<Notice>()
      .eq(Notice::getUserId, getLoginUserId())
      .eq(Notice::getType,"notice")
      .eq(Notice::getStatus,0)));
    json.put("letter",noticeService.count(new LambdaQueryWrapper<Notice>()
      .eq(Notice::getUserId, getLoginUserId())
      .eq(Notice::getType,"letter")
      .eq(Notice::getStatus,0)));
    json.put("todo",noticeService.count(new LambdaQueryWrapper<Notice>()
      .eq(Notice::getUserId, getLoginUserId())
      .eq(Notice::getType,"todo")
      .eq(Notice::getStatus,0)));
    return success("操作成功",json);
  }

}
