<?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="shop.alien.mapper.StoreInfoMapper">

    <!--
        门店证照查询
        证照类型与状态、提交时间、到期时间映射关系：
          - img_type = 14: 营业执照
          - img_type IN (31, 32): 娱乐经营许可证
          - img_type IN (24, 25): 食品经营许可证
    -->
    <select id="getStoreLicensePage"
            resultType="shop.alien.entity.store.vo.StoreLicenseInfoVo">
        SELECT
            CASE
                WHEN si.img_type = 14 THEN '营业执照'
                WHEN si.img_type IN (31, 32) THEN '娱乐经营许可证'
                WHEN si.img_type IN (24, 25) THEN '食品经营许可证'
                WHEN si.img_type IN (33) THEN '身份证正面'
                WHEN si.img_type IN (34) THEN '身份证反面'
                ELSE ''
            END AS img_description,
            si.img_type AS img_type,
            s.id AS id,
            s.store_name AS store_name,
            s.store_tel AS store_tel,
            su.name AS name,
            si.img_url,
            s.business_section_name,
            s.business_classify_name,
            s.business_types_name,
            CASE
                WHEN si.img_type = 14 THEN s.business_license_status
                WHEN si.img_type IN (31, 32) THEN s.entertainment_licence_status
                WHEN si.img_type IN (24, 25) THEN s.food_licence_status
                WHEN si.img_type IN (33, 34) THEN s.id_card_status
                ELSE ''
            END AS states,
            CASE
                WHEN si.img_type = 14 THEN s.update_business_license_time
                WHEN si.img_type IN (31, 32) THEN s.update_entertainment_licence_time
                WHEN si.img_type IN (24, 25) THEN s.update_food_licence_time
                WHEN si.img_type IN (33, 34) THEN s.update_id_card_time
                ELSE NULL
            END AS submit_date,
            CASE
                WHEN si.img_type = 14 THEN s.business_license_expiration_time
                WHEN si.img_type IN (31, 32) THEN s.entertainment_licence_expiration_time
                WHEN si.img_type IN (24, 25) THEN s.food_licence_expiration_time
                WHEN si.img_type IN (33, 34) THEN s.id_card_expiration_time
                ELSE NULL
            END AS expiration_time,
            CASE
                WHEN si.img_type = 14 THEN s.business_license_reason
                WHEN si.img_type IN (31, 32) THEN s.entertainment_licence_reason
                WHEN si.img_type IN (24, 25) THEN s.food_licence_reason
                WHEN si.img_type IN (33, 34) THEN s.id_card_reason
                ELSE NULL
            END AS expiration_reason
        FROM store_info s
                 LEFT JOIN store_img si
                           ON s.id = si.store_id
                               AND si.img_type IN (14, 24, 25, 31, 32, 33, 34)
                               AND si.delete_flag = 0
                 left join store_user su on s.id = su.store_id
        WHERE s.delete_flag = 0
        <if test="storeName != null and storeName != ''">
            AND s.store_name LIKE CONCAT('%', #{storeName}, '%')
        </if>
        <if test="storeContact != null and storeContact != ''">
            AND su.name LIKE CONCAT('%', #{storeContact}, '%')
        </if>
        <if test="storeTel != null and storeTel != ''">
            AND s.store_tel LIKE CONCAT('%', #{storeTel}, '%')
        </if>
        <if test="businessSection != null and businessSection != ''">
            AND s.business_section = #{businessSection}
        </if>
        <if test="imgType != null">
            AND si.img_type = #{imgType}
        </if>
        <if test="states != null and states != ''">
            AND (
                (si.img_type = 14 AND s.business_license_status = #{states})
                OR (si.img_type IN (31, 32) AND s.entertainment_licence_status = #{states})
                OR (si.img_type IN (24, 25) AND s.food_licence_status = #{states})
            )
        </if>
        <if test="startSubmitDate != null and startSubmitDate != ''">
            AND (
                (si.img_type = 14 AND s.update_business_license_time &gt;= #{startSubmitDate})
                OR (si.img_type IN (31, 32) AND s.update_entertainment_licence_time &gt;= #{startSubmitDate})
                OR (si.img_type IN (24, 25) AND s.update_food_licence_time &gt;= #{startSubmitDate})
            )
        </if>
        <if test="endSubmitDate != null and endSubmitDate != ''">
            AND (
                (si.img_type = 14 AND s.update_business_license_time &lt;= #{endSubmitDate})
                OR (si.img_type IN (31, 32) AND s.update_entertainment_licence_time &lt;= #{endSubmitDate})
                OR (si.img_type IN (24, 25) AND s.update_food_licence_time &lt;= #{endSubmitDate})
            )
        </if>
        ORDER BY s.store_name ASC
    </select>

</mapper>


