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.ErpGroupBo;
import com.maintain.business.domain.vo.ErpGroupVo;
import com.maintain.business.domain.ErpGroup;
import com.maintain.business.mapper.ErpGroupMapper;
import com.maintain.business.service.IErpGroupService;

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 ErpGroupServiceImpl implements IErpGroupService {

    private final ErpGroupMapper baseMapper;
    private final SysDeptMapper deptMapper;

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

    /**
     * 查询维修站点-站点-班组列表
     */
    @Override
    public TableDataInfo<ErpGroupVo> queryPageList(ErpGroupBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpGroup> lqw = buildQueryWrapper(bo);
        Page<ErpGroupVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询维修站点-站点-班组列表
     */
    @Override
    public List<ErpGroupVo> queryList(ErpGroupBo bo) {
        LambdaQueryWrapper<ErpGroup> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

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

    /**
     * 新增维修站点-站点-班组
     */
    @Override
    public Boolean insertByBo(ErpGroupBo bo) {
        ErpGroup add = BeanUtil.toBean(bo, ErpGroup.class);
        SysDept sysDept = deptMapper.selectById(add.getDeptId());
        add.setDeptName(sysDept.getDeptName());
        add.setCreateDeptId(LoginHelper.getDeptId());
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改维修站点-站点-班组
     */
    @Override
    public Boolean updateByBo(ErpGroupBo bo) {
        ErpGroup update = BeanUtil.toBean(bo, ErpGroup.class);
        validEntityBeforeSave(update);
        SysDept sysDept = deptMapper.selectById(update.getDeptId());
        update.setDeptName(sysDept.getDeptName());
        return baseMapper.updateById(update) > 0;
    }

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

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