<?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.social.mapper.TAttaInfoMapper">
      <resultMap id="tAttaInfoMap" type="com.yifu.cloud.plus.v1.yifu.social.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}
        order by a.relation_type
    </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="getLeaveDocTemplate" resultType="java.lang.String">
		SELECT
			a.atta_src
		FROM t_atta_info a where a.id = "1"
	</select>

	<!-- 税友花名册模板ID：11、12、13、14 （社保增减，医保增减）-->
	<select id="getTemplateSrcById" resultMap="tAttaInfoMap">
		SELECT
			a.id,
			a.atta_src
		FROM t_atta_info a where a.id in ('11','12','13','14')
	</select>

	<!-- 1：户配置的章，11社保增花名册模板；12社保减花名册模板；13医保增花名册模板；14医保减花名册模板-->
	<select id="getTemplateSrcByIdAndType" resultType="java.lang.String">
		SELECT
			a.atta_src
		FROM t_atta_info a where a.domain_id = #{domainId} and a.relation_type = #{relationType}
		limit 1
	</select>

</mapper>
