<?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.yanzhu.jh.work.mapper.WorkTrainMapper">
    
    <resultMap type="WorkTrain" id="WorkTrainResult">
        <result property="id"    column="id"    />
        <result property="projectId"    column="project_id"    />
        <result property="projectName"    column="project_name"    />
        <result property="mainImage"    column="main_image"    />
        <result property="trainType"    column="train_type"    />
        <result property="trainTitle"    column="train_title"    />
        <result property="trainNature"    column="train_nature"    />
        <result property="trainParticipants"    column="train_participants"    />
        <result property="beginDate"    column="begin_date"    />
        <result property="endDate"    column="end_date"    />
        <result property="trainContent"    column="train_content"    />
        <result property="trainFile"    column="train_file"    />
        <result property="isDel"    column="is_del"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="vDel"    column="v_del"    />
        <result property="trainNatureName"    column="train_nature_name"    />
    </resultMap>

    <resultMap id="WorkTrainWorkTrainDeptResult" type="WorkTrain" extends="WorkTrainResult">
        <collection property="workTrainDeptList" notNullColumn="sub_train_id" javaType="java.util.List" resultMap="WorkTrainDeptResult" />
    </resultMap>

    <resultMap type="WorkTrainDept" id="WorkTrainDeptResult">
        <result property="trainId"    column="sub_train_id"    />
        <result property="deptId"    column="sub_dept_id"    />
        <result property="deptName"    column="sub_dept_name"    />
        <result property="isMain"    column="sub_is_main"    />
    </resultMap>

    <sql id="selectWorkTrainVo">
        select id, project_id, project_name, main_image, train_type, train_title,train_nature, train_participants, begin_date, end_date, train_content, train_file, is_del, create_by, create_time, update_by, update_time, remark from work_train
    </sql>

    <select id="selectWorkTrainList" parameterType="WorkTrain" resultMap="WorkTrainResult">
        select wt.id, wt.project_id, wt.project_name, wt.main_image, wt.train_type, wt.train_title, wt.train_nature, wt.train_participants, wt.begin_date, wt.end_date, wt.train_content, wt.train_file, wt.is_del, wt.create_by, wt.create_time, wt.update_by, wt.update_time, wt.remark, group_concat(sd.dept_id) as trainDeptIds, group_concat(sd.dept_name) as trainDeptNames
        from work_train wt
        left join work_train_dept wtd on wtd.train_id = wt.id
        left join sys_dept sd on sd.dept_id = wtd.dept_id
        left join sur_project sp on sp.id = wt.project_id
        <!--监理单位/总包公司/分包单位-->
        <if test='nowRole == "5" or nowRole == "6" or nowRole == "7"'> left join sur_project_unit_info spui on spui.projectId = sp.id</if>
        <!--普通用户查询项目人员-->
        <if test='nowRole == "15" or nowRole == "16" or nowRole == "17" or nowRole == "99"'> left join sur_project_userinfo spu on spu.project_id = sp.id</if>
        <if test="deptId != null"> left join work_train_dept wtd2 on wtd2.train_id = wt.id and wtd2.is_main ='Y'</if>
        <where>
            <if test="projectId != null"> and wt.project_id = #{projectId}</if>
            <if test="deptId != null"> and wtd2.dept_id = #{deptId}</if>
            <if test="projectName != null  and projectName != ''"> and wt.project_name like concat('%', #{projectName}, '%')</if>
            <if test="trainType != null  and trainType != ''"> and train_type = #{trainType}</if>
            <if test="trainTitle != null  and trainTitle != ''"> and train_title like concat('%', #{trainTitle}, '%')</if>
            <if test="trainNature != null  and trainNature != ''"> and wt.train_nature = #{trainNature}</if>
            <if test="isDel != null "> and wt.is_del = #{isDel}</if>
            <if test="params.beginMarksTime != null and params.beginMarksTime != '' and params.endMarksTime != null and params.endMarksTime != ''"> and wt.begin_date between #{params.beginMarksTime} and #{params.endMarksTime}</if>
            <!-- 查询条件-项目部门 -->
            <if test="projectDeptId != null "> and sp.deptId = #{projectDeptId}</if>
            <!--子部门数据-->
            <if test='nowRole == "4"'> and sp.deptId = #{nowDept}</if>
            <!--监理单位/总包公司/分包单位查询当前关联数据-->
            <if test='nowRole == "5" or nowRole == "6" or nowRole == "7"'> and spui.unitId = #{nowDept}</if>
            <!--普通用户查询项目人员-->
            <if test='nowRole == "15" or nowRole == "16" or nowRole == "17" or nowRole == "99"'> and spu.user_id = #{nowUser} and spu.is_del=0</if>
        </where>
        group by wt.id
        order by wt.create_time desc
    </select>

    <select id="selectBgscreenWorkTrainList" parameterType="WorkTrain" resultMap="WorkTrainResult">
        select wt.id, wt.project_id, wt.project_name, wt.main_image, wt.train_type, wt.train_title, wt.train_nature, sdd.dict_label as train_nature_name, wt.train_participants, wt.begin_date, wt.end_date, wt.train_content, wt.train_file, wt.is_del, wt.create_by, wt.create_time, wt.update_by, wt.update_time, wt.remark, group_concat(sd.dept_id) as trainDeptIds, group_concat(sd.dept_name) as trainDeptNames
        from work_train wt
        left join work_train_dept wtd on wtd.train_id = wt.id
        left join sys_dept sd on sd.dept_id = wtd.dept_id
        <if test='trainType != null  and trainType == "0"'> left join sys_dict_data sdd on sdd.dict_type = 'train_nature_zxpx' and wt.train_nature = sdd.dict_value</if>
        <if test='trainType != null  and trainType == "1"'> left join sys_dict_data sdd on sdd.dict_type = 'train_nature_yjyl' and wt.train_nature = sdd.dict_value</if>
            RIGHT JOIN (
                select gr.train_nature as gr_type ,max(gr.id) as maxid from work_train gr
                left join sur_project sp on sp.id = gr.project_id
                <where>
                    and gr.is_del=0
                    <if test="deptId != null"> and sp.deptId = #{deptId}</if>
                    <if test="trainType != null  and trainType != ''"> and gr.train_type = #{trainType}</if>
                </where>
        group by gr.train_nature) as group_train on group_train.maxid = wt.id
        where wt.is_del=0
        group by wt.id
        order by wt.create_time desc
    </select>

    <select id="selectBgscreenWorkTrainListv2" parameterType="WorkTrain" resultMap="WorkTrainResult">
        select wt.id, wt.project_id, wt.project_name, wt.main_image, wt.train_type, wt.train_title, wt.train_nature, sdd.dict_label as train_nature_name, wt.train_participants, wt.begin_date, wt.end_date, wt.train_content, wt.train_file, wt.is_del, wt.create_by, wt.create_time, wt.update_by, wt.update_time, wt.remark, group_concat(sd.dept_id) as trainDeptIds, group_concat(sd.dept_name) as trainDeptNames
        from work_train wt
        left join work_train_dept wtd on wtd.train_id = wt.id
        left join sys_dept sd on sd.dept_id = wtd.dept_id
        left join sur_project sp on sp.id = wt.project_id
        <if test='trainType != null  and trainType == "0"'> left join sys_dict_data sdd on sdd.dict_type = 'train_nature_zxpx' and wt.train_nature = sdd.dict_value</if>
        <if test='trainType != null  and trainType == "1"'> left join sys_dict_data sdd on sdd.dict_type = 'train_nature_yjyl' and wt.train_nature = sdd.dict_value</if>
        <where>
            and wt.is_del=0
            <if test="nowDept != null and nowDept != ''"> and sp.deptId = #{nowDept}</if>
            <if test="trainType != null  and trainType != ''"> and wt.train_type = #{trainType}</if>
            <if test="projectId != null"> and wt.project_id = #{projectId}</if>
            <if test="prjIds !=null and prjIds.size()>0">
                and wt.project_id in
                <foreach collection="prjIds"  item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
        group by wt.id
        order by wt.create_time desc
        limit 20
    </select>
    
    <select id="selectWorkTrainById" parameterType="Long" resultMap="WorkTrainWorkTrainDeptResult">
        select a.id, a.project_id, a.project_name, a.main_image, a.train_type, a.train_title, a.train_nature, a.train_participants, a.begin_date, a.end_date, a.train_content, a.train_file, a.is_del, a.create_by, a.create_time, a.update_by, a.update_time, a.remark,
        b.train_id as sub_train_id, b.dept_id as sub_dept_id, b.is_main as sub_is_main
        from work_train a
        left join work_train_dept b on b.train_id = a.id and b.is_main ='N'
        where a.id = #{id}
    </select>

    <select id="selectById" parameterType="Long" resultMap="WorkTrainWorkTrainDeptResult">
        select a.id, a.project_id, a.project_name, a.main_image, a.train_type, a.train_title, a.train_nature, a.train_participants, a.begin_date, a.end_date, a.train_content, a.train_file, a.is_del, a.create_by, a.create_time, a.update_by, a.update_time, a.remark,
        b.train_id as sub_train_id, b.dept_id as sub_dept_id, b.is_main as sub_is_main, d.dept_name as sub_dept_name
        from work_train a
        left join work_train_dept b on b.train_id = a.id
        left join sys_dept d on b.dept_id = d.dept_id
        where a.id = #{id}
    </select>

    <select id="findGroupCountByProjectId" parameterType="Long" resultType="map">
        SELECT
            wt.train_type as type,
            sdd.dict_label as typeName,
            count(1) AS total
        FROM
            work_train wt
            LEFT JOIN sys_dict_data sdd ON sdd.dict_type = 'project_train_type'
            AND sdd.dict_value = wt.train_type
        WHERE
            wt.is_del=0
            AND wt.project_id = #{peojectId}
        GROUP BY
            wt.train_type
    </select>
        
    <insert id="insertWorkTrain" parameterType="WorkTrain" useGeneratedKeys="true" keyProperty="id">
        insert into work_train
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="projectId != null">project_id,</if>
            <if test="projectName != null">project_name,</if>
            <if test="mainImage != null">main_image,</if>
            <if test="trainType != null">train_type,</if>
            <if test="trainTitle != null">train_title,</if>
            <if test="trainNature != null">train_nature,</if>
            <if test="trainParticipants != null">train_participants,</if>
            <if test="beginDate != null">begin_date,</if>
            <if test="endDate != null">end_date,</if>
            <if test="trainContent != null">train_content,</if>
            <if test="trainFile != null">train_file,</if>
            <if test="isDel != null">is_del,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="projectId != null">#{projectId},</if>
            <if test="projectName != null">#{projectName},</if>
            <if test="mainImage != null">#{mainImage},</if>
            <if test="trainType != null">#{trainType},</if>
            <if test="trainTitle != null">#{trainTitle},</if>
            <if test="trainNature != null">#{trainNature},</if>
            <if test="trainParticipants != null">#{trainParticipants},</if>
            <if test="beginDate != null">#{beginDate},</if>
            <if test="endDate != null">#{endDate},</if>
            <if test="trainContent != null">#{trainContent},</if>
            <if test="trainFile != null">#{trainFile},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateWorkTrain" parameterType="WorkTrain">
        update work_train
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="projectName != null">project_name = #{projectName},</if>
            <if test="mainImage != null">main_image = #{mainImage},</if>
            <if test="trainType != null">train_type = #{trainType},</if>
            <if test="trainTitle != null">train_title = #{trainTitle},</if>
            <if test="trainNature != null">train_nature = #{trainNature},</if>
            <if test="trainParticipants != null">train_participants = #{trainParticipants},</if>
            <if test="beginDate != null">begin_date = #{beginDate},</if>
            <if test="endDate != null">end_date = #{endDate},</if>
            <if test="trainContent != null">train_content = #{trainContent},</if>
            <if test="trainFile != null">train_file = #{trainFile},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteWorkTrainById" parameterType="Long">
        delete from work_train where id = #{id}
    </delete>

    <delete id="deleteWorkTrainByIds" parameterType="String">
        delete from work_train where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    
    <delete id="deleteWorkTrainDeptByTrainIds" parameterType="String">
        delete from work_train_dept where train_id in 
        <foreach item="trainId" collection="array" open="(" separator="," close=")">
            #{trainId}
        </foreach>
    </delete>

    <delete id="deleteWorkTrainDeptByTrainId" parameterType="Long">
        delete from work_train_dept where train_id = #{trainId}
    </delete>

    <insert id="batchWorkTrainDept">
        insert into work_train_dept( train_id, dept_id, is_main) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.trainId}, #{item.deptId}, #{item.isMain})
        </foreach>
    </insert>
</mapper>