<?xml version="1.0" encoding="UTF-8"?>

<!--
  ~
  ~      Copyright (c) 2018-2025, lengleng All rights reserved.
  ~
  ~  Redistribution and use in source and binary forms, with or without
  ~  modification, are permitted provided that the following conditions are met:
  ~
  ~ Redistributions of source code must retain the above copyright notice,
  ~  this list of conditions and the following disclaimer.
  ~  Redistributions in binary form must reproduce the above copyright
  ~  notice, this list of conditions and the following disclaimer in the
  ~  documentation and/or other materials provided with the distribution.
  ~  Neither the name of the yifu4cloud.com developer nor the names of its
  ~  contributors may be used to endorse or promote products derived from
  ~  this software without specific prior written permission.
  ~  Author: lengleng (wangiegie@gmail.com)
  ~
  -->

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.yifu.cloud.plus.v1.yifu.archives.mapper.TEmployeeProjectBelongDeptMapper">

	<resultMap id="tEmployeeProjectBelongDeptMap"
			   type="com.yifu.cloud.plus.v1.yifu.archives.entity.TEmployeeProjectBelongDept">
		<id property="id" column="ID"/>
		<result property="empDeptname" column="emp_deptname"/>
		<result property="empDeptid" column="emp_deptid"/>
	</resultMap>
	<sql id="tEmployeeProjectBelongDept_where">
		/* 派单审核通过，或其他路径 2025-3-19 15:22:08 倩倩要求去掉审核条件*/
		<if test="tEmployeeProjectBelongDept != null">
			<if test="tEmployeeProjectBelongDept.deptId != null and tEmployeeProjectBelongDept.deptId.trim() != ''">
				AND a.DEPT_ID = #{tEmployeeProjectBelongDept.deptId}
			</if>
			<if test="tEmployeeProjectBelongDept.empDeptId != null and tEmployeeProjectBelongDept.empDeptId.trim() != ''">
				<if test="tEmployeeProjectBelongDept.empDeptId.trim() == '-1'">
					/* 查询未关联的 */
					AND b.id is null
				</if>
				<if test="tEmployeeProjectBelongDept.empDeptId.trim() == '不查数据'">
					/* 不查数据 */
					AND b.id = '不查数据'
				</if>
			</if>
			<if test="tEmployeeProjectBelongDept.empName != null and tEmployeeProjectBelongDept.empName.trim() != ''">
				AND a.EMP_NAME like concat('%',#{tEmployeeProjectBelongDept.empName},'%')
			</if>
			<if test="tEmployeeProjectBelongDept.empIdcard != null and tEmployeeProjectBelongDept.empIdcard.trim() != ''">
				AND a.EMP_IDCARD like concat('%',#{tEmployeeProjectBelongDept.empIdcard},'%')
			</if>
			<if test="tEmployeeProjectBelongDept.empPhone != null and tEmployeeProjectBelongDept.empPhone.trim() != ''">
				AND e.EMP_PHONE like concat('%',#{tEmployeeProjectBelongDept.empPhone},'%')
			</if>
			<if test="tEmployeeProjectBelongDept.ids != null and tEmployeeProjectBelongDept.ids.size() > 0">
				a.ID in
				<foreach collection="tEmployeeProjectBelongDept.ids" item="param" index="index" open="(" close=")"
						 separator=",">
					#{param}
				</foreach>
			</if>
			<if test="tEmployeeProjectBelongDept.empDeptIdArray != null and tEmployeeProjectBelongDept.empDeptIdArray.length > 0">
				AND b.emp_deptid in
				<foreach item="idStr" index="index" collection="tEmployeeProjectBelongDept.empDeptIdArray" open="("
						 separator="," close=")">
					#{idStr}
				</foreach>
			</if>
			<if test="tEmployeeProjectBelongDept.contractStatusArray != null and tEmployeeProjectBelongDept.contractStatusArray.length > 0">
				AND a.CONTRACT_STATUS in
				<foreach item="idStr" index="index" collection="tEmployeeProjectBelongDept.contractStatusArray" open="("
						 separator="," close=")">
					#{idStr}
				</foreach>
			</if>
			<if test="tEmployeeProjectBelongDept.insuranceStatusArray != null and tEmployeeProjectBelongDept.insuranceStatusArray.length > 0">
				AND a.INSURANCE_STATUS in
				<foreach item="idStr" index="index" collection="tEmployeeProjectBelongDept.insuranceStatusArray"
						 open="("
						 separator="," close=")">
					#{idStr}
				</foreach>
			</if>
			<if test="tEmployeeProjectBelongDept.projectStatusArray != null and tEmployeeProjectBelongDept.projectStatusArray.length > 0">
				AND a.PROJECT_STATUS in
				<foreach item="idStr" index="index" collection="tEmployeeProjectBelongDept.projectStatusArray" open="("
						 separator="," close=")">
					#{idStr}
				</foreach>
			</if>
			<if test="tEmployeeProjectBelongDept.postArray != null and tEmployeeProjectBelongDept.postArray.length > 0">
				AND a.POST in
				<foreach item="idStr" index="index" collection="tEmployeeProjectBelongDept.postArray" open="("
						 separator="," close=")">
					#{idStr}
				</foreach>
			</if>

			<if test="tEmployeeProjectBelongDept.enjoinDateAndLeaveTimeStart != null">
				and if(a.PROJECT_STATUS=0,a.ENJOIN_DATE,a.LEAVE_TIME) <![CDATA[ >= ]]>
				#{tEmployeeProjectBelongDept.enjoinDateAndLeaveTimeStart}
			</if>
			<if test="tEmployeeProjectBelongDept.enjoinDateAndLeaveTimeEnd != null">
				and if(a.PROJECT_STATUS=0,a.ENJOIN_DATE,a.LEAVE_TIME) <![CDATA[ <= ]]>
				#{tEmployeeProjectBelongDept.enjoinDateAndLeaveTimeEnd}
			</if>
		</if>
	</sql>
	<!--tEmployeeProjectBelongDept简单分页查询-->
	<select id="getTEmployeeProjectBelongDeptPage"
			resultType="com.yifu.cloud.plus.v1.yifu.archives.vo.EmployeeProjectScpVO">
		select
		a.EMP_NAME empName
		,a.EMP_IDCARD empIdcard
		,e.EMP_PHONE empPhone
		,b.emp_deptname empDeptname
		,a.POST post
		,if(a.PROJECT_STATUS=0,'在职','离职') projectStatus
		,if(a.PROJECT_STATUS=0,DATE_FORMAT(a.ENJOIN_DATE,'%Y-%m-%d'),DATE_FORMAT(a.LEAVE_TIME,'%Y-%m-%d')) enjoinDateAndLeaveTime
		,a.CONTRACT_STATUS contractStatus
		,a.INSURANCE_STATUS insuranceStatus
		from t_employee_project a
		LEFT JOIN t_employee_info e on a.emp_id=e.id
		LEFT JOIN t_employee_project_belong_dept b on a.id=b.id
		<where>
			a.DELETE_FLAG ='0'
			<include refid="tEmployeeProjectBelongDept_where"/>
		</where>
	</select>

	<!-- 导出的条数 -->
	<select id="getTEmployeeProjectBelongDeptCount" resultType="java.lang.Integer">
		select
		count(1)
		from t_employee_project a
		LEFT JOIN t_employee_info e on a.emp_id=e.id
		LEFT JOIN t_employee_project_belong_dept b on a.id=b.id
		<where>
			a.DELETE_FLAG ='0'
			<include refid="tEmployeeProjectBelongDept_where"/>
		</where>
	</select>

	<!-- 导出的数据 -->
	<select id="getTEmployeeProjectBelongDeptList"
			resultType="com.yifu.cloud.plus.v1.yifu.archives.vo.EmployeeProjectScpVO">
		select
		a.EMP_NAME empName
		,a.EMP_IDCARD empIdcard
		,e.EMP_PHONE empPhone
		,b.emp_deptname empDeptname
		,a.POST post
		,if(a.PROJECT_STATUS=0,'在职','离职') projectStatus
		,if(a.PROJECT_STATUS=0,DATE_FORMAT(a.ENJOIN_DATE,'%Y-%m-%d'),DATE_FORMAT(a.LEAVE_TIME,'%Y-%m-%d'))
		enjoinDateAndLeaveTime
		,a.CONTRACT_STATUS contractStatus
		,a.INSURANCE_STATUS insuranceStatus
		from t_employee_project a
		LEFT JOIN t_employee_info e on a.emp_id=e.id
		LEFT JOIN t_employee_project_belong_dept b on a.id=b.id
		<where>
			a.DELETE_FLAG ='0'
			<include refid="tEmployeeProjectBelongDept_where"/>
		</where>
		<if test="tEmployeeProjectBelongDept != null">
			<if test="tEmployeeProjectBelongDept.limitStart != null">
				limit #{tEmployeeProjectBelongDept.limitStart},#{tEmployeeProjectBelongDept.limitEnd}
			</if>
		</if>
	</select>

	<!-- 获取部门下花名册总数 -->
	<select id="getDeptCount" resultType="java.lang.Integer">
		select count(1)
		from t_employee_project_belong_dept b
		LEFT JOIN t_employee_project a on b.id = a.id
		<where>
			a.DELETE_FLAG ='0' and ((a.PROJECT_SOURCE = '1' and a.STATUS = 1) or a.PROJECT_SOURCE != '1')
			<if test="tEmployeeProjectBelongDept.deptId != null and tEmployeeProjectBelongDept.deptId.trim() != ''">
				AND a.DEPT_ID = #{tEmployeeProjectBelongDept.deptId}
			</if>
			<if test="tEmployeeProjectBelongDept.empDeptId != null and tEmployeeProjectBelongDept.empDeptId.trim() != ''">
				AND b.emp_deptid = #{tEmployeeProjectBelongDept.empDeptId}
			</if>
		</where>
	</select>

	<!-- 根据权限获取对应部门ID下的花名册总数Map -->
	<select id="getDeptCountMap" resultType="com.yifu.cloud.plus.v1.yifu.archives.vo.TEmployeeProjectBelongDeptSearchVo">
		select emp_deptid as empDeptId, count(emp_deptid) as empNum
		from t_employee_project_belong_dept b
		LEFT JOIN t_employee_project a on b.id = a.id
		<where>
			a.DELETE_FLAG ='0' and ((a.PROJECT_SOURCE = '1' and a.STATUS = 1) or a.PROJECT_SOURCE != '1')
			<if test="deptId != null and deptId.trim() != ''">
				AND a.DEPT_ID = #{deptId}
			</if>
			<if test="departIdSet != null and departIdSet.size > 0">
				AND b.emp_deptid in
				<foreach item="idStr" index="index" collection="departIdSet" open="(" separator=","
						 close=")">
					#{idStr}
				</foreach>
			</if>
		</where>
		group by emp_deptid
	</select>

	<!-- 没有关联部门的花名册总数 -->
	<select id="getNoneDeptCount" resultType="java.lang.Integer">
		select count(1)
		from t_employee_project a
		LEFT JOIN t_employee_project_belong_dept b on a.id=b.id
		<where>
			a.DELETE_FLAG ='0' and b.id is null
			<if test="tEmployeeProjectBelongDept.deptId != null and tEmployeeProjectBelongDept.deptId.trim() != ''">
				AND a.DEPT_ID = #{tEmployeeProjectBelongDept.deptId}
			</if>
		</where>
	</select>


</mapper>
