<?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.warehouseBusiness.mapper.TWarehouseAgreementitemsMapper">

    <resultMap type="TWarehouseAgreementitems" id="TWarehouseAgreementitemsResult">
        <result property="fId" column="f_id"/>
        <result property="fPid" column="f_pid"/>
        <result property="fLineno" column="f_lineno"/>
        <result property="fFeeid" column="f_feeid"/>
        <result property="fFeeunitid" column="f_feeUnitid"/>
        <result property="fFromdays" column="f_fromdays"/>
        <result property="fEndays" column="f_endays"/>
        <result property="fPrice" column="f_price"/>
        <result property="fStatus" column="f_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="remark" column="remark"/>
    </resultMap>

    <sql id="selectTWarehouseAgreementitemsVo">
        select f_id, f_pid, f_lineno, f_feeid, f_feeUnitid, f_fromdays, f_endays, f_price, f_status, del_flag, create_by, create_time, update_by, update_time, remark from t_warehouse_agreementitems
    </sql>

    <select id="selectTWarehouseAgreementitemsList" parameterType="TWarehouseAgreementitems"
            resultMap="TWarehouseAgreementitemsResult">
        <include refid="selectTWarehouseAgreementitemsVo"/>
        <where>
            <if test="fPid != null ">and f_pid = #{fPid}</if>
            <if test="fLineno != null ">and f_lineno = #{fLineno}</if>
            <if test="fFeeid != null ">and f_feeid = #{fFeeid}</if>
            <if test="fFeeunitid != null ">and f_feeUnitid = #{fFeeunitid}</if>
            <if test="fFromdays != null ">and f_fromdays = #{fFromdays}</if>
            <if test="fEndays != null ">and f_endays = #{fEndays}</if>
            <if test="fPrice != null ">and f_price = #{fPrice}</if>
            <if test="fStatus != null  and fStatus != ''">and f_status = #{fStatus}</if>
        </where>
    </select>


    <select id="selectTWarehouseAgreementList1" parameterType="TWarehouseAgreement"
            resultType="Map" >
        SELECT DISTINCT
        agre.f_id AS fId,
        corp.f_name AS fCorpid,
        agre.f_contractno AS fContractno,
        goods.f_name AS fGoodsid,
        agre.t_packages AS tPackages,
        agre.f_begindate AS fBegindate,
        agre.f_enddate AS fEnddate,
        agre.f_feetypeid AS fFeetypeid,
        agre.f_billstatus AS fBillstatus
        FROM
        t_warehouse_agreement agre
        LEFT JOIN t_corps corp ON corp.f_id = agre.f_corpid
        LEFT JOIN t_goods goods ON goods.f_id = agre.f_goodsid
        <where>
            <if test="fBillno != null  and fBillno != ''">and agre.f_billno = #{fBillno}</if>
            <if test="fContractno != null  and fContractno != ''">and agre.f_contractno = #{fContractno}</if>
            <if test="fDeptid != null ">and agre.f_deptid = #{fDeptid}</if>
            <if test="fCorpid != null ">and agre.f_corpid = #{fCorpid}</if>
            <if test="fGoodsid != null">and agre.f_goodsid = #{fGoodsid}</if>
            <if test="fStltypeid != null ">and agre.f_stltypeid = #{fStltypeid}</if>
            <if test="fFeetypeid != null ">and agre.f_feetypeid = #{fFeetypeid}</if>
            <if test="tPackages != null  and tPackages != ''">and agre.t_packages = #{tPackages}</if>
            <if test="fTrademodeid != null ">and agre.f_trademodeid = #{fTrademodeid}</if>
            <if test="fFreedays != null ">and agre.f_freedays = #{fFreedays}</if>
            <if test="fBegindate != null ">and agre.f_begindate = #{fBegindate}</if>
            <if test="fEnddate != null ">and agre.f_enddate = #{fEnddate}</if>
            <if test="fBillstatus != null  and fBillstatus != ''">and agre.f_billstatus = #{fBillstatus}</if>
        </where>
    </select>

    <select id="selectTWarehouseAgreementitemsById" parameterType="Long" resultMap="TWarehouseAgreementitemsResult">
        <include refid="selectTWarehouseAgreementitemsVo"/>
        where f_id = #{fId}
    </select>


    <!-- 联查计费规则信息 -->
    <select id="getItemsBytWarehouseAgreementMsg"
            resultMap="TWarehouseAgreementitemsResult"
            parameterType="object">
        SELECT
            items.f_id,
            items.f_pid,
            items.f_lineno,
            items.f_feeid,
            items.f_feeUnitid,
            items.f_fromdays,
            items.f_endays,
            items.f_price,
            items.f_status,
            items.del_flag,
            items.create_by,
            items.create_time,
            items.update_by,
            items.update_time,
            items.remark
        FROM
            t_warehouse_agreement AS agreement
            LEFT JOIN t_warehouse_agreementitems AS items ON agreement.f_id = items.f_pid
        WHERE
            agreement.f_corpid = #{fCorpid}
            AND agreement.t_packages = #{fGoodsid}
            AND items.f_feeUnitid = #{feeUnitid}
            AND agreement.f_status = 0
            AND agreement.del_flag = '0'
            AND agreement.f_feetypeid = '0'
            AND items.del_flag = '0'
        ORDER BY
            items.f_fromdays
    </select>

    <select id="selectTWarehouseAgreementitemsList1" resultType="java.util.Map">
        SELECT DISTINCT
            agre.f_id AS fId,
            corp.f_name AS fCorpname,
            corp.f_id AS fCorpid,
            fee.f_name AS  fName,
            fee.f_id AS feeFId,
            itme.f_feeUnitid AS fFeeUnitid,
            f_price AS fPrice
            FROM
            t_warehouse_agreement agre
            LEFT JOIN t_corps corp ON corp.f_id = agre.f_corpid
            LEFT JOIN t_warehouse_agreementitems itme ON itme.f_pid = agre.f_id
            LEFT JOIN t_fees fee ON fee.f_id = itme.f_feeid
            Left JOIN t_goods goods ON goods.f_typeid = agre.t_packages
        <where>
            <if test="fLineno != null  and fLineno != ''">and corp.f_id = #{fLineno}</if>
            <if test="fFeeid != null  and fFeeid != ''">and goods.f_id = #{fFeeid}</if>
            and agre.f_billstatus = 6
            and agre.f_status = 0
            and agre.f_feetypeid = 1
        </where>
    </select>

    <select id="selectTWarehouseAgreementitemList" resultType="java.util.Map">
        SELECT DISTINCT
        agre.f_id AS fId,
        corp.f_name AS fCorpname,
        corp.f_id AS fCorpid,
        fee.f_name AS  fName,
        fee.f_id AS feeFId,
        itme.f_feeUnitid AS fFeeUnitid,
        f_price AS fPrice
        FROM
        t_warehouse_agreement agre
        LEFT JOIN t_corps corp ON corp.f_id = agre.f_corpid
        LEFT JOIN t_warehouse_agreementitems itme ON itme.f_pid = agre.f_id
        LEFT JOIN t_fees fee ON fee.f_id = itme.f_feeid
        Left JOIN t_goods goods ON goods.f_typeid = agre.t_packages
        <where>
            <if test="fCorpid != null  and fCorpid != ''">
                and corp.f_id = #{fCorpid}
                and agre.f_dc = 'D'
            </if>
            <if test="fFeeid != null  and fFeeid != ''">and goods.f_id = #{fFeeid}</if>
            <if test="fFleet != null  and fFleet != ''">
                 and corp.f_id = #{fFleet}
                 and agre.f_dc = 'C'
            </if>
            <if test="fLabour != null  and fLabour != ''">
                and corp.f_id = #{fLabour}
                and agre.f_dc = 'C'
            </if>
            <if test="fTaskType != null  and fTaskType != ''">
                and agre.f_task_type = #{fTaskType}
            </if>
            <if test="fBilltype != null  and fBilltype != ''">
                and agre.f_billtype  like concat('%', #{fBilltype}, '%')
            </if>
            and agre.f_billstatus = 6
            and agre.f_status = 0
            and agre.f_feetypeid = 1
        </where>
    </select>

    <insert id="insertTWarehouseAgreementitems" parameterType="TWarehouseAgreementitems" useGeneratedKeys="true"
            keyProperty="fId">
        insert into t_warehouse_agreementitems
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="fPid != null">f_pid,</if>
            <if test="fLineno != null">f_lineno,</if>
            <if test="fFeeid != null">f_feeid,</if>
            <if test="fFeeunitid != null">f_feeUnitid,</if>
            <if test="fFromdays != null">f_fromdays,</if>
            <if test="fEndays != null">f_endays,</if>
            <if test="fPrice != null">f_price,</if>
            <if test="fStatus != null">f_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="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="fPid != null">#{fPid},</if>
            <if test="fLineno != null">#{fLineno},</if>
            <if test="fFeeid != null">#{fFeeid},</if>
            <if test="fFeeunitid != null">#{fFeeunitid},</if>
            <if test="fFromdays != null">#{fFromdays},</if>
            <if test="fEndays != null">#{fEndays},</if>
            <if test="fPrice != null">#{fPrice},</if>
            <if test="fStatus != null">#{fStatus},</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="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateTWarehouseAgreementitems" parameterType="TWarehouseAgreementitems">
        update t_warehouse_agreementitems
        <trim prefix="SET" suffixOverrides=",">
            <if test="fPid != null">f_pid = #{fPid},</if>
            <if test="fLineno != null">f_lineno = #{fLineno},</if>
            <if test="fFeeid != null">f_feeid = #{fFeeid},</if>
            <if test="fFeeunitid != null">f_feeUnitid = #{fFeeunitid},</if>
            <if test="fFromdays != null">f_fromdays = #{fFromdays},</if>
            <if test="fEndays != null">f_endays = #{fEndays},</if>
            <if test="fPrice != null">f_price = #{fPrice},</if>
            <if test="fStatus != null">f_status = #{fStatus},</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="remark != null">remark = #{remark},</if>
        </trim>
        where f_id = #{fId}
    </update>

    <delete id="deleteTWarehouseAgreementitemsById" parameterType="Long">
        delete from t_warehouse_agreementitems where f_id = #{fId}
    </delete>

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

    <delete id="deleteByFPid" parameterType="Long">
        delete from t_warehouse_agreementitems where f_pid = #{fId}
    </delete>

    <update id="agreementitemsFollowUpdate" parameterType="Long">
        update
            t_warehouse_agreementitems
        set f_status = #{fettle}
        where
            f_pid = #{fPid}
    </update>

</mapper>