<?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.xunmei.core.edu.mapper.CoreEduTrainingPlanMapper">
    <resultMap type="com.xunmei.common.core.domain.edu.domain.CoreEduTrainingPlan" id="CoreEduTrainingPlanResult">
        <result property="id" column="id"/>
        <result property="planName" column="plan_name"/>
        <result property="planCycle" column="plan_cycle"/>
        <result property="execTimes" column="exec_times"/>
        <result property="planStatus" column="plan_status"/>
        <result property="execOrgType" column="exec_org_type"/>
        <result property="startDate" column="start_date"/>
        <result property="endDate" column="end_date"/>
        <result property="buildTaskNow" column="build_task_now"/>
        <result property="createOrgId" column="create_org_id"/>
        <result property="createOrgName" column="create_org_name"/>
        <result property="createOrgPath" column="create_org_path"/>
        <result property="belongOrgId" column="belong_org_id"/>
        <result property="belongOrgName" column="belong_org_name"/>
        <result property="belongOrgPath" column="belong_org_path"/>
        <result property="standard" column="standard"/>
        <result property="issue" column="issue"/>
        <result property="parentId" column="parent_id"/>
        <result property="deleted" column="deleted"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <resultMap type="com.xunmei.common.core.domain.edu.vo.CoreEduTrainingPlanDataVo"
               id="CoreEduTrainingPlanDataVoResult">
        <result property="id" column="id"/>
        <result property="planName" column="plan_name"/>
        <result property="planCycle" column="plan_cycle"/>
        <result property="execTimes" column="exec_times"/>
        <result property="planStatus" column="plan_status"/>
        <result property="execOrgType" column="exec_org_type"/>
        <result property="startDate" column="start_date"/>
        <result property="endDate" column="end_date"/>
        <result property="buildTaskNow" column="build_task_now"/>
        <result property="createOrgId" column="create_org_id"/>
        <result property="createOrgName" column="create_org_name"/>
        <result property="createOrgPath" column="create_org_path"/>
        <result property="belongOrgId" column="belong_org_id"/>
        <result property="belongOrgName" column="belong_org_name"/>
        <result property="belongOrgPath" column="belong_org_path"/>
        <result property="standard" column="standard"/>
        <result property="issue" column="issue"/>
        <result property="parentId" column="parent_id"/>
        <collection property="planRoleList" ofType="com.xunmei.common.core.domain.edu.domain.CoreEduTrainingPlanToRole"
                    select="com.xunmei.core.edu.mapper.CoreEduTrainingPlanToRoleMapper.selectListByPlanId" column="id">
            <result column="plan_id" property="planId"/>
            <result column="role_id" property="roleId"/>
        </collection>
        <collection property="planExecOrgList"
                    ofType="com.xunmei.common.core.domain.edu.domain.CoreEduTrainingPlanToExecOrg"
                    select="com.xunmei.core.edu.mapper.CoreEduTrainingPlanToExecOrgMapper.selectLisByPlanId"
                    column="id">
            <result column="plan_id" property="planId"/>
            <result column="org_id" property="orgId"/>
        </collection>
    </resultMap>

    <sql id="selectCoreEduTrainingPlanVo">
        select id,
               plan_name,
               plan_cycle,
               exec_times,
               plan_status,
               exec_org_type,
               start_date,
               end_date,
               build_task_now,
               remark,
               create_org_id,
               create_org_name,
               create_org_path,
               belong_org_id,
               belong_org_name,
               belong_org_path,
               standard,
               issue,
               parent_id,
               deleted,
               create_by,
               create_time,
               update_by,
               update_time
        from core_edu_training_plan
    </sql>

    <select id="selectCoreEduTrainingPlanList"
            parameterType="com.xunmei.common.core.domain.edu.domain.CoreEduTrainingPlan"
            resultMap="CoreEduTrainingPlanResult">
        <include refid="selectCoreEduTrainingPlanVo"/>
        <where>
            <if test="planName != null  and planName != ''">
                and plan_name like concat('%', #{planName}, '%')
            </if>
            <if test="planCycle != null ">
                and plan_cycle = #{planCycle}
            </if>
            <if test="planStatus != null ">
                and plan_status = #{planStatus}
            </if>
            <if test="execOrgType != null ">
                and exec_org_type = #{execOrgType}
            </if>
            <if test="createOrgId != null ">
                and create_org_id = #{createOrgId}
            </if>
            <if test="createOrgName != null  and createOrgName != ''">
                and create_org_name like concat('%', #{createOrgName}, '%')
            </if>
        </where>
    </select>

    <select id="selectCoreEduTrainingPlanById" parameterType="Long"
            resultMap="CoreEduTrainingPlanResult">
        <include refid="selectCoreEduTrainingPlanVo"/>
        where id = #{id}
    </select>

    <insert id="insertCoreEduTrainingPlan" parameterType="com.xunmei.common.core.domain.edu.domain.CoreEduTrainingPlan"
            useGeneratedKeys="true" keyProperty="id">
        insert into core_edu_training_plan
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="planName != null">
                plan_name,
            </if>
            <if test="planCycle != null">
                plan_cycle,
            </if>
            <if test="execTimes != null">
                exec_times,
            </if>
            <if test="planStatus != null">
                plan_status,
            </if>
            <if test="execOrgType != null">
                exec_org_type,
            </if>
            <if test="startDate != null">
                start_date,
            </if>
            <if test="endDate != null">
                end_date,
            </if>
            <if test="buildTaskNow != null">
                build_task_now,
            </if>
            <if test="remark != null">
                remark,
            </if>
            <if test="createOrgId != null">
                create_org_id,
            </if>
            <if test="createOrgName != null">
                create_org_name,
            </if>
            <if test="createOrgPath != null">
                create_org_path,
            </if>
            <if test="belongOrgId != null">
                belong_org_id,
            </if>
            <if test="belongOrgName != null">
                belong_org_name,
            </if>
            <if test="belongOrgPath != null">
                belong_org_path,
            </if>
            <if test="standard != null">
                standard,
            </if>
            <if test="issue != null">
                issue,
            </if>
            <if test="parentId != null">
                parent_id,
            </if>
            <if test="deleted != null">
                deleted,
            </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>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="planName != null">
                #{planName},
            </if>
            <if test="planCycle != null">
                #{planCycle},
            </if>
            <if test="execTimes != null">
                #{execTimes},
            </if>
            <if test="planStatus != null">
                #{planStatus},
            </if>
            <if test="execOrgType != null">
                #{execOrgType},
            </if>
            <if test="startDate != null">
                #{startDate},
            </if>
            <if test="endDate != null">
                #{endDate},
            </if>
            <if test="buildTaskNow != null">
                #{buildTaskNow},
            </if>
            <if test="remark != null">
                #{remark},
            </if>
            <if test="createOrgId != null">
                #{createOrgId},
            </if>
            <if test="createOrgName != null">
                #{createOrgName},
            </if>
            <if test="createOrgPath != null">
                #{createOrgPath},
            </if>
            <if test="belongOrgId != null">
                #{belongOrgId},
            </if>
            <if test="belongOrgName != null">
                #{belongOrgName},
            </if>
            <if test="belongOrgPath != null">
                #{belongOrgPath},
            </if>
            <if test="standard != null">
                #{standard},
            </if>
            <if test="issue != null">
                #{issue},
            </if>
            <if test="parentId != null">
                #{parentId},
            </if>
            <if test="deleted != null">
                #{deleted},
            </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>
        </trim>
    </insert>

    <update id="updateCoreEduTrainingPlan" parameterType="com.xunmei.common.core.domain.edu.domain.CoreEduTrainingPlan">
        update core_edu_training_plan
        <trim prefix="SET" suffixOverrides=",">
            <if test="planName != null">
                plan_name =
                #{planName},
            </if>
            <if test="planCycle != null">
                plan_cycle =
                #{planCycle},
            </if>
            <if test="execTimes != null">
                exec_times =
                #{execTimes},
            </if>
            <if test="planStatus != null">
                plan_status =
                #{planStatus},
            </if>
            <if test="execOrgType != null">
                exec_org_type =
                #{execOrgType},
            </if>
            <if test="startDate != null">
                start_date =
                #{startDate},
            </if>
            <if test="endDate != null">
                end_date =
                #{endDate},
            </if>
            <if test="buildTaskNow != null">
                build_task_now =
                #{buildTaskNow},
            </if>
            <if test="remark != null">
                remark =
                #{remark},
            </if>
            <if test="createOrgId != null">
                create_org_id =
                #{createOrgId},
            </if>
            <if test="createOrgName != null">
                create_org_name =
                #{createOrgName},
            </if>
            <if test="createOrgPath != null">
                create_org_path =
                #{createOrgPath},
            </if>
            <if test="belongOrgId != null">
                belong_org_id =
                #{belongOrgId},
            </if>
            <if test="belongOrgName != null">
                belong_org_name =
                #{belongOrgName},
            </if>
            <if test="belongOrgPath != null">
                belong_org_path =
                #{belongOrgPath},
            </if>
            <if test="standard != null">
                standard =
                #{standard},
            </if>
            <if test="issue != null">
                issue =
                #{issue},
            </if>
            <if test="parentId != null">
                parent_id =
                #{parentId},
            </if>
            <if test="deleted != null">
                deleted =
                #{deleted},
            </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>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCoreEduTrainingPlanById" parameterType="Long">
        delete
        from core_edu_training_plan
        where id = #{id}
    </delete>

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

    <select id="selectPageData" resultType="com.xunmei.common.core.domain.edu.vo.CoreEduTrainingPlanPageVo">
        select p.id as id,
        p.plan_name,
        p.create_org_id,
        p.create_org_name,
        p.exec_org_type,
        p.plan_cycle,
        p.exec_times,
        p.remark,
        u.name as updateBy,
        p.plan_status,
        p.issue,
        p.standard,
        P.belong_org_id as belongOrgId,
        P.belong_org_name as belongOrgName
        from core_edu_training_plan p
        left join sys_user u on p.update_by = u.id
        <if test="request.planRoleId!=null">
            left join (select id from core_edu_training_plan p1 inner join core_edu_training_plan_to_role r1 on p1.id =
            r1.plan_id where r1.role_id = #{request.planRoleId} and p1.deleted=0) t on p.id = t.id and p.deleted=0
        </if>
        where p.deleted = 0

        <if test="request.execOrgType!=null">
            and p.exec_org_type = #{request.execOrgType}
        </if>
        <if test="request.planCycle!=null">
            and p.plan_cycle = #{request.planCycle}
        </if>
        <if test="request.planStatus!=null">
            and p.plan_status = #{request.planStatus}
        </if>

        <if test="request.planName!=null">
            and p.plan_name = #{request.planName}
        </if>
        <choose>
            <when test="request.checkSub==true">
                and p.belong_org_path like concat(#{request.belongOrgPath},'%')
            </when>
            <otherwise>
                and p.belong_org_id = #{request.belongOrgId}
            </otherwise>
        </choose>
    </select>

    <select id="selectPlanRole" resultType="com.xunmei.common.core.domain.edu.vo.CoreEduTrainingPlanRoleVo">
        SELECT distinct r.role_id    as roleId,
                        sr.role_name as roleName
        FROM core_edu_training_plan p
                 INNER JOIN core_edu_training_plan_to_role r
                 INNER JOIN sys_role sr on r.role_id = sr.id
        where p.belong_org_id = #{orgId}
    </select>

    <select id="getDetailPlanData"
            resultMap="com.xunmei.core.edu.mapper.CoreEduTrainingPlanMapper.CoreEduTrainingPlanDataVoResult">
        select *
        from core_edu_training_plan p
        where p.deleted = 0
          and p.id = #{id}
    </select>
</mapper>