<?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.xhpc.activity.mapper.XhpcWorkUserMapper">
  <resultMap id="BaseResultMap" type="com.xhpc.activity.domain.XhpcWorkUserDomain">
    <id column="work_user_id" jdbcType="BIGINT" property="workUserId" />
    <result column="user_name" jdbcType="VARCHAR" property="userName" />
    <result column="dept_id" jdbcType="BIGINT" property="deptId" />
    <result column="post_name" jdbcType="VARCHAR" property="postName" />
    <result column="phone" jdbcType="VARCHAR" property="phone" />
    <result column="email" jdbcType="VARCHAR" property="email" />
    <result column="wechat_openid" jdbcType="VARCHAR" property="wechatOpenid" />
    <result column="is_leader" jdbcType="SMALLINT" property="isLeader" />
    <result column="status" jdbcType="SMALLINT" property="status" />
    <result column="del_flag" jdbcType="SMALLINT" property="delFlag" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="create_by" jdbcType="VARCHAR" property="createBy" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="update_by" jdbcType="VARCHAR" property="updateBy" />
  </resultMap>
  <sql id="Base_Column_List">
    u.work_user_id, u.user_name, u.dept_id, u.post_name, u.phone, u.email, u.wechat_openid, u.is_leader,
    u.`status`, u.del_flag, u.create_time, u.create_by, u.update_time, u.update_by
  </sql>


  <select id="selectByUserNameAndPhone" resultType="com.xhpc.activity.domain.XhpcWorkUserDomain">
    select
    <include refid="Base_Column_List" />
    from xhpc_work_user u
    where u.user_name = #{userName} and u.phone=#{phone}
  </select>


  <select id="selectMapListByDeptId" resultType="map">
    select
      work_user_id as 'id',
        user_name as 'name',
        phone,
           email
    from xhpc_work_user
    where del_flag=0 and status=1 and dept_id = #{deptId}
  </select>


  <select id="selectByOrderId" resultType="com.xhpc.activity.domain.XhpcWorkUserDomain">
    select
    <include refid="Base_Column_List" />
    FROM xhpc_work_user u
    LEFT JOIN xhpc_work_order_user ou on ou.user_id=u.work_user_id
    WHERE ou.order_id=#{orderId} and u.del_flag=0 and u.status=1
  </select>


  <select id="selectByPrimaryKey" parameterType="java.lang.Long"
          resultType="com.xhpc.activity.domain.XhpcWorkUserDomain">
    select 
    <include refid="Base_Column_List" />, su.user_name as 'login_name', GROUP_CONCAT(r.role_id) as 'role_id', "123456" as 'login_password'
    from xhpc_work_user u
    left join sys_user su on su.phonenumber = u.phone
    left join sys_user_role r on r.user_id=su.user_id
    where u.work_user_id = #{workUserId,jdbcType=BIGINT}
  </select>


    <update id="deleteLogicByPrimaryKey" parameterType="java.lang.Long">
    update xhpc_work_user set del_flag=2
    where work_user_id = #{workUserId,jdbcType=BIGINT}
  </update>


  <insert id="insert" keyColumn="work_user_id" keyProperty="workUserId" parameterType="com.xhpc.activity.domain.XhpcWorkUserDomain" useGeneratedKeys="true">
    insert into xhpc_work_user (user_name, dept_id, post_name, 
      phone, email, wechat_openid, 
      is_leader, `status`, del_flag, 
      create_time, create_by, update_time, 
      update_by)
    values (#{userName,jdbcType=VARCHAR}, #{deptId,jdbcType=BIGINT}, #{postName,jdbcType=VARCHAR}, 
      #{phone,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, #{wechatOpenid,jdbcType=VARCHAR}, 
      #{isLeader,jdbcType=SMALLINT}, #{status,jdbcType=SMALLINT}, 0,
      sysdate(), #{createBy,jdbcType=VARCHAR}, sysdate(),
      #{updateBy,jdbcType=VARCHAR})
  </insert>


  <update id="updateByPrimaryKey" parameterType="com.xhpc.activity.domain.XhpcWorkUserDomain">
    update xhpc_work_user
    set user_name = #{userName,jdbcType=VARCHAR},
      dept_id = #{deptId,jdbcType=BIGINT},
      post_name = #{postName,jdbcType=VARCHAR},
      phone = #{phone,jdbcType=VARCHAR},
      email = #{email,jdbcType=VARCHAR},
      wechat_openid = #{wechatOpenid,jdbcType=VARCHAR},
      is_leader = #{isLeader,jdbcType=SMALLINT},
      `status` = #{status,jdbcType=SMALLINT},
      update_time = sysdate(),
      update_by = #{updateBy,jdbcType=VARCHAR}
    where work_user_id = #{workUserId,jdbcType=BIGINT}
  </update>


  <update id="updateDomainStatusByPrimaryKey">
    update xhpc_work_user
    set `status` = #{status,jdbcType=SMALLINT},
        update_time = sysdate(),
        update_by = #{updateBy,jdbcType=VARCHAR}
    where work_user_id = #{workUserId,jdbcType=BIGINT}
  </update>


  <select id="selectPage" resultType="com.xhpc.activity.domain.XhpcWorkUserDomain">
    select
    <include refid="Base_Column_List" />, d.dept_name, su.user_name as 'login_name'
    from xhpc_work_user u
    left join xhpc_work_dept d on u.dept_id=d.work_dept_id
    left join sys_user su on su.phonenumber = u.phone
    where u.del_flag=0 and d.del_flag=0
    <if test="params.deptId!=null">
      and (d.work_dept_id=#{params.deptId} or d.parent_dept_id=#{params.deptId})
    </if>
    <if test="params.userName!=null and params.userName!=''">
      and u.user_name like concat('%', #{params.userName}, '%')
    </if>
    <if test="params.phone!=null and params.phone!=''">
      and u.phone like concat('%', #{params.phone}, '%')
    </if>
    <if test="params.email!=null and params.email!=''">
      and u.email like concat('%', #{params.email}, '%')
    </if>
    <if test="params.loginName!=null and params.loginName!=''">
      and su.user_name like concat('%', #{params.loginName}, '%')
    </if>
    <if test="params.deptIds!=null and params.deptIds!=''">
      and find_in_set(u.dept_id, #{params.deptIds})
    </if>
  </select>


  <select id="selectMapList" resultType="java.util.Map">
    select
    u.work_user_id as 'id', u.user_name as 'name'
    from xhpc_work_user u
    where u.del_flag=0 and u.status=1
    <if test="params.deptId!=null">
      and u.dept_id=#{params.deptId}
    </if>
    <if test="params.userName!=null and params.userName!=''">
      and u.user_name like concat('%', #{params.userName}, '%')
    </if>
    <if test="params.phone!=null and params.phone!=''">
      and u.phone like concat('%', #{params.phone}, '%')
    </if>
    <if test="params.email!=null and params.email!=''">
      and u.email like concat('%', #{params.email}, '%')
    </if>

  </select>


    <select id="selectPhoneListByIds" resultType="java.lang.String">
      SELECT GROUP_CONCAT(phone) from xhpc_work_user where find_in_set(work_user_id, #{userIds})
    </select>
</mapper>