package com.jeeplus.flowable.controller;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.flowable.model.MyNoticeList;
import com.jeeplus.flowable.service.MyNoticeService;
import com.jeeplus.logging.annotation.ApiLog;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.service.dto.UserDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Api(value = "我的通知")
@RequestMapping("/flowable/notice")
public class MyNoticeController {

    @Resource
    private MyNoticeService service;

    @GetMapping("/list")
    @ApiOperation(value = "待办列表")
    public ResponseEntity<IPage<MyNoticeList>> noticeList(Page<MyNoticeList> page, String type, String title, String taskName) {
        IPage<MyNoticeList> voPage = service.noticeList2(page, type, title, taskName);
        return ResponseEntity.ok (voPage);
    }

    @ApiOperation(value = "新增")
    @GetMapping("/add")
    public String add(String taskId, String title, String defId, String taskName, String createUser, String createTime, String noticeName, String noticeId) throws Exception{
        UserDTO userDTO = SpringUtil.getBean(IUserApi.class).getByToken(TokenProvider.getCurrentToken());
        return service.add(taskId, title, defId, taskName, createUser, createTime, noticeName, noticeId,userDTO.getId());
    }

    @ApiOperation(value = "修改已读状态")
    @GetMapping("/update")
    public String update(String taskId, String noticeId) {
        return service.update(taskId, noticeId);
    }

    @ApiOperation(value = "库存提醒修改已读状态")
    @GetMapping("/stockUpdate")
    public String stockUpdate(MyNoticeList myNoticeList) {
        return service.stockUpdate(myNoticeList);
    }

    /**
     * 部分标记为已读
     * @param ids
     * @return
     */
    @ApiLog("部分标记为已读")
    @PostMapping("portionRead")
    public ResponseEntity portionRead(String ids) {
        String idArray[] =ids.split(",");
        service.portionRead (Lists.newArrayList (idArray));
        return ResponseEntity.ok ("部分标记为已读成功");
    }

    @ApiOperation(value = "全部标记已读")
    @PostMapping("/readAll")
    public String readAll() {
        return service.readAll();
    }
}
