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.IncidentProcessBo;
import com.ruoyi.zhdd.domain.vo.IncidentProcessVo;
import com.ruoyi.zhdd.domain.IncidentProcess;
import com.ruoyi.zhdd.mapper.IncidentProcessMapper;
import com.ruoyi.zhdd.service.IIncidentProcessService;

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

/**
 * 事件处置流程Service业务层处理
 *
 * @author xitong
 * @date 2021-09-28
 */
@Service
public class IncidentProcessServiceImpl extends ServicePlusImpl<IncidentProcessMapper, IncidentProcess, IncidentProcessVo> implements IIncidentProcessService {

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

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

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

    private LambdaQueryWrapper<IncidentProcess> buildQueryWrapper(IncidentProcessBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IncidentProcess> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getIncidentId() != null, IncidentProcess::getIncidentId, bo.getIncidentId());
        lqw.eq(StringUtils.isNotBlank(bo.getDes()), IncidentProcess::getDes, bo.getDes());
        return lqw;
    }

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

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

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

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