package com.ruoyi.project.training.elearn.controller;

import java.util.List;

import com.ruoyi.project.training.elearn.domain.TElLearn;
import com.ruoyi.project.training.elearn.mapper.TElLearnMapper;
import com.ruoyi.project.training.elearn.service.ITElLearnService;
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.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.training.elearn.domain.TElResource;
import com.ruoyi.project.training.elearn.service.ITElResourceService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 学习资料管理Controller
 *
 * @author ssy
 * @date 2024-06-05
 */
@RestController
@RequestMapping("/elearn/resource")
public class TElResourceController extends BaseController
{
    @Autowired
    private ITElResourceService tElResourceService;

    @Autowired
    private ITElLearnService itElLearnService;

    /**
     * 查询课件列表
     */
    @PreAuthorize("@ss.hasPermi('elearn:resource:list')")
    @GetMapping("/listFile")
    public TableDataInfo listFile(TElResource tElResource)
    {
        startPage();
        List<TElResource> list = tElResourceService.selectFileList(tElResource);
        Long userId = getUserId();
        for (TElResource elResource : list) {
            TElLearn learn = new TElLearn();
            learn.setUserId(userId);
            learn.setResourceId(elResource.getId());
            TElLearn learn1 = itElLearnService.selectLearnByUserIdAndResourceId(learn);
            if (learn1 != null) {
                elResource.setLearnTime(learn1.getLearnTime());
            }
        }
        return getDataTable(list);
    }

    /**
     * 查询学习资料管理列表
     */
    @PreAuthorize("@ss.hasPermi('elearn:resource:list')")
    @GetMapping("/list")
    public TableDataInfo list(TElResource tElResource)
    {
        startPage();
        List<TElResource> list = tElResourceService.selectTElResourceList(tElResource);
        return getDataTable(list);
    }

    /**
     * 导出学习资料管理列表
     */
    @PreAuthorize("@ss.hasPermi('elearn:resource:export')")
    @Log(title = "学习资料管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TElResource tElResource)
    {
        List<TElResource> list = tElResourceService.selectTElResourceList(tElResource);
        ExcelUtil<TElResource> util = new ExcelUtil<TElResource>(TElResource.class);
        return util.exportExcel(list, "resource");
    }

    /**
     * 获取学习资料管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('elearn:resource:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tElResourceService.selectTElResourceById(id));
    }

    /**
     * 新增学习资料管理
     */
    @PreAuthorize("@ss.hasPermi('elearn:resource:add')")
    @Log(title = "学习资料管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TElResource tElResource)
    {
        tElResource.setCreateBy(String.valueOf(getUserId()));
        return toAjax(tElResourceService.insertTElResource(tElResource));
    }

    /**
     * 修改学习资料管理
     */
    @PreAuthorize("@ss.hasPermi('elearn:resource:edit')")
    @Log(title = "学习资料管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TElResource tElResource)
    {
        return toAjax(tElResourceService.updateTElResource(tElResource));
    }

    /**
     * 删除学习资料管理
     */
    @PreAuthorize("@ss.hasPermi('elearn:resource:remove')")
    @Log(title = "学习资料管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tElResourceService.deleteTElResourceByIds(ids));
    }
}
