<?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.testor.module.accident.dao.AccidentReportDao">
    <!-- 开启二级缓存 -->
    <!-- <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> -->
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.testor.module.accident.model.domain.AccidentReport">
        <id column="id" property="id"/>
        <id column="ref_id" property="refId"/>
        <id column="parent_id" property="parentId"/>
        <id column="tenant_id" property="tenantId"/>
        <id column="process_id" property="processId"/>
        <id column="process_status" property="processStatus"/>
        <id column="report_unit" property="reportUnit"/>
        <id column="accident_time" property="accidentTime"/>
        <id column="fake_type" property="fakeType"/>
        <id column="accident_type" property="accidentType"/>
        <id column="reporter" property="reporter"/>
        <id column="accident_level" property="accidentLevel"/>
        <id column="accident_passed" property="accidentPassed"/>
        <id column="accident_cause" property="accidentCause"/>
        <id column="improvements" property="improvements"/>
        <id column="attachment" property="attachment"/>
    </resultMap>

    <!--<select id="getCountByAccidentTime" parameterType="com.testor.module.accident.model.domain.AccidentReport" resultType="string">-->
        <!--SELECT count(*) AS count-->
        <!--FROM t_accident_report-->
        <!--WHERE YEAR(accident_time) = #{accidentReport.attachment} AND process_status='5'-->
        <!--AND org_id in (select org.org_id from t_sys_org org where org.parent_id like concat('%', #{accidentReport.orgId}, '%'))-->
    <!--</select>-->

    <!--<select id="getAccidentReportByAccidentTime" parameterType="com.testor.module.accident.model.domain.AccidentReport" resultType="com.testor.module.accident.model.domain.AccidentReport">-->
        <!--SELECT *-->
        <!--FROM t_accident_report-->
        <!--WHERE YEAR(accident_time) = #{accidentReport.attachment} AND process_status='5'-->
        <!--AND org_id in (select org.org_id from t_sys_org org where org.parent_id like concat('%', #{accidentReport.orgId}, '%'))-->
    <!--</select>-->
    <select id="getCountByAccidentTime" parameterType="com.testor.module.accident.model.domain.AccidentReport" resultType="string">
        SELECT
            count(a.id) AS count
        FROM
            t_accident_report  a
            LEFT JOIN t_sys_org b ON a.org_id=b.org_id
        WHERE
            YEAR ( a.accident_time ) = #{accidentReport.attachment}
            AND a.process_status = '5'
            AND a.status = '0'
            AND b.status = '0'
            and (b.org_id=#{accidentReport.orgId} or b.parent_ids like concat('%', #{accidentReport.orgId}, '%'))
            and b.is_dept='0'
    </select>

    <select id="getAccidentReportByAccidentTime"
            parameterType="com.testor.module.accident.model.domain.AccidentReport"
            resultType="com.testor.module.accident.model.domain.AccidentReport" databaseId="mysql">
        SELECT
            a.*
        FROM
            t_accident_report  a
            LEFT JOIN t_sys_org b ON a.org_id=b.org_id
        WHERE
            YEAR ( a.accident_time ) = #{accidentReport.attachment}
            AND a.process_status = '5'
            AND a.status = '0'
            AND b.status = '0'
            and (b.org_id=#{accidentReport.orgId} or b.parent_ids like concat('%', #{accidentReport.orgId}, '%'))
            and b.is_dept='0'
    </select>
    <select id="getAccidentReportByAccidentTime"
            parameterType="com.testor.module.accident.model.domain.AccidentReport"
            resultType="com.testor.module.accident.model.domain.AccidentReport" databaseId="kingbase">
        SELECT
            a.*
        FROM
            t_accident_report  a
                LEFT JOIN t_sys_org b ON a.org_id=b.org_id
        WHERE
            EXTRACT(YEAR FROM a.accident_time)  = #{accidentReport.attachment}
          AND a.process_status = '5'
          AND a.status = '0'
          AND b.status = '0'
          and (b.org_id=#{accidentReport.orgId} or b.parent_ids like concat('%', #{accidentReport.orgId}, '%'))
          and b.is_dept='0'
    </select>

    <!--<select id="list" parameterType="com.testor.module.accident.model.domain.AccidentReport" resultType="com.testor.module.accident.model.vo.AccidentReportQuery">-->
    <select id="findExportlist" parameterType="com.testor.module.accident.model.req.AccidentReportExportReq" resultType="com.testor.module.accident.model.domain.AccidentReport" databaseId="mysql">
        SELECT id,report_unit,accident_type,accident_level,accident_time,fake_type,basic_unit_id,regional_company_id,
        direct_economic_loss,
        indirect_economic_loss,
        total_economic_loss,
        accident_passed,
        improvements,
        accident_cause,remarks
        FROM t_accident_report
        <where>
            <if test="qwe.processStatus !=null and qwe.processStatus!=''">
               and process_status=#{qwe.processStatus}
            </if>
            <if test="qwe.idList !=null ">
              and   id in (#{qwe.idList})
            </if>
            <if test="qwe.regionalCompanyId !=null and qwe.regionalCompanyId!=''">
               and  regional_company_id=#{qwe.regionalCompanyId}
            </if>
            <if test="qwe.basicUnitId !=null and qwe.basicUnitId!=''">
               and  basic_unit_id=#{qwe.basicUnitId}
            </if>
            <if test="qwe.accidentStartTime!=null and qwe.accidentEndTime!=null ">
                AND accident_time between #{qwe.accidentStartTime} and #{qwe.accidentEndTime}
            </if>
             <if test="qwe.orgSql !=null and qwe.orgSql!=''">
               and  ${qwe.orgSql}
            </if>
            and  status='0'
        </where>

    </select>
    <select id="findExportlist" parameterType="com.testor.module.accident.model.req.AccidentReportExportReq" resultType="com.testor.module.accident.model.domain.AccidentReport" databaseId="kingbase">
        SELECT id,report_unit,accident_type,accident_level,accident_time,fake_type,basic_unit_id,regional_company_id,
        direct_economic_loss,
        indirect_economic_loss,
        total_economic_loss,
        accident_passed,
        improvements,
        accident_cause,remarks
        FROM t_accident_report
        <where>
            <if test="qwe.processStatus !=null and qwe.processStatus!=''">
                and process_status=#{qwe.processStatus}
            </if>
            <if test="qwe.idList !=null ">
                and   id in (#{qwe.idList})
            </if>
            <if test="qwe.regionalCompanyId !=null and qwe.regionalCompanyId!=''">
                and  regional_company_id=#{qwe.regionalCompanyId}
            </if>
            <if test="qwe.basicUnitId !=null and qwe.basicUnitId!=''">
                and  basic_unit_id=#{qwe.basicUnitId}
            </if>
            <if test="qwe.accidentStartTime!=null and qwe.accidentEndTime!=null ">
                AND accident_time between #{qwe.accidentStartTime} and #{qwe.accidentEndTime}
            </if>
            <if test="qwe.orgSql !=null and qwe.orgSql!=''">
                and  ${qwe.orgSql}
            </if>
            and  status='0'
        </where>

    </select>




</mapper>