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.OrgPersonnelChangeEntity;
import org.qinan.safetyeval.domain.gateway.OrgPersonnelChangeGateway;
import org.qinan.safetyeval.domain.query.OrgPersonnelChangeQuery;
import org.qinan.safetyeval.domain.query.PageResult;
import org.qinan.safetyeval.infrastructure.dataobject.OrgPersonnelChangeDO;
import org.qinan.safetyeval.infrastructure.mapper.OrgPersonnelChangeMapper;
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 OrgPersonnelChangeGatewayImpl implements OrgPersonnelChangeGateway {

    @Resource
    private OrgPersonnelChangeMapper orgPersonnelChangeMapper;

    @Resource
    private OrgContextResolver orgContextResolver;

    @Override
    public OrgPersonnelChangeEntity save(OrgPersonnelChangeEntity entity) {
        OrgPersonnelChangeDO dataObject = toDO(entity);
        InsertFieldDefaults.apply(dataObject);
        orgPersonnelChangeMapper.insert(dataObject);
        entity.setId(dataObject.getId());
        return entity;
    }

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

    @Override
    public OrgPersonnelChangeEntity modify(OrgPersonnelChangeEntity entity) {
        OrgPersonnelChangeDO dataObject = toDO(entity);
        dataObject.setId(entity.getId());
        InsertFieldDefaults.applyForUpdate(dataObject);
        orgPersonnelChangeMapper.updateById(dataObject);
        return get(entity.getId());
    }

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

    @Override
    public PageResult<OrgPersonnelChangeEntity> page(OrgPersonnelChangeQuery query) {
        LambdaQueryWrapper<OrgPersonnelChangeDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrgPersonnelChangeDO::getDeleteEnum, "false");
        if (query.getOrgId() != null) {
            wrapper.eq(OrgPersonnelChangeDO::getOrgId, query.getOrgId());
        }
        if (query.getPersonnelId() != null) {
            wrapper.eq(OrgPersonnelChangeDO::getPersonnelId, query.getPersonnelId());
        }
        if (StringUtils.hasText(query.getChangeItem())) {
            wrapper.like(OrgPersonnelChangeDO::getChangeItem, query.getChangeItem());
        }

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

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

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

    private OrgPersonnelChangeDO toDO(OrgPersonnelChangeEntity entity) {
        OrgPersonnelChangeDO dataObject = new OrgPersonnelChangeDO();
        dataObject.setOrgId(orgContextResolver.resolveOrgId(entity.getOrgId()));
        dataObject.setPersonnelId(entity.getPersonnelId());
        dataObject.setChangeItem(entity.getChangeItem());
        dataObject.setChangeTime(entity.getChangeTime());
        dataObject.setOperatorName(entity.getOperatorName());
        dataObject.setTenantId(entity.getTenantId());
        return dataObject;
    }

    private OrgPersonnelChangeEntity toEntity(OrgPersonnelChangeDO dataObject) {
        if (dataObject == null) {
            return null;
        }
        OrgPersonnelChangeEntity entity = new OrgPersonnelChangeEntity();
        entity.setId(dataObject.getId());
        entity.setOrgId(dataObject.getOrgId());
        entity.setPersonnelId(dataObject.getPersonnelId());
        entity.setChangeItem(dataObject.getChangeItem());
        entity.setChangeTime(dataObject.getChangeTime());
        entity.setOperatorName(dataObject.getOperatorName());
        entity.setTenantId(dataObject.getTenantId());
        return entity;
    }
}
