<?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.sundot.airport.item.mapper.ItemSeizureItemsMapper">

    <resultMap type="ItemSeizureItems" id="ItemSeizureItemsResult">
        <result property="tenantId" column="tenant_id"/>
        <result property="revision" column="revision"/>
        <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="id" column="id"/>
        <result property="recordId" column="record_id"/>
        <result property="itemCode" column="item_code"/>
        <result property="quantity" column="quantity"/>
        <result property="remark" column="remark"/>
        <result property="checkPositionCodeOne" column="check_position_code_one"/>
        <result property="isActiveConcealment" column="is_active_concealment"/>
        <result property="checkPositionCodeTwo" column="check_position_code_two"/>
        <result property="checkPositionSpecific" column="check_position_specific"/>
        <result property="itemName" column="item_name"/>
        <result property="categoryCodeOne" column="category_code_one"/>
        <result property="categoryNameOne" column="category_name_one"/>
        <result property="categoryCodeTwo" column="category_code_two"/>
        <result property="categoryNameTwo" column="category_name_two"/>
        <result property="unit" column="unit"/>
        <result property="checkPositionNameOne" column="check_position_name_one"/>
        <result property="checkPositionNameTwo" column="check_position_name_two"/>
        <result property="unitDesc" column="unit_desc"/>
        <result property="handlingMethod" column="handling_method"/>
        <result property="handlingMethodDesc" column="handling_method_desc"/>
        <result property="attachmentId" column="attachment_id"/>
        <result property="attachmentUrl" column="attachment_url"/>
        <result property="attachmentName" column="attachment_name"/>
        <result property="commonContraband" column="common_contraband"/>
        <result property="contrabandDesc" column="contraband_desc"/>
        <result property="location" column="location"/>
        <result property="locationText" column="location_text"/>
    </resultMap>

    <sql id="selectItemSeizureItemsVo">
        select tenant_id,
               revision,
               create_by,
               create_time,
               update_by,
               update_time,
               id,
               record_id,
               item_code,
               quantity,
               remark,
               check_position_code_one,
               is_active_concealment,
               check_position_code_two,
               check_position_specific,
               item_name,
               category_code_one,
               category_name_one,
               category_code_two,
               category_name_two,
               unit,
               check_position_name_one,
               check_position_name_two,
               unit_desc,
               handling_method,
               handling_method_desc,
               attachment_id,
               attachment_url,
               attachment_name,
               common_contraband,
               contraband_desc,
               location,
               location_text
        from item_seizure_items
    </sql>

    <select id="selectItemSeizureItemsList" parameterType="ItemSeizureItems" resultMap="ItemSeizureItemsResult">
        <include refid="selectItemSeizureItemsVo"/>
        <where>
            <if test="tenantId != null  and tenantId != ''">and tenant_id = #{tenantId}</if>
            <if test="revision != null ">and revision = #{revision}</if>
            <if test="recordId != null ">and record_id = #{recordId}</if>
            <if test="itemCode != null  and itemCode != ''">and item_code = #{itemCode}</if>
            <if test="quantity != null ">and quantity = #{quantity}</if>
            <if test="checkPositionCodeOne != null  and checkPositionCodeOne != ''">and check_position_code_one =
                #{checkPositionCodeOne}
            </if>
            <if test="isActiveConcealment != null ">and is_active_concealment = #{isActiveConcealment}</if>
            <if test="checkPositionCodeTwo != null  and checkPositionCodeTwo != ''">and check_position_code_two =
                #{checkPositionCodeTwo}
            </if>
            <if test="checkPositionSpecific != null  and checkPositionSpecific != ''">and check_position_specific =
                #{checkPositionSpecific}
            </if>
            <if test="itemName != null  and itemName != ''">and item_name like concat('%', #{itemName}, '%')</if>
            <if test="categoryCodeOne != null  and categoryCodeOne != ''">and category_code_one = #{categoryCodeOne}
            </if>
            <if test="categoryNameOne != null  and categoryNameOne != ''">and category_name_one = #{categoryNameOne}
            </if>
            <if test="categoryCodeTwo != null  and categoryCodeTwo != ''">and category_code_two = #{categoryCodeTwo}
            </if>
            <if test="categoryNameTwo != null  and categoryNameTwo != ''">and category_name_two = #{categoryNameTwo}
            </if>
            <if test="unit != null  and unit != ''">and unit = #{unit}</if>
            <if test="checkPositionNameOne != null  and checkPositionNameOne != ''">and check_position_name_one =
                #{checkPositionNameOne}
            </if>
            <if test="checkPositionNameTwo != null  and checkPositionNameTwo != ''">and check_position_name_two =
                #{checkPositionNameTwo}
            </if>
            <if test="unitDesc != null  and unitDesc != ''">and unit_desc = #{unitDesc}</if>
            <if test="handlingMethod != null  and handlingMethod != ''">and handling_method = #{handlingMethod}</if>
            <if test="handlingMethodDesc != null  and handlingMethodDesc != ''">and handling_method_desc =
                #{handlingMethodDesc}
            </if>
            <if test="attachmentId != null  and attachmentId != ''">and attachment_id = #{attachmentId}</if>
            <if test="attachmentUrl != null  and attachmentUrl != ''">and attachment_url = #{attachmentUrl}</if>
            <if test="attachmentName != null  and attachmentName != ''">and attachment_name like concat('%',
                #{attachmentName}, '%')
            </if>
            <if test="commonContraband != null  and commonContraband != ''">and common_contraband =
                #{commonContraband}
            </if>
            <if test="contrabandDesc != null  and contrabandDesc != ''">and contraband_desc like
                concat('%', #{contrabandDesc}, '%')
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="selectItemSeizureItemsById" parameterType="Long" resultMap="ItemSeizureItemsResult">
        <include refid="selectItemSeizureItemsVo"/>
        where id = #{id}
    </select>

    <select id="selectItemSeizureItemsByRecordIds" parameterType="java.util.List" resultMap="ItemSeizureItemsResult">
        <include refid="selectItemSeizureItemsVo"/>
        WHERE record_id IN
        <foreach item="recordId" collection="recordIds" open="(" separator="," close=")">
            #{recordId}
        </foreach>
        order by create_time desc
    </select>

    <insert id="insertItemSeizureItems" parameterType="ItemSeizureItems">
        insert into item_seizure_items
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="tenantId != null">tenant_id,</if>
            <if test="revision != null">revision,</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="id != null">id,</if>
            <if test="recordId != null">record_id,</if>
            <if test="itemCode != null and itemCode != ''">item_code,</if>
            <if test="quantity != null">quantity,</if>
            <if test="remark != null">remark,</if>
            <if test="checkPositionCodeOne != null and checkPositionCodeOne != ''">check_position_code_one,</if>
            <if test="isActiveConcealment != null">is_active_concealment,</if>
            <if test="checkPositionCodeTwo != null and checkPositionCodeTwo != ''">check_position_code_two,</if>
            <if test="checkPositionSpecific != null and checkPositionSpecific != ''">check_position_specific,</if>
            <if test="itemName != null and itemName != ''">item_name,</if>
            <if test="categoryCodeOne != null and categoryCodeOne != ''">category_code_one,</if>
            <if test="categoryNameOne != null and categoryNameOne != ''">category_name_one,</if>
            <if test="categoryCodeTwo != null and categoryCodeTwo != ''">category_code_two,</if>
            <if test="categoryNameTwo != null and categoryNameTwo != ''">category_name_two,</if>
            <if test="unit != null and unit != ''">unit,</if>
            <if test="checkPositionNameOne != null and checkPositionNameOne != ''">check_position_name_one,</if>
            <if test="checkPositionNameTwo != null and checkPositionNameTwo != ''">check_position_name_two,</if>
            <if test="unitDesc != null and unitDesc != ''">unit_desc,</if>
            <if test="handlingMethod != null and handlingMethod != ''">handling_method,</if>
            <if test="handlingMethodDesc != null and handlingMethodDesc != ''">handling_method_desc,</if>
            <if test="attachmentId != null">attachment_id,</if>
            <if test="attachmentUrl != null">attachment_url,</if>
            <if test="attachmentName != null">attachment_name,</if>
            <if test="commonContraband != null">common_contraband,</if>
            <if test="contrabandDesc != null">contraband_desc,</if>
            <if test="location != null">location,</if>
            <if test="locationText != null">location_text,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="tenantId != null">#{tenantId},</if>
            <if test="revision != null">#{revision},</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="id != null">#{id},</if>
            <if test="recordId != null">#{recordId},</if>
            <if test="itemCode != null and itemCode != ''">#{itemCode},</if>
            <if test="quantity != null">#{quantity},</if>
            <if test="remark != null">#{remark},</if>
            <if test="checkPositionCodeOne != null and checkPositionCodeOne != ''">#{checkPositionCodeOne},</if>
            <if test="isActiveConcealment != null">#{isActiveConcealment},</if>
            <if test="checkPositionCodeTwo != null and checkPositionCodeTwo != ''">#{checkPositionCodeTwo},</if>
            <if test="checkPositionSpecific != null and checkPositionSpecific != ''">#{checkPositionSpecific},</if>
            <if test="itemName != null and itemName != ''">#{itemName},</if>
            <if test="categoryCodeOne != null and categoryCodeOne != ''">#{categoryCodeOne},</if>
            <if test="categoryNameOne != null and categoryNameOne != ''">#{categoryNameOne},</if>
            <if test="categoryCodeTwo != null and categoryCodeTwo != ''">#{categoryCodeTwo},</if>
            <if test="categoryNameTwo != null and categoryNameTwo != ''">#{categoryNameTwo},</if>
            <if test="unit != null and unit != ''">#{unit},</if>
            <if test="checkPositionNameOne != null and checkPositionNameOne != ''">#{checkPositionNameOne},</if>
            <if test="checkPositionNameTwo != null and checkPositionNameTwo != ''">#{checkPositionNameTwo},</if>
            <if test="unitDesc != null and unitDesc != ''">#{unitDesc},</if>
            <if test="handlingMethod != null and handlingMethod != ''">#{handlingMethod},</if>
            <if test="handlingMethodDesc != null and handlingMethodDesc != ''">#{handlingMethodDesc},</if>
            <if test="attachmentId != null">#{attachmentId},</if>
            <if test="attachmentUrl != null">#{attachmentUrl},</if>
            <if test="attachmentName != null">#{attachmentName},</if>
            <if test="commonContraband != null">#{commonContraband},</if>
            <if test="contrabandDesc != null">#{contrabandDesc},</if>
            <if test="location != null">#{location},</if>
            <if test="locationText != null">#{locationText},</if>
        </trim>
    </insert>

    <update id="updateItemSeizureItems" parameterType="ItemSeizureItems">
        update item_seizure_items
        <trim prefix="SET" suffixOverrides=",">
            <if test="tenantId != null">tenant_id = #{tenantId},</if>
            <if test="revision != null">revision = #{revision},</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="recordId != null">record_id = #{recordId},</if>
            <if test="itemCode != null and itemCode != ''">item_code = #{itemCode},</if>
            <if test="quantity != null">quantity = #{quantity},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="checkPositionCodeOne != null and checkPositionCodeOne != ''">check_position_code_one =
                #{checkPositionCodeOne},
            </if>
            <if test="isActiveConcealment != null">is_active_concealment = #{isActiveConcealment},</if>
            <if test="checkPositionCodeTwo != null and checkPositionCodeTwo != ''">check_position_code_two =
                #{checkPositionCodeTwo},
            </if>
            <if test="checkPositionSpecific != null and checkPositionSpecific != ''">check_position_specific =
                #{checkPositionSpecific},
            </if>
            <if test="itemName != null and itemName != ''">item_name = #{itemName},</if>
            <if test="categoryCodeOne != null and categoryCodeOne != ''">category_code_one = #{categoryCodeOne},</if>
            <if test="categoryNameOne != null and categoryNameOne != ''">category_name_one = #{categoryNameOne},</if>
            <if test="categoryCodeTwo != null and categoryCodeTwo != ''">category_code_two = #{categoryCodeTwo},</if>
            <if test="categoryNameTwo != null and categoryNameTwo != ''">category_name_two = #{categoryNameTwo},</if>
            <if test="unit != null and unit != ''">unit = #{unit},</if>
            <if test="checkPositionNameOne != null and checkPositionNameOne != ''">check_position_name_one =
                #{checkPositionNameOne},
            </if>
            <if test="checkPositionNameTwo != null and checkPositionNameTwo != ''">check_position_name_two =
                #{checkPositionNameTwo},
            </if>
            <if test="unitDesc != null and unitDesc != ''">unit_desc = #{unitDesc},</if>
            <if test="handlingMethod != null and handlingMethod != ''">handling_method = #{handlingMethod},</if>
            <if test="handlingMethodDesc != null and handlingMethodDesc != ''">handling_method_desc =
                #{handlingMethodDesc},
            </if>
            <if test="attachmentId != null">attachment_id = #{attachmentId},</if>
            <if test="attachmentUrl != null">attachment_url = #{attachmentUrl},</if>
            <if test="attachmentName != null">attachment_name = #{attachmentName},</if>
            <if test="commonContraband != null">common_contraband = #{commonContraband},</if>
            <if test="contrabandDesc != null">contraband_desc = #{contrabandDesc},</if>
            <if test="location != null">location = #{location},</if>
            <if test="locationText != null">location_text = #{locationText},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteItemSeizureItemsById" parameterType="Long">
        delete
        from item_seizure_items
        where id = #{id}
    </delete>

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

    <select id="selectByConditions" resultType="com.sundot.airport.item.domain.ItemSeizureDto">
        select isr.*, isi.*
        from item_seizure_record isr
        left join item_seizure_items isi on isi.record_id = isr.id
        where 1 = 1
        <if test="inspectUserId != null">and isr.inspect_user_id = #{inspectUserId}</if>
        <if test="inspectStationId != null">and isr.inspect_station_id = #{inspectStationId}</if>
        <if test="inspectBrigadeId != null">and isr.inspect_brigade_id = #{inspectBrigadeId}</if>
        <if test="inspectDepartmentId != null">and isr.inspect_department_id = #{inspectDepartmentId}</if>
        <if test="inspectTeamId != null">and isr.inspect_team_id = #{inspectTeamId}</if>
        <!-- 时间范围 -->
        <choose>
            <!-- 查询当天数据 -->
            <when test="timeType == 1">
                AND isr.seizure_time >= CURDATE()
                AND isr.seizure_time <![CDATA[ < ]]> DATE_ADD(CURDATE(), INTERVAL 1 DAY)
            </when>
            <!-- 查询本周数据 -->
            <when test="timeType == 2">
                AND isr.seizure_time >= DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY)
                AND isr.seizure_time <![CDATA[ < ]]>DATE_ADD(DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY),
                INTERVAL 7 DAY)
            </when>
            <!-- 查询本月数据 -->
            <when test="timeType == 3">
                AND isr.seizure_time >= DATE_FORMAT(CURDATE(), '%Y-%m-01')
                AND isr.seizure_time <![CDATA[ < ]]>DATE_ADD(DATE_FORMAT(CURDATE(), '%Y-%m-01'), INTERVAL 1 MONTH)
            </when>
            <!-- 自定义日期范围 -->
            <when test="timeType == 4 and startDate != null and endDate != null">
                AND isr.seizure_time BETWEEN #{startDate} AND #{endDate}
            </when>
        </choose>
        <!-- 分类类型 -->
        <choose>
            <when test="seizureType != null and seizureType != '' and seizureType != 'ALL'">
                and isi.category_code_two = #{seizureType}
            </when>
        </choose>
        order by isr.seizure_time desc
    </select>

    <select id="selectGroupCount" resultType="com.sundot.airport.item.domain.ItemSeizureCount">
        select isi.category_code_two categoryCode, isi.category_name_two categoryName, sum(isi.quantity) categoryCount
        from item_seizure_record isr
        left join item_seizure_items isi on isi.record_id = isr.id
        where 1 = 1
        <if test="inspectUserId != null">and isr.inspect_user_id = #{inspectUserId}</if>
        <if test="inspectStationId != null">and isr.inspect_station_id = #{inspectStationId}</if>
        <if test="inspectBrigadeId != null">and isr.inspect_brigade_id = #{inspectBrigadeId}</if>
        <if test="inspectDepartmentId != null">and isr.inspect_department_id = #{inspectDepartmentId}</if>
        <if test="inspectTeamId != null">and isr.inspect_team_id = #{inspectTeamId}</if>
        <!-- 时间范围 -->
        <choose>
            <!-- 查询当天数据 -->
            <when test="timeType == 1">
                AND isr.seizure_time >= CURDATE()
                AND isr.seizure_time <![CDATA[ < ]]> DATE_ADD(CURDATE(), INTERVAL 1 DAY)
            </when>
            <!-- 查询本周数据 -->
            <when test="timeType == 2">
                AND isr.seizure_time >= DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY)
                AND isr.seizure_time <![CDATA[ < ]]>DATE_ADD(DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY),
                INTERVAL 7 DAY)
            </when>
            <!-- 查询本月数据 -->
            <when test="timeType == 3">
                AND isr.seizure_time >= DATE_FORMAT(CURDATE(), '%Y-%m-01')
                AND isr.seizure_time <![CDATA[ < ]]>DATE_ADD(DATE_FORMAT(CURDATE(), '%Y-%m-01'), INTERVAL 1 MONTH)
            </when>
            <!-- 自定义日期范围 -->
            <when test="timeType == 4 and startDate != null and endDate != null">
                AND isr.seizure_time BETWEEN #{startDate} AND #{endDate}
            </when>
        </choose>
        <!-- 分类类型 -->
        <choose>
            <when test="seizureType != null and seizureType != '' and seizureType != 'ALL'">
                and isi.category_code_two = #{seizureType}
            </when>
        </choose>
        group by isi.category_code_two, isi.category_name_two
    </select>

    <select id="selectCount" resultType="com.sundot.airport.item.domain.ItemSeizureTotal">
        select ifnull(sum(isi.quantity), 0) total,
        ifnull(sum(case when isi.handling_method = 'TRANSFER_TO_AIRPORT_POLICE' then isi.quantity else 0 end), 0) as
        policeTotal,
        ifnull(sum(case when isi.is_active_concealment = 1 then isi.quantity else 0 end), 0) as concealTotal
        from item_seizure_record isr
        left join item_seizure_items isi on isi.record_id = isr.id
        where 1 = 1
        <if test="inspectUserId != null">and isr.inspect_user_id = #{inspectUserId}</if>
        <if test="inspectStationId != null">and isr.inspect_station_id = #{inspectStationId}</if>
        <if test="inspectBrigadeId != null">and isr.inspect_brigade_id = #{inspectBrigadeId}</if>
        <if test="inspectDepartmentId != null">and isr.inspect_department_id = #{inspectDepartmentId}</if>
        <if test="inspectTeamId != null">and isr.inspect_team_id = #{inspectTeamId}</if>
        <!-- 时间范围 -->
        <choose>
            <!-- 查询当天数据 -->
            <when test="timeType == 1">
                AND isr.seizure_time >= CURDATE()
                AND isr.seizure_time <![CDATA[ < ]]> DATE_ADD(CURDATE(), INTERVAL 1 DAY)
            </when>
            <!-- 查询本周数据 -->
            <when test="timeType == 2">
                AND isr.seizure_time >= DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY)
                AND isr.seizure_time <![CDATA[ < ]]>DATE_ADD(DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY),
                INTERVAL 7 DAY)
            </when>
            <!-- 查询本月数据 -->
            <when test="timeType == 3">
                AND isr.seizure_time >= DATE_FORMAT(CURDATE(), '%Y-%m-01')
                AND isr.seizure_time <![CDATA[ < ]]>DATE_ADD(DATE_FORMAT(CURDATE(), '%Y-%m-01'), INTERVAL 1 MONTH)
            </when>
            <!-- 自定义日期范围 -->
            <when test="timeType == 4 and startDate != null and endDate != null">
                and (isr.seizure_time >= #{startDate}
                and isr.seizure_time <![CDATA[ < ]]> date_add(#{endDate}, interval 1 day))
            </when>
        </choose>
        <!-- 分类类型 -->
        <choose>
            <when test="seizureType != null and seizureType != '' and seizureType != 'ALL'">
                and isi.category_code_two = #{seizureType}
            </when>
        </choose>
    </select>
</mapper>