<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.iocoder.yudao.module.identity.dal.mysql.generaterecord.GenerateRecordMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->

    <select id="getPageList" resultType="cn.iocoder.yudao.module.identity.controller.admin.generaterecord.vo.GenerateRecordRespVO">
        SELECT
            igr.*, igb.batch_id, phb.goods_name
        FROM identity_generate_record igr
        LEFT JOIN identity_generate_batch igb ON igr.generate_record_id = igb.generate_record_id
        LEFT JOIN product_harvest_batch phb ON phb.harvest_batch_id = igb.batch_id
        <where>
            igr.deleted = 0
            <if test="pageReqVO.generateRecordId != null">and igr.generate_record_id = #{pageReqVO.generateRecordId}</if>
            <if test="pageReqVO.codeRuleFormat != null and pageReqVO.codeRuleFormat != ''">and igr.code_rule_format like concat('%',#{pageReqVO.codeRuleFormat},'%') ESCAPE '/'</if>
            <if test="pageReqVO.belongBusinessName != null and pageReqVO.belongBusinessName != ''">and igr.belong_business_name like concat('%',#{pageReqVO.belongBusinessName},'%') ESCAPE '/'</if>
            <if test="pageReqVO.createTime != null and pageReqVO.createTime.length == 2">
                and igr.create_time BETWEEN #{pageReqVO.createTime[0]} AND #{pageReqVO.createTime[1]}
            </if>
            <if test="pageReqVO.batchId != null and pageReqVO.batchId != ''">and igb.batch_id = #{pageReqVO.batchId}</if>
        </where>
        order by igr.create_time desc
    </select>

    <select id="getGenerateRecordByBatchId" resultType="cn.iocoder.yudao.module.identity.api.generaterecord.dto.GenerateRecordDTO">
        SELECT
            igr.*, igb.batch_id
        FROM identity_generate_record igr
        LEFT JOIN identity_generate_batch igb ON igr.generate_record_id = igb.generate_record_id
        WHERE igr.deleted = 0 AND igb.batch_id = #{batchId}
    </select>

</mapper>
