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.CmcMaterialGive;
import com.ruoyi.oa.service.ICmcMaterialGiveService;
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/materialGive")
public class CmcMaterialGiveController extends BaseController
{
    @Autowired
    private ICmcMaterialGiveService cmcMaterialGiveService;

    @Autowired
    private ICmcMaterialService cmcMaterialService;

    /**
     * 查询cmc耗材发放列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmcMaterialGive cmcMaterialGive)
    {
        startPage();
        List<CmcMaterialGive> list = cmcMaterialGiveService.selectCmcMaterialGiveList(cmcMaterialGive);
        return getDataTable(list);
    }

    /**
     * 导出cmc耗材发放列表
     */
    @Log(title = "cmc耗材发放", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmcMaterialGive cmcMaterialGive)
    {
        List<CmcMaterialGive> list = cmcMaterialGiveService.selectCmcMaterialGiveList(cmcMaterialGive);
        ExcelUtil<CmcMaterialGive> util = new ExcelUtil<CmcMaterialGive>(CmcMaterialGive.class);
        util.exportExcel(response, list, "cmc耗材发放数据");
    }

    /**
     * 获取cmc耗材发放详细信息
     */
    @GetMapping(value = "/{giveId}")
    public AjaxResult getInfo(@PathVariable("giveId") Integer giveId)
    {
        return success(cmcMaterialGiveService.selectCmcMaterialGiveByGiveId(giveId));
    }

    /**
     * 新增cmc耗材发放
     */
    @Log(title = "cmc耗材发放", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmcMaterialGive cmcMaterialGive)
    {
        CmcMaterial cmcMaterial = cmcMaterialService.selectCmcMaterialByMaterialId(cmcMaterialGive.getMaterialId());
        cmcMaterial.setRemain(cmcMaterial.getRemain() - cmcMaterialGive.getGiveNum());
        cmcMaterialService.updateCmcMaterial(cmcMaterial);
        return toAjax(cmcMaterialGiveService.insertCmcMaterialGive(cmcMaterialGive));
    }

    /**
     * 修改cmc耗材发放
     */
    @Log(title = "cmc耗材发放", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmcMaterialGive cmcMaterialGive)
    {
        return toAjax(cmcMaterialGiveService.updateCmcMaterialGive(cmcMaterialGive));
    }

    /**
     * 删除cmc耗材发放
     */
    @Log(title = "cmc耗材发放", businessType = BusinessType.DELETE)
	@DeleteMapping("/{giveIds}")
    public AjaxResult remove(@PathVariable Integer[] giveIds)
    {
        return success(cmcMaterialGiveService.deleteCmcMaterialGiveByGiveIds(giveIds));
    }
}
