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.ErpMaterialReturnsInfoBo;
import com.maintain.business.domain.vo.ErpMaterialReturnsInfoVo;
import com.maintain.business.domain.ErpMaterialReturnsInfo;
import com.maintain.business.mapper.ErpMaterialReturnsInfoMapper;
import com.maintain.business.service.IErpMaterialReturnsInfoService;

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

/**
 * 物料退货明细Service业务层处理
 *
 * @author liushuai
 * @date 2025-04-07
 */
@RequiredArgsConstructor
@Service
public class ErpMaterialReturnsInfoServiceImpl implements IErpMaterialReturnsInfoService {

    private final ErpMaterialReturnsInfoMapper baseMapper;

    /**
     * 查询物料退货明细
     */
    @Override
    public ErpMaterialReturnsInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询物料退货明细列表
     */
    @Override
    public TableDataInfo<ErpMaterialReturnsInfoVo> queryPageList(ErpMaterialReturnsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpMaterialReturnsInfo> lqw = buildQueryWrapper(bo);
        Page<ErpMaterialReturnsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询物料退货明细列表
     */
    @Override
    public List<ErpMaterialReturnsInfoVo> queryList(ErpMaterialReturnsInfoBo bo) {
        LambdaQueryWrapper<ErpMaterialReturnsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpMaterialReturnsInfo> buildQueryWrapper(ErpMaterialReturnsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpMaterialReturnsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getReturnsId() != null, ErpMaterialReturnsInfo::getReturnsId, bo.getReturnsId());
        lqw.eq(bo.getMaterialTypeId() != null, ErpMaterialReturnsInfo::getMaterialTypeId, bo.getMaterialTypeId());
        lqw.like(StringUtils.isNotBlank(bo.getMaterialTypeName()), ErpMaterialReturnsInfo::getMaterialTypeName, bo.getMaterialTypeName());
        lqw.eq(bo.getWarehouseLocationId() != null, ErpMaterialReturnsInfo::getWarehouseLocationId, bo.getWarehouseLocationId());
        lqw.eq(StringUtils.isNotBlank(bo.getWarehouseLocationType()), ErpMaterialReturnsInfo::getWarehouseLocationType, bo.getWarehouseLocationType());
        lqw.eq(bo.getVendorId() != null, ErpMaterialReturnsInfo::getVendorId, bo.getVendorId());
        lqw.like(StringUtils.isNotBlank(bo.getVendorName()), ErpMaterialReturnsInfo::getVendorName, bo.getVendorName());
        lqw.eq(bo.getMaterialId() != null, ErpMaterialReturnsInfo::getMaterialId, bo.getMaterialId());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialCode()), ErpMaterialReturnsInfo::getMaterialCode, bo.getMaterialCode());
        lqw.like(StringUtils.isNotBlank(bo.getMaterialName()), ErpMaterialReturnsInfo::getMaterialName, bo.getMaterialName());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialSpecifications()), ErpMaterialReturnsInfo::getMaterialSpecifications, bo.getMaterialSpecifications());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialUnit()), ErpMaterialReturnsInfo::getMaterialUnit, bo.getMaterialUnit());
        lqw.eq(bo.getMaterialWarrantyPeriod() != null, ErpMaterialReturnsInfo::getMaterialWarrantyPeriod, bo.getMaterialWarrantyPeriod());
        lqw.eq(bo.getReferencePrice() != null, ErpMaterialReturnsInfo::getReferencePrice, bo.getReferencePrice());
        lqw.eq(bo.getSellingPrice() != null, ErpMaterialReturnsInfo::getSellingPrice, bo.getSellingPrice());
        lqw.eq(bo.getReturnsNumber() != null, ErpMaterialReturnsInfo::getReturnsNumber, bo.getReturnsNumber());
        lqw.eq(bo.getReturnsPrice() != null, ErpMaterialReturnsInfo::getReturnsPrice, bo.getReturnsPrice());
        lqw.eq(bo.getReturnsMoney() != null, ErpMaterialReturnsInfo::getReturnsMoney, bo.getReturnsMoney());
        return lqw;
    }

    /**
     * 新增物料退货明细
     */
    @Override
    public Boolean insertByBo(ErpMaterialReturnsInfoBo bo) {
        ErpMaterialReturnsInfo add = BeanUtil.toBean(bo, ErpMaterialReturnsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改物料退货明细
     */
    @Override
    public Boolean updateByBo(ErpMaterialReturnsInfoBo bo) {
        ErpMaterialReturnsInfo update = BeanUtil.toBean(bo, ErpMaterialReturnsInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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