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.OrgPersonnelCertEntity;
import org.qinan.safetyeval.domain.gateway.OrgPersonnelCertGateway;
import org.qinan.safetyeval.domain.query.OrgPersonnelCertQuery;
import org.qinan.safetyeval.domain.query.PageResult;
import org.qinan.safetyeval.infrastructure.dataobject.OrgPersonnelCertDO;
import org.qinan.safetyeval.infrastructure.mapper.OrgPersonnelCertMapper;
import org.qinan.safetyeval.infrastructure.support.InsertFieldDefaults;
import org.qinan.safetyeval.infrastructure.support.OrgContextResolver;
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 OrgPersonnelCertGatewayImpl implements OrgPersonnelCertGateway {

    @Resource
    private OrgPersonnelCertMapper orgPersonnelCertMapper;

    @Resource
    private OrgContextResolver orgContextResolver;

    @Override
    public OrgPersonnelCertEntity save(OrgPersonnelCertEntity entity) {
        OrgPersonnelCertDO dataObject = toDO(entity);
        InsertFieldDefaults.apply(dataObject);
        orgPersonnelCertMapper.insert(dataObject);
        entity.setId(dataObject.getId());
        return entity;
    }

    @Override
    public OrgPersonnelCertEntity get(Long id) {
        OrgPersonnelCertDO dataObject = orgPersonnelCertMapper.selectById(id);
        return toEntity(dataObject);
    }
@Override
    public List<OrgPersonnelCertEntity> listByPersonnelId(Long personnelId) {
        LambdaQueryWrapper<OrgPersonnelCertDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrgPersonnelCertDO::getPersonnelId, personnelId);
        wrapper.eq(OrgPersonnelCertDO::getDeleteEnum, "false");
        List<OrgPersonnelCertDO> dataObjects = orgPersonnelCertMapper.selectList(wrapper);
        return dataObjects.stream().map(this::toEntity).collect(Collectors.toList());
    }

    @Override
    public List<OrgPersonnelCertEntity> listByOrgId(Long orgId) {
        LambdaQueryWrapper<OrgPersonnelCertDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrgPersonnelCertDO::getOrgId, orgId);
        wrapper.eq(OrgPersonnelCertDO::getDeleteEnum, "false");
        List<OrgPersonnelCertDO> dataObjects = orgPersonnelCertMapper.selectList(wrapper);
        return dataObjects.stream().map(this::toEntity).collect(Collectors.toList());
    }

    @Override
    public OrgPersonnelCertEntity modify(OrgPersonnelCertEntity entity) {
        OrgPersonnelCertDO dataObject = toDO(entity);
        dataObject.setId(entity.getId());
        InsertFieldDefaults.applyForUpdate(dataObject);
        orgPersonnelCertMapper.updateById(dataObject);
        return get(entity.getId());
    }

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

    @Override
    public PageResult<OrgPersonnelCertEntity> page(OrgPersonnelCertQuery query) {
        LambdaQueryWrapper<OrgPersonnelCertDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrgPersonnelCertDO::getDeleteEnum, "false");
        if (query.getOrgId() != null) {
            wrapper.eq(OrgPersonnelCertDO::getOrgId, query.getOrgId());
        }
        if (query.getPersonnelId() != null) {
            wrapper.eq(OrgPersonnelCertDO::getPersonnelId, query.getPersonnelId());
        }
        if (StringUtils.hasText(query.getCertName())) {
            wrapper.like(OrgPersonnelCertDO::getCertName, query.getCertName());
        }
        if (query.getCertTypeCode() != null) {
            wrapper.eq(OrgPersonnelCertDO::getCertTypeCode, query.getCertTypeCode());
        }

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

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

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

    private OrgPersonnelCertDO toDO(OrgPersonnelCertEntity entity) {
        OrgPersonnelCertDO dataObject = new OrgPersonnelCertDO();
        dataObject.setOrgId(orgContextResolver.resolveOrgId(entity.getOrgId()));
        dataObject.setPersonnelId(entity.getPersonnelId());
        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.setTenantId(entity.getTenantId());
        return dataObject;
    }

    private OrgPersonnelCertEntity toEntity(OrgPersonnelCertDO dataObject) {
        if (dataObject == null) {
            return null;
        }
        OrgPersonnelCertEntity entity = new OrgPersonnelCertEntity();
        entity.setId(dataObject.getId());
        entity.setPersonnelId(dataObject.getPersonnelId());
        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.setTenantId(dataObject.getTenantId());
        return entity;
    }
}
