package com.maintain.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import com.maintain.common.core.domain.entity.SysDept;
import com.maintain.common.enums.ProjectType;
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.common.utils.TreeBuildUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.maintain.business.domain.bo.ErpProjectTypeBo;
import com.maintain.business.domain.vo.ErpProjectTypeVo;
import com.maintain.business.domain.ErpProjectType;
import com.maintain.business.mapper.ErpProjectTypeMapper;
import com.maintain.business.service.IErpProjectTypeService;

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

/**
 * 维修项目类型Service业务层处理
 *
 * @author liushuai
 * @date 2025-03-28
 */
@RequiredArgsConstructor
@Service
public class ErpProjectTypeServiceImpl implements IErpProjectTypeService {

    private final ErpProjectTypeMapper baseMapper;

    /**
     * 查询维修项目类型
     */
    @Override
    public ErpProjectTypeVo queryById(Long typeId){
        return baseMapper.selectVoById(typeId);
    }

    /**
     * 查询维修项目类型列表
     */
    @Override
    public TableDataInfo<ErpProjectTypeVo> queryPageList(ErpProjectTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpProjectType> lqw = buildQueryWrapper(bo);
        Page<ErpProjectTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询维修项目类型树结构信息
     *
     * @param bo
     */
    @Override
    public List<Tree<Long>> selectProjectTypeTreeList(ErpProjectTypeBo bo) {
        // 类型为空默认是维修
        if (bo.getType() == null) {
            bo.setType(ProjectType.MAINTAIN.getCode());
        }
        LambdaQueryWrapper<ErpProjectType> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ErpProjectType::getType, bo.getType())
            .like(StringUtils.isNotBlank(bo.getTypeName()), ErpProjectType::getTypeName, bo.getTypeName())
            .orderByAsc(ErpProjectType::getParentId);
        List<ErpProjectType> projectTypes = baseMapper.selectList(lqw);
        return buildTypeTreeSelect(projectTypes);
    }

    /**
     * 构建前端所需要下拉树结构
     */
    public List<Tree<Long>> buildTypeTreeSelect(List<ErpProjectType> types) {
        if (CollUtil.isEmpty(types)) {
            return CollUtil.newArrayList();
        }
        return TreeBuildUtils.build(types, (type, tree) -> tree.setId(type.getTypeId()).setParentId(type.getParentId()).setName(type.getTypeName()));
    }

    /**
     * 查询维修项目类型列表
     */
    @Override
    public List<ErpProjectTypeVo> queryList(ErpProjectTypeBo bo) {
        LambdaQueryWrapper<ErpProjectType> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpProjectType> buildQueryWrapper(ErpProjectTypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpProjectType> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTypeName()), ErpProjectType::getTypeName, bo.getTypeName());
        lqw.eq(bo.getParentId() != null, ErpProjectType::getParentId, bo.getParentId());
        lqw.eq(StringUtils.isNotBlank(bo.getAncestors()), ErpProjectType::getAncestors, bo.getAncestors());
        lqw.eq(bo.getOrderNum() != null, ErpProjectType::getOrderNum, bo.getOrderNum());
        lqw.eq(bo.getType() != null, ErpProjectType::getType, bo.getType());
        return lqw;
    }

    /**
     * 新增维修项目类型
     */
    @Override
    public Boolean insertByBo(ErpProjectTypeBo bo) {
        ErpProjectType add = BeanUtil.toBean(bo, ErpProjectType.class);
        validEntityBeforeSave(add);
        LambdaQueryWrapper<ErpProjectType> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ErpProjectType::getTypeName, add.getTypeName()).eq(ErpProjectType::getType, add.getType());
        if (!baseMapper.selectList(queryWrapper).isEmpty()) {
            throw new RuntimeException("类型名称已存在，请调整后再试！");
        }

        ErpProjectType projectType = baseMapper.selectById(bo.getParentId());
        add.setAncestors(projectType.getAncestors() + StringUtils.SEPARATOR + bo.getParentId());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setTypeId(add.getTypeId());
        }
        return flag;
    }

    /**
     * 修改维修项目类型
     */
    @Override
    public Boolean updateByBo(ErpProjectTypeBo bo) {
        ErpProjectType update = BeanUtil.toBean(bo, ErpProjectType.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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