<?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.productclassify.dao.ProductclassifyMapper">

    <resultMap id="resultMap" type="com.cftech.productclassify.model.Productclassify">
        <id column="id" property="id"/>
        <result column="classify_name" property="classifyName"/>
        <result column="classify_number" property="classifyNumber"/>
        <result column="classify_img" property="classifyImg"/>
        <result column="sort" property="sort"/>
        <result column="whether_recommend" property="whetherRecommend"/>
        <result column="parent_id" property="parentId"/>
        <result column="parentName" property="parentName"/>
        <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"/>
    </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_name,
                                                classify_number,
                                                classify_img,
                                                sort,
                                                whether_recommend,
                                                parent_id,
                                                accounts_id,
                                                del_flag,
                                                status,
                                                create_time,
                                                update_time,
                                                description,
                                                create_by,
                                                update_by
                        </sql>


    <insert id="save" parameterType="com.cftech.productclassify.model.Productclassify" useGeneratedKeys="true"
            keyProperty="id">
        insert into t_aidea_product_classify
        (
        <include refid="sqlColumns"/>
        )
        values
        (
        #{id, jdbcType=BIGINT},
        #{classifyName, jdbcType=VARCHAR},
        #{classifyNumber, jdbcType=VARCHAR},
        #{classifyImg, jdbcType=VARCHAR},
        #{sort, jdbcType=VARCHAR},
        #{whetherRecommend, jdbcType=VARCHAR},
        #{parentId, jdbcType=VARCHAR},
        #{accountsId, jdbcType=BIGINT},
        #{delFlag, jdbcType=TINYINT},
        #{status, jdbcType=VARCHAR},
        now(),
        now(),
        #{description, jdbcType=VARCHAR},
        #{createBy, jdbcType=BIGINT},
        #{updateBy, jdbcType=BIGINT}
        )
    </insert>


    <select id="fetchTreeByPage" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM t_aidea_product_classify
        <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="fetchById" parameterType="java.lang.Long" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM t_aidea_product_classify t
        WHERE t.id=#{id}
    </select>

    <select id="count" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT
        COUNT(1)
        FROM
        t_aidea_product_classify a
        LEFT JOIN t_aidea_product_classify b ON b.id = a.parent_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_name classifyName,
        a.classify_number classifyNumber,
        a.classify_img classifyImg,
        a.sort sort,
        a.whether_recommend whetherRecommend,
        a.parent_id parentId,
        a.del_flag delFlag,
        a.create_time createTime,
        a.update_time updateTime,
        a.description description,
        a.create_by crateBy,
        a.update_by updateBy,
        b.classify_name parentName
        FROM
        t_aidea_product_classify a
        LEFT JOIN t_aidea_product_classify b ON b.id = a.parent_id
        AND b.del_flag = '0' and a.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="selectIdsByNumber" parameterType="java.lang.String" resultType="java.lang.String">
        SELECT
        GROUP_CONCAT(c.id) id
        FROM
            t_aidea_product_classify c
        WHERE
        FIND_IN_SET(
        c.classify_number,
        #{classifyNumber})
        and c.del_flag = 0
    </select>


    <update id="update" parameterType="com.cftech.productclassify.model.Productclassify">
        update t_aidea_product_classify
        <set>
            <if test="id != null">
                id = #{id, jdbcType=BIGINT},
            </if>
            <if test="classifyName != null">
                classify_name = #{classifyName, jdbcType=VARCHAR},
            </if>
            <if test="classifyNumber != null">
                classify_number = #{classifyNumber, jdbcType=VARCHAR},
            </if>
            <if test="classifyImg != null">
                classify_img = #{classifyImg, jdbcType=VARCHAR},
            </if>
            <if test="sort != null">
                sort = #{sort, jdbcType=VARCHAR},
            </if>
            <if test="whetherRecommend != null">
                whether_recommend = #{whetherRecommend, jdbcType=VARCHAR},
            </if>
            <if test="parentId != null">
                parent_id = #{parentId, 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>
        </set>
        where id=#{id,jdbcType=BIGINT}
    </update>

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

    <select id="productList" resultType="com.cftech.productclassify.model.ProductclassifyVO">
    SELECT
    t.id AS ID,
    t.product_name AS productName,
    t.classify_id AS classifyId,
    t.product_img AS productImg,
    t.dosaga_from AS dosagaFrom,
    t.common_name AS commonName,
    t.format,
    t.product_number AS productNumber,
    t.accounts_id AS accountsId,
    t.is_rs AS isRs,
        t.size AS size,
        t.isreading AS isreading,
        t.reading AS reading,
        c.classify_name AS classifyName
        FROM t_aidea_product t LEFT JOIN t_aidea_product_classify c
        ON find_in_set(c.id,t.classify_id) > 0 WHERE t.del_flag = 0 and c.del_flag = 0
        <if test="productName !=null and productName!=''">
            and
            t.product_name like concat('%',#{productName},'%')
        </if>
        <if test="classifyName !=null and classifyName!=''">
            and
            c.id = #{classifyName}
        </if>
        <if test="dosagaFrom !=null and dosagaFrom!=''">
            and
            t.dosaga_from = #{dosagaFrom}
        </if>
        GROUP by t.id
        ORDER BY
        t.create_time
        DESC
        <if test="page!=null and pageSize !='' and pageSize!=null and page !=''">
            limit #{page},#{pageSize}
        </if>
    </select>
    <!--商品分类菜单数据回填!-->
    <select id="productMenu" resultType="com.cftech.productclassify.model.ProductMenuVO">
        SELECT
            t.id,
            t.classify_name AS classifyName,
            t.parent_id AS parentId,
            a.dosaga_from AS dosagaFrom
        FROM t_aidea_product_classify t LEFT JOIN t_aidea_product a ON   find_in_set(t.id,a.classify_id) > 0 where t.del_flag = 0 GROUP BY t.id
    </select>
    <select id="hotproduct" resultType="com.cftech.productclassify.model.ProductVO">
        SELECT
        id,
        product_name AS productName,
        format,
        product_img AS productImg
        FROM t_aidea_product
        where del_flag = 0
        ORDER BY reading DESC
        LIMIT 0,4
    </select>
    <select id="medicine" resultType="com.cftech.productclassify.model.ProductVO">
    SELECT
        id,
        product_name AS productName,
        format,
        product_img AS productImg
        FROM t_aidea_product
        WHERE size= 1 and  del_flag = 0
        ORDER BY create_time desc
    LIMIT 0,4
    </select>
    <select id="productDosage" resultType="com.cftech.productclassify.model.ProductVO">
        SELECT  dosaga_from dosageName FROM  t_aidea_product WHERE  del_flag = 0 GROUP  BY  dosaga_from ORDER BY create_time DESC
    </select>


</mapper>