package com.xinelu.manage.controller.trainingitem;


import com.xinelu.common.annotation.Log;
import com.xinelu.common.constant.Constants;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.custominterface.Insert;
import com.xinelu.common.custominterface.Update;
import com.xinelu.common.enums.BusinessType;
import com.xinelu.manage.domain.trainingitem.TrainingItem;
import com.xinelu.manage.dto.trainingitem.TrainingItemAndDirectory;
import com.xinelu.manage.service.trainingitem.ITrainingItemService;
import com.xinelu.manage.vo.trainingitem.TrainingItemAndCategoryVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 学习培训项目信息Controller
 *
 * @author xinyilu
 * @date 2023-04-20
 */
@RestController
@RequestMapping("/system/trainingItem")
public class TrainingItemController extends BaseController {
    @Resource
    private ITrainingItemService trainingItemService;

    /**
     * 查询学习培训项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:trainingItem:list')")
    @GetMapping("/list")
    public TableDataInfo list(TrainingItem trainingItem) {
        startPage();
        List<TrainingItemAndCategoryVO> list = trainingItemService.selectTrainingItemList(trainingItem);
        return getDataTable(list);
    }

    /**
     * 获取学习培训项目信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:trainingItem:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        if (Objects.isNull(id)) {
            return AjaxResult.error("学习培训课程信息为空！");
        }
        return AjaxResult.success(trainingItemService.selectTrainingItemById(id));
    }

    /**
     * 新增学习培训项目信息
     */
    @PreAuthorize("@ss.hasPermi('system:trainingItem:add')")
    @Log(title = "学习培训课程信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody @Validated(Insert.class) TrainingItemAndDirectory trainingItem) {
        return trainingItemService.insertTrainingItem(trainingItem);
    }

    /**
     * 修改学习培训项目信息
     */
    @PreAuthorize("@ss.hasPermi('system:trainingItem:edit')")
    @Log(title = "学习培训课程信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody @Validated(Update.class) TrainingItemAndDirectory trainingItem) {
        if (Objects.isNull(trainingItem) || Objects.isNull(trainingItem.getTrainingItemId())) {
            return AjaxResult.error("请输入需要修改信息！");
        }
        return trainingItemService.updateTrainingItem(trainingItem);
    }

    /**
     * 删除学习培训项目信息
     */
    @PreAuthorize("@ss.hasPermi('system:trainingItem:remove')")
    @Log(title = "学习培训课程信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        if (Objects.isNull(ids)) {
            return AjaxResult.error("请传入需要删除的信息！");
        }
        return trainingItemService.deleteTrainingItemByIds(ids);
    }

    /**
     * 图片上传
     */
    @PostMapping("/uploadUrl")
    public AjaxResult uploadUrl(@RequestParam("file") MultipartFile multipartFile, String type) throws Exception {
        if (StringUtils.isBlank(type)) {
            return AjaxResult.error("请选择所要上传的路径类型！");
        }
        if (Objects.isNull(multipartFile) || StringUtils.isBlank(multipartFile.getOriginalFilename())) {
            return AjaxResult.error("当前文件不存在，无法上传！");
        }
        if (multipartFile.getOriginalFilename().contains(Constants.EMPTY)) {
            return AjaxResult.error("当前文件名含有空格，请先去除空格在上传！");
        }
        return trainingItemService.uploadUrl(multipartFile, type);
    }

    /**
     * 上传视频
     */
    @PostMapping("/uploadVideoUrl")
    public AjaxResult uploadVideoUrl(@RequestParam("file") MultipartFile multipartFile) throws Exception {
        if (Objects.isNull(multipartFile)) {
            return AjaxResult.error("请选择视频！");
        }
        return trainingItemService.uploadVideoUrl(multipartFile);
    }

    /**
     * 父级项目组
     */
    @GetMapping("/selectTrainingParent")
    public TableDataInfo selectTrainingParent(TrainingItemAndDirectory trainingItem) {
        startPage();
        List<TrainingItemAndCategoryVO> list = trainingItemService.selectTrainingParent(trainingItem);
        return getDataTable(list);
    }

    /**
     * 修改上下架状态
     */
    @PostMapping("/updateTrainingShelfFlag")
    public AjaxResult updateTrainingShelfFlag(@RequestBody TrainingItem trainingItem) {
        if (Objects.isNull(trainingItem) || Objects.isNull(trainingItem.getId()) || Objects.isNull(trainingItem.getTrainingShelfFlag())) {
            return AjaxResult.error("请选择学习培训项目信息！");
        }
        return trainingItemService.updateTrainingShelfFlag(trainingItem);
    }

    /**
     * 查询下级项目
     */
    @GetMapping("selectSubordinateItem")
    public AjaxResult selectSubordinateItem(TrainingItem trainingItem) {
        if (Objects.isNull(trainingItem) || Objects.isNull(trainingItem.getTrainingParentId())) {
            return AjaxResult.error("请选择需要查询的项目信息！");
        }
        return trainingItemService.selectSubordinateItem(trainingItem);
    }
}