package com.cirs.biz.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.cirs.biz.domain.avgEvaluation;
import com.cirs.common.core.redis.RedisCache;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cirs.common.annotation.Log;
import com.cirs.common.core.controller.BaseController;
import com.cirs.common.core.domain.AjaxResult;
import com.cirs.common.enums.BusinessType;
import com.cirs.biz.domain.TEvaluation;
import com.cirs.biz.service.ITEvaluationService;
import com.cirs.common.utils.poi.ExcelUtil;
import com.cirs.common.core.page.TableDataInfo;

/**
 * 评价Controller
 * 
 * @author allen
 * @date 2023-11-03
 */
@RestController
@RequestMapping("/biz/evaluation")
public class TEvaluationController extends BaseController
{
    @Autowired
    private ITEvaluationService tEvaluationService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询评价列表
     */
    @PreAuthorize("@ss.hasPermi('biz:evaluation:list')")
    @GetMapping("/list")
    public TableDataInfo list(TEvaluation tEvaluation)
    {
        startPage();
        List<TEvaluation> list = tEvaluationService.selectTEvaluationList(tEvaluation);
        return getDataTable(list);
    }

    /**
     * 导出评价列表
     */
    @PreAuthorize("@ss.hasPermi('biz:evaluation:export')")
    @Log(title = "评价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TEvaluation tEvaluation)
    {
        List<TEvaluation> list = tEvaluationService.selectTEvaluationList(tEvaluation);
        ExcelUtil<TEvaluation> util = new ExcelUtil<TEvaluation>(TEvaluation.class);
        util.exportExcel(response, list, "评价数据");
    }

    /**
     * 获取评价详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:evaluation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tEvaluationService.selectTEvaluationById(id));
    }

    /**
     * 新增评价
     */
    @PreAuthorize("@ss.hasPermi('biz:evaluation:add')")
    @Log(title = "评价", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TEvaluation tEvaluation)
    {
//        redisCache.deleteObject("top10_components");
        return toAjax(tEvaluationService.insertTEvaluation(tEvaluation));
    }

    /**
     * 修改评价
     */
    @PreAuthorize("@ss.hasPermi('biz:evaluation:edit')")
    @Log(title = "评价", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TEvaluation tEvaluation)
    {
//        redisCache.deleteObject("top10_components");
        return toAjax(tEvaluationService.updateTEvaluation(tEvaluation));
    }

    /**
     * 删除评价
     */
    @PreAuthorize("@ss.hasPermi('biz:evaluation:remove')")
    @Log(title = "评价", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
//        redisCache.deleteObject("top10_components");
        return toAjax(tEvaluationService.deleteTEvaluationByIds(ids));
    }
    //拿到对元器件的平均评价
    @PreAuthorize("@ss.hasPermi('biz:evaluation:getmark')")

    @GetMapping("/componentid/{componentid}")
    public AjaxResult getscore(@PathVariable String componentid)
    {

        return success(tEvaluationService.getscore(componentid));
    }

    @PreAuthorize("@ss.hasPermi('biz:evaluation:get_top10')")
    @GetMapping("/get_top10")
    public AjaxResult get_top10(){
        List<avgEvaluation> avgEvaluations =  redisCache.getCacheList("top10_components");
        System.out.println(avgEvaluations);
        if (avgEvaluations==null||avgEvaluations.size()==0) {
            List<avgEvaluation> avgs = tEvaluationService.get_top10();
            redisCache.setCacheList("top10_components",avgs);
            return success(avgs);
        } else {
            return success(avgEvaluations);
        }

    }


}
