<?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.th.demo.mapping.maint.AreaMapper" >
    <resultMap id="BaseResultMap" type="com.th.demo.model.maint.Area" >
        <id column="id" property="id" jdbcType="VARCHAR" />

        <result column="name" property="name" jdbcType="VARCHAR" />
        <result column="code" property="code" jdbcType="VARCHAR" />
        <result column="add_id" property="addId" jdbcType="VARCHAR" />
        <result column="add_time" property="addTime" jdbcType="TIMESTAMP" />
        <result column="modify_id" property="modifyId" jdbcType="VARCHAR" />
        <result column="modify_time" property="modifyTime" jdbcType="TIMESTAMP" />
        <result column="cancel_id" property="cancelId" jdbcType="VARCHAR" />
        <result column="cancel_time" property="cancelTime" jdbcType="TIMESTAMP" />
        <result column="cancel_flag" property="cancelFlag" jdbcType="VARCHAR" />
        <result column="belong_id" property="belongId" jdbcType="VARCHAR" />

        <association  column="ware_id" property="ware" select="com.th.demo.mapping.maint.WareMapper.selectByPrimaryKey" />
    </resultMap>
    <sql id="Base_Column_List" >
        id, ware_id, name, code, add_id, add_time, modify_id, modify_time, cancel_id, cancel_time,
    cancel_flag, belong_id
    </sql>

    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
        select
        <include refid="Base_Column_List" />
        from t_maint_area
        where id = #{param1,jdbcType=VARCHAR}
    </select>

    <select id="selectByWareId" resultMap="BaseResultMap" >
        select t.*
        from t_maint_area t
        where t.ware_id =  #{param1,jdbcType=VARCHAR}
          and t.belong_id =  #{param2,jdbcType=VARCHAR}
          and ifnull(t.cancel_flag,'0') = '0'
        order by t.code
    </select>

    <update id="updateByPrimaryKey" parameterType="com.th.demo.model.maint.Area" >
        update t_maint_area
        set ware_id = #{ware.id,jdbcType=VARCHAR},
            name = #{name,jdbcType=VARCHAR},
            code = #{code,jdbcType=VARCHAR},
            add_id = #{addId,jdbcType=VARCHAR},
            add_time = #{addTime,jdbcType=TIMESTAMP},
            modify_id = #{modifyId,jdbcType=VARCHAR},
            modify_time = #{modifyTime,jdbcType=TIMESTAMP},
            cancel_id = #{cancelId,jdbcType=VARCHAR},
            cancel_time = #{cancelTime,jdbcType=TIMESTAMP},
            cancel_flag = #{cancelFlag,jdbcType=VARCHAR},
            belong_id = #{belongId,jdbcType=VARCHAR}
        where id = #{id,jdbcType=VARCHAR}
    </update>

    <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
        delete from t_maint_area
        where id = #{param1,jdbcType=VARCHAR}
    </delete>

    <insert id="insert" parameterType="com.th.demo.model.maint.Area" >
        <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String">
            SELECT  REPLACE(UUID(),'-','') as id from dual
        </selectKey>


        insert into t_maint_area (id, ware_id, name,
        code, add_id, add_time,
        modify_id, modify_time, cancel_id,
        cancel_time, cancel_flag, belong_id)
        values (#{id,jdbcType=VARCHAR}, #{ware.id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR},
        #{code,jdbcType=VARCHAR}, #{addId,jdbcType=VARCHAR}, #{addTime,jdbcType=TIMESTAMP},
        #{modifyId,jdbcType=VARCHAR}, #{modifyTime,jdbcType=TIMESTAMP}, #{cancelId,jdbcType=VARCHAR},
        #{cancelTime,jdbcType=TIMESTAMP}, #{cancelFlag,jdbcType=VARCHAR}, #{belongId,jdbcType=VARCHAR})
    </insert>


    <update id="updateByPrimaryKeySelective" parameterType="com.th.demo.model.maint.Area" >
        update t_maint_stack
        <set >
            <if test="wareId != null" >
                ware_id = #{wareId,jdbcType=VARCHAR},
            </if>
            <if test="name != null" >
                name = #{name,jdbcType=VARCHAR},
            </if>
            <if test="code != null" >
                code = #{code,jdbcType=VARCHAR},
            </if>
            <if test="addId != null" >
                add_id = #{addId,jdbcType=VARCHAR},
            </if>
            <if test="addTime != null" >
                add_time = #{addTime,jdbcType=TIMESTAMP},
            </if>
            <if test="modifyId != null" >
                modify_id = #{modifyId,jdbcType=VARCHAR},
            </if>
            <if test="modifyTime != null" >
                modify_time = #{modifyTime,jdbcType=TIMESTAMP},
            </if>
            <if test="cancelId != null" >
                cancel_id = #{cancelId,jdbcType=VARCHAR},
            </if>
            <if test="cancelTime != null" >
                cancel_time = #{cancelTime,jdbcType=TIMESTAMP},
            </if>
            <if test="cancelFlag != null" >
                cancel_flag = #{cancelFlag,jdbcType=VARCHAR},
            </if>
            <if test="belongId != null" >
                belong_id = #{belongId,jdbcType=VARCHAR},
            </if>
        </set>
        where id = #{id,jdbcType=VARCHAR}
    </update>



    <select id="selectByWareNameStackName" resultMap="BaseResultMap" >
        select t.*
        from t_maint_stack t ,t_maint_ware d
        where t.ware_id = d.id
          and d.id in  (
            select  a.ware_id from t_maint_userware a where  a.user_id = #{param3,jdbcType=VARCHAR}
        )  and d.name = #{param1,jdbcType=VARCHAR}
          and t.name = #{param2,jdbcType=VARCHAR}
          and   t.belong_id = #{param4,jdbcType=VARCHAR}
          and ifnull(t.cancel_flag,'0') = '0'
        order by t.code
    </select>
</mapper>