<?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.gxwebsoft.oa.mapper.TaskMapper">

    <!-- 关联查询sql -->
    <sql id="selectSql">
        SELECT a.*,
        b.nickname as promoterName,b.avatar as promoterAvatar,b.alias as promoterAlias,b.company_name as promoterCompanyName,
        c.nickname as commanderName,c.avatar as commanderAvatar,c.alias as commanderAlias,c.company_name as commanderCompanyName,
        d.nickname as lastNickname ,d.avatar as lastAvatar,d.real_name as lastRealName,d.company_name as lastCompanyName,
        e.app_id,e.app_name
        FROM oa_task a
        LEFT JOIN sys_user b ON a.promoter = b.user_id
        LEFT JOIN sys_user c ON a.commander = c.user_id
        LEFT JOIN sys_user d ON a.last_read_user = d.user_id
        LEFT JOIN oa_app e ON a.app_id = e.app_id
        <where>
            <if test="param.taskId != null">
                AND a.task_id = #{param.taskId}
            </if>
            <if test="param.name != null">
                AND a.name LIKE CONCAT('%', #{param.name}, '%')
            </if>
            <if test="param.taskType != null">
              AND a.task_type IN
              <foreach collection="param.taskType.split(',')" item="item" separator="," open="(" close=")">
                #{item}
              </foreach>
            </if>
            <if test="param.customerId != null">
                AND a.customer_id = #{param.customerId}
            </if>
            <if test="param.appId != null">
              AND a.app_id = #{param.appId}
            </if>
            <if test="param.projectId != null">
                AND a.project_id = #{param.projectId}
            </if>
            <if test="param.assetsId != null">
                AND a.assets_id = #{param.assetsId}
            </if>
            <if test="param.startTime != null">
                AND a.start_time LIKE CONCAT('%', #{param.startTime}, '%')
            </if>
            <if test="param.endTime != null">
                AND a.end_time LIKE CONCAT('%', #{param.endTime}, '%')
            </if>
            <if test="param.content != null">
                AND a.content LIKE CONCAT('%', #{param.content}, '%')
            </if>
            <if test="param.promoter != null">
                AND a.promoter = #{param.promoter}
            </if>
            <if test="param.commander != null">
                AND (a.commander = #{param.commander} OR a.promoter = #{param.commander})
            </if>
            <if test="param.progress != null">
              AND a.progress IN
              <foreach collection="param.progress.split(',')" item="item" separator="," open="(" close=")">
                #{item}
              </foreach>
            </if>
            <if test="param.priority != null">
                AND a.priority LIKE CONCAT('%', #{param.priority}, '%')
            </if>
            <if test="param.quality != null">
                AND a.quality LIKE CONCAT('%', #{param.quality}, '%')
            </if>
            <if test="param.day != null">
                AND a.day = #{param.day}
            </if>
            <if test="param.userId != null">
              AND a.user_id = #{param.userId}
            </if>
<!--            <if test="param.userId != null">-->
<!--              AND a.task_id IN (SELECT task_id FROM oa_task_user WHERE user_id=#{param.userId})-->
<!--            </if>-->
            <if test="param.organizationId != null">
                AND a.organization_id = #{param.organizationId}
            </if>
            <if test="param.shopId != null">
                AND a.shop_id = #{param.shopId}
            </if>
            <if test="param.sortNumber != null">
                AND a.sort_number = #{param.sortNumber}
            </if>
            <if test="param.comments != null">
                AND a.comments LIKE CONCAT('%', #{param.comments}, '%')
            </if>
            <if test="param.status != null">
                AND a.status = #{param.status}
            </if>
            <if test="param.deleted != null">
                AND a.deleted = #{param.deleted}
            </if>
            <if test="param.deleted == null">
                AND a.deleted = 0
            </if>
            <if test="param.createTimeStart != null">
                AND a.create_time &gt;= #{param.createTimeStart}
            </if>
            <if test="param.createTimeEnd != null">
                AND a.create_time &lt;= #{param.createTimeEnd}
            </if>
            <if test="param.keywords != null">
              AND (a.name LIKE CONCAT('%', #{param.keywords}, '%')
                       OR a.task_id LIKE CONCAT('%', #{param.keywords}, '%')
                       OR b.nickname LIKE CONCAT('%', #{param.keywords}, '%')
                       OR b.real_name LIKE CONCAT('%', #{param.keywords}, '%')
                       OR b.company_name LIKE CONCAT('%', #{param.keywords}, '%')
                       OR c.nickname LIKE CONCAT('%', #{param.keywords}, '%')
                       OR c.real_name LIKE CONCAT('%', #{param.keywords}, '%')
                       OR e.app_name LIKE CONCAT('%', #{param.keywords}, '%')
                  )
            </if>
            <if test="param.appName != null">
              AND b.app_name LIKE CONCAT('%', #{param.appName}, '%')
            </if>
        </where>
    </sql>

    <!-- 分页查询 -->
    <select id="selectPageRel" resultType="com.gxwebsoft.oa.entity.Task">
        <include refid="selectSql"></include>
    </select>

    <!-- 查询全部 -->
    <select id="selectListRel" resultType="com.gxwebsoft.oa.entity.Task">
        <include refid="selectSql"></include>
    </select>

</mapper>
