云
8 小时以前 02a1ae81eba7b48223052fb833859f0a65dd7d21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package cn.iocoder.yudao.module.mes.dal.mysql.pro.pallet;
 
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.mes.controller.admin.pro.pallet.vo.MesProPalletPageReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.pro.pallet.MesProPalletDO;
import cn.iocoder.yudao.module.mes.enums.pro.MesProPalletStatusEnum;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
 
import java.util.List;
 
/**
 * MES 托盘码 Mapper
 *
 * @author 超级管理员
 */
@Mapper
public interface MesProPalletMapper extends BaseMapperX<MesProPalletDO> {
 
    default MesProPalletDO selectByCode(String code) {
        return selectOne(MesProPalletDO::getCode, code);
    }
 
    default MesProPalletDO selectByScanCode(String code) {
        return selectOne(new LambdaQueryWrapperX<MesProPalletDO>().and(q ->
                q.eq(MesProPalletDO::getBusinessCode, code).or().eq(MesProPalletDO::getCode, code)
                        .or().eq(MesProPalletDO::getTempCode, code)));
    }
 
    default MesProPalletDO selectByScanCodeForUpdate(String code) {
        return selectOne(new LambdaQueryWrapperX<MesProPalletDO>().and(q ->
                q.eq(MesProPalletDO::getBusinessCode, code).or().eq(MesProPalletDO::getCode, code)
                        .or().eq(MesProPalletDO::getTempCode, code)).last("FOR UPDATE"));
    }
 
    default MesProPalletDO selectByTempCode(String tempCode) {
        return selectOne(MesProPalletDO::getTempCode, tempCode);
    }
 
    @Select("SELECT * FROM mes_pro_pallet WHERE id = #{id} AND deleted = 0 FOR UPDATE")
    MesProPalletDO selectByIdForUpdate(@Param("id") Long id);
 
    default List<MesProPalletDO> selectListByBatchId(Long batchId) {
        return selectList(MesProPalletDO::getBatchId, batchId);
    }
 
    /** 查询未绑定生产批次的临时托盘(无正式托盘码且未作废),供批次新增/补录绑定托盘下拉使用 */
    default List<MesProPalletDO> selectUnboundList() {
        return selectList(new LambdaQueryWrapperX<MesProPalletDO>()
                .isNull(MesProPalletDO::getBatchId)
                .isNull(MesProPalletDO::getCode)
                .eq(MesProPalletDO::getStatus, MesProPalletStatusEnum.IN_USE.getStatus())
                .orderByAsc(MesProPalletDO::getId));
    }
 
    /**
     * 查询袋码绑定托盘时可选的目标托盘:未作废的临时托盘与正式托盘。
     * <p>
     * 传入品种时仅返回该品种的托盘,以及尚未确定品种的预生成临时托盘(item_id 为空,绑定袋码时回填品种)。
     *
     * @param itemId 袋码品种编号,可为空
     * @return 托盘列表
     */
    default List<MesProPalletDO> selectBindableList(Long itemId) {
        return selectList(new LambdaQueryWrapperX<MesProPalletDO>()
                .ne(MesProPalletDO::getStatus, MesProPalletStatusEnum.CANCELED.getStatus())
                .and(itemId != null, q -> q.eq(MesProPalletDO::getItemId, itemId)
                        .or().isNull(MesProPalletDO::getItemId))
                .orderByDesc(MesProPalletDO::getId));
    }
 
    default PageResult<MesProPalletDO> selectPage(MesProPalletPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<MesProPalletDO>()
                .likeIfPresent(MesProPalletDO::getBusinessCode, reqVO.getBusinessCode())
                .likeIfPresent(MesProPalletDO::getTempCode, reqVO.getTempCode())
                .eqIfPresent(MesProPalletDO::getBatchId, reqVO.getBatchId())
                .likeIfPresent(MesProPalletDO::getBatchCode, reqVO.getBatchCode())
                .eqIfPresent(MesProPalletDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(MesProPalletDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(MesProPalletDO::getId)
                .and(cn.hutool.core.util.StrUtil.isNotBlank(reqVO.getCode()), q ->
                        q.like(MesProPalletDO::getCode, reqVO.getCode()).or()
                                .like(MesProPalletDO::getBusinessCode, reqVO.getCode()).or()
                                .like(MesProPalletDO::getTempCode, reqVO.getCode())));
    }
 
    /**
     * 查询批次内托盘最大序号(包含已逻辑删除记录,保证序号不回退、码值唯一)
     *
     * @param batchId 批次编号
     * @return 最大托盘序号,无记录返回 0
     */
    @Select("SELECT IFNULL(MAX(pallet_no), 0) FROM mes_pro_pallet WHERE batch_id = #{batchId}")
    Integer selectMaxPalletNo(@Param("batchId") Long batchId);
 
    /**
     * 物理删除某个生成组下尚未绑定批次的临时托盘(仅用于生成失败/取消后的半成品清理)
     * <p>
     * 已绑定批次的托盘可能已被后续业务使用,即使带有生成组标记也不能删。
     *
     * @param generateUuid 生成组 UUID
     * @param limit        单次最多删除行数
     * @return 实际删除行数
     */
    @org.apache.ibatis.annotations.Delete("DELETE FROM mes_pro_pallet WHERE generate_uuid = #{generateUuid} "
            + "AND batch_id IS NULL AND code IS NULL LIMIT #{limit}")
    int deletePhysicallyByGenerateUuidChunk(@Param("generateUuid") String generateUuid, @Param("limit") int limit);
 
    @org.apache.ibatis.annotations.Update("UPDATE mes_pro_pallet 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") java.time.LocalDateTime outboundTime);
 
    /** 整托扫码入库回写:幂等(已有入库标记的不再更新),用于防重复入库 */
    @org.apache.ibatis.annotations.Update("UPDATE mes_pro_pallet SET inbound_event_id = #{eventId}, inbound_time = NOW(), update_time = NOW() "
            + "WHERE id = #{id} AND deleted = 0 AND inbound_event_id IS NULL")
    int markInbound(@Param("id") Long id, @Param("eventId") Long eventId);
 
    /** 单袋、整托、整批入库后,只有有效袋码全部入库的托盘才同步标记已入库。 */
    @org.apache.ibatis.annotations.Update("UPDATE mes_pro_pallet p SET inbound_event_id = #{eventId}, inbound_time = NOW(), update_time = NOW() "
            + "WHERE p.batch_id = #{batchId} AND p.deleted = 0 AND p.status <> 20 AND p.inbound_event_id IS NULL "
            + "AND EXISTS (SELECT 1 FROM mes_pro_bag_code b WHERE b.pallet_id = p.id AND b.deleted = 0 AND b.status <> 30) "
            + "AND NOT EXISTS (SELECT 1 FROM mes_pro_bag_code b WHERE b.pallet_id = p.id AND b.deleted = 0 AND b.status <> 30 AND b.inbound_event_id IS NULL)")
    int markCompletedInboundByBatchId(@Param("batchId") Long batchId, @Param("eventId") Long eventId);
 
    @org.apache.ibatis.annotations.Update("UPDATE mes_pro_pallet SET sales_id = NULL, outbound_time = NULL, update_time = NOW() "
            + "WHERE id = #{id} AND deleted = 0")
    void clearOutbound(@Param("id") Long id);
 
}