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

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import com.ruoyi.project.system.service.ISysUserService;
import com.ruoyi.project.training.elearn.domain.TElResource;
import com.ruoyi.project.training.elearn.service.ITElResourceService;
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.TElLearn;
import com.ruoyi.project.training.elearn.service.ITElLearnService;
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/learn")
public class TElLearnController extends BaseController
{
    @Autowired
    private ITElLearnService tElLearnService;

    @Autowired
    private ISysUserService sysUserService;

    @PutMapping("/updateLearnTime")
    public AjaxResult updateLearnTime(@RequestBody TElResource resource)
    {
        Long userId = getUserId();
        Long resourceId = resource.getId();
        TElLearn tElLearn = new TElLearn();
        tElLearn.setUserId(userId);
        tElLearn.setResourceId(resourceId);
        TElLearn learn = tElLearnService.selectLearnByUserIdAndResourceId(tElLearn);
        if (learn == null) {
            tElLearn.setLearnTime("1");
            tElLearn.setDeptId(sysUserService.selectUserById(userId).getDeptId());
            tElLearnService.insertTElLearn(tElLearn);
        } else {
            String learnTime = learn.getLearnTime();
            learn.setLearnTime((Integer.parseInt(learnTime) + 1) + "");
            tElLearnService.updateTElLearn(learn);
        }
        return AjaxResult.success(learn);
    }

    /**
     * 查询在线学习记录列表
     */
    @GetMapping("/list")
    public AjaxResult list(TElLearn tElLearn)
    {
        return AjaxResult.success(tElLearnService.selectTElLearnList(tElLearn));
    }

    /**
     * 导出在线学习记录列表
     */
    @PreAuthorize("@ss.hasPermi('elearn:learn:export')")
    @Log(title = "在线学习记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TElLearn tElLearn)
    {
        List<TElLearn> list = tElLearnService.selectTElLearnList(tElLearn);
        ExcelUtil<TElLearn> util = new ExcelUtil<TElLearn>(TElLearn.class);
        return util.exportExcel(list, "learn");
    }

    /**
     * 获取在线学习记录详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tElLearnService.selectTElLearnById(id));
    }

    /**
     * 新增在线学习记录
     */
    @Log(title = "在线学习记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TElLearn tElLearn)
    {
        return toAjax(tElLearnService.insertTElLearn(tElLearn));
    }

    /**
     * 修改在线学习记录
     */
    @Log(title = "在线学习记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TElLearn tElLearn)
    {
        return toAjax(tElLearnService.updateTElLearn(tElLearn));
    }

    /**
     * 删除在线学习记录
     */
    @Log(title = "在线学习记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tElLearnService.deleteTElLearnByIds(ids));
    }
}
