package com.maintain.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
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.ErpMaterialPickingInfoBo;
import com.maintain.business.domain.vo.ErpMaterialPickingInfoVo;
import com.maintain.business.domain.ErpMaterialPickingInfo;
import com.maintain.business.mapper.ErpMaterialPickingInfoMapper;
import com.maintain.business.service.IErpMaterialPickingInfoService;

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

/**
 * 报修单-出库单明细Service业务层处理
 *
 * @author liushuai
 * @date 2025-04-01
 */
@RequiredArgsConstructor
@Service
public class ErpMaterialPickingInfoServiceImpl implements IErpMaterialPickingInfoService {

    private final ErpMaterialPickingInfoMapper baseMapper;

    /**
     * 查询报修单-出库单明细
     */
    @Override
    public ErpMaterialPickingInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询报修单-出库单明细列表
     */
    @Override
    public TableDataInfo<ErpMaterialPickingInfoVo> queryPageList(ErpMaterialPickingInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpMaterialPickingInfo> lqw = buildQueryWrapper(bo);
        Page<ErpMaterialPickingInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询报修单-出库单明细列表
     */
    @Override
    public List<ErpMaterialPickingInfoVo> queryList(ErpMaterialPickingInfoBo bo) {
        LambdaQueryWrapper<ErpMaterialPickingInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpMaterialPickingInfo> buildQueryWrapper(ErpMaterialPickingInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpMaterialPickingInfo> lqw = Wrappers.lambdaQuery();
        return lqw;
    }

    /**
     * 新增报修单-出库单明细
     */
    @Override
    public Boolean insertByBo(ErpMaterialPickingInfoBo bo) {
        ErpMaterialPickingInfo add = BeanUtil.toBean(bo, ErpMaterialPickingInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改报修单-出库单明细
     */
    @Override
    public Boolean updateByBo(ErpMaterialPickingInfoBo bo) {
        ErpMaterialPickingInfo update = BeanUtil.toBean(bo, ErpMaterialPickingInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除报修单-出库单明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
