package com.maintain.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.maintain.business.domain.ErpMaterialReturnsInfo;
import com.maintain.business.domain.bo.ErpMaterialReturnsInfoBo;
import com.maintain.business.domain.vo.ErpMaterialReturnsInfoVo;
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> customQueryPageList(ErpMaterialPickingInfoBo bo, PageQuery pageQuery) {
        Page<ErpMaterialPickingInfoVo> result = baseMapper.customQueryPageList(pageQuery.build(), this.buildCustomQueryWrapper(bo));
        return TableDataInfo.build(result);
    }

    /**
     * 查询物料退货明细列表
     */
    @Override
    public List<ErpMaterialPickingInfoVo> customQueryList(ErpMaterialPickingInfoBo bo) {
        return baseMapper.customQueryList(this.buildCustomQueryWrapper(bo));
    }

    private QueryWrapper<ErpMaterialPickingInfo> buildCustomQueryWrapper(ErpMaterialPickingInfoBo bo) {
        QueryWrapper<ErpMaterialPickingInfo> query = Wrappers.query();
        query.eq(null != bo.getMaterialId(),"empi.material_id", bo.getMaterialId())
            .in(!bo.getStateList().isEmpty(),"empi.state", bo.getStateList());
        query.orderByDesc("empi.create_time");
        return query;
    }

    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;
    }
}
