package com.gxwebsoft.open.controller;

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.BatchParam;
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/open/notice")
public class OpenNoticeController extends BaseController {
    @Resource
    private NoticeService noticeService;

    @ApiOperation("分页查询消息记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<Notice>> page(NoticeParam param) {
        // 验证签名
//        isCheckSign();
        // 使用关联查询
        param.setUserId(getLoginUserId());
        return success(noticeService.pageRel(param));
    }

    @ApiOperation("查询全部消息记录表")
    @GetMapping()
    public ApiResult<List<Notice>> list(NoticeParam param) {
        // 验证签名
//        isCheckSign();
        // 使用关联查询
        param.setUserId(getLoginUserId());
        return success(noticeService.listRel(param));
    }

    @ApiOperation("根据id查询消息记录表")
    @GetMapping("/{id}")
    public ApiResult<Notice> get(@PathVariable("id") Integer id) {
        // 验证签名
//        isCheckSign();
        return success(noticeService.getById(id));
        // 使用关联查询
        //return success(noticeService.getByIdRel(id));
    }

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

    @ApiOperation("修改消息记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Notice notice) {
        // 验证签名
//        isCheckSign();
        if (noticeService.updateById(notice)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

    @ApiOperation("批量添加消息记录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Notice> list) {
        // 验证签名
//        isCheckSign();
        if (noticeService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("批量修改消息记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Notice> batchParam) {
        // 验证签名
//        isCheckSign();
        if (batchParam.update(noticeService, "notice_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("批量删除消息记录表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        // 验证签名
//        isCheckSign();
        if (noticeService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
