package com.yanzhu.jh.bigscreen.web.controller;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.flowable.service.IFlowBusinessKeyService;
import com.ruoyi.system.domain.FlowTaskEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/bgscreen/flow")
public class FloweController extends BaseController {
    @Autowired
    IFlowBusinessKeyService flowBusinessKeyService;

    @Autowired
    private RedisCache redisCache;

    @GetMapping("/groupByCategory")
    public AjaxResult groupByCategory(long deptId,long projectId){
        String key="bgscreen_flow_groupByCategory_"+deptId+"_"+projectId;
        Object obj=redisCache.getCacheObject(key);
        if(obj!=null){
            return AjaxResult.success(obj);
        }

        FlowTaskEntity where=new FlowTaskEntity();
        if(deptId<=0){
            if (SecurityUtils.isUserB()) {
                where.setPrjIds(getProjectIds());
            }
        }
        where.setDeployId(deptId<1?null:""+deptId);
        where.setBusinessKey(projectId<1?null:""+projectId);
        List<FlowTaskEntity> list=flowBusinessKeyService.groupByCategory(where);
        redisCache.setCacheObject(key, list, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return AjaxResult.success(list);
    }

    @GetMapping("/groupByUnit")
    public AjaxResult groupByUnit(long deptId,long projectId){
        String key="bgscreen_flow_groupByUnit_"+deptId+"_"+projectId;
        Object obj=redisCache.getCacheObject(key);
        if(obj!=null){
            return AjaxResult.success(obj);
        }
        FlowTaskEntity where=new FlowTaskEntity();
        if(deptId<=0){
            if (SecurityUtils.isUserB()) {
                where.setPrjIds(getProjectIds());
            }
        }
        where.setDeployId(deptId<1?"":""+deptId);
        where.setBusinessKey(projectId<1?"":""+projectId);
        List<FlowTaskEntity> list=flowBusinessKeyService.groupByUnit(where);
        redisCache.setCacheObject(key, list, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return AjaxResult.success(list);
    }


    @PostMapping("/listByUnit")
    public TableDataInfo listByUnit(@RequestBody FlowTaskEntity where){
        startPage();
        List<FlowTaskEntity> list=flowBusinessKeyService.listByUnit(where);
        if(where.getDeptId()<=0){
            if (SecurityUtils.isUserB()) {
                where.setPrjIds(getProjectIds());
            }
        }
        return getDataTable(list);
    }

    @PostMapping("/listByState")
    public TableDataInfo listByState(@RequestBody FlowTaskEntity where){
        startPage();
        List<FlowTaskEntity> list=flowBusinessKeyService.listByState(where);
        if(where.getDeptId()<=0){
            if (SecurityUtils.isUserB()) {
                where.setPrjIds(getProjectIds());
            }
        }
        return getDataTable(list);
    }

}
