<?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.nantian.cloud.jljc.mapper.TradeKindMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.nantian.cloud.jljc.entity.TradeKind">
        <id column="id" property="id" />
        <result column="category" property="category" />
        <result column="sort" property="sort" />
        <result column="code" property="code" />
        <result column="name" property="name" />
        <result column="type" property="type" />
        <result column="userid" property="userid"/>
        <result column="update_time" property="updateTime"/>
        <result column="del_flag" property="delFlag"/>
        <result column="dept_id" property="deptId"/>
    </resultMap>

    <resultMap id="BaseResultMapVo" type="com.nantian.cloud.jljc.vo.TradeKindTreeVo">
        <id column="value" property="value" />
        <result column="pvalue" property="pvalue" />
        <result column="label" property="label" />
        <result column="type" property="type" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, category, sort, code, name, type, userid, update_time, del_flag, dept_id
    </sql>
    <select id="getNameByCode" resultType="com.nantian.cloud.jljc.entity.TradeKind">
        select <include refid="Base_Column_List"></include>
        from trade_kind
        <if test="code!=null and code!=''">
            where code = #{code}
        </if>
    </select>

    <select id="selectbycode" resultType="com.nantian.cloud.jljc.entity.TradeKind">
        select * from trade_kind
        <if test="code!=null and code!=''">
            where code = #{code}
        </if>
    </select>

    <select id="getTradeKindTree" resultType="com.nantian.cloud.jljc.vo.TradeKindTreeVo">
        select code as value, sort as pvalue, name as label, type
        from trade_kind
        where category != '0'
    </select>

<!--    <select id="selectCategory" resultType="com.nantian.cloud.jljc.entity.TradeKind">
        select <include refid="Base_Column_List"></include>
        from trade_kind
        where LENGTH(code)=1
    </select>-->

    <select id="selectCategory" resultType="com.nantian.cloud.jljc.entity.TradeKind">
        select k.id, k.category, k.sort, k.code, k.name, k.type
        from(
            select DISTINCT kind.sort from trade_info info left join trade_kind kind
            on info.trade_num = kind.code where kind.sort is not null and kind.sort!="0"
        ) t left join trade_kind k on t.sort = k.code
    </select>

</mapper>
