<?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 = 35: 其他资质证明
    -->
    <select id="getStoreLicensePage"
            resultType="shop.alien.entity.store.vo.StoreLicenseInfoVo">
        SELECT
            CASE
                WHEN si.img_type = 14 THEN '营业执照'
                WHEN si.img_type = 35 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 = 35 THEN slh.license_execute_status
                ELSE ''
            END AS states,
            CASE
                WHEN si.img_type = 14 THEN s.update_business_license_time
                WHEN si.img_type = 35 THEN slh.created_time
                ELSE NULL
            END AS submit_date,
            CASE
                WHEN si.img_type = 14 THEN s.business_license_expiration_time
                WHEN si.img_type = 35 THEN NULL
                ELSE NULL
            END AS expiration_time,
            CASE
                WHEN si.img_type = 14 THEN s.business_license_reason
                WHEN si.img_type = 35 THEN slh.reason_refusal
                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, 35)
                               AND si.delete_flag = 0
                 LEFT JOIN store_user su ON s.id = su.store_id
                 LEFT JOIN (
                     SELECT slh.store_id, slh.license_execute_status, slh.created_time, slh.reason_refusal, slh.img_url
                     FROM store_license_history slh
                     INNER JOIN (
                         SELECT store_id, MAX(created_time) AS max_time
                         FROM store_license_history
                         WHERE license_status = 2 AND delete_flag = 0
                         GROUP BY store_id
                     ) slh2 ON slh.store_id = slh2.store_id 
                             AND slh.created_time = slh2.max_time
                     WHERE slh.license_status = 2 AND slh.delete_flag = 0
                 ) slh ON s.id = slh.store_id AND si.img_type = 35
        WHERE s.delete_flag = 0
            AND si.img_type IS NOT NULL
        <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 = 35 AND slh.license_execute_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 = 35 AND slh.created_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 = 35 AND slh.created_time &lt;= #{endSubmitDate})
            )
        </if>
        ORDER BY submit_date DESC, s.store_name ASC
    </select>

</mapper>


