package com.ruoyi.zhdd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
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.zhdd.domain.IncidentTask;
import com.ruoyi.zhdd.domain.IncidentTaskCommand;
import com.ruoyi.zhdd.domain.IncidentTaskPerson;
import com.ruoyi.zhdd.domain.Plan;
import com.ruoyi.zhdd.domain.bo.IncidentTaskBo;
import com.ruoyi.zhdd.domain.bo.IncidentTasksBo;
import com.ruoyi.zhdd.domain.vo.IncidentTaskVo;
import com.ruoyi.zhdd.mapper.IncidentTaskMapper;
import com.ruoyi.zhdd.service.IIncidentTaskCommandService;
import com.ruoyi.zhdd.service.IIncidentTaskPersonService;
import com.ruoyi.zhdd.service.IIncidentTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 事件处置流程Service业务层处理
 *
 * @author xitong
 * @date 2021-09-28
 */
@Service
public class IncidentTaskServiceImpl extends ServicePlusImpl<IncidentTaskMapper, IncidentTask, IncidentTaskVo> implements IIncidentTaskService {

    @Autowired
    private IIncidentTaskPersonService incidentTaskPersonService;
    @Autowired
    private IIncidentTaskCommandService incidentTaskCommandService;

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

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

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

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(IncidentTasksBo bo) {
        // 新增之前删除之前的内容
        if (StrUtil.isNotBlank(bo.getId())) {
            removeById(bo.getId());
            incidentTaskPersonService.remove(Wrappers.<IncidentTaskPerson>lambdaQuery().eq(IncidentTaskPerson::getIncidentTaskId, bo.getId()));
            incidentTaskCommandService.remove(Wrappers.<IncidentTaskCommand>lambdaQuery().eq(IncidentTaskCommand::getIncidentTaskId, bo.getId()));
        }
        IncidentTask add = BeanUtil.toBean(bo, IncidentTask.class);
        save(add);
        // 新增指令和人员
        if (bo.getIncidentTaskCommands() != null) {
            for (IncidentTaskCommand incidentTaskCommand : bo.getIncidentTaskCommands()) {
                incidentTaskCommand.setId(null);
                incidentTaskCommand.setIncidentTaskId(add.getId());
            }
        }
        if (bo.getIncidentTaskPeoples() != null) {
            for (IncidentTaskPerson incidentTaskPeople : bo.getIncidentTaskPeoples()) {
                incidentTaskPeople.setId(null);
                incidentTaskPeople.setIncidentTaskId(add.getId());
            }
        }
        incidentTaskCommandService.saveAll(bo.getIncidentTaskCommands());
        incidentTaskPersonService.saveAll(bo.getIncidentTaskPeoples());
        return true;
    }

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

    @Override
    public List<IncidentTaskVo> listTaskInfo(String id) {
        List<IncidentTaskVo> incidentTaskVo = listVo(Wrappers.<IncidentTask>lambdaQuery().eq(IncidentTask::getIncidentId, id));
        if (incidentTaskVo != null) {
            // 查询指令和人员
            for (IncidentTaskVo taskVo : incidentTaskVo) {
                taskVo.setTaskCommandVos(incidentTaskCommandService.listVo(Wrappers.<IncidentTaskCommand>lambdaQuery().eq(IncidentTaskCommand::getIncidentTaskId,taskVo.getId())));
                taskVo.setTaskPersonVos(incidentTaskPersonService.listVo(Wrappers.<IncidentTaskPerson>lambdaQuery().eq(IncidentTaskPerson::getIncidentTaskId,taskVo.getId())));
            }
        }
        return incidentTaskVo;
    }

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

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