<?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.project.mapper.ProProjectApplyMapper">
    
    <resultMap type="ProProjectApply" id="ProProjectApplyResult">
        <result property="id"    column="id"    />
        <result property="deptId"    column="dept_id"    />
        <result property="projId"    column="proj_id"    />
        <result property="projName"    column="proj_name"    />
        <result property="parProjName"    column="par_proj_name"    />
        <result property="applyType"    column="apply_type"    />
        <result property="applyStatus"    column="apply_status"    />
        <result property="applyReason"    column="apply_reason"    />
        <result property="applyFiles"    column="apply_files"    />
        <result property="applyUser"    column="apply_user"    />
        <result property="useTime"    column="use_time"    />
        <result property="isDel"    column="is_del"    />
        <result property="createByDeptName"    column="createByDeptName"    />
        <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"    />
    </resultMap>

    <resultMap id="ProProjectApplyProProjectApplyDetailResult" type="ProProjectApply" extends="ProProjectApplyResult">
        <collection property="proProjectApplyDetailList" notNullColumn="sub_id" javaType="java.util.List" resultMap="ProProjectApplyDetailResult" />
    </resultMap>

    <resultMap type="ProProjectApplyDetail" id="ProProjectApplyDetailResult">
        <result property="id"    column="sub_id"    />
        <result property="applyId"    column="sub_apply_id"    />
        <result property="superTypeKey"    column="sub_super_type_key"    />
        <result property="superTypeName"    column="sub_super_type_name"    />
        <result property="typeId"    column="sub_type_id"    />
        <result property="typeName"    column="sub_type_name"    />
        <result property="assetsId"    column="sub_assets_id"    />
        <result property="assetsName"    column="sub_assets_name"    />
        <result property="assetsUnit"    column="sub_assets_unit"    />
        <result property="number"    column="sub_number"    />
        <result property="useTime"    column="sub_use_time"    />
        <result property="useReason"    column="sub_use_reason"    />
        <result property="price"    column="sub_price"    />
        <result property="totalPrice"    column="sub_total_price"    />
        <result property="isDel"    column="sub_is_del"    />
        <result property="createBy"    column="sub_create_by"    />
        <result property="createTime"    column="sub_create_time"    />
        <result property="updateBy"    column="sub_update_by"    />
        <result property="updateTime"    column="sub_update_time"    />
        <result property="remark"    column="sub_remark"    />
    </resultMap>

    <sql id="selectProProjectApplyVo">
        select id, dept_id, proj_id, proj_name, par_proj_name, apply_type, apply_status, apply_reason, apply_files, apply_user, use_time, is_del, create_by, create_time, update_by, update_time, remark from pro_project_apply
    </sql>

    <select id="selectProProjectApplyList" parameterType="ProProjectApply" resultMap="ProProjectApplyResult">
        <include refid="selectProProjectApplyVo"/>
        <where>  
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="projId != null "> and proj_id = #{projId}</if>
            <if test="projName != null  and projName != ''"> and proj_name like concat('%', #{projName}, '%')</if>
            <if test="parProjName != null  and parProjName != ''"> and par_proj_name like concat('%', #{parProjName}, '%')</if>
            <if test="applyType != null  and applyType != ''"> and apply_type = #{applyType}</if>
            <if test="applyStatus != null  and applyStatus != ''"> and apply_status = #{applyStatus}</if>
            <if test="applyUser != null "> and apply_user = #{applyUser}</if>
            <if test="useTime != null "> and use_time = #{useTime}</if>
            <if test="isDel != null  and isDel != ''"> and is_del = #{isDel}</if>
        </where>
    </select>
    
    <select id="selectProProjectApplyById" parameterType="Long" resultMap="ProProjectApplyProProjectApplyDetailResult">
        select a.id, a.dept_id, a.proj_id, a.proj_name, a.par_proj_name, a.apply_type, a.apply_status, a.apply_reason, a.apply_files, a.apply_user, a.use_time, a.is_del, sd.dept_name as createByDeptName, a.create_by, a.create_time, a.update_by, a.update_time, a.remark,
 b.id as sub_id, b.apply_id as sub_apply_id, b.super_type_key as sub_super_type_key, b.super_type_name as sub_super_type_name, b.type_id as sub_type_id, b.type_name as sub_type_name, b.assets_id as sub_assets_id, b.assets_name as sub_assets_name, b.assets_unit as sub_assets_unit, b.number as sub_number, b.use_time as sub_use_time, b.use_reason as sub_use_reason, b.price as sub_price, b.total_price as sub_total_price, b.is_del as sub_is_del, b.create_by as sub_create_by, b.create_time as sub_create_time, b.update_by as sub_update_by, b.update_time as sub_update_time, b.remark as sub_remark
        from pro_project_apply a
        left join sys_dept sd on a.dept_id = sd.dept_id
        left join pro_project_apply_detail b on b.apply_id = a.id
        where a.id = #{id}
    </select>
        
    <insert id="insertProProjectApply" parameterType="ProProjectApply" useGeneratedKeys="true" keyProperty="id">
        insert into pro_project_apply
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deptId != null">dept_id,</if>
            <if test="projId != null">proj_id,</if>
            <if test="projName != null">proj_name,</if>
            <if test="parProjName != null">par_proj_name,</if>
            <if test="applyType != null">apply_type,</if>
            <if test="applyStatus != null">apply_status,</if>
            <if test="applyReason != null">apply_reason,</if>
            <if test="applyFiles != null">apply_files,</if>
            <if test="applyUser != null">apply_user,</if>
            <if test="useTime != null">use_time,</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="deptId != null">#{deptId},</if>
            <if test="projId != null">#{projId},</if>
            <if test="projName != null">#{projName},</if>
            <if test="parProjName != null">#{parProjName},</if>
            <if test="applyType != null">#{applyType},</if>
            <if test="applyStatus != null">#{applyStatus},</if>
            <if test="applyReason != null">#{applyReason},</if>
            <if test="applyFiles != null">#{applyFiles},</if>
            <if test="applyUser != null">#{applyUser},</if>
            <if test="useTime != null">#{useTime},</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="updateProProjectApply" parameterType="ProProjectApply">
        update pro_project_apply
        <trim prefix="SET" suffixOverrides=",">
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="projId != null">proj_id = #{projId},</if>
            <if test="projName != null">proj_name = #{projName},</if>
            <if test="parProjName != null">par_proj_name = #{parProjName},</if>
            <if test="applyType != null">apply_type = #{applyType},</if>
            <if test="applyStatus != null">apply_status = #{applyStatus},</if>
            <if test="applyReason != null">apply_reason = #{applyReason},</if>
            <if test="applyFiles != null">apply_files = #{applyFiles},</if>
            <if test="applyUser != null">apply_user = #{applyUser},</if>
            <if test="useTime != null">use_time = #{useTime},</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="deleteProProjectApplyById" parameterType="Long">
        delete from pro_project_apply where id = #{id}
    </delete>

    <delete id="deleteProProjectApplyByIds" parameterType="String">
        delete from pro_project_apply where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    
    <delete id="deleteProProjectApplyDetailByApplyIds" parameterType="String">
        delete from pro_project_apply_detail where apply_id in 
        <foreach item="applyId" collection="array" open="(" separator="," close=")">
            #{applyId}
        </foreach>
    </delete>

    <delete id="deleteProProjectApplyDetailByApplyId" parameterType="Long">
        delete from pro_project_apply_detail where apply_id = #{applyId}
    </delete>

    <insert id="batchProProjectApplyDetail">
        insert into pro_project_apply_detail( id, apply_id, super_type_key, super_type_name, type_id, type_name, assets_id, assets_name, assets_unit, number, use_time, use_reason, price, total_price, is_del, create_by, create_time, update_by, update_time, remark) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.id}, #{item.applyId}, #{item.superTypeKey}, #{item.superTypeName}, #{item.typeId}, #{item.typeName}, #{item.assetsId}, #{item.assetsName}, #{item.assetsUnit}, #{item.number}, #{item.useTime}, #{item.useReason}, #{item.price}, #{item.totalPrice}, #{item.isDel}, #{item.createBy}, #{item.createTime}, #{item.updateBy}, #{item.updateTime}, #{item.remark})
        </foreach>
    </insert>
</mapper>