<?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="cn.ezhizao.project.business.socialSecurity.mapper.BizSocialSecurityConfirmMapper">
    <!-- 基础的返回map 多表复杂的 自行组合 -->
    <resultMap id="BaseResultMap" type="cn.ezhizao.project.business.socialSecurity.domain.BizSocialSecurityConfirm">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="id" jdbcType="BIGINT" property="id"/>
        <result column="company_id" jdbcType="BIGINT" property="companyId"/>
    </resultMap>


    <!--带条件的List查询-->
    <select id="getList" resultMap="BaseResultMap">
        <include refid="getListSql"/>
    </select>

    <!--带条件的List查询 数据总条数-->
    <select id="getTotal" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM (<include refid="getListSql"/>) AS C
    </select>

    <sql id="getListSql">
        SELECT t1.*, t2.name as company_name,
        (select concat(frtd.year, '-', frtd.month) from biz_social_security_confirm_detail frtd where frtd.social_security_confirm_id = t1.id order by year desc, month desc limit 1) currentDate,
        t3.account_name as from_company_name, t2.tax_type
        FROM biz_social_security_confirm t1
        left join biz_company t2 on t2.id = t1.company_id
        left join biz_tenant t3 on t3.id = t1.tenant_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0 AND
            <if test="id != null  ">
                t1.id = #{id} AND
            </if>
            <if test="workOrderId != null">
                t1.work_order_id = #{workOrderId} AND
            </if>
            <if test="companyId != null  ">
                t1.company_id = #{companyId} AND
            </if>
            <if test="accountSetsId != null  ">
                t1.account_sets_id = #{accountSetsId} AND
            </if>
            <if test="taxType != null and taxType != ''">
                t2.tax_type = #{taxType} AND
            </if>
            <if test="fromCompanyName != null and fromCompanyName != ''">
                (t3.account_name like concat('%', #{fromCompanyName}, '%')) AND
            </if>
            <if test="keyword != null and keyword != ''">
                (t2.name like concat('%', #{keyword}, '%') or t2.code like concat('%', #{keyword}, '%')) AND
            </if>
            <if test="currentEmployeeId != null  ">
                t1.current_employee_id = #{currentEmployeeId} AND
            </if>
            <if test="employeeIds != null  and employeeIds != '' ">
                t1.employee_ids = #{employeeIds} AND
            </if>
            <if test="createTime != null  ">
                t1.create_time = #{createTime} AND
            </if>
            <if test="creatorId != null  ">
                t1.creator_id = #{creatorId} AND
            </if>
            <if test="updateTime != null  ">
                t1.update_time = #{updateTime} AND
            </if>
            <if test="updaterId != null  ">
                t1.updater_id = #{updaterId} AND
            </if>
            <if test="deleted != null  ">
                t1.deleted = #{deleted} AND
            </if>
            <if test="version != null  ">
                t1.version = #{version} AND
            </if>
            <if test="month != null and year != null">
                <if test="tenantId != null">
                    exists (select 1 from biz_entrust eb
                    where eb.deleted = 0
                    and (eb.to_tenant_id = #{tenantId} or eb.from_tenant_id = #{tenantId})
                    and date_format(eb.current_month, '%Y%m') = concat(#{year}, #{month})
                    and eb.work_order_id = t1.work_order_id
                    ) AND
                </if>
                <if test="status != null and status != 0">
                    AND exists (select 1 from biz_social_security_confirm_detail fsd where fsd.social_security_confirm_id = t1.id and
                    fsd.month = #{month} and fsd.year = #{year} and fsd.status = #{status}) AND
                </if>
                <if test="status != null and status == 0">
                    AND not exists (select 1 from biz_social_security_confirm_detail fsd where fsd.social_security_confirm_id = t1.id and
                    fsd.month = #{month} and fsd.year = #{year}) AND
                </if>
            </if>
        </trim>
    </sql>
    <update id="updateByWorkOrderId"
            parameterType="java.util.List">
        update biz_social_security_confirm set is_stop=1
        WHERE work_order_id in
        <foreach collection="ids" item="id" index="index" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
</mapper>
