<?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.neusoft.smart.pos.org.persistent.mapper.OrgMapper">
    <resultMap id="BaseResultMap" type="com.neusoft.smart.pos.org.persistent.domain.Org">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="linkman" jdbcType="VARCHAR" property="linkman"/>
        <result column="phone" jdbcType="VARCHAR" property="phone"/>
        <result column="comment" jdbcType="VARCHAR" property="comment"/>
        <result column="create_date" jdbcType="TIMESTAMP" property="createDate"/>
        <result column="shopnum" jdbcType="INTEGER" property="shopnum"/>
        <result column="posnum" jdbcType="INTEGER" property="posnum"/>
        <result column="type" jdbcType="VARCHAR" property="type"/>
    </resultMap>
    <sql id="Example_Where_Clause">
        <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="Update_By_Example_Where_Clause">
        <where>
            <foreach collection="example.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">
    org.id, org.name, org.linkman, org.phone, org.comment, org.posnum, org.type, date_format(org.create_date, '%Y-%m-%d') as create_date
  </sql>
    <select id="selectByExample" parameterType="com.neusoft.smart.pos.org.persistent.domain.OrgExample"
            resultMap="BaseResultMap">
        select
        <if test="distinct">
            distinct
        </if>
        <include refid="Base_Column_List"/>
        , count(shop.id) as shopnum
        from `org`
        Left Join `shop` ON org.id = shop.org
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>            
        </if>
       	group by org.id
        <if test="orderByClause != null">
            order by ${orderByClause}
        </if>
        <if test="offset != null and limit != null">
            limit ${offset}, ${limit}
        </if>
    </select>
    <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        ,(select count(id) from `shop` where org = #{id,jdbcType=INTEGER}) as shopnum
        from `org`
        where id = #{id,jdbcType=INTEGER}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from `org`
    where id = #{id,jdbcType=INTEGER}
  </delete>
    <delete id="deleteByExample" parameterType="com.neusoft.smart.pos.org.persistent.domain.OrgExample">
        delete from `org`
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>
        </if>
    </delete>
    <insert id="insert" parameterType="com.neusoft.smart.pos.org.persistent.domain.Org">
    insert into `org` (id, name, linkman, 
      phone, comment, type)
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{linkman,jdbcType=VARCHAR}, 
      #{phone,jdbcType=VARCHAR}, #{comment,jdbcType=VARCHAR}, #{type,jdbcType=VARCHAR})
  </insert>
    <insert id="insertSelective" parameterType="com.neusoft.smart.pos.org.persistent.domain.Org">
    	<selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="id">
          SELECT LAST_INSERT_ID()
      	</selectKey>
        insert into `org`
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="name != null">
                name,
            </if>
            <if test="linkman != null">
                linkman,
            </if>
            <if test="phone != null">
                phone,
            </if>
            <if test="comment != null">
                comment,
            </if>
            <if test="type != null">
                type,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=INTEGER},
            </if>
            <if test="name != null">
                #{name,jdbcType=VARCHAR},
            </if>
            <if test="linkman != null">
                #{linkman,jdbcType=VARCHAR},
            </if>
            <if test="phone != null">
                #{phone,jdbcType=VARCHAR},
            </if>
            <if test="comment != null">
                #{comment,jdbcType=VARCHAR},
            </if>
            <if test="type != null">
                #{type,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>
    <select id="countByExample" parameterType="com.neusoft.smart.pos.org.persistent.domain.OrgExample"
            resultType="java.lang.Integer">
        select count(*) from `org`
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>
        </if>
    </select>
    <update id="updateByExampleSelective" parameterType="map">
        update `org`
        <set>
            <if test="record.id != null">
                id = #{record.id,jdbcType=INTEGER},
            </if>
            <if test="record.name != null">
                name = #{record.name,jdbcType=VARCHAR},
            </if>
            <if test="record.linkman != null">
                linkman = #{record.linkman,jdbcType=VARCHAR},
            </if>
            <if test="record.phone != null">
                phone = #{record.phone,jdbcType=VARCHAR},
            </if>
            <if test="record.comment != null">
                comment = #{record.comment,jdbcType=VARCHAR},
            </if>
            <if test="record.posnum != null">
                posnum = #{record.posnum,jdbcType=INTEGER},
            </if>
            <if test="record.status != null">
                status = #{record.status,jdbcType=VARCHAR},
            </if>
            <if test="record.type != null">
                type = #{record.type,jdbcType=VARCHAR},
            </if>
        </set>
        <if test="_parameter != null">
            <include refid="Update_By_Example_Where_Clause"/>
        </if>
    </update>
    <update id="updateByExample" parameterType="map">
        update `org`
        set id = #{record.id,jdbcType=INTEGER},
        name = #{record.name,jdbcType=VARCHAR},
        linkman = #{record.linkman,jdbcType=VARCHAR},
        phone = #{record.phone,jdbcType=VARCHAR},
        comment = #{record.comment,jdbcType=VARCHAR},
        posnum = #{record.posnum,jdbcType=INTEGER},
        status = #{record.status,jdbcType=VARCHAR},
        type = #{record.type,jdbcType=VARCHAR}
        <if test="_parameter != null">
            <include refid="Update_By_Example_Where_Clause"/>
        </if>
    </update>
    <update id="updateByPrimaryKeySelective" parameterType="com.neusoft.smart.pos.org.persistent.domain.Org">
        update `org`
        <set>
            <if test="name != null">
                name = #{name,jdbcType=VARCHAR},
            </if>
            <if test="linkman != null">
                linkman = #{linkman,jdbcType=VARCHAR},
            </if>
            <if test="phone != null">
                phone = #{phone,jdbcType=VARCHAR},
            </if>
            <if test="comment != null">
                comment = #{comment,jdbcType=VARCHAR},
            </if>
            <if test="posnum != null">
                posnum = #{posnum,jdbcType=INTEGER},
            </if>
            <if test="status != null">
                status = #{status,jdbcType=VARCHAR},
            </if>
            <if test="type != null">
                type = #{type,jdbcType=VARCHAR},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.neusoft.smart.pos.org.persistent.domain.Org">
    update `org`
    set name = #{name,jdbcType=VARCHAR},
      linkman = #{linkman,jdbcType=VARCHAR},
      phone = #{phone,jdbcType=VARCHAR},
      comment = #{comment,jdbcType=VARCHAR},
      posnum = #{posnum,jdbcType=INTEGER},
      status = #{status,jdbcType=VARCHAR},
      type = #{type,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>