<?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.TPurchaseMapper">
    
    <resultMap type="TPurchase" id="TPurchaseResult">
        <result property="purchaseId"    column="purchase_id"    />
        <result property="purchaseDeptId"    column="purchase_dept_id"    />
        <result property="purchaseType"    column="purchase_type"    />
        <result property="handledByUserId"    column="handled_by_user_id"    />
        <result property="purchaseName"    column="purchase_name"    />
        <result property="specifications"    column="specifications"    />
        <result property="modelType"    column="model_type"    />
        <result property="applicationDescription"    column="application_description"    />
        <result property="userId"    column="user_id"    />
        <result property="price"    column="price"    />
        <result property="count"    column="count"    />
        <result property="total"    column="total"    />
        <result property="purchaseDeptManagerId"    column="purchase_dept_manager_id"    />
        <result property="approvedUserId"    column="approved_user_id"    />
        <result property="remark"    column="remark"    />
        <result property="createTime"    column="create_time"    />
        <result property="isDel"    column="is_del"    />
        <result property="updateTime"    column="update_time"    />
        <result property="purchaseStatus"    column="purchase_status"    />
        <result property="approvedTime"    column="approved_time"    />
        <result property="purchaseDeptName"    column="purchase_dept_name"    />
        <result property="userName"    column="user_name"    />
        <result property="approvedUserName" column="approved_user_name"/>
        <result property="purchaseDeptManagerName" column="purchase_dept_manager_name"/>
        <result property="handledByUserName" column="handled_by_user_name"/>

    </resultMap>

    <sql id="selectTPurchaseVo">
        SELECT
            p.purchase_id,
            p.purchase_dept_id,
            p.purchase_type,
            p.handled_by_user_id,
            p.purchase_name,
            p.specifications,
            p.model_type,
            p.application_description,
            p.user_id,
            p.price,
            p.count,
            p.total,
            p.purchase_dept_manager_id,
            p.approved_user_id,
            p.remark,
            p.create_time,
            p.is_del,
            p.update_time,
            p.purchase_status,
            p.approved_time,
            (SELECT t1.nick_name FROM sys_user t1 WHERE t1.user_id = p.user_id) user_name,
            (SELECT t1.nick_name FROM sys_user t1 WHERE t1.user_id = p.approved_user_id) approved_user_name,
            (SELECT t1.nick_name FROM sys_user t1 WHERE t1.user_id = p.purchase_dept_manager_id) purchase_dept_manager_name,
            (SELECT t1.nick_name FROM sys_user t1 WHERE t1.user_id = t.handled_by_user_id) handled_by_user_name
        FROM
            t_purchase p
    </sql>

    <select id="selectTPurchaseList" parameterType="TPurchase" resultMap="TPurchaseResult">
        select (select dept_name from sys_dept where dept_id =purchase_dept_id)purchase_dept_name,
        t2.nick_name user_name,
        (SELECT t1.nick_name FROM sys_user t1 WHERE t1.user_id = t.approved_user_id) approved_user_name,
        (SELECT t1.nick_name FROM sys_user t1 WHERE t1.user_id = t.handled_by_user_id) handled_by_user_name,
        t.purchase_id, t.purchase_type,t.handled_by_user_id,t.purchase_dept_id, t.purchase_name, t.specifications, t.model_type, t.application_description, t.user_id, price, count, total, purchase_dept_manager_id, approved_user_id, t.remark, t.create_time, t.is_del, t.update_time, t.purchase_status, t.approved_time from t_purchase t,sys_user t2
        <where>
        t.user_id=t2.user_id
            <if test="purchaseDeptId != null "> and t.purchase_dept_id = #{purchaseDeptId}</if>
            <if test="handledByUserId != null "> and t.handled_by_user_id = #{handledByUserId}</if>
            <if test="purchaseName != null  and purchaseName != ''"> and t.purchase_name like concat('%', #{purchaseName}, '%')</if>
            <if test="specifications != null  and specifications != ''"> and t.specifications = #{specifications}</if>
            <if test="modelType != null  and modelType != ''"> and t.model_type = #{modelType}</if>
            <if test="applicationDescription != null  and applicationDescription != ''"> and t.application_description like concat('%', #{applicationDescription}, '%')</if>
            <if test="userId != null "> and t.user_id = #{userId}</if>
            <if test="userName != null "> and t2.nick_name like  concat('%', #{userName}, '%')</if>
            <if test="price != null "> and t.price = #{price}</if>
            <if test="count != null "> and t.count = #{count}</if>
            <if test="total != null "> and t.total = #{total}</if>
            <if test="purchaseDeptManagerId != null "> and t.purchase_dept_manager_id = #{purchaseDeptManagerId}</if>
            <if test="approvedUserId != null "> and t.approved_user_id = #{approvedUserId}</if>
            <if test="isDel != null  and isDel != ''"> and t.is_del = #{isDel}</if>
            <if test="purchaseStatus != null  and purchaseStatus != ''"> and t.purchase_status = #{purchaseStatus}</if>
            <if test="approvedTime != null "> and t.approved_time = #{approvedTime}</if>
            <if test="purchaseType != null  and purchaseType != ''"> and purchase_type = #{purchaseType}</if>
        </where>
        ORDER BY t.create_time DESC
    </select>
    
    <select id="selectTPurchaseById" parameterType="Long" resultMap="TPurchaseResult">
        <include refid="selectTPurchaseVo"/>
        where p.purchase_id = #{purchaseId}
    </select>
        
    <insert id="insertTPurchase" parameterType="TPurchase">
        insert into t_purchase
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="purchaseId != null">purchase_id,</if>
            <if test="purchaseDeptId != null">purchase_dept_id,</if>
            <if test="purchaseType != null">purchase_type,</if>
            <if test="handledByUserId != null">handled_by_user_id,</if>
            <if test="purchaseName != null">purchase_name,</if>
            <if test="specifications != null">specifications,</if>
            <if test="modelType != null">model_type,</if>
            <if test="applicationDescription != null">application_description,</if>
            <if test="userId != null">user_id,</if>
            <if test="price != null">price,</if>
            <if test="count != null">count,</if>
            <if test="total != null">total,</if>
            <if test="purchaseDeptManagerId != null">purchase_dept_manager_id,</if>
            <if test="approvedUserId != null">approved_user_id,</if>
            <if test="remark != null">remark,</if>
            <if test="createTime != null">create_time,</if>
            <if test="isDel != null">is_del,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="purchaseStatus != null">purchase_status,</if>
            <if test="approvedTime != null">approved_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="purchaseId != null">#{purchaseId},</if>
            <if test="purchaseDeptId != null">#{purchaseDeptId},</if>
            <if test="purchaseType != null">#{purchaseType},</if>
            <if test="handledByUserId != null">#{handledByUserId},</if>
            <if test="purchaseName != null">#{purchaseName},</if>
            <if test="specifications != null">#{specifications},</if>
            <if test="modelType != null">#{modelType},</if>
            <if test="applicationDescription != null">#{applicationDescription},</if>
            <if test="userId != null">#{userId},</if>
            <if test="price != null">#{price},</if>
            <if test="count != null">#{count},</if>
            <if test="total != null">#{total},</if>
            <if test="purchaseDeptManagerId != null">#{purchaseDeptManagerId},</if>
            <if test="approvedUserId != null">#{approvedUserId},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="purchaseStatus != null">#{purchaseStatus},</if>
            <if test="approvedTime != null">#{approvedTime},</if>
         </trim>
    </insert>

    <update id="updateTPurchase" parameterType="TPurchase">
        update t_purchase
        <trim prefix="SET" suffixOverrides=",">
            <if test="purchaseDeptId != null">purchase_dept_id = #{purchaseDeptId},</if>
            <if test="purchaseType != null">purchase_type = #{purchaseType},</if>
            <if test="handledByUserId != null">handled_by_user_id = #{handledByUserId},</if>
            <if test="purchaseName != null">purchase_name = #{purchaseName},</if>
            <if test="specifications != null">specifications = #{specifications},</if>
            <if test="modelType != null">model_type = #{modelType},</if>
            <if test="applicationDescription != null">application_description = #{applicationDescription},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="price != null">price = #{price},</if>
            <if test="count != null">count = #{count},</if>
            <if test="total != null">total = #{total},</if>
            <if test="purchaseDeptManagerId != null">purchase_dept_manager_id = #{purchaseDeptManagerId},</if>
            <if test="approvedUserId != null">approved_user_id = #{approvedUserId},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="purchaseStatus != null">purchase_status = #{purchaseStatus},</if>
            <if test="approvedTime != null">approved_time = #{approvedTime},</if>
        </trim>
        where purchase_id = #{purchaseId}
    </update>

    <delete id="deleteTPurchaseById" parameterType="Long">
        delete from t_purchase where purchase_id = #{purchaseId}
    </delete>

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