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.yanzhu.jh.project.domain.SurProjectMaterialSeal;
import com.yanzhu.jh.project.service.ISurProjectMaterialSealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@RequestMapping("/bgscreen/materialSeal")
public class ProjectMaterialSealController extends BaseController {

    @Autowired
    ISurProjectMaterialSealService materialSealService;

    @Autowired
    private RedisCache redisCache;


    @PostMapping("/selectTop20")
    public AjaxResult selectTop20(@RequestBody SurProjectMaterialSeal where){
        String key="bgscreen_materialSeal_selectTop20_"+where.getDeptId()+"-"+where.getProjectId();
        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<SurProjectMaterialSeal> list=materialSealService.selectTop20(where);
        redisCache.setCacheObject(key, list, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return AjaxResult.success(list);
    }
    @PostMapping("/groupTop12Month")
    public AjaxResult groupTop12Month(@RequestBody SurProjectMaterialSeal where){
        String key="bgscreen_materialSeal_groupTop12Month_"+where.getDeptId()+"-"+where.getProjectId();
        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<SurProjectMaterialSeal> list=materialSealService.groupTop12Month(where);
        redisCache.setCacheObject(key, list, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return AjaxResult.success(list);
    }
    @PostMapping("/selectTop12Month")
    public TableDataInfo selectTop12Month(@RequestBody SurProjectMaterialSeal where){
        startPage();
        Long deptId=where.getDeptId();
        if(deptId==null || deptId<=0){
            if (SecurityUtils.isUserB()) {
                where.setPrjIds(getProjectIds());
            }
        }
        List<SurProjectMaterialSeal> list=materialSealService.selectTop12Month(where);
        return getDataTable(list);
    }

}
