package com.ruoyi.zhdd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
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.zhdd.domain.bo.PlanTaskBo;
import com.ruoyi.zhdd.domain.vo.PlanTaskVo;
import com.ruoyi.zhdd.domain.PlanTask;
import com.ruoyi.zhdd.mapper.PlanTaskMapper;
import com.ruoyi.zhdd.service.IPlanTaskService;

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

/**
 * 预案任务管理Service业务层处理
 *
 * @author xitong
 * @date 2021-09-28
 */
@Service
public class PlanTaskServiceImpl extends ServicePlusImpl<PlanTaskMapper, PlanTask, PlanTaskVo> implements IPlanTaskService {

    @Override
    public PlanTaskVo queryById(String id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<PlanTaskVo> queryPageList(PlanTaskBo bo) {
        PagePlus<PlanTask, PlanTaskVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<PlanTaskVo> queryList(PlanTaskBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<PlanTask> buildQueryWrapper(PlanTaskBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PlanTask> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPlanId() != null, PlanTask::getPlanId, bo.getPlanId());
        lqw.like(StringUtils.isNotBlank(bo.getTaskName()), PlanTask::getTaskName, bo.getTaskName());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskDes()), PlanTask::getTaskDes, bo.getTaskDes());
        return lqw;
    }

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

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

    @Override
    public Map<Integer, List<PlanTaskVo>> queryPlanTaskByPlanId(String planId) {
        PlanTaskBo planTaskBo = new PlanTaskBo();
        planTaskBo.setPlanId(planId);
        List<PlanTaskVo> list = queryList(planTaskBo);
        Map<Integer, List<PlanTaskVo>> collect = list.stream().collect(Collectors.groupingBy(PlanTaskVo::getLevel));
        return collect;
    }

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

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