package com.maintain.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
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 com.maintain.system.mapper.SysDeptMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.maintain.business.domain.bo.ErpWarehouseBo;
import com.maintain.business.domain.vo.ErpWarehouseVo;
import com.maintain.business.domain.ErpWarehouse;
import com.maintain.business.mapper.ErpWarehouseMapper;
import com.maintain.business.service.IErpWarehouseService;

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

/**
 * 维修站点-站点-仓库Service业务层处理
 *
 * @author liushuai
 * @date 2025-03-25
 */
@RequiredArgsConstructor
@Service
public class ErpWarehouseServiceImpl implements IErpWarehouseService {

    private final ErpWarehouseMapper baseMapper;
    private final SysDeptMapper deptMapper;

    /**
     * 查询维修站点-站点-仓库
     */
    @Override
    public ErpWarehouseVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询维修站点-站点-仓库列表
     */
    @Override
    public TableDataInfo<ErpWarehouseVo> queryPageList(ErpWarehouseBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpWarehouse> lqw = buildQueryWrapper(bo);
        Page<ErpWarehouseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询维修站点-站点-仓库列表
     */
    @Override
    public List<ErpWarehouseVo> queryList(ErpWarehouseBo bo) {
        LambdaQueryWrapper<ErpWarehouse> lqw = Wrappers.lambdaQuery();
        if (bo.getDeptId() != null) {
            List<SysDept> deptList = deptMapper.selectChildrenDeptById(bo.getDeptId(), null);
            lqw.in(ErpWarehouse::getDeptId, deptList.stream().map(SysDept::getDeptId).collect(Collectors.toList()));
        }
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpWarehouse> buildQueryWrapper(ErpWarehouseBo bo) {
        List<SysDept> deptList = deptMapper.selectChildrenDeptById(LoginHelper.getDeptId(), null);
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpWarehouse> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getCode()), ErpWarehouse::getCode, bo.getCode());
        lqw.like(StringUtils.isNotBlank(bo.getName()), ErpWarehouse::getName, bo.getName());
        lqw.eq(bo.getIsEnable() != null, ErpWarehouse::getIsEnable, bo.getIsEnable());
        lqw.eq(bo.getDeptId() != null, ErpWarehouse::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getDeptName()), ErpWarehouse::getDeptName, bo.getDeptName());
        lqw.in(!deptList.isEmpty(), ErpWarehouse::getCreateDeptId, deptList.stream().map(SysDept::getDeptId).collect(Collectors.toList()));
        lqw.orderByDesc(ErpWarehouse::getCreateTime);
        return lqw;
    }

    /**
     * 新增维修站点-站点-仓库
     */
    @Override
    public Boolean insertByBo(ErpWarehouseBo bo) {
        ErpWarehouse add = BeanUtil.toBean(bo, ErpWarehouse.class);
        validEntityBeforeSave(add);
        add.setCreateDeptId(LoginHelper.getDeptId());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改维修站点-站点-仓库
     */
    @Override
    public Boolean updateByBo(ErpWarehouseBo bo) {
        ErpWarehouse update = BeanUtil.toBean(bo, ErpWarehouse.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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