<?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.TSupervisionPersonMapper">

    <resultMap id="BaseResultMap" type="com.nantian.cloud.jljc.entity.TSupervisionPerson">
        <result column="id" jdbcType="INTEGER" property="id"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="code" jdbcType="VARCHAR" property="code"/>
        <result column="organizationid" jdbcType="VARCHAR" property="organizationid"/>
        <result column="jobs" jdbcType="VARCHAR" property="jobs"/>
        <result column="area_code" jdbcType="VARCHAR" property="areaCode"/>
        <result column="userid" jdbcType="INTEGER" property="userid"/>
        <result column="state" jdbcType="VARCHAR" property="state"/>
        <result column="del_flag" jdbcType="VARCHAR" property="delFlag"/>
        <result column="remarks" jdbcType="VARCHAR" property="remarks"/>
        <result column="telephone" jdbcType="VARCHAR" property="telephone"/>
        <result column="person_type" jdbcType="VARCHAR" property="personType"/>
        <result column="low_type" jdbcType="VARCHAR" property="lowType"/>
        <result column="organ" jdbcType="VARCHAR" property="organ"/>
        <result column="cellphone" jdbcType="VARCHAR" property="cellphone"/>
        <result column="sex" jdbcType="VARCHAR" property="sex"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, name, code, organizationid, jobs, area_code, userid, state, del_flag, remarks, telephone,
        organ,person_type,low_type,cellphone,sex
    </sql>

    <insert id="insertall" parameterType="com.nantian.cloud.jljc.entity.TSupervisionPerson">
    insert into t_supervision_person ( name, code,
      organizationid, jobs, area_code, 
      userid,organ,person_type,low_type,cellphone,
      remarks, telephone,sex)
    values ( #{name,jdbcType=VARCHAR}, #{code,jdbcType=VARCHAR},
      #{organizationid,jdbcType=VARCHAR}, #{jobs,jdbcType=VARCHAR}, #{areaCode,jdbcType=VARCHAR}, 
      #{userid,jdbcType=INTEGER},#{organ,jdbcType=VARCHAR},#{personType,jdbcType=VARCHAR},#{lowType,jdbcType=VARCHAR},#{cellphone,jdbcType=VARCHAR},
      #{remarks,jdbcType=VARCHAR}, #{telephone,jdbcType=VARCHAR},#{sex,jdbcType=VARCHAR})
  </insert>
    <insert id="insertbyexcel" parameterType="java.util.List">
        insert into t_supervision_person ( name, code,
        organizationid, jobs, area_code,
        userid,organ,person_type,low_type,cellphone,
        remarks, telephone,sex)
         VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.name,jdbcType=VARCHAR}, #{item.code,jdbcType=VARCHAR},
            #{item.organizationid,jdbcType=VARCHAR}, #{item.jobs,jdbcType=VARCHAR}, #{item.areaCode,jdbcType=VARCHAR},
            #{item.userid,jdbcType=INTEGER},#{item.organ,jdbcType=VARCHAR},#{item.personType,jdbcType=VARCHAR},#{item.lowType,jdbcType=VARCHAR},#{item.cellphone,jdbcType=VARCHAR},
            #{item.remarks,jdbcType=VARCHAR}, #{item.telephone,jdbcType=VARCHAR},#{item.sex,jdbcType=VARCHAR}
            )
        </foreach>
    </insert>

    <select id="selectallname" resultType="java.lang.String">
        select name from t_supervision_person;
    </select>

    <select id="selectall" resultType="com.nantian.cloud.jljc.entity.TSupervisionPerson">
        select a.id, a.name, a.code, a.organizationid,a.sex, a.jobs, a.area_code, a.userid, a.state, a.del_flag, a.remarks, a.telephone
        ,a.area_code dept_id, d.meaning as areaName,  o.`name` as organizationName,a.organ,a.low_type,a.person_type,a.cellphone
        from t_supervision_person a
        left join dict_region d on a.area_code = d.id
        left join t_supervision_organization o on a.organizationid = 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.jobs!=null and param.jobs != ''">
                and a.jobs =#{param.jobs}
            </if>
            <if test="param.name!=null and param.name != ''">
                and a.`name` like concat("%",#{param.name},"%")
            </if>
            <if test="param.organizationid!=null and param.organizationid != ''">
                and a.organizationid =#{param.organizationid}
            </if>
            <if test="param.state!=null and param.state != ''">
                and a.state =#{param.state}
            </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="getJCPerson" resultType="com.nantian.cloud.jljc.entity.TSupervisionPerson">
        select p.id, p.`name`, p.`code`, p.organizationid, p.jobs, p.area_code, p.userid, p.state,p.sex,
        p.del_flag, p.remarks, p.telephone,o.area_code dept_id,p.organ,p.low_type,p.person_type,p.cellphone
        from t_supervision_person p left join t_supervision_organization o on o.area_code = #{deptId}
        where  p.organizationid = o.id and p.del_flag = '1'
    </select>

    <select id="getByOrg" resultType="com.nantian.cloud.jljc.entity.TSupervisionPerson">
        select p.id, p.`name`, p.`code`, p.organizationid, p.jobs, p.area_code, p.userid,p.sex,
        p.state, p.del_flag, p.remarks, p.telephone,,p.organ,p.low_type,p.person_type,a.cellphone
        from (select <include refid="Base_Column_List"></include> from t_supervision_person where del_flag = '1') p
        <if test=" organizationIds!=null and organizationIds!='' ">
            where organizationid in
            <foreach collection="organizationIds" index="index" item="id" separator="," open="(" close=")">
                #{id}
            </foreach>
        </if>
    </select>

    <select id="personTree" resultType="com.nantian.cloud.jljc.vo.RegionPersonTreeVo">
        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.code, a.organizationid, a.jobs, a.area_code, a.userid, a.state, a.del_flag, a.remarks,a.sex,
        a.telephone, a.area_code dept_id, d.meaning as areaName, o.`name` as organizationName,
        a.organ,a.low_type,a.person_type,a.cellphone
        from t_supervision_person a
        left join dict_region d on a.area_code = d.id
        left join t_supervision_organization o on a.organizationid = 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.jobs!=null and param.jobs != ''">
            and a.jobs =#{param.jobs}
        </if>
        <if test="param.name!=null and param.name != ''">
            and a.`name` like concat("%",#{param.name},"%")
        </if>
        <if test="param.organizationid!=null and param.organizationid != ''">
            and a.organizationid =#{param.organizationid}
        </if>
        <if test="param.state!=null and param.state != ''">
            and a.state =#{param.state}
        </if>
        <if test="param.id!=null and param.id != ''">
            and a.id =#{param.id}
        </if>
    </select>

    <select id="regionTree" resultType="com.nantian.cloud.jljc.vo.RegionPersonTreeVo">
        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="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_person a
            left join dict_region d on a.area_code = d.id
            left join t_supervision_organization o on a.organizationid = 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.jobs!=null and param.jobs != ''">
                and a.jobs =#{param.jobs}
            </if>
            <if test="param.name!=null and param.name != ''">
                and a.`name` like concat("%",#{param.name},"%")
            </if>
            <if test="param.organizationid!=null and param.organizationid != ''">
                and a.organizationid =#{param.organizationid}
            </if>
            <if test="param.state!=null and param.state != ''">
                and a.state =#{param.state}
            </if>
            <if test="param.id!=null and param.id != ''">
                and a.id =#{param.id}
            </if>
        )t
    </select>

    <select id="getOrgRegion" resultType="java.lang.String">
        select area_code from t_supervision_organization where id = #{organizationid}
    </select>

    <select id="selectListByOrgRegion" resultType="com.nantian.cloud.jljc.entity.TSupervisionPerson">
        select <include refid="Base_Column_List"></include>
        from t_supervision_person where 1 = 1 and organizationid in (
            select id from t_supervision_organization where 1 = 1 and area_code like concat(#{areaCode},'%')
        )
    </select>

    <update id="updateall" parameterType="com.nantian.cloud.jljc.entity.TSupervisionPerson">
    UPDATE t_supervision_person a set
     a.area_code =#{areaCode},
     a.`code` =#{code},
     a.jobs =#{jobs},
     a.`name` = #{name},
     a.organizationid = #{organizationid},
     a.remarks = #{remarks},
     a.state = #{state},
     a.sex = #{sex},
     a.telephone =#{telephone},
     a.userid =#{userid},
     a.organ=#{organ},a.low_type=#{lowType},a.person_type=#{personType},a.cellphone=#{cellphone}
     where a.id =#{id}
  </update>

    <update id="deleteall" parameterType="com.nantian.cloud.jljc.entity.TSupervisionPerson">
    UPDATE t_supervision_person a set a.del_flag = '4' WHERE a.id=#{id}
  </update>
    <update id="updatebyexcel" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" open="" close="" separator=";">
            UPDATE t_supervision_person a
            <set>
                a.code=#{item.code,jdbcType=VARCHAR},
                a.organizationid= #{item.organizationid,jdbcType=VARCHAR},
                a.jobs=#{item.jobs,jdbcType=VARCHAR},
                a.area_code=#{item.areaCode,jdbcType=VARCHAR},
                a.organ=#{item.organ,jdbcType=VARCHAR},
                a.person_type=#{item.personType,jdbcType=VARCHAR},
                a.low_type=#{item.lowType,jdbcType=VARCHAR},
                a.cellphone=#{item.cellphone,jdbcType=VARCHAR},
                a.remarks=#{item.remarks,jdbcType=VARCHAR},
                a.telephone=#{item.telephone,jdbcType=VARCHAR},
                a.sex=#{item.sex,jdbcType=VARCHAR}
            </set>
            <where>
                a.name= #{item.name,jdbcType=VARCHAR}
            </where>
        </foreach>
    </update>
</mapper>