<?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.pcloud.book.weixinclock.dao.impl.WeixinClockGroupClassifyDaoImpl">

    <resultMap id="BaseResultMap" type="com.pcloud.book.weixinclock.entity.WeixinClockGroupClassify">
        <id column="weixin_clock_group_classify_id" property="weixinClockGroupClassifyId" jdbcType="BIGINT"/>
        <result column="book_group_classify_id" property="bookGroupClassifyId" jdbcType="BIGINT"/>
        <result column="weixin_clock_id" property="weixinClockId" jdbcType="BIGINT"/>
    </resultMap>

    <resultMap id="ClockBaseInfoMap" type="com.pcloud.book.weixinclock.dto.WeixinClockDto">
        <id column="weixin_clock_id" property="weixinClockId" jdbcType="BIGINT"/>
        <result column="clock_keyword" property="clockKeyword" jdbcType="VARCHAR"/>
        <result column="clock_guide" property="clockGuide" jdbcType="VARCHAR"/>
        <result column="clock_introduction" property="clockIntroduction" jdbcType="VARCHAR"/>
        <result column="clock_prize_id" property="clockPrizeId" jdbcType="BIGINT"/>
        <result column="clock_prize_url" property="clockPrizeUrl" jdbcType="VARCHAR"/>
        <result column="is_open_coupon" property="isOpenCoupon" jdbcType="INTEGER"/>
        <result column="is_open_prize" property="isOpenPrize" jdbcType="INTEGER"/>
        <result column="create_user" property="createUser" jdbcType="BIGINT"/>
        <result column="create_date" property="createDate" jdbcType="TIMESTAMP"/>
        <result column="last_modified_user" property="lastModifiedUser" jdbcType="BIGINT"/>
        <result column="last_modified_date" property="lastModifiedDate" jdbcType="TIMESTAMP"/>
        <collection property="weixinClockRewards" column="weixin_clock_id"
                    select="com.pcloud.book.weixinclock.dao.impl.WeixinClockRewardDaoImpl.listRewardDetail4Adviser" />
    </resultMap>

    <insert id="batchInsert" parameterType="weixinClockGroupClassify" useGeneratedKeys="true" keyProperty="weixinClockGroupClassifyId" flushCache="true">
        insert into weixin_clock_group_classify (
        book_group_classify_id,
        weixin_clock_id
        ) values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.bookGroupClassifyId,jdbcType=BIGINT},
            #{item.weixinClockId,jdbcType=INTEGER}
            )
        </foreach>
    </insert>

    <select id="getKeywordByGroupClassify" parameterType="map" resultType="String">
        SELECT
	      wc.clock_keyword
        FROM
	        weixin_clock_group_classify wcc
        LEFT JOIN weixin_clock wc ON wcc.weixin_clock_id = wc.weixin_clock_id
        WHERE
	        wcc.book_group_classify_id = #{bookGroupClassifyId}
	    ORDER BY wc.create_date DESC
	    LIMIT 1
    </select>

    <select id="getClockBaseInfoByClassify" parameterType="map" resultMap="ClockBaseInfoMap">
        SELECT
	    wc.weixin_clock_id, wc.clock_keyword, wc.clock_guide, wc.clock_introduction, wc.clock_prize_id, wc.clock_prize_url, wc.create_user, wc.create_date,
        wc.last_modified_user, wc.last_modified_date, wc.is_open_coupon, wc.is_open_prize
        FROM
	        weixin_clock_group_classify wcc
        LEFT JOIN weixin_clock wc ON wcc.weixin_clock_id = wc.weixin_clock_id
        WHERE
	        wcc.book_group_classify_id = #{bookGroupClassifyId}
	    ORDER BY wc.create_date DESC
	    LIMIT 1
    </select>

    <!--获取打卡群统计信息-->
    <select id="getClockGroupStatistics" parameterType="Long" resultType="com.pcloud.book.group.vo.StatisticVO">
        SELECT
        COUNT(DISTINCT t1.book_group_classify_id) classifyCount,
        COUNT(DISTINCT t2.id) groupNumber
        FROM
        weixin_clock_group_classify t1
        INNER JOIN book_group_qrcode t2 ON t1.book_group_classify_id = t2.classify_id
        WHERE
        t2.is_delete = 0
        AND t1.weixin_clock_id = #{weixinClockId}
    </select>

    <!-- 删除书籍应用关联信息 -->
    <delete id="deleteById" parameterType="long" flushCache="true">
		DELETE FROM
			weixin_clock_group_classify
		WHERE
			weixin_clock_id = #{weixinClockId}
	</delete>

    <select id="getClassifyInfo4Update" parameterType="long" resultType="com.pcloud.book.weixinclock.dto.ClassifyInfoDto">
        SELECT
        c.id classifyId,
        c.classify classify,
        count(DISTINCT g.id) groupsCount
        FROM
        weixin_clock_group_classify wc
        LEFT JOIN book_group_classify c ON c.id = wc.book_group_classify_id
        LEFT JOIN book_group_qrcode g ON c.id = g.classify_id
        WHERE c.is_delete = 0
        AND g.is_delete = 0
        AND wc.weixin_clock_id = #{weixinClockId}
        group by c.id
        order by c.id desc, g.id desc
    </select>
</mapper>