package org.springblade.system.service.impl;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.core.domain.entity.KyDataClassify;
import org.springblade.common.core.domain.entity.KyDataClassifyVo;
import org.springblade.common.utils.StringUtils;
import org.springblade.system.mapper.KyDataClassifyMapper;
import org.springblade.system.service.KyDataClassifyService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 马三炮
 * @date 2025/9/1
 */
@Service
@Slf4j
public class KyDataClassifyServiceImpl implements KyDataClassifyService {

    @Resource
    private KyDataClassifyMapper kyDataClassifyMapper;

    /**
     * 获取数据分类管理列表
     * @param kyDataClassify
     * @return
     */
    @Override
    public List<KyDataClassify> selectKyDataClassifyList(KyDataClassify kyDataClassify) {
        return kyDataClassifyMapper.selectKyDataClassifyList(kyDataClassify);
    }

    /**
     * 新增数据分类
     * @param kyDataClassify
     * @return
     */
    @Override
    public int addKyDataClassify(KyDataClassify kyDataClassify) {
        //判读数据名称是否存在
        KyDataClassify kyDataClassifyOld = kyDataClassifyMapper.selectKyDataClassifyByDataTypeName(kyDataClassify);
        if (StringUtils.isNotNull(kyDataClassifyOld)){
            return 0;
        }
        return kyDataClassifyMapper.addKyDataClassify(kyDataClassify);
    }

    /**
     * 修改数据分类
     * @param kyDataClassify
     * @return
     */
    @Override
    public int updateKyDataClassify(KyDataClassify kyDataClassify) {
        //判读数据名称是否存在
        KyDataClassify kyDataClassifyOld = kyDataClassifyMapper.selectKyDataClassifyByDataTypeName(kyDataClassify);
        if (StringUtils.isNotNull(kyDataClassifyOld) && !kyDataClassify.getId().equals(kyDataClassifyOld.getId())){
            return 0;
        }
        return kyDataClassifyMapper.updateKyDataClassify(kyDataClassify);
    }

    /**
     * 删除数据分类
     * @param kyDataClassify
     * @return
     */
    @Override
    public int delKyDataClassify(KyDataClassify kyDataClassify) {
        return kyDataClassifyMapper.delKyDataClassify(kyDataClassify);
    }

    /**
     * 获取数据分类管理下拉框
     * @param kyDataClassify
     * @return
     */
    @Override
    public List<KyDataClassifyVo> listAll(KyDataClassify kyDataClassify) {
        //获取父级
        List<KyDataClassifyVo> KyDataClassifyVoList = kyDataClassifyMapper.getParenList();
        if (KyDataClassifyVoList.size()>0){
            //获取子级
            for (KyDataClassifyVo kyDataClassifyVo:KyDataClassifyVoList) {
                kyDataClassify.setPid(kyDataClassifyVo.getCode());
                List<KyDataClassify> kyDataClassifyList = kyDataClassifyMapper.listAll(kyDataClassify);
                kyDataClassifyVo.setKyDataClassifyList(kyDataClassifyList);
            }
        }
        return KyDataClassifyVoList;
    }

	@Override
	public KyDataClassify getById(KyDataClassify dto) {
		return kyDataClassifyMapper.getById(dto);
	}
}
