<?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.cftech.productdetail.dao.ProductDetailMapper">

    <resultMap id="resultMap" type="com.cftech.productdetail.model.ProductDetail">
        <id column="id" property="id"/>
        <result column="product_detail_number" property="productDetailNumber"/>
        <result column="materiel_number" property="materielNumber"/>
        <result column="product_number" property="productNumber"/>
        <result column="product_name" property="productName"/>
        <result column="product_classify_number" property="productClassifyNumber"/>
        <result column="into_wareroom_number" property="intoWareroomNumber"/>
        <result column="out_wareroom_number" property="outWareroomNumber"/>
        <result column="order_number" property="orderNumber"/>
        <result column="logistics_number" property="logisticsNumber"/>
        <result column="whether_sell" property="whetherSell"/>
        <result column="accounts_id" property="accountsId"/>
        <result column="del_flag" property="delFlag"/>
        <result column="status" property="status"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="description" property="description"/>
        <result column="create_by" property="createBy"/>
        <result column="update_by" property="updateBy"/>
        <result column="out_wareroom_time" property="outWareroomTime"/>
    </resultMap>

    <sql id="sqlWhere">
        <if test="conds!=null">
            <trim prefix="WHERE" prefixOverrides="AND|OR">
                <foreach collection="conds.conds" index="index" item="cond">
                    ${cond.linkType}
                    <if test="cond.condType == 'EQUAL'">${cond.param} = #{cond.value}</if>
                    <if test="cond.condType == 'NOTEQUAL'">${cond.param} &lt;&gt; #{cond.value}</if>
                    <if test="cond.condType == 'GREATEQUAL'">${cond.param} &gt;= #{cond.value}</if>
                    <if test="cond.condType == 'GREATTHAN'">${cond.param} &gt; #{cond.value}</if>
                    <if test="cond.condType == 'LESSEQUAL'">${cond.param} &lt;= #{cond.value}</if>
                    <if test="cond.condType == 'LESSTHAN'">${cond.param} &lt; #{cond.value}</if>
                    <if test="cond.condType == 'BETWEEN'">${cond.param} BETWEEN #{cond.startValue} AND
                        #{cond.endValue}
                    </if>
                    <if test="cond.condType == 'ISNULL'">${cond.param} IS NULL</if>
                    <if test="cond.condType == 'NOTNULL'">${cond.param} IS NOT NULL</if>
                    <if test="cond.condType == 'LIKE'">${cond.param} LIKE #{cond.value}</if>
                    <if test="cond.condType == 'IN'">${cond.param} IN
                        <foreach item="item" index="index" collection="cond.value" open="(" separator="," close=")">
                            #{item}
                        </foreach>
                    </if>
                </foreach>
            </trim>
        </if>
    </sql>

    <sql id="sqlColumns">
                                    id,
                                                product_detail_number,
                                                materiel_number,
                                                product_number,
                                                product_name,
                                                product_classify_number,
                                                into_wareroom_number,
                                                out_wareroom_number,
                                                order_number,
                                                logistics_number,
                                                whether_sell,
                                                accounts_id,
                                                del_flag,
                                                status,
                                                create_time,
                                                update_time,
                                                description,
                                                create_by,
                                                update_by,
                                                out_wareroom_time
                        </sql>


    <insert id="save" parameterType="com.cftech.productdetail.model.ProductDetail" useGeneratedKeys="true"
            keyProperty="id">
        insert into t_aidea_product_detail
        (
        <include refid="sqlColumns"/>
        )
        values
        (
        #{id, jdbcType=BIGINT},
        #{productDetailNumber, jdbcType=VARCHAR},
        #{materielNumber, jdbcType=VARCHAR},
        #{productNumber, jdbcType=VARCHAR},
        #{productName, jdbcType=VARCHAR},
        #{productClassifyNumber, jdbcType=VARCHAR},
        #{intoWareroomNumber, jdbcType=VARCHAR},
        #{outWareroomNumber, jdbcType=VARCHAR},
        #{orderNumber, jdbcType=VARCHAR},
        #{logisticsNumber, jdbcType=VARCHAR},
        #{whetherSell, jdbcType=VARCHAR},
        #{accountsId, jdbcType=BIGINT},
        #{delFlag, jdbcType=TINYINT},
        #{status, jdbcType=VARCHAR},
        now(),
        now(),
        #{description, jdbcType=VARCHAR},
        #{createBy, jdbcType=BIGINT},
        #{updateBy, jdbcType=BIGINT},
        #{outWareroomTime, jdbcType=TIMESTAMP}
        )
    </insert>

    <select id="fetchById" parameterType="java.lang.Long" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM t_aidea_product_detail t
        WHERE t.id=#{id}
    </select>

    <select id="count" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM t_aidea_product_detail d
        LEFT JOIN t_aidea_product p on d.product_number = p.product_number and p.del_flag = 0
        LEFT JOIN t_aidea_product_classify c on d.product_classify_number = c.classify_number and c.del_flag = 0
        <include refid="sqlWhere"/>

    </select>

    <select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        d.id id,
        d.product_detail_number productDetailNumber,
        d.materiel_number materielNumber,
        d.product_number productNumber,
        p.product_name productName,
        d.product_classify_number productClassifyNumber,
        d.into_wareroom_number intoWareroomNumber,
        d.out_wareroom_number outWareroomNumber,
        d.order_number orderNumber,
        d.logistics_number logisticsNumber,
        d.whether_sell whetherSell,
        d.accounts_id accountsId,
        d.del_flag delFlag,
        d.STATUS,
        d.create_time createTime,
        d.update_time updateTime,
        d.description description,
        d.create_by createBy,
        d.update_by updateBy,
        d.out_wareroom_time outWareroomTime,
        p.product_img productImg,
        GROUP_CONCAT(c.classify_name) classifyName
        FROM
        t_aidea_product_detail d
        LEFT JOIN t_aidea_product p ON d.product_number = p.product_number
        AND p.del_flag = 0
        LEFT JOIN t_aidea_product_classify c ON FIND_IN_SET(c.classify_number,d.product_classify_number )
        AND c.del_flag = 0
        <include refid="sqlWhere"/>
        GROUP BY d.id
        <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
        <if test="limit>0">limit #{offset},#{limit}</if>
    </select>

    <update id="update" parameterType="com.cftech.productdetail.model.ProductDetail">
        update t_aidea_product_detail
        <set>
            <if test="id != null">
                id = #{id, jdbcType=BIGINT},
            </if>
            <if test="productDetailNumber != null">
                product_detail_number = #{productDetailNumber, jdbcType=VARCHAR},
            </if>
            <if test="materielNumber != null">
                materiel_number = #{materielNumber, jdbcType=VARCHAR},
            </if>
            <if test="productNumber != null">
                product_number = #{productNumber, jdbcType=VARCHAR},
            </if>
            <if test="productName != null">
                product_name = #{productName, jdbcType=VARCHAR},
            </if>
            <if test="productClassifyNumber != null">
                product_classify_number = #{productClassifyNumber, jdbcType=VARCHAR},
            </if>
            <if test="intoWareroomNumber != null">
                into_wareroom_number = #{intoWareroomNumber, jdbcType=VARCHAR},
            </if>
            <if test="outWareroomNumber != null">
                out_wareroom_number = #{outWareroomNumber, jdbcType=VARCHAR},
            </if>
            <if test="orderNumber != null">
                order_number = #{orderNumber, jdbcType=VARCHAR},
            </if>
            <if test="logisticsNumber != null">
                logistics_number = #{logisticsNumber, jdbcType=VARCHAR},
            </if>
            <if test="whetherSell != null">
                whether_sell = #{whetherSell, jdbcType=VARCHAR},
            </if>
            <if test="accountsId != null">
                accounts_id = #{accountsId, jdbcType=BIGINT},
            </if>
            <if test="delFlag != null">
                del_flag = #{delFlag, jdbcType=TINYINT},
            </if>
            <if test="status != null">
                status = #{status, jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                create_time = #{createTime, jdbcType=TIMESTAMP},
            </if>
            <if test="description != null">
                description = #{description, jdbcType=VARCHAR},
            </if>
            <if test="createBy != null">
                create_by = #{createBy, jdbcType=BIGINT},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy, jdbcType=BIGINT},
            </if>
            <if test="outWareroomTime != null">
                out_wareroom_time = #{outWareroomTime, jdbcType=TIMESTAMP},
            </if>
        </set>
        where id=#{id,jdbcType=BIGINT}
    </update>

    <update id="delete" parameterType="java.lang.Long">
        update t_aidea_product_detail set del_flag=1 where id=#{id,jdbcType=BIGINT}
    </update>
</mapper>