<?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.xintong.visualinspection.dao.cluster.DepartmentDao" >
    <resultMap id="BaseResultMap" type="com.xintong.visualinspection.bean.Organ" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="organname" property="organname" jdbcType="VARCHAR" />
        <result column="parentid" property="parentid" jdbcType="INTEGER" />
        <result column="remark" property="remark" jdbcType="VARCHAR" />
        <result column="classtype" property="classtype" jdbcType="VARCHAR" />
        <result column="organcode" property="organcode" jdbcType="VARCHAR" />
        <result column="isshow" property="isshow" jdbcType="VARCHAR" />
        <result column="deptype" property="deptype" jdbcType="VARCHAR" />
        <result column="status" property="status" jdbcType="INTEGER" />
    </resultMap>

	<resultMap id="FeeStationMap" type="com.xintong.visualinspection.bean.FeeStation" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="name" property="name" jdbcType="VARCHAR" />
        <result column="lanes_number" property="lanes_number" jdbcType="INTEGER" />
        <result column="exit_number" property="exit_numer" jdbcType="INTEGER" />
        <result column="entrance_number" property="entrance_numer" jdbcType="INTEGER" />
        <result column="exit_etc" property="exit_etc" jdbcType="VARCHAR" />
        <result column="entrance_etc" property="entrance_etc" jdbcType="VARCHAR" />
        <result column="organ_id" property="deptid" jdbcType="VARCHAR" />
    </resultMap>
    <select id="getAll" resultMap="BaseResultMap"  >
        SELECT * 
        FROM t_sys_organ
    </select>

    <!-- <select id="getOne" parameterType="java.lang.Long" resultMap="BaseResultMap" >
        SELECT
        id,organname,remark,parentid,classtype,organcode,isshow,deptype,status
        FROM t_sys_organ
        WHERE id = #{id}
    </select> -->
    <select id="getAllFS" resultMap="FeeStationMap">
        SELECT * FROM t_br_layer_fee_station
    </select>
    
    <select id="getFsBydeptId" parameterType="java.lang.Integer" resultMap="FeeStationMap">
        SELECT * FROM t_br_layer_fee_station where organ_id = #{id}
    </select>
    
    <select id="getOrgan" parameterType="com.xintong.visualinspection.bean.Organ" resultMap="BaseResultMap">
        SELECT 
        id,organname,remark,parentid,classtype,organcode,isshow,deptype,status
         FROM t_sys_organ
         where 1=1
         <if test="organname != null">and organname = #{organname} </if>
         <if test="remark != null">and remark = #{remark} </if>
         <if test="parentid != null and parentid!=0">and parentid = #{parentid} </if>
         <if test="classtype != null">and classtype = #{classtype} </if>
         <if test="organcode != null">and organcode = #{organcode} </if>
         <if test="isshow != null">and isshow = #{isshow} </if>
         <if test="deptype != null">and deptype = #{deptype} </if>
         <if test="status != null">and status = #{status} </if>
         <if test="id != null and id != 0">and id = #{id} </if>
    </select>
    

    <insert id="insert" parameterType="com.xintong.visualinspection.bean.Organ" >
        INSERT INTO
        t_sys_organ
        (organname,parentid)
        VALUES
        (#{organname},#{parentid})
    </insert>

    <update id="update" parameterType="com.xintong.visualinspection.bean.Organ" >
        UPDATE
        t_sys_organ
        SET
        <if test="organname != null">organname = #{organname},</if>
        <if test="remark != null">remark = #{remark},</if>
        <if test="parentid != null and parentid!=0">parentid = #{parentid},</if>
        <if test="classtype != null">classtype = #{classtype},</if>
        <if test="organcode != null">organcode = #{organcode},</if>
        <if test="isshow != null">isshow = #{isshow},</if>
        <if test="deptype != null">deptype = #{deptype},</if>
        id = #{id}
        WHERE
        id = #{id}
    </update>

    <delete id="delete" parameterType="java.lang.Integer" >
        update t_sys_organ
         set status=-1
        WHERE
        id =#{id} or parentid=#{id}
    </delete>

	<select id="selectFeeStationGroup" resultType="com.xintong.visualinspection.bean.StatisticsBean"  >
		SELECT COUNT(1) AS people_num,s.ORGAN_ID AS dept_id, s.name from t_sys_users t 
			       LEFT JOIN t_br_layer_fee_station s ON t.ORGANID = s.ORGAN_ID
			WHERE t.POSITIONID =1 AND s.ORGAN_ID IS NOT NULL GROUP BY s.ORGAN_ID 
	</select>
    
</mapper>