package com.ruoyi.web.controller.school;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TbSchoolMajor;
import com.ruoyi.system.service.ITbSchoolMajorService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学校专业Controller
 * 
 * @author ruoyi
 * @date 2022-08-29
 */
@Controller
@RequestMapping("/system/major")
public class TbSchoolMajorController extends BaseController
{
    private String prefix = "system/major";

    @Autowired
    private ITbSchoolMajorService tbSchoolMajorService;

    @RequiresPermissions("system:major:view")
    @GetMapping()
    public String major()
    {
        return prefix + "/major";
    }

    /**
     * 查询学校专业列表
     */
    @RequiresPermissions("system:major:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TbSchoolMajor tbSchoolMajor)
    {
        startPage();
        List<TbSchoolMajor> list = tbSchoolMajorService.selectTbSchoolMajorList(tbSchoolMajor);
        return getDataTable(list);
    }

    /**
     * 导出学校专业列表
     */
    @RequiresPermissions("system:major:export")
    @Log(title = "学校专业", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TbSchoolMajor tbSchoolMajor)
    {
        List<TbSchoolMajor> list = tbSchoolMajorService.selectTbSchoolMajorList(tbSchoolMajor);
        ExcelUtil<TbSchoolMajor> util = new ExcelUtil<TbSchoolMajor>(TbSchoolMajor.class);
        return util.exportExcel(list, "学校专业数据");
    }

    /**
     * 新增学校专业
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存学校专业
     */
    @RequiresPermissions("system:major:add")
    @Log(title = "学校专业", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TbSchoolMajor tbSchoolMajor)
    {
        tbSchoolMajor.setCreator(getUserId());
        tbSchoolMajor.setCheckStatus(1);
        return toAjax(tbSchoolMajorService.insertTbSchoolMajor(tbSchoolMajor));
    }

    /**
     * 修改学校专业
     */
    @RequiresPermissions("system:major:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TbSchoolMajor tbSchoolMajor = tbSchoolMajorService.selectTbSchoolMajorById(id);
        mmap.put("tbSchoolMajor", tbSchoolMajor);
        List<Long> checkedList=new ArrayList<>();
        Arrays.asList(tbSchoolMajor.getNeedMaterial().split(",")).stream().forEach(s -> {
            checkedList.add(Long.parseLong(s));
        });
        mmap.put("checkedList",checkedList);
        return prefix + "/edit";
    }

    /**
     * 修改保存学校专业
     */
    @RequiresPermissions("system:major:edit")
    @Log(title = "学校专业", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TbSchoolMajor tbSchoolMajor)
    {
        tbSchoolMajor.setModifiedBy(getUserId());
        return toAjax(tbSchoolMajorService.updateTbSchoolMajor(tbSchoolMajor));
    }

    /**
     * 删除学校专业
     */
    @RequiresPermissions("system:major:remove")
    @Log(title = "学校专业", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tbSchoolMajorService.deleteTbSchoolMajorByIds(ids));
    }
}
