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.ErpMaterialCheckInfoBo;
import com.maintain.business.domain.vo.ErpMaterialCheckInfoVo;
import com.maintain.business.domain.ErpMaterialCheckInfo;
import com.maintain.business.mapper.ErpMaterialCheckInfoMapper;
import com.maintain.business.service.IErpMaterialCheckInfoService;

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

/**
 * 物资盘点明细Service业务层处理
 *
 * @author liushuai
 * @date 2025-04-03
 */
@RequiredArgsConstructor
@Service
public class ErpMaterialCheckInfoServiceImpl implements IErpMaterialCheckInfoService {

    private final ErpMaterialCheckInfoMapper baseMapper;

    /**
     * 查询物资盘点明细
     */
    @Override
    public ErpMaterialCheckInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询物资盘点明细列表
     */
    @Override
    public TableDataInfo<ErpMaterialCheckInfoVo> queryPageList(ErpMaterialCheckInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpMaterialCheckInfo> lqw = buildQueryWrapper(bo);
        Page<ErpMaterialCheckInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询物资盘点明细列表
     */
    @Override
    public List<ErpMaterialCheckInfoVo> queryList(ErpMaterialCheckInfoBo bo) {
        LambdaQueryWrapper<ErpMaterialCheckInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpMaterialCheckInfo> buildQueryWrapper(ErpMaterialCheckInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpMaterialCheckInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCheckId() != null, ErpMaterialCheckInfo::getCheckId, bo.getCheckId());
        lqw.eq(bo.getMaterialId() != null, ErpMaterialCheckInfo::getMaterialId, bo.getMaterialId());
        lqw.eq(bo.getMaterialTypeId() != null, ErpMaterialCheckInfo::getMaterialTypeId, bo.getMaterialTypeId());
        lqw.like(StringUtils.isNotBlank(bo.getMaterialTypeName()), ErpMaterialCheckInfo::getMaterialTypeName, bo.getMaterialTypeName());
        lqw.eq(bo.getWarehouseLocationId() != null, ErpMaterialCheckInfo::getWarehouseLocationId, bo.getWarehouseLocationId());
        lqw.eq(StringUtils.isNotBlank(bo.getWarehouseLocationType()), ErpMaterialCheckInfo::getWarehouseLocationType, bo.getWarehouseLocationType());
        lqw.eq(bo.getVendorId() != null, ErpMaterialCheckInfo::getVendorId, bo.getVendorId());
        lqw.like(StringUtils.isNotBlank(bo.getVendorName()), ErpMaterialCheckInfo::getVendorName, bo.getVendorName());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialCode()), ErpMaterialCheckInfo::getMaterialCode, bo.getMaterialCode());
        lqw.like(StringUtils.isNotBlank(bo.getMaterialName()), ErpMaterialCheckInfo::getMaterialName, bo.getMaterialName());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialSpecifications()), ErpMaterialCheckInfo::getMaterialSpecifications, bo.getMaterialSpecifications());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialUnit()), ErpMaterialCheckInfo::getMaterialUnit, bo.getMaterialUnit());
        lqw.eq(bo.getMaterialWarrantyPeriod() != null, ErpMaterialCheckInfo::getMaterialWarrantyPeriod, bo.getMaterialWarrantyPeriod());
        lqw.eq(bo.getInventory() != null, ErpMaterialCheckInfo::getInventory, bo.getInventory());
        lqw.eq(bo.getCheckNum() != null, ErpMaterialCheckInfo::getCheckNum, bo.getCheckNum());
        lqw.eq(bo.getPeriodOutNum() != null, ErpMaterialCheckInfo::getPeriodOutNum, bo.getPeriodOutNum());
        lqw.eq(bo.getResultNum() != null, ErpMaterialCheckInfo::getResultNum, bo.getResultNum());
        lqw.eq(bo.getResultState() != null, ErpMaterialCheckInfo::getResultState, bo.getResultState());
        return lqw;
    }

    /**
     * 新增物资盘点明细
     */
    @Override
    public Boolean insertByBo(ErpMaterialCheckInfoBo bo) {
        ErpMaterialCheckInfo add = BeanUtil.toBean(bo, ErpMaterialCheckInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改物资盘点明细
     */
    @Override
    public Boolean updateByBo(ErpMaterialCheckInfoBo bo) {
        ErpMaterialCheckInfo update = BeanUtil.toBean(bo, ErpMaterialCheckInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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