自动化立体仓库 - WMS系统
zwl
2025-04-16 b4b2da143af53a7422135abffd5306b282be6a39
src/main/java/com/zy/asrs/controller/OrderController.java
@@ -1,29 +1,30 @@
package com.zy.asrs.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.core.annotations.ManagerAuth;
import com.core.common.BaseRes;
import com.core.common.Cools;
import com.core.common.DateUtils;
import com.core.common.R;
import com.zy.asrs.entity.Order;
import com.zy.asrs.entity.OrderDetl;
import com.zy.asrs.entity.param.InitOrderParam;
import com.zy.asrs.service.OrderDetlService;
import com.zy.asrs.service.OrderService;
import com.core.common.*;
import com.core.exception.CoolException;
import com.zy.asrs.entity.*;
import com.zy.asrs.entity.param.OrderDomainParam;
import com.zy.asrs.service.*;
import com.zy.common.model.DetlDto;
import com.zy.common.web.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
@Slf4j
@RestController
public class OrderController extends BaseController {
@@ -31,6 +32,61 @@
    private OrderService orderService;
    @Autowired
    private OrderDetlService orderDetlService;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    private DocTypeService docTypeService;
    @Autowired
    private WrkDetlService wrkDetlService;
    @Autowired
    private WaitPakinService waitPakinService;
    @Autowired
    private LocDetlService locDetlService;
    @Value("${erp.address.URL}")
    //端口
    private String URL;
    @Value("${erp.address.inaddress}")
    //单据,商品档案地址
    private String inaddress;
    @Autowired
    private ApiLogService apiLogService;
    @Autowired
    private MatService matService;
    @Autowired
    private TagService tagService;
    @RequestMapping(value = "/order/nav/list/auth")
    @ManagerAuth
    public R navList(@RequestParam(required = false) String orderNo){
        EntityWrapper<Order> wrapper = new EntityWrapper<>();
        if (!Cools.isEmpty(orderNo)) {
            wrapper.like("order_no", orderNo);
        }
        wrapper.le("settle", 2).eq("status", 1);
        wrapper.orderBy("create_time", false);
        List<Order> orders = orderService.selectList(wrapper);
        // 保留出库单
        if (!Cools.isEmpty(orders)) {
            Iterator<Order> iterator = orders.iterator();
            while (iterator.hasNext()) {
                Order order = iterator.next();
                if (order.getDocType() != null) {
                    DocType docType = docTypeService.selectById(order.getDocType());
                    if (docType != null) {
                        if (docType.getPakout() == 0) {
                            iterator.remove();
                        }
                    }
                }
            }
        }
        return R.ok().add(orders);
    }
    @RequestMapping(value = "/order/head/page/auth")
    @ManagerAuth
@@ -42,7 +98,10 @@
        EntityWrapper<Order> wrapper = new EntityWrapper<>();
        excludeTrash(param);
        convert(param, wrapper);
        if (!Cools.isEmpty(orderByField)){wrapper.orderBy(humpToLine(orderByField), "asc".equals(orderByType));}
        if (!Cools.isEmpty(orderByField)){wrapper.orderBy(humpToLine(orderByField), "asc".equals(orderByType));} else {
            wrapper.orderBy("settle").orderBy("create_time", false);
        }
        wrapper.eq("status", 1);
        return R.ok(orderService.selectPage(new Page<>(curr, limit), wrapper));
    }
@@ -52,21 +111,141 @@
        return R.ok().add(orderDetlService.selectList(new EntityWrapper<OrderDetl>().eq("order_id", orderId)));
    }
    @RequestMapping(value = "/order/init/auth")
    @ManagerAuth
    public R init(@RequestBody InitOrderParam param){
        System.out.println(JSON.toJSONString(param));
        return R.ok();
    @RequestMapping(value = "/order/form/add/auth")
    @ManagerAuth(memo = "手动添加订单")
    @Transactional
    public R formAdd(@RequestBody OrderDomainParam param){
        if(Cools.isEmpty(param.getOrderNo())){
            String prefix = "";
            DocType docType = docTypeService.selectById(param.getDocType());
            if(!Cools.isEmpty(docType)){
                prefix = docType.getPakin() == 1 ? "RK" : "CK";
            }
            param.setOrderNo(prefix + snowflakeIdWorker.nextId());
        }
        Order order = orderService.selectByNo(param.getOrderNo());
        if (order != null) {
            return R.error("单据编号已存在");
        }
        Date now = new Date();
        order = new Order(
                String.valueOf(snowflakeIdWorker.nextId()),    // 编号[非空]
                param.getOrderNo(),    // 订单编号
                DateUtils.convert(now),    // 单据日期
                param.getDocType(),    // 单据类型
                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,    // 状态
                getUserId(),    // 添加人员
                now,    // 添加时间
                getUserId(),    // 修改人员
                now,    // 修改时间
                null    // 备注
        );
        if (!orderService.insert(order)) {
            throw new CoolException("保存订单主档失败");
        }
        List<DetlDto> list = new ArrayList<>();
        for (OrderDetl orderDetl : param.getOrderDetlList()) {
            DetlDto dto = new DetlDto(orderDetl.getMatnr(), orderDetl.getBatch());
            if (DetlDto.has(list, dto)) {
                OrderDetl item = orderDetlService.selectItem(order.getId(), orderDetl.getMatnr(), orderDetl.getBatch());
                item.setAnfme(item.getAnfme() + orderDetl.getAnfme());
                if (!orderDetlService.updateById(item)) {
                    throw new CoolException("保存订单明细档失败");
                }
            } else {
                list.add(dto);
                orderDetl.setOrderId(order.getId());
                orderDetl.setOrderNo(order.getOrderNo());
                orderDetl.setCreateBy(getUserId());
                orderDetl.setCreateTime(now);
                orderDetl.setUpdateBy(getUserId());
                orderDetl.setUpdateTime(now);
                orderDetl.setStatus(1);
                orderDetl.setQty(0.0D);
                if (!orderDetlService.insert(orderDetl)) {
                    throw new CoolException("保存订单明细档失败");
                }
            }
        }
        return R.ok("订单添加成功");
    }
    @RequestMapping(value = "/order/form/modify/auth")
    @ManagerAuth(memo = "手动修改订单")
    @Transactional
    public R formModify(@RequestBody OrderDomainParam param){
        Order order = orderService.selectById(param.getOrderId());
        if (order == null || order.getStatus() == 0) {
            return R.error("订单不存在");
        }
        Date now = new Date();
        Long userId = getUserId();
        // 修改主档
        if (!param.getDocType().equals(order.getDocType())) {
            order.setDocType(param.getDocType());
            order.setUpdateBy(userId);
            order.setUpdateTime(now);
            if (!orderService.updateById(order)) {
                throw new CoolException("修改订单类型失败");
            }
        }
        // 修改明细档
//        List<OrderDetl> orderDetls = orderDetlService.selectByOrderId(order.getId());
        // 1.清空明细档
        if (!orderDetlService.delete(new EntityWrapper<OrderDetl>().eq("order_id", order.getId()))) {
            throw new CoolException("清空订单明细失败");
        }
        // 2.重组数据
        List<DetlDto> list = new ArrayList<>();
        for (OrderDetl orderDetl : param.getOrderDetlList()) {
            DetlDto dto = new DetlDto(orderDetl.getMatnr(), orderDetl.getBatch());
            if (DetlDto.has(list, dto)) {
                OrderDetl item = orderDetlService.selectItem(order.getId(), orderDetl.getMatnr(), orderDetl.getBatch());
                item.setAnfme(item.getAnfme() + orderDetl.getAnfme());
                if (!orderDetlService.updateById(item)) {
                    throw new CoolException("保存订单明细档失败");
                }
            } else {
                list.add(dto);
                orderDetl.setOrderId(order.getId());
                orderDetl.setOrderNo(order.getOrderNo());
                orderDetl.setCreateBy(getUserId());
                orderDetl.setCreateTime(now);
                orderDetl.setUpdateBy(getUserId());
                orderDetl.setUpdateTime(now);
                orderDetl.setStatus(1);
                orderDetl.setQty(0.0D);
                if (!orderDetlService.insert(orderDetl)) {
                    throw new CoolException("保存订单明细档失败");
                }
            }
        }
        return R.ok("订单修改成功");
    }
    // ------------------------------------------------------------------------------------------------
@@ -104,29 +283,43 @@
        }
    }
    @RequestMapping(value = "/order/add/auth")
    @RequestMapping(value = "/order/update/auth")
    @ManagerAuth
    public R add(Order order) {
        orderService.insert(order);
        return R.ok();
    }
   @RequestMapping(value = "/order/update/auth")
   @ManagerAuth
    public R update(Order order){
        if (Cools.isEmpty(order) || null==order.getId()){
        if (Cools.isEmpty(order) || null == order.getId()){
            return R.error();
        }
        orderService.updateById(order);
        //订单完结前,判断是否存在作业中数据,存在则不能完结
        if(order.getSettle() == 4){
            int wrkCount = wrkDetlService.selectCount(new EntityWrapper<WrkDetl>().eq("order_no",order.getOrderNo()));
            int pakinCount = waitPakinService.selectCount(new EntityWrapper<WaitPakin>().eq("order_no",order.getOrderNo()));
            if (wrkCount > 0 || pakinCount > 0) {
                throw new CoolException("存在作业中数据,不能完结。请检查入库通知档和工作档");
            }
        }
        order.setUpdateBy(getUserId());
        order.setUpdateTime(new Date());
        if (!orderService.updateById(order)) {
            throw new CoolException("修改订单失败");
        }
        return R.ok();
    }
    @RequestMapping(value = "/order/delete/auth")
    @ManagerAuth
    public R delete(@RequestParam(value="ids[]") Long[] ids){
         for (Long id : ids){
            orderService.deleteById(id);
        }
    @ManagerAuth(memo = "手动删除订单")
    @Transactional
    public R delete(@RequestParam Long orderId){
        orderService.remove(orderId);
//        Order order = orderService.selectById(orderId);
//        if (order != null) {
//            order.setStatus(0);
//        }
//        if (!orderService.updateById(order)) {
//            throw new CoolException("删除订单失败");
//        }
//        orderDetlService.modifyStatus(orderId, 0);
        return R.ok();
    }
@@ -151,7 +344,7 @@
        for (Order order : page.getRecords()){
            Map<String, Object> map = new HashMap<>();
            map.put("id", order.getId());
            map.put("value", order.getId());
            map.put("value", order.getOrderNo());
            result.add(map);
        }
        return R.ok(result);
@@ -167,4 +360,93 @@
        return R.ok();
    }
    @RequestMapping(value = "/order/form/add/authtest")
    @ManagerAuth(memo = "手动添加订单")
    @Transactional
    public R formAddTest(@RequestBody OrderDomainParam param){
        if(Cools.isEmpty(param.getOrderNo())){
            String prefix = "";
            DocType docType = docTypeService.selectById(param.getDocType());
            if(!Cools.isEmpty(docType)){
                prefix = docType.getPakin() == 1 ? "RK" : "CK";
            }
            param.setOrderNo(prefix + snowflakeIdWorker.nextId());
        }
        Order order = orderService.selectByNo(param.getOrderNo());
        if (order != null) {
            return R.error("单据编号已存在");
        }
        Date now = new Date();
        order = new Order(
                String.valueOf(snowflakeIdWorker.nextId()),    // 编号[非空]
                param.getOrderNo(),    // 订单编号
                DateUtils.convert(now),    // 单据日期
                param.getDocType(),    // 单据类型
                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,    // 状态
                getUserId(),    // 添加人员
                now,    // 添加时间
                getUserId(),    // 修改人员
                now,    // 修改时间
                null    // 备注
        );
        if (!orderService.insert(order)) {
            throw new CoolException("保存订单主档失败");
        }
        List<DetlDto> list = new ArrayList<>();
        for (OrderDetl orderDetl : param.getOrderDetlList()) {
            DetlDto dto = new DetlDto(orderDetl.getMatnr(), orderDetl.getBatch());
            if (DetlDto.has(list, dto)) {
                OrderDetl item = orderDetlService.selectItem(order.getId(), orderDetl.getMatnr(), orderDetl.getBatch());
                item.setAnfme(item.getAnfme() + orderDetl.getAnfme());
                if (!orderDetlService.updateById(item)) {
                    throw new CoolException("保存订单明细档失败");
                }
            } else {
                list.add(dto);
                orderDetl.setOrderId(order.getId());
                orderDetl.setOrderNo(order.getOrderNo());
                orderDetl.setCreateBy(getUserId());
                orderDetl.setCreateTime(now);
                orderDetl.setUpdateBy(getUserId());
                orderDetl.setUpdateTime(now);
                orderDetl.setStatus(1);
                orderDetl.setQty(0.0D);
                //给添加子订单
                Set<String> exist = new HashSet<>();
                List<LocDetl> locDetls = locDetlService.queryStock(orderDetl.getMatnr(), orderDetl.getBatch(), null, exist);
                if (!orderDetlService.insert(orderDetl)) {
                    throw new CoolException("保存订单明细档失败");
                }
            }
        }
        return R.ok("订单添加成功");
    }
}