package com.ruoyi.zhdd.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.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.zhdd.domain.IncidentTaskCommand;
import com.ruoyi.zhdd.domain.IncidentTaskPerson;
import com.ruoyi.zhdd.domain.bo.IncidentTaskCommandBo;
import com.ruoyi.zhdd.domain.bo.IncidentTaskPersonBo;
import com.ruoyi.zhdd.domain.vo.IncidentTaskCommandVo;
import com.ruoyi.zhdd.domain.vo.IncidentTaskPersonVo;
import com.ruoyi.zhdd.mapper.IncidentTaskCommandMapper;
import com.ruoyi.zhdd.mapper.IncidentTaskPersonMapper;
import com.ruoyi.zhdd.service.IIncidentTaskCommandService;
import com.ruoyi.zhdd.service.IIncidentTaskPersonService;
import org.springframework.stereotype.Service;

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

/**
 * 事件处置方案人员Service业务层处理
 *
 * @author xintong
 * @date 2021-11-27
 */
@Service
public class IncidentTaskCommandServiceImpl extends ServicePlusImpl<IncidentTaskCommandMapper, IncidentTaskCommand, IncidentTaskCommandVo> implements IIncidentTaskCommandService {

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

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

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

    private LambdaQueryWrapper<IncidentTaskCommand> buildQueryWrapper(IncidentTaskCommandBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IncidentTaskCommand> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getIncidentTaskId()), IncidentTaskCommand::getIncidentTaskId, bo.getIncidentTaskId());
        lqw.eq(StringUtils.isNotBlank(bo.getCommand()), IncidentTaskCommand::getCommand, bo.getCommand());
        return lqw;
    }

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

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

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

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