package org.dromara.service.impl;

import cn.hutool.core.date.DateUtil;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.domain.bo.TblBreakdownBo;
import org.dromara.domain.vo.TblBreakdownVo;
import org.dromara.domain.TblBreakdown;
import org.dromara.mapper.TblBreakdownMapper;
import org.dromara.service.ITblBreakdownService;

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

/**
 * 故障管理Service业务层处理
 *
 * @author Lion Li
 * @date 2024-08-11
 */
@RequiredArgsConstructor
@Service
public class TblBreakdownServiceImpl implements ITblBreakdownService {

    private final TblBreakdownMapper baseMapper;

    /**
     * 查询故障管理
     *
     * @param id 主键
     * @return 故障管理
     */
    @Override
    public TblBreakdownVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询故障管理列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 故障管理分页列表
     */
    @Override
    public TableDataInfo<TblBreakdownVo> queryPageList(TblBreakdownBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TblBreakdown> lqw = buildQueryWrapper(bo);
        Page<TblBreakdownVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的故障管理列表
     *
     * @param bo 查询条件
     * @return 故障管理列表
     */
    @Override
    public List<TblBreakdownVo> queryList(TblBreakdownBo bo) {
        LambdaQueryWrapper<TblBreakdown> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TblBreakdown> buildQueryWrapper(TblBreakdownBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TblBreakdown> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), TblBreakdown::getName, bo.getName());
        lqw.eq(bo.getVal() != null, TblBreakdown::getVal, bo.getVal());
        lqw.eq(bo.getSensorId() != null, TblBreakdown::getSensorId, bo.getSensorId());
        lqw.like(StringUtils.isNotBlank(bo.getSensorName()), TblBreakdown::getSensorName, bo.getSensorName());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), TblBreakdown::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getHandler()), TblBreakdown::getHandler, bo.getHandler());
        lqw.eq(StringUtils.isNotBlank(bo.getHandlerWay()), TblBreakdown::getHandlerWay, bo.getHandlerWay());
        lqw.eq(StringUtils.isNotBlank(bo.getHandlerContent()), TblBreakdown::getHandlerContent, bo.getHandlerContent());
        lqw.eq(bo.getHandlerTime() != null, TblBreakdown::getHandlerTime, bo.getHandlerTime());
        lqw.eq(bo.getFaultTime() != null, TblBreakdown::getFaultTime, bo.getFaultTime());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TblBreakdown::getStatus, bo.getStatus());
        if(bo.getParams()!=null && bo.getParams().get("startTime")!=null && bo.getParams().get("endTime")!=null){
            String start= bo.getParams().get("startTime").toString();
            String end= bo.getParams().get("endTime").toString();
            lqw.gt( TblBreakdown::getCreateTime, start);
            lqw.lt( TblBreakdown::getCreateTime, end);
        }
        return lqw;
    }

    /**
     * 新增故障管理
     *
     * @param bo 故障管理
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TblBreakdownBo bo) {
        TblBreakdown add = MapstructUtils.convert(bo, TblBreakdown.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改故障管理
     *
     * @param bo 故障管理
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TblBreakdownBo bo) {
        TblBreakdown update = MapstructUtils.convert(bo, TblBreakdown.class);
        validEntityBeforeSave(update);
        if(update.getHandler()!=null && update.getHandlerTime()==null){
            update.setHandlerTime(DateUtil.date());
            update.setStatus("1");
        }
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TblBreakdown entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除故障管理信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
