<?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="MdeGoodsMapper" >
  <resultMap id="BaseResultMap" type="com.wechat.model.dbEntity.MdeGoods" >
    <id column="GOODS_ID" property="goodsId" jdbcType="INTEGER" />
    <result column="GOODS_NO" property="goodsNo" jdbcType="VARCHAR" />
    <result column="SUBHEAD_CN" property="subheadCn" jdbcType="VARCHAR" />
    <result column="SUBHEAD_EN" property="subheadEn" jdbcType="VARCHAR" />
    <result column="TITLE_CN" property="titleCn" jdbcType="VARCHAR" />
    <result column="TITLE_EN" property="titleEn" jdbcType="VARCHAR" />
    <result column="RESUME_CN" property="resumeCn" jdbcType="VARCHAR" />
    <result column="RESUME_EN" property="resumeEn" jdbcType="VARCHAR" />
    <result column="CATEGORY_ID" property="categoryId" jdbcType="INTEGER" />
    <result column="SELL_PRICE" property="sellPrice" jdbcType="DECIMAL" />
    <result column="MARKET_PRICE" property="marketPrice" jdbcType="DECIMAL" />
    <result column="COST_PRICE" property="costPrice" jdbcType="DECIMAL" />
    <result column="UP_TIME" property="upTime" jdbcType="TIMESTAMP" />
    <result column="DOWN_TIME" property="downTime" jdbcType="TIMESTAMP" />
    <result column="STORE_NUMS" property="storeNums" jdbcType="INTEGER" />
    <result column="IMG" property="img" jdbcType="VARCHAR" />
    <result column="AD_IMG" property="adImg" jdbcType="VARCHAR" />
    <result column="ON_SALE" property="onSale" jdbcType="CHAR" />
    <result column="KEYWORDS" property="keywords" jdbcType="VARCHAR" />
    <result column="DESCRIPTION" property="description" jdbcType="VARCHAR" />
    <result column="SEARCH_WORDS" property="searchWords" jdbcType="VARCHAR" />
    <result column="WEIGHT" property="weight" jdbcType="DECIMAL" />
    <result column="POINT" property="point" jdbcType="INTEGER" />
    <result column="UNIT_CN" property="unitCn" jdbcType="VARCHAR" />
    <result column="UNIT_EN" property="unitEn" jdbcType="VARCHAR" />
    <result column="BRAND_ID" property="brandId" jdbcType="INTEGER" />
    <result column="ORIGIN_ID" property="originId" jdbcType="INTEGER" />
    <result column="VISIT" property="visit" jdbcType="INTEGER" />
    <result column="FAVORITE" property="favorite" jdbcType="INTEGER" />
    <result column="SORT" property="sort" jdbcType="SMALLINT" />
    <result column="COMMENTS" property="comments" jdbcType="INTEGER" />
    <result column="SALE" property="sale" jdbcType="INTEGER" />
    <result column="GRADE" property="grade" jdbcType="INTEGER" />
    <result column="TYPE" property="type" jdbcType="TINYINT" />
    <result column="CONDITION_DATE" property="conditionDate" jdbcType="DATE" />
    <result column="CONDITION_VIP" property="conditionVip" jdbcType="CHAR" />
    <result column="CONDITION_TIME" property="conditionTime" jdbcType="INTEGER" />
    <result column="VERSION_NO" property="versionNo" jdbcType="INTEGER" />
    <result column="CREATE_BY" property="createBy" jdbcType="VARCHAR" />
    <result column="CREATE_DATE" property="createDate" jdbcType="TIMESTAMP" />
    <result column="UPDATE_BY" property="updateBy" jdbcType="VARCHAR" />
    <result column="UPDATE_DATE" property="updateDate" jdbcType="TIMESTAMP" />
    <result column="REMARKS" property="remarks" jdbcType="VARCHAR" />
    <result column="DEL_FLAG" property="delFlag" jdbcType="CHAR" />
    <result column="CONTENT" property="content" jdbcType="LONGVARCHAR" />
  </resultMap>
  
  <sql id="Base_Column_List" >
    GOODS_ID, GOODS_NO, SUBHEAD_CN, SUBHEAD_EN, TITLE_CN, TITLE_EN, RESUME_CN, RESUME_EN, CATEGORY_ID, 
    SELL_PRICE, MARKET_PRICE, COST_PRICE, UP_TIME, DOWN_TIME, STORE_NUMS, IMG, AD_IMG, 
    ON_SALE, KEYWORDS, DESCRIPTION, SEARCH_WORDS, WEIGHT, POINT, UNIT_CN, UNIT_EN, BRAND_ID, 
    ORIGIN_ID, VISIT, FAVORITE, SORT, COMMENTS, SALE, GRADE, TYPE, CONDITION_DATE, CONDITION_VIP, CONDITION_TIME,
    VERSION_NO, CREATE_BY, CREATE_DATE, UPDATE_BY, UPDATE_DATE, REMARKS, DEL_FLAG, CONTENT
  </sql>
  
  
  <select id="getUpTimeGroup" resultType="com.wechat.model.dto.UpTimeGroupDto" >
    SELECT DISTINCT
		case 
	   <![CDATA[ when TO_DAYS(a.UP_TIME) < TO_DAYS(NOW()) then DATE_SUB(NOW(), interval 1 day) 
			when TO_DAYS(a.UP_TIME) > TO_DAYS(NOW()) then DATE_ADD(NOW(), interval 1 day) ]]>
		else a.UP_TIME end	as upTime
		,a.ON_SALE   as onSale
	FROM
		mde_goods a 
	WHERE
		( a.ON_SALE = '1' OR a.ON_SALE = '2' ) 
		AND a.DEL_FLAG = '0'
	ORDER BY a.ON_SALE,a.UP_TIME
  </select>
  
  <select id="getUpTimeShow" parameterType="java.util.Date" resultType="com.wechat.model.dto.UpTimeGroupDto"  >
    SELECT
		upTime 
		,onSale
	FROM
		(
		SELECT
			max( a.UP_TIME ) AS upTime 
			,a.ON_SALE  as onSale
		FROM
			mde_goods a 
		WHERE
			a.ON_SALE = '1' 
			AND a.DEL_FLAG = '0' 
			AND TO_DAYS( a.UP_TIME ) = TO_DAYS(#{date}) 
		UNION
		SELECT
			min( a.UP_TIME ) AS upTime 
			,a.ON_SALE  as onSale
		FROM
			mde_goods a 
		WHERE
			a.ON_SALE = '2' 
			AND a.DEL_FLAG = '0' 
			AND TO_DAYS( a.UP_TIME ) = TO_DAYS(#{date}) 
		) x 
	ORDER BY
		IFNULL( x.onSale, '9' ) 
	LIMIT 1
  </select>
  
  
  <select id="getGoodsListByUpTime"  resultType="com.wechat.model.dto.GoodsListDto"  >
    SELECT
    	a.GOODS_ID          as  goodsId
    	<choose>
			<when test="lang != null and lang == 'en'.toString()">
				,a.SUBHEAD_EN        as  subhead
				,a.TITLE_EN          as  title
				,a.RESUME_EN         as  resume
				,a.UNIT_EN           as  unit
			</when>
			<otherwise>
				,a.SUBHEAD_CN        as  subhead
				,a.TITLE_CN          as  title
				,a.RESUME_CN         as  resume
				,a.UNIT_CN           as  unit
			</otherwise>
		</choose>
	    ,a.SELL_PRICE        as  sellPrice
	    ,a.MARKET_PRICE      as  marketPrice
	    ,a.SALE              as  sale
	    ,a.UP_TIME           as  upTime
	    ,a.STORE_NUMS        as  storeNums
	    ,a.IMG               as  img
	    ,a.ON_SALE           as  onSale
	    ,a.SORT              as  sort
	    ,a.TYPE              as  type
	    ,a.CONDITION_DATE    as  conditionDate
	    ,a.CONDITION_VIP     as  conditionVip
	    ,a.CONDITION_TIME    as  conditionTime
	FROM
		mde_goods a
	WHERE
			a.DEL_FLAG = '0' 
		AND a.ON_SALE  = #{onSale}
	<if test="upTime != null" >
        AND a.UP_TIME  = #{upTime}
    </if>
    <if test='upTime == null and  onSale == "1"' >
    	<!-- 热销商品 -->
       <![CDATA[  AND a.UP_TIME  <= #{date} ]]>
    </if>
	<if test='upTime == null and  onSale == "2"' >
		<!-- 预热商品 -->
       <![CDATA[  AND a.UP_TIME  >= #{date} ]]>
    </if>
	ORDER BY
		a.UP_TIME,a.ON_SALE, a.sort,a.CREATE_DATE desc
  </select>
  
  <select id="getGoodsDetail"  resultType="com.wechat.model.dto.GoodsDetailDto"  >
    SELECT
    	a.GOODS_ID          as  goodsId
    	<choose>
			<when test="lang != null and lang == 'en'.toString()">
				,a.SUBHEAD_EN        as  subhead
				,a.TITLE_EN          as  title
				,a.RESUME_EN         as  resume
				,a.UNIT_EN           as  unit
			</when>
			<otherwise>
				,a.SUBHEAD_CN        as  subhead
				,a.TITLE_CN          as  title
				,a.RESUME_CN         as  resume
				,a.UNIT_CN           as  unit
			</otherwise>
		</choose>
	    ,a.SELL_PRICE        as  sellPrice
	    ,a.MARKET_PRICE      as  marketPrice
	    ,a.UP_TIME           as  upTime
	    ,a.STORE_NUMS        as  storeNums
	    ,a.SALE              as  sale
	    ,a.IMG               as  img
	    ,a.AD_IMG            as  adImg
	    ,a.SHARE_IMG         as  shareImg
	    ,a.WEIGHT            as  weight
	    ,a.ON_SALE           as  onSale
	    ,a.SORT              as  sort
	    ,a.TYPE              as  type
	    ,a.CONTENT           as  content
	    ,a.CONDITION_DATE    as  conditionDate
	    ,a.CONDITION_VIP     as  conditionVip
	    ,a.CONDITION_TIME    as  conditionTime
	FROM
		mde_goods a
	WHERE
			a.DEL_FLAG = '0' 
		AND a.GOODS_ID  = #{goodsId}
  </select>
  
  <select id="getGoodsAllInfo"  resultMap="BaseResultMap"  >
    SELECT
    	<include refid="Base_Column_List" />
	FROM
		mde_goods a
	WHERE
			a.DEL_FLAG = '0' 
		AND a.GOODS_ID  = #{goodsId}
		AND a.ON_SALE  != '0' 
  </select>

  <select id="checkQualification" parameterType="java.util.Map" resultType="java.lang.Integer"  >
	    SELECT
			count(1)
		FROM
			mde_goods a 
		WHERE
			a.GOODS_ID  = #{goodsId}
			AND a.DEL_FLAG = '0' 
   <![CDATA[AND a.STORE_NUMS >= #{goodsNums} ]]>
	 	<if test="createDate != null" >
	 		<!-- 用户注册时间小于限制时间 -->
   <![CDATA[AND a.CONDITION_DATE  >= #{createDate} ]]>
	    </if>

  </select>
  
</mapper>