package com.ruoyi.web.controller.api;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ApiResult;
import com.ruoyi.mall.domain.BusinessTutorial;
import com.ruoyi.mall.domain.OfflineServices;
import com.ruoyi.mall.domain.OfflineServicesRegister;
import com.ruoyi.mall.domain.Vo.OfflineServicesRegisterVo;
import com.ruoyi.mall.domain.Vo.OfflineServicesVo;
import com.ruoyi.mall.service.IBusinessTutorialService;
import com.ruoyi.mall.service.IOfflineServicesRegisterService;
import com.ruoyi.mall.service.IOfflineServicesService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**我的-教程
 *
 */
@RestController
@RequestMapping("/api/Tutorial")
public class ApiTutorialController extends BaseController {

    @Resource
    private IBusinessTutorialService tutorialService;

    /**教程分类列表
     *
     * @param pageNum
     * @param pageSize
     * @param type 教程类型 1解决方案 2核心功能
     * @return
     */
    @GetMapping("/getTutorialList")
    public AjaxResult getOfflineServicesList(@RequestParam(defaultValue = "1") Integer pageNum,
                                            @RequestParam(defaultValue = "10") Integer pageSize,
                                             @NotNull(message = "getTutorialList.type.isnull") String type) {
        try{
            PageHelper.startPage(pageNum, pageSize);
            BusinessTutorial businessTutorial= new BusinessTutorial();
            businessTutorial.setType(type);
            List<BusinessTutorial> list = tutorialService.selectBusinessTutorialList(businessTutorial);
            PageInfo<BusinessTutorial> pageInfo = new PageInfo<>(list);
            return AjaxResult.success().put("data",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("教程分类列表异常");
        }
    }

    /**教程详情
     *
     * @param tutorialId 教程id
     * @return
     */
    @GetMapping("/getTutorialDetail")
    public AjaxResult getOfflineServiceDetail(@NotNull(message = "getTutorialDetail.tutorialId.isnull") Long tutorialId) {
        try{
            //查详情
            BusinessTutorial businessTutorial = tutorialService.selectBusinessTutorialByTutorialId(tutorialId);
            //阅读量+1
            BusinessTutorial updateRead = new BusinessTutorial();
            updateRead.setTutorialId(tutorialId);
            updateRead.setReadNum(businessTutorial.getReadNum()+1);
            tutorialService.updateBusinessTutorial(updateRead);
            return AjaxResult.success().put("data",businessTutorial);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取教程详情异常");
        }

    }


}
