<?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.WeixinClockDaoImpl">

    <resultMap id="BaseResultMap" type="com.pcloud.book.weixinclock.entity.WeixinClock">
        <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"/>
    </resultMap>

    <resultMap id="BaseResultListMapDTO" 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"/>
        <association property="statisticVO" column="weixin_clock_id"
                     select="com.pcloud.book.weixinclock.dao.impl.WeixinClockGroupClassifyDaoImpl.getClockGroupStatistics" />
    </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"/>
        <association property="statisticVO" column="weixin_clock_id"
                     select="com.pcloud.book.weixinclock.dao.impl.WeixinClockGroupClassifyDaoImpl.getClockGroupStatistics" />
        <collection property="weixinClockRewards" column="weixin_clock_id"
                    select="com.pcloud.book.weixinclock.dao.impl.WeixinClockRewardDaoImpl.listRewardDetail4Adviser" />
        <collection property="classifyInfoDtos" column="weixin_clock_id"
                    select="com.pcloud.book.weixinclock.dao.impl.WeixinClockGroupClassifyDaoImpl.getClassifyInfo4Update" />
    </resultMap>

    <sql id="Base_Column_List">
        weixin_clock_id, clock_keyword, clock_guide, clock_introduction, clock_prize_id, clock_prize_url, is_open_coupon, is_open_prize, create_user, create_date,
        last_modified_user, last_modified_date
    </sql>

    <insert id="insert" parameterType="weixinClock" useGeneratedKeys="true" keyProperty="weixinClockId" flushCache="true">
        INSERT INTO weixin_clock (
        clock_keyword, clock_guide, clock_introduction, clock_prize_id, clock_prize_url, is_open_coupon, is_open_prize, create_user, create_date,
        last_modified_user, last_modified_date
        )
        VALUES (
        #{clockKeyword, jdbcType=VARCHAR}, #{clockGuide, jdbcType=VARCHAR}, #{clockIntroduction, jdbcType=VARCHAR}, #{clockPrizeId, jdbcType=BIGINT},
        #{clockPrizeUrl, jdbcType=VARCHAR}, #{isOpenCoupon}, #{isOpenPrize}, #{createUser, jdbcType=VARCHAR}, NOW(), #{lastModifiedUser, jdbcType=BIGINT}, NOW()
        )
    </insert>

    <!-- 获取基本信息-->
    <select id="getBaseInfoById" parameterType="long" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM weixin_clock
        WHERE weixin_clock_id = #{weixinClockId}
        LIMIT 1
    </select>

    <!--修改微信群签到打卡-->
    <update id="update" parameterType="weixinClock">
        UPDATE weixin_clock
        <set>
            <if test="clockKeyword != null">
                clock_keyword = #{clockKeyword, jdbcType=VARCHAR},
            </if>
            <if test="clockGuide != null">
                clock_guide = #{clockGuide, jdbcType=VARCHAR},
            </if>
            <if test="clockIntroduction != null">
                clock_introduction = #{clockIntroduction, jdbcType=VARCHAR},
            </if>
            <if test="clockPrizeId != null">
                clock_prize_id = #{clockPrizeId, jdbcType=BIGINT},
            </if>
            <if test="clockPrizeUrl != null">
                clock_prize_url = #{clockPrizeUrl, jdbcType=VARCHAR},
            </if>
            <if test="isOpenCoupon != null">
                is_open_coupon = #{isOpenCoupon},
            </if>
            <if test="isOpenPrize != null">
                is_open_prize = #{isOpenPrize},
            </if>
            <if test="lastModifiedUser != null">
                last_modified_user = #{lastModifiedUser, jdbcType=BIGINT},
            </if>
            last_modified_date = NOW()
        </set>
        WHERE weixin_clock_id = #{weixinClockId}
    </update>

    <!--删除微信群签到打卡-->
    <delete id="deleteWeixinClock" parameterType="map">
        DELETE FROM weixin_clock
        WHERE weixin_clock_id = #{weixinClockId}
    </delete>

    <select id="listWeixinClock4Adviser" parameterType="map" resultMap="BaseResultListMapDTO">
        SELECT  weixin_clock_id, clock_keyword, clock_guide, clock_introduction, is_open_coupon, is_open_prize,
        clock_prize_id, clock_prize_url, create_user, create_date, last_modified_user, last_modified_date
        FROM
        weixin_clock
        WHERE create_user = #{adviserId}
        <if test="keyword != null">
            and (clock_keyword like CONCAT('%', #{keyword}, '%'))
        </if>
        order by last_modified_date desc
    </select>

    <select id="getWeixinClockBaseInfo" parameterType="Long" resultMap="ClockBaseInfoMap">
        SELECT  weixin_clock_id, clock_keyword, clock_guide, clock_introduction, is_open_coupon, is_open_prize,
        clock_prize_id, clock_prize_url, create_user, create_date, last_modified_user, last_modified_date
        FROM
        weixin_clock
        WHERE
        weixin_clock_id = #{weixinClockId}
    </select>

    <select id="listGroupClassifyDetail4Clock" resultType="com.pcloud.book.group.vo.BookGroupClassifyVO" parameterType="map">
        SELECT
        c.id classifyId,
        c.book_id bookId,
        b.BOOK_NAME bookName,
        c.classify,
        c.book_group_id bookGroupId,
        sum(IFNULL(g.user_number,0)) userNumber
        FROM weixin_clock_group_classify wcg
        INNER JOIN book_group_classify c ON wcg.book_group_classify_id = c.id
        INNER JOIN book_group_qrcode g ON c.id = g.classify_id
        LEFT JOIN book b ON c.book_id = b.book_id
        WHERE c.is_delete = 0
        AND g.is_delete = 0
        AND b.is_delete = 0
        AND wcg.weixin_clock_id = #{weixinClockId}
        group by c.id
        order by c.id desc, g.id desc
    </select>

    <select id="getByWxGroupId" resultMap="ClockBaseInfoMap" parameterType="string">
        select cl.weixin_clock_id, cl.clock_keyword, cl.clock_guide, cl.clock_introduction, cl.clock_prize_id, cl.clock_prize_url, cl.is_open_coupon, cl.is_open_prize, cl.create_user, cl.create_date,
               cl.last_modified_user, cl.last_modified_date
        from weixin_clock cl
        left join weixin_clock_group_classify wc on cl.weixin_clock_id = wc.weixin_clock_id
        left join book_group_classify c on wc.book_group_classify_id = c.id
        left join book_group_qrcode g ON c.id = g.classify_id
        where g.weixin_group_id = #{wxGroupId} limit 1
    </select>

</mapper>