<?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.tmerclub.cloud.flow.mapper.FormMapper">
  <resultMap id="formMap" type="com.tmerclub.cloud.flow.model.Form">
    <id column="form_id" property="formId" />
    <result column="shop_id" property="shopId"/>
    <result column="form_name" property="formName"/>
    <result column="content" property="content"/>
    <result column="from_type" property="fromType"/>
    <result column="time_type" property="timeType"/>
    <result column="time_format" property="timeFormat"/>
    <result column="form_item_ids" property="formItemIds"/>
    <result column="start_time" property="startTime"/>
    <result column="end_time" property="endTime"/>
    <result column="time_range" property="timeRange"/>
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="seq" property="seq"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `form_id`,`shop_id`,`form_name`,`content`,`from_type`,`time_type`,`time_format`,`form_item_ids`,`start_time`,`end_time`,`time_range`,`create_time`,`update_time`,`seq`
  </sql>
  <select id="list" resultMap="formMap">
    select <include refid="Vo_Column_List"/>
    from form
    <where>
      <if test="formParam.formName != null and formParam.formName != ''">
        trim(replace(form_name,' ','')) like concat('%', #{formParam.formName}, '%')
      </if>
      <if test="formParam.shopId != null">
        and shop_id = #{formParam.shopId}
      </if>
      <if test="formParam.timeType != null">
        and time_type = #{formParam.timeType}
      </if>
      <if test="formParam.recommendForm">
        and shop_id is null
      </if>
    </where>
    order by seq ASC, update_time DESC
  </select>
  <select id="getByFormId" resultMap="formMap">
    select <include refid="Vo_Column_List"/> from form where form_id = #{formId}
  </select>
  <insert id="save">
    insert into form
    (`shop_id`,`form_name`,`content`,`from_type`,`time_type`,`time_format`,`form_item_ids`,`start_time`,`end_time`,`time_range`,`seq`)
    values
    (#{form.shopId},#{form.formName},#{form.content},#{form.fromType},#{form.timeType},#{form.timeFormat},#{form.formItemIds},#{form.startTime},#{form.endTime},#{form.timeRange},#{form.seq});
  </insert>
  <update id="update">
    update form
    <set>
      <if test="form.shopId != null">
        `shop_id` = #{form.shopId},
      </if>
      <if test="form.formName != null">
        `form_name` = #{form.formName},
      </if>
      <if test="form.content != null">
        `content` = #{form.content},
      </if>
      <if test="form.fromType != null">
        `from_type` = #{form.fromType},
      </if>
      <if test="form.timeType != null">
        `time_type` = #{form.timeType},
      </if>
      <if test="form.timeFormat != null">
        `time_format` = #{form.timeFormat},
      </if>
      <if test="form.formItemIds != null">
        `form_item_ids` = #{form.formItemIds},
      </if>
      <if test="form.startTime != null">
        `start_time` = #{form.startTime},
      </if>
      <if test="form.endTime != null">
        `end_time` = #{form.endTime},
      </if>
      <if test="form.timeRange != null">
        `time_range` = #{form.timeRange},
      </if>
      <if test="form.seq != null">
        `seq` = #{form.seq},
      </if>
    </set>
    where form_id = #{form.formId}
  </update>
  <delete id="deleteById">
    delete from form where form_id = #{formId}
  </delete>

</mapper>
