package org.qinan.safetyeval.infrastructure.support;

import org.qinan.safetyeval.infrastructure.dataobject.OrgPersonnelDO;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 绕过 MyBatis-Plus {@code @TableLogic} 拦截，读写含逻辑删除标记的人员记录。
 */
@Repository
public class OrgPersonnelRawRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    public OrgPersonnelDO selectAnyByOrgIdAndAccount(Long orgId, String account) {
        String sql = "SELECT * FROM org_personnel WHERE org_id = ? AND account = ? LIMIT 1";
        List<OrgPersonnelDO> rows = jdbcTemplate.query(sql, this::mapRow, orgId, account);
        return rows.isEmpty() ? null : rows.get(0);
    }

    public int restoreById(OrgPersonnelDO dataObject) {
        String sql = "UPDATE org_personnel SET "
                + "org_id = ?, dept_id = ?, post_id = ?, user_name = ?, account = ?, "
                + "gender_code = ?, gender_name = ?, birth_date = ?, id_card_no = ?, "
                + "current_address = ?, office_address = ?, education_code = ?, education_name = ?, "
                + "graduate_school = ?, major = ?, employment_status_code = ?, employment_status_name = ?, "
                + "person_type_code = ?, person_type_name = ?, qual_scope = ?, "
                + "professional_level_code = ?, professional_level_name = ?, evaluator_cert_no = ?, "
                + "education_type_code = ?, education_type_name = ?, education_level_code = ?, education_level_name = ?, "
                + "title_name = ?, register_engineer_flag = ?, publications = ?, ability_declaration = ?, "
                + "work_experience = ?, proof_material_url = ?, delete_enum = 'false', "
                + "update_time = ?, update_id = ?, update_name = ? "
                + "WHERE id = ?";
        return jdbcTemplate.update(sql,
                dataObject.getOrgId(),
                dataObject.getDeptId(),
                dataObject.getPostId(),
                dataObject.getUserName(),
                dataObject.getAccount(),
                dataObject.getGenderCode(),
                dataObject.getGenderName(),
                dataObject.getBirthDate(),
                dataObject.getIdCardNo(),
                dataObject.getCurrentAddress(),
                dataObject.getOfficeAddress(),
                dataObject.getEducationCode(),
                dataObject.getEducationName(),
                dataObject.getGraduateSchool(),
                dataObject.getMajor(),
                dataObject.getEmploymentStatusCode(),
                dataObject.getEmploymentStatusName(),
                dataObject.getPersonTypeCode(),
                dataObject.getPersonTypeName(),
                dataObject.getQualScope(),
                dataObject.getProfessionalLevelCode(),
                dataObject.getProfessionalLevelName(),
                dataObject.getEvaluatorCertNo(),
                dataObject.getEducationTypeCode(),
                dataObject.getEducationTypeName(),
                dataObject.getEducationLevelCode(),
                dataObject.getEducationLevelName(),
                dataObject.getTitleName(),
                dataObject.getRegisterEngineerFlag(),
                dataObject.getPublications(),
                dataObject.getAbilityDeclaration(),
                dataObject.getWorkExperience(),
                dataObject.getProofMaterialUrl(),
                dataObject.getUpdateTime(),
                dataObject.getUpdateId(),
                dataObject.getUpdateName(),
                dataObject.getId());
    }

    private OrgPersonnelDO mapRow(ResultSet rs, int rowNum) throws SQLException {
        OrgPersonnelDO row = new OrgPersonnelDO();
        row.setId(rs.getLong("id"));
        row.setOrgId(rs.getLong("org_id"));
        long deptId = rs.getLong("dept_id");
        if (!rs.wasNull()) {
            row.setDeptId(deptId);
        }
        long postId = rs.getLong("post_id");
        if (!rs.wasNull()) {
            row.setPostId(postId);
        }
        row.setUserName(rs.getString("user_name"));
        row.setAccount(rs.getString("account"));
        int genderCode = rs.getInt("gender_code");
        if (!rs.wasNull()) {
            row.setGenderCode(genderCode);
        }
        row.setGenderName(rs.getString("gender_name"));
        java.sql.Date birthDate = rs.getDate("birth_date");
        if (birthDate != null) {
            row.setBirthDate(birthDate.toLocalDate());
        }
        row.setIdCardNo(rs.getString("id_card_no"));
        row.setCurrentAddress(rs.getString("current_address"));
        row.setOfficeAddress(rs.getString("office_address"));
        row.setEducationCode(rs.getString("education_code"));
        row.setEducationName(rs.getString("education_name"));
        row.setGraduateSchool(rs.getString("graduate_school"));
        row.setMajor(rs.getString("major"));
        int employmentStatusCode = rs.getInt("employment_status_code");
        if (!rs.wasNull()) {
            row.setEmploymentStatusCode(employmentStatusCode);
        }
        row.setEmploymentStatusName(rs.getString("employment_status_name"));
        row.setPersonTypeCode(rs.getString("person_type_code"));
        row.setPersonTypeName(rs.getString("person_type_name"));
        row.setQualScope(rs.getString("qual_scope"));
        row.setProfessionalLevelCode(rs.getString("professional_level_code"));
        row.setProfessionalLevelName(rs.getString("professional_level_name"));
        row.setEvaluatorCertNo(rs.getString("evaluator_cert_no"));
        row.setEducationTypeCode(rs.getString("education_type_code"));
        row.setEducationTypeName(rs.getString("education_type_name"));
        row.setEducationLevelCode(rs.getString("education_level_code"));
        row.setEducationLevelName(rs.getString("education_level_name"));
        row.setTitleName(rs.getString("title_name"));
        int registerEngineerFlag = rs.getInt("register_engineer_flag");
        if (!rs.wasNull()) {
            row.setRegisterEngineerFlag(registerEngineerFlag);
        }
        row.setPublications(rs.getString("publications"));
        row.setAbilityDeclaration(rs.getString("ability_declaration"));
        row.setWorkExperience(rs.getString("work_experience"));
        row.setProofMaterialUrl(rs.getString("proof_material_url"));
        row.setDeleteEnum(rs.getString("delete_enum"));
        row.setRemarks(rs.getString("remarks"));
        row.setCreateName(rs.getString("create_name"));
        row.setUpdateName(rs.getString("update_name"));
        long tenantId = rs.getLong("tenant_id");
        if (!rs.wasNull()) {
            row.setTenantId(tenantId);
        }
        int version = rs.getInt("version");
        if (!rs.wasNull()) {
            row.setVersion(version);
        }
        LocalDateTime createTime = rs.getTimestamp("create_time") != null
                ? rs.getTimestamp("create_time").toLocalDateTime() : null;
        row.setCreateTime(createTime);
        LocalDateTime updateTime = rs.getTimestamp("update_time") != null
                ? rs.getTimestamp("update_time").toLocalDateTime() : null;
        row.setUpdateTime(updateTime);
        long createId = rs.getLong("create_id");
        if (!rs.wasNull()) {
            row.setCreateId(createId);
        }
        long updateId = rs.getLong("update_id");
        if (!rs.wasNull()) {
            row.setUpdateId(updateId);
        }
        row.setEnv(rs.getString("env"));
        return row;
    }
}
