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.ErpSettlementMaterialInfoBo;
import com.maintain.business.domain.vo.ErpSettlementMaterialInfoVo;
import com.maintain.business.domain.ErpSettlementMaterialInfo;
import com.maintain.business.mapper.ErpSettlementMaterialInfoMapper;
import com.maintain.business.service.IErpSettlementMaterialInfoService;

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

/**
 * 结算物料明细Service业务层处理
 *
 * @author liushuai
 * @date 2025-04-16
 */
@RequiredArgsConstructor
@Service
public class ErpSettlementMaterialInfoServiceImpl implements IErpSettlementMaterialInfoService {

    private final ErpSettlementMaterialInfoMapper baseMapper;

    /**
     * 查询结算物料明细
     */
    @Override
    public ErpSettlementMaterialInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询结算物料明细列表
     */
    @Override
    public TableDataInfo<ErpSettlementMaterialInfoVo> queryPageList(ErpSettlementMaterialInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpSettlementMaterialInfo> lqw = buildQueryWrapper(bo);
        Page<ErpSettlementMaterialInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询结算物料明细列表
     */
    @Override
    public List<ErpSettlementMaterialInfoVo> queryList(ErpSettlementMaterialInfoBo bo) {
        LambdaQueryWrapper<ErpSettlementMaterialInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpSettlementMaterialInfo> buildQueryWrapper(ErpSettlementMaterialInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpSettlementMaterialInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSettlementId() != null, ErpSettlementMaterialInfo::getSettlementId, bo.getSettlementId());
        lqw.eq(bo.getPickingId() != null, ErpSettlementMaterialInfo::getPickingId, bo.getPickingId());
        lqw.eq(bo.getPickingInfoId() != null, ErpSettlementMaterialInfo::getPickingInfoId, bo.getPickingInfoId());
        lqw.eq(bo.getMaterialTypeId() != null, ErpSettlementMaterialInfo::getMaterialTypeId, bo.getMaterialTypeId());
        lqw.like(StringUtils.isNotBlank(bo.getMaterialTypeName()), ErpSettlementMaterialInfo::getMaterialTypeName, bo.getMaterialTypeName());
        lqw.like(StringUtils.isNotBlank(bo.getWarehouseName()), ErpSettlementMaterialInfo::getWarehouseName, bo.getWarehouseName());
        lqw.like(StringUtils.isNotBlank(bo.getWarehouseLocationType()), ErpSettlementMaterialInfo::getWarehouseLocationType, bo.getWarehouseLocationType());
        lqw.like(StringUtils.isNotBlank(bo.getVendorName()), ErpSettlementMaterialInfo::getVendorName, bo.getVendorName());
        lqw.eq(bo.getMaterialId() != null, ErpSettlementMaterialInfo::getMaterialId, bo.getMaterialId());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialCode()), ErpSettlementMaterialInfo::getMaterialCode, bo.getMaterialCode());
        lqw.like(StringUtils.isNotBlank(bo.getMaterialName()), ErpSettlementMaterialInfo::getMaterialName, bo.getMaterialName());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialSpecifications()), ErpSettlementMaterialInfo::getMaterialSpecifications, bo.getMaterialSpecifications());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialUnit()), ErpSettlementMaterialInfo::getMaterialUnit, bo.getMaterialUnit());
        lqw.eq(bo.getMaterialWarrantyPeriod() != null, ErpSettlementMaterialInfo::getMaterialWarrantyPeriod, bo.getMaterialWarrantyPeriod());
        lqw.eq(bo.getState() != null, ErpSettlementMaterialInfo::getState, bo.getState());
        return lqw;
    }

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

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

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

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