package com.ruoyi.fbs.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
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.common.core.mybatisplus.core.ServicePlusImpl;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.BaseException;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.fbs.bo.FbsProjectTaskApplyQueryBo;
import com.ruoyi.fbs.domain.FbsProjectTaskApply;
import com.ruoyi.fbs.domain.dto.TaskApplyDTO;
import com.ruoyi.fbs.mapper.FbsProjectTaskApplyMapper;
import com.ruoyi.fbs.service.IFbsProjectTaskApplyService;
import org.springframework.stereotype.Service;

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

/**
 * 分包商项目任务申请Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-17
 */
@Service
public class FbsProjectTaskApplyServiceImpl extends ServicePlusImpl<FbsProjectTaskApplyMapper, FbsProjectTaskApply> implements IFbsProjectTaskApplyService {

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

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

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

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

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

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

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(FbsProjectTaskApply entity){
		LambdaQueryWrapper<FbsProjectTaskApply> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(FbsProjectTaskApply::getTaskId, entity.getTaskId());
		wrapper.eq(FbsProjectTaskApply::getUserId, entity.getUserId());
		List<FbsProjectTaskApply> list = baseMapper.selectList(wrapper);
		if (CollUtil.isNotEmpty(list)) {
			throw new BaseException("该任务已申请");
		}
    }

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

	@Override
	public Boolean appTaskApply(TaskApplyDTO dto) {
		FbsProjectTaskApply fbsProjectTaskApply = BeanUtil.copyProperties(dto, FbsProjectTaskApply.class);
		validEntityBeforeSave(fbsProjectTaskApply);
		return save(fbsProjectTaskApply);
	}

	@Override
	public List<Long> getTaskIdsByUserId(Long userId) {
		List<FbsProjectTaskApply> list = baseMapper.selectList(Wrappers.<FbsProjectTaskApply>lambdaQuery().eq(FbsProjectTaskApply::getUserId, userId));
		return list.stream().map(FbsProjectTaskApply::getTaskId).collect(Collectors.toList());
	}

	@Override
	public Boolean cancelApply(TaskApplyDTO dto) {
		int delete = baseMapper.delete(Wrappers.<FbsProjectTaskApply>lambdaQuery().eq(FbsProjectTaskApply::getTaskId, dto.getTaskId())
			.eq(FbsProjectTaskApply::getUserId, dto.getUserId()));
		return delete>0;
	}
}
