package com.maintain.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.maintain.business.domain.vo.ErpWarehouseVo;
import com.maintain.business.mapper.ErpWarehouseMapper;
import com.maintain.common.core.domain.entity.SysDept;
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 com.maintain.system.mapper.SysDeptMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.maintain.business.domain.bo.ErpWarehouseLocationBo;
import com.maintain.business.domain.vo.ErpWarehouseLocationVo;
import com.maintain.business.domain.ErpWarehouseLocation;
import com.maintain.business.mapper.ErpWarehouseLocationMapper;
import com.maintain.business.service.IErpWarehouseLocationService;

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

/**
 * 仓库货位Service业务层处理
 *
 * @author liushuai
 * @date 2025-04-03
 */
@RequiredArgsConstructor
@Service
public class ErpWarehouseLocationServiceImpl implements IErpWarehouseLocationService {

    private final ErpWarehouseLocationMapper baseMapper;
    private final SysDeptMapper deptMapper;
    private final ErpWarehouseMapper warehouseMapper;

    /**
     * 查询仓库货位
     */
    @Override
    public ErpWarehouseLocationVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询仓库货位列表
     */
    @Override
    public TableDataInfo<ErpWarehouseLocationVo> queryPageList(ErpWarehouseLocationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpWarehouseLocation> lqw = buildQueryWrapper(bo);
        Page<ErpWarehouseLocationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询仓库货位列表
     */
    @Override
    public List<ErpWarehouseLocationVo> queryList(ErpWarehouseLocationBo bo) {
        LambdaQueryWrapper<ErpWarehouseLocation> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpWarehouseLocation> buildQueryWrapper(ErpWarehouseLocationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpWarehouseLocation> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getLocationCode()), ErpWarehouseLocation::getLocationCode, bo.getLocationCode());
        lqw.like(StringUtils.isNotBlank(bo.getLocationType()), ErpWarehouseLocation::getLocationType, bo.getLocationType());
        return lqw;
    }

    /**
     * 新增仓库货位
     */
    @Override
    public Boolean insertByBo(ErpWarehouseLocationBo bo) {
        ErpWarehouseLocation add = BeanUtil.toBean(bo, ErpWarehouseLocation.class);
        validEntityBeforeSave(add);
        LambdaQueryWrapper<ErpWarehouseLocation> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ErpWarehouseLocation::getLocationCode, add.getLocationCode());
        queryWrapper.eq(ErpWarehouseLocation::getLocationType, add.getLocationType());
        if (!baseMapper.selectList(queryWrapper).isEmpty()) {
            throw new RuntimeException("仓库货位已存在，请调整后再试");
        }
        if (add.getDeptId() != null) {
            SysDept sysDept = deptMapper.selectVoById(add.getDeptId());
            add.setDeptName(sysDept.getDeptName());
        }
        if (add.getWarehouseId() != null) {
            ErpWarehouseVo warehouseVo = warehouseMapper.selectVoById(add.getWarehouseId());
            add.setWarehouseCode(warehouseVo.getCode());
            add.setWarehouseName(warehouseVo.getName());
        }
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改仓库货位
     */
    @Override
    public Boolean updateByBo(ErpWarehouseLocationBo bo) {
        ErpWarehouseLocation update = BeanUtil.toBean(bo, ErpWarehouseLocation.class);
        validEntityBeforeSave(update);
        LambdaQueryWrapper<ErpWarehouseLocation> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.ne(ErpWarehouseLocation::getId, update.getId())
            .eq(ErpWarehouseLocation::getLocationCode, update.getLocationCode())
            .eq(ErpWarehouseLocation::getLocationType, update.getLocationType());
        if (!baseMapper.selectList(queryWrapper).isEmpty()) {
            throw new RuntimeException("仓库货位已存在，请调整后再试");
        }
        return baseMapper.updateById(update) > 0;
    }

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

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