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

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

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

    <sql id="log_collect">
            i.NAME AS itemName,
            u.NAME AS userName,
            iul.id,
            iul.deleted,
            iul.created,
            iul.modified,
            iul.item_id,
            iul.user_id,
            iul.log_date,
            iul.task_situation
    </sql>

    <select id="logListByUserId" parameterType="java.lang.Long" resultType="com.dayou.vo.ItemUserLogVO">
        SELECT
            <include refid="log_collect"/>,s.name as stageName
        FROM
            item_user_log iul
                INNER JOIN (
                SELECT
                distinct iu1.item_id,
                    iu1.user_id
                FROM
                    item_user iu1
                WHERE
                    EXISTS ( SELECT item_id FROM item_user iu2 WHERE iu2.user_id = #{userId} AND iu1.item_id = iu2.item_id )) temp ON iul.item_id = temp.item_id
                AND iul.user_id = temp.user_id
                INNER JOIN item i ON i.id = temp.item_id
                INNER JOIN user u ON u.id = temp.user_id
        left join item_stage s on s.id = iul.stage_id
        WHERE
            u.deleted = 0
          AND i.deleted = 0
          AND iul.deleted = 0
        <choose>
            <when test="startDate!=null">
                and log_date &gt;= #{startDate}
            </when>
            <otherwise>
                and log_date &gt;= date_add(curDate(),INTERVAL -10 DAY)
            </otherwise>
        </choose>
        <choose>
            <when test="endDate!=null">
                and log_date &lt;= #{endDate}
            </when>
            <otherwise>
                and log_date &lt;= curDate()
            </otherwise>
        </choose>
        AND iul.item_id = #{itemId}
    </select>

    <select id="logListByDepartmentId" parameterType="java.lang.Long" resultType="com.dayou.vo.ItemUserLogVO">
        SELECT
        <include refid="log_collect"/>,s.name as stageName
        FROM
            item_user_log iul
                INNER JOIN
            (	select distinct up.user_id from user_post up where EXISTS
                (	select id from post where post.deleted=0
                                        and up.post_id =post.id
                                        <if test="departmentId!=null">
                                            and department_id = #{departmentId}
                                        </if>
                ) and up.deleted = 0) temp on temp.user_id = iul.user_id
                INNER JOIN item i ON i.id = iul.item_id
                INNER JOIN user u ON u.id = temp.user_id
        left join item_stage s on s.id = iul.stage_id
        WHERE
            u.deleted = 0
          AND i.deleted = 0
          AND iul.deleted = 0
            <choose>
                <when test="startDate!=null">
                    and log_date &gt;= #{startDate}
                </when>
                <otherwise>
                    and log_date &gt;= date_add(curDate(),INTERVAL -10 DAY)
                </otherwise>
            </choose>
            <choose>
                <when test="endDate!=null">
                    and log_date &lt;= #{endDate}
                </when>
                <otherwise>
                    and log_date &lt;= curDate()
                </otherwise>
            </choose>
        <if test="itemId!=null">
            and iul.item_id = #{itemId}
        </if>
    </select>

    <select id="page" parameterType="com.dayou.vo.ItemUserLogVO" resultType="com.dayou.vo.ItemUserLogVO">
        SELECT
        iul.id,iul.log_date as logDate,i.name as itemName ,iul.created,s.name as stageName
        FROM
        item_user_log iul left join item i on iul.item_id = i.id
        left join item_stage s on s.id = iul.stage_id
        where iul.deleted = 0 and i.deleted = 0 and iul.user_id = #{userId}
        <if test="itemUserLogVO!=null and itemUserLogVO.itemName!=null and itemUserLogVO.itemName!='' ">
            and i.name like concat ('%',#{itemUserLogVO.itemName},'%')
        </if>
        order by iul.log_date DESC
    </select>

    <select id="detail" parameterType="java.lang.Long" resultType="com.dayou.vo.ItemUserLogVO">
        SELECT
        <include refid="Base_Column_List"/> , i.name as itemName,s.name as stageName
        FROM
        item_user_log iul left join item i on iul.item_id = i.id left join item_stage s on s.id = iul.stage_id
        where iul.deleted = 0 and i.deleted = 0 and iul.id = #{logId}
    </select>

    <select id="getLogs" resultType="com.dayou.vo.ItemUserLogVO">
        SELECT
        iul.id,
        iul.item_id,
        iul.log_date,
        i.NAME AS itemName
        FROM
        item_user_log iul
        LEFT JOIN item i ON i.id = iul.item_id
        WHERE
        DATE_SUB( curDate(), INTERVAL 10 DAY ) &lt;= date( iul.log_date )
        AND iul.deleted = 0
        AND i.deleted = 0
order by iul.log_date ASC
    </select>

    <select id="logStat" resultType="com.dayou.vo.LogStatVO">
        SELECT
            (select count(*) from item_user_log where log_date = curDate() and deleted = 0) as currentDayNum,
            (select count(*) from item_user_log where deleted = 0) as totalNum,
            (select u.name from item_user_log iul left join user u on u.id = iul.user_id where iul.deleted =0 and u.deleted = 0  order by iul.created DESC LIMIT 1) as userName,
(select log_date from item_user_log where deleted =0 order by created DESC LIMIT 1) as logDate
    </select>
</mapper>
