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.OrgPositionEntity;
import org.qinan.safetyeval.domain.gateway.OrgPositionGateway;
import org.qinan.safetyeval.domain.query.OrgPositionQuery;
import org.qinan.safetyeval.domain.query.PageResult;
import org.qinan.safetyeval.infrastructure.dataobject.OrgPositionDO;
import org.qinan.safetyeval.infrastructure.mapper.OrgPositionMapper;
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 OrgPositionGatewayImpl implements OrgPositionGateway {

    @Resource
    private OrgPositionMapper orgPositionMapper;

    @Resource
    private OrgContextResolver orgContextResolver;

    @Override
    public OrgPositionEntity save(OrgPositionEntity entity) {
        OrgPositionDO dataObject = toDO(entity);
        InsertFieldDefaults.apply(dataObject);
        orgPositionMapper.insert(dataObject);
        entity.setId(dataObject.getId());
        return entity;
    }

    @Override
    public OrgPositionEntity get(Long id) {
        OrgPositionDO dataObject = orgPositionMapper.selectById(id);
        return toEntity(dataObject);
    }

    @Override
    public OrgPositionEntity modify(OrgPositionEntity entity) {
        OrgPositionDO dataObject = toDO(entity);
        dataObject.setId(entity.getId());
        InsertFieldDefaults.applyForUpdate(dataObject);
        orgPositionMapper.updateById(dataObject);
        return get(entity.getId());
    }

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

    @Override
    public PageResult<OrgPositionEntity> page(OrgPositionQuery query) {
        LambdaQueryWrapper<OrgPositionDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrgPositionDO::getDeleteEnum, "false");
        if (query.getOrgId() != null) {
            wrapper.eq(OrgPositionDO::getOrgId, query.getOrgId());
        }
        if (query.getDeptId() != null) {
            wrapper.eq(OrgPositionDO::getDeptId, query.getDeptId());
        }
        if (StringUtils.hasText(query.getPositionName())) {
            wrapper.like(OrgPositionDO::getPositionName, query.getPositionName());
        }

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

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

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

    private OrgPositionDO toDO(OrgPositionEntity entity) {
        OrgPositionDO dataObject = new OrgPositionDO();
        dataObject.setOrgId(orgContextResolver.resolveOrgId(entity.getOrgId()));
        dataObject.setDeptId(entity.getDeptId());
        dataObject.setPositionName(entity.getPositionName());
        dataObject.setDutyDesc(entity.getDutyDesc());
        dataObject.setRemarks(entity.getRemark());
        dataObject.setTenantId(entity.getTenantId());
        return dataObject;
    }

    private OrgPositionEntity toEntity(OrgPositionDO dataObject) {
        if (dataObject == null) {
            return null;
        }
        OrgPositionEntity entity = new OrgPositionEntity();
        entity.setId(dataObject.getId());
        entity.setDeptId(dataObject.getDeptId());
        entity.setPositionName(dataObject.getPositionName());
        entity.setDutyDesc(dataObject.getDutyDesc());
        entity.setRemark(dataObject.getRemarks());
        entity.setTenantId(dataObject.getTenantId());
        return entity;
    }
}
