package org.dromara.rfid.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.rfid.domain.bo.RfidReadRecordBo;
import org.dromara.rfid.domain.vo.RfidReadRecordVo;
import org.dromara.rfid.domain.RfidReadRecord;
import org.dromara.rfid.mapper.RfidReadRecordMapper;
import org.dromara.rfid.service.IRfidReadRecordService;

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

/**
 * 读取记录Service业务层处理
 *
 * @author zch
 * @date 2025-11-25
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class RfidReadRecordServiceImpl implements IRfidReadRecordService {

    private final RfidReadRecordMapper baseMapper;

    /**
     * 查询读取记录
     *
     * @param id 主键
     * @return 读取记录
     */
    @Override
    public RfidReadRecordVo queryById(Long id){
        return baseMapper.selectCustomRfidReadRecordVoById(id);
    }

    /**
     * 分页查询读取记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 读取记录分页列表
     */
    @Override
    public TableDataInfo<RfidReadRecordVo> queryPageList(RfidReadRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<RfidReadRecord> lqw = buildQueryWrapper(bo);
        // 使用自定义 Mapper XML + MyBatis-Plus Wrapper 进行分页查询
        Page<RfidReadRecordVo> result = baseMapper.selectCustomRfidReadRecordVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的读取记录列表
     *
     * @param bo 查询条件
     * @return 读取记录列表
     */
    @Override
    public List<RfidReadRecordVo> queryList(RfidReadRecordBo bo) {
        LambdaQueryWrapper<RfidReadRecord> lqw = buildQueryWrapper(bo);
        // 使用自定义 Mapper XML + MyBatis-Plus Wrapper 查询列表
        return baseMapper.selectCustomRfidReadRecordVoList(lqw);
    }

    private LambdaQueryWrapper<RfidReadRecord> buildQueryWrapper(RfidReadRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<RfidReadRecord> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(RfidReadRecord::getId);
        lqw.eq(bo.getDeviceId() != null, RfidReadRecord::getDeviceId, bo.getDeviceId());
        lqw.eq(StringUtils.isNotBlank(bo.getReadStatus()), RfidReadRecord::getReadStatus, bo.getReadStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getBarcode()), RfidReadRecord::getBarcode, bo.getBarcode());
        lqw.eq(bo.getRecordTime() != null, RfidReadRecord::getRecordTime, bo.getRecordTime());
        lqw.eq(StringUtils.isNotBlank(bo.getAlarmFlag()), RfidReadRecord::getAlarmFlag, bo.getAlarmFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getAlarmLevel()), RfidReadRecord::getAlarmLevel, bo.getAlarmLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getAlarmType()), RfidReadRecord::getAlarmType, bo.getAlarmType());
        lqw.eq(StringUtils.isNotBlank(bo.getAlarmAction()), RfidReadRecord::getAlarmAction, bo.getAlarmAction());
        return lqw;
    }

    /**
     * 新增读取记录
     *
     * @param bo 读取记录
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(RfidReadRecordBo bo) {
        RfidReadRecord add = MapstructUtils.convert(bo, RfidReadRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改读取记录
     *
     * @param bo 读取记录
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(RfidReadRecordBo bo) {
        RfidReadRecord update = MapstructUtils.convert(bo, RfidReadRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 校验并批量删除读取记录信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
