<?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.product.dao.ProductMapper">

    <resultMap id="resultMap" type="com.cftech.product.model.Product">
        <id column="id" property="id"/>
        <result column="classify_id" property="classifyId"/>
        <result column="product_name" property="productName"/>
        <result column="common_name" property="commonName"/>
        <result column="dosaga_from" property="dosagaFrom"/>
        <result column="format" property="format"/>
        <result column="approve_number" property="approveNumber"/>
        <result column="manufacturer" property="manufacturer"/>
        <result column="recommend_measure" property="recommendMeasure"/>
        <result column="tips" property="tips"/>
        <result column="take_type" property="takeType"/>
        <result column="take_frequency" property="takeFrequency"/>
        <result column="take_amount" property="takeAmount"/>
        <result column="stock" property="stock"/>
        <result column="price" property="price"/>
        <result column="product_img" property="productImg"/>
        <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="product_number" property="productNumber"/>
        <result column="product_img_detail" property="productImgDetail"/>
        <result column="is_rs" property="isRs"/>
        <result column="reading" property="reading"/>
        <result column="size" property="size"/>
        <result column="isreading" property="isreading"/>
        <result column="unit" property="unit"/>
        <result column="invoice_code" property="invoiceCode"/>
    </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,
                                                classify_id,
                                                product_name,
                                                common_name,
                                                dosaga_from,
                                                format,
                                                approve_number,
                                                manufacturer,
                                                recommend_measure,
                                                tips,
                                                take_type,
                                                take_frequency,
                                                take_amount,
                                                stock,
                                                price,
                                                product_img,
                                                accounts_id,
                                                del_flag,
                                                status,
                                                create_time,
                                                update_time,
                                                description,
                                                create_by,
                                                update_by,
                                                product_number,
                                                product_img_detail,
                                                is_rs,
                                                reading,
                                                size,
                                                isreading,
                                                unit,
                                                invoice_code
                        </sql>

    <sql id="productList">
                    t.id AS ID,
                    t.product_name AS productName,
                    t.classify_id AS classifyId,
                    t.product_img AS productImg,
                    t.recommend_measure AS recommendMeasure,
                    t.dosaga_from AS dosagaFrom,
                    t.common_name AS commonName,
                    t.format,
                    t.approve_number AS approveNumber,
                    t.manufacturer,
                    t.description,
                    t.tips,
                    t.take_type AS takeType,
                    t.take_frequency AS takeFrequency,
                    t.take_amount AS takeAmount,
                    t.stock,
                    t.price,
                    t.is_rs AS isRs,
                    t.product_number AS productNumber,
                    t.accounts_id AS accountsId,
                    c.classify_name AS classifyName,
                    t.product_img_detail AS productImgDetail,
                    t.reading,
                    t.isreading,
                    t.invoice_code invoiceCode
    </sql>

    <insert id="save" parameterType="com.cftech.product.model.Product" useGeneratedKeys="true"
            keyProperty="id">
        insert into t_aidea_product
        (
        <include refid="sqlColumns"/>
        )
        values
        (
        #{id, jdbcType=BIGINT},
        #{classifyId, jdbcType=VARCHAR},
        #{productName, jdbcType=VARCHAR},
        #{commonName, jdbcType=VARCHAR},
        #{dosagaFrom, jdbcType=VARCHAR},
        #{format, jdbcType=VARCHAR},
        #{approveNumber, jdbcType=VARCHAR},
        #{manufacturer, jdbcType=VARCHAR},
        #{recommendMeasure, jdbcType=VARCHAR},
        #{tips, jdbcType=VARCHAR},
        #{takeType , jdbcType=VARCHAR},
        #{takeFrequency, jdbcType=VARCHAR},
        #{takeAmount, jdbcType=VARCHAR},
        #{stock, jdbcType=BIGINT},
        #{price, jdbcType=DOUBLE},
        #{productImg, jdbcType=VARCHAR},
        #{accountsId, jdbcType=BIGINT},
        #{delFlag, jdbcType=TINYINT},
        #{status, jdbcType=VARCHAR},
        now(),
        now(),
        #{description, jdbcType=VARCHAR},
        #{createBy, jdbcType=BIGINT},
        #{updateBy, jdbcType=BIGINT},
        #{productNumber,jdbcType=VARCHAR},
        #{productImgDetail,jdbcType=VARCHAR},
        #{isRs,jdbcType=VARCHAR},
        #{reading,jdbcType=VARCHAR},
        #{size, jdbcType=BIGINT},
        #{isreading, jdbcType=BIGINT},
        #{unit,jdbcType=VARCHAR},
        #{invoiceCode,jdbcType=VARCHAR}
        )
    </insert>

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

    <select id="count" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM t_aidea_product a
        LEFT JOIN t_aidea_product_classify b ON a.classify_id = b.id
        AND b.del_flag = '0'
        <include refid="sqlWhere"/>

    </select>

    <select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        a.id id,
        a.classify_id classfyId,
        b.classify_name classifyName,
        a.product_name productName,
        a.common_name commonName,
        a.dosaga_from dosagaFrom,
        a.format format,
        a.approve_number approveNumber,
        a.manufacturer manufacturer,
        a.recommend_measure recommendMeasure,
        a.tips tips,
        a.take_type takeType,
        a.take_frequency takeFrequency,
        a.take_amount takeAmount,
        a.stock stock,
        a.price price,
        a.product_img productImg,
        a.accounts_id accountsId,
        a.del_flag delFlag,
        a.STATUS,
        a.create_time createTime,
        a.update_time updateTime,
        a.description,
        a.create_by createBy,
        a.update_by updateBy,
        a.product_number productNumber,
        a.is_rs isRs,
        a.reading reading,
        a.invoice_code
        FROM
        t_aidea_product a
        LEFT JOIN t_aidea_product_classify b ON a.classify_id = b.id
        AND b.del_flag = '0'
        <include refid="sqlWhere"/>
        <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
        <if test="limit>0">limit #{offset},#{limit}</if>
    </select>

    <select id="selectProduct"  parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        a.id id,
        a.classify_id classfyId,
        GROUP_CONCAT(b.classify_name) classifyName,
        a.product_name productName,
        a.common_name commonName,
        a.dosaga_from dosagaFrom,
        a.format format,
        a.approve_number approveNumber,
        a.manufacturer manufacturer,
        a.recommend_measure recommendMeasure,
        a.tips tips,
        a.take_type takeType,
        a.take_frequency takeFrequency,
        a.take_amount takeAmount,
        a.stock stock,
        a.price price,
        a.product_img productImg,
        a.accounts_id accountsId,
        a.del_flag delFlag,
        a.STATUS,
        a.create_time createTime,
        a.update_time updateTime,
        a.description,
        a.create_by createBy,
        a.update_by updateBy,
        a.product_number productNumber,
        a.is_rs isRs,
        a.reading reading,
        a.invoice_code
        FROM
        t_aidea_product a
        LEFT JOIN t_aidea_product_classify b ON FIND_IN_SET(b.id,a.classify_id)
        AND b.del_flag = '0'
        <include refid="sqlWhere"/>
        <if test="classifyId != null">
            <foreach collection="classifyId" index="index" item="item" open="" separator="" close="">
                AND FIND_IN_SET(#{item},a.classify_id)
            </foreach>
        </if>
        GROUP BY a.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.product.model.Product">
        update t_aidea_product
        <set>
            <if test="id != null">
                id = #{id, jdbcType=BIGINT},
            </if>
            <if test="classifyId != null">
                classify_id = #{classifyId, jdbcType=VARCHAR},
            </if>
            <if test="productName != null">
                product_name = #{productName, jdbcType=VARCHAR},
            </if>
            <if test="commonName != null">
                common_name = #{commonName, jdbcType=VARCHAR},
            </if>
            <if test="dosagaFrom != null">
                dosaga_from = #{dosagaFrom, jdbcType=VARCHAR},
            </if>
            <if test="format != null">
                format = #{format, jdbcType=VARCHAR},
            </if>
            <if test="productNumber != null">
                product_number = #{productNumber, jdbcType=VARCHAR},
            </if>
            <if test="approveNumber != null">
                approve_number = #{approveNumber, jdbcType=VARCHAR},
            </if>
            <if test="manufacturer != null">
                manufacturer = #{manufacturer, jdbcType=VARCHAR},
            </if>
            <if test="recommendMeasure != null">
                recommend_measure = #{recommendMeasure, jdbcType=VARCHAR},
            </if>
            <if test="tips != null">
                tips = #{tips, jdbcType=VARCHAR},
            </if>
            <if test="takeType != null">
                take_type = #{takeType, jdbcType=VARCHAR},
            </if>
            <if test="takeFrequency != null">
                take_frequency = #{takeFrequency, jdbcType=VARCHAR},
            </if>
            <if test="takeAmount != null">
                take_amount = #{takeAmount, jdbcType=VARCHAR},
            </if>
            <if test="stock != null">
                stock = #{stock, jdbcType=BIGINT},
            </if>
            <if test="price != null">
                price = #{price, jdbcType=DOUBLE},
            </if>
            <if test="productImg != null">
                product_img = #{productImg, 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="productImgDetail != null">
                product_img_detail = #{productImgDetail, jdbcType=VARCHAR},
            </if>
            <if test="isRs != null">
                is_rs = #{isRs, jdbcType=VARCHAR},
            </if>
            <if test="reading != null">
                reading = #{reading , jdbcType=VARCHAR},
            </if>
            <if test="size != null">
                size = #{size, jdbcType=BIGINT},
            </if>
            <if test="unit != null">
                unit = #{unit , jdbcType=VARCHAR},
            </if>
            <if test="isreading != null">
                isreading = #{isreading , jdbcType=BIGINT},
            </if>
            <if test="invoiceCode != null">
                invoice_code = #{invoiceCode , jdbcType=VARCHAR}
            </if>
        </set>
        where id=#{id,jdbcType=BIGINT}
    </update>

    <update id="delete" parameterType="java.lang.Long">
        update t_aidea_product set del_flag=1 where id=#{id,jdbcType=BIGINT}
    </update>
    <!-- 根据ID查询商品详情!-->
    <select id="product" resultType="com.cftech.product.model.ProductVO">
        select
        <include refid="productList"/>
        FROM t_aidea_product t LEFT JOIN t_aidea_product_classify c
        ON c.id = t.classify_id WHERE t.del_flag=0 and t.id= #{ID}
    </select>
</mapper>