package com.maintain.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.ErpMaterialInventoryRecordBo;
import com.maintain.business.domain.vo.ErpMaterialInventoryRecordVo;
import com.maintain.business.domain.ErpMaterialInventoryRecord;
import com.maintain.business.mapper.ErpMaterialInventoryRecordMapper;
import com.maintain.business.service.IErpMaterialInventoryRecordService;

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

/**
 * 物料库存记录Service业务层处理
 *
 * @author liushuai
 * @date 2025-07-16
 */
@RequiredArgsConstructor
@Service
public class ErpMaterialInventoryRecordServiceImpl implements IErpMaterialInventoryRecordService {

    private final ErpMaterialInventoryRecordMapper baseMapper;

    /**
     * 查询物料库存记录
     */
    @Override
    public ErpMaterialInventoryRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询物料库存记录列表
     */
    @Override
    public TableDataInfo<ErpMaterialInventoryRecordVo> queryPageList(ErpMaterialInventoryRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpMaterialInventoryRecord> lqw = buildQueryWrapper(bo);
        Page<ErpMaterialInventoryRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询物料库存记录列表(自定义查询)
     *
     * @param bo
     * @param pageQuery
     */
    @Override
    public TableDataInfo<ErpMaterialInventoryRecordVo> customQueryPageList(ErpMaterialInventoryRecordBo bo, PageQuery pageQuery) {
        Page<ErpMaterialInventoryRecordVo> queryPageList = baseMapper.customQueryPageList(pageQuery.build(), this.customBuildQueryWrapper(bo));
        return TableDataInfo.build(queryPageList);
    }

    private QueryWrapper<ErpMaterialInventoryRecord> customBuildQueryWrapper(ErpMaterialInventoryRecordBo bo) {
        QueryWrapper<ErpMaterialInventoryRecord> query = Wrappers.query();
        query.eq(null != bo.getMaterialId(),"t.material_id", bo.getMaterialId())
            .eq(null != bo.getWarehouseId(),"t.warehouse_id", bo.getWarehouseId())
            .like(StringUtils.isNotBlank(bo.getMaterialName()),"t.material_name", bo.getMaterialName())
            .like(StringUtils.isNotBlank(bo.getMaterialCode()),"t.material_code", bo.getMaterialCode())
            .like(StringUtils.isNotBlank(bo.getMaterialSpecifications()),"t.material_specifications", bo.getMaterialSpecifications())
            .like(StringUtils.isNotBlank(bo.getVendorName()),"t.vendor_name", bo.getVendorName())
            .like(StringUtils.isNotBlank(bo.getWarehouseName()), "t.warehouseName", bo.getWarehouseName());
        return query;
    }

    /**
     * 查询物料库存记录列表
     */
    @Override
    public List<ErpMaterialInventoryRecordVo> queryListByMaterialId(ErpMaterialInventoryRecordBo bo) {
        return baseMapper.queryListByWarehouseIdAndMaterialId(bo.getWarehouseId(), bo.getMaterialId(), bo.getType());
    }

    private LambdaQueryWrapper<ErpMaterialInventoryRecord> buildQueryWrapper(ErpMaterialInventoryRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpMaterialInventoryRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getInfoId() != null, ErpMaterialInventoryRecord::getInfoId, bo.getInfoId());
        lqw.eq(bo.getMaterialId() != null, ErpMaterialInventoryRecord::getMaterialId, bo.getMaterialId());
        lqw.eq(bo.getDeptId() != null, ErpMaterialInventoryRecord::getDeptId, bo.getDeptId());
        lqw.eq(bo.getWarehouseId() != null, ErpMaterialInventoryRecord::getWarehouseId, bo.getWarehouseId());
        lqw.eq(bo.getNumber() != null, ErpMaterialInventoryRecord::getNumber, bo.getNumber());
        lqw.eq(bo.getPrice() != null, ErpMaterialInventoryRecord::getPrice, bo.getPrice());
        lqw.eq(bo.getMoney() != null, ErpMaterialInventoryRecord::getMoney, bo.getMoney());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), ErpMaterialInventoryRecord::getType, bo.getType());
        return lqw;
    }

    /**
     * 新增物料库存记录
     */
    @Override
    public Boolean insertByBo(ErpMaterialInventoryRecordBo bo) {
        ErpMaterialInventoryRecord add = BeanUtil.toBean(bo, ErpMaterialInventoryRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改物料库存记录
     */
    @Override
    public Boolean updateByBo(ErpMaterialInventoryRecordBo bo) {
        ErpMaterialInventoryRecord update = BeanUtil.toBean(bo, ErpMaterialInventoryRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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