|  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONArray; | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.Query; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
|---|
|  |  |  | import com.zy.asrs.common.domain.dto.DetlDto; | 
|---|
|  |  |  | import com.zy.asrs.common.domain.dto.WrkTraceVo; | 
|---|
|  |  |  | import com.zy.asrs.common.domain.param.OrderDomainParam; | 
|---|
|  |  |  | import com.zy.asrs.common.wms.entity.*; | 
|---|
|  |  |  | import com.zy.asrs.common.wms.mapper.OrderDetlMapper; | 
|---|
|  |  |  | import com.zy.asrs.common.wms.service.*; | 
|---|
|  |  |  | import com.zy.asrs.framework.annotations.ManagerAuth; | 
|---|
|  |  |  | import com.zy.asrs.framework.common.Cools; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.zy.asrs.framework.common.DateUtils; | 
|---|
|  |  |  | import com.zy.asrs.common.web.BaseController; | 
|---|
|  |  |  | import com.zy.asrs.framework.exception.CoolException; | 
|---|
|  |  |  | import org.apache.ibatis.annotations.Param; | 
|---|
|  |  |  | import org.springframework.beans.BeanUtils; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.jdbc.core.JdbcTemplate; | 
|---|
|  |  |  | import org.springframework.transaction.annotation.Transactional; | 
|---|
|  |  |  | import org.springframework.transaction.interceptor.TransactionAspectSupport; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.*; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.io.UnsupportedEncodingException; | 
|---|
|  |  |  | import java.math.BigDecimal; | 
|---|
|  |  |  | import java.net.URLDecoder; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @RestController | 
|---|
|  |  |  | 
|---|
|  |  |  | private SnowflakeIdWorker snowflakeIdWorker; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private DocTypeService docTypeService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private LocInPrintMatService locInPrintMatService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private MatService matService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @RequestMapping(value = "/order/list/orderNo") | 
|---|
|  |  |  | public R orderListorderNo(@RequestParam String orderNo) { | 
|---|
|  |  |  | //数量修改成为完成数量 | 
|---|
|  |  |  | List<OrderDetl> orderDetl1 = orderDetlService.list(new LambdaQueryWrapper<OrderDetl>().eq(OrderDetl::getOrderNo, orderNo)); | 
|---|
|  |  |  | List<OrderDetl> orderDetls= new ArrayList<OrderDetl>(); | 
|---|
|  |  |  | for (OrderDetl o: orderDetl1) { | 
|---|
|  |  |  | BigDecimal c1 = new BigDecimal(o.getEnableQty()).setScale(2,BigDecimal.ROUND_HALF_UP); | 
|---|
|  |  |  | o.setAnfme(c1.doubleValue()); | 
|---|
|  |  |  | if (o.getAnfme().equals(0.0)){ | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | orderDetls.add(o); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return R.ok(orderDetls); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @RequestMapping(value = "/order/list/all") | 
|---|
|  |  |  | @ManagerAuth | 
|---|
|  |  |  | public R orderList() { | 
|---|
|  |  |  | return R.ok(orderService.selectAllorderNo(getHostId())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @RequestMapping(value = "/order/comb/list/all") | 
|---|
|  |  |  | @ManagerAuth | 
|---|
|  |  |  | public R orderListComb() { | 
|---|
|  |  |  | return R.ok(orderService.selectAllCombOrderNo(getHostId())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //    @RequestMapping(value = "/outOrder/list/all") | 
|---|
|  |  |  | //    @ManagerAuth | 
|---|
|  |  |  | //    public R orderOutList() { | 
|---|
|  |  |  | //        return R.ok(orderService.selectAllOutOrderNo(getHostId())); | 
|---|
|  |  |  | //    } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @RequestMapping(value = "/outOrder/list/all") | 
|---|
|  |  |  | @ManagerAuth | 
|---|
|  |  |  | public R orderOutListOrder(@RequestBody String orderNo) { | 
|---|
|  |  |  | return R.ok(orderService.selectAllOutOrderNo(orderNo,getHostId())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @RequestMapping(value = "/order/{id}/auth") | 
|---|
|  |  |  | @ManagerAuth | 
|---|
|  |  |  | 
|---|
|  |  |  | @RequestParam(required = false) String condition, | 
|---|
|  |  |  | @RequestParam(required = false) String timeRange, | 
|---|
|  |  |  | @RequestParam Map<String, Object> param) { | 
|---|
|  |  |  | LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>(); | 
|---|
|  |  |  | wrapper.eq(Order::getHostId, getHostId()); | 
|---|
|  |  |  | QueryWrapper<Order> wrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | wrapper.eq("host_id", getHostId()); | 
|---|
|  |  |  | if (!Cools.isEmpty(condition)) { | 
|---|
|  |  |  | wrapper.like(Order::getId, condition); | 
|---|
|  |  |  | wrapper.like("id", condition); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!Cools.isEmpty(timeRange)) { | 
|---|
|  |  |  | String[] range = timeRange.split(RANGE_TIME_LINK); | 
|---|
|  |  |  | wrapper.ge(Order::getCreateTime, DateUtils.convert(range[0])); | 
|---|
|  |  |  | wrapper.le(Order::getCreateTime, DateUtils.convert(range[1])); | 
|---|
|  |  |  | wrapper.ge("create_time", DateUtils.convert(range[0])); | 
|---|
|  |  |  | wrapper.le("create_time", DateUtils.convert(range[1])); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | allLike(Order.class, param.keySet(), wrapper, condition); | 
|---|
|  |  |  | return R.ok(orderService.page(new Page<>(curr, limit), wrapper)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | @ManagerAuth | 
|---|
|  |  |  | public R head(@RequestParam(defaultValue = "1")Integer curr, | 
|---|
|  |  |  | @RequestParam(defaultValue = "10")Integer limit, | 
|---|
|  |  |  | @RequestParam(required = false) String condition, | 
|---|
|  |  |  | @RequestParam(required = false)String orderByField, | 
|---|
|  |  |  | @RequestParam(required = false)String orderByType, | 
|---|
|  |  |  | @RequestParam Map<String, Object> param){ | 
|---|
|  |  |  | LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>(); | 
|---|
|  |  |  | @RequestParam Map<String, Object> param) throws UnsupportedEncodingException { | 
|---|
|  |  |  | if(!Cools.isEmpty(condition)){ | 
|---|
|  |  |  | condition = condition.trim(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | QueryWrapper<Order> wrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | excludeTrash(param); | 
|---|
|  |  |  | wrapper.eq(Order::getStatus, 1); | 
|---|
|  |  |  | wrapper.eq(Order::getHostId, getHostId()); | 
|---|
|  |  |  | wrapper.eq("status", 1); | 
|---|
|  |  |  | wrapper.eq("host_id", getHostId()); | 
|---|
|  |  |  | if (!Cools.isEmpty(param.get("order_no"))) { | 
|---|
|  |  |  | wrapper.eq(Order::getOrderNo, param.get("order_no")); | 
|---|
|  |  |  | wrapper.eq("order_no", param.get("order_no")); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!Cools.isEmpty(param.get("create_time"))) { | 
|---|
|  |  |  | String[] range = param.get("create_time").toString().split(RANGE_TIME_LINK); | 
|---|
|  |  |  | wrapper.ge(Order::getCreateTime, DateUtils.convert(range[0])); | 
|---|
|  |  |  | wrapper.le(Order::getCreateTime, DateUtils.convert(range[1])); | 
|---|
|  |  |  | wrapper.ge("create_time", DateUtils.convert(range[0])); | 
|---|
|  |  |  | wrapper.le("create_time", DateUtils.convert(range[1])); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!Cools.isEmpty(param.get("doc_type"))) { | 
|---|
|  |  |  | wrapper.eq(Order::getDocType, param.get("doc_type")); | 
|---|
|  |  |  | wrapper.eq("doc_type", param.get("doc_type")); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!Cools.isEmpty(param.get("docName"))) { | 
|---|
|  |  |  | DocType docType = docTypeService.getOne(new LambdaQueryWrapper<DocType>().eq(DocType::getDocName, param.get("docName"))); | 
|---|
|  |  |  | wrapper.eq(Order::getDocType, docType.getDocId()); | 
|---|
|  |  |  | String docName = URLDecoder.decode(param.get("docName").toString(), "UTF-8"); | 
|---|
|  |  |  | DocType docType = docTypeService.getOne(new LambdaQueryWrapper<DocType>().eq(DocType::getDocName, docName)); | 
|---|
|  |  |  | wrapper.eq("doc_type", docType.getDocId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!Cools.isEmpty(param.get("settle"))) { | 
|---|
|  |  |  | wrapper.eq(Order::getSettle, param.get("settle")); | 
|---|
|  |  |  | wrapper.eq("settle", param.get("settle")); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | param.remove("order_no"); | 
|---|
|  |  |  | allLike(Order.class, param.keySet(), wrapper, condition); | 
|---|
|  |  |  | wrapper.orderByDesc("create_time"); | 
|---|
|  |  |  | return R.ok(orderService.page(new Page<>(curr, limit), wrapper)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!orderDetlService.remove(new LambdaQueryWrapper<OrderDetl>().eq(OrderDetl::getOrderId, orderId))) { | 
|---|
|  |  |  | throw new CoolException("删除单据明细失败"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return R.ok(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @RequestMapping(value = "/order/dy") | 
|---|
|  |  |  | @ManagerAuth(memo = "手动删除订单") | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | public R dy(@RequestParam Long orderId){ | 
|---|
|  |  |  | List<OrderDetl> list = orderDetlService.list(new LambdaQueryWrapper<OrderDetl>().eq(OrderDetl::getOrderId, orderId)); | 
|---|
|  |  |  | for (OrderDetl orderDetl : list) { | 
|---|
|  |  |  | Mat mat = matService.getOne(new LambdaQueryWrapper<Mat>().eq(Mat::getMatnr, orderDetl.getMatnr())); | 
|---|
|  |  |  | LocInPrintMat locInPrintMat = new LocInPrintMat(); | 
|---|
|  |  |  | locInPrintMat.setMatnr(orderDetl.getMatnr()); | 
|---|
|  |  |  | locInPrintMat.setMaktx(orderDetl.getMaktx()); | 
|---|
|  |  |  | locInPrintMat.setStatus(1); | 
|---|
|  |  |  | locInPrintMat.setOrderNo(orderDetl.getOrderNo()); | 
|---|
|  |  |  | locInPrintMat.setBatch(orderDetl.getBatch()); | 
|---|
|  |  |  | locInPrintMat.setCreateBy(getUserId()); | 
|---|
|  |  |  | locInPrintMat.setCreateTime(new Date()); | 
|---|
|  |  |  | locInPrintMat.setUpdateTime(new Date()); | 
|---|
|  |  |  | if(!Cools.isEmpty(mat)){ | 
|---|
|  |  |  | locInPrintMat.setDecrees(mat.getDecrees()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | locInPrintMatService.save(locInPrintMat); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return R.ok(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | double totalQty = 0; | 
|---|
|  |  |  | double wrkQty = 0; | 
|---|
|  |  |  | double lackQty = 0; | 
|---|
|  |  |  | double endQty = 0; | 
|---|
|  |  |  | for (OrderDetl orderDetl : orderDetls) { | 
|---|
|  |  |  | totalQty = totalQty + orderDetl.getAnfme(); | 
|---|
|  |  |  | wrkQty = wrkQty + orderDetl.getQty(); | 
|---|
|  |  |  | double issued = Optional.of(orderDetl.getAnfme() - orderDetl.getQty()).orElse(0.0D); | 
|---|
|  |  |  | wrkQty = wrkQty + orderDetl.getWorkQty(); | 
|---|
|  |  |  | endQty = endQty + orderDetl.getQty(); | 
|---|
|  |  |  | double issued = Optional.of(orderDetl.getAnfme() - orderDetl.getWorkQty()).orElse(0.0D); | 
|---|
|  |  |  | if (issued > 0.0) { | 
|---|
|  |  |  | List<LocDetl> locDetls = locDetlService.queryStock(orderDetl.getMatnr(), orderDetl.getBatch(), orderDetl.getOrigin(), null); | 
|---|
|  |  |  | List<LocDetl> locDetls = locDetlService.queryStock(orderDetl.getMatnr(), orderDetl.getBatch(), hostId); | 
|---|
|  |  |  | for (LocDetl locDetl : locDetls) { | 
|---|
|  |  |  | if (issued > 0) { | 
|---|
|  |  |  | issued = issued - locDetl.getAnfme(); | 
|---|
|  |  |  | 
|---|
|  |  |  | WrkMast wrkMast = wrkMastService.getOne(new LambdaQueryWrapper<WrkMast>().eq(WrkMast::getWrkNo, wrkDetl.getWrkNo()).eq(WrkMast::getIoTime, wrkDetl.getIoTime())); | 
|---|
|  |  |  | if (wrkMast == null) { | 
|---|
|  |  |  | WrkMastLog wrkMastLog = wrkMastLogService.getOne(new LambdaQueryWrapper<WrkMastLog>().eq(WrkMastLog::getWrkNo, wrkDetl.getWrkNo()).eq(WrkMastLog::getIoTime, wrkDetl.getIoTime())); | 
|---|
|  |  |  | if (wrkMastLog.getIoType()==103){ | 
|---|
|  |  |  | wrkMastLog = wrkMastLogService.getOne(new LambdaQueryWrapper<WrkMastLog>().eq(WrkMastLog::getWrkNo, wrkDetl.getWrkNo()).eq(WrkMastLog::getIoTime, wrkDetl.getIoTime()).eq(WrkMastLog::getIoType,53)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (wrkMastLog != null) { | 
|---|
|  |  |  | wrkMast = new WrkMast(); | 
|---|
|  |  |  | BeanUtils.copyProperties(wrkMastLog, wrkMast); | 
|---|
|  |  |  | 
|---|
|  |  |  | .add("totalQty", totalQty) | 
|---|
|  |  |  | .add("wrkQty", wrkQty) | 
|---|
|  |  |  | .add("lackQty", lackQty) | 
|---|
|  |  |  | .add("endQty",endQty) | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | 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()); | 
|---|
|  |  |  | OrderDetl item = orderDetlService.selectItem(order.getId(), orderDetl.getMatnr(), orderDetl.getBatch(), getHostId()); | 
|---|
|  |  |  | item.setAnfme(item.getAnfme() + orderDetl.getAnfme()); | 
|---|
|  |  |  | if (!orderDetlService.updateById(item)) { | 
|---|
|  |  |  | throw new CoolException("保存订单明细档失败"); | 
|---|
|  |  |  | 
|---|
|  |  |  | orderDetl.setUpdateTime(now); | 
|---|
|  |  |  | orderDetl.setStatus(1); | 
|---|
|  |  |  | orderDetl.setQty(0.0D); | 
|---|
|  |  |  | orderDetl.setWorkQty(0.0D); | 
|---|
|  |  |  | if (!orderDetlService.save(orderDetl)) { | 
|---|
|  |  |  | throw new CoolException("保存订单明细档失败"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | return R.ok().add(orders); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @RequestMapping(value = "/order/form/modify/auth") | 
|---|
|  |  |  | @ManagerAuth(memo = "手动修改订单") | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | public R formModify(@RequestBody OrderDomainParam param){ | 
|---|
|  |  |  | Order order = orderService.getOne(new LambdaQueryWrapper<Order>() | 
|---|
|  |  |  | .eq(Order::getId, param.getOrderId()) | 
|---|
|  |  |  | .eq(Order::getHostId, getHostId())); | 
|---|
|  |  |  | 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.remove(new LambdaQueryWrapper<OrderDetl>().eq(OrderDetl::getOrderId, 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(), getHostId()); | 
|---|
|  |  |  | 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.save(orderDetl)) { | 
|---|
|  |  |  | throw new CoolException("保存订单明细档失败"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return R.ok("订单修改成功"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|