<?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.zm.order.tool.server.dao.OrderStatusFlowsDao">
    <resultMap id="OrderStatusFlowsMap" type="com.zm.order.tool.server.model.OrderStatusFlows">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="biz_type" jdbcType="INTEGER" property="bizType"/>
        <result column="doc_type" jdbcType="INTEGER" property="docType"/>
        <result column="before_status" jdbcType="INTEGER" property="beforeStatus"/>
        <result column="operation" jdbcType="VARCHAR" property="operation"/>
        <result column="after_status" jdbcType="INTEGER" property="afterStatus"/>
        <result column="created_at" jdbcType="TIMESTAMP" property="createdAt"/>
        <result column="updated_at" jdbcType="TIMESTAMP" property="updatedAt"/>
        <result column="is_deleted" jdbcType="INTEGER" property="isDeleted"/>
        <result column="is_activated" jdbcType="INTEGER" property="isActivated"/>
    </resultMap>

    <sql id="tb">
        order_status_flows
    </sql>

    <sql id="cols_all">
        id,
        <include refid="cols_exclude_id"/>
    </sql>

    <sql id="cols_exclude_id">
        biz_type, doc_type, before_status, operation, after_status, created_at, updated_at, is_deleted, is_activated
    </sql>

    <sql id="values">
      #{bizType}, #{docType},
      #{beforeStatus}, #{operation}, #{afterStatus},
       now(), now(), 0, 0
  </sql>

    <insert id="insert" parameterType="com.zm.order.tool.server.model.OrderStatusFlows" keyColumn="id" keyProperty="id"
            useGeneratedKeys="true">
        insert into
        <include refid="tb"/>
        (<include refid="cols_exclude_id"/>)
        values
        (<include refid="values"/>)
    </insert>

    <select id="selectList" parameterType="map" resultMap="OrderStatusFlowsMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
            <if test="bizType != null  ">
                biz_type = #{bizType}
            </if>
            <if test="docType != null ">
                and doc_type = #{docType}
            </if>
            <if test="beforeStatus != null">
                and before_status = #{beforeStatus}
            </if>
            <if test="operation != null and operation !=''">
                and operation = #{operation}
            </if>
            <if test="afterStatus != null">
                and after_status = #{afterStatus}
            </if>
            and is_deleted =0
        </where>
        order by updated_at desc, id asc
        <if test="start != null">
            limit ${start}, ${pageSize}
        </if>
    </select>

    <select id="count" parameterType="map" resultType="int">
        select
       count(1)
        from
        <include refid="tb"/>
        <where>
            <if test="bizType != null">
                biz_type = #{bizType}
            </if>
            <if test="docType != null">
                and doc_type = #{docType}
            </if>
            <if test="beforeStatus != null">
                and before_status = #{beforeStatus}
            </if>
            <if test="operation != null and operation !='' ">
                and operation = #{operation}
            </if>
            <if test="afterStatus != null">
                and after_status = #{afterStatus}
            </if>
            and is_deleted =0
        </where>
    </select>


    <update id="update" parameterType="com.zm.order.tool.server.model.OrderStatusFlows">
        update
        <include refid="tb"/>
        set
        biz_type = #{bizType},
        doc_type= #{docType},
        before_status =#{beforeStatus},
        operation =#{operation},
        after_status =#{afterStatus},
        updated_at = now()
        where
        id =#{id}
    </update>


    <insert id="inserts" parameterType="list">
        insert into
        <include refid="tb"/>
        (<include refid="cols_exclude_id"/>)
        values
        <foreach collection="orderStatusFlowsList" item="orderStatusFlows" index="index" separator=",">
            (
             #{orderStatusFlows.bizType},  #{orderStatusFlows.docType}, #{orderStatusFlows.beforeStatus}, #{orderStatusFlows.operation},
            #{orderStatusFlows.afterStatus},   now(), now(), 0,0
            )
        </foreach>
    </insert>


    <update id="activated" parameterType="list">
        update
        <include refid="tb"/>
        set
        is_activated = 1,
        updated_at = now()
        where
        id in
        <foreach collection="idList" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </update>



    <delete id="deleted" parameterType="list">
        delete
        from
        <include refid="tb"/>
        where
        id in
        <foreach collection="idList" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>


    <select id="selectListById" parameterType="map" resultMap="OrderStatusFlowsMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
       where
        id in
        <foreach collection="idList" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>

    </select>


</mapper>