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.OrgQualificationEntity;
import org.qinan.safetyeval.domain.gateway.OrgQualificationGateway;
import org.qinan.safetyeval.domain.query.OrgQualificationQuery;
import org.qinan.safetyeval.domain.query.PageResult;
import org.qinan.safetyeval.infrastructure.dataobject.OrgQualificationDO;
import org.qinan.safetyeval.infrastructure.mapper.OrgQualificationMapper;
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 OrgQualificationGatewayImpl implements OrgQualificationGateway {

    @Resource
    private OrgQualificationMapper orgQualificationMapper;

    @Resource
    private OrgContextResolver orgContextResolver;

    @Override
    public OrgQualificationEntity save(OrgQualificationEntity entity) {
        OrgQualificationDO dataObject = toDO(entity);
        InsertFieldDefaults.apply(dataObject);
        orgQualificationMapper.insert(dataObject);
        entity.setId(dataObject.getId());
        return entity;
    }

    @Override
    public OrgQualificationEntity get(Long id) {
        OrgQualificationDO dataObject = orgQualificationMapper.selectById(id);
        return toEntity(dataObject);
    }
@Override
    public OrgQualificationEntity getByCertNo(String certNo) {
        LambdaQueryWrapper<OrgQualificationDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrgQualificationDO::getCertNo, certNo);
        wrapper.eq(OrgQualificationDO::getDeleteEnum, "false");
        OrgQualificationDO dataObject = orgQualificationMapper.selectOne(wrapper);
        return toEntity(dataObject);
    }

    @Override
    public OrgQualificationEntity modify(OrgQualificationEntity entity) {
        OrgQualificationDO dataObject = toDO(entity);
        dataObject.setId(entity.getId());
        InsertFieldDefaults.applyForUpdate(dataObject);
        orgQualificationMapper.updateById(dataObject);
        return get(entity.getId());
    }

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

    @Override
    public PageResult<OrgQualificationEntity> page(OrgQualificationQuery query) {
        LambdaQueryWrapper<OrgQualificationDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrgQualificationDO::getDeleteEnum, "false");
        if (query.getOrgId() != null) {
            wrapper.eq(OrgQualificationDO::getOrgId, query.getOrgId());
        }
        if (StringUtils.hasText(query.getCertName())) {
            wrapper.like(OrgQualificationDO::getCertName, query.getCertName());
        }
        if (StringUtils.hasText(query.getCertNo())) {
            wrapper.eq(OrgQualificationDO::getCertNo, query.getCertNo());
        }
        if (query.getEnableFlag() != null) {
            wrapper.eq(OrgQualificationDO::getEnableFlag, query.getEnableFlag());
        }

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

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

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

    @Override
    public List<OrgQualificationEntity> classGet(Long id) {
        return orgQualificationMapper.selectList(new LambdaQueryWrapper<OrgQualificationDO>()
                        .eq(OrgQualificationDO::getOrgId, id)
                        .eq(OrgQualificationDO::getDeleteEnum, "false")).stream()
                .map(this::toEntity)
                .collect(Collectors.toList());
    }

    private OrgQualificationDO toDO(OrgQualificationEntity entity) {
        OrgQualificationDO dataObject = new OrgQualificationDO();
        dataObject.setOrgId(orgContextResolver.resolveOrgId(entity.getOrgId()));
        dataObject.setLicenseTypeCode(entity.getLicenseTypeCode());
        dataObject.setLicenseTypeName(entity.getLicenseTypeName());
        dataObject.setCertName(entity.getCertName());
        dataObject.setCertNo(entity.getCertNo());
        dataObject.setIssueDate(entity.getIssueDate());
        dataObject.setIssueOrg(entity.getIssueOrg());
        dataObject.setValidStartDate(entity.getValidStartDate());
        dataObject.setValidEndDate(entity.getValidEndDate());
        dataObject.setCertImageUrl(entity.getCertImageUrl());
        dataObject.setEnableFlag(entity.getEnableFlag());
        dataObject.setRemarks(entity.getRemark());
        dataObject.setTenantId(entity.getTenantId());
        return dataObject;
    }

    private OrgQualificationEntity toEntity(OrgQualificationDO dataObject) {
        if (dataObject == null) {
            return null;
        }
        OrgQualificationEntity entity = new OrgQualificationEntity();
        entity.setId(dataObject.getId());
        entity.setLicenseTypeCode(dataObject.getLicenseTypeCode());
        entity.setLicenseTypeName(dataObject.getLicenseTypeName());
        entity.setCertName(dataObject.getCertName());
        entity.setCertNo(dataObject.getCertNo());
        entity.setIssueDate(dataObject.getIssueDate());
        entity.setIssueOrg(dataObject.getIssueOrg());
        entity.setValidStartDate(dataObject.getValidStartDate());
        entity.setValidEndDate(dataObject.getValidEndDate());
        entity.setCertImageUrl(dataObject.getCertImageUrl());
        entity.setEnableFlag(dataObject.getEnableFlag());
        entity.setRemark(dataObject.getRemarks());
        entity.setTenantId(dataObject.getTenantId());
        return entity;
    }
}
