package com.ruoyi.wgz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.wgz.bo.WgzScoreRecordQueryBo;
import com.ruoyi.wgz.domain.WgzScoreRecord;
import com.ruoyi.wgz.mapper.WgzScoreRecordMapper;
import com.ruoyi.wgz.service.IWgzScoreRecordService;

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

/**
 * 务工者评分记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-21
 */
@Service
public class WgzScoreRecordServiceImpl extends ServicePlusImpl<WgzScoreRecordMapper, WgzScoreRecord> implements IWgzScoreRecordService {

    @Override
    public WgzScoreRecord queryById(Long id){
        return getById(id);
    }

    @Override
    public TableDataInfo<WgzScoreRecord> queryPageList(WgzScoreRecordQueryBo bo) {
        Page<WgzScoreRecord> result = page(PageUtils.buildPage(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<WgzScoreRecord> queryList(WgzScoreRecordQueryBo bo) {
        return list(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<WgzScoreRecord> buildQueryWrapper(WgzScoreRecordQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WgzScoreRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getRecruitId() != null, WgzScoreRecord::getRecruitId, bo.getRecruitId());
        lqw.eq(bo.getUserId() != null, WgzScoreRecord::getUserId, bo.getUserId());
        lqw.eq(bo.getScoreBy() != null, WgzScoreRecord::getScoreBy, bo.getScoreBy());
        lqw.eq(bo.getScore() != null, WgzScoreRecord::getScore, bo.getScore());
        lqw.eq(StrUtil.isNotBlank(bo.getContent()), WgzScoreRecord::getContent, bo.getContent());
        return lqw;
    }

    @Override
    public Boolean insert(WgzScoreRecord bo) {
        WgzScoreRecord add = BeanUtil.toBean(bo, WgzScoreRecord.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean update(WgzScoreRecord bo) {
        WgzScoreRecord update = BeanUtil.toBean(bo, WgzScoreRecord.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(WgzScoreRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
