package com.xinelu.manage.controller.trainingcategory;


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.enums.BusinessType;
import com.xinelu.common.utils.poi.ExcelUtil;
import com.xinelu.manage.domain.trainingcategory.TrainingCategory;
import com.xinelu.manage.service.trainingcategory.ITrainingCategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 学习培训分类Controller
 *
 * @author xinyilu
 * @date 2023-04-20
 */
@RestController
@RequestMapping("/system/trainingCategory")
public class TrainingCategoryController extends BaseController {
    @Resource
    private ITrainingCategoryService trainingCategoryService;

    /**
     * 查询学习培训分类列表
     */
    @PreAuthorize("@ss.hasPermi('system:trainingCategory:list')")
    @GetMapping("/list")
    public TableDataInfo list(TrainingCategory trainingCategory) {
        startPage();
        List<TrainingCategory> list = trainingCategoryService.selectTrainingCategoryList(trainingCategory);
        return getDataTable(list);
    }

    /**
     * 导出学习培训分类列表
     */
    @PreAuthorize("@ss.hasPermi('system:trainingCategory:export')")
    @Log(title = "学习培训分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TrainingCategory trainingCategory) {
        List<TrainingCategory> list = trainingCategoryService.selectTrainingCategoryList(trainingCategory);
        ExcelUtil<TrainingCategory> util = new ExcelUtil<>(TrainingCategory.class);
        util.exportExcel(response, list, "学习培训分类数据");
    }

    /**
     * 获取学习培训分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:trainingCategory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(trainingCategoryService.selectTrainingCategoryById(id));
    }

    /**
     * 新增学习培训分类
     */
    @PreAuthorize("@ss.hasPermi('system:trainingCategory:add')")
    @Log(title = "学习培训分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody TrainingCategory trainingCategory) {
        return toAjax(trainingCategoryService.insertTrainingCategory(trainingCategory));
    }

    /**
     * 修改学习培训分类
     */
    @PreAuthorize("@ss.hasPermi('system:trainingCategory:edit')")
    @Log(title = "学习培训分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody TrainingCategory trainingCategory) {
        return trainingCategoryService.updateTrainingCategory(trainingCategory);
    }

    /**
     * 删除学习培训分类
     */
    @PreAuthorize("@ss.hasPermi('system:trainingCategory:remove')")
    @Log(title = "学习培训分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return trainingCategoryService.deleteTrainingCategoryByIds(ids);
    }

    /**
     * 图片上传
     */
    @PostMapping("/uploadTrainingCategoryPicture")
    public AjaxResult uploadTrainingCategoryPicture(@RequestParam("file") MultipartFile multipartFile) throws Exception {
        if (Objects.isNull(multipartFile) || StringUtils.isBlank(multipartFile.getOriginalFilename())) {
            return AjaxResult.error("当前文件不存在，无法上传！");
        }
        if (multipartFile.getOriginalFilename().contains(Constants.EMPTY)) {
            return AjaxResult.error("当前文件名含有空格，请先去除空格在上传！");
        }
        return trainingCategoryService.uploadTrainingCategoryPicture(multipartFile);
    }
}