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.QualFilingCommitmentEntity;
import org.qinan.safetyeval.domain.gateway.QualFilingCommitmentGateway;
import org.qinan.safetyeval.domain.query.QualFilingCommitmentQuery;
import org.qinan.safetyeval.domain.query.PageResult;
import org.qinan.safetyeval.infrastructure.dataobject.QualFilingCommitmentDO;
import org.qinan.safetyeval.infrastructure.mapper.QualFilingCommitmentMapper;
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 QualFilingCommitmentGatewayImpl implements QualFilingCommitmentGateway {

    @Resource
    private QualFilingCommitmentMapper qualFilingCommitmentMapper;

    @Override
    public QualFilingCommitmentEntity save(QualFilingCommitmentEntity entity) {
        QualFilingCommitmentDO dataObject = toDO(entity);
        InsertFieldDefaults.apply(dataObject);
        qualFilingCommitmentMapper.insert(dataObject);
        entity.setId(dataObject.getId());
        return entity;
    }

    @Override
    public QualFilingCommitmentEntity get(Long id) {
        QualFilingCommitmentDO dataObject = qualFilingCommitmentMapper.selectById(id);
        return toEntity(dataObject);
    }
@Override
    public QualFilingCommitmentEntity getByFilingId(Long filingId) {
        LambdaQueryWrapper<QualFilingCommitmentDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QualFilingCommitmentDO::getFilingId, filingId);
        wrapper.eq(QualFilingCommitmentDO::getDeleteEnum, "false");
        QualFilingCommitmentDO dataObject = qualFilingCommitmentMapper.selectOne(wrapper);
        return toEntity(dataObject);
    }

    @Override
    public QualFilingCommitmentEntity modify(QualFilingCommitmentEntity entity) {
        QualFilingCommitmentDO dataObject = toDO(entity);
        dataObject.setId(entity.getId());
        InsertFieldDefaults.applyForUpdate(dataObject);
        qualFilingCommitmentMapper.updateById(dataObject);
        return get(entity.getId());
    }

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

    @Override
    public void physicalDeleteByFilingId(Long filingId) {
        if (filingId != null) {
            qualFilingCommitmentMapper.physicalDeleteByFilingId(filingId);
        }
    }

    @Override
    public PageResult<QualFilingCommitmentEntity> page(QualFilingCommitmentQuery query) {
        LambdaQueryWrapper<QualFilingCommitmentDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QualFilingCommitmentDO::getDeleteEnum, "false");
        if (query.getFilingId() != null) {
            wrapper.eq(QualFilingCommitmentDO::getFilingId, query.getFilingId());
        }

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

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

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

    private QualFilingCommitmentDO toDO(QualFilingCommitmentEntity entity) {
        QualFilingCommitmentDO dataObject = new QualFilingCommitmentDO();
        dataObject.setFilingId(entity.getFilingId());
        dataObject.setCommitmentContent(entity.getCommitmentContent());
        dataObject.setLegalRepSignatureUrl(entity.getLegalRepSignatureUrl());
        dataObject.setSignDate(entity.getSignDate());
        dataObject.setLegalRepPersonnelId(entity.getLegalRepPersonnelId());
        dataObject.setLegalRepName(entity.getLegalRepName());
        dataObject.setTenantId(entity.getTenantId());
        return dataObject;
    }

    private QualFilingCommitmentEntity toEntity(QualFilingCommitmentDO dataObject) {
        if (dataObject == null) {
            return null;
        }
        QualFilingCommitmentEntity entity = new QualFilingCommitmentEntity();
        entity.setId(dataObject.getId());
        entity.setFilingId(dataObject.getFilingId());
        entity.setCommitmentContent(dataObject.getCommitmentContent());
        entity.setLegalRepSignatureUrl(dataObject.getLegalRepSignatureUrl());
        entity.setSignDate(dataObject.getSignDate());
        entity.setLegalRepPersonnelId(dataObject.getLegalRepPersonnelId());
        entity.setLegalRepName(dataObject.getLegalRepName());
        entity.setTenantId(dataObject.getTenantId());
        return entity;
    }
}
