package com.ruoyi.web.controller.ehs;

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

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ehsMaterial.domain.EhsMaterial;
import com.ruoyi.ehsMaterial.service.IEhsMaterialService;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.ehsMaterialDetail.domain.EhsMaterialDetail;
import com.ruoyi.ehsMaterialDetail.service.IEhsMaterialDetailService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 资源管理出库入库明细Controller
 *
 * @author ruoyi
 * @date 2023-12-05
 */
@Api(tags="资源管理出库入库明细管理")
@RestController
@RequestMapping("/EhsMaterialDetail/EhsMaterialDetail")
public class EhsMaterialDetailController extends BaseController
{
    @Autowired
    private IEhsMaterialDetailService ehsMaterialDetailService;
	@Autowired
	private IEhsMaterialService ehsMaterialService;
/**
 * 查询资源管理出库入库明细列表
 */
@PreAuthorize("@ss.hasPermi('EhsMaterialDetail:EhsMaterialDetail:list')")
@GetMapping("/list")
	public TableDataInfo list(EhsMaterialDetail ehsMaterialDetail)
	{
		startPage();
		List<EhsMaterialDetail> list = ehsMaterialDetailService.selectEhsMaterialDetailList(ehsMaterialDetail);
		return getDataTable(list);
	}

	/**
	 * 导出资源管理出库入库明细列表
	 */
	@PreAuthorize("@ss.hasPermi('EhsMaterialDetail:EhsMaterialDetail:export')")
	@Log(title = "资源管理出库入库明细", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, EhsMaterialDetail ehsMaterialDetail)
	{
		List<EhsMaterialDetail> list = ehsMaterialDetailService.selectEhsMaterialDetailList(ehsMaterialDetail);
		ExcelUtil<EhsMaterialDetail> util = new ExcelUtil<EhsMaterialDetail>(EhsMaterialDetail.class);
		util.exportExcel(response, list, "资源管理出库入库明细数据");
	}

	/**
	 * 获取资源管理出库入库明细详细信息
	 */
	@PreAuthorize("@ss.hasPermi('EhsMaterialDetail:EhsMaterialDetail:query')")
	@GetMapping(value = "/{materialDetailId}")
	public AjaxResult getInfo(@PathVariable("materialDetailId") Long materialDetailId)
	{
		return success(ehsMaterialDetailService.selectEhsMaterialDetailByMaterialDetailId(materialDetailId));
	}

    /**
     * 新增资源管理出库入库明细
     */
    @ApiOperation("新增资源管理出库入库明细")
    @PreAuthorize("@ss.hasPermi('EhsMaterialDetail:EhsMaterialDetail:add')")
    @Log(title = "资源管理出库入库明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EhsMaterialDetail ehsMaterialDetail)
    {
	    ehsMaterialDetail.setSendTime(new Date());
	    ehsMaterialDetail.setSendUserId(SecurityUtils.getUserId());
        return toAjax(ehsMaterialDetailService.save(ehsMaterialDetail));
    }

    /**
     * 修改资源管理出库入库明细
     */
    @ApiOperation("执行出库指令")
    @PreAuthorize("@ss.hasPermi('EhsMaterialDetail:EhsMaterialDetail:edit')")
    @Log(title = "执行出库指令", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EhsMaterialDetail ehsMaterialDetail)
    {
	    ehsMaterialDetail.setOutboundTime(new Date());
	    ehsMaterialDetail.setOutboundUserId(SecurityUtils.getUserId());
	    EhsMaterialDetail md = ehsMaterialDetailService.selectEhsMaterialDetailByMaterialDetailId(ehsMaterialDetail.getMaterialDetailId());
	    ehsMaterialDetail.setOutboundQuantity(md.getOutboundQuantity()) ;
	    //更新物资表数量
	    EhsMaterial ehsMaterial = ehsMaterialService.selectEhsMaterialByMaterialId(ehsMaterialDetail.getMaterialDetailId());
	    ehsMaterial.setMaterialQuantity(String.valueOf(Long.parseLong(ehsMaterial.getMaterialQuantity())- Long.parseLong(md.getSendQuantity()) ));
	    return toAjax(ehsMaterialDetailService.updateById(ehsMaterialDetail));
    }

    /**
     * 删除资源管理出库入库明细
     */
    @ApiOperation("按物资表删除未执行出库指令")
    @PreAuthorize("@ss.hasPermi('EhsMaterialDetail:EhsMaterialDetail:remove')")
    @Log(title = "按物资表删除未执行出库指令", businessType = BusinessType.DELETE)
	@DeleteMapping("/{materialDetailIds}")
    public AjaxResult remove(@PathVariable Long  materialId)
    {
        //return toAjax(ehsMaterialDetailService.removeByIds(Arrays.asList(materialDetailIds)));
	    return toAjax(ehsMaterialDetailService.deleteEhsMaterialDetailByMaterialId(materialId));
    }
}
