<?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.ccdi.project.mapper.CcdiProjectOverviewEmployeeResultMapper">

    <resultMap id="CcdiProjectOverviewEmployeeResultMap"
               type="com.ruoyi.ccdi.project.domain.entity.CcdiProjectOverviewEmployeeResult">
        <id property="id" column="id"/>
        <result property="projectId" column="project_id"/>
        <result property="staffIdCard" column="staff_id_card"/>
        <result property="staffCode" column="staff_code"/>
        <result property="staffName" column="staff_name"/>
        <result property="deptId" column="dept_id"/>
        <result property="deptName" column="dept_name"/>
        <result property="ruleCount" column="rule_count"/>
        <result property="modelCount" column="model_count"/>
        <result property="hitCount" column="hit_count"/>
        <result property="riskLevelCode" column="risk_level_code"/>
        <result property="riskPoint" column="risk_point"/>
        <result property="modelCodesCsv" column="model_codes_csv"/>
        <result property="modelNamesJson" column="model_names_json"/>
        <result property="hitRulesJson" column="hit_rules_json"/>
        <result property="modelHitSummaryJson" column="model_hit_summary_json"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <resultMap id="CcdiProjectOverviewEmployeeHitRowMap"
               type="com.ruoyi.ccdi.project.domain.vo.CcdiProjectOverviewEmployeeHitRowVO">
        <result property="projectId" column="project_id"/>
        <result property="staffIdCard" column="staff_id_card"/>
        <result property="staffName" column="staff_name"/>
        <result property="staffCode" column="staff_code"/>
        <result property="deptId" column="dept_id"/>
        <result property="deptName" column="dept_name"/>
        <result property="modelCode" column="model_code"/>
        <result property="modelName" column="model_name"/>
        <result property="ruleCode" column="rule_code"/>
        <result property="ruleName" column="rule_name"/>
        <result property="riskLevel" column="risk_level"/>
    </resultMap>

    <sql id="resolvedEmployeeHitRowsSql">
        select distinct
            tr.project_id,
            coalesce(direct_staff.id_card, statement_staff.id_card, family_staff.id_card) as staff_id_card,
            coalesce(direct_staff.name, statement_staff.name, family_staff.name) as staff_name,
            cast(coalesce(direct_staff.staff_id, statement_staff.staff_id, family_staff.staff_id) as char) as staff_code,
            coalesce(direct_staff.dept_id, statement_staff.dept_id, family_staff.dept_id) as dept_id,
            dept.dept_name,
            tr.model_code,
            tr.model_name,
            tr.rule_code,
            tr.rule_name,
            tr.risk_level
        from ccdi_bank_statement_tag_result tr
        left join ccdi_base_staff direct_staff
            on tr.object_type = 'STAFF_ID_CARD'
           and tr.object_key = direct_staff.id_card
        left join ccdi_bank_statement bs
            on tr.bank_statement_id = bs.bank_statement_id
        left join ccdi_base_staff statement_staff
            on (tr.object_key is null or tr.object_key = '')
           and bs.cret_no = statement_staff.id_card
        left join ccdi_staff_fmy_relation relation
            on relation.status = 1
           and (
                ((tr.object_key is null or tr.object_key = '') and bs.cret_no = relation.relation_cert_no)
                or ((tr.object_key is not null and tr.object_key != '') and tr.object_type != 'STAFF_ID_CARD'
                    and tr.object_key = relation.relation_cert_no)
           )
        left join ccdi_base_staff family_staff
            on relation.person_id = family_staff.id_card
        left join sys_dept dept
            on dept.dept_id = coalesce(direct_staff.dept_id, statement_staff.dept_id, family_staff.dept_id)
        where tr.project_id = #{projectId}
          and coalesce(direct_staff.id_card, statement_staff.id_card, family_staff.id_card) is not null
    </sql>

    <delete id="deleteByProjectId">
        delete from ccdi_project_overview_employee_result
        where project_id = #{projectId}
    </delete>

    <insert id="insertBatch" parameterType="java.util.List">
        insert into ccdi_project_overview_employee_result (
            project_id, staff_id_card, staff_code, staff_name, dept_id, dept_name,
            rule_count, model_count, hit_count, risk_level_code, risk_point,
            model_codes_csv, model_names_json, hit_rules_json, model_hit_summary_json,
            create_by, create_time, update_by, update_time, remark
        ) values
        <foreach collection="list" item="item" separator=",">
            (
                #{item.projectId}, #{item.staffIdCard}, #{item.staffCode}, #{item.staffName},
                #{item.deptId}, #{item.deptName}, #{item.ruleCount}, #{item.modelCount},
                #{item.hitCount}, #{item.riskLevelCode}, #{item.riskPoint},
                #{item.modelCodesCsv}, #{item.modelNamesJson}, #{item.hitRulesJson}, #{item.modelHitSummaryJson},
                #{item.createBy}, #{item.createTime}, #{item.updateBy}, #{item.updateTime}, #{item.remark}
            )
        </foreach>
        on duplicate key update
            staff_code = values(staff_code),
            staff_name = values(staff_name),
            dept_id = values(dept_id),
            dept_name = values(dept_name),
            rule_count = values(rule_count),
            model_count = values(model_count),
            hit_count = values(hit_count),
            risk_level_code = values(risk_level_code),
            risk_point = values(risk_point),
            model_codes_csv = values(model_codes_csv),
            model_names_json = values(model_names_json),
            hit_rules_json = values(hit_rules_json),
            model_hit_summary_json = values(model_hit_summary_json),
            update_by = values(update_by),
            update_time = values(update_time),
            remark = values(remark)
    </insert>

    <select id="selectByProjectId" resultMap="CcdiProjectOverviewEmployeeResultMap">
        select
            id,
            project_id,
            staff_id_card,
            staff_code,
            staff_name,
            dept_id,
            dept_name,
            rule_count,
            model_count,
            hit_count,
            risk_level_code,
            risk_point,
            model_codes_csv,
            model_names_json,
            hit_rules_json,
            model_hit_summary_json,
            create_by,
            create_time,
            update_by,
            update_time,
            remark
        from ccdi_project_overview_employee_result
        where project_id = #{projectId}
        order by id asc
    </select>

    <select id="selectEmployeeHitRowsByProjectId" resultMap="CcdiProjectOverviewEmployeeHitRowMap">
        <include refid="resolvedEmployeeHitRowsSql"/>
        order by staff_id_card asc, model_code asc, rule_code asc
    </select>

</mapper>
