<?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.boco.nbd.wios.manage.mapper.def.WallboxApplyMapper">

    <resultMap type="com.boco.nbd.wios.manage.entity.bo.WallboxApply" id="WallboxApplyResult">
        <result property="id" column="id"/>
        <result property="orderId" column="order_id"/>
        <result property="supplierId" column="supplier_id"/>
        <result property="supplierTeam" column="supplier_team"/>
        <result property="wallboxItemId" column="wallbox_item_id"/>
        <result property="wallboxModel" column="wallbox_model"/>
        <result property="materialType" column="material_type"/>
        <result property="trackingNumber" column="tracking_number"/>
        <result property="trackingStatus" column="tracking_status"/>
        <result property="installStatus" column="install_status"/>
        <result property="regionId" column="region_id"/>
        <result property="address" column="address"/>
        <result property="outOrderId" column="out_order_id"/>
        <result property="ifEmergency" column="if_emergency"/>
        <result property="ifTimeout" column="if_timeout"/>
        <result property="checkAccount" column="check_account"/>
        <result property="checkStatus" column="check_status"/>
        <result property="checkRemarks" column="check_remarks"/>
        <result property="checkTime" column="check_time"/>
        <result property="installReserveTime" column="install_reserve_time"/>
        <result property="installProcess" column="install_process"/>
        <result property="createAccount" column="create_account"/>
        <result property="createdTime" column="created_time"/>
        <result property="modifyAccount" column="modify_account"/>
        <result property="modifyTime" column="modify_time"/>
    </resultMap>

    <sql id="selectWallboxApplyVo">
        select id,
               order_id,
               supplier_id,
               supplier_team,
               wallbox_item_id,
               wallbox_model,
               material_type,
               tracking_number,
               tracking_status,
               install_status,
               region_id,
               address,
               out_order_id,
               if_emergency,
               if_timeout,
               check_account,
               check_status,
               check_remarks,
               check_time,
               install_reserve_time,
               install_process,
               create_account,
               created_time,
               modify_account,
               modify_time
        from t_wallbox_apply
    </sql>

    <select id="selectWallboxApplyList" parameterType="com.boco.nbd.wios.manage.entity.bo.WallboxApply"
            resultMap="WallboxApplyResult">
        <include refid="selectWallboxApplyVo"/>
        <where>
            <if test="orderId != null  and orderId != ''">and order_id = #{orderId}</if>
            <if test="supplierId != null ">and supplier_id = #{supplierId}</if>
            <if test="supplierTeam != null  and supplierTeam != ''">and supplier_team like concat('%', #{supplierTeam},
                '%')
            </if>
            <if test="wallboxItemId != null  and wallboxItemId != ''">and wallbox_item_id = #{wallboxItemId}</if>
            <if test="wallboxModel != null  and wallboxModel != ''">and wallbox_model = #{wallboxModel}</if>
            <if test="materialType != null  and materialType != ''">and material_type = #{materialType}</if>
            <if test="trackingNumber != null  and trackingNumber != ''">and tracking_number = #{trackingNumber}</if>
            <if test="trackingStatus != null  and trackingStatus != ''">and tracking_status = #{trackingStatus}</if>
            <if test="installStatus != null  and installStatus != ''">and install_status = #{installStatus}</if>
            <if test="regionId != null  and regionId != ''">and region_id = #{regionId}</if>
            <if test="address != null  and address != ''">and address = #{address}</if>
            <if test="outOrderId != null  and outOrderId != ''">and out_order_id = #{outOrderId}</if>
            <if test="ifEmergency != null ">and if_emergency = #{ifEmergency}</if>
            <if test="ifTimeout != null ">and if_timeout = #{ifTimeout}</if>
            <if test="checkAccount != null  and checkAccount != ''">and check_account = #{checkAccount}</if>
            <if test="checkStatus != null ">and check_status = #{checkStatus}</if>
            <if test="checkRemarks != null  and checkRemarks != ''">and check_remarks = #{checkRemarks}</if>
            <if test="checkTime != null ">and check_time = #{checkTime}</if>
            <if test="installReserveTime != null ">and install_reserve_time = #{installReserveTime}</if>
            <if test="installProcess != null ">and install_process = #{installProcess}</if>
            <if test="createAccount != null  and createAccount != ''">and create_account = #{createAccount}</if>
            <if test="createdTime != null ">and created_time = #{createdTime}</if>
            <if test="modifyAccount != null  and modifyAccount != ''">and modify_account = #{modifyAccount}</if>
            <if test="modifyTime != null ">and modify_time = #{modifyTime}</if>
        </where>
    </select>

    <select id="selectWallboxApplyListFromOrder" parameterType="com.boco.nbd.wios.manage.entity.vo.WallboxApplyReqVO"
            resultType="com.boco.nbd.wios.manage.entity.vo.WallboxApplyInstallListVO">
        SELECT t_wallbox_apply.id,
        t_wallbox_apply.order_id,
        t_wallbox_apply.supplier_id,
        t_wallbox_apply.supplier_team,
        t_wallbox_apply.wallbox_item_id,
        t_wallbox_apply.wallbox_model,
        t_wallbox_apply.material_type,
        t_wallbox_apply.tracking_status,
        case
        when t_wallbox_apply.tracking_status = 0 then '未发货'
        when t_wallbox_apply.tracking_status = 1 then '已发货'
        else null end trackingStatusName,
        t_order.region_id,
        reg.name as region_name,
        t_order.address,
        t_wallbox_apply.out_order_id,
        t_wallbox_apply.if_emergency,
        case
            when t_wallbox_apply.if_emergency = 1 then '是'
            when t_wallbox_apply.if_emergency = 0 then '否'
                else null end ifEmergencyName,
        t_wallbox_apply.if_timeout,
        t_wallbox_apply.check_account,
        t_wallbox_apply.check_status,
        case
        when t_wallbox_apply.check_status = 1 then '已申请'
        when t_wallbox_apply.check_status = 2 then '已通过'
        when t_wallbox_apply.check_status = 3 then '已驳回'
        when t_wallbox_apply.check_status = 4 then '已关闭'
        else null end checkStatusName,
        t_wallbox_apply.check_remarks,
        t_wallbox_apply.check_time,
        t_wallbox_apply.install_reserve_time,
        t_wallbox_apply.install_process,
        t_wallbox_apply.create_account,
        t_wallbox_apply.created_time,
        t_wallbox_apply.modify_account,
        t_wallbox_apply.modify_time,
        t_wallbox_apply.area_name,
        t_order.shipno as tracking_number,
        t_order.status as install_status,
        td.logistic_order deliveryLogisticOrder,td.logistic_company logisticCompany,td.sender sender,td.sender_phone senderPhone,td.receiver receiver,
        tc.name wallboxModelName
        FROM t_wallbox_apply
        LEFT JOIN t_order ON t_wallbox_apply.order_id = t_order.id
        left join t_region reg on reg.id=t_wallbox_apply.region_id
        left join t_delivery td on td.install_order = t_wallbox_apply.order_id
        left join t_oem_cascade tc on tc.id = t_wallbox_apply.wallbox_model
        <where>
            <if test="supplierTeam != null  and supplierTeam != ''">and t_wallbox_apply.supplier_team like concat('%',
                #{supplierTeam},
                '%')
            </if>
            <if test="checkStatus != null ">and t_wallbox_apply.check_status = #{checkStatus}</if>
            <if test="supplierId != null and regionId != ''">and t_wallbox_apply.supplier_id = #{supplierId}</if>
            <if test="trackingStatus != null ">and t_wallbox_apply.tracking_status = #{trackingStatus}</if>
            <if test="regionId != null  and regionId != ''">and t_wallbox_apply.region_id = #{regionId}</if>
            <if test="wallboxItemId != null  and wallboxItemId != ''">and t_wallbox_apply.wallbox_item_id like
                concat('%', #{wallboxItemId}, '%')
            </if>
        </where>
      group by t_wallbox_apply.order_id
    </select>

    <select id="selectWallboxApplyById" parameterType="Long"
            resultType="com.boco.nbd.wios.manage.entity.vo.WallboxApplyInstallInfoVO">
        SELECT t_wallbox_apply.id,
               t_wallbox_apply.order_id,
               t_wallbox_apply.supplier_id,
               t_wallbox_apply.supplier_team,
               t_order.wallbox_model,
               t_order.item_model as material_type,
               t_wallbox_apply.tracking_status,
               t_order.region_id,
               reg.name           as region_name,
               t_order.install_reserve_time,
               t_order.address,
               t_order.out_order_id,
               t_wallbox_apply.if_emergency,
               t_wallbox_apply.if_timeout,
               t_wallbox_apply.check_account,
               wb_account.name    as check_name,
               t_wallbox_apply.check_status,
               t_wallbox_apply.check_remarks,
               t_wallbox_apply.check_time,
               t_order.install_reserve_time,
               t_wallbox_apply.create_account,
               t_wallbox_apply.created_time,
               t_wallbox_apply.modify_account,
               t_wallbox_apply.modify_time,
               t_order.shipno     as tracking_number,
               t_order.status     as install_status,
               tc.name as wallboxModelName
        FROM t_wallbox_apply
                 LEFT JOIN t_order ON t_wallbox_apply.order_id = t_order.id
                 left join t_region reg on reg.id = t_wallbox_apply.region_id
                 left join wb_account on wb_account.id = t_wallbox_apply.check_account
                 left join t_oem_cascade tc on tc.id = t_wallbox_apply.wallbox_model
        where t_wallbox_apply.id = #{id}
    </select>

    <select id="selectWallboxDetail" parameterType="com.boco.nbd.wios.manage.entity.dto.WallboxApplyDetailQueryDTO"
            resultType="com.boco.nbd.wios.manage.entity.vo.WallboxApplyDetailVO">
        SELECT t_wallbox_apply.id,
               t_wallbox_apply.order_id,
               t_wallbox_apply.supplier_team,
               t_wallbox_apply.tracking_status,
               t_wallbox_apply.tracking_number,
               t_wallbox_apply.supplier_id,
               t_wallbox_apply.wallbox_item_id as wallbox_code,
               t_wallbox_apply.if_emergency,
               t_wallbox_apply.if_timeout,
               t_wallbox_apply.check_account,
               t_wallbox_apply.check_status,
               t_wallbox_apply.check_remarks,
               t_wallbox_apply.check_time,
               t_wallbox_apply.create_account,
               t_wallbox_apply.created_time,
               t_wallbox_apply.modify_account,
               t_wallbox_apply.modify_time,
               wb_account.name                 as checkName,
               t_order.user_name,
               t_order.user_phone,
               t_order.wallbox_model,
               t_order.item_model,
               t_order.region_level,
               t_order.province_name,
               t_order.area_name,
               t_order.country_name,
               t_order.warehouse_id,
               t_order.address,
               t_order.out_order_id,
               t_order.install_finish_time,
               t_order.shipno                  as tracking_number

        FROM t_order
                 LEFT JOIN t_wallbox_apply ON t_wallbox_apply.order_id = t_order.id
                 left join wb_account on t_wallbox_apply.check_account = wb_account.id
        where t_wallbox_apply.order_id = #{orderId}
    </select>


    <select id="selectWallboxApplyByOrderId" parameterType="String" resultMap="WallboxApplyResult">
        <include refid="selectWallboxApplyVo"/>
        where order_id = #{id}
    </select>


    <insert id="insertWallboxApply" parameterType="com.boco.nbd.wios.manage.entity.bo.WallboxApply"
            useGeneratedKeys="true"
            keyProperty="id">
        insert into t_wallbox_apply
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderId != null and orderId != ''">order_id,</if>
            <if test="supplierId != null">supplier_id,</if>
            <if test="supplierTeam != null">supplier_team,</if>
            <if test="wallboxItemId != null and wallboxItemId != ''">wallbox_item_id,</if>
            <if test="wallboxModel != null">wallbox_model,</if>
            <if test="materialType != null">material_type,</if>
            <if test="trackingNumber != null">tracking_number,</if>
            <if test="trackingStatus != null">tracking_status,</if>
            <if test="installStatus != null">install_status,</if>
            <if test="regionId != null">region_id,</if>
            <if test="address != null">address,</if>
            <if test="outOrderId != null">out_order_id,</if>
            <if test="ifEmergency != null">if_emergency,</if>
            <if test="ifTimeout != null">if_timeout,</if>
            <if test="checkAccount != null">check_account,</if>
            <if test="checkStatus != null">check_status,</if>
            <if test="checkRemarks != null">check_remarks,</if>
            <if test="checkTime != null">check_time,</if>
            <if test="installReserveTime != null">install_reserve_time,</if>
            <if test="installProcess != null">install_process,</if>
            <if test="createAccount != null">create_account,</if>
            <if test="createdTime != null">created_time,</if>
            <if test="modifyAccount != null">modify_account,</if>
            <if test="modifyTime != null">modify_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderId != null and orderId != ''">#{orderId},</if>
            <if test="supplierId != null">#{supplierId},</if>
            <if test="supplierTeam != null">#{supplierTeam},</if>
            <if test="wallboxItemId != null and wallboxItemId != ''">#{wallboxItemId},</if>
            <if test="wallboxModel != null">#{wallboxModel},</if>
            <if test="materialType != null">#{materialType},</if>
            <if test="trackingNumber != null">#{trackingNumber},</if>
            <if test="trackingStatus != null">#{trackingStatus},</if>
            <if test="installStatus != null">#{installStatus},</if>
            <if test="regionId != null">#{regionId},</if>
            <if test="address != null">#{address},</if>
            <if test="outOrderId != null">#{outOrderId},</if>
            <if test="ifEmergency != null">#{ifEmergency},</if>
            <if test="ifTimeout != null">#{ifTimeout},</if>
            <if test="checkAccount != null">#{checkAccount},</if>
            <if test="checkStatus != null">#{checkStatus},</if>
            <if test="checkRemarks != null">#{checkRemarks},</if>
            <if test="checkTime != null">#{checkTime},</if>
            <if test="installReserveTime != null">#{installReserveTime},</if>
            <if test="installProcess != null">#{installProcess},</if>
            <if test="createAccount != null">#{createAccount},</if>
            <if test="createdTime != null">#{createdTime},</if>
            <if test="modifyAccount != null">#{modifyAccount},</if>
            <if test="modifyTime != null">#{modifyTime},</if>
        </trim>
    </insert>

    <update id="updateWallboxApply" parameterType="com.boco.nbd.wios.manage.entity.bo.WallboxApply">
        update t_wallbox_apply
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderId != null and orderId != ''">order_id = #{orderId},</if>
            <if test="supplierId != null">supplier_id = #{supplierId},</if>
            <if test="supplierTeam != null">supplier_team = #{supplierTeam},</if>
            <if test="wallboxItemId != null and wallboxItemId != ''">wallbox_item_id = #{wallboxItemId},</if>
            <if test="wallboxModel != null">wallbox_model = #{wallboxModel},</if>
            <if test="materialType != null">material_type = #{materialType},</if>
            <if test="trackingNumber != null">tracking_number = #{trackingNumber},</if>
            <if test="trackingStatus != null">tracking_status = #{trackingStatus},</if>
            <if test="installStatus != null">install_status = #{installStatus},</if>
            <if test="regionId != null">region_id = #{regionId},</if>
            <if test="address != null">address = #{address},</if>
            <if test="outOrderId != null">out_order_id = #{outOrderId},</if>
            <if test="ifEmergency != null">if_emergency = #{ifEmergency},</if>
            <if test="ifTimeout != null">if_timeout = #{ifTimeout},</if>
            <if test="checkAccount != null">check_account = #{checkAccount},</if>
            <if test="checkStatus != null">check_status = #{checkStatus},</if>
            <if test="checkRemarks != null">check_remarks = #{checkRemarks},</if>
            <if test="checkTime != null">check_time = #{checkTime},</if>
            <if test="installReserveTime != null">install_reserve_time = #{installReserveTime},</if>
            <if test="installProcess != null">install_process = #{installProcess},</if>
            <if test="createAccount != null">create_account = #{createAccount},</if>
            <if test="createdTime != null">created_time = #{createdTime},</if>
            <if test="modifyAccount != null">modify_account = #{modifyAccount},</if>
            <if test="modifyTime != null">modify_time = #{modifyTime},</if>
        </trim>
        where id = #{id}
    </update>

    <!-- 批量更新 WallboxApply -->
    <update id="batchUpdateWallboxApply" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" separator=";">
            UPDATE t_wallbox_apply
            SET check_status = #{item.checkStatus},
            check_remarks = #{item.checkRemarks},
            modify_account = #{item.modifyAccount},
            modify_time = CURRENT_TIMESTAMP
            WHERE id = #{item.id}
        </foreach>
    </update>


    <delete id="deleteWallboxApplyById" parameterType="Long">
        delete
        from t_wallbox_apply
        where id = #{id}
    </delete>

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