自动化立体仓库 - WMS系统
#
luxiaotao1123
2022-04-09 65ad59fc53b0fee72bb06da3b89bcc4867848ee8
src/main/java/com/zy/asrs/controller/OpenController.java
@@ -1,18 +1,14 @@
package com.zy.asrs.controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.Cools;
import com.core.common.R;
import com.core.common.*;
import com.core.exception.CoolException;
import com.zy.asrs.entity.BasDevp;
import com.zy.asrs.entity.LocDetl;
import com.zy.asrs.entity.MatCode;
import com.zy.asrs.entity.WrkMast;
import com.zy.asrs.entity.param.OpenApiStockOutParam;
import com.zy.asrs.entity.result.PakoutVo;
import com.zy.asrs.entity.result.StoPreTab;
import com.zy.asrs.entity.DocType;
import com.zy.asrs.entity.Mat;
import com.zy.asrs.entity.Order;
import com.zy.asrs.entity.OrderDetl;
import com.zy.asrs.entity.param.OpenOrderPakinParam;
import com.zy.asrs.service.*;
import com.zy.common.model.LocDetlDto;
import com.zy.common.model.DetlDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
@@ -21,162 +17,135 @@
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
/**
 * Created by vincent on 2021/3/19
 * Created by vincent on 2022/4/8
 */
@RestController
@RequestMapping("/open/api")
@RequestMapping("open/asrs")
public class OpenController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetlService orderDetlService;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    private DocTypeService docTypeService;
    @Autowired
    private MatService matService;
    @Autowired
    private LocDetlService locDetlService;
    @Autowired
    private MatCodeService matCodeService;
    @Autowired
    private WorkService workService;
    @Autowired
    private BasDevpService basDevpService;
    @Autowired
    private WrkMastService wrkMastService;
    @PostMapping("/stockOut/prew")
    /**
     * 添加入库单
     */
    @PostMapping("/order/pakin/default/v1")
    @Transactional
    public R stockOutPrew(@RequestBody List<OpenApiStockOutParam> params){
        List<StoPreTab> result = new ArrayList<>();
        StringBuilder errorMsg = new StringBuilder();
        boolean error = false;
        for (OpenApiStockOutParam param : params) {
            Double sumAnfme = Optional.ofNullable(locDetlService.getSumAnfme(param.getMatnr())).orElse(0.0D);
            double lack = 0.0D;
            if (sumAnfme < param.getAnfme()) {
                lack = param.getAnfme() - sumAnfme;
                if (!error) {
                    error = true;
                }
                MatCode mat = matCodeService.selectById(param.getMatnr());
                // 视图对象
                StoPreTab tab = new StoPreTab();
                tab.setTitle(param.getMatnr() + (mat==null?"": "(" + mat.getMatName() + ")"));
                tab.setMatnr(param.getMatnr());
                tab.setMaktx((mat==null?"": "(" + mat.getMatName() + ")"));
                tab.setAnfme(param.getTotal());
                tab.setLocNo("缺货");
                tab.setTotal(lack);
                tab.setReduce(lack);
                tab.setRemQty(0.0D);
                tab.setPrior(false);
                tab.setPrior$("×");
                tab.setType(0);
                result.add(tab);
                errorMsg.append(mat == null ? param.getMatnr() : mat.getMatName()).append("库存不足,缺货数量:").append(param.getAnfme() - sumAnfme).append("</br>");
//                continue;
            }
            List<LocDetl> locDetls = locDetlService.selectPakoutByRule(param.getMatnr());
            double issued = Optional.of(param.getAnfme() - lack).orElse(0.0D) ;
            for (LocDetl locDetl : locDetls) {
                if (issued > 0) {
                    // 视图对象
                    StoPreTab tab = new StoPreTab();
                    tab.setTitle(locDetl.getMatnr() + "(" + locDetl.getMaktx() + ")");
                    tab.setMatnr(locDetl.getMatnr());
                    tab.setMaktx(locDetl.getMaktx());
                    tab.setAnfme(param.getTotal());
                    tab.setLocNo(locDetl.getLocNo());
//                    tab.setNodeId(locDetl.getNodeId());
                    tab.setTotal(locDetl.getAnfme());
                    tab.setReduce(issued>=locDetl.getAnfme()?locDetl.getAnfme():issued);
                    tab.setRemQty(tab.getTotal() - tab.getReduce());
                    tab.setPrior(false);
                    tab.setPrior$("×");
                    tab.setType(2);
                    result.add(tab);
                    // 剩余待出数量递减
                    issued = issued - locDetl.getAnfme();
                }
            }
    public synchronized R orderCreate(@RequestBody OpenOrderPakinParam param) {
        if (Cools.isEmpty(param)) {
            return R.parse(BaseRes.PARAM);
        }
//        if (error) {
//            return R.error(errorMsg.toString());
//        }
        return R.ok(errorMsg).add(result);
    }
    @PostMapping("/stockOut")
    @Transactional
    public R stockOut(@RequestBody List<OpenApiStockOutParam> params){
        List<PakoutVo> result = new ArrayList<>();
        if (!Cools.isEmpty(params)) {
            for (OpenApiStockOutParam param : params) {
                // 推荐货位补仓工作档防重
                if (wrkMastService.selectCount(new EntityWrapper<WrkMast>().eq("packed", param.getAllotNo()).le("wrk_sts", 14)) > 0) {
                    throw new CoolException("当前补仓任务正在执行,请耐心等待");
                }
                Double sumAnfme = Optional.ofNullable(locDetlService.getSumAnfme(param.getMatnr())).orElse(0.0D);
                if (!Cools.isEmpty(param.getAllotNo()) && sumAnfme < param.getAnfme()) {
                    throw new CoolException("立库库存不足,缺货数量:" + (param.getAnfme() - sumAnfme));
                }
                double lack = 0.0D;
                if (sumAnfme < param.getAnfme()) {
                    lack = param.getAnfme() - sumAnfme;
                    PakoutVo pakoutVo = new PakoutVo();
                    pakoutVo.setLocNo("缺货");
                    pakoutVo.setAnfme(lack);
                    pakoutVo.setMatnr(param.getMatnr());
//                    pakoutVo.setZpallet("param.getZpallet()");
                    result.add(pakoutVo);
//                    throw new CoolException(param.getMatnr() + "物料数量不足,缺货数量:" + (param.getAnfme() - sumAnfme));
                }
                List<LocDetl> locDetls = locDetlService.selectPakoutByRule(param.getMatnr());
                double issued = Optional.of(param.getAnfme()-lack).orElse(0.0D) ;
                for (LocDetl locDetl : locDetls) {
                    if (issued > 0) {
                        // 生成出库工作档
                        // 全板
                        if (issued>=locDetl.getAnfme()) {
                            BasDevp staNo = basDevpService.checkSiteStatus(103);
                            List<LocDetlDto> detlDtos = new ArrayList<>();
                            LocDetlDto dto = new LocDetlDto();
                            dto.setLocDetl(locDetl);
                            dto.setCount(issued>=locDetl.getAnfme()?locDetl.getAnfme():issued);
                            detlDtos.add(dto);
                            workService.stockOut(staNo, detlDtos, 101, 9527L, param.getAllotNo(), Cools.isEmpty(param.getAllotNo()));
                        // 拣料
                        } else {
                            BasDevp staNo = basDevpService.checkSiteStatus(113);
                            List<LocDetlDto> detlDtos = new ArrayList<>();
                            LocDetlDto dto = new LocDetlDto();
                            dto.setLocDetl(locDetl);
                            dto.setCount(issued>=locDetl.getAnfme()?locDetl.getAnfme():issued);
                            detlDtos.add(dto);
                            workService.stockOut(staNo, detlDtos, 103, 9527L, param.getAllotNo(), Cools.isEmpty(param.getAllotNo()));
                        }
                        PakoutVo pakoutVo = new PakoutVo();
                        pakoutVo.setLocNo(locDetl.getLocNo());
                        pakoutVo.setAnfme(issued>=locDetl.getAnfme()?locDetl.getAnfme():issued);
                        pakoutVo.setMatnr(locDetl.getMatnr());
                        pakoutVo.setZpallet(locDetl.getZpallet());
                        result.add(pakoutVo);
                        // 剩余待出数量递减
                        issued = issued - locDetl.getAnfme();
                    }
                }
        if (Cools.isEmpty(param.getOrderNo())) {
            return R.error("单据编号[orderNo]不能为空");
        }
        if (Cools.isEmpty(param.getOrderType())) {
            return R.error("单据类型[orderType]不能为空");
        }
        if (Cools.isEmpty(param.getOrderDetails())) {
            return R.error("单据明细[orderDetails]不能为空");
        }
        Order order = orderService.selectByNo(param.getOrderNo());
        if (!Cools.isEmpty(order)) {
            return R.error(param.getOrderNo() + "单据已存在,请勿重复提交");
        }
        DocType docType = docTypeService.selectOrAdd(param.getOrderType());
        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    // 备注
        );
        if (!orderService.insert(order)) {
            throw new CoolException("生成单据主档失败,请联系管理员");
        }
        // 单据明细档
        List<DetlDto> list = new ArrayList<>();
        List<DetlDto> 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);
            }
        }
        return R.ok().add(result);
        for (DetlDto detlDto : list) {
            Mat mat = matService.selectByMatnr(detlDto.getMatnr());
            if (Cools.isEmpty(mat)) {
                throw new CoolException(detlDto.getMatnr() + "编号商品检索失败,请先添加商品");
            }
            OrderDetl orderDetl = new OrderDetl();
            orderDetl.sync(mat);
            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.insert(orderDetl)) {
                throw new CoolException("生成单据明细失败,请联系管理员");
            }
        }
        return R.ok();
    }
    /**
     * 库存统计
     */
    @RequestMapping("/stock/default/v1")
    public R queryStock() {
        return R.ok(locDetlService.queryStockTotal());
    }
}