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.QualFilingChangeDetailEntity;
import org.qinan.safetyeval.domain.gateway.QualFilingChangeDetailGateway;
import org.qinan.safetyeval.domain.query.QualFilingChangeDetailQuery;
import org.qinan.safetyeval.domain.query.PageResult;
import org.qinan.safetyeval.infrastructure.dataobject.QualFilingChangeDetailDO;
import org.qinan.safetyeval.infrastructure.mapper.QualFilingChangeDetailMapper;
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 QualFilingChangeDetailGatewayImpl implements QualFilingChangeDetailGateway {

    @Resource
    private QualFilingChangeDetailMapper qualFilingChangeDetailMapper;

    @Override
    public QualFilingChangeDetailEntity save(QualFilingChangeDetailEntity entity) {
        QualFilingChangeDetailDO dataObject = toDO(entity);
        InsertFieldDefaults.apply(dataObject);
        qualFilingChangeDetailMapper.insert(dataObject);
        entity.setId(dataObject.getId());
        return entity;
    }

    @Override
    public QualFilingChangeDetailEntity get(Long id) {
        QualFilingChangeDetailDO dataObject = qualFilingChangeDetailMapper.selectById(id);
        return toEntity(dataObject);
    }
@Override
    public List<QualFilingChangeDetailEntity> listByChangeId(Long changeId) {
        LambdaQueryWrapper<QualFilingChangeDetailDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QualFilingChangeDetailDO::getChangeId, changeId);
        wrapper.eq(QualFilingChangeDetailDO::getDeleteEnum, "false");
        List<QualFilingChangeDetailDO> dataObjects = qualFilingChangeDetailMapper.selectList(wrapper);
        return dataObjects.stream().map(this::toEntity).collect(Collectors.toList());
    }

    @Override
    public List<QualFilingChangeDetailEntity> listByFilingId(Long filingId) {
        List<QualFilingChangeDetailDO> dataObjects = qualFilingChangeDetailMapper.selectByFilingId(filingId);
        return dataObjects.stream().map(this::toEntity).collect(Collectors.toList());
    }

    @Override
    public int countByFilingId(Long filingId) {
        return qualFilingChangeDetailMapper.countByFilingId(filingId);
    }

    @Override
    public QualFilingChangeDetailEntity modify(QualFilingChangeDetailEntity entity) {
        QualFilingChangeDetailDO dataObject = toDO(entity);
        dataObject.setId(entity.getId());
        InsertFieldDefaults.applyForUpdate(dataObject);
        qualFilingChangeDetailMapper.updateById(dataObject);
        return get(entity.getId());
    }

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

    @Override
    public PageResult<QualFilingChangeDetailEntity> page(QualFilingChangeDetailQuery query) {
        LambdaQueryWrapper<QualFilingChangeDetailDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QualFilingChangeDetailDO::getDeleteEnum, "false");
        if (query.getChangeId() != null) {
            wrapper.eq(QualFilingChangeDetailDO::getChangeId, query.getChangeId());
        }
        if (StringUtils.hasText(query.getChangeField())) {
            wrapper.like(QualFilingChangeDetailDO::getChangeField, query.getChangeField());
        }

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

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

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

    private QualFilingChangeDetailDO toDO(QualFilingChangeDetailEntity entity) {
        QualFilingChangeDetailDO dataObject = new QualFilingChangeDetailDO();
        dataObject.setChangeId(entity.getChangeId());
        dataObject.setFormStepCode(entity.getFormStepCode());
        dataObject.setFormStepName(entity.getFormStepName());
        dataObject.setChangeField(entity.getChangeField());
        dataObject.setChangeContent(entity.getChangeContent());
        dataObject.setChangeItemCode(entity.getChangeItemCode());
        dataObject.setChangeItemName(entity.getChangeItemName());
        dataObject.setBeforeValue(entity.getBeforeValue());
        dataObject.setAfterValue(entity.getAfterValue());
        dataObject.setTenantId(entity.getTenantId());
        dataObject.setOrgId(entity.getOrgId());
        dataObject.setCreateName(entity.getCreateName());
        return dataObject;
    }

    private QualFilingChangeDetailEntity toEntity(QualFilingChangeDetailDO dataObject) {
        if (dataObject == null) {
            return null;
        }
        QualFilingChangeDetailEntity entity = new QualFilingChangeDetailEntity();
        entity.setId(dataObject.getId());
        entity.setChangeId(dataObject.getChangeId());
        entity.setFormStepCode(dataObject.getFormStepCode());
        entity.setFormStepName(dataObject.getFormStepName());
        entity.setChangeField(dataObject.getChangeField());
        entity.setChangeContent(dataObject.getChangeContent());
        entity.setChangeItemCode(dataObject.getChangeItemCode());
        entity.setChangeItemName(dataObject.getChangeItemName());
        entity.setBeforeValue(dataObject.getBeforeValue());
        entity.setAfterValue(dataObject.getAfterValue());
        entity.setTenantId(dataObject.getTenantId());
        entity.setOrgId(dataObject.getOrgId());
        entity.setCreateName(dataObject.getCreateName());
        entity.setCreateTime(dataObject.getCreateTime());
        return entity;
    }
}
