<?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.ProductInfoMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.nantian.cloud.jljc.entity.ProductInfo">
        <id column="id" property="id"/>
        <result column="num" property="num"/>
        <result column="product_num" property="productNum"/>
        <result column="product_name" property="productName"/>
        <result column="unit" property="unit"/>
        <result column="trade_product_code" property="tradeProductCode"/>
        <result column="trade_name" property="tradeName"/>
        <result column="userid" property="userid"/>
        <result column="update_time" property="updateTime"/>
        <result column="del_flag" property="delFlag"/>
        <result column="dept_id" property="deptId"/>
        <result column="custom_code_flag" property="customCodeFlag"/>
    </resultMap>

    <resultMap id="ProductInfoTreeVo" type="com.nantian.cloud.jljc.vo.ProductInfoTreeVo">
        <id column="value" property="value"/>
        <result column="label" property="label"/>
        <result column="pvalue" property="pvalue"/>
        <result column="plabel" property="plabel"/>
        <result column="unit" property="unit"/>
        <result column="kindName" property="kindName"/>
    </resultMap>

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

    <resultMap id="InfoVo" type="com.nantian.cloud.jljc.vo.ProductInfoVo">
        <result column="product_num" property="productNum"/>
        <result column="product_name" property="productName"/>
        <result column="unit" property="unit"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, num, product_num, product_name, unit, trade_name, trade_product_code, userid, update_time, del_flag, dept_id, custom_code_flag
    </sql>

    <select id="getInfoByStr" resultType="com.nantian.cloud.jljc.entity.ProductInfo">
        select
        <include refid="Base_Column_List"></include>
        from product_info
        where id in
        <foreach collection="list" index="index" item="i" separator="," open="(" close=")">
            #{i}
        </foreach>
    </select>

    <select id="getTreeByTrade" resultType="com.nantian.cloud.jljc.vo.ProductInfoTreeVo">
          select info.product_num as value, info.product_name as label,
            info.trade_product_code as pvalue, t.name as plabel, info.unit as unit, null as kindName
            from product_info info left join trade_product t on info.trade_product_code = t.code and info.del_flag = "1"
            union
            select trade.code as value, trade.name as label, 0, null, null, tInfo.kind_name as kindName
            from trade_product trade
            left join trade_info tInfo on tInfo.big_kind = trade.name
    </select>

    <select id="getInfoByName" resultType="com.nantian.cloud.jljc.vo.ProductInfoVo">
        select product_num, product_name, unit
        from product_info
        where product_name like concat("%",#{name},"%") and del_flag = "1"
    </select>

    <select id="getPageScope" resultType="com.nantian.cloud.jljc.entity.ProductInfo">
        select p.id, p.num, p.product_num, p.product_name, p.unit, k.name trade_name, p.trade_product_code, p.userid, p.update_time, p.del_flag, p.dept_id, p.custom_code_flag
        from product_info p
        left join trade_kind k on p.trade_product_code = k.code
        <trim prefix="where" prefixOverrides="and">
            <if test="query.productNum != null and query.productNum != ''">
                and p.product_num like concat("%",#{query.productNum},"%")
            </if>
            <if test="query.productName != null and query.productName != ''">
                and p.product_name like concat("%",#{query.productName},"%")
            </if>
            <if test="query.tradeName != null and query.tradeName != ''">
                and k.name like concat("%",#{query.tradeName},"%")
            </if>
            <if test="query.tradeProductCode != null and query.tradeProductCode != ''">
                and p.trade_product_code like concat("%",#{query.tradeProductCode},"%")
            </if>
            <if test="query.delFlag != null and query.delFlag != ''">
                and p.del_flag = #{query.delFlag}
            </if>
        </trim>
    </select>

    <select id="getTrade" resultType="java.lang.String">
        select DISTINCT k.name tradeName
        from product_info p
        left join trade_kind k on p.trade_product_code = k.code
        where p.del_flag = "1"
    </select>

    <select id="getCustomCode" resultType="java.lang.String">
        select product_num
        from product_info
        where product_num like concat("%",#{prevProductNum},'Z',"%")  and del_flag = "1"
    </select>

    <select id="getTree" resultType="com.nantian.cloud.jljc.vo.TreeVo">
        select p2.product_num as value, p2.product_name as label, p1.product_num as pvalue, p1.product_name as plabel
        from `product_info` p2
        left join `product_info` p1 on p2.product_num like concat(p1.product_num,"%")
        and LENGTH(p2.product_num)-LENGTH(p1.product_num)>=2
        and 3 >= LENGTH(p2.product_num)-LENGTH(p1.product_num) and p1.del_flag = "1" and p2.del_flag = "1"
    </select>

    <select id="getProductInfoList" resultType="java.util.Map">
        select product_num as value, product_name as label
        from product_info
        where del_flag = "1"
    </select>

</mapper>
