<?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.ym.mec.biz.dal.dao.OrganizationDao">

    <resultMap type="com.ym.mec.biz.dal.entity.Organization" id="Organization">
        <result column="id_" property="id"/>
        <result column="name_" property="name"/>
        <result column="area_name_" property="areaName"/>
        <result column="area_id_" property="areaId"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="del_flag_" property="delFlag" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="register_date_" property="registerDate"/>
        <result column="linkman_" property="linkman"/>
        <result column="mobile_" property="mobile"/>
        <result column="address_" property="address"/>
    </resultMap>

    <!-- 根据主键查询一条记录 -->
    <select id="get" resultMap="Organization">
		SELECT * FROM organization WHERE id_ = #{id} 
	</select>

    <!-- 全查询 -->
    <select id="findAll" resultMap="Organization">
		SELECT * FROM organization ORDER BY id_
	</select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.Organization" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        INSERT INTO organization (id_,name_,area_id_,create_time_,update_time_,register_date_,linkman_,mobile_,address_)
        VALUES(#{id},#{name},#{areaId},now(),now(),#{registerDate},#{linkman},#{mobile},#{address})
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.Organization">
        UPDATE organization
        <set>
            <if test="delFlag != null">
                del_flag_ = #{delFlag,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="areaId != null">
                area_id_ = #{areaId},
            </if>
            <if test="updateTime != null">
                update_time_ = #{updateTime},
            </if>
            <if test="name != null">
                name_ = #{name},
            </if>
            <if test="registerDate != null">
                register_date_ = #{registerDate},
            </if>
            <if test="linkman != null">
                linkman_ = #{linkman},
            </if>
            <if test="mobile != null">
                mobile_ = #{mobile},
            </if>
            <if test="address != null">
                address_ = #{address},
            </if>
        </set>
        WHERE id_ = #{id}
    </update>

    <!-- 根据主键删除一条记录 -->
    <delete id="delete">
		UPDATE organization SET del_flag_ = 1 WHERE id_ = #{id}
	</delete>

    <sql id="queryPageSql">
        <where>
            <if test="delFlag != null">
                o.del_flag_ = #{delFlag,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
        </where>
    </sql>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="Organization" parameterType="map">
        SELECT o.*,sa.name_ area_name_ FROM organization o LEFT JOIN sys_area sa ON o.area_id_ = sa.id_
        <include refid="queryPageSql"/>
         ORDER BY id_
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
		SELECT COUNT(o.id_) FROM organization o <include refid="queryPageSql"/>
	</select>

    <select id="findByParentId" resultMap="Organization">
        SELECT o.*,sa.name_ area_name_ FROM organization o
        LEFT JOIN sys_area sa ON o.area_id_ = sa.id_
        <include refid="queryPageSql"/>
    </select>

    <select id="findByOrganIds" resultType="java.lang.String">
        SELECT name_ FROM organization WHERE del_flag_ = 0 AND FIND_IN_SET(id_,#{organIds})
    </select>
</mapper>
