package com.jeeplus.test.jobPosion.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.test.jobPosion.domain.Position;
import com.jeeplus.test.jobPosion.mapper.PositionMapper;
import com.jeeplus.test.jobPosion.service.dto.PositionCategoryDTO;
import com.jeeplus.test.jobPosion.service.dto.PositionDTO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class PositionService extends ServiceImpl<PositionMapper, Position> {

    @Autowired
    private PositionMapper positionMapper;

    public IPage<Position> findList(Page<Position> page, QueryWrapper<Position> queryWrapper){
        queryWrapper.eq("p.del_flag","0");
        queryWrapper.orderByAsc("p.sort").orderByDesc("p.create_date");
        return positionMapper.findList(page, queryWrapper);
    }

    public void updateSortByIdList(List<PositionDTO> positionDTOList){
        List<PositionDTO> list = new ArrayList<>();
        positionDTOList.stream().forEach(item->{
            if(ObjectUtil.isNotEmpty(item)){
                if(StringUtils.isNotBlank(item.getId())&&ObjectUtil.isNotEmpty(item.getSort())){
                    list.add(item);
                }
            }
        });
        positionMapper.updateSortByIdList(list);
    }
}
