package com.ruoyi.web.controller.materialType;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
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.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.materialType.domain.CMaterialType;
import com.ruoyi.materialType.service.ICMaterialTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物料类别Controller
 *
 * @author ruoyi
 * @date 2024-03-01
 */
@RestController
@RequestMapping("/materialType/materialType")
@DataSource(DataSourceType.QUOT)
public class CMaterialTypeController extends BaseController
{
    @Autowired
    private ICMaterialTypeService cMaterialTypeService;

    /**
     * 查询物料类别列表
     */
    @PreAuthorize("@ss.hasPermi('materialType:materialType:list')")
    @GetMapping("/list")
    public TableDataInfo list(CMaterialType cMaterialType)
    {
        startPage();
        List<CMaterialType> list = cMaterialTypeService.selectCMaterialTypeList(cMaterialType);
        return getDataTable(list);
    }

    /**
     * 导出物料类别列表
     */
    @PreAuthorize("@ss.hasPermi('materialType:materialType:export')")
    @Log(title = "物料类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CMaterialType cMaterialType)
    {
        List<CMaterialType> list = cMaterialTypeService.selectCMaterialTypeList(cMaterialType);
        ExcelUtil<CMaterialType> util = new ExcelUtil<CMaterialType>(CMaterialType.class);
        util.exportExcel(response, list, "物料类别数据");
    }

    /**
     * 获取物料类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('materialType:materialType:query')")
    @GetMapping(value = "/{typeId}")
    public AjaxResult getInfo(@PathVariable("typeId") Long typeId)
    {
        return success(cMaterialTypeService.selectCMaterialTypeByTypeId(typeId));
    }

    /**
     * 新增物料类别
     */
    @PreAuthorize("@ss.hasPermi('materialType:materialType:add')")
    @Log(title = "物料类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CMaterialType cMaterialType)
    {

        if (!cMaterialTypeService.checkTypeNoUnique(cMaterialType))
        {
            return error("添加物料类型失败,编码:'" + cMaterialType.getTypeNo() + "' 已存在");
        }
        return toAjax(cMaterialTypeService.insertCMaterialType(cMaterialType));
    }

    /**
     * 修改物料类别
     */
    @PreAuthorize("@ss.hasPermi('materialType:materialType:edit')")
    @Log(title = "物料类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CMaterialType cMaterialType)
    {
        return toAjax(cMaterialTypeService.updateCMaterialType(cMaterialType));
    }

    /**
     * 删除物料类别
     */
    @PreAuthorize("@ss.hasPermi('materialType:materialType:remove')")
    @Log(title = "物料类别", businessType = BusinessType.DELETE)
	@DeleteMapping("/{typeIds}")
    public AjaxResult remove(@PathVariable Long[] typeIds)
    {
        return toAjax(cMaterialTypeService.deleteCMaterialTypeByTypeIds(typeIds));
    }
}
