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.QualFilingPersonnelCertEntity;
import org.qinan.safetyeval.domain.gateway.QualFilingPersonnelCertGateway;
import org.qinan.safetyeval.domain.query.QualFilingPersonnelCertQuery;
import org.qinan.safetyeval.domain.query.PageResult;
import org.qinan.safetyeval.infrastructure.dataobject.QualFilingPersonnelCertDO;
import org.qinan.safetyeval.infrastructure.mapper.QualFilingPersonnelCertMapper;
import org.qinan.safetyeval.infrastructure.support.InsertFieldDefaults;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

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

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

    @Resource
    private QualFilingPersonnelCertMapper qualFilingPersonnelCertMapper;

    @Override
    public QualFilingPersonnelCertEntity save(QualFilingPersonnelCertEntity entity) {
        QualFilingPersonnelCertDO dataObject = toDO(entity);
        InsertFieldDefaults.apply(dataObject);
        qualFilingPersonnelCertMapper.insert(dataObject);
        entity.setId(dataObject.getId());
        return entity;
    }

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

    @Override
    public List<QualFilingPersonnelCertEntity> listByFilingPersonnelId(Long filingPersonnelId) {
        LambdaQueryWrapper<QualFilingPersonnelCertDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QualFilingPersonnelCertDO::getFilingPersonnelId, filingPersonnelId);
        wrapper.eq(QualFilingPersonnelCertDO::getDeleteEnum, "false");
        List<QualFilingPersonnelCertDO> dataObjects = qualFilingPersonnelCertMapper.selectList(wrapper);
        return dataObjects.stream().map(this::toEntity).collect(Collectors.toList());
    }

    @Override
    public QualFilingPersonnelCertEntity modify(QualFilingPersonnelCertEntity entity) {
        QualFilingPersonnelCertDO dataObject = toDO(entity);
        dataObject.setId(entity.getId());
        InsertFieldDefaults.applyForUpdate(dataObject);
        qualFilingPersonnelCertMapper.updateById(dataObject);
        return get(entity.getId());
    }

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

    @Override
    public PageResult<QualFilingPersonnelCertEntity> page(QualFilingPersonnelCertQuery query) {
        LambdaQueryWrapper<QualFilingPersonnelCertDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QualFilingPersonnelCertDO::getDeleteEnum, "false");
        if (query.getFilingId() != null) {
            wrapper.eq(QualFilingPersonnelCertDO::getFilingId, query.getFilingId());
        }
        if (query.getFilingPersonnelId() != null) {
            wrapper.eq(QualFilingPersonnelCertDO::getFilingPersonnelId, query.getFilingPersonnelId());
        }
        if (StringUtils.hasText(query.getCertName())) {
            wrapper.like(QualFilingPersonnelCertDO::getCertName, query.getCertName());
        }

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

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

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

    private QualFilingPersonnelCertDO toDO(QualFilingPersonnelCertEntity entity) {
        QualFilingPersonnelCertDO dataObject = new QualFilingPersonnelCertDO();
        dataObject.setFilingId(entity.getFilingId());
        dataObject.setFilingPersonnelId(entity.getFilingPersonnelId());
        dataObject.setCertName(entity.getCertName());
        dataObject.setCertTypeCode(entity.getCertTypeCode());
        dataObject.setCertTypeName(entity.getCertTypeName());
        dataObject.setCertCategoryCode(entity.getCertCategoryCode());
        dataObject.setCertCategoryName(entity.getCertCategoryName());
        dataObject.setOperationCategoryCode(entity.getOperationCategoryCode());
        dataObject.setOperationCategoryName(entity.getOperationCategoryName());
        dataObject.setCertNo(entity.getCertNo());
        dataObject.setIssueOrg(entity.getIssueOrg());
        dataObject.setValidStartDate(entity.getValidStartDate());
        dataObject.setValidEndDate(entity.getValidEndDate());
        dataObject.setReviewDate(entity.getReviewDate());
        dataObject.setCertAttachmentUrl(entity.getCertAttachmentUrl());
        dataObject.setSourceCertId(entity.getSourceCertId());
        dataObject.setTenantId(entity.getTenantId());
        return dataObject;
    }

    private QualFilingPersonnelCertEntity toEntity(QualFilingPersonnelCertDO dataObject) {
        if (dataObject == null) {
            return null;
        }
        QualFilingPersonnelCertEntity entity = new QualFilingPersonnelCertEntity();
        entity.setId(dataObject.getId());
        entity.setFilingId(dataObject.getFilingId());
        entity.setFilingPersonnelId(dataObject.getFilingPersonnelId());
        entity.setCertName(dataObject.getCertName());
        entity.setCertTypeCode(dataObject.getCertTypeCode());
        entity.setCertTypeName(dataObject.getCertTypeName());
        entity.setCertCategoryCode(dataObject.getCertCategoryCode());
        entity.setCertCategoryName(dataObject.getCertCategoryName());
        entity.setOperationCategoryCode(dataObject.getOperationCategoryCode());
        entity.setOperationCategoryName(dataObject.getOperationCategoryName());
        entity.setCertNo(dataObject.getCertNo());
        entity.setIssueOrg(dataObject.getIssueOrg());
        entity.setValidStartDate(dataObject.getValidStartDate());
        entity.setValidEndDate(dataObject.getValidEndDate());
        entity.setReviewDate(dataObject.getReviewDate());
        entity.setCertAttachmentUrl(dataObject.getCertAttachmentUrl());
        entity.setSourceCertId(dataObject.getSourceCertId());
        entity.setTenantId(dataObject.getTenantId());
        return entity;
    }
}
