<?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.gree.mall.manager.commonmapper.SopRulePushMapper">
    <resultMap id="BaseResultMap" type="com.gree.mall.manager.bean.sop.SopRuleData">
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <result column="rule_name" jdbcType="VARCHAR" property="ruleName"/>
        <result column="company_wechat_id" jdbcType="VARCHAR" property="companyWechatId"/>
        <result column="company_name" jdbcType="VARCHAR" property="companyName"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="create_by" jdbcType="VARCHAR" property="createBy"/>
        <result column="status" jdbcType="TINYINT" property="status"/>
        <collection property="sopPushTask" ofType="com.gree.mall.manager.bean.sop.SopPushTaskData" select="task" column="id">
            <id column="id" jdbcType="VARCHAR" property="id"/>
            <result column="task_name" jdbcType="VARCHAR" property="taskName"/>
            <result column="type" jdbcType="TINYINT" property="type"/>
            <result column="status" jdbcType="TINYINT" property="status"/>
            <result column="push_type" jdbcType="TINYINT" property="pushType"/>
            <result column="push_min" jdbcType="INTEGER" property="pushMin"/>
            <result column="push_day" jdbcType="INTEGER" property="pushDay"/>
            <result column="push_day_min" jdbcType="VARCHAR" property="pushDayMin"/>
            <result column="send_type" jdbcType="TINYINT" property="sendType"/>
            <result column="send_url_name" jdbcType="VARCHAR" property="sendUrlName"/>
            <result column="send_url" jdbcType="VARCHAR" property="sendUrl"/>
            <result column="send_file_name" jdbcType="VARCHAR" property="sendFileName"/>
            <result column="send_appid" jdbcType="VARCHAR" property="sendAppid"/>
            <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
            <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
            <collection property="tags" ofType="com.gree.mall.manager.plus.entity.Tag" select="tag" column="id">
                <id column="tag_id" jdbcType="VARCHAR" property="tagId"/>
                <result column="group_id" jdbcType="VARCHAR" property="groupId"/>
                <result column="group_name" jdbcType="VARCHAR" property="groupName"/>
                <result column="name" jdbcType="VARCHAR" property="name"/>
                <result column="sort_num" jdbcType="INTEGER" property="sortNum"/>
                <result column="deleted" jdbcType="BIT" property="deleted"/>
                <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
                <result column="company_wechat_id" jdbcType="VARCHAR" property="companyWechatId"/>
                <result column="company_name" jdbcType="VARCHAR" property="companyName"/>
            </collection>
        </collection>
        <collection property="enterpriseWechatWorkers" ofType="com.gree.mall.manager.plus.entity.EnterpriseWechatWorker"
                    select="worker" column="id">
            <id column="id" jdbcType="INTEGER" property="id"/>
            <result column="wechat_user_id" jdbcType="VARCHAR" property="wechatUserId"/>
            <result column="name" jdbcType="VARCHAR" property="name"/>
            <result column="gender" jdbcType="INTEGER" property="gender"/>
            <result column="mobile" jdbcType="VARCHAR" property="mobile"/>
            <result column="email" jdbcType="VARCHAR" property="email"/>
            <result column="main_department" jdbcType="VARCHAR" property="mainDepartment"/>
            <result column="department_name" jdbcType="VARCHAR" property="departmentName"/>
            <result column="qr_code" jdbcType="VARCHAR" property="qrCode"/>
            <result column="config_id" jdbcType="VARCHAR" property="configId"/>
            <result column="qr_code2" jdbcType="VARCHAR" property="qrCode2"/>
            <result column="department_ids" jdbcType="VARCHAR" property="departmentIds"/>
            <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
            <result column="position" jdbcType="VARCHAR" property="position"/>
            <result column="corp_id" jdbcType="VARCHAR" property="corpId"/>
        </collection>
    </resultMap>

    <select id="list" resultMap="BaseResultMap">
        SELECT sr.*
        FROM sop_rule sr
        where 1=1
        <if test="id != null and id != ''">
            and sr.id =#{id}
        </if>
        <if test="ruleName != null and ruleName != ''">
            and sr.rule_name like concat('%',#{ruleName},'%')
        </if>
        <if test="status != null and status != ''">
            and sr.status = #{status}
        </if>
        <if test="companyWechatIds != null">
            and sr.company_wechat_id in
            <foreach collection="companyWechatIds" item="item" open="(" separator="," close=")" index="index">
                #{item}
            </foreach>
        </if>
        order by sr.create_time desc
    </select>

    <select id="task" resultType="com.gree.mall.manager.plus.entity.SopPushTask">
        SELECT spt.*
        FROM sop_rule_task srt
                 LEFT JOIN sop_push_task spt ON spt.id = srt.task_id
        where srt.rule_id = #{id}
    </select>

    <select id="worker" resultType="com.gree.mall.manager.plus.entity.EnterpriseWechatWorker">
        SELECT
            eww.*
        FROM
            sop_rule_worker srw
                LEFT JOIN enterprise_wechat_worker eww ON eww.wechat_user_id = srw.wechat_user_id
                AND srw.corp_id = eww.corp_id
        where srw.rule_id = #{id}
    </select>

    <resultMap id="detailTaskMap" type="com.gree.mall.manager.bean.sop.SopPushTaskData">
            <id column="id" jdbcType="VARCHAR" property="id"/>
            <result column="task_name" jdbcType="VARCHAR" property="taskName"/>
            <result column="type" jdbcType="TINYINT" property="type"/>
            <result column="status" jdbcType="TINYINT" property="status"/>
            <result column="push_type" jdbcType="TINYINT" property="pushType"/>
            <result column="push_min" jdbcType="INTEGER" property="pushMin"/>
            <result column="push_day" jdbcType="INTEGER" property="pushDay"/>
            <result column="push_day_min" jdbcType="VARCHAR" property="pushDayMin"/>
            <result column="send_type" jdbcType="TINYINT" property="sendType"/>
            <result column="send_url_name" jdbcType="VARCHAR" property="sendUrlName"/>
            <result column="send_url" jdbcType="VARCHAR" property="sendUrl"/>
            <result column="send_file_name" jdbcType="VARCHAR" property="sendFileName"/>
            <result column="send_appid" jdbcType="VARCHAR" property="sendAppid"/>
            <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
            <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <collection property="tags" ofType="com.gree.mall.manager.plus.entity.Tag" select="tag" column="id">
            <id column="tag_id" jdbcType="VARCHAR" property="tagId"/>
            <result column="group_id" jdbcType="VARCHAR" property="groupId"/>
            <result column="group_name" jdbcType="VARCHAR" property="groupName"/>
            <result column="name" jdbcType="VARCHAR" property="name"/>
            <result column="sort_num" jdbcType="INTEGER" property="sortNum"/>
            <result column="deleted" jdbcType="BIT" property="deleted"/>
            <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
            <result column="company_wechat_id" jdbcType="VARCHAR" property="companyWechatId"/>
            <result column="company_name" jdbcType="VARCHAR" property="companyName"/>
        </collection>
    </resultMap>

    <select id="detailTask"  resultMap="detailTaskMap">
        SELECT spt.*
        FROM
            sop_push_task spt
        where spt.id = #{id}
    </select>

    <select id="tag" resultType="com.gree.mall.manager.plus.entity.Tag">
        SELECT
            t.*
        FROM
            sop_push_task_tag ptt
                LEFT JOIN tag t ON ptt.tag_id = t.tag_id
        where ptt.task_id = #{id}
    </select>


    <select id="getTaskPush"  resultMap="detailTaskMap">
        SELECT spt.*
        FROM sop_rule_task srt
                 LEFT JOIN sop_push_task spt ON spt.id = srt.task_id
        where srt.rule_id = #{id}
    </select>

</mapper>