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.QualFilingReviewEntity;
import org.qinan.safetyeval.domain.gateway.QualFilingReviewGateway;
import org.qinan.safetyeval.domain.query.PageResult;
import org.qinan.safetyeval.domain.query.QualFilingReviewQuery;
import org.qinan.safetyeval.infrastructure.dataobject.QualFilingReviewDO;
import org.qinan.safetyeval.infrastructure.mapper.QualFilingReviewMapper;
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 QualFilingReviewGatewayImpl implements QualFilingReviewGateway {

    @Resource
    private QualFilingReviewMapper qualFilingReviewMapper;

    @Override
    public QualFilingReviewEntity save(QualFilingReviewEntity entity) {
        QualFilingReviewDO dataObject = toDO(entity);
        InsertFieldDefaults.applyIgnoreOrgId(dataObject);
        qualFilingReviewMapper.insert(dataObject);
        entity.setId(dataObject.getId());
        entity.setTenantId(dataObject.getTenantId());
        return entity;
    }

    @Override
    public QualFilingReviewEntity get(Long id) {
        QualFilingReviewDO dataObject = qualFilingReviewMapper.selectById(id);
        return toEntity(dataObject);
    }

    @Override
    public QualFilingReviewEntity modify(QualFilingReviewEntity entity) {
        QualFilingReviewDO dataObject = toDO(entity);
        dataObject.setId(entity.getId());
        InsertFieldDefaults.applyForUpdate(dataObject);
        qualFilingReviewMapper.updateById(dataObject);
        return get(entity.getId());
    }

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

    @Override
    public PageResult<QualFilingReviewEntity> page(QualFilingReviewQuery query) {
        LambdaQueryWrapper<QualFilingReviewDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QualFilingReviewDO::getDeleteEnum, "false");
        if (query.getFilingId() != null) {
            wrapper.eq(QualFilingReviewDO::getFilingId, query.getFilingId());
        }
        if (StringUtils.hasText(query.getReviewResult())) {
            wrapper.eq(QualFilingReviewDO::getReviewResult, query.getReviewResult());
        }
        if (query.getFilingExpertId() != null) {
            wrapper.eq(QualFilingReviewDO::getFilingExpertId, query.getFilingExpertId());
        }

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

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

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

    private QualFilingReviewDO toDO(QualFilingReviewEntity entity) {
        QualFilingReviewDO dataObject = new QualFilingReviewDO();
        dataObject.setFilingId(entity.getFilingId());
        dataObject.setReviewResult(entity.getReviewResult());
        dataObject.setReviewResultRemark(entity.getReviewResultRemark());
        dataObject.setFilingExpertId(entity.getFilingExpertId());
        dataObject.setFilingExpertName(entity.getFilingExpertName());
        dataObject.setReviewOpinion(entity.getReviewOpinion());
        dataObject.setTenantId(entity.getTenantId());
        return dataObject;
    }

    private QualFilingReviewEntity toEntity(QualFilingReviewDO dataObject) {
        if (dataObject == null) {
            return null;
        }
        QualFilingReviewEntity entity = new QualFilingReviewEntity();
        entity.setId(dataObject.getId());
        entity.setFilingId(dataObject.getFilingId());
        entity.setReviewResult(dataObject.getReviewResult());
        entity.setReviewResultRemark(dataObject.getReviewResultRemark());
        entity.setFilingExpertId(dataObject.getFilingExpertId());
        entity.setFilingExpertName(dataObject.getFilingExpertName());
        entity.setReviewOpinion(dataObject.getReviewOpinion());
        entity.setTenantId(dataObject.getTenantId());
        return entity;
    }
}
