<?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.xunmei.system.mapper.SysOrgMapper">
    <resultMap type="com.xunmei.system.api.domain.SysOrg" id="SysOrgResult">
        <result property="id" column="id"/>
        <result property="address" column="address"/>
        <result property="code" column="code"/>
        <result property="guid" column="guid"/>
        <result property="isLock" column="is_lock"/>
        <result property="manager" column="manager"/>
        <result property="managerPhone" column="manager_phone"/>
        <result property="name" column="name"/>
        <result property="parentId" column="parent_id"/>
        <result property="phone" column="phone"/>
        <result property="cityCode" column="city_code"/>
        <result property="type" column="type"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="path" column="path"/>
        <result property="parentGuid" column="parent_guid"/>
        <result property="deleted" column="deleted"/>
        <result property="sort" column="sort"/>
        <result property="source" column="source"/>
        <result property="shortName" column="short_name"/>
        <result property="remark" column="remark"/>
        <result property="source" column="source"/>
        <result property="createBy" column="create_by"/>
        <result property="updateBy" column="update_by"/>
    </resultMap>

    <sql id="selectSysOrgVo">
        select id,
               address,
               code,
               guid,
               is_lock,
               manager,
               manager_phone,
               name,
               parent_id,
               phone,
               city_code,
               type,
               create_time,
               update_time,
               parent_guid,
               deleted,
               sort,
               create_by,
               update_by,
               weather_city_code,
               org_extend_id,
               source,
               short_name,
               remark
        from sys_org
    </sql>

    <select id="selectSysOrgList" parameterType="com.xunmei.system.api.domain.SysOrg"
            resultMap="SysOrgResult">
        <include refid="selectSysOrgVo"/>
        <where>
            <if test="address != null  and address != ''">
                and address = #{address}
            </if>
            <if test="code != null  and code != ''">
                and code = #{code}
            </if>
            <!--                        <if test="deviceCount != null ">-->
            <!--                            and device_count = #{deviceCount}-->
            <!--                        </if>-->
            <if test="guid != null  and guid != ''">
                and guid = #{guid}
            </if>
            <if test="isLock != null ">
                and is_lock = #{isLock}
            </if>
            <if test="manager != null  and manager != ''">
                and manager = #{manager}
            </if>
            <if test="managerPhone != null  and managerPhone != ''">
                and manager_phone = #{managerPhone}
            </if>
            <if test="name != null  and name != ''">
                and name like concat('%', #{name}, '%')
            </if>
            <if test="parentId != null ">
                and parent_id = #{parentId}
            </if>
            <if test="phone != null  and phone != ''">
                and phone = #{phone}
            </if>
            <if test="cityCode != null  and cityCode != ''">
                and city_code = #{cityCode}
            </if>
            <!--                        <if test="openYear != null ">-->
            <!--                            and open_year = #{openYear}-->
            <!--                        </if>-->
            <!--                        <if test="fitYear != null ">-->
            <!--                            and fit_year = #{fitYear}-->
            <!--                        </if>-->
            <!--                        <if test="floorage != null  and floorage != ''">-->
            <!--                            and floorage = #{floorage}-->
            <!--                        </if>-->
            <!--                        <if test="employee != null ">-->
            <!--                            and employee = #{employee}-->
            <!--                        </if>-->
            <!--                        <if test="isOwnRight != null ">-->
            <!--                            and is_own_right = #{isOwnRight}-->
            <!--                        </if>-->
            <!--                        <if test="isRent != null ">-->
            <!--                            and is_rent = #{isRent}-->
            <!--                        </if>-->
            <!--                        <if test="isOnly != null ">-->
            <!--                            and is_only = #{isOnly}-->
            <!--                        </if>-->
            <!--                        <if test="buildYear != null ">-->
            <!--                            and build_year = #{buildYear}-->
            <!--                        </if>-->
            <!--                        <if test="placeType != null ">-->
            <!--                            and place_type = #{placeType}-->
            <!--                        </if>-->
            <!--                        <if test="buildHeight != null ">-->
            <!--                            and build_height = #{buildHeight}-->
            <!--                        </if>-->
            <!--                        <if test="social != null ">-->
            <!--                            and social = #{social}-->
            <!--                        </if>-->
            <!--                        <if test="naturalEnvir != null ">-->
            <!--                            and natural_envir = #{naturalEnvir}-->
            <!--                        </if>-->
            <if test="type != null ">
                and type = #{type}
            </if>
            <if test="updateBy != null  and updateBy != ''">
                and update_by like concat('%', #{updateBy}, '%')
            </if>
            <!--                        <if test="modifiedId != null ">-->
            <!--                            and modified_id = #{modifiedId}-->
            <!--                        </if>-->
            <if test="path != null  and path != ''">
                and path = #{path}
            </if>
            <!--                        <if test="vid != null ">-->
            <!--                            and vid = #{vid}-->
            <!--                        </if>-->
            <!--                        <if test="level != null ">-->
            <!--                            and level = #{level}-->
            <!--                        </if>-->
            <!--                        <if test="oid != null  and oid != ''">-->
            <!--                            and oid = #{oid}-->
            <!--                        </if>-->
            <if test="parentGuid != null  and parentGuid != ''">
                and parent_guid = #{parentGuid}
            </if>
            <if test="deleted != null ">
                and deleted = #{deleted}
            </if>
            <if test="sort != null ">
                and sort = #{sort}
            </if>
            <!--                        <if test="location != null ">-->
            <!--                            and location = #{location}-->
            <!--                        </if>-->
        </where>
    </select>

    <select id="selectSysOrgById" parameterType="Long"
            resultMap="SysOrgResult">
        <include refid="selectSysOrgVo"/>
        where id = #{id}
    </select>
    <select id="selectCheckSubOrgIdList" resultType="java.lang.Long">
        select id
        from sys_org
        where path like concat((select path from sys_org where id = #{orgId}), '%')
          and deleted = 0
    </select>
    <select id="findListByOrgType" resultType="java.lang.Long">
        select a.id
        from sys_org a
        where a.type = #{execOrgType}
          and a.deleted = 0
          and a.is_lock = 0
    </select>
    <insert id="insertSysOrg" parameterType="com.xunmei.system.api.domain.SysOrg" useGeneratedKeys="true"
            keyProperty="id">
        insert into sys_org
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="address != null">
                address,
            </if>
            <if test="code != null">
                code,
            </if>
            <!--                    <if test="deviceCount != null">device_count,-->
            <!--                    </if>-->
            <if test="guid != null">
                guid,
            </if>
            <if test="isLock != null">
                is_lock,
            </if>
            <if test="manager != null">
                manager,
            </if>
            <if test="managerPhone != null">
                manager_phone,
            </if>
            <if test="name != null">
                name,
            </if>
            <if test="shortName != null">
                short_name,
            </if>
            <if test="parentId != null">
                parent_id,
            </if>
            <if test="phone != null">
                phone,
            </if>
            <if test="cityCode != null">
                city_code,
            </if>
            <!--                    <if test="openYear != null">open_year,-->
            <!--                    </if>-->
            <!--                    <if test="fitYear != null">fit_year,-->
            <!--                    </if>-->
            <!--                    <if test="floorage != null">floorage,-->
            <!--                    </if>-->
            <!--                    <if test="employee != null">employee,-->
            <!--                    </if>-->
            <!--                    <if test="isOwnRight != null">is_own_right,-->
            <!--                    </if>-->
            <!--                    <if test="isRent != null">is_rent,-->
            <!--                    </if>-->
            <!--                    <if test="isOnly != null">is_only,-->
            <!--                    </if>-->
            <!--                    <if test="buildYear != null">build_year,-->
            <!--                    </if>-->
            <!--                    <if test="placeType != null">place_type,-->
            <!--                    </if>-->
            <!--                    <if test="buildHeight != null">build_height,-->
            <!--                    </if>-->
            <!--                    <if test="social != null">social,-->
            <!--                    </if>-->
            <!--                    <if test="naturalEnvir != null">natural_envir,-->
            <!--                    </if>-->
            <if test="type != null">
                type,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateBy != null">
                update_by,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <!--                    <if test="modifiedId != null">modified_id,-->
            <!--                    </if>-->
            <if test="path != null">
                path,
            </if>
            <!--                    <if test="vid != null">vid,-->
            <!--                    </if>-->
            <!--                    <if test="level != null">level,-->
            <!--                    </if>-->
            <if test="oid != null">
                oid,
            </if>
            <if test="parentGuid != null">
                parent_guid,
            </if>
            <if test="deleted != null">
                deleted,
            </if>
            <if test="sort != null">
                sort,
            </if>
            <if test="source != null">
                source,
            </if>
            <if test="remark != null">
                remark,
            </if>
            <!--                    <if test="location != null">location,-->
            <!--                    </if>-->
            <if test="createBy != null">
                create_by,
            </if>
            <if test="updateBy != null">
                update_by,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="address != null">
                #{address},
            </if>
            <if test="code != null">
                #{code},
            </if>
            <!--                    <if test="deviceCount != null">#{deviceCount},-->
            <!--                    </if>-->
            <if test="guid != null">
                #{guid},
            </if>
            <if test="isLock != null">
                #{isLock},
            </if>
            <if test="manager != null">
                #{manager},
            </if>
            <if test="managerPhone != null">
                #{managerPhone},
            </if>
            <if test="name != null">
                #{name},
            </if>
            <if test="shortName != null">
                #{shortName},
            </if>
            <if test="parentId != null">
                #{parentId},
            </if>
            <if test="phone != null">
                #{phone},
            </if>
            <if test="cityCode != null">
                #{cityCode},
            </if>
            <!--                    <if test="openYear != null">#{openYear},-->
            <!--                    </if>-->
            <!--                    <if test="fitYear != null">#{fitYear},-->
            <!--                    </if>-->
            <!--                    <if test="floorage != null">#{floorage},-->
            <!--                    </if>-->
            <!--                    <if test="employee != null">#{employee},-->
            <!--                    </if>-->
            <!--                    <if test="isOwnRight != null">#{isOwnRight},-->
            <!--                    </if>-->
            <!--                    <if test="isRent != null">#{isRent},-->
            <!--                    </if>-->
            <!--                    <if test="isOnly != null">#{isOnly},-->
            <!--                    </if>-->
            <!--                    <if test="buildYear != null">#{buildYear},-->
            <!--                    </if>-->
            <!--                    <if test="placeType != null">#{placeType},-->
            <!--                    </if>-->
            <!--                    <if test="buildHeight != null">#{buildHeight},-->
            <!--                    </if>-->
            <!--                    <if test="social != null">#{social},-->
            <!--                    </if>-->
            <!--                    <if test="naturalEnvir != null">#{naturalEnvir},-->
            <!--                    </if>-->
            <if test="type != null">
                #{type},
            </if>
            <if test="createTime != null">
                #{createTime},
            </if>
            <if test="updateBy != null">
                #{updateBy},
            </if>
            <if test="updateTime != null">
                #{updateTime},
            </if>
            <!--                    <if test="modifiedId != null">#{modifiedId},-->
            <!--                    </if>-->
            <if test="path != null">
                #{path},
            </if>
            <!--                    <if test="vid != null">#{vid},-->
            <!--                    </if>-->
            <!--                    <if test="level != null">#{level},-->
            <!--                    </if>-->
            <!--                    <if test="oid != null">#{oid},-->
            <!--                    </if>-->
            <if test="parentGuid != null">
                #{parentGuid},
            </if>
            <if test="deleted != null">
                #{deleted},
            </if>
            <if test="sort != null">
                #{sort},
            </if>
            <if test="source != null">
                #{source},
            </if>
            <if test="remark != null">
                #{remark},
            </if>
            <!--                    <if test="location != null">#{location},-->
            <!--                    </if>-->
            <if test="createBy != null">
                #{createBy},
            </if>
            <if test="updateBy != null">
                #{updateBy},
            </if>
        </trim>
    </insert>

    <update id="updateSysOrg" parameterType="com.xunmei.system.api.domain.SysOrg">
        update sys_org
        <trim prefix="SET" suffixOverrides=",">
            <if test="address != null">
                address =
                #{address},
            </if>
            <if test="code != null">
                code =
                #{code},
            </if>
            <!--                    <if test="deviceCount != null">device_count =-->
            <!--                        #{deviceCount},-->
            <!--                    </if>-->
            <if test="guid != null">
                guid =
                #{guid},
            </if>
            <if test="isLock != null">
                is_lock =
                #{isLock},
            </if>
            <if test="manager != null">
                manager =
                #{manager},
            </if>
            <if test="managerPhone != null">
                manager_phone =
                #{managerPhone},
            </if>
            <if test="name != null">
                name =
                #{name},
            </if>
            <if test="shortName != null">
                short_name =
                #{shortName},
            </if>
            <if test="parentId != null">
                parent_id =
                #{parentId},
            </if>
            <if test="phone != null">
                phone =
                #{phone},
            </if>
            <if test="cityCode != null">
                city_code =
                #{cityCode},
            </if>
            <!--                    <if test="openYear != null">open_year =-->
            <!--                        #{openYear},-->
            <!--                    </if>-->
            <!--                    <if test="fitYear != null">fit_year =-->
            <!--                        #{fitYear},-->
            <!--                    </if>-->
            <!--                    <if test="floorage != null">floorage =-->
            <!--                        #{floorage},-->
            <!--                    </if>-->
            <!--                    <if test="employee != null">employee =-->
            <!--                        #{employee},-->
            <!--                    </if>-->
            <!--                    <if test="isOwnRight != null">is_own_right =-->
            <!--                        #{isOwnRight},-->
            <!--                    </if>-->
            <!--                    <if test="isRent != null">is_rent =-->
            <!--                        #{isRent},-->
            <!--                    </if>-->
            <!--                    <if test="isOnly != null">is_only =-->
            <!--                        #{isOnly},-->
            <!--                    </if>-->
            <!--                    <if test="buildYear != null">build_year =-->
            <!--                        #{buildYear},-->
            <!--                    </if>-->
            <!--                    <if test="placeType != null">place_type =-->
            <!--                        #{placeType},-->
            <!--                    </if>-->
            <!--                    <if test="buildHeight != null">build_height =-->
            <!--                        #{buildHeight},-->
            <!--                    </if>-->
            <!--                    <if test="social != null">social =-->
            <!--                        #{social},-->
            <!--                    </if>-->
            <!--                    <if test="naturalEnvir != null">natural_envir =-->
            <!--                        #{naturalEnvir},-->
            <!--                    </if>-->
            <if test="type != null">
                type =
                #{type},
            </if>
            <if test="createTime != null">
                create_time =
                #{createTime},
            </if>
            <if test="updateBy != null">
                update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">
                update_time =
                #{updateTime},
            </if>
            <!--                    <if test="modifiedId != null">modified_id =-->
            <!--                        #{modifiedId},-->
            <!--                    </if>-->
            <if test="path != null">
                path =
                #{path},
            </if>
            <!--                    <if test="vid != null">vid =-->
            <!--                        #{vid},-->
            <!--                    </if>-->
            <!--                    <if test="level != null">level =-->
            <!--                        #{level},-->
            <!--                    </if>-->
            <!--                    <if test="oid != null">oid =-->
            <!--                        #{oid},-->
            <!--                    </if>-->
            <if test="parentGuid != null">
                parent_guid =
                #{parentGuid},
            </if>
            <if test="deleted != null">
                deleted =
                #{deleted},
            </if>
            <if test="sort != null">
                sort =
                #{sort},
            </if>
            <if test="source != null">
                source =
                #{source},
            </if>
            <if test="remark != null">
                remark =
                #{remark},
            </if>
            <!--                    <if test="location != null">location =-->
            <!--                        #{location},-->
            <!--                    </if>-->
            <if test="createBy != null">
                create_by =
                #{createBy},
            </if>
            <if test="updateBy != null">
                update_by =
                #{updateBy},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSysOrgById" parameterType="Long">
        delete
        from sys_org
        where id = #{id}
    </delete>

    <delete id="deleteSysOrgByIds" parameterType="String">
        delete from sys_org where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectSysOrgByUserId" resultType="com.xunmei.system.api.domain.SysOrg">
        select so.*
        from sys_org so
                 inner join sys_user suo on so.id = suo.org_id
        where suo.id = #{userId}
    </select>
    <select id="getParentName" resultType="com.xunmei.common.core.vo.IdNameVo">
        select so.id,so.name,p.name as extra
        from sys_org so
        left join sys_org p on so.parent_id=p.id
        where id in
        <foreach collection="ids" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
        and so.deleted=0 and p.deleted=0
    </select>
</mapper>