<?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.ruoyi.kaohe.mapper.KhPingceMapper">
    
    <resultMap type="KhPingce" id="KhPingceResult">
        <result property="id"    column="id"    />
        <result property="pcTitle"    column="pc_title"    />
        <result property="pcDescription"    column="pc_description"    />
        <result property="templateId"    column="template_id"    />
        <result property="templateName"    column="template_name"    />
        <result property="bkhdxType"    column="bkhdx_type"    />
        <result property="pcResultId"    column="pc_result_id"    />
        <result property="state"    column="state"    />
        <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"    />
        <collection property="pcBkhdxs" javaType="ArrayList"
                    ofType="Bkhdx"
                    select="getBkhdxs" column="{id=id}"/>
        <!--<collection  property="pcEmps" ofType="KhEmployee" javaType="list">
            <result property="id"    column="emp_id"    />
            <result property="empName"    column="emp_name"  />
            <result property="deptId"    column="dept_id"  />
            <result property="deptName"    column="dept_name"  />
        </collection >-->
    </resultMap>
    <resultMap id="BkhdxsResult" type="Bkhdx">
        <result property="bkhdxId"    column="bkhdx_id"    />
        <result property="bkhdxName"    column="bkhdx_name"  />
        <result property="deptId"    column="dept_id"  />
        <result property="deptName"    column="dept_name"  />
        <result property="wordId"    column="word_id"  />
        <result property="position"    column="position"  />
    </resultMap>

    <sql id="selectKhPingceVo">
        select id, pc_title, pc_description, template_id, template_name, bkhdx_type, pc_result_id, state, create_by, create_time, update_by, update_time, remark from kh_pingce    </sql>

    <select id="selectKhPingceList" parameterType="KhPingce" resultMap="KhPingceResult">
        <include refid="selectKhPingceVo"></include>
        <where>  
            <if test="pcTitle != null  and pcTitle != ''"> and pc_title like CONCAT('%', #{pcTitle}, '%')</if>
            <if test="pcDescription != null  and pcDescription != ''"> and pc_description = #{pcDescription}</if>
            <if test="templateId != null "> and template_id = #{templateId}</if>
            <if test="templateName != null  and templateName != ''"> and template_name like concat('%', #{templateName}, '%')</if>
            <if test="pcResultId != null "> and pc_result_id = #{pcResultId}</if>
            <if test="state != null  and state != ''"> and state = #{state}</if>
        </where>
        ORDER BY id DESC
    </select>

    <select id="getBkhdxs"  resultMap="BkhdxsResult">
        select pe.bkhdx_id,pe.bkhdx_name,pe.dept_id,pe.dept_name,emp.word_id,emp.`position` from kh_pc_emp pe left join kh_employee emp
        on pe.bkhdx_id = emp.id
        <where>
            <if test="id != null ">  pe.pc_id = #{id}</if>
        </where>
    </select>

    
    <select id="selectKhPingceById" parameterType="Long" resultMap="KhPingceResult">
        <include refid="selectKhPingceVo"></include>
        where id = #{id}
    </select>

    <insert id="insertKhPingce" parameterType="KhPingce" useGeneratedKeys="true" keyProperty="id">
        insert into kh_pingce
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="pcTitle != null and pcTitle != ''">pc_title,</if>
            <if test="pcDescription != null">pc_description,</if>
            <if test="templateId != null">template_id,</if>
            <if test="templateName != null and templateName != ''">template_name,</if>
            <if test="bkhdxType != null and bkhdxType != ''">bkhdx_type,</if>
            <if test="pcResultId != null">pc_result_id,</if>
            <if test="state != null">state,</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="pcTitle != null and pcTitle != ''">#{pcTitle},</if>
            <if test="pcDescription != null">#{pcDescription},</if>
            <if test="templateId != null">#{templateId},</if>
            <if test="templateName != null and templateName != ''">#{templateName},</if>
            <if test="bkhdxType != null and bkhdxType != ''">#{bkhdxType},</if>
            <if test="pcResultId != null">#{pcResultId},</if>
            <if test="state != null">#{state},</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="updateKhPingce" parameterType="KhPingce">
        update kh_pingce
        <trim prefix="SET" suffixOverrides=",">
            <if test="pcTitle != null and pcTitle != ''">pc_title = #{pcTitle},</if>
            <if test="pcDescription != null">pc_description = #{pcDescription},</if>
            <if test="templateId != null">template_id = #{templateId},</if>
            <if test="templateName != null and templateName != ''">template_name = #{templateName},</if>
            <if test="bkhdxType != null and bkhdxType != ''">bkhdx_type = #{bkhdxType},</if>
            <if test="pcResultId != null">pc_result_id = #{pcResultId},</if>
            <if test="state != null">state = #{state},</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="deleteKhPingceById" parameterType="Long">
        delete from kh_pingce where id = #{id}
    </delete>

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