<?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.pluss.platform.mapper.MerchantGuideMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.pluss.platform.entity.MerchantGuide">
        <id column="id" property="id" />
        <result column="merchantCode" property="merchantCode" />
        <result column="merchantName" property="merchantName" />
        <result column="name" property="name" />
        <result column="status" property="status" />
        <result column="createTime" property="createTime" />
        <result column="updateTime" property="updateTime" />
    </resultMap>

    <select id="userMerchantGuide" resultType="cn.pluss.platform.entity.MerchantGuide">
        SELECT null id, #{merchantCode} merchantCode, #{merchantName} merchantName, IFNULL(mg.`name`, gl.`name`) `name`, gl.createTime, gl.updateTime,
               IF (tCount.`status` = 0, gl.`status`,
                   IFNULL (mg.`status`, 0)
                   ) `status`, gl.id guideId
        FROM tb_pluss_guide_label gl
                 LEFT JOIN (
            SELECT * FROM tb_pluss_merchant_guide WHERE merchantCode = #{merchantCode}
        ) mg ON gl.id = mg.guideId
        , (
            SELECT SUM(`status`) `status` FROM tb_pluss_merchant_guide mg WHERE mg.merchantCode = #{merchantCode}
        ) tCount
        UNION
        SELECT id, merchantCode, merchantName, `name`, createTime, updateTime, `status`, null guideId FROM tb_pluss_merchant_guide
        WHERE `guideId` IS NULL AND merchantCode = #{merchantCode}
        ORDER BY createTime
    </select>
</mapper>
