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 com.ruoyi.zhdd.domain.IncidentProcess;
import com.ruoyi.zhdd.service.IIncidentProcessService;
import org.springframework.beans.factory.annotation.Autowired;
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.IncidentBo;
import com.ruoyi.zhdd.domain.vo.IncidentVo;
import com.ruoyi.zhdd.domain.Incident;
import com.ruoyi.zhdd.mapper.IncidentMapper;
import com.ruoyi.zhdd.service.IIncidentService;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 事件基础Service业务层处理
 *
 * @author xitong
 * @date 2021-09-28
 */
@Service
public class IncidentServiceImpl extends ServicePlusImpl<IncidentMapper, Incident, IncidentVo> implements IIncidentService {

    @Autowired
    private IIncidentProcessService incidentProcessService;

    @Override
    public IncidentVo queryById(Long id){
        return getVoById(id);
    }

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

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

    private LambdaQueryWrapper<Incident> buildQueryWrapper(IncidentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Incident> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getType() != null, Incident::getType, bo.getType());
        lqw.eq(bo.getLevel() != null, Incident::getLevel, bo.getLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getAddr()), Incident::getAddr, bo.getAddr());
        lqw.eq(StringUtils.isNotBlank(bo.getLocations()), Incident::getLocations, bo.getLocations());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateDept()), Incident::getCreateDept, bo.getCreateDept());
        lqw.eq(StringUtils.isNotBlank(bo.getSource()), Incident::getSource, bo.getSource());
        lqw.eq(StringUtils.isNotBlank(bo.getDes()), Incident::getDes, bo.getDes());
        lqw.eq(StringUtils.isNotBlank(bo.getPic()), Incident::getPic, bo.getPic());
        lqw.eq(StringUtils.isNotBlank(bo.getVideo()), Incident::getVideo, bo.getVideo());
        lqw.eq(StringUtils.isNotBlank(bo.getExpr1()), Incident::getExpr1, bo.getExpr1());
        lqw.eq(StringUtils.isNotBlank(bo.getExpr2()), Incident::getExpr2, bo.getExpr2());
        lqw.eq(StringUtils.isNotBlank(bo.getExprJson()), Incident::getExprJson, bo.getExprJson());
        lqw.like(StringUtils.isNotBlank(bo.getName()), Incident::getName, bo.getName());
        lqw.eq(bo.getStatus() != null, Incident::getStatus, bo.getStatus());
        lqw.eq(bo.getMadinDept() != null, Incident::getMadinDept, bo.getMadinDept());
        lqw.eq(StringUtils.isNotBlank(bo.getAssistDept()), Incident::getAssistDept, bo.getAssistDept());
        return lqw;
    }

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

    @Override
    public Boolean updateByBo(IncidentBo bo) {
        // 是否为状态变更
//        boolean flag = false;
        // 查询变更前信息
        IncidentVo beforeInfo = getVoById(bo.getId());
        /*if (beforeInfo != null) {
            flag = !(beforeInfo.getStatus().equals(bo.getStatus()));
        }*/
        Incident update = BeanUtil.toBean(bo, Incident.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

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

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