package com.jeeplus.test.jobPosion.service;

import cn.hutool.extra.servlet.ServletUtil;
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.test.jobPosion.domain.JobPosition;
import com.jeeplus.test.jobPosion.domain.PositionLog;
import com.jeeplus.test.jobPosion.mapper.PositionLogMapper;
import com.jeeplus.test.jobPosion.service.dto.PositionLogDTO;
import com.jeeplus.test.jobPosion.service.mapstruct.PositionLogWrapper;
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 javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class PositionLogService extends ServiceImpl<PositionLogMapper, PositionLog> {

    @Autowired
    private PositionLogMapper positionLogMapper;

    public void saveLog(PositionLogDTO positionLogDTO, HttpServletRequest request){
        PositionLog positionLog = PositionLogWrapper.INSTANCE.toEntity(positionLogDTO);
        //ip地址
        positionLog.setRemoteAddr(ServletUtil.getClientIP ( request ));

        positionLogMapper.insert(positionLog);
    }

    public void saveLogBatch(List<PositionLogDTO> positionLogDTOList, HttpServletRequest request){
        List<PositionLog> positionLogList = new ArrayList<>();
        positionLogDTOList.stream().forEach(item->{
            PositionLog positionLog = PositionLogWrapper.INSTANCE.toEntity(item);
            //ip地址
            positionLog.setRemoteAddr(ServletUtil.getClientIP ( request ));
            positionLogList.add(positionLog);
        });
        this.saveBatch(positionLogList);
    }

    public IPage<PositionLog> findList(Page<PositionLog> page, QueryWrapper<PositionLog> queryWrapper){
        queryWrapper.eq("pl.del_flag","0");
        return positionLogMapper.findList(page,queryWrapper);
    }

}
