<?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.yifu.cloud.plus.v1.yifu.archives.mapper.TAttaInfoMapper">
      <resultMap id="tAttaInfoMap" type="com.yifu.cloud.plus.v1.yifu.archives.entity.TAttaInfo">
            <id property="id" column="id"/>
            <result property="attaName" column="atta_name"/>
            <result property="attaSrc" column="atta_src"/>
            <result property="attaSize" column="atta_size"/>
            <result property="attaType" column="atta_type"/>
            <result property="createBy" column="create_by"/>
            <result property="createTime" column="create_time"/>
            <result property="relationType" column="relation_type"/>
            <result property="domainId" column="domain_id"/>
      </resultMap>
      <sql id="Base_Column_List">
                 a.id,
                      a.atta_name,
                      a.atta_src,
                      a.atta_size,
                      a.atta_type,
                      a.create_by,
                      a.create_time,
                      a.relation_type,
                      a.domain_id
      </sql>
      <sql id="tAttaInfo_where">
            <if test="tAttaInfo != null">
                <if test="tAttaInfo.id != null and tAttaInfo.id.trim() != ''">
                    AND a.id = #{tAttaInfo.id}
                </if>
                <if test="tAttaInfo.attaName != null and tAttaInfo.attaName.trim() != ''">
                    AND a.atta_name = #{tAttaInfo.attaName}
                </if>
                <if test="tAttaInfo.attaSrc != null and tAttaInfo.attaSrc.trim() != ''">
                    AND a.atta_src = #{tAttaInfo.attaSrc}
                </if>
                <if test="tAttaInfo.attaSize != null">
                    AND a.atta_size = #{tAttaInfo.attaSize}
                </if>
                <if test="tAttaInfo.attaType != null and tAttaInfo.attaType.trim() != ''">
                    AND a.atta_type = #{tAttaInfo.attaType}
                </if>
                <if test="tAttaInfo.createBy != null and tAttaInfo.createBy.trim() != ''">
                    AND a.create_by = #{tAttaInfo.createBy}
                </if>
                <if test="tAttaInfo.createTime != null">
                    AND a.create_time = #{tAttaInfo.createTime}
                </if>
                <if test="tAttaInfo.relationType != null and tAttaInfo.relationType.trim() != ''">
                    AND a.relation_type = #{tAttaInfo.relationType}
                </if>
                <if test="tAttaInfo.domainId != null and tAttaInfo.domainId.trim() != ''">
                    AND a.domain_id = #{tAttaInfo.domainId}
                </if>
            </if>
      </sql>
    <!--tAttaInfo简单分页查询-->
    <select id="getTAttaInfoPage" resultMap="tAttaInfoMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_atta_info a
        <where>
            1=1
            <include refid="tAttaInfo_where"/>
        </where>
    </select>

    <!--tAttaInfo 获取list-->
    <select id="getTAttaInfoList" resultMap="tAttaInfoMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_atta_info a
        <where>
            1=1
            <include refid="tAttaInfo_where"/>
        </where>
    </select>

    <!--根据domainId 获取list-->
    <select id="getTAttaInfoListByDoMainId" resultMap="tAttaInfoMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_atta_info a
        where a.domain_id = #{domainId}
    </select>

    <select id="getKeenAtta" resultMap="tAttaInfoMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_atta_info a
        <where>
            1=1
            <if test="values != null and values.size() > 0">
                and  a.domain_id in
                <foreach collection="values" item="param" index="index" open="(" close=")" separator=",">
                    #{param}
                </foreach>
            </if>
            and a.create_time >= #{dayStart}
            and a.relation_type in ("3","4")
        </where>
    </select>

	<update id="updateDomainId">
		update t_atta_info
		<trim prefix="set" suffixOverrides=",">
			domain_id=#{domainId}
		</trim>
		WHERE
		<if test="ids != null and ids.size() > 0">
			id in
			<foreach collection="ids" item="param" index="index" open="(" close=")" separator=",">
				#{param}
			</foreach>
		</if>
	</update>

	<select id="getAttaByApplyId" resultMap="tAttaInfoMap">
		SELECT
			<include refid="Base_Column_List"/>
		FROM t_atta_info a
		where EXISTS (select 1 from m_salary_prova_atta b where b.SALARY_PROVA_ID = #{applyId} and a.id = b.ATTA_ID);
	</select>

	<delete id="deleteByDomainId">
		delete from t_atta_info where domain_id=#{domainId}
	</delete>

	<delete id="deleteByDomainIdAndType">
		delete from t_atta_info where domain_id=#{domainId} and relation_type = #{relationType}
	</delete>

	<delete id="deleteByDomainIdAndOther">
		delete from t_atta_info where domain_id=#{domainId} and relation_type not in ('0','2','26','27')
	</delete>
	<select id="getAttInfoByParam" resultType="com.yifu.cloud.plus.v1.yifu.archives.vo.TAttaInfoVo">
		SELECT
		b.id,
		b.atta_name,
		b.atta_src,
		b.atta_size,
		b.atta_type,
		b.create_by,
		b.create_time,
		b.relation_type,
		b.domain_id, a.EMP_NAME,a.EMP_IDCARD
		from (select id,EMP_NAME,EMP_IDCARD from t_pre_emp_main where `STATUS`='4' and SOUR_TYPE='0'
		                                    and DEPT_NO=#{departNo}
		                                    and AUDIT_TIME >= #{startDate}
		                                    and AUDIT_TIME <![CDATA[ <= ]]> #{endDate}) a
		LEFT JOIN  (select * from t_atta_info where RELATION_TYPE ='23' GROUP BY  DOMAIN_ID  ) b  on a.id=b.DOMAIN_ID
		where b.DOMAIN_ID is not null
	</select>
	<select id="getAttInfoCountByParam" resultType="java.lang.Integer">
		SELECT
			count(1)
		from (select id from t_pre_emp_main where `STATUS`='4' and SOUR_TYPE='0'
											  and DEPT_NO = #{departNo}
											  and AUDIT_TIME >= #{startDate}
											  and AUDIT_TIME <![CDATA[ <= ]]> #{endDate}) a
				 LEFT JOIN  (select * from t_atta_info where RELATION_TYPE ='23' GROUP BY  DOMAIN_ID  ) b  on a.id=b.DOMAIN_ID
		where b.DOMAIN_ID is not null
	</select>
</mapper>
