liu
昨天 4ca4444ad33a2a1a51f5c06e78ee9365dfb1eed6
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/dal/mysql/pro/bagcode/MesProBagCodeMapper.java
@@ -5,6 +5,7 @@
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.mes.controller.admin.pro.bagcode.vo.MesProBagCodePageReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.pro.bagcode.MesProBagCodeDO;
import cn.iocoder.yudao.module.mes.enums.pro.MesProBagCodeStatusEnum;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@@ -27,6 +28,39 @@
        return selectOne(MesProBagCodeDO::getCode, code);
    }
    default MesProBagCodeDO selectByCodeForUpdate(String code) {
        return selectOne(new LambdaQueryWrapperX<MesProBagCodeDO>()
                .eq(MesProBagCodeDO::getCode, code).last("FOR UPDATE"));
    }
    @org.apache.ibatis.annotations.Update("UPDATE mes_pro_bag_code SET inbound_time = NOW(), inbound_event_id = #{eventId}, inbound_device_code = #{deviceCode}, inbound_line_code = #{lineCode}, update_time = NOW() WHERE id = #{id} AND deleted = 0 AND inbound_event_id IS NULL")
    int markInbound(@org.apache.ibatis.annotations.Param("id") Long id, @org.apache.ibatis.annotations.Param("eventId") Long eventId,
                    @org.apache.ibatis.annotations.Param("deviceCode") String deviceCode, @org.apache.ibatis.annotations.Param("lineCode") String lineCode);
    /** 批次整批入库时批量标记其下未入库袋码(幂等),保持袋码与库存状态一致 */
    @Update("UPDATE mes_pro_bag_code SET inbound_time = NOW(), inbound_event_id = #{eventId}, inbound_line_code = #{lineCode}, update_time = NOW() "
            + "WHERE deleted = 0 AND inbound_event_id IS NULL AND batch_id = #{batchId}")
    int markInboundByBatchId(@Param("batchId") Long batchId, @Param("eventId") Long eventId,
                             @Param("lineCode") String lineCode);
    /** 整托入库时批量标记托盘下未入库袋码(幂等) */
    @Update("UPDATE mes_pro_bag_code SET inbound_time = NOW(), inbound_event_id = #{eventId}, inbound_line_code = #{lineCode}, update_time = NOW() "
            + "WHERE deleted = 0 AND inbound_event_id IS NULL AND pallet_id = #{palletId}")
    int markInboundByPalletId(@Param("palletId") Long palletId, @Param("eventId") Long eventId,
                              @Param("lineCode") String lineCode);
    /**
     * 更新袋码质检状态(种子质检合格/不合格时记录该袋结果)
     *
     * @param id       袋码编号
     * @param qcStatus 质检状态(见 MesProBagCodeQcStatusEnum)
     */
    default void updateQcStatus(Long id, Integer qcStatus) {
        update(null, new LambdaUpdateWrapper<MesProBagCodeDO>()
                .eq(MesProBagCodeDO::getId, id)
                .set(MesProBagCodeDO::getQcStatus, qcStatus));
    }
    default List<MesProBagCodeDO> selectListByUuid(String uuid) {
        return selectList(MesProBagCodeDO::getUuid, uuid);
    }
@@ -37,6 +71,29 @@
    default List<MesProBagCodeDO> selectListByBatchId(Long batchId) {
        return selectList(MesProBagCodeDO::getBatchId, batchId);
    }
    /**
     * 查询生成组下可印刷的袋码(排除已作废),用于二维码标签导出
     * <p>
     * 已作废袋码的标签不得再印刷:其标签若贴到产品上,消费者扫码会被提示「该追溯码已作废」,
     * 导致真品被误判为假货。
     */
    default List<MesProBagCodeDO> selectPrintableListByUuid(String uuid) {
        return selectList(new LambdaQueryWrapperX<MesProBagCodeDO>()
                .eq(MesProBagCodeDO::getUuid, uuid)
                .ne(MesProBagCodeDO::getStatus, MesProBagCodeStatusEnum.CANCELED.getStatus())
                .orderByAsc(MesProBagCodeDO::getBagNo));
    }
    /**
     * 查询批次下可印刷的袋码(排除已作废),用于二维码标签导出
     */
    default List<MesProBagCodeDO> selectPrintableListByBatchId(Long batchId) {
        return selectList(new LambdaQueryWrapperX<MesProBagCodeDO>()
                .eq(MesProBagCodeDO::getBatchId, batchId)
                .ne(MesProBagCodeDO::getStatus, MesProBagCodeStatusEnum.CANCELED.getStatus())
                .orderByAsc(MesProBagCodeDO::getBagNo));
    }
    default Long selectCountByBatchId(Long batchId) {
@@ -58,9 +115,11 @@
                .likeIfPresent(MesProBagCodeDO::getBatchCode, reqVO.getBatchCode())
                .eqIfPresent(MesProBagCodeDO::getUuid, reqVO.getUuid())
                .eqIfPresent(MesProBagCodeDO::getPalletId, reqVO.getPalletId())
                .eqIfPresent(MesProBagCodeDO::getItemId, reqVO.getItemId())
                .eqIfPresent(MesProBagCodeDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(MesProBagCodeDO::getCreateTime, reqVO.getCreateTime())
                .orderByAsc(MesProBagCodeDO::getBagNo));
                .orderByDesc(MesProBagCodeDO::getCreateTime)
                .orderByDesc(MesProBagCodeDO::getId));
    }
    /**
@@ -78,6 +137,18 @@
    }
    /**
     * 托盘正式码生成后,回写其下所有袋码的冗余托盘码
     *
     * @param palletId   托盘编号
     * @param palletCode 正式托盘码
     */
    default void updatePalletCodeByPalletId(Long palletId, String palletCode) {
        update(null, new LambdaUpdateWrapper<MesProBagCodeDO>()
                .set(MesProBagCodeDO::getPalletCode, palletCode)
                .eq(MesProBagCodeDO::getPalletId, palletId));
    }
    /**
     * 批量解绑托盘(置空托盘编号与托盘码)
     *
     * @param ids 袋码编号列表
@@ -90,13 +161,36 @@
    }
    /**
     * 查询批次内袋码最大序号(包含已逻辑删除记录,保证序号不回退、码值唯一)
     * 查询全局最大袋序号(袋序号为全局序号,不区分批次;包含已逻辑删除记录,保证序号不回退)
     * <p>
     * 仅用于为 Redis 序号分配提供基准值,不要在业务中直接用「返回值 + 1」作为起始序号——
     * 那是先读后写,并发生成会产生重复序号。请使用
     * {@code MesCodeRedisDAO#allocateBagNoStart(int, Integer)} 原子分配。
     *
     * @param batchId 批次编号
     * @return 最大袋序号,无记录返回 0
     * @return 全局最大袋序号,无记录返回 0
     */
    @Select("SELECT IFNULL(MAX(bag_no), 0) FROM mes_pro_bag_code WHERE batch_id = #{batchId}")
    Integer selectMaxBagNo(@Param("batchId") Long batchId);
    @Select("SELECT IFNULL(MAX(bag_no), 0) FROM mes_pro_bag_code")
    Integer selectMaxBagNo();
    @Update("<script>UPDATE mes_pro_bag_code SET batch_id = #{batchId}, batch_code = #{batchCode}, update_time = NOW() " +
            "WHERE deleted = 0 AND batch_id IS NULL AND status IN " +
            "<foreach collection='statuses' item='st' open='(' separator=',' close=')'>#{st}</foreach> AND id IN " +
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>#{id}</foreach></script>")
    int bindBatchByIds(@Param("ids") Collection<Long> ids, @Param("batchId") Long batchId,
                       @Param("batchCode") String batchCode, @Param("statuses") Collection<Integer> statuses);
    /** 查询托盘下尚未绑定批次的袋码(托盘绑定批次时级联录入用) */
    default List<MesProBagCodeDO> selectUnboundListByPalletIds(Collection<Long> palletIds) {
        return selectList(new LambdaQueryWrapperX<MesProBagCodeDO>()
                .in(MesProBagCodeDO::getPalletId, palletIds)
                .isNull(MesProBagCodeDO::getBatchId)
                .orderByAsc(MesProBagCodeDO::getId));
    }
    /** 批次内有效袋码的品种编号集合(忽略历史空品种袋码),用于录入批次时的品种一致性校验 */
    @Select("SELECT DISTINCT item_id FROM mes_pro_bag_code " +
            "WHERE deleted = 0 AND status <> 30 AND batch_id = #{batchId} AND item_id IS NOT NULL")
    List<Long> selectDistinctItemIdsByBatchId(@Param("batchId") Long batchId);
    default List<MesProBagCodeDO> selectListByIdsForUpdate(Collection<Long> ids) {
        return selectList(new LambdaQueryWrapperX<MesProBagCodeDO>()
@@ -112,6 +206,15 @@
                   @Param("toStatus") Integer toStatus, @Param("cancelTime") LocalDateTime cancelTime,
                   @Param("cancelReason") String cancelReason);
    @Update("UPDATE mes_pro_bag_code SET sales_id = #{salesId}, outbound_time = #{outboundTime}, update_time = NOW() "
            + "WHERE id = #{id} AND deleted = 0")
    void markOutbound(@Param("id") Long id, @Param("salesId") Long salesId,
                      @Param("outboundTime") LocalDateTime outboundTime);
    @Update("UPDATE mes_pro_bag_code SET sales_id = NULL, outbound_time = NULL, update_time = NOW() "
            + "WHERE id = #{id} AND deleted = 0")
    void clearOutbound(@Param("id") Long id);
    @Select("SELECT COUNT(*) FROM mes_pro_bag_code WHERE batch_id = #{batchId} " +
            "AND deleted = 0 AND status <> 30")
    int selectEffectiveCountByBatchId(@Param("batchId") Long batchId);