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.ErpRepairForm;
import com.maintain.business.domain.bo.ErpRepairFormBo;
import com.maintain.business.domain.vo.ErpRepairFormVo;
import com.maintain.common.core.domain.entity.SysDept;
import com.maintain.common.helper.LoginHelper;
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.ErpMaterialPutawayInfoBo;
import com.maintain.business.domain.vo.ErpMaterialPutawayInfoVo;
import com.maintain.business.domain.ErpMaterialPutawayInfo;
import com.maintain.business.mapper.ErpMaterialPutawayInfoMapper;
import com.maintain.business.service.IErpMaterialPutawayInfoService;

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

/**
 * 物料入库明细Service业务层处理
 *
 * @author liushuai
 * @date 2025-04-03
 */
@RequiredArgsConstructor
@Service
public class ErpMaterialPutawayInfoServiceImpl implements IErpMaterialPutawayInfoService {

    private final ErpMaterialPutawayInfoMapper baseMapper;

    /**
     * 查询物料入库明细
     */
    @Override
    public ErpMaterialPutawayInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询物料入库明细列表
     */
    @Override
    public TableDataInfo<ErpMaterialPutawayInfoVo> queryPageList(ErpMaterialPutawayInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpMaterialPutawayInfo> lqw = buildQueryWrapper(bo);
        Page<ErpMaterialPutawayInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询物料入库明细列表(自定义分页查询)
     *
     * @param bo
     * @param pageQuery
     */
    @Override
    public TableDataInfo<ErpMaterialPutawayInfoVo> customQueryPageList(ErpMaterialPutawayInfoBo bo, PageQuery pageQuery) {
        Page<ErpMaterialPutawayInfoVo> carVoPage = baseMapper.customQueryPageList(pageQuery.build(), this.buildCustomQueryWrapper(bo));
        return TableDataInfo.build(carVoPage);
    }

    /**
     * 查询物料入库明细列表
     */
    @Override
    public List<ErpMaterialPutawayInfoVo> customQueryList(ErpMaterialPutawayInfoBo bo) {
        return baseMapper.customQueryList(this.buildCustomQueryWrapper(bo));
    }

    private QueryWrapper<ErpMaterialPutawayInfo> buildCustomQueryWrapper(ErpMaterialPutawayInfoBo bo) {
        QueryWrapper<ErpMaterialPutawayInfo> query = Wrappers.query();
        query.like(null != bo.getPutawayCode(),"emp.putaway_code", bo.getPutawayCode())
                .like(null != bo.getVendorName(),"empi.vendor_name", bo.getVendorName())
                    .eq(null != bo.getMaterialId(),"empi.material_id", bo.getMaterialId())
                    .like(null != bo.getMaterialCode(),"empi.material_code", bo.getMaterialCode())
                        .like(null != bo.getMaterialName(),"empi.material_name", bo.getMaterialName());
        query.orderByDesc("empi.create_time");
        return query;
    }


    private LambdaQueryWrapper<ErpMaterialPutawayInfo> buildQueryWrapper(ErpMaterialPutawayInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpMaterialPutawayInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPutawayId() != null, ErpMaterialPutawayInfo::getPutawayId, bo.getPutawayId());
        lqw.eq(bo.getMaterialTypeId() != null, ErpMaterialPutawayInfo::getMaterialTypeId, bo.getMaterialTypeId());
        lqw.like(StringUtils.isNotBlank(bo.getMaterialTypeName()), ErpMaterialPutawayInfo::getMaterialTypeName, bo.getMaterialTypeName());
        lqw.eq(bo.getWarehouseLocationId() != null, ErpMaterialPutawayInfo::getWarehouseLocationId, bo.getWarehouseLocationId());
        lqw.eq(StringUtils.isNotBlank(bo.getWarehouseLocationType()), ErpMaterialPutawayInfo::getWarehouseLocationType, bo.getWarehouseLocationType());
        lqw.eq(bo.getVendorId() != null, ErpMaterialPutawayInfo::getVendorId, bo.getVendorId());
        lqw.like(StringUtils.isNotBlank(bo.getVendorName()), ErpMaterialPutawayInfo::getVendorName, bo.getVendorName());
        lqw.eq(bo.getMaterialId() != null, ErpMaterialPutawayInfo::getMaterialId, bo.getMaterialId());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialCode()), ErpMaterialPutawayInfo::getMaterialCode, bo.getMaterialCode());
        lqw.like(StringUtils.isNotBlank(bo.getMaterialName()), ErpMaterialPutawayInfo::getMaterialName, bo.getMaterialName());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialSpecifications()), ErpMaterialPutawayInfo::getMaterialSpecifications, bo.getMaterialSpecifications());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialUnit()), ErpMaterialPutawayInfo::getMaterialUnit, bo.getMaterialUnit());
        lqw.eq(bo.getMaterialWarrantyPeriod() != null, ErpMaterialPutawayInfo::getMaterialWarrantyPeriod, bo.getMaterialWarrantyPeriod());
        lqw.eq(bo.getReferencePrice() != null, ErpMaterialPutawayInfo::getReferencePrice, bo.getReferencePrice());
        lqw.eq(bo.getSellingPrice() != null, ErpMaterialPutawayInfo::getSellingPrice, bo.getSellingPrice());
        lqw.eq(bo.getPutawayNumber() != null, ErpMaterialPutawayInfo::getPutawayNumber, bo.getPutawayNumber());
        lqw.eq(bo.getPutawayPrice() != null, ErpMaterialPutawayInfo::getPutawayPrice, bo.getPutawayPrice());
        lqw.eq(bo.getPutawayMoney() != null, ErpMaterialPutawayInfo::getPutawayMoney, bo.getPutawayMoney());
        return lqw;
    }

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

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

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

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