<?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.ruoyi.rc.mapper.TAuditMapper">
    
    <resultMap type="TAudit" id="TAuditResult">
        <result property="id"    column="id"    />
        <result property="year"    column="year"    />
        <result property="preAuditTime"    column="pre_audit_time"    />
        <result property="auditTime"    column="audit_time"    />
        <result property="auditResult"    column="audit_result"    />
        <result property="remarks"    column="remarks"    />
        <result property="deptId"    column="dept_id"    />
    </resultMap>

    <sql id="selectTAuditVo">
        select d.id, d.year, d.pre_audit_time, d.audit_time, d.audit_result, d.remarks, d.dept_id from t_audit d
    </sql>

    <select id="selectTAuditList" parameterType="TAudit" resultMap="TAuditResult">
        <include refid="selectTAuditVo"/>
        <where>  
            <if test="year != null  and year != ''"> and year = #{year}</if>
            <if test="preAuditTime != null "> and pre_audit_time = #{preAuditTime}</if>
            <if test="auditTime != null "> and audit_time = #{auditTime}</if>
            <if test="auditResult != null  and auditResult != ''"> and audit_result = #{auditResult}</if>
            <if test="remarks != null  and remarks != ''"> and remarks = #{remarks}</if>
            <if test="deptId != null "> and dept_id like concat(concat('%', #{deptId}), '%')</if>
            ${params.dataScope}
        </where>
    </select>

    <select id="selectTAuditLatest" parameterType="TAudit" resultMap="TAuditResult">
        <include refid="selectTAuditVo"/>
        where dept_id like concat(concat('%', #{deptId}), '%')
        order by d.audit_time desc, d.pre_audit_time desc
        limit 1
    </select>
    
    <select id="selectTAuditById" parameterType="Long" resultMap="TAuditResult">
        <include refid="selectTAuditVo"/>
        where id = #{id}
    </select>

    <insert id="insertTAudit" parameterType="TAudit" useGeneratedKeys="true" keyProperty="id">
        insert into t_audit
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="year != null">year,</if>
            <if test="preAuditTime != null">pre_audit_time,</if>
            <if test="auditTime != null">audit_time,</if>
            <if test="auditResult != null">audit_result,</if>
            <if test="remarks != null">remarks,</if>
            <if test="deptId != null">dept_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="year != null">#{year},</if>
            <if test="preAuditTime != null">#{preAuditTime},</if>
            <if test="auditTime != null">#{auditTime},</if>
            <if test="auditResult != null">#{auditResult},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="deptId != null">#{deptId},</if>
         </trim>
    </insert>

    <update id="updateTAudit" parameterType="TAudit">
        update t_audit
        <trim prefix="SET" suffixOverrides=",">
            <if test="year != null">year = #{year},</if>
            <if test="preAuditTime != null">pre_audit_time = #{preAuditTime},</if>
            <if test="auditTime != null">audit_time = #{auditTime},</if>
            <if test="auditResult != null">audit_result = #{auditResult},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTAuditById" parameterType="Long">
        delete from t_audit where id = #{id}
    </delete>

    <delete id="deleteTAuditByIds" parameterType="String">
        delete from t_audit where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectHomeData1" resultType="int">
      select count(*) from t_audit where audit_time > now() and extract(year from audit_time) = extract(year from now());
    </select>

    <select id="selectHomeData2" resultType="java.math.BigDecimal">
      select (select count(*) from t_audit where audit_result='绿区') / (select count(*) from t_audit) from dual;
    </select>

    <select id="selectHomeData3" parameterType="Long" resultType="int">
      select count(*) from t_open_item
      where questionnaire_id in (select questionnaire_id from t_questionnaire where audit_id = #{auditId});
    </select>

    <select id="selectHomeData4" parameterType="Long" resultType="int">
      select count(*) from t_open_item
      where questionnaire_id in (select questionnaire_id from t_questionnaire where audit_id = #{auditId})
      and status = 4;
    </select>
</mapper>