<?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.hssx.pcbms.mapper.TaskMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hssx.pcbms.entity.Task">
        <id column="id" property="id" />
        <result column="name" property="name" />
        <result column="code" property="code" />
        <result column="plan_time" property="planTime" />
        <result column="work_load" property="workLoad" />
        <result column="recipient_id" property="recipientId" />
        <result column="payer" property="payer" />
        <result column="payee" property="payee" />
        <result column="fee" property="fee" />
        <result column="tag_id" property="tagId" />
        <result column="delay_time" property="delayTime" />
        <result column="state" property="state" />
        <result column="publish_id" property="publishId" />
        <result column="content" property="content" />
        <result column="indate" property="indate" />
    </resultMap>

    <resultMap id="BaseResultMapVO" type="com.hssx.pcbms.entity.vo.TaskDTO">
        <id column="id" property="id" />
        <result column="name" property="name" />
        <result column="code" property="code" />
        <result column="plan_time" property="planTime" />
        <result column="work_load" property="workLoad" />
        <result column="recipient_id" property="recipientId" />
        <result column="payer" property="payer" />
        <result column="payee" property="payee" />
        <result column="fee" property="fee" />
        <result column="tag_id" property="tagId" />
        <result column="delay_time" property="delayTime" />
        <result column="state" property="state" />
        <result column="publish_id" property="publishId" />
        <result column="publishName" property="publisherName" />
        <result column="tagname" property="tagName" />
        <result column="recipient" property="recipientName" />
        <result column="pHedaPic" property="publisherHedaPic" />
        <result column="rHedaPic" property="recipientHedaPic" />
        <result column="content" property="content" />
        <result column="indate" property="indate" />
        <collection property="participantsVOS" select="selectPaticaterByTaskId" column="id"
                    ofType="com.hssx.pcbms.entity.vo.TaskParticipantsVO"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, name, code, plan_time, work_load, recipient_id, payer, payee, fee, tag_id, delay_time, state, publish_id,content
    </sql>

    <select id="selectListByCondition" resultMap="BaseResultMapVO">
        select
        t.id, t.name, t.code, t.plan_time, t.work_load, t.recipient_id,t.content,t.indate,
        t.payer, t.payee, t.fee, t.tag_id, t.delay_time, t.state, t.publish_id,
        u.name publishName,tt.name tagname,ru.name recipient,t.content,u.head_url pHedaPic,ru.head_url rHedaPic
        from
        task t
        left join user u
        on t.publish_id = u.id
        left join task_tags tt
        on tt.id = t.tag_id
        left join user ru
        on t.recipient_id = ru.id
        <where>
            t.state !=8
            <if test="taskVO.keyName != null and taskVO.keyName != ''">
                and t.name like concat('%',#{taskVO.keyName},'%')
            </if>
            <if test="taskVO.startTime != null and taskVO.startTime != ''and taskVO.endTime != null and taskVO.endTime != ''">
                and DATE_FORMAT(t.indate,'%Y-%m-%d') between #{taskVO.startTime} and #{taskVO.endTime}
            </if>
            <if test="taskVO.uid != null">
                and t.publish_id =#{taskVO.uid}
            </if>
            <if test="taskVO.tagId != null and taskVO.tagId != ''">
                and t.tag_id = #{taskVO.tagId}
            </if>
            <if test="taskVO.state != null">
                and t.state = #{taskVO.state}
            </if>
        </where>
        order by t.indate desc
    </select>
    <select id="selectListByTaskIds" resultMap="BaseResultMapVO">
        select
        t.id, t.name, t.code, t.plan_time, t.work_load, t.recipient_id,t.content,t.indate,
        t.payer, t.payee, t.fee, t.tag_id, t.delay_time, t.state, t.publish_id,
        u.name publishName,tt.name tagname,ru.name recipient,u.head_url pHedaPic,ru.head_url rHedaPic
        from
        task t
        left join user u
        on t.publish_id = u.id
        left join task_tags tt
        on tt.id = t.tag_id
        left join user ru
        on t.recipient_id = ru.id
        <where>
            <foreach collection="list" separator="," close=")" item="item" open="t.id in (" index="index">
                #{item}
            </foreach>
            and t.state !=8
            <if test="taskVO.keyName != null and taskVO.keyName != ''">
                and t.name like concat('%',#{taskVO.keyName},'%')
            </if>
            <if test="taskVO.startTime != null and taskVO.startTime != ''and taskVO.endTime != null and taskVO.endTime != ''">
                and DATE_FORMAT(t.indate,'%Y-%m-%d') between #{taskVO.startTime} and #{taskVO.endTime}
            </if>
            <if test="taskVO.tagId != null and taskVO.tagId != ''">
                and t.tag_id = #{taskVO.tagId}
            </if>
            <if test="taskVO.state != null">
                and t.state = #{taskVO.state}
            </if>
        </where>
        order by t.indate desc
    </select>
    <resultMap id="PaticaterMapVO" type="com.hssx.pcbms.entity.vo.TaskParticipantsVO">
        <id column="id" property="id" />
        <result column="user_id" property="userId" />
        <result column="task_id" property="taskId" />
        <result column="head_pic" property="headPic"/>
        <result column="name" property="userName"/>
    </resultMap>

    <select id="selectPaticaterByTaskId" resultMap="PaticaterMapVO">
        select tp.id, tp.user_id, tp.task_id,u.name
          from task_participants tp
          left join user u
          on tp.user_id = u.id
          where tp.task_id = #{id}
    </select>

    <select id="getDetailById" resultMap="BaseResultMapVO">
        select
        t.id, t.name, t.code, t.plan_time, t.work_load, t.recipient_id,t.indate,
        t.payer, t.payee, t.fee, t.tag_id, t.delay_time, t.state, t.publish_id,
        u.name publishName,tt.name tagname,ru.name recipient,t.content,u.head_url pHedaPic,ru.head_url rHedaPic
        from
        task t
        left join user u
        on t.publish_id = u.id
        left join task_tags tt
        on tt.id = t.tag_id
        left join user ru
        on t.recipient_id = ru.id
        <where>
            t.id = #{taskVO.id}
        </where>
    </select>

</mapper>
