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 com.yanzhu.jh.bigscreen.web.vo.FlowTypeMapper;
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);
    }


    @GetMapping("/groupByUnitFinish")
    public AjaxResult groupByUnitFinish(long deptId,long projectId){
        String key="bgscreen_flow_groupByUnitFinish_"+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.groupByUnitFinish(where);
        redisCache.setCacheObject(key, list, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return AjaxResult.success(list);
    }


    @GetMapping("/groupByUnitTotal")
    public AjaxResult groupByUnitTotal(long deptId,long projectId){
        String key="bgscreen_flow_groupByUnitTotal_"+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.groupByUnitTotal(where);
        redisCache.setCacheObject(key, list, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return AjaxResult.success(list);
    }



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

    @PostMapping("/listByCategory")
    public  TableDataInfo listByCategory(@RequestBody FlowTaskEntity where){
        if(where.getDeptId()<=0){
            if (SecurityUtils.isUserB()) {
                where.setPrjIds(getProjectIds());
            }
        }
        startPage();
        List<FlowTaskEntity> list=flowBusinessKeyService.listByCategory(where);
        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);
    }

    @GetMapping("/groupByProject")
    public AjaxResult groupByProject(long deptId){
        List<FlowTaskEntity> list=flowBusinessKeyService.groupByProject(deptId);
        return AjaxResult.success(list);
    }

    /**
     * 根据条件查询安全类作业工作流列表
     * JiangYuQi
     */
    @GetMapping("/findSafetyWorkList")
    public AjaxResult findSafetyWorkList(long deptId,long projectId,String flowType){
        String key="bgscreen_flow_findSafetyWorkList_"+deptId+"_"+projectId+"_"+flowType;
        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.setProcDefKey(FlowTypeMapper.getFlowProcdefKey(flowType));
        where.setProjectDeptId(deptId<1?"":""+deptId);
        where.setBusinessKey(projectId<1?"":""+projectId);
        List<FlowTaskEntity> list=flowBusinessKeyService.findSafetyWorkList(where);
        redisCache.setCacheObject(key, list, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return AjaxResult.success(list);
    }

    /**
     * 根据条件查询安全类作业工作流列表
     * JiangYuQi
     */
    @GetMapping("/findFormDatasByProcInsId/{procInsId}")
    public AjaxResult findFormDatasByProcInsId(@PathVariable("procInsId") String procInsId){
        return AjaxResult.success(flowBusinessKeyService.selectFormDatasByProcInsId(procInsId));
    }

}
