package com.gxwebsoft.oa.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.entity.Notice;
import com.gxwebsoft.oa.param.NoticeParam;
import com.gxwebsoft.oa.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 消息记录表控制器
 *
 * @author 科技小王子
 * @since 2023-03-22 14:07:26
 */
@Api(tags = "消息记录表管理")
@RestController
@RequestMapping("/api/oa/notice")
public class NoticeController extends BaseController {
    @Resource
    private NoticeService noticeService;

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

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

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

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

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

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

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

    @PreAuthorize("hasAuthority('oa:notice:update')")
    @OperationLog
    @ApiOperation("批量修改消息记录表")
    @PutMapping("/batch")
    public ApiResult<?> updateBatch(@RequestBody List<Notice> list) {
        if (noticeService.updateBatchById(list)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('oa:notice:remove')")
    @OperationLog
    @ApiOperation("批量删除消息记录表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (noticeService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

  @PreAuthorize("hasAuthority('oa:notice:list')")
  @ApiOperation("统计信息")
  @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);
  }
}
