package com.ozs.rpm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ozs.rpm.dto.chart.ChartClassDTO;
import com.ozs.rpm.mapper.ChartClassMapper;
import com.ozs.rpm.service.ChartClassService;
import com.ozs.rpm.util.R;
import com.ozs.rpm.util.RandomUtil;

@Service
public class ChartClassServiceImpl implements ChartClassService {

	@Autowired
	private ChartClassMapper chartClassMapper;

	@Override
	public R<List<ChartClassDTO>> listChartClassTree(String className) throws Exception {
		return new R<>(chartClassMapper.listChartClassByName(className));
	}

	@Override
	public R<ChartClassDTO> getChartClass(String classCode) throws Exception {
		return new R<>(chartClassMapper.getChartClassByCode(classCode));
	}

	@Transactional
	@Override
	public R<String> saveChartClass(ChartClassDTO ChartClassDTO) throws Exception {
		R<String> r = new R<>();
		// TODO 判断菜单名称是否重复
		if (chartClassMapper.getChartClassByName(ChartClassDTO.getClassName()) != null) {
			r.setCode("-1");
			r.setMsg("分类名称已存在");
			return r;
		}

		// TODO 判断菜单编号是否重复
		// 判断新建菜单是否为第一级菜单
		if (ChartClassDTO.getPCode() == null || ChartClassDTO.getPCode().equals("0")) {
			ChartClassDTO.setClassCode(RandomUtil.generateShortUuid());
			ChartClassDTO.setClassLevel(1);
		} else {
			ChartClassDTO m = chartClassMapper.getChartClassByCode(ChartClassDTO.getPCode());
			ChartClassDTO.setClassCode(ChartClassDTO.getPCode() + RandomUtil.generateShortUuid());
			ChartClassDTO.setClassLevel(m.getClassLevel() + 1);

		}
		// 默认新建为叶子节点/
		chartClassMapper.saveChartClass(ChartClassDTO);
		r.setData("添加成功");
		return r;
	}

	@Transactional
	@Override
	public R<String> updateChartClass(ChartClassDTO ChartClassDTO) throws Exception {
		R<String> r = new R<>();
		// 判断菜单名称是否重复
		ChartClassDTO m = chartClassMapper.getChartClassByName(ChartClassDTO.getClassName());
		if (m != null && !m.getClassCode().equals(ChartClassDTO.getClassCode())) {
			r.setCode("-1");
			r.setMsg("分类名称已存在");
			return r;
		}
		// 更新分类信息
		chartClassMapper.updateChartClass(ChartClassDTO);
		// 修改后的classCode
		String newClassCode = null;
		if (ChartClassDTO.getPCode() != null) {
			newClassCode = ChartClassDTO.getPCode() + ChartClassDTO.getClassCode().substring(ChartClassDTO.getClassCode().length() - 4, ChartClassDTO.getClassCode().length());
		}
		// 判断是否修改了PCode，修改Pcode则更新classCode和其下所有子类code
		if (newClassCode != null && !ChartClassDTO.getClassCode().equals(newClassCode)) {
			chartClassMapper.updateChartClassCode(ChartClassDTO.getClassCode(), newClassCode);
		}
		r.setData("修改成功");
		return r;
	}

	@Override
	public R<String> removeChartClass(String classCode) throws Exception {
		R<String> r = new R<>();
		// 判断是否有图表关联分类
		if (chartClassMapper.countChartByClassCode(classCode) != 0) {
			r.setCode("-1");
			r.setMsg("菜单下存在报表，不可删除");
			return r;
		}
		chartClassMapper.removeChartClassByCode(classCode);
		r.setData("删除成功");
		return r;
	}

}
