package com.ruoyi.project.production.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
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.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.production.domain.TSaiCategory;
import com.ruoyi.project.production.service.ITSaiCategoryService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * SAI类别Controller
 *
 * @author ruoyi
 * @date 2023-04-18
 */
@RestController
@RequestMapping("/production/category")
public class TSaiCategoryController extends BaseController
{
    @Autowired
    private ITSaiCategoryService tSaiCategoryService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询SAI类别列表
     */
    @GetMapping("/categoryList")
    public AjaxResult categoryList()
    {
        return AjaxResult.success(tSaiCategoryService.selectTSaiCategoryList(null));
    }

    /**
     * 查询SAI类别列表
     */
    @PreAuthorize("@ss.hasPermi('production:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(TSaiCategory tSaiCategory)
    {
        startPage();
        List<TSaiCategory> list = tSaiCategoryService.selectTSaiCategoryList(tSaiCategory);
        return getDataTable(list);
    }

    /**
     * 导出SAI类别列表
     */
    @PreAuthorize("@ss.hasPermi('production:category:export')")
    @Log(title = "SAI类别", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TSaiCategory tSaiCategory)
    {
        List<TSaiCategory> list = tSaiCategoryService.selectTSaiCategoryList(tSaiCategory);
        ExcelUtil<TSaiCategory> util = new ExcelUtil<TSaiCategory>(TSaiCategory.class);
        return util.exportExcel(list, "category");
    }

    /**
     * 获取SAI类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('production:category:query')")
    @GetMapping(value = "/{saiCategoryId}")
    public AjaxResult getInfo(@PathVariable("saiCategoryId") Long saiCategoryId)
    {
        return AjaxResult.success(tSaiCategoryService.selectTSaiCategoryById(saiCategoryId));
    }

    /**
     * 新增SAI类别
     */
    @PreAuthorize("@ss.hasPermi('production:category:add')")
    @Log(title = "SAI类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TSaiCategory tSaiCategory)
    {
        Long userId = getUserId();
        SysUser sysUser = userService.selectUserById(userId);
        tSaiCategory.setDeptId(sysUser.getDeptId());
        tSaiCategory.setCreateBy(sysUser.getUserId().toString());
        tSaiCategory.setCreateTime(new Date());
        return toAjax(tSaiCategoryService.insertTSaiCategory(tSaiCategory));
    }

    /**
     * 修改SAI类别
     */
    @PreAuthorize("@ss.hasPermi('production:category:edit')")
    @Log(title = "SAI类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TSaiCategory tSaiCategory)
    {
        Long userId = getUserId();
        SysUser sysUser = userService.selectUserById(userId);
        tSaiCategory.setUpdateBy(sysUser.getUserId().toString());
        tSaiCategory.setUpdateTime(new Date());
        return toAjax(tSaiCategoryService.updateTSaiCategory(tSaiCategory));
    }

    /**
     * 删除SAI类别
     */
    @PreAuthorize("@ss.hasPermi('production:category:remove')")
    @Log(title = "SAI类别", businessType = BusinessType.DELETE)
	@DeleteMapping("/{saiCategoryIds}")
    public AjaxResult remove(@PathVariable Long[] saiCategoryIds)
    {
        return toAjax(tSaiCategoryService.deleteTSaiCategoryByIds(saiCategoryIds));
    }
}
