<?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.zehong.system.mapper.TWorkTaskMapper">
    
    <resultMap type="TWorkTask" id="TWorkTaskResult">
        <result property="taskId"    column="task_id"    />
        <result property="taskType"    column="task_type"    />
        <result property="taskTitle"    column="task_title"    />
        <result property="taskContent"    column="task_content"    />
        <result property="status"    column="status"    />
        <result property="createEnterpriseId"    column="create_enterprise_id"    />
        <result property="createBy"    column="create_by"    />
        <result property="groupNum"    column="group_num"    />
        <result property="createTime"    column="create_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="isReport"    column="is_report"    />
        <result property="isDel"    column="is_del"    />
        <result property="approvalSuggestion"    column="approval_suggestion"    />
        <collection property="taskMemberList"  ofType="TWorkTaskMember" column="task_id" select="getTaskMember"/>
    </resultMap>
    <resultMap type="TWorkTask" id="TWorkTaskResultTwo">
        <result property="taskId"    column="task_id"    />
        <result property="taskType"    column="task_type"    />
        <result property="taskTitle"    column="task_title"    />
        <result property="taskContent"    column="task_content"    />
        <result property="status"    column="status"    />
        <result property="createEnterpriseId"    column="create_enterprise_id"    />
        <result property="createBy"    column="create_by"    />
        <result property="groupNum"    column="group_num"    />
        <result property="createTime"    column="create_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="isReport"    column="is_report"    />
        <result property="isDel"    column="is_del"    />
        <result property="memberId" column="memberId"/>
        <result property="approvalSuggestion"    column="approval_suggestion"    />
    </resultMap>
    <resultMap id="taskMember" type="TWorkTaskMember">
        <id column="id" property="id"></id>
        <result column="member_id" property="memberId"></result>
        <result column="member_name" property="memberName"></result>
    </resultMap>

    <sql id="selectTWorkTaskVo">
        SELECT
            task_id,
            task_type,
            task_title,
            task_content,
            STATUS,
            create_enterprise_id,
            create_by,
            create_time,
            end_time,
            is_report,
            is_del,
            group_num,
            approval_suggestion
        FROM
            t_work_task
    </sql>

    <select id="getTaskMember" parameterType="java.lang.Long" resultMap="taskMember">
        select
        mb.id,
        mb.member_id,
        person.person_name as member_name
        from
        t_work_task_member mb
        LEFT JOIN t_line_patrol_person person ON person.person_id = mb.member_id
        where mb.task_id = #{taskId}
    </select>

    <select id="selectTWorkTaskList" parameterType="TWorkTask" resultMap="TWorkTaskResult">
        <include refid="selectTWorkTaskVo"/>
        <where>  
            <if test="taskType != null "> and task_type = #{taskType}</if>
            <if test="taskTitle != null  and taskTitle != ''"> and task_title like concat('%', #{taskTitle}, '%')</if>
            <if test="taskContent != null  and taskContent != ''"> and task_content = #{taskContent}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="createEnterpriseId != null "> and create_enterprise_id = #{createEnterpriseId}</if>
            <if test="endTime != null "> and end_time = #{endTime}</if>
            <if test="groupNum"> and group_num =#{groupNum}</if>
            <if test="isReport != null "> and is_report = #{isReport}</if>
            <if test="approvalSuggestion != null  and approvalSuggestion != ''"> and approval_suggestion = #{approvalSuggestion}</if>
            <if test="isDel != null "> and is_del = #{isDel}</if>
        </where>
        order by create_time desc
    </select>
    
    <select id="selectTWorkTaskById" parameterType="Long" resultMap="TWorkTaskResult">
        <include refid="selectTWorkTaskVo"/>
        where task_id = #{taskId}
    </select>
        
    <insert id="insertTWorkTask" parameterType="TWorkTask" useGeneratedKeys="true" keyProperty="taskId">
        insert into t_work_task
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskType != null">task_type,</if>
            <if test="taskTitle != null">task_title,</if>
            <if test="taskContent != null">task_content,</if>
            <if test="status != null">status,</if>
            <if test="createEnterpriseId != null">create_enterprise_id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="groupNum != null">group_num,</if>
            <if test="createTime != null">create_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="isReport != null">is_report,</if>
            <if test="approvalSuggestion != null">approval_suggestion,</if>
            <if test="isDel != null">is_del,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskType != null">#{taskType},</if>
            <if test="taskTitle != null">#{taskTitle},</if>
            <if test="taskContent != null">#{taskContent},</if>
            <if test="status != null">#{status},</if>
            <if test="createEnterpriseId != null">#{createEnterpriseId},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="groupNum !=null">#{groupNum},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="isReport != null">#{isReport},</if>
            <if test="approvalSuggestion != null">#{approvalSuggestion},</if>
            <if test="isDel != null">#{isDel},</if>
         </trim>
    </insert>

    <update id="updateTWorkTask" parameterType="TWorkTask">
        update t_work_task
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskType != null">task_type = #{taskType},</if>
            <if test="taskTitle != null">task_title = #{taskTitle},</if>
            <if test="taskContent != null">task_content = #{taskContent},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createEnterpriseId != null">create_enterprise_id = #{createEnterpriseId},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="groupNum !=null">group_num =#{groupNum},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="isReport != null">is_report = #{isReport},</if>
            <if test="approvalSuggestion != null">approval_suggestion = #{approvalSuggestion},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
        </trim>
        where task_id = #{taskId}
    </update>
    <select id="selectTaskListxiao" parameterType="TWorkTask" resultMap="TWorkTaskResultTwo">
        SELECT t.*,m.member_id as memberId,IFNULL(m.progress,0) AS progress,m.id as taskMemberId  FROM t_work_task t
        LEFT JOIN t_work_task_member m on m.task_id = t.task_id and m.member_id =#{memberId}
        <where>
            <if test="taskType != null "> and t.task_type = #{taskType}</if>
            <if test="taskTitle != null  and taskTitle != ''"> and t.task_title like concat('%', #{taskTitle}, '%')</if>
            <if test="taskContent != null  and taskContent != ''"> and t.task_content = #{taskContent}</if>
            <if test="status != null ">
              <if test="status !=0 and status !=3">and t.status = #{status}</if>
                <if test="status ==0">and (t.status = 0 or t.status =3)</if>
            </if>
            <if test="createEnterpriseId != null "> and t.create_enterprise_id = #{createEnterpriseId}</if>
            <if test="endTime != null "> and t.end_time = #{endTime}</if>
            <if test="groupNum !=null and groupNum!='-1'"> and t.group_num =#{groupNum}</if>
            <if test="isReport != null "> and t.is_report = #{isReport}</if>
            <if test="isDel != null "> and t.is_del = #{isDel}</if>
            <if test="memberId !=null and userType==3 and taskType==1">
               and m.member_id = #{memberId}
            </if>
        </where>
         order by  progress desc, t.create_time desc
    </select>
    <delete id="deleteTWorkTaskById" parameterType="Long">
        delete from t_work_task where task_id = #{taskId}
    </delete>

    <delete id="deleteTWorkTaskByIds" parameterType="String">
        delete from t_work_task where task_id in 
        <foreach item="taskId" collection="array" open="(" separator="," close=")">
            #{taskId}
        </foreach>
    </delete>

    <select id="selectPersonName" parameterType="Long" resultType="java.util.HashMap">
        SELECT GROUP_CONCAT(person_name) AS personName,
                GROUP_CONCAT(person_id) AS personId
        FROM t_line_patrol_person
        WHERE person_id IN (SELECT member_id FROM t_work_task_member WHERE task_id = #{taskId})
    </select>
</mapper>