<?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.goafanti.common.dao.DepartmentMapper">
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.Department">
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="create_id" jdbcType="VARCHAR" property="createId" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="deleted_sign" jdbcType="INTEGER" property="deletedSign" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="type" jdbcType="VARCHAR" property="type" />
    <result column="manager_id" jdbcType="VARCHAR" property="managerId" />
    <result column="dep_no" jdbcType="VARCHAR" property="depNo" />
    <result column="super_id" jdbcType="VARCHAR" property="superId" />
    <result column="remarks" jdbcType="VARCHAR" property="remarks" />
    <result column="status" jdbcType="VARCHAR" property="status" />
    <result column="abbreviation" jdbcType="VARCHAR" property="abbreviation" />
    <result column="finance_id" jdbcType="VARCHAR" property="financeId" />
    <result column="province" jdbcType="INTEGER" property="province" />
    <result column="working_hours_type" jdbcType="INTEGER" property="workingHoursType" />
    <result column="hide_sign" jdbcType="INTEGER" property="hideSign" />
    <result column="lvl" jdbcType="INTEGER" property="lvl" />
  </resultMap>
  <sql id="Base_Column_List">
    id, create_id, create_time, update_time, deleted_sign, `name`, `type`, manager_id,
    dep_no, super_id, remarks, `status`, abbreviation, finance_id, province, working_hours_type,
    hide_sign, lvl
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from department
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    delete from department
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.Department">
    insert into department (id, create_id, create_time,
      update_time, deleted_sign, `name`,
      `type`, manager_id, dep_no,
      super_id, remarks, `status`,
      abbreviation, finance_id, province,
      working_hours_type, hide_sign, lvl
      )
    values (#{id,jdbcType=VARCHAR}, #{createId,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP},
      #{updateTime,jdbcType=TIMESTAMP}, #{deletedSign,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR},
      #{type,jdbcType=VARCHAR}, #{managerId,jdbcType=VARCHAR}, #{depNo,jdbcType=VARCHAR},
      #{superId,jdbcType=VARCHAR}, #{remarks,jdbcType=VARCHAR}, #{status,jdbcType=VARCHAR},
      #{abbreviation,jdbcType=VARCHAR}, #{financeId,jdbcType=VARCHAR}, #{province,jdbcType=INTEGER},
      #{workingHoursType,jdbcType=INTEGER}, #{hideSign,jdbcType=INTEGER}, #{lvl,jdbcType=INTEGER}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.goafanti.common.model.Department">
    insert into department
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="createId != null">
        create_id,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
      <if test="deletedSign != null">
        deleted_sign,
      </if>
      <if test="name != null">
        `name`,
      </if>
      <if test="type != null">
        `type`,
      </if>
      <if test="managerId != null">
        manager_id,
      </if>
      <if test="depNo != null">
        dep_no,
      </if>
      <if test="superId != null">
        super_id,
      </if>
      <if test="remarks != null">
        remarks,
      </if>
      <if test="status != null">
        `status`,
      </if>
      <if test="abbreviation != null">
        abbreviation,
      </if>
      <if test="financeId != null">
        finance_id,
      </if>
      <if test="province != null">
        province,
      </if>
      <if test="workingHoursType != null">
        working_hours_type,
      </if>
      <if test="hideSign != null">
        hide_sign,
      </if>
      <if test="lvl != null">
        lvl,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="createId != null">
        #{createId,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="deletedSign != null">
        #{deletedSign,jdbcType=INTEGER},
      </if>
      <if test="name != null">
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="type != null">
        #{type,jdbcType=VARCHAR},
      </if>
      <if test="managerId != null">
        #{managerId,jdbcType=VARCHAR},
      </if>
      <if test="depNo != null">
        #{depNo,jdbcType=VARCHAR},
      </if>
      <if test="superId != null">
        #{superId,jdbcType=VARCHAR},
      </if>
      <if test="remarks != null">
        #{remarks,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        #{status,jdbcType=VARCHAR},
      </if>
      <if test="abbreviation != null">
        #{abbreviation,jdbcType=VARCHAR},
      </if>
      <if test="financeId != null">
        #{financeId,jdbcType=VARCHAR},
      </if>
      <if test="province != null">
        #{province,jdbcType=INTEGER},
      </if>
      <if test="workingHoursType != null">
        #{workingHoursType,jdbcType=INTEGER},
      </if>
      <if test="hideSign != null">
        #{hideSign,jdbcType=INTEGER},
      </if>
      <if test="lvl != null">
        #{lvl,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.Department">
    update department
    <set>
      <if test="createId != null">
        create_id = #{createId,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="deletedSign != null">
        deleted_sign = #{deletedSign,jdbcType=INTEGER},
      </if>
      <if test="name != null">
        `name` = #{name,jdbcType=VARCHAR},
      </if>
      <if test="type != null">
        `type` = #{type,jdbcType=VARCHAR},
      </if>
      <if test="managerId != null">
        manager_id = #{managerId,jdbcType=VARCHAR},
      </if>
      <if test="depNo != null">
        dep_no = #{depNo,jdbcType=VARCHAR},
      </if>
      <if test="superId != null">
        super_id = #{superId,jdbcType=VARCHAR},
      </if>
      <if test="remarks != null">
        remarks = #{remarks,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        `status` = #{status,jdbcType=VARCHAR},
      </if>
      <if test="abbreviation != null">
        abbreviation = #{abbreviation,jdbcType=VARCHAR},
      </if>
      <if test="financeId != null">
        finance_id = #{financeId,jdbcType=VARCHAR},
      </if>
      <if test="province != null">
        province = #{province,jdbcType=INTEGER},
      </if>
      <if test="workingHoursType != null">
        working_hours_type = #{workingHoursType,jdbcType=INTEGER},
      </if>
      <if test="hideSign != null">
        hide_sign = #{hideSign,jdbcType=INTEGER},
      </if>
      <if test="lvl != null">
        lvl = #{lvl,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.Department">
    update department
    set create_id = #{createId,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      deleted_sign = #{deletedSign,jdbcType=INTEGER},
      `name` = #{name,jdbcType=VARCHAR},
      `type` = #{type,jdbcType=VARCHAR},
      manager_id = #{managerId,jdbcType=VARCHAR},
      dep_no = #{depNo,jdbcType=VARCHAR},
      super_id = #{superId,jdbcType=VARCHAR},
      remarks = #{remarks,jdbcType=VARCHAR},
      `status` = #{status,jdbcType=VARCHAR},
      abbreviation = #{abbreviation,jdbcType=VARCHAR},
      finance_id = #{financeId,jdbcType=VARCHAR},
      province = #{province,jdbcType=INTEGER},
      working_hours_type = #{workingHoursType,jdbcType=INTEGER},
      hide_sign = #{hideSign,jdbcType=INTEGER},
      lvl = #{lvl,jdbcType=INTEGER}
    where id = #{id,jdbcType=VARCHAR}
  </update>


  <select id="selectOrganizationList" parameterType="String" resultType="com.goafanti.organization.bo.OrganizationListOut">
    select
    om.id,om.create_id as createId,om.create_time as createTime,
    om.update_time as updateTime,om.deleted_sign as deletedSign,
    om.name,om.type,
    om.dep_no as depNo,
    om.remarks,
    om.status,
    om.hide_sign hideSign ,
    dm.name as superName,
    wh.name as workingHoursName,
    x.name as managerName
    from department om
    left join department dm on om.super_id = dm.id
    left join admin x on om.manager_id = x.id
    left join working_hours wh  on om.working_hours_type =wh.`type`
    where 	om.deleted_sign = 0
    <if test="name !=null">
      AND om.name like CONCAT('%',#{name,jdbcType=VARCHAR},'%')
    </if>
    <if test="superId !=null">
      AND om.super_id=#{superId,jdbcType=VARCHAR}
    </if>
    <if test="type !=null">
      AND om.type=#{type,jdbcType=VARCHAR}
    </if>
    <if test="depNo !=null">
      AND om.dep_no=#{depNo,jdbcType=VARCHAR}
    </if>
    <if test="page_sql != null">
      ${page_sql}
    </if>
  </select>
  <select id="selectOrganizationCount" resultType="java.lang.Integer">
    select
    count(0)
    from department om
    left join department dm on om.super_id = dm.id
    left join admin x on om.manager_id = x.id
    where om.deleted_sign = 0
    <if test="name !=null">
      AND om.name like CONCAT('%',#{name,jdbcType=VARCHAR},'%')
    </if>
    <if test="superId !=null">
      AND om.super_id=#{superId,jdbcType=VARCHAR}
    </if>
    <if test="type !=null">
      AND om.type=#{type,jdbcType=VARCHAR}
    </if>
    <if test="depNo !=null">
      AND om.dep_no=#{depNo,jdbcType=VARCHAR}
    </if>
  </select>
  <select id="selectSuperId"  resultType="com.goafanti.organization.bo.DepOut">
    select
    id,name,dep_no as depNo,lvl,super_id superId
    from department
    where status ='0'
    <if test="lvl != null ">
        and lvl= #{lvl}
    </if>
      <if test="hideSign != null ">
    and hide_sign= #{hideSign}
      </if>
    <if test="id != null ">
      and super_id= #{id}
    </if>
    order by depNo
  </select>
  <insert id="insert1" parameterType="com.goafanti.organization.bo.OrganizationListOut">
    insert into department(
      id,create_id,create_time,update_time,deleted_sign,
      name,type,manager_id,dep_no,super_id,status)
    values(#{id,jdbcType=VARCHAR},#{createId,jdbcType=VARCHAR},
           #{createTime,jdbcType=VARCHAR},#{updateTime,jdbcType=VARCHAR},
           #{deletedSign,jdbcType=VARCHAR},#{name,jdbcType=VARCHAR},
           #{type,jdbcType=VARCHAR},#{managerId,jdbcType=VARCHAR},
           #{depNo,jdbcType=VARCHAR},#{superId,jdbcType=VARCHAR},
           #{status,jdbcType=VARCHAR})
  </insert>
  <!-- 得到上级 -->
  <select id="selectDepNo" resultType="String">
    select
      dep_no as depNo
    from department
    where
      id=#{superId,jdbcType=VARCHAR}
  </select>

  <select id="selectDepNoByName" resultType="String">
    select
      dep_no as depNo
    from department
    where
      name=#{name,jdbcType=VARCHAR}
  </select>
  <select id="selectDepNoCount" parameterType="String" resultType="java.lang.Integer">
    select
    count(0)
    from department
    where
    <!-- dep_no like concat(#{depNo,jdbcType=VARCHAR},'%') -->
    super_id=#{superId,jdbcType=VARCHAR}
  </select>
  <select id="selectName" resultType="com.goafanti.admin.bo.AdminListBo">
    select
      name,id
    from admin
    where status= 0
      and name  like concat('%',#{name,jdbcType=VARCHAR},'%')
  </select>
  <select id="selectAllById" resultType="com.goafanti.organization.bo.OrganizationListOut">
    select a.id, a.create_id as createId, a.create_time as createTime, a.update_time as updateTime,
           a.deleted_sign as deletedSign, a.name, a.type, a.manager_id as managerId, a.dep_no as depNo,
           a.super_id as superId, a.remarks, a.status, a.abbreviation, a.finance_id financeId,a.hide_sign hideSign,
           a.province, c.name financeName, a.working_hours_type workingHoursType, b.name as managerName
    from department a
           left join admin b on a.manager_id = b.id left join admin c on a.finance_id = c.id
    where a.id=#{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteById" parameterType="java.lang.String">
    delete from department
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <select id="selectAdminNameById" parameterType="java.lang.String" resultType="java.lang.String">
    select
      name
    from admin
    where
      id=#{id,jdbcType=VARCHAR}
  </select>
  <select id="selectIdByDepNo" parameterType="java.lang.String" resultType="java.lang.String">
    select
      id
    from department
    where
      dep_no=#{depNo,jdbcType=VARCHAR}
  </select>
  <select id="selectIDNBySuperId" parameterType="java.lang.String" resultType="com.goafanti.organization.bo.OrganizationListOut">
    select
      id,create_id as createId,create_time as createTime,
      update_time as updateTime,deleted_sign as deletedSign,
      name,type,manager_id as managerId,
      dep_no as depNo,
      super_id as superId,
      remarks,status
    from department
    where
      super_id=#{id,jdbcType=VARCHAR}
    order by dep_no
  </select>
  <update id="updateByNumber" parameterType="com.goafanti.organization.bo.OrganizationListOut">
    update department
    <set>
      dep_no = #{depNo,jdbcType=VARCHAR},
        lvl= #{lvl}
    </set>
    where
    id=#{id,jdbcType=VARCHAR}
  </update>
  <select id="selectCountBySuperId" parameterType="String" resultType="java.lang.Integer">
    select
      count(0)
    from department
    where
      super_id=#{superId,jdbcType=VARCHAR}
  </select>
  <select id="selectIdByName" parameterType="java.lang.String" resultType="java.lang.String">
    select
      id
    from department
    where
      name=#{name,jdbcType=VARCHAR}
  </select>
  <select id="selectNameById" parameterType="java.lang.String" resultType="java.lang.String">
    select
      name
    from department
    where
      id=#{id,jdbcType=VARCHAR}
  </select>
  <select id="selectDepNoById" resultType="String">
    select
      dep_no as depNo
    from department
    where
      id=#{id,jdbcType=VARCHAR}
  </select>
  <select id="selectUserById" parameterType="java.lang.String" resultType="java.lang.Integer">
    select
      count(0)
    from admin
    where
      department_id=#{id,jdbcType=VARCHAR}
  </select>
  <!-- 查询当前层级 -->
  <select id="selectCurrentDeps" parameterType="java.lang.String" resultType="string">
    select id from department where manager_id = #{aid,jdbcType=VARCHAR}
    union
    select id from department where id = (select department_id from admin where id = #{aid,jdbcType=VARCHAR})
  </select>
  <!-- 查询下一层级  -->
  <select id="selectSubDeps"  parameterType="java.lang.String" resultType="string">
    select id from department where super_id in (#{superIds,jdbcType=VARCHAR})
  </select>

  <!-- 查询下一层级  -->
  <select id="selectSubDepsList"  parameterType="java.lang.String" resultType="com.goafanti.common.model.Department">
    select id from department where super_id in (#{superIds,jdbcType=VARCHAR})
  </select>

  <select id="selectBysuperId"  resultType="com.goafanti.admin.bo.DepCountBo">
    select a.id ,a.name ,0 userCount,0 inputCount,0 receiveCount, 0 completeCount, 0 interviewCount
    from department a  where a.super_id= #{id}
  </select>

  <select id="selectBysuperName" resultType="com.goafanti.admin.bo.DepCountBo">
    select a.id ,a.name ,0 userCount,0 inputCount,0 receiveCount, 0 completeCount, 0 interviewCount
    from department a left join department b on a.super_id=b.id where b.name= #{name}
  </select>

  <!-- 查询所有 -->
  <select id="selectAllDeps" resultType="com.goafanti.common.model.Department">
    select id from department
  </select>
  <select id="getMaxAbbreviation"  parameterType="java.lang.String" resultType="java.lang.String">
    select max(contract_no) as maxNo from t_order_new
    where create_time > '2019-5-31'
      and contract_no like  concat(#{abbreviation,jdbcType=VARCHAR},'%')
  </select>

  <select id="getDepAll" resultType="com.goafanti.admin.bo.AdminCustomerBo">
    select a.id aid,a.name aName,d.id depId,d.name depName,ifnull(b.userCount,0)userCount,ifnull(b.channelCount,0)channelCount,
    ifnull(b.signCount,0)signCount,ifnull(b.newCount,0)newCount,ifnull(b.channelNewCount,0)channelNewCount,ifnull( b.receiveCount,0)receiveCount,
    ifnull( b.completeCount,0)completeCount,ifnull(b.channelCompleteCount,0)channelCompleteCount
    from admin a left join (select x.aid,x.userCount,x.channelCount,signCount,x.newCount,x.channelNewCount,x.receiveCount,
    ifnull(y.completeCount,0)completeCount,ifnull(y.channelCompleteCount,0)channelCompleteCount
    from (select aid,sum(if( share_type=0 and channel=0,1,0))userCount,
    sum(if(share_type=3 and  channel=1,1,0))channelCount,
    sum(if( share_type=2 ,1,0))signCount,
    sum(if(share_type=0 and source =1  and channel=0,1,0))newCount,
    sum(if(share_type=0 and source =1  and channel=1,1,0))channelNewCount,
    sum(if(share_type=0 and source =2  and channel=0,1,0))receiveCount
    from `user` where `type` =1 and new_channel=0
    <if test="startTime != null and endTime != null">
      and  transfer_time  between #{startTime,jdbcType=VARCHAR} and #{endTime,jdbcType=VARCHAR}
    </if>
    group by aid)x left join (select a.aid,sum(if(b.channel=0,1,0))completeCount,
    sum(if(b.channel=1,1,0))channelCompleteCount from public_release a left join public_release_details prd on a.id=prd.id
    left join `user` b on prd.uid =b.id
    where prd.clock_in =1
    <if test="startTime != null and endTime != null">
      and prd.clock_in_time between #{startTime,jdbcType=VARCHAR} and #{endTime,jdbcType=VARCHAR}
    </if>
    group by a.aid ) y on x.aid=y.aid)b on a.id=b.aid
    left join department d on a.department_id=d.id
    where  a.status= 0  and a.type=1
    <if test="adminName !=null">
      and a.name  like concat('%',#{adminName},'%')
    </if>
    <if test="depId !=null">
      and d.id= #{depId}
    </if>
  </select>
  <select id="AlldepCount" resultType="com.goafanti.admin.bo.AdminCustomerBo">
    select a.id aid,a.name aName ,a.department_id depId,c.name depName,ifnull(userCount,0)userCount,
    ifnull(channelCount,0)channelCount,ifnull(signCount,0)signCount,ifnull(newCount,0)newCount,
    ifnull(channelNewCount,0)channelNewCount,ifnull(receiveCount,0)receiveCount,ifnull(completeCount,0)completeCount,
    ifnull(channelCompleteCount,0)channelCompleteCount
    from admin a left join (select aid,sum(if( share_type=0 and channel=0,1,0))userCount,
    sum(if(share_type=3 and  channel=1,1,0))channelCount,
    sum(if( share_type=2 ,1,0))signCount,
    sum(if(share_type=0 and source =1  and channel=0,1,0))newCount,
    sum(if(share_type=0 and source =1  and channel=1,1,0))channelNewCount,
    sum(if(share_type=0 and source =2  and channel=0,1,0))receiveCount
    from `user` where `type` =1 and new_channel=0
    <if test="startTime != null and endTime != null">
      and  transfer_time  between #{startTime,jdbcType=VARCHAR} and #{endTime,jdbcType=VARCHAR}
    </if>
    group by aid)b on a.id=b.aid
    left join (select a.aid,sum(if(c.channel=0,1,0))completeCount,
    sum(if(c.channel=1,1,0))channelCompleteCount
    from public_release a left join public_release_details b on a.id=b.prid left join `user` c on b.uid=c.id
    where b.clock_in =1
    <if test="startTime != null and endTime != null">
      and b.clock_in_time between #{startTime,jdbcType=VARCHAR} and #{endTime,jdbcType=VARCHAR}
    </if>
    group by a.aid ) y on a.id=y.aid
    left join department c on a.department_id =c.id
    where  a.status= 0  and a.type=1
    <if test="depIds!= null">
      and a.department_id in
      <foreach close=")" collection="depIds" item="deps" open="(" separator=",">
        #{deps.id}
      </foreach>
    </if>
  </select>
  <select id="selectByDepNo" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from department where super_id=  #{superId}
    order by dep_no desc limit 1
  </select>
    <select id="getFinanceDep" resultType="java.lang.String">
      select id from department
      where finance_id = #{aid};
    </select>

</mapper>
