<?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.WMicroformMapper">
  <resultMap id="BaseResultMap" type="com.fuxi.ws.data.entity.WMicroform">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Jul 04 12:31:33 CST 2019.
    -->
    <id column="FormID" jdbcType="INTEGER" property="formid" />
    <result column="FormTitle" jdbcType="NVARCHAR" property="formtitle" />
    <result column="Summary" jdbcType="NVARCHAR" property="summary" />
    <result column="FormUrl" jdbcType="VARCHAR" property="formurl" />
    <result column="MouldID" jdbcType="SMALLINT" property="mouldid" />
    <result column="ShowNavigation" jdbcType="TINYINT" property="shownavigation" />
    <result column="ShowMenu" jdbcType="TINYINT" property="showmenu" />
    <result column="NavigationType" jdbcType="TINYINT" property="navigationtype" />
    <result column="ShowIndex" jdbcType="SMALLINT" property="showindex" />
    <result column="Status" jdbcType="TINYINT" property="status" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Jul 04 12:31:33 CST 2019.
    -->
    <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 - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Jul 04 12:31:33 CST 2019.
    -->
    FormID, FormTitle, Summary, FormUrl, MouldID, ShowNavigation, ShowMenu, NavigationType, 
    ShowIndex, Status
  </sql>
  <select id="selectByExample" parameterType="com.fuxi.ws.data.entity.WMicroformExample" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Jul 04 12:31:33 CST 2019.
    -->
    select
    <if test="distinct">
      distinct
    </if>
    'false' as QUERYID,
    <include refid="Base_Column_List" />
    from W_MicroForm
    <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 - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Jul 04 12:31:33 CST 2019.
    -->
    select 
    <include refid="Base_Column_List" />
    from W_MicroForm
    where FormID = #{formid,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Jul 04 12:31:33 CST 2019.
    -->
    delete from W_MicroForm
    where FormID = #{formid,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.fuxi.ws.data.entity.WMicroform">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Jul 04 12:31:33 CST 2019.
    -->
    insert into W_MicroForm (FormID, FormTitle, Summary, 
      FormUrl, MouldID, ShowNavigation, 
      ShowMenu, NavigationType, ShowIndex, 
      Status)
    values (#{formid,jdbcType=INTEGER}, #{formtitle,jdbcType=NVARCHAR}, #{summary,jdbcType=NVARCHAR}, 
      #{formurl,jdbcType=VARCHAR}, #{mouldid,jdbcType=SMALLINT}, #{shownavigation,jdbcType=TINYINT}, 
      #{showmenu,jdbcType=TINYINT}, #{navigationtype,jdbcType=TINYINT}, #{showindex,jdbcType=SMALLINT}, 
      #{status,jdbcType=TINYINT})
  </insert>
  <insert id="insertSelective" parameterType="com.fuxi.ws.data.entity.WMicroform">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Jul 04 12:31:33 CST 2019.
    -->
    insert into W_MicroForm
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="formid != null">
        FormID,
      </if>
      <if test="formtitle != null">
        FormTitle,
      </if>
      <if test="summary != null">
        Summary,
      </if>
      <if test="formurl != null">
        FormUrl,
      </if>
      <if test="mouldid != null">
        MouldID,
      </if>
      <if test="shownavigation != null">
        ShowNavigation,
      </if>
      <if test="showmenu != null">
        ShowMenu,
      </if>
      <if test="navigationtype != null">
        NavigationType,
      </if>
      <if test="showindex != null">
        ShowIndex,
      </if>
      <if test="status != null">
        Status,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="formid != null">
        #{formid,jdbcType=INTEGER},
      </if>
      <if test="formtitle != null">
        #{formtitle,jdbcType=NVARCHAR},
      </if>
      <if test="summary != null">
        #{summary,jdbcType=NVARCHAR},
      </if>
      <if test="formurl != null">
        #{formurl,jdbcType=VARCHAR},
      </if>
      <if test="mouldid != null">
        #{mouldid,jdbcType=SMALLINT},
      </if>
      <if test="shownavigation != null">
        #{shownavigation,jdbcType=TINYINT},
      </if>
      <if test="showmenu != null">
        #{showmenu,jdbcType=TINYINT},
      </if>
      <if test="navigationtype != null">
        #{navigationtype,jdbcType=TINYINT},
      </if>
      <if test="showindex != null">
        #{showindex,jdbcType=SMALLINT},
      </if>
      <if test="status != null">
        #{status,jdbcType=TINYINT},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.fuxi.ws.data.entity.WMicroformExample" resultType="java.lang.Long">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Jul 04 12:31:33 CST 2019.
    -->
    select count(*) from W_MicroForm
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByPrimaryKeySelective" parameterType="com.fuxi.ws.data.entity.WMicroform">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Jul 04 12:31:33 CST 2019.
    -->
    update W_MicroForm
    <set>
      <if test="formtitle != null">
        FormTitle = #{formtitle,jdbcType=NVARCHAR},
      </if>
      <if test="summary != null">
        Summary = #{summary,jdbcType=NVARCHAR},
      </if>
      <if test="formurl != null">
        FormUrl = #{formurl,jdbcType=VARCHAR},
      </if>
      <if test="mouldid != null">
        MouldID = #{mouldid,jdbcType=SMALLINT},
      </if>
      <if test="shownavigation != null">
        ShowNavigation = #{shownavigation,jdbcType=TINYINT},
      </if>
      <if test="showmenu != null">
        ShowMenu = #{showmenu,jdbcType=TINYINT},
      </if>
      <if test="navigationtype != null">
        NavigationType = #{navigationtype,jdbcType=TINYINT},
      </if>
      <if test="showindex != null">
        ShowIndex = #{showindex,jdbcType=SMALLINT},
      </if>
      <if test="status != null">
        Status = #{status,jdbcType=TINYINT},
      </if>
    </set>
    where FormID = #{formid,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.fuxi.ws.data.entity.WMicroform">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Jul 04 12:31:33 CST 2019.
    -->
    update W_MicroForm
    set FormTitle = #{formtitle,jdbcType=NVARCHAR},
      Summary = #{summary,jdbcType=NVARCHAR},
      FormUrl = #{formurl,jdbcType=VARCHAR},
      MouldID = #{mouldid,jdbcType=SMALLINT},
      ShowNavigation = #{shownavigation,jdbcType=TINYINT},
      ShowMenu = #{showmenu,jdbcType=TINYINT},
      NavigationType = #{navigationtype,jdbcType=TINYINT},
      ShowIndex = #{showindex,jdbcType=SMALLINT},
      Status = #{status,jdbcType=TINYINT}
    where FormID = #{formid,jdbcType=INTEGER}
  </update>
	
	<!-- 读取微页面列表信息 -->
	<select id="mocroformList" resultType="java.util.LinkedHashMap">		
		SELECT a.FormID, a.FormTitle, a.FormUrl,isnull(a.Summary,'') Summary,a.MouldID,a.ShowNavigation,a.ShowMenu,isnull(a.NavigationType,1) NavigationType,
		(select cast(count(*) as varchar) from
		W_GoodsPv p where p.FormID=a.FormID) FormPv,
		(select cast(count(*) as varchar) from
		W_UV u where u.FormID=a.FormID and u.Type=2) FormUv,
		(select top 1 t.ShowArea from W_Showset t where t.FormID=a.FormID) ShowArea
		FROM W_MicroForm AS a
		<if test="conditions != null and conditions != ''">
			where
			${conditions}
		</if>
		order by a.ShowIndex asc,a.FormID desc		
	</select>

	<!-- 获取微页面ID -->
	<select id="getFormID" resultType="java.lang.Integer">
		SELECT isnull(max(FormID),4) + 1 as FormID
		FROM 
		(select Formid from W_ShowSetso
		union all
		select Formid from W_ShowSet
		union all
		select Formid from W_MicroForm
		) a		
	</select>
	
	<update id="setMicroFromOn">		
		delete W_ShowSet 
		where FormID = #{formid,jdbcType=INTEGER}
		
		insert into W_ShowSet (ShowSetID, ShowSetName, DepartmentID, ShowSetDec, FormID, ShowArea, 
			ShowIndex, ShowGoodsName, ShowTag, ShowPirce, ShowLabel, LabelPlace, 
            LabelUrl, CartStyle, ShowKind,MouldID,NumOfRow, OrderBy, Creater, Created, Modifier, Modified)
        select a.ShowSetID,a.ShowSetName,a.DepartmentID,a.ShowSetDec,a.FormID,a.ShowArea, 
			a.ShowIndex,a.ShowGoodsName,a.ShowTag,a.ShowPirce,a.ShowLabel,a.LabelPlace, 
            a.LabelUrl,a.CartStyle,a.ShowKind,a.MouldID,a.NumOfRow,a.OrderBy,a.Creater,a.Created,a.Modifier,a.Modified
        from W_ShowSetso a inner join 
        W_MicroForm b on a.FormID = b.FormID
        where a.FormID = #{formid,jdbcType=INTEGER}
        
        insert into W_ShowSetline (ShowSetID, LineID, LineIndex, ShowStyle)
        select a.ShowSetID, a.LineID, a.LineIndex, a.ShowStyle
        from W_ShowSetlineso a inner join
        W_ShowSetso b on a.ShowSetID=b.ShowSetID
        where b.FormID = #{formid,jdbcType=INTEGER}
        
        insert into W_ShowSetDtl (ShowSetID, LineID, ShowSetDtlId, ClumIndex, GoodsTypeID, 
        	GoodsID, ImgPath, FocusImg, ADUrl, TicketID, TicketStyle, Modified)
        select a.ShowSetID, a.LineID, a.ShowSetDtlId, a.ClumIndex, a.GoodsTypeID, a.GoodsID, 
        	a.ImgPath, a.FocusImg, a.ADUrl, a.TicketID, a.TicketStyle, a.Modified
        from W_ShowSetDtlso a inner join
        W_ShowSetso b on a.ShowSetID=b.ShowSetID
        where b.FormID = #{formid,jdbcType=INTEGER}
        
		update W_MicroForm	set	Status = 1
		where FormID = #{formid,jdbcType=INTEGER}
	</update>
	
	<update id="setMicroFromOff">		
		delete W_ShowSet 
		where FormID = #{formid,jdbcType=INTEGER}
        
		update W_MicroForm	set	Status = 0
		where FormID = #{formid,jdbcType=INTEGER}
	</update>
	
	<update id="setAsFirstPage">
	    update W_MicroForm	set	ShowIndex=1
		where ShowIndex = 0
		
		update W_MicroForm	set	Status = 1,ShowIndex=0
		where FormID = #{formid,jdbcType=INTEGER}
			
		update W_ShowSet set ShowArea=(case formid when 1 then 0 else formid end)
		where ShowArea=1
				
		update W_ShowSetSo set ShowArea=(case when FormID=#{formid,jdbcType=INTEGER} then 1 else (case formid when 1 then 0 else formid end) end)
				
		delete W_ShowSet 
		where FormID = #{formid,jdbcType=INTEGER}
		
		insert into W_ShowSet (ShowSetID, ShowSetName, DepartmentID, ShowSetDec, FormID, ShowArea, 
			ShowIndex, ShowGoodsName, ShowTag, ShowPirce, ShowLabel, LabelPlace, 
            LabelUrl, CartStyle, ShowKind,MouldID,NumOfRow,OrderBy, Creater, Created, Modifier, Modified)
        select a.ShowSetID,a.ShowSetName,a.DepartmentID,a.ShowSetDec,a.FormID,a.ShowArea, 
			a.ShowIndex,a.ShowGoodsName,a.ShowTag,a.ShowPirce,a.ShowLabel,a.LabelPlace, 
            a.LabelUrl,a.CartStyle,a.ShowKind,a.MouldID,a.NumOfRow,a.OrderBy,a.Creater,a.Created,a.Modifier,a.Modified
        from W_ShowSetso a inner join 
        W_MicroForm b on a.FormID = b.FormID
        where a.FormID = #{formid,jdbcType=INTEGER}
        
        insert into W_ShowSetline (ShowSetID, LineID, LineIndex, ShowStyle)
        select a.ShowSetID, a.LineID, a.LineIndex, a.ShowStyle
        from W_ShowSetlineso a inner join
        W_ShowSetso b on a.ShowSetID=b.ShowSetID
        where b.FormID = #{formid,jdbcType=INTEGER}
        
        insert into W_ShowSetDtl (ShowSetID, LineID, ShowSetDtlId, ClumIndex, GoodsTypeID, 
        	GoodsID, ImgPath, FocusImg, ADUrl, TicketID, TicketStyle, Modified)
        select a.ShowSetID, a.LineID, a.ShowSetDtlId, a.ClumIndex, a.GoodsTypeID, a.GoodsID, 
        	a.ImgPath, a.FocusImg, a.ADUrl, a.TicketID, a.TicketStyle, a.Modified
        from W_ShowSetDtlso a inner join
        W_ShowSetso b on a.ShowSetID=b.ShowSetID
        where b.FormID = #{formid,jdbcType=INTEGER}
	</update>
	
	
</mapper>