package com.ktg.mes.pro.controller;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import cn.hutool.core.collection.CollectionUtil;
import com.ktg.common.annotation.RepeatSubmit;
import com.ktg.common.utils.StringUtils;
import com.ktg.mes.md.domain.MdItem;
import com.ktg.mes.md.service.IMdItemService;
import com.ktg.mes.pro.domain.ProAssemble;
import com.ktg.mes.pro.service.IProAssembleService;
import com.ktg.mes.wm.domain.WmMaterialStock;
import com.ktg.mes.wm.domain.WmStorageArea;
import com.ktg.mes.wm.domain.WmStorageLocation;
import com.ktg.mes.wm.domain.WmWarehouse;
import com.ktg.mes.wm.service.IWmMaterialStockService;
import com.ktg.mes.wm.service.IWmStorageAreaService;
import com.ktg.mes.wm.service.IWmStorageLocationService;
import com.ktg.mes.wm.service.IWmWarehouseService;
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.ktg.common.annotation.Log;
import com.ktg.common.core.controller.BaseController;
import com.ktg.common.core.domain.AjaxResult;
import com.ktg.common.enums.BusinessType;
import com.ktg.mes.pro.domain.ProAssembleLine;
import com.ktg.mes.pro.service.IProAssembleLineService;
import com.ktg.common.utils.poi.ExcelUtil;
import com.ktg.common.core.page.TableDataInfo;
/**
* 产品组装明细Controller
*
* @author Brant
* @date 2024-09-16
*/
@RestController
@RequestMapping("/pro/assembleLine")
public class ProAssembleLineController extends BaseController
{
@Autowired
private IProAssembleService proAssembleService;
@Autowired
private IProAssembleLineService proAssembleLineService;
@Autowired
private IWmMaterialStockService wmMaterialStockService;
@Autowired
private IWmWarehouseService wmWarehouseService;
@Autowired
private IWmStorageLocationService wmStorageLocationService;
@Autowired
private IWmStorageAreaService wmStorageAreaService;
@Autowired
private IMdItemService mdItemService;
/**
* 查询产品组装明细列表
*/
@PreAuthorize("@ss.hasPermi('pro:assembleLine:list')")
@GetMapping("/list")
public TableDataInfo list(ProAssembleLine proAssembleLine)
{
startPage();
List<ProAssembleLine> list = proAssembleLineService.selectProAssembleLineList(proAssembleLine);
return getDataTable(list);
}
/**
* 导出产品组装明细列表
*/
@PreAuthorize("@ss.hasPermi('pro:assembleLine:export')")
@Log(title = "产品组装明细", businessType = BusinessType.EXPORT)
@PostMapping("/export")
public void export(HttpServletResponse response, ProAssembleLine proAssembleLine)
{
List<ProAssembleLine> list = proAssembleLineService.selectProAssembleLineList(proAssembleLine);
ExcelUtil<ProAssembleLine> util = new ExcelUtil<ProAssembleLine>(ProAssembleLine.class);
util.exportExcel(response, list, "产品组装明细数据");
}
/**
* 获取产品组装明细详细信息
*/
@PreAuthorize("@ss.hasPermi('pro:assembleLine:query')")
@GetMapping(value = "/{lineId}")
public AjaxResult getInfo(@PathVariable("lineId") Long lineId)
{
return AjaxResult.success(proAssembleLineService.selectProAssembleLineByLineId(lineId));
}
/**
* 新增产品组装明细
*/
@PreAuthorize("@ss.hasPermi('pro:assembleLine:add')")
@Log(title = "产品组装明细", businessType = BusinessType.INSERT)
@PostMapping
@RepeatSubmit()
public AjaxResult add(@RequestBody ProAssembleLine proAssembleLine)
{
return toAjax(proAssembleLineService.insertProAssembleLine(proAssembleLine));
}
/**
* 修改产品组装明细
*/
@PreAuthorize("@ss.hasPermi('pro:assembleLine:edit')")
@Log(title = "产品组装明细", businessType = BusinessType.UPDATE)
@PutMapping
public AjaxResult edit(@RequestBody ProAssembleLine proAssembleLine)
{
return toAjax(proAssembleLineService.updateProAssembleLine(proAssembleLine));
}
@PreAuthorize("@ss.hasPermi('pro:assembleLine:edit')")
@Log(title = "产品组装明细", businessType = BusinessType.UPDATE)
@PutMapping(value="/tran")
@RepeatSubmit()
public AjaxResult tran(@RequestBody List<ProAssembleLine> proAssembleLineList)
{
logger.info("产品组装过帐功能开始");
logger.info(String.valueOf(proAssembleLineList));
WmMaterialStock wms=new WmMaterialStock();
List<WmMaterialStock> wmsList=new ArrayList<>();
String warehouseCode="LEW";
String itemCode="";
String itemName="";
BigDecimal quantityOnhand=new BigDecimal(0);
//扣帐前数量
BigDecimal quantityOnhandBefore=new BigDecimal(0);
//扣帐数量
BigDecimal quantityTransaction=new BigDecimal(0);
//待扣帐数量
BigDecimal quantityTransactionGap=new BigDecimal(0);
//扣帐后数量
BigDecimal quantityOnhandAfter=new BigDecimal(0);
int rowCount=0;
int i=0;
if (CollectionUtil.isEmpty(proAssembleLineList)){
return AjaxResult.error("产品组装明细不能为空!");
}
ProAssemble proAssemble=new ProAssemble();
proAssemble=proAssembleService.selectProAssembleByAssembleCode(proAssembleLineList.get(0).getAssembleCode());
if (StringUtils.isNull(proAssemble)){
return AjaxResult.error("产品组装单号不存在!");
}
if ("2".equals(proAssemble.getStatus())){
return AjaxResult.error("产品组装单号状态为2(已过帐状态),不能重复过帐");
}
//判断物料是否存在
MdItem assembleItem=mdItemService.selectMdItemByItemCode(proAssemble.getItemCode());
if (StringUtils.isNull(assembleItem)){
return AjaxResult.error("产品组装母件料号不存在,母件料号"+proAssemble.getItemCode());
}
//判断线边仓是否有库存,循环每个料号,判断库存
for (ProAssembleLine assembleLine:proAssembleLineList) {
quantityOnhand=new BigDecimal(0);
itemCode=assembleLine.getItemCode();
itemName=assembleLine.getItemName();
if (StringUtils.isNull(assembleLine.getQuantityTransaction())){
return AjaxResult.error("料号过账数量不能为空,料号:"+itemCode+" "+itemName+" ");
}
wmsList=wmMaterialStockService.selectWmMaterialStockListByWarehouseCodeItemCode(warehouseCode,itemCode);
if (CollectionUtil.isEmpty(wmsList)){
return AjaxResult.error("料号线边仓无库存,料号:"+itemCode+" "+itemName+" 线边仓:LEW");
}
//计算线边仓的总数
for (WmMaterialStock tempwms: wmsList) {
//计算每个料的线边仓总数量
quantityOnhand=tempwms.getQuantityOnhand().add(quantityOnhand);
}
//计算过帐数量和线边仓总数的关系
if (assembleLine.getQuantityTransaction().compareTo(quantityOnhand) >0){
//计算线边仓缺数数量
quantityTransactionGap=assembleLine.getQuantityTransaction().subtract(quantityOnhand);
return AjaxResult.error("料号线边仓库存不满足过帐数,料号:"+itemCode+" "+itemName+"过账数量:"+assembleLine.getQuantityTransaction()+" 线边仓总数量:"+String.valueOf(quantityOnhand)+" 线边仓缺料数量:"+quantityTransactionGap);
}
}
//扣帐操作----------------------------------------------------------------------------
//过帐功能,扣除线边仓库存的数,如果多个库位有库存,已扣除数据少的库位
for (ProAssembleLine assembleLine:proAssembleLineList) {
quantityOnhand=new BigDecimal(0);
itemCode=assembleLine.getItemCode();
itemName=assembleLine.getItemName();
//组装扣料需求数量
quantityTransaction=assembleLine.getQuantityTransaction();
wmsList=wmMaterialStockService.selectWmMaterialStockListByWarehouseCodeItemCode(warehouseCode,itemCode);
rowCount=1;
quantityTransactionGap=new BigDecimal(0);
//过帐计算线边仓库的库存数量
for (WmMaterialStock tempwms: wmsList) {
if (rowCount==1) {
//线边仓第一个库位的数量满足扣帐数量,则直接进行扣除功能
if (quantityTransaction.compareTo(tempwms.getQuantityOnhand()) <= 0) {
quantityOnhandBefore=tempwms.getQuantityOnhand();
quantityOnhandAfter=quantityOnhandBefore.subtract(quantityTransaction);
tempwms.setQuantityOnhand(quantityOnhandAfter);
tempwms.setAttr1(assembleLine.getAssembleCode());
//更新物料在库的数量
wmMaterialStockService.updateWmMaterialStock(tempwms);
logger.info("线边仓第一个库位的数量满足扣帐数量,扣帐前库存数量:"+tempwms.getQuantityOnhand()+" 扣账数量:"+quantityTransaction+"扣帐后库存数量:"+quantityOnhandBefore);
logger.info(String.valueOf(tempwms));
break;
} else {
quantityOnhandBefore=tempwms.getQuantityOnhand();
quantityTransactionGap=quantityTransaction.subtract(quantityOnhandBefore);
tempwms.setQuantityOnhand(new BigDecimal(0));
tempwms.setAttr1(assembleLine.getAssembleCode());
//更新物料在库的数量
wmMaterialStockService.updateWmMaterialStock(tempwms);
logger.info("线边仓第一个库位的数量不满足扣帐数量,扣除所有数量,需求数量:"+quantityTransaction+"库存和扣帐数量:"+quantityOnhandBefore+" 待扣帐数量:"+quantityTransactionGap);
logger.info(String.valueOf(tempwms));
}
}
if (rowCount>1) {
//待扣帐数据小于在库数量
if (quantityTransactionGap.compareTo(tempwms.getQuantityOnhand())<0){
quantityOnhandBefore=tempwms.getQuantityOnhand();
quantityTransactionGap=quantityTransactionGap.subtract(quantityOnhandBefore);
tempwms.setQuantityOnhand(new BigDecimal(0));
tempwms.setAttr1(assembleLine.getAssembleCode());
//更新物料在库的数量
wmMaterialStockService.updateWmMaterialStock(tempwms);
logger.info("线边仓第"+rowCount+"个库位的数量不满足扣帐数量,需求数量:"+quantityTransaction+"库存和扣帐数量:"+quantityOnhandBefore+" 待扣帐数量:"+quantityTransactionGap);
logger.info(String.valueOf(tempwms));
}
else {
quantityOnhandBefore=tempwms.getQuantityOnhand();
quantityOnhandAfter=quantityOnhandBefore.subtract(quantityTransactionGap);
tempwms.setQuantityOnhand(quantityOnhandAfter);
tempwms.setAttr1(assembleLine.getAssembleCode());
//更新物料在库的数量
wmMaterialStockService.updateWmMaterialStock(tempwms);
logger.info("线边仓第"+rowCount+"个库位的数量满足扣帐数量,扣帐前库存数量:"+tempwms.getQuantityOnhand()+" 扣账数量:"+quantityTransactionGap+"扣帐后库存数量:"+quantityOnhandBefore+" 待扣帐数量:"+quantityTransactionGap);
logger.info(String.valueOf(tempwms));
break;
}
}
//循环下一个库位的库存数量
rowCount+=1;
}
assembleLine.setRemark("扣帐完成");
proAssembleLineService.updateProAssembleLine(assembleLine);
}
//组装成品料号作入库作业
List<WmMaterialStock> wmsAssoembleList=wmMaterialStockService.selectWmMaterialStockListByWarehouseCodeItemCode(warehouseCode,proAssemble.getItemCode());
if (CollectionUtil.isEmpty(wmsAssoembleList)){
logger.info("产品组装成品无线边仓库存资料,新增入库资料");
//得到仓库的资料
WmWarehouse wh=wmWarehouseService.selectWmWarehouseByWarehouseCode(warehouseCode);
if (StringUtils.isNull(wh)){
return AjaxResult.error("仓库资料不在存在:仓库代码:"+warehouseCode);
}
WmStorageLocation wsl=new WmStorageLocation();
wsl.setWarehouseCode(warehouseCode);
List<WmStorageLocation> whLocationList=wmStorageLocationService.selectWmStorageLocationList(wsl);
if (CollectionUtil.isEmpty(whLocationList)){
return AjaxResult.error("仓库库区不在存在:仓库代码:"+warehouseCode);
}
//ArrayList<Long> whLocationIds=new ArrayList<Long>();
Long[] locationIds=new Long[whLocationList.size()];
i=0;
for (WmStorageLocation tempLocation: whLocationList) {
locationIds[i]=tempLocation.getLocationId();
i+=1;
}
List<WmStorageArea> whAreaList= wmStorageAreaService.selectWmStorageAreaListByLocationIds(locationIds);
logger.info("库位信息");
logger.info(String.valueOf(whAreaList));
if (CollectionUtil.isEmpty(whAreaList))
{
return AjaxResult.error("此仓库无库位数据,不能组装过帐功能,仓库编码:"+warehouseCode);
}
WmStorageArea wmStorageAreaStockIn=whAreaList.get(0);
//生成入库单信息,库位取预设第一个来入库数量
WmMaterialStock assembleStockIn=new WmMaterialStock();
assembleStockIn.setItemTypeId(assembleItem.getItemTypeId());
assembleStockIn.setItemId(assembleItem.getItemId());
assembleStockIn.setItemCode(assembleItem.getItemCode());
assembleStockIn.setItemName(assembleItem.getItemName());
assembleStockIn.setSpecification(assembleItem.getSpecification());
assembleStockIn.setUnitOfMeasure(assembleItem.getUnitOfMeasure());
assembleStockIn.setMeasureName(assembleItem.getMeasureName());
assembleStockIn.setWarehouseId(wh.getWarehouseId());
assembleStockIn.setWarehouseCode(wh.getWarehouseCode());
assembleStockIn.setWarehouseName(wh.getWarehouseName());
assembleStockIn.setLocationId(wmStorageAreaStockIn.getLocationId());
assembleStockIn.setLocationCode(wmStorageAreaStockIn.getLocationCode());
assembleStockIn.setLocationName(wmStorageAreaStockIn.getLocationName());
assembleStockIn.setAreaId(wmStorageAreaStockIn.getAreaId());
assembleStockIn.setAreaCode(wmStorageAreaStockIn.getAreaCode());
assembleStockIn.setAreaName(wmStorageAreaStockIn.getAreaName());
//预设为0批次
assembleStockIn.setBatchCode("0");
assembleStockIn.setQuantityOnhand(proAssemble.getQuantityRequest());
assembleStockIn.setTranCode(proAssemble.getAssembleCode());
wmMaterialStockService.insertWmMaterialStock(assembleStockIn);
}else
{
logger.info("产品组装成品有线边仓库存资料,更新库存资料");
WmMaterialStock assembleStockIn=wmsAssoembleList.get(0);
assembleStockIn.setQuantityOnhand(assembleStockIn.getQuantityOnhand().add(proAssemble.getQuantityRequest()));
assembleStockIn.setUpdateBy(getUsername());
assembleStockIn.setUpdateTime(new Date());
assembleStockIn.setTranCode(proAssemble.getAssembleCode());
wmMaterialStockService.updateWmMaterialStock(assembleStockIn);
}
//更新组装单的状态,为2过帐状态
proAssemble.setStatus("2");
proAssemble.setAssembleUserName(getUsername());
proAssemble.setUpdateBy(getUsername());
proAssemble.setUpdateTime(new Date());
proAssembleService.updateProAssemble(proAssemble);
logger.info("产品组装过帐功能结束");
return AjaxResult.success("产品组装过帐成功",proAssembleLineList);
}
/**
* 删除产品组装明细
*/
@PreAuthorize("@ss.hasPermi('pro:assembleLine:remove')")
@Log(title = "产品组装明细", businessType = BusinessType.DELETE)
@DeleteMapping("/{lineIds}")
public AjaxResult remove(@PathVariable Long[] lineIds)
{
return toAjax(proAssembleLineService.deleteProAssembleLineByLineIds(lineIds));
}
}