<?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.shipping.mapper.TWarehousebillsCntritemsMapper">
    
    <resultMap type="TWarehousebillsCntritems" id="TWarehousebillsCntritemsResult">
        <result property="fId"    column="f_id"    />
        <result property="fPid"    column="f_pid"    />
        <result property="fCntrno"    column="f_cntrno"    />
        <result property="fSealno"    column="f_sealno"    />
        <result property="fGoodsid"    column="f_goodsid"    />
        <result property="fPackageid"    column="f_packageid"    />
        <result property="fCntrid"    column="f_cntrid"    />
        <result property="fCntrcount"    column="f_cntrcount"    />
        <result property="fCntrweight"    column="f_cntrweight"    />
        <result property="fCntrstatus"    column="f_cntrstatus"    />
        <result property="fSoc"    column="f_soc"    />
        <result property="fTemperature"    column="f_temperature"    />
        <result property="fDraught"    column="f_Draught"    />
        <result property="fHumidity"    column="f_Humidity"    />
        <result property="fPrecooling"    column="f_precooling"    />
        <result property="fPlantakecntr"    column="f_plantakecntr"    />
        <result property="fIfdanger"    column="f_IFDANGER"    />
        <result property="fManual"    column="f_manual"    />
        <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="selectTWarehousebillsCntritemsVo">
        select f_id, f_pid, f_cntrno, f_sealno, f_goodsid, f_packageid, f_cntrid, f_cntrcount, f_cntrweight, f_cntrstatus, f_soc, f_temperature, f_Draught, f_Humidity, f_precooling, f_plantakecntr, f_IFDANGER,f_manual, create_by, create_time, update_by, update_time, remark from t_warehousebills_cntritems
    </sql>

    <select id="selectTWarehousebillsCntritemsList" parameterType="TWarehousebillsCntritems" resultMap="TWarehousebillsCntritemsResult">
        SELECT
        tw.f_id,
        tw.f_pid,
        tw.f_cntrno,
        tw.f_sealno,
        tw.f_goodsid,
        tg.f_name goodsName,
        tw.f_packageid,
        sd.dict_label packageName,
        tw.f_cntrid,
        tc.f_no cntrName,
        tw.f_cntrcount,
        tw.f_cntrweight,
        tw.f_cntrstatus,
        tw.f_soc,
        tw.f_temperature,
        tw.f_Draught,
        tw.f_Humidity,
        tw.f_precooling,
        si.dict_label precoolingName,
        tw.f_plantakecntr,
        tw.f_IFDANGER,
        sc.dict_label ifdangerName,
        tw.create_by,
        tw.create_time,
        tw.update_by,
        tw.update_time,
        tw.remark,
        tw.f_manual
        FROM
        t_warehousebills_cntritems tw
        LEFT JOIN t_goods tg ON tg.f_id = tw.f_goodsid
        LEFT JOIN sys_dict_data sd ON sd.dict_value = tw.f_packageid AND sd.dict_type = 'f_packageid'
        LEFT JOIN t_cntr tc ON tc.f_id = tw.f_cntrid
        LEFT JOIN sys_dict_data si ON si.dict_value = tw.f_precooling AND si.dict_type = 'f_precooling'
        LEFT JOIN sys_dict_data sc ON sc.dict_value = tw.f_IFDANGER AND sc.dict_type = 'f_IFDANGER'
        <where>  
            <if test="fPid != null "> and tw.f_pid = #{fPid}</if>
            <if test="fCntrno != null  and fCntrno != ''"> and tw.f_cntrno = #{fCntrno}</if>
            <if test="fSealno != null  and fSealno != ''"> and tw.f_sealno = #{fSealno}</if>
            <if test="fGoodsid != null "> and tw.f_goodsid = #{fGoodsid}</if>
            <if test="fPackageid != null  and fPackageid != ''"> and tw.f_packageid = #{fPackageid}</if>
            <if test="fCntrid != null "> and tw.f_cntrid = #{fCntrid}</if>
            <if test="fCntrcount != null "> and tw.f_cntrcount = #{fCntrcount}</if>
            <if test="fCntrweight != null "> and tw.f_cntrweight = #{fCntrweight}</if>
            <if test="fCntrstatus != null  and fCntrstatus != ''"> and tw.f_cntrstatus = #{fCntrstatus}</if>
            <if test="fSoc != null  and fSoc != ''"> and tw.f_soc = #{fSoc}</if>
            <if test="fTemperature != null  and fTemperature != ''"> and tw.f_temperature = #{fTemperature}</if>
            <if test="fDraught != null  and fDraught != ''"> and tw.f_Draught = #{fDraught}</if>
            <if test="fHumidity != null  and fHumidity != ''"> and tw.f_Humidity = #{fHumidity}</if>
            <if test="fPrecooling != null  and fPrecooling != ''"> and tw.f_precooling = #{fPrecooling}</if>
            <if test="fPlantakecntr != null "> and tw.f_plantakecntr = #{fPlantakecntr}</if>
            <if test="fIfdanger != null  and fIfdanger != ''"> and tw.f_IFDANGER = #{fIfdanger}</if>
        </where>
    </select>
    
    <select id="selectTWarehousebillsCntritemsById" parameterType="Long" resultMap="TWarehousebillsCntritemsResult">
        <include refid="selectTWarehousebillsCntritemsVo"/>
        where f_id = #{fId}
    </select>
    <select id="selectTWarehousebillsCntritemsByPId" parameterType="Long" resultMap="TWarehousebillsCntritemsResult">
        <include refid="selectTWarehousebillsCntritemsVo"/>
        where f_pid = #{fId}
    </select>
        
    <insert id="insertTWarehousebillsCntritems" parameterType="TWarehousebillsCntritems" useGeneratedKeys="true" keyProperty="fId">
        insert into t_warehousebills_cntritems
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="fPid != null">f_pid,</if>
            <if test="fCntrno != null and fCntrno != ''">f_cntrno,</if>
            <if test="fSealno != null and fSealno != ''">f_sealno,</if>
            <if test="fGoodsid != null">f_goodsid,</if>
            <if test="fPackageid != null and fPackageid != ''">f_packageid,</if>
            <if test="fCntrid != null">f_cntrid,</if>
            <if test="fCntrcount != null">f_cntrcount,</if>
            <if test="fCntrweight != null">f_cntrweight,</if>
            <if test="fCntrstatus != null">f_cntrstatus,</if>
            <if test="fSoc != null">f_soc,</if>
            <if test="fTemperature != null">f_temperature,</if>
            <if test="fDraught != null">f_Draught,</if>
            <if test="fHumidity != null">f_Humidity,</if>
            <if test="fPrecooling != null">f_precooling,</if>
            <if test="fPlantakecntr != null">f_plantakecntr,</if>
            <if test="fIfdanger != null">f_IFDANGER,</if>
            <if test="fManual != null">f_manual,</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="fCntrno != null and fCntrno != ''">#{fCntrno},</if>
            <if test="fSealno != null and fSealno != ''">#{fSealno},</if>
            <if test="fGoodsid != null">#{fGoodsid},</if>
            <if test="fPackageid != null and fPackageid != ''">#{fPackageid},</if>
            <if test="fCntrid != null">#{fCntrid},</if>
            <if test="fCntrcount != null">#{fCntrcount},</if>
            <if test="fCntrweight != null">#{fCntrweight},</if>
            <if test="fCntrstatus != null">#{fCntrstatus},</if>
            <if test="fSoc != null">#{fSoc},</if>
            <if test="fTemperature != null">#{fTemperature},</if>
            <if test="fDraught != null">#{fDraught},</if>
            <if test="fHumidity != null">#{fHumidity},</if>
            <if test="fPrecooling != null">#{fPrecooling},</if>
            <if test="fPlantakecntr != null">#{fPlantakecntr},</if>
            <if test="fIfdanger != null">#{fIfdanger},</if>
            <if test="fManual != null">#{fManual},</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="updateTWarehousebillsCntritems" parameterType="TWarehousebillsCntritems">
        update t_warehousebills_cntritems
        <trim prefix="SET" suffixOverrides=",">
            <if test="fPid != null">f_pid = #{fPid},</if>
            <if test="fCntrno != null and fCntrno != ''">f_cntrno = #{fCntrno},</if>
            <if test="fSealno != null and fSealno != ''">f_sealno = #{fSealno},</if>
            <if test="fGoodsid != null">f_goodsid = #{fGoodsid},</if>
            <if test="fPackageid != null and fPackageid != ''">f_packageid = #{fPackageid},</if>
            <if test="fCntrid != null">f_cntrid = #{fCntrid},</if>
            <if test="fCntrcount != null">f_cntrcount = #{fCntrcount},</if>
            <if test="fCntrweight != null">f_cntrweight = #{fCntrweight},</if>
            <if test="fCntrstatus != null">f_cntrstatus = #{fCntrstatus},</if>
            <if test="fSoc != null">f_soc = #{fSoc},</if>
            <if test="fTemperature != null">f_temperature = #{fTemperature},</if>
            <if test="fDraught != null">f_Draught = #{fDraught},</if>
            <if test="fHumidity != null">f_Humidity = #{fHumidity},</if>
            <if test="fPrecooling != null">f_precooling = #{fPrecooling},</if>
            <if test="fPlantakecntr != null">f_plantakecntr = #{fPlantakecntr},</if>
            <if test="fIfdanger != null">f_IFDANGER = #{fIfdanger},</if>
            <if test="fManual != null">f_manual = #{fManual},</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="deleteTWarehousebillsCntritemsById" parameterType="Long">
        delete from t_warehousebills_cntritems where f_id = #{fId}
    </delete>

    <delete id="deleteTWarehousebillsCntritemsByIds" parameterType="String">
        delete from t_warehousebills_cntritems where f_id in 
        <foreach item="fId" collection="array" open="(" separator="," close=")">
            #{fId}
        </foreach>
    </delete>
    <delete id="deleteTWarehousebillsCntritemsfPid" parameterType="Long">
        delete from t_warehousebills_cntritems where f_pid = #{fPid}
    </delete>

    <delete id="deleteTWarehousebillsCntritemsfPids" parameterType="String">
        delete from t_warehousebills_cntritems where f_pid in
        <foreach item="fPid" collection="array" open="(" separator="," close=")">
            #{fPid}
        </foreach>
    </delete>
    <select id="bookingSpace" resultType="map">
        SELECT
	CASE MONTH(tc.createTime) WHEN '1' THEN SUM(tc.number) ELSE 0 END AS January,
	 CASE MONTH(tc.createTime) WHEN '2' THEN SUM(tc.number) ELSE 0 END AS February,
	 CASE MONTH(tc.createTime) WHEN '3' THEN SUM(tc.number) ELSE 0 END AS March,
	 CASE MONTH(tc.createTime) WHEN '4' THEN SUM(tc.number) ELSE 0 END AS April,
	 CASE MONTH(tc.createTime) WHEN '5' THEN SUM(tc.number) ELSE 0 END AS May,
	 CASE MONTH(tc.createTime) WHEN '6' THEN SUM(tc.number) ELSE 0 END AS June,
	 CASE MONTH(tc.createTime) WHEN '7' THEN SUM(tc.number) ELSE 0 END AS July,
	 CASE MONTH(tc.createTime) WHEN '8' THEN SUM(tc.number) ELSE 0 END AS August,
	 CASE MONTH(tc.createTime) WHEN '9' THEN SUM(tc.number) ELSE 0 END AS September,
	 CASE MONTH(tc.createTime) WHEN '10' THEN SUM(tc.number) ELSE 0 END AS October,
	 CASE MONTH(tc.createTime) WHEN '11' THEN SUM(tc.number) ELSE 0 END AS November,
	 CASE MONTH(tc.createTime) WHEN '12' THEN SUM(tc.number) ELSE 0 END AS December,
	 tp.f_name corpName
FROM
(
SELECT
	tc.f_cntrno fCntrno,
	tc.create_time createTime,
	tw.f_corpid fCorpid,
	tn.f_no,
	CASE
	WHEN SUBSTR(tn.f_no,1,2) = '20' THEN 1
	WHEN  SUBSTR(tn.f_no,1,2) ='40' THEN 2
	ELSE 0
	END AS number
FROM
	t_warehousebills_cntritems tc
	LEFT JOIN t_warehousebills tw ON tc.f_pid = tw.f_id
	LEFT JOIN t_cntrno tr ON tc.f_cntrno = tr.f_no
	LEFT JOIN t_cntr tn ON tr.f_typeid = tn.f_id
	WHERE tw.f_corpid IS NOT NULL
	AND tn.f_no IS NOT NULL
	AND YEAR(tc.create_time) = YEAR(NOW())
	)tc
	LEFT JOIN t_corps tp ON tc.fCorpid = tp.f_id
	GROUP BY tc.fCorpid
	ORDER BY CONVERT(tp.f_name USING gbk) asc
	LIMIT 10
    </select>
    <delete id="deleteTWarehousebillsCntritemOlder" parameterType="object">
        delete from t_warehousebills_cntritems where f_pid = #{fPid} and f_cntrno = #{fCntrno}
    </delete>
    <select id="receipt" resultType="com.ruoyi.shipping.domain.JoabBoxVo">
        SELECT
	cn.f_cntrno AS containerNo,
	tw.f_mblno AS billOfLadingNo,
	'N' AS tradeFlag,
	tv.f_name AS vesselNameCn,
	tv.f_ename AS vesselNameEn,
	ty.f_no AS voyageNumber,
	LEFT ( tc.f_no, 2 ) AS containerSize,
	RIGHT ( tc.f_no, 2 ) AS containerType,
	'kh' AS containerUser,
    CASE WHEN ty.f_ATA IS NOT NULL THEN ty.f_ATA ELSE NOW()
    END AS arriveTime,
	'1' AS contState,
	'青岛凯和志诚物流有限公司' AS contEnterprise,
	'91370202MA3UG1T04A' AS contEnterpriseNo,
    CASE WHEN ty.f_ATD IS NOT NULL THEN ty.f_ATD ELSE NOW()
    END AS contTime,
	'青岛凯和志诚物流有限公司' AS useEnterprise,
	'91370202MA3UG1T04A' AS useEnterpriseNo,
	'QDYG' AS placeOfDeliveryCode,
	'大港' AS placeOfReturn,
	'DGX' AS placeOfReturnCode,
	'青岛凯和志诚物流有限公司' AS printEnterprise,
	'91370202MA3UG1T04A' AS printEnterpriseNo,
	NOW() AS uploadTime
FROM
	t_warehousebills_cntritems cn
	LEFT JOIN t_warehousebills_cntr cr ON cn.f_pid = cr.f_pid
	LEFT JOIN t_warehousebills tw ON cn.f_pid = tw.f_id
	LEFT JOIN t_vessel tv ON tw.f_vslid = tv.f_id
	LEFT JOIN t_voyage ty ON tw.f_voyid = ty.f_id
	LEFT JOIN t_cntr tc ON cr.f_cntrid = tc.f_id
	LEFT JOIN t_cntrno tn ON cn.f_cntrno = tn.f_no
        where cn.f_pid in
        <foreach item="fPid" collection="fPids" open="(" separator="," close=")">
            #{fPid}
        </foreach>
    </select>
    
</mapper>