package com.zhongzheng.controller.user;

import java.util.List;
import java.util.Arrays;

import com.zhongzheng.common.utils.ServletUtils;
import com.zhongzheng.framework.web.service.WxTokenService;
import com.zhongzheng.modules.activity.service.IActivityTaskUserService;
import com.zhongzheng.modules.user.bo.UserStudyRecordAddBo;
import com.zhongzheng.modules.user.bo.UserStudyRecordEditBo;
import com.zhongzheng.modules.user.bo.UserStudyRecordQueryBo;
import com.zhongzheng.modules.user.entity.ClientLoginUser;
import com.zhongzheng.modules.user.service.IUserStudyRecordService;
import com.zhongzheng.modules.user.vo.UserStudyRecordVo;
import lombok.RequiredArgsConstructor;
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.zhongzheng.common.annotation.Log;
import com.zhongzheng.common.core.controller.BaseController;
import com.zhongzheng.common.core.domain.AjaxResult;
import com.zhongzheng.common.enums.BusinessType;
import com.zhongzheng.common.utils.poi.ExcelUtil;
import com.zhongzheng.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 用户学习记录Controller
 *
 * @author ruoyi
 * @date 2021-06-25
 */
@Api(value = "用户学习记录控制器", tags = {"用户学习记录管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/study/record")
public class UserStudyRecordController extends BaseController {

    private final IUserStudyRecordService iUserStudyRecordService;

    private final WxTokenService wxTokenService;

    /**
     * 查询用户学习记录列表
     */
    @ApiOperation("查询用户学习记录列表")
    @GetMapping("/list")
    public TableDataInfo<UserStudyRecordVo> list(UserStudyRecordQueryBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        startPage();
        List<UserStudyRecordVo> list = iUserStudyRecordService.selectList(bo);
        return getDataTable(list);
    }



    /**
     * 获取用户学习记录详细信息
     */
    @ApiOperation("获取用户学习记录详细信息")
    @GetMapping("/{recordId}")
    public AjaxResult<UserStudyRecordVo> getInfo(@PathVariable("recordId" ) Long recordId) {
        return AjaxResult.success(iUserStudyRecordService.queryById(recordId));
    }

    /**
     * 新增用户学习记录
     */
    @ApiOperation("新增用户学习记录")
    @Log(title = "用户学习记录", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Integer> add(@RequestBody UserStudyRecordAddBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        return AjaxResult.success(iUserStudyRecordService.insertByAddBo(bo));
    }

    /**
     * 修改用户学习记录
     */
    @ApiOperation("修改用户学习记录")
    @Log(title = "用户学习记录", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult<Void> edit(@RequestBody UserStudyRecordEditBo bo) {
        return toAjax(iUserStudyRecordService.updateByEditBo(bo) ? 1 : 0);
    }


}
