package org.dromara.inspection.service.impl;

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 lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.inspection.domain.bo.OpsInspectionMatterBo;
import org.dromara.inspection.domain.vo.OpsInspectionMatterVo;
import org.dromara.inspection.domain.OpsInspectionMatter;
import org.dromara.inspection.mapper.OpsInspectionMatterMapper;
import org.dromara.inspection.service.IOpsInspectionMatterService;

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

/**
 * 运维-巡检-待办Service业务层处理
 *
 * @author LionLi
 * @date 2025-09-19
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class OpsInspectionMatterServiceImpl implements IOpsInspectionMatterService {

    private final OpsInspectionMatterMapper baseMapper;

    /**
     * 查询运维-巡检-待办
     *
     * @param id 主键
     * @return 运维-巡检-待办
     */
    @Override
    public OpsInspectionMatterVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询运维-巡检-待办列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 运维-巡检-待办分页列表
     */
    @Override
    public TableDataInfo<OpsInspectionMatterVo> queryPageList(OpsInspectionMatterBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OpsInspectionMatter> lqw = buildQueryWrapper(bo);
        Page<OpsInspectionMatterVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的运维-巡检-待办列表
     *
     * @param bo 查询条件
     * @return 运维-巡检-待办列表
     */
    @Override
    public List<OpsInspectionMatterVo> queryList(OpsInspectionMatterBo bo) {
        LambdaQueryWrapper<OpsInspectionMatter> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OpsInspectionMatter> buildQueryWrapper(OpsInspectionMatterBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OpsInspectionMatter> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(OpsInspectionMatter::getId);
        lqw.eq(bo.getProjectId() != null, OpsInspectionMatter::getProjectId, bo.getProjectId());
        lqw.like(StringUtils.isNotBlank(bo.getTaskName()), OpsInspectionMatter::getTaskName, bo.getTaskName());
        lqw.eq(StringUtils.isNotBlank(bo.getDescribeValue()), OpsInspectionMatter::getDescribeValue, bo.getDescribeValue());
        lqw.eq(bo.getTaskBeginTime() != null, OpsInspectionMatter::getTaskBeginTime, bo.getTaskBeginTime());
        lqw.eq(bo.getTaskEndTime() != null, OpsInspectionMatter::getTaskEndTime, bo.getTaskEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskTimeInfo()), OpsInspectionMatter::getTaskTimeInfo, bo.getTaskTimeInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskLevel()), OpsInspectionMatter::getTaskLevel, bo.getTaskLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskType()), OpsInspectionMatter::getTaskType, bo.getTaskType());
        return lqw;
    }

    /**
     * 新增运维-巡检-待办
     *
     * @param bo 运维-巡检-待办
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(OpsInspectionMatterBo bo) {
        OpsInspectionMatter add = MapstructUtils.convert(bo, OpsInspectionMatter.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改运维-巡检-待办
     *
     * @param bo 运维-巡检-待办
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(OpsInspectionMatterBo bo) {
        OpsInspectionMatter update = MapstructUtils.convert(bo, OpsInspectionMatter.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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