<?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.heketai.website.mapper.ProductMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.heketai.website.entity.Product">
        <id column="id" property="id"/>
        <result column="category_id" property="categoryId"/>
        <result column="goods_type" property="goodsType"/>
        <result column="goods_sn" property="goodsSn"/>
        <result column="img" property="img"/>
        <result column="manual" property="manual"/>
        <result column="package_specification" property="packageSpecification"/>
        <result column="app_ids" property="appIds"/>
        <result column="attr_1" property="attr1"/>
        <result column="attr_2" property="attr2"/>
        <result column="attr_3" property="attr3"/>
        <result column="attr_4" property="attr4"/>
        <result column="attr_5" property="attr5"/>
        <result column="attr_6" property="attr6"/>
        <result column="attr_7" property="attr7"/>
        <result column="attr_8" property="attr8"/>
        <result column="attr_9" property="attr9"/>
        <result column="attr_10" property="attr10"/>
        <result column="is_summary" property="isSummary"/>
        <result column="manual_ch" property="manualCh"/>
        <result column="sort" property="sort"/>
        <result column="silk_screen" property="silkScreen"/>
        <result column="remarks" property="remarks"/>
        <result column="only_coding" property="onlyCoding"/>
    </resultMap>

    <sql id="BaseColumnList">
        id
        ,category_id
        ,goods_type
        ,goods_sn
        ,img
        ,manual
        ,manual_ch
        ,package_specification
        ,attr_1
        ,attr_2
        ,attr_3
        ,attr_4
        ,attr_5
        ,attr_6
        ,attr_7
        ,attr_8
        ,attr_9
        ,attr_10
        ,app_ids
        ,sort
        ,silk_screen
        ,remarks
        ,only_coding
    </sql>

    <select id="queryByK3ids" resultMap="BaseResultMap">
        SELECT
        <include refid="BaseColumnList"/>
        FROM product
        WHERE goods_type IN
        <foreach collection="ids" item="id" index="index" open="("
                 close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <select id="queryByAppId" resultMap="BaseResultMap">
        SELECT
        p.id,
        p.category_id,
        p.goods_type,
        p.goods_sn,
        p.img,
        p.manual,
        p.manual_ch,
        p.package_specification,
        p.attr_1,
        p.attr_2,
        p.attr_3,
        p.attr_4,
        p.attr_5,
        p.attr_6,
        p.attr_7,
        p.attr_8,
        p.attr_9,
        p.attr_10,
        p.app_ids,
        p.sort,
        p.silk_screen,
        p.remarks,
        p.only_coding,
        c.is_summary
        FROM product p left join product_category c on p.category_id = c.id
        WHERE
        -- c.is_summary = 0 and (
        1 = 1 and (
        <if test="product.appIds!= null">
            app_ids like concat('%', #{product.appIds}, '%')
        </if>
        <if test="product.goodsType!= null">
            goods_type like concat('%', #{product.goodsType}, '%')
            or package_specification like concat('%', #{product.goodsType}, '%')
            or goods_sn like concat('%', #{product.goodsType}, '%')
        </if>
        )
        limit #{startIndex},#{pageSize}
    </select>

    <!--分页数量查询-->
    <select id="queryByAppIdCount" resultType="int">
        select count(1)
        from product p left join product_category c on p.category_id = c.id
        WHERE
        1=1 and (
        <if test="product.appIds!= null">
            app_ids like concat('%', #{product.appIds}, '%')
        </if>
        <if test="product.goodsType!= null">
            goods_type like concat('%', #{product.goodsType}, '%')
            or package_specification like concat('%', #{product.goodsType}, '%')
            or goods_sn like concat('%', #{product.goodsType}, '%')
        </if>
        )
    </select>
    <!-- 内容去重查詢 -->
    <select id="queryDistint" resultMap="BaseResultMap">
        select goods_type,
               package_specification,
               attr_1,
               attr_2,
               attr_3,
               attr_4,
               attr_5,
               attr_6,
               attr_7,
               attr_8,
               attr_9,
               attr_10
        from product
        where category_id = #{categoryId}
        group by goods_type,
                 package_specification,
                 attr_1,
                 attr_2,
                 attr_3,
                 attr_4,
                 attr_5,
                 attr_6,
                 attr_7,
                 attr_8,
                 attr_9,
                 attr_10
    </select>
    <!-- -->
    <select id="queryValueByVo" resultMap="BaseResultMap">
        SELECT
        <include refid="BaseColumnList"/>
        FROM product
        WHERE category_id = #{vo.categoryId}
        <if test="vo.packageSpecification != null">
            and package_specification IN
            <foreach collection="vo.packageSpecification" item="id" index="index" open="("
                     close=")" separator=",">
                #{id}
            </foreach>
        </if>

        <if test="vo.attr1 != null">
            and attr_1 IN
            <foreach collection="vo.attr1" item="id" index="index" open="("
                     close=")" separator=",">
                #{id}
            </foreach>
        </if>

        <if test="vo.attr2 != null">
            and attr_2 IN
            <foreach collection="vo.attr2" item="id" index="index" open="("
                     close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="vo.attr3 != null">
            and attr_3 IN
            <foreach collection="vo.attr3" item="id" index="index" open="("
                     close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="vo.attr4 != null">
            and attr_4 IN
            <foreach collection="vo.attr4" item="id" index="index" open="("
                     close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="vo.attr5 != null">
            and attr_5 IN
            <foreach collection="vo.attr5" item="id" index="index" open="("
                     close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="vo.attr6 != null">
            and attr_6 IN
            <foreach collection="vo.attr6" item="id" index="index" open="("
                     close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="vo.attr7 != null">
            and attr_7 IN
            <foreach collection="vo.attr7" item="id" index="index" open="("
                     close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="vo.attr8 != null">
            and attr_8 IN
            <foreach collection="vo.attr8" item="id" index="index" open="("
                     close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="vo.attr9 != null">
            and attr_9 IN
            <foreach collection="vo.attr9" item="id" index="index" open="("
                     close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="vo.attr10 != null">
            and attr_10 IN
            <foreach collection="vo.attr10" item="id" index="index" open="("
                     close=")" separator=",">
                #{id}
            </foreach>
        </if>

        <if test="vo.specification != null and vo.specification !=''">
            and package_specification=#{vo.specification}
        </if>

        <if test="vo.keyWord != null and vo.keyWord !=''">
            and (goods_type like '%${vo.keyWord}%' or goods_sn like '%${vo.keyWord}%' or package_specification like
            '%${vo.keyWord}%')
        </if>
        limit #{startIndex},#{pageSize}
    </select>

    <select id="queryCountByVo" resultType="int">
        SELECT count(1)
        FROM product
        WHERE category_id = #{vo.categoryId}
        <if test="vo.specification != null and vo.specification !=''">
            and package_specification=#{vo.specification}
        </if>
        <if test="vo.keyWord != null and vo.keyWord !=''">
            and (goods_type like '%${vo.keyWord}%' or goods_sn like '%${vo.keyWord}%')
        </if>
        <if test="vo.packageSpecification != null">
            and package_specification IN
            <foreach collection="vo.packageSpecification" item="id" index="index" open="("
                     close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="vo.attr1 != null">
            and attr_1 IN
            <foreach collection="vo.attr1" item="id" index="index" open="("
                     close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="vo.attr2 != null">
            and attr_2 IN
            <foreach collection="vo.attr2" item="id" index="index" open="("
                     close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="vo.attr3 != null">
            and attr_3 IN
            <foreach collection="vo.attr3" item="id" index="index" open="("
                     close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="vo.attr4 != null">
            and attr_4 IN
            <foreach collection="vo.attr4" item="id" index="index" open="("
                     close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="vo.attr5 != null">
            and attr_5 IN
            <foreach collection="vo.attr5" item="id" index="index" open="("
                     close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="vo.attr6 != null">
            and attr_6 IN
            <foreach collection="vo.attr6" item="id" index="index" open="("
                     close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="vo.attr7 != null">
            and attr_7 IN
            <foreach collection="vo.attr7" item="id" index="index" open="("
                     close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="vo.attr8 != null">
            and attr_8 IN
            <foreach collection="vo.attr8" item="id" index="index" open="("
                     close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="vo.attr9 != null">
            and attr_9 IN
            <foreach collection="vo.attr9" item="id" index="index" open="("
                     close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="vo.attr10 != null">
            and attr_10 IN
            <foreach collection="vo.attr10" item="id" index="index" open="("
                     close=")" separator=",">
                #{id}
            </foreach>
        </if>
    </select>

    <select id="selectMyPage" resultType="com.heketai.website.entity.dto.ReplacePictureDto">
        SELECT
        img,
        package_specification
        FROM product
        WHERE
        1 = 1
        <if test="img != null">
            and img like concat('%', #{img}, '%')
        </if>
        <if test="packageSpecification != null">
            and package_specification like concat('%', #{packageSpecification}, '%')
        </if>
        group by package_specification,img
        limit #{page},#{size}
    </select>

    <select id="selectMyCount" resultType="java.lang.Integer">
        SELECT
        count(*)
        FROM
        (SELECT
        p.img,
        p.package_specification
        FROM product as p
        WHERE
        1 = 1
        <if test="img != null">
            and p.img like concat('%', #{img}, '%')
        </if>
        <if test="packageSpecification != null">
            and p.package_specification like concat('%', #{packageSpecification}, '%')
        </if>
        group by package_specification,img) as a
        WHERE
        1 = 1

    </select>

    <update id="updateMyImg">
        update product
        set img = #{img}
        where package_specification = #{packageSpecification}
    </update>

    <resultMap id="ProductVOMap" type="com.heketai.website.common.vo.ProductVO">
        <id column="id" property="id"/>
        <result column="category_id" property="categoryId"/>
        <result column="goods_type" property="goodsType"/>
        <result column="goods_sn" property="goodsSn"/>
        <result column="img" property="img"/>
        <result column="manual" property="manual"/>
        <result column="package_specification" property="packageSpecification"/>
        <result column="manual_ch" property="manualCh"/>
        <result column="sort" property="sort"/>
        <result column="attr_1" property="attr1"/>
        <result column="attr_2" property="attr2"/>
        <result column="attr_3" property="attr3"/>
        <result column="attr_4" property="attr4"/>
        <result column="attr_5" property="attr5"/>
        <result column="attr_6" property="attr6"/>
        <result column="attr_7" property="attr7"/>
        <result column="attr_8" property="attr8"/>
        <result column="attr_9" property="attr9"/>
        <result column="attr_10" property="attr10"/>
        <result column="silk_screen" property="silkScreen"/>
        <result column="remarks" property="remarks"/>
        <result column="only_coding" property="onlyCoding"/>
        <collection property="VFPProductCategory" column="category_id"
                    select="com.heketai.website.mapper.ProductCategoryMapper.findById"/>
    </resultMap>
    <select id="findByAll" resultMap="ProductVOMap">
        select id,
        category_id,
        goods_type,
        goods_sn,
        img,
        manual,
        manual_ch,
        sort,
        package_specification,
        attr_1,
        attr_2,
        attr_3,
        attr_4,
        attr_5,
        attr_6,
        attr_7,
        attr_8,
        attr_9,
        attr_10
        from product
        where
        sort = #{request.seekType}
        <if test="request.seekType==2">
            <if test='request.seek != null and request.seek != ""'>
                <if test="request.seekFields == null or request.seekFields ==''">
                    and (goods_type like concat('%', #{request.seek}, '%')
                    or package_specification like concat('%', #{request.seek}, '%'))
                </if>
                <if test='request.seekFields != null and request.seekFields !=""'>
                    and (goods_type like concat('%', #{request.seek}, '%')
                    and package_specification = #{request.seekFields})
                </if>
            </if>

            <if test='request.seek == null or request.seek == ""'>
             <if test="request.seekFields !=null and request.seekFields!=''">
                 and package_specification = #{request.seekFields}
             </if>

            </if>
        </if>

        <if test="request.seekType == 1">
            <if test='request.seek != null and request.seek != ""'>
                <if test="request.seekFields == null or request.seekFields ==''">
                    and (package_specification like concat('%', #{request.seek}, '%') or attr_2 like
                    concat('%', #{request.seek}, '%'))
                </if>
                <if test='request.seekFields != null and request.seekFields !=""'>
                    and (package_specification like concat('%', #{request.seek}, '%') and attr_2 like concat('%',
                    #{request.seekFields}, '%'))
                </if>
            </if>
            <if test='request.seek == null or request.seek == ""'>
                <if test="request.seekFields != null and request.seekFields !=''">
                    and attr_2 like concat('%', #{request.seekFields},'%')
                </if>
           </if>
        </if>

        <if test='request.categoryId !=null and request.categoryId!=""'>
            and category_id = #{request.categoryId}
        </if>
    </select>
    <select id="findByFz" resultType="java.lang.String">
        SELECT package_specification
        FROM `product`
        where sort = #{sort}
        group by package_specification
    </select>
    <select id="findByJd" resultType="java.lang.String">
        SELECT attr_2
        FROM `product`
        where sort = #{sort}
        group by attr_2
    </select>
    <update id="updateSort">
        update product
        set sort = #{sort}
        where category_id in (77, 78, 79, 80, 81, 82, 83, 85, 86, 87, 118, 119)
    </update>

    <select id="queryProductVo" resultMap="BaseResultMap">
        SELECT
        <include refid="BaseColumnList"/>
        FROM product
        WHERE category_id = #{vo.categoryId}
        <if test="vo.goodsType != null">
            and goods_type = #{vo.goodsType}
        </if>
        <if test="vo.packageSpecification != null">
            and package_specification = #{vo.packageSpecification}
        </if>

        <if test="vo.attr1 != null">
            and attr_1 = #{vo.attr1}
        </if>

        <if test="vo.attr2 != null">
            and attr_2 = #{vo.attr2}
        </if>
        <if test="vo.attr3 != null">
            and attr_3 = #{vo.attr3}
        </if>
        <if test="vo.attr4 != null">
            and attr_4 = #{vo.attr4}
        </if>
        <if test="vo.attr5 != null">
            and attr_5 = #{vo.attr5}
        </if>
        <if test="vo.attr6 != null">
            and attr_6 = #{vo.attr6}
        </if>
        <if test="vo.attr7 != null">
            and attr_7 = #{vo.attr7}
        </if>
        <if test="vo.attr8 != null">
            and attr_8 = #{vo.attr8}
        </if>
        <if test="vo.attr9 != null">
            and attr_9 = #{vo.attr9}
        </if>
        <if test="vo.attr10 != null">
            and attr_10 = #{vo.attr10}
        </if>
        limit #{startIndex},#{pageSize}
    </select>

    <select id="queryProductAll" resultMap="BaseResultMap">
        SELECT
        <include refid="BaseColumnList"/>
        FROM product
        WHERE category_id = #{vo.categoryId}
        <if test='vo.goodsType != null and vo.goodsType !=""' >
            and goods_type = #{vo.goodsType}
        </if>
        <if test="vo.packageSpecification != null and vo.packageSpecification !=''">
            and package_specification = #{vo.packageSpecification}
        </if>

        <if test="vo.attr1 != null and vo.attr1  !=''">
            and attr_1 = #{vo.attr1}
        </if>

        <if test="vo.attr2 != null and vo.attr2  !=''">
            and attr_2 = #{vo.attr2}
        </if>
        <if test="vo.attr3 != null and vo.attr3  !=''">
            and attr_3 = #{vo.attr3}
        </if>
        <if test="vo.attr4 != null and vo.attr4  !=''">
            and attr_4 = #{vo.attr4}
        </if>
        <if test="vo.attr5 != null and vo.attr5  !=''">
            and attr_5 = #{vo.attr5}
        </if>
        <if test="vo.attr6 != null and vo.attr6  !=''">
            and attr_6 = #{vo.attr6}
        </if>
        <if test="vo.attr7 != null and vo.attr7  !=''">
            and attr_7 = #{vo.attr7}
        </if>
        <if test="vo.attr8 != null and vo.attr8  !=''">
            and attr_8 = #{vo.attr8}
        </if>
        <if test="vo.attr9 != null and vo.attr9  !=''">
            and attr_9 = #{vo.attr9}
        </if>
        <if test="vo.attr10 != null and vo.attr10  !=''">
            and attr_10 = #{vo.attr10}
        </if>
    </select>
    <select id="queryScope" resultType="com.heketai.website.entity.Product">
        SELECT
        <include refid="BaseColumnList"/>
        FROM product
        WHERE category_id = #{dto.categoryId}

        <if test="vo.attr1 != null">
            and attr_1 dto.
        </if>

        <if test="vo.attr2 != null">
            and attr_2 = #{vo.attr2}
        </if>
        <if test="vo.attr3 != null">
            and attr_3 = #{vo.attr3}
        </if>
        <if test="vo.attr4 != null">
            and attr_4 = #{vo.attr4}
        </if>
        <if test="vo.attr5 != null">
            and attr_5 = #{vo.attr5}
        </if>
        <if test="vo.attr6 != null">
            and attr_6 = #{vo.attr6}
        </if>
        <if test="vo.attr7 != null">
            and attr_7 = #{vo.attr7}
        </if>
        <if test="vo.attr8 != null">
            and attr_8 = #{vo.attr8}
        </if>
        <if test="vo.attr9 != null">
            and attr_9 = #{vo.attr9}
        </if>
        <if test="vo.attr10 != null">
            and attr_10 = #{vo.attr10}
        </if>
    </select>
    <select id="queryBySearch" resultType="com.heketai.website.entity.Product"
            parameterType="java.lang.String">
        SELECT
        <include refid="BaseColumnList"/>
        FROM product
        WHERE
        1=1
        <if test='searchValue != null and searchValue != ""'>
            and (goods_type like concat('%', #{searchValue}, '%')
            or package_specification like concat('%', #{searchValue}, '%'))
        </if>

    </select>
</mapper>
