package com.ruoyi.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.biz.domain.vo.ArUserVo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.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 org.springframework.stereotype.Service;
import com.ruoyi.biz.domain.bo.ArUserRecordBo;
import com.ruoyi.biz.domain.vo.ArUserRecordVo;
import com.ruoyi.biz.domain.ArUserRecord;
import com.ruoyi.biz.mapper.ArUserRecordMapper;
import com.ruoyi.biz.service.IArUserRecordService;

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

/**
 * 用户使用记录Service业务层处理
 *
 * @author gwd
 * @date 2023-11-10
 */
@RequiredArgsConstructor
@Service
public class ArUserRecordServiceImpl implements IArUserRecordService {

    private final ArUserRecordMapper baseMapper;

    /**
     * 查询用户使用记录
     */
    @Override
    public ArUserRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户使用记录列表
     */
    @Override
    public TableDataInfo<ArUserVo> queryPageList(Long classificationId, PageQuery pageQuery) {
        QueryWrapper lqw = new QueryWrapper<>();
        lqw.eq("r.classification_id",classificationId);
        lqw.eq("u.del_flag",0);
        Page page = pageQuery.build();
        List<ArUserVo> result = baseMapper.selectUserVo(page, lqw);
        return TableDataInfo.build(page.setRecords(result));
    }

    /**
     * 查询用户使用记录列表
     */
    @Override
    public List<ArUserRecordVo> queryList(ArUserRecordBo bo) {
        LambdaQueryWrapper<ArUserRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ArUserRecord> buildQueryWrapper(ArUserRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ArUserRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, ArUserRecord::getUserId, bo.getUserId());
        lqw.eq(bo.getClassificationId() != null, ArUserRecord::getClassificationId, bo.getClassificationId());
        return lqw;
    }

    /**
     * 新增用户使用记录
     */
    @Override
    public Boolean insertByBo(ArUserRecordBo bo) {
        ArUserRecord add = BeanUtil.toBean(bo, ArUserRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户使用记录
     */
    @Override
    public Boolean updateByBo(ArUserRecordBo bo) {
        ArUserRecord update = BeanUtil.toBean(bo, ArUserRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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