<?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.orderPlan.mapper.FtmsorderbillscntrsMapper">

    <resultMap type="ftmsorderbillscntrs" id="ftmsorderbillscntrsResult">
        <result property="id" column="id"/>
        <result property="pId" column="p_id"/>
        <result property="orgId" column="org_id"/>
        <result property="cntrId" column="cntr_id"/>
        <result property="cntrQty" column="cntr_qty"/>
        <result property="cntrPlanQty" column="cntr_plan_qty"/>
        <result property="cntrBlcQty" column="cntr_blc_qty"/>
        <result property="priceType" column="price_type"/>
        <result property="planQty" column="plan_qty"/>
        <result property="priceDr" column="price_dr"/>
        <result property="planBlcQty" column="plan_blc_qty"/>
        <result property="priceCr" column="price_cr"/>
        <result property="billStatus" column="bill_status"/>
        <result property="delFlag" column="del_flag"/>
        <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="remarks" column="remarks"/>
    </resultMap>

    <resultMap type="ftmsorderbillsplans" id="ftmsorderbillsplansResult">
        <result property="id" column="id"/>
        <result property="pId" column="p_id"/>
        <result property="orgId" column="org_id"/>
        <result property="cntrId" column="cntr_id"/>
        <result property="cntrQty" column="cntr_qty"/>
        <result property="carQty" column="car_qty"/>
        <result property="carcorPid" column="carcor_pid"/>
        <result property="pricecrType" column="pricecr_type"/>
        <result property="pricecr" column="pricecr"/>
        <result property="ifNeedInvCr" column="if_need_inv_cr"/>
        <result property="carnolist" column="carnolist"/>
        <result property="planDate" column="plan_date"/>
        <result property="planreMarks" column="planre_marks"/>
        <result property="acceptDate" column="accept_date"/>
        <result property="acceptRemarks" column="accept_remarks"/>
        <result property="billStatus" column="bill_status"/>
        <result property="delFlag" column="del_flag"/>
        <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="remarks" column="remarks"/>
    </resultMap>

    <sql id="selectftmsorderbillscntrsVo">
        select id, p_id, org_id, cntr_id, cntr_qty, cntr_plan_qty, cntr_blc_qty, price_type, plan_qty, price_dr, plan_blc_qty, price_cr, bill_status, del_flag, create_by, create_time, update_by, update_time, remarks from F_TMSORDERBILLSCNTRS
    </sql>

    <select id="selectftmsorderbillscntrsList" parameterType="ftmsorderbillscntrs"
            resultMap="ftmsorderbillscntrsResult">
        <include refid="selectftmsorderbillscntrsVo"/>
        <where>
            <if test="pId != null "> and p_id = #{pId}</if>
            <if test="orgId != null "> and org_id = #{orgId}</if>
            <if test="cntrId != null "> and cntr_id = #{cntrId}</if>
            <if test="cntrQty != null "> and cntr_qty = #{cntrQty}</if>
            <if test="cntrPlanQty != null "> and cntr_plan_qty = #{cntrPlanQty}</if>
            <if test="cntrBlcQty != null "> and cntr_blc_qty = #{cntrBlcQty}</if>
            <if test="priceType != null  and priceType != ''"> and price_type = #{priceType}</if>
            <if test="planQty != null "> and plan_qty = #{planQty}</if>
            <if test="priceDr != null "> and price_dr = #{priceDr}</if>
            <if test="planBlcQty != null "> and plan_blc_qty = #{planBlcQty}</if>
            <if test="priceCr != null "> and price_cr = #{priceCr}</if>
            <if test="billStatus != null "> and bill_status = #{billStatus}</if>
            <if test="remarks != null  and remarks != ''"> and remarks = #{remarks}</if>
        </where>
    </select>

    <select id="selectftmsorderbillscntrsById" parameterType="Long" resultMap="ftmsorderbillscntrsResult">
        <include refid="selectftmsorderbillscntrsVo"/>
        where id = #{id}
    </select>

    <select id="selectftmsorderbillscntrsByPId" parameterType="Long" resultMap="ftmsorderbillscntrsResult">
        <include refid="selectftmsorderbillscntrsVo"/>
        where p_id = #{id}
    </select>

    <select id="selectftmsorderbillscntrs" resultType="java.lang.Integer">
        select IFNULL(cntr_qty,null) manageQuantity from F_TMSORDERBILLSCNTRS
        where to_days(create_time) = to_days(now())
    </select>

    <insert id="insertftmsorderbillscntrs" parameterType="ftmsorderbillscntrs" useGeneratedKeys="true" keyProperty="id">
        insert into F_TMSORDERBILLSCNTRS
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="pId != null">p_id,</if>
            <if test="orgId != null">org_id,</if>
            <if test="cntrId != null">cntr_id,</if>
            <if test="cntrQty != null">cntr_qty,</if>
            <if test="cntrPlanQty != null">cntr_plan_qty,</if>
            <if test="cntrBlcQty != null">cntr_blc_qty,</if>
            <if test="priceType != null">price_type,</if>
            <if test="planQty != null">plan_qty,</if>
            <if test="priceDr != null">price_dr,</if>
            <if test="planBlcQty != null">plan_blc_qty,</if>
            <if test="priceCr != null">price_cr,</if>
            <if test="billStatus != null">bill_status,</if>
            <if test="delFlag != null">del_flag,</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="remarks != null">remarks,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="pId != null">#{pId},</if>
            <if test="orgId != null">#{orgId},</if>
            <if test="cntrId != null">#{cntrId},</if>
            <if test="cntrQty != null">#{cntrQty},</if>
            <if test="cntrPlanQty != null">#{cntrPlanQty},</if>
            <if test="cntrBlcQty != null">#{cntrBlcQty},</if>
            <if test="priceType != null">#{priceType},</if>
            <if test="planQty != null">#{planQty},</if>
            <if test="priceDr != null">#{priceDr},</if>
            <if test="planBlcQty != null">#{planBlcQty},</if>
            <if test="priceCr != null">#{priceCr},</if>
            <if test="billStatus != null">#{billStatus},</if>
            <if test="delFlag != null">#{delFlag},</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="remarks != null">#{remarks},</if>
        </trim>
    </insert>

    <update id="updateftmsorderbillscntrs" parameterType="ftmsorderbillscntrs">
        update F_TMSORDERBILLSCNTRS
        <trim prefix="SET" suffixOverrides=",">
            <if test="pId != null">p_id = #{pId},</if>
            <if test="orgId != null">org_id = #{orgId},</if>
            <if test="cntrId != null">cntr_id = #{cntrId},</if>
            <if test="cntrQty != null">cntr_qty = #{cntrQty},</if>
            <if test="cntrPlanQty != null">cntr_plan_qty = #{cntrPlanQty},</if>
            <if test="cntrBlcQty != null">cntr_blc_qty = #{cntrBlcQty},</if>
            <if test="priceType != null">price_type = #{priceType},</if>
            <if test="planQty != null">plan_qty = #{planQty},</if>
            <if test="priceDr != null">price_dr = #{priceDr},</if>
            <if test="planBlcQty != null">plan_blc_qty = #{planBlcQty},</if>
            <if test="priceCr != null">price_cr = #{priceCr},</if>
            <if test="billStatus != null">bill_status = #{billStatus},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</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="remarks != null">remarks = #{remarks},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateTmsorderbillScntrsQty" parameterType="ftmsorderbillsplans">
        update F_TMSORDERBILLSCNTRS
        <trim prefix="SET" suffixOverrides=",">
            <if test="carQty != null and carQty != 0">cntr_plan_qty = cntr_plan_qty + #{cntrQty},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateTmsorderCntrStatusByOrgId" parameterType="ftmsorderbillsplans">
        update F_TMSORDERBILLSCNTRS
        set bill_status = #{billStatus}
        where org_id = #{orgId}
    </update>

    <delete id="deleteftmsorderbillscntrsById" parameterType="Long">
        delete from F_TMSORDERBILLSCNTRS where id = #{id}
    </delete>

    <delete id="deleteftmsorderbillscntrsByPId" parameterType="Long">
        delete from F_TMSORDERBILLSCNTRS where p_id = #{id}
    </delete>

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

</mapper>