<?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.nantian.cloud.jljc.mapper.TSupervisionOrganizationMapper">
    <resultMap id="BaseResultMap" type="com.nantian.cloud.jljc.entity.TSupervisionOrganization">
        <result column="id" jdbcType="INTEGER" property="id"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="pid" jdbcType="INTEGER" property="pid"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="userid" jdbcType="INTEGER" property="userid"/>
        <result column="code" jdbcType="VARCHAR" property="code"/>
        <result column="area_code" jdbcType="VARCHAR" property="areaCode"/>
        <result column="state" jdbcType="VARCHAR" property="state"/>
        <result column="del_flag" jdbcType="VARCHAR" property="delFlag"/>
        <result column="remarks" jdbcType="VARCHAR" property="remarks"/>
        <result column="address" jdbcType="VARCHAR" property="address"/>
        <result column="fax" jdbcType="VARCHAR" property="fax"/>
        <result column="postcode" jdbcType="VARCHAR" property="postcode"/>
        <result column="contact" jdbcType="VARCHAR" property="contact"/>
        <result column="telephone" jdbcType="VARCHAR" property="telephone"/>
    </resultMap>

    <resultMap id="BaseResultMapVo" type="com.nantian.cloud.jljc.vo.TreeVo">
        <id column="value" property="value"/>
        <result column="pvalue" property="pvalue"/>
        <result column="label" property="label"/>
        <result column="plabel" property="plabel"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, name, pid, update_time, userid, code, area_code, state, del_flag, remarks,
        address, fax, postcode, contact, telephone
    </sql>

    <insert id="insertall" parameterType="com.nantian.cloud.jljc.entity.TSupervisionOrganization">
    insert into t_supervision_organization (name, pid,
      update_time, userid, code, 
      area_code, state,
      remarks, address, fax, postcode, contact, telephone)
    values ( #{name,jdbcType=VARCHAR}, #{pid,jdbcType=INTEGER},
      #{updateTime,jdbcType=TIMESTAMP}, #{userid,jdbcType=INTEGER}, #{code,jdbcType=VARCHAR}, 
      #{areaCode,jdbcType=VARCHAR}, #{state,jdbcType=VARCHAR},
      #{remarks,jdbcType=VARCHAR}, #{address,jdbcType=VARCHAR}, #{fax,jdbcType=VARCHAR}, #{postcode,jdbcType=VARCHAR},
      #{contact,jdbcType=VARCHAR}, #{telephone,jdbcType=VARCHAR})
  </insert>

    <select id="selectall" resultType="com.nantian.cloud.jljc.entity.TSupervisionOrganization">
        select a.id, a.name, a.pid, a.update_time, a.userid, a.code, a.area_code, a.state, a.del_flag, a.remarks
        ,a.area_code dept_id, d.meaning as areaName, o.name as pname
        ,a.address, a.fax, a.postcode, a.contact, a.telephone
        from t_supervision_organization a
        left join dict_region d on a.area_code = d.id
        left join t_supervision_organization o on a.pid = o.id
        <trim prefix="where" prefixOverrides="and">
            <if test="param.areaCode !=null and param.areaCode!=''">
                and a.area_code =#{param.areaCode}
            </if>
            <if test="param.state !=null and param.state!=''">
                and a.state =#{param.state}
            </if>
            <if test="param.pid !=null and param.pid!=''">
                and a.pid =#{param.pid}
            </if>
            <if test="param.name !=null and param.name!=''">
                and a.`name` like concat("%",#{param.name},"%")
            </if>
            <if test="param.id !=null and param.id!=''">
                and a.id =#{param.id}
            </if>
            and a.del_flag = '1'
        </trim>
        order by a.area_code, a.name asc
    </select>

    <select id="getList" resultType="com.nantian.cloud.jljc.entity.TSupervisionOrganization">
        select a.id, a.name, a.pid, a.update_time, a.userid, a.code, a.area_code, a.state, a.del_flag, a.remarks
        ,a.area_code dept_id, d.meaning as areaName, o.name as pname
        ,a.address, a.fax, a.postcode, a.contact, a.telephone
        from t_supervision_organization a
        left join dict_region d on a.area_code = d.id
        left join t_supervision_organization o on a.pid = o.id
        <trim prefix="where" prefixOverrides="and">
            <if test="param.areaCode !=null and param.areaCode!=''">
                and a.area_code =#{param.areaCode}
            </if>
            <if test="param.state !=null and param.state!=''">
                and a.state =#{param.state}
            </if>
            <if test="param.pid !=null and param.pid!=''">
                and a.pid =#{param.pid}
            </if>
            <if test="param.name !=null and param.name!=''">
                and a.`name` like concat("%",#{param.name},"%")
            </if>
            <if test="param.id !=null and param.id!=''">
                and a.id =#{param.id}
            </if>
            and a.del_flag = '1'
        </trim>
        order by a.area_code, a.name asc
    </select>

    <select id="getTree" resultType="com.nantian.cloud.jljc.vo.TreeVo">
    	select o.area_code as value, o.name as label,
        case when o.area_code = #{regionCode} then 0 else o.pid end pvalue,
    	po.name as plabel, o.area_code as dept_id
        from t_supervision_organization o
		left join t_supervision_organization po on o.pid = po.id
		where o.del_flag = '1'
        <if test="deptId !=null and deptId !=''">
            and o.area_code like concat(#{deptId},'%')
        </if>
  </select>

    <select id="regionTree" resultType="com.nantian.cloud.jljc.vo.RegionOrganizationTreeVo">
        select left(id,5) as value, '省属' as label, id as pvalue, meaning as plabel
        from dict_region  where id = '530000'
        union
        select left(id,5) as value, concat(right(meaning,1),'属') as label, id as pvalue, meaning as plabel
        from dict_region  where del_flag = "1" and pid = '530000'
        union
        select distinct id as value, meaning as label, pid as pvalue, null
        from dict_region  where del_flag = "1"
    </select>

    <select id="organizationTree" resultType="com.nantian.cloud.jljc.vo.RegionOrganizationTreeVo">
		select distinct a.id as value, '' as label, case when a.area_code like '%00' then left(a.area_code,5) else a.area_code end pvalue, d.meaning as plabel,
		a.id, a.name, a.pid, a.update_time, a.userid, a.code, a.area_code, a.state, a.del_flag, a.remarks
        ,a.area_code dept_id, d.meaning as areaName, o.name as pname,a.address, a.fax, a.postcode, a.contact, a.telephone
        from t_supervision_organization a
        left join dict_region d on a.area_code = d.id
        left join t_supervision_organization o on a.pid = o.id
        where a.del_flag = '1'
        <if test="param.areaCode !=null and param.areaCode!=''">
            and a.area_code =#{param.areaCode}
        </if>
        <if test="param.state !=null and param.state!=''">
            and a.state =#{param.state}
        </if>
        <if test="param.pid !=null and param.pid!=''">
            and a.pid =#{param.pid}
        </if>
        <if test="param.name !=null and param.name!=''">
            and a.`name` like concat("%",#{param.name},"%")
        </if>
        <if test="param.id !=null and param.id!=''">
            and a.id =#{param.id}
        </if>
    </select>

    <select id="getRegionStr" resultType="java.lang.String">
        select GROUP_CONCAT(concat(t.pvalue,',',t.value)) value
        from(
            select distinct case when a.area_code like '%00' then left(a.area_code,5) else a.area_code end pvalue, a.area_code value
            from t_supervision_organization a
            left join dict_region d on a.area_code = d.id
            left join t_supervision_organization o on a.pid = o.id
            where a.del_flag = '1'
            <if test="param.areaCode !=null and param.areaCode!=''">
                and a.area_code =#{param.areaCode}
            </if>
            <if test="param.state !=null and param.state!=''">
                and a.state =#{param.state}
            </if>
            <if test="param.pid !=null and param.pid!=''">
                and a.pid =#{param.pid}
            </if>
            <if test="param.name !=null and param.name!=''">
                and a.`name` like concat("%",#{param.name},"%")
            </if>
            <if test="param.id !=null and param.id!=''">
                and a.id =#{param.id}
            </if>
        )t
    </select>

    <update id="updateall" parameterType="com.nantian.cloud.jljc.entity.TSupervisionOrganization">
    update t_supervision_organization a
     set
     a.area_code = #{areaCode},
     a.`code` =#{code},
     a.`name` = #{name},
     a.pid = #{pid},
     a.remarks = #{remarks},
     a.state = #{state},
     a.update_time = #{updateTime},
     a.userid =#{userid},
     a.address = #{address},
     a.fax = #{fax},
     a.postcode = #{postcode},
     a.contact = #{contact},
     a.telephone = #{telephone}
     WHERE a.id =#{id}
  </update>

    <update id="deleteall" parameterType="com.nantian.cloud.jljc.entity.TSupervisionOrganization">
    UPDATE t_supervision_organization a set a.del_flag = '4' where a.id =#{id}
  </update>
</mapper>