<?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.devplatform.admin.modules.emproficient.dao.EmProficientDao">
  <!-- Result Map -->
  <resultMap id="BaseResultMap" type="com.devplatform.admin.modules.emproficient.bean.EmProficient">
    <result column="id" property="id"/>
    <result column="name" property="name"/>
    <result column="gender" property="gender"/>
    <result column="birthday" property="birthday"/>
    <result column="politic_countenance" property="politicCountenance"/>
    <result column="nation" property="nation"/>
    <result column="card_type" property="cardType"/>
    <result column="card_number" property="cardNumber"/>
    <result column="mobile" property="mobile"/>
    <result column="types" property="types"/>
    <result column="skill" property="skill"/>
    <result column="status" property="status"/>
    <result column="sys_sign" property="sysSign"/>
    <result column="deleted" property="deleted"/>
    <result column="create_user_id" property="createUserId"/>
    <result column="create_time" property="createTime"/>
    <result column="update_user_id" property="updateUserId"/>
    <result column="update_time" property="updateTime"/>
    <result column="personnel_type" property="personnelType"/>
    <result column="byx1" property="byx1"/>
    <result column="byx2" property="byx2"/>
    <result column="byx3" property="byx3"/>
  </resultMap>

  <!-- em_proficient table all fields -->
  <sql id="Base_Column_List">
        id,name,gender,birthday,politic_countenance,nation,card_type,card_number,mobile,types,skill,status,sys_sign,deleted,create_user_id,create_time,update_user_id,update_time,personnel_type,byx1,byx2,byx3
    </sql>

  <!-- 公共查询条件 -->
  <sql id="Example_Where_Clause">
    where deleted=0
    <if test="id!=null and id!=''">and id = #{id}</if>
    <if test="name!=null and name!=''">and name = #{name}</if>
    <if test="gender!=null ">and gender = #{gender}</if>
    <if test="birthday!=null ">and birthday = #{birthday}</if>
    <if test="politicCountenance!=null ">and politic_countenance = #{politicCountenance}</if>
    <if test="nation!=null ">and nation = #{nation}</if>
    <if test="cardNumber!=null and cardNumber!=''">and card_number = #{cardNumber}</if>
    <if test="mobile!=null and mobile!=''">and mobile = #{mobile}</if>
    <if test="types!=null and types!=''">and types = #{types}</if>
    <if test="skill!=null and skill!=''">and skill = #{skill}</if>
    <if test="status!=null ">and status = #{status}</if>
    <if test="sysSign!=null and sysSign!=''">and sys_sign = #{sysSign}</if>
    <if test="deleted!=null ">and deleted = #{deleted}</if>
    <if test="createUserId!=null and createUserId!=''">and create_user_id = #{createUserId}</if>
    <if test="createTime!=null ">and create_time = #{createTime}</if>
    <if test="updateUserId!=null and updateUserId!=''">and update_user_id = #{updateUserId}</if>
    <if test="updateTime!=null ">and update_time = #{updateTime}</if>
    <if test="byx1!=null and byx1!=''">and byx1 = #{byx1}</if>
    <if test="byx2!=null and byx2!=''">and byx2 = #{byx2}</if>
    <if test="byx3!=null and byx3!=''">and byx3 = #{byx3}</if>
  </sql>
  <select id="getProficientListPage" resultType="java.util.HashMap">
    select * from (SELECT p.*,
    n.name AS nationName,
    (SELECT GROUP_CONCAT(sd.name) FROM sys_dictionary sd WHERE sd.deleted = 0 AND FIND_IN_SET(sd.id, p.types) AND sd.station_id = p.station_id) AS
    typesName,
    sdd.name as cardTypeName
    /*(SELECT name FROM sys_dictionary sd WHERE sd.deleted = 0 AND sd.id = p.card_type ) as cardTypeName*/
    FROM em_proficient p
    LEFT JOIN sys_nation n ON p.nation = n.id
    LEFT JOIN sys_dictionary sdd ON sdd.id = p.card_type and sdd.station_id = p.station_id
    where p.deleted = 0
    <if test="params.name!=null and params.name!=''">and INSTR(p.name, #{params.name})</if>
    <if test="params.card_number!=null and params.card_number!=''">
      and INSTR(p.card_number, #{params.card_number})
    </if>
    <if test="params.status!=null">
      and p.status = #{params.status}
    </if>
    <if test="params.mobile!=null and params.mobile!=''">
      and INSTR(p.mobile, #{params.mobile})
    </if>
    <if test="params.politic_countenance!=null and params.politic_countenance!=''">and p.politic_countenance =
      #{params.politic_countenance}
    </if>
    <if test="params.personnelType!=null and params.personnelType!=''">and p.personnel_type =
      #{params.personnelType}
    </if>
    <if test="params.nation!=null and params.nation!=''">and p.nation = #{params.nation}</if>
    <if test="params.types!=null">
      <foreach close=" " collection="params.types" item="item" open=" and " separator=" and ">
        find_in_set(#{item}, p.types)
      </foreach>
    </if>
    <if test="params.ids!=null and params.ids.size()>0">
      and p.id not in
      <foreach close=")" collection="params.ids" index="index" item="item" open="(" separator=",">
        #{item}
      </foreach>
    </if>
    <if test="params.stationId != null and params.stationId != ''">
      and p.station_id = #{params.stationId}
    </if>
    ) aa
    <choose>
      <when test="params.sort != null and params.sort != '' and params.order != null and params.order != ''">
        ORDER BY ${params.sort} ${params.order}
      </when>
      <otherwise>
        ORDER BY personnel_type, create_time DESC
      </otherwise>
    </choose>
  </select>
  <select id="getProficientList" resultType="java.util.HashMap">
    SELECT p.*,
    n.name AS nationName
    FROM em_proficient p
    LEFT JOIN sys_nation n ON p.nation = n.id
    <where>
      AND p.deleted = 0
      <if test="params.name!=null and params.name!=''">and INSTR(p.name, #{params.name})</if>
      <if test="params.card_number!=null and params.card_number!=''">and INSTR(p.card_number,
        #{params.card_number})
      </if>
      <if test="params.mobile!=null and params.mobile!=''">and INSTR(p.mobile, #{params.mobile})
      </if>
      <if test="params.personnelType!=null and params.personnelType!=''">and p.personnel_type =
        #{params.personnelType}
      </if>
      <if test="params.politic_countenance!=null and params.politic_countenance!=''">and p.politic_countenance =
        #{params.politic_countenance}
      </if>
      <if test="params.nation!=null and params.nation!=''">and p.nation = #{params.nation}</if>
      <if test="params.types!=null">
        <foreach close=" " collection="params.types" item="item" open=" and " separator=" and ">
          find_in_set(#{item}, p.types)
        </foreach>
      </if>
      <if test="params.stationId != null and params.stationId != ''">
        and p.station_id = #{params.stationId}
      </if>
    </where>
    <choose>
      <when test="params.sort != null and params.sort != '' and params.order != null and params.order != ''">
        ORDER BY ${params.sort} ${params.order}
      </when>
      <otherwise>
        ORDER BY personnel_type, create_time DESC
      </otherwise>
    </choose>
  </select>
  <select id="getInfoById" resultMap="InfoMap">
        SELECT ep.*, sn.name AS nationName, sd.name AS cardTypeName
        FROM em_proficient ep
                 LEFT JOIN sys_nation sn ON sn.id = ep.nation
                 LEFT JOIN sys_dictionary sd ON sd.id = ep.card_type AND sd.deleted = 0 AND sd.station_id = ep.station_id
        WHERE ep.id= #{id}
    </select>
  <update id="deleteProficient">
        UPDATE em_proficient
        SET deleted = 1
        WHERE id = #{id}
    </update>

  <resultMap id="InfoMap" type="java.util.HashMap">
    <result column="id" property="id"/>
    <result column="name" property="name"/>
    <result column="gender" property="gender"/>
    <result column="birthday" property="birthday"/>
    <result column="politic_countenance" property="politicCountenance"/>
    <result column="nation" property="nation"/>
    <result column="card_type" property="cardType"/>
    <result column="card_number" property="cardNumber"/>
    <result column="mobile" property="mobile"/>
    <result column="types" property="types"/>
    <result column="skill" property="skill"/>
    <result column="status" property="status"/>
    <result column="sys_sign" property="sysSign"/>
    <result column="deleted" property="deleted"/>
    <result column="create_user_id" property="createUserId"/>
    <result column="create_time" property="createTime"/>
    <result column="update_user_id" property="updateUserId"/>
    <result column="update_time" property="updateTime"/>
    <result column="personnel_type" property="personnelType"/>
    <result column="byx1" property="byx1"/>
    <result column="byx2" property="byx2"/>
    <result column="byx3" property="byx3"/>
    <result column="nationName" property="nationName"/>
    <result column="cardTypeName" property="cardTypeName"/>
  </resultMap>
</mapper>