package org.qinan.safetyeval.infrastructure.gatewayimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.qinan.safetyeval.domain.entity.QualFilingPersonnelEntity;
import org.qinan.safetyeval.domain.gateway.QualFilingPersonnelGateway;
import org.qinan.safetyeval.domain.query.QualFilingPersonnelQuery;
import org.qinan.safetyeval.domain.query.PageResult;
import org.qinan.safetyeval.infrastructure.dataobject.QualFilingPersonnelDO;
import org.qinan.safetyeval.infrastructure.mapper.QualFilingPersonnelMapper;
import org.qinan.safetyeval.infrastructure.support.InsertFieldDefaults;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 备案申请人员Gateway实现（Infrastructure层）
 *
 * @author safety-eval
 */
@Component
public class QualFilingPersonnelGatewayImpl implements QualFilingPersonnelGateway {

    @Resource
    private QualFilingPersonnelMapper qualFilingPersonnelMapper;

    @Override
    public QualFilingPersonnelEntity save(QualFilingPersonnelEntity entity) {
        QualFilingPersonnelDO dataObject = toDO(entity);
        InsertFieldDefaults.apply(dataObject);
        qualFilingPersonnelMapper.insert(dataObject);
        entity.setId(dataObject.getId());
        return entity;
    }

    @Override
    public QualFilingPersonnelEntity get(Long id) {
        QualFilingPersonnelDO dataObject = qualFilingPersonnelMapper.selectById(id);
        return toEntity(dataObject);
    }
@Override
    public List<QualFilingPersonnelEntity> listByFilingId(Long filingId) {
        LambdaQueryWrapper<QualFilingPersonnelDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QualFilingPersonnelDO::getFilingId, filingId);
        wrapper.eq(QualFilingPersonnelDO::getDeleteEnum, "false");
        List<QualFilingPersonnelDO> dataObjects = qualFilingPersonnelMapper.selectList(wrapper);
        return dataObjects.stream().map(this::toEntity).collect(Collectors.toList());
    }

    @Override
    public QualFilingPersonnelEntity modify(QualFilingPersonnelEntity entity) {
        QualFilingPersonnelDO dataObject = toDO(entity);
        dataObject.setId(entity.getId());
        InsertFieldDefaults.applyForUpdate(dataObject);
        qualFilingPersonnelMapper.updateById(dataObject);
        return get(entity.getId());
    }

    @Override
    public void delete(Long id) {
        qualFilingPersonnelMapper.deleteById(id);
    }

    @Override
    public PageResult<QualFilingPersonnelEntity> page(QualFilingPersonnelQuery query) {
        LambdaQueryWrapper<QualFilingPersonnelDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QualFilingPersonnelDO::getDeleteEnum, "false");
        if (query.getFilingId() != null) {
            wrapper.eq(QualFilingPersonnelDO::getFilingId, query.getFilingId());
        }
        if (StringUtils.hasText(query.getPersonName())) {
            wrapper.like(QualFilingPersonnelDO::getPersonName, query.getPersonName());
        }

        Page<QualFilingPersonnelDO> page = new Page<>(query.getPageNum(), query.getPageSize());
        IPage<QualFilingPersonnelDO> result = qualFilingPersonnelMapper.selectPage(page, wrapper);

        List<QualFilingPersonnelEntity> entities = result.getRecords().stream()
                .map(this::toEntity)
                .collect(Collectors.toList());

        return PageResult.of(entities, result.getTotal(), result.getCurrent(), result.getSize());
    }

    @Override
    public int countByFilingId(Long filingId) {
        LambdaQueryWrapper<QualFilingPersonnelDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QualFilingPersonnelDO::getFilingId, filingId);
        wrapper.eq(QualFilingPersonnelDO::getDeleteEnum, "false");
        return qualFilingPersonnelMapper.selectCount(wrapper).intValue();
    }

    @Override
    public int countByFilingIdAndRegisterEngineerFlag(Long filingId, Integer registerEngineerFlag) {
        LambdaQueryWrapper<QualFilingPersonnelDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QualFilingPersonnelDO::getFilingId, filingId);
        wrapper.eq(QualFilingPersonnelDO::getDeleteEnum, "false");
        wrapper.eq(QualFilingPersonnelDO::getRegisterEngineerFlag, registerEngineerFlag);
        return qualFilingPersonnelMapper.selectCount(wrapper).intValue();
    }

    @Override
    public int countByFilingIdAndJoinWorkDateAfter(Long filingId, LocalDate joinWorkDate) {
        LambdaQueryWrapper<QualFilingPersonnelDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QualFilingPersonnelDO::getFilingId, filingId);
        wrapper.eq(QualFilingPersonnelDO::getDeleteEnum, "false");
        wrapper.gt(QualFilingPersonnelDO::getJoinWorkDate, joinWorkDate);
        return qualFilingPersonnelMapper.selectCount(wrapper).intValue();
    }

    private QualFilingPersonnelDO toDO(QualFilingPersonnelEntity entity) {
        QualFilingPersonnelDO dataObject = new QualFilingPersonnelDO();
        dataObject.setFilingId(entity.getFilingId());
        dataObject.setSourcePersonnelId(entity.getSourcePersonnelId());
        dataObject.setPersonName(entity.getPersonName());
        dataObject.setPersonTypeCode(entity.getPersonTypeCode());
        dataObject.setPersonTypeName(entity.getPersonTypeName());
        dataObject.setPositionName(entity.getPositionName());
        dataObject.setTitleName(entity.getTitleName());
        dataObject.setGenderCode(entity.getGenderCode());
        dataObject.setGenderName(entity.getGenderName());
        dataObject.setBirthDate(entity.getBirthDate());
        dataObject.setJoinWorkDate(entity.getJoinWorkDate());
        dataObject.setIdCardNo(entity.getIdCardNo());
        dataObject.setCurrentAddress(entity.getCurrentAddress());
        dataObject.setOfficeAddress(entity.getOfficeAddress());
        dataObject.setEducationCode(entity.getEducationCode());
        dataObject.setEducationName(entity.getEducationName());
        dataObject.setGraduateSchool(entity.getGraduateSchool());
        dataObject.setMajor(entity.getMajor());
        dataObject.setQualScope(entity.getQualScope());
        dataObject.setPublications(entity.getPublications());
        dataObject.setProfessionalLevelCert(entity.getProfessionalLevelCert());
        dataObject.setRegisterEngineerFlag(entity.getRegisterEngineerFlag());
        dataObject.setAbilityDeclaration(entity.getAbilityDeclaration());
        dataObject.setWorkExperience(entity.getWorkExperience());
        dataObject.setProofMaterialUrl(entity.getProofMaterialUrl());
        dataObject.setTenantId(entity.getTenantId());
        return dataObject;
    }

    private QualFilingPersonnelEntity toEntity(QualFilingPersonnelDO dataObject) {
        if (dataObject == null) {
            return null;
        }
        QualFilingPersonnelEntity entity = new QualFilingPersonnelEntity();
        entity.setId(dataObject.getId());
        entity.setFilingId(dataObject.getFilingId());
        entity.setSourcePersonnelId(dataObject.getSourcePersonnelId());
        entity.setPersonName(dataObject.getPersonName());
        entity.setPersonTypeCode(dataObject.getPersonTypeCode());
        entity.setPersonTypeName(dataObject.getPersonTypeName());
        entity.setPositionName(dataObject.getPositionName());
        entity.setTitleName(dataObject.getTitleName());
        entity.setGenderCode(dataObject.getGenderCode());
        entity.setGenderName(dataObject.getGenderName());
        entity.setBirthDate(dataObject.getBirthDate());
        entity.setJoinWorkDate(dataObject.getJoinWorkDate());
        entity.setIdCardNo(dataObject.getIdCardNo());
        entity.setCurrentAddress(dataObject.getCurrentAddress());
        entity.setOfficeAddress(dataObject.getOfficeAddress());
        entity.setEducationCode(dataObject.getEducationCode());
        entity.setEducationName(dataObject.getEducationName());
        entity.setGraduateSchool(dataObject.getGraduateSchool());
        entity.setMajor(dataObject.getMajor());
        entity.setQualScope(dataObject.getQualScope());
        entity.setPublications(dataObject.getPublications());
        entity.setProfessionalLevelCert(dataObject.getProfessionalLevelCert());
        entity.setRegisterEngineerFlag(dataObject.getRegisterEngineerFlag());
        entity.setAbilityDeclaration(dataObject.getAbilityDeclaration());
        entity.setWorkExperience(dataObject.getWorkExperience());
        entity.setProofMaterialUrl(dataObject.getProofMaterialUrl());
        entity.setTenantId(dataObject.getTenantId());
        return entity;
    }
}
