package org.dromara.main.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.main.domain.MainPosition;
import org.dromara.main.domain.bo.MainPositionBo;
import org.dromara.main.domain.vo.MainPositionVo;
import org.dromara.main.mapper.MainPositionMapper;
import org.dromara.main.service.IMainPositionService;
import org.springframework.stereotype.Service;

/**
 * 岗位Service业务层处理
 */
@RequiredArgsConstructor
@Service
public class MainPositionServiceImpl extends ServiceImpl<MainPositionMapper, MainPosition> implements IMainPositionService {

    private final MainPositionMapper baseMapper;

    /**
     * 查询岗位分页列表
     */
    @Override
    public Page<MainPositionVo> queryPageList(MainPositionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MainPosition> lqw = buildQueryWrapper(bo);
        Page<MainPositionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return result;
    }

    /**
     * 查询岗位详情
     */
    @Override
    public MainPositionVo queryById(Long id) {
        MainPosition position = baseMapper.selectById(id);
        return MapstructUtils.convert(position, MainPositionVo.class);
    }

    /**
     * 新增岗位
     */
    @Override
    public Boolean insertPosition(MainPositionBo bo) {
        MainPosition position = MapstructUtils.convert(bo, MainPosition.class);
        validEntityBeforeSave(position);
        return baseMapper.insert(position) > 0;
    }

    /**
     * 修改岗位
     */
    @Override
    public Boolean updatePosition(MainPositionBo bo) {
        MainPosition position = MapstructUtils.convert(bo, MainPosition.class);
        validEntityBeforeSave(position);
        return baseMapper.updateById(position) > 0;
    }

    /**
     * 构建查询条件
     */
    private LambdaQueryWrapper<MainPosition> buildQueryWrapper(MainPositionBo bo) {
        LambdaQueryWrapper<MainPosition> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTenantId()), MainPosition::getTenantId, bo.getTenantId());
        lqw.like(StringUtils.isNotBlank(bo.getPostName()), MainPosition::getPostName, bo.getPostName());
        lqw.eq(StringUtils.isNotBlank(bo.getPostType()), MainPosition::getPostType, bo.getPostType());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkProvince()), MainPosition::getWorkProvince, bo.getWorkProvince());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkCity()), MainPosition::getWorkCity, bo.getWorkCity());
        lqw.eq(StringUtils.isNotBlank(bo.getPostLevel()), MainPosition::getPostLevel, bo.getPostLevel());
        lqw.eq(bo.getIsUrgent() != null, MainPosition::getIsUrgent, bo.getIsUrgent());
        lqw.orderByDesc(MainPosition::getCreateTime);
        return lqw;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MainPosition entity) {
        // 可以添加业务校验逻辑
    }
}
