package com.ruoyi.web.controller.ehs;

import java.math.BigDecimal;
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)
    {
    	if(ehsMaterialDetail.getOperateType()==0) {//出库指令新增
		    ehsMaterialDetail.setSendUserId(SecurityUtils.getUserId());
		    ehsMaterialDetail.setSendTime(new Date());
	    }else{//入库新增
		    //跟新明细表
		    ehsMaterialDetail.setOutboundTime(new Date());
		    ehsMaterialDetail.setOutboundUserId(SecurityUtils.getUserId());
	    }
	    //更新物资表数量
	    EhsMaterial ehsMaterial = ehsMaterialService.selectEhsMaterialByMaterialId(ehsMaterialDetail.getMaterialId());
	    BigDecimal string1 = new BigDecimal(ehsMaterial.getMaterialQuantity());//主表的数量
	    BigDecimal string2 = new BigDecimal(
	    		ehsMaterialDetail.getOperateType()==1?ehsMaterialDetail.getOutboundQuantity():ehsMaterialDetail.getSendQuantity());//入库指令的数量
	    BigDecimal result2 = string1.add(string2);//库存
	    ehsMaterial = new EhsMaterial();
	    ehsMaterial.setMaterialQuantity(ehsMaterialDetail.getOperateType()==1?result2.toString():null);
	    ehsMaterial.setMaterialId(ehsMaterialDetail.getMaterialId());
	    ehsMaterialDetail.setInventory(ehsMaterialDetail.getOperateType()==1?result2:string1);
	    ehsMaterialDetailService.save(ehsMaterialDetail);//
	    return toAjax(ehsMaterialService.updateById(ehsMaterial));
    }

    /**
     * 修改资源管理出库入库明细
     */
    @ApiOperation("执行出库指令")
    @PreAuthorize("@ss.hasPermi('EhsMaterialDetail:EhsMaterialDetail:edit')")
    @Log(title = "执行出库指令", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EhsMaterialDetail ehsMaterialDetail)
    {

	    EhsMaterialDetail md = new EhsMaterialDetail();
	    md.setMaterialId(ehsMaterialDetail.getMaterialId());
	    md.setGetOut("1");//查询操作人为空的
	    md.setOperateType(0);//查询出库操作
	    List<EhsMaterialDetail> list = ehsMaterialDetailService.selectEhsMaterialDetailList(md);
	    if(list.size()>0){
		    ehsMaterialDetail.setOutboundQuantity(list.get(0).getOutboundQuantity()) ;
		    //更新物资表数量
		    EhsMaterial ehsMaterial = ehsMaterialService.selectEhsMaterialByMaterialId(ehsMaterialDetail.getMaterialId());
		    BigDecimal string1 = new BigDecimal(ehsMaterial.getMaterialQuantity());//主表的数量
		    BigDecimal string2 = new BigDecimal(list.get(0).getSendQuantity());//出库指令的数量
		    BigDecimal result2 = string1.subtract(string2);//库存
		    ehsMaterial = new EhsMaterial();
		    ehsMaterial.setMaterialQuantity(result2.toString());
		    ehsMaterial.setMaterialId(ehsMaterialDetail.getMaterialId());
		    ehsMaterialService.updateById(ehsMaterial);
		    ////更新明细表
		    md = new EhsMaterialDetail();
		    md.setMaterialDetailId(list.get(0).getMaterialDetailId());
		    md.setOutboundTime(new Date());
		    md.setOutboundUserId(SecurityUtils.getUserId());
		    md.setOutboundQuantity(list.get(0).getSendQuantity());
		    md.setInventory(result2);
		    md.setReceiveName(ehsMaterialDetail.getReceiveName());
		    md.setReceiveTel(ehsMaterialDetail.getReceiveTel());
		    md.setHandledBy(ehsMaterialDetail.getHandledBy());
		    md.setOutTime(ehsMaterialDetail.getOutTime());
		    return toAjax(ehsMaterialDetailService.updateById(md));
	    }
	    return error("未查询到出库指令");
    }

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