package com.ruoyi.bgt.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 com.ruoyi.bgt.bo.BgtProjectRecruitApplyQueryBo;
import com.ruoyi.bgt.domain.BgtProjectRecruitApply;
import com.ruoyi.bgt.domain.dto.BgtProjectRecruitApplyQueryDTO;
import com.ruoyi.bgt.domain.vo.BgtProjectRecruitApplyVO;
import com.ruoyi.bgt.mapper.BgtProjectRecruitApplyMapper;
import com.ruoyi.bgt.service.IBgtProjectRecruitApplyService;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import org.springframework.stereotype.Service;

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

/**
 * 包工头招工申请Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-18
 */
@Service
public class BgtProjectRecruitApplyServiceImpl extends ServicePlusImpl<BgtProjectRecruitApplyMapper, BgtProjectRecruitApply> implements IBgtProjectRecruitApplyService {

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

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

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

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

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

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

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

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

	@Override
	public List<BgtProjectRecruitApplyVO> appQueryList(BgtProjectRecruitApplyQueryDTO dto) {
		return baseMapper.appQueryList(dto);
	}
}
