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.domain.entity.SysDictData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.yanzhu.jh.project.domain.SurProjectStandard;
import com.yanzhu.jh.project.service.ISurProjectStandardService;
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/standard")
public class ProjectStandardController extends BaseController {
    @Autowired
    ISurProjectStandardService standardService;

    @Autowired
    private RedisCache redisCache;

    @GetMapping("/getList")
    public AjaxResult getList(Long deptId,Long projectId){
        SurProjectStandard where=new SurProjectStandard();
        where.setProjectId(projectId);
        where.setIsDel(0l);
        String key="bgscreen_standard_getList_"+projectId;
        Object obj=redisCache.getCacheObject(key);
        if(obj!=null){
            return AjaxResult.success(obj);
        }
        List<SurProjectStandard> list=standardService.selectSurProjectStandardList(where);
        redisCache.setCacheObject(key, list, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return AjaxResult.success(list);
    }

    @PostMapping("/selectList")
    public TableDataInfo selectList(@RequestBody SurProjectStandard where){
        startPage();
        Long deptId= where.getDeptId();
        if(deptId==null || deptId<=0){
            if (SecurityUtils.isUserB()) {
                where.setPrjIds(getProjectIds());
            }
        }
        List<SurProjectStandard> list=standardService.selectList(where);
        return getDataTable(list);
    }



    @PostMapping("/groupByType")
    public AjaxResult groupByType(@RequestBody SurProjectStandard where){
        String key="bgscreen_standard_groupByType_"+where.getProjectId()+"_"+where.getDeptId();
        Object obj=redisCache.getCacheObject(key);
        if(obj!=null){
            return AjaxResult.success(obj);
        }
        Long deptId= where.getDeptId();
        if(deptId==null || deptId<=0){
            if (SecurityUtils.isUserB()) {
                where.setPrjIds(getProjectIds());
            }
        }
        List<SysDictData> list=standardService.groupByType(where);
        redisCache.setCacheObject(key, list, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return AjaxResult.success(list);
    }


    @GetMapping("/groupByProject")
    public AjaxResult groupByProject() {
        List<SurProjectStandard> list=standardService.groupByProject();
        return AjaxResult.success(list);
    }


}
