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.QualFilingExpertEntity;
import org.qinan.safetyeval.domain.gateway.QualFilingExpertGateway;
import org.qinan.safetyeval.domain.query.PageResult;
import org.qinan.safetyeval.domain.query.QualFilingExpertQuery;
import org.qinan.safetyeval.infrastructure.dataobject.QualFilingExpertDO;
import org.qinan.safetyeval.infrastructure.mapper.QualFilingExpertMapper;
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 QualFilingExpertGatewayImpl implements QualFilingExpertGateway {

    @Resource
    private QualFilingExpertMapper qualFilingExpertMapper;

    @Override
    public QualFilingExpertEntity save(QualFilingExpertEntity entity) {
        QualFilingExpertDO dataObject = toDO(entity);
        InsertFieldDefaults.applyIgnoreOrgId(dataObject);
        qualFilingExpertMapper.insert(dataObject);
        entity.setId(dataObject.getId());
        entity.setTenantId(dataObject.getTenantId());
        return entity;
    }

    @Override
    public QualFilingExpertEntity get(Long id) {
        QualFilingExpertDO dataObject = qualFilingExpertMapper.selectById(id);
        return toEntity(dataObject);
    }

    @Override
    public QualFilingExpertEntity getByAccount(String account) {
        LambdaQueryWrapper<QualFilingExpertDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QualFilingExpertDO::getAccount, account);
        wrapper.eq(QualFilingExpertDO::getDeleteEnum, "false");
        QualFilingExpertDO dataObject = qualFilingExpertMapper.selectOne(wrapper);
        return toEntity(dataObject);
    }

    @Override
    public QualFilingExpertEntity modify(QualFilingExpertEntity entity) {
        QualFilingExpertDO dataObject = toDO(entity);
        dataObject.setId(entity.getId());
        InsertFieldDefaults.applyForUpdate(dataObject);
        qualFilingExpertMapper.updateById(dataObject);
        return get(entity.getId());
    }

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

    @Override
    public PageResult<QualFilingExpertEntity> page(QualFilingExpertQuery query) {
        LambdaQueryWrapper<QualFilingExpertDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QualFilingExpertDO::getDeleteEnum, "false");
        if (StringUtils.hasText(query.getUserName())) {
            wrapper.like(QualFilingExpertDO::getUserName, query.getUserName());
        }
        if (StringUtils.hasText(query.getAccount())) {
            wrapper.like(QualFilingExpertDO::getAccount, query.getAccount());
        }
        if (query.getGenderCode() != null) {
            wrapper.eq(QualFilingExpertDO::getGenderCode, query.getGenderCode());
        }
        if (StringUtils.hasText(query.getIdCardNo())) {
            wrapper.like(QualFilingExpertDO::getIdCardNo, query.getIdCardNo());
        }

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

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

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

    private QualFilingExpertDO toDO(QualFilingExpertEntity entity) {
        QualFilingExpertDO dataObject = new QualFilingExpertDO();
        dataObject.setUserName(entity.getUserName());
        dataObject.setAccount(entity.getAccount());
        dataObject.setGenderCode(entity.getGenderCode());
        dataObject.setGenderName(entity.getGenderName());
        dataObject.setIdCardNo(entity.getIdCardNo());
        dataObject.setCertificate(entity.getCertificate());
        dataObject.setTenantId(entity.getTenantId());
        return dataObject;
    }

    private QualFilingExpertEntity toEntity(QualFilingExpertDO dataObject) {
        if (dataObject == null) {
            return null;
        }
        QualFilingExpertEntity entity = new QualFilingExpertEntity();
        entity.setId(dataObject.getId());
        entity.setUserName(dataObject.getUserName());
        entity.setAccount(dataObject.getAccount());
        entity.setGenderCode(dataObject.getGenderCode());
        entity.setGenderName(dataObject.getGenderName());
        entity.setIdCardNo(dataObject.getIdCardNo());
        entity.setCertificate(dataObject.getCertificate());
        entity.setTenantId(dataObject.getTenantId());
        return entity;
    }
}
