package com.bonus.material.ma.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.bonus.common.biz.domain.TreeBuild;
import com.bonus.common.biz.domain.TreeNode;
import com.bonus.common.biz.enums.HttpCodeEnum;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.material.ma.domain.vo.MaTypeVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.material.ma.mapper.PartTypeMapper;
import com.bonus.material.ma.domain.PartType;
import com.bonus.material.ma.service.IPartTypeService;

/**
 * 配件类型管理Service业务层处理
 * 
 * @author xsheng
 * @date 2024-09-27
 */
@Service
@Slf4j
public class PartTypeServiceImpl implements IPartTypeService 
{
    @Autowired
    private PartTypeMapper partTypeMapper;

    /**
     * 查询配件类型管理
     * 
     * @param id 配件类型管理主键
     * @return 配件类型管理
     */
    @Override
    public PartType selectPartTypeByPaId(Long id)
    {
        return partTypeMapper.selectPartTypeByPaId(id);
    }

    /**
     * 查询配件类型管理列表
     * 
     * @param partType 配件类型管理
     * @return 配件类型管理
     */
    @Override
    public AjaxResult selectPartTypeList(PartType partType)
    {
        List<TreeNode> groupList = new ArrayList<>();
        List<TreeNode> list = new ArrayList<>();
        try {
            list = partTypeMapper.selectPartTypeList(partType);
            if (CollectionUtils.isNotEmpty(list)) {
                // 创建树形结构（数据集合作为参数）
                TreeBuild treeBuild = new TreeBuild(list);
                // 原查询结果转换树形结构
                groupList = treeBuild.buildTree();
            }
        } catch (Exception e) {
            log.error("配件类型树-查询失败", e);
        }
        return AjaxResult.success(groupList);
    }

    /**
     * 新增配件类型管理
     * 
     * @param partType 配件类型管理
     * @return 结果
     */
    @Override
    public AjaxResult insertPartType(PartType partType)
    {
        if (partType == null || partType.getPaName() == null) {
            return AjaxResult.error(HttpCodeEnum.TO_PARAM_NULL.getCode(), HttpCodeEnum.TO_PARAM_NULL.getMsg());
        }
        //新增判重，二级和三级不同类型下面可以重复，一级不能重复
        PartType type = partTypeMapper.selectByName(partType);
        if (type != null) {
            return AjaxResult.error(HttpCodeEnum.NAME_DUPLICATE.getCode(), HttpCodeEnum.NAME_DUPLICATE.getMsg());
        }
        partType.setCreateTime(DateUtils.getNowDate());
        partType.setCreateBy(SecurityUtils.getUserId().toString());
        partType.setParentId(partType.getId() != 0 ? partType.getId() : 0L);
        partType.setLevel(partType.getLevel() != null ? String.valueOf(Integer.valueOf(partType.getLevel()) + 1) : "1");
        int result = partTypeMapper.insertPartType(partType);
        if (result > 0) {
            return AjaxResult.success(HttpCodeEnum.SUCCESS.getMsg(), result);
        }
        return AjaxResult.error(HttpCodeEnum.FAIL.getCode(), HttpCodeEnum.FAIL.getMsg());
    }

    /**
     * 修改配件类型管理
     * 
     * @param partType 配件类型管理
     * @return 结果
     */
    @Override
    public AjaxResult updatePartType(PartType partType)
    {
        if (partType == null || partType.getPaName() == null || partType.getParentId() == null || partType.getId() == null) {
            return AjaxResult.error(HttpCodeEnum.TO_PARAM_NULL.getCode(), HttpCodeEnum.TO_PARAM_NULL.getMsg());
        }
        //修改判重，二级和三级不同类型下面可以重复，一级不能重复
        PartType type = partTypeMapper.selectByName(partType);
        if (type != null) {
            if (!Objects.equals(type.getId(), partType.getId())) {
                return AjaxResult.error(HttpCodeEnum.NAME_DUPLICATE.getCode(), HttpCodeEnum.NAME_DUPLICATE.getMsg());
            }
        }
        partType.setUpdateTime(DateUtils.getNowDate());
        partType.setUpdateBy(SecurityUtils.getUserId().toString());
        int result = partTypeMapper.updatePartType(partType);
        if (result > 0) {
            return AjaxResult.success(HttpCodeEnum.SUCCESS.getMsg(), result);
        }
        return AjaxResult.error(HttpCodeEnum.FAIL.getCode(), HttpCodeEnum.FAIL.getMsg());
    }

    /**
     * 删除配件类型管理信息
     * 
     * @param id 配件类型管理主键
     * @return 结果
     */
    @Override
    public AjaxResult deletePartTypeByPaId(Long id)
    {
        //判断配件类型下是否有下级配件
        int count = partTypeMapper.selectById(id);
        if (count > 0) {
            return AjaxResult.error(HttpCodeEnum.FAIL.getCode(), "该类型下有下级配件，不允许删除");
        }
        //判断配件是否在用，在用则不能删除
        int i = partTypeMapper.selectPart(id);
        if (i > 0) {
            return AjaxResult.error(HttpCodeEnum.FAIL.getCode(), "该配件类型处于在用状态，不允许删除");
        }
        int result = partTypeMapper.deletePartTypeByPaId(id);
        if (result > 0) {
            return AjaxResult.success(HttpCodeEnum.SUCCESS.getMsg(), result);
        }
        return AjaxResult.error(HttpCodeEnum.FAIL.getCode(), HttpCodeEnum.FAIL.getMsg());
    }

    /**
     * 获取配件类型树
     * @param dto
     * @return
     */
    @Override
    public AjaxResult getPartTree(PartType dto) {
        List<TreeNode> groupList = new ArrayList<>();
        List<TreeNode> list = new ArrayList<>();
        try {
            list = partTypeMapper.getPartTree(dto);
            if (CollectionUtils.isNotEmpty(list)) {
                // 创建树形结构（数据集合作为参数）
                TreeBuild treeBuild = new TreeBuild(list);
                // 原查询结果转换树形结构
                groupList = treeBuild.buildTree();
            }
        } catch (Exception e) {
            log.error("配件所属上级树-查询失败", e);
        }
        return AjaxResult.success(groupList);
    }

    /**
     * 获取配件类型列表
     * @param partType
     * @return
     */
    @Override
    public List<PartType> getTypeList(PartType partType) {
        return partTypeMapper.getTypeList(partType);
    }

    @Override
    public List<Integer> selectParentId(PartType partType) {
        return partTypeMapper.selectParentId(partType);
    }

    @Override
    public List<PartType> getListByParentId(Long id, PartType type) {
        return partTypeMapper.getListByTypeName(id, type);
    }
}
