package com.ruoyi.web.controller.material;

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

import com.ruoyi.clMaterial.domain.CYlMaterial;
import com.ruoyi.clMaterial.service.ICYlMaterialService;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.material.domain.CMaterial;
import com.ruoyi.materialType.domain.CMaterialType;
import com.ruoyi.materialType.service.ICMaterialTypeService;
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.material.service.ICMaterialService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物料管理Controller
 *
 * @author ruoyi
 * @date 2024-02-28
 */
@RestController
@RequestMapping("/material/material")
@DataSource(DataSourceType.QUOT)
public class CMaterialController extends BaseController
{
    @Autowired
    private ICMaterialService cMaterialService;

    @Autowired
    private ICYlMaterialService cYlMaterialService;

    @Autowired
    private ICMaterialTypeService cMaterialTypeService;


    /**
     * 查询物料管理列表
     */
    @PreAuthorize("@ss.hasPermi('material:material:list')")
    @GetMapping("/list")
    public TableDataInfo list(CMaterial cMaterial)
    {

        CMaterial A = new CMaterial();
        startPage();
        List<CMaterial> list = cMaterialService.selectCMaterialList(cMaterial);
        return getDataTable(list);
    }

    /**
     * 物料类别列表
     */
    @PreAuthorize("@ss.hasPermi('material:material:list')")
    @GetMapping("/cTypelist")
    public AjaxResult cTypelist(CMaterialType cMaterialType)
    {
        AjaxResult ajax = AjaxResult.success();
/*
        ajax.put("cTypeList", cFactoryService.selectCTypelist());
*/
        ajax.put("cTypeList", cMaterialTypeService.selectCMaterialTypeList(cMaterialType));
        return ajax;
    }

    /**
     * 查询材料管理列表
     */
    @PreAuthorize("@ss.hasPermi('material:material:list')")
    @GetMapping("/clMaterialList")
    public AjaxResult clMaterialList(CYlMaterial cYlMaterial)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("cmaterials", cYlMaterialService.selectCYlMaterialList(cYlMaterial));
        return ajax;
    }

    /**
     * 导出物料管理列表
     */
    @PreAuthorize("@ss.hasPermi('material:material:export')")
    @Log(title = "物料管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CMaterial cMaterial)
    {
        List<CMaterial> list = cMaterialService.selectCMaterialList(cMaterial);
        ExcelUtil<CMaterial> util = new ExcelUtil<CMaterial>(CMaterial.class);
        util.exportExcel(response, list, "物料管理数据");
    }

    /**
     * 获取物料管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('material:material:query')")
    @GetMapping(value = "/{materialId}")
    public AjaxResult getInfo(@PathVariable("materialId") Long materialId)
    {
        CMaterial c = cMaterialService.selectCMaterialByMaterialId(materialId);
        return success(c);
    }

    /**
     * 新增物料管理
     */
    @PreAuthorize("@ss.hasPermi('material:material:add')")
    @Log(title = "物料管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CMaterial cMaterial)
    {
        return toAjax(cMaterialService.insertCMaterial(cMaterial));
    }

    /**
     * 修改物料管理
     */
    @PreAuthorize("@ss.hasPermi('material:material:edit')")
    @Log(title = "物料管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CMaterial cMaterial)
    {
        return toAjax(cMaterialService.updateCMaterial(cMaterial));
    }

    /**
     * 删除物料管理
     */
    @PreAuthorize("@ss.hasPermi('material:material:remove')")
    @Log(title = "物料管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{materialIds}")
    public AjaxResult remove(@PathVariable Long[] materialIds)
    {
        return toAjax(cMaterialService.deleteCMaterialByMaterialIds(materialIds));
    }
}
