package com.maintain.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.maintain.common.utils.StringUtils;
import com.maintain.common.core.page.TableDataInfo;
import com.maintain.common.core.domain.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 com.maintain.business.domain.bo.ErpMaintainMalfunctionBo;
import com.maintain.business.domain.vo.ErpMaintainMalfunctionVo;
import com.maintain.business.domain.ErpMaintainMalfunction;
import com.maintain.business.mapper.ErpMaintainMalfunctionMapper;
import com.maintain.business.service.IErpMaintainMalfunctionService;

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

/**
 * 维修与故障关联Service业务层处理
 *
 * @author liushuai
 * @date 2025-03-28
 */
@RequiredArgsConstructor
@Service
public class ErpMaintainMalfunctionServiceImpl implements IErpMaintainMalfunctionService {

    private final ErpMaintainMalfunctionMapper baseMapper;

    /**
     * 查询维修与故障关联
     */
    @Override
    public ErpMaintainMalfunctionVo queryById(Long malfunctionId){
        return baseMapper.selectVoById(malfunctionId);
    }

    /**
     * 查询维修与故障关联列表
     */
    @Override
    public TableDataInfo<ErpMaintainMalfunctionVo> queryPageList(ErpMaintainMalfunctionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpMaintainMalfunction> lqw = buildQueryWrapper(bo);
        Page<ErpMaintainMalfunctionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询维修与故障关联列表
     */
    @Override
    public List<ErpMaintainMalfunctionVo> queryList(ErpMaintainMalfunctionBo bo) {
        LambdaQueryWrapper<ErpMaintainMalfunction> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpMaintainMalfunction> buildQueryWrapper(ErpMaintainMalfunctionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpMaintainMalfunction> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMalfunctionId() != null, ErpMaintainMalfunction::getMalfunctionId, bo.getMalfunctionId());
        lqw.like(StringUtils.isNotBlank(bo.getMalfunctionName()), ErpMaintainMalfunction::getMalfunctionName, bo.getMalfunctionName());
        lqw.eq(bo.getMaintainId() != null, ErpMaintainMalfunction::getMaintainId, bo.getMaintainId());
        lqw.like(StringUtils.isNotBlank(bo.getMaintainName()), ErpMaintainMalfunction::getMaintainName, bo.getMaintainName());
        return lqw;
    }

    /**
     * 新增维修与故障关联
     */
    @Override
    public Boolean insertByBo(ErpMaintainMalfunctionBo bo) {
        ErpMaintainMalfunction add = BeanUtil.toBean(bo, ErpMaintainMalfunction.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setMalfunctionId(add.getMalfunctionId());
        }
        return flag;
    }

    /**
     * 修改维修与故障关联
     */
    @Override
    public Boolean updateByBo(ErpMaintainMalfunctionBo bo) {
        ErpMaintainMalfunction update = BeanUtil.toBean(bo, ErpMaintainMalfunction.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除维修与故障关联
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
