<?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="com.bonus.canteen.core.menu.mapper.MenuMaterialRegisterMapper">
    <update id="updateMaterialRegisterStatus">
        UPDATE menu_material_register SET if_success = #{status} WHERE material_id = #{materialId}
    </update>

    <select id="listNotRegisterMaterialPic" resultType="com.bonus.canteen.core.drp.v2.po.NotRegisterMaterialPicModel">
        SELECT
            mm.material_id,
            mm.material_name,
            mmrp.image_url
        FROM
            menu_material mm
                LEFT JOIN menu_material_register mmr ON mmr.material_id = mm.material_id
                LEFT JOIN menu_material_register_pic mmrp ON mmrp.material_id = mm.material_id
        WHERE
            mm.del_flag = #{delFlag}
            AND (mmr.if_success != #{commonYes} or mmr.if_success is null)
	        AND mmrp.image_url IS NOT NULL
    </select>
    <select id="pageRegisterMaterial" resultType="com.bonus.canteen.core.drp.v2.vo.DrpAndroidRegisterMaterialPageVO">
        SELECT
            mm.material_id,
            mm.material_name,
            IF(mmr.if_success =1,1,2) ifRegister,
            IF( t1.imageNum > 0, 1, 2 ) ifCollect,
            t1.imageNum
        FROM
                menu_material mm
                LEFT JOIN menu_material_register mmr ON mm.material_id = mmr.material_id
                LEFT JOIN ( SELECT material_id, count( image_url ) imageNum FROM menu_material_register_pic GROUP BY material_id ) t1 ON t1.material_id = mmr.material_id
        WHERE
            mm.del_flag = 2
            <if test="content.ifRegister == 1">
                AND mmr.if_success = 1
            </if>
            <if test="content.ifRegister == 2">
                AND (mmr.if_success != 1 or mmr.if_success is null)
            </if>
            <if test="content.materialInfo != null and content.materialInfo != ''">
              and (
              mm.material_name like concat('%', #{content.materialInfo}, '%')
              or mm.pinyin_initials like concat(#{content.materialInfo}, '%')
              or mm.bar_code like concat('%', #{content.materialInfo}, '%')
              or mm.material_code like concat('%', #{content.materialInfo}, '%')
              )
            </if>
            <if test="content.ifCollect == 1">
                and t1.imageNum > 0
            </if>
            <if test="content.ifCollect == 2">
                and (t1.imageNum = 0 or t1.imageNum is null)
            </if>
            and (mm.area_id = #{content.areaId} or mm.area_id is null)
    </select>
    <select id="pageNotRegisterMaterial" resultType="com.bonus.canteen.core.drp.v2.vo.DrpAndroidRegisterMaterialPageVO">
        SELECT
            mm.material_id,
            mm.material_name,
            IF
            ( t1.imageNum > 0, 1, 2 ) ifCollect
        FROM
            menu_material mm
                LEFT JOIN menu_material_register mmr ON mmr.material_id = mm.material_id
                LEFT JOIN ( SELECT material_id, count( image_url ) imageNum FROM menu_material_register_pic GROUP BY material_id ) t1 ON t1.material_id = mm.material_id
        WHERE
            (mmr.if_success != 1 or mmr.if_success is null)
            AND mm.del_flag = 2
        <if test="content.materialInfo != null and content.materialInfo != ''">
            and (
            mm.material_name like concat('%', #{content.materialInfo}, '%')
            or mm.pinyin_initials like concat(#{content.materialInfo}, '%')
            or mm.bar_code like concat('%', #{content.materialInfo}, '%')
            or mm.material_code like concat('%', #{content.materialInfo}, '%')
            )
        </if>
        <if test="content.ifCollect == 1">
            and t1.imageNum > 0
        </if>
        <if test="content.ifCollect == 2">
            and (t1.imageNum = 0 or t1.imageNum is null)
        </if>
    </select>
    <select id="listThisWaitRegisterMaterial"
            resultType="com.bonus.canteen.core.drp.v2.vo.DrpAiThisWaitRegisterMaterialListVO">
        SELECT DISTINCT
            mm.material_id,
            mm.material_name
        FROM
            menu_material mm
                LEFT JOIN menu_material_register mmr ON mmr.material_id = mm.material_id
                LEFT JOIN menu_material_register_pic mmrp ON mmrp.material_id = mm.material_id
        WHERE
            mm.del_flag = 2
            AND (mmr.if_success != 1 or mmr.if_success is null)
	        AND mmrp.image_url IS NOT NULL
            <if test="content.materialInfo != null and content.materialInfo != ''">
                and (
                mm.material_name like concat('%', #{content.materialInfo}, '%')
                or mm.pinyin_initials like concat(#{content.materialInfo}, '%')
                or mm.bar_code like concat('%', #{content.materialInfo}, '%')
                or mm.material_code like concat('%', #{content.materialInfo}, '%')
                )
            </if>
    </select>
</mapper>
