package net.ferrum.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import net.ferrum.business.domain.QsOperatorRecord;
import net.ferrum.business.domain.bo.QsOperatorRecordBo;
import net.ferrum.business.domain.vo.QsOperatorRecordVo;
import net.ferrum.business.mapper.QsOperatorRecordMapper;
import net.ferrum.business.service.IQsOperatorRecordService;
import net.ferrum.common.core.domain.PageQuery;
import net.ferrum.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 操作员操作记录Service业务层处理
 *
 * @author 26554
 * @date 2025-04-21
 */
@RequiredArgsConstructor
@Service
public class QsOperatorRecordServiceImpl implements IQsOperatorRecordService {

    private final QsOperatorRecordMapper baseMapper;

    /**
     * 查询操作员操作记录
     */
    @Override
    public QsOperatorRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询操作员操作记录列表
     */
    @Override
    public TableDataInfo<QsOperatorRecordVo> queryPageList(QsOperatorRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<QsOperatorRecord> lqw = buildQueryWrapper(bo);
        Page<QsOperatorRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询操作员操作记录列表
     */
    @Override
    public List<QsOperatorRecordVo> queryList(QsOperatorRecordBo bo) {
        LambdaQueryWrapper<QsOperatorRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<QsOperatorRecord> buildQueryWrapper(QsOperatorRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<QsOperatorRecord> lqw = Wrappers.lambdaQuery();
        lqw.like(bo.getOperatorId() != null, QsOperatorRecord::getOperatorId, bo.getOperatorId());
        lqw.eq(bo.getOperation() != null, QsOperatorRecord::getOperation, bo.getOperation());
        lqw.like(bo.getProjectId() != null, QsOperatorRecord::getProjectId, bo.getProjectId());
        return lqw;
    }

    /**
     * 新增操作员操作记录
     */
    @Override
    public Boolean insertByBo(QsOperatorRecordBo bo) {
        QsOperatorRecord add = BeanUtil.toBean(bo, QsOperatorRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改操作员操作记录
     */
    @Override
    public Boolean updateByBo(QsOperatorRecordBo bo) {
        QsOperatorRecord update = BeanUtil.toBean(bo, QsOperatorRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(QsOperatorRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除操作员操作记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
