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

    <resultMap type="TWarehouseAgreement" id="TWarehouseAgreementResult">
        <result property="rankNo" column="rank_no"/>
        <result property="fId" column="f_id"/>
        <result property="fBillno" column="f_billno"/>
        <result property="fContractno" column="f_contractno"/>
        <result property="fDeptid" column="f_deptid"/>
        <result property="fDeptName" column="f_deptname"/>
        <result property="fCorpid" column="f_corpid"/>
        <result property="fCorpName" column="f_corpname"/>
        <result property="fGoodsid" column="f_goodsid"/>
        <result property="fStltypeName" column="f_stltypename"/>
        <result property="fStltypeid" column="f_stltypeid"/>
        <result property="fFeetypeid" column="f_feetypeid"/>
        <result property="fFeetypeName" column="f_feetypename"/>
        <result property="tPackages" column="t_packages"/>
        <result property="fTrademodeid" column="f_trademodeid"/>
        <result property="fTrademodeName" column="f_trademodename"/>
        <result property="fFreedays" column="f_freedays"/>
        <result property="fBegindate" column="f_begindate"/>
        <result property="fEnddate" column="f_enddate"/>
        <result property="fBillstatus" column="f_billstatus"/>
        <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"/>
        <result property="fTaskTypeName" column="f_task_type_name"/>
        <result property="fTaskType" column="f_task_type"/>
        <result property="fDc" column="f_dc"/>
        <result property="fBilltype" column="f_billtype"/>
        <result property="fBilltypeName" column="f_billtype_name"/>
        <result property="fWarehouseid" column="f_warehouseid"/>
    </resultMap>

    <sql id="selectTWarehouseAgreementVo">
        select f_id,
               f_billno,
               f_contractno,
               f_deptid,
               f_corpid,
               f_goodsid,
               f_stltypeid,
               f_feetypeid,
               t_packages,
               f_trademodeid,
               f_freedays,
               f_begindate,
               f_enddate,
               f_billstatus,
               f_status,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time,
               remark,
               f_task_type,
               f_dc,
               f_billtype,
               f_billtype_name,
               f_warehouseid
        from t_warehouse_agreement
    </sql>

    <select id="selectTWarehouseAgreementList" parameterType="TWarehouseAgreement"
            resultMap="TWarehouseAgreementResult">
        select
            @rank:=@rank + 1 as rank_no,
            a.*
        from
        (
            select
            twg.f_id,
            twg.f_billno,
            twg.f_contractno,
            twg.f_deptid,
            dept.dept_name as f_deptname,
            corp.f_name as f_corpname,
            twg.f_goodsid,
            dict.dict_label as f_stltypename,
            twg.f_stltypeid,
            twg.f_feetypeid,
            CASE
            WHEN twg.f_feetypeid='D' THEN '收'
            WHEN twg.f_feetypeid='C' THEN '付'
            END as f_feetypename,
            twg.t_packages,
            twg.f_trademodeid,
            dttradmode.dict_label as f_trademodename,
            twg.f_freedays,
            twg.f_begindate,
            twg.f_enddate,
            CASE
            WHEN twg.f_billstatus=1 THEN '保存'
            WHEN twg.f_billstatus=2 THEN '暂存'
            WHEN twg.f_billstatus=3 THEN '审批驳回'
            WHEN twg.f_billstatus=4 THEN '提交审核'
            WHEN twg.f_billstatus=5 THEN '审核中'
            WHEN twg.f_billstatus=6 THEN '审核完成'
            END as f_billstatus,
            twg.f_status,
            twg.del_flag,
            twg.create_by,
            twg.create_time,
            twg.update_by,
            twg.update_time,
            twg.remark,
            CASE
            WHEN twg.f_task_type=1 THEN '车队作业费'
            WHEN twg.f_task_type=2 THEN '劳务作业费'
            END as f_task_type_name,
            twg.f_task_type,
            twg.f_dc,
            twg.f_billtype,
            twg.f_billtype_name,
            twg.f_warehouseid
            from t_warehouse_agreement twg
            LEFT JOIN t_corps corp ON corp.f_id = twg.f_corpid
            LEFT JOIN sys_dept dept ON dept.dept_id = twg.f_deptid
            LEFT JOIN (
            SELECT
            *
            FROM
            sys_dict_data dictIn
            WHERE
            dictIn.dict_label =
            IF(
            (
            (dictIn.dict_type = 'data_stltype_type' AND dictIn.`status`='0' AND dictIn.dict_value=1) OR
            (dictIn.dict_type = 'data_stltype_type' AND dictIn.`status`='0' AND dictIn.dict_value=0)
            ),
            dictIn.dict_label,NULL
            )
            ) dict ON dict.dict_value = twg.f_billtype
            LEFT JOIN (
            SELECT
            *
            FROM
            sys_dict_data dictIn
            WHERE
            dictIn.dict_label =
            IF(
            (dictIn.dict_type = 'data_trademodes' AND dictIn.`status`='0'),
            dictIn.dict_label,NULL
            )
            ) dttradmode ON dttradmode.dict_value = twg.f_billtype
        <where>
            <if test="fBillno != null  and fBillno != ''">and twg.f_billno = #{fBillno}</if>
            <if test="fContractno != null  and fContractno != ''">and twg.f_contractno = #{fContractno}</if>
            <if test="fDeptid != null ">and twg.f_deptid = #{fDeptid}</if>
            <if test="fCorpid != null ">and twg.f_corpid = #{fCorpid}</if>
            <if test="fGoodsid != null ">and twg.f_goodsid = #{fGoodsid}</if>
            <if test="fStltypeid != null ">and twg.f_stltypeid = #{fStltypeid}</if>
            <if test="fFeetypeid != null ">and twg.f_feetypeid = #{fFeetypeid}</if>
            <if test="tPackages != null  and tPackages != ''">and twg.t_packages = #{tPackages}</if>
            <if test="fTrademodeid != null ">and twg.f_trademodeid = #{fTrademodeid}</if>
            <if test="fFreedays != null ">and twg.f_freedays = #{fFreedays}</if>
            <if test="fBegindate != null ">and twg.f_begindate = #{fBegindate}</if>
            <if test="fEnddate != null ">and twg.f_enddate = #{fEnddate}</if>
            <if test="fTaskType != null ">and twg.f_task_type = #{fTaskType}</if>
            <if test="fBillstatus != null  and fBillstatus != ''">and twg.f_billstatus = #{fBillstatus}</if>
            <if test="fStatus != null  and fStatus != ''">and twg.f_status = #{fStatus}</if>
            <if test="fBilltype != null  and fBilltype != ''">and twg.f_billtype like concat('%', #{fBilltype}, '%')
            </if>
            <if test="fBilltypeName != null  and fBilltypeName != ''">and twg.f_billtype_name like concat('%',
                #{fBilltypeName}, '%')
            </if>
        </where>
        ) a,
        (select @rank:=0) b
    </select>

    <select id="selectTWarehouseAgreementList1" parameterType="TWarehouseAgreement"
            resultType="Map">
        SELECT DISTINCT
        agre.f_id AS fId,
        agre.f_billno AS fBillno,
        corp.f_name AS fCorpid,
        agre.f_contractno AS fContractno,
        goods.f_id AS fGoodsid,
        goods.f_name AS fGoodsids,
        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,
        agre.f_status AS fStatus,
        agre.f_task_type AS fTaskType,
        agre.f_dc AS fDc,
        agre.f_billtype_name AS fBilltypeName
        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 JSON_CONTAINS( 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="fTaskType != null ">and agre.f_task_type = #{fTaskType}</if>
            <if test="fDc != null ">and agre.f_dc = #{fDc}</if>
            <if test="fEnddate != null ">and agre.f_enddate = #{fEnddate}</if>
            <if test="fBillstatus != null  and fBillstatus != ''">and agre.f_billstatus = #{fBillstatus}</if>
            <if test="fStatus != null  and fStatus != ''">and agre.f_status = #{fStatus}</if>
            <if test="fBilltype != null  and fBilltype != ''">and agre.f_billtype like concat('%', #{fBilltype}, '%')
            </if>
            <if test="fBilltypeName != null  and fBilltypeName != ''">and agre.f_billtype_name like concat('%',
                #{fBilltypeName}, '%')
            </if>
        </where>
    </select>

    <select id="selectTWarehouseAgreementById" parameterType="Long" resultMap="TWarehouseAgreementResult">
        <include refid="selectTWarehouseAgreementVo"/>
        where f_id = #{fId}
    </select>

    <select id="selectTWarehouseAgreementByCorpsId" parameterType="TWarehouseAgreement" resultType="map">
        SELECT DISTINCT
            agre.f_id AS fId,
            agre.f_billno AS fBillno,
            corp.f_name AS fName,
            agre.f_contractno AS fContractno,
            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,
            agre.f_status AS fStatus,
            agre.f_task_type AS fTaskType,
            agre.f_dc AS fDc,
            agre.f_billtype_name AS fBilltypeName
        FROM
            t_warehouse_agreement agre
                LEFT JOIN t_corps corp ON corp.f_id = agre.f_corpid
        <where>
            agre.f_task_type = 1
            <if test="fCorpId != null  and fCorpId != ''">and corp.f_id = #{fCorpId}</if>
        </where>
    </select>

    <insert id="insertTWarehouseAgreement" parameterType="TWarehouseAgreement" useGeneratedKeys="true"
            keyProperty="fId">
        insert into t_warehouse_agreement
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="fBillno != null and fBillno != ''">f_billno,</if>
            <if test="fContractno != null and fContractno != ''">f_contractno,</if>
            <if test="fDeptid != null">f_deptid,</if>
            <if test="fCorpid != null">f_corpid,</if>
            <if test="fGoodsid != null">f_goodsid,</if>
            <if test="fStltypeid != null">f_stltypeid,</if>
            <if test="fFeetypeid != null">f_feetypeid,</if>
            <if test="tPackages != null">t_packages,</if>
            <if test="fWarehouseid != null">f_warehouseid,</if>
            <if test="fTrademodeid != null">f_trademodeid,</if>
            <if test="fFreedays != null">f_freedays,</if>
            <if test="fBegindate != null">f_begindate,</if>
            <if test="fEnddate != null">f_enddate,</if>
            <if test="fBillstatus != null">f_billstatus,</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>
            <if test="fTaskType != null">f_task_type,</if>
            <if test="fDc != null">f_dc,</if>
            <if test="fBilltype != null">f_billtype,</if>
            <if test="fBilltypeName != null">f_billtype_name,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="fBillno != null and fBillno != ''">#{fBillno},</if>
            <if test="fContractno != null and fContractno != ''">#{fContractno},</if>
            <if test="fDeptid != null">#{fDeptid},</if>
            <if test="fCorpid != null">#{fCorpid},</if>
            <if test="fGoodsid != null">#{fGoodsid},</if>
            <if test="fStltypeid != null">#{fStltypeid},</if>
            <if test="fFeetypeid != null">#{fFeetypeid},</if>
            <if test="tPackages != null">#{tPackages},</if>
            <if test="fWarehouseid != null">#{fWarehouseid},</if>
            <if test="fTrademodeid != null">#{fTrademodeid},</if>
            <if test="fFreedays != null">#{fFreedays},</if>
            <if test="fBegindate != null">#{fBegindate},</if>
            <if test="fEnddate != null">#{fEnddate},</if>
            <if test="fBillstatus != null">#{fBillstatus},</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>
            <if test="fTaskType != null">#{fTaskType},</if>
            <if test="fDc != null">#{fDc},</if>
            <if test="fBilltype != null">#{fBilltype},</if>
            <if test="fBilltypeName != null">#{fBilltypeName},</if>
        </trim>
    </insert>

    <update id="updateTWarehouseAgreement" parameterType="TWarehouseAgreement">
        update t_warehouse_agreement
        <trim prefix="SET" suffixOverrides=",">
            <if test="fBillno != null and fBillno != ''">f_billno = #{fBillno},</if>
            <if test="fContractno != null and fContractno != ''">f_contractno = #{fContractno},</if>
            <if test="fDeptid != null">f_deptid = #{fDeptid},</if>
            <if test="fCorpid != null">f_corpid = #{fCorpid},</if>
            <if test="fGoodsid != null">f_goodsid = #{fGoodsid},</if>
            <if test="fStltypeid != null">f_stltypeid = #{fStltypeid},</if>
            <if test="fFeetypeid != null">f_feetypeid = #{fFeetypeid},</if>
            <if test="tPackages != null">t_packages = #{tPackages},</if>
            <if test="fWarehouseid != null">f_warehouseid = #{fWarehouseid},</if>
            <if test="fTrademodeid != null">f_trademodeid = #{fTrademodeid},</if>
            <if test="fFreedays != null">f_freedays = #{fFreedays},</if>
            <if test="fBegindate != null">f_begindate = #{fBegindate},</if>
            <if test="fEnddate != null">f_enddate = #{fEnddate},</if>
            <if test="fBillstatus != null">f_billstatus = #{fBillstatus},</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>
            <if test="fTaskType != null">f_task_type = #{fTaskType},</if>
            <if test="fDc != null">f_dc = #{fDc},</if>
            <if test="fBilltype != null">f_billtype = #{fBilltype},</if>
            <if test="fBilltypeName != null">f_billtype_name = #{fBilltypeName},</if>
        </trim>
        where f_id = #{fId}
    </update>

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

    <delete id="deleteTWarehouseAgreementByIds" parameterType="String">
        delete
        w.*,
        wa.*
        from
        t_warehouse_agreement w
        LEFT JOIN t_warehouse_agreementitems wa ON wa.f_pid = w.f_id
        where w.f_id in
        <foreach item="fId" collection="array" open="(" separator="," close=")">
            #{fId}
        </foreach>
    </delete>

    <select id="agreementRemind" parameterType="TWarehouseAgreement" resultType="Map">
        select distinct
        agre.f_id AS fId,
        agre.f_feetypeid AS fFeetypeid,
        corp.f_name AS fCorpid,
        agre.f_contractno AS fContractno,
        agre.f_begindate AS fBegindate,
        agre.f_enddate AS fEnddate,
        agre.create_by AS createBy,
        agre.create_time AS createTime,
        agre.remark AS remark
        from t_warehouse_agreement agre
        left join t_corps corp on corp.f_id = agre.f_corpid
        where
        agre.f_status = '0'
        and datediff(date_format(agre.f_enddate, '%Y-%m-%d'), date_format(now(), '%Y-%m-%d')) &lt;= #{remindDays}
        <if test="fCorpid != null ">and agre.f_corpid = #{fCorpid}</if>
        <if test="fContractno != null  and fContractno != ''">and agre.f_contractno = #{fContractno}</if>
        <if test="fBegindate != null ">and agre.f_begindate = #{fBegindate}</if>
        <if test="fEnddate != null ">and agre.f_enddate = #{fEnddate}</if>
    </select>

    <select id="biContractCount" resultType="map">
        select count(f_id) as contractCount
        from t_warehouse_agreement
        where f_feetypeid = 0
        <if test="beginDate != null and beginDate != ''">and f_enddate &gt;= #{beginDate}</if>
        <if test="endDate != null and endDate != ''">and f_enddate &lt; #{endDate}</if>
        <if test="external != null and external != ''">
            and f_corpid = (select f_pid from t_customer_contact where f_tel = #{external})
        </if>
        <if test="customerId != null">and f_corpid = #{customerId}</if>
    </select>

    <select id="biContractList" resultType="map">
        select
        ifnull(tc.f_cname, tc.f_name) as customerName,
        ta.f_begindate as beginDate,
        ta.f_enddate as endDate
        from t_warehouse_agreement ta left join t_corps tc on tc.f_id = ta.f_corpid
        where ta.f_feetypeid = 0
        <if test="external != null and external != ''">
            and ta.f_corpid = (select f_pid from t_customer_contact where f_tel = #{external})
        </if>
        <if test="customerId != null">and ta.f_corpid = #{customerId}</if>
        order by ta.create_time desc
    </select>

    <select id="selectTGoodsTypeByTypeId" resultType="com.ruoyi.basicData.domain.TGoods">
        select
            f_typeid as typeId,
            f_name as fName
        from
            t_goods
        WHERE
        f_typeid in
        <foreach collection="typeIds" item="typeId" open="(" separator="," close=")">
            #{typeId}
        </foreach>
    </select>

    <select id="selectTWarehouseByIds" resultType="com.ruoyi.common.core.domain.entity.TWarehouse">
        select
            f_id as fId,
            f_name as fName
        from
            t_warehouse
        WHERE
            f_id in
        <foreach collection="fIds" item="fId" open="(" separator="," close=")">
            #{fId}
        </foreach>
    </select>
</mapper>
