package com.ruoyi.web.controller.factory;

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

import com.ruoyi.clMaterial.domain.CYlMaterial;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
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.factory.domain.CFactory;
import com.ruoyi.factory.service.ICFactoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 车间管理Controller
 *
 * @author ruoyi
 * @date 2024-02-29
 */
@RestController
@RequestMapping("/factory/factory")
@DataSource(DataSourceType.QUOT)
public class CFactoryController extends BaseController
{
    @Autowired
    private ICFactoryService cFactoryService;

    @Autowired
    private ICMaterialTypeService cMaterialTypeService;

    /**
     * 查询车间管理列表
     */
    @PreAuthorize("@ss.hasPermi('factory:factory:list')")
    @GetMapping("/list")
    public TableDataInfo list(CFactory cFactory)
    {
        startPage();
        List<CFactory> list = cFactoryService.selectCFactoryList(cFactory);
        return getDataTable(list);
    }

    /**
     * 物料类别列表
     */
    @PreAuthorize("@ss.hasPermi('factory:factory: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('factory:factory:export')")
    @Log(title = "车间管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CFactory cFactory)
    {
        List<CFactory> list = cFactoryService.selectCFactoryList(cFactory);
        ExcelUtil<CFactory> util = new ExcelUtil<CFactory>(CFactory.class);
        util.exportExcel(response, list, "车间管理数据");
    }

    /**
     * 获取车间管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('factory:factory:query')")
    @GetMapping(value = "/{factoryId}")
    public AjaxResult getInfo(@PathVariable("factoryId") Long factoryId)
    {
        return success(cFactoryService.selectCFactoryByFactoryId(factoryId));
    }

    /**
     * 新增车间管理
     */
    @PreAuthorize("@ss.hasPermi('factory:factory:add')")
    @Log(title = "车间管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CFactory cFactory)
    {
        if (!cFactoryService.checkFactoryNoUnique(cFactory))
        {
            return error("添加车间失败,编码:'" + cFactory.getFactoryNo() + "' 已存在");
        }
        return toAjax(cFactoryService.insertCFactory(cFactory));
    }

    /**
     * 修改车间管理
     */
    @PreAuthorize("@ss.hasPermi('factory:factory:edit')")
    @Log(title = "车间管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CFactory cFactory)
    {
        return toAjax(cFactoryService.updateCFactory(cFactory));
    }

    /**
     * 删除车间管理
     */
    @PreAuthorize("@ss.hasPermi('factory:factory:remove')")
    @Log(title = "车间管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{factoryIds}")
    public AjaxResult remove(@PathVariable Long[] factoryIds)
    {
        return toAjax(cFactoryService.deleteCFactoryByFactoryIds(factoryIds));
    }
}
