<?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.system.dal.mysql.biz.MaterialInfoMapper">

    <!--
        多表关联分页查询：返回物资类别名称、仓库名称、供应商名称
        说明：
        - material_info 作为主表
        - material_category 关联 category_name
        - store_manage 关联 store_name
        - supplier_info 关联 supplier_name
     -->

    <select id="selectMaterialInfoPage"
            resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.material.MaterialInfoRespVO">
        SELECT
            mi.id,
            mi.material_number,
            mi.material_name,
            mi.category_id,
            mc.category_name AS category_name,
            mi.store_id,
            sm.store_name AS store_name,
            mi.material_unit,
            mi.supplier_id,
            si.supplier_name AS supplier_name,
            mi.specification,
            mi.material_count,
            mi.max_num,
            mi.min_num,
            mi.shelf_life,
            mi.status,
            mi.tenant_id,
            mi.create_time,
            mi.update_time,
            mi.creator,
            mi.updater
        FROM material_info mi
        LEFT JOIN material_category mc ON mc.id = mi.category_id
        LEFT JOIN store_manage sm ON sm.id = mi.store_id
        LEFT JOIN supplier_info si ON si.id = mi.supplier_id
        WHERE 1=1
            <if test="pageVO.categoryId != null">
                AND mi.category_id = #{pageVO.categoryId}
            </if>
            <if test="pageVO.materialNumber != null and pageVO.materialNumber != ''">
                AND mi.material_number LIKE CONCAT('%', #{pageVO.materialNumber}, '%')
            </if>
            <if test="pageVO.materialName != null and pageVO.materialName != ''">
                AND mi.material_name LIKE CONCAT('%', #{pageVO.materialName}, '%')
            </if>
            <if test="pageVO.storeId != null">
                AND mi.store_id = #{pageVO.storeId}
            </if>
            <if test="pageVO.tenantIds != null and pageVO.tenantIds.length &gt; 0">
                AND mi.tenant_id IN
                <foreach collection="pageVO.tenantIds" item="tid" open="(" separator="," close=")">
                    #{tid}
                </foreach>
            </if>
        ORDER BY mi.id DESC
    </select>

    <select id="selectMaterialInfoById"
            resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.material.MaterialInfoRespVO">
        SELECT
            mi.id,
            mi.material_number,
            mi.material_name,
            mi.category_id,
            mc.category_name AS category_name,
            mi.store_id,
            sm.store_name AS store_name,
            mi.material_unit,
            mi.supplier_id,
            si.supplier_name AS supplier_name,
            mi.specification,
            mi.material_count,
            mi.max_num,
            mi.min_num,
            mi.shelf_life,
            mi.status,
            mi.tenant_id,
            mi.create_time,
            mi.update_time,
            mi.creator,
            mi.updater
        FROM material_info mi
        LEFT JOIN material_category mc ON mc.id = mi.category_id
        LEFT JOIN store_manage sm ON sm.id = mi.store_id
        LEFT JOIN supplier_info si ON si.id = mi.supplier_id
        WHERE mi.id = #{id}
        LIMIT 1
    </select>

    <select id="selectMaterialInfoByTenantId"
            resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.material.MaterialInfoRespVO">
        SELECT
        mi.id,
        mi.material_number,
        mi.material_name,
        mi.category_id,
        mc.category_name AS category_name,
        mi.store_id,
        sm.store_name AS store_name,
        mi.material_unit,
        mi.supplier_id,
        si.supplier_name AS supplier_name,
        mi.specification,
        mi.material_count,
        mi.max_num,
        mi.min_num,
        mi.shelf_life,
        mi.status,
        mi.tenant_id,
        mi.create_time,
        mi.update_time,
        mi.creator,
        mi.updater,
        mi.material_in_price
        FROM material_info mi
        LEFT JOIN material_category mc ON mc.id = mi.category_id
        LEFT JOIN store_manage sm ON sm.id = mi.store_id
        LEFT JOIN supplier_info si ON si.id = mi.supplier_id
        WHERE mi.tenant_id = #{tenantId}
    </select>

</mapper>

