package org.springblade.flow.engine.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.api.R;
import org.springblade.flow.engine.entity.FlowBusRelation;
import org.springblade.flow.engine.entity.FlowMsgRemind;
import org.springblade.flow.engine.service.FlowMsgRemindService;
import org.springblade.flow.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api(tags = "消息提醒", value = "FlowMsgRemind")
@RestController
@RequestMapping("BNMN")
public class FlowMsgRemindController {

    @Autowired
    private FlowMsgRemindService flowMsgRemindService;

    @Resource
    private UserProvider userProvider;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "消息提醒列表")
    @ApiOperationSupport(order = 1)
    public R list(){
        JSONObject result =new JSONObject();
        List<FlowMsgRemind>launchList=new ArrayList<>();
        List<FlowMsgRemind>waitList=new ArrayList<>();
        List<FlowMsgRemind>trialList=new ArrayList<>();
        List<FlowMsgRemind>copyList=new ArrayList<>();
        QueryWrapper<FlowMsgRemind> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(FlowMsgRemind::getType, 0)
        .eq(FlowMsgRemind::getUserId,userProvider.getUserId());
        queryWrapper.orderByDesc("id");
        List<FlowMsgRemind> list = flowMsgRemindService.list(queryWrapper);
        for (FlowMsgRemind msg : list){
           if (msg.getType() == 0){
               launchList.add(msg);
           }else if (msg.getType() == 1){
               waitList.add(msg);
           }else if (msg.getType() == 2){
               trialList.add(msg);
           }else if (msg.getType() == 3){
               copyList.add(msg);
           }
       }
        result.put("launch",launchList);
        result.put("wait",waitList);
        result.put("trial",trialList);
        result.put("copy",copyList);
        return R.data(result);
    }

    /**
     * 更新
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "更新 update", notes = "传入FlowMsgRemind")
    public R update(@Valid @RequestBody FlowMsgRemind flowMsgRemind) {
        flowMsgRemind.setUpdateTime(new Date());
        flowMsgRemind.setStatus(1);
        flowMsgRemindService.updateById(flowMsgRemind);
        return R.success("更新成功");
    }

}
