<?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.generator.mapper.SourcePurchaseMapper">

    <resultMap type="SourcePurchase" id="SourcePurchaseResult">
        <id property="purchaseId" column="purchase_id" />
        <result property="projectId" column="project_id" />
        <result property="userId" column="user_id" />
        <result property="orderNo" column="order_no" />
        <result property="amount" column="amount" />
        <result property="payStatus" column="pay_status" />
        <result property="resourceStatus" column="resource_status" />
        <result property="purchaseTime" column="purchase_time" />
        <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="projectName" column="project_name" />
    </resultMap>

    <sql id="selectSourcePurchaseVo">
        select p.purchase_id, p.project_id, p.user_id, p.order_no, p.amount, p.pay_status, p.resource_status,
               p.purchase_time, p.create_by, p.create_time, p.update_by, p.update_time, p.remark,
               sp.project_name
        from source_purchase p
        left join source_project sp on sp.project_id = p.project_id
    </sql>

    <select id="selectSourcePurchaseByPurchaseId" parameterType="Long" resultMap="SourcePurchaseResult">
        <include refid="selectSourcePurchaseVo"/>
        where p.purchase_id = #{purchaseId}
    </select>

    <select id="selectPaidPurchase" resultMap="SourcePurchaseResult">
        <include refid="selectSourcePurchaseVo"/>
        where p.user_id = #{userId} and p.project_id = #{projectId} and p.pay_status = '1'
        order by p.purchase_time desc
        limit 1
    </select>

    <select id="selectSourcePurchaseList" parameterType="SourcePurchase" resultMap="SourcePurchaseResult">
        <include refid="selectSourcePurchaseVo"/>
        <where>
            <if test="projectId != null"> and p.project_id = #{projectId}</if>
            <if test="userId != null"> and p.user_id = #{userId}</if>
            <if test="orderNo != null and orderNo != ''"> and p.order_no like concat('%', #{orderNo}, '%')</if>
            <if test="payStatus != null and payStatus != ''"> and p.pay_status = #{payStatus}</if>
            <if test="resourceStatus != null and resourceStatus != ''"> and p.resource_status = #{resourceStatus}</if>
            <if test="projectName != null and projectName != ''"> and sp.project_name like concat('%', #{projectName}, '%')</if>
        </where>
        order by p.purchase_time desc, p.create_time desc
    </select>

    <select id="selectSourcePurchasesByUserId" parameterType="Long" resultMap="SourcePurchaseResult">
        <include refid="selectSourcePurchaseVo"/>
        where p.user_id = #{userId}
        order by p.purchase_time desc, p.create_time desc
    </select>

    <insert id="insertSourcePurchase" parameterType="SourcePurchase" useGeneratedKeys="true" keyProperty="purchaseId">
        insert into source_purchase
        <trim prefix="(" suffix=")" suffixOverrides=",">
            project_id,
            user_id,
            order_no,
            amount,
            pay_status,
            resource_status,
            purchase_time,
            <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=",">
            #{projectId},
            #{userId},
            #{orderNo},
            #{amount},
            #{payStatus},
            #{resourceStatus},
            #{purchaseTime},
            <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="updateSourcePurchase" parameterType="SourcePurchase">
        update source_purchase
        <trim prefix="SET" suffixOverrides=",">
            <if test="payStatus != null">pay_status = #{payStatus},</if>
            <if test="resourceStatus != null">resource_status = #{resourceStatus},</if>
            <if test="purchaseTime != null">purchase_time = #{purchaseTime},</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 purchase_id = #{purchaseId}
    </update>
</mapper>
