<?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="org.springblade.system.mapper.CargoTypeMapper">

    <resultMap id="cargoTypeResultMap" type="org.springblade.system.pojo.vo.CargoTypeVO">
        <result column="id" property="id"/>
        <result column="create_user" property="createUser"/>
        <result column="create_user_name" property="createUserName"/>
        <result column="create_dept" property="createDept"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_user_name" property="updateUserName"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="type_level" property="typeLevel"/>
        <result column="type_level_name" property="typeLevelName"/>
        <result column="parent_id" property="parentId"/>
        <result column="parent_cargo_name" property="parentCargoName"/>
        <result column="parent_cargo_code" property="parentCargoCode"/>
        <result column="cargo_name" property="cargoName"/>
        <result column="cargo_code" property="cargoCode"/>
        <result column="data_source" property="dataSource"/>
        <result column="remark" property="remark"/>
    </resultMap>

    <select id="selectCargoTypePage" resultMap="cargoTypeResultMap">
        SELECT
            ct.id,
            ct.create_user,
            cu.real_name AS create_user_name,
            ct.create_dept,
            ct.create_time,
            ct.update_user,
            uu.real_name AS update_user_name,
            ct.update_time,
            ct.status,
            ct.is_deleted,
            ct.type_level,
            CASE ct.type_level WHEN 1 THEN '一级货物类型' WHEN 2 THEN '二级货物类型' ELSE '' END AS type_level_name,
            ct.parent_id,
            pct.cargo_name AS parent_cargo_name,
            ct.parent_cargo_code,
            ct.cargo_name,
            ct.cargo_code,
            ct.data_source,
            ct.remark
        FROM
            blade_cargo_type ct
            LEFT JOIN blade_cargo_type pct ON pct.id = ct.parent_id AND pct.is_deleted = 0
            LEFT JOIN blade_user cu ON cu.id = ct.create_user
            LEFT JOIN blade_user uu ON uu.id = ct.update_user
        WHERE
            ct.is_deleted = 0
        <if test="cargoType.typeLevel != null">
            AND ct.type_level = #{cargoType.typeLevel}
        </if>
        <if test="cargoType.cargoName != null and cargoType.cargoName != ''">
            <bind name="cargoNameLike" value="'%' + cargoType.cargoName + '%'"/>
            AND ct.cargo_name LIKE #{cargoNameLike}
        </if>
        <if test="cargoType.cargoCode != null and cargoType.cargoCode != ''">
            <bind name="cargoCodeLike" value="'%' + cargoType.cargoCode + '%'"/>
            AND ct.cargo_code LIKE #{cargoCodeLike}
        </if>
        <if test="cargoType.parentCargoName != null and cargoType.parentCargoName != ''">
            <bind name="parentCargoNameLike" value="'%' + cargoType.parentCargoName + '%'"/>
            AND pct.cargo_name LIKE #{parentCargoNameLike}
        </if>
        <if test="cargoType.parentCargoCode != null and cargoType.parentCargoCode != ''">
            <bind name="parentCargoCodeLike" value="'%' + cargoType.parentCargoCode + '%'"/>
            AND ct.parent_cargo_code LIKE #{parentCargoCodeLike}
        </if>
        ORDER BY ct.create_time DESC
    </select>

</mapper>
