package com.zy.asrs.common.wms.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.zy.asrs.common.domain.dto.DetlDto; import com.zy.asrs.common.domain.param.OpenOrderPakinParam; import com.zy.asrs.common.domain.param.OpenOrderPakoutParam; import com.zy.asrs.common.wms.entity.DocType; import com.zy.asrs.common.wms.entity.Mat; import com.zy.asrs.common.wms.entity.Order; import com.zy.asrs.common.wms.entity.OrderDetl; import com.zy.asrs.common.wms.service.*; import com.zy.asrs.framework.common.Cools; import com.zy.asrs.framework.common.DateUtils; import com.zy.asrs.framework.common.SnowflakeIdWorker; import com.zy.asrs.framework.exception.CoolException; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * Created by vincent on 2022/4/9 */ @Slf4j @Service public class OpenServiceImpl implements OpenService { @Autowired private OrderService orderService; @Autowired private OrderDetlService orderDetlService; @Autowired private DocTypeService docTypeService; @Autowired private SnowflakeIdWorker snowflakeIdWorker; @Autowired private MatService matService; @Override @Transactional public void pakinOrderCreate(OpenOrderPakinParam param) { Order order = orderService.selectByNo(param.getOrderNo(), param.getHostId()); if (!Cools.isEmpty(order)) { throw new CoolException(param.getOrderNo() + "单据已存在,请勿重复提交"); } DocType docType = docTypeService.selectOrAdd(param.getOrderType(), Boolean.TRUE); Date now = new Date(); // 单据主档 order = new Order( String.valueOf(snowflakeIdWorker.nextId()), // 编号[非空] param.getOrderNo(), // 订单编号 DateUtils.convert(now), // 单据日期 docType.getDocId(), // 单据类型 null, // 项目编号 null, // null, // 调拨项目编号 null, // 初始票据号 null, // 票据号 null, // 客户编号 null, // 客户 null, // 联系方式 null, // 操作人员 null, // 合计金额 null, // 优惠率 null, // 优惠金额 null, // 销售或采购费用合计 null, // 实付金额 null, // 付款类型 null, // 业务员 null, // 结算天数 null, // 邮费支付类型 null, // 邮费 null, // 付款时间 null, // 发货时间 null, // 物流名称 null, // 物流单号 1L, // 订单状态 1, // 状态 9527L, // 添加人员 now, // 添加时间 9527L, // 修改人员 now, // 修改时间 null, // 备注 param.getHostId() ); if (!orderService.save(order)) { throw new CoolException("生成单据主档失败,请联系管理员"); } // 单据明细档 List list = new ArrayList<>(); List orderDetails = param.getOrderDetails(); for (DetlDto detail : orderDetails) { DetlDto dto = new DetlDto(detail.getMatnr(), detail.getBatch(), detail.getAnfme()); if (DetlDto.has(list, dto)) { DetlDto detlDto = DetlDto.find(list, dto.getMatnr(), dto.getBatch()); assert detlDto != null; detlDto.setAnfme(detlDto.getAnfme() + detail.getAnfme()); } else { list.add(dto); } } for (DetlDto detlDto : list) { Mat mat = matService.getOne(new LambdaQueryWrapper().eq(Mat::getMatnr, detlDto.getMatnr()).eq(Mat::getHostId, param.getHostId())); if (Cools.isEmpty(mat)) { throw new CoolException(detlDto.getMatnr() + "编号商品检索失败,请先添加商品"); } OrderDetl orderDetl = new OrderDetl(); orderDetl.sync(mat); orderDetl.setBatch(detlDto.getBatch()); orderDetl.setAnfme(detlDto.getAnfme()); orderDetl.setOrderId(order.getId()); orderDetl.setOrderNo(order.getOrderNo()); orderDetl.setCreateBy(9527L); orderDetl.setCreateTime(now); orderDetl.setUpdateBy(9527L); orderDetl.setUpdateTime(now); orderDetl.setStatus(1); orderDetl.setQty(0.0D); if (!orderDetlService.save(orderDetl)) { throw new CoolException("生成单据明细失败,请联系管理员"); } } } @Override @Transactional public void pakoutOrderCreate(OpenOrderPakoutParam param) { Order order = orderService.selectByNo(param.getOrderNo(), param.getHostId()); // 如果单据不存在则添加;如果单据存在,作业中无法修改,反之则修改单据 if (!Cools.isEmpty(order)) { if (order.getSettle() > 1L) { throw new CoolException(param.getOrderNo() + "正在出库,无法修改单据"); } orderService.removeById(order.getId()); } DocType docType = docTypeService.selectOrAdd(param.getOrderType(), Boolean.FALSE); Date now = new Date(); // 单据主档 order = new Order( String.valueOf(snowflakeIdWorker.nextId()), // 编号[非空] param.getOrderNo(), // 订单编号 DateUtils.convert(now), // 单据日期 docType.getDocId(), // 单据类型 null, // 项目编号 null, // null, // 调拨项目编号 null, // 初始票据号 null, // 票据号 null, // 客户编号 null, // 客户 null, // 联系方式 null, // 操作人员 null, // 合计金额 null, // 优惠率 null, // 优惠金额 null, // 销售或采购费用合计 null, // 实付金额 null, // 付款类型 null, // 业务员 null, // 结算天数 null, // 邮费支付类型 null, // 邮费 null, // 付款时间 null, // 发货时间 null, // 物流名称 null, // 物流单号 1L, // 订单状态 1, // 状态 9527L, // 添加人员 now, // 添加时间 9527L, // 修改人员 now, // 修改时间 null, // 备注 param.getHostId() ); if (!orderService.save(order)) { throw new CoolException("生成单据主档失败,请联系管理员"); } // 单据明细档 List list = new ArrayList<>(); List orderDetails = param.getOrderDetails(); for (DetlDto detail : orderDetails) { DetlDto dto = new DetlDto(detail.getMatnr(), detail.getBatch(), detail.getAnfme()); if (DetlDto.has(list, dto)) { DetlDto detlDto = DetlDto.find(list, dto.getMatnr(), dto.getBatch()); assert detlDto != null; detlDto.setAnfme(detlDto.getAnfme() + detail.getAnfme()); } else { list.add(dto); } } for (DetlDto detlDto : list) { Mat mat = matService.getOne(new LambdaQueryWrapper().eq(Mat::getMatnr, detlDto.getMatnr()).eq(Mat::getHostId, param.getHostId())); if (Cools.isEmpty(mat)) { throw new CoolException(detlDto.getMatnr() + "编号商品检索失败,请先添加商品"); } OrderDetl orderDetl = new OrderDetl(); orderDetl.sync(mat); orderDetl.setBatch(detlDto.getBatch()); orderDetl.setAnfme(detlDto.getAnfme()); orderDetl.setOrderId(order.getId()); orderDetl.setOrderNo(order.getOrderNo()); orderDetl.setCreateBy(9527L); orderDetl.setCreateTime(now); orderDetl.setUpdateBy(9527L); orderDetl.setUpdateTime(now); orderDetl.setStatus(1); orderDetl.setQty(0.0D); if (!orderDetlService.save(orderDetl)) { throw new CoolException("生成单据明细失败,请联系管理员"); } } } }