package com.ruoyi.web.controller.oa;

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

import com.ruoyi.oa.domain.CmcMaterial;
import com.ruoyi.oa.service.ICmcMaterialService;
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.oa.domain.CmcMaterialBuy;
import com.ruoyi.oa.service.ICmcMaterialBuyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * cmc耗材购买Controller
 * 
 * @author cmc
 * @date 2024-07-31
 */
@RestController
@RequestMapping("/oa/materialBuy")
public class CmcMaterialBuyController extends BaseController
{
    @Autowired
    private ICmcMaterialBuyService cmcMaterialBuyService;

    @Autowired
    private ICmcMaterialService cmcMaterialService;

    /**
     * 查询cmc耗材购买列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmcMaterialBuy cmcMaterialBuy)
    {
        startPage();
        List<CmcMaterialBuy> list = cmcMaterialBuyService.selectCmcMaterialBuyList(cmcMaterialBuy);
        return getDataTable(list);
    }

    /**
     * 导出cmc耗材购买列表
     */
    @Log(title = "cmc耗材购买", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmcMaterialBuy cmcMaterialBuy)
    {
        List<CmcMaterialBuy> list = cmcMaterialBuyService.selectCmcMaterialBuyList(cmcMaterialBuy);
        ExcelUtil<CmcMaterialBuy> util = new ExcelUtil<CmcMaterialBuy>(CmcMaterialBuy.class);
        util.exportExcel(response, list, "cmc耗材购买数据");
    }

    /**
     * 获取cmc耗材购买详细信息
     */
    @GetMapping(value = "/{buyId}")
    public AjaxResult getInfo(@PathVariable("buyId") Integer buyId)
    {
        return success(cmcMaterialBuyService.selectCmcMaterialBuyByBuyId(buyId));
    }

    /**
     * 新增cmc耗材购买
     */
    @Log(title = "cmc耗材购买", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmcMaterialBuy cmcMaterialBuy)
    {
        CmcMaterial cmcMaterial = cmcMaterialService.selectCmcMaterialByMaterialId(cmcMaterialBuy.getMaterialId());
        cmcMaterial.setRemain(cmcMaterial.getRemain() + cmcMaterialBuy.getTotalNum());
        cmcMaterialService.updateCmcMaterial(cmcMaterial);
        return toAjax(cmcMaterialBuyService.insertCmcMaterialBuy(cmcMaterialBuy));
    }

    /**
     * 修改cmc耗材购买
     */
    @Log(title = "cmc耗材购买", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmcMaterialBuy cmcMaterialBuy)
    {
        return toAjax(cmcMaterialBuyService.updateCmcMaterialBuy(cmcMaterialBuy));
    }

    /**
     * 删除cmc耗材购买
     */
    @Log(title = "cmc耗材购买", businessType = BusinessType.DELETE)
	@DeleteMapping("/{buyIds}")
    public AjaxResult remove(@PathVariable Integer[] buyIds)
    {
        return success(cmcMaterialBuyService.deleteCmcMaterialBuyByBuyIds(buyIds));
    }
}
