<?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.dayou.mapper.MarketLogMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.MarketLog">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="user_id" property="userId" />
        <result column="task_situation" property="taskSituation" />
        <result column="log_date" property="logDate" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        ml.id,
        ml.deleted,
        ml.created,
        ml.modified,
        ml.user_id, ml.task_situation, ml.log_date
    </sql>

    <select id="getList" resultType="com.dayou.vo.ItemUserLogVO">
        select <include refid="Base_Column_List"/>,u.name as userName
        from market_log ml left join user u on u.id = ml.user_id
where u.deleted = 0 and ml.deleted = 0
        <choose>
            <when test="startDate!=null">
                and ml.log_date &gt;= #{startDate}
            </when>
            <otherwise>
                and ml.log_date &gt;= date_add(curDate(),INTERVAL -10 DAY)
            </otherwise>
        </choose>
        <choose>
            <when test="endDate!=null">
                and ml.log_date &lt;= #{endDate}
            </when>
            <otherwise>
                and ml.log_date &lt;= curDate()
            </otherwise>
        </choose>
        <if test="userIds!=null and userIds.size!=0">
            and  ml.user_id in
            <foreach collection="userIds" open="(" close=")" separator="," item="userId">
                #{userId}
            </foreach>
        </if>
        <if test="keyword!=null and keyword!='' ">
            and (
                u.name like concat ('%',#{keyword},'%')
                or ml.task_situation like concat ('%',#{keyword},'%')
            )
        </if>
    </select>

    <select id="getPage" parameterType="com.dayou.entity.MarketLog" resultType="com.dayou.vo.MarketLogVO">
        SELECT
        <include refid="Base_Column_List"/>,(
            SELECT
                count( id )
            FROM
                business_reply
            WHERE
                biz_type = 'MARKET_LOG_REPLY'
              AND biz_table_id = ml.id
              AND deleted = 0
              AND receiver_id = #{marketLog.userId}
              and state = 0
        ) AS replyCount
        FROM
            market_log ml
        WHERE
            ml.user_id = #{marketLog.userId}
          AND ml.deleted = 0
order by ml.id DESC
    </select>

</mapper>
