<?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.fuxi.ws.data.dao.WLabelMapper">
	<resultMap id="BaseResultMap" type="com.fuxi.ws.data.entity.WLabel">
		<!-- WARNING - @mbggenerated This element is automatically generated by 
			MyBatis Generator, do not modify. This element was generated on Wed Jul 20 
			16:41:42 CST 2016. -->
		<id column="LabelID" jdbcType="INTEGER" property="labelid" />
		<result column="LabelName" jdbcType="NVARCHAR" property="labelname" />
		<result column="LabelType" jdbcType="TINYINT" property="labeltype" />
		<result column="MinLastBuy" jdbcType="SMALLINT" property="minlastbuy" />
		<result column="MaxLastBuy" jdbcType="SMALLINT" property="maxlastbuy" />
		<result column="MinBuyNum" jdbcType="SMALLINT" property="minbuynum" />
		<result column="MaxBuyNum" jdbcType="SMALLINT" property="maxbuynum" />
		<result column="MinBuyMoney" jdbcType="DECIMAL" property="minbuymoney" />
		<result column="MaxBuyMoney" jdbcType="DECIMAL" property="maxbuymoney" />
		<result column="Color" jdbcType="VARCHAR" property="color" />
		<result column="WeixinGroupID" jdbcType="VARCHAR" property="weixingroupid" />
		<result column="ShowIndex" jdbcType="SMALLINT" property="showindex" />
	</resultMap>
	<sql id="Example_Where_Clause">
		<!-- WARNING - @mbggenerated This element is automatically generated by 
			MyBatis Generator, do not modify. This element was generated on Wed Jul 20 
			16:41:42 CST 2016. -->
		<where>
			<foreach collection="oredCriteria" item="criteria" separator="or">
				<if test="criteria.valid">
					<trim prefix="(" prefixOverrides="and" suffix=")">
						<foreach collection="criteria.criteria" item="criterion">
							<choose>
								<when test="criterion.noValue">
									and ${criterion.condition}
								</when>
								<when test="criterion.singleValue">
									and ${criterion.condition} #{criterion.value}
								</when>
								<when test="criterion.betweenValue">
									and ${criterion.condition} #{criterion.value}
									and
									#{criterion.secondValue}
								</when>
								<when test="criterion.listValue">
									and ${criterion.condition}
									<foreach close=")" collection="criterion.value" item="listItem"
										open="(" separator=",">
										#{listItem}
									</foreach>
								</when>
							</choose>
						</foreach>
					</trim>
				</if>
			</foreach>
		</where>
	</sql>
	<sql id="Base_Column_List">
		<!-- WARNING - @mbggenerated This element is automatically generated by 
			MyBatis Generator, do not modify. This element was generated on Wed Jul 20 
			16:41:42 CST 2016. -->
		LabelID, LabelName, LabelType, MinLastBuy, MaxLastBuy, MinBuyNum,
		MaxBuyNum, MinBuyMoney,
		MaxBuyMoney, Color, WeixinGroupID, ShowIndex
	</sql>
	<select id="selectByExample" parameterType="com.fuxi.ws.data.entity.WLabelExample"
		resultMap="BaseResultMap">
		<!-- WARNING - @mbggenerated This element is automatically generated by 
			MyBatis Generator, do not modify. This element was generated on Wed Jul 20 
			16:41:42 CST 2016. -->
		select
		<if test="distinct">
			distinct
		</if>
		'false' as QUERYID,
		<include refid="Base_Column_List" />
		from W_Label
		<if test="_parameter != null">
			<include refid="Example_Where_Clause" />
		</if>
		<if test="orderByClause != null">
			order by ${orderByClause}
		</if>
	</select>
	<select id="selectByPrimaryKey" parameterType="java.lang.Integer"
		resultMap="BaseResultMap">
		<!-- WARNING - @mbggenerated This element is automatically generated by 
			MyBatis Generator, do not modify. This element was generated on Wed Jul 20 
			16:41:42 CST 2016. -->
		select
		<include refid="Base_Column_List" />
		from W_Label
		where LabelID = #{labelid,jdbcType=INTEGER}
	</select>
	<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
		<!-- WARNING - @mbggenerated This element is automatically generated by 
			MyBatis Generator, do not modify. This element was generated on Wed Jul 20 
			16:41:42 CST 2016. -->
		delete from W_Label
		where LabelID = #{labelid,jdbcType=INTEGER}
	</delete>
	<insert id="insert" parameterType="com.fuxi.ws.data.entity.WLabel">
		<!-- WARNING - @mbggenerated This element is automatically generated by 
			MyBatis Generator, do not modify. This element was generated on Wed Jul 20 
			16:41:42 CST 2016. -->
		insert into W_Label (LabelID, LabelName, LabelType,
		MinLastBuy,
		MaxLastBuy, MinBuyNum,
		MaxBuyNum, MinBuyMoney, MaxBuyMoney,
		Color,
		WeixinGroupID, ShowIndex
		)
		values (#{labelid,jdbcType=INTEGER},
		#{labelname,jdbcType=NVARCHAR},
		#{labeltype,jdbcType=TINYINT},
		#{minlastbuy,jdbcType=SMALLINT}, #{maxlastbuy,jdbcType=SMALLINT},
		#{minbuynum,jdbcType=SMALLINT},
		#{maxbuynum,jdbcType=SMALLINT},
		#{minbuymoney,jdbcType=DECIMAL},
		#{maxbuymoney,jdbcType=DECIMAL},
		#{color,jdbcType=VARCHAR}, #{weixingroupid,jdbcType=VARCHAR},
		#{showindex,jdbcType=SMALLINT}
		)
	</insert>
	<insert id="insertSelective" parameterType="com.fuxi.ws.data.entity.WLabel">
		<!-- WARNING - @mbggenerated This element is automatically generated by 
			MyBatis Generator, do not modify. This element was generated on Wed Jul 20 
			16:41:42 CST 2016. -->
		insert into W_Label
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="labelid != null">
				LabelID,
			</if>
			<if test="labelname != null">
				LabelName,
			</if>
			<if test="labeltype != null">
				LabelType,
			</if>
			<if test="minlastbuy != null">
				MinLastBuy,
			</if>
			<if test="maxlastbuy != null">
				MaxLastBuy,
			</if>
			<if test="minbuynum != null">
				MinBuyNum,
			</if>
			<if test="maxbuynum != null">
				MaxBuyNum,
			</if>
			<if test="minbuymoney != null">
				MinBuyMoney,
			</if>
			<if test="maxbuymoney != null">
				MaxBuyMoney,
			</if>
			<if test="color != null">
				Color,
			</if>
			<if test="weixingroupid != null">
				WeixinGroupID,
			</if>
			<if test="showindex != null">
				ShowIndex,
			</if>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
			<if test="labelid != null">
				#{labelid,jdbcType=INTEGER},
			</if>
			<if test="labelname != null">
				#{labelname,jdbcType=NVARCHAR},
			</if>
			<if test="labeltype != null">
				#{labeltype,jdbcType=TINYINT},
			</if>
			<if test="minlastbuy != null">
				#{minlastbuy,jdbcType=SMALLINT},
			</if>
			<if test="maxlastbuy != null">
				#{maxlastbuy,jdbcType=SMALLINT},
			</if>
			<if test="minbuynum != null">
				#{minbuynum,jdbcType=SMALLINT},
			</if>
			<if test="maxbuynum != null">
				#{maxbuynum,jdbcType=SMALLINT},
			</if>
			<if test="minbuymoney != null">
				#{minbuymoney,jdbcType=DECIMAL},
			</if>
			<if test="maxbuymoney != null">
				#{maxbuymoney,jdbcType=DECIMAL},
			</if>
			<if test="color != null">
				#{color,jdbcType=VARCHAR},
			</if>
			<if test="weixingroupid != null">
				#{weixingroupid,jdbcType=VARCHAR},
			</if>
			<if test="showindex != null">
				#{showindex,jdbcType=SMALLINT},
			</if>
		</trim>
	</insert>
	<select id="countByExample" parameterType="com.fuxi.ws.data.entity.WLabelExample"
		resultType="java.lang.Integer">
		<!-- WARNING - @mbggenerated This element is automatically generated by 
			MyBatis Generator, do not modify. This element was generated on Wed Jul 20 
			16:41:42 CST 2016. -->
		select count(*) from W_Label
		<if test="_parameter != null">
			<include refid="Example_Where_Clause" />
		</if>
	</select>
	<update id="updateByPrimaryKeySelective" parameterType="com.fuxi.ws.data.entity.WLabel">
		<!-- WARNING - @mbggenerated This element is automatically generated by 
			MyBatis Generator, do not modify. This element was generated on Wed Jul 20 
			16:41:42 CST 2016. -->
		update W_Label
		<set>
			<if test="labelname != null">
				LabelName = #{labelname,jdbcType=NVARCHAR},
			</if>
			<if test="labeltype != null">
				LabelType = #{labeltype,jdbcType=TINYINT},
			</if>
			<if test="minlastbuy != null">
				MinLastBuy = #{minlastbuy,jdbcType=SMALLINT},
			</if>
			<if test="maxlastbuy != null">
				MaxLastBuy = #{maxlastbuy,jdbcType=SMALLINT},
			</if>
			<if test="minbuynum != null">
				MinBuyNum = #{minbuynum,jdbcType=SMALLINT},
			</if>
			<if test="maxbuynum != null">
				MaxBuyNum = #{maxbuynum,jdbcType=SMALLINT},
			</if>
			<if test="minbuymoney != null">
				MinBuyMoney = #{minbuymoney,jdbcType=DECIMAL},
			</if>
			<if test="maxbuymoney != null">
				MaxBuyMoney = #{maxbuymoney,jdbcType=DECIMAL},
			</if>
			<if test="color != null">
				Color = #{color,jdbcType=VARCHAR},
			</if>
			<if test="weixingroupid != null">
				WeixinGroupID = #{weixingroupid,jdbcType=VARCHAR},
			</if>
			<if test="showindex != null">
				ShowIndex = #{showindex,jdbcType=SMALLINT},
			</if>
		</set>
		where LabelID = #{labelid,jdbcType=INTEGER}
	</update>
	<update id="updateByPrimaryKey" parameterType="com.fuxi.ws.data.entity.WLabel">
		<!-- WARNING - @mbggenerated This element is automatically generated by 
			MyBatis Generator, do not modify. This element was generated on Wed Jul 20 
			16:41:42 CST 2016. -->
		update W_Label
		set LabelName = #{labelname,jdbcType=NVARCHAR},
		LabelType = #{labeltype,jdbcType=TINYINT},
		MinLastBuy =
		#{minlastbuy,jdbcType=SMALLINT},
		MaxLastBuy =
		#{maxlastbuy,jdbcType=SMALLINT},
		MinBuyNum =
		#{minbuynum,jdbcType=SMALLINT},
		MaxBuyNum =
		#{maxbuynum,jdbcType=SMALLINT},
		MinBuyMoney =
		#{minbuymoney,jdbcType=DECIMAL},
		MaxBuyMoney =
		#{maxbuymoney,jdbcType=DECIMAL},
		Color = #{color,jdbcType=VARCHAR},
		WeixinGroupID = #{weixingroupid,jdbcType=VARCHAR},
		ShowIndex =
		#{showindex,jdbcType=SMALLINT}
		where LabelID =
		#{labelid,jdbcType=INTEGER}
	</update>

	<select id="selectlabel" resultType="java.util.LinkedHashMap">
		SELECT
		LabelID,LabelName,LabelType
		FROM W_Label
		Where LabelType=
		#{labeltype,jdbcType=TINYINT}
	</select>

	<select id="getlabeltypecountbygrouplabelid" parameterType="java.lang.String"
		resultType="java.lang.Integer">
		select COUNT(*) labeltypecount from (
		select distinct
		b.LabelType from W_Label b inner join
		W_GroupLabelDtl c on
		c.LabelID=b.LabelID
		where
		c.GroupLabelID=#{grouplabelid,jdbcType=VARCHAR}
		) m
	</select>

	<select id="getAllLabel" resultType="java.util.LinkedHashMap">
		SELECT *
		FROM W_Label
	</select>



	<select id="noSelectLabel" resultType="java.util.LinkedHashMap">
		select * from W_Label
		<if test="list.size() &gt;0">
			where
			LabelID not in
			<foreach close=")" collection="list" index="index" item="item"
				open="(" separator=",">
				#{item}
			</foreach>
		</if>
	</select>

	<select id="selectlabelByQtid" resultType="java.util.LinkedHashMap">
		SELECT distinct
		a.LabelID,a.LabelName,a.LabelType
		FROM W_Label a inner join
		W_QuestionDtl b on a.LabelType=b.HasFraction
		Where b.QtID=
		#{qtid,jdbcType=TINYINT}
	</select>

	<select id="selectgoodslabel" resultType="java.util.LinkedHashMap">
	 select
		l.labelname,isnull(viplabel.vipsum,0)
		val,isnull(goodslbabel.goodsnum,0) gnum
		from W_Label l
		left join
		(
			select g.LabelID,COUNT(*) goodsnum from W_GoodsLabel g
			where exists(
			select 1 from W_Goods gd 
 			inner join WD_Ordersdtl od on gd.Code=od.code
 			inner join WD_Orders o on o.OrderID=od.orderid
 			where g.GoodsID=gd.GoodsID and ${conditions}
			)	
 			group by g.LabelID
		)
		as goodslbabel on l.labelid=goodslbabel.labelid
		left join(
			select  vt.LabelID,COUNT(*) vipsum from (
				select distinct g.LabelID,o.Vipid from W_GoodsLabel g
				inner join W_Goods gd on g.GoodsID=gd.GoodsID
 				inner join WD_Ordersdtl od on gd.Code=od.code
 				inner join WD_Orders o on o.OrderID=od.orderid
				where  ${conditions} and vipid <![CDATA[<>]]>'-111'
				 
 			) vt GROUP by vt.LabelID
		) as
		viplabel on l.labelid=viplabel.labelid   where l.LabelType=2
	</select>

	<select id="selectgoodslabeldetail" resultType="java.util.LinkedHashMap">
		with temp as ( select tb.Code,tb.GoodsNum,SUM(dt.Quantity) as totalQty
		from (
		select od.Code,sum(quantity) as GoodsNum
		from WD_Ordersdtl od with(nolock)
		inner join WD_Orders o with(nolock) on od.OrderID = o.OrderID
		WHERE ${date} and vipid <![CDATA[<>]]>'-111'
		group by od.Code
		) tb inner join WD_Ordersdtl dt on dt.code=tb.Code
		group by tb.Code,tb.GoodsNum)
		select l.LabelName,temp.GoodsNum,g.goodsid
		,temp.totalQty,gd.GoodName,gd.ImgPath,gd.Code,gt.GoodsType,gd.Price,other.LabelName
		as OtherName
		from W_GoodsLabel g with(nolock)
		inner join W_Label l with(nolock) on l.LabelID=g.LabelID
		INNER JOIN W_Goods gd with(nolock) on gd.goodsid=g.goodsid
		left join GoodsType gt on gt.GoodsTypeID=gd.GoodsTypeID
		inner join temp on gd.Code=temp.Code
		join (SELECT g.goodsid,
		LabelName = (STUFF(( SELECT '，' + b.labelname
		FROM W_GoodsLabel a join W_Label b on a.labelid=b.labelid
		WHERE goodsid = g.goodsid 
		FOR
		XML PATH('')
		), 1, 1, ''))
		FROM W_GoodsLabel AS g join W_Goods gd on gd.goodsid=g.goodsid
		where ${labelidStr} 
		GROUP BY g.goodsid ) other on other.goodsid=g.goodsid
	</select>
</mapper>