<?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.FtmsorderbillsplansMapper">

    <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="planCntrQty" column="plan_cntr_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="selectftmsorderbillsplansVo">
        select id, p_id, org_id, cntr_id, cntr_qty, car_qty, plan_cntr_qty, carcor_pid, pricecr_type, pricecr, if_need_inv_cr, carnolist, plan_date, planre_marks, accept_date, accept_remarks, bill_status, del_flag, create_by, create_time, update_by, update_time, remarks from F_TMSORDERBILLSPLANS
    </sql>

    <select id="selectftmsorderbillsplansList" parameterType="ftmsorderbillsplans"
            resultMap="ftmsorderbillsplansResult">
        <include refid="selectftmsorderbillsplansVo"/>
        <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="carQty != null ">and car_qty = #{carQty}</if>
            <if test="carQty != null ">and plan_cntr_qty = #{planCntrQty}</if>
            <if test="carcorPid != null ">and carcor_pid = #{carcorPid}</if>
            <if test="pricecrType != null  and pricecrType != ''">and pricecr_type = #{pricecrType}</if>
            <if test="pricecr != null ">and pricecr = #{pricecr}</if>
            <if test="ifNeedInvCr != null  and ifNeedInvCr != ''">and if_need_inv_cr = #{ifNeedInvCr}</if>
            <if test="carnolist != null  and carnolist != ''">and carnolist = #{carnolist}</if>
            <if test="planDate != null ">and plan_date = #{planDate}</if>
            <if test="planreMarks != null  and planreMarks != ''">and planre_marks = #{planreMarks}</if>
            <if test="acceptDate != null ">and accept_date = #{acceptDate}</if>
            <if test="acceptRemarks != null  and acceptRemarks != ''">and accept_remarks = #{acceptRemarks}</if>
            <if test="billStatus != null ">and bill_status = #{billStatus}</if>
            <if test="remarks != null  and remarks != ''">and remarks = #{remarks}</if>
        </where>
    </select>

    <select id="selectftmsorderbillsplansMapList" parameterType="ftmsorderbills" resultType="Map">
        SELECT
            plan.id,
            plan.carnolist,
            plan.car_qty AS carQty,
            plan.cntr_qty AS cntrQty,
            f.bill_no AS billNo,
            f.act_id AS actId,
            c.f_name AS corpId,
            sysCntr.dict_label AS cntrName,
            p.nick_name AS planUserId,
            a.nick_name AS transUserId,
            type.dict_label AS billType,
            trans.dict_label AS transType,
            prop.dict_label AS transProp,
            f.if_contracted AS ifContracted,
            f.contract_no AS contractNo,
            f.accstl_type AS accstlType,
            f.accdays,
            m.f_name AS carcorPid,
            f.load_addr AS loadAddr,
            f.md_load_addr AS mdLoadAddr,
            f.un_load_addr AS unLoadAddr,
            g.f_name AS goodsId,
            f.goods_price AS goodsPrice,
            f.mblno,
            f.load_date AS loadDate,
            f.qty_plan AS qtyPlan,
            f.qty_dis_patch AS qtyDisPatch,
            f.weight_plan AS weightPlan,
            plan.bill_status AS billStatus,
            f.create_time AS createTime
        FROM
            F_TMSORDERBILLSPLANS plan
            LEFT JOIN F_TMSORDERBILLS f ON f.id = plan.org_id
            LEFT JOIN sys_dict_data sysCntr ON sysCntr.dict_type = 'data_cntrId'
            AND sysCntr.dict_value = plan.cntr_id
            LEFT JOIN sys_dict_data type ON type.dict_type = 'data_billType'
            AND type.dict_value = f.bill_type
            LEFT JOIN sys_dict_data trans ON trans.dict_type = 'data_transType'
            AND trans.dict_value = f.trans_type
            LEFT JOIN sys_dict_data prop ON prop.dict_type = 'data_transProp'
            AND prop.dict_value = f.trans_prop
            LEFT JOIN t_corps m ON m.f_id = plan.carcor_pid
            AND m.f_typeid = '["4"]'
            LEFT JOIN t_corps c ON c.f_id = f.corp_id
            LEFT JOIN sys_user p ON p.user_id = f.plan_user_id
            LEFT JOIN sys_user a ON a.user_id = f.trans_user_id
            LEFT JOIN t_goods g ON g.f_id = f.goods_id
        WHERE
            plan.bill_status >= 6
            <if test="corpId != null and corpId != ''">
                AND f.corp_id = #{corpId}
            </if>
            <if test="actId != null and actId != 0">
                AND f.act_id = #{actId}
            </if>
            <if test="billType != null and billType != ''">
                AND f.bill_type = #{billType}
            </if>
            <if test="transType != null and transType != ''">
                AND f.trans_type = #{transType}
            </if>
            <if test="transProp != null and transProp != ''">
                AND f.trans_prop = #{transProp}
            </if>
            <if test="goodsId != null and goodsId != 0">
                AND f.goods_id = #{goodsId}
            </if>
            <if test="mblno != null and mblno != ''">
                AND f.mblno LIKE "%${mblno}%"
            </if>
            <if test="loadAddr != null and loadAddr != ''">
                AND f.load_addr LIKE "%${loadAddr}%"
            </if>
            <if test="mdLoadAddr != null and mdLoadAddr != ''">
                AND f.md_load_addr LIKE "%${mdLoadAddr}%"
            </if>
            <if test="unLoadAddr != null and unLoadAddr != ''">
                AND f.un_load_addr LIKE "%${unLoadAddr}%"
            </if>
            <if test="billStatus != null and billStatus != ''">
                AND f.bill_status LIKE "%${billStatus}%"
            </if>
        ORDER BY plan.id desc
    </select>

    <select id="selectftmsorderbillsplansCountList" parameterType="ftmsorderbillsplans" resultType="int">
        select count(id) from F_TMSORDERBILLSPLANS
        <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="carQty != null ">and car_qty = #{carQty}</if>
            <if test="carQty != null ">and plan_cntr_qty = #{planCntrQty}</if>
            <if test="carcorPid != null ">and carcor_pid = #{carcorPid}</if>
            <if test="pricecrType != null  and pricecrType != ''">and pricecr_type = #{pricecrType}</if>
            <if test="pricecr != null ">and pricecr = #{pricecr}</if>
            <if test="ifNeedInvCr != null  and ifNeedInvCr != ''">and if_need_inv_cr = #{ifNeedInvCr}</if>
            <if test="carnolist != null  and carnolist != ''">and carnolist = #{carnolist}</if>
            <if test="planDate != null ">and plan_date = #{planDate}</if>
            <if test="planreMarks != null  and planreMarks != ''">and planre_marks = #{planreMarks}</if>
            <if test="acceptDate != null ">and accept_date = #{acceptDate}</if>
            <if test="acceptRemarks != null  and acceptRemarks != ''">and accept_remarks = #{acceptRemarks}</if>
            <if test="billStatus != null ">and bill_status = #{billStatus}</if>
            <if test="remarks != null  and remarks != ''">and remarks = #{remarks}</if>
        </where>
    </select>

    <select id="selectftmsorderbillsplansById" parameterType="Long" resultMap="ftmsorderbillsplansResult">
        <include refid="selectftmsorderbillsplansVo"/>
        where id = #{id}
    </select>

    <select id="selectftmsorderbillsplansByPId" parameterType="Long" resultMap="ftmsorderbillsplansResult">
        <include refid="selectftmsorderbillsplansVo"/>
        where p_id = #{id}
    </select>

    <select id="selectftmsorderbillsplansByOrgId" parameterType="Long" resultMap="ftmsorderbillsplansResult">
        <include refid="selectftmsorderbillsplansVo"/>
        where org_id = #{orgId}
    </select>

    <insert id="insertftmsorderbillsplans" parameterType="ftmsorderbillsplans" useGeneratedKeys="true" keyProperty="id">
        insert into F_TMSORDERBILLSPLANS
        <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="carQty != null">car_qty,</if>
            <if test="planCntrQty != null">plan_cntr_qty,</if>
            <if test="carcorPid != null">carcor_pid,</if>
            <if test="pricecrType != null">pricecr_type,</if>
            <if test="pricecr != null">pricecr,</if>
            <if test="ifNeedInvCr != null">if_need_inv_cr,</if>
            <if test="carnolist != null">carnolist,</if>
            <if test="planDate != null">plan_date,</if>
            <if test="planreMarks != null">planre_marks,</if>
            <if test="acceptDate != null">accept_date,</if>
            <if test="acceptRemarks != null">accept_remarks,</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="carQty != null">#{carQty},</if>
            <if test="planCntrQty != null">#{planCntrQty},</if>
            <if test="carcorPid != null">#{carcorPid},</if>
            <if test="pricecrType != null">#{pricecrType},</if>
            <if test="pricecr != null">#{pricecr},</if>
            <if test="ifNeedInvCr != null">#{ifNeedInvCr},</if>
            <if test="carnolist != null">#{carnolist},</if>
            <if test="planDate != null">#{planDate},</if>
            <if test="planreMarks != null">#{planreMarks},</if>
            <if test="acceptDate != null">#{acceptDate},</if>
            <if test="acceptRemarks != null">#{acceptRemarks},</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="updateftmsorderbillsplans" parameterType="ftmsorderbillsplans">
        update F_TMSORDERBILLSPLANS
        <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="carQty != null">car_qty = #{carQty},</if>
            <if test="planCntrQty != null">plan_cntr_qty = #{planCntrQty},</if>
            <if test="carcorPid != null">carcor_pid = #{carcorPid},</if>
            <if test="pricecrType != null">pricecr_type = #{pricecrType},</if>
            <if test="pricecr != null">pricecr = #{pricecr},</if>
            <if test="ifNeedInvCr != null">if_need_inv_cr = #{ifNeedInvCr},</if>
            <if test="carnolist != null">carnolist = #{carnolist},</if>
            <if test="planDate != null">plan_date = #{planDate},</if>
            <if test="planreMarks != null">planre_marks = #{planreMarks},</if>
            <if test="acceptDate != null">accept_date = #{acceptDate},</if>
            <if test="acceptRemarks != null">accept_remarks = #{acceptRemarks},</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="updateTmsorderSplanStatusByOrgId" parameterType="ftmsorderbillsplans">
        update F_TMSORDERBILLSPLANS
        set bill_status = #{billStatus}
        where org_id = #{orgId}
    </update>

    <delete id="deleteftmsorderbillsplansById" parameterType="Long">
        delete from F_TMSORDERBILLSPLANS where id = #{id}
    </delete>

    <delete id="deleteftmsorderbillsplansByPId" parameterType="Long">
        delete from F_TMSORDERBILLSPLANS where p_id = #{pId}
    </delete>

    <delete id="deleteftmsorderbillsplansByOrgId" parameterType="Long">
        delete from F_TMSORDERBILLSPLANS where org_id = #{orgId}
    </delete>

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

</mapper>